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/param.h>
19 #include <linux/time.h>
22 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs4_dir_unlink(struct inode *,struct dentry *);
24 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27 static int autofs4_dir_open(struct inode *inode, struct file *file);
28 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
29 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
30 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
32 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
33 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
35 const struct file_operations autofs4_root_operations = {
36 .open = dcache_dir_open,
37 .release = dcache_dir_close,
38 .read = generic_read_dir,
39 .readdir = autofs4_root_readdir,
40 .ioctl = autofs4_root_ioctl,
43 const struct file_operations autofs4_dir_operations = {
44 .open = autofs4_dir_open,
45 .release = dcache_dir_close,
46 .read = generic_read_dir,
47 .readdir = dcache_readdir,
50 const struct inode_operations autofs4_indirect_root_inode_operations = {
51 .lookup = autofs4_lookup,
52 .unlink = autofs4_dir_unlink,
53 .symlink = autofs4_dir_symlink,
54 .mkdir = autofs4_dir_mkdir,
55 .rmdir = autofs4_dir_rmdir,
58 const struct inode_operations autofs4_direct_root_inode_operations = {
59 .lookup = autofs4_lookup,
60 .unlink = autofs4_dir_unlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
63 .follow_link = autofs4_follow_link,
66 const struct inode_operations autofs4_dir_inode_operations = {
67 .lookup = autofs4_lookup,
68 .unlink = autofs4_dir_unlink,
69 .symlink = autofs4_dir_symlink,
70 .mkdir = autofs4_dir_mkdir,
71 .rmdir = autofs4_dir_rmdir,
74 static int autofs4_root_readdir(struct file *file, void *dirent,
77 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
78 int oz_mode = autofs4_oz_mode(sbi);
80 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
83 * Don't set reghost flag if:
84 * 1) f_pos is larger than zero -- we've already been here.
85 * 2) we haven't even enabled reghosting in the 1st place.
86 * 3) this is the daemon doing a readdir
88 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
89 sbi->needs_reghost = 1;
91 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
93 return dcache_readdir(file, dirent, filldir);
96 static int autofs4_dir_open(struct inode *inode, struct file *file)
98 struct dentry *dentry = file->f_path.dentry;
99 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
101 DPRINTK("file=%p dentry=%p %.*s",
102 file, dentry, dentry->d_name.len, dentry->d_name.name);
104 if (autofs4_oz_mode(sbi))
108 * An empty directory in an autofs file system is always a
109 * mount point. The daemon must have failed to mount this
110 * during lookup so it doesn't exist. This can happen, for
111 * example, if user space returns an incorrect status for a
112 * mount request. Otherwise we're doing a readdir on the
113 * autofs file system so just let the libfs routines handle
116 spin_lock(&dcache_lock);
117 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
118 spin_unlock(&dcache_lock);
121 spin_unlock(&dcache_lock);
124 return dcache_dir_open(inode, file);
127 static int try_to_fill_dentry(struct dentry *dentry, int flags)
129 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
130 struct autofs_info *ino = autofs4_dentry_ino(dentry);
133 /* Block on any pending expiry here; invalidate the dentry
134 when expiration is done to trigger mount request with a new
136 spin_lock(&sbi->fs_lock);
137 if (ino->flags & AUTOFS_INF_EXPIRING) {
138 spin_unlock(&sbi->fs_lock);
140 DPRINTK("waiting for expire %p name=%.*s",
141 dentry, dentry->d_name.len, dentry->d_name.name);
143 status = autofs4_wait(sbi, dentry, NFY_NONE);
145 DPRINTK("expire done status=%d", status);
148 * If the directory still exists the mount request must
149 * continue otherwise it can't be followed at the right
150 * time during the walk.
152 status = d_invalidate(dentry);
153 if (status != -EBUSY)
158 spin_unlock(&sbi->fs_lock);
160 DPRINTK("dentry=%p %.*s ino=%p",
161 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
164 * Wait for a pending mount, triggering one if there
167 if (dentry->d_inode == NULL) {
168 DPRINTK("waiting for mount name=%.*s",
169 dentry->d_name.len, dentry->d_name.name);
171 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
173 DPRINTK("mount done status=%d", status);
175 /* Turn this into a real negative dentry? */
176 if (status == -ENOENT) {
177 spin_lock(&dentry->d_lock);
178 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
179 spin_unlock(&dentry->d_lock);
182 /* Return a negative dentry, but leave it "pending" */
185 /* Trigger mount for path component or follow link */
186 } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
187 flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
188 current->link_count) {
189 DPRINTK("waiting for mount name=%.*s",
190 dentry->d_name.len, dentry->d_name.name);
192 spin_lock(&dentry->d_lock);
193 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
194 spin_unlock(&dentry->d_lock);
195 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
197 DPRINTK("mount done status=%d", status);
200 spin_lock(&dentry->d_lock);
201 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
202 spin_unlock(&dentry->d_lock);
207 /* Initialize expiry counter after successful mount */
209 ino->last_used = jiffies;
211 spin_lock(&dentry->d_lock);
212 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
213 spin_unlock(&dentry->d_lock);
218 /* For autofs direct mounts the follow link triggers the mount */
219 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
221 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
222 struct autofs_info *ino = autofs4_dentry_ino(dentry);
223 int oz_mode = autofs4_oz_mode(sbi);
224 unsigned int lookup_type;
227 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
228 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
231 /* If it's our master or we shouldn't trigger a mount we're done */
232 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
234 !(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
237 /* If an expire request is pending wait for it. */
238 spin_lock(&sbi->fs_lock);
239 if (ino->flags & AUTOFS_INF_EXPIRING) {
240 spin_unlock(&sbi->fs_lock);
242 DPRINTK("waiting for active request %p name=%.*s",
243 dentry, dentry->d_name.len, dentry->d_name.name);
245 status = autofs4_wait(sbi, dentry, NFY_NONE);
247 DPRINTK("request done status=%d", status);
251 spin_unlock(&sbi->fs_lock);
254 * If the dentry contains directories then it is an
255 * autofs multi-mount with no root mount offset. So
256 * don't try to mount it again.
258 spin_lock(&dcache_lock);
259 if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
260 (!d_mountpoint(dentry) && __simple_empty(dentry))) {
261 spin_unlock(&dcache_lock);
263 status = try_to_fill_dentry(dentry, 0);
268 * The mount succeeded but if there is no root mount
269 * it must be an autofs multi-mount with no root offset
270 * so we don't need to follow the mount.
272 if (d_mountpoint(dentry)) {
273 if (!autofs4_follow_mount(&nd->path.mnt,
282 spin_unlock(&dcache_lock);
289 return ERR_PTR(status);
293 * Revalidate is called on every cache lookup. Some of those
294 * cache lookups may actually happen while the dentry is not
295 * yet completely filled in, and revalidate has to delay such
298 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
300 struct inode *dir = dentry->d_parent->d_inode;
301 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
302 int oz_mode = autofs4_oz_mode(sbi);
303 int flags = nd ? nd->flags : 0;
307 spin_lock(&sbi->fs_lock);
308 if (autofs4_ispending(dentry)) {
309 /* The daemon never causes a mount to trigger */
310 spin_unlock(&sbi->fs_lock);
316 * A zero status is success otherwise we have a
317 * negative error code.
319 status = try_to_fill_dentry(dentry, flags);
324 * A status of EAGAIN here means that the dentry has gone
325 * away while waiting for an expire to complete. If we are
326 * racing with expire lookup will wait for it so this must
327 * be a revalidate and we need to send it to lookup.
329 if (status == -EAGAIN)
334 spin_unlock(&sbi->fs_lock);
336 /* Negative dentry.. invalidate if "old" */
337 if (dentry->d_inode == NULL)
340 /* Check for a non-mountpoint directory with no contents */
341 spin_lock(&dcache_lock);
342 if (S_ISDIR(dentry->d_inode->i_mode) &&
343 !d_mountpoint(dentry) &&
344 __simple_empty(dentry)) {
345 DPRINTK("dentry=%p %.*s, emptydir",
346 dentry, dentry->d_name.len, dentry->d_name.name);
347 spin_unlock(&dcache_lock);
349 /* The daemon never causes a mount to trigger */
354 * A zero status is success otherwise we have a
355 * negative error code.
357 status = try_to_fill_dentry(dentry, flags);
363 spin_unlock(&dcache_lock);
368 void autofs4_dentry_release(struct dentry *de)
370 struct autofs_info *inf;
372 DPRINTK("releasing %p", de);
374 inf = autofs4_dentry_ino(de);
378 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
381 spin_lock(&sbi->lookup_lock);
382 if (!list_empty(&inf->active))
383 list_del(&inf->active);
384 if (!list_empty(&inf->expiring))
385 list_del(&inf->expiring);
386 spin_unlock(&sbi->lookup_lock);
392 autofs4_free_ino(inf);
396 /* For dentries of directories in the root dir */
397 static struct dentry_operations autofs4_root_dentry_operations = {
398 .d_revalidate = autofs4_revalidate,
399 .d_release = autofs4_dentry_release,
402 /* For other dentries */
403 static struct dentry_operations autofs4_dentry_operations = {
404 .d_revalidate = autofs4_revalidate,
405 .d_release = autofs4_dentry_release,
408 static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
410 unsigned int len = name->len;
411 unsigned int hash = name->hash;
412 const unsigned char *str = name->name;
413 struct list_head *p, *head;
415 spin_lock(&dcache_lock);
416 spin_lock(&sbi->lookup_lock);
417 head = &sbi->active_list;
418 list_for_each(p, head) {
419 struct autofs_info *ino;
420 struct dentry *dentry;
423 ino = list_entry(p, struct autofs_info, active);
424 dentry = ino->dentry;
426 spin_lock(&dentry->d_lock);
429 if (atomic_read(&dentry->d_count) == 0)
432 qstr = &dentry->d_name;
434 if (dentry->d_name.hash != hash)
436 if (dentry->d_parent != parent)
439 if (qstr->len != len)
441 if (memcmp(qstr->name, str, len))
444 if (d_unhashed(dentry)) {
446 spin_unlock(&dentry->d_lock);
447 spin_unlock(&sbi->lookup_lock);
448 spin_unlock(&dcache_lock);
452 spin_unlock(&dentry->d_lock);
454 spin_unlock(&sbi->lookup_lock);
455 spin_unlock(&dcache_lock);
460 static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
462 unsigned int len = name->len;
463 unsigned int hash = name->hash;
464 const unsigned char *str = name->name;
465 struct list_head *p, *head;
467 spin_lock(&dcache_lock);
468 spin_lock(&sbi->lookup_lock);
469 head = &sbi->expiring_list;
470 list_for_each(p, head) {
471 struct autofs_info *ino;
472 struct dentry *dentry;
475 ino = list_entry(p, struct autofs_info, expiring);
476 dentry = ino->dentry;
478 spin_lock(&dentry->d_lock);
480 /* Bad luck, we've already been dentry_iput */
481 if (!dentry->d_inode)
484 qstr = &dentry->d_name;
486 if (dentry->d_name.hash != hash)
488 if (dentry->d_parent != parent)
491 if (qstr->len != len)
493 if (memcmp(qstr->name, str, len))
496 if (d_unhashed(dentry)) {
498 spin_unlock(&dentry->d_lock);
499 spin_unlock(&sbi->lookup_lock);
500 spin_unlock(&dcache_lock);
504 spin_unlock(&dentry->d_lock);
506 spin_unlock(&sbi->lookup_lock);
507 spin_unlock(&dcache_lock);
512 /* Lookups in the root directory */
513 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
515 struct autofs_sb_info *sbi;
516 struct autofs_info *ino;
517 struct dentry *expiring, *unhashed;
520 DPRINTK("name = %.*s",
521 dentry->d_name.len, dentry->d_name.name);
523 /* File name too long to exist */
524 if (dentry->d_name.len > NAME_MAX)
525 return ERR_PTR(-ENAMETOOLONG);
527 sbi = autofs4_sbi(dir->i_sb);
528 oz_mode = autofs4_oz_mode(sbi);
530 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
531 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
533 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
536 * If we are racing with expire the request might not
537 * be quite complete but the directory has been removed
538 * so it must have been successful, so just wait for it.
540 ino = autofs4_dentry_ino(expiring);
541 spin_lock(&sbi->fs_lock);
542 if (ino->flags & AUTOFS_INF_EXPIRING) {
543 spin_unlock(&sbi->fs_lock);
544 DPRINTK("wait for incomplete expire %p name=%.*s",
545 expiring, expiring->d_name.len,
546 expiring->d_name.name);
547 autofs4_wait(sbi, expiring, NFY_NONE);
548 DPRINTK("request completed");
551 spin_unlock(&sbi->fs_lock);
553 spin_lock(&sbi->lookup_lock);
554 if (!list_empty(&ino->expiring))
555 list_del_init(&ino->expiring);
556 spin_unlock(&sbi->lookup_lock);
560 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
565 * Mark the dentry incomplete but don't hash it. We do this
566 * to serialize our inode creation operations (symlink and
567 * mkdir) which prevents deadlock during the callback to
568 * the daemon. Subsequent user space lookups for the same
569 * dentry are placed on the wait queue while the daemon
570 * itself is allowed passage unresticted so the create
571 * operation itself can then hash the dentry. Finally,
572 * we check for the hashed dentry and return the newly
575 dentry->d_op = &autofs4_root_dentry_operations;
578 * And we need to ensure that the same dentry is used for
579 * all following lookup calls until it is hashed so that
580 * the dentry flags are persistent throughout the request.
582 ino = autofs4_init_ino(NULL, sbi, 0555);
584 return ERR_PTR(-ENOMEM);
586 dentry->d_fsdata = ino;
587 ino->dentry = dentry;
589 spin_lock(&sbi->lookup_lock);
590 list_add(&ino->active, &sbi->active_list);
591 spin_unlock(&sbi->lookup_lock);
593 d_instantiate(dentry, NULL);
597 spin_lock(&dentry->d_lock);
598 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
599 spin_unlock(&dentry->d_lock);
600 if (dentry->d_op && dentry->d_op->d_revalidate) {
601 mutex_unlock(&dir->i_mutex);
602 (dentry->d_op->d_revalidate)(dentry, nd);
603 mutex_lock(&dir->i_mutex);
608 * If we are still pending, check if we had to handle
609 * a signal. If so we can force a restart..
611 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
612 /* See if we were interrupted */
613 if (signal_pending(current)) {
614 sigset_t *sigset = ¤t->pending.signal;
615 if (sigismember (sigset, SIGKILL) ||
616 sigismember (sigset, SIGQUIT) ||
617 sigismember (sigset, SIGINT)) {
620 return ERR_PTR(-ERESTARTNOINTR);
624 spin_lock(&dentry->d_lock);
625 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
626 spin_unlock(&dentry->d_lock);
631 * If this dentry is unhashed, then we shouldn't honour this
632 * lookup. Returning ENOENT here doesn't do the right thing
633 * for all system calls, but it should be OK for the operations
634 * we permit from an autofs.
636 if (!oz_mode && d_unhashed(dentry)) {
638 * A user space application can (and has done in the past)
639 * remove and re-create this directory during the callback.
640 * This can leave us with an unhashed dentry, but a
641 * successful mount! So we need to perform another
642 * cached lookup in case the dentry now exists.
644 struct dentry *parent = dentry->d_parent;
645 struct dentry *new = d_lookup(parent, &dentry->d_name);
649 dentry = ERR_PTR(-ENOENT);
663 static int autofs4_dir_symlink(struct inode *dir,
664 struct dentry *dentry,
667 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
668 struct autofs_info *ino = autofs4_dentry_ino(dentry);
669 struct autofs_info *p_ino;
673 DPRINTK("%s <- %.*s", symname,
674 dentry->d_name.len, dentry->d_name.name);
676 if (!autofs4_oz_mode(sbi))
679 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
683 spin_lock(&sbi->lookup_lock);
684 if (!list_empty(&ino->active))
685 list_del_init(&ino->active);
686 spin_unlock(&sbi->lookup_lock);
688 ino->size = strlen(symname);
689 cp = kmalloc(ino->size + 1, GFP_KERNEL);
691 if (!dentry->d_fsdata)
698 inode = autofs4_get_inode(dir->i_sb, ino);
701 if (!dentry->d_fsdata)
705 d_add(dentry, inode);
707 if (dir == dir->i_sb->s_root->d_inode)
708 dentry->d_op = &autofs4_root_dentry_operations;
710 dentry->d_op = &autofs4_dentry_operations;
712 dentry->d_fsdata = ino;
713 ino->dentry = dget(dentry);
714 atomic_inc(&ino->count);
715 p_ino = autofs4_dentry_ino(dentry->d_parent);
716 if (p_ino && dentry->d_parent != dentry)
717 atomic_inc(&p_ino->count);
721 dir->i_mtime = CURRENT_TIME;
729 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
730 * that the file no longer exists. However, doing that means that the
731 * VFS layer can turn the dentry into a negative dentry. We don't want
732 * this, because the unlink is probably the result of an expire.
733 * We simply d_drop it and add it to a expiring list in the super block,
734 * which allows the dentry lookup to check for an incomplete expire.
736 * If a process is blocked on the dentry waiting for the expire to finish,
737 * it will invalidate the dentry and try to mount with a new one.
739 * Also see autofs4_dir_rmdir()..
741 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
743 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
744 struct autofs_info *ino = autofs4_dentry_ino(dentry);
745 struct autofs_info *p_ino;
747 /* This allows root to remove symlinks */
748 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
751 if (atomic_dec_and_test(&ino->count)) {
752 p_ino = autofs4_dentry_ino(dentry->d_parent);
753 if (p_ino && dentry->d_parent != dentry)
754 atomic_dec(&p_ino->count);
758 dentry->d_inode->i_size = 0;
759 clear_nlink(dentry->d_inode);
761 dir->i_mtime = CURRENT_TIME;
763 spin_lock(&dcache_lock);
764 spin_lock(&sbi->lookup_lock);
765 if (list_empty(&ino->expiring))
766 list_add(&ino->expiring, &sbi->expiring_list);
767 spin_unlock(&sbi->lookup_lock);
768 spin_lock(&dentry->d_lock);
770 spin_unlock(&dentry->d_lock);
771 spin_unlock(&dcache_lock);
776 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
778 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
779 struct autofs_info *ino = autofs4_dentry_ino(dentry);
780 struct autofs_info *p_ino;
782 DPRINTK("dentry %p, removing %.*s",
783 dentry, dentry->d_name.len, dentry->d_name.name);
785 if (!autofs4_oz_mode(sbi))
788 spin_lock(&dcache_lock);
789 if (!list_empty(&dentry->d_subdirs)) {
790 spin_unlock(&dcache_lock);
793 spin_lock(&sbi->lookup_lock);
794 if (list_empty(&ino->expiring))
795 list_add(&ino->expiring, &sbi->expiring_list);
796 spin_unlock(&sbi->lookup_lock);
797 spin_lock(&dentry->d_lock);
799 spin_unlock(&dentry->d_lock);
800 spin_unlock(&dcache_lock);
802 if (atomic_dec_and_test(&ino->count)) {
803 p_ino = autofs4_dentry_ino(dentry->d_parent);
804 if (p_ino && dentry->d_parent != dentry)
805 atomic_dec(&p_ino->count);
808 dentry->d_inode->i_size = 0;
809 clear_nlink(dentry->d_inode);
817 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
819 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
820 struct autofs_info *ino = autofs4_dentry_ino(dentry);
821 struct autofs_info *p_ino;
824 if (!autofs4_oz_mode(sbi))
827 DPRINTK("dentry %p, creating %.*s",
828 dentry, dentry->d_name.len, dentry->d_name.name);
830 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
834 spin_lock(&sbi->lookup_lock);
835 if (!list_empty(&ino->active))
836 list_del_init(&ino->active);
837 spin_unlock(&sbi->lookup_lock);
839 inode = autofs4_get_inode(dir->i_sb, ino);
841 if (!dentry->d_fsdata)
845 d_add(dentry, inode);
847 if (dir == dir->i_sb->s_root->d_inode)
848 dentry->d_op = &autofs4_root_dentry_operations;
850 dentry->d_op = &autofs4_dentry_operations;
852 dentry->d_fsdata = ino;
853 ino->dentry = dget(dentry);
854 atomic_inc(&ino->count);
855 p_ino = autofs4_dentry_ino(dentry->d_parent);
856 if (p_ino && dentry->d_parent != dentry)
857 atomic_inc(&p_ino->count);
860 dir->i_mtime = CURRENT_TIME;
865 /* Get/set timeout ioctl() operation */
866 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
867 unsigned long __user *p)
870 unsigned long ntimeout;
872 if ((rv = get_user(ntimeout, p)) ||
873 (rv = put_user(sbi->exp_timeout/HZ, p)))
876 if (ntimeout > ULONG_MAX/HZ)
877 sbi->exp_timeout = 0;
879 sbi->exp_timeout = ntimeout * HZ;
884 /* Return protocol version */
885 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
887 return put_user(sbi->version, p);
890 /* Return protocol sub version */
891 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
893 return put_user(sbi->sub_version, p);
897 * Tells the daemon whether we need to reghost or not. Also, clears
898 * the reghost_needed flag.
900 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
904 DPRINTK("returning %d", sbi->needs_reghost);
906 status = put_user(sbi->needs_reghost, p);
910 sbi->needs_reghost = 0;
915 * Enable / Disable reghosting ioctl() operation
917 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
922 status = get_user(val, p);
924 DPRINTK("reghost = %d", val);
929 /* turn on/off reghosting, with the val */
930 sbi->reghost_enabled = val;
935 * Tells the daemon whether it can umount the autofs mount.
937 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
944 DPRINTK("returning %d", status);
946 status = put_user(status, p);
951 /* Identify autofs4_dentries - this is so we can tell if there's
952 an extra dentry refcount or not. We only hold a refcount on the
953 dentry if its non-negative (ie, d_inode != NULL)
955 int is_autofs4_dentry(struct dentry *dentry)
957 return dentry && dentry->d_inode &&
958 (dentry->d_op == &autofs4_root_dentry_operations ||
959 dentry->d_op == &autofs4_dentry_operations) &&
960 dentry->d_fsdata != NULL;
964 * ioctl()'s on the root directory is the chief method for the daemon to
965 * generate kernel reactions
967 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
968 unsigned int cmd, unsigned long arg)
970 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
971 void __user *p = (void __user *)arg;
973 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
974 cmd,arg,sbi,task_pgrp_nr(current));
976 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
977 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
980 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
984 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
985 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
986 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
987 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
988 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
989 autofs4_catatonic_mode(sbi);
991 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
992 return autofs4_get_protover(sbi, p);
993 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
994 return autofs4_get_protosubver(sbi, p);
995 case AUTOFS_IOC_SETTIMEOUT:
996 return autofs4_get_set_timeout(sbi, p);
998 case AUTOFS_IOC_TOGGLEREGHOST:
999 return autofs4_toggle_reghost(sbi, p);
1000 case AUTOFS_IOC_ASKREGHOST:
1001 return autofs4_ask_reghost(sbi, p);
1003 case AUTOFS_IOC_ASKUMOUNT:
1004 return autofs4_ask_umount(filp->f_path.mnt, p);
1006 /* return a single thing to expire */
1007 case AUTOFS_IOC_EXPIRE:
1008 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
1009 /* same as above, but can send multiple expires through pipe */
1010 case AUTOFS_IOC_EXPIRE_MULTI:
1011 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);