root.c 26 KB

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