Blame view

fs/nfs/namespace.c 6.96 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
55a975937   Trond Myklebust   NFS: Ensure the c...
2
3
4
5
  /*
   * linux/fs/nfs/namespace.c
   *
   * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
54ceac451   David Howells   NFS: Share NFS su...
6
   * - Modified by David Howells <dhowells@redhat.com>
55a975937   Trond Myklebust   NFS: Ensure the c...
7
8
9
   *
   * NFS namespace
   */
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
10
  #include <linux/module.h>
55a975937   Trond Myklebust   NFS: Ensure the c...
11
  #include <linux/dcache.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
12
  #include <linux/gfp.h>
55a975937   Trond Myklebust   NFS: Ensure the c...
13
14
15
16
17
18
  #include <linux/mount.h>
  #include <linux/namei.h>
  #include <linux/nfs_fs.h>
  #include <linux/string.h>
  #include <linux/sunrpc/clnt.h>
  #include <linux/vfs.h>
7ebb93159   Bryan Schumaker   NFS: use secinfo ...
19
  #include <linux/sunrpc/gss_api.h>
f7b422b17   David Howells   NFS: Split fs/nfs...
20
  #include "internal.h"
55a975937   Trond Myklebust   NFS: Ensure the c...
21
22
  
  #define NFSDBG_FACILITY		NFSDBG_VFS
65f27f384   David Howells   WorkStruct: Pass ...
23
  static void nfs_expire_automounts(struct work_struct *work);
f7b422b17   David Howells   NFS: Split fs/nfs...
24

a3dab2935   Adrian Bunk   make nfs_automoun...
25
  static LIST_HEAD(nfs_automount_list);
65f27f384   David Howells   WorkStruct: Pass ...
26
  static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
51d8fa6a1   Trond Myklebust   NFS: Add timeout ...
27
  int nfs_mountpoint_expiry_timeout = 500 * HZ;
55a975937   Trond Myklebust   NFS: Ensure the c...
28
  /*
f7b422b17   David Howells   NFS: Split fs/nfs...
29
   * nfs_path - reconstruct the path given an arbitrary dentry
b514f872f   Al Viro   nfs: make nfs_pat...
30
   * @base - used to return pointer to the end of devname part of path
f7b422b17   David Howells   NFS: Split fs/nfs...
31
32
33
   * @dentry - pointer to dentry
   * @buffer - result buffer
   * @buflen - length of buffer
97a548682   Ben Hutchings   nfs: Show origina...
34
   * @flags - options (see below)
f7b422b17   David Howells   NFS: Split fs/nfs...
35
   *
b514f872f   Al Viro   nfs: make nfs_pat...
36
37
   * Helper function for constructing the server pathname
   * by arbitrary hashed dentry.
f7b422b17   David Howells   NFS: Split fs/nfs...
38
39
   *
   * This is mainly for use in figuring out the path on the
b514f872f   Al Viro   nfs: make nfs_pat...
40
41
   * server side when automounting on top of an existing partition
   * and in generating /proc/mounts and friends.
97a548682   Ben Hutchings   nfs: Show origina...
42
43
44
45
46
   *
   * Supported flags:
   * NFS_PATH_CANONICAL: ensure there is exactly one slash after
   *		       the original device (export) name
   *		       (if unset, the original name is returned verbatim)
f7b422b17   David Howells   NFS: Split fs/nfs...
47
   */
97a548682   Ben Hutchings   nfs: Show origina...
48
49
  char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
  	       unsigned flags)
