]>
Commit | Line | Data |
---|---|---|
ec26815a | 1 | /* mountpoint management |
1da177e4 LT |
2 | * |
3 | * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/init.h> | |
1da177e4 LT |
15 | #include <linux/fs.h> |
16 | #include <linux/pagemap.h> | |
17 | #include <linux/mount.h> | |
18 | #include <linux/namei.h> | |
5a0e3ad6 | 19 | #include <linux/gfp.h> |
1da177e4 LT |
20 | #include "internal.h" |
21 | ||
22 | ||
23 | static struct dentry *afs_mntpt_lookup(struct inode *dir, | |
24 | struct dentry *dentry, | |
25 | struct nameidata *nd); | |
26 | static int afs_mntpt_open(struct inode *inode, struct file *file); | |
008b150a | 27 | static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd); |
08e0e7c8 | 28 | static void afs_mntpt_expiry_timed_out(struct work_struct *work); |
1da177e4 | 29 | |
4b6f5d20 | 30 | const struct file_operations afs_mntpt_file_operations = { |
1da177e4 | 31 | .open = afs_mntpt_open, |
6038f373 | 32 | .llseek = noop_llseek, |
1da177e4 LT |
33 | }; |
34 | ||
754661f1 | 35 | const struct inode_operations afs_mntpt_inode_operations = { |
1da177e4 LT |
36 | .lookup = afs_mntpt_lookup, |
37 | .follow_link = afs_mntpt_follow_link, | |
38 | .readlink = page_readlink, | |
416351f2 | 39 | .getattr = afs_getattr, |
1da177e4 LT |
40 | }; |
41 | ||
bec5eb61 | 42 | const struct inode_operations afs_autocell_inode_operations = { |
43 | .follow_link = afs_mntpt_follow_link, | |
44 | .getattr = afs_getattr, | |
45 | }; | |
46 | ||
1da177e4 | 47 | static LIST_HEAD(afs_vfsmounts); |
08e0e7c8 | 48 | static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out); |
1da177e4 | 49 | |
c1206a2c | 50 | static unsigned long afs_mntpt_expiry_timeout = 10 * 60; |
1da177e4 | 51 | |
1da177e4 LT |
52 | /* |
53 | * check a symbolic link to see whether it actually encodes a mountpoint | |
54 | * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately | |
55 | */ | |
00d3b7a4 | 56 | int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key) |
1da177e4 LT |
57 | { |
58 | struct page *page; | |
1da177e4 LT |
59 | size_t size; |
60 | char *buf; | |
61 | int ret; | |
62 | ||
416351f2 DH |
63 | _enter("{%x:%u,%u}", |
64 | vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique); | |
1da177e4 LT |
65 | |
66 | /* read the contents of the symlink into the pagecache */ | |
f6d335c0 AV |
67 | page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, |
68 | afs_page_filler, key); | |
1da177e4 LT |
69 | if (IS_ERR(page)) { |
70 | ret = PTR_ERR(page); | |
71 | goto out; | |
72 | } | |
73 | ||
74 | ret = -EIO; | |
1da177e4 LT |
75 | if (PageError(page)) |
76 | goto out_free; | |
77 | ||
6fe6900e NP |
78 | buf = kmap(page); |
79 | ||
1da177e4 LT |
80 | /* examine the symlink's contents */ |
81 | size = vnode->status.size; | |
08e0e7c8 | 82 | _debug("symlink to %*.*s", (int) size, (int) size, buf); |
1da177e4 LT |
83 | |
84 | if (size > 2 && | |
85 | (buf[0] == '%' || buf[0] == '#') && | |
86 | buf[size - 1] == '.' | |
87 | ) { | |
88 | _debug("symlink is a mountpoint"); | |
89 | spin_lock(&vnode->lock); | |
08e0e7c8 | 90 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
1da177e4 LT |
91 | spin_unlock(&vnode->lock); |
92 | } | |
93 | ||
94 | ret = 0; | |
95 | ||
1da177e4 | 96 | kunmap(page); |
6fe6900e | 97 | out_free: |
1da177e4 | 98 | page_cache_release(page); |
ec26815a | 99 | out: |
1da177e4 LT |
100 | _leave(" = %d", ret); |
101 | return ret; | |
ec26815a | 102 | } |
1da177e4 | 103 | |
1da177e4 LT |
104 | /* |
105 | * no valid lookup procedure on this sort of dir | |
106 | */ | |
107 | static struct dentry *afs_mntpt_lookup(struct inode *dir, | |
108 | struct dentry *dentry, | |
109 | struct nameidata *nd) | |
110 | { | |
08e0e7c8 | 111 | _enter("%p,%p{%p{%s},%s}", |
1da177e4 LT |
112 | dir, |
113 | dentry, | |
114 | dentry->d_parent, | |
115 | dentry->d_parent ? | |
116 | dentry->d_parent->d_name.name : (const unsigned char *) "", | |
117 | dentry->d_name.name); | |
118 | ||
119 | return ERR_PTR(-EREMOTE); | |
ec26815a | 120 | } |
1da177e4 | 121 | |
1da177e4 LT |
122 | /* |
123 | * no valid open procedure on this sort of dir | |
124 | */ | |
125 | static int afs_mntpt_open(struct inode *inode, struct file *file) | |
126 | { | |
08e0e7c8 | 127 | _enter("%p,%p{%p{%s},%s}", |
1da177e4 | 128 | inode, file, |
1d56a969 JS |
129 | file->f_path.dentry->d_parent, |
130 | file->f_path.dentry->d_parent ? | |
131 | file->f_path.dentry->d_parent->d_name.name : | |
1da177e4 | 132 | (const unsigned char *) "", |
1d56a969 | 133 | file->f_path.dentry->d_name.name); |
1da177e4 LT |
134 | |
135 | return -EREMOTE; | |
ec26815a | 136 | } |
1da177e4 | 137 | |
1da177e4 LT |
138 | /* |
139 | * create a vfsmount to be automounted | |
140 | */ | |
141 | static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt) | |
142 | { | |
143 | struct afs_super_info *super; | |
144 | struct vfsmount *mnt; | |
bec5eb61 | 145 | struct afs_vnode *vnode; |
083fd8b2 | 146 | struct page *page; |
bec5eb61 | 147 | char *devname, *options; |
148 | bool rwpath = false; | |
1da177e4 LT |
149 | int ret; |
150 | ||
08e0e7c8 | 151 | _enter("{%s}", mntpt->d_name.name); |
1da177e4 LT |
152 | |
153 | BUG_ON(!mntpt->d_inode); | |
154 | ||
1da177e4 LT |
155 | ret = -ENOMEM; |
156 | devname = (char *) get_zeroed_page(GFP_KERNEL); | |
157 | if (!devname) | |
083fd8b2 | 158 | goto error_no_devname; |
1da177e4 LT |
159 | |
160 | options = (char *) get_zeroed_page(GFP_KERNEL); | |
161 | if (!options) | |
083fd8b2 | 162 | goto error_no_options; |
1da177e4 | 163 | |
bec5eb61 | 164 | vnode = AFS_FS_I(mntpt->d_inode); |
165 | if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) { | |
166 | /* if the directory is a pseudo directory, use the d_name */ | |
167 | static const char afs_root_cell[] = ":root.cell."; | |
168 | unsigned size = mntpt->d_name.len; | |
169 | ||
170 | ret = -ENOENT; | |
171 | if (size < 2 || size > AFS_MAXCELLNAME) | |
172 | goto error_no_page; | |
173 | ||
174 | if (mntpt->d_name.name[0] == '.') { | |
175 | devname[0] = '#'; | |
176 | memcpy(devname + 1, mntpt->d_name.name, size - 1); | |
177 | memcpy(devname + size, afs_root_cell, | |
178 | sizeof(afs_root_cell)); | |
179 | rwpath = true; | |
180 | } else { | |
181 | devname[0] = '%'; | |
182 | memcpy(devname + 1, mntpt->d_name.name, size); | |
183 | memcpy(devname + size + 1, afs_root_cell, | |
184 | sizeof(afs_root_cell)); | |
185 | } | |
186 | } else { | |
187 | /* read the contents of the AFS special symlink */ | |
188 | loff_t size = i_size_read(mntpt->d_inode); | |
189 | char *buf; | |
190 | ||
191 | ret = -EINVAL; | |
192 | if (size > PAGE_SIZE - 1) | |
193 | goto error_no_page; | |
194 | ||
195 | page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL); | |
196 | if (IS_ERR(page)) { | |
197 | ret = PTR_ERR(page); | |
198 | goto error_no_page; | |
199 | } | |
200 | ||
201 | ret = -EIO; | |
202 | if (PageError(page)) | |
203 | goto error; | |
204 | ||
205 | buf = kmap_atomic(page, KM_USER0); | |
206 | memcpy(devname, buf, size); | |
207 | kunmap_atomic(buf, KM_USER0); | |
208 | page_cache_release(page); | |
209 | page = NULL; | |
1da177e4 LT |
210 | } |
211 | ||
1da177e4 LT |
212 | /* work out what options we want */ |
213 | super = AFS_FS_S(mntpt->d_sb); | |
214 | memcpy(options, "cell=", 5); | |
215 | strcpy(options + 5, super->volume->cell->name); | |
bec5eb61 | 216 | if (super->volume->type == AFSVL_RWVOL || rwpath) |
1da177e4 LT |
217 | strcat(options, ",rwpath"); |
218 | ||
219 | /* try and do the mount */ | |
08e0e7c8 | 220 | _debug("--- attempting mount %s -o %s ---", devname, options); |
1f5ce9e9 | 221 | mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options); |
08e0e7c8 | 222 | _debug("--- mount result %p ---", mnt); |
1da177e4 LT |
223 | |
224 | free_page((unsigned long) devname); | |
225 | free_page((unsigned long) options); | |
08e0e7c8 | 226 | _leave(" = %p", mnt); |
1da177e4 LT |
227 | return mnt; |
228 | ||
ec26815a | 229 | error: |
083fd8b2 DH |
230 | page_cache_release(page); |
231 | error_no_page: | |
232 | free_page((unsigned long) options); | |
233 | error_no_options: | |
234 | free_page((unsigned long) devname); | |
235 | error_no_devname: | |
08e0e7c8 | 236 | _leave(" = %d", ret); |
1da177e4 | 237 | return ERR_PTR(ret); |
ec26815a | 238 | } |
1da177e4 | 239 | |
1da177e4 LT |
240 | /* |
241 | * follow a link from a mountpoint directory, thus causing it to be mounted | |
242 | */ | |
008b150a | 243 | static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
244 | { |
245 | struct vfsmount *newmnt; | |
1da177e4 LT |
246 | int err; |
247 | ||
00d3b7a4 | 248 | _enter("%p{%s},{%s:%p{%s},}", |
1da177e4 LT |
249 | dentry, |
250 | dentry->d_name.name, | |
4ac91378 | 251 | nd->path.mnt->mnt_devname, |
1da177e4 | 252 | dentry, |
4ac91378 | 253 | nd->path.dentry->d_name.name); |
1da177e4 | 254 | |
4ac91378 JB |
255 | dput(nd->path.dentry); |
256 | nd->path.dentry = dget(dentry); | |
08e0e7c8 | 257 | |
4ac91378 | 258 | newmnt = afs_mntpt_do_automount(nd->path.dentry); |
1da177e4 | 259 | if (IS_ERR(newmnt)) { |
1d957f9b | 260 | path_put(&nd->path); |
008b150a | 261 | return (void *)newmnt; |
1da177e4 LT |
262 | } |
263 | ||
08e0e7c8 | 264 | mntget(newmnt); |
8d66bf54 | 265 | err = do_add_mount(newmnt, &nd->path, MNT_SHRINKABLE, &afs_vfsmounts); |
08e0e7c8 DH |
266 | switch (err) { |
267 | case 0: | |
09da5916 | 268 | path_put(&nd->path); |
4ac91378 JB |
269 | nd->path.mnt = newmnt; |
270 | nd->path.dentry = dget(newmnt->mnt_root); | |
08e0e7c8 DH |
271 | schedule_delayed_work(&afs_mntpt_expiry_timer, |
272 | afs_mntpt_expiry_timeout * HZ); | |
273 | break; | |
274 | case -EBUSY: | |
275 | /* someone else made a mount here whilst we were busy */ | |
4ac91378 | 276 | while (d_mountpoint(nd->path.dentry) && |
9393bd07 | 277 | follow_down(&nd->path)) |
08e0e7c8 DH |
278 | ; |
279 | err = 0; | |
280 | default: | |
281 | mntput(newmnt); | |
282 | break; | |
1da177e4 LT |
283 | } |
284 | ||
08e0e7c8 | 285 | _leave(" = %d", err); |
008b150a | 286 | return ERR_PTR(err); |
ec26815a | 287 | } |
1da177e4 | 288 | |
1da177e4 LT |
289 | /* |
290 | * handle mountpoint expiry timer going off | |
291 | */ | |
08e0e7c8 | 292 | static void afs_mntpt_expiry_timed_out(struct work_struct *work) |
1da177e4 | 293 | { |
08e0e7c8 DH |
294 | _enter(""); |
295 | ||
296 | if (!list_empty(&afs_vfsmounts)) { | |
297 | mark_mounts_for_expiry(&afs_vfsmounts); | |
298 | schedule_delayed_work(&afs_mntpt_expiry_timer, | |
299 | afs_mntpt_expiry_timeout * HZ); | |
300 | } | |
301 | ||
302 | _leave(""); | |
303 | } | |
1da177e4 | 304 | |
08e0e7c8 DH |
305 | /* |
306 | * kill the AFS mountpoint timer if it's still running | |
307 | */ | |
308 | void afs_mntpt_kill_timer(void) | |
309 | { | |
310 | _enter(""); | |
1da177e4 | 311 | |
08e0e7c8 DH |
312 | ASSERT(list_empty(&afs_vfsmounts)); |
313 | cancel_delayed_work(&afs_mntpt_expiry_timer); | |
314 | flush_scheduled_work(); | |
315 | } |