2 * linux/fs/nfs/namespace.c
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5 * - Modified by David Howells <dhowells@redhat.com>
10 #include <linux/dcache.h>
11 #include <linux/gfp.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(struct work_struct *work);
24 static LIST_HEAD(nfs_automount_list);
25 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
26 int nfs_mountpoint_expiry_timeout = 500 * HZ;
28 static struct vfsmount *nfs_do_submount(struct super_block *sb,
29 struct dentry *dentry,
31 struct nfs_fattr *fattr);
34 * nfs_path - reconstruct the path given an arbitrary dentry
35 * @base - used to return pointer to the end of devname part of path
36 * @dentry - pointer to dentry
37 * @buffer - result buffer
38 * @buflen - length of buffer
40 * Helper function for constructing the server pathname
41 * by arbitrary hashed dentry.
43 * This is mainly for use in figuring out the path on the
44 * server side when automounting on top of an existing partition
45 * and in generating /proc/mounts and friends.
47 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
59 seq = read_seqbegin(&rename_lock);
62 spin_lock(&dentry->d_lock);
65 namelen = dentry->d_name.len;
66 buflen -= namelen + 1;
70 memcpy(end, dentry->d_name.name, namelen);
72 spin_unlock(&dentry->d_lock);
73 dentry = dentry->d_parent;
75 if (read_seqretry(&rename_lock, seq)) {
76 spin_unlock(&dentry->d_lock);
82 spin_unlock(&dentry->d_lock);
89 base = dentry->d_fsdata;
91 spin_unlock(&dentry->d_lock);
96 namelen = strlen(base);
97 /* Strip off excess slashes in base string */
98 while (namelen > 0 && base[namelen - 1] == '/')
102 spin_lock(&dentry->d_lock);
107 memcpy(end, base, namelen);
108 spin_unlock(&dentry->d_lock);
112 spin_lock(&dentry->d_lock);
114 if (read_seqretry(&rename_lock, seq))
117 return ERR_PTR(-ENAMETOOLONG);
121 * nfs_d_automount - Handle crossing a mountpoint on the server
122 * @path - The mountpoint
124 * When we encounter a mountpoint on the server, we want to set up
125 * a mountpoint on the client too, to prevent inode numbers from
126 * colliding, and to allow "df" to work properly.
127 * On NFSv4, we also want to allow for the fact that different
128 * filesystems may be migrated to different servers in a failover
129 * situation, and that different filesystems may want to use
130 * different security flavours.
132 struct vfsmount *nfs_d_automount(struct path *path)
134 struct vfsmount *mnt;
135 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
136 struct dentry *parent;
137 struct nfs_fh *fh = NULL;
138 struct nfs_fattr *fattr = NULL;
141 dprintk("--> nfs_d_automount()\n");
143 mnt = ERR_PTR(-ESTALE);
144 if (IS_ROOT(path->dentry))
147 mnt = ERR_PTR(-ENOMEM);
148 fh = nfs_alloc_fhandle();
149 fattr = nfs_alloc_fattr();
150 if (fh == NULL || fattr == NULL)
153 dprintk("%s: enter\n", __func__);
155 /* Look it up again to get its attributes */
156 parent = dget_parent(path->dentry);
157 err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
158 &path->dentry->d_name,
166 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
167 mnt = nfs_do_refmount(path->mnt->mnt_sb, path->dentry);
169 mnt = nfs_do_submount(path->mnt->mnt_sb, path->dentry, fh, fattr);
173 dprintk("%s: done, success\n", __func__);
174 mntget(mnt); /* prevent immediate expiration */
175 mnt_set_expiry(mnt, &nfs_automount_list);
176 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
179 nfs_free_fattr(fattr);
180 nfs_free_fhandle(fh);
182 dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
186 const struct inode_operations nfs_mountpoint_inode_operations = {
187 .getattr = nfs_getattr,
190 const struct inode_operations nfs_referral_inode_operations = {
193 static void nfs_expire_automounts(struct work_struct *work)
195 struct list_head *list = &nfs_automount_list;
197 mark_mounts_for_expiry(list);
198 if (!list_empty(list))
199 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
202 void nfs_release_automount_timer(void)
204 if (list_empty(&nfs_automount_list))
205 cancel_delayed_work(&nfs_automount_task);
209 * Clone a mountpoint of the appropriate type
211 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
213 struct nfs_clone_mount *mountdata)
216 struct vfsmount *mnt = ERR_PTR(-EINVAL);
217 switch (server->nfs_client->rpc_ops->version) {
220 mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
223 mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
227 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
232 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
233 * @sb - superblock of parent directory
234 * @dentry - parent directory
235 * @fh - filehandle for new root dentry
236 * @fattr - attributes for new root inode
239 static struct vfsmount *nfs_do_submount(struct super_block *sb,
240 struct dentry *dentry,
242 struct nfs_fattr *fattr)
244 struct nfs_clone_mount mountdata = {
250 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
251 char *page = (char *) __get_free_page(GFP_USER);
254 dprintk("--> nfs_do_submount()\n");
256 dprintk("%s: submounting on %s/%s\n", __func__,
257 dentry->d_parent->d_name.name,
258 dentry->d_name.name);
261 devname = nfs_devname(dentry, page, PAGE_SIZE);
262 mnt = (struct vfsmount *)devname;
265 mnt = nfs_do_clone_mount(NFS_SB(sb), devname, &mountdata);
267 free_page((unsigned long)page);
269 dprintk("%s: done\n", __func__);
271 dprintk("<-- nfs_do_submount() = %p\n", mnt);