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