0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/slab.h>
0016 #include <linux/namei.h>
0017 #include <linux/module.h>
0018 #include <linux/exportfs.h>
0019 #include <linux/sunrpc/svc_xprt.h>
0020
0021 #include "nfsd.h"
0022 #include "nfsfh.h"
0023 #include "netns.h"
0024 #include "pnfs.h"
0025 #include "filecache.h"
0026 #include "trace.h"
0027
0028 #define NFSDDBG_FACILITY NFSDDBG_EXPORT
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #define EXPKEY_HASHBITS 8
0040 #define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
0041 #define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
0042
0043 static void expkey_put(struct kref *ref)
0044 {
0045 struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
0046
0047 if (test_bit(CACHE_VALID, &key->h.flags) &&
0048 !test_bit(CACHE_NEGATIVE, &key->h.flags))
0049 path_put(&key->ek_path);
0050 auth_domain_put(key->ek_client);
0051 kfree_rcu(key, ek_rcu);
0052 }
0053
0054 static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
0055 {
0056 return sunrpc_cache_pipe_upcall(cd, h);
0057 }
0058
0059 static void expkey_request(struct cache_detail *cd,
0060 struct cache_head *h,
0061 char **bpp, int *blen)
0062 {
0063
0064 struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
0065 char type[5];
0066
0067 qword_add(bpp, blen, ek->ek_client->name);
0068 snprintf(type, 5, "%d", ek->ek_fsidtype);
0069 qword_add(bpp, blen, type);
0070 qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
0071 (*bpp)[-1] = '\n';
0072 }
0073
0074 static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
0075 struct svc_expkey *old);
0076 static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
0077
0078 static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
0079 {
0080
0081 char *buf;
0082 int len;
0083 struct auth_domain *dom = NULL;
0084 int err;
0085 int fsidtype;
0086 char *ep;
0087 struct svc_expkey key;
0088 struct svc_expkey *ek = NULL;
0089
0090 if (mesg[mlen - 1] != '\n')
0091 return -EINVAL;
0092 mesg[mlen-1] = 0;
0093
0094 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
0095 err = -ENOMEM;
0096 if (!buf)
0097 goto out;
0098
0099 err = -EINVAL;
0100 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
0101 goto out;
0102
0103 err = -ENOENT;
0104 dom = auth_domain_find(buf);
0105 if (!dom)
0106 goto out;
0107 dprintk("found domain %s\n", buf);
0108
0109 err = -EINVAL;
0110 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
0111 goto out;
0112 fsidtype = simple_strtoul(buf, &ep, 10);
0113 if (*ep)
0114 goto out;
0115 dprintk("found fsidtype %d\n", fsidtype);
0116 if (key_len(fsidtype)==0)
0117 goto out;
0118 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
0119 goto out;
0120 dprintk("found fsid length %d\n", len);
0121 if (len != key_len(fsidtype))
0122 goto out;
0123
0124
0125 key.h.flags = 0;
0126 key.h.expiry_time = get_expiry(&mesg);
0127 if (key.h.expiry_time == 0)
0128 goto out;
0129
0130 key.ek_client = dom;
0131 key.ek_fsidtype = fsidtype;
0132 memcpy(key.ek_fsid, buf, len);
0133
0134 ek = svc_expkey_lookup(cd, &key);
0135 err = -ENOMEM;
0136 if (!ek)
0137 goto out;
0138
0139
0140 err = -EINVAL;
0141 len = qword_get(&mesg, buf, PAGE_SIZE);
0142 if (len < 0)
0143 goto out;
0144 dprintk("Path seems to be <%s>\n", buf);
0145 err = 0;
0146 if (len == 0) {
0147 set_bit(CACHE_NEGATIVE, &key.h.flags);
0148 ek = svc_expkey_update(cd, &key, ek);
0149 if (ek)
0150 trace_nfsd_expkey_update(ek, NULL);
0151 else
0152 err = -ENOMEM;
0153 } else {
0154 err = kern_path(buf, 0, &key.ek_path);
0155 if (err)
0156 goto out;
0157
0158 dprintk("Found the path %s\n", buf);
0159
0160 ek = svc_expkey_update(cd, &key, ek);
0161 if (ek)
0162 trace_nfsd_expkey_update(ek, buf);
0163 else
0164 err = -ENOMEM;
0165 path_put(&key.ek_path);
0166 }
0167 cache_flush();
0168 out:
0169 if (ek)
0170 cache_put(&ek->h, cd);
0171 if (dom)
0172 auth_domain_put(dom);
0173 kfree(buf);
0174 return err;
0175 }
0176
0177 static int expkey_show(struct seq_file *m,
0178 struct cache_detail *cd,
0179 struct cache_head *h)
0180 {
0181 struct svc_expkey *ek ;
0182 int i;
0183
0184 if (h ==NULL) {
0185 seq_puts(m, "#domain fsidtype fsid [path]\n");
0186 return 0;
0187 }
0188 ek = container_of(h, struct svc_expkey, h);
0189 seq_printf(m, "%s %d 0x", ek->ek_client->name,
0190 ek->ek_fsidtype);
0191 for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
0192 seq_printf(m, "%08x", ek->ek_fsid[i]);
0193 if (test_bit(CACHE_VALID, &h->flags) &&
0194 !test_bit(CACHE_NEGATIVE, &h->flags)) {
0195 seq_printf(m, " ");
0196 seq_path(m, &ek->ek_path, "\\ \t\n");
0197 }
0198 seq_printf(m, "\n");
0199 return 0;
0200 }
0201
0202 static inline int expkey_match (struct cache_head *a, struct cache_head *b)
0203 {
0204 struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
0205 struct svc_expkey *new = container_of(b, struct svc_expkey, h);
0206
0207 if (orig->ek_fsidtype != new->ek_fsidtype ||
0208 orig->ek_client != new->ek_client ||
0209 memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
0210 return 0;
0211 return 1;
0212 }
0213
0214 static inline void expkey_init(struct cache_head *cnew,
0215 struct cache_head *citem)
0216 {
0217 struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
0218 struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
0219
0220 kref_get(&item->ek_client->ref);
0221 new->ek_client = item->ek_client;
0222 new->ek_fsidtype = item->ek_fsidtype;
0223
0224 memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
0225 }
0226
0227 static inline void expkey_update(struct cache_head *cnew,
0228 struct cache_head *citem)
0229 {
0230 struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
0231 struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
0232
0233 new->ek_path = item->ek_path;
0234 path_get(&item->ek_path);
0235 }
0236
0237 static struct cache_head *expkey_alloc(void)
0238 {
0239 struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
0240 if (i)
0241 return &i->h;
0242 else
0243 return NULL;
0244 }
0245
0246 static void expkey_flush(void)
0247 {
0248
0249
0250
0251
0252 mutex_lock(&nfsd_mutex);
0253 nfsd_file_cache_purge(current->nsproxy->net_ns);
0254 mutex_unlock(&nfsd_mutex);
0255 }
0256
0257 static const struct cache_detail svc_expkey_cache_template = {
0258 .owner = THIS_MODULE,
0259 .hash_size = EXPKEY_HASHMAX,
0260 .name = "nfsd.fh",
0261 .cache_put = expkey_put,
0262 .cache_upcall = expkey_upcall,
0263 .cache_request = expkey_request,
0264 .cache_parse = expkey_parse,
0265 .cache_show = expkey_show,
0266 .match = expkey_match,
0267 .init = expkey_init,
0268 .update = expkey_update,
0269 .alloc = expkey_alloc,
0270 .flush = expkey_flush,
0271 };
0272
0273 static int
0274 svc_expkey_hash(struct svc_expkey *item)
0275 {
0276 int hash = item->ek_fsidtype;
0277 char * cp = (char*)item->ek_fsid;
0278 int len = key_len(item->ek_fsidtype);
0279
0280 hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
0281 hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
0282 hash &= EXPKEY_HASHMASK;
0283 return hash;
0284 }
0285
0286 static struct svc_expkey *
0287 svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
0288 {
0289 struct cache_head *ch;
0290 int hash = svc_expkey_hash(item);
0291
0292 ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
0293 if (ch)
0294 return container_of(ch, struct svc_expkey, h);
0295 else
0296 return NULL;
0297 }
0298
0299 static struct svc_expkey *
0300 svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
0301 struct svc_expkey *old)
0302 {
0303 struct cache_head *ch;
0304 int hash = svc_expkey_hash(new);
0305
0306 ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
0307 if (ch)
0308 return container_of(ch, struct svc_expkey, h);
0309 else
0310 return NULL;
0311 }
0312
0313
0314 #define EXPORT_HASHBITS 8
0315 #define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
0316
0317 static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
0318 {
0319 struct nfsd4_fs_location *locations = fsloc->locations;
0320 int i;
0321
0322 if (!locations)
0323 return;
0324
0325 for (i = 0; i < fsloc->locations_count; i++) {
0326 kfree(locations[i].path);
0327 kfree(locations[i].hosts);
0328 }
0329
0330 kfree(locations);
0331 fsloc->locations = NULL;
0332 }
0333
0334 static int export_stats_init(struct export_stats *stats)
0335 {
0336 stats->start_time = ktime_get_seconds();
0337 return nfsd_percpu_counters_init(stats->counter, EXP_STATS_COUNTERS_NUM);
0338 }
0339
0340 static void export_stats_reset(struct export_stats *stats)
0341 {
0342 nfsd_percpu_counters_reset(stats->counter, EXP_STATS_COUNTERS_NUM);
0343 }
0344
0345 static void export_stats_destroy(struct export_stats *stats)
0346 {
0347 nfsd_percpu_counters_destroy(stats->counter, EXP_STATS_COUNTERS_NUM);
0348 }
0349
0350 static void svc_export_put(struct kref *ref)
0351 {
0352 struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
0353 path_put(&exp->ex_path);
0354 auth_domain_put(exp->ex_client);
0355 nfsd4_fslocs_free(&exp->ex_fslocs);
0356 export_stats_destroy(&exp->ex_stats);
0357 kfree(exp->ex_uuid);
0358 kfree_rcu(exp, ex_rcu);
0359 }
0360
0361 static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
0362 {
0363 return sunrpc_cache_pipe_upcall(cd, h);
0364 }
0365
0366 static void svc_export_request(struct cache_detail *cd,
0367 struct cache_head *h,
0368 char **bpp, int *blen)
0369 {
0370
0371 struct svc_export *exp = container_of(h, struct svc_export, h);
0372 char *pth;
0373
0374 qword_add(bpp, blen, exp->ex_client->name);
0375 pth = d_path(&exp->ex_path, *bpp, *blen);
0376 if (IS_ERR(pth)) {
0377
0378 (*bpp)[0] = '\n';
0379 return;
0380 }
0381 qword_add(bpp, blen, pth);
0382 (*bpp)[-1] = '\n';
0383 }
0384
0385 static struct svc_export *svc_export_update(struct svc_export *new,
0386 struct svc_export *old);
0387 static struct svc_export *svc_export_lookup(struct svc_export *);
0388
0389 static int check_export(struct path *path, int *flags, unsigned char *uuid)
0390 {
0391 struct inode *inode = d_inode(path->dentry);
0392
0393
0394
0395
0396
0397 if (!S_ISDIR(inode->i_mode) &&
0398 !S_ISLNK(inode->i_mode) &&
0399 !S_ISREG(inode->i_mode))
0400 return -ENOTDIR;
0401
0402
0403
0404
0405
0406 if (*flags & NFSEXP_V4ROOT)
0407 *flags |= NFSEXP_READONLY;
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417 if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
0418 !(*flags & NFSEXP_FSID) &&
0419 uuid == NULL) {
0420 dprintk("exp_export: export of non-dev fs without fsid\n");
0421 return -EINVAL;
0422 }
0423
0424 if (!inode->i_sb->s_export_op ||
0425 !inode->i_sb->s_export_op->fh_to_dentry) {
0426 dprintk("exp_export: export of invalid fs type.\n");
0427 return -EINVAL;
0428 }
0429
0430 if (is_idmapped_mnt(path->mnt)) {
0431 dprintk("exp_export: export of idmapped mounts not yet supported.\n");
0432 return -EINVAL;
0433 }
0434
0435 if (inode->i_sb->s_export_op->flags & EXPORT_OP_NOSUBTREECHK &&
0436 !(*flags & NFSEXP_NOSUBTREECHECK)) {
0437 dprintk("%s: %s does not support subtree checking!\n",
0438 __func__, inode->i_sb->s_type->name);
0439 return -EINVAL;
0440 }
0441 return 0;
0442
0443 }
0444
0445 #ifdef CONFIG_NFSD_V4
0446
0447 static int
0448 fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
0449 {
0450 int len;
0451 int migrated, i, err;
0452
0453
0454 if (fsloc->locations)
0455 return -EINVAL;
0456
0457
0458 err = get_uint(mesg, &fsloc->locations_count);
0459 if (err)
0460 return err;
0461 if (fsloc->locations_count > MAX_FS_LOCATIONS)
0462 return -EINVAL;
0463 if (fsloc->locations_count == 0)
0464 return 0;
0465
0466 fsloc->locations = kcalloc(fsloc->locations_count,
0467 sizeof(struct nfsd4_fs_location),
0468 GFP_KERNEL);
0469 if (!fsloc->locations)
0470 return -ENOMEM;
0471 for (i=0; i < fsloc->locations_count; i++) {
0472
0473 err = -EINVAL;
0474 len = qword_get(mesg, buf, PAGE_SIZE);
0475 if (len <= 0)
0476 goto out_free_all;
0477 err = -ENOMEM;
0478 fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
0479 if (!fsloc->locations[i].hosts)
0480 goto out_free_all;
0481 err = -EINVAL;
0482
0483 len = qword_get(mesg, buf, PAGE_SIZE);
0484 if (len <= 0)
0485 goto out_free_all;
0486 err = -ENOMEM;
0487 fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
0488 if (!fsloc->locations[i].path)
0489 goto out_free_all;
0490 }
0491
0492 err = get_int(mesg, &migrated);
0493 if (err)
0494 goto out_free_all;
0495 err = -EINVAL;
0496 if (migrated < 0 || migrated > 1)
0497 goto out_free_all;
0498 fsloc->migrated = migrated;
0499 return 0;
0500 out_free_all:
0501 nfsd4_fslocs_free(fsloc);
0502 return err;
0503 }
0504
0505 static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
0506 {
0507 struct exp_flavor_info *f;
0508 u32 listsize;
0509 int err;
0510
0511
0512 if (exp->ex_nflavors)
0513 return -EINVAL;
0514
0515 err = get_uint(mesg, &listsize);
0516 if (err)
0517 return err;
0518 if (listsize > MAX_SECINFO_LIST)
0519 return -EINVAL;
0520
0521 for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
0522 err = get_uint(mesg, &f->pseudoflavor);
0523 if (err)
0524 return err;
0525
0526
0527
0528
0529
0530
0531 err = get_uint(mesg, &f->flags);
0532 if (err)
0533 return err;
0534
0535 if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
0536 return -EINVAL;
0537 }
0538 exp->ex_nflavors = listsize;
0539 return 0;
0540 }
0541
0542 #else
0543 static inline int
0544 fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
0545 static inline int
0546 secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
0547 #endif
0548
0549 static inline int
0550 nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
0551 {
0552 int len;
0553
0554
0555 if (*puuid)
0556 return -EINVAL;
0557
0558
0559 len = qword_get(mesg, buf, PAGE_SIZE);
0560 if (len != EX_UUID_LEN)
0561 return -EINVAL;
0562
0563 *puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
0564 if (*puuid == NULL)
0565 return -ENOMEM;
0566
0567 return 0;
0568 }
0569
0570 static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
0571 {
0572
0573 char *buf;
0574 int len;
0575 int err;
0576 struct auth_domain *dom = NULL;
0577 struct svc_export exp = {}, *expp;
0578 int an_int;
0579
0580 if (mesg[mlen-1] != '\n')
0581 return -EINVAL;
0582 mesg[mlen-1] = 0;
0583
0584 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
0585 if (!buf)
0586 return -ENOMEM;
0587
0588
0589 err = -EINVAL;
0590 len = qword_get(&mesg, buf, PAGE_SIZE);
0591 if (len <= 0)
0592 goto out;
0593
0594 err = -ENOENT;
0595 dom = auth_domain_find(buf);
0596 if (!dom)
0597 goto out;
0598
0599
0600 err = -EINVAL;
0601 if ((len = qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
0602 goto out1;
0603
0604 err = kern_path(buf, 0, &exp.ex_path);
0605 if (err)
0606 goto out1;
0607
0608 exp.ex_client = dom;
0609 exp.cd = cd;
0610 exp.ex_devid_map = NULL;
0611
0612
0613 err = -EINVAL;
0614 exp.h.expiry_time = get_expiry(&mesg);
0615 if (exp.h.expiry_time == 0)
0616 goto out3;
0617
0618
0619 err = get_int(&mesg, &an_int);
0620 if (err == -ENOENT) {
0621 err = 0;
0622 set_bit(CACHE_NEGATIVE, &exp.h.flags);
0623 } else {
0624 if (err || an_int < 0)
0625 goto out3;
0626 exp.ex_flags= an_int;
0627
0628
0629 err = get_int(&mesg, &an_int);
0630 if (err)
0631 goto out3;
0632 exp.ex_anon_uid= make_kuid(current_user_ns(), an_int);
0633
0634
0635 err = get_int(&mesg, &an_int);
0636 if (err)
0637 goto out3;
0638 exp.ex_anon_gid= make_kgid(current_user_ns(), an_int);
0639
0640
0641 err = get_int(&mesg, &an_int);
0642 if (err)
0643 goto out3;
0644 exp.ex_fsid = an_int;
0645
0646 while ((len = qword_get(&mesg, buf, PAGE_SIZE)) > 0) {
0647 if (strcmp(buf, "fsloc") == 0)
0648 err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
0649 else if (strcmp(buf, "uuid") == 0)
0650 err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
0651 else if (strcmp(buf, "secinfo") == 0)
0652 err = secinfo_parse(&mesg, buf, &exp);
0653 else
0654
0655
0656
0657
0658 break;
0659 if (err)
0660 goto out4;
0661 }
0662
0663 err = check_export(&exp.ex_path, &exp.ex_flags, exp.ex_uuid);
0664 if (err)
0665 goto out4;
0666
0667
0668
0669
0670
0671 if (exp.h.expiry_time < seconds_since_boot())
0672 goto out4;
0673
0674
0675
0676
0677
0678
0679
0680 err = -EINVAL;
0681 if (!uid_valid(exp.ex_anon_uid))
0682 goto out4;
0683 if (!gid_valid(exp.ex_anon_gid))
0684 goto out4;
0685 err = 0;
0686
0687 nfsd4_setup_layout_type(&exp);
0688 }
0689
0690 expp = svc_export_lookup(&exp);
0691 if (!expp) {
0692 err = -ENOMEM;
0693 goto out4;
0694 }
0695 expp = svc_export_update(&exp, expp);
0696 if (expp) {
0697 trace_nfsd_export_update(expp);
0698 cache_flush();
0699 exp_put(expp);
0700 } else
0701 err = -ENOMEM;
0702 out4:
0703 nfsd4_fslocs_free(&exp.ex_fslocs);
0704 kfree(exp.ex_uuid);
0705 out3:
0706 path_put(&exp.ex_path);
0707 out1:
0708 auth_domain_put(dom);
0709 out:
0710 kfree(buf);
0711 return err;
0712 }
0713
0714 static void exp_flags(struct seq_file *m, int flag, int fsid,
0715 kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
0716 static void show_secinfo(struct seq_file *m, struct svc_export *exp);
0717
0718 static int is_export_stats_file(struct seq_file *m)
0719 {
0720
0721
0722
0723
0724
0725 return !strcmp(m->file->f_path.dentry->d_name.name, "export_stats");
0726 }
0727
0728 static int svc_export_show(struct seq_file *m,
0729 struct cache_detail *cd,
0730 struct cache_head *h)
0731 {
0732 struct svc_export *exp;
0733 bool export_stats = is_export_stats_file(m);
0734
0735 if (h == NULL) {
0736 if (export_stats)
0737 seq_puts(m, "#path domain start-time\n#\tstats\n");
0738 else
0739 seq_puts(m, "#path domain(flags)\n");
0740 return 0;
0741 }
0742 exp = container_of(h, struct svc_export, h);
0743 seq_path(m, &exp->ex_path, " \t\n\\");
0744 seq_putc(m, '\t');
0745 seq_escape(m, exp->ex_client->name, " \t\n\\");
0746 if (export_stats) {
0747 seq_printf(m, "\t%lld\n", exp->ex_stats.start_time);
0748 seq_printf(m, "\tfh_stale: %lld\n",
0749 percpu_counter_sum_positive(&exp->ex_stats.counter[EXP_STATS_FH_STALE]));
0750 seq_printf(m, "\tio_read: %lld\n",
0751 percpu_counter_sum_positive(&exp->ex_stats.counter[EXP_STATS_IO_READ]));
0752 seq_printf(m, "\tio_write: %lld\n",
0753 percpu_counter_sum_positive(&exp->ex_stats.counter[EXP_STATS_IO_WRITE]));
0754 seq_putc(m, '\n');
0755 return 0;
0756 }
0757 seq_putc(m, '(');
0758 if (test_bit(CACHE_VALID, &h->flags) &&
0759 !test_bit(CACHE_NEGATIVE, &h->flags)) {
0760 exp_flags(m, exp->ex_flags, exp->ex_fsid,
0761 exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
0762 if (exp->ex_uuid) {
0763 int i;
0764 seq_puts(m, ",uuid=");
0765 for (i = 0; i < EX_UUID_LEN; i++) {
0766 if ((i&3) == 0 && i)
0767 seq_putc(m, ':');
0768 seq_printf(m, "%02x", exp->ex_uuid[i]);
0769 }
0770 }
0771 show_secinfo(m, exp);
0772 }
0773 seq_puts(m, ")\n");
0774 return 0;
0775 }
0776 static int svc_export_match(struct cache_head *a, struct cache_head *b)
0777 {
0778 struct svc_export *orig = container_of(a, struct svc_export, h);
0779 struct svc_export *new = container_of(b, struct svc_export, h);
0780 return orig->ex_client == new->ex_client &&
0781 path_equal(&orig->ex_path, &new->ex_path);
0782 }
0783
0784 static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
0785 {
0786 struct svc_export *new = container_of(cnew, struct svc_export, h);
0787 struct svc_export *item = container_of(citem, struct svc_export, h);
0788
0789 kref_get(&item->ex_client->ref);
0790 new->ex_client = item->ex_client;
0791 new->ex_path = item->ex_path;
0792 path_get(&item->ex_path);
0793 new->ex_fslocs.locations = NULL;
0794 new->ex_fslocs.locations_count = 0;
0795 new->ex_fslocs.migrated = 0;
0796 new->ex_layout_types = 0;
0797 new->ex_uuid = NULL;
0798 new->cd = item->cd;
0799 export_stats_reset(&new->ex_stats);
0800 }
0801
0802 static void export_update(struct cache_head *cnew, struct cache_head *citem)
0803 {
0804 struct svc_export *new = container_of(cnew, struct svc_export, h);
0805 struct svc_export *item = container_of(citem, struct svc_export, h);
0806 int i;
0807
0808 new->ex_flags = item->ex_flags;
0809 new->ex_anon_uid = item->ex_anon_uid;
0810 new->ex_anon_gid = item->ex_anon_gid;
0811 new->ex_fsid = item->ex_fsid;
0812 new->ex_devid_map = item->ex_devid_map;
0813 item->ex_devid_map = NULL;
0814 new->ex_uuid = item->ex_uuid;
0815 item->ex_uuid = NULL;
0816 new->ex_fslocs.locations = item->ex_fslocs.locations;
0817 item->ex_fslocs.locations = NULL;
0818 new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
0819 item->ex_fslocs.locations_count = 0;
0820 new->ex_fslocs.migrated = item->ex_fslocs.migrated;
0821 item->ex_fslocs.migrated = 0;
0822 new->ex_layout_types = item->ex_layout_types;
0823 new->ex_nflavors = item->ex_nflavors;
0824 for (i = 0; i < MAX_SECINFO_LIST; i++) {
0825 new->ex_flavors[i] = item->ex_flavors[i];
0826 }
0827 }
0828
0829 static struct cache_head *svc_export_alloc(void)
0830 {
0831 struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
0832 if (!i)
0833 return NULL;
0834
0835 if (export_stats_init(&i->ex_stats)) {
0836 kfree(i);
0837 return NULL;
0838 }
0839
0840 return &i->h;
0841 }
0842
0843 static const struct cache_detail svc_export_cache_template = {
0844 .owner = THIS_MODULE,
0845 .hash_size = EXPORT_HASHMAX,
0846 .name = "nfsd.export",
0847 .cache_put = svc_export_put,
0848 .cache_upcall = svc_export_upcall,
0849 .cache_request = svc_export_request,
0850 .cache_parse = svc_export_parse,
0851 .cache_show = svc_export_show,
0852 .match = svc_export_match,
0853 .init = svc_export_init,
0854 .update = export_update,
0855 .alloc = svc_export_alloc,
0856 };
0857
0858 static int
0859 svc_export_hash(struct svc_export *exp)
0860 {
0861 int hash;
0862
0863 hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
0864 hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
0865 hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
0866 return hash;
0867 }
0868
0869 static struct svc_export *
0870 svc_export_lookup(struct svc_export *exp)
0871 {
0872 struct cache_head *ch;
0873 int hash = svc_export_hash(exp);
0874
0875 ch = sunrpc_cache_lookup_rcu(exp->cd, &exp->h, hash);
0876 if (ch)
0877 return container_of(ch, struct svc_export, h);
0878 else
0879 return NULL;
0880 }
0881
0882 static struct svc_export *
0883 svc_export_update(struct svc_export *new, struct svc_export *old)
0884 {
0885 struct cache_head *ch;
0886 int hash = svc_export_hash(old);
0887
0888 ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
0889 if (ch)
0890 return container_of(ch, struct svc_export, h);
0891 else
0892 return NULL;
0893 }
0894
0895
0896 static struct svc_expkey *
0897 exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
0898 u32 *fsidv, struct cache_req *reqp)
0899 {
0900 struct svc_expkey key, *ek;
0901 int err;
0902
0903 if (!clp)
0904 return ERR_PTR(-ENOENT);
0905
0906 key.ek_client = clp;
0907 key.ek_fsidtype = fsid_type;
0908 memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
0909
0910 ek = svc_expkey_lookup(cd, &key);
0911 if (ek == NULL)
0912 return ERR_PTR(-ENOMEM);
0913 err = cache_check(cd, &ek->h, reqp);
0914 if (err) {
0915 trace_nfsd_exp_find_key(&key, err);
0916 return ERR_PTR(err);
0917 }
0918 return ek;
0919 }
0920
0921 static struct svc_export *
0922 exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
0923 const struct path *path, struct cache_req *reqp)
0924 {
0925 struct svc_export *exp, key;
0926 int err;
0927
0928 if (!clp)
0929 return ERR_PTR(-ENOENT);
0930
0931 key.ex_client = clp;
0932 key.ex_path = *path;
0933 key.cd = cd;
0934
0935 exp = svc_export_lookup(&key);
0936 if (exp == NULL)
0937 return ERR_PTR(-ENOMEM);
0938 err = cache_check(cd, &exp->h, reqp);
0939 if (err) {
0940 trace_nfsd_exp_get_by_name(&key, err);
0941 return ERR_PTR(err);
0942 }
0943 return exp;
0944 }
0945
0946
0947
0948
0949 static struct svc_export *
0950 exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
0951 {
0952 struct dentry *saved = dget(path->dentry);
0953 struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
0954
0955 while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
0956 struct dentry *parent = dget_parent(path->dentry);
0957 dput(path->dentry);
0958 path->dentry = parent;
0959 exp = exp_get_by_name(cd, clp, path, NULL);
0960 }
0961 dput(path->dentry);
0962 path->dentry = saved;
0963 return exp;
0964 }
0965
0966
0967
0968
0969
0970
0971
0972
0973 int
0974 exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
0975 struct knfsd_fh *f, int maxsize)
0976 {
0977 struct svc_export *exp;
0978 struct path path;
0979 struct inode *inode;
0980 struct svc_fh fh;
0981 int err;
0982 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
0983 struct cache_detail *cd = nn->svc_export_cache;
0984
0985 err = -EPERM;
0986
0987 if (kern_path(name, 0, &path)) {
0988 printk("nfsd: exp_rootfh path not found %s", name);
0989 return err;
0990 }
0991 inode = d_inode(path.dentry);
0992
0993 dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
0994 name, path.dentry, clp->name,
0995 inode->i_sb->s_id, inode->i_ino);
0996 exp = exp_parent(cd, clp, &path);
0997 if (IS_ERR(exp)) {
0998 err = PTR_ERR(exp);
0999 goto out;
1000 }
1001
1002
1003
1004
1005 fh_init(&fh, maxsize);
1006 if (fh_compose(&fh, exp, path.dentry, NULL))
1007 err = -EINVAL;
1008 else
1009 err = 0;
1010 memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
1011 fh_put(&fh);
1012 exp_put(exp);
1013 out:
1014 path_put(&path);
1015 return err;
1016 }
1017
1018 static struct svc_export *exp_find(struct cache_detail *cd,
1019 struct auth_domain *clp, int fsid_type,
1020 u32 *fsidv, struct cache_req *reqp)
1021 {
1022 struct svc_export *exp;
1023 struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
1024 struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
1025 if (IS_ERR(ek))
1026 return ERR_CAST(ek);
1027
1028 exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
1029 cache_put(&ek->h, nn->svc_expkey_cache);
1030
1031 if (IS_ERR(exp))
1032 return ERR_CAST(exp);
1033 return exp;
1034 }
1035
1036 __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
1037 {
1038 struct exp_flavor_info *f;
1039 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
1040
1041
1042 if (exp->ex_client == rqstp->rq_gssclient)
1043 return 0;
1044
1045 for (f = exp->ex_flavors; f < end; f++) {
1046 if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
1047 return 0;
1048 }
1049
1050 if (exp->ex_nflavors == 0) {
1051 if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
1052 rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
1053 return 0;
1054 }
1055
1056
1057
1058
1059
1060
1061
1062 if (nfsd4_spo_must_allow(rqstp))
1063 return 0;
1064
1065 return rqstp->rq_vers < 4 ? nfserr_acces : nfserr_wrongsec;
1066 }
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077 struct svc_export *
1078 rqst_exp_get_by_name(struct svc_rqst *rqstp, struct path *path)
1079 {
1080 struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1081 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1082 struct cache_detail *cd = nn->svc_export_cache;
1083
1084 if (rqstp->rq_client == NULL)
1085 goto gss;
1086
1087
1088 exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
1089 if (PTR_ERR(exp) == -ENOENT)
1090 goto gss;
1091 if (IS_ERR(exp))
1092 return exp;
1093
1094 if (exp->ex_nflavors > 0)
1095 return exp;
1096 gss:
1097
1098 if (rqstp->rq_gssclient == NULL)
1099 return exp;
1100 gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
1101 if (PTR_ERR(gssexp) == -ENOENT)
1102 return exp;
1103 if (!IS_ERR(exp))
1104 exp_put(exp);
1105 return gssexp;
1106 }
1107
1108 struct svc_export *
1109 rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
1110 {
1111 struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
1112 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1113 struct cache_detail *cd = nn->svc_export_cache;
1114
1115 if (rqstp->rq_client == NULL)
1116 goto gss;
1117
1118
1119 exp = exp_find(cd, rqstp->rq_client, fsid_type,
1120 fsidv, &rqstp->rq_chandle);
1121 if (PTR_ERR(exp) == -ENOENT)
1122 goto gss;
1123 if (IS_ERR(exp))
1124 return exp;
1125
1126 if (exp->ex_nflavors > 0)
1127 return exp;
1128 gss:
1129
1130 if (rqstp->rq_gssclient == NULL)
1131 return exp;
1132 gssexp = exp_find(cd, rqstp->rq_gssclient, fsid_type, fsidv,
1133 &rqstp->rq_chandle);
1134 if (PTR_ERR(gssexp) == -ENOENT)
1135 return exp;
1136 if (!IS_ERR(exp))
1137 exp_put(exp);
1138 return gssexp;
1139 }
1140
1141 struct svc_export *
1142 rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
1143 {
1144 struct dentry *saved = dget(path->dentry);
1145 struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
1146
1147 while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
1148 struct dentry *parent = dget_parent(path->dentry);
1149 dput(path->dentry);
1150 path->dentry = parent;
1151 exp = rqst_exp_get_by_name(rqstp, path);
1152 }
1153 dput(path->dentry);
1154 path->dentry = saved;
1155 return exp;
1156 }
1157
1158 struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
1159 {
1160 u32 fsidv[2];
1161
1162 mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
1163
1164 return rqst_exp_find(rqstp, FSID_NUM, fsidv);
1165 }
1166
1167
1168
1169
1170
1171
1172 __be32
1173 exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
1174 {
1175 struct svc_export *exp;
1176 __be32 rv;
1177
1178 exp = rqst_find_fsidzero_export(rqstp);
1179 if (IS_ERR(exp))
1180 return nfserrno(PTR_ERR(exp));
1181 rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
1182 exp_put(exp);
1183 return rv;
1184 }
1185
1186 static struct flags {
1187 int flag;
1188 char *name[2];
1189 } expflags[] = {
1190 { NFSEXP_READONLY, {"ro", "rw"}},
1191 { NFSEXP_INSECURE_PORT, {"insecure", ""}},
1192 { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
1193 { NFSEXP_ALLSQUASH, {"all_squash", ""}},
1194 { NFSEXP_ASYNC, {"async", "sync"}},
1195 { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
1196 { NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
1197 { NFSEXP_NOHIDE, {"nohide", ""}},
1198 { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
1199 { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
1200 { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
1201 { NFSEXP_V4ROOT, {"v4root", ""}},
1202 { NFSEXP_PNFS, {"pnfs", ""}},
1203 { NFSEXP_SECURITY_LABEL, {"security_label", ""}},
1204 { 0, {"", ""}}
1205 };
1206
1207 static void show_expflags(struct seq_file *m, int flags, int mask)
1208 {
1209 struct flags *flg;
1210 int state, first = 0;
1211
1212 for (flg = expflags; flg->flag; flg++) {
1213 if (flg->flag & ~mask)
1214 continue;
1215 state = (flg->flag & flags) ? 0 : 1;
1216 if (*flg->name[state])
1217 seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
1218 }
1219 }
1220
1221 static void show_secinfo_flags(struct seq_file *m, int flags)
1222 {
1223 seq_printf(m, ",");
1224 show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
1225 }
1226
1227 static bool secinfo_flags_equal(int f, int g)
1228 {
1229 f &= NFSEXP_SECINFO_FLAGS;
1230 g &= NFSEXP_SECINFO_FLAGS;
1231 return f == g;
1232 }
1233
1234 static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
1235 {
1236 int flags;
1237
1238 flags = (*fp)->flags;
1239 seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
1240 (*fp)++;
1241 while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
1242 seq_printf(m, ":%d", (*fp)->pseudoflavor);
1243 (*fp)++;
1244 }
1245 return flags;
1246 }
1247
1248 static void show_secinfo(struct seq_file *m, struct svc_export *exp)
1249 {
1250 struct exp_flavor_info *f;
1251 struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
1252 int flags;
1253
1254 if (exp->ex_nflavors == 0)
1255 return;
1256 f = exp->ex_flavors;
1257 flags = show_secinfo_run(m, &f, end);
1258 if (!secinfo_flags_equal(flags, exp->ex_flags))
1259 show_secinfo_flags(m, flags);
1260 while (f != end) {
1261 flags = show_secinfo_run(m, &f, end);
1262 show_secinfo_flags(m, flags);
1263 }
1264 }
1265
1266 static void exp_flags(struct seq_file *m, int flag, int fsid,
1267 kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
1268 {
1269 struct user_namespace *userns = m->file->f_cred->user_ns;
1270
1271 show_expflags(m, flag, NFSEXP_ALLFLAGS);
1272 if (flag & NFSEXP_FSID)
1273 seq_printf(m, ",fsid=%d", fsid);
1274 if (!uid_eq(anonu, make_kuid(userns, (uid_t)-2)) &&
1275 !uid_eq(anonu, make_kuid(userns, 0x10000-2)))
1276 seq_printf(m, ",anonuid=%u", from_kuid_munged(userns, anonu));
1277 if (!gid_eq(anong, make_kgid(userns, (gid_t)-2)) &&
1278 !gid_eq(anong, make_kgid(userns, 0x10000-2)))
1279 seq_printf(m, ",anongid=%u", from_kgid_munged(userns, anong));
1280 if (fsloc && fsloc->locations_count > 0) {
1281 char *loctype = (fsloc->migrated) ? "refer" : "replicas";
1282 int i;
1283
1284 seq_printf(m, ",%s=", loctype);
1285 seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
1286 seq_putc(m, '@');
1287 seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
1288 for (i = 1; i < fsloc->locations_count; i++) {
1289 seq_putc(m, ';');
1290 seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
1291 seq_putc(m, '@');
1292 seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
1293 }
1294 }
1295 }
1296
1297 static int e_show(struct seq_file *m, void *p)
1298 {
1299 struct cache_head *cp = p;
1300 struct svc_export *exp = container_of(cp, struct svc_export, h);
1301 struct cache_detail *cd = m->private;
1302 bool export_stats = is_export_stats_file(m);
1303
1304 if (p == SEQ_START_TOKEN) {
1305 seq_puts(m, "# Version 1.1\n");
1306 if (export_stats)
1307 seq_puts(m, "# Path Client Start-time\n#\tStats\n");
1308 else
1309 seq_puts(m, "# Path Client(Flags) # IPs\n");
1310 return 0;
1311 }
1312
1313 exp_get(exp);
1314 if (cache_check(cd, &exp->h, NULL))
1315 return 0;
1316 exp_put(exp);
1317 return svc_export_show(m, cd, cp);
1318 }
1319
1320 const struct seq_operations nfs_exports_op = {
1321 .start = cache_seq_start_rcu,
1322 .next = cache_seq_next_rcu,
1323 .stop = cache_seq_stop_rcu,
1324 .show = e_show,
1325 };
1326
1327
1328
1329
1330 int
1331 nfsd_export_init(struct net *net)
1332 {
1333 int rv;
1334 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1335
1336 dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
1337
1338 nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
1339 if (IS_ERR(nn->svc_export_cache))
1340 return PTR_ERR(nn->svc_export_cache);
1341 rv = cache_register_net(nn->svc_export_cache, net);
1342 if (rv)
1343 goto destroy_export_cache;
1344
1345 nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
1346 if (IS_ERR(nn->svc_expkey_cache)) {
1347 rv = PTR_ERR(nn->svc_expkey_cache);
1348 goto unregister_export_cache;
1349 }
1350 rv = cache_register_net(nn->svc_expkey_cache, net);
1351 if (rv)
1352 goto destroy_expkey_cache;
1353 return 0;
1354
1355 destroy_expkey_cache:
1356 cache_destroy_net(nn->svc_expkey_cache, net);
1357 unregister_export_cache:
1358 cache_unregister_net(nn->svc_export_cache, net);
1359 destroy_export_cache:
1360 cache_destroy_net(nn->svc_export_cache, net);
1361 return rv;
1362 }
1363
1364
1365
1366
1367 void
1368 nfsd_export_flush(struct net *net)
1369 {
1370 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1371
1372 cache_purge(nn->svc_expkey_cache);
1373 cache_purge(nn->svc_export_cache);
1374 }
1375
1376
1377
1378
1379 void
1380 nfsd_export_shutdown(struct net *net)
1381 {
1382 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1383
1384 dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
1385
1386 cache_unregister_net(nn->svc_expkey_cache, net);
1387 cache_unregister_net(nn->svc_export_cache, net);
1388 cache_destroy_net(nn->svc_expkey_cache, net);
1389 cache_destroy_net(nn->svc_export_cache, net);
1390 svcauth_unix_purge(net);
1391
1392 dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
1393 }