root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. const 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. const 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. const 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. const 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_WANT_EXPIRE)
  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. return -ENOMEM;
  496. }
  497. inode->i_private = cp;
  498. inode->i_size = size;
  499. d_add(dentry, inode);
  500. dget(dentry);
  501. atomic_inc(&ino->count);
  502. p_ino = autofs4_dentry_ino(dentry->d_parent);
  503. if (p_ino && !IS_ROOT(dentry))
  504. atomic_inc(&p_ino->count);
  505. dir->i_mtime = current_time(dir);
  506. return 0;
  507. }
  508. /*
  509. * NOTE!
  510. *
  511. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  512. * that the file no longer exists. However, doing that means that the
  513. * VFS layer can turn the dentry into a negative dentry. We don't want
  514. * this, because the unlink is probably the result of an expire.
  515. * We simply d_drop it and add it to a expiring list in the super block,
  516. * which allows the dentry lookup to check for an incomplete expire.
  517. *
  518. * If a process is blocked on the dentry waiting for the expire to finish,
  519. * it will invalidate the dentry and try to mount with a new one.
  520. *
  521. * Also see autofs4_dir_rmdir()..
  522. */
  523. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  524. {
  525. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  526. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  527. struct autofs_info *p_ino;
  528. /* This allows root to remove symlinks */
  529. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  530. return -EPERM;
  531. if (atomic_dec_and_test(&ino->count)) {
  532. p_ino = autofs4_dentry_ino(dentry->d_parent);
  533. if (p_ino && !IS_ROOT(dentry))
  534. atomic_dec(&p_ino->count);
  535. }
  536. dput(ino->dentry);
  537. d_inode(dentry)->i_size = 0;
  538. clear_nlink(d_inode(dentry));
  539. dir->i_mtime = current_time(dir);
  540. spin_lock(&sbi->lookup_lock);
  541. __autofs4_add_expiring(dentry);
  542. d_drop(dentry);
  543. spin_unlock(&sbi->lookup_lock);
  544. return 0;
  545. }
  546. /*
  547. * Version 4 of autofs provides a pseudo direct mount implementation
  548. * that relies on directories at the leaves of a directory tree under
  549. * an indirect mount to trigger mounts. To allow for this we need to
  550. * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
  551. * of the directory tree. There is no need to clear the automount flag
  552. * following a mount or restore it after an expire because these mounts
  553. * are always covered. However, it is necessary to ensure that these
  554. * flags are clear on non-empty directories to avoid unnecessary calls
  555. * during path walks.
  556. */
  557. static void autofs_set_leaf_automount_flags(struct dentry *dentry)
  558. {
  559. struct dentry *parent;
  560. /* root and dentrys in the root are already handled */
  561. if (IS_ROOT(dentry->d_parent))
  562. return;
  563. managed_dentry_set_managed(dentry);
  564. parent = dentry->d_parent;
  565. /* only consider parents below dentrys in the root */
  566. if (IS_ROOT(parent->d_parent))
  567. return;
  568. managed_dentry_clear_managed(parent);
  569. }
  570. static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
  571. {
  572. struct list_head *d_child;
  573. struct dentry *parent;
  574. /* flags for dentrys in the root are handled elsewhere */
  575. if (IS_ROOT(dentry->d_parent))
  576. return;
  577. managed_dentry_clear_managed(dentry);
  578. parent = dentry->d_parent;
  579. /* only consider parents below dentrys in the root */
  580. if (IS_ROOT(parent->d_parent))
  581. return;
  582. d_child = &dentry->d_child;
  583. /* Set parent managed if it's becoming empty */
  584. if (d_child->next == &parent->d_subdirs &&
  585. d_child->prev == &parent->d_subdirs)
  586. managed_dentry_set_managed(parent);
  587. }
  588. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  589. {
  590. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  591. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  592. struct autofs_info *p_ino;
  593. pr_debug("dentry %p, removing %pd\n", dentry, dentry);
  594. if (!autofs4_oz_mode(sbi))
  595. return -EACCES;
  596. spin_lock(&sbi->lookup_lock);
  597. if (!simple_empty(dentry)) {
  598. spin_unlock(&sbi->lookup_lock);
  599. return -ENOTEMPTY;
  600. }
  601. __autofs4_add_expiring(dentry);
  602. d_drop(dentry);
  603. spin_unlock(&sbi->lookup_lock);
  604. if (sbi->version < 5)
  605. autofs_clear_leaf_automount_flags(dentry);
  606. if (atomic_dec_and_test(&ino->count)) {
  607. p_ino = autofs4_dentry_ino(dentry->d_parent);
  608. if (p_ino && dentry->d_parent != dentry)
  609. atomic_dec(&p_ino->count);
  610. }
  611. dput(ino->dentry);
  612. d_inode(dentry)->i_size = 0;
  613. clear_nlink(d_inode(dentry));
  614. if (dir->i_nlink)
  615. drop_nlink(dir);
  616. return 0;
  617. }
  618. static int autofs4_dir_mkdir(struct inode *dir,
  619. struct dentry *dentry, umode_t mode)
  620. {
  621. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  622. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  623. struct autofs_info *p_ino;
  624. struct inode *inode;
  625. if (!autofs4_oz_mode(sbi))
  626. return -EACCES;
  627. pr_debug("dentry %p, creating %pd\n", dentry, dentry);
  628. BUG_ON(!ino);
  629. autofs4_clean_ino(ino);
  630. autofs4_del_active(dentry);
  631. inode = autofs4_get_inode(dir->i_sb, S_IFDIR | mode);
  632. if (!inode)
  633. return -ENOMEM;
  634. d_add(dentry, inode);
  635. if (sbi->version < 5)
  636. autofs_set_leaf_automount_flags(dentry);
  637. dget(dentry);
  638. atomic_inc(&ino->count);
  639. p_ino = autofs4_dentry_ino(dentry->d_parent);
  640. if (p_ino && !IS_ROOT(dentry))
  641. atomic_inc(&p_ino->count);
  642. inc_nlink(dir);
  643. dir->i_mtime = current_time(dir);
  644. return 0;
  645. }
  646. /* Get/set timeout ioctl() operation */
  647. #ifdef CONFIG_COMPAT
  648. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  649. compat_ulong_t __user *p)
  650. {
  651. unsigned long ntimeout;
  652. int rv;
  653. rv = get_user(ntimeout, p);
  654. if (rv)
  655. goto error;
  656. rv = put_user(sbi->exp_timeout/HZ, p);
  657. if (rv)
  658. goto error;
  659. if (ntimeout > UINT_MAX/HZ)
  660. sbi->exp_timeout = 0;
  661. else
  662. sbi->exp_timeout = ntimeout * HZ;
  663. return 0;
  664. error:
  665. return rv;
  666. }
  667. #endif
  668. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  669. unsigned long __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 > ULONG_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. /* Return protocol version */
  688. static inline int autofs4_get_protover(struct autofs_sb_info *sbi,
  689. int __user *p)
  690. {
  691. return put_user(sbi->version, p);
  692. }
  693. /* Return protocol sub version */
  694. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi,
  695. int __user *p)
  696. {
  697. return put_user(sbi->sub_version, p);
  698. }
  699. /*
  700. * Tells the daemon whether it can umount the autofs mount.
  701. */
  702. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  703. {
  704. int status = 0;
  705. if (may_umount(mnt))
  706. status = 1;
  707. pr_debug("may umount %d\n", status);
  708. status = put_user(status, p);
  709. return status;
  710. }
  711. /* Identify autofs4_dentries - this is so we can tell if there's
  712. * an extra dentry refcount or not. We only hold a refcount on the
  713. * dentry if its non-negative (ie, d_inode != NULL)
  714. */
  715. int is_autofs4_dentry(struct dentry *dentry)
  716. {
  717. return dentry && d_really_is_positive(dentry) &&
  718. dentry->d_op == &autofs4_dentry_operations &&
  719. dentry->d_fsdata != NULL;
  720. }
  721. /*
  722. * ioctl()'s on the root directory is the chief method for the daemon to
  723. * generate kernel reactions
  724. */
  725. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  726. unsigned int cmd, unsigned long arg)
  727. {
  728. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  729. void __user *p = (void __user *)arg;
  730. pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
  731. cmd, arg, sbi, task_pgrp_nr(current));
  732. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  733. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  734. return -ENOTTY;
  735. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  736. return -EPERM;
  737. switch (cmd) {
  738. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  739. return autofs4_wait_release(sbi, (autofs_wqt_t) arg, 0);
  740. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  741. return autofs4_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
  742. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  743. autofs4_catatonic_mode(sbi);
  744. return 0;
  745. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  746. return autofs4_get_protover(sbi, p);
  747. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  748. return autofs4_get_protosubver(sbi, p);
  749. case AUTOFS_IOC_SETTIMEOUT:
  750. return autofs4_get_set_timeout(sbi, p);
  751. #ifdef CONFIG_COMPAT
  752. case AUTOFS_IOC_SETTIMEOUT32:
  753. return autofs4_compat_get_set_timeout(sbi, p);
  754. #endif
  755. case AUTOFS_IOC_ASKUMOUNT:
  756. return autofs4_ask_umount(filp->f_path.mnt, p);
  757. /* return a single thing to expire */
  758. case AUTOFS_IOC_EXPIRE:
  759. return autofs4_expire_run(inode->i_sb,
  760. filp->f_path.mnt, sbi, p);
  761. /* same as above, but can send multiple expires through pipe */
  762. case AUTOFS_IOC_EXPIRE_MULTI:
  763. return autofs4_expire_multi(inode->i_sb,
  764. filp->f_path.mnt, sbi, p);
  765. default:
  766. return -EINVAL;
  767. }
  768. }
  769. static long autofs4_root_ioctl(struct file *filp,
  770. unsigned int cmd, unsigned long arg)
  771. {
  772. struct inode *inode = file_inode(filp);
  773. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  774. }
  775. #ifdef CONFIG_COMPAT
  776. static long autofs4_root_compat_ioctl(struct file *filp,
  777. unsigned int cmd, unsigned long arg)
  778. {
  779. struct inode *inode = file_inode(filp);
  780. int ret;
  781. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  782. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  783. else
  784. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  785. (unsigned long) compat_ptr(arg));
  786. return ret;
  787. }
  788. #endif