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