]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nfs/namespace.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / namespace.c
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 - pointer to dentry
34 * @buffer - result buffer
35 * @buflen - length of buffer
36 *
37 * Helper function for constructing the server pathname
38 * by arbitrary hashed dentry.
39 *
40 * This is mainly for use in figuring out the path on the
41 * server side when automounting on top of an existing partition
42 * and in generating /proc/mounts and friends.
43 */
44 char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
45 {
46 char *end;
47 int namelen;
48 unsigned seq;
49 const char *base;
50
51 rename_retry:
52 end = buffer+buflen;
53 *--end = '\0';
54 buflen--;
55
56 seq = read_seqbegin(&rename_lock);
57 rcu_read_lock();
58 while (1) {
59 spin_lock(&dentry->d_lock);
60 if (IS_ROOT(dentry))
61 break;
62 namelen = dentry->d_name.len;
63 buflen -= namelen + 1;
64 if (buflen < 0)
65 goto Elong_unlock;
66 end -= namelen;
67 memcpy(end, dentry->d_name.name, namelen);
68 *--end = '/';
69 spin_unlock(&dentry->d_lock);
70 dentry = dentry->d_parent;
71 }
72 if (read_seqretry(&rename_lock, seq)) {
73 spin_unlock(&dentry->d_lock);
74 rcu_read_unlock();
75 goto rename_retry;
76 }
77 if (*end != '/') {
78 if (--buflen < 0) {
79 spin_unlock(&dentry->d_lock);
80 rcu_read_unlock();
81 goto Elong;
82 }
83 *--end = '/';
84 }
85 *p = end;
86 base = dentry->d_fsdata;
87 if (!base) {
88 spin_unlock(&dentry->d_lock);
89 rcu_read_unlock();
90 WARN_ON(1);
91 return end;
92 }
93 namelen = strlen(base);
94 /* Strip off excess slashes in base string */
95 while (namelen > 0 && base[namelen - 1] == '/')
96 namelen--;
97 buflen -= namelen;
98 if (buflen < 0) {
99 spin_unlock(&dentry->d_lock);
100 rcu_read_unlock();
101 goto Elong;
102 }
103 end -= namelen;
104 memcpy(end, base, namelen);
105 spin_unlock(&dentry->d_lock);
106 rcu_read_unlock();
107 return end;
108 Elong_unlock:
109 spin_unlock(&dentry->d_lock);
110 rcu_read_unlock();
111 if (read_seqretry(&rename_lock, seq))
112 goto rename_retry;
113 Elong:
114 return ERR_PTR(-ENAMETOOLONG);
115 }
116 EXPORT_SYMBOL_GPL(nfs_path);
117
118 /*
119 * nfs_d_automount - Handle crossing a mountpoint on the server
120 * @path - The mountpoint
121 *
122 * When we encounter a mountpoint on the server, we want to set up
123 * a mountpoint on the client too, to prevent inode numbers from
124 * colliding, and to allow "df" to work properly.
125 * On NFSv4, we also want to allow for the fact that different
126 * filesystems may be migrated to different servers in a failover
127 * situation, and that different filesystems may want to use
128 * different security flavours.
129 */
130 struct vfsmount *nfs_d_automount(struct path *path)
131 {
132 struct vfsmount *mnt;
133 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
134 struct nfs_fh *fh = NULL;
135 struct nfs_fattr *fattr = NULL;
136
137 dprintk("--> nfs_d_automount()\n");
138
139 mnt = ERR_PTR(-ESTALE);
140 if (IS_ROOT(path->dentry))
141 goto out_nofree;
142
143 mnt = ERR_PTR(-ENOMEM);
144 fh = nfs_alloc_fhandle();
145 fattr = nfs_alloc_fattr();
146 if (fh == NULL || fattr == NULL)
147 goto out;
148
149 dprintk("%s: enter\n", __func__);
150
151 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
152 if (IS_ERR(mnt))
153 goto out;
154
155 dprintk("%s: done, success\n", __func__);
156 mntget(mnt); /* prevent immediate expiration */
157 mnt_set_expiry(mnt, &nfs_automount_list);
158 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
159
160 out:
161 nfs_free_fattr(fattr);
162 nfs_free_fhandle(fh);
163 out_nofree:
164 if (IS_ERR(mnt))
165 dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
166 else
167 dprintk("<-- %s() = %p\n", __func__, mnt);
168 return mnt;
169 }
170
171 const struct inode_operations nfs_mountpoint_inode_operations = {
172 .getattr = nfs_getattr,
173 };
174
175 const struct inode_operations nfs_referral_inode_operations = {
176 };
177
178 static void nfs_expire_automounts(struct work_struct *work)
179 {
180 struct list_head *list = &nfs_automount_list;
181
182 mark_mounts_for_expiry(list);
183 if (!list_empty(list))
184 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
185 }
186
187 void nfs_release_automount_timer(void)
188 {
189 if (list_empty(&nfs_automount_list))
190 cancel_delayed_work(&nfs_automount_task);
191 }
192
193 /*
194 * Clone a mountpoint of the appropriate type
195 */
196 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
197 const char *devname,
198 struct nfs_clone_mount *mountdata)
199 {
200 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
201 }
202
203 /**
204 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
205 * @dentry - parent directory
206 * @fh - filehandle for new root dentry
207 * @fattr - attributes for new root inode
208 * @authflavor - security flavor to use when performing the mount
209 *
210 */
211 struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
212 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
213 {
214 struct nfs_clone_mount mountdata = {
215 .sb = dentry->d_sb,
216 .dentry = dentry,
217 .fh = fh,
218 .fattr = fattr,
219 .authflavor = authflavor,
220 };
221 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
222 char *page = (char *) __get_free_page(GFP_USER);
223 char *devname;
224
225 dprintk("--> nfs_do_submount()\n");
226
227 dprintk("%s: submounting on %s/%s\n", __func__,
228 dentry->d_parent->d_name.name,
229 dentry->d_name.name);
230 if (page == NULL)
231 goto out;
232 devname = nfs_devname(dentry, page, PAGE_SIZE);
233 mnt = (struct vfsmount *)devname;
234 if (IS_ERR(devname))
235 goto free_page;
236 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
237 free_page:
238 free_page((unsigned long)page);
239 out:
240 dprintk("%s: done\n", __func__);
241
242 dprintk("<-- nfs_do_submount() = %p\n", mnt);
243 return mnt;
244 }
245 EXPORT_SYMBOL_GPL(nfs_do_submount);
246
247 struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry,
248 struct nfs_fh *fh, struct nfs_fattr *fattr)
249 {
250 int err;
251 struct dentry *parent = dget_parent(dentry);
252
253 /* Look it up again to get its attributes */
254 err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
255 dput(parent);
256 if (err != 0)
257 return ERR_PTR(err);
258
259 return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor);
260 }
261 EXPORT_SYMBOL_GPL(nfs_submount);