f7b422b17   David Howells   NFS: Split fs/nfs...
50
  {
949854d02   Nick Piggin   fs: Use rename lo...
51
  	char *end;
f7b422b17   David Howells   NFS: Split fs/nfs...
52
  	int namelen;
949854d02   Nick Piggin   fs: Use rename lo...
53
  	unsigned seq;
b514f872f   Al Viro   nfs: make nfs_pat...
54
  	const char *base;
f7b422b17   David Howells   NFS: Split fs/nfs...
55

949854d02   Nick Piggin   fs: Use rename lo...
56
57
  rename_retry:
  	end = buffer+buflen;
f7b422b17   David Howells   NFS: Split fs/nfs...
58
59
  	*--end = '\0';
  	buflen--;
949854d02   Nick Piggin   fs: Use rename lo...
60
61
62
  
  	seq = read_seqbegin(&rename_lock);
  	rcu_read_lock();
b514f872f   Al Viro   nfs: make nfs_pat...
63
64
65
66
  	while (1) {
  		spin_lock(&dentry->d_lock);
  		if (IS_ROOT(dentry))
  			break;
f7b422b17   David Howells   NFS: Split fs/nfs...
67
68
69
  		namelen = dentry->d_name.len;
  		buflen -= namelen + 1;
  		if (buflen < 0)
ce5101932   Josh Triplett   NFS: Release dcac...
70
  			goto Elong_unlock;
f7b422b17   David Howells   NFS: Split fs/nfs...
71
72
73
  		end -= namelen;
  		memcpy(end, dentry->d_name.name, namelen);
  		*--end = '/';
b514f872f   Al Viro   nfs: make nfs_pat...
74
  		spin_unlock(&dentry->d_lock);
f7b422b17   David Howells   NFS: Split fs/nfs...
75
76
  		dentry = dentry->d_parent;
  	}
b514f872f   Al Viro   nfs: make nfs_pat...
77
78
79
  	if (read_seqretry(&rename_lock, seq)) {
  		spin_unlock(&dentry->d_lock);
  		rcu_read_unlock();
949854d02   Nick Piggin   fs: Use rename lo...
80
  		goto rename_retry;
b514f872f   Al Viro   nfs: make nfs_pat...
81
  	}
97a548682   Ben Hutchings   nfs: Show origina...
82
  	if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
b514f872f   Al Viro   nfs: make nfs_pat...
83
84
85
  		if (--buflen < 0) {
  			spin_unlock(&dentry->d_lock);
  			rcu_read_unlock();
0b75b35c7   Trond Myklebust   NFS: Fix nfs_path...
86
  			goto Elong;
b514f872f   Al Viro   nfs: make nfs_pat...
87
  		}
0b75b35c7   Trond Myklebust   NFS: Fix nfs_path...
88
89
  		*--end = '/';
  	}
b514f872f   Al Viro   nfs: make nfs_pat...
90
91
92
93
94
95
96
97
  	*p = end;
  	base = dentry->d_fsdata;
  	if (!base) {
  		spin_unlock(&dentry->d_lock);
  		rcu_read_unlock();
  		WARN_ON(1);
  		return end;
  	}
f7b422b17   David Howells   NFS: Split fs/nfs...
98
  	namelen = strlen(base);
86a6c211d   Benjamin Coddington   NFS: Trim extra s...
99
  	if (*end == '/') {
97a548682   Ben Hutchings   nfs: Show origina...
100
101
102
103
  		/* Strip off excess slashes in base string */
  		while (namelen > 0 && base[namelen - 1] == '/')
  			namelen--;
  	}
f7b422b17   David Howells   NFS: Split fs/nfs...
104
  	buflen -= namelen;
b514f872f   Al Viro   nfs: make nfs_pat...
105
  	if (buflen < 0) {
1c34092ad   Dan Carpenter   nfs: lock() vs un...
106
  		spin_unlock(&dentry->d_lock);
b514f872f   Al Viro   nfs: make nfs_pat...
107
  		rcu_read_unlock();
f7b422b17   David Howells   NFS: Split fs/nfs...
108
  		goto Elong;
b514f872f   Al Viro   nfs: make nfs_pat...
109
  	}
f7b422b17   David Howells   NFS: Split fs/nfs...
110
111
  	end -= namelen;
  	memcpy(end, base, namelen);
b514f872f   Al Viro   nfs: make nfs_pat...
112
113
  	spin_unlock(&dentry->d_lock);
  	rcu_read_unlock();
f7b422b17   David Howells   NFS: Split fs/nfs...
114
  	return end;
ce5101932   Josh Triplett   NFS: Release dcac...
115
  Elong_unlock:
1c34092ad   Dan Carpenter   nfs: lock() vs un...
116
  	spin_unlock(&dentry->d_lock);
949854d02   Nick Piggin   fs: Use rename lo...
117
118
119
  	rcu_read_unlock();
  	if (read_seqretry(&rename_lock, seq))
  		goto rename_retry;
f7b422b17   David Howells   NFS: Split fs/nfs...
120
121
122
  Elong:
  	return ERR_PTR(-ENAMETOOLONG);
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
123
  EXPORT_SYMBOL_GPL(nfs_path);
f7b422b17   David Howells   NFS: Split fs/nfs...
124
125
  
  /*
36d43a437   David Howells   NFS: Use d_automo...
126
127
   * nfs_d_automount - Handle crossing a mountpoint on the server
   * @path - The mountpoint
55a975937   Trond Myklebust   NFS: Ensure the c...
128
129
130
131
132
133
134
135
136
   *
   * When we encounter a mountpoint on the server, we want to set up
   * a mountpoint on the client too, to prevent inode numbers from
   * colliding, and to allow "df" to work properly.
   * On NFSv4, we also want to allow for the fact that different
   * filesystems may be migrated to different servers in a failover
   * situation, and that different filesystems may want to use
   * different security flavours.
   */
