root.c 28 KB

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