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