blob: 655925373b9161c6a6c64ad157002d10cf8064e2 [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}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400116EXPORT_SYMBOL_GPL(nfs_path);
David Howellsf7b422b2006-06-09 09:34:33 -0400117
118/*
David Howells36d43a42011-01-14 18:45:42 +0000119 * nfs_d_automount - Handle crossing a mountpoint on the server
120 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400121 *
122 * When we encounter a mountpoint on the server, we want to set up
123 * a mountpoint on the client too, to prevent inode numbers from
124 * colliding, and to allow "df" to work properly.
125 * On NFSv4, we also want to allow for the fact that different
126 * filesystems may be migrated to different servers in a failover
127 * situation, and that different filesystems may want to use
128 * different security flavours.
129 */
David Howells36d43a42011-01-14 18:45:42 +0000130struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400131{
132 struct vfsmount *mnt;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400133 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400134 struct nfs_fh *fh = NULL;
135 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400136
David Howells36d43a42011-01-14 18:45:42 +0000137 dprintk("--> nfs_d_automount()\n");
David Howells54ceac42006-08-22 20:06:13 -0400138
David Howells36d43a42011-01-14 18:45:42 +0000139 mnt = ERR_PTR(-ESTALE);
140 if (IS_ROOT(path->dentry))
141 goto out_nofree;
Denis V. Lunev44d57592008-08-11 12:02:34 +0400142
David Howells36d43a42011-01-14 18:45:42 +0000143 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400144 fh = nfs_alloc_fhandle();
145 fattr = nfs_alloc_fattr();
146 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000147 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400148
Harvey Harrison3110ff82008-05-02 13:42:44 -0700149 dprintk("%s: enter\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400150
Bryan Schumaker281cad42012-04-27 13:27:45 -0400151 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400152 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000153 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400154
David Howellsea5b7782011-01-14 19:10:03 +0000155 dprintk("%s: done, success\n", __func__);
156 mntget(mnt); /* prevent immediate expiration */
157 mnt_set_expiry(mnt, &nfs_automount_list);
158 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
David Howells36d43a42011-01-14 18:45:42 +0000159
Trond Myklebust55a97592006-06-09 09:34:19 -0400160out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400161 nfs_free_fattr(fattr);
162 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000163out_nofree:
Chuck Leverd7c32672012-02-15 16:35:17 -0500164 if (IS_ERR(mnt))
165 dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
166 else
167 dprintk("<-- %s() = %p\n", __func__, mnt);
David Howells36d43a42011-01-14 18:45:42 +0000168 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400169}
170
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800171const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400172 .getattr = nfs_getattr,
173};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400174
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800175const struct inode_operations nfs_referral_inode_operations = {
Manoj Naik6b97fd32006-06-09 09:34:29 -0400176};
177
David Howells65f27f32006-11-22 14:55:48 +0000178static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400179{
David Howells65f27f32006-11-22 14:55:48 +0000180 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400181
182 mark_mounts_for_expiry(list);
183 if (!list_empty(list))
184 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
185}
186
187void nfs_release_automount_timer(void)
188{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400189 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400190 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400191}
David Howellsf7b422b2006-06-09 09:34:33 -0400192
193/*
194 * Clone a mountpoint of the appropriate type
195 */
David Howells509de812006-08-22 20:06:11 -0400196static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
197 const char *devname,
David Howellsf7b422b2006-06-09 09:34:33 -0400198 struct nfs_clone_mount *mountdata)
199{
David Howells54ceac42006-08-22 20:06:13 -0400200 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400201}
202
203/**
204 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
David Howellsf7b422b2006-06-09 09:34:33 -0400205 * @dentry - parent directory
206 * @fh - filehandle for new root dentry
207 * @fattr - attributes for new root inode
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000208 * @authflavor - security flavor to use when performing the mount
David Howellsf7b422b2006-06-09 09:34:33 -0400209 *
210 */
Bryan Schumaker281cad42012-04-27 13:27:45 -0400211struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
212 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
David Howellsf7b422b2006-06-09 09:34:33 -0400213{
214 struct nfs_clone_mount mountdata = {
Al Virof8ad9c42011-03-16 06:32:07 -0400215 .sb = dentry->d_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400216 .dentry = dentry,
217 .fh = fh,
218 .fattr = fattr,
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000219 .authflavor = authflavor,
David Howellsf7b422b2006-06-09 09:34:33 -0400220 };
221 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
222 char *page = (char *) __get_free_page(GFP_USER);
223 char *devname;
224
David Howells54ceac42006-08-22 20:06:13 -0400225 dprintk("--> nfs_do_submount()\n");
226
Harvey Harrison3110ff82008-05-02 13:42:44 -0700227 dprintk("%s: submounting on %s/%s\n", __func__,
David Howellsf7b422b2006-06-09 09:34:33 -0400228 dentry->d_parent->d_name.name,
229 dentry->d_name.name);
230 if (page == NULL)
231 goto out;
Al Virob514f872011-03-16 06:26:11 -0400232 devname = nfs_devname(dentry, page, PAGE_SIZE);
David Howellsf7b422b2006-06-09 09:34:33 -0400233 mnt = (struct vfsmount *)devname;
234 if (IS_ERR(devname))
235 goto free_page;
Al Virof8ad9c42011-03-16 06:32:07 -0400236 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400237free_page:
238 free_page((unsigned long)page);
239out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700240 dprintk("%s: done\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400241
242 dprintk("<-- nfs_do_submount() = %p\n", mnt);
David Howellsf7b422b2006-06-09 09:34:33 -0400243 return mnt;
244}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400245EXPORT_SYMBOL_GPL(nfs_do_submount);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400246
247struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
248 struct nfs_fh *fh, struct nfs_fattr *fattr)
249{
250 int err;
251 struct dentry *parent = dget_parent(dentry);
252
253 /* Look it up again to get its attributes */
Bryan Schumaker80a16b22012-04-27 13:27:46 -0400254 err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400255 dput(parent);
256 if (err != 0)
257 return ERR_PTR(err);
258
259 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
260}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400261EXPORT_SYMBOL_GPL(nfs_submount);