]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* -*- c -*- --------------------------------------------------------------- * |
2 | * | |
3 | * linux/fs/autofs/root.c | |
4 | * | |
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | |
6 | * Copyright 1999-2000 Jeremy Fitzhardinge <[email protected]> | |
34ca959c | 7 | * Copyright 2001-2006 Ian Kent <[email protected]> |
1da177e4 LT |
8 | * |
9 | * This file is part of the Linux kernel and is made available under | |
10 | * the terms of the GNU General Public License, version 2, or at your | |
11 | * option, any later version, incorporated herein by reference. | |
12 | * | |
13 | * ------------------------------------------------------------------------- */ | |
14 | ||
16f7e0fe | 15 | #include <linux/capability.h> |
1da177e4 LT |
16 | #include <linux/errno.h> |
17 | #include <linux/stat.h> | |
5a0e3ad6 | 18 | #include <linux/slab.h> |
1da177e4 LT |
19 | #include <linux/param.h> |
20 | #include <linux/time.h> | |
c9243f5b | 21 | #include <linux/compat.h> |
00e300e1 | 22 | #include <linux/mutex.h> |
c9243f5b | 23 | |
1da177e4 LT |
24 | #include "autofs_i.h" |
25 | ||
26 | static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *); | |
27 | static int autofs4_dir_unlink(struct inode *,struct dentry *); | |
28 | static int autofs4_dir_rmdir(struct inode *,struct dentry *); | |
18bb1db3 | 29 | static int autofs4_dir_mkdir(struct inode *,struct dentry *,umode_t); |
3663df70 | 30 | static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long); |
5a44a73b | 31 | #ifdef CONFIG_COMPAT |
c9243f5b | 32 | static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long); |
5a44a73b | 33 | #endif |
1da177e4 | 34 | static int autofs4_dir_open(struct inode *inode, struct file *file); |
00cd8dd3 | 35 | static struct dentry *autofs4_lookup(struct inode *,struct dentry *, unsigned int); |
71e469db | 36 | static struct vfsmount *autofs4_d_automount(struct path *); |
1aed3e42 | 37 | static int autofs4_d_manage(struct dentry *, bool); |
b89b12b4 | 38 | static void autofs4_dentry_release(struct dentry *); |
1da177e4 | 39 | |
4b6f5d20 | 40 | const struct file_operations autofs4_root_operations = { |
1da177e4 LT |
41 | .open = dcache_dir_open, |
42 | .release = dcache_dir_close, | |
43 | .read = generic_read_dir, | |
aa55ddf3 | 44 | .readdir = dcache_readdir, |
59af1584 | 45 | .llseek = dcache_dir_lseek, |
3663df70 | 46 | .unlocked_ioctl = autofs4_root_ioctl, |
c9243f5b AB |
47 | #ifdef CONFIG_COMPAT |
48 | .compat_ioctl = autofs4_root_compat_ioctl, | |
49 | #endif | |
1da177e4 LT |
50 | }; |
51 | ||
4b6f5d20 | 52 | const struct file_operations autofs4_dir_operations = { |
1da177e4 | 53 | .open = autofs4_dir_open, |
ff9cd499 | 54 | .release = dcache_dir_close, |
1da177e4 | 55 | .read = generic_read_dir, |
ff9cd499 | 56 | .readdir = dcache_readdir, |
59af1584 | 57 | .llseek = dcache_dir_lseek, |
1da177e4 LT |
58 | }; |
59 | ||
754661f1 | 60 | const struct inode_operations autofs4_dir_inode_operations = { |
1da177e4 LT |
61 | .lookup = autofs4_lookup, |
62 | .unlink = autofs4_dir_unlink, | |
63 | .symlink = autofs4_dir_symlink, | |
64 | .mkdir = autofs4_dir_mkdir, | |
65 | .rmdir = autofs4_dir_rmdir, | |
66 | }; | |
67 | ||
71e469db | 68 | const struct dentry_operations autofs4_dentry_operations = { |
71e469db IK |
69 | .d_automount = autofs4_d_automount, |
70 | .d_manage = autofs4_d_manage, | |
71 | .d_release = autofs4_dentry_release, | |
72 | }; | |
73 | ||
4f8427d1 IK |
74 | static void autofs4_add_active(struct dentry *dentry) |
75 | { | |
76 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
77 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
78 | if (ino) { | |
79 | spin_lock(&sbi->lookup_lock); | |
80 | if (!ino->active_count) { | |
81 | if (list_empty(&ino->active)) | |
82 | list_add(&ino->active, &sbi->active_list); | |
83 | } | |
84 | ino->active_count++; | |
85 | spin_unlock(&sbi->lookup_lock); | |
86 | } | |
87 | return; | |
88 | } | |
89 | ||
90 | static void autofs4_del_active(struct dentry *dentry) | |
91 | { | |
92 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
93 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
94 | if (ino) { | |
95 | spin_lock(&sbi->lookup_lock); | |
96 | ino->active_count--; | |
97 | if (!ino->active_count) { | |
98 | if (!list_empty(&ino->active)) | |
99 | list_del_init(&ino->active); | |
100 | } | |
101 | spin_unlock(&sbi->lookup_lock); | |
102 | } | |
103 | return; | |
104 | } | |
105 | ||
1da177e4 LT |
106 | static int autofs4_dir_open(struct inode *inode, struct file *file) |
107 | { | |
a4669ed8 | 108 | struct dentry *dentry = file->f_path.dentry; |
1da177e4 | 109 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
f360ce3b | 110 | |
1da177e4 LT |
111 | DPRINTK("file=%p dentry=%p %.*s", |
112 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
113 | ||
114 | if (autofs4_oz_mode(sbi)) | |
115 | goto out; | |
116 | ||
ff9cd499 IK |
117 | /* |
118 | * An empty directory in an autofs file system is always a | |
119 | * mount point. The daemon must have failed to mount this | |
120 | * during lookup so it doesn't exist. This can happen, for | |
121 | * example, if user space returns an incorrect status for a | |
122 | * mount request. Otherwise we're doing a readdir on the | |
123 | * autofs file system so just let the libfs routines handle | |
124 | * it. | |
125 | */ | |
e7854723 | 126 | spin_lock(&sbi->lookup_lock); |
2fd6b7f5 | 127 | spin_lock(&dentry->d_lock); |
c42c7f7e | 128 | if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { |
2fd6b7f5 | 129 | spin_unlock(&dentry->d_lock); |
e7854723 | 130 | spin_unlock(&sbi->lookup_lock); |
ff9cd499 | 131 | return -ENOENT; |
1da177e4 | 132 | } |
2fd6b7f5 | 133 | spin_unlock(&dentry->d_lock); |
e7854723 | 134 | spin_unlock(&sbi->lookup_lock); |
1da177e4 | 135 | |
1da177e4 | 136 | out: |
ff9cd499 | 137 | return dcache_dir_open(inode, file); |
1da177e4 LT |
138 | } |
139 | ||
b89b12b4 | 140 | static void autofs4_dentry_release(struct dentry *de) |
1da177e4 | 141 | { |
b89b12b4 AV |
142 | struct autofs_info *ino = autofs4_dentry_ino(de); |
143 | struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb); | |
1da177e4 LT |
144 | |
145 | DPRINTK("releasing %p", de); | |
146 | ||
b89b12b4 AV |
147 | if (!ino) |
148 | return; | |
149 | ||
150 | if (sbi) { | |
151 | spin_lock(&sbi->lookup_lock); | |
152 | if (!list_empty(&ino->active)) | |
153 | list_del(&ino->active); | |
154 | if (!list_empty(&ino->expiring)) | |
155 | list_del(&ino->expiring); | |
156 | spin_unlock(&sbi->lookup_lock); | |
1da177e4 | 157 | } |
b89b12b4 AV |
158 | |
159 | autofs4_free_ino(ino); | |
1da177e4 LT |
160 | } |
161 | ||
6510c9d8 | 162 | static struct dentry *autofs4_lookup_active(struct dentry *dentry) |
25767378 | 163 | { |
6510c9d8 IK |
164 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
165 | struct dentry *parent = dentry->d_parent; | |
166 | struct qstr *name = &dentry->d_name; | |
25767378 IK |
167 | unsigned int len = name->len; |
168 | unsigned int hash = name->hash; | |
169 | const unsigned char *str = name->name; | |
170 | struct list_head *p, *head; | |
171 | ||
25767378 IK |
172 | spin_lock(&sbi->lookup_lock); |
173 | head = &sbi->active_list; | |
174 | list_for_each(p, head) { | |
175 | struct autofs_info *ino; | |
e4d5ade7 | 176 | struct dentry *active; |
25767378 IK |
177 | struct qstr *qstr; |
178 | ||
179 | ino = list_entry(p, struct autofs_info, active); | |
e4d5ade7 | 180 | active = ino->dentry; |
25767378 | 181 | |
e4d5ade7 | 182 | spin_lock(&active->d_lock); |
25767378 IK |
183 | |
184 | /* Already gone? */ | |
b7ab39f6 | 185 | if (active->d_count == 0) |
25767378 IK |
186 | goto next; |
187 | ||
e4d5ade7 | 188 | qstr = &active->d_name; |
25767378 | 189 | |
e4d5ade7 | 190 | if (active->d_name.hash != hash) |
25767378 | 191 | goto next; |
e4d5ade7 | 192 | if (active->d_parent != parent) |
25767378 IK |
193 | goto next; |
194 | ||
195 | if (qstr->len != len) | |
196 | goto next; | |
197 | if (memcmp(qstr->name, str, len)) | |
198 | goto next; | |
199 | ||
4b1ae27a | 200 | if (d_unhashed(active)) { |
b7ab39f6 | 201 | dget_dlock(active); |
4b1ae27a AV |
202 | spin_unlock(&active->d_lock); |
203 | spin_unlock(&sbi->lookup_lock); | |
4b1ae27a AV |
204 | return active; |
205 | } | |
25767378 | 206 | next: |
e4d5ade7 | 207 | spin_unlock(&active->d_lock); |
25767378 IK |
208 | } |
209 | spin_unlock(&sbi->lookup_lock); | |
25767378 IK |
210 | |
211 | return NULL; | |
212 | } | |
213 | ||
6510c9d8 | 214 | static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) |
f50b6f86 | 215 | { |
6510c9d8 IK |
216 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
217 | struct dentry *parent = dentry->d_parent; | |
218 | struct qstr *name = &dentry->d_name; | |
f50b6f86 IK |
219 | unsigned int len = name->len; |
220 | unsigned int hash = name->hash; | |
221 | const unsigned char *str = name->name; | |
222 | struct list_head *p, *head; | |
223 | ||
5f6f4f28 IK |
224 | spin_lock(&sbi->lookup_lock); |
225 | head = &sbi->expiring_list; | |
f50b6f86 IK |
226 | list_for_each(p, head) { |
227 | struct autofs_info *ino; | |
cb4b492a | 228 | struct dentry *expiring; |
f50b6f86 IK |
229 | struct qstr *qstr; |
230 | ||
5f6f4f28 | 231 | ino = list_entry(p, struct autofs_info, expiring); |
cb4b492a | 232 | expiring = ino->dentry; |
f50b6f86 | 233 | |
cb4b492a | 234 | spin_lock(&expiring->d_lock); |
f50b6f86 IK |
235 | |
236 | /* Bad luck, we've already been dentry_iput */ | |
cb4b492a | 237 | if (!expiring->d_inode) |
f50b6f86 IK |
238 | goto next; |
239 | ||
cb4b492a | 240 | qstr = &expiring->d_name; |
f50b6f86 | 241 | |
cb4b492a | 242 | if (expiring->d_name.hash != hash) |
f50b6f86 | 243 | goto next; |
cb4b492a | 244 | if (expiring->d_parent != parent) |
f50b6f86 IK |
245 | goto next; |
246 | ||
247 | if (qstr->len != len) | |
248 | goto next; | |
249 | if (memcmp(qstr->name, str, len)) | |
250 | goto next; | |
251 | ||
4b1ae27a | 252 | if (d_unhashed(expiring)) { |
b7ab39f6 | 253 | dget_dlock(expiring); |
4b1ae27a AV |
254 | spin_unlock(&expiring->d_lock); |
255 | spin_unlock(&sbi->lookup_lock); | |
4b1ae27a AV |
256 | return expiring; |
257 | } | |
f50b6f86 | 258 | next: |
cb4b492a | 259 | spin_unlock(&expiring->d_lock); |
f50b6f86 | 260 | } |
5f6f4f28 | 261 | spin_unlock(&sbi->lookup_lock); |
f50b6f86 IK |
262 | |
263 | return NULL; | |
264 | } | |
265 | ||
10584211 IK |
266 | static int autofs4_mount_wait(struct dentry *dentry) |
267 | { | |
268 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
269 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
3c319985 | 270 | int status = 0; |
10584211 IK |
271 | |
272 | if (ino->flags & AUTOFS_INF_PENDING) { | |
273 | DPRINTK("waiting for mount name=%.*s", | |
274 | dentry->d_name.len, dentry->d_name.name); | |
275 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); | |
276 | DPRINTK("mount wait done status=%d", status); | |
10584211 | 277 | } |
3c319985 IK |
278 | ino->last_used = jiffies; |
279 | return status; | |
10584211 IK |
280 | } |
281 | ||
282 | static int do_expire_wait(struct dentry *dentry) | |
283 | { | |
284 | struct dentry *expiring; | |
285 | ||
286 | expiring = autofs4_lookup_expiring(dentry); | |
287 | if (!expiring) | |
288 | return autofs4_expire_wait(dentry); | |
289 | else { | |
290 | /* | |
291 | * If we are racing with expire the request might not | |
292 | * be quite complete, but the directory has been removed | |
293 | * so it must have been successful, just wait for it. | |
294 | */ | |
295 | autofs4_expire_wait(expiring); | |
296 | autofs4_del_expiring(expiring); | |
297 | dput(expiring); | |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
302 | static struct dentry *autofs4_mountpoint_changed(struct path *path) | |
303 | { | |
304 | struct dentry *dentry = path->dentry; | |
305 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
306 | ||
307 | /* | |
308 | * If this is an indirect mount the dentry could have gone away | |
309 | * as a result of an expire and a new one created. | |
310 | */ | |
311 | if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) { | |
312 | struct dentry *parent = dentry->d_parent; | |
3c319985 | 313 | struct autofs_info *ino; |
10584211 IK |
314 | struct dentry *new = d_lookup(parent, &dentry->d_name); |
315 | if (!new) | |
316 | return NULL; | |
3c319985 IK |
317 | ino = autofs4_dentry_ino(new); |
318 | ino->last_used = jiffies; | |
10584211 IK |
319 | dput(path->dentry); |
320 | path->dentry = new; | |
321 | } | |
322 | return path->dentry; | |
323 | } | |
324 | ||
71e469db | 325 | static struct vfsmount *autofs4_d_automount(struct path *path) |
10584211 IK |
326 | { |
327 | struct dentry *dentry = path->dentry; | |
328 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
329 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
330 | int status; | |
331 | ||
332 | DPRINTK("dentry=%p %.*s", | |
333 | dentry, dentry->d_name.len, dentry->d_name.name); | |
334 | ||
335 | /* The daemon never triggers a mount. */ | |
336 | if (autofs4_oz_mode(sbi)) | |
337 | return NULL; | |
338 | ||
339 | /* | |
340 | * If an expire request is pending everyone must wait. | |
341 | * If the expire fails we're still mounted so continue | |
342 | * the follow and return. A return of -EAGAIN (which only | |
343 | * happens with indirect mounts) means the expire completed | |
344 | * and the directory was removed, so just go ahead and try | |
345 | * the mount. | |
346 | */ | |
347 | status = do_expire_wait(dentry); | |
348 | if (status && status != -EAGAIN) | |
349 | return NULL; | |
350 | ||
351 | /* Callback to the daemon to perform the mount or wait */ | |
352 | spin_lock(&sbi->fs_lock); | |
353 | if (ino->flags & AUTOFS_INF_PENDING) { | |
354 | spin_unlock(&sbi->fs_lock); | |
355 | status = autofs4_mount_wait(dentry); | |
356 | if (status) | |
357 | return ERR_PTR(status); | |
358 | spin_lock(&sbi->fs_lock); | |
359 | goto done; | |
360 | } | |
361 | ||
362 | /* | |
363 | * If the dentry is a symlink it's equivalent to a directory | |
b5b80177 | 364 | * having d_mountpoint() true, so there's no need to call back |
10584211 IK |
365 | * to the daemon. |
366 | */ | |
367 | if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)) | |
368 | goto done; | |
b5b80177 IK |
369 | if (!d_mountpoint(dentry)) { |
370 | /* | |
371 | * It's possible that user space hasn't removed directories | |
372 | * after umounting a rootless multi-mount, although it | |
373 | * should. For v5 have_submounts() is sufficient to handle | |
374 | * this because the leaves of the directory tree under the | |
375 | * mount never trigger mounts themselves (they have an autofs | |
376 | * trigger mount mounted on them). But v4 pseudo direct mounts | |
377 | * do need the leaves to to trigger mounts. In this case we | |
378 | * have no choice but to use the list_empty() check and | |
379 | * require user space behave. | |
380 | */ | |
381 | if (sbi->version > 4) { | |
382 | if (have_submounts(dentry)) | |
383 | goto done; | |
384 | } else { | |
385 | spin_lock(&dentry->d_lock); | |
386 | if (!list_empty(&dentry->d_subdirs)) { | |
387 | spin_unlock(&dentry->d_lock); | |
388 | goto done; | |
389 | } | |
390 | spin_unlock(&dentry->d_lock); | |
391 | } | |
10584211 | 392 | ino->flags |= AUTOFS_INF_PENDING; |
10584211 IK |
393 | spin_unlock(&sbi->fs_lock); |
394 | status = autofs4_mount_wait(dentry); | |
395 | if (status) | |
396 | return ERR_PTR(status); | |
397 | spin_lock(&sbi->fs_lock); | |
398 | ino->flags &= ~AUTOFS_INF_PENDING; | |
10584211 | 399 | } |
10584211 | 400 | done: |
b5b80177 IK |
401 | if (!(ino->flags & AUTOFS_INF_EXPIRING)) { |
402 | /* | |
3c319985 IK |
403 | * Any needed mounting has been completed and the path |
404 | * updated so clear DCACHE_NEED_AUTOMOUNT so we don't | |
405 | * call ->d_automount() on rootless multi-mounts since | |
406 | * it can lead to an incorrect ELOOP error return. | |
407 | * | |
b5b80177 IK |
408 | * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and |
409 | * symlinks as in all other cases the dentry will be covered by | |
410 | * an actual mount so ->d_automount() won't be called during | |
411 | * the follow. | |
412 | */ | |
3c319985 | 413 | spin_lock(&dentry->d_lock); |
b5b80177 IK |
414 | if ((!d_mountpoint(dentry) && |
415 | !list_empty(&dentry->d_subdirs)) || | |
416 | (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))) | |
417 | __managed_dentry_clear_automount(dentry); | |
418 | spin_unlock(&dentry->d_lock); | |
419 | } | |
10584211 IK |
420 | spin_unlock(&sbi->fs_lock); |
421 | ||
422 | /* Mount succeeded, check if we ended up with a new dentry */ | |
423 | dentry = autofs4_mountpoint_changed(path); | |
424 | if (!dentry) | |
425 | return ERR_PTR(-ENOENT); | |
426 | ||
427 | return NULL; | |
428 | } | |
429 | ||
1aed3e42 | 430 | int autofs4_d_manage(struct dentry *dentry, bool rcu_walk) |
b5b80177 IK |
431 | { |
432 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
433 | ||
434 | DPRINTK("dentry=%p %.*s", | |
435 | dentry, dentry->d_name.len, dentry->d_name.name); | |
436 | ||
437 | /* The daemon never waits. */ | |
1aed3e42 | 438 | if (autofs4_oz_mode(sbi)) { |
83fb96bf IK |
439 | if (rcu_walk) |
440 | return 0; | |
b5b80177 IK |
441 | if (!d_mountpoint(dentry)) |
442 | return -EISDIR; | |
443 | return 0; | |
444 | } | |
445 | ||
ab90911f DH |
446 | /* We need to sleep, so we need pathwalk to be in ref-mode */ |
447 | if (rcu_walk) | |
448 | return -ECHILD; | |
449 | ||
b5b80177 IK |
450 | /* Wait for pending expires */ |
451 | do_expire_wait(dentry); | |
452 | ||
453 | /* | |
454 | * This dentry may be under construction so wait on mount | |
455 | * completion. | |
456 | */ | |
457 | return autofs4_mount_wait(dentry); | |
458 | } | |
459 | ||
1da177e4 | 460 | /* Lookups in the root directory */ |
00cd8dd3 | 461 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
1da177e4 LT |
462 | { |
463 | struct autofs_sb_info *sbi; | |
25767378 | 464 | struct autofs_info *ino; |
10584211 | 465 | struct dentry *active; |
1da177e4 | 466 | |
10584211 | 467 | DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name); |
1da177e4 | 468 | |
718c604a | 469 | /* File name too long to exist */ |
1da177e4 | 470 | if (dentry->d_name.len > NAME_MAX) |
718c604a | 471 | return ERR_PTR(-ENAMETOOLONG); |
1da177e4 LT |
472 | |
473 | sbi = autofs4_sbi(dir->i_sb); | |
718c604a | 474 | |
1da177e4 | 475 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
c0bcc9d5 IK |
476 | current->pid, task_pgrp_nr(current), sbi->catatonic, |
477 | autofs4_oz_mode(sbi)); | |
1da177e4 | 478 | |
6510c9d8 | 479 | active = autofs4_lookup_active(dentry); |
90387c9c | 480 | if (active) { |
10584211 | 481 | return active; |
aa952eb2 | 482 | } else { |
4b1ae27a | 483 | /* |
10584211 IK |
484 | * A dentry that is not within the root can never trigger a |
485 | * mount operation, unless the directory already exists, so we | |
486 | * can return fail immediately. The daemon however does need | |
487 | * to create directories within the file system. | |
4b1ae27a | 488 | */ |
10584211 IK |
489 | if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent)) |
490 | return ERR_PTR(-ENOENT); | |
491 | ||
492 | /* Mark entries in the root as mount triggers */ | |
b650c858 | 493 | if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) |
b5b80177 | 494 | __managed_dentry_set_managed(dentry); |
10584211 | 495 | |
26e6c910 | 496 | ino = autofs4_new_ino(sbi); |
4b1ae27a AV |
497 | if (!ino) |
498 | return ERR_PTR(-ENOMEM); | |
499 | ||
500 | dentry->d_fsdata = ino; | |
501 | ino->dentry = dentry; | |
5f6f4f28 | 502 | |
4b1ae27a AV |
503 | autofs4_add_active(dentry); |
504 | ||
505 | d_instantiate(dentry, NULL); | |
506 | } | |
1da177e4 LT |
507 | return NULL; |
508 | } | |
509 | ||
510 | static int autofs4_dir_symlink(struct inode *dir, | |
511 | struct dentry *dentry, | |
512 | const char *symname) | |
513 | { | |
514 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
515 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 516 | struct autofs_info *p_ino; |
1da177e4 | 517 | struct inode *inode; |
0bf71d4d | 518 | size_t size = strlen(symname); |
1da177e4 LT |
519 | char *cp; |
520 | ||
521 | DPRINTK("%s <- %.*s", symname, | |
522 | dentry->d_name.len, dentry->d_name.name); | |
523 | ||
524 | if (!autofs4_oz_mode(sbi)) | |
525 | return -EACCES; | |
526 | ||
5a37db30 AV |
527 | BUG_ON(!ino); |
528 | ||
26e6c910 | 529 | autofs4_clean_ino(ino); |
1da177e4 | 530 | |
4b1ae27a AV |
531 | autofs4_del_active(dentry); |
532 | ||
0bf71d4d | 533 | cp = kmalloc(size + 1, GFP_KERNEL); |
5a37db30 | 534 | if (!cp) |
25767378 | 535 | return -ENOMEM; |
1da177e4 LT |
536 | |
537 | strcpy(cp, symname); | |
538 | ||
726a5e06 | 539 | inode = autofs4_get_inode(dir->i_sb, S_IFLNK | 0555); |
25767378 IK |
540 | if (!inode) { |
541 | kfree(cp); | |
542 | if (!dentry->d_fsdata) | |
543 | kfree(ino); | |
544 | return -ENOMEM; | |
545 | } | |
292c5ee8 | 546 | inode->i_private = cp; |
0bf71d4d | 547 | inode->i_size = size; |
1864f7bd | 548 | d_add(dentry, inode); |
1da177e4 | 549 | |
5a37db30 | 550 | dget(dentry); |
1aff3c8b IK |
551 | atomic_inc(&ino->count); |
552 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
553 | if (p_ino && dentry->d_parent != dentry) | |
554 | atomic_inc(&p_ino->count); | |
1da177e4 LT |
555 | |
556 | dir->i_mtime = CURRENT_TIME; | |
557 | ||
558 | return 0; | |
559 | } | |
560 | ||
561 | /* | |
562 | * NOTE! | |
563 | * | |
564 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache | |
565 | * that the file no longer exists. However, doing that means that the | |
566 | * VFS layer can turn the dentry into a negative dentry. We don't want | |
f50b6f86 | 567 | * this, because the unlink is probably the result of an expire. |
5f6f4f28 IK |
568 | * We simply d_drop it and add it to a expiring list in the super block, |
569 | * which allows the dentry lookup to check for an incomplete expire. | |
1da177e4 LT |
570 | * |
571 | * If a process is blocked on the dentry waiting for the expire to finish, | |
572 | * it will invalidate the dentry and try to mount with a new one. | |
573 | * | |
574 | * Also see autofs4_dir_rmdir().. | |
575 | */ | |
576 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) | |
577 | { | |
578 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
579 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 580 | struct autofs_info *p_ino; |
1da177e4 LT |
581 | |
582 | /* This allows root to remove symlinks */ | |
d78e53c8 | 583 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
1da177e4 LT |
584 | return -EACCES; |
585 | ||
1aff3c8b IK |
586 | if (atomic_dec_and_test(&ino->count)) { |
587 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
588 | if (p_ino && dentry->d_parent != dentry) | |
589 | atomic_dec(&p_ino->count); | |
590 | } | |
1da177e4 LT |
591 | dput(ino->dentry); |
592 | ||
593 | dentry->d_inode->i_size = 0; | |
ce71ec36 | 594 | clear_nlink(dentry->d_inode); |
1da177e4 LT |
595 | |
596 | dir->i_mtime = CURRENT_TIME; | |
597 | ||
e7854723 IK |
598 | spin_lock(&sbi->lookup_lock); |
599 | __autofs4_add_expiring(dentry); | |
f50b6f86 IK |
600 | spin_lock(&dentry->d_lock); |
601 | __d_drop(dentry); | |
602 | spin_unlock(&dentry->d_lock); | |
e7854723 | 603 | spin_unlock(&sbi->lookup_lock); |
1da177e4 LT |
604 | |
605 | return 0; | |
606 | } | |
607 | ||
dd89f90d IK |
608 | /* |
609 | * Version 4 of autofs provides a pseudo direct mount implementation | |
610 | * that relies on directories at the leaves of a directory tree under | |
611 | * an indirect mount to trigger mounts. To allow for this we need to | |
612 | * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves | |
613 | * of the directory tree. There is no need to clear the automount flag | |
614 | * following a mount or restore it after an expire because these mounts | |
25985edc | 615 | * are always covered. However, it is necessary to ensure that these |
dd89f90d IK |
616 | * flags are clear on non-empty directories to avoid unnecessary calls |
617 | * during path walks. | |
618 | */ | |
619 | static void autofs_set_leaf_automount_flags(struct dentry *dentry) | |
620 | { | |
621 | struct dentry *parent; | |
622 | ||
623 | /* root and dentrys in the root are already handled */ | |
624 | if (IS_ROOT(dentry->d_parent)) | |
625 | return; | |
626 | ||
627 | managed_dentry_set_managed(dentry); | |
628 | ||
629 | parent = dentry->d_parent; | |
630 | /* only consider parents below dentrys in the root */ | |
631 | if (IS_ROOT(parent->d_parent)) | |
632 | return; | |
633 | managed_dentry_clear_managed(parent); | |
634 | return; | |
635 | } | |
636 | ||
637 | static void autofs_clear_leaf_automount_flags(struct dentry *dentry) | |
638 | { | |
639 | struct list_head *d_child; | |
640 | struct dentry *parent; | |
641 | ||
642 | /* flags for dentrys in the root are handled elsewhere */ | |
643 | if (IS_ROOT(dentry->d_parent)) | |
644 | return; | |
645 | ||
646 | managed_dentry_clear_managed(dentry); | |
647 | ||
648 | parent = dentry->d_parent; | |
649 | /* only consider parents below dentrys in the root */ | |
650 | if (IS_ROOT(parent->d_parent)) | |
651 | return; | |
652 | d_child = &dentry->d_u.d_child; | |
653 | /* Set parent managed if it's becoming empty */ | |
654 | if (d_child->next == &parent->d_subdirs && | |
655 | d_child->prev == &parent->d_subdirs) | |
656 | managed_dentry_set_managed(parent); | |
657 | return; | |
658 | } | |
659 | ||
1da177e4 LT |
660 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) |
661 | { | |
662 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
663 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 664 | struct autofs_info *p_ino; |
1da177e4 | 665 | |
f50b6f86 IK |
666 | DPRINTK("dentry %p, removing %.*s", |
667 | dentry, dentry->d_name.len, dentry->d_name.name); | |
668 | ||
1da177e4 LT |
669 | if (!autofs4_oz_mode(sbi)) |
670 | return -EACCES; | |
671 | ||
2fd6b7f5 NP |
672 | spin_lock(&sbi->lookup_lock); |
673 | spin_lock(&dentry->d_lock); | |
1da177e4 | 674 | if (!list_empty(&dentry->d_subdirs)) { |
2fd6b7f5 NP |
675 | spin_unlock(&dentry->d_lock); |
676 | spin_unlock(&sbi->lookup_lock); | |
1da177e4 LT |
677 | return -ENOTEMPTY; |
678 | } | |
2fd6b7f5 | 679 | __autofs4_add_expiring(dentry); |
1da177e4 LT |
680 | __d_drop(dentry); |
681 | spin_unlock(&dentry->d_lock); | |
e7854723 | 682 | spin_unlock(&sbi->lookup_lock); |
1da177e4 | 683 | |
dd89f90d IK |
684 | if (sbi->version < 5) |
685 | autofs_clear_leaf_automount_flags(dentry); | |
686 | ||
1aff3c8b IK |
687 | if (atomic_dec_and_test(&ino->count)) { |
688 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
689 | if (p_ino && dentry->d_parent != dentry) | |
690 | atomic_dec(&p_ino->count); | |
691 | } | |
1da177e4 | 692 | dput(ino->dentry); |
1da177e4 | 693 | dentry->d_inode->i_size = 0; |
ce71ec36 | 694 | clear_nlink(dentry->d_inode); |
1da177e4 LT |
695 | |
696 | if (dir->i_nlink) | |
9a53c3a7 | 697 | drop_nlink(dir); |
1da177e4 LT |
698 | |
699 | return 0; | |
700 | } | |
701 | ||
18bb1db3 | 702 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
703 | { |
704 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
705 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 706 | struct autofs_info *p_ino; |
1da177e4 LT |
707 | struct inode *inode; |
708 | ||
d78e53c8 | 709 | if (!autofs4_oz_mode(sbi)) |
1da177e4 LT |
710 | return -EACCES; |
711 | ||
712 | DPRINTK("dentry %p, creating %.*s", | |
713 | dentry, dentry->d_name.len, dentry->d_name.name); | |
714 | ||
5a37db30 AV |
715 | BUG_ON(!ino); |
716 | ||
26e6c910 | 717 | autofs4_clean_ino(ino); |
25767378 | 718 | |
4b1ae27a AV |
719 | autofs4_del_active(dentry); |
720 | ||
726a5e06 | 721 | inode = autofs4_get_inode(dir->i_sb, S_IFDIR | 0555); |
5a37db30 | 722 | if (!inode) |
25767378 | 723 | return -ENOMEM; |
1864f7bd | 724 | d_add(dentry, inode); |
1da177e4 | 725 | |
dd89f90d IK |
726 | if (sbi->version < 5) |
727 | autofs_set_leaf_automount_flags(dentry); | |
728 | ||
5a37db30 | 729 | dget(dentry); |
1aff3c8b IK |
730 | atomic_inc(&ino->count); |
731 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
732 | if (p_ino && dentry->d_parent != dentry) | |
733 | atomic_inc(&p_ino->count); | |
d8c76e6f | 734 | inc_nlink(dir); |
1da177e4 LT |
735 | dir->i_mtime = CURRENT_TIME; |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | /* Get/set timeout ioctl() operation */ | |
c9243f5b AB |
741 | #ifdef CONFIG_COMPAT |
742 | static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi, | |
743 | compat_ulong_t __user *p) | |
744 | { | |
745 | int rv; | |
746 | unsigned long ntimeout; | |
747 | ||
748 | if ((rv = get_user(ntimeout, p)) || | |
749 | (rv = put_user(sbi->exp_timeout/HZ, p))) | |
750 | return rv; | |
751 | ||
752 | if (ntimeout > UINT_MAX/HZ) | |
753 | sbi->exp_timeout = 0; | |
754 | else | |
755 | sbi->exp_timeout = ntimeout * HZ; | |
756 | ||
757 | return 0; | |
758 | } | |
759 | #endif | |
760 | ||
1da177e4 LT |
761 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, |
762 | unsigned long __user *p) | |
763 | { | |
764 | int rv; | |
765 | unsigned long ntimeout; | |
766 | ||
d78e53c8 SB |
767 | if ((rv = get_user(ntimeout, p)) || |
768 | (rv = put_user(sbi->exp_timeout/HZ, p))) | |
1da177e4 LT |
769 | return rv; |
770 | ||
d78e53c8 | 771 | if (ntimeout > ULONG_MAX/HZ) |
1da177e4 LT |
772 | sbi->exp_timeout = 0; |
773 | else | |
774 | sbi->exp_timeout = ntimeout * HZ; | |
775 | ||
776 | return 0; | |
777 | } | |
778 | ||
779 | /* Return protocol version */ | |
780 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) | |
781 | { | |
782 | return put_user(sbi->version, p); | |
783 | } | |
784 | ||
785 | /* Return protocol sub version */ | |
786 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) | |
787 | { | |
788 | return put_user(sbi->sub_version, p); | |
789 | } | |
790 | ||
1da177e4 LT |
791 | /* |
792 | * Tells the daemon whether it can umount the autofs mount. | |
793 | */ | |
794 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) | |
795 | { | |
796 | int status = 0; | |
797 | ||
e3474a8e | 798 | if (may_umount(mnt)) |
1da177e4 LT |
799 | status = 1; |
800 | ||
801 | DPRINTK("returning %d", status); | |
802 | ||
803 | status = put_user(status, p); | |
804 | ||
805 | return status; | |
806 | } | |
807 | ||
808 | /* Identify autofs4_dentries - this is so we can tell if there's | |
809 | an extra dentry refcount or not. We only hold a refcount on the | |
810 | dentry if its non-negative (ie, d_inode != NULL) | |
811 | */ | |
812 | int is_autofs4_dentry(struct dentry *dentry) | |
813 | { | |
814 | return dentry && dentry->d_inode && | |
b650c858 | 815 | dentry->d_op == &autofs4_dentry_operations && |
1da177e4 LT |
816 | dentry->d_fsdata != NULL; |
817 | } | |
818 | ||
819 | /* | |
820 | * ioctl()'s on the root directory is the chief method for the daemon to | |
821 | * generate kernel reactions | |
822 | */ | |
3663df70 FW |
823 | static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp, |
824 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
825 | { |
826 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); | |
827 | void __user *p = (void __user *)arg; | |
828 | ||
829 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", | |
a47afb0f | 830 | cmd,arg,sbi,task_pgrp_nr(current)); |
1da177e4 | 831 | |
d78e53c8 SB |
832 | if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || |
833 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT) | |
1da177e4 LT |
834 | return -ENOTTY; |
835 | ||
d78e53c8 | 836 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
1da177e4 LT |
837 | return -EPERM; |
838 | ||
839 | switch(cmd) { | |
840 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ | |
841 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); | |
842 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ | |
843 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); | |
844 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ | |
845 | autofs4_catatonic_mode(sbi); | |
846 | return 0; | |
847 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ | |
848 | return autofs4_get_protover(sbi, p); | |
849 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ | |
850 | return autofs4_get_protosubver(sbi, p); | |
851 | case AUTOFS_IOC_SETTIMEOUT: | |
852 | return autofs4_get_set_timeout(sbi, p); | |
c9243f5b AB |
853 | #ifdef CONFIG_COMPAT |
854 | case AUTOFS_IOC_SETTIMEOUT32: | |
855 | return autofs4_compat_get_set_timeout(sbi, p); | |
856 | #endif | |
1da177e4 | 857 | |
1da177e4 | 858 | case AUTOFS_IOC_ASKUMOUNT: |
a4669ed8 | 859 | return autofs4_ask_umount(filp->f_path.mnt, p); |
1da177e4 LT |
860 | |
861 | /* return a single thing to expire */ | |
862 | case AUTOFS_IOC_EXPIRE: | |
a4669ed8 | 863 | return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p); |
1da177e4 LT |
864 | /* same as above, but can send multiple expires through pipe */ |
865 | case AUTOFS_IOC_EXPIRE_MULTI: | |
a4669ed8 | 866 | return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p); |
1da177e4 LT |
867 | |
868 | default: | |
869 | return -ENOSYS; | |
870 | } | |
871 | } | |
3663df70 FW |
872 | |
873 | static long autofs4_root_ioctl(struct file *filp, | |
874 | unsigned int cmd, unsigned long arg) | |
875 | { | |
3663df70 | 876 | struct inode *inode = filp->f_dentry->d_inode; |
de47de74 | 877 | return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg); |
3663df70 | 878 | } |
c9243f5b AB |
879 | |
880 | #ifdef CONFIG_COMPAT | |
881 | static long autofs4_root_compat_ioctl(struct file *filp, | |
882 | unsigned int cmd, unsigned long arg) | |
883 | { | |
884 | struct inode *inode = filp->f_path.dentry->d_inode; | |
885 | int ret; | |
886 | ||
c9243f5b AB |
887 | if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL) |
888 | ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg); | |
889 | else | |
890 | ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, | |
891 | (unsigned long)compat_ptr(arg)); | |
c9243f5b AB |
892 | |
893 | return ret; | |
894 | } | |
895 | #endif |