root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 int 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. /*
  294. * If an expire request is pending everyone must wait.
  295. * If the expire fails we're still mounted so continue
  296. * the follow and return. A return of -EAGAIN (which only
  297. * happens with indirect mounts) means the expire completed
  298. * and the directory was removed, so just go ahead and try
  299. * the mount.
  300. */
  301. status = do_expire_wait(path, 0);
  302. if (status && status != -EAGAIN)
  303. return NULL;
  304. /* Callback to the daemon to perform the mount or wait */
  305. spin_lock(&sbi->fs_lock);
  306. if (ino->flags & AUTOFS_INF_PENDING) {
  307. spin_unlock(&sbi->fs_lock);
  308. status = autofs_mount_wait(path, 0);
  309. if (status)
  310. return ERR_PTR(status);
  311. goto done;
  312. }
  313. /*
  314. * If the dentry is a symlink it's equivalent to a directory
  315. * having path_is_mountpoint() true, so there's no need to call
  316. * back to the daemon.
  317. */
  318. if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
  319. spin_unlock(&sbi->fs_lock);
  320. goto done;
  321. }
  322. if (!path_is_mountpoint(path)) {
  323. /*
  324. * It's possible that user space hasn't removed directories
  325. * after umounting a rootless multi-mount, although it
  326. * should. For v5 path_has_submounts() is sufficient to
  327. * handle this because the leaves of the directory tree under
  328. * the mount never trigger mounts themselves (they have an
  329. * autofs trigger mount mounted on them). But v4 pseudo direct
  330. * mounts do need the leaves to trigger mounts. In this case
  331. * we have no choice but to use the autofs_empty() check and
  332. * require user space behave.
  333. */
  334. if (sbi->version > 4) {
  335. if (path_has_submounts(path)) {
  336. spin_unlock(&sbi->fs_lock);
  337. goto done;
  338. }
  339. } else {
  340. if (!autofs_empty(ino)) {
  341. spin_unlock(&sbi->fs_lock);
  342. goto done;
  343. }
  344. }
  345. ino->flags |= AUTOFS_INF_PENDING;
  346. spin_unlock(&sbi->fs_lock);
  347. status = autofs_mount_wait(path, 0);
  348. spin_lock(&sbi->fs_lock);
  349. ino->flags &= ~AUTOFS_INF_PENDING;
  350. if (status) {
  351. spin_unlock(&sbi->fs_lock);
  352. return ERR_PTR(status);
  353. }
  354. }
  355. spin_unlock(&sbi->fs_lock);
  356. done:
  357. /* Mount succeeded, check if we ended up with a new dentry */
  358. dentry = autofs_mountpoint_changed(path);
  359. if (!dentry)
  360. return ERR_PTR(-ENOENT);
  361. return NULL;
  362. }
  363. static int autofs_d_manage(const struct path *path, bool rcu_walk)
  364. {
  365. struct dentry *dentry = path->dentry;
  366. struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
  367. struct autofs_info *ino = autofs_dentry_ino(dentry);
  368. int status;
  369. pr_debug("dentry=%p %pd\n", dentry, dentry);
  370. /* The daemon never waits. */
  371. if (autofs_oz_mode(sbi)) {
  372. if (!path_is_mountpoint(path))
  373. return -EISDIR;
  374. return 0;
  375. }
  376. /* Wait for pending expires */
  377. if (do_expire_wait(path, rcu_walk) == -ECHILD)
  378. return -ECHILD;
  379. /*
  380. * This dentry may be under construction so wait on mount
  381. * completion.
  382. */
  383. status = autofs_mount_wait(path, rcu_walk);
  384. if (status)
  385. return status;
  386. if (rcu_walk) {
  387. /* We don't need fs_lock in rcu_walk mode,
  388. * just testing 'AUTOFS_INF_WANT_EXPIRE' is enough.
  389. *
  390. * We only return -EISDIR when certain this isn't
  391. * a mount-trap.
  392. */
  393. struct inode *inode;
  394. if (ino->flags & AUTOFS_INF_WANT_EXPIRE)
  395. return 0;
  396. if (path_is_mountpoint(path))
  397. return 0;
  398. inode = d_inode_rcu(dentry);
  399. if (inode && S_ISLNK(inode->i_mode))
  400. return -EISDIR;
  401. if (!autofs_empty(ino))
  402. return -EISDIR;
  403. return 0;
  404. }
  405. spin_lock(&sbi->fs_lock);
  406. /*
  407. * If the dentry has been selected for expire while we slept
  408. * on the lock then it might go away. We'll deal with that in
  409. * ->d_automount() and wait on a new mount if the expire
  410. * succeeds or return here if it doesn't (since there's no
  411. * mount to follow with a rootless multi-mount).
  412. */
  413. if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
  414. /*
  415. * Any needed mounting has been completed and the path
  416. * updated so check if this is a rootless multi-mount so
  417. * we can avoid needless calls ->d_automount() and avoid
  418. * an incorrect ELOOP error return.
  419. */
  420. if ((!path_is_mountpoint(path) && !autofs_empty(ino)) ||
  421. (d_really_is_positive(dentry) && d_is_symlink(dentry)))
  422. status = -EISDIR;
  423. }
  424. spin_unlock(&sbi->fs_lock);
  425. return status;
  426. }
  427. /* Lookups in the root directory */
  428. static struct dentry *autofs_lookup(struct inode *dir,
  429. struct dentry *dentry, unsigned int flags)
  430. {
  431. struct autofs_sb_info *sbi;
  432. struct autofs_info *ino;
  433. struct dentry *active;
  434. pr_debug("name = %pd\n", dentry);
  435. /* File name too long to exist */
  436. if (dentry->d_name.len > NAME_MAX)
  437. return ERR_PTR(-ENAMETOOLONG);
  438. sbi = autofs_sbi(dir->i_sb);
  439. pr_debug("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
  440. current->pid, task_pgrp_nr(current),
  441. sbi->flags & AUTOFS_SBI_CATATONIC,
  442. autofs_oz_mode(sbi));
  443. active = autofs_lookup_active(dentry);
  444. if (active)
  445. return active;
  446. else {
  447. /*
  448. * A dentry that is not within the root can never trigger a
  449. * mount operation, unless the directory already exists, so we
  450. * can return fail immediately. The daemon however does need
  451. * to create directories within the file system.
  452. */
  453. if (!autofs_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  454. return ERR_PTR(-ENOENT);
  455. ino = autofs_new_ino(sbi);
  456. if (!ino)
  457. return ERR_PTR(-ENOMEM);
  458. spin_lock(&sbi->lookup_lock);
  459. spin_lock(&dentry->d_lock);
  460. /* Mark entries in the root as mount triggers */
  461. if (IS_ROOT(dentry->d_parent) &&
  462. autofs_type_indirect(sbi->type))
  463. __managed_dentry_set_managed(dentry);
  464. dentry->d_fsdata = ino;
  465. ino->dentry = dentry;
  466. list_add(&ino->active, &sbi->active_list);
  467. spin_unlock(&sbi->lookup_lock);
  468. spin_unlock(&dentry->d_lock);
  469. }
  470. return NULL;
  471. }
  472. static int autofs_dir_permission(struct mnt_idmap *idmap,
  473. struct inode *inode, int mask)
  474. {
  475. if (mask & MAY_WRITE) {
  476. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  477. if (!autofs_oz_mode(sbi))
  478. return -EACCES;
  479. /* autofs_oz_mode() needs to allow path walks when the
  480. * autofs mount is catatonic but the state of an autofs
  481. * file system needs to be preserved over restarts.
  482. */
  483. if (sbi->flags & AUTOFS_SBI_CATATONIC)
  484. return -EACCES;
  485. }
  486. return generic_permission(idmap, inode, mask);
  487. }
  488. static int autofs_dir_symlink(struct mnt_idmap *idmap,
  489. struct inode *dir, struct dentry *dentry,
  490. const char *symname)
  491. {
  492. struct autofs_info *ino = autofs_dentry_ino(dentry);
  493. struct autofs_info *p_ino;
  494. struct inode *inode;
  495. size_t size = strlen(symname);
  496. char *cp;
  497. pr_debug("%s <- %pd\n", symname, dentry);
  498. BUG_ON(!ino);
  499. autofs_clean_ino(ino);
  500. autofs_del_active(dentry);
  501. cp = kmalloc(size + 1, GFP_KERNEL);
  502. if (!cp)
  503. return -ENOMEM;
  504. strcpy(cp, symname);
  505. inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555);
  506. if (!inode) {
  507. kfree(cp);
  508. return -ENOMEM;
  509. }
  510. inode->i_private = cp;
  511. inode->i_size = size;
  512. d_add(dentry, inode);
  513. dget(dentry);
  514. p_ino = autofs_dentry_ino(dentry->d_parent);
  515. p_ino->count++;
  516. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  517. return 0;
  518. }
  519. /*
  520. * NOTE!
  521. *
  522. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  523. * that the file no longer exists. However, doing that means that the
  524. * VFS layer can turn the dentry into a negative dentry. We don't want
  525. * this, because the unlink is probably the result of an expire.
  526. * We simply d_drop it and add it to a expiring list in the super block,
  527. * which allows the dentry lookup to check for an incomplete expire.
  528. *
  529. * If a process is blocked on the dentry waiting for the expire to finish,
  530. * it will invalidate the dentry and try to mount with a new one.
  531. *
  532. * Also see autofs_dir_rmdir()..
  533. */
  534. static int autofs_dir_unlink(struct inode *dir, struct dentry *dentry)
  535. {
  536. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  537. struct autofs_info *ino = autofs_dentry_ino(dentry);
  538. struct autofs_info *p_ino;
  539. p_ino = autofs_dentry_ino(dentry->d_parent);
  540. p_ino->count--;
  541. dput(ino->dentry);
  542. d_inode(dentry)->i_size = 0;
  543. clear_nlink(d_inode(dentry));
  544. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  545. spin_lock(&sbi->lookup_lock);
  546. __autofs_add_expiring(dentry);
  547. d_drop(dentry);
  548. spin_unlock(&sbi->lookup_lock);
  549. return 0;
  550. }
  551. /*
  552. * Version 4 of autofs provides a pseudo direct mount implementation
  553. * that relies on directories at the leaves of a directory tree under
  554. * an indirect mount to trigger mounts. To allow for this we need to
  555. * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
  556. * of the directory tree. There is no need to clear the automount flag
  557. * following a mount or restore it after an expire because these mounts
  558. * are always covered. However, it is necessary to ensure that these
  559. * flags are clear on non-empty directories to avoid unnecessary calls
  560. * during path walks.
  561. */
  562. static void autofs_set_leaf_automount_flags(struct dentry *dentry)
  563. {
  564. struct dentry *parent;
  565. /* root and dentrys in the root are already handled */
  566. if (IS_ROOT(dentry->d_parent))
  567. return;
  568. managed_dentry_set_managed(dentry);
  569. parent = dentry->d_parent;
  570. /* only consider parents below dentrys in the root */
  571. if (IS_ROOT(parent->d_parent))
  572. return;
  573. managed_dentry_clear_managed(parent);
  574. }
  575. static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
  576. {
  577. struct dentry *parent;
  578. /* flags for dentrys in the root are handled elsewhere */
  579. if (IS_ROOT(dentry->d_parent))
  580. return;
  581. managed_dentry_clear_managed(dentry);
  582. parent = dentry->d_parent;
  583. /* only consider parents below dentrys in the root */
  584. if (IS_ROOT(parent->d_parent))
  585. return;
  586. if (autofs_dentry_ino(parent)->count == 2)
  587. managed_dentry_set_managed(parent);
  588. }
  589. static int autofs_dir_rmdir(struct inode *dir, struct dentry *dentry)
  590. {
  591. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  592. struct autofs_info *ino = autofs_dentry_ino(dentry);
  593. struct autofs_info *p_ino;
  594. pr_debug("dentry %p, removing %pd\n", dentry, dentry);
  595. if (ino->count != 1)
  596. return -ENOTEMPTY;
  597. spin_lock(&sbi->lookup_lock);
  598. __autofs_add_expiring(dentry);
  599. d_drop(dentry);
  600. spin_unlock(&sbi->lookup_lock);
  601. if (sbi->version < 5)
  602. autofs_clear_leaf_automount_flags(dentry);
  603. p_ino = autofs_dentry_ino(dentry->d_parent);
  604. p_ino->count--;
  605. dput(ino->dentry);
  606. d_inode(dentry)->i_size = 0;
  607. clear_nlink(d_inode(dentry));
  608. if (dir->i_nlink)
  609. drop_nlink(dir);
  610. return 0;
  611. }
  612. static int autofs_dir_mkdir(struct mnt_idmap *idmap,
  613. struct inode *dir, struct dentry *dentry,
  614. umode_t mode)
  615. {
  616. struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
  617. struct autofs_info *ino = autofs_dentry_ino(dentry);
  618. struct autofs_info *p_ino;
  619. struct inode *inode;
  620. pr_debug("dentry %p, creating %pd\n", dentry, dentry);
  621. BUG_ON(!ino);
  622. autofs_clean_ino(ino);
  623. autofs_del_active(dentry);
  624. inode = autofs_get_inode(dir->i_sb, S_IFDIR | mode);
  625. if (!inode)
  626. return -ENOMEM;
  627. d_add(dentry, inode);
  628. if (sbi->version < 5)
  629. autofs_set_leaf_automount_flags(dentry);
  630. dget(dentry);
  631. p_ino = autofs_dentry_ino(dentry->d_parent);
  632. p_ino->count++;
  633. inc_nlink(dir);
  634. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  635. return 0;
  636. }
  637. /* Get/set timeout ioctl() operation */
  638. #ifdef CONFIG_COMPAT
  639. static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
  640. compat_ulong_t __user *p)
  641. {
  642. unsigned long ntimeout;
  643. int rv;
  644. rv = get_user(ntimeout, p);
  645. if (rv)
  646. goto error;
  647. rv = put_user(sbi->exp_timeout/HZ, p);
  648. if (rv)
  649. goto error;
  650. if (ntimeout > UINT_MAX/HZ)
  651. sbi->exp_timeout = 0;
  652. else
  653. sbi->exp_timeout = ntimeout * HZ;
  654. return 0;
  655. error:
  656. return rv;
  657. }
  658. #endif
  659. static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
  660. unsigned long __user *p)
  661. {
  662. unsigned long ntimeout;
  663. int rv;
  664. rv = get_user(ntimeout, p);
  665. if (rv)
  666. goto error;
  667. rv = put_user(sbi->exp_timeout/HZ, p);
  668. if (rv)
  669. goto error;
  670. if (ntimeout > ULONG_MAX/HZ)
  671. sbi->exp_timeout = 0;
  672. else
  673. sbi->exp_timeout = ntimeout * HZ;
  674. return 0;
  675. error:
  676. return rv;
  677. }
  678. /* Return protocol version */
  679. static inline int autofs_get_protover(struct autofs_sb_info *sbi,
  680. int __user *p)
  681. {
  682. return put_user(sbi->version, p);
  683. }
  684. /* Return protocol sub version */
  685. static inline int autofs_get_protosubver(struct autofs_sb_info *sbi,
  686. int __user *p)
  687. {
  688. return put_user(sbi->sub_version, p);
  689. }
  690. /*
  691. * Tells the daemon whether it can umount the autofs mount.
  692. */
  693. static inline int autofs_ask_umount(struct vfsmount *mnt, int __user *p)
  694. {
  695. int status = 0;
  696. if (may_umount(mnt))
  697. status = 1;
  698. pr_debug("may umount %d\n", status);
  699. status = put_user(status, p);
  700. return status;
  701. }
  702. /* Identify autofs_dentries - this is so we can tell if there's
  703. * an extra dentry refcount or not. We only hold a refcount on the
  704. * dentry if its non-negative (ie, d_inode != NULL)
  705. */
  706. int is_autofs_dentry(struct dentry *dentry)
  707. {
  708. return dentry && d_really_is_positive(dentry) &&
  709. dentry->d_op == &autofs_dentry_operations &&
  710. dentry->d_fsdata != NULL;
  711. }
  712. /*
  713. * ioctl()'s on the root directory is the chief method for the daemon to
  714. * generate kernel reactions
  715. */
  716. static int autofs_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  717. unsigned int cmd, unsigned long arg)
  718. {
  719. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  720. void __user *p = (void __user *)arg;
  721. pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
  722. cmd, arg, sbi, task_pgrp_nr(current));
  723. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  724. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  725. return -ENOTTY;
  726. if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  727. return -EPERM;
  728. switch (cmd) {
  729. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  730. return autofs_wait_release(sbi, (autofs_wqt_t) arg, 0);
  731. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  732. return autofs_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
  733. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  734. autofs_catatonic_mode(sbi);
  735. return 0;
  736. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  737. return autofs_get_protover(sbi, p);
  738. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  739. return autofs_get_protosubver(sbi, p);
  740. case AUTOFS_IOC_SETTIMEOUT:
  741. return autofs_get_set_timeout(sbi, p);
  742. #ifdef CONFIG_COMPAT
  743. case AUTOFS_IOC_SETTIMEOUT32:
  744. return autofs_compat_get_set_timeout(sbi, p);
  745. #endif
  746. case AUTOFS_IOC_ASKUMOUNT:
  747. return autofs_ask_umount(filp->f_path.mnt, p);
  748. /* return a single thing to expire */
  749. case AUTOFS_IOC_EXPIRE:
  750. return autofs_expire_run(inode->i_sb, filp->f_path.mnt, sbi, p);
  751. /* same as above, but can send multiple expires through pipe */
  752. case AUTOFS_IOC_EXPIRE_MULTI:
  753. return autofs_expire_multi(inode->i_sb,
  754. filp->f_path.mnt, sbi, p);
  755. default:
  756. return -EINVAL;
  757. }
  758. }
  759. static long autofs_root_ioctl(struct file *filp,
  760. unsigned int cmd, unsigned long arg)
  761. {
  762. struct inode *inode = file_inode(filp);
  763. return autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
  764. }
  765. #ifdef CONFIG_COMPAT
  766. static long autofs_root_compat_ioctl(struct file *filp,
  767. unsigned int cmd, unsigned long arg)
  768. {
  769. struct inode *inode = file_inode(filp);
  770. int ret;
  771. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  772. ret = autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
  773. else
  774. ret = autofs_root_ioctl_unlocked(inode, filp, cmd,
  775. (unsigned long) compat_ptr(arg));
  776. return ret;
  777. }
  778. #endif