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/errno.h>
16 #include <linux/stat.h>
17 #include <linux/param.h>
18 #include <linux/time.h>
19 #include <linux/smp_lock.h>
22 static struct dentry *autofs4_dir_lookup(struct inode *,struct dentry *, struct nameidata *);
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_root_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_root_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_dir_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 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 update_atime(dentry->d_inode);
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, struct dentry, d_child);
149 if (!d_unhashed(de) && de->d_inode) {
150 spin_unlock(&dcache_lock);
151 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
153 spin_lock(&dcache_lock);
157 if (list != &dentry->d_subdirs)
159 spin_unlock(&dcache_lock);
167 static int autofs4_dir_open(struct inode *inode, struct file *file)
169 struct dentry *dentry = file->f_dentry;
170 struct vfsmount *mnt = file->f_vfsmnt;
171 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
174 DPRINTK("file=%p dentry=%p %.*s",
175 file, dentry, dentry->d_name.len, dentry->d_name.name);
177 if (autofs4_oz_mode(sbi))
180 if (autofs4_ispending(dentry)) {
181 DPRINTK("dentry busy");
185 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
189 /* In case there are stale directory dentrys from a failed mount */
190 spin_lock(&dcache_lock);
191 empty = list_empty(&dentry->d_subdirs);
192 spin_unlock(&dcache_lock);
195 d_invalidate(dentry);
197 nd.flags = LOOKUP_DIRECTORY;
198 status = (dentry->d_op->d_revalidate)(dentry, &nd);
204 if (d_mountpoint(dentry)) {
205 struct file *fp = NULL;
206 struct vfsmount *fp_mnt = mntget(mnt);
207 struct dentry *fp_dentry = dget(dentry);
209 while (follow_down(&fp_mnt, &fp_dentry) && d_mountpoint(fp_dentry));
211 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
212 status = PTR_ERR(fp);
214 file->private_data = NULL;
217 file->private_data = fp;
223 static int autofs4_dir_close(struct inode *inode, struct file *file)
225 struct dentry *dentry = file->f_dentry;
226 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
228 DPRINTK("file=%p dentry=%p %.*s",
229 file, dentry, dentry->d_name.len, dentry->d_name.name);
231 if (autofs4_oz_mode(sbi))
234 if (autofs4_ispending(dentry)) {
235 DPRINTK("dentry busy");
239 if (d_mountpoint(dentry)) {
240 struct file *fp = file->private_data;
245 filp_close(fp, current->files);
246 file->private_data = NULL;
252 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
254 struct dentry *dentry = file->f_dentry;
255 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
258 DPRINTK("file=%p dentry=%p %.*s",
259 file, dentry, dentry->d_name.len, dentry->d_name.name);
261 if (autofs4_oz_mode(sbi))
264 if (autofs4_ispending(dentry)) {
265 DPRINTK("dentry busy");
269 if (d_mountpoint(dentry)) {
270 struct file *fp = file->private_data;
275 if (!fp->f_op || !fp->f_op->readdir)
278 status = vfs_readdir(fp, filldir, dirent);
279 file->f_pos = fp->f_pos;
281 autofs4_copy_atime(file, fp);
285 return autofs4_dcache_readdir(file, dirent, filldir);
288 static int try_to_fill_dentry(struct dentry *dentry,
289 struct super_block *sb,
290 struct autofs_sb_info *sbi, int flags)
292 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
295 /* Block on any pending expiry here; invalidate the dentry
296 when expiration is done to trigger mount request with a new
298 if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
299 DPRINTK("waiting for expire %p name=%.*s",
300 dentry, dentry->d_name.len, dentry->d_name.name);
302 status = autofs4_wait(sbi, dentry, NFY_NONE);
304 DPRINTK("expire done status=%d", status);
309 DPRINTK("dentry=%p %.*s ino=%p",
310 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
312 /* Wait for a pending mount, triggering one if there isn't one already */
313 if (dentry->d_inode == NULL) {
314 DPRINTK("waiting for mount name=%.*s",
315 dentry->d_name.len, dentry->d_name.name);
317 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
319 DPRINTK("mount done status=%d", status);
321 if (status && dentry->d_inode)
322 return 0; /* Try to get the kernel to invalidate this dentry */
324 /* Turn this into a real negative dentry? */
325 if (status == -ENOENT) {
326 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
327 spin_lock(&dentry->d_lock);
328 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
329 spin_unlock(&dentry->d_lock);
332 /* Return a negative dentry, but leave it "pending" */
335 /* Trigger mount for path component or follow link */
336 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
337 current->link_count) {
338 DPRINTK("waiting for mount name=%.*s",
339 dentry->d_name.len, dentry->d_name.name);
341 spin_lock(&dentry->d_lock);
342 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
343 spin_unlock(&dentry->d_lock);
344 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
346 DPRINTK("mount done status=%d", status);
349 spin_lock(&dentry->d_lock);
350 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
351 spin_unlock(&dentry->d_lock);
356 /* We don't update the usages for the autofs daemon itself, this
357 is necessary for recursive autofs mounts */
358 if (!autofs4_oz_mode(sbi))
359 autofs4_update_usage(dentry);
361 spin_lock(&dentry->d_lock);
362 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
363 spin_unlock(&dentry->d_lock);
368 * Revalidate is called on every cache lookup. Some of those
369 * cache lookups may actually happen while the dentry is not
370 * yet completely filled in, and revalidate has to delay such
373 static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
375 struct inode * dir = dentry->d_parent->d_inode;
376 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
377 int oz_mode = autofs4_oz_mode(sbi);
378 int flags = nd ? nd->flags : 0;
382 if (autofs4_ispending(dentry)) {
384 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
388 /* Negative dentry.. invalidate if "old" */
389 if (dentry->d_inode == NULL)
390 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
392 /* Check for a non-mountpoint directory with no contents */
393 spin_lock(&dcache_lock);
394 if (S_ISDIR(dentry->d_inode->i_mode) &&
395 !d_mountpoint(dentry) &&
396 list_empty(&dentry->d_subdirs)) {
397 DPRINTK("dentry=%p %.*s, emptydir",
398 dentry, dentry->d_name.len, dentry->d_name.name);
399 spin_unlock(&dcache_lock);
401 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
404 spin_unlock(&dcache_lock);
406 /* Update the usage list */
408 autofs4_update_usage(dentry);
413 static void autofs4_dentry_release(struct dentry *de)
415 struct autofs_info *inf;
417 DPRINTK("releasing %p", de);
419 inf = autofs4_dentry_ino(de);
426 autofs4_free_ino(inf);
430 /* For dentries of directories in the root dir */
431 static struct dentry_operations autofs4_root_dentry_operations = {
432 .d_revalidate = autofs4_revalidate,
433 .d_release = autofs4_dentry_release,
436 /* For other dentries */
437 static struct dentry_operations autofs4_dentry_operations = {
438 .d_revalidate = autofs4_revalidate,
439 .d_release = autofs4_dentry_release,
442 /* Lookups in non-root dirs never find anything - if it's there, it's
443 already in the dcache */
444 static struct dentry *autofs4_dir_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
447 DPRINTK("ignoring lookup of %.*s/%.*s",
448 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
449 dentry->d_name.len, dentry->d_name.name);
452 dentry->d_fsdata = NULL;
457 /* Lookups in the root directory */
458 static struct dentry *autofs4_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
460 struct autofs_sb_info *sbi;
463 DPRINTK("name = %.*s",
464 dentry->d_name.len, dentry->d_name.name);
466 if (dentry->d_name.len > NAME_MAX)
467 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
469 sbi = autofs4_sbi(dir->i_sb);
471 oz_mode = autofs4_oz_mode(sbi);
472 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
473 current->pid, process_group(current), sbi->catatonic, oz_mode);
476 * Mark the dentry incomplete, but add it. This is needed so
477 * that the VFS layer knows about the dentry, and we can count
478 * on catching any lookups through the revalidate.
480 * Let all the hard work be done by the revalidate function that
481 * needs to be able to do this anyway..
483 * We need to do this before we release the directory semaphore.
485 dentry->d_op = &autofs4_root_dentry_operations;
488 spin_lock(&dentry->d_lock);
489 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
490 spin_unlock(&dentry->d_lock);
492 dentry->d_fsdata = NULL;
495 if (dentry->d_op && dentry->d_op->d_revalidate) {
497 (dentry->d_op->d_revalidate)(dentry, nd);
502 * If we are still pending, check if we had to handle
503 * a signal. If so we can force a restart..
505 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
506 /* See if we were interrupted */
507 if (signal_pending(current)) {
508 sigset_t *sigset = ¤t->pending.signal;
509 if (sigismember (sigset, SIGKILL) ||
510 sigismember (sigset, SIGQUIT) ||
511 sigismember (sigset, SIGINT)) {
512 return ERR_PTR(-ERESTARTNOINTR);
518 * If this dentry is unhashed, then we shouldn't honour this
519 * lookup even if the dentry is positive. Returning ENOENT here
520 * doesn't do the right thing for all system calls, but it should
521 * be OK for the operations we permit from an autofs.
523 if ( dentry->d_inode && d_unhashed(dentry) )
524 return ERR_PTR(-ENOENT);
529 static int autofs4_dir_symlink(struct inode *dir,
530 struct dentry *dentry,
533 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
534 struct autofs_info *ino = autofs4_dentry_ino(dentry);
538 DPRINTK("%s <- %.*s", symname,
539 dentry->d_name.len, dentry->d_name.name);
541 if (!autofs4_oz_mode(sbi))
544 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
548 ino->size = strlen(symname);
549 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
558 inode = autofs4_get_inode(dir->i_sb, ino);
559 d_instantiate(dentry, inode);
561 if (dir == dir->i_sb->s_root->d_inode)
562 dentry->d_op = &autofs4_root_dentry_operations;
564 dentry->d_op = &autofs4_dentry_operations;
566 dentry->d_fsdata = ino;
567 ino->dentry = dget(dentry);
570 dir->i_mtime = CURRENT_TIME;
578 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
579 * that the file no longer exists. However, doing that means that the
580 * VFS layer can turn the dentry into a negative dentry. We don't want
581 * this, because since the unlink is probably the result of an expire.
582 * We simply d_drop it, which allows the dentry lookup to remount it
585 * If a process is blocked on the dentry waiting for the expire to finish,
586 * it will invalidate the dentry and try to mount with a new one.
588 * Also see autofs4_dir_rmdir()..
590 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
592 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
593 struct autofs_info *ino = autofs4_dentry_ino(dentry);
595 /* This allows root to remove symlinks */
596 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
601 dentry->d_inode->i_size = 0;
602 dentry->d_inode->i_nlink = 0;
604 dir->i_mtime = CURRENT_TIME;
611 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
613 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
614 struct autofs_info *ino = autofs4_dentry_ino(dentry);
616 if (!autofs4_oz_mode(sbi))
619 spin_lock(&dcache_lock);
620 if (!list_empty(&dentry->d_subdirs)) {
621 spin_unlock(&dcache_lock);
625 spin_unlock(&dcache_lock);
629 dentry->d_inode->i_size = 0;
630 dentry->d_inode->i_nlink = 0;
638 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
640 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
641 struct autofs_info *ino = autofs4_dentry_ino(dentry);
644 if ( !autofs4_oz_mode(sbi) )
647 DPRINTK("dentry %p, creating %.*s",
648 dentry, dentry->d_name.len, dentry->d_name.name);
650 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
654 inode = autofs4_get_inode(dir->i_sb, ino);
655 d_instantiate(dentry, inode);
657 if (dir == dir->i_sb->s_root->d_inode)
658 dentry->d_op = &autofs4_root_dentry_operations;
660 dentry->d_op = &autofs4_dentry_operations;
662 dentry->d_fsdata = ino;
663 ino->dentry = dget(dentry);
666 dir->i_mtime = CURRENT_TIME;
671 /* Get/set timeout ioctl() operation */
672 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
673 unsigned long __user *p)
676 unsigned long ntimeout;
678 if ( (rv = get_user(ntimeout, p)) ||
679 (rv = put_user(sbi->exp_timeout/HZ, p)) )
682 if ( ntimeout > ULONG_MAX/HZ )
683 sbi->exp_timeout = 0;
685 sbi->exp_timeout = ntimeout * HZ;
690 /* Return protocol version */
691 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
693 return put_user(sbi->version, p);
696 /* Return protocol sub version */
697 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
699 return put_user(sbi->sub_version, p);
703 * Tells the daemon whether we need to reghost or not. Also, clears
704 * the reghost_needed flag.
706 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
710 DPRINTK("returning %d", sbi->needs_reghost);
712 status = put_user(sbi->needs_reghost, p);
716 sbi->needs_reghost = 0;
721 * Enable / Disable reghosting ioctl() operation
723 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
728 status = get_user(val, p);
730 DPRINTK("reghost = %d", val);
735 /* turn on/off reghosting, with the val */
736 sbi->reghost_enabled = val;
741 * Tells the daemon whether it can umount the autofs mount.
743 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
747 if (may_umount(mnt) == 0)
750 DPRINTK("returning %d", status);
752 status = put_user(status, p);
757 /* Identify autofs4_dentries - this is so we can tell if there's
758 an extra dentry refcount or not. We only hold a refcount on the
759 dentry if its non-negative (ie, d_inode != NULL)
761 int is_autofs4_dentry(struct dentry *dentry)
763 return dentry && dentry->d_inode &&
764 (dentry->d_op == &autofs4_root_dentry_operations ||
765 dentry->d_op == &autofs4_dentry_operations) &&
766 dentry->d_fsdata != NULL;
770 * ioctl()'s on the root directory is the chief method for the daemon to
771 * generate kernel reactions
773 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
774 unsigned int cmd, unsigned long arg)
776 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
777 void __user *p = (void __user *)arg;
779 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
780 cmd,arg,sbi,process_group(current));
782 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
783 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
786 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
790 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
791 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
792 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
793 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
794 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
795 autofs4_catatonic_mode(sbi);
797 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
798 return autofs4_get_protover(sbi, p);
799 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
800 return autofs4_get_protosubver(sbi, p);
801 case AUTOFS_IOC_SETTIMEOUT:
802 return autofs4_get_set_timeout(sbi, p);
804 case AUTOFS_IOC_TOGGLEREGHOST:
805 return autofs4_toggle_reghost(sbi, p);
806 case AUTOFS_IOC_ASKREGHOST:
807 return autofs4_ask_reghost(sbi, p);
809 case AUTOFS_IOC_ASKUMOUNT:
810 return autofs4_ask_umount(filp->f_vfsmnt, p);
812 /* return a single thing to expire */
813 case AUTOFS_IOC_EXPIRE:
814 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
815 /* same as above, but can send multiple expires through pipe */
816 case AUTOFS_IOC_EXPIRE_MULTI:
817 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);