root.c 24 KB

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