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