root.c 23 KB

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