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