root.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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, task_pgrp_nr(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 don't hash it. We do this
  489. * to serialize our inode creation operations (symlink and
  490. * mkdir) which prevents deadlock during the callback to
  491. * the daemon. Subsequent user space lookups for the same
  492. * dentry are placed on the wait queue while the daemon
  493. * itself is allowed passage unresticted so the create
  494. * operation itself can then hash the dentry. Finally,
  495. * we check for the hashed dentry and return the newly
  496. * hashed dentry.
  497. */
  498. dentry->d_op = &autofs4_root_dentry_operations;
  499. dentry->d_fsdata = NULL;
  500. d_instantiate(dentry, NULL);
  501. } else {
  502. struct autofs_info *ino = autofs4_dentry_ino(unhashed);
  503. DPRINTK("rehash %p with %p", dentry, unhashed);
  504. /*
  505. * If we are racing with expire the request might not
  506. * be quite complete but the directory has been removed
  507. * so it must have been successful, so just wait for it.
  508. * We need to ensure the AUTOFS_INF_EXPIRING flag is clear
  509. * before continuing as revalidate may fail when calling
  510. * try_to_fill_dentry (returning EAGAIN) if we don't.
  511. */
  512. while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  513. DPRINTK("wait for incomplete expire %p name=%.*s",
  514. unhashed, unhashed->d_name.len,
  515. unhashed->d_name.name);
  516. autofs4_wait(sbi, unhashed, NFY_NONE);
  517. DPRINTK("request completed");
  518. }
  519. dentry = unhashed;
  520. }
  521. if (!oz_mode) {
  522. spin_lock(&dentry->d_lock);
  523. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  524. spin_unlock(&dentry->d_lock);
  525. }
  526. if (dentry->d_op && dentry->d_op->d_revalidate) {
  527. mutex_unlock(&dir->i_mutex);
  528. (dentry->d_op->d_revalidate)(dentry, nd);
  529. mutex_lock(&dir->i_mutex);
  530. }
  531. /*
  532. * If we are still pending, check if we had to handle
  533. * a signal. If so we can force a restart..
  534. */
  535. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  536. /* See if we were interrupted */
  537. if (signal_pending(current)) {
  538. sigset_t *sigset = &current->pending.signal;
  539. if (sigismember (sigset, SIGKILL) ||
  540. sigismember (sigset, SIGQUIT) ||
  541. sigismember (sigset, SIGINT)) {
  542. if (unhashed)
  543. dput(unhashed);
  544. return ERR_PTR(-ERESTARTNOINTR);
  545. }
  546. }
  547. spin_lock(&dentry->d_lock);
  548. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  549. spin_unlock(&dentry->d_lock);
  550. }
  551. /*
  552. * If this dentry is unhashed, then we shouldn't honour this
  553. * lookup. Returning ENOENT here doesn't do the right thing
  554. * for all system calls, but it should be OK for the operations
  555. * we permit from an autofs.
  556. */
  557. if (!oz_mode && d_unhashed(dentry)) {
  558. /*
  559. * A user space application can (and has done in the past)
  560. * remove and re-create this directory during the callback.
  561. * This can leave us with an unhashed dentry, but a
  562. * successful mount! So we need to perform another
  563. * cached lookup in case the dentry now exists.
  564. */
  565. struct dentry *parent = dentry->d_parent;
  566. struct dentry *new = d_lookup(parent, &dentry->d_name);
  567. if (new != NULL)
  568. dentry = new;
  569. else
  570. dentry = ERR_PTR(-ENOENT);
  571. if (unhashed)
  572. dput(unhashed);
  573. return dentry;
  574. }
  575. if (unhashed)
  576. return dentry;
  577. return NULL;
  578. }
  579. static int autofs4_dir_symlink(struct inode *dir,
  580. struct dentry *dentry,
  581. const char *symname)
  582. {
  583. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  584. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  585. struct autofs_info *p_ino;
  586. struct inode *inode;
  587. char *cp;
  588. DPRINTK("%s <- %.*s", symname,
  589. dentry->d_name.len, dentry->d_name.name);
  590. if (!autofs4_oz_mode(sbi))
  591. return -EACCES;
  592. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  593. if (ino == NULL)
  594. return -ENOSPC;
  595. ino->size = strlen(symname);
  596. ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
  597. if (cp == NULL) {
  598. kfree(ino);
  599. return -ENOSPC;
  600. }
  601. strcpy(cp, symname);
  602. inode = autofs4_get_inode(dir->i_sb, ino);
  603. d_add(dentry, inode);
  604. if (dir == dir->i_sb->s_root->d_inode)
  605. dentry->d_op = &autofs4_root_dentry_operations;
  606. else
  607. dentry->d_op = &autofs4_dentry_operations;
  608. dentry->d_fsdata = ino;
  609. ino->dentry = dget(dentry);
  610. atomic_inc(&ino->count);
  611. p_ino = autofs4_dentry_ino(dentry->d_parent);
  612. if (p_ino && dentry->d_parent != dentry)
  613. atomic_inc(&p_ino->count);
  614. ino->inode = inode;
  615. dir->i_mtime = CURRENT_TIME;
  616. return 0;
  617. }
  618. /*
  619. * NOTE!
  620. *
  621. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  622. * that the file no longer exists. However, doing that means that the
  623. * VFS layer can turn the dentry into a negative dentry. We don't want
  624. * this, because the unlink is probably the result of an expire.
  625. * We simply d_drop it and add it to a rehash candidates list in the
  626. * super block, which allows the dentry lookup to reuse it retaining
  627. * the flags, such as expire in progress, in case we're racing with expire.
  628. *
  629. * If a process is blocked on the dentry waiting for the expire to finish,
  630. * it will invalidate the dentry and try to mount with a new one.
  631. *
  632. * Also see autofs4_dir_rmdir()..
  633. */
  634. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  635. {
  636. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  637. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  638. struct autofs_info *p_ino;
  639. /* This allows root to remove symlinks */
  640. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  641. return -EACCES;
  642. if (atomic_dec_and_test(&ino->count)) {
  643. p_ino = autofs4_dentry_ino(dentry->d_parent);
  644. if (p_ino && dentry->d_parent != dentry)
  645. atomic_dec(&p_ino->count);
  646. }
  647. dput(ino->dentry);
  648. dentry->d_inode->i_size = 0;
  649. clear_nlink(dentry->d_inode);
  650. dir->i_mtime = CURRENT_TIME;
  651. spin_lock(&dcache_lock);
  652. spin_lock(&sbi->rehash_lock);
  653. list_add(&ino->rehash, &sbi->rehash_list);
  654. spin_unlock(&sbi->rehash_lock);
  655. spin_lock(&dentry->d_lock);
  656. __d_drop(dentry);
  657. spin_unlock(&dentry->d_lock);
  658. spin_unlock(&dcache_lock);
  659. return 0;
  660. }
  661. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  662. {
  663. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  664. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  665. struct autofs_info *p_ino;
  666. DPRINTK("dentry %p, removing %.*s",
  667. dentry, dentry->d_name.len, dentry->d_name.name);
  668. if (!autofs4_oz_mode(sbi))
  669. return -EACCES;
  670. spin_lock(&dcache_lock);
  671. if (!list_empty(&dentry->d_subdirs)) {
  672. spin_unlock(&dcache_lock);
  673. return -ENOTEMPTY;
  674. }
  675. spin_lock(&sbi->rehash_lock);
  676. list_add(&ino->rehash, &sbi->rehash_list);
  677. spin_unlock(&sbi->rehash_lock);
  678. spin_lock(&dentry->d_lock);
  679. __d_drop(dentry);
  680. spin_unlock(&dentry->d_lock);
  681. spin_unlock(&dcache_lock);
  682. if (atomic_dec_and_test(&ino->count)) {
  683. p_ino = autofs4_dentry_ino(dentry->d_parent);
  684. if (p_ino && dentry->d_parent != dentry)
  685. atomic_dec(&p_ino->count);
  686. }
  687. dput(ino->dentry);
  688. dentry->d_inode->i_size = 0;
  689. clear_nlink(dentry->d_inode);
  690. if (dir->i_nlink)
  691. drop_nlink(dir);
  692. return 0;
  693. }
  694. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  695. {
  696. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  697. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  698. struct autofs_info *p_ino;
  699. struct inode *inode;
  700. if (!autofs4_oz_mode(sbi))
  701. return -EACCES;
  702. DPRINTK("dentry %p, creating %.*s",
  703. dentry, dentry->d_name.len, dentry->d_name.name);
  704. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  705. if (ino == NULL)
  706. return -ENOSPC;
  707. inode = autofs4_get_inode(dir->i_sb, ino);
  708. d_add(dentry, inode);
  709. if (dir == dir->i_sb->s_root->d_inode)
  710. dentry->d_op = &autofs4_root_dentry_operations;
  711. else
  712. dentry->d_op = &autofs4_dentry_operations;
  713. dentry->d_fsdata = ino;
  714. ino->dentry = dget(dentry);
  715. atomic_inc(&ino->count);
  716. p_ino = autofs4_dentry_ino(dentry->d_parent);
  717. if (p_ino && dentry->d_parent != dentry)
  718. atomic_inc(&p_ino->count);
  719. ino->inode = inode;
  720. inc_nlink(dir);
  721. dir->i_mtime = CURRENT_TIME;
  722. return 0;
  723. }
  724. /* Get/set timeout ioctl() operation */
  725. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  726. unsigned long __user *p)
  727. {
  728. int rv;
  729. unsigned long ntimeout;
  730. if ((rv = get_user(ntimeout, p)) ||
  731. (rv = put_user(sbi->exp_timeout/HZ, p)))
  732. return rv;
  733. if (ntimeout > ULONG_MAX/HZ)
  734. sbi->exp_timeout = 0;
  735. else
  736. sbi->exp_timeout = ntimeout * HZ;
  737. return 0;
  738. }
  739. /* Return protocol version */
  740. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  741. {
  742. return put_user(sbi->version, p);
  743. }
  744. /* Return protocol sub version */
  745. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  746. {
  747. return put_user(sbi->sub_version, p);
  748. }
  749. /*
  750. * Tells the daemon whether we need to reghost or not. Also, clears
  751. * the reghost_needed flag.
  752. */
  753. static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
  754. {
  755. int status;
  756. DPRINTK("returning %d", sbi->needs_reghost);
  757. status = put_user(sbi->needs_reghost, p);
  758. if (status)
  759. return status;
  760. sbi->needs_reghost = 0;
  761. return 0;
  762. }
  763. /*
  764. * Enable / Disable reghosting ioctl() operation
  765. */
  766. static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
  767. {
  768. int status;
  769. int val;
  770. status = get_user(val, p);
  771. DPRINTK("reghost = %d", val);
  772. if (status)
  773. return status;
  774. /* turn on/off reghosting, with the val */
  775. sbi->reghost_enabled = val;
  776. return 0;
  777. }
  778. /*
  779. * Tells the daemon whether it can umount the autofs mount.
  780. */
  781. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  782. {
  783. int status = 0;
  784. if (may_umount(mnt))
  785. status = 1;
  786. DPRINTK("returning %d", status);
  787. status = put_user(status, p);
  788. return status;
  789. }
  790. /* Identify autofs4_dentries - this is so we can tell if there's
  791. an extra dentry refcount or not. We only hold a refcount on the
  792. dentry if its non-negative (ie, d_inode != NULL)
  793. */
  794. int is_autofs4_dentry(struct dentry *dentry)
  795. {
  796. return dentry && dentry->d_inode &&
  797. (dentry->d_op == &autofs4_root_dentry_operations ||
  798. dentry->d_op == &autofs4_dentry_operations) &&
  799. dentry->d_fsdata != NULL;
  800. }
  801. /*
  802. * ioctl()'s on the root directory is the chief method for the daemon to
  803. * generate kernel reactions
  804. */
  805. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  806. unsigned int cmd, unsigned long arg)
  807. {
  808. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  809. void __user *p = (void __user *)arg;
  810. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  811. cmd,arg,sbi,task_pgrp_nr(current));
  812. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  813. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  814. return -ENOTTY;
  815. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  816. return -EPERM;
  817. switch(cmd) {
  818. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  819. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  820. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  821. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  822. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  823. autofs4_catatonic_mode(sbi);
  824. return 0;
  825. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  826. return autofs4_get_protover(sbi, p);
  827. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  828. return autofs4_get_protosubver(sbi, p);
  829. case AUTOFS_IOC_SETTIMEOUT:
  830. return autofs4_get_set_timeout(sbi, p);
  831. case AUTOFS_IOC_TOGGLEREGHOST:
  832. return autofs4_toggle_reghost(sbi, p);
  833. case AUTOFS_IOC_ASKREGHOST:
  834. return autofs4_ask_reghost(sbi, p);
  835. case AUTOFS_IOC_ASKUMOUNT:
  836. return autofs4_ask_umount(filp->f_path.mnt, p);
  837. /* return a single thing to expire */
  838. case AUTOFS_IOC_EXPIRE:
  839. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  840. /* same as above, but can send multiple expires through pipe */
  841. case AUTOFS_IOC_EXPIRE_MULTI:
  842. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  843. default:
  844. return -ENOSYS;
  845. }
  846. }