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