2 * linux/fs/nfs/namespace.c
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
9 #include <linux/config.h>
11 #include <linux/dcache.h>
12 #include <linux/mount.h>
13 #include <linux/namei.h>
14 #include <linux/nfs_fs.h>
15 #include <linux/string.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/vfs.h>
20 #define NFSDBG_FACILITY NFSDBG_VFS
22 static void nfs_expire_automounts(void *list);
24 LIST_HEAD(nfs_automount_list);
25 static DECLARE_WORK(nfs_automount_task, nfs_expire_automounts, &nfs_automount_list);
26 int nfs_mountpoint_expiry_timeout = 500 * HZ;
29 * nfs_path - reconstruct the path given an arbitrary dentry
30 * @base - arbitrary string to prepend to the path
31 * @dentry - pointer to dentry
32 * @buffer - result buffer
33 * @buflen - length of buffer
35 * Helper function for constructing the path from the
36 * root dentry to an arbitrary hashed dentry.
38 * This is mainly for use in figuring out the path on the
39 * server side when automounting on top of an existing partition.
41 char *nfs_path(const char *base, const struct dentry *dentry,
42 char *buffer, ssize_t buflen)
44 char *end = buffer+buflen;
49 spin_lock(&dcache_lock);
50 while (!IS_ROOT(dentry)) {
51 namelen = dentry->d_name.len;
52 buflen -= namelen + 1;
56 memcpy(end, dentry->d_name.name, namelen);
58 dentry = dentry->d_parent;
60 spin_unlock(&dcache_lock);
61 namelen = strlen(base);
62 /* Strip off excess slashes in base string */
63 while (namelen > 0 && base[namelen - 1] == '/')
69 memcpy(end, base, namelen);
72 return ERR_PTR(-ENAMETOOLONG);
76 * nfs_follow_mountpoint - handle crossing a mountpoint on the server
77 * @dentry - dentry of mountpoint
78 * @nd - nameidata info
80 * When we encounter a mountpoint on the server, we want to set up
81 * a mountpoint on the client too, to prevent inode numbers from
82 * colliding, and to allow "df" to work properly.
83 * On NFSv4, we also want to allow for the fact that different
84 * filesystems may be migrated to different servers in a failover
85 * situation, and that different filesystems may want to use
86 * different security flavours.
88 static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
91 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
92 struct dentry *parent;
94 struct nfs_fattr fattr;
97 BUG_ON(IS_ROOT(dentry));
98 dprintk("%s: enter\n", __FUNCTION__);
100 nd->dentry = dget(dentry);
101 if (d_mountpoint(nd->dentry))
103 /* Look it up again */
104 parent = dget_parent(nd->dentry);
105 err = server->rpc_ops->lookup(parent->d_inode, &nd->dentry->d_name, &fh, &fattr);
110 if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL)
111 mnt = nfs_do_refmount(nd->mnt, nd->dentry);
113 mnt = nfs_do_submount(nd->mnt, nd->dentry, &fh, &fattr);
119 err = do_add_mount(mnt, nd, nd->mnt->mnt_flags|MNT_SHRINKABLE, &nfs_automount_list);
129 nd->dentry = dget(mnt->mnt_root);
130 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
132 dprintk("%s: done, returned %d\n", __FUNCTION__, err);
138 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
144 struct inode_operations nfs_mountpoint_inode_operations = {
145 .follow_link = nfs_follow_mountpoint,
146 .getattr = nfs_getattr,
149 struct inode_operations nfs_referral_inode_operations = {
150 .follow_link = nfs_follow_mountpoint,
153 static void nfs_expire_automounts(void *data)
155 struct list_head *list = (struct list_head *)data;
157 mark_mounts_for_expiry(list);
158 if (!list_empty(list))
159 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
162 void nfs_release_automount_timer(void)
164 if (list_empty(&nfs_automount_list)) {
165 cancel_delayed_work(&nfs_automount_task);
166 flush_scheduled_work();
171 * Clone a mountpoint of the appropriate type
173 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server, char *devname,
174 struct nfs_clone_mount *mountdata)
177 struct vfsmount *mnt = NULL;
178 switch (server->rpc_ops->version) {
181 mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
184 mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, mountdata);
188 return vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
193 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
194 * @mnt_parent - mountpoint of parent directory
195 * @dentry - parent directory
196 * @fh - filehandle for new root dentry
197 * @fattr - attributes for new root inode
200 struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
201 const struct dentry *dentry, struct nfs_fh *fh,
202 struct nfs_fattr *fattr)
204 struct nfs_clone_mount mountdata = {
205 .sb = mnt_parent->mnt_sb,
210 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
211 char *page = (char *) __get_free_page(GFP_USER);
214 dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
215 dentry->d_parent->d_name.name,
216 dentry->d_name.name);
219 devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
220 mnt = (struct vfsmount *)devname;
223 mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
225 free_page((unsigned long)page);
227 dprintk("%s: done\n", __FUNCTION__);