root.c 24 KB

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