xref: /OK3568_Linux_fs/kernel/fs/nfsd/export.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * NFS exporting and validation.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * We maintain a list of clients, each of which has a list of
6*4882a593Smuzhiyun  * exports. To export an fs to a given client, you first have
7*4882a593Smuzhiyun  * to create the client entry with NFSCTL_ADDCLIENT, which
8*4882a593Smuzhiyun  * creates a client control block and adds it to the hash
9*4882a593Smuzhiyun  * table. Then, you call NFSCTL_EXPORT for each fs.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/namei.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/exportfs.h>
19*4882a593Smuzhiyun #include <linux/sunrpc/svc_xprt.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "nfsd.h"
22*4882a593Smuzhiyun #include "nfsfh.h"
23*4882a593Smuzhiyun #include "netns.h"
24*4882a593Smuzhiyun #include "pnfs.h"
25*4882a593Smuzhiyun #include "filecache.h"
26*4882a593Smuzhiyun #include "trace.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define NFSDDBG_FACILITY	NFSDDBG_EXPORT
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * We have two caches.
32*4882a593Smuzhiyun  * One maps client+vfsmnt+dentry to export options - the export map
33*4882a593Smuzhiyun  * The other maps client+filehandle-fragment to export options. - the expkey map
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * The export options are actually stored in the first map, and the
36*4882a593Smuzhiyun  * second map contains a reference to the entry in the first map.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define	EXPKEY_HASHBITS		8
40*4882a593Smuzhiyun #define	EXPKEY_HASHMAX		(1 << EXPKEY_HASHBITS)
41*4882a593Smuzhiyun #define	EXPKEY_HASHMASK		(EXPKEY_HASHMAX -1)
42*4882a593Smuzhiyun 
expkey_put(struct kref * ref)43*4882a593Smuzhiyun static void expkey_put(struct kref *ref)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (test_bit(CACHE_VALID, &key->h.flags) &&
48*4882a593Smuzhiyun 	    !test_bit(CACHE_NEGATIVE, &key->h.flags))
49*4882a593Smuzhiyun 		path_put(&key->ek_path);
50*4882a593Smuzhiyun 	auth_domain_put(key->ek_client);
51*4882a593Smuzhiyun 	kfree_rcu(key, ek_rcu);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
expkey_upcall(struct cache_detail * cd,struct cache_head * h)54*4882a593Smuzhiyun static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	return sunrpc_cache_pipe_upcall(cd, h);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
expkey_request(struct cache_detail * cd,struct cache_head * h,char ** bpp,int * blen)59*4882a593Smuzhiyun static void expkey_request(struct cache_detail *cd,
60*4882a593Smuzhiyun 			   struct cache_head *h,
61*4882a593Smuzhiyun 			   char **bpp, int *blen)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	/* client fsidtype \xfsid */
64*4882a593Smuzhiyun 	struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
65*4882a593Smuzhiyun 	char type[5];
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	qword_add(bpp, blen, ek->ek_client->name);
68*4882a593Smuzhiyun 	snprintf(type, 5, "%d", ek->ek_fsidtype);
69*4882a593Smuzhiyun 	qword_add(bpp, blen, type);
70*4882a593Smuzhiyun 	qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
71*4882a593Smuzhiyun 	(*bpp)[-1] = '\n';
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
75*4882a593Smuzhiyun 					    struct svc_expkey *old);
76*4882a593Smuzhiyun static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
77*4882a593Smuzhiyun 
expkey_parse(struct cache_detail * cd,char * mesg,int mlen)78*4882a593Smuzhiyun static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	/* client fsidtype fsid expiry [path] */
81*4882a593Smuzhiyun 	char *buf;
82*4882a593Smuzhiyun 	int len;
83*4882a593Smuzhiyun 	struct auth_domain *dom = NULL;
84*4882a593Smuzhiyun 	int err;
85*4882a593Smuzhiyun 	int fsidtype;
86*4882a593Smuzhiyun 	char *ep;
87*4882a593Smuzhiyun 	struct svc_expkey key;
88*4882a593Smuzhiyun 	struct svc_expkey *ek = NULL;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	if (mesg[mlen - 1] != '\n')
91*4882a593Smuzhiyun 		return -EINVAL;
92*4882a593Smuzhiyun 	mesg[mlen-1] = 0;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
95*4882a593Smuzhiyun 	err = -ENOMEM;
96*4882a593Smuzhiyun 	if (!buf)
97*4882a593Smuzhiyun 		goto out;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	err = -EINVAL;
100*4882a593Smuzhiyun 	if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
101*4882a593Smuzhiyun 		goto out;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	err = -ENOENT;
104*4882a593Smuzhiyun 	dom = auth_domain_find(buf);
105*4882a593Smuzhiyun 	if (!dom)
106*4882a593Smuzhiyun 		goto out;
107*4882a593Smuzhiyun 	dprintk("found domain %s\n", buf);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	err = -EINVAL;
110*4882a593Smuzhiyun 	if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
111*4882a593Smuzhiyun 		goto out;
112*4882a593Smuzhiyun 	fsidtype = simple_strtoul(buf, &ep, 10);
113*4882a593Smuzhiyun 	if (*ep)
114*4882a593Smuzhiyun 		goto out;
115*4882a593Smuzhiyun 	dprintk("found fsidtype %d\n", fsidtype);
116*4882a593Smuzhiyun 	if (key_len(fsidtype)==0) /* invalid type */
117*4882a593Smuzhiyun 		goto out;
118*4882a593Smuzhiyun 	if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
119*4882a593Smuzhiyun 		goto out;
120*4882a593Smuzhiyun 	dprintk("found fsid length %d\n", len);
121*4882a593Smuzhiyun 	if (len != key_len(fsidtype))
122*4882a593Smuzhiyun 		goto out;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	/* OK, we seem to have a valid key */
125*4882a593Smuzhiyun 	key.h.flags = 0;
126*4882a593Smuzhiyun 	key.h.expiry_time = get_expiry(&mesg);
127*4882a593Smuzhiyun 	if (key.h.expiry_time == 0)
128*4882a593Smuzhiyun 		goto out;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	key.ek_client = dom;
131*4882a593Smuzhiyun 	key.ek_fsidtype = fsidtype;
132*4882a593Smuzhiyun 	memcpy(key.ek_fsid, buf, len);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	ek = svc_expkey_lookup(cd, &key);
135*4882a593Smuzhiyun 	err = -ENOMEM;
136*4882a593Smuzhiyun 	if (!ek)
137*4882a593Smuzhiyun 		goto out;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* now we want a pathname, or empty meaning NEGATIVE  */
140*4882a593Smuzhiyun 	err = -EINVAL;
141*4882a593Smuzhiyun 	len = qword_get(&mesg, buf, PAGE_SIZE);
142*4882a593Smuzhiyun 	if (len < 0)
143*4882a593Smuzhiyun 		goto out;
144*4882a593Smuzhiyun 	dprintk("Path seems to be <%s>\n", buf);
145*4882a593Smuzhiyun 	err = 0;
146*4882a593Smuzhiyun 	if (len == 0) {
147*4882a593Smuzhiyun 		set_bit(CACHE_NEGATIVE, &key.h.flags);
148*4882a593Smuzhiyun 		ek = svc_expkey_update(cd, &key, ek);
149*4882a593Smuzhiyun 		if (ek)
150*4882a593Smuzhiyun 			trace_nfsd_expkey_update(ek, NULL);
151*4882a593Smuzhiyun 		else
152*4882a593Smuzhiyun 			err = -ENOMEM;
153*4882a593Smuzhiyun 	} else {
154*4882a593Smuzhiyun 		err = kern_path(buf, 0, &key.ek_path);
155*4882a593Smuzhiyun 		if (err)
156*4882a593Smuzhiyun 			goto out;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		dprintk("Found the path %s\n", buf);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		ek = svc_expkey_update(cd, &key, ek);
161*4882a593Smuzhiyun 		if (ek)
162*4882a593Smuzhiyun 			trace_nfsd_expkey_update(ek, buf);
163*4882a593Smuzhiyun 		else
164*4882a593Smuzhiyun 			err = -ENOMEM;
165*4882a593Smuzhiyun 		path_put(&key.ek_path);
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 	cache_flush();
168*4882a593Smuzhiyun  out:
169*4882a593Smuzhiyun 	if (ek)
170*4882a593Smuzhiyun 		cache_put(&ek->h, cd);
171*4882a593Smuzhiyun 	if (dom)
172*4882a593Smuzhiyun 		auth_domain_put(dom);
173*4882a593Smuzhiyun 	kfree(buf);
174*4882a593Smuzhiyun 	return err;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
expkey_show(struct seq_file * m,struct cache_detail * cd,struct cache_head * h)177*4882a593Smuzhiyun static int expkey_show(struct seq_file *m,
178*4882a593Smuzhiyun 		       struct cache_detail *cd,
179*4882a593Smuzhiyun 		       struct cache_head *h)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	struct svc_expkey *ek ;
182*4882a593Smuzhiyun 	int i;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (h ==NULL) {
185*4882a593Smuzhiyun 		seq_puts(m, "#domain fsidtype fsid [path]\n");
186*4882a593Smuzhiyun 		return 0;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	ek = container_of(h, struct svc_expkey, h);
189*4882a593Smuzhiyun 	seq_printf(m, "%s %d 0x", ek->ek_client->name,
190*4882a593Smuzhiyun 		   ek->ek_fsidtype);
191*4882a593Smuzhiyun 	for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
192*4882a593Smuzhiyun 		seq_printf(m, "%08x", ek->ek_fsid[i]);
193*4882a593Smuzhiyun 	if (test_bit(CACHE_VALID, &h->flags) &&
194*4882a593Smuzhiyun 	    !test_bit(CACHE_NEGATIVE, &h->flags)) {
195*4882a593Smuzhiyun 		seq_printf(m, " ");
196*4882a593Smuzhiyun 		seq_path(m, &ek->ek_path, "\\ \t\n");
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 	seq_printf(m, "\n");
199*4882a593Smuzhiyun 	return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
expkey_match(struct cache_head * a,struct cache_head * b)202*4882a593Smuzhiyun static inline int expkey_match (struct cache_head *a, struct cache_head *b)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
205*4882a593Smuzhiyun 	struct svc_expkey *new = container_of(b, struct svc_expkey, h);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (orig->ek_fsidtype != new->ek_fsidtype ||
208*4882a593Smuzhiyun 	    orig->ek_client != new->ek_client ||
209*4882a593Smuzhiyun 	    memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
210*4882a593Smuzhiyun 		return 0;
211*4882a593Smuzhiyun 	return 1;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
expkey_init(struct cache_head * cnew,struct cache_head * citem)214*4882a593Smuzhiyun static inline void expkey_init(struct cache_head *cnew,
215*4882a593Smuzhiyun 				   struct cache_head *citem)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
218*4882a593Smuzhiyun 	struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	kref_get(&item->ek_client->ref);
221*4882a593Smuzhiyun 	new->ek_client = item->ek_client;
222*4882a593Smuzhiyun 	new->ek_fsidtype = item->ek_fsidtype;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
expkey_update(struct cache_head * cnew,struct cache_head * citem)227*4882a593Smuzhiyun static inline void expkey_update(struct cache_head *cnew,
228*4882a593Smuzhiyun 				   struct cache_head *citem)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
231*4882a593Smuzhiyun 	struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	new->ek_path = item->ek_path;
234*4882a593Smuzhiyun 	path_get(&item->ek_path);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
expkey_alloc(void)237*4882a593Smuzhiyun static struct cache_head *expkey_alloc(void)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
240*4882a593Smuzhiyun 	if (i)
241*4882a593Smuzhiyun 		return &i->h;
242*4882a593Smuzhiyun 	else
243*4882a593Smuzhiyun 		return NULL;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
expkey_flush(void)246*4882a593Smuzhiyun static void expkey_flush(void)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	/*
249*4882a593Smuzhiyun 	 * Take the nfsd_mutex here to ensure that the file cache is not
250*4882a593Smuzhiyun 	 * destroyed while we're in the middle of flushing.
251*4882a593Smuzhiyun 	 */
252*4882a593Smuzhiyun 	mutex_lock(&nfsd_mutex);
253*4882a593Smuzhiyun 	nfsd_file_cache_purge(current->nsproxy->net_ns);
254*4882a593Smuzhiyun 	mutex_unlock(&nfsd_mutex);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun static const struct cache_detail svc_expkey_cache_template = {
258*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
259*4882a593Smuzhiyun 	.hash_size	= EXPKEY_HASHMAX,
260*4882a593Smuzhiyun 	.name		= "nfsd.fh",
261*4882a593Smuzhiyun 	.cache_put	= expkey_put,
262*4882a593Smuzhiyun 	.cache_upcall	= expkey_upcall,
263*4882a593Smuzhiyun 	.cache_request	= expkey_request,
264*4882a593Smuzhiyun 	.cache_parse	= expkey_parse,
265*4882a593Smuzhiyun 	.cache_show	= expkey_show,
266*4882a593Smuzhiyun 	.match		= expkey_match,
267*4882a593Smuzhiyun 	.init		= expkey_init,
268*4882a593Smuzhiyun 	.update       	= expkey_update,
269*4882a593Smuzhiyun 	.alloc		= expkey_alloc,
270*4882a593Smuzhiyun 	.flush		= expkey_flush,
271*4882a593Smuzhiyun };
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun static int
svc_expkey_hash(struct svc_expkey * item)274*4882a593Smuzhiyun svc_expkey_hash(struct svc_expkey *item)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	int hash = item->ek_fsidtype;
277*4882a593Smuzhiyun 	char * cp = (char*)item->ek_fsid;
278*4882a593Smuzhiyun 	int len = key_len(item->ek_fsidtype);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
281*4882a593Smuzhiyun 	hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
282*4882a593Smuzhiyun 	hash &= EXPKEY_HASHMASK;
283*4882a593Smuzhiyun 	return hash;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun static struct svc_expkey *
svc_expkey_lookup(struct cache_detail * cd,struct svc_expkey * item)287*4882a593Smuzhiyun svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	struct cache_head *ch;
290*4882a593Smuzhiyun 	int hash = svc_expkey_hash(item);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
293*4882a593Smuzhiyun 	if (ch)
294*4882a593Smuzhiyun 		return container_of(ch, struct svc_expkey, h);
295*4882a593Smuzhiyun 	else
296*4882a593Smuzhiyun 		return NULL;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun static struct svc_expkey *
svc_expkey_update(struct cache_detail * cd,struct svc_expkey * new,struct svc_expkey * old)300*4882a593Smuzhiyun svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
301*4882a593Smuzhiyun 		  struct svc_expkey *old)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun 	struct cache_head *ch;
304*4882a593Smuzhiyun 	int hash = svc_expkey_hash(new);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
307*4882a593Smuzhiyun 	if (ch)
308*4882a593Smuzhiyun 		return container_of(ch, struct svc_expkey, h);
309*4882a593Smuzhiyun 	else
310*4882a593Smuzhiyun 		return NULL;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun #define	EXPORT_HASHBITS		8
315*4882a593Smuzhiyun #define	EXPORT_HASHMAX		(1<< EXPORT_HASHBITS)
316*4882a593Smuzhiyun 
nfsd4_fslocs_free(struct nfsd4_fs_locations * fsloc)317*4882a593Smuzhiyun static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	struct nfsd4_fs_location *locations = fsloc->locations;
320*4882a593Smuzhiyun 	int i;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (!locations)
323*4882a593Smuzhiyun 		return;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	for (i = 0; i < fsloc->locations_count; i++) {
326*4882a593Smuzhiyun 		kfree(locations[i].path);
327*4882a593Smuzhiyun 		kfree(locations[i].hosts);
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	kfree(locations);
331*4882a593Smuzhiyun 	fsloc->locations = NULL;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
svc_export_put(struct kref * ref)334*4882a593Smuzhiyun static void svc_export_put(struct kref *ref)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
337*4882a593Smuzhiyun 	path_put(&exp->ex_path);
338*4882a593Smuzhiyun 	auth_domain_put(exp->ex_client);
339*4882a593Smuzhiyun 	nfsd4_fslocs_free(&exp->ex_fslocs);
340*4882a593Smuzhiyun 	kfree(exp->ex_uuid);
341*4882a593Smuzhiyun 	kfree_rcu(exp, ex_rcu);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
svc_export_upcall(struct cache_detail * cd,struct cache_head * h)344*4882a593Smuzhiyun static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	return sunrpc_cache_pipe_upcall(cd, h);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
svc_export_request(struct cache_detail * cd,struct cache_head * h,char ** bpp,int * blen)349*4882a593Smuzhiyun static void svc_export_request(struct cache_detail *cd,
350*4882a593Smuzhiyun 			       struct cache_head *h,
351*4882a593Smuzhiyun 			       char **bpp, int *blen)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	/*  client path */
354*4882a593Smuzhiyun 	struct svc_export *exp = container_of(h, struct svc_export, h);
355*4882a593Smuzhiyun 	char *pth;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	qword_add(bpp, blen, exp->ex_client->name);
358*4882a593Smuzhiyun 	pth = d_path(&exp->ex_path, *bpp, *blen);
359*4882a593Smuzhiyun 	if (IS_ERR(pth)) {
360*4882a593Smuzhiyun 		/* is this correct? */
361*4882a593Smuzhiyun 		(*bpp)[0] = '\n';
362*4882a593Smuzhiyun 		return;
363*4882a593Smuzhiyun 	}
364*4882a593Smuzhiyun 	qword_add(bpp, blen, pth);
365*4882a593Smuzhiyun 	(*bpp)[-1] = '\n';
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun static struct svc_export *svc_export_update(struct svc_export *new,
369*4882a593Smuzhiyun 					    struct svc_export *old);
370*4882a593Smuzhiyun static struct svc_export *svc_export_lookup(struct svc_export *);
371*4882a593Smuzhiyun 
check_export(struct inode * inode,int * flags,unsigned char * uuid)372*4882a593Smuzhiyun static int check_export(struct inode *inode, int *flags, unsigned char *uuid)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/*
376*4882a593Smuzhiyun 	 * We currently export only dirs, regular files, and (for v4
377*4882a593Smuzhiyun 	 * pseudoroot) symlinks.
378*4882a593Smuzhiyun 	 */
379*4882a593Smuzhiyun 	if (!S_ISDIR(inode->i_mode) &&
380*4882a593Smuzhiyun 	    !S_ISLNK(inode->i_mode) &&
381*4882a593Smuzhiyun 	    !S_ISREG(inode->i_mode))
382*4882a593Smuzhiyun 		return -ENOTDIR;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/*
385*4882a593Smuzhiyun 	 * Mountd should never pass down a writeable V4ROOT export, but,
386*4882a593Smuzhiyun 	 * just to make sure:
387*4882a593Smuzhiyun 	 */
388*4882a593Smuzhiyun 	if (*flags & NFSEXP_V4ROOT)
389*4882a593Smuzhiyun 		*flags |= NFSEXP_READONLY;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	/* There are two requirements on a filesystem to be exportable.
392*4882a593Smuzhiyun 	 * 1:  We must be able to identify the filesystem from a number.
393*4882a593Smuzhiyun 	 *       either a device number (so FS_REQUIRES_DEV needed)
394*4882a593Smuzhiyun 	 *       or an FSID number (so NFSEXP_FSID or ->uuid is needed).
395*4882a593Smuzhiyun 	 * 2:  We must be able to find an inode from a filehandle.
396*4882a593Smuzhiyun 	 *       This means that s_export_op must be set.
397*4882a593Smuzhiyun 	 */
398*4882a593Smuzhiyun 	if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
399*4882a593Smuzhiyun 	    !(*flags & NFSEXP_FSID) &&
400*4882a593Smuzhiyun 	    uuid == NULL) {
401*4882a593Smuzhiyun 		dprintk("exp_export: export of non-dev fs without fsid\n");
402*4882a593Smuzhiyun 		return -EINVAL;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	if (!inode->i_sb->s_export_op ||
406*4882a593Smuzhiyun 	    !inode->i_sb->s_export_op->fh_to_dentry) {
407*4882a593Smuzhiyun 		dprintk("exp_export: export of invalid fs type.\n");
408*4882a593Smuzhiyun 		return -EINVAL;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	return 0;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun #ifdef CONFIG_NFSD_V4
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun static int
fsloc_parse(char ** mesg,char * buf,struct nfsd4_fs_locations * fsloc)418*4882a593Smuzhiyun fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun 	int len;
421*4882a593Smuzhiyun 	int migrated, i, err;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/* more than one fsloc */
424*4882a593Smuzhiyun 	if (fsloc->locations)
425*4882a593Smuzhiyun 		return -EINVAL;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* listsize */
428*4882a593Smuzhiyun 	err = get_uint(mesg, &fsloc->locations_count);
429*4882a593Smuzhiyun 	if (err)
430*4882a593Smuzhiyun 		return err;
431*4882a593Smuzhiyun 	if (fsloc->locations_count > MAX_FS_LOCATIONS)
432*4882a593Smuzhiyun 		return -EINVAL;
433*4882a593Smuzhiyun 	if (fsloc->locations_count == 0)
434*4882a593Smuzhiyun 		return 0;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	fsloc->locations = kcalloc(fsloc->locations_count,
437*4882a593Smuzhiyun 				   sizeof(struct nfsd4_fs_location),
438*4882a593Smuzhiyun 				   GFP_KERNEL);
439*4882a593Smuzhiyun 	if (!fsloc->locations)
440*4882a593Smuzhiyun 		return -ENOMEM;
441*4882a593Smuzhiyun 	for (i=0; i < fsloc->locations_count; i++) {
442*4882a593Smuzhiyun 		/* colon separated host list */
443*4882a593Smuzhiyun 		err = -EINVAL;
444*4882a593Smuzhiyun 		len = qword_get(mesg, buf, PAGE_SIZE);
445*4882a593Smuzhiyun 		if (len <= 0)
446*4882a593Smuzhiyun 			goto out_free_all;
447*4882a593Smuzhiyun 		err = -ENOMEM;
448*4882a593Smuzhiyun 		fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
449*4882a593Smuzhiyun 		if (!fsloc->locations[i].hosts)
450*4882a593Smuzhiyun 			goto out_free_all;
451*4882a593Smuzhiyun 		err = -EINVAL;
452*4882a593Smuzhiyun 		/* slash separated path component list */
453*4882a593Smuzhiyun 		len = qword_get(mesg, buf, PAGE_SIZE);
454*4882a593Smuzhiyun 		if (len <= 0)
455*4882a593Smuzhiyun 			goto out_free_all;
456*4882a593Smuzhiyun 		err = -ENOMEM;
457*4882a593Smuzhiyun 		fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
458*4882a593Smuzhiyun 		if (!fsloc->locations[i].path)
459*4882a593Smuzhiyun 			goto out_free_all;
460*4882a593Smuzhiyun 	}
461*4882a593Smuzhiyun 	/* migrated */
462*4882a593Smuzhiyun 	err = get_int(mesg, &migrated);
463*4882a593Smuzhiyun 	if (err)
464*4882a593Smuzhiyun 		goto out_free_all;
465*4882a593Smuzhiyun 	err = -EINVAL;
466*4882a593Smuzhiyun 	if (migrated < 0 || migrated > 1)
467*4882a593Smuzhiyun 		goto out_free_all;
468*4882a593Smuzhiyun 	fsloc->migrated = migrated;
469*4882a593Smuzhiyun 	return 0;
470*4882a593Smuzhiyun out_free_all:
471*4882a593Smuzhiyun 	nfsd4_fslocs_free(fsloc);
472*4882a593Smuzhiyun 	return err;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun 
secinfo_parse(char ** mesg,char * buf,struct svc_export * exp)475*4882a593Smuzhiyun static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun 	struct exp_flavor_info *f;
478*4882a593Smuzhiyun 	u32 listsize;
479*4882a593Smuzhiyun 	int err;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/* more than one secinfo */
482*4882a593Smuzhiyun 	if (exp->ex_nflavors)
483*4882a593Smuzhiyun 		return -EINVAL;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	err = get_uint(mesg, &listsize);
486*4882a593Smuzhiyun 	if (err)
487*4882a593Smuzhiyun 		return err;
488*4882a593Smuzhiyun 	if (listsize > MAX_SECINFO_LIST)
489*4882a593Smuzhiyun 		return -EINVAL;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
492*4882a593Smuzhiyun 		err = get_uint(mesg, &f->pseudoflavor);
493*4882a593Smuzhiyun 		if (err)
494*4882a593Smuzhiyun 			return err;
495*4882a593Smuzhiyun 		/*
496*4882a593Smuzhiyun 		 * XXX: It would be nice to also check whether this
497*4882a593Smuzhiyun 		 * pseudoflavor is supported, so we can discover the
498*4882a593Smuzhiyun 		 * problem at export time instead of when a client fails
499*4882a593Smuzhiyun 		 * to authenticate.
500*4882a593Smuzhiyun 		 */
501*4882a593Smuzhiyun 		err = get_uint(mesg, &f->flags);
502*4882a593Smuzhiyun 		if (err)
503*4882a593Smuzhiyun 			return err;
504*4882a593Smuzhiyun 		/* Only some flags are allowed to differ between flavors: */
505*4882a593Smuzhiyun 		if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
506*4882a593Smuzhiyun 			return -EINVAL;
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun 	exp->ex_nflavors = listsize;
509*4882a593Smuzhiyun 	return 0;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun #else /* CONFIG_NFSD_V4 */
513*4882a593Smuzhiyun static inline int
fsloc_parse(char ** mesg,char * buf,struct nfsd4_fs_locations * fsloc)514*4882a593Smuzhiyun fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
515*4882a593Smuzhiyun static inline int
secinfo_parse(char ** mesg,char * buf,struct svc_export * exp)516*4882a593Smuzhiyun secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
517*4882a593Smuzhiyun #endif
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun static inline int
nfsd_uuid_parse(char ** mesg,char * buf,unsigned char ** puuid)520*4882a593Smuzhiyun nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	int len;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	/* more than one uuid */
525*4882a593Smuzhiyun 	if (*puuid)
526*4882a593Smuzhiyun 		return -EINVAL;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	/* expect a 16 byte uuid encoded as \xXXXX... */
529*4882a593Smuzhiyun 	len = qword_get(mesg, buf, PAGE_SIZE);
530*4882a593Smuzhiyun 	if (len != EX_UUID_LEN)
531*4882a593Smuzhiyun 		return -EINVAL;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	*puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
534*4882a593Smuzhiyun 	if (*puuid == NULL)
535*4882a593Smuzhiyun 		return -ENOMEM;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	return 0;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun 
svc_export_parse(struct cache_detail * cd,char * mesg,int mlen)540*4882a593Smuzhiyun static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun 	/* client path expiry [flags anonuid anongid fsid] */
543*4882a593Smuzhiyun 	char *buf;
544*4882a593Smuzhiyun 	int len;
545*4882a593Smuzhiyun 	int err;
546*4882a593Smuzhiyun 	struct auth_domain *dom = NULL;
547*4882a593Smuzhiyun 	struct svc_export exp = {}, *expp;
548*4882a593Smuzhiyun 	int an_int;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	if (mesg[mlen-1] != '\n')
551*4882a593Smuzhiyun 		return -EINVAL;
552*4882a593Smuzhiyun 	mesg[mlen-1] = 0;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
555*4882a593Smuzhiyun 	if (!buf)
556*4882a593Smuzhiyun 		return -ENOMEM;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	/* client */
559*4882a593Smuzhiyun 	err = -EINVAL;
560*4882a593Smuzhiyun 	len = qword_get(&mesg, buf, PAGE_SIZE);
561*4882a593Smuzhiyun 	if (len <= 0)
562*4882a593Smuzhiyun 		goto out;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	err = -ENOENT;
565*4882a593Smuzhiyun 	dom = auth_domain_find(buf);
566*4882a593Smuzhiyun 	if (!dom)
567*4882a593Smuzhiyun 		goto out;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* path */
570*4882a593Smuzhiyun 	err = -EINVAL;
571*4882a593Smuzhiyun 	if ((len = qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
572*4882a593Smuzhiyun 		goto out1;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	err = kern_path(buf, 0, &exp.ex_path);
575*4882a593Smuzhiyun 	if (err)
576*4882a593Smuzhiyun 		goto out1;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	exp.ex_client = dom;
579*4882a593Smuzhiyun 	exp.cd = cd;
580*4882a593Smuzhiyun 	exp.ex_devid_map = NULL;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	/* expiry */
583*4882a593Smuzhiyun 	err = -EINVAL;
584*4882a593Smuzhiyun 	exp.h.expiry_time = get_expiry(&mesg);
585*4882a593Smuzhiyun 	if (exp.h.expiry_time == 0)
586*4882a593Smuzhiyun 		goto out3;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	/* flags */
589*4882a593Smuzhiyun 	err = get_int(&mesg, &an_int);
590*4882a593Smuzhiyun 	if (err == -ENOENT) {
591*4882a593Smuzhiyun 		err = 0;
592*4882a593Smuzhiyun 		set_bit(CACHE_NEGATIVE, &exp.h.flags);
593*4882a593Smuzhiyun 	} else {
594*4882a593Smuzhiyun 		if (err || an_int < 0)
595*4882a593Smuzhiyun 			goto out3;
596*4882a593Smuzhiyun 		exp.ex_flags= an_int;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 		/* anon uid */
599*4882a593Smuzhiyun 		err = get_int(&mesg, &an_int);
600*4882a593Smuzhiyun 		if (err)
601*4882a593Smuzhiyun 			goto out3;
602*4882a593Smuzhiyun 		exp.ex_anon_uid= make_kuid(current_user_ns(), an_int);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 		/* anon gid */
605*4882a593Smuzhiyun 		err = get_int(&mesg, &an_int);
606*4882a593Smuzhiyun 		if (err)
607*4882a593Smuzhiyun 			goto out3;
608*4882a593Smuzhiyun 		exp.ex_anon_gid= make_kgid(current_user_ns(), an_int);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 		/* fsid */
611*4882a593Smuzhiyun 		err = get_int(&mesg, &an_int);
612*4882a593Smuzhiyun 		if (err)
613*4882a593Smuzhiyun 			goto out3;
614*4882a593Smuzhiyun 		exp.ex_fsid = an_int;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 		while ((len = qword_get(&mesg, buf, PAGE_SIZE)) > 0) {
617*4882a593Smuzhiyun 			if (strcmp(buf, "fsloc") == 0)
618*4882a593Smuzhiyun 				err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
619*4882a593Smuzhiyun 			else if (strcmp(buf, "uuid") == 0)
620*4882a593Smuzhiyun 				err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
621*4882a593Smuzhiyun 			else if (strcmp(buf, "secinfo") == 0)
622*4882a593Smuzhiyun 				err = secinfo_parse(&mesg, buf, &exp);
623*4882a593Smuzhiyun 			else
624*4882a593Smuzhiyun 				/* quietly ignore unknown words and anything
625*4882a593Smuzhiyun 				 * following. Newer user-space can try to set
626*4882a593Smuzhiyun 				 * new values, then see what the result was.
627*4882a593Smuzhiyun 				 */
628*4882a593Smuzhiyun 				break;
629*4882a593Smuzhiyun 			if (err)
630*4882a593Smuzhiyun 				goto out4;
631*4882a593Smuzhiyun 		}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 		err = check_export(d_inode(exp.ex_path.dentry), &exp.ex_flags,
634*4882a593Smuzhiyun 				   exp.ex_uuid);
635*4882a593Smuzhiyun 		if (err)
636*4882a593Smuzhiyun 			goto out4;
637*4882a593Smuzhiyun 		/*
638*4882a593Smuzhiyun 		 * No point caching this if it would immediately expire.
639*4882a593Smuzhiyun 		 * Also, this protects exportfs's dummy export from the
640*4882a593Smuzhiyun 		 * anon_uid/anon_gid checks:
641*4882a593Smuzhiyun 		 */
642*4882a593Smuzhiyun 		if (exp.h.expiry_time < seconds_since_boot())
643*4882a593Smuzhiyun 			goto out4;
644*4882a593Smuzhiyun 		/*
645*4882a593Smuzhiyun 		 * For some reason exportfs has been passing down an
646*4882a593Smuzhiyun 		 * invalid (-1) uid & gid on the "dummy" export which it
647*4882a593Smuzhiyun 		 * uses to test export support.  To make sure exportfs
648*4882a593Smuzhiyun 		 * sees errors from check_export we therefore need to
649*4882a593Smuzhiyun 		 * delay these checks till after check_export:
650*4882a593Smuzhiyun 		 */
651*4882a593Smuzhiyun 		err = -EINVAL;
652*4882a593Smuzhiyun 		if (!uid_valid(exp.ex_anon_uid))
653*4882a593Smuzhiyun 			goto out4;
654*4882a593Smuzhiyun 		if (!gid_valid(exp.ex_anon_gid))
655*4882a593Smuzhiyun 			goto out4;
656*4882a593Smuzhiyun 		err = 0;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 		nfsd4_setup_layout_type(&exp);
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	expp = svc_export_lookup(&exp);
662*4882a593Smuzhiyun 	if (!expp) {
663*4882a593Smuzhiyun 		err = -ENOMEM;
664*4882a593Smuzhiyun 		goto out4;
665*4882a593Smuzhiyun 	}
666*4882a593Smuzhiyun 	expp = svc_export_update(&exp, expp);
667*4882a593Smuzhiyun 	if (expp) {
668*4882a593Smuzhiyun 		trace_nfsd_export_update(expp);
669*4882a593Smuzhiyun 		cache_flush();
670*4882a593Smuzhiyun 		exp_put(expp);
671*4882a593Smuzhiyun 	} else
672*4882a593Smuzhiyun 		err = -ENOMEM;
673*4882a593Smuzhiyun out4:
674*4882a593Smuzhiyun 	nfsd4_fslocs_free(&exp.ex_fslocs);
675*4882a593Smuzhiyun 	kfree(exp.ex_uuid);
676*4882a593Smuzhiyun out3:
677*4882a593Smuzhiyun 	path_put(&exp.ex_path);
678*4882a593Smuzhiyun out1:
679*4882a593Smuzhiyun 	auth_domain_put(dom);
680*4882a593Smuzhiyun out:
681*4882a593Smuzhiyun 	kfree(buf);
682*4882a593Smuzhiyun 	return err;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun static void exp_flags(struct seq_file *m, int flag, int fsid,
686*4882a593Smuzhiyun 		kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
687*4882a593Smuzhiyun static void show_secinfo(struct seq_file *m, struct svc_export *exp);
688*4882a593Smuzhiyun 
svc_export_show(struct seq_file * m,struct cache_detail * cd,struct cache_head * h)689*4882a593Smuzhiyun static int svc_export_show(struct seq_file *m,
690*4882a593Smuzhiyun 			   struct cache_detail *cd,
691*4882a593Smuzhiyun 			   struct cache_head *h)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	struct svc_export *exp ;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (h ==NULL) {
696*4882a593Smuzhiyun 		seq_puts(m, "#path domain(flags)\n");
697*4882a593Smuzhiyun 		return 0;
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 	exp = container_of(h, struct svc_export, h);
700*4882a593Smuzhiyun 	seq_path(m, &exp->ex_path, " \t\n\\");
701*4882a593Smuzhiyun 	seq_putc(m, '\t');
702*4882a593Smuzhiyun 	seq_escape(m, exp->ex_client->name, " \t\n\\");
703*4882a593Smuzhiyun 	seq_putc(m, '(');
704*4882a593Smuzhiyun 	if (test_bit(CACHE_VALID, &h->flags) &&
705*4882a593Smuzhiyun 	    !test_bit(CACHE_NEGATIVE, &h->flags)) {
706*4882a593Smuzhiyun 		exp_flags(m, exp->ex_flags, exp->ex_fsid,
707*4882a593Smuzhiyun 			  exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
708*4882a593Smuzhiyun 		if (exp->ex_uuid) {
709*4882a593Smuzhiyun 			int i;
710*4882a593Smuzhiyun 			seq_puts(m, ",uuid=");
711*4882a593Smuzhiyun 			for (i = 0; i < EX_UUID_LEN; i++) {
712*4882a593Smuzhiyun 				if ((i&3) == 0 && i)
713*4882a593Smuzhiyun 					seq_putc(m, ':');
714*4882a593Smuzhiyun 				seq_printf(m, "%02x", exp->ex_uuid[i]);
715*4882a593Smuzhiyun 			}
716*4882a593Smuzhiyun 		}
717*4882a593Smuzhiyun 		show_secinfo(m, exp);
718*4882a593Smuzhiyun 	}
719*4882a593Smuzhiyun 	seq_puts(m, ")\n");
720*4882a593Smuzhiyun 	return 0;
721*4882a593Smuzhiyun }
svc_export_match(struct cache_head * a,struct cache_head * b)722*4882a593Smuzhiyun static int svc_export_match(struct cache_head *a, struct cache_head *b)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun 	struct svc_export *orig = container_of(a, struct svc_export, h);
725*4882a593Smuzhiyun 	struct svc_export *new = container_of(b, struct svc_export, h);
726*4882a593Smuzhiyun 	return orig->ex_client == new->ex_client &&
727*4882a593Smuzhiyun 		path_equal(&orig->ex_path, &new->ex_path);
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
svc_export_init(struct cache_head * cnew,struct cache_head * citem)730*4882a593Smuzhiyun static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	struct svc_export *new = container_of(cnew, struct svc_export, h);
733*4882a593Smuzhiyun 	struct svc_export *item = container_of(citem, struct svc_export, h);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	kref_get(&item->ex_client->ref);
736*4882a593Smuzhiyun 	new->ex_client = item->ex_client;
737*4882a593Smuzhiyun 	new->ex_path = item->ex_path;
738*4882a593Smuzhiyun 	path_get(&item->ex_path);
739*4882a593Smuzhiyun 	new->ex_fslocs.locations = NULL;
740*4882a593Smuzhiyun 	new->ex_fslocs.locations_count = 0;
741*4882a593Smuzhiyun 	new->ex_fslocs.migrated = 0;
742*4882a593Smuzhiyun 	new->ex_layout_types = 0;
743*4882a593Smuzhiyun 	new->ex_uuid = NULL;
744*4882a593Smuzhiyun 	new->cd = item->cd;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
export_update(struct cache_head * cnew,struct cache_head * citem)747*4882a593Smuzhiyun static void export_update(struct cache_head *cnew, struct cache_head *citem)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	struct svc_export *new = container_of(cnew, struct svc_export, h);
750*4882a593Smuzhiyun 	struct svc_export *item = container_of(citem, struct svc_export, h);
751*4882a593Smuzhiyun 	int i;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	new->ex_flags = item->ex_flags;
754*4882a593Smuzhiyun 	new->ex_anon_uid = item->ex_anon_uid;
755*4882a593Smuzhiyun 	new->ex_anon_gid = item->ex_anon_gid;
756*4882a593Smuzhiyun 	new->ex_fsid = item->ex_fsid;
757*4882a593Smuzhiyun 	new->ex_devid_map = item->ex_devid_map;
758*4882a593Smuzhiyun 	item->ex_devid_map = NULL;
759*4882a593Smuzhiyun 	new->ex_uuid = item->ex_uuid;
760*4882a593Smuzhiyun 	item->ex_uuid = NULL;
761*4882a593Smuzhiyun 	new->ex_fslocs.locations = item->ex_fslocs.locations;
762*4882a593Smuzhiyun 	item->ex_fslocs.locations = NULL;
763*4882a593Smuzhiyun 	new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
764*4882a593Smuzhiyun 	item->ex_fslocs.locations_count = 0;
765*4882a593Smuzhiyun 	new->ex_fslocs.migrated = item->ex_fslocs.migrated;
766*4882a593Smuzhiyun 	item->ex_fslocs.migrated = 0;
767*4882a593Smuzhiyun 	new->ex_layout_types = item->ex_layout_types;
768*4882a593Smuzhiyun 	new->ex_nflavors = item->ex_nflavors;
769*4882a593Smuzhiyun 	for (i = 0; i < MAX_SECINFO_LIST; i++) {
770*4882a593Smuzhiyun 		new->ex_flavors[i] = item->ex_flavors[i];
771*4882a593Smuzhiyun 	}
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun 
svc_export_alloc(void)774*4882a593Smuzhiyun static struct cache_head *svc_export_alloc(void)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
777*4882a593Smuzhiyun 	if (i)
778*4882a593Smuzhiyun 		return &i->h;
779*4882a593Smuzhiyun 	else
780*4882a593Smuzhiyun 		return NULL;
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun static const struct cache_detail svc_export_cache_template = {
784*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
785*4882a593Smuzhiyun 	.hash_size	= EXPORT_HASHMAX,
786*4882a593Smuzhiyun 	.name		= "nfsd.export",
787*4882a593Smuzhiyun 	.cache_put	= svc_export_put,
788*4882a593Smuzhiyun 	.cache_upcall	= svc_export_upcall,
789*4882a593Smuzhiyun 	.cache_request	= svc_export_request,
790*4882a593Smuzhiyun 	.cache_parse	= svc_export_parse,
791*4882a593Smuzhiyun 	.cache_show	= svc_export_show,
792*4882a593Smuzhiyun 	.match		= svc_export_match,
793*4882a593Smuzhiyun 	.init		= svc_export_init,
794*4882a593Smuzhiyun 	.update		= export_update,
795*4882a593Smuzhiyun 	.alloc		= svc_export_alloc,
796*4882a593Smuzhiyun };
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun static int
svc_export_hash(struct svc_export * exp)799*4882a593Smuzhiyun svc_export_hash(struct svc_export *exp)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun 	int hash;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
804*4882a593Smuzhiyun 	hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
805*4882a593Smuzhiyun 	hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
806*4882a593Smuzhiyun 	return hash;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun static struct svc_export *
svc_export_lookup(struct svc_export * exp)810*4882a593Smuzhiyun svc_export_lookup(struct svc_export *exp)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun 	struct cache_head *ch;
813*4882a593Smuzhiyun 	int hash = svc_export_hash(exp);
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	ch = sunrpc_cache_lookup_rcu(exp->cd, &exp->h, hash);
816*4882a593Smuzhiyun 	if (ch)
817*4882a593Smuzhiyun 		return container_of(ch, struct svc_export, h);
818*4882a593Smuzhiyun 	else
819*4882a593Smuzhiyun 		return NULL;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun static struct svc_export *
svc_export_update(struct svc_export * new,struct svc_export * old)823*4882a593Smuzhiyun svc_export_update(struct svc_export *new, struct svc_export *old)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun 	struct cache_head *ch;
826*4882a593Smuzhiyun 	int hash = svc_export_hash(old);
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
829*4882a593Smuzhiyun 	if (ch)
830*4882a593Smuzhiyun 		return container_of(ch, struct svc_export, h);
831*4882a593Smuzhiyun 	else
832*4882a593Smuzhiyun 		return NULL;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun static struct svc_expkey *
exp_find_key(struct cache_detail * cd,struct auth_domain * clp,int fsid_type,u32 * fsidv,struct cache_req * reqp)837*4882a593Smuzhiyun exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
838*4882a593Smuzhiyun 	     u32 *fsidv, struct cache_req *reqp)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun 	struct svc_expkey key, *ek;
841*4882a593Smuzhiyun 	int err;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	if (!clp)
844*4882a593Smuzhiyun 		return ERR_PTR(-ENOENT);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	key.ek_client = clp;
847*4882a593Smuzhiyun 	key.ek_fsidtype = fsid_type;
848*4882a593Smuzhiyun 	memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	ek = svc_expkey_lookup(cd, &key);
851*4882a593Smuzhiyun 	if (ek == NULL)
852*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
853*4882a593Smuzhiyun 	err = cache_check(cd, &ek->h, reqp);
854*4882a593Smuzhiyun 	if (err) {
855*4882a593Smuzhiyun 		trace_nfsd_exp_find_key(&key, err);
856*4882a593Smuzhiyun 		return ERR_PTR(err);
857*4882a593Smuzhiyun 	}
858*4882a593Smuzhiyun 	return ek;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun static struct svc_export *
exp_get_by_name(struct cache_detail * cd,struct auth_domain * clp,const struct path * path,struct cache_req * reqp)862*4882a593Smuzhiyun exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
863*4882a593Smuzhiyun 		const struct path *path, struct cache_req *reqp)
864*4882a593Smuzhiyun {
865*4882a593Smuzhiyun 	struct svc_export *exp, key;
866*4882a593Smuzhiyun 	int err;
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	if (!clp)
869*4882a593Smuzhiyun 		return ERR_PTR(-ENOENT);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	key.ex_client = clp;
872*4882a593Smuzhiyun 	key.ex_path = *path;
873*4882a593Smuzhiyun 	key.cd = cd;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	exp = svc_export_lookup(&key);
876*4882a593Smuzhiyun 	if (exp == NULL)
877*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
878*4882a593Smuzhiyun 	err = cache_check(cd, &exp->h, reqp);
879*4882a593Smuzhiyun 	if (err) {
880*4882a593Smuzhiyun 		trace_nfsd_exp_get_by_name(&key, err);
881*4882a593Smuzhiyun 		return ERR_PTR(err);
882*4882a593Smuzhiyun 	}
883*4882a593Smuzhiyun 	return exp;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun /*
887*4882a593Smuzhiyun  * Find the export entry for a given dentry.
888*4882a593Smuzhiyun  */
889*4882a593Smuzhiyun static struct svc_export *
exp_parent(struct cache_detail * cd,struct auth_domain * clp,struct path * path)890*4882a593Smuzhiyun exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun 	struct dentry *saved = dget(path->dentry);
893*4882a593Smuzhiyun 	struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
896*4882a593Smuzhiyun 		struct dentry *parent = dget_parent(path->dentry);
897*4882a593Smuzhiyun 		dput(path->dentry);
898*4882a593Smuzhiyun 		path->dentry = parent;
899*4882a593Smuzhiyun 		exp = exp_get_by_name(cd, clp, path, NULL);
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 	dput(path->dentry);
902*4882a593Smuzhiyun 	path->dentry = saved;
903*4882a593Smuzhiyun 	return exp;
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun /*
909*4882a593Smuzhiyun  * Obtain the root fh on behalf of a client.
910*4882a593Smuzhiyun  * This could be done in user space, but I feel that it adds some safety
911*4882a593Smuzhiyun  * since its harder to fool a kernel module than a user space program.
912*4882a593Smuzhiyun  */
913*4882a593Smuzhiyun int
exp_rootfh(struct net * net,struct auth_domain * clp,char * name,struct knfsd_fh * f,int maxsize)914*4882a593Smuzhiyun exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
915*4882a593Smuzhiyun 	   struct knfsd_fh *f, int maxsize)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun 	struct svc_export	*exp;
918*4882a593Smuzhiyun 	struct path		path;
919*4882a593Smuzhiyun 	struct inode		*inode;
920*4882a593Smuzhiyun 	struct svc_fh		fh;
921*4882a593Smuzhiyun 	int			err;
922*4882a593Smuzhiyun 	struct nfsd_net		*nn = net_generic(net, nfsd_net_id);
923*4882a593Smuzhiyun 	struct cache_detail	*cd = nn->svc_export_cache;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	err = -EPERM;
926*4882a593Smuzhiyun 	/* NB: we probably ought to check that it's NUL-terminated */
927*4882a593Smuzhiyun 	if (kern_path(name, 0, &path)) {
928*4882a593Smuzhiyun 		printk("nfsd: exp_rootfh path not found %s", name);
929*4882a593Smuzhiyun 		return err;
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 	inode = d_inode(path.dentry);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
934*4882a593Smuzhiyun 		 name, path.dentry, clp->name,
935*4882a593Smuzhiyun 		 inode->i_sb->s_id, inode->i_ino);
936*4882a593Smuzhiyun 	exp = exp_parent(cd, clp, &path);
937*4882a593Smuzhiyun 	if (IS_ERR(exp)) {
938*4882a593Smuzhiyun 		err = PTR_ERR(exp);
939*4882a593Smuzhiyun 		goto out;
940*4882a593Smuzhiyun 	}
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	/*
943*4882a593Smuzhiyun 	 * fh must be initialized before calling fh_compose
944*4882a593Smuzhiyun 	 */
945*4882a593Smuzhiyun 	fh_init(&fh, maxsize);
946*4882a593Smuzhiyun 	if (fh_compose(&fh, exp, path.dentry, NULL))
947*4882a593Smuzhiyun 		err = -EINVAL;
948*4882a593Smuzhiyun 	else
949*4882a593Smuzhiyun 		err = 0;
950*4882a593Smuzhiyun 	memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
951*4882a593Smuzhiyun 	fh_put(&fh);
952*4882a593Smuzhiyun 	exp_put(exp);
953*4882a593Smuzhiyun out:
954*4882a593Smuzhiyun 	path_put(&path);
955*4882a593Smuzhiyun 	return err;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun 
exp_find(struct cache_detail * cd,struct auth_domain * clp,int fsid_type,u32 * fsidv,struct cache_req * reqp)958*4882a593Smuzhiyun static struct svc_export *exp_find(struct cache_detail *cd,
959*4882a593Smuzhiyun 				   struct auth_domain *clp, int fsid_type,
960*4882a593Smuzhiyun 				   u32 *fsidv, struct cache_req *reqp)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun 	struct svc_export *exp;
963*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
964*4882a593Smuzhiyun 	struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
965*4882a593Smuzhiyun 	if (IS_ERR(ek))
966*4882a593Smuzhiyun 		return ERR_CAST(ek);
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
969*4882a593Smuzhiyun 	cache_put(&ek->h, nn->svc_expkey_cache);
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	if (IS_ERR(exp))
972*4882a593Smuzhiyun 		return ERR_CAST(exp);
973*4882a593Smuzhiyun 	return exp;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
check_nfsd_access(struct svc_export * exp,struct svc_rqst * rqstp)976*4882a593Smuzhiyun __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	struct exp_flavor_info *f;
979*4882a593Smuzhiyun 	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	/* legacy gss-only clients are always OK: */
982*4882a593Smuzhiyun 	if (exp->ex_client == rqstp->rq_gssclient)
983*4882a593Smuzhiyun 		return 0;
984*4882a593Smuzhiyun 	/* ip-address based client; check sec= export option: */
985*4882a593Smuzhiyun 	for (f = exp->ex_flavors; f < end; f++) {
986*4882a593Smuzhiyun 		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
987*4882a593Smuzhiyun 			return 0;
988*4882a593Smuzhiyun 	}
989*4882a593Smuzhiyun 	/* defaults in absence of sec= options: */
990*4882a593Smuzhiyun 	if (exp->ex_nflavors == 0) {
991*4882a593Smuzhiyun 		if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
992*4882a593Smuzhiyun 		    rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
993*4882a593Smuzhiyun 			return 0;
994*4882a593Smuzhiyun 	}
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	/* If the compound op contains a spo_must_allowed op,
997*4882a593Smuzhiyun 	 * it will be sent with integrity/protection which
998*4882a593Smuzhiyun 	 * will have to be expressly allowed on mounts that
999*4882a593Smuzhiyun 	 * don't support it
1000*4882a593Smuzhiyun 	 */
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	if (nfsd4_spo_must_allow(rqstp))
1003*4882a593Smuzhiyun 		return 0;
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	return rqstp->rq_vers < 4 ? nfserr_acces : nfserr_wrongsec;
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun /*
1009*4882a593Smuzhiyun  * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
1010*4882a593Smuzhiyun  * auth_unix client) if it's available and has secinfo information;
1011*4882a593Smuzhiyun  * otherwise, will try to use rq_gssclient.
1012*4882a593Smuzhiyun  *
1013*4882a593Smuzhiyun  * Called from functions that handle requests; functions that do work on
1014*4882a593Smuzhiyun  * behalf of mountd are passed a single client name to use, and should
1015*4882a593Smuzhiyun  * use exp_get_by_name() or exp_find().
1016*4882a593Smuzhiyun  */
1017*4882a593Smuzhiyun struct svc_export *
rqst_exp_get_by_name(struct svc_rqst * rqstp,struct path * path)1018*4882a593Smuzhiyun rqst_exp_get_by_name(struct svc_rqst *rqstp, struct path *path)
1019*4882a593Smuzhiyun {
1020*4882a593Smuzhiyun 	struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1021*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1022*4882a593Smuzhiyun 	struct cache_detail *cd = nn->svc_export_cache;
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	if (rqstp->rq_client == NULL)
1025*4882a593Smuzhiyun 		goto gss;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	/* First try the auth_unix client: */
1028*4882a593Smuzhiyun 	exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
1029*4882a593Smuzhiyun 	if (PTR_ERR(exp) == -ENOENT)
1030*4882a593Smuzhiyun 		goto gss;
1031*4882a593Smuzhiyun 	if (IS_ERR(exp))
1032*4882a593Smuzhiyun 		return exp;
1033*4882a593Smuzhiyun 	/* If it has secinfo, assume there are no gss/... clients */
1034*4882a593Smuzhiyun 	if (exp->ex_nflavors > 0)
1035*4882a593Smuzhiyun 		return exp;
1036*4882a593Smuzhiyun gss:
1037*4882a593Smuzhiyun 	/* Otherwise, try falling back on gss client */
1038*4882a593Smuzhiyun 	if (rqstp->rq_gssclient == NULL)
1039*4882a593Smuzhiyun 		return exp;
1040*4882a593Smuzhiyun 	gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
1041*4882a593Smuzhiyun 	if (PTR_ERR(gssexp) == -ENOENT)
1042*4882a593Smuzhiyun 		return exp;
1043*4882a593Smuzhiyun 	if (!IS_ERR(exp))
1044*4882a593Smuzhiyun 		exp_put(exp);
1045*4882a593Smuzhiyun 	return gssexp;
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun struct svc_export *
rqst_exp_find(struct svc_rqst * rqstp,int fsid_type,u32 * fsidv)1049*4882a593Smuzhiyun rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
1050*4882a593Smuzhiyun {
1051*4882a593Smuzhiyun 	struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1052*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1053*4882a593Smuzhiyun 	struct cache_detail *cd = nn->svc_export_cache;
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	if (rqstp->rq_client == NULL)
1056*4882a593Smuzhiyun 		goto gss;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	/* First try the auth_unix client: */
1059*4882a593Smuzhiyun 	exp = exp_find(cd, rqstp->rq_client, fsid_type,
1060*4882a593Smuzhiyun 		       fsidv, &rqstp->rq_chandle);
1061*4882a593Smuzhiyun 	if (PTR_ERR(exp) == -ENOENT)
1062*4882a593Smuzhiyun 		goto gss;
1063*4882a593Smuzhiyun 	if (IS_ERR(exp))
1064*4882a593Smuzhiyun 		return exp;
1065*4882a593Smuzhiyun 	/* If it has secinfo, assume there are no gss/... clients */
1066*4882a593Smuzhiyun 	if (exp->ex_nflavors > 0)
1067*4882a593Smuzhiyun 		return exp;
1068*4882a593Smuzhiyun gss:
1069*4882a593Smuzhiyun 	/* Otherwise, try falling back on gss client */
1070*4882a593Smuzhiyun 	if (rqstp->rq_gssclient == NULL)
1071*4882a593Smuzhiyun 		return exp;
1072*4882a593Smuzhiyun 	gssexp = exp_find(cd, rqstp->rq_gssclient, fsid_type, fsidv,
1073*4882a593Smuzhiyun 						&rqstp->rq_chandle);
1074*4882a593Smuzhiyun 	if (PTR_ERR(gssexp) == -ENOENT)
1075*4882a593Smuzhiyun 		return exp;
1076*4882a593Smuzhiyun 	if (!IS_ERR(exp))
1077*4882a593Smuzhiyun 		exp_put(exp);
1078*4882a593Smuzhiyun 	return gssexp;
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun struct svc_export *
rqst_exp_parent(struct svc_rqst * rqstp,struct path * path)1082*4882a593Smuzhiyun rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	struct dentry *saved = dget(path->dentry);
1085*4882a593Smuzhiyun 	struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
1088*4882a593Smuzhiyun 		struct dentry *parent = dget_parent(path->dentry);
1089*4882a593Smuzhiyun 		dput(path->dentry);
1090*4882a593Smuzhiyun 		path->dentry = parent;
1091*4882a593Smuzhiyun 		exp = rqst_exp_get_by_name(rqstp, path);
1092*4882a593Smuzhiyun 	}
1093*4882a593Smuzhiyun 	dput(path->dentry);
1094*4882a593Smuzhiyun 	path->dentry = saved;
1095*4882a593Smuzhiyun 	return exp;
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun 
rqst_find_fsidzero_export(struct svc_rqst * rqstp)1098*4882a593Smuzhiyun struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
1099*4882a593Smuzhiyun {
1100*4882a593Smuzhiyun 	u32 fsidv[2];
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	return rqst_exp_find(rqstp, FSID_NUM, fsidv);
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun /*
1108*4882a593Smuzhiyun  * Called when we need the filehandle for the root of the pseudofs,
1109*4882a593Smuzhiyun  * for a given NFSv4 client.   The root is defined to be the
1110*4882a593Smuzhiyun  * export point with fsid==0
1111*4882a593Smuzhiyun  */
1112*4882a593Smuzhiyun __be32
exp_pseudoroot(struct svc_rqst * rqstp,struct svc_fh * fhp)1113*4882a593Smuzhiyun exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	struct svc_export *exp;
1116*4882a593Smuzhiyun 	__be32 rv;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	exp = rqst_find_fsidzero_export(rqstp);
1119*4882a593Smuzhiyun 	if (IS_ERR(exp))
1120*4882a593Smuzhiyun 		return nfserrno(PTR_ERR(exp));
1121*4882a593Smuzhiyun 	rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
1122*4882a593Smuzhiyun 	exp_put(exp);
1123*4882a593Smuzhiyun 	return rv;
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun static struct flags {
1127*4882a593Smuzhiyun 	int flag;
1128*4882a593Smuzhiyun 	char *name[2];
1129*4882a593Smuzhiyun } expflags[] = {
1130*4882a593Smuzhiyun 	{ NFSEXP_READONLY, {"ro", "rw"}},
1131*4882a593Smuzhiyun 	{ NFSEXP_INSECURE_PORT, {"insecure", ""}},
1132*4882a593Smuzhiyun 	{ NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
1133*4882a593Smuzhiyun 	{ NFSEXP_ALLSQUASH, {"all_squash", ""}},
1134*4882a593Smuzhiyun 	{ NFSEXP_ASYNC, {"async", "sync"}},
1135*4882a593Smuzhiyun 	{ NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
1136*4882a593Smuzhiyun 	{ NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
1137*4882a593Smuzhiyun 	{ NFSEXP_NOHIDE, {"nohide", ""}},
1138*4882a593Smuzhiyun 	{ NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
1139*4882a593Smuzhiyun 	{ NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
1140*4882a593Smuzhiyun 	{ NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
1141*4882a593Smuzhiyun 	{ NFSEXP_V4ROOT, {"v4root", ""}},
1142*4882a593Smuzhiyun 	{ NFSEXP_PNFS, {"pnfs", ""}},
1143*4882a593Smuzhiyun 	{ NFSEXP_SECURITY_LABEL, {"security_label", ""}},
1144*4882a593Smuzhiyun 	{ 0, {"", ""}}
1145*4882a593Smuzhiyun };
1146*4882a593Smuzhiyun 
show_expflags(struct seq_file * m,int flags,int mask)1147*4882a593Smuzhiyun static void show_expflags(struct seq_file *m, int flags, int mask)
1148*4882a593Smuzhiyun {
1149*4882a593Smuzhiyun 	struct flags *flg;
1150*4882a593Smuzhiyun 	int state, first = 0;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	for (flg = expflags; flg->flag; flg++) {
1153*4882a593Smuzhiyun 		if (flg->flag & ~mask)
1154*4882a593Smuzhiyun 			continue;
1155*4882a593Smuzhiyun 		state = (flg->flag & flags) ? 0 : 1;
1156*4882a593Smuzhiyun 		if (*flg->name[state])
1157*4882a593Smuzhiyun 			seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
1158*4882a593Smuzhiyun 	}
1159*4882a593Smuzhiyun }
1160*4882a593Smuzhiyun 
show_secinfo_flags(struct seq_file * m,int flags)1161*4882a593Smuzhiyun static void show_secinfo_flags(struct seq_file *m, int flags)
1162*4882a593Smuzhiyun {
1163*4882a593Smuzhiyun 	seq_printf(m, ",");
1164*4882a593Smuzhiyun 	show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun 
secinfo_flags_equal(int f,int g)1167*4882a593Smuzhiyun static bool secinfo_flags_equal(int f, int g)
1168*4882a593Smuzhiyun {
1169*4882a593Smuzhiyun 	f &= NFSEXP_SECINFO_FLAGS;
1170*4882a593Smuzhiyun 	g &= NFSEXP_SECINFO_FLAGS;
1171*4882a593Smuzhiyun 	return f == g;
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun 
show_secinfo_run(struct seq_file * m,struct exp_flavor_info ** fp,struct exp_flavor_info * end)1174*4882a593Smuzhiyun static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	int flags;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	flags = (*fp)->flags;
1179*4882a593Smuzhiyun 	seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
1180*4882a593Smuzhiyun 	(*fp)++;
1181*4882a593Smuzhiyun 	while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
1182*4882a593Smuzhiyun 		seq_printf(m, ":%d", (*fp)->pseudoflavor);
1183*4882a593Smuzhiyun 		(*fp)++;
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun 	return flags;
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun 
show_secinfo(struct seq_file * m,struct svc_export * exp)1188*4882a593Smuzhiyun static void show_secinfo(struct seq_file *m, struct svc_export *exp)
1189*4882a593Smuzhiyun {
1190*4882a593Smuzhiyun 	struct exp_flavor_info *f;
1191*4882a593Smuzhiyun 	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
1192*4882a593Smuzhiyun 	int flags;
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	if (exp->ex_nflavors == 0)
1195*4882a593Smuzhiyun 		return;
1196*4882a593Smuzhiyun 	f = exp->ex_flavors;
1197*4882a593Smuzhiyun 	flags = show_secinfo_run(m, &f, end);
1198*4882a593Smuzhiyun 	if (!secinfo_flags_equal(flags, exp->ex_flags))
1199*4882a593Smuzhiyun 		show_secinfo_flags(m, flags);
1200*4882a593Smuzhiyun 	while (f != end) {
1201*4882a593Smuzhiyun 		flags = show_secinfo_run(m, &f, end);
1202*4882a593Smuzhiyun 		show_secinfo_flags(m, flags);
1203*4882a593Smuzhiyun 	}
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun 
exp_flags(struct seq_file * m,int flag,int fsid,kuid_t anonu,kgid_t anong,struct nfsd4_fs_locations * fsloc)1206*4882a593Smuzhiyun static void exp_flags(struct seq_file *m, int flag, int fsid,
1207*4882a593Smuzhiyun 		kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun 	struct user_namespace *userns = m->file->f_cred->user_ns;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	show_expflags(m, flag, NFSEXP_ALLFLAGS);
1212*4882a593Smuzhiyun 	if (flag & NFSEXP_FSID)
1213*4882a593Smuzhiyun 		seq_printf(m, ",fsid=%d", fsid);
1214*4882a593Smuzhiyun 	if (!uid_eq(anonu, make_kuid(userns, (uid_t)-2)) &&
1215*4882a593Smuzhiyun 	    !uid_eq(anonu, make_kuid(userns, 0x10000-2)))
1216*4882a593Smuzhiyun 		seq_printf(m, ",anonuid=%u", from_kuid_munged(userns, anonu));
1217*4882a593Smuzhiyun 	if (!gid_eq(anong, make_kgid(userns, (gid_t)-2)) &&
1218*4882a593Smuzhiyun 	    !gid_eq(anong, make_kgid(userns, 0x10000-2)))
1219*4882a593Smuzhiyun 		seq_printf(m, ",anongid=%u", from_kgid_munged(userns, anong));
1220*4882a593Smuzhiyun 	if (fsloc && fsloc->locations_count > 0) {
1221*4882a593Smuzhiyun 		char *loctype = (fsloc->migrated) ? "refer" : "replicas";
1222*4882a593Smuzhiyun 		int i;
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 		seq_printf(m, ",%s=", loctype);
1225*4882a593Smuzhiyun 		seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
1226*4882a593Smuzhiyun 		seq_putc(m, '@');
1227*4882a593Smuzhiyun 		seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
1228*4882a593Smuzhiyun 		for (i = 1; i < fsloc->locations_count; i++) {
1229*4882a593Smuzhiyun 			seq_putc(m, ';');
1230*4882a593Smuzhiyun 			seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
1231*4882a593Smuzhiyun 			seq_putc(m, '@');
1232*4882a593Smuzhiyun 			seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
1233*4882a593Smuzhiyun 		}
1234*4882a593Smuzhiyun 	}
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
e_show(struct seq_file * m,void * p)1237*4882a593Smuzhiyun static int e_show(struct seq_file *m, void *p)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun 	struct cache_head *cp = p;
1240*4882a593Smuzhiyun 	struct svc_export *exp = container_of(cp, struct svc_export, h);
1241*4882a593Smuzhiyun 	struct cache_detail *cd = m->private;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	if (p == SEQ_START_TOKEN) {
1244*4882a593Smuzhiyun 		seq_puts(m, "# Version 1.1\n");
1245*4882a593Smuzhiyun 		seq_puts(m, "# Path Client(Flags) # IPs\n");
1246*4882a593Smuzhiyun 		return 0;
1247*4882a593Smuzhiyun 	}
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	exp_get(exp);
1250*4882a593Smuzhiyun 	if (cache_check(cd, &exp->h, NULL))
1251*4882a593Smuzhiyun 		return 0;
1252*4882a593Smuzhiyun 	exp_put(exp);
1253*4882a593Smuzhiyun 	return svc_export_show(m, cd, cp);
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun const struct seq_operations nfs_exports_op = {
1257*4882a593Smuzhiyun 	.start	= cache_seq_start_rcu,
1258*4882a593Smuzhiyun 	.next	= cache_seq_next_rcu,
1259*4882a593Smuzhiyun 	.stop	= cache_seq_stop_rcu,
1260*4882a593Smuzhiyun 	.show	= e_show,
1261*4882a593Smuzhiyun };
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun /*
1264*4882a593Smuzhiyun  * Initialize the exports module.
1265*4882a593Smuzhiyun  */
1266*4882a593Smuzhiyun int
nfsd_export_init(struct net * net)1267*4882a593Smuzhiyun nfsd_export_init(struct net *net)
1268*4882a593Smuzhiyun {
1269*4882a593Smuzhiyun 	int rv;
1270*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
1275*4882a593Smuzhiyun 	if (IS_ERR(nn->svc_export_cache))
1276*4882a593Smuzhiyun 		return PTR_ERR(nn->svc_export_cache);
1277*4882a593Smuzhiyun 	rv = cache_register_net(nn->svc_export_cache, net);
1278*4882a593Smuzhiyun 	if (rv)
1279*4882a593Smuzhiyun 		goto destroy_export_cache;
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
1282*4882a593Smuzhiyun 	if (IS_ERR(nn->svc_expkey_cache)) {
1283*4882a593Smuzhiyun 		rv = PTR_ERR(nn->svc_expkey_cache);
1284*4882a593Smuzhiyun 		goto unregister_export_cache;
1285*4882a593Smuzhiyun 	}
1286*4882a593Smuzhiyun 	rv = cache_register_net(nn->svc_expkey_cache, net);
1287*4882a593Smuzhiyun 	if (rv)
1288*4882a593Smuzhiyun 		goto destroy_expkey_cache;
1289*4882a593Smuzhiyun 	return 0;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun destroy_expkey_cache:
1292*4882a593Smuzhiyun 	cache_destroy_net(nn->svc_expkey_cache, net);
1293*4882a593Smuzhiyun unregister_export_cache:
1294*4882a593Smuzhiyun 	cache_unregister_net(nn->svc_export_cache, net);
1295*4882a593Smuzhiyun destroy_export_cache:
1296*4882a593Smuzhiyun 	cache_destroy_net(nn->svc_export_cache, net);
1297*4882a593Smuzhiyun 	return rv;
1298*4882a593Smuzhiyun }
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun /*
1301*4882a593Smuzhiyun  * Flush exports table - called when last nfsd thread is killed
1302*4882a593Smuzhiyun  */
1303*4882a593Smuzhiyun void
nfsd_export_flush(struct net * net)1304*4882a593Smuzhiyun nfsd_export_flush(struct net *net)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	cache_purge(nn->svc_expkey_cache);
1309*4882a593Smuzhiyun 	cache_purge(nn->svc_export_cache);
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun /*
1313*4882a593Smuzhiyun  * Shutdown the exports module.
1314*4882a593Smuzhiyun  */
1315*4882a593Smuzhiyun void
nfsd_export_shutdown(struct net * net)1316*4882a593Smuzhiyun nfsd_export_shutdown(struct net *net)
1317*4882a593Smuzhiyun {
1318*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	cache_unregister_net(nn->svc_expkey_cache, net);
1323*4882a593Smuzhiyun 	cache_unregister_net(nn->svc_export_cache, net);
1324*4882a593Smuzhiyun 	cache_destroy_net(nn->svc_expkey_cache, net);
1325*4882a593Smuzhiyun 	cache_destroy_net(nn->svc_export_cache, net);
1326*4882a593Smuzhiyun 	svcauth_unix_purge(net);
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
1329*4882a593Smuzhiyun }
1330