1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved 4 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org> 5 * Copyright 2001-2006 Ian Kent <raven@themaw.net> 6 */ 7 8 #include <linux/capability.h> 9 #include <linux/compat.h> 10 #include <linux/string.h> 11 12 #include "autofs_i.h" 13 14 static int autofs_dir_permission(struct mnt_idmap *, struct inode *, int); 15 static int autofs_dir_symlink(struct mnt_idmap *, struct inode *, 16 struct dentry *, const char *); 17 static int autofs_dir_unlink(struct inode *, struct dentry *); 18 static int autofs_dir_rmdir(struct inode *, struct dentry *); 19 static struct dentry *autofs_dir_mkdir(struct mnt_idmap *, struct inode *, 20 struct dentry *, umode_t); 21 static long autofs_root_ioctl(struct file *, unsigned int, unsigned long); 22 #ifdef CONFIG_COMPAT 23 static long autofs_root_compat_ioctl(struct file *, 24 unsigned int, unsigned long); 25 #endif 26 static int autofs_dir_open(struct inode *inode, struct file *file); 27 static struct dentry *autofs_lookup(struct inode *, 28 struct dentry *, unsigned int); 29 static struct vfsmount *autofs_d_automount(struct path *); 30 static int autofs_d_manage(const struct path *, bool); 31 static void autofs_dentry_release(struct dentry *); 32 33 const struct file_operations autofs_root_operations = { 34 .open = dcache_dir_open, 35 .release = dcache_dir_close, 36 .read = generic_read_dir, 37 .iterate_shared = dcache_readdir, 38 .llseek = dcache_dir_lseek, 39 .unlocked_ioctl = autofs_root_ioctl, 40 #ifdef CONFIG_COMPAT 41 .compat_ioctl = autofs_root_compat_ioctl, 42 #endif 43 }; 44 45 const struct file_operations autofs_dir_operations = { 46 .open = autofs_dir_open, 47 .release = dcache_dir_close, 48 .read = generic_read_dir, 49 .iterate_shared = dcache_readdir, 50 .llseek = dcache_dir_lseek, 51 }; 52 53 const struct inode_operations autofs_dir_inode_operations = { 54 .lookup = autofs_lookup, 55 .permission = autofs_dir_permission, 56 .unlink = autofs_dir_unlink, 57 .symlink = autofs_dir_symlink, 58 .mkdir = autofs_dir_mkdir, 59 .rmdir = autofs_dir_rmdir, 60 }; 61 62 const struct dentry_operations autofs_dentry_operations = { 63 .d_automount = autofs_d_automount, 64 .d_manage = autofs_d_manage, 65 .d_release = autofs_dentry_release, 66 }; 67 68 static void autofs_del_active(struct dentry *dentry) 69 { 70 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 71 struct autofs_info *ino; 72 73 ino = autofs_dentry_ino(dentry); 74 spin_lock(&sbi->lookup_lock); 75 list_del_init(&ino->active); 76 spin_unlock(&sbi->lookup_lock); 77 } 78 79 static int autofs_dir_open(struct inode *inode, struct file *file) 80 { 81 struct dentry *dentry = file->f_path.dentry; 82 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 83 struct autofs_info *ino = autofs_dentry_ino(dentry); 84 85 pr_debug("file=%p dentry=%p %pd\n", file, dentry, dentry); 86 87 if (autofs_oz_mode(sbi)) 88 goto out; 89 90 /* 91 * An empty directory in an autofs file system is always a 92 * mount point. The daemon must have failed to mount this 93 * during lookup so it doesn't exist. This can happen, for 94 * example, if user space returns an incorrect status for a 95 * mount request. Otherwise we're doing a readdir on the 96 * autofs file system so just let the libfs routines handle 97 * it. 98 */ 99 spin_lock(&sbi->lookup_lock); 100 if (!path_is_mountpoint(&file->f_path) && autofs_empty(ino)) { 101 spin_unlock(&sbi->lookup_lock); 102 return -ENOENT; 103 } 104 spin_unlock(&sbi->lookup_lock); 105 106 out: 107 return dcache_dir_open(inode, file); 108 } 109 110 static void autofs_dentry_release(struct dentry *de) 111 { 112 struct autofs_info *ino = autofs_dentry_ino(de); 113 struct autofs_sb_info *sbi = autofs_sbi(de->d_sb); 114 115 pr_debug("releasing %p\n", de); 116 117 if (!ino) 118 return; 119 120 if (sbi) { 121 spin_lock(&sbi->lookup_lock); 122 if (!list_empty(&ino->active)) 123 list_del(&ino->active); 124 if (!list_empty(&ino->expiring)) 125 list_del(&ino->expiring); 126 spin_unlock(&sbi->lookup_lock); 127 } 128 129 autofs_free_ino(ino); 130 } 131 132 static struct dentry *autofs_lookup_active(struct dentry *dentry) 133 { 134 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 135 struct dentry *parent = dentry->d_parent; 136 const struct qstr *name = &dentry->d_name; 137 unsigned int len = name->len; 138 unsigned int hash = name->hash; 139 const unsigned char *str = name->name; 140 struct list_head *p, *head; 141 142 head = &sbi->active_list; 143 if (list_empty(head)) 144 return NULL; 145 spin_lock(&sbi->lookup_lock); 146 list_for_each(p, head) { 147 struct autofs_info *ino; 148 struct dentry *active; 149 const struct qstr *qstr; 150 151 ino = list_entry(p, struct autofs_info, active); 152 active = ino->dentry; 153 154 spin_lock(&active->d_lock); 155 156 /* Already gone? */ 157 if ((int) d_count(active) <= 0) 158 goto next; 159 160 qstr = &active->d_name; 161 162 if (active->d_name.hash != hash) 163 goto next; 164 if (active->d_parent != parent) 165 goto next; 166 167 if (qstr->len != len) 168 goto next; 169 if (memcmp(qstr->name, str, len)) 170 goto next; 171 172 if (d_unhashed(active)) { 173 dget_dlock(active); 174 spin_unlock(&active->d_lock); 175 spin_unlock(&sbi->lookup_lock); 176 return active; 177 } 178 next: 179 spin_unlock(&active->d_lock); 180 } 181 spin_unlock(&sbi->lookup_lock); 182 183 return NULL; 184 } 185 186 static struct dentry *autofs_lookup_expiring(struct dentry *dentry, 187 bool rcu_walk) 188 { 189 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 190 struct dentry *parent = dentry->d_parent; 191 const struct qstr *name = &dentry->d_name; 192 unsigned int len = name->len; 193 unsigned int hash = name->hash; 194 const unsigned char *str = name->name; 195 struct list_head *p, *head; 196 197 head = &sbi->expiring_list; 198 if (list_empty(head)) 199 return NULL; 200 spin_lock(&sbi->lookup_lock); 201 list_for_each(p, head) { 202 struct autofs_info *ino; 203 struct dentry *expiring; 204 const struct qstr *qstr; 205 206 if (rcu_walk) { 207 spin_unlock(&sbi->lookup_lock); 208 return ERR_PTR(-ECHILD); 209 } 210 211 ino = list_entry(p, struct autofs_info, expiring); 212 expiring = ino->dentry; 213 214 spin_lock(&expiring->d_lock); 215 216 /* We've already been dentry_iput or unlinked */ 217 if (d_really_is_negative(expiring)) 218 goto next; 219 220 qstr = &expiring->d_name; 221 222 if (expiring->d_name.hash != hash) 223 goto next; 224 if (expiring->d_parent != parent) 225 goto next; 226 227 if (qstr->len != len) 228 goto next; 229 if (memcmp(qstr->name, str, len)) 230 goto next; 231 232 if (d_unhashed(expiring)) { 233 dget_dlock(expiring); 234 spin_unlock(&expiring->d_lock); 235 spin_unlock(&sbi->lookup_lock); 236 return expiring; 237 } 238 next: 239 spin_unlock(&expiring->d_lock); 240 } 241 spin_unlock(&sbi->lookup_lock); 242 243 return NULL; 244 } 245 246 static int autofs_mount_wait(const struct path *path, bool rcu_walk) 247 { 248 struct autofs_sb_info *sbi = autofs_sbi(path->dentry->d_sb); 249 struct autofs_info *ino = autofs_dentry_ino(path->dentry); 250 int status = 0; 251 252 if (ino->flags & AUTOFS_INF_PENDING) { 253 if (rcu_walk) 254 return -ECHILD; 255 pr_debug("waiting for mount name=%pd\n", path->dentry); 256 status = autofs_wait(sbi, path, NFY_MOUNT); 257 pr_debug("mount wait done status=%d\n", status); 258 ino->last_used = jiffies; 259 return status; 260 } 261 if (!(sbi->flags & AUTOFS_SBI_STRICTEXPIRE)) 262 ino->last_used = jiffies; 263 return status; 264 } 265 266 static int do_expire_wait(const struct path *path, bool rcu_walk) 267 { 268 struct dentry *dentry = path->dentry; 269 struct dentry *expiring; 270 271 expiring = autofs_lookup_expiring(dentry, rcu_walk); 272 if (IS_ERR(expiring)) 273 return PTR_ERR(expiring); 274 if (!expiring) 275 return autofs_expire_wait(path, rcu_walk); 276 else { 277 const struct path this = { .mnt = path->mnt, .dentry = expiring }; 278 /* 279 * If we are racing with expire the request might not 280 * be quite complete, but the directory has been removed 281 * so it must have been successful, just wait for it. 282 */ 283 autofs_expire_wait(&this, 0); 284 autofs_del_expiring(expiring); 285 dput(expiring); 286 } 287 return 0; 288 } 289 290 static struct dentry *autofs_mountpoint_changed(struct path *path) 291 { 292 struct dentry *dentry = path->dentry; 293 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 294 295 /* If this is an indirect mount the dentry could have gone away 296 * and a new one created. 297 * 298 * This is unusual and I can't remember the case for which it 299 * was originally added now. But an example of how this can 300 * happen is an autofs indirect mount that has the "browse" 301 * option set and also has the "symlink" option in the autofs 302 * map entry. In this case the daemon will remove the browse 303 * directory and create a symlink as the mount leaving the 304 * struct path stale. 305 * 306 * Another not so obvious case is when a mount in an autofs 307 * indirect mount that uses the "nobrowse" option is being 308 * expired at the same time as a path walk. If the mount has 309 * been umounted but the mount point directory seen before 310 * becoming unhashed (during a lockless path walk) when a stat 311 * family system call is made the mount won't be re-mounted as 312 * it should. In this case the mount point that's been removed 313 * (by the daemon) will be stale and the a new mount point 314 * dentry created. 315 */ 316 if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) { 317 struct dentry *parent = dentry->d_parent; 318 struct autofs_info *ino; 319 struct dentry *new; 320 321 new = d_lookup(parent, &dentry->d_name); 322 if (!new) 323 return NULL; 324 ino = autofs_dentry_ino(new); 325 ino->last_used = jiffies; 326 dput(path->dentry); 327 path->dentry = new; 328 } 329 return path->dentry; 330 } 331 332 static struct vfsmount *autofs_d_automount(struct path *path) 333 { 334 struct dentry *dentry = path->dentry; 335 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 336 struct autofs_info *ino = autofs_dentry_ino(dentry); 337 int status; 338 339 pr_debug("dentry=%p %pd\n", dentry, dentry); 340 341 /* The daemon never triggers a mount. */ 342 if (autofs_oz_mode(sbi)) 343 return NULL; 344 345 /* Refuse to trigger mount if current namespace is not the owner 346 * and the mount is propagation private. 347 */ 348 if (sbi->mnt_ns_id != to_ns_common(current->nsproxy->mnt_ns)->ns_id) { 349 if (vfsmount_to_propagation_flags(path->mnt) & MS_PRIVATE) 350 return ERR_PTR(-EPERM); 351 } 352 353 /* 354 * If an expire request is pending everyone must wait. 355 * If the expire fails we're still mounted so continue 356 * the follow and return. A return of -EAGAIN (which only 357 * happens with indirect mounts) means the expire completed 358 * and the directory was removed, so just go ahead and try 359 * the mount. 360 */ 361 status = do_expire_wait(path, 0); 362 if (status && status != -EAGAIN) 363 return NULL; 364 365 /* Callback to the daemon to perform the mount or wait */ 366 spin_lock(&sbi->fs_lock); 367 if (ino->flags & AUTOFS_INF_PENDING) { 368 spin_unlock(&sbi->fs_lock); 369 status = autofs_mount_wait(path, 0); 370 if (status) 371 return ERR_PTR(status); 372 goto done; 373 } 374 375 /* 376 * If the dentry is a symlink it's equivalent to a directory 377 * having path_is_mountpoint() true, so there's no need to call 378 * back to the daemon. 379 */ 380 if (d_really_is_positive(dentry) && d_is_symlink(dentry)) { 381 spin_unlock(&sbi->fs_lock); 382 goto done; 383 } 384 385 if (!path_is_mountpoint(path)) { 386 /* 387 * It's possible that user space hasn't removed directories 388 * after umounting a rootless multi-mount, although it 389 * should. For v5 path_has_submounts() is sufficient to 390 * handle this because the leaves of the directory tree under 391 * the mount never trigger mounts themselves (they have an 392 * autofs trigger mount mounted on them). But v4 pseudo direct 393 * mounts do need the leaves to trigger mounts. In this case 394 * we have no choice but to use the autofs_empty() check and 395 * require user space behave. 396 */ 397 if (sbi->version > 4) { 398 if (path_has_submounts(path)) { 399 spin_unlock(&sbi->fs_lock); 400 goto done; 401 } 402 } else { 403 if (!autofs_empty(ino)) { 404 spin_unlock(&sbi->fs_lock); 405 goto done; 406 } 407 } 408 ino->flags |= AUTOFS_INF_PENDING; 409 spin_unlock(&sbi->fs_lock); 410 status = autofs_mount_wait(path, 0); 411 spin_lock(&sbi->fs_lock); 412 ino->flags &= ~AUTOFS_INF_PENDING; 413 if (status) { 414 spin_unlock(&sbi->fs_lock); 415 return ERR_PTR(status); 416 } 417 } 418 spin_unlock(&sbi->fs_lock); 419 done: 420 /* Mount succeeded, check if we ended up with a new dentry */ 421 dentry = autofs_mountpoint_changed(path); 422 if (!dentry) 423 return ERR_PTR(-ENOENT); 424 425 return NULL; 426 } 427 428 static int autofs_d_manage(const struct path *path, bool rcu_walk) 429 { 430 struct dentry *dentry = path->dentry; 431 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb); 432 struct autofs_info *ino = autofs_dentry_ino(dentry); 433 int status; 434 435 pr_debug("dentry=%p %pd\n", dentry, dentry); 436 437 /* The daemon never waits. */ 438 if (autofs_oz_mode(sbi)) { 439 if (!path_is_mountpoint(path)) 440 return -EISDIR; 441 return 0; 442 } 443 444 /* Wait for pending expires */ 445 if (do_expire_wait(path, rcu_walk) == -ECHILD) 446 return -ECHILD; 447 448 /* 449 * This dentry may be under construction so wait on mount 450 * completion. 451 */ 452 status = autofs_mount_wait(path, rcu_walk); 453 if (status) 454 return status; 455 456 if (rcu_walk) { 457 /* We don't need fs_lock in rcu_walk mode, 458 * just testing 'AUTOFS_INF_WANT_EXPIRE' is enough. 459 * 460 * We only return -EISDIR when certain this isn't 461 * a mount-trap. 462 */ 463 struct inode *inode; 464 465 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) 466 return 0; 467 if (path_is_mountpoint(path)) 468 return 0; 469 inode = d_inode_rcu(dentry); 470 if (inode && S_ISLNK(inode->i_mode)) 471 return -EISDIR; 472 if (!autofs_empty(ino)) 473 return -EISDIR; 474 return 0; 475 } 476 477 spin_lock(&sbi->fs_lock); 478 /* 479 * If the dentry has been selected for expire while we slept 480 * on the lock then it might go away. We'll deal with that in 481 * ->d_automount() and wait on a new mount if the expire 482 * succeeds or return here if it doesn't (since there's no 483 * mount to follow with a rootless multi-mount). 484 */ 485 if (!(ino->flags & AUTOFS_INF_EXPIRING)) { 486 /* 487 * Any needed mounting has been completed and the path 488 * updated so check if this is a rootless multi-mount so 489 * we can avoid needless calls ->d_automount() and avoid 490 * an incorrect ELOOP error return. 491 */ 492 if ((!path_is_mountpoint(path) && !autofs_empty(ino)) || 493 (d_really_is_positive(dentry) && d_is_symlink(dentry))) 494 status = -EISDIR; 495 } 496 spin_unlock(&sbi->fs_lock); 497 498 return status; 499 } 500 501 /* Lookups in the root directory */ 502 static struct dentry *autofs_lookup(struct inode *dir, 503 struct dentry *dentry, unsigned int flags) 504 { 505 struct autofs_sb_info *sbi; 506 struct autofs_info *ino; 507 struct dentry *active; 508 509 pr_debug("name = %pd\n", dentry); 510 511 /* File name too long to exist */ 512 if (dentry->d_name.len > NAME_MAX) 513 return ERR_PTR(-ENAMETOOLONG); 514 515 sbi = autofs_sbi(dir->i_sb); 516 517 pr_debug("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n", 518 current->pid, task_pgrp_nr(current), 519 sbi->flags & AUTOFS_SBI_CATATONIC, 520 autofs_oz_mode(sbi)); 521 522 active = autofs_lookup_active(dentry); 523 if (active) 524 return active; 525 else { 526 /* 527 * A dentry that is not within the root can never trigger a 528 * mount operation, unless the directory already exists, so we 529 * can return fail immediately. The daemon however does need 530 * to create directories within the file system. 531 */ 532 if (!autofs_oz_mode(sbi) && !IS_ROOT(dentry->d_parent)) 533 return ERR_PTR(-ENOENT); 534 535 ino = autofs_new_ino(sbi); 536 if (!ino) 537 return ERR_PTR(-ENOMEM); 538 539 spin_lock(&sbi->lookup_lock); 540 spin_lock(&dentry->d_lock); 541 /* Mark entries in the root as mount triggers */ 542 if (IS_ROOT(dentry->d_parent) && 543 autofs_type_indirect(sbi->type)) 544 __managed_dentry_set_managed(dentry); 545 dentry->d_fsdata = ino; 546 ino->dentry = dentry; 547 548 list_add(&ino->active, &sbi->active_list); 549 spin_unlock(&sbi->lookup_lock); 550 spin_unlock(&dentry->d_lock); 551 } 552 return NULL; 553 } 554 555 static int autofs_dir_permission(struct mnt_idmap *idmap, 556 struct inode *inode, int mask) 557 { 558 if (mask & MAY_WRITE) { 559 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb); 560 561 if (!autofs_oz_mode(sbi)) 562 return -EACCES; 563 564 /* autofs_oz_mode() needs to allow path walks when the 565 * autofs mount is catatonic but the state of an autofs 566 * file system needs to be preserved over restarts. 567 */ 568 if (sbi->flags & AUTOFS_SBI_CATATONIC) 569 return -EACCES; 570 } 571 572 return generic_permission(idmap, inode, mask); 573 } 574 575 static int autofs_dir_symlink(struct mnt_idmap *idmap, 576 struct inode *dir, struct dentry *dentry, 577 const char *symname) 578 { 579 struct autofs_info *ino = autofs_dentry_ino(dentry); 580 struct autofs_info *p_ino; 581 struct inode *inode; 582 char *cp; 583 584 pr_debug("%s <- %pd\n", symname, dentry); 585 586 BUG_ON(!ino); 587 588 autofs_clean_ino(ino); 589 590 autofs_del_active(dentry); 591 592 cp = kstrdup(symname, GFP_KERNEL); 593 if (!cp) 594 return -ENOMEM; 595 596 inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555); 597 if (!inode) { 598 kfree(cp); 599 return -ENOMEM; 600 } 601 inode->i_private = cp; 602 inode->i_size = strlen(cp); 603 604 d_make_persistent(dentry, inode); 605 p_ino = autofs_dentry_ino(dentry->d_parent); 606 p_ino->count++; 607 608 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 609 610 return 0; 611 } 612 613 /* 614 * NOTE! 615 * 616 * Normal filesystems would do a "d_delete()" to tell the VFS dcache 617 * that the file no longer exists. However, doing that means that the 618 * VFS layer can turn the dentry into a negative dentry. We don't want 619 * this, because the unlink is probably the result of an expire. 620 * We simply d_drop it and add it to a expiring list in the super block, 621 * which allows the dentry lookup to check for an incomplete expire. 622 * 623 * If a process is blocked on the dentry waiting for the expire to finish, 624 * it will invalidate the dentry and try to mount with a new one. 625 * 626 * Also see autofs_dir_rmdir().. 627 */ 628 static int autofs_dir_unlink(struct inode *dir, struct dentry *dentry) 629 { 630 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); 631 struct autofs_info *p_ino; 632 633 p_ino = autofs_dentry_ino(dentry->d_parent); 634 p_ino->count--; 635 d_make_discardable(dentry); 636 637 d_inode(dentry)->i_size = 0; 638 clear_nlink(d_inode(dentry)); 639 640 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 641 642 spin_lock(&sbi->lookup_lock); 643 __autofs_add_expiring(dentry); 644 d_drop(dentry); 645 spin_unlock(&sbi->lookup_lock); 646 647 return 0; 648 } 649 650 /* 651 * Version 4 of autofs provides a pseudo direct mount implementation 652 * that relies on directories at the leaves of a directory tree under 653 * an indirect mount to trigger mounts. To allow for this we need to 654 * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves 655 * of the directory tree. There is no need to clear the automount flag 656 * following a mount or restore it after an expire because these mounts 657 * are always covered. However, it is necessary to ensure that these 658 * flags are clear on non-empty directories to avoid unnecessary calls 659 * during path walks. 660 */ 661 static void autofs_set_leaf_automount_flags(struct dentry *dentry) 662 { 663 struct dentry *parent; 664 665 /* root and dentrys in the root are already handled */ 666 if (IS_ROOT(dentry->d_parent)) 667 return; 668 669 managed_dentry_set_managed(dentry); 670 671 parent = dentry->d_parent; 672 /* only consider parents below dentrys in the root */ 673 if (IS_ROOT(parent->d_parent)) 674 return; 675 managed_dentry_clear_managed(parent); 676 } 677 678 static void autofs_clear_leaf_automount_flags(struct dentry *dentry) 679 { 680 struct dentry *parent; 681 682 /* flags for dentrys in the root are handled elsewhere */ 683 if (IS_ROOT(dentry->d_parent)) 684 return; 685 686 managed_dentry_clear_managed(dentry); 687 688 parent = dentry->d_parent; 689 /* only consider parents below dentrys in the root */ 690 if (IS_ROOT(parent->d_parent)) 691 return; 692 if (autofs_dentry_ino(parent)->count == 2) 693 managed_dentry_set_managed(parent); 694 } 695 696 static int autofs_dir_rmdir(struct inode *dir, struct dentry *dentry) 697 { 698 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); 699 struct autofs_info *ino = autofs_dentry_ino(dentry); 700 struct autofs_info *p_ino; 701 702 pr_debug("dentry %p, removing %pd\n", dentry, dentry); 703 704 if (ino->count != 1) 705 return -ENOTEMPTY; 706 707 spin_lock(&sbi->lookup_lock); 708 __autofs_add_expiring(dentry); 709 d_drop(dentry); 710 spin_unlock(&sbi->lookup_lock); 711 712 if (sbi->version < 5) 713 autofs_clear_leaf_automount_flags(dentry); 714 715 p_ino = autofs_dentry_ino(dentry->d_parent); 716 p_ino->count--; 717 d_make_discardable(dentry); 718 d_inode(dentry)->i_size = 0; 719 clear_nlink(d_inode(dentry)); 720 721 if (dir->i_nlink) 722 drop_nlink(dir); 723 724 return 0; 725 } 726 727 static struct dentry *autofs_dir_mkdir(struct mnt_idmap *idmap, 728 struct inode *dir, struct dentry *dentry, 729 umode_t mode) 730 { 731 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); 732 struct autofs_info *ino = autofs_dentry_ino(dentry); 733 struct autofs_info *p_ino; 734 struct inode *inode; 735 736 pr_debug("dentry %p, creating %pd\n", dentry, dentry); 737 738 BUG_ON(!ino); 739 740 autofs_clean_ino(ino); 741 742 autofs_del_active(dentry); 743 744 inode = autofs_get_inode(dir->i_sb, S_IFDIR | mode); 745 if (!inode) 746 return ERR_PTR(-ENOMEM); 747 748 if (sbi->version < 5) 749 autofs_set_leaf_automount_flags(dentry); 750 751 d_make_persistent(dentry, inode); 752 p_ino = autofs_dentry_ino(dentry->d_parent); 753 p_ino->count++; 754 inc_nlink(dir); 755 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 756 757 return NULL; 758 } 759 760 /* Get/set timeout ioctl() operation */ 761 #ifdef CONFIG_COMPAT 762 static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi, 763 compat_ulong_t __user *p) 764 { 765 unsigned long ntimeout; 766 int rv; 767 768 rv = get_user(ntimeout, p); 769 if (rv) 770 goto error; 771 772 rv = put_user(sbi->exp_timeout/HZ, p); 773 if (rv) 774 goto error; 775 776 if (ntimeout > UINT_MAX/HZ) 777 sbi->exp_timeout = 0; 778 else 779 sbi->exp_timeout = ntimeout * HZ; 780 781 return 0; 782 error: 783 return rv; 784 } 785 #endif 786 787 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi, 788 unsigned long __user *p) 789 { 790 unsigned long ntimeout; 791 int rv; 792 793 rv = get_user(ntimeout, p); 794 if (rv) 795 goto error; 796 797 rv = put_user(sbi->exp_timeout/HZ, p); 798 if (rv) 799 goto error; 800 801 if (ntimeout > ULONG_MAX/HZ) 802 sbi->exp_timeout = 0; 803 else 804 sbi->exp_timeout = ntimeout * HZ; 805 806 return 0; 807 error: 808 return rv; 809 } 810 811 /* Return protocol version */ 812 static inline int autofs_get_protover(struct autofs_sb_info *sbi, 813 int __user *p) 814 { 815 return put_user(sbi->version, p); 816 } 817 818 /* Return protocol sub version */ 819 static inline int autofs_get_protosubver(struct autofs_sb_info *sbi, 820 int __user *p) 821 { 822 return put_user(sbi->sub_version, p); 823 } 824 825 /* 826 * Tells the daemon whether it can umount the autofs mount. 827 */ 828 static inline int autofs_ask_umount(struct vfsmount *mnt, int __user *p) 829 { 830 int status = 0; 831 832 if (may_umount(mnt)) 833 status = 1; 834 835 pr_debug("may umount %d\n", status); 836 837 status = put_user(status, p); 838 839 return status; 840 } 841 842 /* Identify autofs_dentries - this is so we can tell if there's 843 * an extra dentry refcount or not. We only hold a refcount on the 844 * dentry if its non-negative (ie, d_inode != NULL) 845 */ 846 int is_autofs_dentry(struct dentry *dentry) 847 { 848 return dentry && d_really_is_positive(dentry) && 849 dentry->d_op == &autofs_dentry_operations && 850 dentry->d_fsdata != NULL; 851 } 852 853 /* 854 * ioctl()'s on the root directory is the chief method for the daemon to 855 * generate kernel reactions 856 */ 857 static int autofs_root_ioctl_unlocked(struct inode *inode, struct file *filp, 858 unsigned int cmd, unsigned long arg) 859 { 860 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb); 861 void __user *p = (void __user *)arg; 862 863 pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n", 864 cmd, arg, sbi, task_pgrp_nr(current)); 865 866 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || 867 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT) 868 return -ENOTTY; 869 870 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) 871 return -EPERM; 872 873 switch (cmd) { 874 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ 875 return autofs_wait_release(sbi, (autofs_wqt_t) arg, 0); 876 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ 877 return autofs_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT); 878 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ 879 autofs_catatonic_mode(sbi); 880 return 0; 881 case AUTOFS_IOC_PROTOVER: /* Get protocol version */ 882 return autofs_get_protover(sbi, p); 883 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ 884 return autofs_get_protosubver(sbi, p); 885 case AUTOFS_IOC_SETTIMEOUT: 886 return autofs_get_set_timeout(sbi, p); 887 #ifdef CONFIG_COMPAT 888 case AUTOFS_IOC_SETTIMEOUT32: 889 return autofs_compat_get_set_timeout(sbi, p); 890 #endif 891 892 case AUTOFS_IOC_ASKUMOUNT: 893 return autofs_ask_umount(filp->f_path.mnt, p); 894 895 /* return a single thing to expire */ 896 case AUTOFS_IOC_EXPIRE: 897 return autofs_expire_run(inode->i_sb, filp->f_path.mnt, sbi, p); 898 /* same as above, but can send multiple expires through pipe */ 899 case AUTOFS_IOC_EXPIRE_MULTI: 900 return autofs_expire_multi(inode->i_sb, 901 filp->f_path.mnt, sbi, p); 902 903 default: 904 return -EINVAL; 905 } 906 } 907 908 static long autofs_root_ioctl(struct file *filp, 909 unsigned int cmd, unsigned long arg) 910 { 911 struct inode *inode = file_inode(filp); 912 913 return autofs_root_ioctl_unlocked(inode, filp, cmd, arg); 914 } 915 916 #ifdef CONFIG_COMPAT 917 static long autofs_root_compat_ioctl(struct file *filp, 918 unsigned int cmd, unsigned long arg) 919 { 920 struct inode *inode = file_inode(filp); 921 int ret; 922 923 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL) 924 ret = autofs_root_ioctl_unlocked(inode, filp, cmd, arg); 925 else 926 ret = autofs_root_ioctl_unlocked(inode, filp, cmd, 927 (unsigned long) compat_ptr(arg)); 928 929 return ret; 930 } 931 #endif 932