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