blob: 9287eb666322c7d1030a8659fe8ea1a124933673 [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"
Trond Myklebust55a97592006-06-09 09:34:19 -040022
23#define NFSDBG_FACILITY NFSDBG_VFS
24
David Howells65f27f32006-11-22 14:55:48 +000025static void nfs_expire_automounts(struct work_struct *work);
David Howellsf7b422b2006-06-09 09:34:33 -040026
Adrian Bunka3dab292008-04-14 21:41:32 +030027static LIST_HEAD(nfs_automount_list);
David Howells65f27f32006-11-22 14:55:48 +000028static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
Trond Myklebust51d8fa62006-06-09 09:34:20 -040029int nfs_mountpoint_expiry_timeout = 500 * HZ;
30
Trond Myklebust55a97592006-06-09 09:34:19 -040031/*
David Howellsf7b422b2006-06-09 09:34:33 -040032 * nfs_path - reconstruct the path given an arbitrary dentry
Al Virob514f872011-03-16 06:26:11 -040033 * @base - used to return pointer to the end of devname part of path
David Howellsf7b422b2006-06-09 09:34:33 -040034 * @dentry - pointer to dentry
35 * @buffer - result buffer
36 * @buflen - length of buffer
Ben Hutchings97a54862012-10-21 19:23:52 +010037 * @flags - options (see below)
David Howellsf7b422b2006-06-09 09:34:33 -040038 *
Al Virob514f872011-03-16 06:26:11 -040039 * Helper function for constructing the server pathname
40 * by arbitrary hashed dentry.
David Howellsf7b422b2006-06-09 09:34:33 -040041 *
42 * This is mainly for use in figuring out the path on the
Al Virob514f872011-03-16 06:26:11 -040043 * server side when automounting on top of an existing partition
44 * and in generating /proc/mounts and friends.
Ben Hutchings97a54862012-10-21 19:23:52 +010045 *
46 * Supported flags:
47 * NFS_PATH_CANONICAL: ensure there is exactly one slash after
48 * the original device (export) name
49 * (if unset, the original name is returned verbatim)
David Howellsf7b422b2006-06-09 09:34:33 -040050 */
Ben Hutchings97a54862012-10-21 19:23:52 +010051char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
52 unsigned flags)
David Howellsf7b422b2006-06-09 09:34:33 -040053{
Nick Piggin949854d2011-01-07 17:49:37 +110054 char *end;
David Howellsf7b422b2006-06-09 09:34:33 -040055 int namelen;
Nick Piggin949854d2011-01-07 17:49:37 +110056 unsigned seq;
Al Virob514f872011-03-16 06:26:11 -040057 const char *base;
David Howellsf7b422b2006-06-09 09:34:33 -040058
Nick Piggin949854d2011-01-07 17:49:37 +110059rename_retry:
60 end = buffer+buflen;
David Howellsf7b422b2006-06-09 09:34:33 -040061 *--end = '\0';
62 buflen--;
Nick Piggin949854d2011-01-07 17:49:37 +110063
64 seq = read_seqbegin(&rename_lock);
65 rcu_read_lock();
Al Virob514f872011-03-16 06:26:11 -040066 while (1) {
67 spin_lock(&dentry->d_lock);
68 if (IS_ROOT(dentry))
69 break;
David Howellsf7b422b2006-06-09 09:34:33 -040070 namelen = dentry->d_name.len;
71 buflen -= namelen + 1;
72 if (buflen < 0)
Josh Triplettce510192006-07-24 16:30:00 -070073 goto Elong_unlock;
David Howellsf7b422b2006-06-09 09:34:33 -040074 end -= namelen;
75 memcpy(end, dentry->d_name.name, namelen);
76 *--end = '/';
Al Virob514f872011-03-16 06:26:11 -040077 spin_unlock(&dentry->d_lock);
David Howellsf7b422b2006-06-09 09:34:33 -040078 dentry = dentry->d_parent;
79 }
Al Virob514f872011-03-16 06:26:11 -040080 if (read_seqretry(&rename_lock, seq)) {
81 spin_unlock(&dentry->d_lock);
82 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +110083 goto rename_retry;
Al Virob514f872011-03-16 06:26:11 -040084 }
Ben Hutchings97a54862012-10-21 19:23:52 +010085 if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
Al Virob514f872011-03-16 06:26:11 -040086 if (--buflen < 0) {
87 spin_unlock(&dentry->d_lock);
88 rcu_read_unlock();
Trond Myklebust0b75b352009-06-22 15:09:14 -040089 goto Elong;
Al Virob514f872011-03-16 06:26:11 -040090 }
Trond Myklebust0b75b352009-06-22 15:09:14 -040091 *--end = '/';
92 }
Al Virob514f872011-03-16 06:26:11 -040093 *p = end;
94 base = dentry->d_fsdata;
95 if (!base) {
96 spin_unlock(&dentry->d_lock);
97 rcu_read_unlock();
98 WARN_ON(1);
99 return end;
100 }
David Howellsf7b422b2006-06-09 09:34:33 -0400101 namelen = strlen(base);
Benjamin Coddington86a6c212016-06-15 15:02:55 -0400102 if (*end == '/') {
Ben Hutchings97a54862012-10-21 19:23:52 +0100103 /* Strip off excess slashes in base string */
104 while (namelen > 0 && base[namelen - 1] == '/')
105 namelen--;
106 }
David Howellsf7b422b2006-06-09 09:34:33 -0400107 buflen -= namelen;
Al Virob514f872011-03-16 06:26:11 -0400108 if (buflen < 0) {
Dan Carpenter1c340922011-03-20 14:22:07 +0300109 spin_unlock(&dentry->d_lock);
Al Virob514f872011-03-16 06:26:11 -0400110 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400111 goto Elong;
Al Virob514f872011-03-16 06:26:11 -0400112 }
David Howellsf7b422b2006-06-09 09:34:33 -0400113 end -= namelen;
114 memcpy(end, base, namelen);
Al Virob514f872011-03-16 06:26:11 -0400115 spin_unlock(&dentry->d_lock);
116 rcu_read_unlock();
David Howellsf7b422b2006-06-09 09:34:33 -0400117 return end;
Josh Triplettce510192006-07-24 16:30:00 -0700118Elong_unlock:
Dan Carpenter1c340922011-03-20 14:22:07 +0300119 spin_unlock(&dentry->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100120 rcu_read_unlock();
121 if (read_seqretry(&rename_lock, seq))
122 goto rename_retry;
David Howellsf7b422b2006-06-09 09:34:33 -0400123Elong:
124 return ERR_PTR(-ENAMETOOLONG);
125}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400126EXPORT_SYMBOL_GPL(nfs_path);
David Howellsf7b422b2006-06-09 09:34:33 -0400127
128/*
David Howells36d43a42011-01-14 18:45:42 +0000129 * nfs_d_automount - Handle crossing a mountpoint on the server
130 * @path - The mountpoint
Trond Myklebust55a97592006-06-09 09:34:19 -0400131 *
132 * When we encounter a mountpoint on the server, we want to set up
133 * a mountpoint on the client too, to prevent inode numbers from
134 * colliding, and to allow "df" to work properly.
135 * On NFSv4, we also want to allow for the fact that different
136 * filesystems may be migrated to different servers in a failover
137 * situation, and that different filesystems may want to use
138 * different security flavours.
139 */
David Howells36d43a42011-01-14 18:45:42 +0000140struct vfsmount *nfs_d_automount(struct path *path)
Trond Myklebust55a97592006-06-09 09:34:19 -0400141{
142 struct vfsmount *mnt;
David Howells2b0143b2015-03-17 22:25:59 +0000143 struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400144 struct nfs_fh *fh = NULL;
145 struct nfs_fattr *fattr = NULL;
Trond Myklebust55a97592006-06-09 09:34:19 -0400146
David Howells36d43a42011-01-14 18:45:42 +0000147 if (IS_ROOT(path->dentry))
Anna Schumakere36d48e2017-04-07 14:15:09 -0400148 return ERR_PTR(-ESTALE);
Denis V. Lunev44d57592008-08-11 12:02:34 +0400149
David Howells36d43a42011-01-14 18:45:42 +0000150 mnt = ERR_PTR(-ENOMEM);
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400151 fh = nfs_alloc_fhandle();
152 fattr = nfs_alloc_fattr();
153 if (fh == NULL || fattr == NULL)
David Howells36d43a42011-01-14 18:45:42 +0000154 goto out;
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400155
Bryan Schumaker281cad42012-04-27 13:27:45 -0400156 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
Trond Myklebust55a97592006-06-09 09:34:19 -0400157 if (IS_ERR(mnt))
David Howells36d43a42011-01-14 18:45:42 +0000158 goto out;
Trond Myklebust55a97592006-06-09 09:34:19 -0400159
David Howellsea5b7782011-01-14 19:10:03 +0000160 mntget(mnt); /* prevent immediate expiration */
161 mnt_set_expiry(mnt, &nfs_automount_list);
162 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
David Howells36d43a42011-01-14 18:45:42 +0000163
Trond Myklebust55a97592006-06-09 09:34:19 -0400164out:
Trond Myklebusta4d7f162010-04-16 16:22:46 -0400165 nfs_free_fattr(fattr);
166 nfs_free_fhandle(fh);
David Howells36d43a42011-01-14 18:45:42 +0000167 return mnt;
Trond Myklebust55a97592006-06-09 09:34:19 -0400168}
169
Trond Myklebustab225412013-01-22 00:17:06 -0500170static int
David Howellsa528d352017-01-31 16:46:22 +0000171nfs_namespace_getattr(const struct path *path, struct kstat *stat,
172 u32 request_mask, unsigned int query_flags)
Trond Myklebustab225412013-01-22 00:17:06 -0500173{
David Howellsa528d352017-01-31 16:46:22 +0000174 if (NFS_FH(d_inode(path->dentry))->size != 0)
175 return nfs_getattr(path, stat, request_mask, query_flags);
176 generic_fillattr(d_inode(path->dentry), stat);
Trond Myklebustab225412013-01-22 00:17:06 -0500177 return 0;
178}
179
180static int
181nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
182{
David Howells2b0143b2015-03-17 22:25:59 +0000183 if (NFS_FH(d_inode(dentry))->size != 0)
Trond Myklebustab225412013-01-22 00:17:06 -0500184 return nfs_setattr(dentry, attr);
185 return -EACCES;
186}
187
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800188const struct inode_operations nfs_mountpoint_inode_operations = {
Trond Myklebust55a97592006-06-09 09:34:19 -0400189 .getattr = nfs_getattr,
Trond Myklebustab225412013-01-22 00:17:06 -0500190 .setattr = nfs_setattr,
Trond Myklebust55a97592006-06-09 09:34:19 -0400191};
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400192
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800193const struct inode_operations nfs_referral_inode_operations = {
Trond Myklebustab225412013-01-22 00:17:06 -0500194 .getattr = nfs_namespace_getattr,
195 .setattr = nfs_namespace_setattr,
Manoj Naik6b97fd32006-06-09 09:34:29 -0400196};
197
David Howells65f27f32006-11-22 14:55:48 +0000198static void nfs_expire_automounts(struct work_struct *work)
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400199{
David Howells65f27f32006-11-22 14:55:48 +0000200 struct list_head *list = &nfs_automount_list;
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400201
202 mark_mounts_for_expiry(list);
203 if (!list_empty(list))
204 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
205}
206
207void nfs_release_automount_timer(void)
208{
Trond Myklebust3d39c692007-08-07 15:28:33 -0400209 if (list_empty(&nfs_automount_list))
Trond Myklebust560aef72007-08-27 09:14:56 -0400210 cancel_delayed_work(&nfs_automount_task);
Trond Myklebust51d8fa62006-06-09 09:34:20 -0400211}
David Howellsf7b422b2006-06-09 09:34:33 -0400212
213/*
214 * Clone a mountpoint of the appropriate type
215 */
David Howells509de812006-08-22 20:06:11 -0400216static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
217 const char *devname,
David Howellsf7b422b2006-06-09 09:34:33 -0400218 struct nfs_clone_mount *mountdata)
219{
Eric W. Biederman93faccbb2017-02-01 06:06:16 +1300220 return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
David Howellsf7b422b2006-06-09 09:34:33 -0400221}
222
223/**
224 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
Trond Myklebust302fad72019-02-18 13:32:38 -0500225 * @dentry: parent directory
226 * @fh: filehandle for new root dentry
227 * @fattr: attributes for new root inode
228 * @authflavor: security flavor to use when performing the mount
David Howellsf7b422b2006-06-09 09:34:33 -0400229 *
230 */
Bryan Schumaker281cad42012-04-27 13:27:45 -0400231struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
232 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
David Howellsf7b422b2006-06-09 09:34:33 -0400233{
234 struct nfs_clone_mount mountdata = {
Al Virof8ad9c42011-03-16 06:32:07 -0400235 .sb = dentry->d_sb,
David Howellsf7b422b2006-06-09 09:34:33 -0400236 .dentry = dentry,
237 .fh = fh,
238 .fattr = fattr,
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000239 .authflavor = authflavor,
David Howellsf7b422b2006-06-09 09:34:33 -0400240 };
Anna Schumakere36d48e2017-04-07 14:15:09 -0400241 struct vfsmount *mnt;
David Howellsf7b422b2006-06-09 09:34:33 -0400242 char *page = (char *) __get_free_page(GFP_USER);
243 char *devname;
244
David Howellsf7b422b2006-06-09 09:34:33 -0400245 if (page == NULL)
Anna Schumakere36d48e2017-04-07 14:15:09 -0400246 return ERR_PTR(-ENOMEM);
David Howells54ceac42006-08-22 20:06:13 -0400247
Anna Schumakere36d48e2017-04-07 14:15:09 -0400248 devname = nfs_devname(dentry, page, PAGE_SIZE);
249 if (IS_ERR(devname))
Kees Cookfe3b81b2017-04-04 17:08:42 -0700250 mnt = ERR_CAST(devname);
Anna Schumakere36d48e2017-04-07 14:15:09 -0400251 else
252 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
253
254 free_page((unsigned long)page);
David Howellsf7b422b2006-06-09 09:34:33 -0400255 return mnt;
256}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400257EXPORT_SYMBOL_GPL(nfs_do_submount);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400258
259struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
260 struct nfs_fh *fh, struct nfs_fattr *fattr)
261{
262 int err;
263 struct dentry *parent = dget_parent(dentry);
264
265 /* Look it up again to get its attributes */
David Howells2b0143b2015-03-17 22:25:59 +0000266 err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
Bryan Schumaker281cad42012-04-27 13:27:45 -0400267 dput(parent);
268 if (err != 0)
269 return ERR_PTR(err);
270
271 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
272}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400273EXPORT_SYMBOL_GPL(nfs_submount);