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