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