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