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