xref: /linux/fs/nfsd/export.c (revision 14c5eb685cdefbd32e73d2723071ecbd8effbce9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NFS exporting and validation.
4  *
5  * We maintain a list of clients, each of which has a list of
6  * exports. To export an fs to a given client, you first have
7  * to create the client entry with NFSCTL_ADDCLIENT, which
8  * creates a client control block and adds it to the hash
9  * table. Then, you call NFSCTL_EXPORT for each fs.
10  *
11  *
12  * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
13  */
14 
15 #include <linux/slab.h>
16 #include <linux/namei.h>
17 #include <linux/module.h>
18 #include <linux/exportfs.h>
19 #include <linux/sunrpc/svc_xprt.h>
20 #include <net/genetlink.h>
21 #include <uapi/linux/nfsd_netlink.h>
22 
23 #include "nfsd.h"
24 #include "nfsfh.h"
25 #include "netns.h"
26 #include "pnfs.h"
27 #include "filecache.h"
28 #include "trace.h"
29 #include "netlink.h"
30 
31 #define NFSDDBG_FACILITY	NFSDDBG_EXPORT
32 
33 /*
34  * We have two caches.
35  * One maps client+vfsmnt+dentry to export options - the export map
36  * The other maps client+filehandle-fragment to export options. - the expkey map
37  *
38  * The export options are actually stored in the first map, and the
39  * second map contains a reference to the entry in the first map.
40  */
41 
42 #define	EXPKEY_HASHBITS		8
43 #define	EXPKEY_HASHMAX		(1 << EXPKEY_HASHBITS)
44 #define	EXPKEY_HASHMASK		(EXPKEY_HASHMAX -1)
45 
46 static void expkey_put(struct kref *ref)
47 {
48 	struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
49 
50 	if (test_bit(CACHE_VALID, &key->h.flags) &&
51 	    !test_bit(CACHE_NEGATIVE, &key->h.flags))
52 		path_put(&key->ek_path);
53 	auth_domain_put(key->ek_client);
54 	kfree_rcu(key, ek_rcu);
55 }
56 
57 static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
58 {
59 	return sunrpc_cache_upcall(cd, h);
60 }
61 
62 static void expkey_request(struct cache_detail *cd,
63 			   struct cache_head *h,
64 			   char **bpp, int *blen)
65 {
66 	/* client fsidtype \xfsid */
67 	struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
68 	char type[5];
69 
70 	qword_add(bpp, blen, ek->ek_client->name);
71 	snprintf(type, 5, "%d", ek->ek_fsidtype);
72 	qword_add(bpp, blen, type);
73 	qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
74 	(*bpp)[-1] = '\n';
75 }
76 
77 static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
78 					    struct svc_expkey *old);
79 static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
80 
81 static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
82 {
83 	/* client fsidtype fsid expiry [path] */
84 	char *buf;
85 	int len;
86 	struct auth_domain *dom = NULL;
87 	int err;
88 	u8 fsidtype;
89 	struct svc_expkey key;
90 	struct svc_expkey *ek = NULL;
91 
92 	if (mesg[mlen - 1] != '\n')
93 		return -EINVAL;
94 	mesg[mlen-1] = 0;
95 
96 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
97 	err = -ENOMEM;
98 	if (!buf)
99 		goto out;
100 
101 	err = -EINVAL;
102 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
103 		goto out;
104 
105 	err = -ENOENT;
106 	dom = auth_domain_find(buf);
107 	if (!dom)
108 		goto out;
109 	dprintk("found domain %s\n", buf);
110 
111 	err = -EINVAL;
112 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
113 		goto out;
114 	if (kstrtou8(buf, 10, &fsidtype))
115 		goto out;
116 	dprintk("found fsidtype %u\n", fsidtype);
117 	if (key_len(fsidtype)==0) /* invalid type */
118 		goto out;
119 	if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
120 		goto out;
121 	dprintk("found fsid length %d\n", len);
122 	if (len != key_len(fsidtype))
123 		goto out;
124 
125 	/* OK, we seem to have a valid key */
126 	key.h.flags = 0;
127 	err = get_expiry(&mesg, &key.h.expiry_time);
128 	if (err)
129 		goto out;
130 
131 	key.ek_client = dom;
132 	key.ek_fsidtype = fsidtype;
133 	memcpy(key.ek_fsid, buf, len);
134 
135 	ek = svc_expkey_lookup(cd, &key);
136 	err = -ENOMEM;
137 	if (!ek)
138 		goto out;
139 
140 	/* now we want a pathname, or empty meaning NEGATIVE  */
141 	err = -EINVAL;
142 	len = qword_get(&mesg, buf, PAGE_SIZE);
143 	if (len < 0)
144 		goto out;
145 	dprintk("Path seems to be <%s>\n", buf);
146 	err = 0;
147 	if (len == 0) {
148 		set_bit(CACHE_NEGATIVE, &key.h.flags);
149 		ek = svc_expkey_update(cd, &key, ek);
150 		if (ek)
151 			trace_nfsd_expkey_update(ek, NULL);
152 		else
153 			err = -ENOMEM;
154 	} else {
155 		err = kern_path(buf, 0, &key.ek_path);
156 		if (err)
157 			goto out;
158 
159 		dprintk("Found the path %s\n", buf);
160 
161 		ek = svc_expkey_update(cd, &key, ek);
162 		if (ek)
163 			trace_nfsd_expkey_update(ek, buf);
164 		else
165 			err = -ENOMEM;
166 		path_put(&key.ek_path);
167 	}
168 	cache_flush();
169  out:
170 	if (ek)
171 		cache_put(&ek->h, cd);
172 	if (dom)
173 		auth_domain_put(dom);
174 	kfree(buf);
175 	return err;
176 }
177 
178 static int expkey_show(struct seq_file *m,
179 		       struct cache_detail *cd,
180 		       struct cache_head *h)
181 {
182 	struct svc_expkey *ek ;
183 	int i;
184 
185 	if (h ==NULL) {
186 		seq_puts(m, "#domain fsidtype fsid [path]\n");
187 		return 0;
188 	}
189 	ek = container_of(h, struct svc_expkey, h);
190 	seq_printf(m, "%s %d 0x", ek->ek_client->name,
191 		   ek->ek_fsidtype);
192 	for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
193 		seq_printf(m, "%08x", ek->ek_fsid[i]);
194 	if (test_bit(CACHE_VALID, &h->flags) &&
195 	    !test_bit(CACHE_NEGATIVE, &h->flags)) {
196 		seq_printf(m, " ");
197 		seq_path(m, &ek->ek_path, "\\ \t\n");
198 	}
199 	seq_printf(m, "\n");
200 	return 0;
201 }
202 
203 static inline int expkey_match (struct cache_head *a, struct cache_head *b)
204 {
205 	struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
206 	struct svc_expkey *new = container_of(b, struct svc_expkey, h);
207 
208 	if (orig->ek_fsidtype != new->ek_fsidtype ||
209 	    orig->ek_client != new->ek_client ||
210 	    memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
211 		return 0;
212 	return 1;
213 }
214 
215 static inline void expkey_init(struct cache_head *cnew,
216 				   struct cache_head *citem)
217 {
218 	struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
219 	struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
220 
221 	kref_get(&item->ek_client->ref);
222 	new->ek_client = item->ek_client;
223 	new->ek_fsidtype = item->ek_fsidtype;
224 
225 	memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
226 }
227 
228 static inline void expkey_update(struct cache_head *cnew,
229 				   struct cache_head *citem)
230 {
231 	struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
232 	struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
233 
234 	new->ek_path = item->ek_path;
235 	path_get(&item->ek_path);
236 }
237 
238 static struct cache_head *expkey_alloc(void)
239 {
240 	struct svc_expkey *i = kmalloc_obj(*i);
241 	if (i)
242 		return &i->h;
243 	else
244 		return NULL;
245 }
246 
247 static void expkey_flush(void)
248 {
249 	/*
250 	 * Take the nfsd_mutex here to ensure that the file cache is not
251 	 * destroyed while we're in the middle of flushing.
252 	 */
253 	mutex_lock(&nfsd_mutex);
254 	nfsd_file_cache_purge(current->nsproxy->net_ns);
255 	mutex_unlock(&nfsd_mutex);
256 }
257 
258 static int expkey_notify(struct cache_detail *cd, struct cache_head *h)
259 {
260 	return nfsd_cache_notify(cd, h, NFSD_CACHE_TYPE_EXPKEY);
261 }
262 
263 static const struct cache_detail svc_expkey_cache_template = {
264 	.owner		= THIS_MODULE,
265 	.hash_size	= EXPKEY_HASHMAX,
266 	.name		= "nfsd.fh",
267 	.cache_put	= expkey_put,
268 	.cache_upcall	= expkey_upcall,
269 	.cache_notify	= expkey_notify,
270 	.cache_request	= expkey_request,
271 	.cache_parse	= expkey_parse,
272 	.cache_show	= expkey_show,
273 	.match		= expkey_match,
274 	.init		= expkey_init,
275 	.update       	= expkey_update,
276 	.alloc		= expkey_alloc,
277 	.flush		= expkey_flush,
278 };
279 
280 static int
281 svc_expkey_hash(struct svc_expkey *item)
282 {
283 	int hash = item->ek_fsidtype;
284 	char * cp = (char*)item->ek_fsid;
285 	int len = key_len(item->ek_fsidtype);
286 
287 	hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
288 	hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
289 	hash &= EXPKEY_HASHMASK;
290 	return hash;
291 }
292 
293 static struct svc_expkey *
294 svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
295 {
296 	struct cache_head *ch;
297 	int hash = svc_expkey_hash(item);
298 
299 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
300 	if (ch)
301 		return container_of(ch, struct svc_expkey, h);
302 	else
303 		return NULL;
304 }
305 
306 static struct svc_expkey *
307 svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
308 		  struct svc_expkey *old)
309 {
310 	struct cache_head *ch;
311 	int hash = svc_expkey_hash(new);
312 
313 	ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
314 	if (ch)
315 		return container_of(ch, struct svc_expkey, h);
316 	else
317 		return NULL;
318 }
319 
320 /**
321  * nfsd_nl_expkey_get_reqs_dumpit - dump pending expkey requests
322  * @skb: reply buffer
323  * @cb: netlink metadata and command arguments
324  *
325  * Walk the expkey cache's pending request list and create a netlink
326  * message with a nested entry for each cache_request, containing the
327  * seqno, client string, fsidtype and fsid.
328  *
329  * Uses cb->args[0] as a seqno cursor for dump continuation across
330  * multiple netlink messages.
331  *
332  * Returns the size of the reply or a negative errno.
333  */
334 int nfsd_nl_expkey_get_reqs_dumpit(struct sk_buff *skb,
335 				   struct netlink_callback *cb)
336 {
337 	struct nfsd_net *nn;
338 	struct cache_detail *cd;
339 	struct cache_head **items;
340 	u64 *seqnos;
341 	int cnt, i, emitted;
342 	void *hdr;
343 	int ret;
344 
345 	nn = net_generic(sock_net(skb->sk), nfsd_net_id);
346 
347 	mutex_lock(&nfsd_mutex);
348 
349 	cd = nn->svc_expkey_cache;
350 	if (!cd) {
351 		ret = -ENODEV;
352 		goto out_unlock;
353 	}
354 
355 	cnt = sunrpc_cache_requests_count(cd);
356 	if (!cnt) {
357 		ret = 0;
358 		goto out_unlock;
359 	}
360 
361 	items = kzalloc_objs(*items, cnt);
362 	seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL);
363 	if (!items || !seqnos) {
364 		ret = -ENOMEM;
365 		goto out_alloc;
366 	}
367 
368 	cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt,
369 					     cb->args[0]);
370 	if (!cnt) {
371 		ret = 0;
372 		goto out_alloc;
373 	}
374 
375 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
376 			  cb->nlh->nlmsg_seq, &nfsd_nl_family,
377 			  NLM_F_MULTI, NFSD_CMD_EXPKEY_GET_REQS);
378 	if (!hdr) {
379 		ret = -ENOBUFS;
380 		goto out_put;
381 	}
382 
383 	emitted = 0;
384 	for (i = 0; i < cnt; i++) {
385 		struct svc_expkey *ek;
386 		struct nlattr *nest;
387 
388 		ek = container_of(items[i], struct svc_expkey, h);
389 
390 		nest = nla_nest_start(skb, NFSD_A_EXPKEY_REQS_REQUESTS);
391 		if (!nest)
392 			break;
393 
394 		if (nla_put_u64_64bit(skb, NFSD_A_EXPKEY_SEQNO,
395 				      seqnos[i], 0) ||
396 		    nla_put_string(skb, NFSD_A_EXPKEY_CLIENT,
397 				   ek->ek_client->name) ||
398 		    nla_put_u8(skb, NFSD_A_EXPKEY_FSIDTYPE,
399 			       ek->ek_fsidtype) ||
400 		    nla_put(skb, NFSD_A_EXPKEY_FSID,
401 			    key_len(ek->ek_fsidtype), ek->ek_fsid)) {
402 			nla_nest_cancel(skb, nest);
403 			break;
404 		}
405 
406 		nla_nest_end(skb, nest);
407 		cb->args[0] = seqnos[i];
408 		emitted++;
409 	}
410 
411 	if (!emitted) {
412 		genlmsg_cancel(skb, hdr);
413 		ret = -EMSGSIZE;
414 		goto out_put;
415 	}
416 
417 	genlmsg_end(skb, hdr);
418 	ret = skb->len;
419 out_put:
420 	for (i = 0; i < cnt; i++)
421 		cache_put(items[i], cd);
422 out_alloc:
423 	kfree(seqnos);
424 	kfree(items);
425 out_unlock:
426 	mutex_unlock(&nfsd_mutex);
427 	return ret;
428 }
429 
430 /**
431  * nfsd_nl_parse_one_expkey - parse one expkey entry from netlink
432  * @cd: cache_detail for the expkey cache
433  * @attr: nested attribute containing expkey fields
434  *
435  * Parses one expkey entry from a netlink message and updates the
436  * cache. Mirrors the logic in expkey_parse().
437  *
438  * Returns 0 on success or a negative errno.
439  */
440 static int nfsd_nl_parse_one_expkey(struct cache_detail *cd,
441 				    struct nlattr *attr)
442 {
443 	struct nlattr *tb[NFSD_A_EXPKEY_PATH + 1];
444 	struct auth_domain *dom = NULL;
445 	struct svc_expkey key;
446 	struct svc_expkey *ek = NULL;
447 	struct timespec64 boot;
448 	int err;
449 	u8 fsidtype;
450 	int fsid_len;
451 
452 	err = nla_parse_nested(tb, NFSD_A_EXPKEY_PATH, attr,
453 			       nfsd_expkey_nl_policy, NULL);
454 	if (err)
455 		return err;
456 
457 	/* client (required) */
458 	if (!tb[NFSD_A_EXPKEY_CLIENT])
459 		return -EINVAL;
460 
461 	dom = auth_domain_find(nla_data(tb[NFSD_A_EXPKEY_CLIENT]));
462 	if (!dom)
463 		return -ENOENT;
464 
465 	/* fsidtype (required) */
466 	if (!tb[NFSD_A_EXPKEY_FSIDTYPE]) {
467 		err = -EINVAL;
468 		goto out_dom;
469 	}
470 	fsidtype = nla_get_u8(tb[NFSD_A_EXPKEY_FSIDTYPE]);
471 	if (key_len(fsidtype) == 0) {
472 		err = -EINVAL;
473 		goto out_dom;
474 	}
475 
476 	/* fsid (required) */
477 	if (!tb[NFSD_A_EXPKEY_FSID]) {
478 		err = -EINVAL;
479 		goto out_dom;
480 	}
481 	fsid_len = nla_len(tb[NFSD_A_EXPKEY_FSID]);
482 	if (fsid_len != key_len(fsidtype)) {
483 		err = -EINVAL;
484 		goto out_dom;
485 	}
486 
487 	/* expiry (required, wallclock seconds) */
488 	if (!tb[NFSD_A_EXPKEY_EXPIRY]) {
489 		err = -EINVAL;
490 		goto out_dom;
491 	}
492 
493 	key.h.flags = 0;
494 	getboottime64(&boot);
495 	key.h.expiry_time = nla_get_u64(tb[NFSD_A_EXPKEY_EXPIRY]) -
496 			    boot.tv_sec;
497 	key.ek_client = dom;
498 	key.ek_fsidtype = fsidtype;
499 	memcpy(key.ek_fsid, nla_data(tb[NFSD_A_EXPKEY_FSID]), fsid_len);
500 
501 	ek = svc_expkey_lookup(cd, &key);
502 	if (!ek) {
503 		err = -ENOMEM;
504 		goto out_dom;
505 	}
506 
507 	if (tb[NFSD_A_EXPKEY_NEGATIVE]) {
508 		set_bit(CACHE_NEGATIVE, &key.h.flags);
509 		ek = svc_expkey_update(cd, &key, ek);
510 		if (ek)
511 			trace_nfsd_expkey_update(ek, NULL);
512 		else
513 			err = -ENOMEM;
514 	} else if (tb[NFSD_A_EXPKEY_PATH]) {
515 		err = kern_path(nla_data(tb[NFSD_A_EXPKEY_PATH]), 0,
516 				&key.ek_path);
517 		if (err)
518 			goto out_ek;
519 		ek = svc_expkey_update(cd, &key, ek);
520 		if (ek)
521 			trace_nfsd_expkey_update(ek,
522 					nla_data(tb[NFSD_A_EXPKEY_PATH]));
523 		else
524 			err = -ENOMEM;
525 		path_put(&key.ek_path);
526 	} else {
527 		err = -EINVAL;
528 		goto out_ek;
529 	}
530 
531 	cache_flush();
532 
533 out_ek:
534 	if (ek)
535 		cache_put(&ek->h, cd);
536 out_dom:
537 	auth_domain_put(dom);
538 	return err;
539 }
540 
541 /**
542  * nfsd_nl_expkey_set_reqs_doit - respond to expkey requests
543  * @skb: reply buffer
544  * @info: netlink metadata and command arguments
545  *
546  * Parse one or more expkey cache responses from userspace and
547  * update the expkey cache accordingly.
548  *
549  * Returns 0 on success or a negative errno.
550  */
551 int nfsd_nl_expkey_set_reqs_doit(struct sk_buff *skb,
552 				 struct genl_info *info)
553 {
554 	struct nfsd_net *nn;
555 	struct cache_detail *cd;
556 	const struct nlattr *attr;
557 	int rem, ret = 0;
558 
559 	nn = net_generic(genl_info_net(info), nfsd_net_id);
560 
561 	mutex_lock(&nfsd_mutex);
562 
563 	cd = nn->svc_expkey_cache;
564 	if (!cd) {
565 		ret = -ENODEV;
566 		goto out_unlock;
567 	}
568 
569 	nlmsg_for_each_attr_type(attr, NFSD_A_EXPKEY_REQS_REQUESTS,
570 				 info->nlhdr, GENL_HDRLEN, rem) {
571 		ret = nfsd_nl_parse_one_expkey(cd, (struct nlattr *)attr);
572 		if (ret)
573 			break;
574 	}
575 
576 out_unlock:
577 	mutex_unlock(&nfsd_mutex);
578 	return ret;
579 }
580 
581 #define	EXPORT_HASHBITS		8
582 #define	EXPORT_HASHMAX		(1<< EXPORT_HASHBITS)
583 
584 static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
585 {
586 	struct nfsd4_fs_location *locations = fsloc->locations;
587 	int i;
588 
589 	if (!locations)
590 		return;
591 
592 	for (i = 0; i < fsloc->locations_count; i++) {
593 		kfree(locations[i].path);
594 		kfree(locations[i].hosts);
595 	}
596 
597 	kfree(locations);
598 	fsloc->locations = NULL;
599 }
600 
601 static int export_stats_init(struct export_stats *stats)
602 {
603 	stats->start_time = ktime_get_seconds();
604 	return percpu_counter_init_many(stats->counter, 0, GFP_KERNEL,
605 					EXP_STATS_COUNTERS_NUM);
606 }
607 
608 static void export_stats_reset(struct export_stats *stats)
609 {
610 	if (stats) {
611 		int i;
612 
613 		for (i = 0; i < EXP_STATS_COUNTERS_NUM; i++)
614 			percpu_counter_set(&stats->counter[i], 0);
615 	}
616 }
617 
618 static void export_stats_destroy(struct export_stats *stats)
619 {
620 	if (stats)
621 		percpu_counter_destroy_many(stats->counter,
622 					    EXP_STATS_COUNTERS_NUM);
623 }
624 
625 static void svc_export_release(struct rcu_head *rcu_head)
626 {
627 	struct svc_export *exp = container_of(rcu_head, struct svc_export,
628 			ex_rcu);
629 
630 	nfsd4_fslocs_free(&exp->ex_fslocs);
631 	export_stats_destroy(exp->ex_stats);
632 	kfree(exp->ex_stats);
633 	kfree(exp->ex_uuid);
634 	kfree(exp);
635 }
636 
637 static void svc_export_put(struct kref *ref)
638 {
639 	struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
640 
641 	path_put(&exp->ex_path);
642 	auth_domain_put(exp->ex_client);
643 	call_rcu(&exp->ex_rcu, svc_export_release);
644 }
645 
646 /**
647  * nfsd_nl_svc_export_get_reqs_dumpit - dump pending svc_export requests
648  * @skb: reply buffer
649  * @cb: netlink metadata and command arguments
650  *
651  * Walk the svc_export cache's pending request list and create a netlink
652  * message with a nested entry for each cache_request, containing the
653  * seqno, client string, and path.
654  *
655  * Uses cb->args[0] as a seqno cursor for dump continuation across
656  * multiple netlink messages.
657  *
658  * Returns the size of the reply or a negative errno.
659  */
660 int nfsd_nl_svc_export_get_reqs_dumpit(struct sk_buff *skb,
661 				       struct netlink_callback *cb)
662 {
663 	struct nfsd_net *nn;
664 	struct cache_detail *cd;
665 	struct cache_head **items;
666 	u64 *seqnos;
667 	int cnt, i, emitted;
668 	char *pathbuf;
669 	void *hdr;
670 	int ret;
671 
672 	nn = net_generic(sock_net(skb->sk), nfsd_net_id);
673 
674 	mutex_lock(&nfsd_mutex);
675 
676 	cd = nn->svc_export_cache;
677 	if (!cd) {
678 		ret = -ENODEV;
679 		goto out_unlock;
680 	}
681 
682 	cnt = sunrpc_cache_requests_count(cd);
683 	if (!cnt) {
684 		ret = 0;
685 		goto out_unlock;
686 	}
687 
688 	items = kzalloc_objs(*items, cnt);
689 	seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL);
690 	pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
691 	if (!items || !seqnos || !pathbuf) {
692 		ret = -ENOMEM;
693 		goto out_alloc;
694 	}
695 
696 	cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt,
697 					     cb->args[0]);
698 	if (!cnt) {
699 		ret = 0;
700 		goto out_alloc;
701 	}
702 
703 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
704 			  cb->nlh->nlmsg_seq, &nfsd_nl_family,
705 			  NLM_F_MULTI, NFSD_CMD_SVC_EXPORT_GET_REQS);
706 	if (!hdr) {
707 		ret = -ENOBUFS;
708 		goto out_put;
709 	}
710 
711 	emitted = 0;
712 	for (i = 0; i < cnt; i++) {
713 		struct svc_export *exp;
714 		struct nlattr *nest;
715 		char *pth;
716 
717 		exp = container_of(items[i], struct svc_export, h);
718 
719 		pth = d_path(&exp->ex_path, pathbuf, PATH_MAX);
720 		if (IS_ERR(pth))
721 			continue;
722 
723 		nest = nla_nest_start(skb,
724 				      NFSD_A_SVC_EXPORT_REQS_REQUESTS);
725 		if (!nest)
726 			break;
727 
728 		if (nla_put_u64_64bit(skb, NFSD_A_SVC_EXPORT_SEQNO,
729 				      seqnos[i], 0) ||
730 		    nla_put_string(skb, NFSD_A_SVC_EXPORT_CLIENT,
731 				   exp->ex_client->name) ||
732 		    nla_put_string(skb, NFSD_A_SVC_EXPORT_PATH, pth)) {
733 			nla_nest_cancel(skb, nest);
734 			break;
735 		}
736 
737 		nla_nest_end(skb, nest);
738 		cb->args[0] = seqnos[i];
739 		emitted++;
740 	}
741 
742 	if (!emitted) {
743 		genlmsg_cancel(skb, hdr);
744 		ret = -EMSGSIZE;
745 		goto out_put;
746 	}
747 
748 	genlmsg_end(skb, hdr);
749 	ret = skb->len;
750 out_put:
751 	for (i = 0; i < cnt; i++)
752 		cache_put(items[i], cd);
753 out_alloc:
754 	kfree(pathbuf);
755 	kfree(seqnos);
756 	kfree(items);
757 out_unlock:
758 	mutex_unlock(&nfsd_mutex);
759 	return ret;
760 }
761 
762 /**
763  * nfsd_nl_parse_fslocations - parse fslocations from netlink
764  * @attr: NFSD_A_SVC_EXPORT_FSLOCATIONS nested attribute
765  * @fsloc: fslocations struct to fill in
766  *
767  * Returns 0 on success or a negative errno.
768  */
769 static int nfsd_nl_parse_fslocations(struct nlattr *attr,
770 				     struct nfsd4_fs_locations *fsloc)
771 {
772 	struct nlattr *loc_attr;
773 	int rem, count = 0;
774 	int err;
775 
776 	if (fsloc->locations)
777 		return -EINVAL;
778 
779 	/* Count locations first */
780 	nla_for_each_nested_type(loc_attr, NFSD_A_FSLOCATIONS_LOCATION,
781 				 attr, rem)
782 		count++;
783 
784 	if (count > MAX_FS_LOCATIONS)
785 		return -EINVAL;
786 	if (!count)
787 		return 0;
788 
789 	fsloc->locations = kzalloc_objs(struct nfsd4_fs_location, count);
790 	if (!fsloc->locations)
791 		return -ENOMEM;
792 
793 	nla_for_each_nested_type(loc_attr, NFSD_A_FSLOCATIONS_LOCATION,
794 				 attr, rem) {
795 		struct nlattr *tb[NFSD_A_FSLOCATION_PATH + 1];
796 		struct nfsd4_fs_location *loc;
797 
798 		err = nla_parse_nested(tb, NFSD_A_FSLOCATION_PATH, loc_attr,
799 				       nfsd_fslocation_nl_policy, NULL);
800 		if (err)
801 			goto out_free;
802 
803 		if (!tb[NFSD_A_FSLOCATION_HOST] ||
804 		    !tb[NFSD_A_FSLOCATION_PATH]) {
805 			err = -EINVAL;
806 			goto out_free;
807 		}
808 
809 		loc = &fsloc->locations[fsloc->locations_count++];
810 		loc->hosts = kstrdup(nla_data(tb[NFSD_A_FSLOCATION_HOST]),
811 				     GFP_KERNEL);
812 		loc->path = kstrdup(nla_data(tb[NFSD_A_FSLOCATION_PATH]),
813 				    GFP_KERNEL);
814 		if (!loc->hosts || !loc->path) {
815 			err = -ENOMEM;
816 			goto out_free;
817 		}
818 	}
819 
820 	return 0;
821 out_free:
822 	nfsd4_fslocs_free(fsloc);
823 	return err;
824 }
825 
826 static struct svc_export *svc_export_update(struct svc_export *new,
827 					    struct svc_export *old);
828 static struct svc_export *svc_export_lookup(struct svc_export *);
829 static int check_export(const struct path *path, int *flags,
830 			unsigned char *uuid);
831 
832 /**
833  * nfsd_nl_parse_one_export - parse one svc_export entry from a netlink message
834  * @cd: cache_detail for the svc_export cache
835  * @attr: nested attribute containing svc-export fields
836  *
837  * Parses one svc-export entry from a netlink message and updates the
838  * cache. Mirrors the logic in svc_export_parse().
839  *
840  * Returns 0 on success or a negative errno.
841  */
842 static int nfsd_nl_parse_one_export(struct cache_detail *cd,
843 				    struct nlattr *attr)
844 {
845 	struct nlattr *tb[NFSD_A_SVC_EXPORT_FSID + 1];
846 	struct auth_domain *dom = NULL;
847 	struct svc_export exp = {}, *expp;
848 	struct nlattr *secinfo_attr;
849 	struct timespec64 boot;
850 	int err, rem;
851 
852 	err = nla_parse_nested(tb, NFSD_A_SVC_EXPORT_FSID, attr,
853 			       nfsd_svc_export_nl_policy, NULL);
854 	if (err)
855 		return err;
856 
857 	/* client (required) */
858 	if (!tb[NFSD_A_SVC_EXPORT_CLIENT])
859 		return -EINVAL;
860 
861 	dom = auth_domain_find(nla_data(tb[NFSD_A_SVC_EXPORT_CLIENT]));
862 	if (!dom)
863 		return -ENOENT;
864 
865 	/* path (required) */
866 	if (!tb[NFSD_A_SVC_EXPORT_PATH]) {
867 		err = -EINVAL;
868 		goto out_dom;
869 	}
870 
871 	err = kern_path(nla_data(tb[NFSD_A_SVC_EXPORT_PATH]), 0,
872 			&exp.ex_path);
873 	if (err)
874 		goto out_dom;
875 
876 	exp.ex_client = dom;
877 	exp.cd = cd;
878 	exp.ex_devid_map = NULL;
879 	exp.ex_xprtsec_modes = NFSEXP_XPRTSEC_ALL;
880 
881 	/* expiry (required, wallclock seconds) */
882 	if (!tb[NFSD_A_SVC_EXPORT_EXPIRY]) {
883 		err = -EINVAL;
884 		goto out_path;
885 	}
886 	getboottime64(&boot);
887 	exp.h.expiry_time = nla_get_u64(tb[NFSD_A_SVC_EXPORT_EXPIRY]) -
888 			    boot.tv_sec;
889 
890 	if (tb[NFSD_A_SVC_EXPORT_NEGATIVE]) {
891 		set_bit(CACHE_NEGATIVE, &exp.h.flags);
892 	} else {
893 		/* flags */
894 		if (tb[NFSD_A_SVC_EXPORT_FLAGS])
895 			exp.ex_flags = nla_get_u32(tb[NFSD_A_SVC_EXPORT_FLAGS]);
896 
897 		/* anon uid */
898 		if (tb[NFSD_A_SVC_EXPORT_ANON_UID]) {
899 			u32 uid = nla_get_u32(tb[NFSD_A_SVC_EXPORT_ANON_UID]);
900 
901 			exp.ex_anon_uid = make_kuid(current_user_ns(), uid);
902 		}
903 
904 		/* anon gid */
905 		if (tb[NFSD_A_SVC_EXPORT_ANON_GID]) {
906 			u32 gid = nla_get_u32(tb[NFSD_A_SVC_EXPORT_ANON_GID]);
907 
908 			exp.ex_anon_gid = make_kgid(current_user_ns(), gid);
909 		}
910 
911 		/* fsid */
912 		if (tb[NFSD_A_SVC_EXPORT_FSID])
913 			exp.ex_fsid = nla_get_s32(tb[NFSD_A_SVC_EXPORT_FSID]);
914 
915 		/* fslocations */
916 		if (tb[NFSD_A_SVC_EXPORT_FSLOCATIONS]) {
917 			struct nlattr *fsl = tb[NFSD_A_SVC_EXPORT_FSLOCATIONS];
918 
919 			err = nfsd_nl_parse_fslocations(fsl,
920 							&exp.ex_fslocs);
921 			if (err)
922 				goto out_path;
923 		}
924 
925 		/* uuid */
926 		if (tb[NFSD_A_SVC_EXPORT_UUID]) {
927 			if (nla_len(tb[NFSD_A_SVC_EXPORT_UUID]) !=
928 			    EX_UUID_LEN) {
929 				err = -EINVAL;
930 				goto out_fslocs;
931 			}
932 			exp.ex_uuid = kmemdup(nla_data(tb[NFSD_A_SVC_EXPORT_UUID]),
933 					      EX_UUID_LEN, GFP_KERNEL);
934 			if (!exp.ex_uuid) {
935 				err = -ENOMEM;
936 				goto out_fslocs;
937 			}
938 		}
939 
940 		/* secinfo (multi-attr) */
941 		nla_for_each_nested_type(secinfo_attr,
942 					 NFSD_A_SVC_EXPORT_SECINFO,
943 					 attr, rem) {
944 			struct nlattr *ftb[NFSD_A_AUTH_FLAVOR_FLAGS + 1];
945 			struct exp_flavor_info *f;
946 
947 			if (exp.ex_nflavors >= MAX_SECINFO_LIST) {
948 				err = -EINVAL;
949 				goto out_uuid;
950 			}
951 
952 			err = nla_parse_nested(ftb,
953 					       NFSD_A_AUTH_FLAVOR_FLAGS,
954 					       secinfo_attr,
955 					       nfsd_auth_flavor_nl_policy,
956 					       NULL);
957 			if (err)
958 				goto out_uuid;
959 
960 			f = &exp.ex_flavors[exp.ex_nflavors++];
961 
962 			if (ftb[NFSD_A_AUTH_FLAVOR_PSEUDOFLAVOR])
963 				f->pseudoflavor = nla_get_u32(ftb[NFSD_A_AUTH_FLAVOR_PSEUDOFLAVOR]);
964 
965 			if (ftb[NFSD_A_AUTH_FLAVOR_FLAGS])
966 				f->flags = nla_get_u32(ftb[NFSD_A_AUTH_FLAVOR_FLAGS]);
967 
968 			/* Only some flags are allowed to differ between flavors: */
969 			if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp.ex_flags)) {
970 				err = -EINVAL;
971 				goto out_uuid;
972 			}
973 		}
974 
975 		/* xprtsec (multi-attr u32) */
976 		if (tb[NFSD_A_SVC_EXPORT_XPRTSEC]) {
977 			struct nlattr *xp_attr;
978 
979 			exp.ex_xprtsec_modes = 0;
980 			nla_for_each_nested_type(xp_attr,
981 						 NFSD_A_SVC_EXPORT_XPRTSEC,
982 						 attr, rem) {
983 				u32 mode = nla_get_u32(xp_attr);
984 
985 				if (mode > NFSEXP_XPRTSEC_MTLS) {
986 					err = -EINVAL;
987 					goto out_uuid;
988 				}
989 				exp.ex_xprtsec_modes |= mode;
990 			}
991 		}
992 
993 		err = check_export(&exp.ex_path, &exp.ex_flags,
994 				   exp.ex_uuid);
995 		if (err)
996 			goto out_uuid;
997 
998 		if (exp.h.expiry_time < seconds_since_boot())
999 			goto out_uuid;
1000 
1001 		err = -EINVAL;
1002 		if (!uid_valid(exp.ex_anon_uid))
1003 			goto out_uuid;
1004 		if (!gid_valid(exp.ex_anon_gid))
1005 			goto out_uuid;
1006 		err = 0;
1007 
1008 		nfsd4_setup_layout_type(&exp);
1009 	}
1010 
1011 	expp = svc_export_lookup(&exp);
1012 	if (!expp) {
1013 		err = -ENOMEM;
1014 		goto out_uuid;
1015 	}
1016 	expp = svc_export_update(&exp, expp);
1017 	if (expp) {
1018 		trace_nfsd_export_update(expp);
1019 		cache_flush();
1020 		exp_put(expp);
1021 	} else {
1022 		err = -ENOMEM;
1023 	}
1024 
1025 out_uuid:
1026 	kfree(exp.ex_uuid);
1027 out_fslocs:
1028 	nfsd4_fslocs_free(&exp.ex_fslocs);
1029 out_path:
1030 	path_put(&exp.ex_path);
1031 out_dom:
1032 	auth_domain_put(dom);
1033 	return err;
1034 }
1035 
1036 /**
1037  * nfsd_nl_svc_export_set_reqs_doit - respond to svc_export requests
1038  * @skb: reply buffer
1039  * @info: netlink metadata and command arguments
1040  *
1041  * Parse one or more svc_export cache responses from userspace and
1042  * update the export cache accordingly.
1043  *
1044  * Returns 0 on success or a negative errno.
1045  */
1046 int nfsd_nl_svc_export_set_reqs_doit(struct sk_buff *skb,
1047 				     struct genl_info *info)
1048 {
1049 	struct nfsd_net *nn;
1050 	struct cache_detail *cd;
1051 	const struct nlattr *attr;
1052 	int rem, ret = 0;
1053 
1054 	nn = net_generic(genl_info_net(info), nfsd_net_id);
1055 
1056 	mutex_lock(&nfsd_mutex);
1057 
1058 	cd = nn->svc_export_cache;
1059 	if (!cd) {
1060 		ret = -ENODEV;
1061 		goto out_unlock;
1062 	}
1063 
1064 	nlmsg_for_each_attr_type(attr, NFSD_A_SVC_EXPORT_REQS_REQUESTS,
1065 				 info->nlhdr, GENL_HDRLEN, rem) {
1066 		ret = nfsd_nl_parse_one_export(cd, (struct nlattr *)attr);
1067 		if (ret)
1068 			break;
1069 	}
1070 
1071 out_unlock:
1072 	mutex_unlock(&nfsd_mutex);
1073 	return ret;
1074 }
1075 
1076 static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
1077 {
1078 	return sunrpc_cache_upcall(cd, h);
1079 }
1080 
1081 static int svc_export_notify(struct cache_detail *cd, struct cache_head *h)
1082 {
1083 	return nfsd_cache_notify(cd, h, NFSD_CACHE_TYPE_SVC_EXPORT);
1084 }
1085 
1086 static void svc_export_request(struct cache_detail *cd,
1087 			       struct cache_head *h,
1088 			       char **bpp, int *blen)
1089 {
1090 	/*  client path */
1091 	struct svc_export *exp = container_of(h, struct svc_export, h);
1092 	char *pth;
1093 
1094 	qword_add(bpp, blen, exp->ex_client->name);
1095 	pth = d_path(&exp->ex_path, *bpp, *blen);
1096 	if (IS_ERR(pth)) {
1097 		/* is this correct? */
1098 		(*bpp)[0] = '\n';
1099 		return;
1100 	}
1101 	qword_add(bpp, blen, pth);
1102 	(*bpp)[-1] = '\n';
1103 }
1104 
1105 static int check_export(const struct path *path, int *flags, unsigned char *uuid)
1106 {
1107 	struct inode *inode = d_inode(path->dentry);
1108 
1109 	/*
1110 	 * We currently export only dirs, regular files, and (for v4
1111 	 * pseudoroot) symlinks.
1112 	 */
1113 	if (!S_ISDIR(inode->i_mode) &&
1114 	    !S_ISLNK(inode->i_mode) &&
1115 	    !S_ISREG(inode->i_mode))
1116 		return -ENOTDIR;
1117 
1118 	/*
1119 	 * Mountd should never pass down a writeable V4ROOT export, but,
1120 	 * just to make sure:
1121 	 */
1122 	if (*flags & NFSEXP_V4ROOT)
1123 		*flags |= NFSEXP_READONLY;
1124 
1125 	/* There are two requirements on a filesystem to be exportable.
1126 	 * 1:  We must be able to identify the filesystem from a number.
1127 	 *       either a device number (so FS_REQUIRES_DEV needed)
1128 	 *       or an FSID number (so NFSEXP_FSID or ->uuid is needed).
1129 	 * 2:  We must be able to find an inode from a filehandle.
1130 	 *       This means that s_export_op must be set and comply with
1131 	 *       the requirements for remote filesystem export.
1132 	 * 3: We must not currently be on an idmapped mount.
1133 	 */
1134 	if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
1135 	    !(*flags & NFSEXP_FSID) &&
1136 	    uuid == NULL) {
1137 		dprintk("exp_export: export of non-dev fs without fsid\n");
1138 		return -EINVAL;
1139 	}
1140 
1141 	if (!exportfs_may_export(inode->i_sb->s_export_op)) {
1142 		dprintk("exp_export: export of invalid fs type (%s).\n",
1143 			inode->i_sb->s_type->name);
1144 		return -EINVAL;
1145 	}
1146 
1147 	if (is_idmapped_mnt(path->mnt)) {
1148 		dprintk("exp_export: export of idmapped mounts not yet supported.\n");
1149 		return -EINVAL;
1150 	}
1151 
1152 	if (inode->i_sb->s_export_op->flags & EXPORT_OP_NOSUBTREECHK &&
1153 	    !(*flags & NFSEXP_NOSUBTREECHECK)) {
1154 		dprintk("%s: %s does not support subtree checking!\n",
1155 			__func__, inode->i_sb->s_type->name);
1156 		return -EINVAL;
1157 	}
1158 	return 0;
1159 }
1160 
1161 #ifdef CONFIG_NFSD_V4
1162 
1163 static int
1164 fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
1165 {
1166 	int len;
1167 	int migrated, i, err;
1168 
1169 	/* more than one fsloc */
1170 	if (fsloc->locations)
1171 		return -EINVAL;
1172 
1173 	/* listsize */
1174 	err = get_uint(mesg, &fsloc->locations_count);
1175 	if (err)
1176 		return err;
1177 	if (fsloc->locations_count > MAX_FS_LOCATIONS)
1178 		return -EINVAL;
1179 	if (fsloc->locations_count == 0)
1180 		return 0;
1181 
1182 	fsloc->locations = kzalloc_objs(struct nfsd4_fs_location,
1183 					fsloc->locations_count);
1184 	if (!fsloc->locations)
1185 		return -ENOMEM;
1186 	for (i=0; i < fsloc->locations_count; i++) {
1187 		/* colon separated host list */
1188 		err = -EINVAL;
1189 		len = qword_get(mesg, buf, PAGE_SIZE);
1190 		if (len <= 0)
1191 			goto out_free_all;
1192 		err = -ENOMEM;
1193 		fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
1194 		if (!fsloc->locations[i].hosts)
1195 			goto out_free_all;
1196 		err = -EINVAL;
1197 		/* slash separated path component list */
1198 		len = qword_get(mesg, buf, PAGE_SIZE);
1199 		if (len <= 0)
1200 			goto out_free_all;
1201 		err = -ENOMEM;
1202 		fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
1203 		if (!fsloc->locations[i].path)
1204 			goto out_free_all;
1205 	}
1206 	/* migrated */
1207 	err = get_int(mesg, &migrated);
1208 	if (err)
1209 		goto out_free_all;
1210 	err = -EINVAL;
1211 	if (migrated < 0 || migrated > 1)
1212 		goto out_free_all;
1213 	fsloc->migrated = migrated;
1214 	return 0;
1215 out_free_all:
1216 	nfsd4_fslocs_free(fsloc);
1217 	return err;
1218 }
1219 
1220 static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
1221 {
1222 	struct exp_flavor_info *f;
1223 	u32 listsize;
1224 	int err;
1225 
1226 	/* more than one secinfo */
1227 	if (exp->ex_nflavors)
1228 		return -EINVAL;
1229 
1230 	err = get_uint(mesg, &listsize);
1231 	if (err)
1232 		return err;
1233 	if (listsize > MAX_SECINFO_LIST)
1234 		return -EINVAL;
1235 
1236 	for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
1237 		err = get_uint(mesg, &f->pseudoflavor);
1238 		if (err)
1239 			return err;
1240 		/*
1241 		 * XXX: It would be nice to also check whether this
1242 		 * pseudoflavor is supported, so we can discover the
1243 		 * problem at export time instead of when a client fails
1244 		 * to authenticate.
1245 		 */
1246 		err = get_uint(mesg, &f->flags);
1247 		if (err)
1248 			return err;
1249 		/* Only some flags are allowed to differ between flavors: */
1250 		if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
1251 			return -EINVAL;
1252 	}
1253 	exp->ex_nflavors = listsize;
1254 	return 0;
1255 }
1256 
1257 #else /* CONFIG_NFSD_V4 */
1258 static inline int
1259 fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
1260 static inline int
1261 secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
1262 #endif
1263 
1264 static int xprtsec_parse(char **mesg, char *buf, struct svc_export *exp)
1265 {
1266 	unsigned int i, mode, listsize;
1267 	int err;
1268 
1269 	err = get_uint(mesg, &listsize);
1270 	if (err)
1271 		return err;
1272 	if (listsize > NFSEXP_XPRTSEC_NUM)
1273 		return -EINVAL;
1274 
1275 	exp->ex_xprtsec_modes = 0;
1276 	for (i = 0; i < listsize; i++) {
1277 		err = get_uint(mesg, &mode);
1278 		if (err)
1279 			return err;
1280 		if (mode > NFSEXP_XPRTSEC_MTLS)
1281 			return -EINVAL;
1282 		exp->ex_xprtsec_modes |= mode;
1283 	}
1284 	return 0;
1285 }
1286 
1287 static inline int
1288 nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
1289 {
1290 	int len;
1291 
1292 	/* more than one uuid */
1293 	if (*puuid)
1294 		return -EINVAL;
1295 
1296 	/* expect a 16 byte uuid encoded as \xXXXX... */
1297 	len = qword_get(mesg, buf, PAGE_SIZE);
1298 	if (len != EX_UUID_LEN)
1299 		return -EINVAL;
1300 
1301 	*puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
1302 	if (*puuid == NULL)
1303 		return -ENOMEM;
1304 
1305 	return 0;
1306 }
1307 
1308 static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
1309 {
1310 	/* client path expiry [flags anonuid anongid fsid] */
1311 	char *buf;
1312 	int err;
1313 	struct auth_domain *dom = NULL;
1314 	struct svc_export exp = {}, *expp;
1315 	int an_int;
1316 
1317 	if (mesg[mlen-1] != '\n')
1318 		return -EINVAL;
1319 	mesg[mlen-1] = 0;
1320 
1321 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1322 	if (!buf)
1323 		return -ENOMEM;
1324 
1325 	/* client */
1326 	err = -EINVAL;
1327 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
1328 		goto out;
1329 
1330 	err = -ENOENT;
1331 	dom = auth_domain_find(buf);
1332 	if (!dom)
1333 		goto out;
1334 
1335 	/* path */
1336 	err = -EINVAL;
1337 	if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
1338 		goto out1;
1339 
1340 	err = kern_path(buf, 0, &exp.ex_path);
1341 	if (err)
1342 		goto out1;
1343 
1344 	exp.ex_client = dom;
1345 	exp.cd = cd;
1346 	exp.ex_devid_map = NULL;
1347 	exp.ex_xprtsec_modes = NFSEXP_XPRTSEC_ALL;
1348 
1349 	/* expiry */
1350 	err = get_expiry(&mesg, &exp.h.expiry_time);
1351 	if (err)
1352 		goto out3;
1353 
1354 	/* flags */
1355 	err = get_int(&mesg, &an_int);
1356 	if (err == -ENOENT) {
1357 		err = 0;
1358 		set_bit(CACHE_NEGATIVE, &exp.h.flags);
1359 	} else {
1360 		if (err || an_int < 0)
1361 			goto out3;
1362 		exp.ex_flags= an_int;
1363 
1364 		/* anon uid */
1365 		err = get_int(&mesg, &an_int);
1366 		if (err)
1367 			goto out3;
1368 		exp.ex_anon_uid= make_kuid(current_user_ns(), an_int);
1369 
1370 		/* anon gid */
1371 		err = get_int(&mesg, &an_int);
1372 		if (err)
1373 			goto out3;
1374 		exp.ex_anon_gid= make_kgid(current_user_ns(), an_int);
1375 
1376 		/* fsid */
1377 		err = get_int(&mesg, &an_int);
1378 		if (err)
1379 			goto out3;
1380 		exp.ex_fsid = an_int;
1381 
1382 		while (qword_get(&mesg, buf, PAGE_SIZE) > 0) {
1383 			if (strcmp(buf, "fsloc") == 0)
1384 				err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
1385 			else if (strcmp(buf, "uuid") == 0)
1386 				err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
1387 			else if (strcmp(buf, "secinfo") == 0)
1388 				err = secinfo_parse(&mesg, buf, &exp);
1389 			else if (strcmp(buf, "xprtsec") == 0)
1390 				err = xprtsec_parse(&mesg, buf, &exp);
1391 			else
1392 				/* quietly ignore unknown words and anything
1393 				 * following. Newer user-space can try to set
1394 				 * new values, then see what the result was.
1395 				 */
1396 				break;
1397 			if (err)
1398 				goto out4;
1399 		}
1400 
1401 		err = check_export(&exp.ex_path, &exp.ex_flags, exp.ex_uuid);
1402 		if (err)
1403 			goto out4;
1404 
1405 		/*
1406 		 * No point caching this if it would immediately expire.
1407 		 * Also, this protects exportfs's dummy export from the
1408 		 * anon_uid/anon_gid checks:
1409 		 */
1410 		if (exp.h.expiry_time < seconds_since_boot())
1411 			goto out4;
1412 		/*
1413 		 * For some reason exportfs has been passing down an
1414 		 * invalid (-1) uid & gid on the "dummy" export which it
1415 		 * uses to test export support.  To make sure exportfs
1416 		 * sees errors from check_export we therefore need to
1417 		 * delay these checks till after check_export:
1418 		 */
1419 		err = -EINVAL;
1420 		if (!uid_valid(exp.ex_anon_uid))
1421 			goto out4;
1422 		if (!gid_valid(exp.ex_anon_gid))
1423 			goto out4;
1424 		err = 0;
1425 
1426 		if (exp.ex_flags & NFSEXP_PNFS)
1427 			nfsd4_setup_layout_type(&exp);
1428 	}
1429 
1430 	expp = svc_export_lookup(&exp);
1431 	if (!expp) {
1432 		err = -ENOMEM;
1433 		goto out4;
1434 	}
1435 	expp = svc_export_update(&exp, expp);
1436 	if (expp) {
1437 		trace_nfsd_export_update(expp);
1438 		cache_flush();
1439 		exp_put(expp);
1440 	} else
1441 		err = -ENOMEM;
1442 out4:
1443 	nfsd4_fslocs_free(&exp.ex_fslocs);
1444 	kfree(exp.ex_uuid);
1445 out3:
1446 	path_put(&exp.ex_path);
1447 out1:
1448 	auth_domain_put(dom);
1449 out:
1450 	kfree(buf);
1451 	return err;
1452 }
1453 
1454 static void exp_flags(struct seq_file *m, int flag, int fsid,
1455 		kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
1456 static void show_secinfo(struct seq_file *m, struct svc_export *exp);
1457 
1458 static int is_export_stats_file(struct seq_file *m)
1459 {
1460 	/*
1461 	 * The export_stats file uses the same ops as the exports file.
1462 	 * We use the file's name to determine the reported info per export.
1463 	 * There is no rename in nsfdfs, so d_name.name is stable.
1464 	 */
1465 	return !strcmp(m->file->f_path.dentry->d_name.name, "export_stats");
1466 }
1467 
1468 static int svc_export_show(struct seq_file *m,
1469 			   struct cache_detail *cd,
1470 			   struct cache_head *h)
1471 {
1472 	struct svc_export *exp;
1473 	bool export_stats = is_export_stats_file(m);
1474 
1475 	if (h == NULL) {
1476 		if (export_stats)
1477 			seq_puts(m, "#path domain start-time\n#\tstats\n");
1478 		else
1479 			seq_puts(m, "#path domain(flags)\n");
1480 		return 0;
1481 	}
1482 	exp = container_of(h, struct svc_export, h);
1483 	seq_path(m, &exp->ex_path, " \t\n\\");
1484 	seq_putc(m, '\t');
1485 	seq_escape(m, exp->ex_client->name, " \t\n\\");
1486 	if (export_stats) {
1487 		struct percpu_counter *counter = exp->ex_stats->counter;
1488 
1489 		seq_printf(m, "\t%lld\n", exp->ex_stats->start_time);
1490 		seq_printf(m, "\tfh_stale: %lld\n",
1491 			   percpu_counter_sum_positive(&counter[EXP_STATS_FH_STALE]));
1492 		seq_printf(m, "\tio_read: %lld\n",
1493 			   percpu_counter_sum_positive(&counter[EXP_STATS_IO_READ]));
1494 		seq_printf(m, "\tio_write: %lld\n",
1495 			   percpu_counter_sum_positive(&counter[EXP_STATS_IO_WRITE]));
1496 		seq_putc(m, '\n');
1497 		return 0;
1498 	}
1499 	seq_putc(m, '(');
1500 	if (test_bit(CACHE_VALID, &h->flags) &&
1501 	    !test_bit(CACHE_NEGATIVE, &h->flags)) {
1502 		exp_flags(m, exp->ex_flags, exp->ex_fsid,
1503 			  exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
1504 		if (exp->ex_uuid) {
1505 			int i;
1506 			seq_puts(m, ",uuid=");
1507 			for (i = 0; i < EX_UUID_LEN; i++) {
1508 				if ((i&3) == 0 && i)
1509 					seq_putc(m, ':');
1510 				seq_printf(m, "%02x", exp->ex_uuid[i]);
1511 			}
1512 		}
1513 		show_secinfo(m, exp);
1514 	}
1515 	seq_puts(m, ")\n");
1516 	return 0;
1517 }
1518 static int svc_export_match(struct cache_head *a, struct cache_head *b)
1519 {
1520 	struct svc_export *orig = container_of(a, struct svc_export, h);
1521 	struct svc_export *new = container_of(b, struct svc_export, h);
1522 	return orig->ex_client == new->ex_client &&
1523 		path_equal(&orig->ex_path, &new->ex_path);
1524 }
1525 
1526 static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
1527 {
1528 	struct svc_export *new = container_of(cnew, struct svc_export, h);
1529 	struct svc_export *item = container_of(citem, struct svc_export, h);
1530 
1531 	kref_get(&item->ex_client->ref);
1532 	new->ex_client = item->ex_client;
1533 	new->ex_path = item->ex_path;
1534 	path_get(&item->ex_path);
1535 	new->ex_fslocs.locations = NULL;
1536 	new->ex_fslocs.locations_count = 0;
1537 	new->ex_fslocs.migrated = 0;
1538 	new->ex_layout_types = 0;
1539 	new->ex_uuid = NULL;
1540 	new->cd = item->cd;
1541 	export_stats_reset(new->ex_stats);
1542 }
1543 
1544 static void export_update(struct cache_head *cnew, struct cache_head *citem)
1545 {
1546 	struct svc_export *new = container_of(cnew, struct svc_export, h);
1547 	struct svc_export *item = container_of(citem, struct svc_export, h);
1548 	int i;
1549 
1550 	new->ex_flags = item->ex_flags;
1551 	new->ex_anon_uid = item->ex_anon_uid;
1552 	new->ex_anon_gid = item->ex_anon_gid;
1553 	new->ex_fsid = item->ex_fsid;
1554 	new->ex_devid_map = item->ex_devid_map;
1555 	item->ex_devid_map = NULL;
1556 	new->ex_uuid = item->ex_uuid;
1557 	item->ex_uuid = NULL;
1558 	new->ex_fslocs.locations = item->ex_fslocs.locations;
1559 	item->ex_fslocs.locations = NULL;
1560 	new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
1561 	item->ex_fslocs.locations_count = 0;
1562 	new->ex_fslocs.migrated = item->ex_fslocs.migrated;
1563 	item->ex_fslocs.migrated = 0;
1564 	new->ex_layout_types = item->ex_layout_types;
1565 	new->ex_nflavors = item->ex_nflavors;
1566 	for (i = 0; i < MAX_SECINFO_LIST; i++) {
1567 		new->ex_flavors[i] = item->ex_flavors[i];
1568 	}
1569 	new->ex_xprtsec_modes = item->ex_xprtsec_modes;
1570 }
1571 
1572 static struct cache_head *svc_export_alloc(void)
1573 {
1574 	struct svc_export *i = kmalloc_obj(*i);
1575 	if (!i)
1576 		return NULL;
1577 
1578 	i->ex_stats = kmalloc_obj(*(i->ex_stats));
1579 	if (!i->ex_stats) {
1580 		kfree(i);
1581 		return NULL;
1582 	}
1583 
1584 	if (export_stats_init(i->ex_stats)) {
1585 		kfree(i->ex_stats);
1586 		kfree(i);
1587 		return NULL;
1588 	}
1589 
1590 	return &i->h;
1591 }
1592 
1593 static const struct cache_detail svc_export_cache_template = {
1594 	.owner		= THIS_MODULE,
1595 	.hash_size	= EXPORT_HASHMAX,
1596 	.name		= "nfsd.export",
1597 	.cache_put	= svc_export_put,
1598 	.cache_upcall	= svc_export_upcall,
1599 	.cache_notify	= svc_export_notify,
1600 	.cache_request	= svc_export_request,
1601 	.cache_parse	= svc_export_parse,
1602 	.cache_show	= svc_export_show,
1603 	.match		= svc_export_match,
1604 	.init		= svc_export_init,
1605 	.update		= export_update,
1606 	.alloc		= svc_export_alloc,
1607 };
1608 
1609 static int
1610 svc_export_hash(struct svc_export *exp)
1611 {
1612 	int hash;
1613 
1614 	hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
1615 	hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
1616 	hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
1617 	return hash;
1618 }
1619 
1620 static struct svc_export *
1621 svc_export_lookup(struct svc_export *exp)
1622 {
1623 	struct cache_head *ch;
1624 	int hash = svc_export_hash(exp);
1625 
1626 	ch = sunrpc_cache_lookup_rcu(exp->cd, &exp->h, hash);
1627 	if (ch)
1628 		return container_of(ch, struct svc_export, h);
1629 	else
1630 		return NULL;
1631 }
1632 
1633 static struct svc_export *
1634 svc_export_update(struct svc_export *new, struct svc_export *old)
1635 {
1636 	struct cache_head *ch;
1637 	int hash = svc_export_hash(old);
1638 
1639 	ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
1640 	if (ch)
1641 		return container_of(ch, struct svc_export, h);
1642 	else
1643 		return NULL;
1644 }
1645 
1646 
1647 static struct svc_expkey *
1648 exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
1649 	     u32 *fsidv, struct cache_req *reqp)
1650 {
1651 	struct svc_expkey key, *ek;
1652 	int err;
1653 
1654 	if (!clp)
1655 		return ERR_PTR(-ENOENT);
1656 
1657 	key.ek_client = clp;
1658 	key.ek_fsidtype = fsid_type;
1659 	memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
1660 
1661 	ek = svc_expkey_lookup(cd, &key);
1662 	if (ek == NULL)
1663 		return ERR_PTR(-ENOMEM);
1664 	err = cache_check(cd, &ek->h, reqp);
1665 	if (err) {
1666 		trace_nfsd_exp_find_key(&key, err);
1667 		return ERR_PTR(err);
1668 	}
1669 	return ek;
1670 }
1671 
1672 static struct svc_export *
1673 exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
1674 		const struct path *path, struct cache_req *reqp)
1675 {
1676 	struct svc_export *exp, key;
1677 	int err;
1678 
1679 	if (!clp)
1680 		return ERR_PTR(-ENOENT);
1681 
1682 	key.ex_client = clp;
1683 	key.ex_path = *path;
1684 	key.cd = cd;
1685 
1686 	exp = svc_export_lookup(&key);
1687 	if (exp == NULL)
1688 		return ERR_PTR(-ENOMEM);
1689 	err = cache_check(cd, &exp->h, reqp);
1690 	if (err) {
1691 		trace_nfsd_exp_get_by_name(&key, err);
1692 		return ERR_PTR(err);
1693 	}
1694 	return exp;
1695 }
1696 
1697 /*
1698  * Find the export entry for a given dentry.
1699  */
1700 static struct svc_export *
1701 exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
1702 {
1703 	struct dentry *saved = dget(path->dentry);
1704 	struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
1705 
1706 	while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
1707 		struct dentry *parent = dget_parent(path->dentry);
1708 		dput(path->dentry);
1709 		path->dentry = parent;
1710 		exp = exp_get_by_name(cd, clp, path, NULL);
1711 	}
1712 	dput(path->dentry);
1713 	path->dentry = saved;
1714 	return exp;
1715 }
1716 
1717 
1718 
1719 /*
1720  * Obtain the root fh on behalf of a client.
1721  * This could be done in user space, but I feel that it adds some safety
1722  * since its harder to fool a kernel module than a user space program.
1723  */
1724 int
1725 exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
1726 	   struct knfsd_fh *f, int maxsize)
1727 {
1728 	struct svc_export	*exp;
1729 	struct path		path;
1730 	struct inode		*inode __maybe_unused;
1731 	struct svc_fh		fh;
1732 	int			err;
1733 	struct nfsd_net		*nn = net_generic(net, nfsd_net_id);
1734 	struct cache_detail	*cd = nn->svc_export_cache;
1735 
1736 	err = -EPERM;
1737 	/* NB: we probably ought to check that it's NUL-terminated */
1738 	if (kern_path(name, 0, &path)) {
1739 		printk("nfsd: exp_rootfh path not found %s", name);
1740 		return err;
1741 	}
1742 	inode = d_inode(path.dentry);
1743 
1744 	dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%llu)\n",
1745 		 name, path.dentry, clp->name,
1746 		 inode->i_sb->s_id, inode->i_ino);
1747 	exp = exp_parent(cd, clp, &path);
1748 	if (IS_ERR(exp)) {
1749 		err = PTR_ERR(exp);
1750 		goto out;
1751 	}
1752 
1753 	/*
1754 	 * fh must be initialized before calling fh_compose
1755 	 */
1756 	fh_init(&fh, maxsize);
1757 	if (fh_compose(&fh, exp, path.dentry, NULL))
1758 		err = -EINVAL;
1759 	else
1760 		err = 0;
1761 	memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
1762 	fh_put(&fh);
1763 	exp_put(exp);
1764 out:
1765 	path_put(&path);
1766 	return err;
1767 }
1768 
1769 static struct svc_export *exp_find(struct cache_detail *cd,
1770 				   struct auth_domain *clp, int fsid_type,
1771 				   u32 *fsidv, struct cache_req *reqp)
1772 {
1773 	struct svc_export *exp;
1774 	struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
1775 	struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
1776 	if (IS_ERR(ek))
1777 		return ERR_CAST(ek);
1778 
1779 	exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
1780 	cache_put(&ek->h, nn->svc_expkey_cache);
1781 
1782 	if (IS_ERR(exp))
1783 		return ERR_CAST(exp);
1784 	return exp;
1785 }
1786 
1787 /**
1788  * check_xprtsec_policy - check if access to export is allowed by the
1789  *			  xprtsec policy
1790  * @exp: svc_export that is being accessed.
1791  * @rqstp: svc_rqst attempting to access @exp.
1792  *
1793  * Helper function for check_nfsd_access().  Note that callers should be
1794  * using check_nfsd_access() instead of calling this function directly.  The
1795  * one exception is __fh_verify() since it has logic that may result in one
1796  * or both of the helpers being skipped.
1797  *
1798  * Return values:
1799  *   %nfs_ok if access is granted, or
1800  *   %nfserr_wrongsec if access is denied
1801  */
1802 __be32 check_xprtsec_policy(struct svc_export *exp, struct svc_rqst *rqstp)
1803 {
1804 	struct svc_xprt *xprt = rqstp->rq_xprt;
1805 
1806 	if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_NONE) {
1807 		if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags))
1808 			return nfs_ok;
1809 	}
1810 	if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_TLS) {
1811 		if (test_bit(XPT_TLS_SESSION, &xprt->xpt_flags) &&
1812 		    !test_bit(XPT_PEER_AUTH, &xprt->xpt_flags))
1813 			return nfs_ok;
1814 	}
1815 	if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_MTLS) {
1816 		if (test_bit(XPT_TLS_SESSION, &xprt->xpt_flags) &&
1817 		    test_bit(XPT_PEER_AUTH, &xprt->xpt_flags))
1818 			return nfs_ok;
1819 	}
1820 	return nfserr_wrongsec;
1821 }
1822 
1823 /**
1824  * check_security_flavor - check if access to export is allowed by the
1825  *			   security flavor
1826  * @exp: svc_export that is being accessed.
1827  * @rqstp: svc_rqst attempting to access @exp.
1828  * @may_bypass_gss: reduce strictness of authorization check
1829  *
1830  * Helper function for check_nfsd_access().  Note that callers should be
1831  * using check_nfsd_access() instead of calling this function directly.  The
1832  * one exception is __fh_verify() since it has logic that may result in one
1833  * or both of the helpers being skipped.
1834  *
1835  * Return values:
1836  *   %nfs_ok if access is granted, or
1837  *   %nfserr_wrongsec if access is denied
1838  */
1839 __be32 check_security_flavor(struct svc_export *exp, struct svc_rqst *rqstp,
1840 			     bool may_bypass_gss)
1841 {
1842 	struct exp_flavor_info *f, *end = exp->ex_flavors + exp->ex_nflavors;
1843 
1844 	/* legacy gss-only clients are always OK: */
1845 	if (exp->ex_client == rqstp->rq_gssclient)
1846 		return nfs_ok;
1847 	/* ip-address based client; check sec= export option: */
1848 	for (f = exp->ex_flavors; f < end; f++) {
1849 		if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
1850 			return nfs_ok;
1851 	}
1852 	/* defaults in absence of sec= options: */
1853 	if (exp->ex_nflavors == 0) {
1854 		if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
1855 		    rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
1856 			return nfs_ok;
1857 	}
1858 
1859 	/* If the compound op contains a spo_must_allowed op,
1860 	 * it will be sent with integrity/protection which
1861 	 * will have to be expressly allowed on mounts that
1862 	 * don't support it
1863 	 */
1864 
1865 	if (nfsd4_spo_must_allow(rqstp))
1866 		return nfs_ok;
1867 
1868 	/* Some calls may be processed without authentication
1869 	 * on GSS exports. For example NFS2/3 calls on root
1870 	 * directory, see section 2.3.2 of rfc 2623.
1871 	 * For "may_bypass_gss" check that export has really
1872 	 * enabled some flavor with authentication (GSS or any
1873 	 * other) and also check that the used auth flavor is
1874 	 * without authentication (none or sys).
1875 	 */
1876 	if (may_bypass_gss && (
1877 	     rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
1878 	     rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)) {
1879 		for (f = exp->ex_flavors; f < end; f++) {
1880 			if (f->pseudoflavor >= RPC_AUTH_DES)
1881 				return 0;
1882 		}
1883 	}
1884 
1885 	return nfserr_wrongsec;
1886 }
1887 
1888 /**
1889  * check_nfsd_access - check if access to export is allowed.
1890  * @exp: svc_export that is being accessed.
1891  * @rqstp: svc_rqst attempting to access @exp.
1892  * @may_bypass_gss: reduce strictness of authorization check
1893  *
1894  * Return values:
1895  *   %nfs_ok if access is granted, or
1896  *   %nfserr_wrongsec if access is denied
1897  */
1898 __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
1899 			 bool may_bypass_gss)
1900 {
1901 	__be32 status;
1902 
1903 	status = check_xprtsec_policy(exp, rqstp);
1904 	if (status != nfs_ok)
1905 		return status;
1906 	return check_security_flavor(exp, rqstp, may_bypass_gss);
1907 }
1908 
1909 /*
1910  * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
1911  * auth_unix client) if it's available and has secinfo information;
1912  * otherwise, will try to use rq_gssclient.
1913  *
1914  * Called from functions that handle requests; functions that do work on
1915  * behalf of mountd are passed a single client name to use, and should
1916  * use exp_get_by_name() or exp_find().
1917  */
1918 struct svc_export *
1919 rqst_exp_get_by_name(struct svc_rqst *rqstp, const struct path *path)
1920 {
1921 	struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1922 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1923 	struct cache_detail *cd = nn->svc_export_cache;
1924 
1925 	if (rqstp->rq_client == NULL)
1926 		goto gss;
1927 
1928 	/* First try the auth_unix client: */
1929 	exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
1930 	if (PTR_ERR(exp) == -ENOENT)
1931 		goto gss;
1932 	if (IS_ERR(exp))
1933 		return exp;
1934 	/* If it has secinfo, assume there are no gss/... clients */
1935 	if (exp->ex_nflavors > 0)
1936 		return exp;
1937 gss:
1938 	/* Otherwise, try falling back on gss client */
1939 	if (rqstp->rq_gssclient == NULL)
1940 		return exp;
1941 	gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
1942 	if (PTR_ERR(gssexp) == -ENOENT)
1943 		return exp;
1944 	if (!IS_ERR(exp))
1945 		exp_put(exp);
1946 	return gssexp;
1947 }
1948 
1949 /**
1950  * rqst_exp_find - Find an svc_export in the context of a rqst or similar
1951  * @reqp:	The handle to be used to suspend the request if a cache-upcall is needed
1952  *		If NULL, missing in-cache information will result in failure.
1953  * @net:	The network namespace in which the request exists
1954  * @cl:		default auth_domain to use for looking up the export
1955  * @gsscl:	an alternate auth_domain defined using deprecated gss/krb5 format.
1956  * @fsid_type:	The type of fsid to look for
1957  * @fsidv:	The actual fsid to look up in the context of either client.
1958  *
1959  * Perform a lookup for @cl/@fsidv in the given @net for an export.  If
1960  * none found and @gsscl specified, repeat the lookup.
1961  *
1962  * Returns an export, or an error pointer.
1963  */
1964 struct svc_export *
1965 rqst_exp_find(struct cache_req *reqp, struct net *net,
1966 	      struct auth_domain *cl, struct auth_domain *gsscl,
1967 	      int fsid_type, u32 *fsidv)
1968 {
1969 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1970 	struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1971 	struct cache_detail *cd = nn->svc_export_cache;
1972 
1973 	if (!cl)
1974 		goto gss;
1975 
1976 	/* First try the auth_unix client: */
1977 	exp = exp_find(cd, cl, fsid_type, fsidv, reqp);
1978 	if (PTR_ERR(exp) == -ENOENT)
1979 		goto gss;
1980 	if (IS_ERR(exp))
1981 		return exp;
1982 	/* If it has secinfo, assume there are no gss/... clients */
1983 	if (exp->ex_nflavors > 0)
1984 		return exp;
1985 gss:
1986 	/* Otherwise, try falling back on gss client */
1987 	if (!gsscl)
1988 		return exp;
1989 	gssexp = exp_find(cd, gsscl, fsid_type, fsidv, reqp);
1990 	if (PTR_ERR(gssexp) == -ENOENT)
1991 		return exp;
1992 	if (!IS_ERR(exp))
1993 		exp_put(exp);
1994 	return gssexp;
1995 }
1996 
1997 struct svc_export *
1998 rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
1999 {
2000 	struct dentry *saved = dget(path->dentry);
2001 	struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
2002 
2003 	while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
2004 		struct dentry *parent = dget_parent(path->dentry);
2005 		dput(path->dentry);
2006 		path->dentry = parent;
2007 		exp = rqst_exp_get_by_name(rqstp, path);
2008 	}
2009 	dput(path->dentry);
2010 	path->dentry = saved;
2011 	return exp;
2012 }
2013 
2014 struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
2015 {
2016 	u32 fsidv[2];
2017 
2018 	mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
2019 
2020 	return rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp),
2021 			     rqstp->rq_client, rqstp->rq_gssclient,
2022 			     FSID_NUM, fsidv);
2023 }
2024 
2025 /*
2026  * Called when we need the filehandle for the root of the pseudofs,
2027  * for a given NFSv4 client.   The root is defined to be the
2028  * export point with fsid==0
2029  */
2030 __be32
2031 exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
2032 {
2033 	struct svc_export *exp;
2034 	__be32 rv;
2035 
2036 	exp = rqst_find_fsidzero_export(rqstp);
2037 	if (IS_ERR(exp))
2038 		return nfserrno(PTR_ERR(exp));
2039 	rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
2040 	exp_put(exp);
2041 	return rv;
2042 }
2043 
2044 static struct flags {
2045 	int flag;
2046 	char *name[2];
2047 } expflags[] = {
2048 	{ NFSEXP_READONLY, {"ro", "rw"}},
2049 	{ NFSEXP_INSECURE_PORT, {"insecure", ""}},
2050 	{ NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
2051 	{ NFSEXP_ALLSQUASH, {"all_squash", ""}},
2052 	{ NFSEXP_ASYNC, {"async", "sync"}},
2053 	{ NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
2054 	{ NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
2055 	{ NFSEXP_SECURITY_LABEL, {"security_label", ""}},
2056 	{ NFSEXP_SIGN_FH, {"sign_fh", ""}},
2057 	{ NFSEXP_NOHIDE, {"nohide", ""}},
2058 	{ NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
2059 	{ NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
2060 	{ NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
2061 	{ NFSEXP_V4ROOT, {"v4root", ""}},
2062 	{ NFSEXP_PNFS, {"pnfs", ""}},
2063 	{ 0, {"", ""}}
2064 };
2065 
2066 static void show_expflags(struct seq_file *m, int flags, int mask)
2067 {
2068 	struct flags *flg;
2069 	int state, first = 0;
2070 
2071 	for (flg = expflags; flg->flag; flg++) {
2072 		if (flg->flag & ~mask)
2073 			continue;
2074 		state = (flg->flag & flags) ? 0 : 1;
2075 		if (*flg->name[state])
2076 			seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
2077 	}
2078 }
2079 
2080 static void show_secinfo_flags(struct seq_file *m, int flags)
2081 {
2082 	seq_printf(m, ",");
2083 	show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
2084 }
2085 
2086 static bool secinfo_flags_equal(int f, int g)
2087 {
2088 	f &= NFSEXP_SECINFO_FLAGS;
2089 	g &= NFSEXP_SECINFO_FLAGS;
2090 	return f == g;
2091 }
2092 
2093 static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
2094 {
2095 	int flags;
2096 
2097 	flags = (*fp)->flags;
2098 	seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
2099 	(*fp)++;
2100 	while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
2101 		seq_printf(m, ":%d", (*fp)->pseudoflavor);
2102 		(*fp)++;
2103 	}
2104 	return flags;
2105 }
2106 
2107 static void show_secinfo(struct seq_file *m, struct svc_export *exp)
2108 {
2109 	struct exp_flavor_info *f;
2110 	struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
2111 	int flags;
2112 
2113 	if (exp->ex_nflavors == 0)
2114 		return;
2115 	f = exp->ex_flavors;
2116 	flags = show_secinfo_run(m, &f, end);
2117 	if (!secinfo_flags_equal(flags, exp->ex_flags))
2118 		show_secinfo_flags(m, flags);
2119 	while (f != end) {
2120 		flags = show_secinfo_run(m, &f, end);
2121 		show_secinfo_flags(m, flags);
2122 	}
2123 }
2124 
2125 static void exp_flags(struct seq_file *m, int flag, int fsid,
2126 		kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
2127 {
2128 	struct user_namespace *userns = m->file->f_cred->user_ns;
2129 
2130 	show_expflags(m, flag, NFSEXP_ALLFLAGS);
2131 	if (flag & NFSEXP_FSID)
2132 		seq_printf(m, ",fsid=%d", fsid);
2133 	if (!uid_eq(anonu, make_kuid(userns, (uid_t)-2)) &&
2134 	    !uid_eq(anonu, make_kuid(userns, 0x10000-2)))
2135 		seq_printf(m, ",anonuid=%u", from_kuid_munged(userns, anonu));
2136 	if (!gid_eq(anong, make_kgid(userns, (gid_t)-2)) &&
2137 	    !gid_eq(anong, make_kgid(userns, 0x10000-2)))
2138 		seq_printf(m, ",anongid=%u", from_kgid_munged(userns, anong));
2139 	if (fsloc && fsloc->locations_count > 0) {
2140 		char *loctype = (fsloc->migrated) ? "refer" : "replicas";
2141 		int i;
2142 
2143 		seq_printf(m, ",%s=", loctype);
2144 		seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
2145 		seq_putc(m, '@');
2146 		seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
2147 		for (i = 1; i < fsloc->locations_count; i++) {
2148 			seq_putc(m, ';');
2149 			seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
2150 			seq_putc(m, '@');
2151 			seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
2152 		}
2153 	}
2154 }
2155 
2156 static int e_show(struct seq_file *m, void *p)
2157 {
2158 	struct cache_head *cp = p;
2159 	struct svc_export *exp = container_of(cp, struct svc_export, h);
2160 	struct cache_detail *cd = m->private;
2161 	bool export_stats = is_export_stats_file(m);
2162 
2163 	if (p == SEQ_START_TOKEN) {
2164 		seq_puts(m, "# Version 1.1\n");
2165 		if (export_stats)
2166 			seq_puts(m, "# Path Client Start-time\n#\tStats\n");
2167 		else
2168 			seq_puts(m, "# Path Client(Flags) # IPs\n");
2169 		return 0;
2170 	}
2171 
2172 	if (cache_check_rcu(cd, &exp->h, NULL))
2173 		return 0;
2174 
2175 	return svc_export_show(m, cd, cp);
2176 }
2177 
2178 const struct seq_operations nfs_exports_op = {
2179 	.start	= cache_seq_start_rcu,
2180 	.next	= cache_seq_next_rcu,
2181 	.stop	= cache_seq_stop_rcu,
2182 	.show	= e_show,
2183 };
2184 
2185 /*
2186  * Initialize the exports module.
2187  */
2188 int
2189 nfsd_export_init(struct net *net)
2190 {
2191 	int rv;
2192 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2193 
2194 	dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
2195 
2196 	nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
2197 	if (IS_ERR(nn->svc_export_cache))
2198 		return PTR_ERR(nn->svc_export_cache);
2199 	rv = cache_register_net(nn->svc_export_cache, net);
2200 	if (rv)
2201 		goto destroy_export_cache;
2202 
2203 	nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
2204 	if (IS_ERR(nn->svc_expkey_cache)) {
2205 		rv = PTR_ERR(nn->svc_expkey_cache);
2206 		goto unregister_export_cache;
2207 	}
2208 	rv = cache_register_net(nn->svc_expkey_cache, net);
2209 	if (rv)
2210 		goto destroy_expkey_cache;
2211 	return 0;
2212 
2213 destroy_expkey_cache:
2214 	cache_destroy_net(nn->svc_expkey_cache, net);
2215 unregister_export_cache:
2216 	cache_unregister_net(nn->svc_export_cache, net);
2217 destroy_export_cache:
2218 	cache_destroy_net(nn->svc_export_cache, net);
2219 	return rv;
2220 }
2221 
2222 /*
2223  * Flush exports table - called when last nfsd thread is killed
2224  */
2225 void
2226 nfsd_export_flush(struct net *net)
2227 {
2228 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2229 
2230 	cache_purge(nn->svc_expkey_cache);
2231 	cache_purge(nn->svc_export_cache);
2232 }
2233 
2234 /*
2235  * Shutdown the exports module.
2236  */
2237 void
2238 nfsd_export_shutdown(struct net *net)
2239 {
2240 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2241 
2242 	dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
2243 
2244 	cache_unregister_net(nn->svc_expkey_cache, net);
2245 	cache_unregister_net(nn->svc_export_cache, net);
2246 	cache_destroy_net(nn->svc_expkey_cache, net);
2247 	cache_destroy_net(nn->svc_export_cache, net);
2248 	svcauth_unix_purge(net);
2249 
2250 	dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
2251 }
2252