• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/nfs/namespace.c
4  *
5  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
6  * - Modified by David Howells <dhowells@redhat.com>
7  *
8  * NFS namespace
9  */
10 
11 #include <linux/module.h>
12 #include <linux/dcache.h>
13 #include <linux/gfp.h>
14 #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>
20 #include <linux/sunrpc/gss_api.h>
21 #include "internal.h"
22 
23 #define NFSDBG_FACILITY		NFSDBG_VFS
24 
25 static void nfs_expire_automounts(struct work_struct *work);
26 
27 static LIST_HEAD(nfs_automount_list);
28 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
29 int nfs_mountpoint_expiry_timeout = 500 * HZ;
30 
31 /*
32  * nfs_path - reconstruct the path given an arbitrary dentry
33  * @base - used to return pointer to the end of devname part of path
34  * @dentry_in - pointer to dentry
35  * @buffer - result buffer
36  * @buflen_in - length of buffer
37  * @flags - options (see below)
38  *
39  * Helper function for constructing the server pathname
40  * by arbitrary hashed dentry.
41  *
42  * This is mainly for use in figuring out the path on the
43  * server side when automounting on top of an existing partition
44  * and in generating /proc/mounts and friends.
45  *
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)
50  */
nfs_path(char ** p,struct dentry * dentry_in,char * buffer,ssize_t buflen_in,unsigned flags)51 char *nfs_path(char **p, struct dentry *dentry_in, char *buffer,
52 	       ssize_t buflen_in, unsigned flags)
53 {
54 	char *end;
55 	int namelen;
56 	unsigned seq;
57 	const char *base;
58 	struct dentry *dentry;
59 	ssize_t buflen;
60 
61 rename_retry:
62 	buflen = buflen_in;
63 	dentry = dentry_in;
64 	end = buffer+buflen;
65 	*--end = '\0';
66 	buflen--;
67 
68 	seq = read_seqbegin(&rename_lock);
69 	rcu_read_lock();
70 	while (1) {
71 		spin_lock(&dentry->d_lock);
72 		if (IS_ROOT(dentry))
73 			break;
74 		namelen = dentry->d_name.len;
75 		buflen -= namelen + 1;
76 		if (buflen < 0)
77 			goto Elong_unlock;
78 		end -= namelen;
79 		memcpy(end, dentry->d_name.name, namelen);
80 		*--end = '/';
81 		spin_unlock(&dentry->d_lock);
82 		dentry = dentry->d_parent;
83 	}
84 	if (read_seqretry(&rename_lock, seq)) {
85 		spin_unlock(&dentry->d_lock);
86 		rcu_read_unlock();
87 		goto rename_retry;
88 	}
89 	if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
90 		if (--buflen < 0) {
91 			spin_unlock(&dentry->d_lock);
92 			rcu_read_unlock();
93 			goto Elong;
94 		}
95 		*--end = '/';
96 	}
97 	*p = end;
98 	base = dentry->d_fsdata;
99 	if (!base) {
100 		spin_unlock(&dentry->d_lock);
101 		rcu_read_unlock();
102 		WARN_ON(1);
103 		return end;
104 	}
105 	namelen = strlen(base);
106 	if (*end == '/') {
107 		/* Strip off excess slashes in base string */
108 		while (namelen > 0 && base[namelen - 1] == '/')
109 			namelen--;
110 	}
111 	buflen -= namelen;
112 	if (buflen < 0) {
113 		spin_unlock(&dentry->d_lock);
114 		rcu_read_unlock();
115 		goto Elong;
116 	}
117 	end -= namelen;
118 	memcpy(end, base, namelen);
119 	spin_unlock(&dentry->d_lock);
120 	rcu_read_unlock();
121 	return end;
122 Elong_unlock:
123 	spin_unlock(&dentry->d_lock);
124 	rcu_read_unlock();
125 	if (read_seqretry(&rename_lock, seq))
126 		goto rename_retry;
127 Elong:
128 	return ERR_PTR(-ENAMETOOLONG);
129 }
130 EXPORT_SYMBOL_GPL(nfs_path);
131 
132 /*
133  * nfs_d_automount - Handle crossing a mountpoint on the server
134  * @path - The mountpoint
135  *
136  * When we encounter a mountpoint on the server, we want to set up
137  * a mountpoint on the client too, to prevent inode numbers from
138  * colliding, and to allow "df" to work properly.
139  * On NFSv4, we also want to allow for the fact that different
140  * filesystems may be migrated to different servers in a failover
141  * situation, and that different filesystems may want to use
142  * different security flavours.
143  */
nfs_d_automount(struct path * path)144 struct vfsmount *nfs_d_automount(struct path *path)
145 {
146 	struct vfsmount *mnt;
147 	struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
148 	struct nfs_fh *fh = NULL;
149 	struct nfs_fattr *fattr = NULL;
150 
151 	if (IS_ROOT(path->dentry))
152 		return ERR_PTR(-ESTALE);
153 
154 	mnt = ERR_PTR(-ENOMEM);
155 	fh = nfs_alloc_fhandle();
156 	fattr = nfs_alloc_fattr();
157 	if (fh == NULL || fattr == NULL)
158 		goto out;
159 
160 	mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
161 	if (IS_ERR(mnt))
162 		goto out;
163 
164 	mntget(mnt); /* prevent immediate expiration */
165 	mnt_set_expiry(mnt, &nfs_automount_list);
166 	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
167 
168 out:
169 	nfs_free_fattr(fattr);
170 	nfs_free_fhandle(fh);
171 	return mnt;
172 }
173 
174 static int
nfs_namespace_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)175 nfs_namespace_getattr(const struct path *path, struct kstat *stat,
176 			u32 request_mask, unsigned int query_flags)
177 {
178 	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);
181 	return 0;
182 }
183 
184 static int
nfs_namespace_setattr(struct dentry * dentry,struct iattr * attr)185 nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
186 {
187 	if (NFS_FH(d_inode(dentry))->size != 0)
188 		return nfs_setattr(dentry, attr);
189 	return -EACCES;
190 }
191 
192 const struct inode_operations nfs_mountpoint_inode_operations = {
193 	.getattr	= nfs_getattr,
194 	.setattr	= nfs_setattr,
195 };
196 
197 const struct inode_operations nfs_referral_inode_operations = {
198 	.getattr	= nfs_namespace_getattr,
199 	.setattr	= nfs_namespace_setattr,
200 };
201 
nfs_expire_automounts(struct work_struct * work)202 static void nfs_expire_automounts(struct work_struct *work)
203 {
204 	struct list_head *list = &nfs_automount_list;
205 
206 	mark_mounts_for_expiry(list);
207 	if (!list_empty(list))
208 		schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
209 }
210 
nfs_release_automount_timer(void)211 void nfs_release_automount_timer(void)
212 {
213 	if (list_empty(&nfs_automount_list))
214 		cancel_delayed_work(&nfs_automount_task);
215 }
216 
217 /*
218  * Clone a mountpoint of the appropriate type
219  */
nfs_do_clone_mount(struct nfs_server * server,const char * devname,struct nfs_clone_mount * mountdata)220 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
221 					   const char *devname,
222 					   struct nfs_clone_mount *mountdata)
223 {
224 	return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
225 }
226 
227 /**
228  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
229  * @dentry: parent directory
230  * @fh: filehandle for new root dentry
231  * @fattr: attributes for new root inode
232  * @authflavor: security flavor to use when performing the mount
233  *
234  */
nfs_do_submount(struct dentry * dentry,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t authflavor)235 struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
236 				 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
237 {
238 	struct nfs_clone_mount mountdata = {
239 		.sb = dentry->d_sb,
240 		.dentry = dentry,
241 		.fh = fh,
242 		.fattr = fattr,
243 		.authflavor = authflavor,
244 	};
245 	struct vfsmount *mnt;
246 	char *page = (char *) __get_free_page(GFP_USER);
247 	char *devname;
248 
249 	if (page == NULL)
250 		return ERR_PTR(-ENOMEM);
251 
252 	devname = nfs_devname(dentry, page, PAGE_SIZE);
253 	if (IS_ERR(devname))
254 		mnt = ERR_CAST(devname);
255 	else
256 		mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
257 
258 	free_page((unsigned long)page);
259 	return mnt;
260 }
261 EXPORT_SYMBOL_GPL(nfs_do_submount);
262 
nfs_submount(struct nfs_server * server,struct dentry * dentry,struct nfs_fh * fh,struct nfs_fattr * fattr)263 struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
264 			      struct nfs_fh *fh, struct nfs_fattr *fattr)
265 {
266 	int err;
267 	struct dentry *parent = dget_parent(dentry);
268 
269 	/* Look it up again to get its attributes */
270 	err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
271 	dput(parent);
272 	if (err != 0)
273 		return ERR_PTR(err);
274 
275 	return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
276 }
277 EXPORT_SYMBOL_GPL(nfs_submount);
278