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_dir_close(struct inode *inode, struct file *file);
29 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
32 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
34 const struct file_operations autofs4_root_operations = {
35 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = autofs4_root_readdir,
39 .ioctl = autofs4_root_ioctl,
42 const struct file_operations autofs4_dir_operations = {
43 .open = autofs4_dir_open,
44 .release = autofs4_dir_close,
45 .read = generic_read_dir,
46 .readdir = autofs4_dir_readdir,
49 const struct inode_operations autofs4_indirect_root_inode_operations = {
50 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
57 const struct inode_operations autofs4_direct_root_inode_operations = {
58 .lookup = autofs4_lookup,
59 .unlink = autofs4_dir_unlink,
60 .mkdir = autofs4_dir_mkdir,
61 .rmdir = autofs4_dir_rmdir,
62 .follow_link = autofs4_follow_link,
65 const struct inode_operations autofs4_dir_inode_operations = {
66 .lookup = autofs4_lookup,
67 .unlink = autofs4_dir_unlink,
68 .symlink = autofs4_dir_symlink,
69 .mkdir = autofs4_dir_mkdir,
70 .rmdir = autofs4_dir_rmdir,
73 static int autofs4_root_readdir(struct file *file, void *dirent,
76 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
77 int oz_mode = autofs4_oz_mode(sbi);
79 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
82 * Don't set reghost flag if:
83 * 1) f_pos is larger than zero -- we've already been here.
84 * 2) we haven't even enabled reghosting in the 1st place.
85 * 3) this is the daemon doing a readdir
87 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
88 sbi->needs_reghost = 1;
90 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
92 return dcache_readdir(file, dirent, filldir);
95 static int autofs4_dir_open(struct inode *inode, struct file *file)
97 struct dentry *dentry = file->f_path.dentry;
98 struct vfsmount *mnt = file->f_path.mnt;
99 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
100 struct dentry *cursor;
103 status = dcache_dir_open(inode, file);
107 cursor = file->private_data;
108 cursor->d_fsdata = NULL;
110 DPRINTK("file=%p dentry=%p %.*s",
111 file, dentry, dentry->d_name.len, dentry->d_name.name);
113 if (autofs4_oz_mode(sbi))
116 if (autofs4_ispending(dentry)) {
117 DPRINTK("dentry busy");
118 dcache_dir_close(inode, file);
124 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
128 /* In case there are stale directory dentrys from a failed mount */
129 spin_lock(&dcache_lock);
130 empty = list_empty(&dentry->d_subdirs);
131 spin_unlock(&dcache_lock);
134 d_invalidate(dentry);
136 nd.flags = LOOKUP_DIRECTORY;
137 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
142 dcache_dir_close(inode, file);
147 if (d_mountpoint(dentry)) {
148 struct file *fp = NULL;
149 struct path fp_path = { .dentry = dentry, .mnt = mnt };
153 if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
155 dcache_dir_close(inode, file);
159 fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
160 status = PTR_ERR(fp);
162 dcache_dir_close(inode, file);
165 cursor->d_fsdata = fp;
172 static int autofs4_dir_close(struct inode *inode, struct file *file)
174 struct dentry *dentry = file->f_path.dentry;
175 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
176 struct dentry *cursor = file->private_data;
179 DPRINTK("file=%p dentry=%p %.*s",
180 file, dentry, dentry->d_name.len, dentry->d_name.name);
182 if (autofs4_oz_mode(sbi))
185 if (autofs4_ispending(dentry)) {
186 DPRINTK("dentry busy");
191 if (d_mountpoint(dentry)) {
192 struct file *fp = cursor->d_fsdata;
197 filp_close(fp, current->files);
200 dcache_dir_close(inode, file);
204 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
206 struct dentry *dentry = file->f_path.dentry;
207 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
208 struct dentry *cursor = file->private_data;
211 DPRINTK("file=%p dentry=%p %.*s",
212 file, dentry, dentry->d_name.len, dentry->d_name.name);
214 if (autofs4_oz_mode(sbi))
217 if (autofs4_ispending(dentry)) {
218 DPRINTK("dentry busy");
222 if (d_mountpoint(dentry)) {
223 struct file *fp = cursor->d_fsdata;
228 if (!fp->f_op || !fp->f_op->readdir)
231 status = vfs_readdir(fp, filldir, dirent);
232 file->f_pos = fp->f_pos;
234 autofs4_copy_atime(file, fp);
238 return dcache_readdir(file, dirent, filldir);
241 static int try_to_fill_dentry(struct dentry *dentry, int flags)
243 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
244 struct autofs_info *ino = autofs4_dentry_ino(dentry);
247 /* Block on any pending expiry here; invalidate the dentry
248 when expiration is done to trigger mount request with a new
250 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
251 DPRINTK("waiting for expire %p name=%.*s",
252 dentry, dentry->d_name.len, dentry->d_name.name);
254 status = autofs4_wait(sbi, dentry, NFY_NONE);
256 DPRINTK("expire done status=%d", status);
259 * If the directory still exists the mount request must
260 * continue otherwise it can't be followed at the right
261 * time during the walk.
263 status = d_invalidate(dentry);
264 if (status != -EBUSY)
268 DPRINTK("dentry=%p %.*s ino=%p",
269 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
272 * Wait for a pending mount, triggering one if there
275 if (dentry->d_inode == NULL) {
276 DPRINTK("waiting for mount name=%.*s",
277 dentry->d_name.len, dentry->d_name.name);
279 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
281 DPRINTK("mount done status=%d", status);
283 /* Turn this into a real negative dentry? */
284 if (status == -ENOENT) {
285 spin_lock(&dentry->d_lock);
286 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
287 spin_unlock(&dentry->d_lock);
290 /* Return a negative dentry, but leave it "pending" */
293 /* Trigger mount for path component or follow link */
294 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
295 current->link_count) {
296 DPRINTK("waiting for mount name=%.*s",
297 dentry->d_name.len, dentry->d_name.name);
299 spin_lock(&dentry->d_lock);
300 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
301 spin_unlock(&dentry->d_lock);
302 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
304 DPRINTK("mount done status=%d", status);
307 spin_lock(&dentry->d_lock);
308 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
309 spin_unlock(&dentry->d_lock);
314 /* Initialize expiry counter after successful mount */
316 ino->last_used = jiffies;
318 spin_lock(&dentry->d_lock);
319 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
320 spin_unlock(&dentry->d_lock);
325 /* For autofs direct mounts the follow link triggers the mount */
326 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
328 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
329 struct autofs_info *ino = autofs4_dentry_ino(dentry);
330 int oz_mode = autofs4_oz_mode(sbi);
331 unsigned int lookup_type;
334 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
335 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
338 /* If it's our master or we shouldn't trigger a mount we're done */
339 lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
340 if (oz_mode || !lookup_type)
343 /* If an expire request is pending wait for it. */
344 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
345 DPRINTK("waiting for active request %p name=%.*s",
346 dentry, dentry->d_name.len, dentry->d_name.name);
348 status = autofs4_wait(sbi, dentry, NFY_NONE);
350 DPRINTK("request done status=%d", status);
354 * If the dentry contains directories then it is an
355 * autofs multi-mount with no root mount offset. So
356 * don't try to mount it again.
358 spin_lock(&dcache_lock);
359 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
360 spin_unlock(&dcache_lock);
362 status = try_to_fill_dentry(dentry, 0);
367 * The mount succeeded but if there is no root mount
368 * it must be an autofs multi-mount with no root offset
369 * so we don't need to follow the mount.
371 if (d_mountpoint(dentry)) {
372 if (!autofs4_follow_mount(&nd->path.mnt,
381 spin_unlock(&dcache_lock);
388 return ERR_PTR(status);
392 * Revalidate is called on every cache lookup. Some of those
393 * cache lookups may actually happen while the dentry is not
394 * yet completely filled in, and revalidate has to delay such
397 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
399 struct inode *dir = dentry->d_parent->d_inode;
400 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
401 int oz_mode = autofs4_oz_mode(sbi);
402 int flags = nd ? nd->flags : 0;
406 if (autofs4_ispending(dentry)) {
407 /* The daemon never causes a mount to trigger */
412 * A zero status is success otherwise we have a
413 * negative error code.
415 status = try_to_fill_dentry(dentry, flags);
420 * A status of EAGAIN here means that the dentry has gone
421 * away while waiting for an expire to complete. If we are
422 * racing with expire lookup will wait for it so this must
423 * be a revalidate and we need to send it to lookup.
425 if (status == -EAGAIN)
431 /* Negative dentry.. invalidate if "old" */
432 if (dentry->d_inode == NULL)
435 /* Check for a non-mountpoint directory with no contents */
436 spin_lock(&dcache_lock);
437 if (S_ISDIR(dentry->d_inode->i_mode) &&
438 !d_mountpoint(dentry) &&
439 __simple_empty(dentry)) {
440 DPRINTK("dentry=%p %.*s, emptydir",
441 dentry, dentry->d_name.len, dentry->d_name.name);
442 spin_unlock(&dcache_lock);
443 /* The daemon never causes a mount to trigger */
448 * A zero status is success otherwise we have a
449 * negative error code.
451 status = try_to_fill_dentry(dentry, flags);
457 spin_unlock(&dcache_lock);
462 void autofs4_dentry_release(struct dentry *de)
464 struct autofs_info *inf;
466 DPRINTK("releasing %p", de);
468 inf = autofs4_dentry_ino(de);
472 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
475 spin_lock(&sbi->lookup_lock);
476 if (!list_empty(&inf->expiring))
477 list_del(&inf->expiring);
478 spin_unlock(&sbi->lookup_lock);
484 autofs4_free_ino(inf);
488 /* For dentries of directories in the root dir */
489 static struct dentry_operations autofs4_root_dentry_operations = {
490 .d_revalidate = autofs4_revalidate,
491 .d_release = autofs4_dentry_release,
494 /* For other dentries */
495 static struct dentry_operations autofs4_dentry_operations = {
496 .d_revalidate = autofs4_revalidate,
497 .d_release = autofs4_dentry_release,
500 static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
502 unsigned int len = name->len;
503 unsigned int hash = name->hash;
504 const unsigned char *str = name->name;
505 struct list_head *p, *head;
507 spin_lock(&dcache_lock);
508 spin_lock(&sbi->lookup_lock);
509 head = &sbi->expiring_list;
510 list_for_each(p, head) {
511 struct autofs_info *ino;
512 struct dentry *dentry;
515 ino = list_entry(p, struct autofs_info, expiring);
516 dentry = ino->dentry;
518 spin_lock(&dentry->d_lock);
520 /* Bad luck, we've already been dentry_iput */
521 if (!dentry->d_inode)
524 qstr = &dentry->d_name;
526 if (dentry->d_name.hash != hash)
528 if (dentry->d_parent != parent)
531 if (qstr->len != len)
533 if (memcmp(qstr->name, str, len))
536 if (d_unhashed(dentry)) {
538 spin_unlock(&dentry->d_lock);
539 spin_unlock(&sbi->lookup_lock);
540 spin_unlock(&dcache_lock);
544 spin_unlock(&dentry->d_lock);
546 spin_unlock(&sbi->lookup_lock);
547 spin_unlock(&dcache_lock);
552 /* Lookups in the root directory */
553 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
555 struct autofs_sb_info *sbi;
556 struct dentry *expiring;
559 DPRINTK("name = %.*s",
560 dentry->d_name.len, dentry->d_name.name);
562 /* File name too long to exist */
563 if (dentry->d_name.len > NAME_MAX)
564 return ERR_PTR(-ENAMETOOLONG);
566 sbi = autofs4_sbi(dir->i_sb);
567 oz_mode = autofs4_oz_mode(sbi);
569 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
570 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
572 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
574 struct autofs_info *ino = autofs4_dentry_ino(expiring);
576 * If we are racing with expire the request might not
577 * be quite complete but the directory has been removed
578 * so it must have been successful, so just wait for it.
580 while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
581 DPRINTK("wait for incomplete expire %p name=%.*s",
582 expiring, expiring->d_name.len,
583 expiring->d_name.name);
584 autofs4_wait(sbi, expiring, NFY_NONE);
585 DPRINTK("request completed");
587 spin_lock(&sbi->lookup_lock);
588 if (!list_empty(&ino->expiring))
589 list_del_init(&ino->expiring);
590 spin_unlock(&sbi->lookup_lock);
595 * Mark the dentry incomplete but don't hash it. We do this
596 * to serialize our inode creation operations (symlink and
597 * mkdir) which prevents deadlock during the callback to
598 * the daemon. Subsequent user space lookups for the same
599 * dentry are placed on the wait queue while the daemon
600 * itself is allowed passage unresticted so the create
601 * operation itself can then hash the dentry. Finally,
602 * we check for the hashed dentry and return the newly
605 dentry->d_op = &autofs4_root_dentry_operations;
607 dentry->d_fsdata = NULL;
608 d_instantiate(dentry, NULL);
611 spin_lock(&dentry->d_lock);
612 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
613 spin_unlock(&dentry->d_lock);
616 if (dentry->d_op && dentry->d_op->d_revalidate) {
617 mutex_unlock(&dir->i_mutex);
618 (dentry->d_op->d_revalidate)(dentry, nd);
619 mutex_lock(&dir->i_mutex);
623 * If we are still pending, check if we had to handle
624 * a signal. If so we can force a restart..
626 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
627 /* See if we were interrupted */
628 if (signal_pending(current)) {
629 sigset_t *sigset = ¤t->pending.signal;
630 if (sigismember (sigset, SIGKILL) ||
631 sigismember (sigset, SIGQUIT) ||
632 sigismember (sigset, SIGINT)) {
633 return ERR_PTR(-ERESTARTNOINTR);
636 spin_lock(&dentry->d_lock);
637 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
638 spin_unlock(&dentry->d_lock);
642 * If this dentry is unhashed, then we shouldn't honour this
643 * lookup. Returning ENOENT here doesn't do the right thing
644 * for all system calls, but it should be OK for the operations
645 * we permit from an autofs.
647 if (!oz_mode && d_unhashed(dentry)) {
649 * A user space application can (and has done in the past)
650 * remove and re-create this directory during the callback.
651 * This can leave us with an unhashed dentry, but a
652 * successful mount! So we need to perform another
653 * cached lookup in case the dentry now exists.
655 struct dentry *parent = dentry->d_parent;
656 struct dentry *new = d_lookup(parent, &dentry->d_name);
660 dentry = ERR_PTR(-ENOENT);
668 static int autofs4_dir_symlink(struct inode *dir,
669 struct dentry *dentry,
672 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
673 struct autofs_info *ino = autofs4_dentry_ino(dentry);
674 struct autofs_info *p_ino;
678 DPRINTK("%s <- %.*s", symname,
679 dentry->d_name.len, dentry->d_name.name);
681 if (!autofs4_oz_mode(sbi))
684 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
688 ino->size = strlen(symname);
689 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
698 inode = autofs4_get_inode(dir->i_sb, ino);
699 d_add(dentry, inode);
701 if (dir == dir->i_sb->s_root->d_inode)
702 dentry->d_op = &autofs4_root_dentry_operations;
704 dentry->d_op = &autofs4_dentry_operations;
706 dentry->d_fsdata = ino;
707 ino->dentry = dget(dentry);
708 atomic_inc(&ino->count);
709 p_ino = autofs4_dentry_ino(dentry->d_parent);
710 if (p_ino && dentry->d_parent != dentry)
711 atomic_inc(&p_ino->count);
714 dir->i_mtime = CURRENT_TIME;
722 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
723 * that the file no longer exists. However, doing that means that the
724 * VFS layer can turn the dentry into a negative dentry. We don't want
725 * this, because the unlink is probably the result of an expire.
726 * We simply d_drop it and add it to a expiring list in the super block,
727 * which allows the dentry lookup to check for an incomplete expire.
729 * If a process is blocked on the dentry waiting for the expire to finish,
730 * it will invalidate the dentry and try to mount with a new one.
732 * Also see autofs4_dir_rmdir()..
734 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
736 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
737 struct autofs_info *ino = autofs4_dentry_ino(dentry);
738 struct autofs_info *p_ino;
740 /* This allows root to remove symlinks */
741 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
744 if (atomic_dec_and_test(&ino->count)) {
745 p_ino = autofs4_dentry_ino(dentry->d_parent);
746 if (p_ino && dentry->d_parent != dentry)
747 atomic_dec(&p_ino->count);
751 dentry->d_inode->i_size = 0;
752 clear_nlink(dentry->d_inode);
754 dir->i_mtime = CURRENT_TIME;
756 spin_lock(&dcache_lock);
757 spin_lock(&sbi->lookup_lock);
758 list_add(&ino->expiring, &sbi->expiring_list);
759 spin_unlock(&sbi->lookup_lock);
760 spin_lock(&dentry->d_lock);
762 spin_unlock(&dentry->d_lock);
763 spin_unlock(&dcache_lock);
768 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
770 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
771 struct autofs_info *ino = autofs4_dentry_ino(dentry);
772 struct autofs_info *p_ino;
774 DPRINTK("dentry %p, removing %.*s",
775 dentry, dentry->d_name.len, dentry->d_name.name);
777 if (!autofs4_oz_mode(sbi))
780 spin_lock(&dcache_lock);
781 if (!list_empty(&dentry->d_subdirs)) {
782 spin_unlock(&dcache_lock);
785 spin_lock(&sbi->lookup_lock);
786 list_add(&ino->expiring, &sbi->expiring_list);
787 spin_unlock(&sbi->lookup_lock);
788 spin_lock(&dentry->d_lock);
790 spin_unlock(&dentry->d_lock);
791 spin_unlock(&dcache_lock);
793 if (atomic_dec_and_test(&ino->count)) {
794 p_ino = autofs4_dentry_ino(dentry->d_parent);
795 if (p_ino && dentry->d_parent != dentry)
796 atomic_dec(&p_ino->count);
799 dentry->d_inode->i_size = 0;
800 clear_nlink(dentry->d_inode);
808 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
810 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
811 struct autofs_info *ino = autofs4_dentry_ino(dentry);
812 struct autofs_info *p_ino;
815 if (!autofs4_oz_mode(sbi))
818 DPRINTK("dentry %p, creating %.*s",
819 dentry, dentry->d_name.len, dentry->d_name.name);
821 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
825 inode = autofs4_get_inode(dir->i_sb, ino);
826 d_add(dentry, inode);
828 if (dir == dir->i_sb->s_root->d_inode)
829 dentry->d_op = &autofs4_root_dentry_operations;
831 dentry->d_op = &autofs4_dentry_operations;
833 dentry->d_fsdata = ino;
834 ino->dentry = dget(dentry);
835 atomic_inc(&ino->count);
836 p_ino = autofs4_dentry_ino(dentry->d_parent);
837 if (p_ino && dentry->d_parent != dentry)
838 atomic_inc(&p_ino->count);
841 dir->i_mtime = CURRENT_TIME;
846 /* Get/set timeout ioctl() operation */
847 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
848 unsigned long __user *p)
851 unsigned long ntimeout;
853 if ((rv = get_user(ntimeout, p)) ||
854 (rv = put_user(sbi->exp_timeout/HZ, p)))
857 if (ntimeout > ULONG_MAX/HZ)
858 sbi->exp_timeout = 0;
860 sbi->exp_timeout = ntimeout * HZ;
865 /* Return protocol version */
866 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
868 return put_user(sbi->version, p);
871 /* Return protocol sub version */
872 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
874 return put_user(sbi->sub_version, p);
878 * Tells the daemon whether we need to reghost or not. Also, clears
879 * the reghost_needed flag.
881 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
885 DPRINTK("returning %d", sbi->needs_reghost);
887 status = put_user(sbi->needs_reghost, p);
891 sbi->needs_reghost = 0;
896 * Enable / Disable reghosting ioctl() operation
898 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
903 status = get_user(val, p);
905 DPRINTK("reghost = %d", val);
910 /* turn on/off reghosting, with the val */
911 sbi->reghost_enabled = val;
916 * Tells the daemon whether it can umount the autofs mount.
918 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
925 DPRINTK("returning %d", status);
927 status = put_user(status, p);
932 /* Identify autofs4_dentries - this is so we can tell if there's
933 an extra dentry refcount or not. We only hold a refcount on the
934 dentry if its non-negative (ie, d_inode != NULL)
936 int is_autofs4_dentry(struct dentry *dentry)
938 return dentry && dentry->d_inode &&
939 (dentry->d_op == &autofs4_root_dentry_operations ||
940 dentry->d_op == &autofs4_dentry_operations) &&
941 dentry->d_fsdata != NULL;
945 * ioctl()'s on the root directory is the chief method for the daemon to
946 * generate kernel reactions
948 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
949 unsigned int cmd, unsigned long arg)
951 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
952 void __user *p = (void __user *)arg;
954 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
955 cmd,arg,sbi,task_pgrp_nr(current));
957 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
958 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
961 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
965 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
966 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
967 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
968 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
969 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
970 autofs4_catatonic_mode(sbi);
972 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
973 return autofs4_get_protover(sbi, p);
974 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
975 return autofs4_get_protosubver(sbi, p);
976 case AUTOFS_IOC_SETTIMEOUT:
977 return autofs4_get_set_timeout(sbi, p);
979 case AUTOFS_IOC_TOGGLEREGHOST:
980 return autofs4_toggle_reghost(sbi, p);
981 case AUTOFS_IOC_ASKREGHOST:
982 return autofs4_ask_reghost(sbi, p);
984 case AUTOFS_IOC_ASKUMOUNT:
985 return autofs4_ask_umount(filp->f_path.mnt, p);
987 /* return a single thing to expire */
988 case AUTOFS_IOC_EXPIRE:
989 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
990 /* same as above, but can send multiple expires through pipe */
991 case AUTOFS_IOC_EXPIRE_MULTI:
992 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);