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