blob: d3178de589e42aeed21053e199b07bf16bb381a5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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>
Bruce Allanf35279d2005-09-06 15:17:08 -070029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
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
Adrian Bunk74cae612006-03-27 01:15:10 -080060static void expkey_put(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
NeilBrownbaab9352006-03-27 01:15:09 -080062 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
NeilBrownbaab9352006-03-27 01:15:09 -080069 auth_domain_put(key->ek_client);
70 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
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
NeilBrown8d270f72006-03-27 01:15:04 -080088static struct svc_expkey *svc_expkey_update(struct svc_expkey *new, struct svc_expkey *old);
89static struct svc_expkey *svc_expkey_lookup(struct svc_expkey *);
Adrian Bunk74cae612006-03-27 01:15:10 -080090static struct cache_detail svc_expkey_cache;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
93{
94 /* client fsidtype fsid [path] */
95 char *buf;
96 int len;
97 struct auth_domain *dom = NULL;
98 int err;
99 int fsidtype;
100 char *ep;
101 struct svc_expkey key;
NeilBrown8d270f72006-03-27 01:15:04 -0800102 struct svc_expkey *ek;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (mesg[mlen-1] != '\n')
105 return -EINVAL;
106 mesg[mlen-1] = 0;
107
108 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
109 err = -ENOMEM;
110 if (!buf) goto out;
111
112 err = -EINVAL;
113 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
114 goto out;
115
116 err = -ENOENT;
117 dom = auth_domain_find(buf);
118 if (!dom)
119 goto out;
120 dprintk("found domain %s\n", buf);
121
122 err = -EINVAL;
123 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
124 goto out;
125 fsidtype = simple_strtoul(buf, &ep, 10);
126 if (*ep)
127 goto out;
128 dprintk("found fsidtype %d\n", fsidtype);
Frank Filz4bdff8c2006-06-30 01:56:11 -0700129 if (key_len(fsidtype)==0) /* invalid type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto out;
131 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
132 goto out;
133 dprintk("found fsid length %d\n", len);
134 if (len != key_len(fsidtype))
135 goto out;
136
137 /* OK, we seem to have a valid key */
138 key.h.flags = 0;
139 key.h.expiry_time = get_expiry(&mesg);
140 if (key.h.expiry_time == 0)
141 goto out;
142
143 key.ek_client = dom;
144 key.ek_fsidtype = fsidtype;
145 memcpy(key.ek_fsid, buf, len);
146
NeilBrown8d270f72006-03-27 01:15:04 -0800147 ek = svc_expkey_lookup(&key);
148 err = -ENOMEM;
149 if (!ek)
150 goto out;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* now we want a pathname, or empty meaning NEGATIVE */
NeilBrown8d270f72006-03-27 01:15:04 -0800153 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) < 0)
155 goto out;
156 dprintk("Path seems to be <%s>\n", buf);
157 err = 0;
158 if (len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 set_bit(CACHE_NEGATIVE, &key.h.flags);
NeilBrown8d270f72006-03-27 01:15:04 -0800160 ek = svc_expkey_update(&key, ek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ek)
NeilBrownbaab9352006-03-27 01:15:09 -0800162 cache_put(&ek->h, &svc_expkey_cache);
NeilBrown8d270f72006-03-27 01:15:04 -0800163 else err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 } else {
165 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 err = path_lookup(buf, 0, &nd);
167 if (err)
168 goto out;
169
170 dprintk("Found the path %s\n", buf);
NeilBrowneab7e2e2006-03-27 01:15:00 -0800171 key.ek_mnt = nd.mnt;
172 key.ek_dentry = nd.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
NeilBrown8d270f72006-03-27 01:15:04 -0800174 ek = svc_expkey_update(&key, ek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (ek)
NeilBrownbaab9352006-03-27 01:15:09 -0800176 cache_put(&ek->h, &svc_expkey_cache);
NeilBrown8d270f72006-03-27 01:15:04 -0800177 else
178 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 path_release(&nd);
180 }
181 cache_flush();
182 out:
183 if (dom)
184 auth_domain_put(dom);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800185 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return err;
187}
188
189static int expkey_show(struct seq_file *m,
190 struct cache_detail *cd,
191 struct cache_head *h)
192{
193 struct svc_expkey *ek ;
194
195 if (h ==NULL) {
196 seq_puts(m, "#domain fsidtype fsid [path]\n");
197 return 0;
198 }
199 ek = container_of(h, struct svc_expkey, h);
200 seq_printf(m, "%s %d 0x%08x", ek->ek_client->name,
201 ek->ek_fsidtype, ek->ek_fsid[0]);
202 if (ek->ek_fsidtype != 1)
203 seq_printf(m, "%08x", ek->ek_fsid[1]);
204 if (ek->ek_fsidtype == 2)
205 seq_printf(m, "%08x", ek->ek_fsid[2]);
206 if (test_bit(CACHE_VALID, &h->flags) &&
207 !test_bit(CACHE_NEGATIVE, &h->flags)) {
208 seq_printf(m, " ");
NeilBrowneab7e2e2006-03-27 01:15:00 -0800209 seq_path(m, ek->ek_mnt, ek->ek_dentry, "\\ \t\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 seq_printf(m, "\n");
212 return 0;
213}
NeilBrown8d270f72006-03-27 01:15:04 -0800214
215static inline int expkey_match (struct cache_head *a, struct cache_head *b)
216{
217 struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
218 struct svc_expkey *new = container_of(b, struct svc_expkey, h);
219
220 if (orig->ek_fsidtype != new->ek_fsidtype ||
221 orig->ek_client != new->ek_client ||
222 memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
223 return 0;
224 return 1;
225}
226
227static inline void expkey_init(struct cache_head *cnew,
228 struct cache_head *citem)
229{
230 struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
231 struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
232
233 kref_get(&item->ek_client->ref);
234 new->ek_client = item->ek_client;
235 new->ek_fsidtype = item->ek_fsidtype;
236 new->ek_fsid[0] = item->ek_fsid[0];
237 new->ek_fsid[1] = item->ek_fsid[1];
238 new->ek_fsid[2] = item->ek_fsid[2];
239}
240
241static inline void expkey_update(struct cache_head *cnew,
242 struct cache_head *citem)
243{
244 struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
245 struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
246
247 new->ek_mnt = mntget(item->ek_mnt);
248 new->ek_dentry = dget(item->ek_dentry);
249}
250
251static struct cache_head *expkey_alloc(void)
252{
253 struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
254 if (i)
255 return &i->h;
256 else
257 return NULL;
258}
259
Adrian Bunk74cae612006-03-27 01:15:10 -0800260static struct cache_detail svc_expkey_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700261 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .hash_size = EXPKEY_HASHMAX,
263 .hash_table = expkey_table,
264 .name = "nfsd.fh",
265 .cache_put = expkey_put,
266 .cache_request = expkey_request,
267 .cache_parse = expkey_parse,
268 .cache_show = expkey_show,
NeilBrown8d270f72006-03-27 01:15:04 -0800269 .match = expkey_match,
270 .init = expkey_init,
271 .update = expkey_update,
272 .alloc = expkey_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
NeilBrown8d270f72006-03-27 01:15:04 -0800275static struct svc_expkey *
276svc_expkey_lookup(struct svc_expkey *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
NeilBrown8d270f72006-03-27 01:15:04 -0800278 struct cache_head *ch;
279 int hash = item->ek_fsidtype;
280 char * cp = (char*)item->ek_fsid;
281 int len = key_len(item->ek_fsidtype);
282
283 hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
284 hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
285 hash &= EXPKEY_HASHMASK;
286
287 ch = sunrpc_cache_lookup(&svc_expkey_cache, &item->h,
288 hash);
289 if (ch)
290 return container_of(ch, struct svc_expkey, h);
291 else
292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
NeilBrown8d270f72006-03-27 01:15:04 -0800295static struct svc_expkey *
296svc_expkey_update(struct svc_expkey *new, struct svc_expkey *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
NeilBrown8d270f72006-03-27 01:15:04 -0800298 struct cache_head *ch;
299 int hash = new->ek_fsidtype;
300 char * cp = (char*)new->ek_fsid;
301 int len = key_len(new->ek_fsidtype);
302
303 hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
304 hash ^= hash_ptr(new->ek_client, EXPKEY_HASHBITS);
305 hash &= EXPKEY_HASHMASK;
306
307 ch = sunrpc_cache_update(&svc_expkey_cache, &new->h,
308 &old->h, hash);
309 if (ch)
310 return container_of(ch, struct svc_expkey, h);
311 else
312 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316#define EXPORT_HASHBITS 8
317#define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
318#define EXPORT_HASHMASK (EXPORT_HASHMAX -1)
319
320static struct cache_head *export_table[EXPORT_HASHMAX];
321
NeilBrownbaab9352006-03-27 01:15:09 -0800322static void svc_export_put(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownbaab9352006-03-27 01:15:09 -0800324 struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
325 dput(exp->ex_dentry);
326 mntput(exp->ex_mnt);
327 auth_domain_put(exp->ex_client);
J.Bruce Fieldsb009a872006-10-04 02:16:17 -0700328 kfree(exp->ex_path);
NeilBrownbaab9352006-03-27 01:15:09 -0800329 kfree(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332static void svc_export_request(struct cache_detail *cd,
333 struct cache_head *h,
334 char **bpp, int *blen)
335{
336 /* client path */
337 struct svc_export *exp = container_of(h, struct svc_export, h);
338 char *pth;
339
340 qword_add(bpp, blen, exp->ex_client->name);
341 pth = d_path(exp->ex_dentry, exp->ex_mnt, *bpp, *blen);
342 if (IS_ERR(pth)) {
343 /* is this correct? */
344 (*bpp)[0] = '\n';
345 return;
346 }
347 qword_add(bpp, blen, pth);
348 (*bpp)[-1] = '\n';
349}
350
Adrian Bunk74cae612006-03-27 01:15:10 -0800351static struct svc_export *svc_export_update(struct svc_export *new,
352 struct svc_export *old);
NeilBrown4f7774c2006-03-27 01:15:03 -0800353static struct svc_export *svc_export_lookup(struct svc_export *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355static int check_export(struct inode *inode, int flags)
356{
357
358 /* We currently export only dirs and regular files.
359 * This is what umountd does.
360 */
361 if (!S_ISDIR(inode->i_mode) &&
362 !S_ISREG(inode->i_mode))
363 return -ENOTDIR;
364
365 /* There are two requirements on a filesystem to be exportable.
366 * 1: We must be able to identify the filesystem from a number.
367 * either a device number (so FS_REQUIRES_DEV needed)
368 * or an FSID number (so NFSEXP_FSID needed).
369 * 2: We must be able to find an inode from a filehandle.
370 * This means that s_export_op must be set.
371 */
372 if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
373 !(flags & NFSEXP_FSID)) {
Greg Banks3e3b4802006-10-02 02:17:41 -0700374 dprintk("exp_export: export of non-dev fs without fsid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
376 }
377 if (!inode->i_sb->s_export_op) {
378 dprintk("exp_export: export of invalid fs type.\n");
379 return -EINVAL;
380 }
381
382 /* Ok, we can export it */;
383 if (!inode->i_sb->s_export_op->find_exported_dentry)
384 inode->i_sb->s_export_op->find_exported_dentry =
385 find_exported_dentry;
386 return 0;
387
388}
389
390static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
391{
392 /* client path expiry [flags anonuid anongid fsid] */
393 char *buf;
394 int len;
395 int err;
396 struct auth_domain *dom = NULL;
397 struct nameidata nd;
398 struct svc_export exp, *expp;
399 int an_int;
400
401 nd.dentry = NULL;
J.Bruce Fieldsb009a872006-10-04 02:16:17 -0700402 exp.ex_path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (mesg[mlen-1] != '\n')
405 return -EINVAL;
406 mesg[mlen-1] = 0;
407
408 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
409 err = -ENOMEM;
410 if (!buf) goto out;
411
412 /* client */
413 len = qword_get(&mesg, buf, PAGE_SIZE);
414 err = -EINVAL;
415 if (len <= 0) goto out;
416
417 err = -ENOENT;
418 dom = auth_domain_find(buf);
419 if (!dom)
420 goto out;
421
422 /* path */
423 err = -EINVAL;
424 if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
425 goto out;
426 err = path_lookup(buf, 0, &nd);
NeilBrowncd156542006-04-10 22:55:27 -0700427 if (err) goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 exp.h.flags = 0;
430 exp.ex_client = dom;
431 exp.ex_mnt = nd.mnt;
432 exp.ex_dentry = nd.dentry;
J.Bruce Fieldsb009a872006-10-04 02:16:17 -0700433 exp.ex_path = kstrdup(buf, GFP_KERNEL);
434 err = -ENOMEM;
435 if (!exp.ex_path)
436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* expiry */
439 err = -EINVAL;
440 exp.h.expiry_time = get_expiry(&mesg);
441 if (exp.h.expiry_time == 0)
442 goto out;
443
444 /* flags */
445 err = get_int(&mesg, &an_int);
446 if (err == -ENOENT)
447 set_bit(CACHE_NEGATIVE, &exp.h.flags);
448 else {
449 if (err || an_int < 0) goto out;
450 exp.ex_flags= an_int;
451
452 /* anon uid */
453 err = get_int(&mesg, &an_int);
454 if (err) goto out;
455 exp.ex_anon_uid= an_int;
456
457 /* anon gid */
458 err = get_int(&mesg, &an_int);
459 if (err) goto out;
460 exp.ex_anon_gid= an_int;
461
462 /* fsid */
463 err = get_int(&mesg, &an_int);
464 if (err) goto out;
465 exp.ex_fsid = an_int;
466
467 err = check_export(nd.dentry->d_inode, exp.ex_flags);
468 if (err) goto out;
469 }
470
NeilBrown4f7774c2006-03-27 01:15:03 -0800471 expp = svc_export_lookup(&exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (expp)
NeilBrown4f7774c2006-03-27 01:15:03 -0800473 expp = svc_export_update(&exp, expp);
474 else
475 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 cache_flush();
NeilBrown4f7774c2006-03-27 01:15:03 -0800477 if (expp == NULL)
478 err = -ENOMEM;
479 else
480 exp_put(expp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 out:
J.Bruce Fieldsb009a872006-10-04 02:16:17 -0700482 kfree(exp.ex_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (nd.dentry)
484 path_release(&nd);
NeilBrowncd156542006-04-10 22:55:27 -0700485 out_no_path:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (dom)
487 auth_domain_put(dom);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800488 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return err;
490}
491
492static void exp_flags(struct seq_file *m, int flag, int fsid, uid_t anonu, uid_t anong);
493
494static int svc_export_show(struct seq_file *m,
495 struct cache_detail *cd,
496 struct cache_head *h)
497{
498 struct svc_export *exp ;
499
500 if (h ==NULL) {
501 seq_puts(m, "#path domain(flags)\n");
502 return 0;
503 }
504 exp = container_of(h, struct svc_export, h);
505 seq_path(m, exp->ex_mnt, exp->ex_dentry, " \t\n\\");
506 seq_putc(m, '\t');
507 seq_escape(m, exp->ex_client->name, " \t\n\\");
508 seq_putc(m, '(');
509 if (test_bit(CACHE_VALID, &h->flags) &&
510 !test_bit(CACHE_NEGATIVE, &h->flags))
511 exp_flags(m, exp->ex_flags, exp->ex_fsid,
512 exp->ex_anon_uid, exp->ex_anon_gid);
513 seq_puts(m, ")\n");
514 return 0;
515}
NeilBrown4f7774c2006-03-27 01:15:03 -0800516static int svc_export_match(struct cache_head *a, struct cache_head *b)
517{
518 struct svc_export *orig = container_of(a, struct svc_export, h);
519 struct svc_export *new = container_of(b, struct svc_export, h);
520 return orig->ex_client == new->ex_client &&
521 orig->ex_dentry == new->ex_dentry &&
522 orig->ex_mnt == new->ex_mnt;
523}
524
525static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
526{
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
530 kref_get(&item->ex_client->ref);
531 new->ex_client = item->ex_client;
532 new->ex_dentry = dget(item->ex_dentry);
533 new->ex_mnt = mntget(item->ex_mnt);
J.Bruce Fieldsb009a872006-10-04 02:16:17 -0700534 new->ex_path = NULL;
NeilBrown4f7774c2006-03-27 01:15:03 -0800535}
536
537static void export_update(struct cache_head *cnew, struct cache_head *citem)
538{
539 struct svc_export *new = container_of(cnew, struct svc_export, h);
540 struct svc_export *item = container_of(citem, struct svc_export, h);
541
542 new->ex_flags = item->ex_flags;
543 new->ex_anon_uid = item->ex_anon_uid;
544 new->ex_anon_gid = item->ex_anon_gid;
545 new->ex_fsid = item->ex_fsid;
J.Bruce Fieldsb009a872006-10-04 02:16:17 -0700546 new->ex_path = item->ex_path;
547 item->ex_path = NULL;
NeilBrown4f7774c2006-03-27 01:15:03 -0800548}
549
550static struct cache_head *svc_export_alloc(void)
551{
552 struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
553 if (i)
554 return &i->h;
555 else
556 return NULL;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559struct cache_detail svc_export_cache = {
Bruce Allanf35279d2005-09-06 15:17:08 -0700560 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .hash_size = EXPORT_HASHMAX,
562 .hash_table = export_table,
563 .name = "nfsd.export",
564 .cache_put = svc_export_put,
565 .cache_request = svc_export_request,
566 .cache_parse = svc_export_parse,
567 .cache_show = svc_export_show,
NeilBrown4f7774c2006-03-27 01:15:03 -0800568 .match = svc_export_match,
569 .init = svc_export_init,
570 .update = export_update,
571 .alloc = svc_export_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572};
573
NeilBrown4f7774c2006-03-27 01:15:03 -0800574static struct svc_export *
575svc_export_lookup(struct svc_export *exp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
NeilBrown4f7774c2006-03-27 01:15:03 -0800577 struct cache_head *ch;
578 int hash;
579 hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
580 hash ^= hash_ptr(exp->ex_dentry, EXPORT_HASHBITS);
581 hash ^= hash_ptr(exp->ex_mnt, EXPORT_HASHBITS);
582
583 ch = sunrpc_cache_lookup(&svc_export_cache, &exp->h,
584 hash);
585 if (ch)
586 return container_of(ch, struct svc_export, h);
587 else
588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Adrian Bunk74cae612006-03-27 01:15:10 -0800591static struct svc_export *
NeilBrown4f7774c2006-03-27 01:15:03 -0800592svc_export_update(struct svc_export *new, struct svc_export *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
NeilBrown4f7774c2006-03-27 01:15:03 -0800594 struct cache_head *ch;
595 int hash;
596 hash = hash_ptr(old->ex_client, EXPORT_HASHBITS);
597 hash ^= hash_ptr(old->ex_dentry, EXPORT_HASHBITS);
598 hash ^= hash_ptr(old->ex_mnt, EXPORT_HASHBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
NeilBrown4f7774c2006-03-27 01:15:03 -0800600 ch = sunrpc_cache_update(&svc_export_cache, &new->h,
601 &old->h,
602 hash);
603 if (ch)
604 return container_of(ch, struct svc_export, h);
605 else
606 return NULL;
607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609
Adrian Bunk74cae612006-03-27 01:15:10 -0800610static struct svc_expkey *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611exp_find_key(svc_client *clp, int fsid_type, u32 *fsidv, struct cache_req *reqp)
612{
613 struct svc_expkey key, *ek;
614 int err;
615
616 if (!clp)
617 return NULL;
618
619 key.ek_client = clp;
620 key.ek_fsidtype = fsid_type;
621 memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
622
NeilBrown8d270f72006-03-27 01:15:04 -0800623 ek = svc_expkey_lookup(&key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (ek != NULL)
625 if ((err = cache_check(&svc_expkey_cache, &ek->h, reqp)))
626 ek = ERR_PTR(err);
627 return ek;
628}
629
630static int exp_set_key(svc_client *clp, int fsid_type, u32 *fsidv,
631 struct svc_export *exp)
632{
633 struct svc_expkey key, *ek;
634
635 key.ek_client = clp;
636 key.ek_fsidtype = fsid_type;
637 memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
NeilBrowneab7e2e2006-03-27 01:15:00 -0800638 key.ek_mnt = exp->ex_mnt;
639 key.ek_dentry = exp->ex_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 key.h.expiry_time = NEVER;
641 key.h.flags = 0;
642
NeilBrown8d270f72006-03-27 01:15:04 -0800643 ek = svc_expkey_lookup(&key);
644 if (ek)
645 ek = svc_expkey_update(&key,ek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (ek) {
NeilBrownbaab9352006-03-27 01:15:09 -0800647 cache_put(&ek->h, &svc_expkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
649 }
650 return -ENOMEM;
651}
652
653/*
654 * Find the client's export entry matching xdev/xino.
655 */
656static inline struct svc_expkey *
657exp_get_key(svc_client *clp, dev_t dev, ino_t ino)
658{
659 u32 fsidv[3];
660
661 if (old_valid_dev(dev)) {
662 mk_fsid_v0(fsidv, dev, ino);
663 return exp_find_key(clp, 0, fsidv, NULL);
664 }
665 mk_fsid_v3(fsidv, dev, ino);
666 return exp_find_key(clp, 3, fsidv, NULL);
667}
668
669/*
670 * Find the client's export entry matching fsid
671 */
672static inline struct svc_expkey *
673exp_get_fsid_key(svc_client *clp, int fsid)
674{
675 u32 fsidv[2];
676
677 mk_fsid_v1(fsidv, fsid);
678
679 return exp_find_key(clp, 1, fsidv, NULL);
680}
681
682svc_export *
683exp_get_by_name(svc_client *clp, struct vfsmount *mnt, struct dentry *dentry,
684 struct cache_req *reqp)
685{
686 struct svc_export *exp, key;
687
688 if (!clp)
689 return NULL;
690
691 key.ex_client = clp;
692 key.ex_mnt = mnt;
693 key.ex_dentry = dentry;
694
NeilBrown4f7774c2006-03-27 01:15:03 -0800695 exp = svc_export_lookup(&key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (exp != NULL)
697 switch (cache_check(&svc_export_cache, &exp->h, reqp)) {
698 case 0: break;
699 case -EAGAIN:
700 exp = ERR_PTR(-EAGAIN);
701 break;
702 default:
703 exp = NULL;
704 }
705
706 return exp;
707}
708
709/*
710 * Find the export entry for a given dentry.
711 */
712struct svc_export *
713exp_parent(svc_client *clp, struct vfsmount *mnt, struct dentry *dentry,
714 struct cache_req *reqp)
715{
716 svc_export *exp;
717
718 dget(dentry);
719 exp = exp_get_by_name(clp, mnt, dentry, reqp);
720
721 while (exp == NULL && !IS_ROOT(dentry)) {
722 struct dentry *parent;
723
724 parent = dget_parent(dentry);
725 dput(dentry);
726 dentry = parent;
727 exp = exp_get_by_name(clp, mnt, dentry, reqp);
728 }
729 dput(dentry);
730 return exp;
731}
732
733/*
734 * Hashtable locking. Write locks are placed only by user processes
735 * wanting to modify export information.
736 * Write locking only done in this file. Read locking
737 * needed externally.
738 */
739
740static DECLARE_RWSEM(hash_sem);
741
742void
743exp_readlock(void)
744{
745 down_read(&hash_sem);
746}
747
748static inline void
749exp_writelock(void)
750{
751 down_write(&hash_sem);
752}
753
754void
755exp_readunlock(void)
756{
757 up_read(&hash_sem);
758}
759
760static inline void
761exp_writeunlock(void)
762{
763 up_write(&hash_sem);
764}
765
766static void exp_fsid_unhash(struct svc_export *exp)
767{
768 struct svc_expkey *ek;
769
770 if ((exp->ex_flags & NFSEXP_FSID) == 0)
771 return;
772
773 ek = exp_get_fsid_key(exp->ex_client, exp->ex_fsid);
774 if (ek && !IS_ERR(ek)) {
775 ek->h.expiry_time = get_seconds()-1;
NeilBrownbaab9352006-03-27 01:15:09 -0800776 cache_put(&ek->h, &svc_expkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 svc_expkey_cache.nextcheck = get_seconds();
779}
780
781static int exp_fsid_hash(svc_client *clp, struct svc_export *exp)
782{
783 u32 fsid[2];
784
785 if ((exp->ex_flags & NFSEXP_FSID) == 0)
786 return 0;
787
788 mk_fsid_v1(fsid, exp->ex_fsid);
789 return exp_set_key(clp, 1, fsid, exp);
790}
791
792static int exp_hash(struct auth_domain *clp, struct svc_export *exp)
793{
794 u32 fsid[2];
795 struct inode *inode = exp->ex_dentry->d_inode;
796 dev_t dev = inode->i_sb->s_dev;
797
798 if (old_valid_dev(dev)) {
799 mk_fsid_v0(fsid, dev, inode->i_ino);
800 return exp_set_key(clp, 0, fsid, exp);
801 }
802 mk_fsid_v3(fsid, dev, inode->i_ino);
803 return exp_set_key(clp, 3, fsid, exp);
804}
805
806static void exp_unhash(struct svc_export *exp)
807{
808 struct svc_expkey *ek;
809 struct inode *inode = exp->ex_dentry->d_inode;
810
811 ek = exp_get_key(exp->ex_client, inode->i_sb->s_dev, inode->i_ino);
812 if (ek && !IS_ERR(ek)) {
813 ek->h.expiry_time = get_seconds()-1;
NeilBrownbaab9352006-03-27 01:15:09 -0800814 cache_put(&ek->h, &svc_expkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 svc_expkey_cache.nextcheck = get_seconds();
817}
818
819/*
820 * Export a file system.
821 */
822int
823exp_export(struct nfsctl_export *nxp)
824{
825 svc_client *clp;
826 struct svc_export *exp = NULL;
827 struct svc_export new;
828 struct svc_expkey *fsid_key = NULL;
829 struct nameidata nd;
830 int err;
831
832 /* Consistency check */
833 err = -EINVAL;
834 if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
835 !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
836 goto out;
837
838 dprintk("exp_export called for %s:%s (%x/%ld fl %x).\n",
839 nxp->ex_client, nxp->ex_path,
840 (unsigned)nxp->ex_dev, (long)nxp->ex_ino,
841 nxp->ex_flags);
842
843 /* Try to lock the export table for update */
844 exp_writelock();
845
846 /* Look up client info */
847 if (!(clp = auth_domain_find(nxp->ex_client)))
848 goto out_unlock;
849
850
851 /* Look up the dentry */
852 err = path_lookup(nxp->ex_path, 0, &nd);
853 if (err)
854 goto out_unlock;
855 err = -EINVAL;
856
857 exp = exp_get_by_name(clp, nd.mnt, nd.dentry, NULL);
858
859 /* must make sure there won't be an ex_fsid clash */
860 if ((nxp->ex_flags & NFSEXP_FSID) &&
861 (fsid_key = exp_get_fsid_key(clp, nxp->ex_dev)) &&
862 !IS_ERR(fsid_key) &&
NeilBrowneab7e2e2006-03-27 01:15:00 -0800863 fsid_key->ek_mnt &&
864 (fsid_key->ek_mnt != nd.mnt || fsid_key->ek_dentry != nd.dentry) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto finish;
866
867 if (exp) {
868 /* just a flags/id/fsid update */
869
870 exp_fsid_unhash(exp);
871 exp->ex_flags = nxp->ex_flags;
872 exp->ex_anon_uid = nxp->ex_anon_uid;
873 exp->ex_anon_gid = nxp->ex_anon_gid;
874 exp->ex_fsid = nxp->ex_dev;
875
876 err = exp_fsid_hash(clp, exp);
877 goto finish;
878 }
879
880 err = check_export(nd.dentry->d_inode, nxp->ex_flags);
881 if (err) goto finish;
882
883 err = -ENOMEM;
884
885 dprintk("nfsd: creating export entry %p for client %p\n", exp, clp);
886
887 new.h.expiry_time = NEVER;
888 new.h.flags = 0;
889 new.ex_client = clp;
890 new.ex_mnt = nd.mnt;
891 new.ex_dentry = nd.dentry;
892 new.ex_flags = nxp->ex_flags;
893 new.ex_anon_uid = nxp->ex_anon_uid;
894 new.ex_anon_gid = nxp->ex_anon_gid;
895 new.ex_fsid = nxp->ex_dev;
896
NeilBrown4f7774c2006-03-27 01:15:03 -0800897 exp = svc_export_lookup(&new);
898 if (exp)
899 exp = svc_export_update(&new, exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
NeilBrown4f7774c2006-03-27 01:15:03 -0800901 if (!exp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto finish;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (exp_hash(clp, exp) ||
905 exp_fsid_hash(clp, exp)) {
906 /* failed to create at least one index */
907 exp_do_unexport(exp);
908 cache_flush();
909 err = -ENOMEM;
910 }
911
912finish:
913 if (exp)
914 exp_put(exp);
915 if (fsid_key && !IS_ERR(fsid_key))
NeilBrownbaab9352006-03-27 01:15:09 -0800916 cache_put(&fsid_key->h, &svc_expkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (clp)
918 auth_domain_put(clp);
919 path_release(&nd);
920out_unlock:
921 exp_writeunlock();
922out:
923 return err;
924}
925
926/*
927 * Unexport a file system. The export entry has already
928 * been removed from the client's list of exported fs's.
929 */
930static void
931exp_do_unexport(svc_export *unexp)
932{
933 unexp->h.expiry_time = get_seconds()-1;
934 svc_export_cache.nextcheck = get_seconds();
935 exp_unhash(unexp);
936 exp_fsid_unhash(unexp);
937}
938
939
940/*
941 * unexport syscall.
942 */
943int
944exp_unexport(struct nfsctl_export *nxp)
945{
946 struct auth_domain *dom;
947 svc_export *exp;
948 struct nameidata nd;
949 int err;
950
951 /* Consistency check */
952 if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
953 !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
954 return -EINVAL;
955
956 exp_writelock();
957
958 err = -EINVAL;
959 dom = auth_domain_find(nxp->ex_client);
960 if (!dom) {
961 dprintk("nfsd: unexport couldn't find %s\n", nxp->ex_client);
962 goto out_unlock;
963 }
964
965 err = path_lookup(nxp->ex_path, 0, &nd);
966 if (err)
967 goto out_domain;
968
969 err = -EINVAL;
970 exp = exp_get_by_name(dom, nd.mnt, nd.dentry, NULL);
971 path_release(&nd);
972 if (!exp)
973 goto out_domain;
974
975 exp_do_unexport(exp);
976 exp_put(exp);
977 err = 0;
978
979out_domain:
980 auth_domain_put(dom);
981 cache_flush();
982out_unlock:
983 exp_writeunlock();
984 return err;
985}
986
987/*
988 * Obtain the root fh on behalf of a client.
989 * This could be done in user space, but I feel that it adds some safety
990 * since its harder to fool a kernel module than a user space program.
991 */
992int
993exp_rootfh(svc_client *clp, char *path, struct knfsd_fh *f, int maxsize)
994{
995 struct svc_export *exp;
996 struct nameidata nd;
997 struct inode *inode;
998 struct svc_fh fh;
999 int err;
1000
1001 err = -EPERM;
1002 /* NB: we probably ought to check that it's NUL-terminated */
1003 if (path_lookup(path, 0, &nd)) {
1004 printk("nfsd: exp_rootfh path not found %s", path);
1005 return err;
1006 }
1007 inode = nd.dentry->d_inode;
1008
1009 dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
1010 path, nd.dentry, clp->name,
1011 inode->i_sb->s_id, inode->i_ino);
1012 exp = exp_parent(clp, nd.mnt, nd.dentry, NULL);
1013 if (!exp) {
1014 dprintk("nfsd: exp_rootfh export not found.\n");
1015 goto out;
1016 }
1017
1018 /*
1019 * fh must be initialized before calling fh_compose
1020 */
1021 fh_init(&fh, maxsize);
1022 if (fh_compose(&fh, exp, nd.dentry, NULL))
1023 err = -EINVAL;
1024 else
1025 err = 0;
1026 memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
1027 fh_put(&fh);
1028 exp_put(exp);
1029out:
1030 path_release(&nd);
1031 return err;
1032}
1033
NeilBrowneab7e2e2006-03-27 01:15:00 -08001034struct svc_export *
1035exp_find(struct auth_domain *clp, int fsid_type, u32 *fsidv,
1036 struct cache_req *reqp)
1037{
1038 struct svc_export *exp;
1039 struct svc_expkey *ek = exp_find_key(clp, fsid_type, fsidv, reqp);
1040 if (!ek || IS_ERR(ek))
1041 return ERR_PTR(PTR_ERR(ek));
1042
1043 exp = exp_get_by_name(clp, ek->ek_mnt, ek->ek_dentry, reqp);
NeilBrownbaab9352006-03-27 01:15:09 -08001044 cache_put(&ek->h, &svc_expkey_cache);
NeilBrowneab7e2e2006-03-27 01:15:00 -08001045
1046 if (!exp || IS_ERR(exp))
1047 return ERR_PTR(PTR_ERR(exp));
1048 return exp;
1049}
1050
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * Called when we need the filehandle for the root of the pseudofs,
1054 * for a given NFSv4 client. The root is defined to be the
1055 * export point with fsid==0
1056 */
1057int
1058exp_pseudoroot(struct auth_domain *clp, struct svc_fh *fhp,
1059 struct cache_req *creq)
1060{
NeilBrowneab7e2e2006-03-27 01:15:00 -08001061 struct svc_export *exp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int rv;
1063 u32 fsidv[2];
1064
1065 mk_fsid_v1(fsidv, 0);
1066
J.Bruce Fieldsf38b20c2006-10-04 02:16:09 -07001067 exp = exp_find(clp, 1, fsidv, creq);
1068 if (IS_ERR(exp) && PTR_ERR(exp) == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return nfserr_dropit;
NeilBrowneab7e2e2006-03-27 01:15:00 -08001070 if (exp == NULL)
J.Bruce Fieldsd0ebd9c2006-10-04 02:16:10 -07001071 return nfserr_perm;
NeilBrowneab7e2e2006-03-27 01:15:00 -08001072 else if (IS_ERR(exp))
J.Bruce Fieldsd0ebd9c2006-10-04 02:16:10 -07001073 return nfserrno(PTR_ERR(exp));
1074 rv = fh_compose(fhp, exp, exp->ex_dentry, NULL);
1075 exp_put(exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return rv;
1077}
1078
1079/* Iterator */
1080
1081static void *e_start(struct seq_file *m, loff_t *pos)
Josh Triplett896440d2006-10-02 02:17:50 -07001082 __acquires(svc_export_cache.hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 loff_t n = *pos;
1085 unsigned hash, export;
1086 struct cache_head *ch;
1087
1088 exp_readlock();
1089 read_lock(&svc_export_cache.hash_lock);
1090 if (!n--)
Greg Banksbc6f02e2006-10-02 02:17:49 -07001091 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 hash = n >> 32;
1093 export = n & ((1LL<<32) - 1);
1094
1095
1096 for (ch=export_table[hash]; ch; ch=ch->next)
1097 if (!export--)
1098 return ch;
1099 n &= ~((1LL<<32) - 1);
1100 do {
1101 hash++;
1102 n += 1LL<<32;
1103 } while(hash < EXPORT_HASHMAX && export_table[hash]==NULL);
1104 if (hash >= EXPORT_HASHMAX)
1105 return NULL;
1106 *pos = n+1;
1107 return export_table[hash];
1108}
1109
1110static void *e_next(struct seq_file *m, void *p, loff_t *pos)
1111{
1112 struct cache_head *ch = p;
1113 int hash = (*pos >> 32);
1114
Greg Banksbc6f02e2006-10-02 02:17:49 -07001115 if (p == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 hash = 0;
1117 else if (ch->next == NULL) {
1118 hash++;
1119 *pos += 1LL<<32;
1120 } else {
1121 ++*pos;
1122 return ch->next;
1123 }
1124 *pos &= ~((1LL<<32) - 1);
1125 while (hash < EXPORT_HASHMAX && export_table[hash] == NULL) {
1126 hash++;
1127 *pos += 1LL<<32;
1128 }
1129 if (hash >= EXPORT_HASHMAX)
1130 return NULL;
1131 ++*pos;
1132 return export_table[hash];
1133}
1134
1135static void e_stop(struct seq_file *m, void *p)
Josh Triplett896440d2006-10-02 02:17:50 -07001136 __releases(svc_export_cache.hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 read_unlock(&svc_export_cache.hash_lock);
1139 exp_readunlock();
1140}
1141
1142static struct flags {
1143 int flag;
1144 char *name[2];
1145} expflags[] = {
1146 { NFSEXP_READONLY, {"ro", "rw"}},
1147 { NFSEXP_INSECURE_PORT, {"insecure", ""}},
1148 { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
1149 { NFSEXP_ALLSQUASH, {"all_squash", ""}},
1150 { NFSEXP_ASYNC, {"async", "sync"}},
1151 { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
1152 { NFSEXP_NOHIDE, {"nohide", ""}},
1153 { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
1154 { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
1155 { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
1156#ifdef MSNFS
1157 { NFSEXP_MSNFS, {"msnfs", ""}},
1158#endif
1159 { 0, {"", ""}}
1160};
1161
1162static void exp_flags(struct seq_file *m, int flag, int fsid, uid_t anonu, uid_t anong)
1163{
1164 int first = 0;
1165 struct flags *flg;
1166
1167 for (flg = expflags; flg->flag; flg++) {
1168 int state = (flg->flag & flag)?0:1;
1169 if (*flg->name[state])
1170 seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
1171 }
1172 if (flag & NFSEXP_FSID)
1173 seq_printf(m, "%sfsid=%d", first++?",":"", fsid);
1174 if (anonu != (uid_t)-2 && anonu != (0x10000-2))
1175 seq_printf(m, "%sanonuid=%d", first++?",":"", anonu);
1176 if (anong != (gid_t)-2 && anong != (0x10000-2))
1177 seq_printf(m, "%sanongid=%d", first++?",":"", anong);
1178}
1179
1180static int e_show(struct seq_file *m, void *p)
1181{
1182 struct cache_head *cp = p;
1183 struct svc_export *exp = container_of(cp, struct svc_export, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Greg Banksbc6f02e2006-10-02 02:17:49 -07001185 if (p == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 seq_puts(m, "# Version 1.1\n");
1187 seq_puts(m, "# Path Client(Flags) # IPs\n");
1188 return 0;
1189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 cache_get(&exp->h);
1192 if (cache_check(&svc_export_cache, &exp->h, NULL))
1193 return 0;
NeilBrownbaab9352006-03-27 01:15:09 -08001194 cache_put(&exp->h, &svc_export_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return svc_export_show(m, &svc_export_cache, cp);
1196}
1197
1198struct seq_operations nfs_exports_op = {
1199 .start = e_start,
1200 .next = e_next,
1201 .stop = e_stop,
1202 .show = e_show,
1203};
1204
1205/*
1206 * Add or modify a client.
1207 * Change requests may involve the list of host addresses. The list of
1208 * exports and possibly existing uid maps are left untouched.
1209 */
1210int
1211exp_addclient(struct nfsctl_client *ncp)
1212{
1213 struct auth_domain *dom;
1214 int i, err;
1215
1216 /* First, consistency check. */
1217 err = -EINVAL;
1218 if (! exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
1219 goto out;
1220 if (ncp->cl_naddr > NFSCLNT_ADDRMAX)
1221 goto out;
1222
1223 /* Lock the hashtable */
1224 exp_writelock();
1225
1226 dom = unix_domain_find(ncp->cl_ident);
1227
1228 err = -ENOMEM;
1229 if (!dom)
1230 goto out_unlock;
1231
1232 /* Insert client into hashtable. */
1233 for (i = 0; i < ncp->cl_naddr; i++)
1234 auth_unix_add_addr(ncp->cl_addrlist[i], dom);
1235
1236 auth_unix_forget_old(dom);
1237 auth_domain_put(dom);
1238
1239 err = 0;
1240
1241out_unlock:
1242 exp_writeunlock();
1243out:
1244 return err;
1245}
1246
1247/*
1248 * Delete a client given an identifier.
1249 */
1250int
1251exp_delclient(struct nfsctl_client *ncp)
1252{
1253 int err;
1254 struct auth_domain *dom;
1255
1256 err = -EINVAL;
1257 if (!exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
1258 goto out;
1259
1260 /* Lock the hashtable */
1261 exp_writelock();
1262
1263 dom = auth_domain_find(ncp->cl_ident);
1264 /* just make sure that no addresses work
1265 * and that it will expire soon
1266 */
1267 if (dom) {
1268 err = auth_unix_forget_old(dom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 auth_domain_put(dom);
1270 }
1271
1272 exp_writeunlock();
1273out:
1274 return err;
1275}
1276
1277/*
1278 * Verify that string is non-empty and does not exceed max length.
1279 */
1280static int
1281exp_verify_string(char *cp, int max)
1282{
1283 int i;
1284
1285 for (i = 0; i < max; i++)
1286 if (!cp[i])
1287 return i;
1288 cp[i] = 0;
1289 printk(KERN_NOTICE "nfsd: couldn't validate string %s\n", cp);
1290 return 0;
1291}
1292
1293/*
1294 * Initialize the exports module.
1295 */
1296void
1297nfsd_export_init(void)
1298{
1299 dprintk("nfsd: initializing export module.\n");
1300
1301 cache_register(&svc_export_cache);
1302 cache_register(&svc_expkey_cache);
1303
1304}
1305
1306/*
1307 * Flush exports table - called when last nfsd thread is killed
1308 */
1309void
1310nfsd_export_flush(void)
1311{
1312 exp_writelock();
1313 cache_purge(&svc_expkey_cache);
1314 cache_purge(&svc_export_cache);
1315 exp_writeunlock();
1316}
1317
1318/*
1319 * Shutdown the exports module.
1320 */
1321void
1322nfsd_export_shutdown(void)
1323{
1324
1325 dprintk("nfsd: shutting down export module.\n");
1326
1327 exp_writelock();
1328
1329 if (cache_unregister(&svc_expkey_cache))
1330 printk(KERN_ERR "nfsd: failed to unregister expkey cache\n");
1331 if (cache_unregister(&svc_export_cache))
1332 printk(KERN_ERR "nfsd: failed to unregister export cache\n");
1333 svcauth_unix_purge();
1334
1335 exp_writeunlock();
1336 dprintk("nfsd: export shutdown complete.\n");
1337}