root.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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/param.h>
  18. #include <linux/time.h>
  19. #include "autofs_i.h"
  20. static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
  21. static int autofs4_dir_unlink(struct inode *,struct dentry *);
  22. static int autofs4_dir_rmdir(struct inode *,struct dentry *);
  23. static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
  24. static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
  25. static int autofs4_dir_open(struct inode *inode, struct file *file);
  26. static int autofs4_dir_close(struct inode *inode, struct file *file);
  27. static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
  28. static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
  29. static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
  30. static void *autofs4_follow_link(struct dentry *, struct nameidata *);
  31. const struct file_operations autofs4_root_operations = {
  32. .open = dcache_dir_open,
  33. .release = dcache_dir_close,
  34. .read = generic_read_dir,
  35. .readdir = autofs4_root_readdir,
  36. .ioctl = autofs4_root_ioctl,
  37. };
  38. const struct file_operations autofs4_dir_operations = {
  39. .open = autofs4_dir_open,
  40. .release = autofs4_dir_close,
  41. .read = generic_read_dir,
  42. .readdir = autofs4_dir_readdir,
  43. };
  44. const struct inode_operations autofs4_indirect_root_inode_operations = {
  45. .lookup = autofs4_lookup,
  46. .unlink = autofs4_dir_unlink,
  47. .symlink = autofs4_dir_symlink,
  48. .mkdir = autofs4_dir_mkdir,
  49. .rmdir = autofs4_dir_rmdir,
  50. };
  51. const struct inode_operations autofs4_direct_root_inode_operations = {
  52. .lookup = autofs4_lookup,
  53. .unlink = autofs4_dir_unlink,
  54. .mkdir = autofs4_dir_mkdir,
  55. .rmdir = autofs4_dir_rmdir,
  56. .follow_link = autofs4_follow_link,
  57. };
  58. const struct inode_operations autofs4_dir_inode_operations = {
  59. .lookup = autofs4_lookup,
  60. .unlink = autofs4_dir_unlink,
  61. .symlink = autofs4_dir_symlink,
  62. .mkdir = autofs4_dir_mkdir,
  63. .rmdir = autofs4_dir_rmdir,
  64. };
  65. static int autofs4_root_readdir(struct file *file, void *dirent,
  66. filldir_t filldir)
  67. {
  68. struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
  69. int oz_mode = autofs4_oz_mode(sbi);
  70. DPRINTK("called, filp->f_pos = %lld", file->f_pos);
  71. /*
  72. * Don't set reghost flag if:
  73. * 1) f_pos is larger than zero -- we've already been here.
  74. * 2) we haven't even enabled reghosting in the 1st place.
  75. * 3) this is the daemon doing a readdir
  76. */
  77. if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
  78. sbi->needs_reghost = 1;
  79. DPRINTK("needs_reghost = %d", sbi->needs_reghost);
  80. return dcache_readdir(file, dirent, filldir);
  81. }
  82. static int autofs4_dir_open(struct inode *inode, struct file *file)
  83. {
  84. struct dentry *dentry = file->f_path.dentry;
  85. struct vfsmount *mnt = file->f_path.mnt;
  86. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  87. struct dentry *cursor;
  88. int status;
  89. status = dcache_dir_open(inode, file);
  90. if (status)
  91. goto out;
  92. cursor = file->private_data;
  93. cursor->d_fsdata = NULL;
  94. DPRINTK("file=%p dentry=%p %.*s",
  95. file, dentry, dentry->d_name.len, dentry->d_name.name);
  96. if (autofs4_oz_mode(sbi))
  97. goto out;
  98. if (autofs4_ispending(dentry)) {
  99. DPRINTK("dentry busy");
  100. dcache_dir_close(inode, file);
  101. status = -EBUSY;
  102. goto out;
  103. }
  104. status = -ENOENT;
  105. if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
  106. struct nameidata nd;
  107. int empty, ret;
  108. /* In case there are stale directory dentrys from a failed mount */
  109. spin_lock(&dcache_lock);
  110. empty = list_empty(&dentry->d_subdirs);
  111. spin_unlock(&dcache_lock);
  112. if (!empty)
  113. d_invalidate(dentry);
  114. nd.flags = LOOKUP_DIRECTORY;
  115. ret = (dentry->d_op->d_revalidate)(dentry, &nd);
  116. if (ret <= 0) {
  117. if (ret < 0)
  118. status = ret;
  119. dcache_dir_close(inode, file);
  120. goto out;
  121. }
  122. }
  123. if (d_mountpoint(dentry)) {
  124. struct file *fp = NULL;
  125. struct vfsmount *fp_mnt = mntget(mnt);
  126. struct dentry *fp_dentry = dget(dentry);
  127. if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
  128. dput(fp_dentry);
  129. mntput(fp_mnt);
  130. dcache_dir_close(inode, file);
  131. goto out;
  132. }
  133. fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
  134. status = PTR_ERR(fp);
  135. if (IS_ERR(fp)) {
  136. dcache_dir_close(inode, file);
  137. goto out;
  138. }
  139. cursor->d_fsdata = fp;
  140. }
  141. return 0;
  142. out:
  143. return status;
  144. }
  145. static int autofs4_dir_close(struct inode *inode, struct file *file)
  146. {
  147. struct dentry *dentry = file->f_path.dentry;
  148. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  149. struct dentry *cursor = file->private_data;
  150. int status = 0;
  151. DPRINTK("file=%p dentry=%p %.*s",
  152. file, dentry, dentry->d_name.len, dentry->d_name.name);
  153. if (autofs4_oz_mode(sbi))
  154. goto out;
  155. if (autofs4_ispending(dentry)) {
  156. DPRINTK("dentry busy");
  157. status = -EBUSY;
  158. goto out;
  159. }
  160. if (d_mountpoint(dentry)) {
  161. struct file *fp = cursor->d_fsdata;
  162. if (!fp) {
  163. status = -ENOENT;
  164. goto out;
  165. }
  166. filp_close(fp, current->files);
  167. }
  168. out:
  169. dcache_dir_close(inode, file);
  170. return status;
  171. }
  172. static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
  173. {
  174. struct dentry *dentry = file->f_path.dentry;
  175. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  176. struct dentry *cursor = file->private_data;
  177. int status;
  178. DPRINTK("file=%p dentry=%p %.*s",
  179. file, dentry, dentry->d_name.len, dentry->d_name.name);
  180. if (autofs4_oz_mode(sbi))
  181. goto out;
  182. if (autofs4_ispending(dentry)) {
  183. DPRINTK("dentry busy");
  184. return -EBUSY;
  185. }
  186. if (d_mountpoint(dentry)) {
  187. struct file *fp = cursor->d_fsdata;
  188. if (!fp)
  189. return -ENOENT;
  190. if (!fp->f_op || !fp->f_op->readdir)
  191. goto out;
  192. status = vfs_readdir(fp, filldir, dirent);
  193. file->f_pos = fp->f_pos;
  194. if (status)
  195. autofs4_copy_atime(file, fp);
  196. return status;
  197. }
  198. out:
  199. return dcache_readdir(file, dirent, filldir);
  200. }
  201. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  202. {
  203. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  204. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  205. int status = 0;
  206. /* Block on any pending expiry here; invalidate the dentry
  207. when expiration is done to trigger mount request with a new
  208. dentry */
  209. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  210. DPRINTK("waiting for expire %p name=%.*s",
  211. dentry, dentry->d_name.len, dentry->d_name.name);
  212. status = autofs4_wait(sbi, dentry, NFY_NONE);
  213. DPRINTK("expire done status=%d", status);
  214. /*
  215. * If the directory still exists the mount request must
  216. * continue otherwise it can't be followed at the right
  217. * time during the walk.
  218. */
  219. status = d_invalidate(dentry);
  220. if (status != -EBUSY)
  221. return -EAGAIN;
  222. }
  223. DPRINTK("dentry=%p %.*s ino=%p",
  224. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  225. /*
  226. * Wait for a pending mount, triggering one if there
  227. * isn't one already
  228. */
  229. if (dentry->d_inode == NULL) {
  230. DPRINTK("waiting for mount name=%.*s",
  231. dentry->d_name.len, dentry->d_name.name);
  232. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  233. DPRINTK("mount done status=%d", status);
  234. /* Turn this into a real negative dentry? */
  235. if (status == -ENOENT) {
  236. spin_lock(&dentry->d_lock);
  237. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  238. spin_unlock(&dentry->d_lock);
  239. return status;
  240. } else if (status) {
  241. /* Return a negative dentry, but leave it "pending" */
  242. return status;
  243. }
  244. /* Trigger mount for path component or follow link */
  245. } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
  246. current->link_count) {
  247. DPRINTK("waiting for mount name=%.*s",
  248. dentry->d_name.len, dentry->d_name.name);
  249. spin_lock(&dentry->d_lock);
  250. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  251. spin_unlock(&dentry->d_lock);
  252. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  253. DPRINTK("mount done status=%d", status);
  254. if (status) {
  255. spin_lock(&dentry->d_lock);
  256. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  257. spin_unlock(&dentry->d_lock);
  258. return status;
  259. }
  260. }
  261. /* Initialize expiry counter after successful mount */
  262. if (ino)
  263. ino->last_used = jiffies;
  264. spin_lock(&dentry->d_lock);
  265. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  266. spin_unlock(&dentry->d_lock);
  267. return status;
  268. }
  269. /* For autofs direct mounts the follow link triggers the mount */
  270. static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
  271. {
  272. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  273. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  274. int oz_mode = autofs4_oz_mode(sbi);
  275. unsigned int lookup_type;
  276. int status;
  277. DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
  278. dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
  279. nd->flags);
  280. /* If it's our master or we shouldn't trigger a mount we're done */
  281. lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
  282. if (oz_mode || !lookup_type)
  283. goto done;
  284. /* If an expire request is pending wait for it. */
  285. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  286. DPRINTK("waiting for active request %p name=%.*s",
  287. dentry, dentry->d_name.len, dentry->d_name.name);
  288. status = autofs4_wait(sbi, dentry, NFY_NONE);
  289. DPRINTK("request done status=%d", status);
  290. }
  291. /*
  292. * If the dentry contains directories then it is an
  293. * autofs multi-mount with no root mount offset. So
  294. * don't try to mount it again.
  295. */
  296. spin_lock(&dcache_lock);
  297. if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
  298. spin_unlock(&dcache_lock);
  299. status = try_to_fill_dentry(dentry, 0);
  300. if (status)
  301. goto out_error;
  302. /*
  303. * The mount succeeded but if there is no root mount
  304. * it must be an autofs multi-mount with no root offset
  305. * so we don't need to follow the mount.
  306. */
  307. if (d_mountpoint(dentry)) {
  308. if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
  309. status = -ENOENT;
  310. goto out_error;
  311. }
  312. }
  313. goto done;
  314. }
  315. spin_unlock(&dcache_lock);
  316. done:
  317. return NULL;
  318. out_error:
  319. path_release(nd);
  320. return ERR_PTR(status);
  321. }
  322. /*
  323. * Revalidate is called on every cache lookup. Some of those
  324. * cache lookups may actually happen while the dentry is not
  325. * yet completely filled in, and revalidate has to delay such
  326. * lookups..
  327. */
  328. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  329. {
  330. struct inode *dir = dentry->d_parent->d_inode;
  331. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  332. int oz_mode = autofs4_oz_mode(sbi);
  333. int flags = nd ? nd->flags : 0;
  334. int status = 1;
  335. /* Pending dentry */
  336. if (autofs4_ispending(dentry)) {
  337. /* The daemon never causes a mount to trigger */
  338. if (oz_mode)
  339. return 1;
  340. /*
  341. * A zero status is success otherwise we have a
  342. * negative error code.
  343. */
  344. status = try_to_fill_dentry(dentry, flags);
  345. if (status == 0)
  346. return 1;
  347. /*
  348. * A status of EAGAIN here means that the dentry has gone
  349. * away while waiting for an expire to complete. If we are
  350. * racing with expire lookup will wait for it so this must
  351. * be a revalidate and we need to send it to lookup.
  352. */
  353. if (status == -EAGAIN)
  354. return 0;
  355. return status;
  356. }
  357. /* Negative dentry.. invalidate if "old" */
  358. if (dentry->d_inode == NULL)
  359. return 0;
  360. /* Check for a non-mountpoint directory with no contents */
  361. spin_lock(&dcache_lock);
  362. if (S_ISDIR(dentry->d_inode->i_mode) &&
  363. !d_mountpoint(dentry) &&
  364. __simple_empty(dentry)) {
  365. DPRINTK("dentry=%p %.*s, emptydir",
  366. dentry, dentry->d_name.len, dentry->d_name.name);
  367. spin_unlock(&dcache_lock);
  368. /* The daemon never causes a mount to trigger */
  369. if (oz_mode)
  370. return 1;
  371. /*
  372. * A zero status is success otherwise we have a
  373. * negative error code.
  374. */
  375. status = try_to_fill_dentry(dentry, flags);
  376. if (status == 0)
  377. return 1;
  378. return status;
  379. }
  380. spin_unlock(&dcache_lock);
  381. return 1;
  382. }
  383. void autofs4_dentry_release(struct dentry *de)
  384. {
  385. struct autofs_info *inf;
  386. DPRINTK("releasing %p", de);
  387. inf = autofs4_dentry_ino(de);
  388. de->d_fsdata = NULL;
  389. if (inf) {
  390. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  391. if (sbi) {
  392. spin_lock(&sbi->rehash_lock);
  393. if (!list_empty(&inf->rehash))
  394. list_del(&inf->rehash);
  395. spin_unlock(&sbi->rehash_lock);
  396. }
  397. inf->dentry = NULL;
  398. inf->inode = NULL;
  399. autofs4_free_ino(inf);
  400. }
  401. }
  402. /* For dentries of directories in the root dir */
  403. static struct dentry_operations autofs4_root_dentry_operations = {
  404. .d_revalidate = autofs4_revalidate,
  405. .d_release = autofs4_dentry_release,
  406. };
  407. /* For other dentries */
  408. static struct dentry_operations autofs4_dentry_operations = {
  409. .d_revalidate = autofs4_revalidate,
  410. .d_release = autofs4_dentry_release,
  411. };
  412. static struct dentry *autofs4_lookup_unhashed(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  413. {
  414. unsigned int len = name->len;
  415. unsigned int hash = name->hash;
  416. const unsigned char *str = name->name;
  417. struct list_head *p, *head;
  418. spin_lock(&dcache_lock);
  419. spin_lock(&sbi->rehash_lock);
  420. head = &sbi->rehash_list;
  421. list_for_each(p, head) {
  422. struct autofs_info *ino;
  423. struct dentry *dentry;
  424. struct qstr *qstr;
  425. ino = list_entry(p, struct autofs_info, rehash);
  426. dentry = ino->dentry;
  427. spin_lock(&dentry->d_lock);
  428. /* Bad luck, we've already been dentry_iput */
  429. if (!dentry->d_inode)
  430. goto next;
  431. qstr = &dentry->d_name;
  432. if (dentry->d_name.hash != hash)
  433. goto next;
  434. if (dentry->d_parent != parent)
  435. goto next;
  436. if (qstr->len != len)
  437. goto next;
  438. if (memcmp(qstr->name, str, len))
  439. goto next;
  440. if (d_unhashed(dentry)) {
  441. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  442. struct inode *inode = dentry->d_inode;
  443. list_del_init(&ino->rehash);
  444. dget(dentry);
  445. /*
  446. * Make the rehashed dentry negative so the VFS
  447. * behaves as it should.
  448. */
  449. if (inode) {
  450. dentry->d_inode = NULL;
  451. list_del_init(&dentry->d_alias);
  452. spin_unlock(&dentry->d_lock);
  453. spin_unlock(&sbi->rehash_lock);
  454. spin_unlock(&dcache_lock);
  455. iput(inode);
  456. return dentry;
  457. }
  458. spin_unlock(&dentry->d_lock);
  459. spin_unlock(&sbi->rehash_lock);
  460. spin_unlock(&dcache_lock);
  461. return dentry;
  462. }
  463. next:
  464. spin_unlock(&dentry->d_lock);
  465. }
  466. spin_unlock(&sbi->rehash_lock);
  467. spin_unlock(&dcache_lock);
  468. return NULL;
  469. }
  470. /* Lookups in the root directory */
  471. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  472. {
  473. struct autofs_sb_info *sbi;
  474. struct dentry *unhashed;
  475. int oz_mode;
  476. DPRINTK("name = %.*s",
  477. dentry->d_name.len, dentry->d_name.name);
  478. /* File name too long to exist */
  479. if (dentry->d_name.len > NAME_MAX)
  480. return ERR_PTR(-ENAMETOOLONG);
  481. sbi = autofs4_sbi(dir->i_sb);
  482. oz_mode = autofs4_oz_mode(sbi);
  483. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  484. current->pid, process_group(current), sbi->catatonic, oz_mode);
  485. unhashed = autofs4_lookup_unhashed(sbi, dentry->d_parent, &dentry->d_name);
  486. if (!unhashed) {
  487. /*
  488. * Mark the dentry incomplete, but add it. This is needed so
  489. * that the VFS layer knows about the dentry, and we can count
  490. * on catching any lookups through the revalidate.
  491. *
  492. * Let all the hard work be done by the revalidate function that
  493. * needs to be able to do this anyway..
  494. *
  495. * We need to do this before we release the directory semaphore.
  496. */
  497. dentry->d_op = &autofs4_root_dentry_operations;
  498. dentry->d_fsdata = NULL;
  499. d_add(dentry, NULL);
  500. } else {
  501. struct autofs_info *ino = autofs4_dentry_ino(unhashed);
  502. DPRINTK("rehash %p with %p", dentry, unhashed);
  503. /*
  504. * If we are racing with expire the request might not
  505. * be quite complete but the directory has been removed
  506. * so it must have been successful, so just wait for it.
  507. */
  508. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  509. DPRINTK("wait for incomplete expire %p name=%.*s",
  510. unhashed, unhashed->d_name.len,
  511. unhashed->d_name.name);
  512. autofs4_wait(sbi, unhashed, NFY_NONE);
  513. DPRINTK("request completed");
  514. }
  515. d_rehash(unhashed);
  516. dentry = unhashed;
  517. }
  518. if (!oz_mode) {
  519. spin_lock(&dentry->d_lock);
  520. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  521. spin_unlock(&dentry->d_lock);
  522. }
  523. if (dentry->d_op && dentry->d_op->d_revalidate) {
  524. mutex_unlock(&dir->i_mutex);
  525. (dentry->d_op->d_revalidate)(dentry, nd);
  526. mutex_lock(&dir->i_mutex);
  527. }
  528. /*
  529. * If we are still pending, check if we had to handle
  530. * a signal. If so we can force a restart..
  531. */
  532. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  533. /* See if we were interrupted */
  534. if (signal_pending(current)) {
  535. sigset_t *sigset = &current->pending.signal;
  536. if (sigismember (sigset, SIGKILL) ||
  537. sigismember (sigset, SIGQUIT) ||
  538. sigismember (sigset, SIGINT)) {
  539. if (unhashed)
  540. dput(unhashed);
  541. return ERR_PTR(-ERESTARTNOINTR);
  542. }
  543. }
  544. spin_lock(&dentry->d_lock);
  545. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  546. spin_unlock(&dentry->d_lock);
  547. }
  548. /*
  549. * If this dentry is unhashed, then we shouldn't honour this
  550. * lookup. Returning ENOENT here doesn't do the right thing
  551. * for all system calls, but it should be OK for the operations
  552. * we permit from an autofs.
  553. */
  554. if (dentry->d_inode && d_unhashed(dentry)) {
  555. /*
  556. * A user space application can (and has done in the past)
  557. * remove and re-create this directory during the callback.
  558. * This can leave us with an unhashed dentry, but a
  559. * successful mount! So we need to perform another
  560. * cached lookup in case the dentry now exists.
  561. */
  562. struct dentry *parent = dentry->d_parent;
  563. struct dentry *new = d_lookup(parent, &dentry->d_name);
  564. if (new != NULL)
  565. dentry = new;
  566. else
  567. dentry = ERR_PTR(-ENOENT);
  568. if (unhashed)
  569. dput(unhashed);
  570. return dentry;
  571. }
  572. if (unhashed)
  573. return dentry;
  574. return NULL;
  575. }
  576. static int autofs4_dir_symlink(struct inode *dir,
  577. struct dentry *dentry,
  578. const char *symname)
  579. {
  580. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  581. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  582. struct autofs_info *p_ino;
  583. struct inode *inode;
  584. char *cp;
  585. DPRINTK("%s <- %.*s", symname,
  586. dentry->d_name.len, dentry->d_name.name);
  587. if (!autofs4_oz_mode(sbi))
  588. return -EACCES;
  589. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  590. if (ino == NULL)
  591. return -ENOSPC;
  592. ino->size = strlen(symname);
  593. ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
  594. if (cp == NULL) {
  595. kfree(ino);
  596. return -ENOSPC;
  597. }
  598. strcpy(cp, symname);
  599. inode = autofs4_get_inode(dir->i_sb, ino);
  600. d_instantiate(dentry, inode);
  601. if (dir == dir->i_sb->s_root->d_inode)
  602. dentry->d_op = &autofs4_root_dentry_operations;
  603. else
  604. dentry->d_op = &autofs4_dentry_operations;
  605. dentry->d_fsdata = ino;
  606. ino->dentry = dget(dentry);
  607. atomic_inc(&ino->count);
  608. p_ino = autofs4_dentry_ino(dentry->d_parent);
  609. if (p_ino && dentry->d_parent != dentry)
  610. atomic_inc(&p_ino->count);
  611. ino->inode = inode;
  612. dir->i_mtime = CURRENT_TIME;
  613. return 0;
  614. }
  615. /*
  616. * NOTE!
  617. *
  618. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  619. * that the file no longer exists. However, doing that means that the
  620. * VFS layer can turn the dentry into a negative dentry. We don't want
  621. * this, because the unlink is probably the result of an expire.
  622. * We simply d_drop it and add it to a rehash candidates list in the
  623. * super block, which allows the dentry lookup to reuse it retaining
  624. * the flags, such as expire in progress, in case we're racing with expire.
  625. *
  626. * If a process is blocked on the dentry waiting for the expire to finish,
  627. * it will invalidate the dentry and try to mount with a new one.
  628. *
  629. * Also see autofs4_dir_rmdir()..
  630. */
  631. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  632. {
  633. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  634. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  635. struct autofs_info *p_ino;
  636. /* This allows root to remove symlinks */
  637. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  638. return -EACCES;
  639. if (atomic_dec_and_test(&ino->count)) {
  640. p_ino = autofs4_dentry_ino(dentry->d_parent);
  641. if (p_ino && dentry->d_parent != dentry)
  642. atomic_dec(&p_ino->count);
  643. }
  644. dput(ino->dentry);
  645. dentry->d_inode->i_size = 0;
  646. clear_nlink(dentry->d_inode);
  647. dir->i_mtime = CURRENT_TIME;
  648. spin_lock(&dcache_lock);
  649. spin_lock(&sbi->rehash_lock);
  650. list_add(&ino->rehash, &sbi->rehash_list);
  651. spin_unlock(&sbi->rehash_lock);
  652. spin_lock(&dentry->d_lock);
  653. __d_drop(dentry);
  654. spin_unlock(&dentry->d_lock);
  655. spin_unlock(&dcache_lock);
  656. return 0;
  657. }
  658. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  659. {
  660. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  661. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  662. struct autofs_info *p_ino;
  663. DPRINTK("dentry %p, removing %.*s",
  664. dentry, dentry->d_name.len, dentry->d_name.name);
  665. if (!autofs4_oz_mode(sbi))
  666. return -EACCES;
  667. spin_lock(&dcache_lock);
  668. if (!list_empty(&dentry->d_subdirs)) {
  669. spin_unlock(&dcache_lock);
  670. return -ENOTEMPTY;
  671. }
  672. spin_lock(&sbi->rehash_lock);
  673. list_add(&ino->rehash, &sbi->rehash_list);
  674. spin_unlock(&sbi->rehash_lock);
  675. spin_lock(&dentry->d_lock);
  676. __d_drop(dentry);
  677. spin_unlock(&dentry->d_lock);
  678. spin_unlock(&dcache_lock);
  679. if (atomic_dec_and_test(&ino->count)) {
  680. p_ino = autofs4_dentry_ino(dentry->d_parent);
  681. if (p_ino && dentry->d_parent != dentry)
  682. atomic_dec(&p_ino->count);
  683. }
  684. dput(ino->dentry);
  685. dentry->d_inode->i_size = 0;
  686. clear_nlink(dentry->d_inode);
  687. if (dir->i_nlink)
  688. drop_nlink(dir);
  689. return 0;
  690. }
  691. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  692. {
  693. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  694. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  695. struct autofs_info *p_ino;
  696. struct inode *inode;
  697. if (!autofs4_oz_mode(sbi))
  698. return -EACCES;
  699. DPRINTK("dentry %p, creating %.*s",
  700. dentry, dentry->d_name.len, dentry->d_name.name);
  701. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  702. if (ino == NULL)
  703. return -ENOSPC;
  704. inode = autofs4_get_inode(dir->i_sb, ino);
  705. d_instantiate(dentry, inode);
  706. if (dir == dir->i_sb->s_root->d_inode)
  707. dentry->d_op = &autofs4_root_dentry_operations;
  708. else
  709. dentry->d_op = &autofs4_dentry_operations;
  710. dentry->d_fsdata = ino;
  711. ino->dentry = dget(dentry);
  712. atomic_inc(&ino->count);
  713. p_ino = autofs4_dentry_ino(dentry->d_parent);
  714. if (p_ino && dentry->d_parent != dentry)
  715. atomic_inc(&p_ino->count);
  716. ino->inode = inode;
  717. inc_nlink(dir);
  718. dir->i_mtime = CURRENT_TIME;
  719. return 0;
  720. }
  721. /* Get/set timeout ioctl() operation */
  722. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  723. unsigned long __user *p)
  724. {
  725. int rv;
  726. unsigned long ntimeout;
  727. if ((rv = get_user(ntimeout, p)) ||
  728. (rv = put_user(sbi->exp_timeout/HZ, p)))
  729. return rv;
  730. if (ntimeout > ULONG_MAX/HZ)
  731. sbi->exp_timeout = 0;
  732. else
  733. sbi->exp_timeout = ntimeout * HZ;
  734. return 0;
  735. }
  736. /* Return protocol version */
  737. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  738. {
  739. return put_user(sbi->version, p);
  740. }
  741. /* Return protocol sub version */
  742. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  743. {
  744. return put_user(sbi->sub_version, p);
  745. }
  746. /*
  747. * Tells the daemon whether we need to reghost or not. Also, clears
  748. * the reghost_needed flag.
  749. */
  750. static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
  751. {
  752. int status;
  753. DPRINTK("returning %d", sbi->needs_reghost);
  754. status = put_user(sbi->needs_reghost, p);
  755. if (status)
  756. return status;
  757. sbi->needs_reghost = 0;
  758. return 0;
  759. }
  760. /*
  761. * Enable / Disable reghosting ioctl() operation
  762. */
  763. static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
  764. {
  765. int status;
  766. int val;
  767. status = get_user(val, p);
  768. DPRINTK("reghost = %d", val);
  769. if (status)
  770. return status;
  771. /* turn on/off reghosting, with the val */
  772. sbi->reghost_enabled = val;
  773. return 0;
  774. }
  775. /*
  776. * Tells the daemon whether it can umount the autofs mount.
  777. */
  778. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  779. {
  780. int status = 0;
  781. if (may_umount(mnt))
  782. status = 1;
  783. DPRINTK("returning %d", status);
  784. status = put_user(status, p);
  785. return status;
  786. }
  787. /* Identify autofs4_dentries - this is so we can tell if there's
  788. an extra dentry refcount or not. We only hold a refcount on the
  789. dentry if its non-negative (ie, d_inode != NULL)
  790. */
  791. int is_autofs4_dentry(struct dentry *dentry)
  792. {
  793. return dentry && dentry->d_inode &&
  794. (dentry->d_op == &autofs4_root_dentry_operations ||
  795. dentry->d_op == &autofs4_dentry_operations) &&
  796. dentry->d_fsdata != NULL;
  797. }
  798. /*
  799. * ioctl()'s on the root directory is the chief method for the daemon to
  800. * generate kernel reactions
  801. */
  802. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  803. unsigned int cmd, unsigned long arg)
  804. {
  805. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  806. void __user *p = (void __user *)arg;
  807. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  808. cmd,arg,sbi,process_group(current));
  809. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  810. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  811. return -ENOTTY;
  812. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  813. return -EPERM;
  814. switch(cmd) {
  815. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  816. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  817. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  818. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  819. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  820. autofs4_catatonic_mode(sbi);
  821. return 0;
  822. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  823. return autofs4_get_protover(sbi, p);
  824. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  825. return autofs4_get_protosubver(sbi, p);
  826. case AUTOFS_IOC_SETTIMEOUT:
  827. return autofs4_get_set_timeout(sbi, p);
  828. case AUTOFS_IOC_TOGGLEREGHOST:
  829. return autofs4_toggle_reghost(sbi, p);
  830. case AUTOFS_IOC_ASKREGHOST:
  831. return autofs4_ask_reghost(sbi, p);
  832. case AUTOFS_IOC_ASKUMOUNT:
  833. return autofs4_ask_umount(filp->f_path.mnt, p);
  834. /* return a single thing to expire */
  835. case AUTOFS_IOC_EXPIRE:
  836. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  837. /* same as above, but can send multiple expires through pipe */
  838. case AUTOFS_IOC_EXPIRE_MULTI:
  839. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  840. default:
  841. return -ENOSYS;
  842. }
  843. }