root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 %.*s",
  101. file, dentry, dentry->d_name.len, dentry->d_name.name);
  102. if (autofs4_oz_mode(sbi))
  103. goto out;
  104. /*
  105. * An empty directory in an autofs file system is always a
  106. * mount point. The daemon must have failed to mount this
  107. * during lookup so it doesn't exist. This can happen, for
  108. * example, if user space returns an incorrect status for a
  109. * mount request. Otherwise we're doing a readdir on the
  110. * autofs file system so just let the libfs routines handle
  111. * it.
  112. */
  113. spin_lock(&sbi->lookup_lock);
  114. if (!d_mountpoint(dentry) && simple_empty(dentry)) {
  115. spin_unlock(&sbi->lookup_lock);
  116. return -ENOENT;
  117. }
  118. spin_unlock(&sbi->lookup_lock);
  119. out:
  120. return dcache_dir_open(inode, file);
  121. }
  122. static void autofs4_dentry_release(struct dentry *de)
  123. {
  124. struct autofs_info *ino = autofs4_dentry_ino(de);
  125. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  126. DPRINTK("releasing %p", de);
  127. if (!ino)
  128. return;
  129. if (sbi) {
  130. spin_lock(&sbi->lookup_lock);
  131. if (!list_empty(&ino->active))
  132. list_del(&ino->active);
  133. if (!list_empty(&ino->expiring))
  134. list_del(&ino->expiring);
  135. spin_unlock(&sbi->lookup_lock);
  136. }
  137. autofs4_free_ino(ino);
  138. }
  139. static struct dentry *autofs4_lookup_active(struct dentry *dentry)
  140. {
  141. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  142. struct dentry *parent = dentry->d_parent;
  143. struct qstr *name = &dentry->d_name;
  144. unsigned int len = name->len;
  145. unsigned int hash = name->hash;
  146. const unsigned char *str = name->name;
  147. struct list_head *p, *head;
  148. head = &sbi->active_list;
  149. if (list_empty(head))
  150. return NULL;
  151. spin_lock(&sbi->lookup_lock);
  152. list_for_each(p, head) {
  153. struct autofs_info *ino;
  154. struct dentry *active;
  155. struct qstr *qstr;
  156. ino = list_entry(p, struct autofs_info, active);
  157. active = ino->dentry;
  158. spin_lock(&active->d_lock);
  159. /* Already gone? */
  160. if ((int) d_count(active) <= 0)
  161. goto next;
  162. qstr = &active->d_name;
  163. if (active->d_name.hash != hash)
  164. goto next;
  165. if (active->d_parent != parent)
  166. goto next;
  167. if (qstr->len != len)
  168. goto next;
  169. if (memcmp(qstr->name, str, len))
  170. goto next;
  171. if (d_unhashed(active)) {
  172. dget_dlock(active);
  173. spin_unlock(&active->d_lock);
  174. spin_unlock(&sbi->lookup_lock);
  175. return active;
  176. }
  177. next:
  178. spin_unlock(&active->d_lock);
  179. }
  180. spin_unlock(&sbi->lookup_lock);
  181. return NULL;
  182. }
  183. static struct dentry *autofs4_lookup_expiring(struct dentry *dentry,
  184. bool rcu_walk)
  185. {
  186. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  187. struct dentry *parent = dentry->d_parent;
  188. struct qstr *name = &dentry->d_name;
  189. unsigned int len = name->len;
  190. unsigned int hash = name->hash;
  191. const unsigned char *str = name->name;
  192. struct list_head *p, *head;
  193. head = &sbi->expiring_list;
  194. if (list_empty(head))
  195. return NULL;
  196. spin_lock(&sbi->lookup_lock);
  197. list_for_each(p, head) {
  198. struct autofs_info *ino;
  199. struct dentry *expiring;
  200. struct qstr *qstr;
  201. if (rcu_walk) {
  202. spin_unlock(&sbi->lookup_lock);
  203. return ERR_PTR(-ECHILD);
  204. }
  205. ino = list_entry(p, struct autofs_info, expiring);
  206. expiring = ino->dentry;
  207. spin_lock(&expiring->d_lock);
  208. /* We've already been dentry_iput or unlinked */
  209. if (!expiring->d_inode)
  210. goto next;
  211. qstr = &expiring->d_name;
  212. if (expiring->d_name.hash != hash)
  213. goto next;
  214. if (expiring->d_parent != parent)
  215. goto next;
  216. if (qstr->len != len)
  217. goto next;
  218. if (memcmp(qstr->name, str, len))
  219. goto next;
  220. if (d_unhashed(expiring)) {
  221. dget_dlock(expiring);
  222. spin_unlock(&expiring->d_lock);
  223. spin_unlock(&sbi->lookup_lock);
  224. return expiring;
  225. }
  226. next:
  227. spin_unlock(&expiring->d_lock);
  228. }
  229. spin_unlock(&sbi->lookup_lock);
  230. return NULL;
  231. }
  232. static int autofs4_mount_wait(struct dentry *dentry, bool rcu_walk)
  233. {
  234. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  235. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  236. int status = 0;
  237. if (ino->flags & AUTOFS_INF_PENDING) {
  238. if (rcu_walk)
  239. return -ECHILD;
  240. DPRINTK("waiting for mount name=%.*s",
  241. dentry->d_name.len, dentry->d_name.name);
  242. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  243. DPRINTK("mount wait done status=%d", status);
  244. }
  245. ino->last_used = jiffies;
  246. return status;
  247. }
  248. static int do_expire_wait(struct dentry *dentry, bool rcu_walk)
  249. {
  250. struct dentry *expiring;
  251. expiring = autofs4_lookup_expiring(dentry, rcu_walk);
  252. if (IS_ERR(expiring))
  253. return PTR_ERR(expiring);
  254. if (!expiring)
  255. return autofs4_expire_wait(dentry, rcu_walk);
  256. else {
  257. /*
  258. * If we are racing with expire the request might not
  259. * be quite complete, but the directory has been removed
  260. * so it must have been successful, just wait for it.
  261. */
  262. autofs4_expire_wait(expiring, 0);
  263. autofs4_del_expiring(expiring);
  264. dput(expiring);
  265. }
  266. return 0;
  267. }
  268. static struct dentry *autofs4_mountpoint_changed(struct path *path)
  269. {
  270. struct dentry *dentry = path->dentry;
  271. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  272. /*
  273. * If this is an indirect mount the dentry could have gone away
  274. * as a result of an expire and a new one created.
  275. */
  276. if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
  277. struct dentry *parent = dentry->d_parent;
  278. struct autofs_info *ino;
  279. struct dentry *new = d_lookup(parent, &dentry->d_name);
  280. if (!new)
  281. return NULL;
  282. ino = autofs4_dentry_ino(new);
  283. ino->last_used = jiffies;
  284. dput(path->dentry);
  285. path->dentry = new;
  286. }
  287. return path->dentry;
  288. }
  289. static struct vfsmount *autofs4_d_automount(struct path *path)
  290. {
  291. struct dentry *dentry = path->dentry;
  292. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  293. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  294. int status;
  295. DPRINTK("dentry=%p %.*s",
  296. dentry, dentry->d_name.len, dentry->d_name.name);
  297. /* The daemon never triggers a mount. */
  298. if (autofs4_oz_mode(sbi))
  299. return NULL;
  300. /*
  301. * If an expire request is pending everyone must wait.
  302. * If the expire fails we're still mounted so continue
  303. * the follow and return. A return of -EAGAIN (which only
  304. * happens with indirect mounts) means the expire completed
  305. * and the directory was removed, so just go ahead and try
  306. * the mount.
  307. */
  308. status = do_expire_wait(dentry, 0);
  309. if (status && status != -EAGAIN)
  310. return NULL;
  311. /* Callback to the daemon to perform the mount or wait */
  312. spin_lock(&sbi->fs_lock);
  313. if (ino->flags & AUTOFS_INF_PENDING) {
  314. spin_unlock(&sbi->fs_lock);
  315. status = autofs4_mount_wait(dentry, 0);
  316. if (status)
  317. return ERR_PTR(status);
  318. goto done;
  319. }
  320. /*
  321. * If the dentry is a symlink it's equivalent to a directory
  322. * having d_mountpoint() true, so there's no need to call back
  323. * to the daemon.
  324. */
  325. if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)) {
  326. spin_unlock(&sbi->fs_lock);
  327. goto done;
  328. }
  329. if (!d_mountpoint(dentry)) {
  330. /*
  331. * It's possible that user space hasn't removed directories
  332. * after umounting a rootless multi-mount, although it
  333. * should. For v5 have_submounts() is sufficient to handle
  334. * this because the leaves of the directory tree under the
  335. * mount never trigger mounts themselves (they have an autofs
  336. * trigger mount mounted on them). But v4 pseudo direct mounts
  337. * do need the leaves to trigger mounts. In this case we
  338. * have no choice but to use the list_empty() check and
  339. * require user space behave.
  340. */
  341. if (sbi->version > 4) {
  342. if (have_submounts(dentry)) {
  343. spin_unlock(&sbi->fs_lock);
  344. goto done;
  345. }
  346. } else {
  347. if (!simple_empty(dentry)) {
  348. spin_unlock(&sbi->fs_lock);
  349. goto done;
  350. }
  351. }
  352. ino->flags |= AUTOFS_INF_PENDING;
  353. spin_unlock(&sbi->fs_lock);
  354. status = autofs4_mount_wait(dentry, 0);
  355. spin_lock(&sbi->fs_lock);
  356. ino->flags &= ~AUTOFS_INF_PENDING;
  357. if (status) {
  358. spin_unlock(&sbi->fs_lock);
  359. return ERR_PTR(status);
  360. }
  361. }
  362. spin_unlock(&sbi->fs_lock);
  363. done:
  364. /* Mount succeeded, check if we ended up with a new dentry */
  365. dentry = autofs4_mountpoint_changed(path);
  366. if (!dentry)
  367. return ERR_PTR(-ENOENT);
  368. return NULL;
  369. }
  370. static int autofs4_d_manage(struct dentry *dentry, bool rcu_walk)
  371. {
  372. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  373. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  374. int status;
  375. DPRINTK("dentry=%p %.*s",
  376. dentry, dentry->d_name.len, dentry->d_name.name);
  377. /* The daemon never waits. */
  378. if (autofs4_oz_mode(sbi)) {
  379. if (!d_mountpoint(dentry))
  380. return -EISDIR;
  381. return 0;
  382. }
  383. /* Wait for pending expires */
  384. if (do_expire_wait(dentry, rcu_walk) == -ECHILD)
  385. return -ECHILD;
  386. /*
  387. * This dentry may be under construction so wait on mount
  388. * completion.
  389. */
  390. status = autofs4_mount_wait(dentry, rcu_walk);
  391. if (status)
  392. return status;
  393. if (rcu_walk) {
  394. /* We don't need fs_lock in rcu_walk mode,
  395. * just testing 'AUTOFS_INFO_NO_RCU' is enough.
  396. * simple_empty() takes a spinlock, so leave it
  397. * to last.
  398. * We only return -EISDIR when certain this isn't
  399. * a mount-trap.
  400. */
  401. struct inode *inode;
  402. if (ino->flags & (AUTOFS_INF_EXPIRING | AUTOFS_INF_NO_RCU))
  403. return 0;
  404. if (d_mountpoint(dentry))
  405. return 0;
  406. inode = ACCESS_ONCE(dentry->d_inode);
  407. if (inode && S_ISLNK(inode->i_mode))
  408. return -EISDIR;
  409. if (list_empty(&dentry->d_subdirs))
  410. return 0;
  411. if (!simple_empty(dentry))
  412. return -EISDIR;
  413. return 0;
  414. }
  415. spin_lock(&sbi->fs_lock);
  416. /*
  417. * If the dentry has been selected for expire while we slept
  418. * on the lock then it might go away. We'll deal with that in
  419. * ->d_automount() and wait on a new mount if the expire
  420. * succeeds or return here if it doesn't (since there's no
  421. * mount to follow with a rootless multi-mount).
  422. */
  423. if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
  424. /*
  425. * Any needed mounting has been completed and the path
  426. * updated so check if this is a rootless multi-mount so
  427. * we can avoid needless calls ->d_automount() and avoid
  428. * an incorrect ELOOP error return.
  429. */
  430. if ((!d_mountpoint(dentry) && !simple_empty(dentry)) ||
  431. (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
  432. status = -EISDIR;
  433. }
  434. spin_unlock(&sbi->fs_lock);
  435. return status;
  436. }
  437. /* Lookups in the root directory */
  438. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  439. {
  440. struct autofs_sb_info *sbi;
  441. struct autofs_info *ino;
  442. struct dentry *active;
  443. DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
  444. /* File name too long to exist */
  445. if (dentry->d_name.len > NAME_MAX)
  446. return ERR_PTR(-ENAMETOOLONG);
  447. sbi = autofs4_sbi(dir->i_sb);
  448. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  449. current->pid, task_pgrp_nr(current), sbi->catatonic,
  450. autofs4_oz_mode(sbi));
  451. active = autofs4_lookup_active(dentry);
  452. if (active) {
  453. return active;
  454. } else {
  455. /*
  456. * A dentry that is not within the root can never trigger a
  457. * mount operation, unless the directory already exists, so we
  458. * can return fail immediately. The daemon however does need
  459. * to create directories within the file system.
  460. */
  461. if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  462. return ERR_PTR(-ENOENT);
  463. /* Mark entries in the root as mount triggers */
  464. if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent))
  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. d_instantiate(dentry, NULL);
  473. }
  474. return NULL;
  475. }
  476. static int autofs4_dir_symlink(struct inode *dir,
  477. struct dentry *dentry,
  478. const char *symname)
  479. {
  480. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  481. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  482. struct autofs_info *p_ino;
  483. struct inode *inode;
  484. size_t size = strlen(symname);
  485. char *cp;
  486. DPRINTK("%s <- %.*s", symname,
  487. dentry->d_name.len, dentry->d_name.name);
  488. if (!autofs4_oz_mode(sbi))
  489. return -EACCES;
  490. BUG_ON(!ino);
  491. autofs4_clean_ino(ino);
  492. autofs4_del_active(dentry);
  493. cp = kmalloc(size + 1, GFP_KERNEL);
  494. if (!cp)
  495. return -ENOMEM;
  496. strcpy(cp, symname);
  497. inode = autofs4_get_inode(dir->i_sb, S_IFLNK | 0555);
  498. if (!inode) {
  499. kfree(cp);
  500. if (!dentry->d_fsdata)
  501. kfree(ino);
  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;
  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. dentry->d_inode->i_size = 0;
  545. clear_nlink(dentry->d_inode);
  546. dir->i_mtime = CURRENT_TIME;
  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. return;
  577. }
  578. static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
  579. {
  580. struct list_head *d_child;
  581. struct dentry *parent;
  582. /* flags for dentrys in the root are handled elsewhere */
  583. if (IS_ROOT(dentry->d_parent))
  584. return;
  585. managed_dentry_clear_managed(dentry);
  586. parent = dentry->d_parent;
  587. /* only consider parents below dentrys in the root */
  588. if (IS_ROOT(parent->d_parent))
  589. return;
  590. d_child = &dentry->d_u.d_child;
  591. /* Set parent managed if it's becoming empty */
  592. if (d_child->next == &parent->d_subdirs &&
  593. d_child->prev == &parent->d_subdirs)
  594. managed_dentry_set_managed(parent);
  595. return;
  596. }
  597. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  598. {
  599. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  600. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  601. struct autofs_info *p_ino;
  602. DPRINTK("dentry %p, removing %.*s",
  603. dentry, dentry->d_name.len, dentry->d_name.name);
  604. if (!autofs4_oz_mode(sbi))
  605. return -EACCES;
  606. spin_lock(&sbi->lookup_lock);
  607. if (!simple_empty(dentry)) {
  608. spin_unlock(&sbi->lookup_lock);
  609. return -ENOTEMPTY;
  610. }
  611. __autofs4_add_expiring(dentry);
  612. d_drop(dentry);
  613. spin_unlock(&sbi->lookup_lock);
  614. if (sbi->version < 5)
  615. autofs_clear_leaf_automount_flags(dentry);
  616. if (atomic_dec_and_test(&ino->count)) {
  617. p_ino = autofs4_dentry_ino(dentry->d_parent);
  618. if (p_ino && dentry->d_parent != dentry)
  619. atomic_dec(&p_ino->count);
  620. }
  621. dput(ino->dentry);
  622. dentry->d_inode->i_size = 0;
  623. clear_nlink(dentry->d_inode);
  624. if (dir->i_nlink)
  625. drop_nlink(dir);
  626. return 0;
  627. }
  628. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  629. {
  630. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  631. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  632. struct autofs_info *p_ino;
  633. struct inode *inode;
  634. if (!autofs4_oz_mode(sbi))
  635. return -EACCES;
  636. DPRINTK("dentry %p, creating %.*s",
  637. dentry, dentry->d_name.len, dentry->d_name.name);
  638. BUG_ON(!ino);
  639. autofs4_clean_ino(ino);
  640. autofs4_del_active(dentry);
  641. inode = autofs4_get_inode(dir->i_sb, S_IFDIR | 0555);
  642. if (!inode)
  643. return -ENOMEM;
  644. d_add(dentry, inode);
  645. if (sbi->version < 5)
  646. autofs_set_leaf_automount_flags(dentry);
  647. dget(dentry);
  648. atomic_inc(&ino->count);
  649. p_ino = autofs4_dentry_ino(dentry->d_parent);
  650. if (p_ino && !IS_ROOT(dentry))
  651. atomic_inc(&p_ino->count);
  652. inc_nlink(dir);
  653. dir->i_mtime = CURRENT_TIME;
  654. return 0;
  655. }
  656. /* Get/set timeout ioctl() operation */
  657. #ifdef CONFIG_COMPAT
  658. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  659. compat_ulong_t __user *p)
  660. {
  661. int rv;
  662. unsigned long ntimeout;
  663. if ((rv = get_user(ntimeout, p)) ||
  664. (rv = put_user(sbi->exp_timeout/HZ, p)))
  665. return rv;
  666. if (ntimeout > UINT_MAX/HZ)
  667. sbi->exp_timeout = 0;
  668. else
  669. sbi->exp_timeout = ntimeout * HZ;
  670. return 0;
  671. }
  672. #endif
  673. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  674. unsigned long __user *p)
  675. {
  676. int rv;
  677. unsigned long ntimeout;
  678. if ((rv = get_user(ntimeout, p)) ||
  679. (rv = put_user(sbi->exp_timeout/HZ, p)))
  680. return rv;
  681. if (ntimeout > ULONG_MAX/HZ)
  682. sbi->exp_timeout = 0;
  683. else
  684. sbi->exp_timeout = ntimeout * HZ;
  685. return 0;
  686. }
  687. /* Return protocol version */
  688. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  689. {
  690. return put_user(sbi->version, p);
  691. }
  692. /* Return protocol sub version */
  693. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  694. {
  695. return put_user(sbi->sub_version, p);
  696. }
  697. /*
  698. * Tells the daemon whether it can umount the autofs mount.
  699. */
  700. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  701. {
  702. int status = 0;
  703. if (may_umount(mnt))
  704. status = 1;
  705. DPRINTK("returning %d", status);
  706. status = put_user(status, p);
  707. return status;
  708. }
  709. /* Identify autofs4_dentries - this is so we can tell if there's
  710. an extra dentry refcount or not. We only hold a refcount on the
  711. dentry if its non-negative (ie, d_inode != NULL)
  712. */
  713. int is_autofs4_dentry(struct dentry *dentry)
  714. {
  715. return dentry && dentry->d_inode &&
  716. dentry->d_op == &autofs4_dentry_operations &&
  717. dentry->d_fsdata != NULL;
  718. }
  719. /*
  720. * ioctl()'s on the root directory is the chief method for the daemon to
  721. * generate kernel reactions
  722. */
  723. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  724. unsigned int cmd, unsigned long arg)
  725. {
  726. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  727. void __user *p = (void __user *)arg;
  728. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  729. cmd,arg,sbi,task_pgrp_nr(current));
  730. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  731. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  732. return -ENOTTY;
  733. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  734. return -EPERM;
  735. switch(cmd) {
  736. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  737. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  738. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  739. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  740. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  741. autofs4_catatonic_mode(sbi);
  742. return 0;
  743. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  744. return autofs4_get_protover(sbi, p);
  745. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  746. return autofs4_get_protosubver(sbi, p);
  747. case AUTOFS_IOC_SETTIMEOUT:
  748. return autofs4_get_set_timeout(sbi, p);
  749. #ifdef CONFIG_COMPAT
  750. case AUTOFS_IOC_SETTIMEOUT32:
  751. return autofs4_compat_get_set_timeout(sbi, p);
  752. #endif
  753. case AUTOFS_IOC_ASKUMOUNT:
  754. return autofs4_ask_umount(filp->f_path.mnt, p);
  755. /* return a single thing to expire */
  756. case AUTOFS_IOC_EXPIRE:
  757. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  758. /* same as above, but can send multiple expires through pipe */
  759. case AUTOFS_IOC_EXPIRE_MULTI:
  760. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  761. default:
  762. return -ENOSYS;
  763. }
  764. }
  765. static long autofs4_root_ioctl(struct file *filp,
  766. unsigned int cmd, unsigned long arg)
  767. {
  768. struct inode *inode = file_inode(filp);
  769. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  770. }
  771. #ifdef CONFIG_COMPAT
  772. static long autofs4_root_compat_ioctl(struct file *filp,
  773. unsigned int cmd, unsigned long arg)
  774. {
  775. struct inode *inode = file_inode(filp);
  776. int ret;
  777. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  778. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  779. else
  780. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  781. (unsigned long)compat_ptr(arg));
  782. return ret;
  783. }
  784. #endif