15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/slab.h>
20 #include <linux/time.h>
26 static int autofs4_dir_symlink(
struct inode *,
struct dentry *,
const char *);
27 static int autofs4_dir_unlink(
struct inode *,
struct dentry *);
28 static int autofs4_dir_rmdir(
struct inode *,
struct dentry *);
30 static long autofs4_root_ioctl(
struct file *,
unsigned int,
unsigned long);
32 static long autofs4_root_compat_ioctl(
struct file *,
unsigned int,
unsigned long);
35 static struct dentry *autofs4_lookup(
struct inode *,
struct dentry *,
unsigned int);
36 static struct vfsmount *autofs4_d_automount(
struct path *);
37 static int autofs4_d_manage(
struct dentry *,
bool);
38 static void autofs4_dentry_release(
struct dentry *);
46 .unlocked_ioctl = autofs4_root_ioctl,
48 .compat_ioctl = autofs4_root_compat_ioctl,
53 .open = autofs4_dir_open,
61 .lookup = autofs4_lookup,
62 .unlink = autofs4_dir_unlink,
63 .symlink = autofs4_dir_symlink,
64 .mkdir = autofs4_dir_mkdir,
65 .rmdir = autofs4_dir_rmdir,
69 .d_automount = autofs4_d_automount,
70 .d_manage = autofs4_d_manage,
71 .d_release = autofs4_dentry_release,
81 if (list_empty(&ino->
active))
90 static void autofs4_del_active(
struct dentry *dentry)
93 struct autofs_info *ino = autofs4_dentry_ino(dentry);
98 if (!list_empty(&ino->
active))
99 list_del_init(&ino->
active);
108 struct dentry *dentry = file->
f_path.dentry;
111 DPRINTK(
"file=%p dentry=%p %.*s",
114 if (autofs4_oz_mode(sbi))
127 spin_lock(&dentry->
d_lock);
128 if (!d_mountpoint(dentry) && list_empty(&dentry->
d_subdirs)) {
129 spin_unlock(&dentry->
d_lock);
133 spin_unlock(&dentry->
d_lock);
140 static void autofs4_dentry_release(
struct dentry *de)
152 if (!list_empty(&ino->
active))
162 static struct dentry *autofs4_lookup_active(
struct dentry *dentry)
167 unsigned int len = name->len;
168 unsigned int hash = name->hash;
169 const unsigned char *
str = name->
name;
182 spin_lock(&active->
d_lock);
190 if (active->
d_name.hash != hash)
195 if (qstr->len != len)
200 if (d_unhashed(active)) {
202 spin_unlock(&active->
d_lock);
207 spin_unlock(&active->
d_lock);
214 static struct dentry *autofs4_lookup_expiring(
struct dentry *dentry)
217 struct dentry *parent = dentry->
d_parent;
218 struct qstr *name = &dentry->
d_name;
219 unsigned int len = name->len;
220 unsigned int hash = name->hash;
221 const unsigned char *str = name->
name;
228 struct dentry *expiring;
234 spin_lock(&expiring->
d_lock);
242 if (expiring->
d_name.hash != hash)
247 if (qstr->len != len)
252 if (d_unhashed(expiring)) {
253 dget_dlock(expiring);
254 spin_unlock(&expiring->
d_lock);
259 spin_unlock(&expiring->
d_lock);
266 static int autofs4_mount_wait(
struct dentry *dentry)
269 struct autofs_info *ino = autofs4_dentry_ino(dentry);
273 DPRINTK(
"waiting for mount name=%.*s",
276 DPRINTK(
"mount wait done status=%d", status);
282 static int do_expire_wait(
struct dentry *dentry)
284 struct dentry *expiring;
286 expiring = autofs4_lookup_expiring(dentry);
296 autofs4_del_expiring(expiring);
302 static struct dentry *autofs4_mountpoint_changed(
struct path *
path)
304 struct dentry *dentry = path->
dentry;
311 if (autofs_type_indirect(sbi->
type) && d_unhashed(dentry)) {
312 struct dentry *parent = dentry->
d_parent;
317 ino = autofs4_dentry_ino(
new);
325 static struct vfsmount *autofs4_d_automount(
struct path *path)
327 struct dentry *dentry = path->
dentry;
329 struct autofs_info *ino = autofs4_dentry_ino(dentry);
336 if (autofs4_oz_mode(sbi))
347 status = do_expire_wait(dentry);
348 if (status && status != -
EAGAIN)
355 status = autofs4_mount_wait(dentry);
357 return ERR_PTR(status);
369 if (!d_mountpoint(dentry)) {
385 spin_lock(&dentry->
d_lock);
387 spin_unlock(&dentry->
d_lock);
390 spin_unlock(&dentry->
d_lock);
394 status = autofs4_mount_wait(dentry);
399 return ERR_PTR(status);
415 spin_lock(&dentry->
d_lock);
416 if ((!d_mountpoint(dentry) &&
419 __managed_dentry_clear_automount(dentry);
420 spin_unlock(&dentry->
d_lock);
425 dentry = autofs4_mountpoint_changed(path);
432 int autofs4_d_manage(
struct dentry *dentry,
bool rcu_walk)
440 if (autofs4_oz_mode(sbi)) {
443 if (!d_mountpoint(dentry))
453 do_expire_wait(dentry);
459 return autofs4_mount_wait(dentry);
463 static struct dentry *autofs4_lookup(
struct inode *dir,
struct dentry *dentry,
unsigned int flags)
475 sbi = autofs4_sbi(dir->
i_sb);
477 DPRINTK(
"pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
479 autofs4_oz_mode(sbi));
481 active = autofs4_lookup_active(dentry);
496 __managed_dentry_set_managed(dentry);
505 autofs4_add_active(dentry);
512 static int autofs4_dir_symlink(
struct inode *dir,
513 struct dentry *dentry,
517 struct autofs_info *ino = autofs4_dentry_ino(dentry);
526 if (!autofs4_oz_mode(sbi))
533 autofs4_del_active(dentry);
550 d_add(dentry, inode);
554 p_ino = autofs4_dentry_ino(dentry->
d_parent);
555 if (p_ino && dentry->
d_parent != dentry)
578 static int autofs4_dir_unlink(
struct inode *dir,
struct dentry *dentry)
581 struct autofs_info *ino = autofs4_dentry_ino(dentry);
589 p_ino = autofs4_dentry_ino(dentry->
d_parent);
590 if (p_ino && dentry->
d_parent != dentry)
601 __autofs4_add_expiring(dentry);
602 spin_lock(&dentry->
d_lock);
604 spin_unlock(&dentry->
d_lock);
621 static void autofs_set_leaf_automount_flags(
struct dentry *dentry)
623 struct dentry *parent;
629 managed_dentry_set_managed(dentry);
635 managed_dentry_clear_managed(parent);
639 static void autofs_clear_leaf_automount_flags(
struct dentry *dentry)
642 struct dentry *parent;
648 managed_dentry_clear_managed(dentry);
658 managed_dentry_set_managed(parent);
662 static int autofs4_dir_rmdir(
struct inode *dir,
struct dentry *dentry)
665 struct autofs_info *ino = autofs4_dentry_ino(dentry);
668 DPRINTK(
"dentry %p, removing %.*s",
671 if (!autofs4_oz_mode(sbi))
675 spin_lock(&dentry->
d_lock);
677 spin_unlock(&dentry->
d_lock);
681 __autofs4_add_expiring(dentry);
683 spin_unlock(&dentry->
d_lock);
687 autofs_clear_leaf_automount_flags(dentry);
690 p_ino = autofs4_dentry_ino(dentry->
d_parent);
691 if (p_ino && dentry->
d_parent != dentry)
704 static int autofs4_dir_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
707 struct autofs_info *ino = autofs4_dentry_ino(dentry);
711 if (!autofs4_oz_mode(sbi))
714 DPRINTK(
"dentry %p, creating %.*s",
721 autofs4_del_active(dentry);
726 d_add(dentry, inode);
729 autofs_set_leaf_automount_flags(dentry);
733 p_ino = autofs4_dentry_ino(dentry->
d_parent);
734 if (p_ino && dentry->
d_parent != dentry)
744 static inline int autofs4_compat_get_set_timeout(
struct autofs_sb_info *sbi,
748 unsigned long ntimeout;
763 static inline int autofs4_get_set_timeout(
struct autofs_sb_info *sbi,
764 unsigned long __user *p)
767 unsigned long ntimeout;
782 static inline int autofs4_get_protover(
struct autofs_sb_info *sbi,
int __user *p)
788 static inline int autofs4_get_protosubver(
struct autofs_sb_info *sbi,
int __user *p)
796 static inline int autofs4_ask_umount(
struct vfsmount *mnt,
int __user *p)
803 DPRINTK(
"returning %d", status);
816 return dentry && dentry->
d_inode &&
817 dentry->
d_op == &autofs4_dentry_operations &&
825 static int autofs4_root_ioctl_unlocked(
struct inode *inode,
struct file *filp,
826 unsigned int cmd,
unsigned long arg)
831 DPRINTK(
"cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
832 cmd,arg,sbi,task_pgrp_nr(
current));
850 return autofs4_get_protover(sbi, p);
852 return autofs4_get_protosubver(sbi, p);
854 return autofs4_get_set_timeout(sbi, p);
857 return autofs4_compat_get_set_timeout(sbi, p);
861 return autofs4_ask_umount(filp->
f_path.mnt, p);
875 static long autofs4_root_ioctl(
struct file *filp,
876 unsigned int cmd,
unsigned long arg)
878 struct inode *inode = filp->f_dentry->d_inode;
879 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
883 static long autofs4_root_compat_ioctl(
struct file *filp,
884 unsigned int cmd,
unsigned long arg)
886 struct inode *inode = filp->
f_path.dentry->d_inode;
890 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
892 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
893 (
unsigned long)compat_ptr(arg));