root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. DEFINE_SPINLOCK(autofs4_lock);
  24. static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
  25. static int autofs4_dir_unlink(struct inode *,struct dentry *);
  26. static int autofs4_dir_rmdir(struct inode *,struct dentry *);
  27. static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
  28. static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
  29. #ifdef CONFIG_COMPAT
  30. static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
  31. #endif
  32. static int autofs4_dir_open(struct inode *inode, struct file *file);
  33. static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
  34. static struct vfsmount *autofs4_d_automount(struct path *);
  35. static int autofs4_d_manage(struct dentry *, bool, bool);
  36. const struct file_operations autofs4_root_operations = {
  37. .open = dcache_dir_open,
  38. .release = dcache_dir_close,
  39. .read = generic_read_dir,
  40. .readdir = 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. .readdir = 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(&autofs4_lock);
  114. spin_lock(&dentry->d_lock);
  115. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  116. spin_unlock(&dentry->d_lock);
  117. spin_unlock(&autofs4_lock);
  118. return -ENOENT;
  119. }
  120. spin_unlock(&dentry->d_lock);
  121. spin_unlock(&autofs4_lock);
  122. out:
  123. return dcache_dir_open(inode, file);
  124. }
  125. void autofs4_dentry_release(struct dentry *de)
  126. {
  127. struct autofs_info *inf;
  128. DPRINTK("releasing %p", de);
  129. inf = autofs4_dentry_ino(de);
  130. if (inf) {
  131. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  132. if (sbi) {
  133. spin_lock(&sbi->lookup_lock);
  134. if (!list_empty(&inf->active))
  135. list_del(&inf->active);
  136. if (!list_empty(&inf->expiring))
  137. list_del(&inf->expiring);
  138. spin_unlock(&sbi->lookup_lock);
  139. }
  140. autofs4_free_ino(inf);
  141. }
  142. }
  143. static struct dentry *autofs4_lookup_active(struct dentry *dentry)
  144. {
  145. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  146. struct dentry *parent = dentry->d_parent;
  147. struct qstr *name = &dentry->d_name;
  148. unsigned int len = name->len;
  149. unsigned int hash = name->hash;
  150. const unsigned char *str = name->name;
  151. struct list_head *p, *head;
  152. spin_lock(&autofs4_lock);
  153. spin_lock(&sbi->lookup_lock);
  154. head = &sbi->active_list;
  155. list_for_each(p, head) {
  156. struct autofs_info *ino;
  157. struct dentry *active;
  158. struct qstr *qstr;
  159. ino = list_entry(p, struct autofs_info, active);
  160. active = ino->dentry;
  161. spin_lock(&active->d_lock);
  162. /* Already gone? */
  163. if (active->d_count == 0)
  164. goto next;
  165. qstr = &active->d_name;
  166. if (active->d_name.hash != hash)
  167. goto next;
  168. if (active->d_parent != parent)
  169. goto next;
  170. if (qstr->len != len)
  171. goto next;
  172. if (memcmp(qstr->name, str, len))
  173. goto next;
  174. if (d_unhashed(active)) {
  175. dget_dlock(active);
  176. spin_unlock(&active->d_lock);
  177. spin_unlock(&sbi->lookup_lock);
  178. spin_unlock(&autofs4_lock);
  179. return active;
  180. }
  181. next:
  182. spin_unlock(&active->d_lock);
  183. }
  184. spin_unlock(&sbi->lookup_lock);
  185. spin_unlock(&autofs4_lock);
  186. return NULL;
  187. }
  188. static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
  189. {
  190. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  191. struct dentry *parent = dentry->d_parent;
  192. struct qstr *name = &dentry->d_name;
  193. unsigned int len = name->len;
  194. unsigned int hash = name->hash;
  195. const unsigned char *str = name->name;
  196. struct list_head *p, *head;
  197. spin_lock(&autofs4_lock);
  198. spin_lock(&sbi->lookup_lock);
  199. head = &sbi->expiring_list;
  200. list_for_each(p, head) {
  201. struct autofs_info *ino;
  202. struct dentry *expiring;
  203. struct qstr *qstr;
  204. ino = list_entry(p, struct autofs_info, expiring);
  205. expiring = ino->dentry;
  206. spin_lock(&expiring->d_lock);
  207. /* Bad luck, we've already been dentry_iput */
  208. if (!expiring->d_inode)
  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. spin_unlock(&autofs4_lock);
  224. return expiring;
  225. }
  226. next:
  227. spin_unlock(&expiring->d_lock);
  228. }
  229. spin_unlock(&sbi->lookup_lock);
  230. spin_unlock(&autofs4_lock);
  231. return NULL;
  232. }
  233. static int autofs4_mount_wait(struct dentry *dentry)
  234. {
  235. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  236. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  237. int status;
  238. if (ino->flags & AUTOFS_INF_PENDING) {
  239. DPRINTK("waiting for mount name=%.*s",
  240. dentry->d_name.len, dentry->d_name.name);
  241. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  242. DPRINTK("mount wait done status=%d", status);
  243. ino->last_used = jiffies;
  244. return status;
  245. }
  246. return 0;
  247. }
  248. static int do_expire_wait(struct dentry *dentry)
  249. {
  250. struct dentry *expiring;
  251. expiring = autofs4_lookup_expiring(dentry);
  252. if (!expiring)
  253. return autofs4_expire_wait(dentry);
  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);
  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 = d_lookup(parent, &dentry->d_name);
  277. if (!new)
  278. return NULL;
  279. dput(path->dentry);
  280. path->dentry = new;
  281. }
  282. return path->dentry;
  283. }
  284. static struct vfsmount *autofs4_d_automount(struct path *path)
  285. {
  286. struct dentry *dentry = path->dentry;
  287. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  288. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  289. int status;
  290. DPRINTK("dentry=%p %.*s",
  291. dentry, dentry->d_name.len, dentry->d_name.name);
  292. /*
  293. * Someone may have manually umounted this or it was a submount
  294. * that has gone away.
  295. */
  296. spin_lock(&dentry->d_lock);
  297. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  298. if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  299. (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  300. __managed_dentry_set_transit(path->dentry);
  301. }
  302. spin_unlock(&dentry->d_lock);
  303. /* The daemon never triggers a mount. */
  304. if (autofs4_oz_mode(sbi))
  305. return NULL;
  306. /*
  307. * If an expire request is pending everyone must wait.
  308. * If the expire fails we're still mounted so continue
  309. * the follow and return. A return of -EAGAIN (which only
  310. * happens with indirect mounts) means the expire completed
  311. * and the directory was removed, so just go ahead and try
  312. * the mount.
  313. */
  314. status = do_expire_wait(dentry);
  315. if (status && status != -EAGAIN)
  316. return NULL;
  317. /* Callback to the daemon to perform the mount or wait */
  318. spin_lock(&sbi->fs_lock);
  319. if (ino->flags & AUTOFS_INF_PENDING) {
  320. spin_unlock(&sbi->fs_lock);
  321. status = autofs4_mount_wait(dentry);
  322. if (status)
  323. return ERR_PTR(status);
  324. spin_lock(&sbi->fs_lock);
  325. goto done;
  326. }
  327. /*
  328. * If the dentry is a symlink it's equivalent to a directory
  329. * having d_mountpoint() true, so there's no need to call back
  330. * to the daemon.
  331. */
  332. if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
  333. goto done;
  334. if (!d_mountpoint(dentry)) {
  335. /*
  336. * It's possible that user space hasn't removed directories
  337. * after umounting a rootless multi-mount, although it
  338. * should. For v5 have_submounts() is sufficient to handle
  339. * this because the leaves of the directory tree under the
  340. * mount never trigger mounts themselves (they have an autofs
  341. * trigger mount mounted on them). But v4 pseudo direct mounts
  342. * do need the leaves to to trigger mounts. In this case we
  343. * have no choice but to use the list_empty() check and
  344. * require user space behave.
  345. */
  346. if (sbi->version > 4) {
  347. if (have_submounts(dentry))
  348. goto done;
  349. } else {
  350. spin_lock(&dentry->d_lock);
  351. if (!list_empty(&dentry->d_subdirs)) {
  352. spin_unlock(&dentry->d_lock);
  353. goto done;
  354. }
  355. spin_unlock(&dentry->d_lock);
  356. }
  357. ino->flags |= AUTOFS_INF_PENDING;
  358. spin_unlock(&sbi->fs_lock);
  359. status = autofs4_mount_wait(dentry);
  360. if (status)
  361. return ERR_PTR(status);
  362. spin_lock(&sbi->fs_lock);
  363. ino->flags &= ~AUTOFS_INF_PENDING;
  364. }
  365. done:
  366. if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
  367. /*
  368. * Any needed mounting has been completed and the path updated
  369. * so turn this into a normal dentry so we don't continually
  370. * call ->d_automount() and ->d_manage().
  371. */
  372. spin_lock(&dentry->d_lock);
  373. __managed_dentry_clear_transit(dentry);
  374. /*
  375. * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
  376. * symlinks as in all other cases the dentry will be covered by
  377. * an actual mount so ->d_automount() won't be called during
  378. * the follow.
  379. */
  380. if ((!d_mountpoint(dentry) &&
  381. !list_empty(&dentry->d_subdirs)) ||
  382. (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
  383. __managed_dentry_clear_automount(dentry);
  384. spin_unlock(&dentry->d_lock);
  385. }
  386. spin_unlock(&sbi->fs_lock);
  387. /* Mount succeeded, check if we ended up with a new dentry */
  388. dentry = autofs4_mountpoint_changed(path);
  389. if (!dentry)
  390. return ERR_PTR(-ENOENT);
  391. return NULL;
  392. }
  393. int autofs4_d_manage(struct dentry *dentry, bool mounting_here, bool rcu_walk)
  394. {
  395. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  396. DPRINTK("dentry=%p %.*s",
  397. dentry, dentry->d_name.len, dentry->d_name.name);
  398. /* The daemon never waits. */
  399. if (autofs4_oz_mode(sbi) || mounting_here) {
  400. if (!d_mountpoint(dentry))
  401. return -EISDIR;
  402. return 0;
  403. }
  404. /* We need to sleep, so we need pathwalk to be in ref-mode */
  405. if (rcu_walk)
  406. return -ECHILD;
  407. /* Wait for pending expires */
  408. do_expire_wait(dentry);
  409. /*
  410. * This dentry may be under construction so wait on mount
  411. * completion.
  412. */
  413. return autofs4_mount_wait(dentry);
  414. }
  415. /* Lookups in the root directory */
  416. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  417. {
  418. struct autofs_sb_info *sbi;
  419. struct autofs_info *ino;
  420. struct dentry *active;
  421. DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
  422. /* File name too long to exist */
  423. if (dentry->d_name.len > NAME_MAX)
  424. return ERR_PTR(-ENAMETOOLONG);
  425. sbi = autofs4_sbi(dir->i_sb);
  426. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  427. current->pid, task_pgrp_nr(current), sbi->catatonic,
  428. autofs4_oz_mode(sbi));
  429. active = autofs4_lookup_active(dentry);
  430. if (active) {
  431. return active;
  432. } else {
  433. /*
  434. * A dentry that is not within the root can never trigger a
  435. * mount operation, unless the directory already exists, so we
  436. * can return fail immediately. The daemon however does need
  437. * to create directories within the file system.
  438. */
  439. if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  440. return ERR_PTR(-ENOENT);
  441. /* Mark entries in the root as mount triggers */
  442. if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent))
  443. __managed_dentry_set_managed(dentry);
  444. ino = autofs4_init_ino(NULL, sbi);
  445. if (!ino)
  446. return ERR_PTR(-ENOMEM);
  447. dentry->d_fsdata = ino;
  448. ino->dentry = dentry;
  449. autofs4_add_active(dentry);
  450. d_instantiate(dentry, NULL);
  451. }
  452. return NULL;
  453. }
  454. static int autofs4_dir_symlink(struct inode *dir,
  455. struct dentry *dentry,
  456. const char *symname)
  457. {
  458. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  459. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  460. struct autofs_info *p_ino;
  461. struct inode *inode;
  462. size_t size = strlen(symname);
  463. char *cp;
  464. DPRINTK("%s <- %.*s", symname,
  465. dentry->d_name.len, dentry->d_name.name);
  466. if (!autofs4_oz_mode(sbi))
  467. return -EACCES;
  468. ino = autofs4_init_ino(ino, sbi);
  469. if (!ino)
  470. return -ENOMEM;
  471. autofs4_del_active(dentry);
  472. cp = kmalloc(size + 1, GFP_KERNEL);
  473. if (!cp) {
  474. if (!dentry->d_fsdata)
  475. kfree(ino);
  476. return -ENOMEM;
  477. }
  478. strcpy(cp, symname);
  479. inode = autofs4_get_inode(dir->i_sb, ino, S_IFLNK | 0555);
  480. if (!inode) {
  481. kfree(cp);
  482. if (!dentry->d_fsdata)
  483. kfree(ino);
  484. return -ENOMEM;
  485. }
  486. inode->i_private = cp;
  487. inode->i_size = size;
  488. d_add(dentry, inode);
  489. dentry->d_fsdata = ino;
  490. ino->dentry = dget(dentry);
  491. atomic_inc(&ino->count);
  492. p_ino = autofs4_dentry_ino(dentry->d_parent);
  493. if (p_ino && dentry->d_parent != dentry)
  494. atomic_inc(&p_ino->count);
  495. dir->i_mtime = CURRENT_TIME;
  496. return 0;
  497. }
  498. /*
  499. * NOTE!
  500. *
  501. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  502. * that the file no longer exists. However, doing that means that the
  503. * VFS layer can turn the dentry into a negative dentry. We don't want
  504. * this, because the unlink is probably the result of an expire.
  505. * We simply d_drop it and add it to a expiring list in the super block,
  506. * which allows the dentry lookup to check for an incomplete expire.
  507. *
  508. * If a process is blocked on the dentry waiting for the expire to finish,
  509. * it will invalidate the dentry and try to mount with a new one.
  510. *
  511. * Also see autofs4_dir_rmdir()..
  512. */
  513. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  514. {
  515. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  516. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  517. struct autofs_info *p_ino;
  518. /* This allows root to remove symlinks */
  519. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  520. return -EACCES;
  521. if (atomic_dec_and_test(&ino->count)) {
  522. p_ino = autofs4_dentry_ino(dentry->d_parent);
  523. if (p_ino && dentry->d_parent != dentry)
  524. atomic_dec(&p_ino->count);
  525. }
  526. dput(ino->dentry);
  527. dentry->d_inode->i_size = 0;
  528. clear_nlink(dentry->d_inode);
  529. dir->i_mtime = CURRENT_TIME;
  530. spin_lock(&autofs4_lock);
  531. autofs4_add_expiring(dentry);
  532. spin_lock(&dentry->d_lock);
  533. __d_drop(dentry);
  534. spin_unlock(&dentry->d_lock);
  535. spin_unlock(&autofs4_lock);
  536. return 0;
  537. }
  538. /*
  539. * Version 4 of autofs provides a pseudo direct mount implementation
  540. * that relies on directories at the leaves of a directory tree under
  541. * an indirect mount to trigger mounts. To allow for this we need to
  542. * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
  543. * of the directory tree. There is no need to clear the automount flag
  544. * following a mount or restore it after an expire because these mounts
  545. * are always covered. However, it is neccessary to ensure that these
  546. * flags are clear on non-empty directories to avoid unnecessary calls
  547. * during path walks.
  548. */
  549. static void autofs_set_leaf_automount_flags(struct dentry *dentry)
  550. {
  551. struct dentry *parent;
  552. /* root and dentrys in the root are already handled */
  553. if (IS_ROOT(dentry->d_parent))
  554. return;
  555. managed_dentry_set_managed(dentry);
  556. parent = dentry->d_parent;
  557. /* only consider parents below dentrys in the root */
  558. if (IS_ROOT(parent->d_parent))
  559. return;
  560. managed_dentry_clear_managed(parent);
  561. return;
  562. }
  563. static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
  564. {
  565. struct list_head *d_child;
  566. struct dentry *parent;
  567. /* flags for dentrys in the root are handled elsewhere */
  568. if (IS_ROOT(dentry->d_parent))
  569. return;
  570. managed_dentry_clear_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. d_child = &dentry->d_u.d_child;
  576. /* Set parent managed if it's becoming empty */
  577. if (d_child->next == &parent->d_subdirs &&
  578. d_child->prev == &parent->d_subdirs)
  579. managed_dentry_set_managed(parent);
  580. return;
  581. }
  582. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  583. {
  584. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  585. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  586. struct autofs_info *p_ino;
  587. DPRINTK("dentry %p, removing %.*s",
  588. dentry, dentry->d_name.len, dentry->d_name.name);
  589. if (!autofs4_oz_mode(sbi))
  590. return -EACCES;
  591. spin_lock(&autofs4_lock);
  592. spin_lock(&sbi->lookup_lock);
  593. spin_lock(&dentry->d_lock);
  594. if (!list_empty(&dentry->d_subdirs)) {
  595. spin_unlock(&dentry->d_lock);
  596. spin_unlock(&sbi->lookup_lock);
  597. spin_unlock(&autofs4_lock);
  598. return -ENOTEMPTY;
  599. }
  600. __autofs4_add_expiring(dentry);
  601. spin_unlock(&sbi->lookup_lock);
  602. __d_drop(dentry);
  603. spin_unlock(&dentry->d_lock);
  604. spin_unlock(&autofs4_lock);
  605. if (sbi->version < 5)
  606. autofs_clear_leaf_automount_flags(dentry);
  607. if (atomic_dec_and_test(&ino->count)) {
  608. p_ino = autofs4_dentry_ino(dentry->d_parent);
  609. if (p_ino && dentry->d_parent != dentry)
  610. atomic_dec(&p_ino->count);
  611. }
  612. dput(ino->dentry);
  613. dentry->d_inode->i_size = 0;
  614. clear_nlink(dentry->d_inode);
  615. if (dir->i_nlink)
  616. drop_nlink(dir);
  617. return 0;
  618. }
  619. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  620. {
  621. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  622. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  623. struct autofs_info *p_ino;
  624. struct inode *inode;
  625. if (!autofs4_oz_mode(sbi))
  626. return -EACCES;
  627. DPRINTK("dentry %p, creating %.*s",
  628. dentry, dentry->d_name.len, dentry->d_name.name);
  629. ino = autofs4_init_ino(ino, sbi);
  630. if (!ino)
  631. return -ENOMEM;
  632. autofs4_del_active(dentry);
  633. inode = autofs4_get_inode(dir->i_sb, ino, S_IFDIR | 0555);
  634. if (!inode) {
  635. if (!dentry->d_fsdata)
  636. kfree(ino);
  637. return -ENOMEM;
  638. }
  639. d_add(dentry, inode);
  640. if (sbi->version < 5)
  641. autofs_set_leaf_automount_flags(dentry);
  642. dentry->d_fsdata = ino;
  643. ino->dentry = dget(dentry);
  644. atomic_inc(&ino->count);
  645. p_ino = autofs4_dentry_ino(dentry->d_parent);
  646. if (p_ino && dentry->d_parent != dentry)
  647. atomic_inc(&p_ino->count);
  648. inc_nlink(dir);
  649. dir->i_mtime = CURRENT_TIME;
  650. return 0;
  651. }
  652. /* Get/set timeout ioctl() operation */
  653. #ifdef CONFIG_COMPAT
  654. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  655. compat_ulong_t __user *p)
  656. {
  657. int rv;
  658. unsigned long ntimeout;
  659. if ((rv = get_user(ntimeout, p)) ||
  660. (rv = put_user(sbi->exp_timeout/HZ, p)))
  661. return rv;
  662. if (ntimeout > UINT_MAX/HZ)
  663. sbi->exp_timeout = 0;
  664. else
  665. sbi->exp_timeout = ntimeout * HZ;
  666. return 0;
  667. }
  668. #endif
  669. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  670. unsigned long __user *p)
  671. {
  672. int rv;
  673. unsigned long ntimeout;
  674. if ((rv = get_user(ntimeout, p)) ||
  675. (rv = put_user(sbi->exp_timeout/HZ, p)))
  676. return rv;
  677. if (ntimeout > ULONG_MAX/HZ)
  678. sbi->exp_timeout = 0;
  679. else
  680. sbi->exp_timeout = ntimeout * HZ;
  681. return 0;
  682. }
  683. /* Return protocol version */
  684. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  685. {
  686. return put_user(sbi->version, p);
  687. }
  688. /* Return protocol sub version */
  689. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  690. {
  691. return put_user(sbi->sub_version, p);
  692. }
  693. /*
  694. * Tells the daemon whether it can umount the autofs mount.
  695. */
  696. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  697. {
  698. int status = 0;
  699. if (may_umount(mnt))
  700. status = 1;
  701. DPRINTK("returning %d", status);
  702. status = put_user(status, p);
  703. return status;
  704. }
  705. /* Identify autofs4_dentries - this is so we can tell if there's
  706. an extra dentry refcount or not. We only hold a refcount on the
  707. dentry if its non-negative (ie, d_inode != NULL)
  708. */
  709. int is_autofs4_dentry(struct dentry *dentry)
  710. {
  711. return dentry && dentry->d_inode &&
  712. dentry->d_op == &autofs4_dentry_operations &&
  713. dentry->d_fsdata != NULL;
  714. }
  715. /*
  716. * ioctl()'s on the root directory is the chief method for the daemon to
  717. * generate kernel reactions
  718. */
  719. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  720. unsigned int cmd, unsigned long arg)
  721. {
  722. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  723. void __user *p = (void __user *)arg;
  724. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  725. cmd,arg,sbi,task_pgrp_nr(current));
  726. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  727. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  728. return -ENOTTY;
  729. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  730. return -EPERM;
  731. switch(cmd) {
  732. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  733. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  734. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  735. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  736. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  737. autofs4_catatonic_mode(sbi);
  738. return 0;
  739. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  740. return autofs4_get_protover(sbi, p);
  741. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  742. return autofs4_get_protosubver(sbi, p);
  743. case AUTOFS_IOC_SETTIMEOUT:
  744. return autofs4_get_set_timeout(sbi, p);
  745. #ifdef CONFIG_COMPAT
  746. case AUTOFS_IOC_SETTIMEOUT32:
  747. return autofs4_compat_get_set_timeout(sbi, p);
  748. #endif
  749. case AUTOFS_IOC_ASKUMOUNT:
  750. return autofs4_ask_umount(filp->f_path.mnt, p);
  751. /* return a single thing to expire */
  752. case AUTOFS_IOC_EXPIRE:
  753. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  754. /* same as above, but can send multiple expires through pipe */
  755. case AUTOFS_IOC_EXPIRE_MULTI:
  756. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  757. default:
  758. return -ENOSYS;
  759. }
  760. }
  761. static long autofs4_root_ioctl(struct file *filp,
  762. unsigned int cmd, unsigned long arg)
  763. {
  764. struct inode *inode = filp->f_dentry->d_inode;
  765. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  766. }
  767. #ifdef CONFIG_COMPAT
  768. static long autofs4_root_compat_ioctl(struct file *filp,
  769. unsigned int cmd, unsigned long arg)
  770. {
  771. struct inode *inode = filp->f_path.dentry->d_inode;
  772. int ret;
  773. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  774. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  775. else
  776. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  777. (unsigned long)compat_ptr(arg));
  778. return ret;
  779. }
  780. #endif