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