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/module.h>
11 #include <linux/dcache.h>
12 #include <linux/gfp.h>
13 #include <linux/mount.h>
14 #include <linux/namei.h>
15 #include <linux/nfs_fs.h>
16 #include <linux/string.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/vfs.h>
19 #include <linux/sunrpc/gss_api.h>
20 #include "internal.h"
21 
22 #define NFSDBG_FACILITY		NFSDBG_VFS
23 
24 static void nfs_expire_automounts(struct work_struct *work);
25 
26 static LIST_HEAD(nfs_automount_list);
27 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
28 int nfs_mountpoint_expiry_timeout = 500 * HZ;
29 
30 /*
31  * nfs_path - reconstruct the path given an arbitrary dentry
32  * @base - used to return pointer to the end of devname part of path
33  * @dentry_in - pointer to dentry
34  * @buffer - result buffer
35  * @buflen_in - length of buffer
36  * @flags - options (see below)
37  *
38  * Helper function for constructing the server pathname
39  * by arbitrary hashed dentry.
40  *
41  * This is mainly for use in figuring out the path on the
42  * server side when automounting on top of an existing partition
43  * and in generating /proc/mounts and friends.
44  *
45  * Supported flags:
46  * NFS_PATH_CANONICAL: ensure there is exactly one slash after
47  *		       the original device (export) name
48  *		       (if unset, the original name is returned verbatim)
49  */
nfs_path(char ** p,struct dentry * dentry_in,char * buffer,ssize_t buflen_in,unsigned flags)50 char *nfs_path(char **p, struct dentry *dentry_in, char *buffer,
51 	       ssize_t buflen_in, unsigned flags)
52 {
53 	char *end;
54 	int namelen;
55 	unsigned seq;
56 	const char *base;
57 	struct dentry *dentry;
58 	ssize_t buflen;
59 
60 rename_retry:
61 	buflen = buflen_in;
62 	dentry = dentry_in;
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 (*end == '/') {
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 EXPORT_SYMBOL_GPL(nfs_path);
130 
131 /*
132  * nfs_d_automount - Handle crossing a mountpoint on the server
133  * @path - The mountpoint
134  *
135  * When we encounter a mountpoint on the server, we want to set up
136  * a mountpoint on the client too, to prevent inode numbers from
137  * colliding, and to allow "df" to work properly.
138  * On NFSv4, we also want to allow for the fact that different
139  * filesystems may be migrated to different servers in a failover
140  * situation, and that different filesystems may want to use
141  * different security flavours.
142  */
nfs_d_automount(struct path * path)143 struct vfsmount *nfs_d_automount(struct path *path)
144 {
145 	struct vfsmount *mnt;
146 	struct nfs_server *server = NFS_SERVER(d_inode(path->dentry));
147 	struct nfs_fh *fh = NULL;
148 	struct nfs_fattr *fattr = NULL;
149 
150 	if (IS_ROOT(path->dentry))
151 		return ERR_PTR(-ESTALE);
152 
153 	mnt = ERR_PTR(-ENOMEM);
154 	fh = nfs_alloc_fhandle();
155 	fattr = nfs_alloc_fattr();
156 	if (fh == NULL || fattr == NULL)
157 		goto out;
158 
159 	mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
160 	if (IS_ERR(mnt))
161 		goto out;
162 
163 	mntget(mnt); /* prevent immediate expiration */
164 	mnt_set_expiry(mnt, &nfs_automount_list);
165 	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
166 
167 out:
168 	nfs_free_fattr(fattr);
169 	nfs_free_fhandle(fh);
170 	return mnt;
171 }
172 
173 static int
nfs_namespace_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)174 nfs_namespace_getattr(const struct path *path, struct kstat *stat,
175 			u32 request_mask, unsigned int query_flags)
176 {
177 	if (NFS_FH(d_inode(path->dentry))->size != 0)
178 		return nfs_getattr(path, stat, request_mask, query_flags);
179 	generic_fillattr(d_inode(path->dentry), stat);
180 	return 0;
181 }
182 
183 static int
nfs_namespace_setattr(struct dentry * dentry,struct iattr * attr)184 nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
185 {
186 	if (NFS_FH(d_inode(dentry))->size != 0)
187 		return nfs_setattr(dentry, attr);
188 	return -EACCES;
189 }
190 
191 const struct inode_operations nfs_mountpoint_inode_operations = {
192 	.getattr	= nfs_getattr,
193 	.setattr	= nfs_setattr,
194 };
195 
196 const struct inode_operations nfs_referral_inode_operations = {
197 	.getattr	= nfs_namespace_getattr,
198 	.setattr	= nfs_namespace_setattr,
199 };
200 
nfs_expire_automounts(struct work_struct * work)201 static void nfs_expire_automounts(struct work_struct *work)
202 {
203 	struct list_head *list = &nfs_automount_list;
204 
205 	mark_mounts_for_expiry(list);
206 	if (!list_empty(list))
207 		schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
208 }
209 
nfs_release_automount_timer(void)210 void nfs_release_automount_timer(void)
211 {
212 	if (list_empty(&nfs_automount_list))
213 		cancel_delayed_work(&nfs_automount_task);
214 }
215 
216 /*
217  * Clone a mountpoint of the appropriate type
218  */
nfs_do_clone_mount(struct nfs_server * server,const char * devname,struct nfs_clone_mount * mountdata)219 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
220 					   const char *devname,
221 					   struct nfs_clone_mount *mountdata)
222 {
223 	return vfs_submount(mountdata->dentry, &nfs_xdev_fs_type, devname, mountdata);
224 }
225 
226 /**
227  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
228  * @dentry - parent directory
229  * @fh - filehandle for new root dentry
230  * @fattr - attributes for new root inode
231  * @authflavor - security flavor to use when performing the mount
232  *
233  */
nfs_do_submount(struct dentry * dentry,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t authflavor)234 struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
235 				 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
236 {
237 	struct nfs_clone_mount mountdata = {
238 		.sb = dentry->d_sb,
239 		.dentry = dentry,
240 		.fh = fh,
241 		.fattr = fattr,
242 		.authflavor = authflavor,
243 	};
244 	struct vfsmount *mnt;
245 	char *page = (char *) __get_free_page(GFP_USER);
246 	char *devname;
247 
248 	if (page == NULL)
249 		return ERR_PTR(-ENOMEM);
250 
251 	devname = nfs_devname(dentry, page, PAGE_SIZE);
252 	if (IS_ERR(devname))
253 		mnt = ERR_CAST(devname);
254 	else
255 		mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
256 
257 	free_page((unsigned long)page);
258 	return mnt;
259 }
260 EXPORT_SYMBOL_GPL(nfs_do_submount);
261 
nfs_submount(struct nfs_server * server,struct dentry * dentry,struct nfs_fh * fh,struct nfs_fattr * fattr)262 struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
263 			      struct nfs_fh *fh, struct nfs_fattr *fattr)
264 {
265 	int err;
266 	struct dentry *parent = dget_parent(dentry);
267 
268 	/* Look it up again to get its attributes */
269 	err = server->nfs_client->rpc_ops->lookup(d_inode(parent), &dentry->d_name, fh, fattr, NULL);
270 	dput(parent);
271 	if (err != 0)
272 		return ERR_PTR(err);
273 
274 	return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
275 }
276 EXPORT_SYMBOL_GPL(nfs_submount);
277