root.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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);
  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. /* For dentries that don't initiate mounting */
  62. const struct dentry_operations autofs4_dentry_operations = {
  63. .d_release = autofs4_dentry_release,
  64. };
  65. /* For dentries that do initiate mounting */
  66. const struct dentry_operations autofs4_mount_dentry_operations = {
  67. .d_automount = autofs4_d_automount,
  68. .d_manage = autofs4_d_manage,
  69. .d_release = autofs4_dentry_release,
  70. };
  71. static void autofs4_add_active(struct dentry *dentry)
  72. {
  73. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  74. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  75. if (ino) {
  76. spin_lock(&sbi->lookup_lock);
  77. if (!ino->active_count) {
  78. if (list_empty(&ino->active))
  79. list_add(&ino->active, &sbi->active_list);
  80. }
  81. ino->active_count++;
  82. spin_unlock(&sbi->lookup_lock);
  83. }
  84. return;
  85. }
  86. static void autofs4_del_active(struct dentry *dentry)
  87. {
  88. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  89. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  90. if (ino) {
  91. spin_lock(&sbi->lookup_lock);
  92. ino->active_count--;
  93. if (!ino->active_count) {
  94. if (!list_empty(&ino->active))
  95. list_del_init(&ino->active);
  96. }
  97. spin_unlock(&sbi->lookup_lock);
  98. }
  99. return;
  100. }
  101. static int autofs4_dir_open(struct inode *inode, struct file *file)
  102. {
  103. struct dentry *dentry = file->f_path.dentry;
  104. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  105. DPRINTK("file=%p dentry=%p %.*s",
  106. file, dentry, dentry->d_name.len, dentry->d_name.name);
  107. if (autofs4_oz_mode(sbi))
  108. goto out;
  109. /*
  110. * An empty directory in an autofs file system is always a
  111. * mount point. The daemon must have failed to mount this
  112. * during lookup so it doesn't exist. This can happen, for
  113. * example, if user space returns an incorrect status for a
  114. * mount request. Otherwise we're doing a readdir on the
  115. * autofs file system so just let the libfs routines handle
  116. * it.
  117. */
  118. spin_lock(&autofs4_lock);
  119. spin_lock(&dentry->d_lock);
  120. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  121. spin_unlock(&dentry->d_lock);
  122. spin_unlock(&autofs4_lock);
  123. return -ENOENT;
  124. }
  125. spin_unlock(&dentry->d_lock);
  126. spin_unlock(&autofs4_lock);
  127. out:
  128. return dcache_dir_open(inode, file);
  129. }
  130. void autofs4_dentry_release(struct dentry *de)
  131. {
  132. struct autofs_info *inf;
  133. DPRINTK("releasing %p", de);
  134. inf = autofs4_dentry_ino(de);
  135. if (inf) {
  136. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  137. if (sbi) {
  138. spin_lock(&sbi->lookup_lock);
  139. if (!list_empty(&inf->active))
  140. list_del(&inf->active);
  141. if (!list_empty(&inf->expiring))
  142. list_del(&inf->expiring);
  143. spin_unlock(&sbi->lookup_lock);
  144. }
  145. autofs4_free_ino(inf);
  146. }
  147. }
  148. static struct dentry *autofs4_lookup_active(struct dentry *dentry)
  149. {
  150. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  151. struct dentry *parent = dentry->d_parent;
  152. struct qstr *name = &dentry->d_name;
  153. unsigned int len = name->len;
  154. unsigned int hash = name->hash;
  155. const unsigned char *str = name->name;
  156. struct list_head *p, *head;
  157. spin_lock(&autofs4_lock);
  158. spin_lock(&sbi->lookup_lock);
  159. head = &sbi->active_list;
  160. list_for_each(p, head) {
  161. struct autofs_info *ino;
  162. struct dentry *active;
  163. struct qstr *qstr;
  164. ino = list_entry(p, struct autofs_info, active);
  165. active = ino->dentry;
  166. spin_lock(&active->d_lock);
  167. /* Already gone? */
  168. if (active->d_count == 0)
  169. goto next;
  170. qstr = &active->d_name;
  171. if (active->d_name.hash != hash)
  172. goto next;
  173. if (active->d_parent != parent)
  174. goto next;
  175. if (qstr->len != len)
  176. goto next;
  177. if (memcmp(qstr->name, str, len))
  178. goto next;
  179. if (d_unhashed(active)) {
  180. dget_dlock(active);
  181. spin_unlock(&active->d_lock);
  182. spin_unlock(&sbi->lookup_lock);
  183. spin_unlock(&autofs4_lock);
  184. return active;
  185. }
  186. next:
  187. spin_unlock(&active->d_lock);
  188. }
  189. spin_unlock(&sbi->lookup_lock);
  190. spin_unlock(&autofs4_lock);
  191. return NULL;
  192. }
  193. static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
  194. {
  195. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  196. struct dentry *parent = dentry->d_parent;
  197. struct qstr *name = &dentry->d_name;
  198. unsigned int len = name->len;
  199. unsigned int hash = name->hash;
  200. const unsigned char *str = name->name;
  201. struct list_head *p, *head;
  202. spin_lock(&autofs4_lock);
  203. spin_lock(&sbi->lookup_lock);
  204. head = &sbi->expiring_list;
  205. list_for_each(p, head) {
  206. struct autofs_info *ino;
  207. struct dentry *expiring;
  208. struct qstr *qstr;
  209. ino = list_entry(p, struct autofs_info, expiring);
  210. expiring = ino->dentry;
  211. spin_lock(&expiring->d_lock);
  212. /* Bad luck, we've already been dentry_iput */
  213. if (!expiring->d_inode)
  214. goto next;
  215. qstr = &expiring->d_name;
  216. if (expiring->d_name.hash != hash)
  217. goto next;
  218. if (expiring->d_parent != parent)
  219. goto next;
  220. if (qstr->len != len)
  221. goto next;
  222. if (memcmp(qstr->name, str, len))
  223. goto next;
  224. if (d_unhashed(expiring)) {
  225. dget_dlock(expiring);
  226. spin_unlock(&expiring->d_lock);
  227. spin_unlock(&sbi->lookup_lock);
  228. spin_unlock(&autofs4_lock);
  229. return expiring;
  230. }
  231. next:
  232. spin_unlock(&expiring->d_lock);
  233. }
  234. spin_unlock(&sbi->lookup_lock);
  235. spin_unlock(&autofs4_lock);
  236. return NULL;
  237. }
  238. static int autofs4_mount_wait(struct dentry *dentry)
  239. {
  240. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  241. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  242. int status;
  243. if (ino->flags & AUTOFS_INF_PENDING) {
  244. DPRINTK("waiting for mount name=%.*s",
  245. dentry->d_name.len, dentry->d_name.name);
  246. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  247. DPRINTK("mount wait done status=%d", status);
  248. ino->last_used = jiffies;
  249. return status;
  250. }
  251. return 0;
  252. }
  253. static int do_expire_wait(struct dentry *dentry)
  254. {
  255. struct dentry *expiring;
  256. expiring = autofs4_lookup_expiring(dentry);
  257. if (!expiring)
  258. return autofs4_expire_wait(dentry);
  259. else {
  260. /*
  261. * If we are racing with expire the request might not
  262. * be quite complete, but the directory has been removed
  263. * so it must have been successful, just wait for it.
  264. */
  265. autofs4_expire_wait(expiring);
  266. autofs4_del_expiring(expiring);
  267. dput(expiring);
  268. }
  269. return 0;
  270. }
  271. static struct dentry *autofs4_mountpoint_changed(struct path *path)
  272. {
  273. struct dentry *dentry = path->dentry;
  274. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  275. /*
  276. * If this is an indirect mount the dentry could have gone away
  277. * as a result of an expire and a new one created.
  278. */
  279. if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
  280. struct dentry *parent = dentry->d_parent;
  281. struct dentry *new = d_lookup(parent, &dentry->d_name);
  282. if (!new)
  283. return NULL;
  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. /*
  298. * Someone may have manually umounted this or it was a submount
  299. * that has gone away.
  300. */
  301. spin_lock(&dentry->d_lock);
  302. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  303. if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  304. (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  305. __managed_dentry_set_transit(path->dentry);
  306. }
  307. spin_unlock(&dentry->d_lock);
  308. /* The daemon never triggers a mount. */
  309. if (autofs4_oz_mode(sbi))
  310. return NULL;
  311. /*
  312. * If an expire request is pending everyone must wait.
  313. * If the expire fails we're still mounted so continue
  314. * the follow and return. A return of -EAGAIN (which only
  315. * happens with indirect mounts) means the expire completed
  316. * and the directory was removed, so just go ahead and try
  317. * the mount.
  318. */
  319. status = do_expire_wait(dentry);
  320. if (status && status != -EAGAIN)
  321. return NULL;
  322. /* Callback to the daemon to perform the mount or wait */
  323. spin_lock(&sbi->fs_lock);
  324. if (ino->flags & AUTOFS_INF_PENDING) {
  325. spin_unlock(&sbi->fs_lock);
  326. status = autofs4_mount_wait(dentry);
  327. if (status)
  328. return ERR_PTR(status);
  329. spin_lock(&sbi->fs_lock);
  330. goto done;
  331. }
  332. /*
  333. * If the dentry is a symlink it's equivalent to a directory
  334. * having d_mountpoint() true, so there's no need to call back
  335. * to the daemon.
  336. */
  337. if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
  338. goto done;
  339. if (!d_mountpoint(dentry)) {
  340. /*
  341. * It's possible that user space hasn't removed directories
  342. * after umounting a rootless multi-mount, although it
  343. * should. For v5 have_submounts() is sufficient to handle
  344. * this because the leaves of the directory tree under the
  345. * mount never trigger mounts themselves (they have an autofs
  346. * trigger mount mounted on them). But v4 pseudo direct mounts
  347. * do need the leaves to to trigger mounts. In this case we
  348. * have no choice but to use the list_empty() check and
  349. * require user space behave.
  350. */
  351. if (sbi->version > 4) {
  352. if (have_submounts(dentry))
  353. goto done;
  354. } else {
  355. spin_lock(&dentry->d_lock);
  356. if (!list_empty(&dentry->d_subdirs)) {
  357. spin_unlock(&dentry->d_lock);
  358. goto done;
  359. }
  360. spin_unlock(&dentry->d_lock);
  361. }
  362. ino->flags |= AUTOFS_INF_PENDING;
  363. spin_unlock(&sbi->fs_lock);
  364. status = autofs4_mount_wait(dentry);
  365. if (status)
  366. return ERR_PTR(status);
  367. spin_lock(&sbi->fs_lock);
  368. ino->flags &= ~AUTOFS_INF_PENDING;
  369. }
  370. done:
  371. if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
  372. /*
  373. * Any needed mounting has been completed and the path updated
  374. * so turn this into a normal dentry so we don't continually
  375. * call ->d_automount() and ->d_manage().
  376. */
  377. spin_lock(&dentry->d_lock);
  378. __managed_dentry_clear_transit(dentry);
  379. /*
  380. * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
  381. * symlinks as in all other cases the dentry will be covered by
  382. * an actual mount so ->d_automount() won't be called during
  383. * the follow.
  384. */
  385. if ((!d_mountpoint(dentry) &&
  386. !list_empty(&dentry->d_subdirs)) ||
  387. (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
  388. __managed_dentry_clear_automount(dentry);
  389. spin_unlock(&dentry->d_lock);
  390. }
  391. spin_unlock(&sbi->fs_lock);
  392. /* Mount succeeded, check if we ended up with a new dentry */
  393. dentry = autofs4_mountpoint_changed(path);
  394. if (!dentry)
  395. return ERR_PTR(-ENOENT);
  396. return NULL;
  397. }
  398. int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
  399. {
  400. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  401. DPRINTK("dentry=%p %.*s",
  402. dentry, dentry->d_name.len, dentry->d_name.name);
  403. /* The daemon never waits. */
  404. if (autofs4_oz_mode(sbi) || mounting_here) {
  405. if (!d_mountpoint(dentry))
  406. return -EISDIR;
  407. return 0;
  408. }
  409. /* Wait for pending expires */
  410. do_expire_wait(dentry);
  411. /*
  412. * This dentry may be under construction so wait on mount
  413. * completion.
  414. */
  415. return autofs4_mount_wait(dentry);
  416. }
  417. /* Lookups in the root directory */
  418. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  419. {
  420. struct autofs_sb_info *sbi;
  421. struct autofs_info *ino;
  422. struct dentry *active;
  423. DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
  424. /* File name too long to exist */
  425. if (dentry->d_name.len > NAME_MAX)
  426. return ERR_PTR(-ENAMETOOLONG);
  427. sbi = autofs4_sbi(dir->i_sb);
  428. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  429. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  430. active = autofs4_lookup_active(dentry);
  431. if (active) {
  432. return active;
  433. } else {
  434. d_set_d_op(dentry, &autofs4_dentry_operations);
  435. /*
  436. * A dentry that is not within the root can never trigger a
  437. * mount operation, unless the directory already exists, so we
  438. * can return fail immediately. The daemon however does need
  439. * to create directories within the file system.
  440. */
  441. if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
  442. return ERR_PTR(-ENOENT);
  443. /* Mark entries in the root as mount triggers */
  444. if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
  445. d_set_d_op(dentry, &autofs4_mount_dentry_operations);
  446. __managed_dentry_set_managed(dentry);
  447. }
  448. ino = autofs4_init_ino(NULL, sbi, 0555);
  449. if (!ino)
  450. return ERR_PTR(-ENOMEM);
  451. dentry->d_fsdata = ino;
  452. ino->dentry = dentry;
  453. autofs4_add_active(dentry);
  454. d_instantiate(dentry, NULL);
  455. }
  456. return NULL;
  457. }
  458. static int autofs4_dir_symlink(struct inode *dir,
  459. struct dentry *dentry,
  460. const char *symname)
  461. {
  462. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  463. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  464. struct autofs_info *p_ino;
  465. struct inode *inode;
  466. char *cp;
  467. DPRINTK("%s <- %.*s", symname,
  468. dentry->d_name.len, dentry->d_name.name);
  469. if (!autofs4_oz_mode(sbi))
  470. return -EACCES;
  471. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  472. if (!ino)
  473. return -ENOMEM;
  474. autofs4_del_active(dentry);
  475. ino->size = strlen(symname);
  476. cp = kmalloc(ino->size + 1, GFP_KERNEL);
  477. if (!cp) {
  478. if (!dentry->d_fsdata)
  479. kfree(ino);
  480. return -ENOMEM;
  481. }
  482. strcpy(cp, symname);
  483. inode = autofs4_get_inode(dir->i_sb, ino);
  484. if (!inode) {
  485. kfree(cp);
  486. if (!dentry->d_fsdata)
  487. kfree(ino);
  488. return -ENOMEM;
  489. }
  490. d_add(dentry, inode);
  491. d_set_d_op(dentry, &autofs4_dentry_operations);
  492. dentry->d_fsdata = ino;
  493. ino->dentry = dget(dentry);
  494. atomic_inc(&ino->count);
  495. p_ino = autofs4_dentry_ino(dentry->d_parent);
  496. if (p_ino && dentry->d_parent != dentry)
  497. atomic_inc(&p_ino->count);
  498. ino->u.symlink = cp;
  499. dir->i_mtime = CURRENT_TIME;
  500. return 0;
  501. }
  502. /*
  503. * NOTE!
  504. *
  505. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  506. * that the file no longer exists. However, doing that means that the
  507. * VFS layer can turn the dentry into a negative dentry. We don't want
  508. * this, because the unlink is probably the result of an expire.
  509. * We simply d_drop it and add it to a expiring list in the super block,
  510. * which allows the dentry lookup to check for an incomplete expire.
  511. *
  512. * If a process is blocked on the dentry waiting for the expire to finish,
  513. * it will invalidate the dentry and try to mount with a new one.
  514. *
  515. * Also see autofs4_dir_rmdir()..
  516. */
  517. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  518. {
  519. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  520. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  521. struct autofs_info *p_ino;
  522. /* This allows root to remove symlinks */
  523. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  524. return -EACCES;
  525. if (atomic_dec_and_test(&ino->count)) {
  526. p_ino = autofs4_dentry_ino(dentry->d_parent);
  527. if (p_ino && dentry->d_parent != dentry)
  528. atomic_dec(&p_ino->count);
  529. }
  530. dput(ino->dentry);
  531. dentry->d_inode->i_size = 0;
  532. clear_nlink(dentry->d_inode);
  533. dir->i_mtime = CURRENT_TIME;
  534. spin_lock(&autofs4_lock);
  535. autofs4_add_expiring(dentry);
  536. spin_lock(&dentry->d_lock);
  537. __d_drop(dentry);
  538. spin_unlock(&dentry->d_lock);
  539. spin_unlock(&autofs4_lock);
  540. return 0;
  541. }
  542. /*
  543. * Version 4 of autofs provides a pseudo direct mount implementation
  544. * that relies on directories at the leaves of a directory tree under
  545. * an indirect mount to trigger mounts. To allow for this we need to
  546. * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
  547. * of the directory tree. There is no need to clear the automount flag
  548. * following a mount or restore it after an expire because these mounts
  549. * are always covered. However, it is neccessary to ensure that these
  550. * flags are clear on non-empty directories to avoid unnecessary calls
  551. * during path walks.
  552. */
  553. static void autofs_set_leaf_automount_flags(struct dentry *dentry)
  554. {
  555. struct dentry *parent;
  556. /* root and dentrys in the root are already handled */
  557. if (IS_ROOT(dentry->d_parent))
  558. return;
  559. managed_dentry_set_managed(dentry);
  560. parent = dentry->d_parent;
  561. /* only consider parents below dentrys in the root */
  562. if (IS_ROOT(parent->d_parent))
  563. return;
  564. managed_dentry_clear_managed(parent);
  565. return;
  566. }
  567. static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
  568. {
  569. struct list_head *d_child;
  570. struct dentry *parent;
  571. /* flags for dentrys in the root are handled elsewhere */
  572. if (IS_ROOT(dentry->d_parent))
  573. return;
  574. managed_dentry_clear_managed(dentry);
  575. parent = dentry->d_parent;
  576. /* only consider parents below dentrys in the root */
  577. if (IS_ROOT(parent->d_parent))
  578. return;
  579. d_child = &dentry->d_u.d_child;
  580. /* Set parent managed if it's becoming empty */
  581. if (d_child->next == &parent->d_subdirs &&
  582. d_child->prev == &parent->d_subdirs)
  583. managed_dentry_set_managed(parent);
  584. return;
  585. }
  586. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  587. {
  588. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  589. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  590. struct autofs_info *p_ino;
  591. DPRINTK("dentry %p, removing %.*s",
  592. dentry, dentry->d_name.len, dentry->d_name.name);
  593. if (!autofs4_oz_mode(sbi))
  594. return -EACCES;
  595. spin_lock(&autofs4_lock);
  596. spin_lock(&sbi->lookup_lock);
  597. spin_lock(&dentry->d_lock);
  598. if (!list_empty(&dentry->d_subdirs)) {
  599. spin_unlock(&dentry->d_lock);
  600. spin_unlock(&sbi->lookup_lock);
  601. spin_unlock(&autofs4_lock);
  602. return -ENOTEMPTY;
  603. }
  604. __autofs4_add_expiring(dentry);
  605. spin_unlock(&sbi->lookup_lock);
  606. __d_drop(dentry);
  607. spin_unlock(&dentry->d_lock);
  608. spin_unlock(&autofs4_lock);
  609. if (sbi->version < 5)
  610. autofs_clear_leaf_automount_flags(dentry);
  611. if (atomic_dec_and_test(&ino->count)) {
  612. p_ino = autofs4_dentry_ino(dentry->d_parent);
  613. if (p_ino && dentry->d_parent != dentry)
  614. atomic_dec(&p_ino->count);
  615. }
  616. dput(ino->dentry);
  617. dentry->d_inode->i_size = 0;
  618. clear_nlink(dentry->d_inode);
  619. if (dir->i_nlink)
  620. drop_nlink(dir);
  621. return 0;
  622. }
  623. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  624. {
  625. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  626. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  627. struct autofs_info *p_ino;
  628. struct inode *inode;
  629. if (!autofs4_oz_mode(sbi))
  630. return -EACCES;
  631. DPRINTK("dentry %p, creating %.*s",
  632. dentry, dentry->d_name.len, dentry->d_name.name);
  633. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  634. if (!ino)
  635. return -ENOMEM;
  636. autofs4_del_active(dentry);
  637. inode = autofs4_get_inode(dir->i_sb, ino);
  638. if (!inode) {
  639. if (!dentry->d_fsdata)
  640. kfree(ino);
  641. return -ENOMEM;
  642. }
  643. d_add(dentry, inode);
  644. if (sbi->version < 5)
  645. autofs_set_leaf_automount_flags(dentry);
  646. dentry->d_fsdata = ino;
  647. ino->dentry = dget(dentry);
  648. atomic_inc(&ino->count);
  649. p_ino = autofs4_dentry_ino(dentry->d_parent);
  650. if (p_ino && dentry->d_parent != 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_mount_dentry_operations ||
  717. dentry->d_op == &autofs4_dentry_operations) &&
  718. dentry->d_fsdata != NULL;
  719. }
  720. /*
  721. * ioctl()'s on the root directory is the chief method for the daemon to
  722. * generate kernel reactions
  723. */
  724. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  725. unsigned int cmd, unsigned long arg)
  726. {
  727. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  728. void __user *p = (void __user *)arg;
  729. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  730. cmd,arg,sbi,task_pgrp_nr(current));
  731. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  732. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  733. return -ENOTTY;
  734. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  735. return -EPERM;
  736. switch(cmd) {
  737. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  738. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  739. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  740. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  741. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  742. autofs4_catatonic_mode(sbi);
  743. return 0;
  744. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  745. return autofs4_get_protover(sbi, p);
  746. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  747. return autofs4_get_protosubver(sbi, p);
  748. case AUTOFS_IOC_SETTIMEOUT:
  749. return autofs4_get_set_timeout(sbi, p);
  750. #ifdef CONFIG_COMPAT
  751. case AUTOFS_IOC_SETTIMEOUT32:
  752. return autofs4_compat_get_set_timeout(sbi, p);
  753. #endif
  754. case AUTOFS_IOC_ASKUMOUNT:
  755. return autofs4_ask_umount(filp->f_path.mnt, p);
  756. /* return a single thing to expire */
  757. case AUTOFS_IOC_EXPIRE:
  758. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  759. /* same as above, but can send multiple expires through pipe */
  760. case AUTOFS_IOC_EXPIRE_MULTI:
  761. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  762. default:
  763. return -ENOSYS;
  764. }
  765. }
  766. static long autofs4_root_ioctl(struct file *filp,
  767. unsigned int cmd, unsigned long arg)
  768. {
  769. struct inode *inode = filp->f_dentry->d_inode;
  770. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  771. }
  772. #ifdef CONFIG_COMPAT
  773. static long autofs4_root_compat_ioctl(struct file *filp,
  774. unsigned int cmd, unsigned long arg)
  775. {
  776. struct inode *inode = filp->f_path.dentry->d_inode;
  777. int ret;
  778. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  779. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  780. else
  781. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  782. (unsigned long)compat_ptr(arg));
  783. return ret;
  784. }
  785. #endif