root.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 path fp_path = { .dentry = dentry, .mnt = mnt };
  126. path_get(&fp_path);
  127. if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
  128. path_put(&fp_path);
  129. dcache_dir_close(inode, file);
  130. goto out;
  131. }
  132. fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
  133. status = PTR_ERR(fp);
  134. if (IS_ERR(fp)) {
  135. dcache_dir_close(inode, file);
  136. goto out;
  137. }
  138. cursor->d_fsdata = fp;
  139. }
  140. return 0;
  141. out:
  142. return status;
  143. }
  144. static int autofs4_dir_close(struct inode *inode, struct file *file)
  145. {
  146. struct dentry *dentry = file->f_path.dentry;
  147. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  148. struct dentry *cursor = file->private_data;
  149. int status = 0;
  150. DPRINTK("file=%p dentry=%p %.*s",
  151. file, dentry, dentry->d_name.len, dentry->d_name.name);
  152. if (autofs4_oz_mode(sbi))
  153. goto out;
  154. if (autofs4_ispending(dentry)) {
  155. DPRINTK("dentry busy");
  156. status = -EBUSY;
  157. goto out;
  158. }
  159. if (d_mountpoint(dentry)) {
  160. struct file *fp = cursor->d_fsdata;
  161. if (!fp) {
  162. status = -ENOENT;
  163. goto out;
  164. }
  165. filp_close(fp, current->files);
  166. }
  167. out:
  168. dcache_dir_close(inode, file);
  169. return status;
  170. }
  171. static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
  172. {
  173. struct dentry *dentry = file->f_path.dentry;
  174. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  175. struct dentry *cursor = file->private_data;
  176. int status;
  177. DPRINTK("file=%p dentry=%p %.*s",
  178. file, dentry, dentry->d_name.len, dentry->d_name.name);
  179. if (autofs4_oz_mode(sbi))
  180. goto out;
  181. if (autofs4_ispending(dentry)) {
  182. DPRINTK("dentry busy");
  183. return -EBUSY;
  184. }
  185. if (d_mountpoint(dentry)) {
  186. struct file *fp = cursor->d_fsdata;
  187. if (!fp)
  188. return -ENOENT;
  189. if (!fp->f_op || !fp->f_op->readdir)
  190. goto out;
  191. status = vfs_readdir(fp, filldir, dirent);
  192. file->f_pos = fp->f_pos;
  193. if (status)
  194. autofs4_copy_atime(file, fp);
  195. return status;
  196. }
  197. out:
  198. return dcache_readdir(file, dirent, filldir);
  199. }
  200. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  201. {
  202. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  203. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  204. struct dentry *new;
  205. int status;
  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. /*
  268. * The dentry that is passed in from lookup may not be the one
  269. * we end up using, as mkdir can create a new one. If this
  270. * happens, and another process tries the lookup at the same time,
  271. * it will set the PENDING flag on this new dentry, but add itself
  272. * to our waitq. Then, if after the lookup succeeds, the first
  273. * process that requested the mount performs another lookup of the
  274. * same directory, it will show up as still pending! So, we need
  275. * to redo the lookup here and clear pending on that dentry.
  276. */
  277. if (d_unhashed(dentry)) {
  278. new = d_lookup(dentry->d_parent, &dentry->d_name);
  279. if (new) {
  280. spin_lock(&new->d_lock);
  281. new->d_flags &= ~DCACHE_AUTOFS_PENDING;
  282. spin_unlock(&new->d_lock);
  283. dput(new);
  284. }
  285. }
  286. return 0;
  287. }
  288. /* For autofs direct mounts the follow link triggers the mount */
  289. static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
  290. {
  291. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  292. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  293. int oz_mode = autofs4_oz_mode(sbi);
  294. unsigned int lookup_type;
  295. int status;
  296. DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
  297. dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
  298. nd->flags);
  299. /* If it's our master or we shouldn't trigger a mount we're done */
  300. lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
  301. if (oz_mode || !lookup_type)
  302. goto done;
  303. /* If an expire request is pending wait for it. */
  304. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  305. DPRINTK("waiting for active request %p name=%.*s",
  306. dentry, dentry->d_name.len, dentry->d_name.name);
  307. status = autofs4_wait(sbi, dentry, NFY_NONE);
  308. DPRINTK("request done status=%d", status);
  309. }
  310. /*
  311. * If the dentry contains directories then it is an
  312. * autofs multi-mount with no root mount offset. So
  313. * don't try to mount it again.
  314. */
  315. spin_lock(&dcache_lock);
  316. if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
  317. spin_unlock(&dcache_lock);
  318. status = try_to_fill_dentry(dentry, 0);
  319. if (status)
  320. goto out_error;
  321. /*
  322. * The mount succeeded but if there is no root mount
  323. * it must be an autofs multi-mount with no root offset
  324. * so we don't need to follow the mount.
  325. */
  326. if (d_mountpoint(dentry)) {
  327. if (!autofs4_follow_mount(&nd->path.mnt,
  328. &nd->path.dentry)) {
  329. status = -ENOENT;
  330. goto out_error;
  331. }
  332. }
  333. goto done;
  334. }
  335. spin_unlock(&dcache_lock);
  336. done:
  337. return NULL;
  338. out_error:
  339. path_put(&nd->path);
  340. return ERR_PTR(status);
  341. }
  342. /*
  343. * Revalidate is called on every cache lookup. Some of those
  344. * cache lookups may actually happen while the dentry is not
  345. * yet completely filled in, and revalidate has to delay such
  346. * lookups..
  347. */
  348. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  349. {
  350. struct inode *dir = dentry->d_parent->d_inode;
  351. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  352. int oz_mode = autofs4_oz_mode(sbi);
  353. int flags = nd ? nd->flags : 0;
  354. int status = 1;
  355. /* Pending dentry */
  356. if (autofs4_ispending(dentry)) {
  357. /* The daemon never causes a mount to trigger */
  358. if (oz_mode)
  359. return 1;
  360. /*
  361. * A zero status is success otherwise we have a
  362. * negative error code.
  363. */
  364. status = try_to_fill_dentry(dentry, flags);
  365. if (status == 0)
  366. return 1;
  367. /*
  368. * A status of EAGAIN here means that the dentry has gone
  369. * away while waiting for an expire to complete. If we are
  370. * racing with expire lookup will wait for it so this must
  371. * be a revalidate and we need to send it to lookup.
  372. */
  373. if (status == -EAGAIN)
  374. return 0;
  375. return status;
  376. }
  377. /* Negative dentry.. invalidate if "old" */
  378. if (dentry->d_inode == NULL)
  379. return 0;
  380. /* Check for a non-mountpoint directory with no contents */
  381. spin_lock(&dcache_lock);
  382. if (S_ISDIR(dentry->d_inode->i_mode) &&
  383. !d_mountpoint(dentry) &&
  384. __simple_empty(dentry)) {
  385. DPRINTK("dentry=%p %.*s, emptydir",
  386. dentry, dentry->d_name.len, dentry->d_name.name);
  387. spin_unlock(&dcache_lock);
  388. /* The daemon never causes a mount to trigger */
  389. if (oz_mode)
  390. return 1;
  391. /*
  392. * A zero status is success otherwise we have a
  393. * negative error code.
  394. */
  395. status = try_to_fill_dentry(dentry, flags);
  396. if (status == 0)
  397. return 1;
  398. return status;
  399. }
  400. spin_unlock(&dcache_lock);
  401. return 1;
  402. }
  403. void autofs4_dentry_release(struct dentry *de)
  404. {
  405. struct autofs_info *inf;
  406. DPRINTK("releasing %p", de);
  407. inf = autofs4_dentry_ino(de);
  408. de->d_fsdata = NULL;
  409. if (inf) {
  410. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  411. if (sbi) {
  412. spin_lock(&sbi->rehash_lock);
  413. if (!list_empty(&inf->rehash))
  414. list_del(&inf->rehash);
  415. spin_unlock(&sbi->rehash_lock);
  416. }
  417. inf->dentry = NULL;
  418. inf->inode = NULL;
  419. autofs4_free_ino(inf);
  420. }
  421. }
  422. /* For dentries of directories in the root dir */
  423. static struct dentry_operations autofs4_root_dentry_operations = {
  424. .d_revalidate = autofs4_revalidate,
  425. .d_release = autofs4_dentry_release,
  426. };
  427. /* For other dentries */
  428. static struct dentry_operations autofs4_dentry_operations = {
  429. .d_revalidate = autofs4_revalidate,
  430. .d_release = autofs4_dentry_release,
  431. };
  432. static struct dentry *autofs4_lookup_unhashed(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
  433. {
  434. unsigned int len = name->len;
  435. unsigned int hash = name->hash;
  436. const unsigned char *str = name->name;
  437. struct list_head *p, *head;
  438. spin_lock(&dcache_lock);
  439. spin_lock(&sbi->rehash_lock);
  440. head = &sbi->rehash_list;
  441. list_for_each(p, head) {
  442. struct autofs_info *ino;
  443. struct dentry *dentry;
  444. struct qstr *qstr;
  445. ino = list_entry(p, struct autofs_info, rehash);
  446. dentry = ino->dentry;
  447. spin_lock(&dentry->d_lock);
  448. /* Bad luck, we've already been dentry_iput */
  449. if (!dentry->d_inode)
  450. goto next;
  451. qstr = &dentry->d_name;
  452. if (dentry->d_name.hash != hash)
  453. goto next;
  454. if (dentry->d_parent != parent)
  455. goto next;
  456. if (qstr->len != len)
  457. goto next;
  458. if (memcmp(qstr->name, str, len))
  459. goto next;
  460. if (d_unhashed(dentry)) {
  461. struct inode *inode = dentry->d_inode;
  462. ino = autofs4_dentry_ino(dentry);
  463. list_del_init(&ino->rehash);
  464. dget(dentry);
  465. /*
  466. * Make the rehashed dentry negative so the VFS
  467. * behaves as it should.
  468. */
  469. if (inode) {
  470. dentry->d_inode = NULL;
  471. list_del_init(&dentry->d_alias);
  472. spin_unlock(&dentry->d_lock);
  473. spin_unlock(&sbi->rehash_lock);
  474. spin_unlock(&dcache_lock);
  475. iput(inode);
  476. return dentry;
  477. }
  478. spin_unlock(&dentry->d_lock);
  479. spin_unlock(&sbi->rehash_lock);
  480. spin_unlock(&dcache_lock);
  481. return dentry;
  482. }
  483. next:
  484. spin_unlock(&dentry->d_lock);
  485. }
  486. spin_unlock(&sbi->rehash_lock);
  487. spin_unlock(&dcache_lock);
  488. return NULL;
  489. }
  490. /* Lookups in the root directory */
  491. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  492. {
  493. struct autofs_sb_info *sbi;
  494. struct dentry *unhashed;
  495. int oz_mode;
  496. DPRINTK("name = %.*s",
  497. dentry->d_name.len, dentry->d_name.name);
  498. /* File name too long to exist */
  499. if (dentry->d_name.len > NAME_MAX)
  500. return ERR_PTR(-ENAMETOOLONG);
  501. sbi = autofs4_sbi(dir->i_sb);
  502. oz_mode = autofs4_oz_mode(sbi);
  503. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  504. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  505. unhashed = autofs4_lookup_unhashed(sbi, dentry->d_parent, &dentry->d_name);
  506. if (!unhashed) {
  507. /*
  508. * Mark the dentry incomplete but don't hash it. We do this
  509. * to serialize our inode creation operations (symlink and
  510. * mkdir) which prevents deadlock during the callback to
  511. * the daemon. Subsequent user space lookups for the same
  512. * dentry are placed on the wait queue while the daemon
  513. * itself is allowed passage unresticted so the create
  514. * operation itself can then hash the dentry. Finally,
  515. * we check for the hashed dentry and return the newly
  516. * hashed dentry.
  517. */
  518. dentry->d_op = &autofs4_root_dentry_operations;
  519. dentry->d_fsdata = NULL;
  520. d_instantiate(dentry, NULL);
  521. } else {
  522. struct autofs_info *ino = autofs4_dentry_ino(unhashed);
  523. DPRINTK("rehash %p with %p", dentry, unhashed);
  524. /*
  525. * If we are racing with expire the request might not
  526. * be quite complete but the directory has been removed
  527. * so it must have been successful, so just wait for it.
  528. * We need to ensure the AUTOFS_INF_EXPIRING flag is clear
  529. * before continuing as revalidate may fail when calling
  530. * try_to_fill_dentry (returning EAGAIN) if we don't.
  531. */
  532. while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  533. DPRINTK("wait for incomplete expire %p name=%.*s",
  534. unhashed, unhashed->d_name.len,
  535. unhashed->d_name.name);
  536. autofs4_wait(sbi, unhashed, NFY_NONE);
  537. DPRINTK("request completed");
  538. }
  539. dentry = unhashed;
  540. }
  541. if (!oz_mode) {
  542. spin_lock(&dentry->d_lock);
  543. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  544. spin_unlock(&dentry->d_lock);
  545. }
  546. if (dentry->d_op && dentry->d_op->d_revalidate) {
  547. mutex_unlock(&dir->i_mutex);
  548. (dentry->d_op->d_revalidate)(dentry, nd);
  549. mutex_lock(&dir->i_mutex);
  550. }
  551. /*
  552. * If we are still pending, check if we had to handle
  553. * a signal. If so we can force a restart..
  554. */
  555. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  556. /* See if we were interrupted */
  557. if (signal_pending(current)) {
  558. sigset_t *sigset = &current->pending.signal;
  559. if (sigismember (sigset, SIGKILL) ||
  560. sigismember (sigset, SIGQUIT) ||
  561. sigismember (sigset, SIGINT)) {
  562. if (unhashed)
  563. dput(unhashed);
  564. return ERR_PTR(-ERESTARTNOINTR);
  565. }
  566. }
  567. spin_lock(&dentry->d_lock);
  568. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  569. spin_unlock(&dentry->d_lock);
  570. }
  571. /*
  572. * If this dentry is unhashed, then we shouldn't honour this
  573. * lookup. Returning ENOENT here doesn't do the right thing
  574. * for all system calls, but it should be OK for the operations
  575. * we permit from an autofs.
  576. */
  577. if (!oz_mode && d_unhashed(dentry)) {
  578. /*
  579. * A user space application can (and has done in the past)
  580. * remove and re-create this directory during the callback.
  581. * This can leave us with an unhashed dentry, but a
  582. * successful mount! So we need to perform another
  583. * cached lookup in case the dentry now exists.
  584. */
  585. struct dentry *parent = dentry->d_parent;
  586. struct dentry *new = d_lookup(parent, &dentry->d_name);
  587. if (new != NULL)
  588. dentry = new;
  589. else
  590. dentry = ERR_PTR(-ENOENT);
  591. if (unhashed)
  592. dput(unhashed);
  593. return dentry;
  594. }
  595. if (unhashed)
  596. return dentry;
  597. return NULL;
  598. }
  599. static int autofs4_dir_symlink(struct inode *dir,
  600. struct dentry *dentry,
  601. const char *symname)
  602. {
  603. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  604. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  605. struct autofs_info *p_ino;
  606. struct inode *inode;
  607. char *cp;
  608. DPRINTK("%s <- %.*s", symname,
  609. dentry->d_name.len, dentry->d_name.name);
  610. if (!autofs4_oz_mode(sbi))
  611. return -EACCES;
  612. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  613. if (ino == NULL)
  614. return -ENOSPC;
  615. ino->size = strlen(symname);
  616. ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
  617. if (cp == NULL) {
  618. kfree(ino);
  619. return -ENOSPC;
  620. }
  621. strcpy(cp, symname);
  622. inode = autofs4_get_inode(dir->i_sb, ino);
  623. d_add(dentry, inode);
  624. if (dir == dir->i_sb->s_root->d_inode)
  625. dentry->d_op = &autofs4_root_dentry_operations;
  626. else
  627. dentry->d_op = &autofs4_dentry_operations;
  628. dentry->d_fsdata = ino;
  629. ino->dentry = dget(dentry);
  630. atomic_inc(&ino->count);
  631. p_ino = autofs4_dentry_ino(dentry->d_parent);
  632. if (p_ino && dentry->d_parent != dentry)
  633. atomic_inc(&p_ino->count);
  634. ino->inode = inode;
  635. dir->i_mtime = CURRENT_TIME;
  636. return 0;
  637. }
  638. /*
  639. * NOTE!
  640. *
  641. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  642. * that the file no longer exists. However, doing that means that the
  643. * VFS layer can turn the dentry into a negative dentry. We don't want
  644. * this, because the unlink is probably the result of an expire.
  645. * We simply d_drop it and add it to a rehash candidates list in the
  646. * super block, which allows the dentry lookup to reuse it retaining
  647. * the flags, such as expire in progress, in case we're racing with expire.
  648. *
  649. * If a process is blocked on the dentry waiting for the expire to finish,
  650. * it will invalidate the dentry and try to mount with a new one.
  651. *
  652. * Also see autofs4_dir_rmdir()..
  653. */
  654. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  655. {
  656. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  657. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  658. struct autofs_info *p_ino;
  659. /* This allows root to remove symlinks */
  660. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  661. return -EACCES;
  662. if (atomic_dec_and_test(&ino->count)) {
  663. p_ino = autofs4_dentry_ino(dentry->d_parent);
  664. if (p_ino && dentry->d_parent != dentry)
  665. atomic_dec(&p_ino->count);
  666. }
  667. dput(ino->dentry);
  668. dentry->d_inode->i_size = 0;
  669. clear_nlink(dentry->d_inode);
  670. dir->i_mtime = CURRENT_TIME;
  671. spin_lock(&dcache_lock);
  672. spin_lock(&sbi->rehash_lock);
  673. list_add(&ino->rehash, &sbi->rehash_list);
  674. spin_unlock(&sbi->rehash_lock);
  675. spin_lock(&dentry->d_lock);
  676. __d_drop(dentry);
  677. spin_unlock(&dentry->d_lock);
  678. spin_unlock(&dcache_lock);
  679. return 0;
  680. }
  681. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  682. {
  683. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  684. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  685. struct autofs_info *p_ino;
  686. DPRINTK("dentry %p, removing %.*s",
  687. dentry, dentry->d_name.len, dentry->d_name.name);
  688. if (!autofs4_oz_mode(sbi))
  689. return -EACCES;
  690. spin_lock(&dcache_lock);
  691. if (!list_empty(&dentry->d_subdirs)) {
  692. spin_unlock(&dcache_lock);
  693. return -ENOTEMPTY;
  694. }
  695. spin_lock(&sbi->rehash_lock);
  696. list_add(&ino->rehash, &sbi->rehash_list);
  697. spin_unlock(&sbi->rehash_lock);
  698. spin_lock(&dentry->d_lock);
  699. __d_drop(dentry);
  700. spin_unlock(&dentry->d_lock);
  701. spin_unlock(&dcache_lock);
  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. if (dir->i_nlink)
  711. drop_nlink(dir);
  712. return 0;
  713. }
  714. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  715. {
  716. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  717. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  718. struct autofs_info *p_ino;
  719. struct inode *inode;
  720. if (!autofs4_oz_mode(sbi))
  721. return -EACCES;
  722. DPRINTK("dentry %p, creating %.*s",
  723. dentry, dentry->d_name.len, dentry->d_name.name);
  724. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  725. if (ino == NULL)
  726. return -ENOSPC;
  727. inode = autofs4_get_inode(dir->i_sb, ino);
  728. d_add(dentry, inode);
  729. if (dir == dir->i_sb->s_root->d_inode)
  730. dentry->d_op = &autofs4_root_dentry_operations;
  731. else
  732. dentry->d_op = &autofs4_dentry_operations;
  733. dentry->d_fsdata = ino;
  734. ino->dentry = dget(dentry);
  735. atomic_inc(&ino->count);
  736. p_ino = autofs4_dentry_ino(dentry->d_parent);
  737. if (p_ino && dentry->d_parent != dentry)
  738. atomic_inc(&p_ino->count);
  739. ino->inode = inode;
  740. inc_nlink(dir);
  741. dir->i_mtime = CURRENT_TIME;
  742. return 0;
  743. }
  744. /* Get/set timeout ioctl() operation */
  745. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  746. unsigned long __user *p)
  747. {
  748. int rv;
  749. unsigned long ntimeout;
  750. if ((rv = get_user(ntimeout, p)) ||
  751. (rv = put_user(sbi->exp_timeout/HZ, p)))
  752. return rv;
  753. if (ntimeout > ULONG_MAX/HZ)
  754. sbi->exp_timeout = 0;
  755. else
  756. sbi->exp_timeout = ntimeout * HZ;
  757. return 0;
  758. }
  759. /* Return protocol version */
  760. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  761. {
  762. return put_user(sbi->version, p);
  763. }
  764. /* Return protocol sub version */
  765. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  766. {
  767. return put_user(sbi->sub_version, p);
  768. }
  769. /*
  770. * Tells the daemon whether we need to reghost or not. Also, clears
  771. * the reghost_needed flag.
  772. */
  773. static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
  774. {
  775. int status;
  776. DPRINTK("returning %d", sbi->needs_reghost);
  777. status = put_user(sbi->needs_reghost, p);
  778. if (status)
  779. return status;
  780. sbi->needs_reghost = 0;
  781. return 0;
  782. }
  783. /*
  784. * Enable / Disable reghosting ioctl() operation
  785. */
  786. static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
  787. {
  788. int status;
  789. int val;
  790. status = get_user(val, p);
  791. DPRINTK("reghost = %d", val);
  792. if (status)
  793. return status;
  794. /* turn on/off reghosting, with the val */
  795. sbi->reghost_enabled = val;
  796. return 0;
  797. }
  798. /*
  799. * Tells the daemon whether it can umount the autofs mount.
  800. */
  801. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  802. {
  803. int status = 0;
  804. if (may_umount(mnt))
  805. status = 1;
  806. DPRINTK("returning %d", status);
  807. status = put_user(status, p);
  808. return status;
  809. }
  810. /* Identify autofs4_dentries - this is so we can tell if there's
  811. an extra dentry refcount or not. We only hold a refcount on the
  812. dentry if its non-negative (ie, d_inode != NULL)
  813. */
  814. int is_autofs4_dentry(struct dentry *dentry)
  815. {
  816. return dentry && dentry->d_inode &&
  817. (dentry->d_op == &autofs4_root_dentry_operations ||
  818. dentry->d_op == &autofs4_dentry_operations) &&
  819. dentry->d_fsdata != NULL;
  820. }
  821. /*
  822. * ioctl()'s on the root directory is the chief method for the daemon to
  823. * generate kernel reactions
  824. */
  825. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  826. unsigned int cmd, unsigned long arg)
  827. {
  828. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  829. void __user *p = (void __user *)arg;
  830. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  831. cmd,arg,sbi,task_pgrp_nr(current));
  832. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  833. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  834. return -ENOTTY;
  835. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  836. return -EPERM;
  837. switch(cmd) {
  838. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  839. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  840. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  841. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  842. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  843. autofs4_catatonic_mode(sbi);
  844. return 0;
  845. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  846. return autofs4_get_protover(sbi, p);
  847. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  848. return autofs4_get_protosubver(sbi, p);
  849. case AUTOFS_IOC_SETTIMEOUT:
  850. return autofs4_get_set_timeout(sbi, p);
  851. case AUTOFS_IOC_TOGGLEREGHOST:
  852. return autofs4_toggle_reghost(sbi, p);
  853. case AUTOFS_IOC_ASKREGHOST:
  854. return autofs4_ask_reghost(sbi, p);
  855. case AUTOFS_IOC_ASKUMOUNT:
  856. return autofs4_ask_umount(filp->f_path.mnt, p);
  857. /* return a single thing to expire */
  858. case AUTOFS_IOC_EXPIRE:
  859. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  860. /* same as above, but can send multiple expires through pipe */
  861. case AUTOFS_IOC_EXPIRE_MULTI:
  862. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  863. default:
  864. return -ENOSYS;
  865. }
  866. }