Merge branch 'stable/xen.pm.bug-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.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
f8ad9c4b 28static struct vfsmount *nfs_do_submount(struct dentry *dentry,
66f37509
AB
29 struct nfs_fh *fh,
30 struct nfs_fattr *fattr);
31
f7b422b1
DH
32/*
33 * nfs_path - reconstruct the path given an arbitrary dentry
b514f872 34 * @base - used to return pointer to the end of devname part of path
f7b422b1
DH
35 * @dentry - pointer to dentry
36 * @buffer - result buffer
37 * @buflen - length of buffer
38 *
b514f872
AV
39 * Helper function for constructing the server pathname
40 * by arbitrary hashed dentry.
f7b422b1
DH
41 *
42 * This is mainly for use in figuring out the path on the
b514f872
AV
43 * server side when automounting on top of an existing partition
44 * and in generating /proc/mounts and friends.
f7b422b1 45 */
b514f872 46char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
f7b422b1 47{
949854d0 48 char *end;
f7b422b1 49 int namelen;
949854d0 50 unsigned seq;
b514f872 51 const char *base;
f7b422b1 52
949854d0
NP
53rename_retry:
54 end = buffer+buflen;
f7b422b1
DH
55 *--end = '\0';
56 buflen--;
949854d0
NP
57
58 seq = read_seqbegin(&rename_lock);
59 rcu_read_lock();
b514f872
AV
60 while (1) {
61 spin_lock(&dentry->d_lock);
62 if (IS_ROOT(dentry))
63 break;
f7b422b1
DH
64 namelen = dentry->d_name.len;
65 buflen -= namelen + 1;
66 if (buflen < 0)
ce510193 67 goto Elong_unlock;
f7b422b1
DH
68 end -= namelen;
69 memcpy(end, dentry->d_name.name, namelen);
70 *--end = '/';
b514f872 71 spin_unlock(&dentry->d_lock);
f7b422b1
DH
72 dentry = dentry->d_parent;
73 }
b514f872
AV
74 if (read_seqretry(&rename_lock, seq)) {
75 spin_unlock(&dentry->d_lock);
76 rcu_read_unlock();
949854d0 77 goto rename_retry;
b514f872 78 }
0b75b35c 79 if (*end != '/') {
b514f872
AV
80 if (--buflen < 0) {
81 spin_unlock(&dentry->d_lock);
82 rcu_read_unlock();
0b75b35c 83 goto Elong;
b514f872 84 }
0b75b35c
TM
85 *--end = '/';
86 }
b514f872
AV
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 }
f7b422b1
DH
95 namelen = strlen(base);
96 /* Strip off excess slashes in base string */
97 while (namelen > 0 && base[namelen - 1] == '/')
98 namelen--;
99 buflen -= namelen;
b514f872
AV
100 if (buflen < 0) {
101 spin_lock(&dentry->d_lock);
102 rcu_read_unlock();
f7b422b1 103 goto Elong;
b514f872 104 }
f7b422b1
DH
105 end -= namelen;
106 memcpy(end, base, namelen);
b514f872
AV
107 spin_unlock(&dentry->d_lock);
108 rcu_read_unlock();
f7b422b1 109 return end;
ce510193 110Elong_unlock:
b514f872 111 spin_lock(&dentry->d_lock);
949854d0
NP
112 rcu_read_unlock();
113 if (read_seqretry(&rename_lock, seq))
114 goto rename_retry;
f7b422b1
DH
115Elong:
116 return ERR_PTR(-ENAMETOOLONG);
117}
118
55a97593 119/*
36d43a43
DH
120 * nfs_d_automount - Handle crossing a mountpoint on the server
121 * @path - The mountpoint
55a97593
TM
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 */
36d43a43 131struct vfsmount *nfs_d_automount(struct path *path)
55a97593
TM
132{
133 struct vfsmount *mnt;
36d43a43 134 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
55a97593 135 struct dentry *parent;
a4d7f168
TM
136 struct nfs_fh *fh = NULL;
137 struct nfs_fattr *fattr = NULL;
55a97593
TM
138 int err;
139
36d43a43 140 dprintk("--> nfs_d_automount()\n");
54ceac45 141
36d43a43
DH
142 mnt = ERR_PTR(-ESTALE);
143 if (IS_ROOT(path->dentry))
144 goto out_nofree;
44d5759d 145
36d43a43 146 mnt = ERR_PTR(-ENOMEM);
a4d7f168
TM
147 fh = nfs_alloc_fhandle();
148 fattr = nfs_alloc_fattr();
149 if (fh == NULL || fattr == NULL)
36d43a43 150 goto out;
a4d7f168 151
3110ff80 152 dprintk("%s: enter\n", __func__);
54ceac45 153
36d43a43
DH
154 /* Look it up again to get its attributes */
155 parent = dget_parent(path->dentry);
8fa5c000 156 err = server->nfs_client->rpc_ops->lookup(parent->d_inode,
36d43a43 157 &path->dentry->d_name,
a4d7f168 158 fh, fattr);
55a97593 159 dput(parent);
36d43a43
DH
160 if (err != 0) {
161 mnt = ERR_PTR(err);
162 goto out;
163 }
55a97593 164
a4d7f168 165 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
f8ad9c4b 166 mnt = nfs_do_refmount(path->dentry);
6b97fd3d 167 else
f8ad9c4b 168 mnt = nfs_do_submount(path->dentry, fh, fattr);
55a97593 169 if (IS_ERR(mnt))
36d43a43 170 goto out;
55a97593 171
ea5b778a
DH
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);
36d43a43 176
55a97593 177out:
a4d7f168
TM
178 nfs_free_fattr(fattr);
179 nfs_free_fhandle(fh);
36d43a43
DH
180out_nofree:
181 dprintk("<-- nfs_follow_mountpoint() = %p\n", mnt);
182 return mnt;
55a97593
TM
183}
184
92e1d5be 185const struct inode_operations nfs_mountpoint_inode_operations = {
55a97593
TM
186 .getattr = nfs_getattr,
187};
51d8fa6a 188
92e1d5be 189const struct inode_operations nfs_referral_inode_operations = {
6b97fd3d
MN
190};
191
65f27f38 192static void nfs_expire_automounts(struct work_struct *work)
51d8fa6a 193{
65f27f38 194 struct list_head *list = &nfs_automount_list;
51d8fa6a
TM
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
201void nfs_release_automount_timer(void)
202{
3d39c691 203 if (list_empty(&nfs_automount_list))
560aef74 204 cancel_delayed_work(&nfs_automount_task);
51d8fa6a 205}
f7b422b1
DH
206
207/*
208 * Clone a mountpoint of the appropriate type
209 */
509de811
DH
210static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
211 const char *devname,
f7b422b1
DH
212 struct nfs_clone_mount *mountdata)
213{
214#ifdef CONFIG_NFS_V4
fd08d7e9 215 struct vfsmount *mnt = ERR_PTR(-EINVAL);
40c55319 216 switch (server->nfs_client->rpc_ops->version) {
f7b422b1
DH
217 case 2:
218 case 3:
54ceac45 219 mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
f7b422b1
DH
220 break;
221 case 4:
54ceac45 222 mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
f7b422b1
DH
223 }
224 return mnt;
225#else
54ceac45 226 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
f7b422b1
DH
227#endif
228}
229
230/**
231 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
f7b422b1
DH
232 * @dentry - parent directory
233 * @fh - filehandle for new root dentry
234 * @fattr - attributes for new root inode
235 *
236 */
f8ad9c4b 237static struct vfsmount *nfs_do_submount(struct dentry *dentry,
66f37509
AB
238 struct nfs_fh *fh,
239 struct nfs_fattr *fattr)
f7b422b1
DH
240{
241 struct nfs_clone_mount mountdata = {
f8ad9c4b 242 .sb = dentry->d_sb,
f7b422b1
DH
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
54ceac45
DH
251 dprintk("--> nfs_do_submount()\n");
252
3110ff80 253 dprintk("%s: submounting on %s/%s\n", __func__,
f7b422b1
DH
254 dentry->d_parent->d_name.name,
255 dentry->d_name.name);
256 if (page == NULL)
257 goto out;
b514f872 258 devname = nfs_devname(dentry, page, PAGE_SIZE);
f7b422b1
DH
259 mnt = (struct vfsmount *)devname;
260 if (IS_ERR(devname))
261 goto free_page;
f8ad9c4b 262 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
f7b422b1
DH
263free_page:
264 free_page((unsigned long)page);
265out:
3110ff80 266 dprintk("%s: done\n", __func__);
54ceac45
DH
267
268 dprintk("<-- nfs_do_submount() = %p\n", mnt);
f7b422b1
DH
269 return mnt;
270}
This page took 0.427002 seconds and 5 git commands to generate.