NFS: Convert v2 into a module
[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
ddda8e0a 10#include <linux/module.h>
55a97593 11#include <linux/dcache.h>
5a0e3ad6 12#include <linux/gfp.h>
55a97593
TM
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>
7ebb9315 19#include <linux/sunrpc/gss_api.h>
f7b422b1 20#include "internal.h"
55a97593
TM
21
22#define NFSDBG_FACILITY NFSDBG_VFS
23
65f27f38 24static void nfs_expire_automounts(struct work_struct *work);
f7b422b1 25
a3dab293 26static LIST_HEAD(nfs_automount_list);
65f27f38 27static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
51d8fa6a
TM
28int nfs_mountpoint_expiry_timeout = 500 * HZ;
29
f7b422b1
DH
30/*
31 * nfs_path - reconstruct the path given an arbitrary dentry
b514f872 32 * @base - used to return pointer to the end of devname part of path
f7b422b1
DH
33 * @dentry - pointer to dentry
34 * @buffer - result buffer
35 * @buflen - length of buffer
36 *
b514f872
AV
37 * Helper function for constructing the server pathname
38 * by arbitrary hashed dentry.
f7b422b1
DH
39 *
40 * This is mainly for use in figuring out the path on the
b514f872
AV
41 * server side when automounting on top of an existing partition
42 * and in generating /proc/mounts and friends.
f7b422b1 43 */
b514f872 44char *nfs_path(char **p, struct dentry *dentry, char *buffer, ssize_t buflen)
f7b422b1 45{
949854d0 46 char *end;
f7b422b1 47 int namelen;
949854d0 48 unsigned seq;
b514f872 49 const char *base;
f7b422b1 50
949854d0
NP
51rename_retry:
52 end = buffer+buflen;
f7b422b1
DH
53 *--end = '\0';
54 buflen--;
949854d0
NP
55
56 seq = read_seqbegin(&rename_lock);
57 rcu_read_lock();
b514f872
AV
58 while (1) {
59 spin_lock(&dentry->d_lock);
60 if (IS_ROOT(dentry))
61 break;
f7b422b1
DH
62 namelen = dentry->d_name.len;
63 buflen -= namelen + 1;
64 if (buflen < 0)
ce510193 65 goto Elong_unlock;
f7b422b1
DH
66 end -= namelen;
67 memcpy(end, dentry->d_name.name, namelen);
68 *--end = '/';
b514f872 69 spin_unlock(&dentry->d_lock);
f7b422b1
DH
70 dentry = dentry->d_parent;
71 }
b514f872
AV
72 if (read_seqretry(&rename_lock, seq)) {
73 spin_unlock(&dentry->d_lock);
74 rcu_read_unlock();
949854d0 75 goto rename_retry;
b514f872 76 }
0b75b35c 77 if (*end != '/') {
b514f872
AV
78 if (--buflen < 0) {
79 spin_unlock(&dentry->d_lock);
80 rcu_read_unlock();
0b75b35c 81 goto Elong;
b514f872 82 }
0b75b35c
TM
83 *--end = '/';
84 }
b514f872
AV
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 }
f7b422b1
DH
93 namelen = strlen(base);
94 /* Strip off excess slashes in base string */
95 while (namelen > 0 && base[namelen - 1] == '/')
96 namelen--;
97 buflen -= namelen;
b514f872 98 if (buflen < 0) {
1c34092a 99 spin_unlock(&dentry->d_lock);
b514f872 100 rcu_read_unlock();
f7b422b1 101 goto Elong;
b514f872 102 }
f7b422b1
DH
103 end -= namelen;
104 memcpy(end, base, namelen);
b514f872
AV
105 spin_unlock(&dentry->d_lock);
106 rcu_read_unlock();
f7b422b1 107 return end;
ce510193 108Elong_unlock:
1c34092a 109 spin_unlock(&dentry->d_lock);
949854d0
NP
110 rcu_read_unlock();
111 if (read_seqretry(&rename_lock, seq))
112 goto rename_retry;
f7b422b1
DH
113Elong:
114 return ERR_PTR(-ENAMETOOLONG);
115}
116
55a97593 117/*
36d43a43
DH
118 * nfs_d_automount - Handle crossing a mountpoint on the server
119 * @path - The mountpoint
55a97593
TM
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 */
36d43a43 129struct vfsmount *nfs_d_automount(struct path *path)
55a97593
TM
130{
131 struct vfsmount *mnt;
281cad46 132 struct nfs_server *server = NFS_SERVER(path->dentry->d_inode);
a4d7f168
TM
133 struct nfs_fh *fh = NULL;
134 struct nfs_fattr *fattr = NULL;
55a97593 135
36d43a43 136 dprintk("--> nfs_d_automount()\n");
54ceac45 137
36d43a43
DH
138 mnt = ERR_PTR(-ESTALE);
139 if (IS_ROOT(path->dentry))
140 goto out_nofree;
44d5759d 141
36d43a43 142 mnt = ERR_PTR(-ENOMEM);
a4d7f168
TM
143 fh = nfs_alloc_fhandle();
144 fattr = nfs_alloc_fattr();
145 if (fh == NULL || fattr == NULL)
36d43a43 146 goto out;
a4d7f168 147
3110ff80 148 dprintk("%s: enter\n", __func__);
54ceac45 149
281cad46 150 mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr);
55a97593 151 if (IS_ERR(mnt))
36d43a43 152 goto out;
55a97593 153
ea5b778a
DH
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);
36d43a43 158
55a97593 159out:
a4d7f168
TM
160 nfs_free_fattr(fattr);
161 nfs_free_fhandle(fh);
36d43a43 162out_nofree:
d7c32675
CL
163 if (IS_ERR(mnt))
164 dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt));
165 else
166 dprintk("<-- %s() = %p\n", __func__, mnt);
36d43a43 167 return mnt;
55a97593
TM
168}
169
92e1d5be 170const struct inode_operations nfs_mountpoint_inode_operations = {
55a97593
TM
171 .getattr = nfs_getattr,
172};
51d8fa6a 173
92e1d5be 174const struct inode_operations nfs_referral_inode_operations = {
6b97fd3d
MN
175};
176
65f27f38 177static void nfs_expire_automounts(struct work_struct *work)
51d8fa6a 178{
65f27f38 179 struct list_head *list = &nfs_automount_list;
51d8fa6a
TM
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
186void nfs_release_automount_timer(void)
187{
3d39c691 188 if (list_empty(&nfs_automount_list))
560aef74 189 cancel_delayed_work(&nfs_automount_task);
51d8fa6a 190}
f7b422b1
DH
191
192/*
193 * Clone a mountpoint of the appropriate type
194 */
509de811
DH
195static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
196 const char *devname,
f7b422b1
DH
197 struct nfs_clone_mount *mountdata)
198{
54ceac45 199 return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
f7b422b1
DH
200}
201
202/**
203 * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
f7b422b1
DH
204 * @dentry - parent directory
205 * @fh - filehandle for new root dentry
206 * @fattr - attributes for new root inode
7ebb9315 207 * @authflavor - security flavor to use when performing the mount
f7b422b1
DH
208 *
209 */
281cad46
BS
210struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh,
211 struct nfs_fattr *fattr, rpc_authflavor_t authflavor)
f7b422b1
DH
212{
213 struct nfs_clone_mount mountdata = {
f8ad9c4b 214 .sb = dentry->d_sb,
f7b422b1
DH
215 .dentry = dentry,
216 .fh = fh,
217 .fattr = fattr,
7ebb9315 218 .authflavor = authflavor,
f7b422b1
DH
219 };
220 struct vfsmount *mnt = ERR_PTR(-ENOMEM);
221 char *page = (char *) __get_free_page(GFP_USER);
222 char *devname;
223
54ceac45
DH
224 dprintk("--> nfs_do_submount()\n");
225
3110ff80 226 dprintk("%s: submounting on %s/%s\n", __func__,
f7b422b1
DH
227 dentry->d_parent->d_name.name,
228 dentry->d_name.name);
229 if (page == NULL)
230 goto out;
b514f872 231 devname = nfs_devname(dentry, page, PAGE_SIZE);
f7b422b1
DH
232 mnt = (struct vfsmount *)devname;
233 if (IS_ERR(devname))
234 goto free_page;
f8ad9c4b 235 mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata);
f7b422b1
DH
236free_page:
237 free_page((unsigned long)page);
238out:
3110ff80 239 dprintk("%s: done\n", __func__);
54ceac45
DH
240
241 dprintk("<-- nfs_do_submount() = %p\n", mnt);
f7b422b1
DH
242 return mnt;
243}
281cad46
BS
244
245struct 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 */
80a16b21 252 err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr);
281cad46
BS
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}
ddda8e0a 259EXPORT_SYMBOL_GPL(nfs_submount);
This page took 0.499955 seconds and 5 git commands to generate.