blob: da67820462f2a8d9c75a3ee9d23a8b9f27705126 [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{
David Howellsf2aedb72019-12-10 07:31:13 -0500143 struct nfs_fs_context *ctx;
144 struct fs_context *fc;
145 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
David Howells2b0143b2015-03-17 22:25:59 +0000146 struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
David Howellsf2aedb72019-12-10 07:31:13 -0500147 struct nfs_client *client = server->nfs_client;
148 int ret;
Trond Myklebust55a97592006-06-09 09:34:19 -0400149
David Howells36d43a42011-01-14 18:45:42 +0000150 if (IS_ROOT(path->dentry))
Anna Schumakere36d48e2017-04-07 14:15:09 -0400151 return ERR_PTR(-ESTALE);
Denis V. Lunev44d57592008-08-11 12:02:34 +0400152
David Howellsf2aedb72019-12-10 07:31:13 -0500153 /* Open a new filesystem context, transferring parameters from the
154 * parent superblock, including the network namespace.
155 */
Scott Mayhew1821b262020-02-20 08:06:20 -0500156 fc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
David Howellsf2aedb72019-12-10 07:31:13 -0500157 if (IS_ERR(fc))
158 return ERR_CAST(fc);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400159
David Howellsf2aedb72019-12-10 07:31:13 -0500160 ctx = nfs_fc2context(fc);
161 ctx->clone_data.dentry = path->dentry;
162 ctx->clone_data.sb = path->dentry->d_sb;
163 ctx->clone_data.fattr = nfs_alloc_fattr();
164 if (!ctx->clone_data.fattr)
165 goto out_fc;
166
167 if (fc->net_ns != client->cl_net) {
168 put_net(fc->net_ns);
169 fc->net_ns = get_net(client->cl_net);
170 }
171
172 /* for submounts we want the same server; referrals will reassign */
173 memcpy(&ctx->nfs_server.address, &client->cl_addr, client->cl_addrlen);
174 ctx->nfs_server.addrlen = client->cl_addrlen;
175 ctx->nfs_server.port = server->port;
176
177 ctx->version = client->rpc_ops->version;
178 ctx->minorversion = client->cl_minorversion;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500179 ctx->nfs_mod = client->cl_nfs_mod;
180 __module_get(ctx->nfs_mod->owner);
David Howellsf2aedb72019-12-10 07:31:13 -0500181
182 ret = client->rpc_ops->submount(fc, server);
183 if (ret < 0) {
184 mnt = ERR_PTR(ret);
185 goto out_fc;
186 }
187
188 up_write(&fc->root->d_sb->s_umount);
189 mnt = vfs_create_mount(fc);
Trond Myklebust55a97592006-06-09 09:34:19 -0400190 if (IS_ERR(mnt))
David Howellsf2aedb72019-12-10 07:31:13 -0500191 goto out_fc;
Trond Myklebust55a97592006-06-09 09:34:19 -0400192
Trond Myklebust22a1ae92019-08-21 18:16:28 -0400193 if (nfs_mountpoint_expiry_timeout < 0)
David Howellsf2aedb72019-12-10 07:31:13 -0500194 goto out_fc;
Trond Myklebust22a1ae92019-08-21 18:16:28 -0400195
David Howellsea5b7782011-01-14 19:10:03 +0000196 mntget(mnt); /* prevent immediate expiration */
197 mnt_set_expiry(mnt, &nfs_automount_list);
198 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
David Howells36d43a42011-01-14 18:45:42 +0000199
David Howellsf2aedb72019-12-10 07:31:13 -0500200out_fc:
201 put_fs_context(fc);
David Howells36d43a42011-01-14 18:45:42 +0000202 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400203}
204
Trond Myklebustab225412013-01-22 00:17:06 -0500205static int
David Howellsa528d352017-01-31 16:46:22 +0000206nfs_namespace_getattr(const struct path *path, struct kstat *stat,
207 u32 request_mask, unsigned int query_flags)
Trond Myklebustab225412013-01-22 00:17:06 -0500208{
David Howellsa528d352017-01-31 16:46:22 +0000209 if (NFS_FH(d_inode(path->dentry))->size != 0)
210 return nfs_getattr(path, stat, request_mask, query_flags);
211 generic_fillattr(d_inode(path->dentry), stat);
Trond Myklebustab225412013-01-22 00:17:06 -0500212 return 0;
213}
214
215static int
216nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
217{
David Howells2b0143b2015-03-17 22:25:59 +0000218 if (NFS_FH(d_inode(dentry))->size != 0)
Trond Myklebustab225412013-01-22 00:17:06 -0500219 return nfs_setattr(dentry, attr);
220 return -EACCES;
221}
222
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800223const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400224 .getattr = nfs_getattr,
Trond Myklebustab225412013-01-22 00:17:06 -0500225 .setattr = nfs_setattr,
Trond Myklebust55a97592006-06-09 09:34:19 -0400226};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400227
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800228const struct inode_operations nfs_referral_inode_operations = {
Trond Myklebustab225412013-01-22 00:17:06 -0500229 .getattr = nfs_namespace_getattr,
230 .setattr = nfs_namespace_setattr,
Manoj Naik6b97fd32006-06-09 09:34:29 -0400231};
232
David Howells65f27f32006-11-22 14:55:48 +0000233static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400234{
David Howells65f27f32006-11-22 14:55:48 +0000235 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400236
237 mark_mounts_for_expiry(list);
238 if (!list_empty(list))
239 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
240}
241
242void nfs_release_automount_timer(void)
243{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400244 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400245 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400246}
David Howellsf7b422b2006-06-09 09:34:33 -0400247
David Howellsf7b422b2006-06-09 09:34:33 -0400248/**
249 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
Trond Myklebust3cab1852020-03-16 11:37:31 -0400250 * @fc: pointer to struct nfs_fs_context
David Howellsf7b422b2006-06-09 09:34:33 -0400251 *
252 */
David Howellsf2aedb72019-12-10 07:31:13 -0500253int nfs_do_submount(struct fs_context *fc)
David Howellsf7b422b2006-06-09 09:34:33 -0400254{
David Howellsf2aedb72019-12-10 07:31:13 -0500255 struct nfs_fs_context *ctx = nfs_fc2context(fc);
256 struct dentry *dentry = ctx->clone_data.dentry;
Al Viro250d69f2019-12-10 07:30:55 -0500257 struct nfs_server *server;
David Howellsf2aedb72019-12-10 07:31:13 -0500258 char *buffer, *p;
259 int ret;
David Howellsf7b422b2006-06-09 09:34:33 -0400260
David Howellsf2aedb72019-12-10 07:31:13 -0500261 /* create a new volume representation */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500262 server = ctx->nfs_mod->rpc_ops->clone_server(NFS_SB(ctx->clone_data.sb),
263 ctx->mntfh,
David Howellsf2aedb72019-12-10 07:31:13 -0500264 ctx->clone_data.fattr,
265 ctx->selected_flavor);
David Howells54ceac42006-08-22 20:06:13 -0400266
Al Viro250d69f2019-12-10 07:30:55 -0500267 if (IS_ERR(server))
David Howellsf2aedb72019-12-10 07:31:13 -0500268 return PTR_ERR(server);
Al Viro250d69f2019-12-10 07:30:55 -0500269
Scott Mayhew62a55d02019-12-10 07:31:14 -0500270 ctx->server = server;
Al Viro250d69f2019-12-10 07:30:55 -0500271
David Howellsf2aedb72019-12-10 07:31:13 -0500272 buffer = kmalloc(4096, GFP_USER);
273 if (!buffer)
274 return -ENOMEM;
Anna Schumakere36d48e2017-04-07 14:15:09 -0400275
David Howellsf2aedb72019-12-10 07:31:13 -0500276 ctx->internal = true;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500277 ctx->clone_data.inherited_bsize = ctx->clone_data.sb->s_blocksize_bits;
David Howellsf2aedb72019-12-10 07:31:13 -0500278
279 p = nfs_devname(dentry, buffer, 4096);
280 if (IS_ERR(p)) {
Scott Mayhewce8866f02019-12-10 07:31:15 -0500281 nfs_errorf(fc, "NFS: Couldn't determine submount pathname");
David Howellsf2aedb72019-12-10 07:31:13 -0500282 ret = PTR_ERR(p);
283 } else {
284 ret = vfs_parse_fs_string(fc, "source", p, buffer + 4096 - p);
285 if (!ret)
286 ret = vfs_get_tree(fc);
287 }
288 kfree(buffer);
289 return ret;
David Howellsf7b422b2006-06-09 09:34:33 -0400290}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400291EXPORT_SYMBOL_GPL(nfs_do_submount);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400292
David Howellsf2aedb72019-12-10 07:31:13 -0500293int nfs_submount(struct fs_context *fc, struct nfs_server *server)
Bryan Schumaker281cad42012-04-27 13:27:45 -0400294{
David Howellsf2aedb72019-12-10 07:31:13 -0500295 struct nfs_fs_context *ctx = nfs_fc2context(fc);
296 struct dentry *dentry = ctx->clone_data.dentry;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400297 struct dentry *parent = dget_parent(dentry);
David Howellsf2aedb72019-12-10 07:31:13 -0500298 int err;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400299
300 /* Look it up again to get its attributes */
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500301 err = server->nfs_client->rpc_ops->lookup(d_inode(parent), dentry,
Scott Mayhew62a55d02019-12-10 07:31:14 -0500302 ctx->mntfh, ctx->clone_data.fattr,
David Howellsf2aedb72019-12-10 07:31:13 -0500303 NULL);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400304 dput(parent);
305 if (err != 0)
David Howellsf2aedb72019-12-10 07:31:13 -0500306 return err;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400307
David Howellsf2aedb72019-12-10 07:31:13 -0500308 ctx->selected_flavor = server->client->cl_auth->au_flavor;
309 return nfs_do_submount(fc);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400310}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400311EXPORT_SYMBOL_GPL(nfs_submount);