blob: a76aeb0c292370fceea6d520b19ffdaf39a6f31a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Trond Myklebust55a97592006-06-09 09:34:19 -04002/*
3 * linux/fs/nfs/namespace.c
4 *
5 * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
David Howells54ceac42006-08-22 20:06:13 -04006 * - Modified by David Howells <dhowells@redhat.com>
Trond Myklebust55a97592006-06-09 09:34:19 -04007 *
8 * NFS namespace
9 */
10
Bryan Schumakerddda8e02012-07-30 16:05:23 -040011#include <linux/module.h>
Trond Myklebust55a97592006-06-09 09:34:19 -040012#include <linux/dcache.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Trond Myklebust55a97592006-06-09 09:34:19 -040014#include <linux/mount.h>
15#include <linux/namei.h>
16#include <linux/nfs_fs.h>
17#include <linux/string.h>
18#include <linux/sunrpc/clnt.h>
19#include <linux/vfs.h>
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000020#include <linux/sunrpc/gss_api.h>
David Howellsf7b422b2006-06-09 09:34:33 -040021#include "internal.h"
Al Viro250d69f2019-12-10 07:30:55 -050022#include "nfs.h"
Trond Myklebust55a97592006-06-09 09:34:19 -040023
24#define NFSDBG_FACILITY NFSDBG_VFS
25
David Howells65f27f32006-11-22 14:55:48 +000026static void nfs_expire_automounts(struct work_struct *work);
David Howellsf7b422b2006-06-09 09:34:33 -040027
Adrian Bunka3dab292008-04-14 21:41:32 +030028static LIST_HEAD(nfs_automount_list);
David Howells65f27f32006-11-22 14:55:48 +000029static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
Trond Myklebust51d8fa62006-06-09 09:34:20 -040030int nfs_mountpoint_expiry_timeout = 500 * HZ;
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
Ben Hutchings97a54862012-10-21 19:23:52 +010038 * @flags - options (see below)
David Howellsf7b422b2006-06-09 09:34:33 -040039 *
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.
Ben Hutchings97a54862012-10-21 19:23:52 +010046 *
47 * Supported flags:
48 * NFS_PATH_CANONICAL: ensure there is exactly one slash after
49 * the original device (export) name
50 * (if unset, the original name is returned verbatim)
David Howellsf7b422b2006-06-09 09:34:33 -040051 */
Ben Hutchings97a54862012-10-21 19:23:52 +010052char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
53 unsigned flags)
David Howellsf7b422b2006-06-09 09:34:33 -040054{
Nick Piggin949854d2011-01-07 17:49:37 +110055 char *end;
David Howellsf7b422b2006-06-09 09:34:33 -040056 int namelen;
Nick Piggin949854d2011-01-07 17:49:37 +110057 unsigned seq;
Al Virob514f872011-03-16 06:26:11 -040058 const char *base;
David Howellsf7b422b2006-06-09 09:34:33 -040059
Nick Piggin949854d2011-01-07 17:49:37 +110060rename_retry:
61 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040062 *--end = '\0';
63 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110064
65 seq = read_seqbegin(&rename_lock);
66 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040067 while (1) {
68 spin_lock(&dentry->d_lock);
69 if (IS_ROOT(dentry))
70 break;
David Howellsf7b422b2006-06-09 09:34:33 -040071 namelen = dentry->d_name.len;
72 buflen -= namelen + 1;
73 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070074 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040075 end -= namelen;
76 memcpy(end, dentry->d_name.name, namelen);
77 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040078 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040079 dentry = dentry->d_parent;
80 }
Al Virob514f872011-03-16 06:26:11 -040081 if (read_seqretry(&rename_lock, seq)) {
82 spin_unlock(&dentry->d_lock);
83 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110084 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040085 }
Ben Hutchings97a54862012-10-21 19:23:52 +010086 if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
Al Virob514f872011-03-16 06:26:11 -040087 if (--buflen < 0) {
88 spin_unlock(&dentry->d_lock);
89 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040090 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040091 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040092 *--end = '/';
93 }
Al Virob514f872011-03-16 06:26:11 -040094 *p = end;
95 base = dentry->d_fsdata;
96 if (!base) {
97 spin_unlock(&dentry->d_lock);
98 rcu_read_unlock();
99 WARN_ON(1);
100 return end;
101 }
David Howellsf7b422b2006-06-09 09:34:33 -0400102 namelen = strlen(base);
Benjamin Coddington86a6c212016-06-15 15:02:55 -0400103 if (*end == '/') {
Ben Hutchings97a54862012-10-21 19:23:52 +0100104 /* Strip off excess slashes in base string */
105 while (namelen > 0 && base[namelen - 1] == '/')
106 namelen--;
107 }
David Howellsf7b422b2006-06-09 09:34:33 -0400108 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -0400109 if (buflen < 0) {
Dan Carpenter1c340922011-03-20 14:22:07 +0300110 spin_unlock(&dentry->d_lock);
Al Virob514f872011-03-16 06:26:11 -0400111 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400112 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400113 }
David Howellsf7b422b2006-06-09 09:34:33 -0400114 end -= namelen;
115 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400116 spin_unlock(&dentry->d_lock);
117 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400118 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700119Elong_unlock:
Dan Carpenter1c340922011-03-20 14:22:07 +0300120 spin_unlock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100121 rcu_read_unlock();
122 if (read_seqretry(&rename_lock, seq))
123 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400124Elong:
125 return ERR_PTR(-ENAMETOOLONG);
126}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400127EXPORT_SYMBOL_GPL(nfs_path);
David Howellsf7b422b2006-06-09 09:34:33 -0400128
129/*
David Howells36d43a42011-01-14 18:45:42 +0000130 * nfs_d_automount - Handle crossing a mountpoint on the server
131 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400132 *
133 * When we encounter a mountpoint on the server, we want to set up
134 * a mountpoint on the client too, to prevent inode numbers from
135 * colliding, and to allow "df" to work properly.
136 * On NFSv4, we also want to allow for the fact that different
137 * filesystems may be migrated to different servers in a failover
138 * situation, and that different filesystems may want to use
139 * different security flavours.
140 */
David Howells36d43a42011-01-14 18:45:42 +0000141struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400142{
143 struct vfsmount *mnt;
David Howells2b0143b2015-03-17 22:25:59 +0000144 struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400145 struct nfs_fh *fh = NULL;
146 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400147
David Howells36d43a42011-01-14 18:45:42 +0000148 if (IS_ROOT(path->dentry))
Anna Schumakere36d48e2017-04-07 14:15:09 -0400149 return ERR_PTR(-ESTALE);
Denis V. Lunev44d57592008-08-11 12:02:34 +0400150
David Howells36d43a42011-01-14 18:45:42 +0000151 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400152 fh = nfs_alloc_fhandle();
153 fattr = nfs_alloc_fattr();
154 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000155 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400156
Bryan Schumaker281cad42012-04-27 13:27:45 -0400157 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400158 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000159 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400160
Trond Myklebust22a1ae92019-08-21 18:16:28 -0400161 if (nfs_mountpoint_expiry_timeout < 0)
162 goto out;
163
David Howellsea5b7782011-01-14 19:10:03 +0000164 mntget(mnt); /* prevent immediate expiration */
165 mnt_set_expiry(mnt, &nfs_automount_list);
166 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
David Howells36d43a42011-01-14 18:45:42 +0000167
Trond Myklebust55a97592006-06-09 09:34:19 -0400168out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400169 nfs_free_fattr(fattr);
170 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000171 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400172}
173
Trond Myklebustab225412013-01-22 00:17:06 -0500174static int
David Howellsa528d352017-01-31 16:46:22 +0000175nfs_namespace_getattr(const struct path *path, struct kstat *stat,
176 u32 request_mask, unsigned int query_flags)
Trond Myklebustab225412013-01-22 00:17:06 -0500177{
David Howellsa528d352017-01-31 16:46:22 +0000178 if (NFS_FH(d_inode(path->dentry))->size != 0)
179 return nfs_getattr(path, stat, request_mask, query_flags);
180 generic_fillattr(d_inode(path->dentry), stat);
Trond Myklebustab225412013-01-22 00:17:06 -0500181 return 0;
182}
183
184static int
185nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
186{
David Howells2b0143b2015-03-17 22:25:59 +0000187 if (NFS_FH(d_inode(dentry))->size != 0)
Trond Myklebustab225412013-01-22 00:17:06 -0500188 return nfs_setattr(dentry, attr);
189 return -EACCES;
190}
191
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800192const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400193 .getattr = nfs_getattr,
Trond Myklebustab225412013-01-22 00:17:06 -0500194 .setattr = nfs_setattr,
Trond Myklebust55a97592006-06-09 09:34:19 -0400195};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400196
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800197const struct inode_operations nfs_referral_inode_operations = {
Trond Myklebustab225412013-01-22 00:17:06 -0500198 .getattr = nfs_namespace_getattr,
199 .setattr = nfs_namespace_setattr,
Manoj Naik6b97fd32006-06-09 09:34:29 -0400200};
201
David Howells65f27f32006-11-22 14:55:48 +0000202static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400203{
David Howells65f27f32006-11-22 14:55:48 +0000204 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400205
206 mark_mounts_for_expiry(list);
207 if (!list_empty(list))
208 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
209}
210
211void nfs_release_automount_timer(void)
212{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400213 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400214 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400215}
David Howellsf7b422b2006-06-09 09:34:33 -0400216
David Howellsf7b422b2006-06-09 09:34:33 -0400217/**
218 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
Trond Myklebust302fad72019-02-18 13:32:38 -0500219 * @dentry: parent directory
220 * @fh: filehandle for new root dentry
221 * @fattr: attributes for new root inode
222 * @authflavor: security flavor to use when performing the mount
David Howellsf7b422b2006-06-09 09:34:33 -0400223 *
224 */
Bryan Schumaker281cad42012-04-27 13:27:45 -0400225struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
226 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
David Howellsf7b422b2006-06-09 09:34:33 -0400227{
Al Viro250d69f2019-12-10 07:30:55 -0500228 struct super_block *sb = dentry->d_sb;
David Howellsf7b422b2006-06-09 09:34:33 -0400229 struct nfs_clone_mount mountdata = {
Al Viro250d69f2019-12-10 07:30:55 -0500230 .sb = sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400231 .dentry = dentry,
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000232 .authflavor = authflavor,
David Howellsf7b422b2006-06-09 09:34:33 -0400233 };
Al Viro250d69f2019-12-10 07:30:55 -0500234 struct nfs_mount_info mount_info = {
235 .fill_super = nfs_clone_super,
236 .set_security = nfs_clone_sb_security,
237 .cloned = &mountdata,
238 .mntfh = fh,
239 };
240 struct nfs_subversion *nfs_mod = NFS_SB(sb)->nfs_client->cl_nfs_mod;
241 struct nfs_server *server;
Anna Schumakere36d48e2017-04-07 14:15:09 -0400242 struct vfsmount *mnt;
David Howellsf7b422b2006-06-09 09:34:33 -0400243 char *page = (char *) __get_free_page(GFP_USER);
244 char *devname;
245
David Howellsf7b422b2006-06-09 09:34:33 -0400246 if (page == NULL)
Anna Schumakere36d48e2017-04-07 14:15:09 -0400247 return ERR_PTR(-ENOMEM);
David Howells54ceac42006-08-22 20:06:13 -0400248
Al Viro250d69f2019-12-10 07:30:55 -0500249 server = nfs_mod->rpc_ops->clone_server(NFS_SB(sb), fh,
250 fattr, authflavor);
251 if (IS_ERR(server))
252 return ERR_CAST(server);
253
254 mount_info.server = server;
255
Anna Schumakere36d48e2017-04-07 14:15:09 -0400256 devname = nfs_devname(dentry, page, PAGE_SIZE);
257 if (IS_ERR(devname))
Kees Cookfe3b81b2017-04-04 17:08:42 -0700258 mnt = ERR_CAST(devname);
Anna Schumakere36d48e2017-04-07 14:15:09 -0400259 else
Al Viro250d69f2019-12-10 07:30:55 -0500260 mnt = vfs_submount(dentry, &nfs_xdev_fs_type, devname, &mount_info);
Anna Schumakere36d48e2017-04-07 14:15:09 -0400261
Al Viro250d69f2019-12-10 07:30:55 -0500262 if (mount_info.server)
263 nfs_free_server(mount_info.server);
Anna Schumakere36d48e2017-04-07 14:15:09 -0400264 free_page((unsigned long)page);
David Howellsf7b422b2006-06-09 09:34:33 -0400265 return mnt;
266}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400267EXPORT_SYMBOL_GPL(nfs_do_submount);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400268
269struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
270 struct nfs_fh *fh, struct nfs_fattr *fattr)
271{
272 int err;
273 struct dentry *parent = dget_parent(dentry);
274
275 /* Look it up again to get its attributes */
David Howells2b0143b2015-03-17 22:25:59 +0000276 err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400277 dput(parent);
278 if (err != 0)
279 return ERR_PTR(err);
280
281 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
282}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400283EXPORT_SYMBOL_GPL(nfs_submount);