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 dentry
*dentry
,
30 struct nfs_fattr
*fattr
);
33 * nfs_path - reconstruct the path given an arbitrary dentry
34 * @base - used to return pointer to the end of devname part of path
35 * @dentry - pointer to dentry
36 * @buffer - result buffer
37 * @buflen - length of buffer
39 * Helper function for constructing the server pathname
40 * by arbitrary hashed dentry.
42 * This is mainly for use in figuring out the path on the
43 * server side when automounting on top of an existing partition
44 * and in generating /proc/mounts and friends.
46 char *nfs_path(char **p
, struct dentry
*dentry
, char *buffer
, ssize_t buflen
)
58 seq
= read_seqbegin(&rename_lock
);
61 spin_lock(&dentry
->d_lock
);
64 namelen
= dentry
->d_name
.len
;
65 buflen
-= namelen
+ 1;
69 memcpy(end
, dentry
->d_name
.name
, namelen
);
71 spin_unlock(&dentry
->d_lock
);
72 dentry
= dentry
->d_parent
;
74 if (read_seqretry(&rename_lock
, seq
)) {
75 spin_unlock(&dentry
->d_lock
);
81 spin_unlock(&dentry
->d_lock
);
88 base
= dentry
->d_fsdata
;
90 spin_unlock(&dentry
->d_lock
);
95 namelen
= strlen(base
);
96 /* Strip off excess slashes in base string */
97 while (namelen
> 0 && base
[namelen
- 1] == '/')
101 spin_unlock(&dentry
->d_lock
);
106 memcpy(end
, base
, namelen
);
107 spin_unlock(&dentry
->d_lock
);
111 spin_unlock(&dentry
->d_lock
);
113 if (read_seqretry(&rename_lock
, seq
))
116 return ERR_PTR(-ENAMETOOLONG
);
120 * nfs_d_automount - Handle crossing a mountpoint on the server
121 * @path - The mountpoint
123 * When we encounter a mountpoint on the server, we want to set up
124 * a mountpoint on the client too, to prevent inode numbers from
125 * colliding, and to allow "df" to work properly.
126 * On NFSv4, we also want to allow for the fact that different
127 * filesystems may be migrated to different servers in a failover
128 * situation, and that different filesystems may want to use
129 * different security flavours.
131 struct vfsmount
*nfs_d_automount(struct path
*path
)
133 struct vfsmount
*mnt
;
134 struct nfs_server
*server
= NFS_SERVER(path
->dentry
->d_inode
);
135 struct dentry
*parent
;
136 struct nfs_fh
*fh
= NULL
;
137 struct nfs_fattr
*fattr
= NULL
;
140 dprintk("--> nfs_d_automount()\n");
142 mnt
= ERR_PTR(-ESTALE
);
143 if (IS_ROOT(path
->dentry
))
146 mnt
= ERR_PTR(-ENOMEM
);
147 fh
= nfs_alloc_fhandle();
148 fattr
= nfs_alloc_fattr();
149 if (fh
== NULL
|| fattr
== NULL
)
152 dprintk("%s: enter\n", __func__
);
154 /* Look it up again to get its attributes */
155 parent
= dget_parent(path
->dentry
);
156 err
= server
->nfs_client
->rpc_ops
->lookup(parent
->d_inode
,
157 &path
->dentry
->d_name
,
165 if (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)
166 mnt
= nfs_do_refmount(path
->dentry
);
168 mnt
= nfs_do_submount(path
->dentry
, fh
, fattr
);
172 dprintk("%s: done, success\n", __func__
);
173 mntget(mnt
); /* prevent immediate expiration */
174 mnt_set_expiry(mnt
, &nfs_automount_list
);
175 schedule_delayed_work(&nfs_automount_task
, nfs_mountpoint_expiry_timeout
);
178 nfs_free_fattr(fattr
);
179 nfs_free_fhandle(fh
);
181 dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt
);
185 const struct inode_operations nfs_mountpoint_inode_operations
= {
186 .getattr
= nfs_getattr
,
189 const struct inode_operations nfs_referral_inode_operations
= {
192 static void nfs_expire_automounts(struct work_struct
*work
)
194 struct list_head
*list
= &nfs_automount_list
;
196 mark_mounts_for_expiry(list
);
197 if (!list_empty(list
))
198 schedule_delayed_work(&nfs_automount_task
, nfs_mountpoint_expiry_timeout
);
201 void nfs_release_automount_timer(void)
203 if (list_empty(&nfs_automount_list
))
204 cancel_delayed_work(&nfs_automount_task
);
208 * Clone a mountpoint of the appropriate type
210 static struct vfsmount
*nfs_do_clone_mount(struct nfs_server
*server
,
212 struct nfs_clone_mount
*mountdata
)
215 struct vfsmount
*mnt
= ERR_PTR(-EINVAL
);
216 switch (server
->nfs_client
->rpc_ops
->version
) {
219 mnt
= vfs_kern_mount(&nfs_xdev_fs_type
, 0, devname
, mountdata
);
222 mnt
= vfs_kern_mount(&nfs4_xdev_fs_type
, 0, devname
, mountdata
);
226 return vfs_kern_mount(&nfs_xdev_fs_type
, 0, devname
, mountdata
);
231 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
232 * @dentry - parent directory
233 * @fh - filehandle for new root dentry
234 * @fattr - attributes for new root inode
237 static struct vfsmount
*nfs_do_submount(struct dentry
*dentry
,
239 struct nfs_fattr
*fattr
)
241 struct nfs_clone_mount mountdata
= {
247 struct vfsmount
*mnt
= ERR_PTR(-ENOMEM
);
248 char *page
= (char *) __get_free_page(GFP_USER
);
251 dprintk("--> nfs_do_submount()\n");
253 dprintk("%s: submounting on %s/%s\n", __func__
,
254 dentry
->d_parent
->d_name
.name
,
255 dentry
->d_name
.name
);
258 devname
= nfs_devname(dentry
, page
, PAGE_SIZE
);
259 mnt
= (struct vfsmount
*)devname
;
262 mnt
= nfs_do_clone_mount(NFS_SB(dentry
->d_sb
), devname
, &mountdata
);
264 free_page((unsigned long)page
);
266 dprintk("%s: done\n", __func__
);
268 dprintk("<-- nfs_do_submount() = %p\n", mnt
);