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