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