blob: 08b9c93675da512e0ef8174a25e1e048f282b527 [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>
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000018#include <linux/sunrpc/gss_api.h>
David Howellsf7b422b2006-06-09 09:34:33 -040019#include "internal.h"
Trond Myklebust55a97592006-06-09 09:34:19 -040020
21#define NFSDBG_FACILITY NFSDBG_VFS
22
David Howells65f27f32006-11-22 14:55:48 +000023static void nfs_expire_automounts(struct work_struct *work);
David Howellsf7b422b2006-06-09 09:34:33 -040024
Adrian Bunka3dab292008-04-14 21:41:32 +030025static LIST_HEAD(nfs_automount_list);
David Howells65f27f32006-11-22 14:55:48 +000026static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
Trond Myklebust51d8fa62006-06-09 09:34:20 -040027int nfs_mountpoint_expiry_timeout = 500 * HZ;
28
Trond Myklebust55a97592006-06-09 09:34:19 -040029/*
David Howellsf7b422b2006-06-09 09:34:33 -040030 * nfs_path - reconstruct the path given an arbitrary dentry
Al Virob514f872011-03-16 06:26:11 -040031 * @base - used to return pointer to the end of devname part of path
David Howellsf7b422b2006-06-09 09:34:33 -040032 * @dentry - pointer to dentry
33 * @buffer - result buffer
34 * @buflen - length of buffer
35 *
Al Virob514f872011-03-16 06:26:11 -040036 * Helper function for constructing the server pathname
37 * by arbitrary hashed dentry.
David Howellsf7b422b2006-06-09 09:34:33 -040038 *
39 * This is mainly for use in figuring out the path on the
Al Virob514f872011-03-16 06:26:11 -040040 * server side when automounting on top of an existing partition
41 * and in generating /proc/mounts and friends.
David Howellsf7b422b2006-06-09 09:34:33 -040042 */
Al Virob514f872011-03-16 06:26:11 -040043char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
David Howellsf7b422b2006-06-09 09:34:33 -040044{
Nick Piggin949854d2011-01-07 17:49:37 +110045 char *end;
David Howellsf7b422b2006-06-09 09:34:33 -040046 int namelen;
Nick Piggin949854d2011-01-07 17:49:37 +110047 unsigned seq;
Al Virob514f872011-03-16 06:26:11 -040048 const char *base;
David Howellsf7b422b2006-06-09 09:34:33 -040049
Nick Piggin949854d2011-01-07 17:49:37 +110050rename_retry:
51 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040052 *--end = '\0';
53 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110054
55 seq = read_seqbegin(&rename_lock);
56 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040057 while (1) {
58 spin_lock(&dentry->d_lock);
59 if (IS_ROOT(dentry))
60 break;
David Howellsf7b422b2006-06-09 09:34:33 -040061 namelen = dentry->d_name.len;
62 buflen -= namelen + 1;
63 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070064 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040065 end -= namelen;
66 memcpy(end, dentry->d_name.name, namelen);
67 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040068 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040069 dentry = dentry->d_parent;
70 }
Al Virob514f872011-03-16 06:26:11 -040071 if (read_seqretry(&rename_lock, seq)) {
72 spin_unlock(&dentry->d_lock);
73 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110074 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040075 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040076 if (*end != '/') {
Al Virob514f872011-03-16 06:26:11 -040077 if (--buflen < 0) {
78 spin_unlock(&dentry->d_lock);
79 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040080 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040081 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040082 *--end = '/';
83 }
Al Virob514f872011-03-16 06:26:11 -040084 *p = end;
85 base = dentry->d_fsdata;
86 if (!base) {
87 spin_unlock(&dentry->d_lock);
88 rcu_read_unlock();
89 WARN_ON(1);
90 return end;
91 }
David Howellsf7b422b2006-06-09 09:34:33 -040092 namelen = strlen(base);
93 /* Strip off excess slashes in base string */
94 while (namelen > 0 && base[namelen - 1] == '/')
95 namelen--;
96 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -040097 if (buflen < 0) {
Dan Carpenter1c340922011-03-20 14:22:07 +030098 spin_unlock(&dentry->d_lock);
Al Virob514f872011-03-16 06:26:11 -040099 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400100 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400101 }
David Howellsf7b422b2006-06-09 09:34:33 -0400102 end -= namelen;
103 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400104 spin_unlock(&dentry->d_lock);
105 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400106 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700107Elong_unlock:
Dan Carpenter1c340922011-03-20 14:22:07 +0300108 spin_unlock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100109 rcu_read_unlock();
110 if (read_seqretry(&rename_lock, seq))
111 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400112Elong:
113 return ERR_PTR(-ENAMETOOLONG);
114}
115
116/*
David Howells36d43a42011-01-14 18:45:42 +0000117 * nfs_d_automount - Handle crossing a mountpoint on the server
118 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400119 *
120 * When we encounter a mountpoint on the server, we want to set up
121 * a mountpoint on the client too, to prevent inode numbers from
122 * colliding, and to allow "df" to work properly.
123 * On NFSv4, we also want to allow for the fact that different
124 * filesystems may be migrated to different servers in a failover
125 * situation, and that different filesystems may want to use
126 * different security flavours.
127 */
David Howells36d43a42011-01-14 18:45:42 +0000128struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400129{
130 struct vfsmount *mnt;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400131 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400132 struct nfs_fh *fh = NULL;
133 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400134
David Howells36d43a42011-01-14 18:45:42 +0000135 dprintk("--> nfs_d_automount()\n");
David Howells54ceac42006-08-22 20:06:13 -0400136
David Howells36d43a42011-01-14 18:45:42 +0000137 mnt = ERR_PTR(-ESTALE);
138 if (IS_ROOT(path->dentry))
139 goto out_nofree;
Denis V. Lunev44d57592008-08-11 12:02:34 +0400140
David Howells36d43a42011-01-14 18:45:42 +0000141 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400142 fh = nfs_alloc_fhandle();
143 fattr = nfs_alloc_fattr();
144 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000145 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400146
Harvey Harrison3110ff82008-05-02 13:42:44 -0700147 dprintk("%s: enter\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400148
Bryan Schumaker281cad42012-04-27 13:27:45 -0400149 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400150 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000151 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400152
David Howellsea5b7782011-01-14 19:10:03 +0000153 dprintk("%s: done, success\n", __func__);
154 mntget(mnt); /* prevent immediate expiration */
155 mnt_set_expiry(mnt, &nfs_automount_list);
156 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
David Howells36d43a42011-01-14 18:45:42 +0000157
Trond Myklebust55a97592006-06-09 09:34:19 -0400158out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400159 nfs_free_fattr(fattr);
160 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000161out_nofree:
Chuck Leverd7c32672012-02-15 16:35:17 -0500162 if (IS_ERR(mnt))
163 dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
164 else
165 dprintk("<-- %s() = %p\n", __func__, mnt);
David Howells36d43a42011-01-14 18:45:42 +0000166 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400167}
168
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800169const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400170 .getattr = nfs_getattr,
171};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400172
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800173const struct inode_operations nfs_referral_inode_operations = {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400174};
175
David Howells65f27f32006-11-22 14:55:48 +0000176static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400177{
David Howells65f27f32006-11-22 14:55:48 +0000178 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400179
180 mark_mounts_for_expiry(list);
181 if (!list_empty(list))
182 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
183}
184
185void nfs_release_automount_timer(void)
186{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400187 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400188 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400189}
David Howellsf7b422b2006-06-09 09:34:33 -0400190
191/*
192 * Clone a mountpoint of the appropriate type
193 */
David Howells509de812006-08-22 20:06:11 -0400194static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
195 const char *devname,
David Howellsf7b422b2006-06-09 09:34:33 -0400196 struct nfs_clone_mount *mountdata)
197{
198#ifdef CONFIG_NFS_V4
Denis V. Lunevfd08d7e2008-07-31 09:38:55 +0400199 struct vfsmount *mnt = ERR_PTR(-EINVAL);
Trond Myklebust40c553192007-12-14 14:56:07 -0500200 switch (server->nfs_client->rpc_ops->version) {
David Howellsf7b422b2006-06-09 09:34:33 -0400201 case 2:
202 case 3:
David Howells54ceac42006-08-22 20:06:13 -0400203 mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400204 break;
205 case 4:
David Howells54ceac42006-08-22 20:06:13 -0400206 mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400207 }
208 return mnt;
209#else
David Howells54ceac42006-08-22 20:06:13 -0400210 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400211#endif
212}
213
214/**
215 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
David Howellsf7b422b2006-06-09 09:34:33 -0400216 * @dentry - parent directory
217 * @fh - filehandle for new root dentry
218 * @fattr - attributes for new root inode
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000219 * @authflavor - security flavor to use when performing the mount
David Howellsf7b422b2006-06-09 09:34:33 -0400220 *
221 */
Bryan Schumaker281cad42012-04-27 13:27:45 -0400222struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
223 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
David Howellsf7b422b2006-06-09 09:34:33 -0400224{
225 struct nfs_clone_mount mountdata = {
Al Virof8ad9c42011-03-16 06:32:07 -0400226 .sb = dentry->d_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400227 .dentry = dentry,
228 .fh = fh,
229 .fattr = fattr,
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000230 .authflavor = authflavor,
David Howellsf7b422b2006-06-09 09:34:33 -0400231 };
232 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
233 char *page = (char *) __get_free_page(GFP_USER);
234 char *devname;
235
David Howells54ceac42006-08-22 20:06:13 -0400236 dprintk("--> nfs_do_submount()\n");
237
Harvey Harrison3110ff82008-05-02 13:42:44 -0700238 dprintk("%s: submounting on %s/%s\n", __func__,
David Howellsf7b422b2006-06-09 09:34:33 -0400239 dentry->d_parent->d_name.name,
240 dentry->d_name.name);
241 if (page == NULL)
242 goto out;
Al Virob514f872011-03-16 06:26:11 -0400243 devname = nfs_devname(dentry, page, PAGE_SIZE);
David Howellsf7b422b2006-06-09 09:34:33 -0400244 mnt = (struct vfsmount *)devname;
245 if (IS_ERR(devname))
246 goto free_page;
Al Virof8ad9c42011-03-16 06:32:07 -0400247 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400248free_page:
249 free_page((unsigned long)page);
250out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700251 dprintk("%s: done\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400252
253 dprintk("<-- nfs_do_submount() = %p\n", mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400254 return mnt;
255}
Bryan Schumaker281cad42012-04-27 13:27:45 -0400256
257struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
258 struct nfs_fh *fh, struct nfs_fattr *fattr)
259{
260 int err;
261 struct dentry *parent = dget_parent(dentry);
262
263 /* Look it up again to get its attributes */
Bryan Schumaker80a16b22012-04-27 13:27:46 -0400264 err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400265 dput(parent);
266 if (err != 0)
267 return ERR_PTR(err);
268
269 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
270}