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