blob: bf1c68009ffd8b64001ecdd4d9f266654d2a3656 [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 Virof8ad9c42011-03-16 06:32:07 -040028static struct vfsmount *nfs_do_submount(struct dentry *dentry,
Adrian Bunk66f37502006-09-27 01:51:13 -070029 struct nfs_fh *fh,
30 struct nfs_fattr *fattr);
31
Trond Myklebust55a97592006-06-09 09:34:19 -040032/*
David Howellsf7b422b2006-06-09 09:34:33 -040033 * nfs_path - reconstruct the path given an arbitrary dentry
Al Virob514f872011-03-16 06:26:11 -040034 * @base - used to return pointer to the end of devname part of path
David Howellsf7b422b2006-06-09 09:34:33 -040035 * @dentry - pointer to dentry
36 * @buffer - result buffer
37 * @buflen - length of buffer
38 *
Al Virob514f872011-03-16 06:26:11 -040039 * Helper function for constructing the server pathname
40 * by arbitrary hashed dentry.
David Howellsf7b422b2006-06-09 09:34:33 -040041 *
42 * This is mainly for use in figuring out the path on the
Al Virob514f872011-03-16 06:26:11 -040043 * server side when automounting on top of an existing partition
44 * and in generating /proc/mounts and friends.
David Howellsf7b422b2006-06-09 09:34:33 -040045 */
Al Virob514f872011-03-16 06:26:11 -040046char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
David Howellsf7b422b2006-06-09 09:34:33 -040047{
Nick Piggin949854d2011-01-07 17:49:37 +110048 char *end;
David Howellsf7b422b2006-06-09 09:34:33 -040049 int namelen;
Nick Piggin949854d2011-01-07 17:49:37 +110050 unsigned seq;
Al Virob514f872011-03-16 06:26:11 -040051 const char *base;
David Howellsf7b422b2006-06-09 09:34:33 -040052
Nick Piggin949854d2011-01-07 17:49:37 +110053rename_retry:
54 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040055 *--end = '\0';
56 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110057
58 seq = read_seqbegin(&rename_lock);
59 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040060 while (1) {
61 spin_lock(&dentry->d_lock);
62 if (IS_ROOT(dentry))
63 break;
David Howellsf7b422b2006-06-09 09:34:33 -040064 namelen = dentry->d_name.len;
65 buflen -= namelen + 1;
66 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070067 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040068 end -= namelen;
69 memcpy(end, dentry->d_name.name, namelen);
70 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040071 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040072 dentry = dentry->d_parent;
73 }
Al Virob514f872011-03-16 06:26:11 -040074 if (read_seqretry(&rename_lock, seq)) {
75 spin_unlock(&dentry->d_lock);
76 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110077 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040078 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040079 if (*end != '/') {
Al Virob514f872011-03-16 06:26:11 -040080 if (--buflen < 0) {
81 spin_unlock(&dentry->d_lock);
82 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040083 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040084 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040085 *--end = '/';
86 }
Al Virob514f872011-03-16 06:26:11 -040087 *p = end;
88 base = dentry->d_fsdata;
89 if (!base) {
90 spin_unlock(&dentry->d_lock);
91 rcu_read_unlock();
92 WARN_ON(1);
93 return end;
94 }
David Howellsf7b422b2006-06-09 09:34:33 -040095 namelen = strlen(base);
96 /* Strip off excess slashes in base string */
97 while (namelen > 0 && base[namelen - 1] == '/')
98 namelen--;
99 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -0400100 if (buflen < 0) {
Dan Carpenter1c340922011-03-20 14:22:07 +0300101 spin_unlock(&dentry->d_lock);
Al Virob514f872011-03-16 06:26:11 -0400102 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400103 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400104 }
David Howellsf7b422b2006-06-09 09:34:33 -0400105 end -= namelen;
106 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400107 spin_unlock(&dentry->d_lock);
108 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400109 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700110Elong_unlock:
Dan Carpenter1c340922011-03-20 14:22:07 +0300111 spin_unlock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100112 rcu_read_unlock();
113 if (read_seqretry(&rename_lock, seq))
114 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400115Elong:
116 return ERR_PTR(-ENAMETOOLONG);
117}
118
119/*
David Howells36d43a42011-01-14 18:45:42 +0000120 * nfs_d_automount - Handle crossing a mountpoint on the server
121 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400122 *
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.
130 */
David Howells36d43a42011-01-14 18:45:42 +0000131struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400132{
133 struct vfsmount *mnt;
David Howells36d43a42011-01-14 18:45:42 +0000134 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
Trond Myklebust55a97592006-06-09 09:34:19 -0400135 struct dentry *parent;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400136 struct nfs_fh *fh = NULL;
137 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400138 int err;
139
David Howells36d43a42011-01-14 18:45:42 +0000140 dprintk("--> nfs_d_automount()\n");
David Howells54ceac42006-08-22 20:06:13 -0400141
David Howells36d43a42011-01-14 18:45:42 +0000142 mnt = ERR_PTR(-ESTALE);
143 if (IS_ROOT(path->dentry))
144 goto out_nofree;
Denis V. Lunev44d57592008-08-11 12:02:34 +0400145
David Howells36d43a42011-01-14 18:45:42 +0000146 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400147 fh = nfs_alloc_fhandle();
148 fattr = nfs_alloc_fattr();
149 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000150 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400151
Harvey Harrison3110ff82008-05-02 13:42:44 -0700152 dprintk("%s: enter\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400153
David Howells36d43a42011-01-14 18:45:42 +0000154 /* Look it up again to get its attributes */
155 parent = dget_parent(path->dentry);
David Howells8fa5c002006-08-22 20:06:12 -0400156 err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
David Howells36d43a42011-01-14 18:45:42 +0000157 &path->dentry->d_name,
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400158 fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400159 dput(parent);
David Howells36d43a42011-01-14 18:45:42 +0000160 if (err != 0) {
161 mnt = ERR_PTR(err);
162 goto out;
163 }
Trond Myklebust55a97592006-06-09 09:34:19 -0400164
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400165 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
Al Virof8ad9c42011-03-16 06:32:07 -0400166 mnt = nfs_do_refmount(path->dentry);
Manoj Naik6b97fd32006-06-09 09:34:29 -0400167 else
Al Virof8ad9c42011-03-16 06:32:07 -0400168 mnt = nfs_do_submount(path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400169 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000170 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400171
David Howellsea5b7782011-01-14 19:10:03 +0000172 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);
David Howells36d43a42011-01-14 18:45:42 +0000176
Trond Myklebust55a97592006-06-09 09:34:19 -0400177out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400178 nfs_free_fattr(fattr);
179 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000180out_nofree:
181 dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
182 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400183}
184
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800185const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400186 .getattr = nfs_getattr,
187};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400188
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800189const struct inode_operations nfs_referral_inode_operations = {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400190};
191
David Howells65f27f32006-11-22 14:55:48 +0000192static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400193{
David Howells65f27f32006-11-22 14:55:48 +0000194 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400195
196 mark_mounts_for_expiry(list);
197 if (!list_empty(list))
198 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
199}
200
201void nfs_release_automount_timer(void)
202{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400203 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400204 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400205}
David Howellsf7b422b2006-06-09 09:34:33 -0400206
207/*
208 * Clone a mountpoint of the appropriate type
209 */
David Howells509de812006-08-22 20:06:11 -0400210static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
211 const char *devname,
David Howellsf7b422b2006-06-09 09:34:33 -0400212 struct nfs_clone_mount *mountdata)
213{
214#ifdef CONFIG_NFS_V4
Denis V. Lunevfd08d7e2008-07-31 09:38:55 +0400215 struct vfsmount *mnt = ERR_PTR(-EINVAL);
Trond Myklebust40c553192007-12-14 14:56:07 -0500216 switch (server->nfs_client->rpc_ops->version) {
David Howellsf7b422b2006-06-09 09:34:33 -0400217 case 2:
218 case 3:
David Howells54ceac42006-08-22 20:06:13 -0400219 mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400220 break;
221 case 4:
David Howells54ceac42006-08-22 20:06:13 -0400222 mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400223 }
224 return mnt;
225#else
David Howells54ceac42006-08-22 20:06:13 -0400226 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400227#endif
228}
229
230/**
231 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
David Howellsf7b422b2006-06-09 09:34:33 -0400232 * @dentry - parent directory
233 * @fh - filehandle for new root dentry
234 * @fattr - attributes for new root inode
235 *
236 */
Al Virof8ad9c42011-03-16 06:32:07 -0400237static struct vfsmount *nfs_do_submount(struct dentry *dentry,
Adrian Bunk66f37502006-09-27 01:51:13 -0700238 struct nfs_fh *fh,
239 struct nfs_fattr *fattr)
David Howellsf7b422b2006-06-09 09:34:33 -0400240{
241 struct nfs_clone_mount mountdata = {
Al Virof8ad9c42011-03-16 06:32:07 -0400242 .sb = dentry->d_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400243 .dentry = dentry,
244 .fh = fh,
245 .fattr = fattr,
246 };
247 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
248 char *page = (char *) __get_free_page(GFP_USER);
249 char *devname;
250
David Howells54ceac42006-08-22 20:06:13 -0400251 dprintk("--> nfs_do_submount()\n");
252
Harvey Harrison3110ff82008-05-02 13:42:44 -0700253 dprintk("%s: submounting on %s/%s\n", __func__,
David Howellsf7b422b2006-06-09 09:34:33 -0400254 dentry->d_parent->d_name.name,
255 dentry->d_name.name);
256 if (page == NULL)
257 goto out;
Al Virob514f872011-03-16 06:26:11 -0400258 devname = nfs_devname(dentry, page, PAGE_SIZE);
David Howellsf7b422b2006-06-09 09:34:33 -0400259 mnt = (struct vfsmount *)devname;
260 if (IS_ERR(devname))
261 goto free_page;
Al Virof8ad9c42011-03-16 06:32:07 -0400262 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400263free_page:
264 free_page((unsigned long)page);
265out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700266 dprintk("%s: done\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400267
268 dprintk("<-- nfs_do_submount() = %p\n", mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400269 return mnt;
270}