36d43a437   David Howells   NFS: Use d_automo...
137
  struct vfsmount *nfs_d_automount(struct path *path)
55a975937   Trond Myklebust   NFS: Ensure the c...
138
139
  {
  	struct vfsmount *mnt;
2b0143b5c   David Howells   VFS: normal files...
140
  	struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
a4d7f1680   Trond Myklebust   NFS: Reduce the s...
141
142
  	struct nfs_fh *fh = NULL;
  	struct nfs_fattr *fattr = NULL;
55a975937   Trond Myklebust   NFS: Ensure the c...
143

36d43a437   David Howells   NFS: Use d_automo...
144
  	if (IS_ROOT(path->dentry))
e36d48e9e   Anna Schumaker   NFS: Remove extra...
145
  		return ERR_PTR(-ESTALE);
44d5759d3   Denis V. Lunev   nfs: BUG_ON in nf...
146

36d43a437   David Howells   NFS: Use d_automo...
147
  	mnt = ERR_PTR(-ENOMEM);
a4d7f1680   Trond Myklebust   NFS: Reduce the s...
148
149
150
  	fh = nfs_alloc_fhandle();
  	fattr = nfs_alloc_fattr();
  	if (fh == NULL || fattr == NULL)
36d43a437   David Howells   NFS: Use d_automo...
151
  		goto out;
a4d7f1680   Trond Myklebust   NFS: Reduce the s...
152

281cad46b   Bryan Schumaker   NFS: Create a sub...
153
  	mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
55a975937   Trond Myklebust   NFS: Ensure the c...
154
  	if (IS_ERR(mnt))
36d43a437   David Howells   NFS: Use d_automo...
155
  		goto out;
55a975937   Trond Myklebust   NFS: Ensure the c...
156

ea5b778a8   David Howells   Unexport do_add_m...
157
158
159
  	mntget(mnt); /* prevent immediate expiration */
  	mnt_set_expiry(mnt, &nfs_automount_list);
  	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
36d43a437   David Howells   NFS: Use d_automo...
160

55a975937   Trond Myklebust   NFS: Ensure the c...
161
  out:
a4d7f1680   Trond Myklebust   NFS: Reduce the s...
162
163
  	nfs_free_fattr(fattr);
  	nfs_free_fhandle(fh);
36d43a437   David Howells   NFS: Use d_automo...
164
  	return mnt;
55a975937   Trond Myklebust   NFS: Ensure the c...
165
  }
ab2254178   Trond Myklebust   NFS: Don't silent...
166
  static int
a528d35e8   David Howells   statx: Add a syst...
167
168
  nfs_namespace_getattr(const struct path *path, struct kstat *stat,
  			u32 request_mask, unsigned int query_flags)
ab2254178   Trond Myklebust   NFS: Don't silent...
169
  {
a528d35e8   David Howells   statx: Add a syst...
170
171
172
  	if (NFS_FH(d_inode(path->dentry))->size != 0)
  		return nfs_getattr(path, stat, request_mask, query_flags);
  	generic_fillattr(d_inode(path->dentry), stat);
ab2254178   Trond Myklebust   NFS: Don't silent...
173
174
175
176
177
178
  	return 0;
  }
  
  static int
  nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
  {
2b0143b5c   David Howells   VFS: normal files...
179
  	if (NFS_FH(d_inode(dentry))->size != 0)
ab2254178   Trond Myklebust   NFS: Don't silent...
180
181
182
  		return nfs_setattr(dentry, attr);
  	return -EACCES;
  }
92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
183
  const struct inode_operations nfs_mountpoint_inode_operations = {
55a975937   Trond Myklebust   NFS: Ensure the c...
184
  	.getattr	= nfs_getattr,
ab2254178   Trond Myklebust   NFS: Don't silent...
185
  	.setattr	= nfs_setattr,
55a975937   Trond Myklebust   NFS: Ensure the c...
186
  };
51d8fa6a1   Trond Myklebust   NFS: Add timeout ...
187

92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
188
  const struct inode_operations nfs_referral_inode_operations = {
ab2254178   Trond Myklebust   NFS: Don't silent...
189
190
  	.getattr	= nfs_namespace_getattr,
  	.setattr	= nfs_namespace_setattr,
6b97fd3da   Manoj Naik   NFSv4: Follow a r...
191
  };
65f27f384   David Howells   WorkStruct: Pass ...
192
  static void nfs_expire_automounts(struct work_struct *work)
