expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. * Reconnect a directory dentry with its parent.
  110. *
  111. * This can return a dentry, or NULL, or an error.
  112. *
  113. * In the first case the returned dentry is the parent of the given
  114. * dentry, and may itself need to be reconnected to its parent.
  115. *
  116. * In the NULL case, a concurrent VFS operation has either renamed or
  117. * removed this directory. The concurrent operation has reconnected our
  118. * dentry, so we no longer need to.
  119. */
  120. static struct dentry *reconnect_one(struct vfsmount *mnt,
  121. struct dentry *dentry, char *nbuf)
  122. {
  123. struct dentry *parent;
  124. struct dentry *tmp;
  125. int err;
  126. parent = ERR_PTR(-EACCES);
  127. mutex_lock(&dentry->d_inode->i_mutex);
  128. if (mnt->mnt_sb->s_export_op->get_parent)
  129. parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
  130. mutex_unlock(&dentry->d_inode->i_mutex);
  131. if (IS_ERR(parent)) {
  132. dprintk("%s: get_parent of %ld failed, err %d\n",
  133. __func__, dentry->d_inode->i_ino, PTR_ERR(parent));
  134. return parent;
  135. }
  136. dprintk("%s: find name of %lu in %lu\n", __func__,
  137. dentry->d_inode->i_ino, parent->d_inode->i_ino);
  138. err = exportfs_get_name(mnt, parent, nbuf, dentry);
  139. if (err == -ENOENT)
  140. goto out_reconnected;
  141. if (err)
  142. goto out_err;
  143. dprintk("%s: found name: %s\n", __func__, nbuf);
  144. mutex_lock(&parent->d_inode->i_mutex);
  145. tmp = lookup_one_len(nbuf, parent, strlen(nbuf));
  146. mutex_unlock(&parent->d_inode->i_mutex);
  147. if (IS_ERR(tmp)) {
  148. dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
  149. goto out_err;
  150. }
  151. if (tmp != dentry) {
  152. dput(tmp);
  153. goto out_reconnected;
  154. }
  155. dput(tmp);
  156. if (IS_ROOT(dentry)) {
  157. err = -ESTALE;
  158. goto out_err;
  159. }
  160. return parent;
  161. out_err:
  162. dput(parent);
  163. return ERR_PTR(err);
  164. out_reconnected:
  165. dput(parent);
  166. /*
  167. * Someone must have renamed our entry into another parent, in
  168. * which case it has been reconnected by the rename.
  169. *
  170. * Or someone removed it entirely, in which case filehandle
  171. * lookup will succeed but the directory is now IS_DEAD and
  172. * subsequent operations on it will fail.
  173. *
  174. * Alternatively, maybe there was no race at all, and the
  175. * filesystem is just corrupt and gave us a parent that doesn't
  176. * actually contain any entry pointing to this inode. So,
  177. * double check that this worked and return -ESTALE if not:
  178. */
  179. if (!dentry_connected(dentry))
  180. return ERR_PTR(-ESTALE);
  181. return NULL;
  182. }
  183. /*
  184. * Make sure target_dir is fully connected to the dentry tree.
  185. *
  186. * On successful return, DCACHE_DISCONNECTED will be cleared on
  187. * target_dir, and target_dir->d_parent->...->d_parent will reach the
  188. * root of the filesystem.
  189. *
  190. * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
  191. * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
  192. * set but already be connected. In that case we'll verify the
  193. * connection to root and then clear the flag.
  194. *
  195. * Note that target_dir could be removed by a concurrent operation. In
  196. * that case reconnect_path may still succeed with target_dir fully
  197. * connected, but further operations using the filehandle will fail when
  198. * necessary (due to S_DEAD being set on the directory).
  199. */
  200. static int
  201. reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
  202. {
  203. int err = -ESTALE;
  204. while (target_dir->d_flags & DCACHE_DISCONNECTED) {
  205. struct dentry *dentry = find_disconnected_root(target_dir);
  206. BUG_ON(dentry == mnt->mnt_sb->s_root);
  207. if (!IS_ROOT(dentry)) {
  208. /* must have found a connected parent - great */
  209. clear_disconnected(target_dir);
  210. dput(dentry);
  211. break;
  212. } else {
  213. struct dentry *parent;
  214. /*
  215. * We have hit the top of a disconnected path, try to
  216. * find parent and connect.
  217. */
  218. parent = reconnect_one(mnt, dentry, nbuf);
  219. if (!parent)
  220. goto out_reconnected;
  221. if (IS_ERR(parent)) {
  222. err = PTR_ERR(parent);
  223. break;
  224. }
  225. dput(parent);
  226. }
  227. dput(dentry);
  228. }
  229. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  230. /* something went wrong - oh-well */
  231. if (!err)
  232. err = -ESTALE;
  233. return err;
  234. }
  235. return 0;
  236. out_reconnected:
  237. clear_disconnected(target_dir);
  238. return 0;
  239. }
  240. struct getdents_callback {
  241. struct dir_context ctx;
  242. char *name; /* name that was found. It already points to a
  243. buffer NAME_MAX+1 is size */
  244. u64 ino; /* the inum we are looking for */
  245. int found; /* inode matched? */
  246. int sequence; /* sequence counter */
  247. };
  248. /*
  249. * A rather strange filldir function to capture
  250. * the name matching the specified inode number.
  251. */
  252. static int filldir_one(void * __buf, const char * name, int len,
  253. loff_t pos, u64 ino, unsigned int d_type)
  254. {
  255. struct getdents_callback *buf = __buf;
  256. int result = 0;
  257. buf->sequence++;
  258. if (buf->ino == ino && len <= NAME_MAX) {
  259. memcpy(buf->name, name, len);
  260. buf->name[len] = '\0';
  261. buf->found = 1;
  262. result = -1;
  263. }
  264. return result;
  265. }
  266. /**
  267. * get_name - default export_operations->get_name function
  268. * @dentry: the directory in which to find a name
  269. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  270. * @child: the dentry for the child directory.
  271. *
  272. * calls readdir on the parent until it finds an entry with
  273. * the same inode number as the child, and returns that.
  274. */
  275. static int get_name(const struct path *path, char *name, struct dentry *child)
  276. {
  277. const struct cred *cred = current_cred();
  278. struct inode *dir = path->dentry->d_inode;
  279. int error;
  280. struct file *file;
  281. struct kstat stat;
  282. struct path child_path = {
  283. .mnt = path->mnt,
  284. .dentry = child,
  285. };
  286. struct getdents_callback buffer = {
  287. .ctx.actor = filldir_one,
  288. .name = name,
  289. };
  290. error = -ENOTDIR;
  291. if (!dir || !S_ISDIR(dir->i_mode))
  292. goto out;
  293. error = -EINVAL;
  294. if (!dir->i_fop)
  295. goto out;
  296. /*
  297. * inode->i_ino is unsigned long, kstat->ino is u64, so the
  298. * former would be insufficient on 32-bit hosts when the
  299. * filesystem supports 64-bit inode numbers. So we need to
  300. * actually call ->getattr, not just read i_ino:
  301. */
  302. error = vfs_getattr_nosec(&child_path, &stat);
  303. if (error)
  304. return error;
  305. buffer.ino = stat.ino;
  306. /*
  307. * Open the directory ...
  308. */
  309. file = dentry_open(path, O_RDONLY, cred);
  310. error = PTR_ERR(file);
  311. if (IS_ERR(file))
  312. goto out;
  313. error = -EINVAL;
  314. if (!file->f_op->iterate)
  315. goto out_close;
  316. buffer.sequence = 0;
  317. while (1) {
  318. int old_seq = buffer.sequence;
  319. error = iterate_dir(file, &buffer.ctx);
  320. if (buffer.found) {
  321. error = 0;
  322. break;
  323. }
  324. if (error < 0)
  325. break;
  326. error = -ENOENT;
  327. if (old_seq == buffer.sequence)
  328. break;
  329. }
  330. out_close:
  331. fput(file);
  332. out:
  333. return error;
  334. }
  335. /**
  336. * export_encode_fh - default export_operations->encode_fh function
  337. * @inode: the object to encode
  338. * @fh: where to store the file handle fragment
  339. * @max_len: maximum length to store there
  340. * @parent: parent directory inode, if wanted
  341. *
  342. * This default encode_fh function assumes that the 32 inode number
  343. * is suitable for locating an inode, and that the generation number
  344. * can be used to check that it is still valid. It places them in the
  345. * filehandle fragment where export_decode_fh expects to find them.
  346. */
  347. static int export_encode_fh(struct inode *inode, struct fid *fid,
  348. int *max_len, struct inode *parent)
  349. {
  350. int len = *max_len;
  351. int type = FILEID_INO32_GEN;
  352. if (parent && (len < 4)) {
  353. *max_len = 4;
  354. return FILEID_INVALID;
  355. } else if (len < 2) {
  356. *max_len = 2;
  357. return FILEID_INVALID;
  358. }
  359. len = 2;
  360. fid->i32.ino = inode->i_ino;
  361. fid->i32.gen = inode->i_generation;
  362. if (parent) {
  363. fid->i32.parent_ino = parent->i_ino;
  364. fid->i32.parent_gen = parent->i_generation;
  365. len = 4;
  366. type = FILEID_INO32_GEN_PARENT;
  367. }
  368. *max_len = len;
  369. return type;
  370. }
  371. int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
  372. int *max_len, struct inode *parent)
  373. {
  374. const struct export_operations *nop = inode->i_sb->s_export_op;
  375. if (nop && nop->encode_fh)
  376. return nop->encode_fh(inode, fid->raw, max_len, parent);
  377. return export_encode_fh(inode, fid, max_len, parent);
  378. }
  379. EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
  380. int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
  381. int connectable)
  382. {
  383. int error;
  384. struct dentry *p = NULL;
  385. struct inode *inode = dentry->d_inode, *parent = NULL;
  386. if (connectable && !S_ISDIR(inode->i_mode)) {
  387. p = dget_parent(dentry);
  388. /*
  389. * note that while p might've ceased to be our parent already,
  390. * it's still pinned by and still positive.
  391. */
  392. parent = p->d_inode;
  393. }
  394. error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
  395. dput(p);
  396. return error;
  397. }
  398. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  399. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  400. int fh_len, int fileid_type,
  401. int (*acceptable)(void *, struct dentry *), void *context)
  402. {
  403. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  404. struct dentry *result, *alias;
  405. char nbuf[NAME_MAX+1];
  406. int err;
  407. /*
  408. * Try to get any dentry for the given file handle from the filesystem.
  409. */
  410. if (!nop || !nop->fh_to_dentry)
  411. return ERR_PTR(-ESTALE);
  412. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  413. if (!result)
  414. result = ERR_PTR(-ESTALE);
  415. if (IS_ERR(result))
  416. return result;
  417. if (S_ISDIR(result->d_inode->i_mode)) {
  418. /*
  419. * This request is for a directory.
  420. *
  421. * On the positive side there is only one dentry for each
  422. * directory inode. On the negative side this implies that we
  423. * to ensure our dentry is connected all the way up to the
  424. * filesystem root.
  425. */
  426. if (result->d_flags & DCACHE_DISCONNECTED) {
  427. err = reconnect_path(mnt, result, nbuf);
  428. if (err)
  429. goto err_result;
  430. }
  431. if (!acceptable(context, result)) {
  432. err = -EACCES;
  433. goto err_result;
  434. }
  435. return result;
  436. } else {
  437. /*
  438. * It's not a directory. Life is a little more complicated.
  439. */
  440. struct dentry *target_dir, *nresult;
  441. /*
  442. * See if either the dentry we just got from the filesystem
  443. * or any alias for it is acceptable. This is always true
  444. * if this filesystem is exported without the subtreecheck
  445. * option. If the filesystem is exported with the subtree
  446. * check option there's a fair chance we need to look at
  447. * the parent directory in the file handle and make sure
  448. * it's connected to the filesystem root.
  449. */
  450. alias = find_acceptable_alias(result, acceptable, context);
  451. if (alias)
  452. return alias;
  453. /*
  454. * Try to extract a dentry for the parent directory from the
  455. * file handle. If this fails we'll have to give up.
  456. */
  457. err = -ESTALE;
  458. if (!nop->fh_to_parent)
  459. goto err_result;
  460. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  461. fh_len, fileid_type);
  462. if (!target_dir)
  463. goto err_result;
  464. err = PTR_ERR(target_dir);
  465. if (IS_ERR(target_dir))
  466. goto err_result;
  467. /*
  468. * And as usual we need to make sure the parent directory is
  469. * connected to the filesystem root. The VFS really doesn't
  470. * like disconnected directories..
  471. */
  472. err = reconnect_path(mnt, target_dir, nbuf);
  473. if (err) {
  474. dput(target_dir);
  475. goto err_result;
  476. }
  477. /*
  478. * Now that we've got both a well-connected parent and a
  479. * dentry for the inode we're after, make sure that our
  480. * inode is actually connected to the parent.
  481. */
  482. err = exportfs_get_name(mnt, target_dir, nbuf, result);
  483. if (!err) {
  484. mutex_lock(&target_dir->d_inode->i_mutex);
  485. nresult = lookup_one_len(nbuf, target_dir,
  486. strlen(nbuf));
  487. mutex_unlock(&target_dir->d_inode->i_mutex);
  488. if (!IS_ERR(nresult)) {
  489. if (nresult->d_inode) {
  490. dput(result);
  491. result = nresult;
  492. } else
  493. dput(nresult);
  494. }
  495. }
  496. /*
  497. * At this point we are done with the parent, but it's pinned
  498. * by the child dentry anyway.
  499. */
  500. dput(target_dir);
  501. /*
  502. * And finally make sure the dentry is actually acceptable
  503. * to NFSD.
  504. */
  505. alias = find_acceptable_alias(result, acceptable, context);
  506. if (!alias) {
  507. err = -EACCES;
  508. goto err_result;
  509. }
  510. return alias;
  511. }
  512. err_result:
  513. dput(result);
  514. return ERR_PTR(err);
  515. }
  516. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  517. MODULE_LICENSE("GPL");