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