Commit | Line | Data |
---|---|---|
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 | 24 | static void nfs_expire_automounts(struct work_struct *work); |
f7b422b1 | 25 | |
a3dab293 | 26 | static LIST_HEAD(nfs_automount_list); |
65f27f38 | 27 | static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts); |
51d8fa6a TM |
28 | int 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 | 44 | char *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 |
51 | rename_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 | 108 | Elong_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 |
113 | Elong: |
114 | return ERR_PTR(-ENAMETOOLONG); | |
115 | } | |
89d77c8f | 116 | EXPORT_SYMBOL_GPL(nfs_path); |
f7b422b1 | 117 | |
55a97593 | 118 | /* |
36d43a43 DH |
119 | * nfs_d_automount - Handle crossing a mountpoint on the server |
120 | * @path - The mountpoint | |
55a97593 TM |
121 | * |
122 | * When we encounter a mountpoint on the server, we want to set up | |
123 | * a mountpoint on the client too, to prevent inode numbers from | |
124 | * colliding, and to allow "df" to work properly. | |
125 | * On NFSv4, we also want to allow for the fact that different | |
126 | * filesystems may be migrated to different servers in a failover | |
127 | * situation, and that different filesystems may want to use | |
128 | * different security flavours. | |
129 | */ | |
36d43a43 | 130 | struct vfsmount *nfs_d_automount(struct path *path) |
55a97593 TM |
131 | { |
132 | struct vfsmount *mnt; | |
281cad46 | 133 | struct nfs_server *server = NFS_SERVER(path->dentry->d_inode); |
a4d7f168 TM |
134 | struct nfs_fh *fh = NULL; |
135 | struct nfs_fattr *fattr = NULL; | |
55a97593 | 136 | |
36d43a43 | 137 | dprintk("--> nfs_d_automount()\n"); |
54ceac45 | 138 | |
36d43a43 DH |
139 | mnt = ERR_PTR(-ESTALE); |
140 | if (IS_ROOT(path->dentry)) | |
141 | goto out_nofree; | |
44d5759d | 142 | |
36d43a43 | 143 | mnt = ERR_PTR(-ENOMEM); |
a4d7f168 TM |
144 | fh = nfs_alloc_fhandle(); |
145 | fattr = nfs_alloc_fattr(); | |
146 | if (fh == NULL || fattr == NULL) | |
36d43a43 | 147 | goto out; |
a4d7f168 | 148 | |
3110ff80 | 149 | dprintk("%s: enter\n", __func__); |
54ceac45 | 150 | |
281cad46 | 151 | mnt = server->nfs_client->rpc_ops->submount(server, path->dentry, fh, fattr); |
55a97593 | 152 | if (IS_ERR(mnt)) |
36d43a43 | 153 | goto out; |
55a97593 | 154 | |
ea5b778a DH |
155 | dprintk("%s: done, success\n", __func__); |
156 | mntget(mnt); /* prevent immediate expiration */ | |
157 | mnt_set_expiry(mnt, &nfs_automount_list); | |
158 | schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout); | |
36d43a43 | 159 | |
55a97593 | 160 | out: |
a4d7f168 TM |
161 | nfs_free_fattr(fattr); |
162 | nfs_free_fhandle(fh); | |
36d43a43 | 163 | out_nofree: |
d7c32675 CL |
164 | if (IS_ERR(mnt)) |
165 | dprintk("<-- %s(): error %ld\n", __func__, PTR_ERR(mnt)); | |
166 | else | |
167 | dprintk("<-- %s() = %p\n", __func__, mnt); | |
36d43a43 | 168 | return mnt; |
55a97593 TM |
169 | } |
170 | ||
92e1d5be | 171 | const struct inode_operations nfs_mountpoint_inode_operations = { |
55a97593 TM |
172 | .getattr = nfs_getattr, |
173 | }; | |
51d8fa6a | 174 | |
92e1d5be | 175 | const struct inode_operations nfs_referral_inode_operations = { |
6b97fd3d MN |
176 | }; |
177 | ||
65f27f38 | 178 | static void nfs_expire_automounts(struct work_struct *work) |
51d8fa6a | 179 | { |
65f27f38 | 180 | struct list_head *list = &nfs_automount_list; |
51d8fa6a TM |
181 | |
182 | mark_mounts_for_expiry(list); | |
183 | if (!list_empty(list)) | |
184 | schedule_delayed_work(&nfs_automount_task, nfs_mountpoint_expiry_timeout); | |
185 | } | |
186 | ||
187 | void nfs_release_automount_timer(void) | |
188 | { | |
3d39c691 | 189 | if (list_empty(&nfs_automount_list)) |
560aef74 | 190 | cancel_delayed_work(&nfs_automount_task); |
51d8fa6a | 191 | } |
f7b422b1 DH |
192 | |
193 | /* | |
194 | * Clone a mountpoint of the appropriate type | |
195 | */ | |
509de811 DH |
196 | static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server, |
197 | const char *devname, | |
f7b422b1 DH |
198 | struct nfs_clone_mount *mountdata) |
199 | { | |
54ceac45 | 200 | return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata); |
f7b422b1 DH |
201 | } |
202 | ||
203 | /** | |
204 | * nfs_do_submount - set up mountpoint when crossing a filesystem boundary | |
f7b422b1 DH |
205 | * @dentry - parent directory |
206 | * @fh - filehandle for new root dentry | |
207 | * @fattr - attributes for new root inode | |
7ebb9315 | 208 | * @authflavor - security flavor to use when performing the mount |
f7b422b1 DH |
209 | * |
210 | */ | |
281cad46 BS |
211 | struct vfsmount *nfs_do_submount(struct dentry *dentry, struct nfs_fh *fh, |
212 | struct nfs_fattr *fattr, rpc_authflavor_t authflavor) | |
f7b422b1 DH |
213 | { |
214 | struct nfs_clone_mount mountdata = { | |
f8ad9c4b | 215 | .sb = dentry->d_sb, |
f7b422b1 DH |
216 | .dentry = dentry, |
217 | .fh = fh, | |
218 | .fattr = fattr, | |
7ebb9315 | 219 | .authflavor = authflavor, |
f7b422b1 DH |
220 | }; |
221 | struct vfsmount *mnt = ERR_PTR(-ENOMEM); | |
222 | char *page = (char *) __get_free_page(GFP_USER); | |
223 | char *devname; | |
224 | ||
54ceac45 DH |
225 | dprintk("--> nfs_do_submount()\n"); |
226 | ||
3110ff80 | 227 | dprintk("%s: submounting on %s/%s\n", __func__, |
f7b422b1 DH |
228 | dentry->d_parent->d_name.name, |
229 | dentry->d_name.name); | |
230 | if (page == NULL) | |
231 | goto out; | |
b514f872 | 232 | devname = nfs_devname(dentry, page, PAGE_SIZE); |
f7b422b1 DH |
233 | mnt = (struct vfsmount *)devname; |
234 | if (IS_ERR(devname)) | |
235 | goto free_page; | |
f8ad9c4b | 236 | mnt = nfs_do_clone_mount(NFS_SB(dentry->d_sb), devname, &mountdata); |
f7b422b1 DH |
237 | free_page: |
238 | free_page((unsigned long)page); | |
239 | out: | |
3110ff80 | 240 | dprintk("%s: done\n", __func__); |
54ceac45 DH |
241 | |
242 | dprintk("<-- nfs_do_submount() = %p\n", mnt); | |
f7b422b1 DH |
243 | return mnt; |
244 | } | |
89d77c8f | 245 | EXPORT_SYMBOL_GPL(nfs_do_submount); |
281cad46 BS |
246 | |
247 | struct vfsmount *nfs_submount(struct nfs_server *server, struct dentry *dentry, | |
248 | struct nfs_fh *fh, struct nfs_fattr *fattr) | |
249 | { | |
250 | int err; | |
251 | struct dentry *parent = dget_parent(dentry); | |
252 | ||
253 | /* Look it up again to get its attributes */ | |
80a16b21 | 254 | err = server->nfs_client->rpc_ops->lookup(parent->d_inode, &dentry->d_name, fh, fattr); |
281cad46 BS |
255 | dput(parent); |
256 | if (err != 0) | |
257 | return ERR_PTR(err); | |
258 | ||
259 | return nfs_do_submount(dentry, fh, fattr, server->client->cl_auth->au_flavor); | |
260 | } | |
ddda8e0a | 261 | EXPORT_SYMBOL_GPL(nfs_submount); |