Merge ../linux-2.6
[linux-2.6] / fs / nfs / namespace.c
1 /*
2  * linux/fs/nfs/namespace.c
3  *
4  * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
5  *
6  * NFS namespace
7  */
8
9 #include <linux/config.h>
10
11 #include <linux/dcache.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(void *list);
23
24 LIST_HEAD(nfs_automount_list);
25 static DECLARE_WORK(nfs_automount_task, nfs_expire_automounts, &nfs_automount_list);
26 int nfs_mountpoint_expiry_timeout = 500 * HZ;
27
28 /*
29  * nfs_path - reconstruct the path given an arbitrary dentry
30  * @base - arbitrary string to prepend to the path
31  * @dentry - pointer to dentry
32  * @buffer - result buffer
33  * @buflen - length of buffer
34  *
35  * Helper function for constructing the path from the
36  * root dentry to an arbitrary hashed dentry.
37  *
38  * This is mainly for use in figuring out the path on the
39  * server side when automounting on top of an existing partition.
40  */
41 char *nfs_path(const char *base, const struct dentry *dentry,
42                char *buffer, ssize_t buflen)
43 {
44         char *end = buffer+buflen;
45         int namelen;
46
47         *--end = '\0';
48         buflen--;
49         spin_lock(&dcache_lock);
50         while (!IS_ROOT(dentry)) {
51                 namelen = dentry->d_name.len;
52                 buflen -= namelen + 1;
53                 if (buflen < 0)
54                         goto Elong_unlock;
55                 end -= namelen;
56                 memcpy(end, dentry->d_name.name, namelen);
57                 *--end = '/';
58                 dentry = dentry->d_parent;
59         }
60         spin_unlock(&dcache_lock);
61         namelen = strlen(base);
62         /* Strip off excess slashes in base string */
63         while (namelen > 0 && base[namelen - 1] == '/')
64                 namelen--;
65         buflen -= namelen;
66         if (buflen < 0)
67                 goto Elong;
68         end -= namelen;
69         memcpy(end, base, namelen);
70         return end;
71 Elong_unlock:
72         spin_unlock(&dcache_lock);
73 Elong:
74         return ERR_PTR(-ENAMETOOLONG);
75 }
76
77 /*
78  * nfs_follow_mountpoint - handle crossing a mountpoint on the server
79  * @dentry - dentry of mountpoint
80  * @nd - nameidata info
81  *
82  * When we encounter a mountpoint on the server, we want to set up
83  * a mountpoint on the client too, to prevent inode numbers from
84  * colliding, and to allow "df" to work properly.
85  * On NFSv4, we also want to allow for the fact that different
86  * filesystems may be migrated to different servers in a failover
87  * situation, and that different filesystems may want to use
88  * different security flavours.
89  */
90 static void * nfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
91 {
92         struct vfsmount *mnt;
93         struct nfs_server *server = NFS_SERVER(dentry->d_inode);
94         struct dentry *parent;
95         struct nfs_fh fh;
96         struct nfs_fattr fattr;
97         int err;
98
99         BUG_ON(IS_ROOT(dentry));
100         dprintk("%s: enter\n", __FUNCTION__);
101         dput(nd->dentry);
102         nd->dentry = dget(dentry);
103         if (d_mountpoint(nd->dentry))
104                 goto out_follow;
105         /* Look it up again */
106         parent = dget_parent(nd->dentry);
107         err = server->rpc_ops->lookup(parent->d_inode, &nd->dentry->d_name, &fh, &fattr);
108         dput(parent);
109         if (err != 0)
110                 goto out_err;
111
112         if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL)
113                 mnt = nfs_do_refmount(nd->mnt, nd->dentry);
114         else
115                 mnt = nfs_do_submount(nd->mnt, nd->dentry, &fh, &fattr);
116         err = PTR_ERR(mnt);
117         if (IS_ERR(mnt))
118                 goto out_err;
119
120         mntget(mnt);
121         err = do_add_mount(mnt, nd, nd->mnt->mnt_flags|MNT_SHRINKABLE, &nfs_automount_list);
122         if (err < 0) {
123                 mntput(mnt);
124                 if (err == -EBUSY)
125                         goto out_follow;
126                 goto out_err;
127         }
128         mntput(nd->mnt);
129         dput(nd->dentry);
130         nd->mnt = mnt;
131         nd->dentry = dget(mnt->mnt_root);
132         schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
133 out:
134         dprintk("%s: done, returned %d\n", __FUNCTION__, err);
135         return ERR_PTR(err);
136 out_err:
137         path_release(nd);
138         goto out;
139 out_follow:
140         while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
141                 ;
142         err = 0;
143         goto out;
144 }
145
146 struct inode_operations nfs_mountpoint_inode_operations = {
147         .follow_link    = nfs_follow_mountpoint,
148         .getattr        = nfs_getattr,
149 };
150
151 struct inode_operations nfs_referral_inode_operations = {
152         .follow_link    = nfs_follow_mountpoint,
153 };
154
155 static void nfs_expire_automounts(void *data)
156 {
157         struct list_head *list = (struct list_head *)data;
158
159         mark_mounts_for_expiry(list);
160         if (!list_empty(list))
161                 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
162 }
163
164 void nfs_release_automount_timer(void)
165 {
166         if (list_empty(&nfs_automount_list)) {
167                 cancel_delayed_work(&nfs_automount_task);
168                 flush_scheduled_work();
169         }
170 }
171
172 /*
173  * Clone a mountpoint of the appropriate type
174  */
175 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server, char *devname,
176                                            struct nfs_clone_mount *mountdata)
177 {
178 #ifdef CONFIG_NFS_V4
179         struct vfsmount *mnt = NULL;
180         switch (server->rpc_ops->version) {
181                 case 2:
182                 case 3:
183                         mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
184                         break;
185                 case 4:
186                         mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, mountdata);
187         }
188         return mnt;
189 #else
190         return vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
191 #endif
192 }
193
194 /**
195  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
196  * @mnt_parent - mountpoint of parent directory
197  * @dentry - parent directory
198  * @fh - filehandle for new root dentry
199  * @fattr - attributes for new root inode
200  *
201  */
202 struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
203                 const struct dentry *dentry, struct nfs_fh *fh,
204                 struct nfs_fattr *fattr)
205 {
206         struct nfs_clone_mount mountdata = {
207                 .sb = mnt_parent->mnt_sb,
208                 .dentry = dentry,
209                 .fh = fh,
210                 .fattr = fattr,
211         };
212         struct vfsmount *mnt = ERR_PTR(-ENOMEM);
213         char *page = (char *) __get_free_page(GFP_USER);
214         char *devname;
215
216         dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
217                         dentry->d_parent->d_name.name,
218                         dentry->d_name.name);
219         if (page == NULL)
220                 goto out;
221         devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
222         mnt = (struct vfsmount *)devname;
223         if (IS_ERR(devname))
224                 goto free_page;
225         mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
226 free_page:
227         free_page((unsigned long)page);
228 out:
229         dprintk("%s: done\n", __FUNCTION__);
230         return mnt;
231 }