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