Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- c -*- --------------------------------------------------------------- * |
| 2 | * |
| 3 | * linux/fs/autofs/root.c |
| 4 | * |
| 5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved |
| 6 | * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org> |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 7 | * Copyright 2001-2006 Ian Kent <raven@themaw.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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. |
| 12 | * |
| 13 | * ------------------------------------------------------------------------- */ |
| 14 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 15 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/param.h> |
| 20 | #include <linux/time.h> |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 21 | #include <linux/compat.h> |
Arnd Bergmann | 00e300e | 2010-09-14 23:00:34 +0200 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "autofs_i.h" |
| 25 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 26 | DEFINE_SPINLOCK(autofs4_lock); |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *); |
| 29 | static int autofs4_dir_unlink(struct inode *,struct dentry *); |
| 30 | static int autofs4_dir_rmdir(struct inode *,struct dentry *); |
| 31 | static int autofs4_dir_mkdir(struct inode *,struct dentry *,int); |
Frederic Weisbecker | 3663df7 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 32 | static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long); |
Felipe Contreras | 5a44a73 | 2010-09-24 15:22:23 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_COMPAT |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 34 | static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long); |
Felipe Contreras | 5a44a73 | 2010-09-24 15:22:23 +0200 | [diff] [blame] | 35 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static int autofs4_dir_open(struct inode *inode, struct file *file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 39 | const struct file_operations autofs4_root_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | .open = dcache_dir_open, |
| 41 | .release = dcache_dir_close, |
| 42 | .read = generic_read_dir, |
Ian Kent | aa55ddf | 2008-07-23 21:30:29 -0700 | [diff] [blame] | 43 | .readdir = dcache_readdir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 44 | .llseek = dcache_dir_lseek, |
Frederic Weisbecker | 3663df7 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 45 | .unlocked_ioctl = autofs4_root_ioctl, |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 46 | #ifdef CONFIG_COMPAT |
| 47 | .compat_ioctl = autofs4_root_compat_ioctl, |
| 48 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 51 | const struct file_operations autofs4_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | .open = autofs4_dir_open, |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 53 | .release = dcache_dir_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | .read = generic_read_dir, |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 55 | .readdir = dcache_readdir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 56 | .llseek = dcache_dir_lseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 59 | const struct inode_operations autofs4_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | .lookup = autofs4_lookup, |
| 61 | .unlink = autofs4_dir_unlink, |
| 62 | .symlink = autofs4_dir_symlink, |
| 63 | .mkdir = autofs4_dir_mkdir, |
| 64 | .rmdir = autofs4_dir_rmdir, |
| 65 | }; |
| 66 | |
Ian Kent | 4f8427d | 2009-12-15 16:45:42 -0800 | [diff] [blame] | 67 | static void autofs4_add_active(struct dentry *dentry) |
| 68 | { |
| 69 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 70 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 71 | if (ino) { |
| 72 | spin_lock(&sbi->lookup_lock); |
| 73 | if (!ino->active_count) { |
| 74 | if (list_empty(&ino->active)) |
| 75 | list_add(&ino->active, &sbi->active_list); |
| 76 | } |
| 77 | ino->active_count++; |
| 78 | spin_unlock(&sbi->lookup_lock); |
| 79 | } |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | static void autofs4_del_active(struct dentry *dentry) |
| 84 | { |
| 85 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 86 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 87 | if (ino) { |
| 88 | spin_lock(&sbi->lookup_lock); |
| 89 | ino->active_count--; |
| 90 | if (!ino->active_count) { |
| 91 | if (!list_empty(&ino->active)) |
| 92 | list_del_init(&ino->active); |
| 93 | } |
| 94 | spin_unlock(&sbi->lookup_lock); |
| 95 | } |
| 96 | return; |
| 97 | } |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static int autofs4_dir_open(struct inode *inode, struct file *file) |
| 100 | { |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 101 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | DPRINTK("file=%p dentry=%p %.*s", |
| 105 | file, dentry, dentry->d_name.len, dentry->d_name.name); |
| 106 | |
| 107 | if (autofs4_oz_mode(sbi)) |
| 108 | goto out; |
| 109 | |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 110 | /* |
| 111 | * An empty directory in an autofs file system is always a |
| 112 | * mount point. The daemon must have failed to mount this |
| 113 | * during lookup so it doesn't exist. This can happen, for |
| 114 | * example, if user space returns an incorrect status for a |
| 115 | * mount request. Otherwise we're doing a readdir on the |
| 116 | * autofs file system so just let the libfs routines handle |
| 117 | * it. |
| 118 | */ |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 119 | spin_lock(&autofs4_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 120 | spin_lock(&dentry->d_lock); |
Ian Kent | c42c7f7 | 2009-12-15 16:45:48 -0800 | [diff] [blame] | 121 | if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 122 | spin_unlock(&dentry->d_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 123 | spin_unlock(&autofs4_lock); |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 124 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 126 | spin_unlock(&dentry->d_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 127 | spin_unlock(&autofs4_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 129 | out: |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 130 | return dcache_dir_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 133 | void autofs4_dentry_release(struct dentry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | struct autofs_info *inf; |
| 136 | |
| 137 | DPRINTK("releasing %p", de); |
| 138 | |
| 139 | inf = autofs4_dentry_ino(de); |
| 140 | de->d_fsdata = NULL; |
| 141 | |
| 142 | if (inf) { |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 143 | struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb); |
| 144 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 145 | if (sbi) { |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 146 | spin_lock(&sbi->lookup_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 147 | if (!list_empty(&inf->active)) |
| 148 | list_del(&inf->active); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 149 | if (!list_empty(&inf->expiring)) |
| 150 | list_del(&inf->expiring); |
| 151 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Jeff Mahoney | c3724b1 | 2007-04-11 23:28:46 -0700 | [diff] [blame] | 154 | inf->dentry = NULL; |
| 155 | inf->inode = NULL; |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | autofs4_free_ino(inf); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* For dentries of directories in the root dir */ |
Al Viro | 08f1151 | 2009-02-20 05:56:19 +0000 | [diff] [blame] | 162 | static const struct dentry_operations autofs4_root_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | .d_release = autofs4_dentry_release, |
| 164 | }; |
| 165 | |
| 166 | /* For other dentries */ |
Al Viro | 08f1151 | 2009-02-20 05:56:19 +0000 | [diff] [blame] | 167 | static const struct dentry_operations autofs4_dentry_operations = { |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 168 | .d_automount = autofs4_d_automount, |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 169 | .d_manage = autofs4_d_manage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | .d_release = autofs4_dentry_release, |
| 171 | }; |
| 172 | |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 173 | static struct dentry *autofs4_lookup_active(struct dentry *dentry) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 174 | { |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 175 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 176 | struct dentry *parent = dentry->d_parent; |
| 177 | struct qstr *name = &dentry->d_name; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 178 | unsigned int len = name->len; |
| 179 | unsigned int hash = name->hash; |
| 180 | const unsigned char *str = name->name; |
| 181 | struct list_head *p, *head; |
| 182 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 183 | spin_lock(&autofs4_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 184 | spin_lock(&sbi->lookup_lock); |
| 185 | head = &sbi->active_list; |
| 186 | list_for_each(p, head) { |
| 187 | struct autofs_info *ino; |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 188 | struct dentry *active; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 189 | struct qstr *qstr; |
| 190 | |
| 191 | ino = list_entry(p, struct autofs_info, active); |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 192 | active = ino->dentry; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 193 | |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 194 | spin_lock(&active->d_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 195 | |
| 196 | /* Already gone? */ |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 197 | if (active->d_count == 0) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 198 | goto next; |
| 199 | |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 200 | qstr = &active->d_name; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 201 | |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 202 | if (active->d_name.hash != hash) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 203 | goto next; |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 204 | if (active->d_parent != parent) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 205 | goto next; |
| 206 | |
| 207 | if (qstr->len != len) |
| 208 | goto next; |
| 209 | if (memcmp(qstr->name, str, len)) |
| 210 | goto next; |
| 211 | |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 212 | if (d_unhashed(active)) { |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 213 | dget_dlock(active); |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 214 | spin_unlock(&active->d_lock); |
| 215 | spin_unlock(&sbi->lookup_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 216 | spin_unlock(&autofs4_lock); |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 217 | return active; |
| 218 | } |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 219 | next: |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 220 | spin_unlock(&active->d_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 221 | } |
| 222 | spin_unlock(&sbi->lookup_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 223 | spin_unlock(&autofs4_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 228 | static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 229 | { |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 230 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 231 | struct dentry *parent = dentry->d_parent; |
| 232 | struct qstr *name = &dentry->d_name; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 233 | unsigned int len = name->len; |
| 234 | unsigned int hash = name->hash; |
| 235 | const unsigned char *str = name->name; |
| 236 | struct list_head *p, *head; |
| 237 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 238 | spin_lock(&autofs4_lock); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 239 | spin_lock(&sbi->lookup_lock); |
| 240 | head = &sbi->expiring_list; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 241 | list_for_each(p, head) { |
| 242 | struct autofs_info *ino; |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 243 | struct dentry *expiring; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 244 | struct qstr *qstr; |
| 245 | |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 246 | ino = list_entry(p, struct autofs_info, expiring); |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 247 | expiring = ino->dentry; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 248 | |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 249 | spin_lock(&expiring->d_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 250 | |
| 251 | /* Bad luck, we've already been dentry_iput */ |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 252 | if (!expiring->d_inode) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 253 | goto next; |
| 254 | |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 255 | qstr = &expiring->d_name; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 256 | |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 257 | if (expiring->d_name.hash != hash) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 258 | goto next; |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 259 | if (expiring->d_parent != parent) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 260 | goto next; |
| 261 | |
| 262 | if (qstr->len != len) |
| 263 | goto next; |
| 264 | if (memcmp(qstr->name, str, len)) |
| 265 | goto next; |
| 266 | |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 267 | if (d_unhashed(expiring)) { |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 268 | dget_dlock(expiring); |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 269 | spin_unlock(&expiring->d_lock); |
| 270 | spin_unlock(&sbi->lookup_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 271 | spin_unlock(&autofs4_lock); |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 272 | return expiring; |
| 273 | } |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 274 | next: |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 275 | spin_unlock(&expiring->d_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 276 | } |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 277 | spin_unlock(&sbi->lookup_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 278 | spin_unlock(&autofs4_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 279 | |
| 280 | return NULL; |
| 281 | } |
| 282 | |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 283 | static int autofs4_mount_wait(struct dentry *dentry) |
| 284 | { |
| 285 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 286 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 287 | int status; |
| 288 | |
| 289 | if (ino->flags & AUTOFS_INF_PENDING) { |
| 290 | DPRINTK("waiting for mount name=%.*s", |
| 291 | dentry->d_name.len, dentry->d_name.name); |
| 292 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); |
| 293 | DPRINTK("mount wait done status=%d", status); |
| 294 | ino->last_used = jiffies; |
| 295 | return status; |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int do_expire_wait(struct dentry *dentry) |
| 301 | { |
| 302 | struct dentry *expiring; |
| 303 | |
| 304 | expiring = autofs4_lookup_expiring(dentry); |
| 305 | if (!expiring) |
| 306 | return autofs4_expire_wait(dentry); |
| 307 | else { |
| 308 | /* |
| 309 | * If we are racing with expire the request might not |
| 310 | * be quite complete, but the directory has been removed |
| 311 | * so it must have been successful, just wait for it. |
| 312 | */ |
| 313 | autofs4_expire_wait(expiring); |
| 314 | autofs4_del_expiring(expiring); |
| 315 | dput(expiring); |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static struct dentry *autofs4_mountpoint_changed(struct path *path) |
| 321 | { |
| 322 | struct dentry *dentry = path->dentry; |
| 323 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 324 | |
| 325 | /* |
| 326 | * If this is an indirect mount the dentry could have gone away |
| 327 | * as a result of an expire and a new one created. |
| 328 | */ |
| 329 | if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) { |
| 330 | struct dentry *parent = dentry->d_parent; |
| 331 | struct dentry *new = d_lookup(parent, &dentry->d_name); |
| 332 | if (!new) |
| 333 | return NULL; |
| 334 | dput(path->dentry); |
| 335 | path->dentry = new; |
| 336 | } |
| 337 | return path->dentry; |
| 338 | } |
| 339 | |
| 340 | struct vfsmount *autofs4_d_automount(struct path *path) |
| 341 | { |
| 342 | struct dentry *dentry = path->dentry; |
| 343 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 344 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 345 | int status; |
| 346 | |
| 347 | DPRINTK("dentry=%p %.*s", |
| 348 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 349 | |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Someone may have manually umounted this or it was a submount |
| 352 | * that has gone away. |
| 353 | */ |
| 354 | spin_lock(&dentry->d_lock); |
| 355 | if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { |
| 356 | if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) && |
| 357 | (dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) |
| 358 | __managed_dentry_set_transit(path->dentry); |
| 359 | } |
| 360 | spin_unlock(&dentry->d_lock); |
| 361 | |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 362 | /* The daemon never triggers a mount. */ |
| 363 | if (autofs4_oz_mode(sbi)) |
| 364 | return NULL; |
| 365 | |
| 366 | /* |
| 367 | * If an expire request is pending everyone must wait. |
| 368 | * If the expire fails we're still mounted so continue |
| 369 | * the follow and return. A return of -EAGAIN (which only |
| 370 | * happens with indirect mounts) means the expire completed |
| 371 | * and the directory was removed, so just go ahead and try |
| 372 | * the mount. |
| 373 | */ |
| 374 | status = do_expire_wait(dentry); |
| 375 | if (status && status != -EAGAIN) |
| 376 | return NULL; |
| 377 | |
| 378 | /* Callback to the daemon to perform the mount or wait */ |
| 379 | spin_lock(&sbi->fs_lock); |
| 380 | if (ino->flags & AUTOFS_INF_PENDING) { |
| 381 | spin_unlock(&sbi->fs_lock); |
| 382 | status = autofs4_mount_wait(dentry); |
| 383 | if (status) |
| 384 | return ERR_PTR(status); |
| 385 | spin_lock(&sbi->fs_lock); |
| 386 | goto done; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * If the dentry is a symlink it's equivalent to a directory |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 391 | * having d_mountpoint() true, so there's no need to call back |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 392 | * to the daemon. |
| 393 | */ |
| 394 | if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)) |
| 395 | goto done; |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 396 | if (!d_mountpoint(dentry)) { |
| 397 | /* |
| 398 | * It's possible that user space hasn't removed directories |
| 399 | * after umounting a rootless multi-mount, although it |
| 400 | * should. For v5 have_submounts() is sufficient to handle |
| 401 | * this because the leaves of the directory tree under the |
| 402 | * mount never trigger mounts themselves (they have an autofs |
| 403 | * trigger mount mounted on them). But v4 pseudo direct mounts |
| 404 | * do need the leaves to to trigger mounts. In this case we |
| 405 | * have no choice but to use the list_empty() check and |
| 406 | * require user space behave. |
| 407 | */ |
| 408 | if (sbi->version > 4) { |
| 409 | if (have_submounts(dentry)) |
| 410 | goto done; |
| 411 | } else { |
| 412 | spin_lock(&dentry->d_lock); |
| 413 | if (!list_empty(&dentry->d_subdirs)) { |
| 414 | spin_unlock(&dentry->d_lock); |
| 415 | goto done; |
| 416 | } |
| 417 | spin_unlock(&dentry->d_lock); |
| 418 | } |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 419 | ino->flags |= AUTOFS_INF_PENDING; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 420 | spin_unlock(&sbi->fs_lock); |
| 421 | status = autofs4_mount_wait(dentry); |
| 422 | if (status) |
| 423 | return ERR_PTR(status); |
| 424 | spin_lock(&sbi->fs_lock); |
| 425 | ino->flags &= ~AUTOFS_INF_PENDING; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 426 | } |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 427 | done: |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 428 | if (!(ino->flags & AUTOFS_INF_EXPIRING)) { |
| 429 | /* |
| 430 | * Any needed mounting has been completed and the path updated |
| 431 | * so turn this into a normal dentry so we don't continually |
| 432 | * call ->d_automount() and ->d_manage(). |
| 433 | */ |
| 434 | spin_lock(&dentry->d_lock); |
| 435 | __managed_dentry_clear_transit(dentry); |
| 436 | /* |
| 437 | * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and |
| 438 | * symlinks as in all other cases the dentry will be covered by |
| 439 | * an actual mount so ->d_automount() won't be called during |
| 440 | * the follow. |
| 441 | */ |
| 442 | if ((!d_mountpoint(dentry) && |
| 443 | !list_empty(&dentry->d_subdirs)) || |
| 444 | (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))) |
| 445 | __managed_dentry_clear_automount(dentry); |
| 446 | spin_unlock(&dentry->d_lock); |
| 447 | } |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 448 | spin_unlock(&sbi->fs_lock); |
| 449 | |
| 450 | /* Mount succeeded, check if we ended up with a new dentry */ |
| 451 | dentry = autofs4_mountpoint_changed(path); |
| 452 | if (!dentry) |
| 453 | return ERR_PTR(-ENOENT); |
| 454 | |
| 455 | return NULL; |
| 456 | } |
| 457 | |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 458 | int autofs4_d_manage(struct dentry *dentry, bool mounting_here) |
| 459 | { |
| 460 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 461 | |
| 462 | DPRINTK("dentry=%p %.*s", |
| 463 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 464 | |
| 465 | /* The daemon never waits. */ |
| 466 | if (autofs4_oz_mode(sbi) || mounting_here) { |
| 467 | if (!d_mountpoint(dentry)) |
| 468 | return -EISDIR; |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* Wait for pending expires */ |
| 473 | do_expire_wait(dentry); |
| 474 | |
| 475 | /* |
| 476 | * This dentry may be under construction so wait on mount |
| 477 | * completion. |
| 478 | */ |
| 479 | return autofs4_mount_wait(dentry); |
| 480 | } |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* Lookups in the root directory */ |
| 483 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 484 | { |
| 485 | struct autofs_sb_info *sbi; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 486 | struct autofs_info *ino; |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 487 | struct dentry *active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 489 | DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 491 | /* File name too long to exist */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | if (dentry->d_name.len > NAME_MAX) |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 493 | return ERR_PTR(-ENAMETOOLONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | sbi = autofs4_sbi(dir->i_sb); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 498 | current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 500 | active = autofs4_lookup_active(dentry); |
Ian Kent | 90387c9 | 2009-12-15 16:45:46 -0800 | [diff] [blame] | 501 | if (active) { |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 502 | return active; |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 503 | } else { |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 504 | d_set_d_op(dentry, &autofs4_root_dentry_operations); |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 505 | |
| 506 | /* |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 507 | * A dentry that is not within the root can never trigger a |
| 508 | * mount operation, unless the directory already exists, so we |
| 509 | * can return fail immediately. The daemon however does need |
| 510 | * to create directories within the file system. |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 511 | */ |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 512 | if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent)) |
| 513 | return ERR_PTR(-ENOENT); |
| 514 | |
| 515 | /* Mark entries in the root as mount triggers */ |
| 516 | if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) { |
| 517 | d_set_d_op(dentry, &autofs4_dentry_operations); |
Ian Kent | b5b8017 | 2011-01-14 18:46:03 +0000 | [diff] [blame] | 518 | __managed_dentry_set_managed(dentry); |
Ian Kent | 1058421 | 2011-01-14 18:45:58 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 521 | ino = autofs4_init_ino(NULL, sbi, 0555); |
| 522 | if (!ino) |
| 523 | return ERR_PTR(-ENOMEM); |
| 524 | |
| 525 | dentry->d_fsdata = ino; |
| 526 | ino->dentry = dentry; |
| 527 | |
| 528 | autofs4_add_active(dentry); |
| 529 | |
| 530 | d_instantiate(dentry, NULL); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | static int autofs4_dir_symlink(struct inode *dir, |
| 536 | struct dentry *dentry, |
| 537 | const char *symname) |
| 538 | { |
| 539 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 540 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 541 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | struct inode *inode; |
| 543 | char *cp; |
| 544 | |
| 545 | DPRINTK("%s <- %.*s", symname, |
| 546 | dentry->d_name.len, dentry->d_name.name); |
| 547 | |
| 548 | if (!autofs4_oz_mode(sbi)) |
| 549 | return -EACCES; |
| 550 | |
| 551 | ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 552 | if (!ino) |
| 553 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 555 | autofs4_del_active(dentry); |
| 556 | |
Ian Kent | ef581a7 | 2008-07-23 21:30:13 -0700 | [diff] [blame] | 557 | ino->size = strlen(symname); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 558 | cp = kmalloc(ino->size + 1, GFP_KERNEL); |
| 559 | if (!cp) { |
| 560 | if (!dentry->d_fsdata) |
| 561 | kfree(ino); |
| 562 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | strcpy(cp, symname); |
| 566 | |
| 567 | inode = autofs4_get_inode(dir->i_sb, ino); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 568 | if (!inode) { |
| 569 | kfree(cp); |
| 570 | if (!dentry->d_fsdata) |
| 571 | kfree(ino); |
| 572 | return -ENOMEM; |
| 573 | } |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 574 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | dentry->d_fsdata = ino; |
| 577 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 578 | atomic_inc(&ino->count); |
| 579 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 580 | if (p_ino && dentry->d_parent != dentry) |
| 581 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | ino->inode = inode; |
| 583 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 584 | ino->u.symlink = cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | dir->i_mtime = CURRENT_TIME; |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * NOTE! |
| 592 | * |
| 593 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache |
| 594 | * that the file no longer exists. However, doing that means that the |
| 595 | * VFS layer can turn the dentry into a negative dentry. We don't want |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 596 | * this, because the unlink is probably the result of an expire. |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 597 | * We simply d_drop it and add it to a expiring list in the super block, |
| 598 | * which allows the dentry lookup to check for an incomplete expire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | * |
| 600 | * If a process is blocked on the dentry waiting for the expire to finish, |
| 601 | * it will invalidate the dentry and try to mount with a new one. |
| 602 | * |
| 603 | * Also see autofs4_dir_rmdir().. |
| 604 | */ |
| 605 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) |
| 606 | { |
| 607 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 608 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 609 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | /* This allows root to remove symlinks */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 612 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | return -EACCES; |
| 614 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 615 | if (atomic_dec_and_test(&ino->count)) { |
| 616 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 617 | if (p_ino && dentry->d_parent != dentry) |
| 618 | atomic_dec(&p_ino->count); |
| 619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | dput(ino->dentry); |
| 621 | |
| 622 | dentry->d_inode->i_size = 0; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 623 | clear_nlink(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
| 625 | dir->i_mtime = CURRENT_TIME; |
| 626 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 627 | spin_lock(&autofs4_lock); |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 628 | autofs4_add_expiring(dentry); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 629 | spin_lock(&dentry->d_lock); |
| 630 | __d_drop(dentry); |
| 631 | spin_unlock(&dentry->d_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 632 | spin_unlock(&autofs4_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) |
| 638 | { |
| 639 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 640 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 641 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 643 | DPRINTK("dentry %p, removing %.*s", |
| 644 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (!autofs4_oz_mode(sbi)) |
| 647 | return -EACCES; |
| 648 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 649 | spin_lock(&autofs4_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 650 | spin_lock(&sbi->lookup_lock); |
| 651 | spin_lock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (!list_empty(&dentry->d_subdirs)) { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 653 | spin_unlock(&dentry->d_lock); |
| 654 | spin_unlock(&sbi->lookup_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 655 | spin_unlock(&autofs4_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return -ENOTEMPTY; |
| 657 | } |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 658 | __autofs4_add_expiring(dentry); |
| 659 | spin_unlock(&sbi->lookup_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | __d_drop(dentry); |
| 661 | spin_unlock(&dentry->d_lock); |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 662 | spin_unlock(&autofs4_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 664 | if (atomic_dec_and_test(&ino->count)) { |
| 665 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 666 | if (p_ino && dentry->d_parent != dentry) |
| 667 | atomic_dec(&p_ino->count); |
| 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | dput(ino->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | dentry->d_inode->i_size = 0; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 671 | clear_nlink(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | if (dir->i_nlink) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 674 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 680 | { |
| 681 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 682 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 683 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | struct inode *inode; |
| 685 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 686 | if (!autofs4_oz_mode(sbi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | return -EACCES; |
| 688 | |
| 689 | DPRINTK("dentry %p, creating %.*s", |
| 690 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 691 | |
| 692 | ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 693 | if (!ino) |
| 694 | return -ENOMEM; |
| 695 | |
Al Viro | 4b1ae27 | 2010-03-03 12:58:31 -0500 | [diff] [blame] | 696 | autofs4_del_active(dentry); |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | inode = autofs4_get_inode(dir->i_sb, ino); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 699 | if (!inode) { |
| 700 | if (!dentry->d_fsdata) |
| 701 | kfree(ino); |
| 702 | return -ENOMEM; |
| 703 | } |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 704 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | dentry->d_fsdata = ino; |
| 707 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | ino->inode = inode; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 713 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | dir->i_mtime = CURRENT_TIME; |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | /* Get/set timeout ioctl() operation */ |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 720 | #ifdef CONFIG_COMPAT |
| 721 | static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi, |
| 722 | compat_ulong_t __user *p) |
| 723 | { |
| 724 | int rv; |
| 725 | unsigned long ntimeout; |
| 726 | |
| 727 | if ((rv = get_user(ntimeout, p)) || |
| 728 | (rv = put_user(sbi->exp_timeout/HZ, p))) |
| 729 | return rv; |
| 730 | |
| 731 | if (ntimeout > UINT_MAX/HZ) |
| 732 | sbi->exp_timeout = 0; |
| 733 | else |
| 734 | sbi->exp_timeout = ntimeout * HZ; |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | #endif |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, |
| 741 | unsigned long __user *p) |
| 742 | { |
| 743 | int rv; |
| 744 | unsigned long ntimeout; |
| 745 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 746 | if ((rv = get_user(ntimeout, p)) || |
| 747 | (rv = put_user(sbi->exp_timeout/HZ, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | return rv; |
| 749 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 750 | if (ntimeout > ULONG_MAX/HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | sbi->exp_timeout = 0; |
| 752 | else |
| 753 | sbi->exp_timeout = ntimeout * HZ; |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* Return protocol version */ |
| 759 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) |
| 760 | { |
| 761 | return put_user(sbi->version, p); |
| 762 | } |
| 763 | |
| 764 | /* Return protocol sub version */ |
| 765 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) |
| 766 | { |
| 767 | return put_user(sbi->sub_version, p); |
| 768 | } |
| 769 | |
| 770 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | * Tells the daemon whether it can umount the autofs mount. |
| 772 | */ |
| 773 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) |
| 774 | { |
| 775 | int status = 0; |
| 776 | |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 777 | if (may_umount(mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | status = 1; |
| 779 | |
| 780 | DPRINTK("returning %d", status); |
| 781 | |
| 782 | status = put_user(status, p); |
| 783 | |
| 784 | return status; |
| 785 | } |
| 786 | |
| 787 | /* Identify autofs4_dentries - this is so we can tell if there's |
| 788 | an extra dentry refcount or not. We only hold a refcount on the |
| 789 | dentry if its non-negative (ie, d_inode != NULL) |
| 790 | */ |
| 791 | int is_autofs4_dentry(struct dentry *dentry) |
| 792 | { |
| 793 | return dentry && dentry->d_inode && |
| 794 | (dentry->d_op == &autofs4_root_dentry_operations || |
| 795 | dentry->d_op == &autofs4_dentry_operations) && |
| 796 | dentry->d_fsdata != NULL; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * ioctl()'s on the root directory is the chief method for the daemon to |
| 801 | * generate kernel reactions |
| 802 | */ |
Frederic Weisbecker | 3663df7 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 803 | static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp, |
| 804 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | { |
| 806 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); |
| 807 | void __user *p = (void __user *)arg; |
| 808 | |
| 809 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 810 | cmd,arg,sbi,task_pgrp_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 812 | if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || |
| 813 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return -ENOTTY; |
| 815 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 816 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | return -EPERM; |
| 818 | |
| 819 | switch(cmd) { |
| 820 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ |
| 821 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); |
| 822 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ |
| 823 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); |
| 824 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ |
| 825 | autofs4_catatonic_mode(sbi); |
| 826 | return 0; |
| 827 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ |
| 828 | return autofs4_get_protover(sbi, p); |
| 829 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ |
| 830 | return autofs4_get_protosubver(sbi, p); |
| 831 | case AUTOFS_IOC_SETTIMEOUT: |
| 832 | return autofs4_get_set_timeout(sbi, p); |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 833 | #ifdef CONFIG_COMPAT |
| 834 | case AUTOFS_IOC_SETTIMEOUT32: |
| 835 | return autofs4_compat_get_set_timeout(sbi, p); |
| 836 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | case AUTOFS_IOC_ASKUMOUNT: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 839 | return autofs4_ask_umount(filp->f_path.mnt, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | /* return a single thing to expire */ |
| 842 | case AUTOFS_IOC_EXPIRE: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 843 | return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | /* same as above, but can send multiple expires through pipe */ |
| 845 | case AUTOFS_IOC_EXPIRE_MULTI: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 846 | return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
| 848 | default: |
| 849 | return -ENOSYS; |
| 850 | } |
| 851 | } |
Frederic Weisbecker | 3663df7 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 852 | |
| 853 | static long autofs4_root_ioctl(struct file *filp, |
| 854 | unsigned int cmd, unsigned long arg) |
| 855 | { |
Frederic Weisbecker | 3663df7 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 856 | struct inode *inode = filp->f_dentry->d_inode; |
Ian Kent | de47de7 | 2010-12-07 13:04:00 +0800 | [diff] [blame] | 857 | return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg); |
Frederic Weisbecker | 3663df7 | 2010-05-19 15:08:17 +0200 | [diff] [blame] | 858 | } |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 859 | |
| 860 | #ifdef CONFIG_COMPAT |
| 861 | static long autofs4_root_compat_ioctl(struct file *filp, |
| 862 | unsigned int cmd, unsigned long arg) |
| 863 | { |
| 864 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 865 | int ret; |
| 866 | |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 867 | if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL) |
| 868 | ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg); |
| 869 | else |
| 870 | ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, |
| 871 | (unsigned long)compat_ptr(arg)); |
Arnd Bergmann | c9243f5 | 2010-07-04 00:15:07 +0200 | [diff] [blame] | 872 | |
| 873 | return ret; |
| 874 | } |
| 875 | #endif |