1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
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.
13 * ------------------------------------------------------------------------- */
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/slab.h>
19 #include <linux/param.h>
20 #include <linux/time.h>
21 #include <linux/compat.h>
22 #include <linux/mutex.h>
26 DEFINE_SPINLOCK(autofs4_lock);
28 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
29 static int autofs4_dir_unlink(struct inode *,struct dentry *);
30 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
31 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
32 static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
34 static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
36 static int autofs4_dir_open(struct inode *inode, struct file *file);
37 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
39 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
40 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
42 const struct file_operations autofs4_root_operations = {
43 .open = dcache_dir_open,
44 .release = dcache_dir_close,
45 .read = generic_read_dir,
46 .readdir = dcache_readdir,
47 .llseek = dcache_dir_lseek,
48 .unlocked_ioctl = autofs4_root_ioctl,
50 .compat_ioctl = autofs4_root_compat_ioctl,
54 const struct file_operations autofs4_dir_operations = {
55 .open = autofs4_dir_open,
56 .release = dcache_dir_close,
57 .read = generic_read_dir,
58 .readdir = dcache_readdir,
59 .llseek = dcache_dir_lseek,
62 const struct inode_operations autofs4_indirect_root_inode_operations = {
63 .lookup = autofs4_lookup,
64 .unlink = autofs4_dir_unlink,
65 .symlink = autofs4_dir_symlink,
66 .mkdir = autofs4_dir_mkdir,
67 .rmdir = autofs4_dir_rmdir,
70 const struct inode_operations autofs4_direct_root_inode_operations = {
71 .lookup = autofs4_lookup,
72 .unlink = autofs4_dir_unlink,
73 .mkdir = autofs4_dir_mkdir,
74 .rmdir = autofs4_dir_rmdir,
77 const struct inode_operations autofs4_dir_inode_operations = {
78 .lookup = autofs4_lookup,
79 .unlink = autofs4_dir_unlink,
80 .symlink = autofs4_dir_symlink,
81 .mkdir = autofs4_dir_mkdir,
82 .rmdir = autofs4_dir_rmdir,
85 static void autofs4_add_active(struct dentry *dentry)
87 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
88 struct autofs_info *ino = autofs4_dentry_ino(dentry);
90 spin_lock(&sbi->lookup_lock);
91 if (!ino->active_count) {
92 if (list_empty(&ino->active))
93 list_add(&ino->active, &sbi->active_list);
96 spin_unlock(&sbi->lookup_lock);
101 static void autofs4_del_active(struct dentry *dentry)
103 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
104 struct autofs_info *ino = autofs4_dentry_ino(dentry);
106 spin_lock(&sbi->lookup_lock);
108 if (!ino->active_count) {
109 if (!list_empty(&ino->active))
110 list_del_init(&ino->active);
112 spin_unlock(&sbi->lookup_lock);
117 static unsigned int autofs4_need_mount(unsigned int flags)
119 unsigned int res = 0;
120 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
125 static int autofs4_dir_open(struct inode *inode, struct file *file)
127 struct dentry *dentry = file->f_path.dentry;
128 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
130 DPRINTK("file=%p dentry=%p %.*s",
131 file, dentry, dentry->d_name.len, dentry->d_name.name);
133 if (autofs4_oz_mode(sbi))
137 * An empty directory in an autofs file system is always a
138 * mount point. The daemon must have failed to mount this
139 * during lookup so it doesn't exist. This can happen, for
140 * example, if user space returns an incorrect status for a
141 * mount request. Otherwise we're doing a readdir on the
142 * autofs file system so just let the libfs routines handle
145 spin_lock(&autofs4_lock);
146 spin_lock(&dentry->d_lock);
147 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
148 spin_unlock(&dentry->d_lock);
149 spin_unlock(&autofs4_lock);
152 spin_unlock(&dentry->d_lock);
153 spin_unlock(&autofs4_lock);
156 return dcache_dir_open(inode, file);
159 static int try_to_fill_dentry(struct dentry *dentry, int flags)
161 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
162 struct autofs_info *ino = autofs4_dentry_ino(dentry);
165 DPRINTK("dentry=%p %.*s ino=%p",
166 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
169 * Wait for a pending mount, triggering one if there
172 if (dentry->d_inode == NULL) {
173 DPRINTK("waiting for mount name=%.*s",
174 dentry->d_name.len, dentry->d_name.name);
176 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
178 DPRINTK("mount done status=%d", status);
180 /* Turn this into a real negative dentry? */
181 if (status == -ENOENT) {
182 spin_lock(&sbi->fs_lock);
183 ino->flags &= ~AUTOFS_INF_PENDING;
184 spin_unlock(&sbi->fs_lock);
187 /* Return a negative dentry, but leave it "pending" */
190 /* Trigger mount for path component or follow link */
191 } else if (ino->flags & AUTOFS_INF_PENDING ||
192 autofs4_need_mount(flags)) {
193 DPRINTK("waiting for mount name=%.*s",
194 dentry->d_name.len, dentry->d_name.name);
196 spin_lock(&sbi->fs_lock);
197 ino->flags |= AUTOFS_INF_PENDING;
198 spin_unlock(&sbi->fs_lock);
199 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
201 DPRINTK("mount done status=%d", status);
204 spin_lock(&sbi->fs_lock);
205 ino->flags &= ~AUTOFS_INF_PENDING;
206 spin_unlock(&sbi->fs_lock);
211 /* Initialize expiry counter after successful mount */
212 ino->last_used = jiffies;
214 spin_lock(&sbi->fs_lock);
215 ino->flags &= ~AUTOFS_INF_PENDING;
216 spin_unlock(&sbi->fs_lock);
221 /* For autofs direct mounts the follow link triggers the mount */
222 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
224 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
225 struct autofs_info *ino = autofs4_dentry_ino(dentry);
226 int oz_mode = autofs4_oz_mode(sbi);
227 unsigned int lookup_type;
230 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
231 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
234 * For an expire of a covered direct or offset mount we need
235 * to break out of follow_down_one() at the autofs mount trigger
236 * (d_mounted--), so we can see the expiring flag, and manage
237 * the blocking and following here until the expire is completed.
240 spin_lock(&sbi->fs_lock);
241 if (ino->flags & AUTOFS_INF_EXPIRING) {
242 spin_unlock(&sbi->fs_lock);
243 /* Follow down to our covering mount. */
244 if (!follow_down_one(&nd->path))
248 spin_unlock(&sbi->fs_lock);
252 /* If an expire request is pending everyone must wait. */
253 autofs4_expire_wait(dentry);
255 /* We trigger a mount for almost all flags */
256 lookup_type = autofs4_need_mount(nd->flags);
257 spin_lock(&sbi->fs_lock);
258 spin_lock(&autofs4_lock);
259 spin_lock(&dentry->d_lock);
260 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
261 spin_unlock(&dentry->d_lock);
262 spin_unlock(&autofs4_lock);
263 spin_unlock(&sbi->fs_lock);
268 * If the dentry contains directories then it is an autofs
269 * multi-mount with no root mount offset. So don't try to
272 if (ino->flags & AUTOFS_INF_PENDING ||
273 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
274 spin_unlock(&dentry->d_lock);
275 spin_unlock(&autofs4_lock);
276 spin_unlock(&sbi->fs_lock);
278 status = try_to_fill_dentry(dentry, nd->flags);
284 spin_unlock(&dentry->d_lock);
285 spin_unlock(&autofs4_lock);
286 spin_unlock(&sbi->fs_lock);
289 * If there is no root mount it must be an autofs
290 * multi-mount with no root offset so we don't need
293 if (d_managed(dentry)) {
294 status = follow_down(&nd->path, false);
304 return ERR_PTR(status);
308 * Revalidate is called on every cache lookup. Some of those
309 * cache lookups may actually happen while the dentry is not
310 * yet completely filled in, and revalidate has to delay such
313 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
316 struct autofs_sb_info *sbi;
318 int flags = nd ? nd->flags : 0;
321 if (flags & LOOKUP_RCU)
324 dir = dentry->d_parent->d_inode;
325 sbi = autofs4_sbi(dir->i_sb);
326 oz_mode = autofs4_oz_mode(sbi);
329 spin_lock(&sbi->fs_lock);
330 if (autofs4_ispending(dentry)) {
331 /* The daemon never causes a mount to trigger */
332 spin_unlock(&sbi->fs_lock);
338 * If the directory has gone away due to an expire
339 * we have been called as ->d_revalidate() and so
340 * we need to return false and proceed to ->lookup().
342 if (autofs4_expire_wait(dentry) == -EAGAIN)
346 * A zero status is success otherwise we have a
347 * negative error code.
349 status = try_to_fill_dentry(dentry, flags);
355 spin_unlock(&sbi->fs_lock);
357 /* Negative dentry.. invalidate if "old" */
358 if (dentry->d_inode == NULL)
361 /* Check for a non-mountpoint directory with no contents */
362 spin_lock(&autofs4_lock);
363 spin_lock(&dentry->d_lock);
364 if (S_ISDIR(dentry->d_inode->i_mode) &&
365 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
366 DPRINTK("dentry=%p %.*s, emptydir",
367 dentry, dentry->d_name.len, dentry->d_name.name);
368 spin_unlock(&dentry->d_lock);
369 spin_unlock(&autofs4_lock);
371 /* The daemon never causes a mount to trigger */
376 * A zero status is success otherwise we have a
377 * negative error code.
379 status = try_to_fill_dentry(dentry, flags);
385 spin_unlock(&dentry->d_lock);
386 spin_unlock(&autofs4_lock);
391 void autofs4_dentry_release(struct dentry *de)
393 struct autofs_info *inf;
395 DPRINTK("releasing %p", de);
397 inf = autofs4_dentry_ino(de);
401 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
404 spin_lock(&sbi->lookup_lock);
405 if (!list_empty(&inf->active))
406 list_del(&inf->active);
407 if (!list_empty(&inf->expiring))
408 list_del(&inf->expiring);
409 spin_unlock(&sbi->lookup_lock);
415 autofs4_free_ino(inf);
419 /* For dentries of directories in the root dir */
420 static const struct dentry_operations autofs4_root_dentry_operations = {
421 .d_release = autofs4_dentry_release,
424 /* For other dentries */
425 static const struct dentry_operations autofs4_dentry_operations = {
426 .d_automount = autofs4_d_automount,
427 .d_manage = autofs4_d_manage,
428 .d_release = autofs4_dentry_release,
431 static struct dentry *autofs4_lookup_active(struct dentry *dentry)
433 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
434 struct dentry *parent = dentry->d_parent;
435 struct qstr *name = &dentry->d_name;
436 unsigned int len = name->len;
437 unsigned int hash = name->hash;
438 const unsigned char *str = name->name;
439 struct list_head *p, *head;
441 spin_lock(&autofs4_lock);
442 spin_lock(&sbi->lookup_lock);
443 head = &sbi->active_list;
444 list_for_each(p, head) {
445 struct autofs_info *ino;
446 struct dentry *active;
449 ino = list_entry(p, struct autofs_info, active);
450 active = ino->dentry;
452 spin_lock(&active->d_lock);
455 if (active->d_count == 0)
458 qstr = &active->d_name;
460 if (active->d_name.hash != hash)
462 if (active->d_parent != parent)
465 if (qstr->len != len)
467 if (memcmp(qstr->name, str, len))
470 if (d_unhashed(active)) {
472 spin_unlock(&active->d_lock);
473 spin_unlock(&sbi->lookup_lock);
474 spin_unlock(&autofs4_lock);
478 spin_unlock(&active->d_lock);
480 spin_unlock(&sbi->lookup_lock);
481 spin_unlock(&autofs4_lock);
486 static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
488 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
489 struct dentry *parent = dentry->d_parent;
490 struct qstr *name = &dentry->d_name;
491 unsigned int len = name->len;
492 unsigned int hash = name->hash;
493 const unsigned char *str = name->name;
494 struct list_head *p, *head;
496 spin_lock(&autofs4_lock);
497 spin_lock(&sbi->lookup_lock);
498 head = &sbi->expiring_list;
499 list_for_each(p, head) {
500 struct autofs_info *ino;
501 struct dentry *expiring;
504 ino = list_entry(p, struct autofs_info, expiring);
505 expiring = ino->dentry;
507 spin_lock(&expiring->d_lock);
509 /* Bad luck, we've already been dentry_iput */
510 if (!expiring->d_inode)
513 qstr = &expiring->d_name;
515 if (expiring->d_name.hash != hash)
517 if (expiring->d_parent != parent)
520 if (qstr->len != len)
522 if (memcmp(qstr->name, str, len))
525 if (d_unhashed(expiring)) {
526 dget_dlock(expiring);
527 spin_unlock(&expiring->d_lock);
528 spin_unlock(&sbi->lookup_lock);
529 spin_unlock(&autofs4_lock);
533 spin_unlock(&expiring->d_lock);
535 spin_unlock(&sbi->lookup_lock);
536 spin_unlock(&autofs4_lock);
541 static int autofs4_mount_wait(struct dentry *dentry)
543 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
544 struct autofs_info *ino = autofs4_dentry_ino(dentry);
547 if (ino->flags & AUTOFS_INF_PENDING) {
548 DPRINTK("waiting for mount name=%.*s",
549 dentry->d_name.len, dentry->d_name.name);
550 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
551 DPRINTK("mount wait done status=%d", status);
552 ino->last_used = jiffies;
558 static int do_expire_wait(struct dentry *dentry)
560 struct dentry *expiring;
562 expiring = autofs4_lookup_expiring(dentry);
564 return autofs4_expire_wait(dentry);
567 * If we are racing with expire the request might not
568 * be quite complete, but the directory has been removed
569 * so it must have been successful, just wait for it.
571 autofs4_expire_wait(expiring);
572 autofs4_del_expiring(expiring);
578 static struct dentry *autofs4_mountpoint_changed(struct path *path)
580 struct dentry *dentry = path->dentry;
581 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
584 * If this is an indirect mount the dentry could have gone away
585 * as a result of an expire and a new one created.
587 if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
588 struct dentry *parent = dentry->d_parent;
589 struct dentry *new = d_lookup(parent, &dentry->d_name);
598 struct vfsmount *autofs4_d_automount(struct path *path)
600 struct dentry *dentry = path->dentry;
601 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
602 struct autofs_info *ino = autofs4_dentry_ino(dentry);
605 DPRINTK("dentry=%p %.*s",
606 dentry, dentry->d_name.len, dentry->d_name.name);
609 * Someone may have manually umounted this or it was a submount
610 * that has gone away.
612 spin_lock(&dentry->d_lock);
613 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
614 if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
615 (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
616 __managed_dentry_set_transit(path->dentry);
618 spin_unlock(&dentry->d_lock);
620 /* The daemon never triggers a mount. */
621 if (autofs4_oz_mode(sbi))
625 * If an expire request is pending everyone must wait.
626 * If the expire fails we're still mounted so continue
627 * the follow and return. A return of -EAGAIN (which only
628 * happens with indirect mounts) means the expire completed
629 * and the directory was removed, so just go ahead and try
632 status = do_expire_wait(dentry);
633 if (status && status != -EAGAIN)
636 /* Callback to the daemon to perform the mount or wait */
637 spin_lock(&sbi->fs_lock);
638 if (ino->flags & AUTOFS_INF_PENDING) {
639 spin_unlock(&sbi->fs_lock);
640 status = autofs4_mount_wait(dentry);
642 return ERR_PTR(status);
643 spin_lock(&sbi->fs_lock);
648 * If the dentry is a symlink it's equivalent to a directory
649 * having d_mountpoint() true, so there's no need to call back
652 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
654 if (!d_mountpoint(dentry)) {
656 * It's possible that user space hasn't removed directories
657 * after umounting a rootless multi-mount, although it
658 * should. For v5 have_submounts() is sufficient to handle
659 * this because the leaves of the directory tree under the
660 * mount never trigger mounts themselves (they have an autofs
661 * trigger mount mounted on them). But v4 pseudo direct mounts
662 * do need the leaves to to trigger mounts. In this case we
663 * have no choice but to use the list_empty() check and
664 * require user space behave.
666 if (sbi->version > 4) {
667 if (have_submounts(dentry))
670 spin_lock(&dentry->d_lock);
671 if (!list_empty(&dentry->d_subdirs)) {
672 spin_unlock(&dentry->d_lock);
675 spin_unlock(&dentry->d_lock);
677 ino->flags |= AUTOFS_INF_PENDING;
678 spin_unlock(&sbi->fs_lock);
679 status = autofs4_mount_wait(dentry);
681 return ERR_PTR(status);
682 spin_lock(&sbi->fs_lock);
683 ino->flags &= ~AUTOFS_INF_PENDING;
686 if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
688 * Any needed mounting has been completed and the path updated
689 * so turn this into a normal dentry so we don't continually
690 * call ->d_automount() and ->d_manage().
692 spin_lock(&dentry->d_lock);
693 __managed_dentry_clear_transit(dentry);
695 * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
696 * symlinks as in all other cases the dentry will be covered by
697 * an actual mount so ->d_automount() won't be called during
700 if ((!d_mountpoint(dentry) &&
701 !list_empty(&dentry->d_subdirs)) ||
702 (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
703 __managed_dentry_clear_automount(dentry);
704 spin_unlock(&dentry->d_lock);
706 spin_unlock(&sbi->fs_lock);
708 /* Mount succeeded, check if we ended up with a new dentry */
709 dentry = autofs4_mountpoint_changed(path);
711 return ERR_PTR(-ENOENT);
716 int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
718 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
720 DPRINTK("dentry=%p %.*s",
721 dentry, dentry->d_name.len, dentry->d_name.name);
723 /* The daemon never waits. */
724 if (autofs4_oz_mode(sbi) || mounting_here) {
725 if (!d_mountpoint(dentry))
730 /* Wait for pending expires */
731 do_expire_wait(dentry);
734 * This dentry may be under construction so wait on mount
737 return autofs4_mount_wait(dentry);
740 /* Lookups in the root directory */
741 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
743 struct autofs_sb_info *sbi;
744 struct autofs_info *ino;
745 struct dentry *active;
747 DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
749 /* File name too long to exist */
750 if (dentry->d_name.len > NAME_MAX)
751 return ERR_PTR(-ENAMETOOLONG);
753 sbi = autofs4_sbi(dir->i_sb);
755 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
756 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
758 active = autofs4_lookup_active(dentry);
762 d_set_d_op(dentry, &autofs4_root_dentry_operations);
765 * A dentry that is not within the root can never trigger a
766 * mount operation, unless the directory already exists, so we
767 * can return fail immediately. The daemon however does need
768 * to create directories within the file system.
770 if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
771 return ERR_PTR(-ENOENT);
773 /* Mark entries in the root as mount triggers */
774 if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
775 d_set_d_op(dentry, &autofs4_dentry_operations);
776 __managed_dentry_set_managed(dentry);
779 ino = autofs4_init_ino(NULL, sbi, 0555);
781 return ERR_PTR(-ENOMEM);
783 dentry->d_fsdata = ino;
784 ino->dentry = dentry;
786 autofs4_add_active(dentry);
788 d_instantiate(dentry, NULL);
793 static int autofs4_dir_symlink(struct inode *dir,
794 struct dentry *dentry,
797 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
798 struct autofs_info *ino = autofs4_dentry_ino(dentry);
799 struct autofs_info *p_ino;
803 DPRINTK("%s <- %.*s", symname,
804 dentry->d_name.len, dentry->d_name.name);
806 if (!autofs4_oz_mode(sbi))
809 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
813 autofs4_del_active(dentry);
815 ino->size = strlen(symname);
816 cp = kmalloc(ino->size + 1, GFP_KERNEL);
818 if (!dentry->d_fsdata)
825 inode = autofs4_get_inode(dir->i_sb, ino);
828 if (!dentry->d_fsdata)
832 d_add(dentry, inode);
834 dentry->d_fsdata = ino;
835 ino->dentry = dget(dentry);
836 atomic_inc(&ino->count);
837 p_ino = autofs4_dentry_ino(dentry->d_parent);
838 if (p_ino && dentry->d_parent != dentry)
839 atomic_inc(&p_ino->count);
843 dir->i_mtime = CURRENT_TIME;
851 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
852 * that the file no longer exists. However, doing that means that the
853 * VFS layer can turn the dentry into a negative dentry. We don't want
854 * this, because the unlink is probably the result of an expire.
855 * We simply d_drop it and add it to a expiring list in the super block,
856 * which allows the dentry lookup to check for an incomplete expire.
858 * If a process is blocked on the dentry waiting for the expire to finish,
859 * it will invalidate the dentry and try to mount with a new one.
861 * Also see autofs4_dir_rmdir()..
863 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
865 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
866 struct autofs_info *ino = autofs4_dentry_ino(dentry);
867 struct autofs_info *p_ino;
869 /* This allows root to remove symlinks */
870 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
873 if (atomic_dec_and_test(&ino->count)) {
874 p_ino = autofs4_dentry_ino(dentry->d_parent);
875 if (p_ino && dentry->d_parent != dentry)
876 atomic_dec(&p_ino->count);
880 dentry->d_inode->i_size = 0;
881 clear_nlink(dentry->d_inode);
883 dir->i_mtime = CURRENT_TIME;
885 spin_lock(&autofs4_lock);
886 autofs4_add_expiring(dentry);
887 spin_lock(&dentry->d_lock);
889 spin_unlock(&dentry->d_lock);
890 spin_unlock(&autofs4_lock);
895 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
897 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
898 struct autofs_info *ino = autofs4_dentry_ino(dentry);
899 struct autofs_info *p_ino;
901 DPRINTK("dentry %p, removing %.*s",
902 dentry, dentry->d_name.len, dentry->d_name.name);
904 if (!autofs4_oz_mode(sbi))
907 spin_lock(&autofs4_lock);
908 spin_lock(&sbi->lookup_lock);
909 spin_lock(&dentry->d_lock);
910 if (!list_empty(&dentry->d_subdirs)) {
911 spin_unlock(&dentry->d_lock);
912 spin_unlock(&sbi->lookup_lock);
913 spin_unlock(&autofs4_lock);
916 __autofs4_add_expiring(dentry);
917 spin_unlock(&sbi->lookup_lock);
919 spin_unlock(&dentry->d_lock);
920 spin_unlock(&autofs4_lock);
922 if (atomic_dec_and_test(&ino->count)) {
923 p_ino = autofs4_dentry_ino(dentry->d_parent);
924 if (p_ino && dentry->d_parent != dentry)
925 atomic_dec(&p_ino->count);
928 dentry->d_inode->i_size = 0;
929 clear_nlink(dentry->d_inode);
937 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
939 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
940 struct autofs_info *ino = autofs4_dentry_ino(dentry);
941 struct autofs_info *p_ino;
944 if (!autofs4_oz_mode(sbi))
947 DPRINTK("dentry %p, creating %.*s",
948 dentry, dentry->d_name.len, dentry->d_name.name);
950 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
954 autofs4_del_active(dentry);
956 inode = autofs4_get_inode(dir->i_sb, ino);
958 if (!dentry->d_fsdata)
962 d_add(dentry, inode);
964 dentry->d_fsdata = ino;
965 ino->dentry = dget(dentry);
966 atomic_inc(&ino->count);
967 p_ino = autofs4_dentry_ino(dentry->d_parent);
968 if (p_ino && dentry->d_parent != dentry)
969 atomic_inc(&p_ino->count);
972 dir->i_mtime = CURRENT_TIME;
977 /* Get/set timeout ioctl() operation */
979 static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
980 compat_ulong_t __user *p)
983 unsigned long ntimeout;
985 if ((rv = get_user(ntimeout, p)) ||
986 (rv = put_user(sbi->exp_timeout/HZ, p)))
989 if (ntimeout > UINT_MAX/HZ)
990 sbi->exp_timeout = 0;
992 sbi->exp_timeout = ntimeout * HZ;
998 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
999 unsigned long __user *p)
1002 unsigned long ntimeout;
1004 if ((rv = get_user(ntimeout, p)) ||
1005 (rv = put_user(sbi->exp_timeout/HZ, p)))
1008 if (ntimeout > ULONG_MAX/HZ)
1009 sbi->exp_timeout = 0;
1011 sbi->exp_timeout = ntimeout * HZ;
1016 /* Return protocol version */
1017 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
1019 return put_user(sbi->version, p);
1022 /* Return protocol sub version */
1023 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
1025 return put_user(sbi->sub_version, p);
1029 * Tells the daemon whether it can umount the autofs mount.
1031 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1035 if (may_umount(mnt))
1038 DPRINTK("returning %d", status);
1040 status = put_user(status, p);
1045 /* Identify autofs4_dentries - this is so we can tell if there's
1046 an extra dentry refcount or not. We only hold a refcount on the
1047 dentry if its non-negative (ie, d_inode != NULL)
1049 int is_autofs4_dentry(struct dentry *dentry)
1051 return dentry && dentry->d_inode &&
1052 (dentry->d_op == &autofs4_root_dentry_operations ||
1053 dentry->d_op == &autofs4_dentry_operations) &&
1054 dentry->d_fsdata != NULL;
1058 * ioctl()'s on the root directory is the chief method for the daemon to
1059 * generate kernel reactions
1061 static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
1062 unsigned int cmd, unsigned long arg)
1064 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
1065 void __user *p = (void __user *)arg;
1067 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
1068 cmd,arg,sbi,task_pgrp_nr(current));
1070 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
1071 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
1074 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
1078 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
1079 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
1080 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
1081 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
1082 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
1083 autofs4_catatonic_mode(sbi);
1085 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
1086 return autofs4_get_protover(sbi, p);
1087 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
1088 return autofs4_get_protosubver(sbi, p);
1089 case AUTOFS_IOC_SETTIMEOUT:
1090 return autofs4_get_set_timeout(sbi, p);
1091 #ifdef CONFIG_COMPAT
1092 case AUTOFS_IOC_SETTIMEOUT32:
1093 return autofs4_compat_get_set_timeout(sbi, p);
1096 case AUTOFS_IOC_ASKUMOUNT:
1097 return autofs4_ask_umount(filp->f_path.mnt, p);
1099 /* return a single thing to expire */
1100 case AUTOFS_IOC_EXPIRE:
1101 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
1102 /* same as above, but can send multiple expires through pipe */
1103 case AUTOFS_IOC_EXPIRE_MULTI:
1104 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
1111 static long autofs4_root_ioctl(struct file *filp,
1112 unsigned int cmd, unsigned long arg)
1114 struct inode *inode = filp->f_dentry->d_inode;
1115 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1118 #ifdef CONFIG_COMPAT
1119 static long autofs4_root_compat_ioctl(struct file *filp,
1120 unsigned int cmd, unsigned long arg)
1122 struct inode *inode = filp->f_path.dentry->d_inode;
1125 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1126 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1128 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1129 (unsigned long)compat_ptr(arg));