blob: 2a3b170e88e00906ba70f9790a057bfa0250a8d1 [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
Bryan Schumakerddda8e02012-07-30 16:05:23 -040010#include <linux/module.h>
Trond Myklebust55a97592006-06-09 09:34:19 -040011#include <linux/dcache.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Trond Myklebust55a97592006-06-09 09:34:19 -040013#include <linux/mount.h>
14#include <linux/namei.h>
15#include <linux/nfs_fs.h>
16#include <linux/string.h>
17#include <linux/sunrpc/clnt.h>
18#include <linux/vfs.h>
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000019#include <linux/sunrpc/gss_api.h>
David Howellsf7b422b2006-06-09 09:34:33 -040020#include "internal.h"
Trond Myklebust55a97592006-06-09 09:34:19 -040021
22#define NFSDBG_FACILITY NFSDBG_VFS
23
David Howells65f27f32006-11-22 14:55:48 +000024static void nfs_expire_automounts(struct work_struct *work);
David Howellsf7b422b2006-06-09 09:34:33 -040025
Adrian Bunka3dab292008-04-14 21:41:32 +030026static LIST_HEAD(nfs_automount_list);
David Howells65f27f32006-11-22 14:55:48 +000027static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
Trond Myklebust51d8fa62006-06-09 09:34:20 -040028int nfs_mountpoint_expiry_timeout = 500 * HZ;
29
Trond Myklebust55a97592006-06-09 09:34:19 -040030/*
David Howellsf7b422b2006-06-09 09:34:33 -040031 * nfs_path - reconstruct the path given an arbitrary dentry
Al Virob514f872011-03-16 06:26:11 -040032 * @base - used to return pointer to the end of devname part of path
David Howellsf7b422b2006-06-09 09:34:33 -040033 * @dentry - pointer to dentry
34 * @buffer - result buffer
35 * @buflen - length of buffer
36 *
Al Virob514f872011-03-16 06:26:11 -040037 * Helper function for constructing the server pathname
38 * by arbitrary hashed dentry.
David Howellsf7b422b2006-06-09 09:34:33 -040039 *
40 * This is mainly for use in figuring out the path on the
Al Virob514f872011-03-16 06:26:11 -040041 * server side when automounting on top of an existing partition
42 * and in generating /proc/mounts and friends.
David Howellsf7b422b2006-06-09 09:34:33 -040043 */
Al Virob514f872011-03-16 06:26:11 -040044char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
David Howellsf7b422b2006-06-09 09:34:33 -040045{
Nick Piggin949854d2011-01-07 17:49:37 +110046 char *end;
David Howellsf7b422b2006-06-09 09:34:33 -040047 int namelen;
Nick Piggin949854d2011-01-07 17:49:37 +110048 unsigned seq;
Al Virob514f872011-03-16 06:26:11 -040049 const char *base;
David Howellsf7b422b2006-06-09 09:34:33 -040050
Nick Piggin949854d2011-01-07 17:49:37 +110051rename_retry:
52 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040053 *--end = '\0';
54 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110055
56 seq = read_seqbegin(&rename_lock);
57 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040058 while (1) {
59 spin_lock(&dentry->d_lock);
60 if (IS_ROOT(dentry))
61 break;
David Howellsf7b422b2006-06-09 09:34:33 -040062 namelen = dentry->d_name.len;
63 buflen -= namelen + 1;
64 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070065 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040066 end -= namelen;
67 memcpy(end, dentry->d_name.name, namelen);
68 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040069 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040070 dentry = dentry->d_parent;
71 }
Al Virob514f872011-03-16 06:26:11 -040072 if (read_seqretry(&rename_lock, seq)) {
73 spin_unlock(&dentry->d_lock);
74 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110075 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040076 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040077 if (*end != '/') {
Al Virob514f872011-03-16 06:26:11 -040078 if (--buflen < 0) {
79 spin_unlock(&dentry->d_lock);
80 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040081 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040082 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040083 *--end = '/';
84 }
Al Virob514f872011-03-16 06:26:11 -040085 *p = end;
86 base = dentry->d_fsdata;
87 if (!base) {
88 spin_unlock(&dentry->d_lock);
89 rcu_read_unlock();
90 WARN_ON(1);
91 return end;
92 }
David Howellsf7b422b2006-06-09 09:34:33 -040093 namelen = strlen(base);
94 /* Strip off excess slashes in base string */
95 while (namelen > 0 && base[namelen - 1] == '/')
96 namelen--;
97 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -040098 if (buflen < 0) {
Dan Carpenter1c340922011-03-20 14:22:07 +030099 spin_unlock(&dentry->d_lock);
Al Virob514f872011-03-16 06:26:11 -0400100 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400101 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400102 }
David Howellsf7b422b2006-06-09 09:34:33 -0400103 end -= namelen;
104 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400105 spin_unlock(&dentry->d_lock);
106 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400107 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700108Elong_unlock:
Dan Carpenter1c340922011-03-20 14:22:07 +0300109 spin_unlock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100110 rcu_read_unlock();
111 if (read_seqretry(&rename_lock, seq))
112 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400113Elong:
114 return ERR_PTR(-ENAMETOOLONG);
115}
116
117/*
David Howells36d43a42011-01-14 18:45:42 +0000118 * nfs_d_automount - Handle crossing a mountpoint on the server
119 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400120 *
121 * When we encounter a mountpoint on the server, we want to set up
122 * a mountpoint on the client too, to prevent inode numbers from
123 * colliding, and to allow "df" to work properly.
124 * On NFSv4, we also want to allow for the fact that different
125 * filesystems may be migrated to different servers in a failover
126 * situation, and that different filesystems may want to use
127 * different security flavours.
128 */
David Howells36d43a42011-01-14 18:45:42 +0000129struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400130{
131 struct vfsmount *mnt;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400132 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400133 struct nfs_fh *fh = NULL;
134 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400135
David Howells36d43a42011-01-14 18:45:42 +0000136 dprintk("--> nfs_d_automount()\n");
David Howells54ceac42006-08-22 20:06:13 -0400137
David Howells36d43a42011-01-14 18:45:42 +0000138 mnt = ERR_PTR(-ESTALE);
139 if (IS_ROOT(path->dentry))
140 goto out_nofree;
Denis V. Lunev44d57592008-08-11 12:02:34 +0400141
David Howells36d43a42011-01-14 18:45:42 +0000142 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400143 fh = nfs_alloc_fhandle();
144 fattr = nfs_alloc_fattr();
145 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000146 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400147
Harvey Harrison3110ff82008-05-02 13:42:44 -0700148 dprintk("%s: enter\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400149
Bryan Schumaker281cad42012-04-27 13:27:45 -0400150 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400151 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000152 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400153
David Howellsea5b7782011-01-14 19:10:03 +0000154 dprintk("%s: done, success\n", __func__);
155 mntget(mnt); /* prevent immediate expiration */
156 mnt_set_expiry(mnt, &nfs_automount_list);
157 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
David Howells36d43a42011-01-14 18:45:42 +0000158
Trond Myklebust55a97592006-06-09 09:34:19 -0400159out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400160 nfs_free_fattr(fattr);
161 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000162out_nofree:
Chuck Leverd7c32672012-02-15 16:35:17 -0500163 if (IS_ERR(mnt))
164 dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
165 else
166 dprintk("<-- %s() = %p\n", __func__, mnt);
David Howells36d43a42011-01-14 18:45:42 +0000167 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400168}
169
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800170const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400171 .getattr = nfs_getattr,
172};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400173
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800174const struct inode_operations nfs_referral_inode_operations = {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400175};
176
David Howells65f27f32006-11-22 14:55:48 +0000177static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400178{
David Howells65f27f32006-11-22 14:55:48 +0000179 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400180
181 mark_mounts_for_expiry(list);
182 if (!list_empty(list))
183 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
184}
185
186void nfs_release_automount_timer(void)
187{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400188 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400189 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400190}
David Howellsf7b422b2006-06-09 09:34:33 -0400191
192/*
193 * Clone a mountpoint of the appropriate type
194 */
David Howells509de812006-08-22 20:06:11 -0400195static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
196 const char *devname,
David Howellsf7b422b2006-06-09 09:34:33 -0400197 struct nfs_clone_mount *mountdata)
198{
David Howells54ceac42006-08-22 20:06:13 -0400199 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400200}
201
202/**
203 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
David Howellsf7b422b2006-06-09 09:34:33 -0400204 * @dentry - parent directory
205 * @fh - filehandle for new root dentry
206 * @fattr - attributes for new root inode
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000207 * @authflavor - security flavor to use when performing the mount
David Howellsf7b422b2006-06-09 09:34:33 -0400208 *
209 */
Bryan Schumaker281cad42012-04-27 13:27:45 -0400210struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
211 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
David Howellsf7b422b2006-06-09 09:34:33 -0400212{
213 struct nfs_clone_mount mountdata = {
Al Virof8ad9c42011-03-16 06:32:07 -0400214 .sb = dentry->d_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400215 .dentry = dentry,
216 .fh = fh,
217 .fattr = fattr,
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000218 .authflavor = authflavor,
David Howellsf7b422b2006-06-09 09:34:33 -0400219 };
220 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
221 char *page = (char *) __get_free_page(GFP_USER);
222 char *devname;
223
David Howells54ceac42006-08-22 20:06:13 -0400224 dprintk("--> nfs_do_submount()\n");
225
Harvey Harrison3110ff82008-05-02 13:42:44 -0700226 dprintk("%s: submounting on %s/%s\n", __func__,
David Howellsf7b422b2006-06-09 09:34:33 -0400227 dentry->d_parent->d_name.name,
228 dentry->d_name.name);
229 if (page == NULL)
230 goto out;
Al Virob514f872011-03-16 06:26:11 -0400231 devname = nfs_devname(dentry, page, PAGE_SIZE);
David Howellsf7b422b2006-06-09 09:34:33 -0400232 mnt = (struct vfsmount *)devname;
233 if (IS_ERR(devname))
234 goto free_page;
Al Virof8ad9c42011-03-16 06:32:07 -0400235 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400236free_page:
237 free_page((unsigned long)page);
238out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700239 dprintk("%s: done\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400240
241 dprintk("<-- nfs_do_submount() = %p\n", mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400242 return mnt;
243}
Bryan Schumaker281cad42012-04-27 13:27:45 -0400244
245struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
246 struct nfs_fh *fh, struct nfs_fattr *fattr)
247{
248 int err;
249 struct dentry *parent = dget_parent(dentry);
250
251 /* Look it up again to get its attributes */
Bryan Schumaker80a16b22012-04-27 13:27:46 -0400252 err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400253 dput(parent);
254 if (err != 0)
255 return ERR_PTR(err);
256
257 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
258}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400259EXPORT_SYMBOL_GPL(nfs_submount);