• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/fs/nfs/namespace.c
3  *
4  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5  * - Modified by David Howells <dhowells@redhat.com>
6  *
7  * NFS namespace
8  */
9 
10 #include <linux/dcache.h>
11 #include <linux/gfp.h>
12 #include <linux/mount.h>
13 #include <linux/namei.h>
14 #include <linux/nfs_fs.h>
15 #include <linux/string.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/vfs.h>
18 #include <linux/sunrpc/gss_api.h>
19 #include "internal.h"
20 
21 #define NFSDBG_FACILITY		NFSDBG_VFS
22 
23 static void nfs_expire_automounts(struct work_struct *work);
24 
25 static LIST_HEAD(nfs_automount_list);
26 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
27 int nfs_mountpoint_expiry_timeout = 500 * HZ;
28 
29 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
30 					struct nfs_fh *fh,
31 					struct nfs_fattr *fattr,
32 					rpc_authflavor_t authflavor);
33 
34 /*
35  * nfs_path - reconstruct the path given an arbitrary dentry
36  * @base - used to return pointer to the end of devname part of path
37  * @dentry - pointer to dentry
38  * @buffer - result buffer
39  * @buflen - length of buffer
40  * @flags - options (see below)
41  *
42  * Helper function for constructing the server pathname
43  * by arbitrary hashed dentry.
44  *
45  * This is mainly for use in figuring out the path on the
46  * server side when automounting on top of an existing partition
47  * and in generating /proc/mounts and friends.
48  *
49  * Supported flags:
50  * NFS_PATH_CANONICAL: ensure there is exactly one slash after
51  *		       the original device (export) name
52  *		       (if unset, the original name is returned verbatim)
53  */
nfs_path(char ** p,struct dentry * dentry,char * buffer,ssize_t buflen,unsigned flags)54 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen,
55 	       unsigned flags)
56 {
57 	char *end;
58 	int namelen;
59 	unsigned seq;
60 	const char *base;
61 
62 rename_retry:
63 	end = buffer+buflen;
64 	*--end = '\0';
65 	buflen--;
66 
67 	seq = read_seqbegin(&rename_lock);
68 	rcu_read_lock();
69 	while (1) {
70 		spin_lock(&dentry->d_lock);
71 		if (IS_ROOT(dentry))
72 			break;
73 		namelen = dentry->d_name.len;
74 		buflen -= namelen + 1;
75 		if (buflen < 0)
76 			goto Elong_unlock;
77 		end -= namelen;
78 		memcpy(end, dentry->d_name.name, namelen);
79 		*--end = '/';
80 		spin_unlock(&dentry->d_lock);
81 		dentry = dentry->d_parent;
82 	}
83 	if (read_seqretry(&rename_lock, seq)) {
84 		spin_unlock(&dentry->d_lock);
85 		rcu_read_unlock();
86 		goto rename_retry;
87 	}
88 	if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
89 		if (--buflen < 0) {
90 			spin_unlock(&dentry->d_lock);
91 			rcu_read_unlock();
92 			goto Elong;
93 		}
94 		*--end = '/';
95 	}
96 	*p = end;
97 	base = dentry->d_fsdata;
98 	if (!base) {
99 		spin_unlock(&dentry->d_lock);
100 		rcu_read_unlock();
101 		WARN_ON(1);
102 		return end;
103 	}
104 	namelen = strlen(base);
105 	if (flags & NFS_PATH_CANONICAL) {
106 		/* Strip off excess slashes in base string */
107 		while (namelen > 0 && base[namelen - 1] == '/')
108 			namelen--;
109 	}
110 	buflen -= namelen;
111 	if (buflen < 0) {
112 		spin_unlock(&dentry->d_lock);
113 		rcu_read_unlock();
114 		goto Elong;
115 	}
116 	end -= namelen;
117 	memcpy(end, base, namelen);
118 	spin_unlock(&dentry->d_lock);
119 	rcu_read_unlock();
120 	return end;
121 Elong_unlock:
122 	spin_unlock(&dentry->d_lock);
123 	rcu_read_unlock();
124 	if (read_seqretry(&rename_lock, seq))
125 		goto rename_retry;
126 Elong:
127 	return ERR_PTR(-ENAMETOOLONG);
128 }
129 
130 #ifdef CONFIG_NFS_V4
nfs_find_best_sec(struct nfs4_secinfo_flavors * flavors)131 rpc_authflavor_t nfs_find_best_sec(struct nfs4_secinfo_flavors *flavors)
132 {
133 	struct gss_api_mech *mech;
134 	struct xdr_netobj oid;
135 	int i;
136 	rpc_authflavor_t pseudoflavor = RPC_AUTH_UNIX;
137 
138 	for (i = 0; i < flavors->num_flavors; i++) {
139 		struct nfs4_secinfo_flavor *flavor;
140 		flavor = &flavors->flavors[i];
141 
142 		if (flavor->flavor == RPC_AUTH_NULL || flavor->flavor == RPC_AUTH_UNIX) {
143 			pseudoflavor = flavor->flavor;
144 			break;
145 		} else if (flavor->flavor == RPC_AUTH_GSS) {
146 			oid.len  = flavor->gss.sec_oid4.len;
147 			oid.data = flavor->gss.sec_oid4.data;
148 			mech = gss_mech_get_by_OID(&oid);
149 			if (!mech)
150 				continue;
151 			pseudoflavor = gss_svc_to_pseudoflavor(mech, flavor->gss.service);
152 			gss_mech_put(mech);
153 			break;
154 		}
155 	}
156 
157 	return pseudoflavor;
158 }
159 
nfs_lookup_mountpoint(struct inode * dir,struct qstr * name,struct nfs_fh * fh,struct nfs_fattr * fattr)160 static struct rpc_clnt *nfs_lookup_mountpoint(struct inode *dir,
161 					      struct qstr *name,
162 					      struct nfs_fh *fh,
163 					      struct nfs_fattr *fattr)
164 {
165 	int err;
166 
167 	if (NFS_PROTO(dir)->version == 4)
168 		return nfs4_proc_lookup_mountpoint(dir, name, fh, fattr);
169 
170 	err = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, name, fh, fattr);
171 	if (err)
172 		return ERR_PTR(err);
173 	return rpc_clone_client(NFS_SERVER(dir)->client);
174 }
175 #else /* CONFIG_NFS_V4 */
nfs_lookup_mountpoint(struct inode * dir,struct qstr * name,struct nfs_fh * fh,struct nfs_fattr * fattr)176 static inline struct rpc_clnt *nfs_lookup_mountpoint(struct inode *dir,
177 						     struct qstr *name,
178 						     struct nfs_fh *fh,
179 						     struct nfs_fattr *fattr)
180 {
181 	int err = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, name, fh, fattr);
182 	if (err)
183 		return ERR_PTR(err);
184 	return rpc_clone_client(NFS_SERVER(dir)->client);
185 }
186 #endif /* CONFIG_NFS_V4 */
187 
188 /*
189  * nfs_d_automount - Handle crossing a mountpoint on the server
190  * @path - The mountpoint
191  *
192  * When we encounter a mountpoint on the server, we want to set up
193  * a mountpoint on the client too, to prevent inode numbers from
194  * colliding, and to allow "df" to work properly.
195  * On NFSv4, we also want to allow for the fact that different
196  * filesystems may be migrated to different servers in a failover
197  * situation, and that different filesystems may want to use
198  * different security flavours.
199  */
nfs_d_automount(struct path * path)200 struct vfsmount *nfs_d_automount(struct path *path)
201 {
202 	struct vfsmount *mnt;
203 	struct dentry *parent;
204 	struct nfs_fh *fh = NULL;
205 	struct nfs_fattr *fattr = NULL;
206 	struct rpc_clnt *client;
207 
208 	dprintk("--> nfs_d_automount()\n");
209 
210 	mnt = ERR_PTR(-ESTALE);
211 	if (IS_ROOT(path->dentry))
212 		goto out_nofree;
213 
214 	mnt = ERR_PTR(-ENOMEM);
215 	fh = nfs_alloc_fhandle();
216 	fattr = nfs_alloc_fattr();
217 	if (fh == NULL || fattr == NULL)
218 		goto out;
219 
220 	dprintk("%s: enter\n", __func__);
221 
222 	/* Look it up again to get its attributes */
223 	parent = dget_parent(path->dentry);
224 	client = nfs_lookup_mountpoint(parent->d_inode, &path->dentry->d_name, fh, fattr);
225 	dput(parent);
226 	if (IS_ERR(client)) {
227 		mnt = ERR_CAST(client);
228 		goto out;
229 	}
230 
231 	if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
232 		mnt = nfs_do_refmount(client, path->dentry);
233 	else
234 		mnt = nfs_do_submount(path->dentry, fh, fattr, client->cl_auth->au_flavor);
235 	rpc_shutdown_client(client);
236 
237 	if (IS_ERR(mnt))
238 		goto out;
239 
240 	dprintk("%s: done, success\n", __func__);
241 	mntget(mnt); /* prevent immediate expiration */
242 	mnt_set_expiry(mnt, &nfs_automount_list);
243 	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
244 
245 out:
246 	nfs_free_fattr(fattr);
247 	nfs_free_fhandle(fh);
248 out_nofree:
249 	if (IS_ERR(mnt))
250 		dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
251 	else
252 		dprintk("<-- %s() = %p\n", __func__, mnt);
253 	return mnt;
254 }
255 
256 static int
nfs_namespace_getattr(struct vfsmount * mnt,struct dentry * dentry,struct kstat * stat)257 nfs_namespace_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
258 {
259 	if (NFS_FH(dentry->d_inode)->size != 0)
260 		return nfs_getattr(mnt, dentry, stat);
261 	generic_fillattr(dentry->d_inode, stat);
262 	return 0;
263 }
264 
265 static int
nfs_namespace_setattr(struct dentry * dentry,struct iattr * attr)266 nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
267 {
268 	if (NFS_FH(dentry->d_inode)->size != 0)
269 		return nfs_setattr(dentry, attr);
270 	return -EACCES;
271 }
272 
273 const struct inode_operations nfs_mountpoint_inode_operations = {
274 	.getattr	= nfs_getattr,
275 	.setattr	= nfs_setattr,
276 };
277 
278 const struct inode_operations nfs_referral_inode_operations = {
279 	.getattr	= nfs_namespace_getattr,
280 	.setattr	= nfs_namespace_setattr,
281 };
282 
nfs_expire_automounts(struct work_struct * work)283 static void nfs_expire_automounts(struct work_struct *work)
284 {
285 	struct list_head *list = &nfs_automount_list;
286 
287 	mark_mounts_for_expiry(list);
288 	if (!list_empty(list))
289 		schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
290 }
291 
nfs_release_automount_timer(void)292 void nfs_release_automount_timer(void)
293 {
294 	if (list_empty(&nfs_automount_list))
295 		cancel_delayed_work(&nfs_automount_task);
296 }
297 
298 /*
299  * Clone a mountpoint of the appropriate type
300  */
nfs_do_clone_mount(struct nfs_server * server,const char * devname,struct nfs_clone_mount * mountdata)301 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
302 					   const char *devname,
303 					   struct nfs_clone_mount *mountdata)
304 {
305 #ifdef CONFIG_NFS_V4
306 	struct vfsmount *mnt = ERR_PTR(-EINVAL);
307 	switch (server->nfs_client->rpc_ops->version) {
308 		case 2:
309 		case 3:
310 			mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
311 			break;
312 		case 4:
313 			mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
314 	}
315 	return mnt;
316 #else
317 	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
318 #endif
319 }
320 
321 /**
322  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
323  * @dentry - parent directory
324  * @fh - filehandle for new root dentry
325  * @fattr - attributes for new root inode
326  * @authflavor - security flavor to use when performing the mount
327  *
328  */
nfs_do_submount(struct dentry * dentry,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t authflavor)329 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
330 					struct nfs_fh *fh,
331 					struct nfs_fattr *fattr,
332 					rpc_authflavor_t authflavor)
333 {
334 	struct nfs_clone_mount mountdata = {
335 		.sb = dentry->d_sb,
336 		.dentry = dentry,
337 		.fh = fh,
338 		.fattr = fattr,
339 		.authflavor = authflavor,
340 	};
341 	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
342 	char *page = (char *) __get_free_page(GFP_USER);
343 	char *devname;
344 
345 	dprintk("--> nfs_do_submount()\n");
346 
347 	dprintk("%s: submounting on %s/%s\n", __func__,
348 			dentry->d_parent->d_name.name,
349 			dentry->d_name.name);
350 	if (page == NULL)
351 		goto out;
352 	devname = nfs_devname(dentry, page, PAGE_SIZE);
353 	mnt = (struct vfsmount *)devname;
354 	if (IS_ERR(devname))
355 		goto free_page;
356 	mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
357 free_page:
358 	free_page((unsigned long)page);
359 out:
360 	dprintk("%s: done\n", __func__);
361 
362 	dprintk("<-- nfs_do_submount() = %p\n", mnt);
363 	return mnt;
364 }
365