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