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