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> |
| 7 | * Copyright 2001-2003 Ian Kent <raven@themaw.net> |
| 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 *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | struct file_operations autofs4_root_operations = { |
| 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 | |
| 42 | struct file_operations autofs4_dir_operations = { |
| 43 | .open = autofs4_dir_open, |
| 44 | .release = autofs4_dir_close, |
| 45 | .read = generic_read_dir, |
| 46 | .readdir = autofs4_dir_readdir, |
| 47 | }; |
| 48 | |
| 49 | struct inode_operations autofs4_root_inode_operations = { |
| 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 | |
| 57 | struct inode_operations autofs4_dir_inode_operations = { |
| 58 | .lookup = autofs4_lookup, |
| 59 | .unlink = autofs4_dir_unlink, |
| 60 | .symlink = autofs4_dir_symlink, |
| 61 | .mkdir = autofs4_dir_mkdir, |
| 62 | .rmdir = autofs4_dir_rmdir, |
| 63 | }; |
| 64 | |
| 65 | static int autofs4_root_readdir(struct file *file, void *dirent, |
| 66 | filldir_t filldir) |
| 67 | { |
| 68 | struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb); |
| 69 | int oz_mode = autofs4_oz_mode(sbi); |
| 70 | |
| 71 | DPRINTK("called, filp->f_pos = %lld", file->f_pos); |
| 72 | |
| 73 | /* |
| 74 | * Don't set reghost flag if: |
| 75 | * 1) f_pos is larger than zero -- we've already been here. |
| 76 | * 2) we haven't even enabled reghosting in the 1st place. |
| 77 | * 3) this is the daemon doing a readdir |
| 78 | */ |
| 79 | if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled) |
| 80 | sbi->needs_reghost = 1; |
| 81 | |
| 82 | DPRINTK("needs_reghost = %d", sbi->needs_reghost); |
| 83 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 84 | return dcache_readdir(file, dirent, filldir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static int autofs4_dir_open(struct inode *inode, struct file *file) |
| 88 | { |
| 89 | struct dentry *dentry = file->f_dentry; |
| 90 | struct vfsmount *mnt = file->f_vfsmnt; |
| 91 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 92 | struct dentry *cursor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | int status; |
| 94 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 95 | status = dcache_dir_open(inode, file); |
| 96 | if (status) |
| 97 | goto out; |
| 98 | |
| 99 | cursor = file->private_data; |
| 100 | cursor->d_fsdata = NULL; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | DPRINTK("file=%p dentry=%p %.*s", |
| 103 | file, dentry, dentry->d_name.len, dentry->d_name.name); |
| 104 | |
| 105 | if (autofs4_oz_mode(sbi)) |
| 106 | goto out; |
| 107 | |
| 108 | if (autofs4_ispending(dentry)) { |
| 109 | DPRINTK("dentry busy"); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 110 | dcache_dir_close(inode, file); |
| 111 | status = -EBUSY; |
| 112 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 115 | status = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) { |
| 117 | struct nameidata nd; |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 118 | int empty, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | /* In case there are stale directory dentrys from a failed mount */ |
| 121 | spin_lock(&dcache_lock); |
| 122 | empty = list_empty(&dentry->d_subdirs); |
| 123 | spin_unlock(&dcache_lock); |
| 124 | |
| 125 | if (!empty) |
| 126 | d_invalidate(dentry); |
| 127 | |
| 128 | nd.flags = LOOKUP_DIRECTORY; |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 129 | ret = (dentry->d_op->d_revalidate)(dentry, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 131 | if (!ret) { |
| 132 | dcache_dir_close(inode, file); |
| 133 | goto out; |
| 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (d_mountpoint(dentry)) { |
| 138 | struct file *fp = NULL; |
| 139 | struct vfsmount *fp_mnt = mntget(mnt); |
| 140 | struct dentry *fp_dentry = dget(dentry); |
| 141 | |
Ian Kent | 9b1e3af | 2005-06-21 17:16:38 -0700 | [diff] [blame] | 142 | if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) { |
| 143 | dput(fp_dentry); |
| 144 | mntput(fp_mnt); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 145 | dcache_dir_close(inode, file); |
| 146 | goto out; |
Ian Kent | 9b1e3af | 2005-06-21 17:16:38 -0700 | [diff] [blame] | 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | fp = dentry_open(fp_dentry, fp_mnt, file->f_flags); |
| 150 | status = PTR_ERR(fp); |
| 151 | if (IS_ERR(fp)) { |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 152 | dcache_dir_close(inode, file); |
| 153 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 155 | cursor->d_fsdata = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return 0; |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 158 | out: |
| 159 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int autofs4_dir_close(struct inode *inode, struct file *file) |
| 163 | { |
| 164 | struct dentry *dentry = file->f_dentry; |
| 165 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 166 | struct dentry *cursor = file->private_data; |
| 167 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | DPRINTK("file=%p dentry=%p %.*s", |
| 170 | file, dentry, dentry->d_name.len, dentry->d_name.name); |
| 171 | |
| 172 | if (autofs4_oz_mode(sbi)) |
| 173 | goto out; |
| 174 | |
| 175 | if (autofs4_ispending(dentry)) { |
| 176 | DPRINTK("dentry busy"); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 177 | status = -EBUSY; |
| 178 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (d_mountpoint(dentry)) { |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 182 | struct file *fp = cursor->d_fsdata; |
| 183 | if (!fp) { |
| 184 | status = -ENOENT; |
| 185 | goto out; |
| 186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | filp_close(fp, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | out: |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 190 | dcache_dir_close(inode, file); |
| 191 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir) |
| 195 | { |
| 196 | struct dentry *dentry = file->f_dentry; |
| 197 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 198 | struct dentry *cursor = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | int status; |
| 200 | |
| 201 | DPRINTK("file=%p dentry=%p %.*s", |
| 202 | file, dentry, dentry->d_name.len, dentry->d_name.name); |
| 203 | |
| 204 | if (autofs4_oz_mode(sbi)) |
| 205 | goto out; |
| 206 | |
| 207 | if (autofs4_ispending(dentry)) { |
| 208 | DPRINTK("dentry busy"); |
| 209 | return -EBUSY; |
| 210 | } |
| 211 | |
| 212 | if (d_mountpoint(dentry)) { |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 213 | struct file *fp = cursor->d_fsdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | if (!fp) |
| 216 | return -ENOENT; |
| 217 | |
| 218 | if (!fp->f_op || !fp->f_op->readdir) |
| 219 | goto out; |
| 220 | |
| 221 | status = vfs_readdir(fp, filldir, dirent); |
| 222 | file->f_pos = fp->f_pos; |
| 223 | if (status) |
| 224 | autofs4_copy_atime(file, fp); |
| 225 | return status; |
| 226 | } |
| 227 | out: |
Ian Kent | f360ce3 | 2006-03-27 01:14:43 -0800 | [diff] [blame] | 228 | return dcache_readdir(file, dirent, filldir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame^] | 231 | static int try_to_fill_dentry(struct dentry *dentry, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame^] | 233 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 234 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | int status = 0; |
| 236 | |
| 237 | /* Block on any pending expiry here; invalidate the dentry |
| 238 | when expiration is done to trigger mount request with a new |
| 239 | dentry */ |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 240 | if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | DPRINTK("waiting for expire %p name=%.*s", |
| 242 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 243 | |
| 244 | status = autofs4_wait(sbi, dentry, NFY_NONE); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | DPRINTK("expire done status=%d", status); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 247 | |
Ian Kent | 1684b2b | 2005-06-21 17:16:41 -0700 | [diff] [blame] | 248 | /* |
| 249 | * If the directory still exists the mount request must |
| 250 | * continue otherwise it can't be followed at the right |
| 251 | * time during the walk. |
| 252 | */ |
| 253 | status = d_invalidate(dentry); |
| 254 | if (status != -EBUSY) |
| 255 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | DPRINTK("dentry=%p %.*s ino=%p", |
| 259 | dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode); |
| 260 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 261 | /* |
| 262 | * Wait for a pending mount, triggering one if there |
| 263 | * isn't one already |
| 264 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | if (dentry->d_inode == NULL) { |
| 266 | DPRINTK("waiting for mount name=%.*s", |
| 267 | dentry->d_name.len, dentry->d_name.name); |
| 268 | |
| 269 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | DPRINTK("mount done status=%d", status); |
| 272 | |
| 273 | if (status && dentry->d_inode) |
| 274 | return 0; /* Try to get the kernel to invalidate this dentry */ |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* Turn this into a real negative dentry? */ |
| 277 | if (status == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | spin_lock(&dentry->d_lock); |
| 279 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 280 | spin_unlock(&dentry->d_lock); |
Ian Kent | 2d753e6 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 281 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } else if (status) { |
| 283 | /* Return a negative dentry, but leave it "pending" */ |
Ian Kent | 2d753e6 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 284 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | /* Trigger mount for path component or follow link */ |
| 287 | } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) || |
| 288 | current->link_count) { |
| 289 | DPRINTK("waiting for mount name=%.*s", |
| 290 | dentry->d_name.len, dentry->d_name.name); |
| 291 | |
| 292 | spin_lock(&dentry->d_lock); |
| 293 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; |
| 294 | spin_unlock(&dentry->d_lock); |
| 295 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); |
| 296 | |
| 297 | DPRINTK("mount done status=%d", status); |
| 298 | |
| 299 | if (status) { |
| 300 | spin_lock(&dentry->d_lock); |
| 301 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 302 | spin_unlock(&dentry->d_lock); |
| 303 | return 0; |
| 304 | } |
| 305 | } |
| 306 | |
Ian Kent | e0a7aae | 2006-03-27 01:14:47 -0800 | [diff] [blame] | 307 | /* Initialize expiry counter after successful mount */ |
| 308 | if (ino) |
| 309 | ino->last_used = jiffies; |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | spin_lock(&dentry->d_lock); |
| 312 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; |
| 313 | spin_unlock(&dentry->d_lock); |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Revalidate is called on every cache lookup. Some of those |
| 319 | * cache lookups may actually happen while the dentry is not |
| 320 | * yet completely filled in, and revalidate has to delay such |
| 321 | * lookups.. |
| 322 | */ |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 323 | static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 325 | struct inode *dir = dentry->d_parent->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 327 | int oz_mode = autofs4_oz_mode(sbi); |
| 328 | int flags = nd ? nd->flags : 0; |
| 329 | int status = 1; |
| 330 | |
| 331 | /* Pending dentry */ |
| 332 | if (autofs4_ispending(dentry)) { |
| 333 | if (!oz_mode) |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame^] | 334 | status = try_to_fill_dentry(dentry, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return status; |
| 336 | } |
| 337 | |
| 338 | /* Negative dentry.. invalidate if "old" */ |
| 339 | if (dentry->d_inode == NULL) |
Ian Kent | 2d753e6 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 340 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | /* Check for a non-mountpoint directory with no contents */ |
| 343 | spin_lock(&dcache_lock); |
| 344 | if (S_ISDIR(dentry->d_inode->i_mode) && |
| 345 | !d_mountpoint(dentry) && |
Ian Kent | 2d753e6 | 2006-03-27 01:14:44 -0800 | [diff] [blame] | 346 | simple_empty_nolock(dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | DPRINTK("dentry=%p %.*s, emptydir", |
| 348 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 349 | spin_unlock(&dcache_lock); |
| 350 | if (!oz_mode) |
Ian Kent | 862b110 | 2006-03-27 01:14:48 -0800 | [diff] [blame^] | 351 | status = try_to_fill_dentry(dentry, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return status; |
| 353 | } |
| 354 | spin_unlock(&dcache_lock); |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | static void autofs4_dentry_release(struct dentry *de) |
| 360 | { |
| 361 | struct autofs_info *inf; |
| 362 | |
| 363 | DPRINTK("releasing %p", de); |
| 364 | |
| 365 | inf = autofs4_dentry_ino(de); |
| 366 | de->d_fsdata = NULL; |
| 367 | |
| 368 | if (inf) { |
| 369 | inf->dentry = NULL; |
| 370 | inf->inode = NULL; |
| 371 | |
| 372 | autofs4_free_ino(inf); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* For dentries of directories in the root dir */ |
| 377 | static struct dentry_operations autofs4_root_dentry_operations = { |
| 378 | .d_revalidate = autofs4_revalidate, |
| 379 | .d_release = autofs4_dentry_release, |
| 380 | }; |
| 381 | |
| 382 | /* For other dentries */ |
| 383 | static struct dentry_operations autofs4_dentry_operations = { |
| 384 | .d_revalidate = autofs4_revalidate, |
| 385 | .d_release = autofs4_dentry_release, |
| 386 | }; |
| 387 | |
| 388 | /* Lookups in the root directory */ |
| 389 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 390 | { |
| 391 | struct autofs_sb_info *sbi; |
| 392 | int oz_mode; |
| 393 | |
| 394 | DPRINTK("name = %.*s", |
| 395 | dentry->d_name.len, dentry->d_name.name); |
| 396 | |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 397 | /* File name too long to exist */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (dentry->d_name.len > NAME_MAX) |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 399 | return ERR_PTR(-ENAMETOOLONG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | sbi = autofs4_sbi(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | oz_mode = autofs4_oz_mode(sbi); |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
| 405 | current->pid, process_group(current), sbi->catatonic, oz_mode); |
| 406 | |
| 407 | /* |
| 408 | * Mark the dentry incomplete, but add it. This is needed so |
| 409 | * that the VFS layer knows about the dentry, and we can count |
| 410 | * on catching any lookups through the revalidate. |
| 411 | * |
| 412 | * Let all the hard work be done by the revalidate function that |
| 413 | * needs to be able to do this anyway.. |
| 414 | * |
| 415 | * We need to do this before we release the directory semaphore. |
| 416 | */ |
| 417 | dentry->d_op = &autofs4_root_dentry_operations; |
| 418 | |
| 419 | if (!oz_mode) { |
| 420 | spin_lock(&dentry->d_lock); |
| 421 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; |
| 422 | spin_unlock(&dentry->d_lock); |
| 423 | } |
| 424 | dentry->d_fsdata = NULL; |
| 425 | d_add(dentry, NULL); |
| 426 | |
| 427 | if (dentry->d_op && dentry->d_op->d_revalidate) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 428 | mutex_unlock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | (dentry->d_op->d_revalidate)(dentry, nd); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 430 | mutex_lock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* |
| 434 | * If we are still pending, check if we had to handle |
| 435 | * a signal. If so we can force a restart.. |
| 436 | */ |
| 437 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { |
| 438 | /* See if we were interrupted */ |
| 439 | if (signal_pending(current)) { |
| 440 | sigset_t *sigset = ¤t->pending.signal; |
| 441 | if (sigismember (sigset, SIGKILL) || |
| 442 | sigismember (sigset, SIGQUIT) || |
| 443 | sigismember (sigset, SIGINT)) { |
| 444 | return ERR_PTR(-ERESTARTNOINTR); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * If this dentry is unhashed, then we shouldn't honour this |
| 451 | * lookup even if the dentry is positive. Returning ENOENT here |
| 452 | * doesn't do the right thing for all system calls, but it should |
| 453 | * be OK for the operations we permit from an autofs. |
| 454 | */ |
Ian Kent | 718c604 | 2006-03-27 01:14:42 -0800 | [diff] [blame] | 455 | if (dentry->d_inode && d_unhashed(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return ERR_PTR(-ENOENT); |
| 457 | |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | static int autofs4_dir_symlink(struct inode *dir, |
| 462 | struct dentry *dentry, |
| 463 | const char *symname) |
| 464 | { |
| 465 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 466 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 467 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | struct inode *inode; |
| 469 | char *cp; |
| 470 | |
| 471 | DPRINTK("%s <- %.*s", symname, |
| 472 | dentry->d_name.len, dentry->d_name.name); |
| 473 | |
| 474 | if (!autofs4_oz_mode(sbi)) |
| 475 | return -EACCES; |
| 476 | |
| 477 | ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); |
| 478 | if (ino == NULL) |
| 479 | return -ENOSPC; |
| 480 | |
| 481 | ino->size = strlen(symname); |
| 482 | ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL); |
| 483 | |
| 484 | if (cp == NULL) { |
| 485 | kfree(ino); |
| 486 | return -ENOSPC; |
| 487 | } |
| 488 | |
| 489 | strcpy(cp, symname); |
| 490 | |
| 491 | inode = autofs4_get_inode(dir->i_sb, ino); |
| 492 | d_instantiate(dentry, inode); |
| 493 | |
| 494 | if (dir == dir->i_sb->s_root->d_inode) |
| 495 | dentry->d_op = &autofs4_root_dentry_operations; |
| 496 | else |
| 497 | dentry->d_op = &autofs4_dentry_operations; |
| 498 | |
| 499 | dentry->d_fsdata = ino; |
| 500 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 501 | atomic_inc(&ino->count); |
| 502 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 503 | if (p_ino && dentry->d_parent != dentry) |
| 504 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | ino->inode = inode; |
| 506 | |
| 507 | dir->i_mtime = CURRENT_TIME; |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * NOTE! |
| 514 | * |
| 515 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache |
| 516 | * that the file no longer exists. However, doing that means that the |
| 517 | * VFS layer can turn the dentry into a negative dentry. We don't want |
| 518 | * this, because since the unlink is probably the result of an expire. |
| 519 | * We simply d_drop it, which allows the dentry lookup to remount it |
| 520 | * if necessary. |
| 521 | * |
| 522 | * If a process is blocked on the dentry waiting for the expire to finish, |
| 523 | * it will invalidate the dentry and try to mount with a new one. |
| 524 | * |
| 525 | * Also see autofs4_dir_rmdir().. |
| 526 | */ |
| 527 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) |
| 528 | { |
| 529 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 530 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 531 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
| 533 | /* This allows root to remove symlinks */ |
| 534 | if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) |
| 535 | return -EACCES; |
| 536 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 537 | if (atomic_dec_and_test(&ino->count)) { |
| 538 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 539 | if (p_ino && dentry->d_parent != dentry) |
| 540 | atomic_dec(&p_ino->count); |
| 541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | dput(ino->dentry); |
| 543 | |
| 544 | dentry->d_inode->i_size = 0; |
| 545 | dentry->d_inode->i_nlink = 0; |
| 546 | |
| 547 | dir->i_mtime = CURRENT_TIME; |
| 548 | |
| 549 | d_drop(dentry); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) |
| 555 | { |
| 556 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 557 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 558 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | if (!autofs4_oz_mode(sbi)) |
| 561 | return -EACCES; |
| 562 | |
| 563 | spin_lock(&dcache_lock); |
| 564 | if (!list_empty(&dentry->d_subdirs)) { |
| 565 | spin_unlock(&dcache_lock); |
| 566 | return -ENOTEMPTY; |
| 567 | } |
| 568 | spin_lock(&dentry->d_lock); |
| 569 | __d_drop(dentry); |
| 570 | spin_unlock(&dentry->d_lock); |
| 571 | spin_unlock(&dcache_lock); |
| 572 | |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 573 | if (atomic_dec_and_test(&ino->count)) { |
| 574 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 575 | if (p_ino && dentry->d_parent != dentry) |
| 576 | atomic_dec(&p_ino->count); |
| 577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | dput(ino->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | dentry->d_inode->i_size = 0; |
| 580 | dentry->d_inode->i_nlink = 0; |
| 581 | |
| 582 | if (dir->i_nlink) |
| 583 | dir->i_nlink--; |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 589 | { |
| 590 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
| 591 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 592 | struct autofs_info *p_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | struct inode *inode; |
| 594 | |
| 595 | if ( !autofs4_oz_mode(sbi) ) |
| 596 | return -EACCES; |
| 597 | |
| 598 | DPRINTK("dentry %p, creating %.*s", |
| 599 | dentry, dentry->d_name.len, dentry->d_name.name); |
| 600 | |
| 601 | ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); |
| 602 | if (ino == NULL) |
| 603 | return -ENOSPC; |
| 604 | |
| 605 | inode = autofs4_get_inode(dir->i_sb, ino); |
| 606 | d_instantiate(dentry, inode); |
| 607 | |
| 608 | if (dir == dir->i_sb->s_root->d_inode) |
| 609 | dentry->d_op = &autofs4_root_dentry_operations; |
| 610 | else |
| 611 | dentry->d_op = &autofs4_dentry_operations; |
| 612 | |
| 613 | dentry->d_fsdata = ino; |
| 614 | ino->dentry = dget(dentry); |
Ian Kent | 1aff3c8 | 2006-03-27 01:14:46 -0800 | [diff] [blame] | 615 | atomic_inc(&ino->count); |
| 616 | p_ino = autofs4_dentry_ino(dentry->d_parent); |
| 617 | if (p_ino && dentry->d_parent != dentry) |
| 618 | atomic_inc(&p_ino->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | ino->inode = inode; |
| 620 | dir->i_nlink++; |
| 621 | dir->i_mtime = CURRENT_TIME; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* Get/set timeout ioctl() operation */ |
| 627 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, |
| 628 | unsigned long __user *p) |
| 629 | { |
| 630 | int rv; |
| 631 | unsigned long ntimeout; |
| 632 | |
| 633 | if ( (rv = get_user(ntimeout, p)) || |
| 634 | (rv = put_user(sbi->exp_timeout/HZ, p)) ) |
| 635 | return rv; |
| 636 | |
| 637 | if ( ntimeout > ULONG_MAX/HZ ) |
| 638 | sbi->exp_timeout = 0; |
| 639 | else |
| 640 | sbi->exp_timeout = ntimeout * HZ; |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* Return protocol version */ |
| 646 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) |
| 647 | { |
| 648 | return put_user(sbi->version, p); |
| 649 | } |
| 650 | |
| 651 | /* Return protocol sub version */ |
| 652 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) |
| 653 | { |
| 654 | return put_user(sbi->sub_version, p); |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Tells the daemon whether we need to reghost or not. Also, clears |
| 659 | * the reghost_needed flag. |
| 660 | */ |
| 661 | static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p) |
| 662 | { |
| 663 | int status; |
| 664 | |
| 665 | DPRINTK("returning %d", sbi->needs_reghost); |
| 666 | |
| 667 | status = put_user(sbi->needs_reghost, p); |
| 668 | if ( status ) |
| 669 | return status; |
| 670 | |
| 671 | sbi->needs_reghost = 0; |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Enable / Disable reghosting ioctl() operation |
| 677 | */ |
| 678 | static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p) |
| 679 | { |
| 680 | int status; |
| 681 | int val; |
| 682 | |
| 683 | status = get_user(val, p); |
| 684 | |
| 685 | DPRINTK("reghost = %d", val); |
| 686 | |
| 687 | if (status) |
| 688 | return status; |
| 689 | |
| 690 | /* turn on/off reghosting, with the val */ |
| 691 | sbi->reghost_enabled = val; |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Tells the daemon whether it can umount the autofs mount. |
| 697 | */ |
| 698 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) |
| 699 | { |
| 700 | int status = 0; |
| 701 | |
| 702 | if (may_umount(mnt) == 0) |
| 703 | status = 1; |
| 704 | |
| 705 | DPRINTK("returning %d", status); |
| 706 | |
| 707 | status = put_user(status, p); |
| 708 | |
| 709 | return status; |
| 710 | } |
| 711 | |
| 712 | /* Identify autofs4_dentries - this is so we can tell if there's |
| 713 | an extra dentry refcount or not. We only hold a refcount on the |
| 714 | dentry if its non-negative (ie, d_inode != NULL) |
| 715 | */ |
| 716 | int is_autofs4_dentry(struct dentry *dentry) |
| 717 | { |
| 718 | return dentry && dentry->d_inode && |
| 719 | (dentry->d_op == &autofs4_root_dentry_operations || |
| 720 | dentry->d_op == &autofs4_dentry_operations) && |
| 721 | dentry->d_fsdata != NULL; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * ioctl()'s on the root directory is the chief method for the daemon to |
| 726 | * generate kernel reactions |
| 727 | */ |
| 728 | static int autofs4_root_ioctl(struct inode *inode, struct file *filp, |
| 729 | unsigned int cmd, unsigned long arg) |
| 730 | { |
| 731 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); |
| 732 | void __user *p = (void __user *)arg; |
| 733 | |
| 734 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", |
| 735 | cmd,arg,sbi,process_group(current)); |
| 736 | |
| 737 | if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || |
| 738 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT ) |
| 739 | return -ENOTTY; |
| 740 | |
| 741 | if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) |
| 742 | return -EPERM; |
| 743 | |
| 744 | switch(cmd) { |
| 745 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ |
| 746 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); |
| 747 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ |
| 748 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); |
| 749 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ |
| 750 | autofs4_catatonic_mode(sbi); |
| 751 | return 0; |
| 752 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ |
| 753 | return autofs4_get_protover(sbi, p); |
| 754 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ |
| 755 | return autofs4_get_protosubver(sbi, p); |
| 756 | case AUTOFS_IOC_SETTIMEOUT: |
| 757 | return autofs4_get_set_timeout(sbi, p); |
| 758 | |
| 759 | case AUTOFS_IOC_TOGGLEREGHOST: |
| 760 | return autofs4_toggle_reghost(sbi, p); |
| 761 | case AUTOFS_IOC_ASKREGHOST: |
| 762 | return autofs4_ask_reghost(sbi, p); |
| 763 | |
| 764 | case AUTOFS_IOC_ASKUMOUNT: |
| 765 | return autofs4_ask_umount(filp->f_vfsmnt, p); |
| 766 | |
| 767 | /* return a single thing to expire */ |
| 768 | case AUTOFS_IOC_EXPIRE: |
| 769 | return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p); |
| 770 | /* same as above, but can send multiple expires through pipe */ |
| 771 | case AUTOFS_IOC_EXPIRE_MULTI: |
| 772 | return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p); |
| 773 | |
| 774 | default: |
| 775 | return -ENOSYS; |
| 776 | } |
| 777 | } |