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/module.h>
11 #include <linux/dcache.h>
12 #include <linux/gfp.h>
13 #include <linux/mount.h>
14 #include <linux/namei.h>
15 #include <linux/nfs_fs.h>
16 #include <linux/string.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/vfs.h>
19 #include <linux/sunrpc/gss_api.h>
22 #define NFSDBG_FACILITY NFSDBG_VFS
24 static void nfs_expire_automounts(struct work_struct *work);
26 static LIST_HEAD(nfs_automount_list);
27 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
28 int nfs_mountpoint_expiry_timeout = 500 * HZ;
31 * nfs_path - reconstruct the path given an arbitrary dentry
32 * @base - used to return pointer to the end of devname part of path
33 * @dentry - pointer to dentry
34 * @buffer - result buffer
35 * @buflen - length of buffer
37 * Helper function for constructing the server pathname
38 * by arbitrary hashed dentry.
40 * This is mainly for use in figuring out the path on the
41 * server side when automounting on top of an existing partition
42 * and in generating /proc/mounts and friends.
44 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
56 seq = read_seqbegin(&rename_lock);
59 spin_lock(&dentry->d_lock);
62 namelen = dentry->d_name.len;
63 buflen -= namelen + 1;
67 memcpy(end, dentry->d_name.name, namelen);
69 spin_unlock(&dentry->d_lock);
70 dentry = dentry->d_parent;
72 if (read_seqretry(&rename_lock, seq)) {
73 spin_unlock(&dentry->d_lock);
79 spin_unlock(&dentry->d_lock);
86 base = dentry->d_fsdata;
88 spin_unlock(&dentry->d_lock);
93 namelen = strlen(base);
94 /* Strip off excess slashes in base string */
95 while (namelen > 0 && base[namelen - 1] == '/')
99 spin_unlock(&dentry->d_lock);
104 memcpy(end, base, namelen);
105 spin_unlock(&dentry->d_lock);
109 spin_unlock(&dentry->d_lock);
111 if (read_seqretry(&rename_lock, seq))
114 return ERR_PTR(-ENAMETOOLONG);
116 EXPORT_SYMBOL_GPL(nfs_path);
119 * nfs_d_automount - Handle crossing a mountpoint on the server
120 * @path - The mountpoint
122 * When we encounter a mountpoint on the server, we want to set up
123 * a mountpoint on the client too, to prevent inode numbers from
124 * colliding, and to allow "df" to work properly.
125 * On NFSv4, we also want to allow for the fact that different
126 * filesystems may be migrated to different servers in a failover
127 * situation, and that different filesystems may want to use
128 * different security flavours.
130 struct vfsmount *nfs_d_automount(struct path *path)
132 struct vfsmount *mnt;
133 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
134 struct nfs_fh *fh = NULL;
135 struct nfs_fattr *fattr = NULL;
137 dprintk("--> nfs_d_automount()\n");
139 mnt = ERR_PTR(-ESTALE);
140 if (IS_ROOT(path->dentry))
143 mnt = ERR_PTR(-ENOMEM);
144 fh = nfs_alloc_fhandle();
145 fattr = nfs_alloc_fattr();
146 if (fh == NULL || fattr == NULL)
149 dprintk("%s: enter\n", __func__);
151 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
155 dprintk("%s: done, success\n", __func__);
156 mntget(mnt); /* prevent immediate expiration */
157 mnt_set_expiry(mnt, &nfs_automount_list);
158 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
161 nfs_free_fattr(fattr);
162 nfs_free_fhandle(fh);
165 dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
167 dprintk("<-- %s() = %p\n", __func__, mnt);
171 const struct inode_operations nfs_mountpoint_inode_operations = {
172 .getattr = nfs_getattr,
175 const struct inode_operations nfs_referral_inode_operations = {
178 static void nfs_expire_automounts(struct work_struct *work)
180 struct list_head *list = &nfs_automount_list;
182 mark_mounts_for_expiry(list);
183 if (!list_empty(list))
184 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
187 void nfs_release_automount_timer(void)
189 if (list_empty(&nfs_automount_list))
190 cancel_delayed_work(&nfs_automount_task);
194 * Clone a mountpoint of the appropriate type
196 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
198 struct nfs_clone_mount *mountdata)
200 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
204 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
205 * @dentry - parent directory
206 * @fh - filehandle for new root dentry
207 * @fattr - attributes for new root inode
208 * @authflavor - security flavor to use when performing the mount
211 struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
212 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
214 struct nfs_clone_mount mountdata = {
219 .authflavor = authflavor,
221 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
222 char *page = (char *) __get_free_page(GFP_USER);
225 dprintk("--> nfs_do_submount()\n");
227 dprintk("%s: submounting on %s/%s\n", __func__,
228 dentry->d_parent->d_name.name,
229 dentry->d_name.name);
232 devname = nfs_devname(dentry, page, PAGE_SIZE);
233 mnt = (struct vfsmount *)devname;
236 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
238 free_page((unsigned long)page);
240 dprintk("%s: done\n", __func__);
242 dprintk("<-- nfs_do_submount() = %p\n", mnt);
245 EXPORT_SYMBOL_GPL(nfs_do_submount);
247 struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
248 struct nfs_fh *fh, struct nfs_fattr *fattr)
251 struct dentry *parent = dget_parent(dentry);
253 /* Look it up again to get its attributes */
254 err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
259 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
261 EXPORT_SYMBOL_GPL(nfs_submount);