expfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (C) Neil Brown 2002
  3. * Copyright (C) Christoph Hellwig 2007
  4. *
  5. * This file contains the code mapping from inodes to NFS file handles,
  6. * and for mapping back from file handles to dentries.
  7. *
  8. * For details on why we do all the strange and hairy things in here
  9. * take a look at Documentation/filesystems/nfs/Exporting.
  10. */
  11. #include <linux/exportfs.h>
  12. #include <linux/fs.h>
  13. #include <linux/file.h>
  14. #include <linux/module.h>
  15. #include <linux/mount.h>
  16. #include <linux/namei.h>
  17. #include <linux/sched.h>
  18. #define dprintk(fmt, args...) do{}while(0)
  19. static int get_name(const struct path *path, char *name, struct dentry *child);
  20. static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir,
  21. char *name, struct dentry *child)
  22. {
  23. const struct export_operations *nop = dir->d_sb->s_export_op;
  24. struct path path = {.mnt = mnt, .dentry = dir};
  25. if (nop->get_name)
  26. return nop->get_name(dir, name, child);
  27. else
  28. return get_name(&path, name, child);
  29. }
  30. /*
  31. * Check if the dentry or any of it's aliases is acceptable.
  32. */
  33. static struct dentry *
  34. find_acceptable_alias(struct dentry *result,
  35. int (*acceptable)(void *context, struct dentry *dentry),
  36. void *context)
  37. {
  38. struct dentry *dentry, *toput = NULL;
  39. struct inode *inode;
  40. if (acceptable(context, result))
  41. return result;
  42. inode = result->d_inode;
  43. spin_lock(&inode->i_lock);
  44. hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  45. dget(dentry);
  46. spin_unlock(&inode->i_lock);
  47. if (toput)
  48. dput(toput);
  49. if (dentry != result && acceptable(context, dentry)) {
  50. dput(result);
  51. return dentry;
  52. }
  53. spin_lock(&inode->i_lock);
  54. toput = dentry;
  55. }
  56. spin_unlock(&inode->i_lock);
  57. if (toput)
  58. dput(toput);
  59. return NULL;
  60. }
  61. /*
  62. * Find root of a disconnected subtree and return a reference to it.
  63. */
  64. static struct dentry *
  65. find_disconnected_root(struct dentry *dentry)
  66. {
  67. dget(dentry);
  68. while (!IS_ROOT(dentry)) {
  69. struct dentry *parent = dget_parent(dentry);
  70. if (!(parent->d_flags & DCACHE_DISCONNECTED)) {
  71. dput(parent);
  72. break;
  73. }
  74. dput(dentry);
  75. dentry = parent;
  76. }
  77. return dentry;
  78. }
  79. static bool dentry_connected(struct dentry *dentry)
  80. {
  81. dget(dentry);
  82. while (dentry->d_flags & DCACHE_DISCONNECTED) {
  83. struct dentry *parent = dget_parent(dentry);
  84. dput(dentry);
  85. if (IS_ROOT(dentry)) {
  86. dput(parent);
  87. return false;
  88. }
  89. dentry = parent;
  90. }
  91. dput(dentry);
  92. return true;
  93. }
  94. static void clear_disconnected(struct dentry *dentry)
  95. {
  96. dget(dentry);
  97. while (dentry->d_flags & DCACHE_DISCONNECTED) {
  98. struct dentry *parent = dget_parent(dentry);
  99. WARN_ON_ONCE(IS_ROOT(dentry));
  100. spin_lock(&dentry->d_lock);
  101. dentry->d_flags &= ~DCACHE_DISCONNECTED;
  102. spin_unlock(&dentry->d_lock);
  103. dput(dentry);
  104. dentry = parent;
  105. }
  106. dput(dentry);
  107. }
  108. /*
  109. * Make sure target_dir is fully connected to the dentry tree.
  110. *
  111. * On successful return, DCACHE_DISCONNECTED will be cleared on
  112. * target_dir, and target_dir->d_parent->...->d_parent will reach the
  113. * root of the filesystem.
  114. *
  115. * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
  116. * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
  117. * set but already be connected. In that case we'll verify the
  118. * connection to root and then clear the flag.
  119. *
  120. * Note that target_dir could be removed by a concurrent operation. In
  121. * that case reconnect_path may still succeed with target_dir fully
  122. * connected, but further operations using the filehandle will fail when
  123. * necessary (due to S_DEAD being set on the directory).
  124. */
  125. static int
  126. reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
  127. {
  128. int noprogress = 0;
  129. int err = -ESTALE;
  130. /*
  131. * It is possible that a confused file system might not let us complete
  132. * the path to the root. For example, if get_parent returns a directory
  133. * in which we cannot find a name for the child. While this implies a
  134. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  135. * the noprogress counter. If we go through the loop 10 times (2 is
  136. * probably enough) without getting anywhere, we just give up
  137. */
  138. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  139. struct dentry *pd = find_disconnected_root(target_dir);
  140. BUG_ON(pd == mnt->mnt_sb->s_root);
  141. if (!IS_ROOT(pd)) {
  142. /* must have found a connected parent - great */
  143. clear_disconnected(target_dir);
  144. dput(pd);
  145. break;
  146. } else {
  147. /*
  148. * We have hit the top of a disconnected path, try to
  149. * find parent and connect.
  150. *
  151. * Racing with some other process renaming a directory
  152. * isn't much of a problem here. If someone renames
  153. * the directory, it will end up properly connected,
  154. * which is what we want
  155. *
  156. * Getting the parent can't be supported generically,
  157. * the locking is too icky.
  158. *
  159. * Instead we just return EACCES. If server reboots
  160. * or inodes get flushed, you lose
  161. */
  162. struct dentry *ppd = ERR_PTR(-EACCES);
  163. struct dentry *npd;
  164. mutex_lock(&pd->d_inode->i_mutex);
  165. if (mnt->mnt_sb->s_export_op->get_parent)
  166. ppd = mnt->mnt_sb->s_export_op->get_parent(pd);
  167. mutex_unlock(&pd->d_inode->i_mutex);
  168. if (IS_ERR(ppd)) {
  169. err = PTR_ERR(ppd);
  170. dprintk("%s: get_parent of %ld failed, err %d\n",
  171. __func__, pd->d_inode->i_ino, err);
  172. dput(pd);
  173. break;
  174. }
  175. dprintk("%s: find name of %lu in %lu\n", __func__,
  176. pd->d_inode->i_ino, ppd->d_inode->i_ino);
  177. err = exportfs_get_name(mnt, ppd, nbuf, pd);
  178. if (err) {
  179. dput(ppd);
  180. dput(pd);
  181. if (err == -ENOENT)
  182. /* some race between get_parent and
  183. * get_name?
  184. */
  185. goto out_reconnected;
  186. break;
  187. }
  188. dprintk("%s: found name: %s\n", __func__, nbuf);
  189. mutex_lock(&ppd->d_inode->i_mutex);
  190. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  191. mutex_unlock(&ppd->d_inode->i_mutex);
  192. if (IS_ERR(npd)) {
  193. err = PTR_ERR(npd);
  194. dprintk("%s: lookup failed: %d\n",
  195. __func__, err);
  196. dput(ppd);
  197. dput(pd);
  198. break;
  199. }
  200. /* we didn't really want npd, we really wanted
  201. * a side-effect of the lookup.
  202. * hopefully, npd == pd, though it isn't really
  203. * a problem if it isn't
  204. */
  205. dput(npd);
  206. dput(ppd);
  207. if (npd == pd)
  208. noprogress = 0;
  209. else
  210. goto out_reconnected;
  211. if (IS_ROOT(pd)) {
  212. /* something went wrong, we have to give up */
  213. dput(pd);
  214. break;
  215. }
  216. }
  217. dput(pd);
  218. }
  219. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  220. /* something went wrong - oh-well */
  221. if (!err)
  222. err = -ESTALE;
  223. return err;
  224. }
  225. return 0;
  226. out_reconnected:
  227. /*
  228. * Someone must have renamed our entry into another parent, in
  229. * which case it has been reconnected by the rename.
  230. *
  231. * Or someone removed it entirely, in which case filehandle
  232. * lookup will succeed but the directory is now IS_DEAD and
  233. * subsequent operations on it will fail.
  234. *
  235. * Alternatively, maybe there was no race at all, and the
  236. * filesystem is just corrupt and gave us a parent that doesn't
  237. * actually contain any entry pointing to this inode. So,
  238. * double check that this worked and return -ESTALE if not:
  239. */
  240. if (!dentry_connected(target_dir))
  241. return -ESTALE;
  242. clear_disconnected(target_dir);
  243. return 0;
  244. }
  245. struct getdents_callback {
  246. struct dir_context ctx;
  247. char *name; /* name that was found. It already points to a
  248. buffer NAME_MAX+1 is size */
  249. u64 ino; /* the inum we are looking for */
  250. int found; /* inode matched? */
  251. int sequence; /* sequence counter */
  252. };
  253. /*
  254. * A rather strange filldir function to capture
  255. * the name matching the specified inode number.
  256. */
  257. static int filldir_one(void * __buf, const char * name, int len,
  258. loff_t pos, u64 ino, unsigned int d_type)
  259. {
  260. struct getdents_callback *buf = __buf;
  261. int result = 0;
  262. buf->sequence++;
  263. if (buf->ino == ino && len <= NAME_MAX) {
  264. memcpy(buf->name, name, len);
  265. buf->name[len] = '\0';
  266. buf->found = 1;
  267. result = -1;
  268. }
  269. return result;
  270. }
  271. /**
  272. * get_name - default export_operations->get_name function
  273. * @dentry: the directory in which to find a name
  274. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  275. * @child: the dentry for the child directory.
  276. *
  277. * calls readdir on the parent until it finds an entry with
  278. * the same inode number as the child, and returns that.
  279. */
  280. static int get_name(const struct path *path, char *name, struct dentry *child)
  281. {
  282. const struct cred *cred = current_cred();
  283. struct inode *dir = path->dentry->d_inode;
  284. int error;
  285. struct file *file;
  286. struct kstat stat;
  287. struct path child_path = {
  288. .mnt = path->mnt,
  289. .dentry = child,
  290. };
  291. struct getdents_callback buffer = {
  292. .ctx.actor = filldir_one,
  293. .name = name,
  294. };
  295. error = -ENOTDIR;
  296. if (!dir || !S_ISDIR(dir->i_mode))
  297. goto out;
  298. error = -EINVAL;
  299. if (!dir->i_fop)
  300. goto out;
  301. /*
  302. * inode->i_ino is unsigned long, kstat->ino is u64, so the
  303. * former would be insufficient on 32-bit hosts when the
  304. * filesystem supports 64-bit inode numbers. So we need to
  305. * actually call ->getattr, not just read i_ino:
  306. */
  307. error = vfs_getattr_nosec(&child_path, &stat);
  308. if (error)
  309. return error;
  310. buffer.ino = stat.ino;
  311. /*
  312. * Open the directory ...
  313. */
  314. file = dentry_open(path, O_RDONLY, cred);
  315. error = PTR_ERR(file);
  316. if (IS_ERR(file))
  317. goto out;
  318. error = -EINVAL;
  319. if (!file->f_op->iterate)
  320. goto out_close;
  321. buffer.sequence = 0;
  322. while (1) {
  323. int old_seq = buffer.sequence;
  324. error = iterate_dir(file, &buffer.ctx);
  325. if (buffer.found) {
  326. error = 0;
  327. break;
  328. }
  329. if (error < 0)
  330. break;
  331. error = -ENOENT;
  332. if (old_seq == buffer.sequence)
  333. break;
  334. }
  335. out_close:
  336. fput(file);
  337. out:
  338. return error;
  339. }
  340. /**
  341. * export_encode_fh - default export_operations->encode_fh function
  342. * @inode: the object to encode
  343. * @fh: where to store the file handle fragment
  344. * @max_len: maximum length to store there
  345. * @parent: parent directory inode, if wanted
  346. *
  347. * This default encode_fh function assumes that the 32 inode number
  348. * is suitable for locating an inode, and that the generation number
  349. * can be used to check that it is still valid. It places them in the
  350. * filehandle fragment where export_decode_fh expects to find them.
  351. */
  352. static int export_encode_fh(struct inode *inode, struct fid *fid,
  353. int *max_len, struct inode *parent)
  354. {
  355. int len = *max_len;
  356. int type = FILEID_INO32_GEN;
  357. if (parent && (len < 4)) {
  358. *max_len = 4;
  359. return FILEID_INVALID;
  360. } else if (len < 2) {
  361. *max_len = 2;
  362. return FILEID_INVALID;
  363. }
  364. len = 2;
  365. fid->i32.ino = inode->i_ino;
  366. fid->i32.gen = inode->i_generation;
  367. if (parent) {
  368. fid->i32.parent_ino = parent->i_ino;
  369. fid->i32.parent_gen = parent->i_generation;
  370. len = 4;
  371. type = FILEID_INO32_GEN_PARENT;
  372. }
  373. *max_len = len;
  374. return type;
  375. }
  376. int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
  377. int *max_len, struct inode *parent)
  378. {
  379. const struct export_operations *nop = inode->i_sb->s_export_op;
  380. if (nop && nop->encode_fh)
  381. return nop->encode_fh(inode, fid->raw, max_len, parent);
  382. return export_encode_fh(inode, fid, max_len, parent);
  383. }
  384. EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
  385. int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
  386. int connectable)
  387. {
  388. int error;
  389. struct dentry *p = NULL;
  390. struct inode *inode = dentry->d_inode, *parent = NULL;
  391. if (connectable && !S_ISDIR(inode->i_mode)) {
  392. p = dget_parent(dentry);
  393. /*
  394. * note that while p might've ceased to be our parent already,
  395. * it's still pinned by and still positive.
  396. */
  397. parent = p->d_inode;
  398. }
  399. error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
  400. dput(p);
  401. return error;
  402. }
  403. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  404. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  405. int fh_len, int fileid_type,
  406. int (*acceptable)(void *, struct dentry *), void *context)
  407. {
  408. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  409. struct dentry *result, *alias;
  410. char nbuf[NAME_MAX+1];
  411. int err;
  412. /*
  413. * Try to get any dentry for the given file handle from the filesystem.
  414. */
  415. if (!nop || !nop->fh_to_dentry)
  416. return ERR_PTR(-ESTALE);
  417. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  418. if (!result)
  419. result = ERR_PTR(-ESTALE);
  420. if (IS_ERR(result))
  421. return result;
  422. if (S_ISDIR(result->d_inode->i_mode)) {
  423. /*
  424. * This request is for a directory.
  425. *
  426. * On the positive side there is only one dentry for each
  427. * directory inode. On the negative side this implies that we
  428. * to ensure our dentry is connected all the way up to the
  429. * filesystem root.
  430. */
  431. if (result->d_flags & DCACHE_DISCONNECTED) {
  432. err = reconnect_path(mnt, result, nbuf);
  433. if (err)
  434. goto err_result;
  435. }
  436. if (!acceptable(context, result)) {
  437. err = -EACCES;
  438. goto err_result;
  439. }
  440. return result;
  441. } else {
  442. /*
  443. * It's not a directory. Life is a little more complicated.
  444. */
  445. struct dentry *target_dir, *nresult;
  446. /*
  447. * See if either the dentry we just got from the filesystem
  448. * or any alias for it is acceptable. This is always true
  449. * if this filesystem is exported without the subtreecheck
  450. * option. If the filesystem is exported with the subtree
  451. * check option there's a fair chance we need to look at
  452. * the parent directory in the file handle and make sure
  453. * it's connected to the filesystem root.
  454. */
  455. alias = find_acceptable_alias(result, acceptable, context);
  456. if (alias)
  457. return alias;
  458. /*
  459. * Try to extract a dentry for the parent directory from the
  460. * file handle. If this fails we'll have to give up.
  461. */
  462. err = -ESTALE;
  463. if (!nop->fh_to_parent)
  464. goto err_result;
  465. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  466. fh_len, fileid_type);
  467. if (!target_dir)
  468. goto err_result;
  469. err = PTR_ERR(target_dir);
  470. if (IS_ERR(target_dir))
  471. goto err_result;
  472. /*
  473. * And as usual we need to make sure the parent directory is
  474. * connected to the filesystem root. The VFS really doesn't
  475. * like disconnected directories..
  476. */
  477. err = reconnect_path(mnt, target_dir, nbuf);
  478. if (err) {
  479. dput(target_dir);
  480. goto err_result;
  481. }
  482. /*
  483. * Now that we've got both a well-connected parent and a
  484. * dentry for the inode we're after, make sure that our
  485. * inode is actually connected to the parent.
  486. */
  487. err = exportfs_get_name(mnt, target_dir, nbuf, result);
  488. if (!err) {
  489. mutex_lock(&target_dir->d_inode->i_mutex);
  490. nresult = lookup_one_len(nbuf, target_dir,
  491. strlen(nbuf));
  492. mutex_unlock(&target_dir->d_inode->i_mutex);
  493. if (!IS_ERR(nresult)) {
  494. if (nresult->d_inode) {
  495. dput(result);
  496. result = nresult;
  497. } else
  498. dput(nresult);
  499. }
  500. }
  501. /*
  502. * At this point we are done with the parent, but it's pinned
  503. * by the child dentry anyway.
  504. */
  505. dput(target_dir);
  506. /*
  507. * And finally make sure the dentry is actually acceptable
  508. * to NFSD.
  509. */
  510. alias = find_acceptable_alias(result, acceptable, context);
  511. if (!alias) {
  512. err = -EACCES;
  513. goto err_result;
  514. }
  515. return alias;
  516. }
  517. err_result:
  518. dput(result);
  519. return ERR_PTR(err);
  520. }
  521. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  522. MODULE_LICENSE("GPL");