expfs.c 15 KB

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