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> |
| 18 | #include <linux/param.h> |
| 19 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "autofs_i.h" |
| 21 | |
| 22 | static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *); |
| 23 | static int autofs4_dir_unlink(struct inode *,struct dentry *); |
| 24 | static int autofs4_dir_rmdir(struct inode *,struct dentry *); |
| 25 | static int autofs4_dir_mkdir(struct inode *,struct dentry *,int); |
| 26 | static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long); |
| 27 | static int autofs4_dir_open(struct inode *inode, struct file *file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 29 | static void *autofs4_follow_link(struct dentry *, struct nameidata *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Ian Kent | 6d5cb92 | 2008-07-23 21:30:15 -0700 | [diff] [blame] | 31 | #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) |
| 32 | #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE) |
| 33 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 34 | const struct file_operations autofs4_root_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .open = dcache_dir_open, |
| 36 | .release = dcache_dir_close, |
| 37 | .read = generic_read_dir, |
Ian Kent | aa55ddf | 2008-07-23 21:30:29 -0700 | [diff] [blame] | 38 | .readdir = dcache_readdir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 39 | .llseek = dcache_dir_lseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | .ioctl = autofs4_root_ioctl, |
| 41 | }; |
| 42 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 43 | const struct file_operations autofs4_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | .open = autofs4_dir_open, |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 45 | .release = dcache_dir_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | .read = generic_read_dir, |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 47 | .readdir = dcache_readdir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 48 | .llseek = dcache_dir_lseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 51 | const struct inode_operations autofs4_indirect_root_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | .lookup = autofs4_lookup, |
| 53 | .unlink = autofs4_dir_unlink, |
| 54 | .symlink = autofs4_dir_symlink, |
| 55 | .mkdir = autofs4_dir_mkdir, |
| 56 | .rmdir = autofs4_dir_rmdir, |
| 57 | }; |
| 58 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 59 | const struct inode_operations autofs4_direct_root_inode_operations = { |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 60 | .lookup = autofs4_lookup, |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 61 | .unlink = autofs4_dir_unlink, |
| 62 | .mkdir = autofs4_dir_mkdir, |
| 63 | .rmdir = autofs4_dir_rmdir, |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 64 | .follow_link = autofs4_follow_link, |
| 65 | }; |
| 66 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 67 | const struct inode_operations autofs4_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | .lookup = autofs4_lookup, |
| 69 | .unlink = autofs4_dir_unlink, |
| 70 | .symlink = autofs4_dir_symlink, |
| 71 | .mkdir = autofs4_dir_mkdir, |
| 72 | .rmdir = autofs4_dir_rmdir, |
| 73 | }; |
| 74 | |
Ian Kent | 4f8427d | 2009-12-15 16:45:42 -0800 | [diff] [blame] | 75 | static void autofs4_add_active(struct dentry *dentry) |
| 76 | { |
| 77 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 78 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 79 | if (ino) { |
| 80 | spin_lock(&sbi->lookup_lock); |
| 81 | if (!ino->active_count) { |
| 82 | if (list_empty(&ino->active)) |
| 83 | list_add(&ino->active, &sbi->active_list); |
| 84 | } |
| 85 | ino->active_count++; |
| 86 | spin_unlock(&sbi->lookup_lock); |
| 87 | } |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | static void autofs4_del_active(struct dentry *dentry) |
| 92 | { |
| 93 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 94 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 95 | if (ino) { |
| 96 | spin_lock(&sbi->lookup_lock); |
| 97 | ino->active_count--; |
| 98 | if (!ino->active_count) { |
| 99 | if (!list_empty(&ino->active)) |
| 100 | list_del_init(&ino->active); |
| 101 | } |
| 102 | spin_unlock(&sbi->lookup_lock); |
| 103 | } |
| 104 | return; |
| 105 | } |
| 106 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 107 | static void autofs4_add_rehash_entry(struct autofs_info *ino, |
| 108 | struct rehash_entry *entry) |
| 109 | { |
| 110 | entry->task = current; |
| 111 | INIT_LIST_HEAD(&entry->list); |
| 112 | list_add(&entry->list, &ino->rehash_list); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | static void autofs4_remove_rehash_entry(struct autofs_info *ino) |
| 117 | { |
| 118 | struct list_head *head = &ino->rehash_list; |
| 119 | struct rehash_entry *entry; |
| 120 | list_for_each_entry(entry, head, list) { |
| 121 | if (entry->task == current) { |
| 122 | list_del(&entry->list); |
| 123 | kfree(entry); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | static void autofs4_remove_rehash_entrys(struct autofs_info *ino) |
| 131 | { |
| 132 | struct autofs_sb_info *sbi = ino->sbi; |
| 133 | struct rehash_entry *entry, *next; |
| 134 | struct list_head *head; |
| 135 | |
| 136 | spin_lock(&sbi->fs_lock); |
| 137 | spin_lock(&sbi->lookup_lock); |
| 138 | if (!(ino->flags & AUTOFS_INF_REHASH)) { |
| 139 | spin_unlock(&sbi->lookup_lock); |
| 140 | spin_unlock(&sbi->fs_lock); |
| 141 | return; |
| 142 | } |
| 143 | ino->flags &= ~AUTOFS_INF_REHASH; |
| 144 | head = &ino->rehash_list; |
| 145 | list_for_each_entry_safe(entry, next, head, list) { |
| 146 | list_del(&entry->list); |
| 147 | kfree(entry); |
| 148 | } |
| 149 | spin_unlock(&sbi->lookup_lock); |
| 150 | spin_unlock(&sbi->fs_lock); |
| 151 | dput(ino->dentry); |
| 152 | |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | static void autofs4_revalidate_drop(struct dentry *dentry, |
| 157 | struct rehash_entry *entry) |
| 158 | { |
| 159 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 160 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 161 | /* |
| 162 | * Add to the active list so we can pick this up in |
| 163 | * ->lookup(). Also add an entry to a rehash list so |
| 164 | * we know when there are no dentrys in flight so we |
| 165 | * know when we can rehash the dentry. |
| 166 | */ |
| 167 | spin_lock(&sbi->lookup_lock); |
| 168 | if (list_empty(&ino->active)) |
| 169 | list_add(&ino->active, &sbi->active_list); |
| 170 | autofs4_add_rehash_entry(ino, entry); |
| 171 | spin_unlock(&sbi->lookup_lock); |
| 172 | if (!(ino->flags & AUTOFS_INF_REHASH)) { |
| 173 | ino->flags |= AUTOFS_INF_REHASH; |
| 174 | dget(dentry); |
| 175 | spin_lock(&dentry->d_lock); |
| 176 | __d_drop(dentry); |
| 177 | spin_unlock(&dentry->d_lock); |
| 178 | } |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | static void autofs4_revalidate_rehash(struct dentry *dentry) |
| 183 | { |
| 184 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 185 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 186 | if (ino->flags & AUTOFS_INF_REHASH) { |
| 187 | spin_lock(&sbi->lookup_lock); |
| 188 | autofs4_remove_rehash_entry(ino); |
| 189 | if (list_empty(&ino->rehash_list)) { |
| 190 | spin_unlock(&sbi->lookup_lock); |
| 191 | ino->flags &= ~AUTOFS_INF_REHASH; |
| 192 | d_rehash(dentry); |
| 193 | dput(ino->dentry); |
| 194 | } else |
| 195 | spin_unlock(&sbi->lookup_lock); |
| 196 | } |
| 197 | return; |
| 198 | } |
| 199 | |
Ian Kent | 36b6413 | 2009-12-15 16:45:44 -0800 | [diff] [blame] | 200 | static unsigned int autofs4_need_mount(unsigned int flags) |
| 201 | { |
| 202 | unsigned int res = 0; |
| 203 | if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS)) |
| 204 | res = 1; |
| 205 | return res; |
| 206 | } |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | static int autofs4_dir_open(struct inode *inode, struct file *file) |
| 209 | { |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 210 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | DPRINTK("file=%p dentry=%p %.*s", |
| 214 | file, dentry, dentry->d_name.len, dentry->d_name.name); |
| 215 | |
| 216 | if (autofs4_oz_mode(sbi)) |
| 217 | goto out; |
| 218 | |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 219 | /* |
| 220 | * An empty directory in an autofs file system is always a |
| 221 | * mount point. The daemon must have failed to mount this |
| 222 | * during lookup so it doesn't exist. This can happen, for |
| 223 | * example, if user space returns an incorrect status for a |
| 224 | * mount request. Otherwise we're doing a readdir on the |
| 225 | * autofs file system so just let the libfs routines handle |
| 226 | * it. |
| 227 | */ |
| 228 | spin_lock(&dcache_lock); |
Ian Kent | c42c7f7 | 2009-12-15 16:45:48 -0800 | [diff] [blame] | 229 | if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | spin_unlock(&dcache_lock); |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 231 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 233 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 235 | out: |
Ian Kent | ff9cd49 | 2008-07-23 21:30:24 -0700 | [diff] [blame] | 236 | return dcache_dir_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 239 | static int try_to_fill_dentry(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame] | 241 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 242 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Jeff Moyer | 9d2de6a | 2008-05-01 04:35:09 -0700 | [diff] [blame] | 243 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | DPRINTK("dentry=%p %.*s ino=%p", |
| 246 | dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode); |
| 247 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 248 | /* |
| 249 | * Wait for a pending mount, triggering one if there |
| 250 | * isn't one already |
| 251 | */ |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 252 | DPRINTK("waiting for mount name=%.*s", |
| 253 | dentry->d_name.len, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 255 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 256 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 257 | DPRINTK("mount done status=%d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 259 | /* Update expiry counter */ |
| 260 | ino->last_used = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 262 | return status; |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* For autofs direct mounts the follow link triggers the mount */ |
| 266 | static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 267 | { |
| 268 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | a537055 | 2006-05-15 09:43:51 -0700 | [diff] [blame] | 269 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 270 | int oz_mode = autofs4_oz_mode(sbi); |
| 271 | unsigned int lookup_type; |
| 272 | int status; |
| 273 | |
| 274 | DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d", |
| 275 | dentry, dentry->d_name.len, dentry->d_name.name, oz_mode, |
| 276 | nd->flags); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 277 | /* |
| 278 | * For an expire of a covered direct or offset mount we need |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 279 | * to break out of follow_down() at the autofs mount trigger |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 280 | * (d_mounted--), so we can see the expiring flag, and manage |
| 281 | * the blocking and following here until the expire is completed. |
| 282 | */ |
| 283 | if (oz_mode) { |
| 284 | spin_lock(&sbi->fs_lock); |
| 285 | if (ino->flags & AUTOFS_INF_EXPIRING) { |
| 286 | spin_unlock(&sbi->fs_lock); |
| 287 | /* Follow down to our covering mount. */ |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 288 | if (!follow_down(&nd->path)) |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 289 | goto done; |
Ian Kent | ec6e8c7 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 290 | goto follow; |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 291 | } |
| 292 | spin_unlock(&sbi->fs_lock); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 293 | goto done; |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 294 | } |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 295 | |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 296 | /* If an expire request is pending everyone must wait. */ |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 297 | autofs4_expire_wait(dentry); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 298 | |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 299 | /* We trigger a mount for almost all flags */ |
Ian Kent | 36b6413 | 2009-12-15 16:45:44 -0800 | [diff] [blame] | 300 | lookup_type = autofs4_need_mount(nd->flags); |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 301 | spin_lock(&sbi->fs_lock); |
| 302 | spin_lock(&dcache_lock); |
| 303 | if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) { |
| 304 | spin_unlock(&dcache_lock); |
| 305 | spin_unlock(&sbi->fs_lock); |
Ian Kent | ec6e8c7 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 306 | goto follow; |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 307 | } |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 308 | |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 309 | /* |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 310 | * If the dentry contains directories then it is an autofs |
| 311 | * multi-mount with no root mount offset. So don't try to |
| 312 | * mount it again. |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 313 | */ |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 314 | if (ino->flags & AUTOFS_INF_PENDING || |
Ian Kent | c42c7f7 | 2009-12-15 16:45:48 -0800 | [diff] [blame] | 315 | (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) { |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 316 | ino->flags |= AUTOFS_INF_PENDING; |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 317 | spin_unlock(&dcache_lock); |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 318 | spin_unlock(&sbi->fs_lock); |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 319 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 320 | status = try_to_fill_dentry(dentry); |
| 321 | |
| 322 | spin_lock(&sbi->fs_lock); |
| 323 | ino->flags &= ~AUTOFS_INF_PENDING; |
| 324 | spin_unlock(&sbi->fs_lock); |
| 325 | |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 326 | if (status) |
| 327 | goto out_error; |
| 328 | |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 329 | goto follow; |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 330 | } |
| 331 | spin_unlock(&dcache_lock); |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 332 | spin_unlock(&sbi->fs_lock); |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 333 | follow: |
| 334 | /* |
| 335 | * If there is no root mount it must be an autofs |
| 336 | * multi-mount with no root offset so we don't need |
| 337 | * to follow it. |
| 338 | */ |
| 339 | if (d_mountpoint(dentry)) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 340 | if (!autofs4_follow_mount(&nd->path)) { |
Ian Kent | 6e60a9a | 2008-07-23 21:30:27 -0700 | [diff] [blame] | 341 | status = -ENOENT; |
| 342 | goto out_error; |
| 343 | } |
| 344 | } |
Ian Kent | 871f943 | 2006-03-27 01:14:58 -0800 | [diff] [blame] | 345 | |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 346 | done: |
| 347 | return NULL; |
| 348 | |
| 349 | out_error: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 350 | path_put(&nd->path); |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 351 | return ERR_PTR(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Revalidate is called on every cache lookup. Some of those |
| 356 | * cache lookups may actually happen while the dentry is not |
| 357 | * yet completely filled in, and revalidate has to delay such |
| 358 | * lookups.. |
| 359 | */ |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 360 | static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 362 | struct inode *dir = dentry->d_parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 364 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
| 365 | struct rehash_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | int flags = nd ? nd->flags : 0; |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 367 | unsigned int mutex_aquired; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 369 | DPRINTK("name = %.*s oz_mode = %d", |
| 370 | dentry->d_name.len, dentry->d_name.name, oz_mode); |
| 371 | |
| 372 | /* Daemon never causes a mount to trigger */ |
| 373 | if (autofs4_oz_mode(sbi)) |
| 374 | return 1; |
| 375 | |
| 376 | entry = kmalloc(sizeof(struct rehash_entry), GFP_KERNEL); |
| 377 | if (!entry) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | mutex_aquired = mutex_trylock(&dir->i_mutex); |
| 381 | |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 382 | spin_lock(&sbi->fs_lock); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 383 | spin_lock(&dcache_lock); |
| 384 | /* Pending dentry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (autofs4_ispending(dentry)) { |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 386 | int status; |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 387 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 388 | /* |
| 389 | * We can only unhash and send this to ->lookup() if |
| 390 | * the directory mutex is held over d_revalidate() and |
| 391 | * ->lookup(). This prevents the VFS from incorrectly |
| 392 | * seeing the dentry as non-existent. |
| 393 | */ |
| 394 | ino->flags |= AUTOFS_INF_PENDING; |
| 395 | if (!mutex_aquired) { |
| 396 | autofs4_revalidate_drop(dentry, entry); |
| 397 | spin_unlock(&dcache_lock); |
| 398 | spin_unlock(&sbi->fs_lock); |
| 399 | return 0; |
| 400 | } |
| 401 | spin_unlock(&dcache_lock); |
| 402 | spin_unlock(&sbi->fs_lock); |
| 403 | mutex_unlock(&dir->i_mutex); |
| 404 | kfree(entry); |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 405 | |
| 406 | /* |
Ian Kent | 06a3598 | 2008-07-23 21:30:28 -0700 | [diff] [blame] | 407 | * If the directory has gone away due to an expire |
| 408 | * we have been called as ->d_revalidate() and so |
| 409 | * we need to return false and proceed to ->lookup(). |
| 410 | */ |
| 411 | if (autofs4_expire_wait(dentry) == -EAGAIN) |
| 412 | return 0; |
| 413 | |
| 414 | /* |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 415 | * A zero status is success otherwise we have a |
| 416 | * negative error code. |
| 417 | */ |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 418 | status = try_to_fill_dentry(dentry); |
| 419 | |
| 420 | spin_lock(&sbi->fs_lock); |
| 421 | ino->flags &= ~AUTOFS_INF_PENDING; |
| 422 | spin_unlock(&sbi->fs_lock); |
| 423 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 424 | if (status == 0) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 425 | return 1; |
| 426 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 427 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | /* Check for a non-mountpoint directory with no contents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (S_ISDIR(dentry->d_inode->i_mode) && |
Ian Kent | c42c7f7 | 2009-12-15 16:45:48 -0800 | [diff] [blame] | 432 | !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | DPRINTK("dentry=%p %.*s, emptydir", |
| 434 | dentry, dentry->d_name.len, dentry->d_name.name); |
Ian Kent | 97e7449 | 2008-07-23 21:30:26 -0700 | [diff] [blame] | 435 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 436 | if (autofs4_need_mount(flags) || current->link_count) { |
| 437 | int status; |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 438 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 439 | /* |
| 440 | * We can only unhash and send this to ->lookup() if |
| 441 | * the directory mutex is held over d_revalidate() and |
| 442 | * ->lookup(). This prevents the VFS from incorrectly |
| 443 | * seeing the dentry as non-existent. |
| 444 | */ |
| 445 | ino->flags |= AUTOFS_INF_PENDING; |
| 446 | if (!mutex_aquired) { |
| 447 | autofs4_revalidate_drop(dentry, entry); |
| 448 | spin_unlock(&dcache_lock); |
| 449 | spin_unlock(&sbi->fs_lock); |
| 450 | return 0; |
| 451 | } |
| 452 | spin_unlock(&dcache_lock); |
| 453 | spin_unlock(&sbi->fs_lock); |
| 454 | mutex_unlock(&dir->i_mutex); |
| 455 | kfree(entry); |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 456 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 457 | /* |
| 458 | * A zero status is success otherwise we have a |
| 459 | * negative error code. |
| 460 | */ |
| 461 | status = try_to_fill_dentry(dentry); |
| 462 | |
| 463 | spin_lock(&sbi->fs_lock); |
| 464 | ino->flags &= ~AUTOFS_INF_PENDING; |
| 465 | spin_unlock(&sbi->fs_lock); |
| 466 | |
| 467 | if (status == 0) |
| 468 | return 1; |
| 469 | |
| 470 | return status; |
| 471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | spin_unlock(&dcache_lock); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 474 | spin_unlock(&sbi->fs_lock); |
| 475 | |
| 476 | if (mutex_aquired) |
| 477 | mutex_unlock(&dir->i_mutex); |
| 478 | |
| 479 | kfree(entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | return 1; |
| 482 | } |
| 483 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 484 | static void autofs4_free_rehash_entrys(struct autofs_info *inf) |
| 485 | { |
| 486 | struct list_head *head = &inf->rehash_list; |
| 487 | struct rehash_entry *entry, *next; |
| 488 | list_for_each_entry_safe(entry, next, head, list) { |
| 489 | list_del(&entry->list); |
| 490 | kfree(entry); |
| 491 | } |
| 492 | } |
| 493 | |
Ian Kent | 34ca959 | 2006-03-27 01:14:54 -0800 | [diff] [blame] | 494 | void autofs4_dentry_release(struct dentry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | struct autofs_info *inf; |
| 497 | |
| 498 | DPRINTK("releasing %p", de); |
| 499 | |
| 500 | inf = autofs4_dentry_ino(de); |
| 501 | de->d_fsdata = NULL; |
| 502 | |
| 503 | if (inf) { |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 504 | struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb); |
| 505 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 506 | if (sbi) { |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 507 | spin_lock(&sbi->lookup_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 508 | if (!list_empty(&inf->active)) |
| 509 | list_del(&inf->active); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 510 | if (!list_empty(&inf->expiring)) |
| 511 | list_del(&inf->expiring); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 512 | if (!list_empty(&inf->rehash_list)) |
| 513 | autofs4_free_rehash_entrys(inf); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 514 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Jeff Mahoney | c3724b1 | 2007-04-11 23:28:46 -0700 | [diff] [blame] | 517 | inf->dentry = NULL; |
| 518 | inf->inode = NULL; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | autofs4_free_ino(inf); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /* For dentries of directories in the root dir */ |
Al Viro | 08f1151 | 2009-02-20 05:56:19 +0000 | [diff] [blame] | 525 | static const struct dentry_operations autofs4_root_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | .d_revalidate = autofs4_revalidate, |
| 527 | .d_release = autofs4_dentry_release, |
| 528 | }; |
| 529 | |
| 530 | /* For other dentries */ |
Al Viro | 08f1151 | 2009-02-20 05:56:19 +0000 | [diff] [blame] | 531 | static const struct dentry_operations autofs4_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | .d_revalidate = autofs4_revalidate, |
| 533 | .d_release = autofs4_dentry_release, |
| 534 | }; |
| 535 | |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 536 | static struct dentry *autofs4_lookup_active(struct dentry *dentry) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 537 | { |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 538 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 539 | struct dentry *parent = dentry->d_parent; |
| 540 | struct qstr *name = &dentry->d_name; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 541 | unsigned int len = name->len; |
| 542 | unsigned int hash = name->hash; |
| 543 | const unsigned char *str = name->name; |
| 544 | struct list_head *p, *head; |
| 545 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 546 | restart: |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 547 | spin_lock(&dcache_lock); |
| 548 | spin_lock(&sbi->lookup_lock); |
| 549 | head = &sbi->active_list; |
| 550 | list_for_each(p, head) { |
| 551 | struct autofs_info *ino; |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 552 | struct dentry *active; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 553 | struct qstr *qstr; |
| 554 | |
| 555 | ino = list_entry(p, struct autofs_info, active); |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 556 | active = ino->dentry; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 557 | |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 558 | spin_lock(&active->d_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 559 | |
| 560 | /* Already gone? */ |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 561 | if (atomic_read(&active->d_count) == 0) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 562 | goto next; |
| 563 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 564 | if (active->d_inode && IS_DEADDIR(active->d_inode)) { |
| 565 | if (!list_empty(&ino->rehash_list)) { |
| 566 | dget(active); |
| 567 | spin_unlock(&active->d_lock); |
| 568 | spin_unlock(&sbi->lookup_lock); |
| 569 | spin_unlock(&dcache_lock); |
| 570 | autofs4_remove_rehash_entrys(ino); |
| 571 | dput(active); |
| 572 | goto restart; |
| 573 | } |
| 574 | goto next; |
| 575 | } |
| 576 | |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 577 | qstr = &active->d_name; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 578 | |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 579 | if (active->d_name.hash != hash) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 580 | goto next; |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 581 | if (active->d_parent != parent) |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 582 | goto next; |
| 583 | |
| 584 | if (qstr->len != len) |
| 585 | goto next; |
| 586 | if (memcmp(qstr->name, str, len)) |
| 587 | goto next; |
| 588 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 589 | dget(active); |
| 590 | spin_unlock(&active->d_lock); |
| 591 | spin_unlock(&sbi->lookup_lock); |
| 592 | spin_unlock(&dcache_lock); |
| 593 | return active; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 594 | next: |
Ian Kent | e4d5ade | 2009-12-15 16:45:49 -0800 | [diff] [blame] | 595 | spin_unlock(&active->d_lock); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 596 | } |
| 597 | spin_unlock(&sbi->lookup_lock); |
| 598 | spin_unlock(&dcache_lock); |
| 599 | |
| 600 | return NULL; |
| 601 | } |
| 602 | |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 603 | static struct dentry *autofs4_lookup_expiring(struct dentry *dentry) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 604 | { |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 605 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
| 606 | struct dentry *parent = dentry->d_parent; |
| 607 | struct qstr *name = &dentry->d_name; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 608 | unsigned int len = name->len; |
| 609 | unsigned int hash = name->hash; |
| 610 | const unsigned char *str = name->name; |
| 611 | struct list_head *p, *head; |
| 612 | |
| 613 | spin_lock(&dcache_lock); |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 614 | spin_lock(&sbi->lookup_lock); |
| 615 | head = &sbi->expiring_list; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 616 | list_for_each(p, head) { |
| 617 | struct autofs_info *ino; |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 618 | struct dentry *expiring; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 619 | struct qstr *qstr; |
| 620 | |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 621 | ino = list_entry(p, struct autofs_info, expiring); |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 622 | expiring = ino->dentry; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 623 | |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 624 | spin_lock(&expiring->d_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 625 | |
| 626 | /* Bad luck, we've already been dentry_iput */ |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 627 | if (!expiring->d_inode) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 628 | goto next; |
| 629 | |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 630 | qstr = &expiring->d_name; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 631 | |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 632 | if (expiring->d_name.hash != hash) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 633 | goto next; |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 634 | if (expiring->d_parent != parent) |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 635 | goto next; |
| 636 | |
| 637 | if (qstr->len != len) |
| 638 | goto next; |
| 639 | if (memcmp(qstr->name, str, len)) |
| 640 | goto next; |
| 641 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 642 | dget(expiring); |
| 643 | spin_unlock(&expiring->d_lock); |
| 644 | spin_unlock(&sbi->lookup_lock); |
| 645 | spin_unlock(&dcache_lock); |
| 646 | return expiring; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 647 | next: |
Ian Kent | cb4b492 | 2009-12-15 16:45:50 -0800 | [diff] [blame] | 648 | spin_unlock(&expiring->d_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 649 | } |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 650 | spin_unlock(&sbi->lookup_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 651 | spin_unlock(&dcache_lock); |
| 652 | |
| 653 | return NULL; |
| 654 | } |
| 655 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 656 | static struct autofs_info *init_new_dentry(struct autofs_sb_info *sbi, |
| 657 | struct dentry *dentry, int oz_mode) |
| 658 | { |
| 659 | struct autofs_info *ino; |
| 660 | |
| 661 | /* |
| 662 | * Mark the dentry incomplete but don't hash it. We do this |
| 663 | * to serialize our inode creation operations (symlink and |
| 664 | * mkdir) which prevents deadlock during the callback to |
| 665 | * the daemon. Subsequent user space lookups for the same |
| 666 | * dentry are placed on the wait queue while the daemon |
| 667 | * itself is allowed passage unresticted so the create |
| 668 | * operation itself can then hash the dentry. Finally, |
| 669 | * we check for the hashed dentry and return the newly |
| 670 | * hashed dentry. |
| 671 | */ |
| 672 | dentry->d_op = &autofs4_root_dentry_operations; |
| 673 | |
| 674 | /* |
| 675 | * And we need to ensure that the same dentry is used for |
| 676 | * all following lookup calls until it is hashed so that |
| 677 | * the dentry flags are persistent throughout the request. |
| 678 | */ |
| 679 | ino = autofs4_init_ino(NULL, sbi, 0555); |
| 680 | if (!ino) |
| 681 | return ERR_PTR(-ENOMEM); |
| 682 | |
| 683 | dentry->d_fsdata = ino; |
| 684 | ino->dentry = dentry; |
| 685 | |
| 686 | /* |
| 687 | * Only set the mount pending flag for new dentrys not created |
| 688 | * by the daemon. |
| 689 | */ |
| 690 | if (!oz_mode) |
| 691 | ino->flags |= AUTOFS_INF_PENDING; |
| 692 | |
| 693 | d_instantiate(dentry, NULL); |
| 694 | |
| 695 | return ino; |
| 696 | } |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | /* Lookups in the root directory */ |
| 699 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 700 | { |
| 701 | struct autofs_sb_info *sbi; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 702 | struct autofs_info *ino; |
Ian Kent | 90387c9 | 2009-12-15 16:45:46 -0800 | [diff] [blame] | 703 | struct dentry *expiring, *active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | int oz_mode; |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 705 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | DPRINTK("name = %.*s", |
| 708 | dentry->d_name.len, dentry->d_name.name); |
| 709 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 710 | /* File name too long to exist */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (dentry->d_name.len > NAME_MAX) |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 712 | return ERR_PTR(-ENAMETOOLONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | sbi = autofs4_sbi(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | oz_mode = autofs4_oz_mode(sbi); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 718 | current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 720 | spin_lock(&sbi->fs_lock); |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 721 | active = autofs4_lookup_active(dentry); |
Ian Kent | 90387c9 | 2009-12-15 16:45:46 -0800 | [diff] [blame] | 722 | if (active) { |
| 723 | dentry = active; |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 724 | ino = autofs4_dentry_ino(dentry); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 725 | /* If this came from revalidate, rehash it */ |
| 726 | autofs4_revalidate_rehash(dentry); |
| 727 | spin_unlock(&sbi->fs_lock); |
Ian Kent | aa952eb | 2009-12-15 16:45:45 -0800 | [diff] [blame] | 728 | } else { |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 729 | spin_unlock(&sbi->fs_lock); |
| 730 | ino = init_new_dentry(sbi, dentry, oz_mode); |
| 731 | if (IS_ERR(ino)) |
| 732 | return (struct dentry *) ino; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 733 | } |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 734 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 735 | autofs4_add_active(dentry); |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (!oz_mode) { |
Ian Kent | 6510c9d | 2009-12-15 16:45:47 -0800 | [diff] [blame] | 738 | expiring = autofs4_lookup_expiring(dentry); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 739 | mutex_unlock(&dir->i_mutex); |
Ian Kent | 8f63aaa8b | 2009-03-31 15:24:45 -0700 | [diff] [blame] | 740 | if (expiring) { |
| 741 | /* |
| 742 | * If we are racing with expire the request might not |
| 743 | * be quite complete but the directory has been removed |
| 744 | * so it must have been successful, so just wait for it. |
| 745 | */ |
Ian Kent | 8f63aaa8b | 2009-03-31 15:24:45 -0700 | [diff] [blame] | 746 | autofs4_expire_wait(expiring); |
Ian Kent | 8f63aaa8b | 2009-03-31 15:24:45 -0700 | [diff] [blame] | 747 | dput(expiring); |
| 748 | } |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 749 | status = try_to_fill_dentry(dentry); |
Ian Kent | 8f63aaa8b | 2009-03-31 15:24:45 -0700 | [diff] [blame] | 750 | mutex_lock(&dir->i_mutex); |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 751 | spin_lock(&sbi->fs_lock); |
| 752 | ino->flags &= ~AUTOFS_INF_PENDING; |
| 753 | spin_unlock(&sbi->fs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 756 | autofs4_del_active(dentry); |
| 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | /* |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 759 | * If we had a mount fail, check if we had to handle |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * a signal. If so we can force a restart.. |
| 761 | */ |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 762 | if (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* See if we were interrupted */ |
| 764 | if (signal_pending(current)) { |
| 765 | sigset_t *sigset = ¤t->pending.signal; |
| 766 | if (sigismember (sigset, SIGKILL) || |
| 767 | sigismember (sigset, SIGQUIT) || |
| 768 | sigismember (sigset, SIGINT)) { |
Ian Kent | 90387c9 | 2009-12-15 16:45:46 -0800 | [diff] [blame] | 769 | if (active) |
| 770 | dput(active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return ERR_PTR(-ERESTARTNOINTR); |
| 772 | } |
| 773 | } |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * User space can (and has done in the past) remove and re-create |
| 778 | * this directory during the callback. This can leave us with an |
| 779 | * unhashed dentry, but a successful mount! So we need to |
| 780 | * perform another cached lookup in case the dentry now exists. |
| 781 | */ |
| 782 | if (!oz_mode && !have_submounts(dentry)) { |
| 783 | struct dentry *new; |
| 784 | new = d_lookup(dentry->d_parent, &dentry->d_name); |
| 785 | if (new) { |
| 786 | if (active) |
| 787 | dput(active); |
| 788 | return new; |
| 789 | } else { |
| 790 | if (!status) |
| 791 | status = -ENOENT; |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 796 | * If we had a mount failure, return status to user space. |
| 797 | * If the mount succeeded and we used a dentry from the active queue |
| 798 | * return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | */ |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 800 | if (status) { |
| 801 | dentry = ERR_PTR(status); |
Ian Kent | 90387c9 | 2009-12-15 16:45:46 -0800 | [diff] [blame] | 802 | if (active) |
| 803 | dput(active); |
Ian Kent | c9ffec4 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 804 | return dentry; |
Ian Kent | 213614d | 2009-12-15 16:45:51 -0800 | [diff] [blame] | 805 | } else { |
| 806 | /* |
| 807 | * Valid successful mount, return active dentry or NULL |
| 808 | * for a new dentry. |
| 809 | */ |
| 810 | if (active) |
| 811 | return active; |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return NULL; |
| 815 | } |
| 816 | |
| 817 | static int autofs4_dir_symlink(struct inode *dir, |
| 818 | struct dentry *dentry, |
| 819 | const char *symname) |
| 820 | { |
| 821 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 822 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 823 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | struct inode *inode; |
| 825 | char *cp; |
| 826 | |
| 827 | DPRINTK("%s <- %.*s", symname, |
| 828 | dentry->d_name.len, dentry->d_name.name); |
| 829 | |
| 830 | if (!autofs4_oz_mode(sbi)) |
| 831 | return -EACCES; |
| 832 | |
| 833 | ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 834 | if (!ino) |
| 835 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Ian Kent | ef581a7 | 2008-07-23 21:30:13 -0700 | [diff] [blame] | 837 | ino->size = strlen(symname); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 838 | cp = kmalloc(ino->size + 1, GFP_KERNEL); |
| 839 | if (!cp) { |
| 840 | if (!dentry->d_fsdata) |
| 841 | kfree(ino); |
| 842 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | strcpy(cp, symname); |
| 846 | |
| 847 | inode = autofs4_get_inode(dir->i_sb, ino); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 848 | if (!inode) { |
| 849 | kfree(cp); |
| 850 | if (!dentry->d_fsdata) |
| 851 | kfree(ino); |
| 852 | return -ENOMEM; |
| 853 | } |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 854 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | if (dir == dir->i_sb->s_root->d_inode) |
| 857 | dentry->d_op = &autofs4_root_dentry_operations; |
| 858 | else |
| 859 | dentry->d_op = &autofs4_dentry_operations; |
| 860 | |
| 861 | dentry->d_fsdata = ino; |
| 862 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 863 | atomic_inc(&ino->count); |
| 864 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 865 | if (p_ino && dentry->d_parent != dentry) |
| 866 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | ino->inode = inode; |
| 868 | |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 869 | ino->u.symlink = cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | dir->i_mtime = CURRENT_TIME; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * NOTE! |
| 877 | * |
| 878 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache |
| 879 | * that the file no longer exists. However, doing that means that the |
| 880 | * 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] | 881 | * this, because the unlink is probably the result of an expire. |
Ian Kent | 5f6f4f2 | 2008-07-23 21:30:09 -0700 | [diff] [blame] | 882 | * We simply d_drop it and add it to a expiring list in the super block, |
| 883 | * which allows the dentry lookup to check for an incomplete expire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | * |
| 885 | * If a process is blocked on the dentry waiting for the expire to finish, |
| 886 | * it will invalidate the dentry and try to mount with a new one. |
| 887 | * |
| 888 | * Also see autofs4_dir_rmdir().. |
| 889 | */ |
| 890 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) |
| 891 | { |
| 892 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 893 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 894 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | /* This allows root to remove symlinks */ |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 897 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | return -EACCES; |
| 899 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 900 | if (atomic_dec_and_test(&ino->count)) { |
| 901 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 902 | if (p_ino && dentry->d_parent != dentry) |
| 903 | atomic_dec(&p_ino->count); |
| 904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | dput(ino->dentry); |
| 906 | |
| 907 | dentry->d_inode->i_size = 0; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 908 | clear_nlink(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
| 910 | dir->i_mtime = CURRENT_TIME; |
| 911 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 912 | spin_lock(&dcache_lock); |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 913 | spin_lock(&dentry->d_lock); |
| 914 | __d_drop(dentry); |
| 915 | spin_unlock(&dentry->d_lock); |
| 916 | spin_unlock(&dcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) |
| 922 | { |
| 923 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 924 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 925 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Ian Kent | f50b6f8 | 2007-02-20 13:58:10 -0800 | [diff] [blame] | 927 | DPRINTK("dentry %p, removing %.*s", |
| 928 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (!autofs4_oz_mode(sbi)) |
| 931 | return -EACCES; |
| 932 | |
| 933 | spin_lock(&dcache_lock); |
| 934 | if (!list_empty(&dentry->d_subdirs)) { |
| 935 | spin_unlock(&dcache_lock); |
| 936 | return -ENOTEMPTY; |
| 937 | } |
| 938 | spin_lock(&dentry->d_lock); |
| 939 | __d_drop(dentry); |
| 940 | spin_unlock(&dentry->d_lock); |
| 941 | spin_unlock(&dcache_lock); |
| 942 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 943 | if (atomic_dec_and_test(&ino->count)) { |
| 944 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 945 | if (p_ino && dentry->d_parent != dentry) |
| 946 | atomic_dec(&p_ino->count); |
| 947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | dput(ino->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | dentry->d_inode->i_size = 0; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 950 | clear_nlink(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | if (dir->i_nlink) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 953 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 959 | { |
| 960 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 961 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 962 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | struct inode *inode; |
| 964 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 965 | if (!autofs4_oz_mode(sbi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return -EACCES; |
| 967 | |
| 968 | DPRINTK("dentry %p, creating %.*s", |
| 969 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 970 | |
| 971 | ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 972 | if (!ino) |
| 973 | return -ENOMEM; |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | inode = autofs4_get_inode(dir->i_sb, ino); |
Ian Kent | 2576737 | 2008-07-23 21:30:12 -0700 | [diff] [blame] | 976 | if (!inode) { |
| 977 | if (!dentry->d_fsdata) |
| 978 | kfree(ino); |
| 979 | return -ENOMEM; |
| 980 | } |
Ian Kent | 1864f7b | 2007-08-22 14:01:54 -0700 | [diff] [blame] | 981 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
| 983 | if (dir == dir->i_sb->s_root->d_inode) |
| 984 | dentry->d_op = &autofs4_root_dentry_operations; |
| 985 | else |
| 986 | dentry->d_op = &autofs4_dentry_operations; |
| 987 | |
| 988 | dentry->d_fsdata = ino; |
| 989 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 990 | atomic_inc(&ino->count); |
| 991 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 992 | if (p_ino && dentry->d_parent != dentry) |
| 993 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | ino->inode = inode; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 995 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | dir->i_mtime = CURRENT_TIME; |
| 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | /* Get/set timeout ioctl() operation */ |
| 1002 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, |
| 1003 | unsigned long __user *p) |
| 1004 | { |
| 1005 | int rv; |
| 1006 | unsigned long ntimeout; |
| 1007 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 1008 | if ((rv = get_user(ntimeout, p)) || |
| 1009 | (rv = put_user(sbi->exp_timeout/HZ, p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | return rv; |
| 1011 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 1012 | if (ntimeout > ULONG_MAX/HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | sbi->exp_timeout = 0; |
| 1014 | else |
| 1015 | sbi->exp_timeout = ntimeout * HZ; |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /* Return protocol version */ |
| 1021 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) |
| 1022 | { |
| 1023 | return put_user(sbi->version, p); |
| 1024 | } |
| 1025 | |
| 1026 | /* Return protocol sub version */ |
| 1027 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) |
| 1028 | { |
| 1029 | return put_user(sbi->sub_version, p); |
| 1030 | } |
| 1031 | |
| 1032 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | * Tells the daemon whether it can umount the autofs mount. |
| 1034 | */ |
| 1035 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) |
| 1036 | { |
| 1037 | int status = 0; |
| 1038 | |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 1039 | if (may_umount(mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | status = 1; |
| 1041 | |
| 1042 | DPRINTK("returning %d", status); |
| 1043 | |
| 1044 | status = put_user(status, p); |
| 1045 | |
| 1046 | return status; |
| 1047 | } |
| 1048 | |
| 1049 | /* Identify autofs4_dentries - this is so we can tell if there's |
| 1050 | an extra dentry refcount or not. We only hold a refcount on the |
| 1051 | dentry if its non-negative (ie, d_inode != NULL) |
| 1052 | */ |
| 1053 | int is_autofs4_dentry(struct dentry *dentry) |
| 1054 | { |
| 1055 | return dentry && dentry->d_inode && |
| 1056 | (dentry->d_op == &autofs4_root_dentry_operations || |
| 1057 | dentry->d_op == &autofs4_dentry_operations) && |
| 1058 | dentry->d_fsdata != NULL; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * ioctl()'s on the root directory is the chief method for the daemon to |
| 1063 | * generate kernel reactions |
| 1064 | */ |
| 1065 | static int autofs4_root_ioctl(struct inode *inode, struct file *filp, |
| 1066 | unsigned int cmd, unsigned long arg) |
| 1067 | { |
| 1068 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); |
| 1069 | void __user *p = (void __user *)arg; |
| 1070 | |
| 1071 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 1072 | cmd,arg,sbi,task_pgrp_nr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 1074 | if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || |
| 1075 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | return -ENOTTY; |
| 1077 | |
Sukadev Bhattiprolu | d78e53c | 2007-05-10 22:23:06 -0700 | [diff] [blame] | 1078 | if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | return -EPERM; |
| 1080 | |
| 1081 | switch(cmd) { |
| 1082 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ |
| 1083 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); |
| 1084 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ |
| 1085 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); |
| 1086 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ |
| 1087 | autofs4_catatonic_mode(sbi); |
| 1088 | return 0; |
| 1089 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ |
| 1090 | return autofs4_get_protover(sbi, p); |
| 1091 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ |
| 1092 | return autofs4_get_protosubver(sbi, p); |
| 1093 | case AUTOFS_IOC_SETTIMEOUT: |
| 1094 | return autofs4_get_set_timeout(sbi, p); |
| 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | case AUTOFS_IOC_ASKUMOUNT: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 1097 | return autofs4_ask_umount(filp->f_path.mnt, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
| 1099 | /* return a single thing to expire */ |
| 1100 | case AUTOFS_IOC_EXPIRE: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 1101 | 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] | 1102 | /* same as above, but can send multiple expires through pipe */ |
| 1103 | case AUTOFS_IOC_EXPIRE_MULTI: |
Josef "Jeff" Sipek | a4669ed | 2006-12-08 02:36:46 -0800 | [diff] [blame] | 1104 | 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] | 1105 | |
| 1106 | default: |
| 1107 | return -ENOSYS; |
| 1108 | } |
| 1109 | } |