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