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 spin_unlock(&dcache_lock
);
74 return ERR_PTR(-ENAMETOOLONG
);
78 * nfs_follow_mountpoint - handle crossing a mountpoint on the server
79 * @dentry - dentry of mountpoint
80 * @nd - nameidata info
82 * When we encounter a mountpoint on the server, we want to set up
83 * a mountpoint on the client too, to prevent inode numbers from
84 * colliding, and to allow "df" to work properly.
85 * On NFSv4, we also want to allow for the fact that different
86 * filesystems may be migrated to different servers in a failover
87 * situation, and that different filesystems may want to use
88 * different security flavours.
90 static void * nfs_follow_mountpoint(struct dentry
*dentry
, struct nameidata
*nd
)
93 struct nfs_server
*server
= NFS_SERVER(dentry
->d_inode
);
94 struct dentry
*parent
;
96 struct nfs_fattr fattr
;
99 BUG_ON(IS_ROOT(dentry
));
100 dprintk("%s: enter\n", __FUNCTION__
);
102 nd
->dentry
= dget(dentry
);
103 if (d_mountpoint(nd
->dentry
))
105 /* Look it up again */
106 parent
= dget_parent(nd
->dentry
);
107 err
= server
->rpc_ops
->lookup(parent
->d_inode
, &nd
->dentry
->d_name
, &fh
, &fattr
);
112 if (fattr
.valid
& NFS_ATTR_FATTR_V4_REFERRAL
)
113 mnt
= nfs_do_refmount(nd
->mnt
, nd
->dentry
);
115 mnt
= nfs_do_submount(nd
->mnt
, nd
->dentry
, &fh
, &fattr
);
121 err
= do_add_mount(mnt
, nd
, nd
->mnt
->mnt_flags
|MNT_SHRINKABLE
, &nfs_automount_list
);
131 nd
->dentry
= dget(mnt
->mnt_root
);
132 schedule_delayed_work(&nfs_automount_task
, nfs_mountpoint_expiry_timeout
);
134 dprintk("%s: done, returned %d\n", __FUNCTION__
, err
);
140 while(d_mountpoint(nd
->dentry
) && follow_down(&nd
->mnt
, &nd
->dentry
))
146 struct inode_operations nfs_mountpoint_inode_operations
= {
147 .follow_link
= nfs_follow_mountpoint
,
148 .getattr
= nfs_getattr
,
151 struct inode_operations nfs_referral_inode_operations
= {
152 .follow_link
= nfs_follow_mountpoint
,
155 static void nfs_expire_automounts(void *data
)
157 struct list_head
*list
= (struct list_head
*)data
;
159 mark_mounts_for_expiry(list
);
160 if (!list_empty(list
))
161 schedule_delayed_work(&nfs_automount_task
, nfs_mountpoint_expiry_timeout
);
164 void nfs_release_automount_timer(void)
166 if (list_empty(&nfs_automount_list
)) {
167 cancel_delayed_work(&nfs_automount_task
);
168 flush_scheduled_work();
173 * Clone a mountpoint of the appropriate type
175 static struct vfsmount
*nfs_do_clone_mount(struct nfs_server
*server
, char *devname
,
176 struct nfs_clone_mount
*mountdata
)
179 struct vfsmount
*mnt
= NULL
;
180 switch (server
->rpc_ops
->version
) {
183 mnt
= vfs_kern_mount(&clone_nfs_fs_type
, 0, devname
, mountdata
);
186 mnt
= vfs_kern_mount(&clone_nfs4_fs_type
, 0, devname
, mountdata
);
190 return vfs_kern_mount(&clone_nfs_fs_type
, 0, devname
, mountdata
);
195 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
196 * @mnt_parent - mountpoint of parent directory
197 * @dentry - parent directory
198 * @fh - filehandle for new root dentry
199 * @fattr - attributes for new root inode
202 struct vfsmount
*nfs_do_submount(const struct vfsmount
*mnt_parent
,
203 const struct dentry
*dentry
, struct nfs_fh
*fh
,
204 struct nfs_fattr
*fattr
)
206 struct nfs_clone_mount mountdata
= {
207 .sb
= mnt_parent
->mnt_sb
,
212 struct vfsmount
*mnt
= ERR_PTR(-ENOMEM
);
213 char *page
= (char *) __get_free_page(GFP_USER
);
216 dprintk("%s: submounting on %s/%s\n", __FUNCTION__
,
217 dentry
->d_parent
->d_name
.name
,
218 dentry
->d_name
.name
);
221 devname
= nfs_devname(mnt_parent
, dentry
, page
, PAGE_SIZE
);
222 mnt
= (struct vfsmount
*)devname
;
225 mnt
= nfs_do_clone_mount(NFS_SB(mnt_parent
->mnt_sb
), devname
, &mountdata
);
227 free_page((unsigned long)page
);
229 dprintk("%s: done\n", __FUNCTION__
);