root.c 24 KB

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