NFS: Move rpc_ops from nfs_server to nfs_client
[safe/jmp/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->nfs_client->rpc_ops->lookup(parent->d_inode,
108                                                   &nd->dentry->d_name,
109                                                   &fh, &fattr);
110         dput(parent);
111         if (err != 0)
112                 goto out_err;
113
114         if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL)
115                 mnt = nfs_do_refmount(nd->mnt, nd->dentry);
116         else
117                 mnt = nfs_do_submount(nd->mnt, nd->dentry, &fh, &fattr);
118         err = PTR_ERR(mnt);
119         if (IS_ERR(mnt))
120                 goto out_err;
121
122         mntget(mnt);
123         err = do_add_mount(mnt, nd, nd->mnt->mnt_flags|MNT_SHRINKABLE, &nfs_automount_list);
124         if (err < 0) {
125                 mntput(mnt);
126                 if (err == -EBUSY)
127                         goto out_follow;
128                 goto out_err;
129         }
130         mntput(nd->mnt);
131         dput(nd->dentry);
132         nd->mnt = mnt;
133         nd->dentry = dget(mnt->mnt_root);
134         schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
135 out:
136         dprintk("%s: done, returned %d\n", __FUNCTION__, err);
137         return ERR_PTR(err);
138 out_err:
139         path_release(nd);
140         goto out;
141 out_follow:
142         while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
143                 ;
144         err = 0;
145         goto out;
146 }
147
148 struct inode_operations nfs_mountpoint_inode_operations = {
149         .follow_link    = nfs_follow_mountpoint,
150         .getattr        = nfs_getattr,
151 };
152
153 struct inode_operations nfs_referral_inode_operations = {
154         .follow_link    = nfs_follow_mountpoint,
155 };
156
157 static void nfs_expire_automounts(void *data)
158 {
159         struct list_head *list = (struct list_head *)data;
160
161         mark_mounts_for_expiry(list);
162         if (!list_empty(list))
163                 schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout);
164 }
165
166 void nfs_release_automount_timer(void)
167 {
168         if (list_empty(&nfs_automount_list)) {
169                 cancel_delayed_work(&nfs_automount_task);
170                 flush_scheduled_work();
171         }
172 }
173
174 /*
175  * Clone a mountpoint of the appropriate type
176  */
177 static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
178                                            const char *devname,
179                                            struct nfs_clone_mount *mountdata)
180 {
181 #ifdef CONFIG_NFS_V4
182         struct vfsmount *mnt = NULL;
183         switch (server->nfs_client->cl_nfsversion) {
184                 case 2:
185                 case 3:
186                         mnt = vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
187                         break;
188                 case 4:
189                         mnt = vfs_kern_mount(&clone_nfs4_fs_type, 0, devname, mountdata);
190         }
191         return mnt;
192 #else
193         return vfs_kern_mount(&clone_nfs_fs_type, 0, devname, mountdata);
194 #endif
195 }
196
197 /**
198  * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
199  * @mnt_parent - mountpoint of parent directory
200  * @dentry - parent directory
201  * @fh - filehandle for new root dentry
202  * @fattr - attributes for new root inode
203  *
204  */
205 struct vfsmount *nfs_do_submount(const struct vfsmount *mnt_parent,
206                 const struct dentry *dentry, struct nfs_fh *fh,
207                 struct nfs_fattr *fattr)
208 {
209         struct nfs_clone_mount mountdata = {
210                 .sb = mnt_parent->mnt_sb,
211                 .dentry = dentry,
212                 .fh = fh,
213                 .fattr = fattr,
214         };
215         struct vfsmount *mnt = ERR_PTR(-ENOMEM);
216         char *page = (char *) __get_free_page(GFP_USER);
217         char *devname;
218
219         dprintk("%s: submounting on %s/%s\n", __FUNCTION__,
220                         dentry->d_parent->d_name.name,
221                         dentry->d_name.name);
222         if (page == NULL)
223                 goto out;
224         devname = nfs_devname(mnt_parent, dentry, page, PAGE_SIZE);
225         mnt = (struct vfsmount *)devname;
226         if (IS_ERR(devname))
227                 goto free_page;
228         mnt = nfs_do_clone_mount(NFS_SB(mnt_parent->mnt_sb), devname, &mountdata);
229 free_page:
230         free_page((unsigned long)page);
231 out:
232         dprintk("%s: done\n", __FUNCTION__);
233         return mnt;
234 }