51d8fa6a1   Trond Myklebust   NFS: Add timeout ...
193
  {
65f27f384   David Howells   WorkStruct: Pass ...
194
  	struct list_head *list = &nfs_automount_list;
51d8fa6a1   Trond Myklebust   NFS: Add timeout ...
195
196
197
198
199
200
201
202
  
  	mark_mounts_for_expiry(list);
  	if (!list_empty(list))
  		schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
  }
  
  void nfs_release_automount_timer(void)
  {
3d39c691f   Trond Myklebust   NFS: Replace flus...
203
  	if (list_empty(&nfs_automount_list))
560aef745   Trond Myklebust   NFS: Fix use of c...
204
  		cancel_delayed_work(&nfs_automount_task);
51d8fa6a1   Trond Myklebust   NFS: Add timeout ...
205
  }
f7b422b17   David Howells   NFS: Split fs/nfs...
206
207
208
209
  
  /*
   * Clone a mountpoint of the appropriate type
   */
509de8111   David Howells   NFS: Add extra co...
210
211
  static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
  					   const char *devname,
f7b422b17   David Howells   NFS: Split fs/nfs...
212
213
  					   struct nfs_clone_mount *mountdata)
  {
93faccbbf   Eric W. Biederman   fs: Better permis...
214
  	return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
f7b422b17   David Howells   NFS: Split fs/nfs...
215
216
217
218
  }
  
  /**
   * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
302fad7bd   Trond Myklebust   NFS: Fix up docum...
219
220
221
222
   * @dentry: parent directory
   * @fh: filehandle for new root dentry
   * @fattr: attributes for new root inode
   * @authflavor: security flavor to use when performing the mount
f7b422b17   David Howells   NFS: Split fs/nfs...
223
224
   *
   */
281cad46b   Bryan Schumaker   NFS: Create a sub...
225
226
  struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
  				 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
f7b422b17   David Howells   NFS: Split fs/nfs...
227
228
  {
  	struct nfs_clone_mount mountdata = {
f8ad9c4ba   Al Viro   nfs: nfs_do_{ref,...
229
  		.sb = dentry->d_sb,
f7b422b17   David Howells   NFS: Split fs/nfs...
230
231
232
  		.dentry = dentry,
  		.fh = fh,
  		.fattr = fattr,
7ebb93159   Bryan Schumaker   NFS: use secinfo ...
233
  		.authflavor = authflavor,
f7b422b17   David Howells   NFS: Split fs/nfs...
234
  	};
e36d48e9e   Anna Schumaker   NFS: Remove extra...
235
  	struct vfsmount *mnt;
f7b422b17   David Howells   NFS: Split fs/nfs...
236
237
  	char *page = (char *) __get_free_page(GFP_USER);
  	char *devname;
f7b422b17   David Howells   NFS: Split fs/nfs...
238
  	if (page == NULL)
e36d48e9e   Anna Schumaker   NFS: Remove extra...
239
  		return ERR_PTR(-ENOMEM);
b514f872f   Al Viro   nfs: make nfs_pat...
240
  	devname = nfs_devname(dentry, page, PAGE_SIZE);
f7b422b17   David Howells   NFS: Split fs/nfs...
241
  	if (IS_ERR(devname))
fe3b81b44   Kees Cook   NFS: Use ERR_CAST...
242
  		mnt = ERR_CAST(devname);
e36d48e9e   Anna Schumaker   NFS: Remove extra...
243
244
  	else
  		mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
54ceac451   David Howells   NFS: Share NFS su...
245

e36d48e9e   Anna Schumaker   NFS: Remove extra...
246
  	free_page((unsigned long)page);
f7b422b17   David Howells   NFS: Split fs/nfs...
247
248
  	return mnt;
  }
89d77c8fa   Bryan Schumaker   NFS: Convert v4 i...
249
  EXPORT_SYMBOL_GPL(nfs_do_submount);
281cad46b   Bryan Schumaker   NFS: Create a sub...
250
251
252
253
254
255
256
257
  
  struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
  			      struct nfs_fh *fh, struct nfs_fattr *fattr)
  {
  	int err;
  	struct dentry *parent = dget_parent(dentry);
  
  	/* Look it up again to get its attributes */
2b0143b5c   David Howells   VFS: normal files...
258
  	err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
281cad46b   Bryan Schumaker   NFS: Create a sub...
259
260
261
262
263
264
  	dput(parent);
  	if (err != 0)
  		return ERR_PTR(err);
  
  	return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
  }
ddda8e0aa   Bryan Schumaker   NFS: Convert v2 i...
265
  EXPORT_SYMBOL_GPL(nfs_submount);