xref: /OK3568_Linux_fs/kernel/fs/lockd/svcshare.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * linux/fs/lockd/svcshare.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Management of DOS shares.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/time.h>
11*4882a593Smuzhiyun #include <linux/unistd.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/sunrpc/clnt.h>
16*4882a593Smuzhiyun #include <linux/sunrpc/svc.h>
17*4882a593Smuzhiyun #include <linux/lockd/lockd.h>
18*4882a593Smuzhiyun #include <linux/lockd/share.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static inline int
nlm_cmp_owner(struct nlm_share * share,struct xdr_netobj * oh)21*4882a593Smuzhiyun nlm_cmp_owner(struct nlm_share *share, struct xdr_netobj *oh)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun 	return share->s_owner.len == oh->len
24*4882a593Smuzhiyun 	    && !memcmp(share->s_owner.data, oh->data, oh->len);
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun __be32
nlmsvc_share_file(struct nlm_host * host,struct nlm_file * file,struct nlm_args * argp)28*4882a593Smuzhiyun nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file,
29*4882a593Smuzhiyun 			struct nlm_args *argp)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	struct nlm_share	*share;
32*4882a593Smuzhiyun 	struct xdr_netobj	*oh = &argp->lock.oh;
33*4882a593Smuzhiyun 	u8			*ohdata;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	for (share = file->f_shares; share; share = share->s_next) {
36*4882a593Smuzhiyun 		if (share->s_host == host && nlm_cmp_owner(share, oh))
37*4882a593Smuzhiyun 			goto update;
38*4882a593Smuzhiyun 		if ((argp->fsm_access & share->s_mode)
39*4882a593Smuzhiyun 		 || (argp->fsm_mode   & share->s_access ))
40*4882a593Smuzhiyun 			return nlm_lck_denied;
41*4882a593Smuzhiyun 	}
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	share = kmalloc(sizeof(*share) + oh->len,
44*4882a593Smuzhiyun 						GFP_KERNEL);
45*4882a593Smuzhiyun 	if (share == NULL)
46*4882a593Smuzhiyun 		return nlm_lck_denied_nolocks;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	/* Copy owner handle */
49*4882a593Smuzhiyun 	ohdata = (u8 *) (share + 1);
50*4882a593Smuzhiyun 	memcpy(ohdata, oh->data, oh->len);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	share->s_file	    = file;
53*4882a593Smuzhiyun 	share->s_host       = host;
54*4882a593Smuzhiyun 	share->s_owner.data = ohdata;
55*4882a593Smuzhiyun 	share->s_owner.len  = oh->len;
56*4882a593Smuzhiyun 	share->s_next       = file->f_shares;
57*4882a593Smuzhiyun 	file->f_shares      = share;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun update:
60*4882a593Smuzhiyun 	share->s_access = argp->fsm_access;
61*4882a593Smuzhiyun 	share->s_mode   = argp->fsm_mode;
62*4882a593Smuzhiyun 	return nlm_granted;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun  * Delete a share.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun __be32
nlmsvc_unshare_file(struct nlm_host * host,struct nlm_file * file,struct nlm_args * argp)69*4882a593Smuzhiyun nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,
70*4882a593Smuzhiyun 			struct nlm_args *argp)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct nlm_share	*share, **shpp;
73*4882a593Smuzhiyun 	struct xdr_netobj	*oh = &argp->lock.oh;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	for (shpp = &file->f_shares; (share = *shpp) != NULL;
76*4882a593Smuzhiyun 					shpp = &share->s_next) {
77*4882a593Smuzhiyun 		if (share->s_host == host && nlm_cmp_owner(share, oh)) {
78*4882a593Smuzhiyun 			*shpp = share->s_next;
79*4882a593Smuzhiyun 			kfree(share);
80*4882a593Smuzhiyun 			return nlm_granted;
81*4882a593Smuzhiyun 		}
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/* X/Open spec says return success even if there was no
85*4882a593Smuzhiyun 	 * corresponding share. */
86*4882a593Smuzhiyun 	return nlm_granted;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun  * Traverse all shares for a given file, and delete
91*4882a593Smuzhiyun  * those owned by the given (type of) host
92*4882a593Smuzhiyun  */
nlmsvc_traverse_shares(struct nlm_host * host,struct nlm_file * file,nlm_host_match_fn_t match)93*4882a593Smuzhiyun void nlmsvc_traverse_shares(struct nlm_host *host, struct nlm_file *file,
94*4882a593Smuzhiyun 		nlm_host_match_fn_t match)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct nlm_share	*share, **shpp;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	shpp = &file->f_shares;
99*4882a593Smuzhiyun 	while ((share = *shpp) !=  NULL) {
100*4882a593Smuzhiyun 		if (match(share->s_host, host)) {
101*4882a593Smuzhiyun 			*shpp = share->s_next;
102*4882a593Smuzhiyun 			kfree(share);
103*4882a593Smuzhiyun 			continue;
104*4882a593Smuzhiyun 		}
105*4882a593Smuzhiyun 		shpp = &share->s_next;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun }
108