blob: 859cdaba4c1ce29a7ac786040680e601d9e6e722 [file] [log] [blame]
Trond Myklebust55a97592006-06-09 09:34:19 -04001/*
2 * linux/fs/nfs/namespace.c
3 *
4 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
David Howells54ceac42006-08-22 20:06:13 -04005 * - Modified by David Howells <dhowells@redhat.com>
Trond Myklebust55a97592006-06-09 09:34:19 -04006 *
7 * NFS namespace
8 */
9
Trond Myklebust55a97592006-06-09 09:34:19 -040010#include <linux/dcache.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Trond Myklebust55a97592006-06-09 09:34:19 -040012#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>
David Howellsf7b422b2006-06-09 09:34:33 -040018#include "internal.h"
Trond Myklebust55a97592006-06-09 09:34:19 -040019
20#define NFSDBG_FACILITY NFSDBG_VFS
21
David Howells65f27f32006-11-22 14:55:48 +000022static void nfs_expire_automounts(struct work_struct *work);
David Howellsf7b422b2006-06-09 09:34:33 -040023
Adrian Bunka3dab292008-04-14 21:41:32 +030024static LIST_HEAD(nfs_automount_list);
David Howells65f27f32006-11-22 14:55:48 +000025static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
Trond Myklebust51d8fa62006-06-09 09:34:20 -040026int nfs_mountpoint_expiry_timeout = 500 * HZ;
27
Al Virob514f872011-03-16 06:26:11 -040028static struct vfsmount *nfs_do_submount(struct super_block *sb,
29 struct dentry *dentry,
Adrian Bunk66f37502006-09-27 01:51:13 -070030 struct nfs_fh *fh,
31 struct nfs_fattr *fattr);
32
Trond Myklebust55a97592006-06-09 09:34:19 -040033/*
David Howellsf7b422b2006-06-09 09:34:33 -040034 * nfs_path - reconstruct the path given an arbitrary dentry
Al Virob514f872011-03-16 06:26:11 -040035 * @base - used to return pointer to the end of devname part of path
David Howellsf7b422b2006-06-09 09:34:33 -040036 * @dentry - pointer to dentry
37 * @buffer - result buffer
38 * @buflen - length of buffer
39 *
Al Virob514f872011-03-16 06:26:11 -040040 * Helper function for constructing the server pathname
41 * by arbitrary hashed dentry.
David Howellsf7b422b2006-06-09 09:34:33 -040042 *
43 * This is mainly for use in figuring out the path on the
Al Virob514f872011-03-16 06:26:11 -040044 * server side when automounting on top of an existing partition
45 * and in generating /proc/mounts and friends.
David Howellsf7b422b2006-06-09 09:34:33 -040046 */
Al Virob514f872011-03-16 06:26:11 -040047char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
David Howellsf7b422b2006-06-09 09:34:33 -040048{
Nick Piggin949854d2011-01-07 17:49:37 +110049 char *end;
David Howellsf7b422b2006-06-09 09:34:33 -040050 int namelen;
Nick Piggin949854d2011-01-07 17:49:37 +110051 unsigned seq;
Al Virob514f872011-03-16 06:26:11 -040052 const char *base;
David Howellsf7b422b2006-06-09 09:34:33 -040053
Nick Piggin949854d2011-01-07 17:49:37 +110054rename_retry:
55 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040056 *--end = '\0';
57 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110058
59 seq = read_seqbegin(&rename_lock);
60 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040061 while (1) {
62 spin_lock(&dentry->d_lock);
63 if (IS_ROOT(dentry))
64 break;
David Howellsf7b422b2006-06-09 09:34:33 -040065 namelen = dentry->d_name.len;
66 buflen -= namelen + 1;
67 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070068 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040069 end -= namelen;
70 memcpy(end, dentry->d_name.name, namelen);
71 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040072 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040073 dentry = dentry->d_parent;
74 }
Al Virob514f872011-03-16 06:26:11 -040075 if (read_seqretry(&rename_lock, seq)) {
76 spin_unlock(&dentry->d_lock);
77 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110078 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040079 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040080 if (*end != '/') {
Al Virob514f872011-03-16 06:26:11 -040081 if (--buflen < 0) {
82 spin_unlock(&dentry->d_lock);
83 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040084 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040085 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040086 *--end = '/';
87 }
Al Virob514f872011-03-16 06:26:11 -040088 *p = end;
89 base = dentry->d_fsdata;
90 if (!base) {
91 spin_unlock(&dentry->d_lock);
92 rcu_read_unlock();
93 WARN_ON(1);
94 return end;
95 }
David Howellsf7b422b2006-06-09 09:34:33 -040096 namelen = strlen(base);
97 /* Strip off excess slashes in base string */
98 while (namelen > 0 && base[namelen - 1] == '/')
99 namelen--;
100 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -0400101 if (buflen < 0) {
102 spin_lock(&dentry->d_lock);
103 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400104 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400105 }
David Howellsf7b422b2006-06-09 09:34:33 -0400106 end -= namelen;
107 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400108 spin_unlock(&dentry->d_lock);
109 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400110 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700111Elong_unlock:
Al Virob514f872011-03-16 06:26:11 -0400112 spin_lock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100113 rcu_read_unlock();
114 if (read_seqretry(&rename_lock, seq))
115 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400116Elong:
117 return ERR_PTR(-ENAMETOOLONG);
118}
119
120/*
David Howells36d43a42011-01-14 18:45:42 +0000121 * nfs_d_automount - Handle crossing a mountpoint on the server
122 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400123 *
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.
131 */
David Howells36d43a42011-01-14 18:45:42 +0000132struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400133{
134 struct vfsmount *mnt;
David Howells36d43a42011-01-14 18:45:42 +0000135 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
Trond Myklebust55a97592006-06-09 09:34:19 -0400136 struct dentry *parent;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400137 struct nfs_fh *fh = NULL;
138 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400139 int err;
140
David Howells36d43a42011-01-14 18:45:42 +0000141 dprintk("--> nfs_d_automount()\n");
David Howells54ceac42006-08-22 20:06:13 -0400142
David Howells36d43a42011-01-14 18:45:42 +0000143 mnt = ERR_PTR(-ESTALE);
144 if (IS_ROOT(path->dentry))
145 goto out_nofree;
Denis V. Lunev44d57592008-08-11 12:02:34 +0400146
David Howells36d43a42011-01-14 18:45:42 +0000147 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400148 fh = nfs_alloc_fhandle();
149 fattr = nfs_alloc_fattr();
150 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000151 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400152
Harvey Harrison3110ff82008-05-02 13:42:44 -0700153 dprintk("%s: enter\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400154
David Howells36d43a42011-01-14 18:45:42 +0000155 /* Look it up again to get its attributes */
156 parent = dget_parent(path->dentry);
David Howells8fa5c002006-08-22 20:06:12 -0400157 err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
David Howells36d43a42011-01-14 18:45:42 +0000158 &path->dentry->d_name,
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400159 fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400160 dput(parent);
David Howells36d43a42011-01-14 18:45:42 +0000161 if (err != 0) {
162 mnt = ERR_PTR(err);
163 goto out;
164 }
Trond Myklebust55a97592006-06-09 09:34:19 -0400165
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400166 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
Al Virob514f872011-03-16 06:26:11 -0400167 mnt = nfs_do_refmount(path->mnt->mnt_sb, path->dentry);
Manoj Naik6b97fd32006-06-09 09:34:29 -0400168 else
Al Virob514f872011-03-16 06:26:11 -0400169 mnt = nfs_do_submount(path->mnt->mnt_sb, path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400170 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000171 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400172
David Howellsea5b7782011-01-14 19:10:03 +0000173 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);
David Howells36d43a42011-01-14 18:45:42 +0000177
Trond Myklebust55a97592006-06-09 09:34:19 -0400178out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400179 nfs_free_fattr(fattr);
180 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000181out_nofree:
182 dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
183 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400184}
185
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800186const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400187 .getattr = nfs_getattr,
188};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400189
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800190const struct inode_operations nfs_referral_inode_operations = {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400191};
192
David Howells65f27f32006-11-22 14:55:48 +0000193static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400194{
David Howells65f27f32006-11-22 14:55:48 +0000195 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400196
197 mark_mounts_for_expiry(list);
198 if (!list_empty(list))
199 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
200}
201
202void nfs_release_automount_timer(void)
203{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400204 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400205 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400206}
David Howellsf7b422b2006-06-09 09:34:33 -0400207
208/*
209 * Clone a mountpoint of the appropriate type
210 */
David Howells509de812006-08-22 20:06:11 -0400211static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
212 const char *devname,
David Howellsf7b422b2006-06-09 09:34:33 -0400213 struct nfs_clone_mount *mountdata)
214{
215#ifdef CONFIG_NFS_V4
Denis V. Lunevfd08d7e2008-07-31 09:38:55 +0400216 struct vfsmount *mnt = ERR_PTR(-EINVAL);
Trond Myklebust40c553192007-12-14 14:56:07 -0500217 switch (server->nfs_client->rpc_ops->version) {
David Howellsf7b422b2006-06-09 09:34:33 -0400218 case 2:
219 case 3:
David Howells54ceac42006-08-22 20:06:13 -0400220 mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400221 break;
222 case 4:
David Howells54ceac42006-08-22 20:06:13 -0400223 mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400224 }
225 return mnt;
226#else
David Howells54ceac42006-08-22 20:06:13 -0400227 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400228#endif
229}
230
231/**
232 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
Al Virob514f872011-03-16 06:26:11 -0400233 * @sb - superblock of parent directory
David Howellsf7b422b2006-06-09 09:34:33 -0400234 * @dentry - parent directory
235 * @fh - filehandle for new root dentry
236 * @fattr - attributes for new root inode
237 *
238 */
Al Virob514f872011-03-16 06:26:11 -0400239static struct vfsmount *nfs_do_submount(struct super_block *sb,
240 struct dentry *dentry,
Adrian Bunk66f37502006-09-27 01:51:13 -0700241 struct nfs_fh *fh,
242 struct nfs_fattr *fattr)
David Howellsf7b422b2006-06-09 09:34:33 -0400243{
244 struct nfs_clone_mount mountdata = {
Al Virob514f872011-03-16 06:26:11 -0400245 .sb = sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400246 .dentry = dentry,
247 .fh = fh,
248 .fattr = fattr,
249 };
250 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
251 char *page = (char *) __get_free_page(GFP_USER);
252 char *devname;
253
David Howells54ceac42006-08-22 20:06:13 -0400254 dprintk("--> nfs_do_submount()\n");
255
Harvey Harrison3110ff82008-05-02 13:42:44 -0700256 dprintk("%s: submounting on %s/%s\n", __func__,
David Howellsf7b422b2006-06-09 09:34:33 -0400257 dentry->d_parent->d_name.name,
258 dentry->d_name.name);
259 if (page == NULL)
260 goto out;
Al Virob514f872011-03-16 06:26:11 -0400261 devname = nfs_devname(dentry, page, PAGE_SIZE);
David Howellsf7b422b2006-06-09 09:34:33 -0400262 mnt = (struct vfsmount *)devname;
263 if (IS_ERR(devname))
264 goto free_page;
Al Virob514f872011-03-16 06:26:11 -0400265 mnt = nfs_do_clone_mount(NFS_SB(sb), devname, &mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400266free_page:
267 free_page((unsigned long)page);
268out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700269 dprintk("%s: done\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400270
271 dprintk("<-- nfs_do_submount() = %p\n", mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400272 return mnt;
273}