blob: 93e60e921f9261866d3ff6bfdf2c2c2a5472a0a4 [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
Ashish Sangwan247db732020-10-05 02:22:43 -070035 * @dentry_in - pointer to dentry
David Howellsf7b422b2006-06-09 09:34:33 -040036 * @buffer - result buffer
Ashish Sangwan247db732020-10-05 02:22:43 -070037 * @buflen_in - 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 */
Ashish Sangwan247db732020-10-05 02:22:43 -070052char *nfs_path(char **p, struct dentry *dentry_in, char *buffer,
53 ssize_t buflen_in, 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;
Ashish Sangwan247db732020-10-05 02:22:43 -070059 struct dentry *dentry;
60 ssize_t buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040061
Nick Piggin949854d2011-01-07 17:49:37 +110062rename_retry:
Ashish Sangwan247db732020-10-05 02:22:43 -070063 buflen = buflen_in;
64 dentry = dentry_in;
Nick Piggin949854d2011-01-07 17:49:37 +110065 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040066 *--end = '\0';
67 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110068
69 seq = read_seqbegin(&rename_lock);
70 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040071 while (1) {
72 spin_lock(&dentry->d_lock);
73 if (IS_ROOT(dentry))
74 break;
David Howellsf7b422b2006-06-09 09:34:33 -040075 namelen = dentry->d_name.len;
76 buflen -= namelen + 1;
77 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070078 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040079 end -= namelen;
80 memcpy(end, dentry->d_name.name, namelen);
81 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040082 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040083 dentry = dentry->d_parent;
84 }
Al Virob514f872011-03-16 06:26:11 -040085 if (read_seqretry(&rename_lock, seq)) {
86 spin_unlock(&dentry->d_lock);
87 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110088 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040089 }
Ben Hutchings97a54862012-10-21 19:23:52 +010090 if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
Al Virob514f872011-03-16 06:26:11 -040091 if (--buflen < 0) {
92 spin_unlock(&dentry->d_lock);
93 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040094 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040095 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040096 *--end = '/';
97 }
Al Virob514f872011-03-16 06:26:11 -040098 *p = end;
99 base = dentry->d_fsdata;
100 if (!base) {
101 spin_unlock(&dentry->d_lock);
102 rcu_read_unlock();
103 WARN_ON(1);
104 return end;
105 }
David Howellsf7b422b2006-06-09 09:34:33 -0400106 namelen = strlen(base);
Benjamin Coddington86a6c212016-06-15 15:02:55 -0400107 if (*end == '/') {
Ben Hutchings97a54862012-10-21 19:23:52 +0100108 /* Strip off excess slashes in base string */
109 while (namelen > 0 && base[namelen - 1] == '/')
110 namelen--;
111 }
David Howellsf7b422b2006-06-09 09:34:33 -0400112 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -0400113 if (buflen < 0) {
Dan Carpenter1c340922011-03-20 14:22:07 +0300114 spin_unlock(&dentry->d_lock);
Al Virob514f872011-03-16 06:26:11 -0400115 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400116 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400117 }
David Howellsf7b422b2006-06-09 09:34:33 -0400118 end -= namelen;
119 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400120 spin_unlock(&dentry->d_lock);
121 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400122 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700123Elong_unlock:
Dan Carpenter1c340922011-03-20 14:22:07 +0300124 spin_unlock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100125 rcu_read_unlock();
126 if (read_seqretry(&rename_lock, seq))
127 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400128Elong:
129 return ERR_PTR(-ENAMETOOLONG);
130}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400131EXPORT_SYMBOL_GPL(nfs_path);
David Howellsf7b422b2006-06-09 09:34:33 -0400132
133/*
David Howells36d43a42011-01-14 18:45:42 +0000134 * nfs_d_automount - Handle crossing a mountpoint on the server
135 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400136 *
137 * When we encounter a mountpoint on the server, we want to set up
138 * a mountpoint on the client too, to prevent inode numbers from
139 * colliding, and to allow "df" to work properly.
140 * On NFSv4, we also want to allow for the fact that different
141 * filesystems may be migrated to different servers in a failover
142 * situation, and that different filesystems may want to use
143 * different security flavours.
144 */
David Howells36d43a42011-01-14 18:45:42 +0000145struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400146{
David Howellsf2aedb72019-12-10 07:31:13 -0500147 struct nfs_fs_context *ctx;
148 struct fs_context *fc;
149 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
David Howells2b0143b2015-03-17 22:25:59 +0000150 struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
David Howellsf2aedb72019-12-10 07:31:13 -0500151 struct nfs_client *client = server->nfs_client;
Trond Myklebust75da9852020-04-02 10:34:36 -0400152 int timeout = READ_ONCE(nfs_mountpoint_expiry_timeout);
David Howellsf2aedb72019-12-10 07:31:13 -0500153 int ret;
Trond Myklebust55a97592006-06-09 09:34:19 -0400154
David Howells36d43a42011-01-14 18:45:42 +0000155 if (IS_ROOT(path->dentry))
Anna Schumakere36d48e2017-04-07 14:15:09 -0400156 return ERR_PTR(-ESTALE);
Denis V. Lunev44d57592008-08-11 12:02:34 +0400157
David Howellsf2aedb72019-12-10 07:31:13 -0500158 /* Open a new filesystem context, transferring parameters from the
159 * parent superblock, including the network namespace.
160 */
Scott Mayhew1821b262020-02-20 08:06:20 -0500161 fc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
David Howellsf2aedb72019-12-10 07:31:13 -0500162 if (IS_ERR(fc))
163 return ERR_CAST(fc);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400164
David Howellsf2aedb72019-12-10 07:31:13 -0500165 ctx = nfs_fc2context(fc);
166 ctx->clone_data.dentry = path->dentry;
167 ctx->clone_data.sb = path->dentry->d_sb;
168 ctx->clone_data.fattr = nfs_alloc_fattr();
169 if (!ctx->clone_data.fattr)
170 goto out_fc;
171
172 if (fc->net_ns != client->cl_net) {
173 put_net(fc->net_ns);
174 fc->net_ns = get_net(client->cl_net);
175 }
176
177 /* for submounts we want the same server; referrals will reassign */
178 memcpy(&ctx->nfs_server.address, &client->cl_addr, client->cl_addrlen);
179 ctx->nfs_server.addrlen = client->cl_addrlen;
180 ctx->nfs_server.port = server->port;
181
182 ctx->version = client->rpc_ops->version;
183 ctx->minorversion = client->cl_minorversion;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500184 ctx->nfs_mod = client->cl_nfs_mod;
185 __module_get(ctx->nfs_mod->owner);
David Howellsf2aedb72019-12-10 07:31:13 -0500186
187 ret = client->rpc_ops->submount(fc, server);
188 if (ret < 0) {
189 mnt = ERR_PTR(ret);
190 goto out_fc;
191 }
192
193 up_write(&fc->root->d_sb->s_umount);
194 mnt = vfs_create_mount(fc);
Trond Myklebust55a97592006-06-09 09:34:19 -0400195 if (IS_ERR(mnt))
David Howellsf2aedb72019-12-10 07:31:13 -0500196 goto out_fc;
Trond Myklebust55a97592006-06-09 09:34:19 -0400197
Trond Myklebust75da9852020-04-02 10:34:36 -0400198 mntget(mnt); /* prevent immediate expiration */
199 if (timeout <= 0)
David Howellsf2aedb72019-12-10 07:31:13 -0500200 goto out_fc;
Trond Myklebust22a1ae92019-08-21 18:16:28 -0400201
David Howellsea5b7782011-01-14 19:10:03 +0000202 mnt_set_expiry(mnt, &nfs_automount_list);
Trond Myklebust75da9852020-04-02 10:34:36 -0400203 schedule_delayed_work(&nfs_automount_task, timeout);
David Howells36d43a42011-01-14 18:45:42 +0000204
David Howellsf2aedb72019-12-10 07:31:13 -0500205out_fc:
206 put_fs_context(fc);
David Howells36d43a42011-01-14 18:45:42 +0000207 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400208}
209
Trond Myklebustab225412013-01-22 00:17:06 -0500210static int
Christian Brauner549c7292021-01-21 14:19:43 +0100211nfs_namespace_getattr(struct user_namespace *mnt_userns,
212 const struct path *path, struct kstat *stat,
213 u32 request_mask, unsigned int query_flags)
Trond Myklebustab225412013-01-22 00:17:06 -0500214{
David Howellsa528d352017-01-31 16:46:22 +0000215 if (NFS_FH(d_inode(path->dentry))->size != 0)
Christian Brauner549c7292021-01-21 14:19:43 +0100216 return nfs_getattr(mnt_userns, path, stat, request_mask,
217 query_flags);
Christian Brauner0d56a452021-01-21 14:19:30 +0100218 generic_fillattr(&init_user_ns, d_inode(path->dentry), stat);
Trond Myklebustab225412013-01-22 00:17:06 -0500219 return 0;
220}
221
222static int
Christian Brauner549c7292021-01-21 14:19:43 +0100223nfs_namespace_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
224 struct iattr *attr)
Trond Myklebustab225412013-01-22 00:17:06 -0500225{
David Howells2b0143b2015-03-17 22:25:59 +0000226 if (NFS_FH(d_inode(dentry))->size != 0)
Christian Brauner549c7292021-01-21 14:19:43 +0100227 return nfs_setattr(mnt_userns, dentry, attr);
Trond Myklebustab225412013-01-22 00:17:06 -0500228 return -EACCES;
229}
230
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800231const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400232 .getattr = nfs_getattr,
Trond Myklebustab225412013-01-22 00:17:06 -0500233 .setattr = nfs_setattr,
Trond Myklebust55a97592006-06-09 09:34:19 -0400234};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400235
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800236const struct inode_operations nfs_referral_inode_operations = {
Trond Myklebustab225412013-01-22 00:17:06 -0500237 .getattr = nfs_namespace_getattr,
238 .setattr = nfs_namespace_setattr,
Manoj Naik6b97fd32006-06-09 09:34:29 -0400239};
240
David Howells65f27f32006-11-22 14:55:48 +0000241static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400242{
David Howells65f27f32006-11-22 14:55:48 +0000243 struct list_head *list = &nfs_automount_list;
Trond Myklebust75da9852020-04-02 10:34:36 -0400244 int timeout = READ_ONCE(nfs_mountpoint_expiry_timeout);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400245
246 mark_mounts_for_expiry(list);
Trond Myklebust75da9852020-04-02 10:34:36 -0400247 if (!list_empty(list) && timeout > 0)
248 schedule_delayed_work(&nfs_automount_task, timeout);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400249}
250
251void nfs_release_automount_timer(void)
252{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400253 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400254 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400255}
David Howellsf7b422b2006-06-09 09:34:33 -0400256
David Howellsf7b422b2006-06-09 09:34:33 -0400257/**
258 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
Trond Myklebust3cab1852020-03-16 11:37:31 -0400259 * @fc: pointer to struct nfs_fs_context
David Howellsf7b422b2006-06-09 09:34:33 -0400260 *
261 */
David Howellsf2aedb72019-12-10 07:31:13 -0500262int nfs_do_submount(struct fs_context *fc)
David Howellsf7b422b2006-06-09 09:34:33 -0400263{
David Howellsf2aedb72019-12-10 07:31:13 -0500264 struct nfs_fs_context *ctx = nfs_fc2context(fc);
265 struct dentry *dentry = ctx->clone_data.dentry;
Al Viro250d69f2019-12-10 07:30:55 -0500266 struct nfs_server *server;
David Howellsf2aedb72019-12-10 07:31:13 -0500267 char *buffer, *p;
268 int ret;
David Howellsf7b422b2006-06-09 09:34:33 -0400269
David Howellsf2aedb72019-12-10 07:31:13 -0500270 /* create a new volume representation */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500271 server = ctx->nfs_mod->rpc_ops->clone_server(NFS_SB(ctx->clone_data.sb),
272 ctx->mntfh,
David Howellsf2aedb72019-12-10 07:31:13 -0500273 ctx->clone_data.fattr,
274 ctx->selected_flavor);
David Howells54ceac42006-08-22 20:06:13 -0400275
Al Viro250d69f2019-12-10 07:30:55 -0500276 if (IS_ERR(server))
David Howellsf2aedb72019-12-10 07:31:13 -0500277 return PTR_ERR(server);
Al Viro250d69f2019-12-10 07:30:55 -0500278
Scott Mayhew62a55d02019-12-10 07:31:14 -0500279 ctx->server = server;
Al Viro250d69f2019-12-10 07:30:55 -0500280
David Howellsf2aedb72019-12-10 07:31:13 -0500281 buffer = kmalloc(4096, GFP_USER);
282 if (!buffer)
283 return -ENOMEM;
Anna Schumakere36d48e2017-04-07 14:15:09 -0400284
David Howellsf2aedb72019-12-10 07:31:13 -0500285 ctx->internal = true;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500286 ctx->clone_data.inherited_bsize = ctx->clone_data.sb->s_blocksize_bits;
David Howellsf2aedb72019-12-10 07:31:13 -0500287
288 p = nfs_devname(dentry, buffer, 4096);
289 if (IS_ERR(p)) {
Scott Mayhewce8866f02019-12-10 07:31:15 -0500290 nfs_errorf(fc, "NFS: Couldn't determine submount pathname");
David Howellsf2aedb72019-12-10 07:31:13 -0500291 ret = PTR_ERR(p);
292 } else {
293 ret = vfs_parse_fs_string(fc, "source", p, buffer + 4096 - p);
294 if (!ret)
295 ret = vfs_get_tree(fc);
296 }
297 kfree(buffer);
298 return ret;
David Howellsf7b422b2006-06-09 09:34:33 -0400299}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400300EXPORT_SYMBOL_GPL(nfs_do_submount);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400301
David Howellsf2aedb72019-12-10 07:31:13 -0500302int nfs_submount(struct fs_context *fc, struct nfs_server *server)
Bryan Schumaker281cad42012-04-27 13:27:45 -0400303{
David Howellsf2aedb72019-12-10 07:31:13 -0500304 struct nfs_fs_context *ctx = nfs_fc2context(fc);
305 struct dentry *dentry = ctx->clone_data.dentry;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400306 struct dentry *parent = dget_parent(dentry);
David Howellsf2aedb72019-12-10 07:31:13 -0500307 int err;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400308
309 /* Look it up again to get its attributes */
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500310 err = server->nfs_client->rpc_ops->lookup(d_inode(parent), dentry,
Scott Mayhew62a55d02019-12-10 07:31:14 -0500311 ctx->mntfh, ctx->clone_data.fattr,
David Howellsf2aedb72019-12-10 07:31:13 -0500312 NULL);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400313 dput(parent);
314 if (err != 0)
David Howellsf2aedb72019-12-10 07:31:13 -0500315 return err;
Bryan Schumaker281cad42012-04-27 13:27:45 -0400316
David Howellsf2aedb72019-12-10 07:31:13 -0500317 ctx->selected_flavor = server->client->cl_auth->au_flavor;
318 return nfs_do_submount(fc);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400319}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400320EXPORT_SYMBOL_GPL(nfs_submount);
Trond Myklebustf30a6ea2020-04-02 12:37:25 -0400321
322static int param_set_nfs_timeout(const char *val, const struct kernel_param *kp)
323{
324 long num;
325 int ret;
326
327 if (!val)
328 return -EINVAL;
329 ret = kstrtol(val, 0, &num);
330 if (ret)
331 return -EINVAL;
332 if (num > 0) {
333 if (num >= INT_MAX / HZ)
334 num = INT_MAX;
335 else
336 num *= HZ;
337 *((int *)kp->arg) = num;
338 if (!list_empty(&nfs_automount_list))
339 mod_delayed_work(system_wq, &nfs_automount_task, num);
340 } else {
341 *((int *)kp->arg) = -1*HZ;
342 cancel_delayed_work(&nfs_automount_task);
343 }
344 return 0;
345}
346
347static int param_get_nfs_timeout(char *buffer, const struct kernel_param *kp)
348{
349 long num = *((int *)kp->arg);
350
351 if (num > 0) {
352 if (num >= INT_MAX - (HZ - 1))
353 num = INT_MAX / HZ;
354 else
355 num = (num + (HZ - 1)) / HZ;
356 } else
357 num = -1;
358 return scnprintf(buffer, PAGE_SIZE, "%li\n", num);
359}
360
361static const struct kernel_param_ops param_ops_nfs_timeout = {
362 .set = param_set_nfs_timeout,
363 .get = param_get_nfs_timeout,
364};
365#define param_check_nfs_timeout(name, p) __param_check(name, p, int);
366
367module_param(nfs_mountpoint_expiry_timeout, nfs_timeout, 0644);
368MODULE_PARM_DESC(nfs_mountpoint_expiry_timeout,
369 "Set the NFS automounted mountpoint timeout value (seconds)."
370 "Values <= 0 turn expiration off.");