xref: /openbmc/linux/fs/autofs/root.c (revision 83268fa6)
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>
12 #include <linux/compat.h>
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);
278 	}
279 	ino->last_used = jiffies;
280 	return status;
281 }
282 
283 static int do_expire_wait(const struct path *path, bool rcu_walk)
284 {
285 	struct dentry *dentry = path->dentry;
286 	struct dentry *expiring;
287 
288 	expiring = autofs_lookup_expiring(dentry, rcu_walk);
289 	if (IS_ERR(expiring))
290 		return PTR_ERR(expiring);
291 	if (!expiring)
292 		return autofs_expire_wait(path, rcu_walk);
293 	else {
294 		const struct path this = { .mnt = path->mnt, .dentry = expiring };
295 		/*
296 		 * If we are racing with expire the request might not
297 		 * be quite complete, but the directory has been removed
298 		 * so it must have been successful, just wait for it.
299 		 */
300 		autofs_expire_wait(&this, 0);
301 		autofs_del_expiring(expiring);
302 		dput(expiring);
303 	}
304 	return 0;
305 }
306 
307 static struct dentry *autofs_mountpoint_changed(struct path *path)
308 {
309 	struct dentry *dentry = path->dentry;
310 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
311 
312 	/*
313 	 * If this is an indirect mount the dentry could have gone away
314 	 * as a result of an expire and a new one created.
315 	 */
316 	if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
317 		struct dentry *parent = dentry->d_parent;
318 		struct autofs_info *ino;
319 		struct dentry *new;
320 
321 		new = d_lookup(parent, &dentry->d_name);
322 		if (!new)
323 			return NULL;
324 		ino = autofs_dentry_ino(new);
325 		ino->last_used = jiffies;
326 		dput(path->dentry);
327 		path->dentry = new;
328 	}
329 	return path->dentry;
330 }
331 
332 static struct vfsmount *autofs_d_automount(struct path *path)
333 {
334 	struct dentry *dentry = path->dentry;
335 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
336 	struct autofs_info *ino = autofs_dentry_ino(dentry);
337 	int status;
338 
339 	pr_debug("dentry=%p %pd\n", dentry, dentry);
340 
341 	/* The daemon never triggers a mount. */
342 	if (autofs_oz_mode(sbi))
343 		return NULL;
344 
345 	/*
346 	 * If an expire request is pending everyone must wait.
347 	 * If the expire fails we're still mounted so continue
348 	 * the follow and return. A return of -EAGAIN (which only
349 	 * happens with indirect mounts) means the expire completed
350 	 * and the directory was removed, so just go ahead and try
351 	 * the mount.
352 	 */
353 	status = do_expire_wait(path, 0);
354 	if (status && status != -EAGAIN)
355 		return NULL;
356 
357 	/* Callback to the daemon to perform the mount or wait */
358 	spin_lock(&sbi->fs_lock);
359 	if (ino->flags & AUTOFS_INF_PENDING) {
360 		spin_unlock(&sbi->fs_lock);
361 		status = autofs_mount_wait(path, 0);
362 		if (status)
363 			return ERR_PTR(status);
364 		goto done;
365 	}
366 
367 	/*
368 	 * If the dentry is a symlink it's equivalent to a directory
369 	 * having path_is_mountpoint() true, so there's no need to call
370 	 * back to the daemon.
371 	 */
372 	if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
373 		spin_unlock(&sbi->fs_lock);
374 		goto done;
375 	}
376 
377 	if (!path_is_mountpoint(path)) {
378 		/*
379 		 * It's possible that user space hasn't removed directories
380 		 * after umounting a rootless multi-mount, although it
381 		 * should. For v5 path_has_submounts() is sufficient to
382 		 * handle this because the leaves of the directory tree under
383 		 * the mount never trigger mounts themselves (they have an
384 		 * autofs trigger mount mounted on them). But v4 pseudo direct
385 		 * mounts do need the leaves to trigger mounts. In this case
386 		 * we have no choice but to use the list_empty() check and
387 		 * require user space behave.
388 		 */
389 		if (sbi->version > 4) {
390 			if (path_has_submounts(path)) {
391 				spin_unlock(&sbi->fs_lock);
392 				goto done;
393 			}
394 		} else {
395 			if (!simple_empty(dentry)) {
396 				spin_unlock(&sbi->fs_lock);
397 				goto done;
398 			}
399 		}
400 		ino->flags |= AUTOFS_INF_PENDING;
401 		spin_unlock(&sbi->fs_lock);
402 		status = autofs_mount_wait(path, 0);
403 		spin_lock(&sbi->fs_lock);
404 		ino->flags &= ~AUTOFS_INF_PENDING;
405 		if (status) {
406 			spin_unlock(&sbi->fs_lock);
407 			return ERR_PTR(status);
408 		}
409 	}
410 	spin_unlock(&sbi->fs_lock);
411 done:
412 	/* Mount succeeded, check if we ended up with a new dentry */
413 	dentry = autofs_mountpoint_changed(path);
414 	if (!dentry)
415 		return ERR_PTR(-ENOENT);
416 
417 	return NULL;
418 }
419 
420 static int autofs_d_manage(const struct path *path, bool rcu_walk)
421 {
422 	struct dentry *dentry = path->dentry;
423 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
424 	struct autofs_info *ino = autofs_dentry_ino(dentry);
425 	int status;
426 
427 	pr_debug("dentry=%p %pd\n", dentry, dentry);
428 
429 	/* The daemon never waits. */
430 	if (autofs_oz_mode(sbi)) {
431 		if (!path_is_mountpoint(path))
432 			return -EISDIR;
433 		return 0;
434 	}
435 
436 	/* Wait for pending expires */
437 	if (do_expire_wait(path, rcu_walk) == -ECHILD)
438 		return -ECHILD;
439 
440 	/*
441 	 * This dentry may be under construction so wait on mount
442 	 * completion.
443 	 */
444 	status = autofs_mount_wait(path, rcu_walk);
445 	if (status)
446 		return status;
447 
448 	if (rcu_walk) {
449 		/* We don't need fs_lock in rcu_walk mode,
450 		 * just testing 'AUTOFS_INFO_NO_RCU' is enough.
451 		 * simple_empty() takes a spinlock, so leave it
452 		 * to last.
453 		 * We only return -EISDIR when certain this isn't
454 		 * a mount-trap.
455 		 */
456 		struct inode *inode;
457 
458 		if (ino->flags & AUTOFS_INF_WANT_EXPIRE)
459 			return 0;
460 		if (path_is_mountpoint(path))
461 			return 0;
462 		inode = d_inode_rcu(dentry);
463 		if (inode && S_ISLNK(inode->i_mode))
464 			return -EISDIR;
465 		if (list_empty(&dentry->d_subdirs))
466 			return 0;
467 		if (!simple_empty(dentry))
468 			return -EISDIR;
469 		return 0;
470 	}
471 
472 	spin_lock(&sbi->fs_lock);
473 	/*
474 	 * If the dentry has been selected for expire while we slept
475 	 * on the lock then it might go away. We'll deal with that in
476 	 * ->d_automount() and wait on a new mount if the expire
477 	 * succeeds or return here if it doesn't (since there's no
478 	 * mount to follow with a rootless multi-mount).
479 	 */
480 	if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
481 		/*
482 		 * Any needed mounting has been completed and the path
483 		 * updated so check if this is a rootless multi-mount so
484 		 * we can avoid needless calls ->d_automount() and avoid
485 		 * an incorrect ELOOP error return.
486 		 */
487 		if ((!path_is_mountpoint(path) && !simple_empty(dentry)) ||
488 		    (d_really_is_positive(dentry) && d_is_symlink(dentry)))
489 			status = -EISDIR;
490 	}
491 	spin_unlock(&sbi->fs_lock);
492 
493 	return status;
494 }
495 
496 /* Lookups in the root directory */
497 static struct dentry *autofs_lookup(struct inode *dir,
498 				    struct dentry *dentry, unsigned int flags)
499 {
500 	struct autofs_sb_info *sbi;
501 	struct autofs_info *ino;
502 	struct dentry *active;
503 
504 	pr_debug("name = %pd\n", dentry);
505 
506 	/* File name too long to exist */
507 	if (dentry->d_name.len > NAME_MAX)
508 		return ERR_PTR(-ENAMETOOLONG);
509 
510 	sbi = autofs_sbi(dir->i_sb);
511 
512 	pr_debug("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
513 		 current->pid, task_pgrp_nr(current), sbi->catatonic,
514 		 autofs_oz_mode(sbi));
515 
516 	active = autofs_lookup_active(dentry);
517 	if (active)
518 		return active;
519 	else {
520 		/*
521 		 * A dentry that is not within the root can never trigger a
522 		 * mount operation, unless the directory already exists, so we
523 		 * can return fail immediately.  The daemon however does need
524 		 * to create directories within the file system.
525 		 */
526 		if (!autofs_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
527 			return ERR_PTR(-ENOENT);
528 
529 		/* Mark entries in the root as mount triggers */
530 		if (IS_ROOT(dentry->d_parent) &&
531 		    autofs_type_indirect(sbi->type))
532 			__managed_dentry_set_managed(dentry);
533 
534 		ino = autofs_new_ino(sbi);
535 		if (!ino)
536 			return ERR_PTR(-ENOMEM);
537 
538 		dentry->d_fsdata = ino;
539 		ino->dentry = dentry;
540 
541 		autofs_add_active(dentry);
542 	}
543 	return NULL;
544 }
545 
546 static int autofs_dir_symlink(struct inode *dir,
547 			       struct dentry *dentry,
548 			       const char *symname)
549 {
550 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
551 	struct autofs_info *ino = autofs_dentry_ino(dentry);
552 	struct autofs_info *p_ino;
553 	struct inode *inode;
554 	size_t size = strlen(symname);
555 	char *cp;
556 
557 	pr_debug("%s <- %pd\n", symname, dentry);
558 
559 	if (!autofs_oz_mode(sbi))
560 		return -EACCES;
561 
562 	/* autofs_oz_mode() needs to allow path walks when the
563 	 * autofs mount is catatonic but the state of an autofs
564 	 * file system needs to be preserved over restarts.
565 	 */
566 	if (sbi->catatonic)
567 		return -EACCES;
568 
569 	BUG_ON(!ino);
570 
571 	autofs_clean_ino(ino);
572 
573 	autofs_del_active(dentry);
574 
575 	cp = kmalloc(size + 1, GFP_KERNEL);
576 	if (!cp)
577 		return -ENOMEM;
578 
579 	strcpy(cp, symname);
580 
581 	inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555);
582 	if (!inode) {
583 		kfree(cp);
584 		return -ENOMEM;
585 	}
586 	inode->i_private = cp;
587 	inode->i_size = size;
588 	d_add(dentry, inode);
589 
590 	dget(dentry);
591 	atomic_inc(&ino->count);
592 	p_ino = autofs_dentry_ino(dentry->d_parent);
593 	if (p_ino && !IS_ROOT(dentry))
594 		atomic_inc(&p_ino->count);
595 
596 	dir->i_mtime = current_time(dir);
597 
598 	return 0;
599 }
600 
601 /*
602  * NOTE!
603  *
604  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
605  * that the file no longer exists. However, doing that means that the
606  * VFS layer can turn the dentry into a negative dentry.  We don't want
607  * this, because the unlink is probably the result of an expire.
608  * We simply d_drop it and add it to a expiring list in the super block,
609  * which allows the dentry lookup to check for an incomplete expire.
610  *
611  * If a process is blocked on the dentry waiting for the expire to finish,
612  * it will invalidate the dentry and try to mount with a new one.
613  *
614  * Also see autofs_dir_rmdir()..
615  */
616 static int autofs_dir_unlink(struct inode *dir, struct dentry *dentry)
617 {
618 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
619 	struct autofs_info *ino = autofs_dentry_ino(dentry);
620 	struct autofs_info *p_ino;
621 
622 	if (!autofs_oz_mode(sbi))
623 		return -EACCES;
624 
625 	/* autofs_oz_mode() needs to allow path walks when the
626 	 * autofs mount is catatonic but the state of an autofs
627 	 * file system needs to be preserved over restarts.
628 	 */
629 	if (sbi->catatonic)
630 		return -EACCES;
631 
632 	if (atomic_dec_and_test(&ino->count)) {
633 		p_ino = autofs_dentry_ino(dentry->d_parent);
634 		if (p_ino && !IS_ROOT(dentry))
635 			atomic_dec(&p_ino->count);
636 	}
637 	dput(ino->dentry);
638 
639 	d_inode(dentry)->i_size = 0;
640 	clear_nlink(d_inode(dentry));
641 
642 	dir->i_mtime = current_time(dir);
643 
644 	spin_lock(&sbi->lookup_lock);
645 	__autofs_add_expiring(dentry);
646 	d_drop(dentry);
647 	spin_unlock(&sbi->lookup_lock);
648 
649 	return 0;
650 }
651 
652 /*
653  * Version 4 of autofs provides a pseudo direct mount implementation
654  * that relies on directories at the leaves of a directory tree under
655  * an indirect mount to trigger mounts. To allow for this we need to
656  * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
657  * of the directory tree. There is no need to clear the automount flag
658  * following a mount or restore it after an expire because these mounts
659  * are always covered. However, it is necessary to ensure that these
660  * flags are clear on non-empty directories to avoid unnecessary calls
661  * during path walks.
662  */
663 static void autofs_set_leaf_automount_flags(struct dentry *dentry)
664 {
665 	struct dentry *parent;
666 
667 	/* root and dentrys in the root are already handled */
668 	if (IS_ROOT(dentry->d_parent))
669 		return;
670 
671 	managed_dentry_set_managed(dentry);
672 
673 	parent = dentry->d_parent;
674 	/* only consider parents below dentrys in the root */
675 	if (IS_ROOT(parent->d_parent))
676 		return;
677 	managed_dentry_clear_managed(parent);
678 }
679 
680 static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
681 {
682 	struct list_head *d_child;
683 	struct dentry *parent;
684 
685 	/* flags for dentrys in the root are handled elsewhere */
686 	if (IS_ROOT(dentry->d_parent))
687 		return;
688 
689 	managed_dentry_clear_managed(dentry);
690 
691 	parent = dentry->d_parent;
692 	/* only consider parents below dentrys in the root */
693 	if (IS_ROOT(parent->d_parent))
694 		return;
695 	d_child = &dentry->d_child;
696 	/* Set parent managed if it's becoming empty */
697 	if (d_child->next == &parent->d_subdirs &&
698 	    d_child->prev == &parent->d_subdirs)
699 		managed_dentry_set_managed(parent);
700 }
701 
702 static int autofs_dir_rmdir(struct inode *dir, struct dentry *dentry)
703 {
704 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
705 	struct autofs_info *ino = autofs_dentry_ino(dentry);
706 	struct autofs_info *p_ino;
707 
708 	pr_debug("dentry %p, removing %pd\n", dentry, dentry);
709 
710 	if (!autofs_oz_mode(sbi))
711 		return -EACCES;
712 
713 	/* autofs_oz_mode() needs to allow path walks when the
714 	 * autofs mount is catatonic but the state of an autofs
715 	 * file system needs to be preserved over restarts.
716 	 */
717 	if (sbi->catatonic)
718 		return -EACCES;
719 
720 	spin_lock(&sbi->lookup_lock);
721 	if (!simple_empty(dentry)) {
722 		spin_unlock(&sbi->lookup_lock);
723 		return -ENOTEMPTY;
724 	}
725 	__autofs_add_expiring(dentry);
726 	d_drop(dentry);
727 	spin_unlock(&sbi->lookup_lock);
728 
729 	if (sbi->version < 5)
730 		autofs_clear_leaf_automount_flags(dentry);
731 
732 	if (atomic_dec_and_test(&ino->count)) {
733 		p_ino = autofs_dentry_ino(dentry->d_parent);
734 		if (p_ino && dentry->d_parent != dentry)
735 			atomic_dec(&p_ino->count);
736 	}
737 	dput(ino->dentry);
738 	d_inode(dentry)->i_size = 0;
739 	clear_nlink(d_inode(dentry));
740 
741 	if (dir->i_nlink)
742 		drop_nlink(dir);
743 
744 	return 0;
745 }
746 
747 static int autofs_dir_mkdir(struct inode *dir,
748 			    struct dentry *dentry, umode_t mode)
749 {
750 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
751 	struct autofs_info *ino = autofs_dentry_ino(dentry);
752 	struct autofs_info *p_ino;
753 	struct inode *inode;
754 
755 	if (!autofs_oz_mode(sbi))
756 		return -EACCES;
757 
758 	/* autofs_oz_mode() needs to allow path walks when the
759 	 * autofs mount is catatonic but the state of an autofs
760 	 * file system needs to be preserved over restarts.
761 	 */
762 	if (sbi->catatonic)
763 		return -EACCES;
764 
765 	pr_debug("dentry %p, creating %pd\n", dentry, dentry);
766 
767 	BUG_ON(!ino);
768 
769 	autofs_clean_ino(ino);
770 
771 	autofs_del_active(dentry);
772 
773 	inode = autofs_get_inode(dir->i_sb, S_IFDIR | mode);
774 	if (!inode)
775 		return -ENOMEM;
776 	d_add(dentry, inode);
777 
778 	if (sbi->version < 5)
779 		autofs_set_leaf_automount_flags(dentry);
780 
781 	dget(dentry);
782 	atomic_inc(&ino->count);
783 	p_ino = autofs_dentry_ino(dentry->d_parent);
784 	if (p_ino && !IS_ROOT(dentry))
785 		atomic_inc(&p_ino->count);
786 	inc_nlink(dir);
787 	dir->i_mtime = current_time(dir);
788 
789 	return 0;
790 }
791 
792 /* Get/set timeout ioctl() operation */
793 #ifdef CONFIG_COMPAT
794 static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
795 						 compat_ulong_t __user *p)
796 {
797 	unsigned long ntimeout;
798 	int rv;
799 
800 	rv = get_user(ntimeout, p);
801 	if (rv)
802 		goto error;
803 
804 	rv = put_user(sbi->exp_timeout/HZ, p);
805 	if (rv)
806 		goto error;
807 
808 	if (ntimeout > UINT_MAX/HZ)
809 		sbi->exp_timeout = 0;
810 	else
811 		sbi->exp_timeout = ntimeout * HZ;
812 
813 	return 0;
814 error:
815 	return rv;
816 }
817 #endif
818 
819 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
820 					  unsigned long __user *p)
821 {
822 	unsigned long ntimeout;
823 	int rv;
824 
825 	rv = get_user(ntimeout, p);
826 	if (rv)
827 		goto error;
828 
829 	rv = put_user(sbi->exp_timeout/HZ, p);
830 	if (rv)
831 		goto error;
832 
833 	if (ntimeout > ULONG_MAX/HZ)
834 		sbi->exp_timeout = 0;
835 	else
836 		sbi->exp_timeout = ntimeout * HZ;
837 
838 	return 0;
839 error:
840 	return rv;
841 }
842 
843 /* Return protocol version */
844 static inline int autofs_get_protover(struct autofs_sb_info *sbi,
845 				       int __user *p)
846 {
847 	return put_user(sbi->version, p);
848 }
849 
850 /* Return protocol sub version */
851 static inline int autofs_get_protosubver(struct autofs_sb_info *sbi,
852 					  int __user *p)
853 {
854 	return put_user(sbi->sub_version, p);
855 }
856 
857 /*
858 * Tells the daemon whether it can umount the autofs mount.
859 */
860 static inline int autofs_ask_umount(struct vfsmount *mnt, int __user *p)
861 {
862 	int status = 0;
863 
864 	if (may_umount(mnt))
865 		status = 1;
866 
867 	pr_debug("may umount %d\n", status);
868 
869 	status = put_user(status, p);
870 
871 	return status;
872 }
873 
874 /* Identify autofs_dentries - this is so we can tell if there's
875  * an extra dentry refcount or not.  We only hold a refcount on the
876  * dentry if its non-negative (ie, d_inode != NULL)
877  */
878 int is_autofs_dentry(struct dentry *dentry)
879 {
880 	return dentry && d_really_is_positive(dentry) &&
881 		dentry->d_op == &autofs_dentry_operations &&
882 		dentry->d_fsdata != NULL;
883 }
884 
885 /*
886  * ioctl()'s on the root directory is the chief method for the daemon to
887  * generate kernel reactions
888  */
889 static int autofs_root_ioctl_unlocked(struct inode *inode, struct file *filp,
890 				       unsigned int cmd, unsigned long arg)
891 {
892 	struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
893 	void __user *p = (void __user *)arg;
894 
895 	pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
896 		 cmd, arg, sbi, task_pgrp_nr(current));
897 
898 	if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
899 	     _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
900 		return -ENOTTY;
901 
902 	if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
903 		return -EPERM;
904 
905 	switch (cmd) {
906 	case AUTOFS_IOC_READY:	/* Wait queue: go ahead and retry */
907 		return autofs_wait_release(sbi, (autofs_wqt_t) arg, 0);
908 	case AUTOFS_IOC_FAIL:	/* Wait queue: fail with ENOENT */
909 		return autofs_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
910 	case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
911 		autofs_catatonic_mode(sbi);
912 		return 0;
913 	case AUTOFS_IOC_PROTOVER: /* Get protocol version */
914 		return autofs_get_protover(sbi, p);
915 	case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
916 		return autofs_get_protosubver(sbi, p);
917 	case AUTOFS_IOC_SETTIMEOUT:
918 		return autofs_get_set_timeout(sbi, p);
919 #ifdef CONFIG_COMPAT
920 	case AUTOFS_IOC_SETTIMEOUT32:
921 		return autofs_compat_get_set_timeout(sbi, p);
922 #endif
923 
924 	case AUTOFS_IOC_ASKUMOUNT:
925 		return autofs_ask_umount(filp->f_path.mnt, p);
926 
927 	/* return a single thing to expire */
928 	case AUTOFS_IOC_EXPIRE:
929 		return autofs_expire_run(inode->i_sb, filp->f_path.mnt, sbi, p);
930 	/* same as above, but can send multiple expires through pipe */
931 	case AUTOFS_IOC_EXPIRE_MULTI:
932 		return autofs_expire_multi(inode->i_sb,
933 					   filp->f_path.mnt, sbi, p);
934 
935 	default:
936 		return -EINVAL;
937 	}
938 }
939 
940 static long autofs_root_ioctl(struct file *filp,
941 			       unsigned int cmd, unsigned long arg)
942 {
943 	struct inode *inode = file_inode(filp);
944 
945 	return autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
946 }
947 
948 #ifdef CONFIG_COMPAT
949 static long autofs_root_compat_ioctl(struct file *filp,
950 				      unsigned int cmd, unsigned long arg)
951 {
952 	struct inode *inode = file_inode(filp);
953 	int ret;
954 
955 	if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
956 		ret = autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
957 	else
958 		ret = autofs_root_ioctl_unlocked(inode, filp, cmd,
959 					      (unsigned long) compat_ptr(arg));
960 
961 	return ret;
962 }
963 #endif
964