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);
198 nd.flags = LOOKUP_DIRECTORY;
199 status = (dentry->d_op->d_revalidate)(dentry, &nd);
205 if (d_mountpoint(dentry)) {
206 struct file *fp = NULL;
207 struct vfsmount *fp_mnt = mntget(mnt);
208 struct dentry *fp_dentry = dget(dentry);
210 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
216 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
217 status = PTR_ERR(fp);
219 file->private_data = NULL;
222 file->private_data = fp;
228 static int autofs4_dir_close(struct inode *inode, struct file *file)
230 struct dentry *dentry = file->f_dentry;
231 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
233 DPRINTK("file=%p dentry=%p %.*s",
234 file, dentry, dentry->d_name.len, dentry->d_name.name);
236 if (autofs4_oz_mode(sbi))
239 if (autofs4_ispending(dentry)) {
240 DPRINTK("dentry busy");
244 if (d_mountpoint(dentry)) {
245 struct file *fp = file->private_data;
250 filp_close(fp, current->files);
251 file->private_data = NULL;
257 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
259 struct dentry *dentry = file->f_dentry;
260 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
263 DPRINTK("file=%p dentry=%p %.*s",
264 file, dentry, dentry->d_name.len, dentry->d_name.name);
266 if (autofs4_oz_mode(sbi))
269 if (autofs4_ispending(dentry)) {
270 DPRINTK("dentry busy");
274 if (d_mountpoint(dentry)) {
275 struct file *fp = file->private_data;
280 if (!fp->f_op || !fp->f_op->readdir)
283 status = vfs_readdir(fp, filldir, dirent);
284 file->f_pos = fp->f_pos;
286 autofs4_copy_atime(file, fp);
290 return autofs4_dcache_readdir(file, dirent, filldir);
293 static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags)
295 struct super_block *sb = mnt->mnt_sb;
296 struct autofs_sb_info *sbi = autofs4_sbi(sb);
297 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
300 /* Block on any pending expiry here; invalidate the dentry
301 when expiration is done to trigger mount request with a new
303 if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
304 DPRINTK("waiting for expire %p name=%.*s",
305 dentry, dentry->d_name.len, dentry->d_name.name);
307 status = autofs4_wait(sbi, dentry, NFY_NONE);
309 DPRINTK("expire done status=%d", status);
312 * If the directory still exists the mount request must
313 * continue otherwise it can't be followed at the right
314 * time during the walk.
316 status = d_invalidate(dentry);
317 if (status != -EBUSY)
321 DPRINTK("dentry=%p %.*s ino=%p",
322 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
324 /* Wait for a pending mount, triggering one if there isn't one already */
325 if (dentry->d_inode == NULL) {
326 DPRINTK("waiting for mount name=%.*s",
327 dentry->d_name.len, dentry->d_name.name);
329 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
331 DPRINTK("mount done status=%d", status);
333 if (status && dentry->d_inode)
334 return 0; /* Try to get the kernel to invalidate this dentry */
336 /* Turn this into a real negative dentry? */
337 if (status == -ENOENT) {
338 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
339 spin_lock(&dentry->d_lock);
340 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
341 spin_unlock(&dentry->d_lock);
344 /* Return a negative dentry, but leave it "pending" */
347 /* Trigger mount for path component or follow link */
348 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
349 current->link_count) {
350 DPRINTK("waiting for mount name=%.*s",
351 dentry->d_name.len, dentry->d_name.name);
353 spin_lock(&dentry->d_lock);
354 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
355 spin_unlock(&dentry->d_lock);
356 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
358 DPRINTK("mount done status=%d", status);
361 spin_lock(&dentry->d_lock);
362 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
363 spin_unlock(&dentry->d_lock);
368 /* We don't update the usages for the autofs daemon itself, this
369 is necessary for recursive autofs mounts */
370 if (!autofs4_oz_mode(sbi))
371 autofs4_update_usage(mnt, dentry);
373 spin_lock(&dentry->d_lock);
374 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
375 spin_unlock(&dentry->d_lock);
380 * Revalidate is called on every cache lookup. Some of those
381 * cache lookups may actually happen while the dentry is not
382 * yet completely filled in, and revalidate has to delay such
385 static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
387 struct inode * dir = dentry->d_parent->d_inode;
388 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
389 int oz_mode = autofs4_oz_mode(sbi);
390 int flags = nd ? nd->flags : 0;
394 if (autofs4_ispending(dentry)) {
396 status = try_to_fill_dentry(nd->mnt, dentry, flags);
400 /* Negative dentry.. invalidate if "old" */
401 if (dentry->d_inode == NULL)
402 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
404 /* Check for a non-mountpoint directory with no contents */
405 spin_lock(&dcache_lock);
406 if (S_ISDIR(dentry->d_inode->i_mode) &&
407 !d_mountpoint(dentry) &&
408 list_empty(&dentry->d_subdirs)) {
409 DPRINTK("dentry=%p %.*s, emptydir",
410 dentry, dentry->d_name.len, dentry->d_name.name);
411 spin_unlock(&dcache_lock);
413 status = try_to_fill_dentry(nd->mnt, dentry, flags);
416 spin_unlock(&dcache_lock);
418 /* Update the usage list */
420 autofs4_update_usage(nd->mnt, dentry);
425 static void autofs4_dentry_release(struct dentry *de)
427 struct autofs_info *inf;
429 DPRINTK("releasing %p", de);
431 inf = autofs4_dentry_ino(de);
438 autofs4_free_ino(inf);
442 /* For dentries of directories in the root dir */
443 static struct dentry_operations autofs4_root_dentry_operations = {
444 .d_revalidate = autofs4_revalidate,
445 .d_release = autofs4_dentry_release,
448 /* For other dentries */
449 static struct dentry_operations autofs4_dentry_operations = {
450 .d_revalidate = autofs4_revalidate,
451 .d_release = autofs4_dentry_release,
454 /* Lookups in the root directory */
455 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
457 struct autofs_sb_info *sbi;
460 DPRINTK("name = %.*s",
461 dentry->d_name.len, dentry->d_name.name);
463 if (dentry->d_name.len > NAME_MAX)
464 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
466 sbi = autofs4_sbi(dir->i_sb);
468 oz_mode = autofs4_oz_mode(sbi);
469 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
470 current->pid, process_group(current), sbi->catatonic, oz_mode);
473 * Mark the dentry incomplete, but add it. This is needed so
474 * that the VFS layer knows about the dentry, and we can count
475 * on catching any lookups through the revalidate.
477 * Let all the hard work be done by the revalidate function that
478 * needs to be able to do this anyway..
480 * We need to do this before we release the directory semaphore.
482 dentry->d_op = &autofs4_root_dentry_operations;
485 spin_lock(&dentry->d_lock);
486 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
487 spin_unlock(&dentry->d_lock);
489 dentry->d_fsdata = NULL;
492 if (dentry->d_op && dentry->d_op->d_revalidate) {
493 mutex_unlock(&dir->i_mutex);
494 (dentry->d_op->d_revalidate)(dentry, nd);
495 mutex_lock(&dir->i_mutex);
499 * If we are still pending, check if we had to handle
500 * a signal. If so we can force a restart..
502 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
503 /* See if we were interrupted */
504 if (signal_pending(current)) {
505 sigset_t *sigset = ¤t->pending.signal;
506 if (sigismember (sigset, SIGKILL) ||
507 sigismember (sigset, SIGQUIT) ||
508 sigismember (sigset, SIGINT)) {
509 return ERR_PTR(-ERESTARTNOINTR);
515 * If this dentry is unhashed, then we shouldn't honour this
516 * lookup even if the dentry is positive. Returning ENOENT here
517 * doesn't do the right thing for all system calls, but it should
518 * be OK for the operations we permit from an autofs.
520 if ( dentry->d_inode && d_unhashed(dentry) )
521 return ERR_PTR(-ENOENT);
526 static int autofs4_dir_symlink(struct inode *dir,
527 struct dentry *dentry,
530 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
531 struct autofs_info *ino = autofs4_dentry_ino(dentry);
535 DPRINTK("%s <- %.*s", symname,
536 dentry->d_name.len, dentry->d_name.name);
538 if (!autofs4_oz_mode(sbi))
541 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
545 ino->size = strlen(symname);
546 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
555 inode = autofs4_get_inode(dir->i_sb, ino);
556 d_instantiate(dentry, inode);
558 if (dir == dir->i_sb->s_root->d_inode)
559 dentry->d_op = &autofs4_root_dentry_operations;
561 dentry->d_op = &autofs4_dentry_operations;
563 dentry->d_fsdata = ino;
564 ino->dentry = dget(dentry);
567 dir->i_mtime = CURRENT_TIME;
575 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
576 * that the file no longer exists. However, doing that means that the
577 * VFS layer can turn the dentry into a negative dentry. We don't want
578 * this, because since the unlink is probably the result of an expire.
579 * We simply d_drop it, which allows the dentry lookup to remount it
582 * If a process is blocked on the dentry waiting for the expire to finish,
583 * it will invalidate the dentry and try to mount with a new one.
585 * Also see autofs4_dir_rmdir()..
587 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
589 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
590 struct autofs_info *ino = autofs4_dentry_ino(dentry);
592 /* This allows root to remove symlinks */
593 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
598 dentry->d_inode->i_size = 0;
599 dentry->d_inode->i_nlink = 0;
601 dir->i_mtime = CURRENT_TIME;
608 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
610 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
611 struct autofs_info *ino = autofs4_dentry_ino(dentry);
613 if (!autofs4_oz_mode(sbi))
616 spin_lock(&dcache_lock);
617 if (!list_empty(&dentry->d_subdirs)) {
618 spin_unlock(&dcache_lock);
621 spin_lock(&dentry->d_lock);
623 spin_unlock(&dentry->d_lock);
624 spin_unlock(&dcache_lock);
628 dentry->d_inode->i_size = 0;
629 dentry->d_inode->i_nlink = 0;
637 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
639 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
640 struct autofs_info *ino = autofs4_dentry_ino(dentry);
643 if ( !autofs4_oz_mode(sbi) )
646 DPRINTK("dentry %p, creating %.*s",
647 dentry, dentry->d_name.len, dentry->d_name.name);
649 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
653 inode = autofs4_get_inode(dir->i_sb, ino);
654 d_instantiate(dentry, inode);
656 if (dir == dir->i_sb->s_root->d_inode)
657 dentry->d_op = &autofs4_root_dentry_operations;
659 dentry->d_op = &autofs4_dentry_operations;
661 dentry->d_fsdata = ino;
662 ino->dentry = dget(dentry);
665 dir->i_mtime = CURRENT_TIME;
670 /* Get/set timeout ioctl() operation */
671 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
672 unsigned long __user *p)
675 unsigned long ntimeout;
677 if ( (rv = get_user(ntimeout, p)) ||
678 (rv = put_user(sbi->exp_timeout/HZ, p)) )
681 if ( ntimeout > ULONG_MAX/HZ )
682 sbi->exp_timeout = 0;
684 sbi->exp_timeout = ntimeout * HZ;
689 /* Return protocol version */
690 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
692 return put_user(sbi->version, p);
695 /* Return protocol sub version */
696 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
698 return put_user(sbi->sub_version, p);
702 * Tells the daemon whether we need to reghost or not. Also, clears
703 * the reghost_needed flag.
705 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
709 DPRINTK("returning %d", sbi->needs_reghost);
711 status = put_user(sbi->needs_reghost, p);
715 sbi->needs_reghost = 0;
720 * Enable / Disable reghosting ioctl() operation
722 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
727 status = get_user(val, p);
729 DPRINTK("reghost = %d", val);
734 /* turn on/off reghosting, with the val */
735 sbi->reghost_enabled = val;
740 * Tells the daemon whether it can umount the autofs mount.
742 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
746 if (may_umount(mnt) == 0)
749 DPRINTK("returning %d", status);
751 status = put_user(status, p);
756 /* Identify autofs4_dentries - this is so we can tell if there's
757 an extra dentry refcount or not. We only hold a refcount on the
758 dentry if its non-negative (ie, d_inode != NULL)
760 int is_autofs4_dentry(struct dentry *dentry)
762 return dentry && dentry->d_inode &&
763 (dentry->d_op == &autofs4_root_dentry_operations ||
764 dentry->d_op == &autofs4_dentry_operations) &&
765 dentry->d_fsdata != NULL;
769 * ioctl()'s on the root directory is the chief method for the daemon to
770 * generate kernel reactions
772 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
773 unsigned int cmd, unsigned long arg)
775 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
776 void __user *p = (void __user *)arg;
778 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
779 cmd,arg,sbi,process_group(current));
781 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
782 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
785 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
789 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
790 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
791 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
792 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
793 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
794 autofs4_catatonic_mode(sbi);
796 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
797 return autofs4_get_protover(sbi, p);
798 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
799 return autofs4_get_protosubver(sbi, p);
800 case AUTOFS_IOC_SETTIMEOUT:
801 return autofs4_get_set_timeout(sbi, p);
803 case AUTOFS_IOC_TOGGLEREGHOST:
804 return autofs4_toggle_reghost(sbi, p);
805 case AUTOFS_IOC_ASKREGHOST:
806 return autofs4_ask_reghost(sbi, p);
808 case AUTOFS_IOC_ASKUMOUNT:
809 return autofs4_ask_umount(filp->f_vfsmnt, p);
811 /* return a single thing to expire */
812 case AUTOFS_IOC_EXPIRE:
813 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
814 /* same as above, but can send multiple expires through pipe */
815 case AUTOFS_IOC_EXPIRE_MULTI:
816 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);