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-2003 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>
20 #include <linux/smp_lock.h>
23 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
24 static int autofs4_dir_unlink(struct inode *,struct dentry *);
25 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
26 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
27 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28 static int autofs4_dir_open(struct inode *inode, struct file *file);
29 static int autofs4_dir_close(struct inode *inode, struct file *file);
30 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
32 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
33 static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
35 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 struct file_operations autofs4_dir_operations = {
44 .open = autofs4_dir_open,
45 .release = autofs4_dir_close,
46 .read = generic_read_dir,
47 .readdir = autofs4_dir_readdir,
50 struct inode_operations autofs4_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 struct inode_operations autofs4_dir_inode_operations = {
59 .lookup = autofs4_lookup,
60 .unlink = autofs4_dir_unlink,
61 .symlink = autofs4_dir_symlink,
62 .mkdir = autofs4_dir_mkdir,
63 .rmdir = autofs4_dir_rmdir,
66 static int autofs4_root_readdir(struct file *file, void *dirent,
69 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
70 int oz_mode = autofs4_oz_mode(sbi);
72 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
75 * Don't set reghost flag if:
76 * 1) f_pos is larger than zero -- we've already been here.
77 * 2) we haven't even enabled reghosting in the 1st place.
78 * 3) this is the daemon doing a readdir
80 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
81 sbi->needs_reghost = 1;
83 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
85 return autofs4_dcache_readdir(file, dirent, filldir);
88 /* Update usage from here to top of tree, so that scan of
89 top-level directories will give a useful result */
90 static void autofs4_update_usage(struct vfsmount *mnt, struct dentry *dentry)
92 struct dentry *top = dentry->d_sb->s_root;
94 spin_lock(&dcache_lock);
95 for(; dentry != top; dentry = dentry->d_parent) {
96 struct autofs_info *ino = autofs4_dentry_ino(dentry);
99 touch_atime(mnt, dentry);
100 ino->last_used = jiffies;
103 spin_unlock(&dcache_lock);
107 * From 2.4 kernel readdir.c
109 static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
112 struct dentry *dentry = filp->f_dentry;
117 if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)
123 if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
129 struct list_head *list;
132 spin_lock(&dcache_lock);
133 list = dentry->d_subdirs.next;
136 if (list == &dentry->d_subdirs) {
137 spin_unlock(&dcache_lock);
147 struct dentry *de = list_entry(list,
148 struct dentry, d_u.d_child);
150 if (!d_unhashed(de) && de->d_inode) {
151 spin_unlock(&dcache_lock);
152 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
154 spin_lock(&dcache_lock);
158 if (list != &dentry->d_subdirs)
160 spin_unlock(&dcache_lock);
168 static int autofs4_dir_open(struct inode *inode, struct file *file)
170 struct dentry *dentry = file->f_dentry;
171 struct vfsmount *mnt = file->f_vfsmnt;
172 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
175 DPRINTK("file=%p dentry=%p %.*s",
176 file, dentry, dentry->d_name.len, dentry->d_name.name);
178 if (autofs4_oz_mode(sbi))
181 if (autofs4_ispending(dentry)) {
182 DPRINTK("dentry busy");
186 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
190 /* In case there are stale directory dentrys from a failed mount */
191 spin_lock(&dcache_lock);
192 empty = list_empty(&dentry->d_subdirs);
193 spin_unlock(&dcache_lock);
196 d_invalidate(dentry);
200 nd.flags = LOOKUP_DIRECTORY;
201 status = (dentry->d_op->d_revalidate)(dentry, &nd);
207 if (d_mountpoint(dentry)) {
208 struct file *fp = NULL;
209 struct vfsmount *fp_mnt = mntget(mnt);
210 struct dentry *fp_dentry = dget(dentry);
212 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
218 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
219 status = PTR_ERR(fp);
221 file->private_data = NULL;
224 file->private_data = fp;
230 static int autofs4_dir_close(struct inode *inode, struct file *file)
232 struct dentry *dentry = file->f_dentry;
233 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
235 DPRINTK("file=%p dentry=%p %.*s",
236 file, dentry, dentry->d_name.len, dentry->d_name.name);
238 if (autofs4_oz_mode(sbi))
241 if (autofs4_ispending(dentry)) {
242 DPRINTK("dentry busy");
246 if (d_mountpoint(dentry)) {
247 struct file *fp = file->private_data;
252 filp_close(fp, current->files);
253 file->private_data = NULL;
259 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
261 struct dentry *dentry = file->f_dentry;
262 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
265 DPRINTK("file=%p dentry=%p %.*s",
266 file, dentry, dentry->d_name.len, dentry->d_name.name);
268 if (autofs4_oz_mode(sbi))
271 if (autofs4_ispending(dentry)) {
272 DPRINTK("dentry busy");
276 if (d_mountpoint(dentry)) {
277 struct file *fp = file->private_data;
282 if (!fp->f_op || !fp->f_op->readdir)
285 status = vfs_readdir(fp, filldir, dirent);
286 file->f_pos = fp->f_pos;
288 autofs4_copy_atime(file, fp);
292 return autofs4_dcache_readdir(file, dirent, filldir);
295 static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags)
297 struct super_block *sb = mnt->mnt_sb;
298 struct autofs_sb_info *sbi = autofs4_sbi(sb);
299 struct autofs_info *ino = autofs4_dentry_ino(dentry);
302 /* Block on any pending expiry here; invalidate the dentry
303 when expiration is done to trigger mount request with a new
305 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
306 DPRINTK("waiting for expire %p name=%.*s",
307 dentry, dentry->d_name.len, dentry->d_name.name);
309 status = autofs4_wait(sbi, dentry, NFY_NONE);
311 DPRINTK("expire done status=%d", status);
314 * If the directory still exists the mount request must
315 * continue otherwise it can't be followed at the right
316 * time during the walk.
318 status = d_invalidate(dentry);
319 if (status != -EBUSY)
323 DPRINTK("dentry=%p %.*s ino=%p",
324 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
327 * Wait for a pending mount, triggering one if there
330 if (dentry->d_inode == NULL) {
331 DPRINTK("waiting for mount name=%.*s",
332 dentry->d_name.len, dentry->d_name.name);
334 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
336 DPRINTK("mount done status=%d", status);
338 if (status && dentry->d_inode)
339 return 0; /* Try to get the kernel to invalidate this dentry */
341 /* Turn this into a real negative dentry? */
342 if (status == -ENOENT) {
343 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
344 spin_lock(&dentry->d_lock);
345 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
346 spin_unlock(&dentry->d_lock);
349 /* Return a negative dentry, but leave it "pending" */
352 /* Trigger mount for path component or follow link */
353 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
354 current->link_count) {
355 DPRINTK("waiting for mount name=%.*s",
356 dentry->d_name.len, dentry->d_name.name);
358 spin_lock(&dentry->d_lock);
359 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
360 spin_unlock(&dentry->d_lock);
361 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
363 DPRINTK("mount done status=%d", status);
366 spin_lock(&dentry->d_lock);
367 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
368 spin_unlock(&dentry->d_lock);
374 * We don't update the usages for the autofs daemon itself, this
375 * is necessary for recursive autofs mounts
377 if (!autofs4_oz_mode(sbi))
378 autofs4_update_usage(mnt, dentry);
380 spin_lock(&dentry->d_lock);
381 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
382 spin_unlock(&dentry->d_lock);
387 * Revalidate is called on every cache lookup. Some of those
388 * cache lookups may actually happen while the dentry is not
389 * yet completely filled in, and revalidate has to delay such
392 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
394 struct inode *dir = dentry->d_parent->d_inode;
395 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
396 int oz_mode = autofs4_oz_mode(sbi);
397 int flags = nd ? nd->flags : 0;
401 if (autofs4_ispending(dentry)) {
403 status = try_to_fill_dentry(nd->mnt, dentry, flags);
407 /* Negative dentry.. invalidate if "old" */
408 if (dentry->d_inode == NULL)
409 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
411 /* Check for a non-mountpoint directory with no contents */
412 spin_lock(&dcache_lock);
413 if (S_ISDIR(dentry->d_inode->i_mode) &&
414 !d_mountpoint(dentry) &&
415 list_empty(&dentry->d_subdirs)) {
416 DPRINTK("dentry=%p %.*s, emptydir",
417 dentry, dentry->d_name.len, dentry->d_name.name);
418 spin_unlock(&dcache_lock);
420 status = try_to_fill_dentry(nd->mnt, dentry, flags);
423 spin_unlock(&dcache_lock);
425 /* Update the usage list */
427 autofs4_update_usage(nd->mnt, dentry);
432 static void autofs4_dentry_release(struct dentry *de)
434 struct autofs_info *inf;
436 DPRINTK("releasing %p", de);
438 inf = autofs4_dentry_ino(de);
445 autofs4_free_ino(inf);
449 /* For dentries of directories in the root dir */
450 static struct dentry_operations autofs4_root_dentry_operations = {
451 .d_revalidate = autofs4_revalidate,
452 .d_release = autofs4_dentry_release,
455 /* For other dentries */
456 static struct dentry_operations autofs4_dentry_operations = {
457 .d_revalidate = autofs4_revalidate,
458 .d_release = autofs4_dentry_release,
461 /* Lookups in the root directory */
462 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
464 struct autofs_sb_info *sbi;
467 DPRINTK("name = %.*s",
468 dentry->d_name.len, dentry->d_name.name);
470 /* File name too long to exist */
471 if (dentry->d_name.len > NAME_MAX)
472 return ERR_PTR(-ENAMETOOLONG);
474 sbi = autofs4_sbi(dir->i_sb);
475 oz_mode = autofs4_oz_mode(sbi);
477 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
478 current->pid, process_group(current), sbi->catatonic, oz_mode);
481 * Mark the dentry incomplete, but add it. This is needed so
482 * that the VFS layer knows about the dentry, and we can count
483 * on catching any lookups through the revalidate.
485 * Let all the hard work be done by the revalidate function that
486 * needs to be able to do this anyway..
488 * We need to do this before we release the directory semaphore.
490 dentry->d_op = &autofs4_root_dentry_operations;
493 spin_lock(&dentry->d_lock);
494 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
495 spin_unlock(&dentry->d_lock);
497 dentry->d_fsdata = NULL;
500 if (dentry->d_op && dentry->d_op->d_revalidate) {
501 mutex_unlock(&dir->i_mutex);
502 (dentry->d_op->d_revalidate)(dentry, nd);
503 mutex_lock(&dir->i_mutex);
507 * If we are still pending, check if we had to handle
508 * a signal. If so we can force a restart..
510 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
511 /* See if we were interrupted */
512 if (signal_pending(current)) {
513 sigset_t *sigset = ¤t->pending.signal;
514 if (sigismember (sigset, SIGKILL) ||
515 sigismember (sigset, SIGQUIT) ||
516 sigismember (sigset, SIGINT)) {
517 return ERR_PTR(-ERESTARTNOINTR);
523 * If this dentry is unhashed, then we shouldn't honour this
524 * lookup even if the dentry is positive. Returning ENOENT here
525 * doesn't do the right thing for all system calls, but it should
526 * be OK for the operations we permit from an autofs.
528 if (dentry->d_inode && d_unhashed(dentry))
529 return ERR_PTR(-ENOENT);
534 static int autofs4_dir_symlink(struct inode *dir,
535 struct dentry *dentry,
538 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
539 struct autofs_info *ino = autofs4_dentry_ino(dentry);
543 DPRINTK("%s <- %.*s", symname,
544 dentry->d_name.len, dentry->d_name.name);
546 if (!autofs4_oz_mode(sbi))
549 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
553 ino->size = strlen(symname);
554 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
563 inode = autofs4_get_inode(dir->i_sb, ino);
564 d_instantiate(dentry, inode);
566 if (dir == dir->i_sb->s_root->d_inode)
567 dentry->d_op = &autofs4_root_dentry_operations;
569 dentry->d_op = &autofs4_dentry_operations;
571 dentry->d_fsdata = ino;
572 ino->dentry = dget(dentry);
575 dir->i_mtime = CURRENT_TIME;
583 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
584 * that the file no longer exists. However, doing that means that the
585 * VFS layer can turn the dentry into a negative dentry. We don't want
586 * this, because since the unlink is probably the result of an expire.
587 * We simply d_drop it, which allows the dentry lookup to remount it
590 * If a process is blocked on the dentry waiting for the expire to finish,
591 * it will invalidate the dentry and try to mount with a new one.
593 * Also see autofs4_dir_rmdir()..
595 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
597 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
598 struct autofs_info *ino = autofs4_dentry_ino(dentry);
600 /* This allows root to remove symlinks */
601 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
606 dentry->d_inode->i_size = 0;
607 dentry->d_inode->i_nlink = 0;
609 dir->i_mtime = CURRENT_TIME;
616 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
618 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
619 struct autofs_info *ino = autofs4_dentry_ino(dentry);
621 if (!autofs4_oz_mode(sbi))
624 spin_lock(&dcache_lock);
625 if (!list_empty(&dentry->d_subdirs)) {
626 spin_unlock(&dcache_lock);
629 spin_lock(&dentry->d_lock);
631 spin_unlock(&dentry->d_lock);
632 spin_unlock(&dcache_lock);
636 dentry->d_inode->i_size = 0;
637 dentry->d_inode->i_nlink = 0;
645 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
647 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
648 struct autofs_info *ino = autofs4_dentry_ino(dentry);
651 if ( !autofs4_oz_mode(sbi) )
654 DPRINTK("dentry %p, creating %.*s",
655 dentry, dentry->d_name.len, dentry->d_name.name);
657 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
661 inode = autofs4_get_inode(dir->i_sb, ino);
662 d_instantiate(dentry, inode);
664 if (dir == dir->i_sb->s_root->d_inode)
665 dentry->d_op = &autofs4_root_dentry_operations;
667 dentry->d_op = &autofs4_dentry_operations;
669 dentry->d_fsdata = ino;
670 ino->dentry = dget(dentry);
673 dir->i_mtime = CURRENT_TIME;
678 /* Get/set timeout ioctl() operation */
679 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
680 unsigned long __user *p)
683 unsigned long ntimeout;
685 if ( (rv = get_user(ntimeout, p)) ||
686 (rv = put_user(sbi->exp_timeout/HZ, p)) )
689 if ( ntimeout > ULONG_MAX/HZ )
690 sbi->exp_timeout = 0;
692 sbi->exp_timeout = ntimeout * HZ;
697 /* Return protocol version */
698 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
700 return put_user(sbi->version, p);
703 /* Return protocol sub version */
704 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
706 return put_user(sbi->sub_version, p);
710 * Tells the daemon whether we need to reghost or not. Also, clears
711 * the reghost_needed flag.
713 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
717 DPRINTK("returning %d", sbi->needs_reghost);
719 status = put_user(sbi->needs_reghost, p);
723 sbi->needs_reghost = 0;
728 * Enable / Disable reghosting ioctl() operation
730 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
735 status = get_user(val, p);
737 DPRINTK("reghost = %d", val);
742 /* turn on/off reghosting, with the val */
743 sbi->reghost_enabled = val;
748 * Tells the daemon whether it can umount the autofs mount.
750 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
754 if (may_umount(mnt) == 0)
757 DPRINTK("returning %d", status);
759 status = put_user(status, p);
764 /* Identify autofs4_dentries - this is so we can tell if there's
765 an extra dentry refcount or not. We only hold a refcount on the
766 dentry if its non-negative (ie, d_inode != NULL)
768 int is_autofs4_dentry(struct dentry *dentry)
770 return dentry && dentry->d_inode &&
771 (dentry->d_op == &autofs4_root_dentry_operations ||
772 dentry->d_op == &autofs4_dentry_operations) &&
773 dentry->d_fsdata != NULL;
777 * ioctl()'s on the root directory is the chief method for the daemon to
778 * generate kernel reactions
780 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
781 unsigned int cmd, unsigned long arg)
783 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
784 void __user *p = (void __user *)arg;
786 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
787 cmd,arg,sbi,process_group(current));
789 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
790 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
793 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
797 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
798 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
799 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
800 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
801 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
802 autofs4_catatonic_mode(sbi);
804 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
805 return autofs4_get_protover(sbi, p);
806 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
807 return autofs4_get_protosubver(sbi, p);
808 case AUTOFS_IOC_SETTIMEOUT:
809 return autofs4_get_set_timeout(sbi, p);
811 case AUTOFS_IOC_TOGGLEREGHOST:
812 return autofs4_toggle_reghost(sbi, p);
813 case AUTOFS_IOC_ASKREGHOST:
814 return autofs4_ask_reghost(sbi, p);
816 case AUTOFS_IOC_ASKUMOUNT:
817 return autofs4_ask_umount(filp->f_vfsmnt, p);
819 /* return a single thing to expire */
820 case AUTOFS_IOC_EXPIRE:
821 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
822 /* same as above, but can send multiple expires through pipe */
823 case AUTOFS_IOC_EXPIRE_MULTI:
824 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);