root.c 26 KB

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