xref: /openbmc/linux/fs/nfs/namespace.c (revision 565d76cb)
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 "internal.h"
19 
20 #define NFSDBG_FACILITY		NFSDBG_VFS
21 
22 static void nfs_expire_automounts(struct work_struct *work);
23 
24 static LIST_HEAD(nfs_automount_list);
25 static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
26 int nfs_mountpoint_expiry_timeout = 500 * HZ;
27 
28 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
29 					struct nfs_fh *fh,
30 					struct nfs_fattr *fattr);
31 
32 /*
33  * nfs_path - reconstruct the path given an arbitrary dentry
34  * @base - used to return pointer to the end of devname part of path
35  * @dentry - pointer to dentry
36  * @buffer - result buffer
37  * @buflen - length of buffer
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 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
47 {
48 	char *end;
49 	int namelen;
50 	unsigned seq;
51 	const char *base;
52 
53 rename_retry:
54 	end = buffer+buflen;
55 	*--end = '\0';
56 	buflen--;
57 
58 	seq = read_seqbegin(&rename_lock);
59 	rcu_read_lock();
60 	while (1) {
61 		spin_lock(&dentry->d_lock);
62 		if (IS_ROOT(dentry))
63 			break;
64 		namelen = dentry->d_name.len;
65 		buflen -= namelen + 1;
66 		if (buflen < 0)
67 			goto Elong_unlock;
68 		end -= namelen;
69 		memcpy(end, dentry->d_name.name, namelen);
70 		*--end = '/';
71 		spin_unlock(&dentry->d_lock);
72 		dentry = dentry->d_parent;
73 	}
74 	if (read_seqretry(&rename_lock, seq)) {
75 		spin_unlock(&dentry->d_lock);
76 		rcu_read_unlock();
77 		goto rename_retry;
78 	}
79 	if (*end != '/') {
80 		if (--buflen < 0) {
81 			spin_unlock(&dentry->d_lock);
82 			rcu_read_unlock();
83 			goto Elong;
84 		}
85 		*--end = '/';
86 	}
87 	*p = end;
88 	base = dentry->d_fsdata;
89 	if (!base) {
90 		spin_unlock(&dentry->d_lock);
91 		rcu_read_unlock();
92 		WARN_ON(1);
93 		return end;
94 	}
95 	namelen = strlen(base);
96 	/* Strip off excess slashes in base string */
97 	while (namelen > 0 && base[namelen - 1] == '/')
98 		namelen--;
99 	buflen -= namelen;
100 	if (buflen < 0) {
101 		spin_unlock(&dentry->d_lock);
102 		rcu_read_unlock();
103 		goto Elong;
104 	}
105 	end -= namelen;
106 	memcpy(end, base, namelen);
107 	spin_unlock(&dentry->d_lock);
108 	rcu_read_unlock();
109 	return end;
110 Elong_unlock:
111 	spin_unlock(&dentry->d_lock);
112 	rcu_read_unlock();
113 	if (read_seqretry(&rename_lock, seq))
114 		goto rename_retry;
115 Elong:
116 	return ERR_PTR(-ENAMETOOLONG);
117 }
118 
119 /*
120  * nfs_d_automount - Handle crossing a mountpoint on the server
121  * @path - The mountpoint
122  *
123  * When we encounter a mountpoint on the server, we want to set up
124  * a mountpoint on the client too, to prevent inode numbers from
125  * colliding, and to allow "df" to work properly.
126  * On NFSv4, we also want to allow for the fact that different
127  * filesystems may be migrated to different servers in a failover
128  * situation, and that different filesystems may want to use
129  * different security flavours.
130  */
131 struct vfsmount *nfs_d_automount(struct path *path)
132 {
133 	struct vfsmount *mnt;
134 	struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
135 	struct dentry *parent;
136 	struct nfs_fh *fh = NULL;
137 	struct nfs_fattr *fattr = NULL;
138 	int err;
139 
140 	dprintk("--> nfs_d_automount()\n");
141 
142 	mnt = ERR_PTR(-ESTALE);
143 	if (IS_ROOT(path->dentry))
144 		goto out_nofree;
145 
146 	mnt = ERR_PTR(-ENOMEM);
147 	fh = nfs_alloc_fhandle();
148 	fattr = nfs_alloc_fattr();
149 	if (fh == NULL || fattr == NULL)
150 		goto out;
151 
152 	dprintk("%s: enter\n", __func__);
153 
154 	/* Look it up again to get its attributes */
155 	parent = dget_parent(path->dentry);
156 	err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
157 						  &path->dentry->d_name,
158 						  fh, fattr);
159 	dput(parent);
160 	if (err != 0) {
161 		mnt = ERR_PTR(err);
162 		goto out;
163 	}
164 
165 	if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
166 		mnt = nfs_do_refmount(path->dentry);
167 	else
168 		mnt = nfs_do_submount(path->dentry, fh, fattr);
169 	if (IS_ERR(mnt))
170 		goto out;
171 
172 	dprintk("%s: done, success\n", __func__);
173 	mntget(mnt); /* prevent immediate expiration */
174 	mnt_set_expiry(mnt, &nfs_automount_list);
175 	schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
176 
177 out:
178 	nfs_free_fattr(fattr);
179 	nfs_free_fhandle(fh);
180 out_nofree:
181 	dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
182 	return mnt;
183 }
184 
185 const struct inode_operations nfs_mountpoint_inode_operations = {
186 	.getattr	= nfs_getattr,
187 };
188 
189 const struct inode_operations nfs_referral_inode_operations = {
190 };
191 
192 static void nfs_expire_automounts(struct work_struct *work)
193 {
194 	struct list_head *list = &nfs_automount_list;
195 
196 	mark_mounts_for_expiry(list);
197 	if (!list_empty(list))
198 		schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
199 }
200 
201 void nfs_release_automount_timer(void)
202 {
203 	if (list_empty(&nfs_automount_list))
204 		cancel_delayed_work(&nfs_automount_task);
205 }
206 
207 /*
208  * Clone a mountpoint of the appropriate type
209  */
210 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
211 					   const char *devname,
212 					   struct nfs_clone_mount *mountdata)
213 {
214 #ifdef CONFIG_NFS_V4
215 	struct vfsmount *mnt = ERR_PTR(-EINVAL);
216 	switch (server->nfs_client->rpc_ops->version) {
217 		case 2:
218 		case 3:
219 			mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
220 			break;
221 		case 4:
222 			mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
223 	}
224 	return mnt;
225 #else
226 	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
227 #endif
228 }
229 
230 /**
231  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
232  * @dentry - parent directory
233  * @fh - filehandle for new root dentry
234  * @fattr - attributes for new root inode
235  *
236  */
237 static struct vfsmount *nfs_do_submount(struct dentry *dentry,
238 					struct nfs_fh *fh,
239 					struct nfs_fattr *fattr)
240 {
241 	struct nfs_clone_mount mountdata = {
242 		.sb = dentry->d_sb,
243 		.dentry = dentry,
244 		.fh = fh,
245 		.fattr = fattr,
246 	};
247 	struct vfsmount *mnt = ERR_PTR(-ENOMEM);
248 	char *page = (char *) __get_free_page(GFP_USER);
249 	char *devname;
250 
251 	dprintk("--> nfs_do_submount()\n");
252 
253 	dprintk("%s: submounting on %s/%s\n", __func__,
254 			dentry->d_parent->d_name.name,
255 			dentry->d_name.name);
256 	if (page == NULL)
257 		goto out;
258 	devname = nfs_devname(dentry, page, PAGE_SIZE);
259 	mnt = (struct vfsmount *)devname;
260 	if (IS_ERR(devname))
261 		goto free_page;
262 	mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
263 free_page:
264 	free_page((unsigned long)page);
265 out:
266 	dprintk("%s: done\n", __func__);
267 
268 	dprintk("<-- nfs_do_submount() = %p\n", mnt);
269 	return mnt;
270 }
271