1 #define MSNFS /* HACK HACK */
3 * linux/fs/nfsd/export.c
5 * NFS exporting and validation.
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.
14 * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
17 #include <linux/unistd.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
21 #include <linux/seq_file.h>
22 #include <linux/smp_lock.h>
24 #include <linux/sunrpc/svc.h>
25 #include <linux/nfsd/nfsd.h>
26 #include <linux/nfsd/nfsfh.h>
27 #include <linux/nfsd/syscall.h>
28 #include <linux/lockd/bind.h>
30 #define NFSDDBG_FACILITY NFSDDBG_EXPORT
31 #define NFSD_PARANOIA 1
33 typedef struct svc_client svc_client;
34 typedef struct svc_export svc_export;
36 static svc_export * exp_parent(svc_client *clp, kdev_t dev,
37 struct dentry *dentry);
38 static svc_export * exp_child(svc_client *clp, kdev_t dev,
39 struct dentry *dentry);
40 static void exp_unexport_all(svc_client *clp);
41 static void exp_do_unexport(svc_export *unexp);
42 static svc_client * exp_getclientbyname(char *name);
43 static void exp_freeclient(svc_client *clp);
44 static void exp_unhashclient(svc_client *clp);
45 static int exp_verify_string(char *cp, int max);
47 #define CLIENT_HASHBITS 6
48 #define CLIENT_HASHMAX (1 << CLIENT_HASHBITS)
49 #define CLIENT_HASHMASK (CLIENT_HASHMAX - 1)
50 #define CLIENT_HASH(a) \
51 ((((a)>>24) ^ ((a)>>16) ^ ((a)>>8) ^(a)) & CLIENT_HASHMASK)
52 /* XXX: is this adequate for 32bit kdev_t ? */
53 #define EXPORT_HASH(dev) ((dev) & (NFSCLNT_EXPMAX - 1))
54 #define EXPORT_FSID_HASH(fsid) ((fsid) & (NFSCLNT_EXPMAX - 1))
57 struct svc_clnthash * h_next;
58 struct in_addr h_addr;
59 struct svc_client * h_client;
61 static struct svc_clnthash * clnt_hash[CLIENT_HASHMAX];
62 static svc_client * clients;
66 static int hash_count;
67 static DECLARE_WAIT_QUEUE_HEAD( hash_wait );
70 * Find the client's export entry matching xdev/xino.
73 exp_get(svc_client *clp, kdev_t dev, ino_t ino)
75 struct list_head *head, *p;
80 head = &clp->cl_export[EXPORT_HASH(dev)];
81 list_for_each(p, head) {
82 svc_export *exp = list_entry(p, svc_export, ex_hash);
83 if (exp->ex_ino == ino && exp->ex_dev == dev)
91 * Find the client's export entry matching fsid
94 exp_get_fsid(svc_client *clp, int fsid)
96 struct list_head *head, *p;
101 head = &clp->cl_expfsid[EXPORT_FSID_HASH(fsid)];
102 list_for_each(p, head) {
103 svc_export *exp = list_entry(p, svc_export, ex_fsid_hash);
104 if (exp->ex_fsid == fsid)
111 * Find the export entry for a given dentry. <gam3@acm.org>
114 exp_parent(svc_client *clp, kdev_t dev, struct dentry *dentry)
116 struct list_head *head = &clp->cl_export[EXPORT_HASH(dev)];
119 list_for_each(p,head) {
120 svc_export *exp = list_entry(p, svc_export, ex_hash);
121 if (is_subdir(dentry, exp->ex_dentry))
128 * Find the child export entry for a given fs. This function is used
129 * only by the export syscall to keep the export tree consistent.
133 exp_child(svc_client *clp, kdev_t dev, struct dentry *dentry)
135 struct list_head *head = &clp->cl_export[EXPORT_HASH(dev)];
139 list_for_each(p, head) {
140 svc_export *exp = list_entry(p, svc_export, ex_hash);
141 struct dentry *ndentry = exp->ex_dentry;
143 if (ndentry && is_subdir(ndentry->d_parent, dentry))
149 /* Update parent pointers of all exports */
150 static void exp_change_parents(svc_client *clp, svc_export *old, svc_export *new)
152 struct list_head *head = &clp->cl_list;
155 list_for_each(p, head) {
156 svc_export *exp = list_entry(p, svc_export, ex_list);
157 if (exp->ex_parent == old)
158 exp->ex_parent = new;
162 static void exp_fsid_unhash(struct svc_export *exp)
165 if ((exp->ex_flags & NFSEXP_FSID) == 0)
168 list_del_init(&exp->ex_fsid_hash);
171 static void exp_fsid_hash(struct svc_client *clp, struct svc_export *exp)
173 struct list_head *head;
175 if ((exp->ex_flags & NFSEXP_FSID) == 0)
177 head = clp->cl_expfsid + EXPORT_FSID_HASH(exp->ex_fsid);
178 list_add(&exp->ex_fsid_hash, head);
182 * Export a file system.
185 exp_export(struct nfsctl_export *nxp)
188 svc_export *exp = NULL, *parent;
189 svc_export *fsid_exp;
191 struct inode *inode = NULL;
196 /* Consistency check */
198 if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
199 !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
202 dprintk("exp_export called for %s:%s (%x/%ld fl %x).\n",
203 nxp->ex_client, nxp->ex_path,
204 nxp->ex_dev, (long) nxp->ex_ino, nxp->ex_flags);
206 /* Try to lock the export table for update */
207 if ((err = exp_writelock()) < 0)
210 /* Look up client info */
212 if (!(clp = exp_getclientbyname(nxp->ex_client)))
216 /* Look up the dentry */
218 if (path_init(nxp->ex_path, LOOKUP_POSITIVE, &nd))
219 err = path_walk(nxp->ex_path, &nd);
223 inode = nd.dentry->d_inode;
228 exp = exp_get(clp, dev, ino);
230 /* must make sure there wont be an ex_fsid clash */
231 if ((nxp->ex_flags & NFSEXP_FSID) &&
232 (fsid_exp = exp_get_fsid(clp, nxp->ex_dev)) &&
237 /* just a flags/id/fsid update */
239 exp_fsid_unhash(exp);
240 exp->ex_flags = nxp->ex_flags;
241 exp->ex_anon_uid = nxp->ex_anon_uid;
242 exp->ex_anon_gid = nxp->ex_anon_gid;
243 exp->ex_fsid = nxp->ex_dev;
244 exp_fsid_hash(clp, exp);
249 /* We currently export only dirs and regular files.
250 * This is what umountd does.
253 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
257 /* There are two requirements on a filesystem to be exportable.
258 * 1: We must be able to identify the filesystem from a number.
259 * either a device number (so FS_REQUIRES_DEV needed)
260 * or an FSID number (so NFSEXP_FSID needed).
261 * 2: We must be able to find an inode from a filehandle.
262 * either using fh_to_dentry (prefered)
263 * or using read_inode (the hack).
265 if (!((inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV)
266 || (nxp->ex_flags & NFSEXP_FSID))
268 (inode->i_sb->s_op->read_inode == NULL
269 && inode->i_sb->s_op->fh_to_dentry == NULL)) {
270 dprintk("exp_export: export of invalid fs type.\n");
274 if ((parent = exp_child(clp, dev, nd.dentry)) != NULL) {
275 dprintk("exp_export: export not valid (Rule 3).\n");
278 /* Is this is a sub-export, must be a proper subset of FS */
279 if ((parent = exp_parent(clp, dev, nd.dentry)) != NULL) {
280 dprintk("exp_export: sub-export not valid (Rule 2).\n");
285 if (!(exp = kmalloc(sizeof(*exp), GFP_USER)))
287 dprintk("nfsd: created export entry %p for client %p\n", exp, clp);
289 strcpy(exp->ex_path, nxp->ex_path);
290 exp->ex_client = clp;
291 exp->ex_parent = parent;
292 exp->ex_dentry = dget(nd.dentry);
293 exp->ex_mnt = mntget(nd.mnt);
294 exp->ex_flags = nxp->ex_flags;
297 exp->ex_anon_uid = nxp->ex_anon_uid;
298 exp->ex_anon_gid = nxp->ex_anon_gid;
299 exp->ex_fsid = nxp->ex_dev;
302 /* Update parent pointers of all exports */
304 exp_change_parents(clp, parent, exp);
306 list_add(&exp->ex_hash, clp->cl_export + EXPORT_HASH(dev));
307 list_add_tail(&exp->ex_list, &clp->cl_list);
309 exp_fsid_hash(clp, exp);
322 * Unexport a file system. The export entry has already
323 * been removed from the client's list of exported fs's.
326 exp_do_unexport(svc_export *unexp)
328 struct dentry *dentry;
329 struct vfsmount *mnt;
332 list_del(&unexp->ex_hash);
333 list_del(&unexp->ex_list);
334 exp_fsid_unhash(unexp);
336 exp_change_parents(unexp->ex_client, unexp, unexp->ex_parent);
338 dentry = unexp->ex_dentry;
340 inode = dentry->d_inode;
341 if (unexp->ex_dev != inode->i_dev || unexp->ex_ino != inode->i_ino)
342 printk(KERN_WARNING "nfsd: bad dentry in unexport!\n");
350 * Revoke all exports for a given client.
351 * This may look very awkward, but we have to do it this way in order
352 * to avoid race conditions (aka mind the parent pointer).
355 exp_unexport_all(svc_client *clp)
357 struct list_head *p = &clp->cl_list;
359 dprintk("unexporting all fs's for clnt %p\n", clp);
361 while (!list_empty(p)) {
362 svc_export *exp = list_entry(p->next, svc_export, ex_list);
363 exp_do_unexport(exp);
371 exp_unexport(struct nfsctl_export *nxp)
376 /* Consistency check */
377 if (!exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
380 if ((err = exp_writelock()) < 0)
384 clp = exp_getclientbyname(nxp->ex_client);
386 svc_export *exp = exp_get(clp, nxp->ex_dev, nxp->ex_ino);
388 exp_do_unexport(exp);
399 * Obtain the root fh on behalf of a client.
400 * This could be done in user space, but I feel that it adds some safety
401 * since its harder to fool a kernel module than a user space program.
404 exp_rootfh(struct svc_client *clp, kdev_t dev, ino_t ino,
405 char *path, struct knfsd_fh *f, int maxsize)
407 struct svc_export *exp;
415 if (path_init(path, LOOKUP_POSITIVE, &nd) &&
416 path_walk(path, &nd)) {
417 printk("nfsd: exp_rootfh path not found %s", path);
420 dev = nd.dentry->d_inode->i_dev;
421 ino = nd.dentry->d_inode->i_ino;
423 dprintk("nfsd: exp_rootfh(%s [%p] %s:%x/%ld)\n",
424 path, nd.dentry, clp->cl_ident, dev, (long) ino);
425 exp = exp_parent(clp, dev, nd.dentry);
427 dprintk("nfsd: exp_rootfh(%s:%x/%ld)\n",
428 clp->cl_ident, dev, (long) ino);
429 if ((exp = exp_get(clp, dev, ino))) {
430 nd.mnt = mntget(exp->ex_mnt);
431 nd.dentry = dget(exp->ex_dentry);
435 dprintk("nfsd: exp_rootfh export not found.\n");
439 inode = nd.dentry->d_inode;
441 printk("exp_rootfh: Aieee, NULL d_inode\n");
444 if (inode->i_dev != dev || inode->i_ino != ino) {
445 printk("exp_rootfh: Aieee, ino/dev mismatch\n");
446 printk("exp_rootfh: arg[dev(%x):ino(%ld)]"
447 " inode[dev(%x):ino(%ld)]\n",
448 dev, (long) ino, inode->i_dev, (long) inode->i_ino);
452 * fh must be initialized before calling fh_compose
454 fh_init(&fh, maxsize);
455 if (fh_compose(&fh, exp, dget(nd.dentry), NULL))
459 memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
469 * Hashtable locking. Write locks are placed only by user processes
470 * wanting to modify export information.
475 while (hash_lock || want_lock)
476 sleep_on(&hash_wait);
484 if (!hash_count && !hash_lock) {
490 current->sigpending = 0;
492 while (hash_count || hash_lock) {
493 interruptible_sleep_on(&hash_wait);
494 if (signal_pending(current))
499 /* restore the task's signals */
500 spin_lock_irq(¤t->sigmask_lock);
501 recalc_sigpending(current);
502 spin_unlock_irq(¤t->sigmask_lock);
504 if (!hash_count && !hash_lock)
512 if (!hash_count && !hash_lock)
513 printk(KERN_WARNING "exp_unlock: not locked!\n");
522 * Find a valid client given an inet address. We always move the most
523 * recently used client to the front of the hash chain to speed up
525 * Locking against other processes is the responsibility of the caller.
528 exp_getclient(struct sockaddr_in *sin)
530 struct svc_clnthash **hp, **head, *tmp;
531 unsigned long addr = sin->sin_addr.s_addr;
533 head = &clnt_hash[CLIENT_HASH(addr)];
535 for (hp = head; (tmp = *hp) != NULL; hp = &(tmp->h_next)) {
536 if (tmp->h_addr.s_addr == addr) {
537 /* Move client to the front */
544 return tmp->h_client;
552 * Find a client given its identifier.
555 exp_getclientbyname(char *ident)
559 for (clp = clients; clp; clp = clp->cl_next) {
560 if (!strcmp(clp->cl_ident, ident))
568 static void *e_start(struct seq_file *m, loff_t *pos)
571 unsigned client, export;
580 export = n & ((1LL<<32) - 1);
581 for (clp = clients; client && clp; clp = clp->cl_next, client--)
585 list_for_each(p, &clp->cl_list)
587 return list_entry(p, svc_export, ex_list);
588 n &= ~((1LL<<32) - 1);
592 } while(clp && list_empty(&clp->cl_list));
596 return list_entry(clp->cl_list.next, svc_export, ex_list);
599 static void *e_next(struct seq_file *m, void *p, loff_t *pos)
606 else if (exp->ex_list.next == &exp->ex_client->cl_list) {
607 clp = exp->ex_client->cl_next;
611 return list_entry(exp->ex_list.next, svc_export, ex_list);
613 *pos &= ~((1LL<<32) - 1);
614 while (clp && list_empty(&clp->cl_list)) {
621 return list_entry(clp->cl_list.next, svc_export, ex_list);
624 static void e_stop(struct seq_file *m, void *p)
634 { NFSEXP_READONLY, {"ro", "rw"}},
635 { NFSEXP_INSECURE_PORT, {"insecure", ""}},
636 { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
637 { NFSEXP_ALLSQUASH, {"all_squash", ""}},
638 { NFSEXP_ASYNC, {"async", "sync"}},
639 { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
640 { NFSEXP_UIDMAP, {"uidmap", ""}},
641 { NFSEXP_KERBEROS, { "kerberos", ""}},
642 { NFSEXP_SUNSECURE, { "sunsecure", ""}},
643 { NFSEXP_NOHIDE, {"nohide", ""}},
644 { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
645 { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
647 { NFSEXP_MSNFS, {"msnfs", ""}},
652 static void exp_flags(struct seq_file *m, int flag, int fsid)
657 for (flg = expflags; flg->flag; flg++) {
658 int state = (flg->flag & flag)?0:1;
659 if (*flg->name[state])
660 seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
662 if (flag & NFSEXP_FSID)
663 seq_printf(m, "%sfsid=%d", first++?",":"", fsid);
666 static inline void mangle(struct seq_file *m, const char *s)
668 seq_escape(m, s, " \t\n\\");
671 static int e_show(struct seq_file *m, void *p)
673 struct svc_export *exp = p;
674 struct svc_client *clp;
677 if (p == (void *)1) {
678 seq_puts(m, "# Version 1.1\n");
679 seq_puts(m, "# Path Client(Flags) # IPs\n");
683 clp = exp->ex_client;
685 mangle(m, exp->ex_path);
687 mangle(m, clp->cl_ident);
689 exp_flags(m, exp->ex_flags, exp->ex_fsid);
691 for (j = 0; j < clp->cl_naddr; j++) {
692 struct svc_clnthash **hp, **head, *tmp;
693 struct in_addr addr = clp->cl_addr[j];
695 head = &clnt_hash[CLIENT_HASH(addr.s_addr)];
696 for (hp = head; (tmp = *hp) != NULL; hp = &(tmp->h_next)) {
697 if (tmp->h_addr.s_addr == addr.s_addr)
703 if (tmp->h_client != clp)
705 seq_printf(m, "%d.%d.%d.%d",
706 htonl(addr.s_addr) >> 24 & 0xff,
707 htonl(addr.s_addr) >> 16 & 0xff,
708 htonl(addr.s_addr) >> 8 & 0xff,
709 htonl(addr.s_addr) >> 0 & 0xff);
710 if (tmp->h_client != clp)
718 struct seq_operations nfs_exports_op = {
726 * Add or modify a client.
727 * Change requests may involve the list of host addresses. The list of
728 * exports and possibly existing uid maps are left untouched.
731 exp_addclient(struct nfsctl_client *ncp)
733 struct svc_clnthash * ch[NFSCLNT_ADDRMAX];
735 int i, err, change = 0, ilen;
737 /* First, consistency check. */
739 if (!(ilen = exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX)))
741 if (ncp->cl_naddr > NFSCLNT_ADDRMAX)
744 /* Lock the hashtable */
745 if ((err = exp_writelock()) < 0)
748 /* First check if this is a change request for a client. */
749 for (clp = clients; clp; clp = clp->cl_next)
750 if (!strcmp(clp->cl_ident, ncp->cl_ident))
757 if (!(clp = kmalloc(sizeof(*clp), GFP_KERNEL)))
759 memset(clp, 0, sizeof(*clp));
760 for (i = 0; i < NFSCLNT_EXPMAX; i++) {
761 INIT_LIST_HEAD(&clp->cl_export[i]);
762 INIT_LIST_HEAD(&clp->cl_expfsid[i]);
764 INIT_LIST_HEAD(&clp->cl_list);
766 dprintk("created client %s (%p)\n", ncp->cl_ident, clp);
768 strcpy(clp->cl_ident, ncp->cl_ident);
769 clp->cl_idlen = ilen;
772 /* Allocate hash buckets */
773 for (i = 0; i < ncp->cl_naddr; i++) {
774 ch[i] = kmalloc(sizeof(struct svc_clnthash), GFP_KERNEL);
784 /* Copy addresses. */
785 for (i = 0; i < ncp->cl_naddr; i++) {
786 clp->cl_addr[i] = ncp->cl_addrlist[i];
788 clp->cl_naddr = ncp->cl_naddr;
790 /* Remove old client hash entries. */
792 exp_unhashclient(clp);
794 /* Insert client into hashtable. */
795 for (i = 0; i < ncp->cl_naddr; i++) {
796 struct in_addr addr = clp->cl_addr[i];
799 hash = CLIENT_HASH(addr.s_addr);
800 ch[i]->h_client = clp;
801 ch[i]->h_addr = addr;
802 ch[i]->h_next = clnt_hash[hash];
803 clnt_hash[hash] = ch[i];
807 clp->cl_next = clients;
819 * Delete a client given an identifier.
822 exp_delclient(struct nfsctl_client *ncp)
824 svc_client **clpp, *clp;
828 if (!exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
831 /* Lock the hashtable */
832 if ((err = exp_writelock()) < 0)
836 for (clpp = &clients; (clp = *clpp); clpp = &(clp->cl_next))
837 if (!strcmp(ncp->cl_ident, clp->cl_ident))
841 *clpp = clp->cl_next;
852 * Free a client. The caller has already removed it from the client list.
855 exp_freeclient(svc_client *clp)
857 exp_unhashclient(clp);
859 /* umap_free(&(clp->cl_umap)); */
860 exp_unexport_all(clp);
861 nfsd_lockd_unexport(clp);
866 * Remove client from hashtable. We first collect all hashtable
867 * entries and free them in one go.
868 * The hash table must be writelocked by the caller.
871 exp_unhashclient(svc_client *clp)
873 struct svc_clnthash **hpp, *hp, *ch[NFSCLNT_ADDRMAX];
878 for (i = 0, count = 0; i < CLIENT_HASHMAX && !err; i++) {
880 while ((hp = *hpp) && !err) {
881 if (hp->h_client == clp) {
884 err = (count >= NFSCLNT_ADDRMAX);
890 if (count != clp->cl_naddr)
891 printk(KERN_WARNING "nfsd: bad address count in freeclient!\n");
894 for (i = 0; i < count; i++)
899 * Lockd is shutting down and tells us to unregister all clients
904 struct svc_client *clp;
907 for (clp = clients; clp; clp = clp->cl_next)
908 nfsd_lockd_unexport(clp);
913 * Verify that string is non-empty and does not exceed max length.
916 exp_verify_string(char *cp, int max)
920 for (i = 0; i < max; i++)
924 printk(KERN_NOTICE "nfsd: couldn't validate string %s\n", cp);
929 * Initialize the exports module.
932 nfsd_export_init(void)
936 dprintk("nfsd: initializing export module.\n");
938 for (i = 0; i < CLIENT_HASHMAX; i++)
945 * Shutdown the exports module.
948 nfsd_export_shutdown(void)
952 dprintk("nfsd: shutting down export module.\n");
954 if (exp_writelock() < 0) {
955 printk(KERN_WARNING "Weird: hashtable locked in exp_shutdown");
958 for (i = 0; i < CLIENT_HASHMAX; i++) {
960 exp_freeclient(clnt_hash[i]->h_client);
962 clients = NULL; /* we may be restarted before the module unloads */
965 dprintk("nfsd: export shutdown complete.\n");