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