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