root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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(const struct path *, 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 (!path_is_mountpoint(&file->f_path) && 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(const struct path *path, bool rcu_walk)
  230. {
  231. struct autofs_sb_info *sbi = autofs4_sbi(path->dentry->d_sb);
  232. struct autofs_info *ino = autofs4_dentry_ino(path->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", path->dentry);
  238. status = autofs4_wait(sbi, path, 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(const struct path *path, bool rcu_walk)
  245. {
  246. struct dentry *dentry = path->dentry;
  247. struct dentry *expiring;
  248. expiring = autofs4_lookup_expiring(dentry, rcu_walk);
  249. if (IS_ERR(expiring))
  250. return PTR_ERR(expiring);
  251. if (!expiring)
  252. return autofs4_expire_wait(path, rcu_walk);
  253. else {
  254. const struct path this = { .mnt = path->mnt, .dentry = expiring };
  255. /*
  256. * If we are racing with expire the request might not
  257. * be quite complete, but the directory has been removed
  258. * so it must have been successful, just wait for it.
  259. */
  260. autofs4_expire_wait(&this, 0);
  261. autofs4_del_expiring(expiring);
  262. dput(expiring);
  263. }
  264. return 0;
  265. }
  266. static struct dentry *autofs4_mountpoint_changed(struct path *path)
  267. {
  268. struct dentry *dentry = path->dentry;
  269. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  270. /*
  271. * If this is an indirect mount the dentry could have gone away
  272. * as a result of an expire and a new one created.
  273. */
  274. if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
  275. struct dentry *parent = dentry->d_parent;
  276. struct autofs_info *ino;
  277. struct dentry *new;
  278. new = d_lookup(parent, &dentry->d_name);
  279. if (!new)
  280. return NULL;
  281. ino = autofs4_dentry_ino(new);
  282. ino->last_used = jiffies;
  283. dput(path->dentry);
  284. path->dentry = new;
  285. }
  286. return path->dentry;
  287. }
  288. static struct vfsmount *autofs4_d_automount(struct path *path)
  289. {
  290. struct dentry *dentry = path->dentry;
  291. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  292. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  293. int status;
  294. pr_debug("dentry=%p %pd\n", dentry, dentry);
  295. /* The daemon never triggers a mount. */
  296. if (autofs4_oz_mode(sbi))
  297. return NULL;
  298. /*
  299. * If an expire request is pending everyone must wait.
  300. * If the expire fails we're still mounted so continue
  301. * the follow and return. A return of -EAGAIN (which only
  302. * happens with indirect mounts) means the expire completed
  303. * and the directory was removed, so just go ahead and try
  304. * the mount.
  305. */
  306. status = do_expire_wait(path, 0);
  307. if (status && status != -EAGAIN)
  308. return NULL;
  309. /* Callback to the daemon to perform the mount or wait */
  310. spin_lock(&sbi->fs_lock);
  311. if (ino->flags & AUTOFS_INF_PENDING) {
  312. spin_unlock(&sbi->fs_lock);
  313. status = autofs4_mount_wait(path, 0);
  314. if (status)
  315. return ERR_PTR(status);
  316. goto done;
  317. }
  318. /*
  319. * If the dentry is a symlink it's equivalent to a directory
  320. * having path_is_mountpoint() true, so there's no need to call
  321. * back to the daemon.
  322. */
  323. if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
  324. spin_unlock(&sbi->fs_lock);
  325. goto done;
  326. }
  327. if (!path_is_mountpoint(path)) {
  328. /*
  329. * It's possible that user space hasn't removed directories
  330. * after umounting a rootless multi-mount, although it
  331. * should. For v5 path_has_submounts() is sufficient to
  332. * handle this because the leaves of the directory tree under
  333. * the mount never trigger mounts themselves (they have an
  334. * autofs trigger mount mounted on them). But v4 pseudo direct
  335. * mounts do need the leaves to trigger mounts. In this case
  336. * we have no choice but to use the list_empty() check and
  337. * require user space behave.
  338. */
  339. if (sbi->version > 4) {
  340. if (path_has_submounts(path)) {
  341. spin_unlock(&sbi->fs_lock);
  342. goto done;
  343. }
  344. } else {
  345. if (!simple_empty(dentry)) {
  346. spin_unlock(&sbi->fs_lock);
  347. goto done;
  348. }
  349. }
  350. ino->flags |= AUTOFS_INF_PENDING;
  351. spin_unlock(&sbi->fs_lock);
  352. status = autofs4_mount_wait(path, 0);
  353. spin_lock(&sbi->fs_lock);
  354. ino->flags &= ~AUTOFS_INF_PENDING;
  355. if (status) {
  356. spin_unlock(&sbi->fs_lock);
  357. return ERR_PTR(status);
  358. }
  359. }
  360. spin_unlock(&sbi->fs_lock);
  361. done:
  362. /* Mount succeeded, check if we ended up with a new dentry */
  363. dentry = autofs4_mountpoint_changed(path);
  364. if (!dentry)
  365. return ERR_PTR(-ENOENT);
  366. return NULL;
  367. }
  368. static int autofs4_d_manage(const struct path *path, bool rcu_walk)
  369. {
  370. struct dentry *dentry = path->dentry;
  371. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  372. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  373. int status;
  374. pr_debug("dentry=%p %pd\n", dentry, dentry);
  375. /* The daemon never waits. */
  376. if (autofs4_oz_mode(sbi)) {
  377. if (!path_is_mountpoint(path))
  378. return -EISDIR;
  379. return 0;
  380. }
  381. /* Wait for pending expires */
  382. if (do_expire_wait(path, rcu_walk) == -ECHILD)
  383. return -ECHILD;
  384. /*
  385. * This dentry may be under construction so wait on mount
  386. * completion.
  387. */
  388. status = autofs4_mount_wait(path, rcu_walk);
  389. if (status)
  390. return status;
  391. if (rcu_walk) {
  392. /* We don't need fs_lock in rcu_walk mode,
  393. * just testing 'AUTOFS_INFO_NO_RCU' is enough.
  394. * simple_empty() takes a spinlock, so leave it
  395. * to last.
  396. * We only return -EISDIR when certain this isn't
  397. * a mount-trap.
  398. */
  399. struct inode *inode;
  400. if (ino->flags & AUTOFS_INF_WANT_EXPIRE)
  401. return 0;
  402. if (path_is_mountpoint(path))
  403. return 0;
  404. inode = d_inode_rcu(dentry);
  405. if (inode && S_ISLNK(inode->i_mode))
  406. return -EISDIR;
  407. if (list_empty(&dentry->d_subdirs))
  408. return 0;
  409. if (!simple_empty(dentry))
  410. return -EISDIR;
  411. return 0;
  412. }
  413. spin_lock(&sbi->fs_lock);
  414. /*
  415. * If the dentry has been selected for expire while we slept
  416. * on the lock then it might go away. We'll deal with that in
  417. * ->d_automount() and wait on a new mount if the expire
  418. * succeeds or return here if it doesn't (since there's no
  419. * mount to follow with a rootless multi-mount).
  420. */
  421. if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
  422. /*
  423. * Any needed mounting has been completed and the path
  424. * updated so check if this is a rootless multi-mount so
  425. * we can avoid needless calls ->d_automount() and avoid
  426. * an incorrect ELOOP error return.
  427. */
  428. if ((!path_is_mountpoint(path) && !simple_empty(dentry)) ||
  429. (d_really_is_positive(dentry) && d_is_symlink(dentry)))
  430. status = -EISDIR;
  431. }
  432. spin_unlock(&sbi->fs_lock);
  433. return status;
  434. }
  435. /* Lookups in the root directory */
  436. static struct dentry *autofs4_lookup(struct inode *dir,
  437. struct dentry *dentry, unsigned int flags)
  438. {
  439. struct autofs_sb_info *sbi;
  440. struct autofs_info *ino;
  441. struct dentry *active;
  442. pr_debug("name = %pd\n", dentry);
  443. /* File name too long to exist */
  444. if (dentry->d_name.len > NAME_MAX)
  445. return ERR_PTR(-ENAMETOOLONG);
  446. sbi = autofs4_sbi(dir->i_sb);
  447. pr_debug("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
  448. current->pid, task_pgrp_nr(current), sbi->catatonic,
  449. autofs4_oz_mode(sbi));
  450. active = autofs4_lookup_active(dentry);
  451. if (active)
  452. return active;
  453. else {
  454. /*
  455. * A dentry that is not within the root can never trigger a
  456. * mount operation, unless the directory already exists, so we
  457. * can return fail immediately. The daemon however does need
  458. * to create directories within the file system.
  459. */
  460. if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  461. return ERR_PTR(-ENOENT);
  462. /* Mark entries in the root as mount triggers */
  463. if (IS_ROOT(dentry->d_parent) &&
  464. autofs_type_indirect(sbi->type))
  465. __managed_dentry_set_managed(dentry);
  466. ino = autofs4_new_ino(sbi);
  467. if (!ino)
  468. return ERR_PTR(-ENOMEM);
  469. dentry->d_fsdata = ino;
  470. ino->dentry = dentry;
  471. autofs4_add_active(dentry);
  472. }
  473. return NULL;
  474. }
  475. static int autofs4_dir_symlink(struct inode *dir,
  476. struct dentry *dentry,
  477. const char *symname)
  478. {
  479. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  480. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  481. struct autofs_info *p_ino;
  482. struct inode *inode;
  483. size_t size = strlen(symname);
  484. char *cp;
  485. pr_debug("%s <- %pd\n", symname, dentry);
  486. if (!autofs4_oz_mode(sbi))
  487. return -EACCES;
  488. BUG_ON(!ino);
  489. autofs4_clean_ino(ino);
  490. autofs4_del_active(dentry);
  491. cp = kmalloc(size + 1, GFP_KERNEL);
  492. if (!cp)
  493. return -ENOMEM;
  494. strcpy(cp, symname);
  495. inode = autofs4_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 = autofs4_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 autofs4_dir_rmdir()..
  525. */
  526. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  527. {
  528. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  529. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  530. struct autofs_info *p_ino;
  531. /* This allows root to remove symlinks */
  532. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  533. return -EPERM;
  534. if (atomic_dec_and_test(&ino->count)) {
  535. p_ino = autofs4_dentry_ino(dentry->d_parent);
  536. if (p_ino && !IS_ROOT(dentry))
  537. atomic_dec(&p_ino->count);
  538. }
  539. dput(ino->dentry);
  540. d_inode(dentry)->i_size = 0;
  541. clear_nlink(d_inode(dentry));
  542. dir->i_mtime = current_time(dir);
  543. spin_lock(&sbi->lookup_lock);
  544. __autofs4_add_expiring(dentry);
  545. d_drop(dentry);
  546. spin_unlock(&sbi->lookup_lock);
  547. return 0;
  548. }
  549. /*
  550. * Version 4 of autofs provides a pseudo direct mount implementation
  551. * that relies on directories at the leaves of a directory tree under
  552. * an indirect mount to trigger mounts. To allow for this we need to
  553. * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
  554. * of the directory tree. There is no need to clear the automount flag
  555. * following a mount or restore it after an expire because these mounts
  556. * are always covered. However, it is necessary to ensure that these
  557. * flags are clear on non-empty directories to avoid unnecessary calls
  558. * during path walks.
  559. */
  560. static void autofs_set_leaf_automount_flags(struct dentry *dentry)
  561. {
  562. struct dentry *parent;
  563. /* root and dentrys in the root are already handled */
  564. if (IS_ROOT(dentry->d_parent))
  565. return;
  566. managed_dentry_set_managed(dentry);
  567. parent = dentry->d_parent;
  568. /* only consider parents below dentrys in the root */
  569. if (IS_ROOT(parent->d_parent))
  570. return;
  571. managed_dentry_clear_managed(parent);
  572. }
  573. static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
  574. {
  575. struct list_head *d_child;
  576. struct dentry *parent;
  577. /* flags for dentrys in the root are handled elsewhere */
  578. if (IS_ROOT(dentry->d_parent))
  579. return;
  580. managed_dentry_clear_managed(dentry);
  581. parent = dentry->d_parent;
  582. /* only consider parents below dentrys in the root */
  583. if (IS_ROOT(parent->d_parent))
  584. return;
  585. d_child = &dentry->d_child;
  586. /* Set parent managed if it's becoming empty */
  587. if (d_child->next == &parent->d_subdirs &&
  588. d_child->prev == &parent->d_subdirs)
  589. managed_dentry_set_managed(parent);
  590. }
  591. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  592. {
  593. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  594. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  595. struct autofs_info *p_ino;
  596. pr_debug("dentry %p, removing %pd\n", dentry, dentry);
  597. if (!autofs4_oz_mode(sbi))
  598. return -EACCES;
  599. spin_lock(&sbi->lookup_lock);
  600. if (!simple_empty(dentry)) {
  601. spin_unlock(&sbi->lookup_lock);
  602. return -ENOTEMPTY;
  603. }
  604. __autofs4_add_expiring(dentry);
  605. d_drop(dentry);
  606. spin_unlock(&sbi->lookup_lock);
  607. if (sbi->version < 5)
  608. autofs_clear_leaf_automount_flags(dentry);
  609. if (atomic_dec_and_test(&ino->count)) {
  610. p_ino = autofs4_dentry_ino(dentry->d_parent);
  611. if (p_ino && dentry->d_parent != dentry)
  612. atomic_dec(&p_ino->count);
  613. }
  614. dput(ino->dentry);
  615. d_inode(dentry)->i_size = 0;
  616. clear_nlink(d_inode(dentry));
  617. if (dir->i_nlink)
  618. drop_nlink(dir);
  619. return 0;
  620. }
  621. static int autofs4_dir_mkdir(struct inode *dir,
  622. struct dentry *dentry, umode_t mode)
  623. {
  624. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  625. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  626. struct autofs_info *p_ino;
  627. struct inode *inode;
  628. if (!autofs4_oz_mode(sbi))
  629. return -EACCES;
  630. pr_debug("dentry %p, creating %pd\n", dentry, dentry);
  631. BUG_ON(!ino);
  632. autofs4_clean_ino(ino);
  633. autofs4_del_active(dentry);
  634. inode = autofs4_get_inode(dir->i_sb, S_IFDIR | mode);
  635. if (!inode)
  636. return -ENOMEM;
  637. d_add(dentry, inode);
  638. if (sbi->version < 5)
  639. autofs_set_leaf_automount_flags(dentry);
  640. dget(dentry);
  641. atomic_inc(&ino->count);
  642. p_ino = autofs4_dentry_ino(dentry->d_parent);
  643. if (p_ino && !IS_ROOT(dentry))
  644. atomic_inc(&p_ino->count);
  645. inc_nlink(dir);
  646. dir->i_mtime = current_time(dir);
  647. return 0;
  648. }
  649. /* Get/set timeout ioctl() operation */
  650. #ifdef CONFIG_COMPAT
  651. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  652. compat_ulong_t __user *p)
  653. {
  654. unsigned long ntimeout;
  655. int rv;
  656. rv = get_user(ntimeout, p);
  657. if (rv)
  658. goto error;
  659. rv = put_user(sbi->exp_timeout/HZ, p);
  660. if (rv)
  661. goto error;
  662. if (ntimeout > UINT_MAX/HZ)
  663. sbi->exp_timeout = 0;
  664. else
  665. sbi->exp_timeout = ntimeout * HZ;
  666. return 0;
  667. error:
  668. return rv;
  669. }
  670. #endif
  671. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  672. unsigned long __user *p)
  673. {
  674. unsigned long ntimeout;
  675. int rv;
  676. rv = get_user(ntimeout, p);
  677. if (rv)
  678. goto error;
  679. rv = put_user(sbi->exp_timeout/HZ, p);
  680. if (rv)
  681. goto error;
  682. if (ntimeout > ULONG_MAX/HZ)
  683. sbi->exp_timeout = 0;
  684. else
  685. sbi->exp_timeout = ntimeout * HZ;
  686. return 0;
  687. error:
  688. return rv;
  689. }
  690. /* Return protocol version */
  691. static inline int autofs4_get_protover(struct autofs_sb_info *sbi,
  692. int __user *p)
  693. {
  694. return put_user(sbi->version, p);
  695. }
  696. /* Return protocol sub version */
  697. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi,
  698. int __user *p)
  699. {
  700. return put_user(sbi->sub_version, p);
  701. }
  702. /*
  703. * Tells the daemon whether it can umount the autofs mount.
  704. */
  705. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  706. {
  707. int status = 0;
  708. if (may_umount(mnt))
  709. status = 1;
  710. pr_debug("may umount %d\n", status);
  711. status = put_user(status, p);
  712. return status;
  713. }
  714. /* Identify autofs4_dentries - this is so we can tell if there's
  715. * an extra dentry refcount or not. We only hold a refcount on the
  716. * dentry if its non-negative (ie, d_inode != NULL)
  717. */
  718. int is_autofs4_dentry(struct dentry *dentry)
  719. {
  720. return dentry && d_really_is_positive(dentry) &&
  721. dentry->d_op == &autofs4_dentry_operations &&
  722. dentry->d_fsdata != NULL;
  723. }
  724. /*
  725. * ioctl()'s on the root directory is the chief method for the daemon to
  726. * generate kernel reactions
  727. */
  728. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  729. unsigned int cmd, unsigned long arg)
  730. {
  731. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  732. void __user *p = (void __user *)arg;
  733. pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
  734. cmd, arg, sbi, task_pgrp_nr(current));
  735. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  736. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  737. return -ENOTTY;
  738. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  739. return -EPERM;
  740. switch (cmd) {
  741. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  742. return autofs4_wait_release(sbi, (autofs_wqt_t) arg, 0);
  743. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  744. return autofs4_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
  745. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  746. autofs4_catatonic_mode(sbi);
  747. return 0;
  748. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  749. return autofs4_get_protover(sbi, p);
  750. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  751. return autofs4_get_protosubver(sbi, p);
  752. case AUTOFS_IOC_SETTIMEOUT:
  753. return autofs4_get_set_timeout(sbi, p);
  754. #ifdef CONFIG_COMPAT
  755. case AUTOFS_IOC_SETTIMEOUT32:
  756. return autofs4_compat_get_set_timeout(sbi, p);
  757. #endif
  758. case AUTOFS_IOC_ASKUMOUNT:
  759. return autofs4_ask_umount(filp->f_path.mnt, p);
  760. /* return a single thing to expire */
  761. case AUTOFS_IOC_EXPIRE:
  762. return autofs4_expire_run(inode->i_sb,
  763. filp->f_path.mnt, sbi, p);
  764. /* same as above, but can send multiple expires through pipe */
  765. case AUTOFS_IOC_EXPIRE_MULTI:
  766. return autofs4_expire_multi(inode->i_sb,
  767. filp->f_path.mnt, sbi, p);
  768. default:
  769. return -EINVAL;
  770. }
  771. }
  772. static long autofs4_root_ioctl(struct file *filp,
  773. unsigned int cmd, unsigned long arg)
  774. {
  775. struct inode *inode = file_inode(filp);
  776. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  777. }
  778. #ifdef CONFIG_COMPAT
  779. static long autofs4_root_compat_ioctl(struct file *filp,
  780. unsigned int cmd, unsigned long arg)
  781. {
  782. struct inode *inode = file_inode(filp);
  783. int ret;
  784. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  785. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  786. else
  787. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  788. (unsigned long) compat_ptr(arg));
  789. return ret;
  790. }
  791. #endif