root.c 24 KB

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