root.c 24 KB

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