expfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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...) pr_debug(fmt, ##args)
  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. if (mnt->mnt_sb->s_export_op->get_parent)
  112. parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
  113. if (IS_ERR(parent)) {
  114. dprintk("get_parent of %lu failed, err %ld\n",
  115. dentry->d_inode->i_ino, PTR_ERR(parent));
  116. return parent;
  117. }
  118. dprintk("%s: find name of %lu in %lu\n", __func__,
  119. dentry->d_inode->i_ino, parent->d_inode->i_ino);
  120. err = exportfs_get_name(mnt, parent, nbuf, dentry);
  121. if (err == -ENOENT)
  122. goto out_reconnected;
  123. if (err)
  124. goto out_err;
  125. dprintk("%s: found name: %s\n", __func__, nbuf);
  126. tmp = lookup_one_unlocked(mnt_idmap(mnt), &QSTR(nbuf), parent);
  127. if (IS_ERR(tmp)) {
  128. dprintk("lookup failed: %ld\n", PTR_ERR(tmp));
  129. err = PTR_ERR(tmp);
  130. goto out_err;
  131. }
  132. if (tmp != dentry) {
  133. /*
  134. * Somebody has renamed it since exportfs_get_name();
  135. * great, since it could've only been renamed if it
  136. * got looked up and thus connected, and it would
  137. * remain connected afterwards. We are done.
  138. */
  139. dput(tmp);
  140. goto out_reconnected;
  141. }
  142. dput(tmp);
  143. if (IS_ROOT(dentry)) {
  144. err = -ESTALE;
  145. goto out_err;
  146. }
  147. return parent;
  148. out_err:
  149. dput(parent);
  150. return ERR_PTR(err);
  151. out_reconnected:
  152. dput(parent);
  153. /*
  154. * Someone must have renamed our entry into another parent, in
  155. * which case it has been reconnected by the rename.
  156. *
  157. * Or someone removed it entirely, in which case filehandle
  158. * lookup will succeed but the directory is now IS_DEAD and
  159. * subsequent operations on it will fail.
  160. *
  161. * Alternatively, maybe there was no race at all, and the
  162. * filesystem is just corrupt and gave us a parent that doesn't
  163. * actually contain any entry pointing to this inode. So,
  164. * double check that this worked and return -ESTALE if not:
  165. */
  166. if (!dentry_connected(dentry))
  167. return ERR_PTR(-ESTALE);
  168. return NULL;
  169. }
  170. /*
  171. * Make sure target_dir is fully connected to the dentry tree.
  172. *
  173. * On successful return, DCACHE_DISCONNECTED will be cleared on
  174. * target_dir, and target_dir->d_parent->...->d_parent will reach the
  175. * root of the filesystem.
  176. *
  177. * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
  178. * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
  179. * set but already be connected. In that case we'll verify the
  180. * connection to root and then clear the flag.
  181. *
  182. * Note that target_dir could be removed by a concurrent operation. In
  183. * that case reconnect_path may still succeed with target_dir fully
  184. * connected, but further operations using the filehandle will fail when
  185. * necessary (due to S_DEAD being set on the directory).
  186. */
  187. static int
  188. reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
  189. {
  190. struct dentry *dentry, *parent;
  191. dentry = dget(target_dir);
  192. while (dentry->d_flags & DCACHE_DISCONNECTED) {
  193. BUG_ON(dentry == mnt->mnt_sb->s_root);
  194. if (IS_ROOT(dentry))
  195. parent = reconnect_one(mnt, dentry, nbuf);
  196. else
  197. parent = dget_parent(dentry);
  198. if (!parent)
  199. break;
  200. dput(dentry);
  201. if (IS_ERR(parent))
  202. return PTR_ERR(parent);
  203. dentry = parent;
  204. }
  205. dput(dentry);
  206. clear_disconnected(target_dir);
  207. return 0;
  208. }
  209. struct getdents_callback {
  210. struct dir_context ctx;
  211. char *name; /* name that was found. It already points to a
  212. buffer NAME_MAX+1 is size */
  213. u64 ino; /* the inum we are looking for */
  214. int found; /* inode matched? */
  215. int sequence; /* sequence counter */
  216. };
  217. /*
  218. * A rather strange filldir function to capture
  219. * the name matching the specified inode number.
  220. */
  221. static bool filldir_one(struct dir_context *ctx, const char *name, int len,
  222. loff_t pos, u64 ino, unsigned int d_type)
  223. {
  224. struct getdents_callback *buf =
  225. container_of(ctx, struct getdents_callback, ctx);
  226. buf->sequence++;
  227. if (buf->ino == ino && len <= NAME_MAX &&
  228. !name_is_dot_dotdot(name, len)) {
  229. memcpy(buf->name, name, len);
  230. buf->name[len] = '\0';
  231. buf->found = 1;
  232. return false; // no more
  233. }
  234. return true;
  235. }
  236. /**
  237. * get_name - default export_operations->get_name function
  238. * @path: the directory in which to find a name
  239. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  240. * @child: the dentry for the child directory.
  241. *
  242. * calls readdir on the parent until it finds an entry with
  243. * the same inode number as the child, and returns that.
  244. */
  245. static int get_name(const struct path *path, char *name, struct dentry *child)
  246. {
  247. const struct cred *cred = current_cred();
  248. struct inode *dir = path->dentry->d_inode;
  249. int error;
  250. struct file *file;
  251. struct kstat stat;
  252. struct path child_path = {
  253. .mnt = path->mnt,
  254. .dentry = child,
  255. };
  256. struct getdents_callback buffer = {
  257. .ctx.actor = filldir_one,
  258. .ctx.count = INT_MAX,
  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_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. #define FILEID_INO64_GEN_LEN 3
  308. /**
  309. * exportfs_encode_ino64_fid - encode non-decodeable 64bit ino file id
  310. * @inode: the object to encode
  311. * @fid: where to store the file handle fragment
  312. * @max_len: maximum length to store there (in 4 byte units)
  313. *
  314. * This generic function is used to encode a non-decodeable file id for
  315. * fanotify for filesystems that do not support NFS export.
  316. */
  317. static int exportfs_encode_ino64_fid(struct inode *inode, struct fid *fid,
  318. int *max_len)
  319. {
  320. if (*max_len < FILEID_INO64_GEN_LEN) {
  321. *max_len = FILEID_INO64_GEN_LEN;
  322. return FILEID_INVALID;
  323. }
  324. fid->i64.ino = inode->i_ino;
  325. fid->i64.gen = inode->i_generation;
  326. *max_len = FILEID_INO64_GEN_LEN;
  327. return FILEID_INO64_GEN;
  328. }
  329. /**
  330. * exportfs_encode_inode_fh - encode a file handle from inode
  331. * @inode: the object to encode
  332. * @fid: where to store the file handle fragment
  333. * @max_len: maximum length to store there
  334. * @parent: parent directory inode, if wanted
  335. * @flags: properties of the requested file handle
  336. *
  337. * Returns an enum fid_type or a negative errno.
  338. */
  339. int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
  340. int *max_len, struct inode *parent, int flags)
  341. {
  342. const struct export_operations *nop = inode->i_sb->s_export_op;
  343. enum fid_type type;
  344. if (!exportfs_can_encode_fh(nop, flags))
  345. return -EOPNOTSUPP;
  346. if (!nop && (flags & EXPORT_FH_FID))
  347. type = exportfs_encode_ino64_fid(inode, fid, max_len);
  348. else
  349. type = nop->encode_fh(inode, fid->raw, max_len, parent);
  350. if (type > 0 && FILEID_USER_FLAGS(type)) {
  351. pr_warn_once("%s: unexpected fh type value 0x%x from fstype %s.\n",
  352. __func__, type, inode->i_sb->s_type->name);
  353. return -EINVAL;
  354. }
  355. return type;
  356. }
  357. EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
  358. /**
  359. * exportfs_encode_fh - encode a file handle from dentry
  360. * @dentry: the object to encode
  361. * @fid: where to store the file handle fragment
  362. * @max_len: maximum length to store there
  363. * @flags: properties of the requested file handle
  364. *
  365. * Returns an enum fid_type or a negative errno.
  366. */
  367. int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
  368. int flags)
  369. {
  370. int error;
  371. struct dentry *p = NULL;
  372. struct inode *inode = dentry->d_inode, *parent = NULL;
  373. if ((flags & EXPORT_FH_CONNECTABLE) && !S_ISDIR(inode->i_mode)) {
  374. p = dget_parent(dentry);
  375. /*
  376. * note that while p might've ceased to be our parent already,
  377. * it's still pinned by and still positive.
  378. */
  379. parent = p->d_inode;
  380. }
  381. error = exportfs_encode_inode_fh(inode, fid, max_len, parent, flags);
  382. dput(p);
  383. return error;
  384. }
  385. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  386. struct dentry *
  387. exportfs_decode_fh_raw(struct vfsmount *mnt, struct fid *fid, int fh_len,
  388. int fileid_type, unsigned int flags,
  389. int (*acceptable)(void *, struct dentry *),
  390. void *context)
  391. {
  392. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  393. struct dentry *result, *alias;
  394. char nbuf[NAME_MAX+1];
  395. int err;
  396. if (fileid_type < 0 || FILEID_USER_FLAGS(fileid_type))
  397. return ERR_PTR(-EINVAL);
  398. /*
  399. * Try to get any dentry for the given file handle from the filesystem.
  400. */
  401. if (!exportfs_can_decode_fh(nop))
  402. return ERR_PTR(-ESTALE);
  403. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  404. if (IS_ERR_OR_NULL(result))
  405. return result;
  406. if ((flags & EXPORT_FH_DIR_ONLY) && !d_is_dir(result)) {
  407. err = -ENOTDIR;
  408. goto err_result;
  409. }
  410. /*
  411. * If no acceptance criteria was specified by caller, a disconnected
  412. * dentry is also accepatable. Callers may use this mode to query if
  413. * file handle is stale or to get a reference to an inode without
  414. * risking the high overhead caused by directory reconnect.
  415. */
  416. if (!acceptable)
  417. return result;
  418. if (d_is_dir(result)) {
  419. /*
  420. * This request is for a directory.
  421. *
  422. * On the positive side there is only one dentry for each
  423. * directory inode. On the negative side this implies that we
  424. * to ensure our dentry is connected all the way up to the
  425. * filesystem root.
  426. */
  427. if (result->d_flags & DCACHE_DISCONNECTED) {
  428. err = reconnect_path(mnt, result, nbuf);
  429. if (err)
  430. goto err_result;
  431. }
  432. if (!acceptable(context, result)) {
  433. err = -EACCES;
  434. goto err_result;
  435. }
  436. return result;
  437. } else {
  438. /*
  439. * It's not a directory. Life is a little more complicated.
  440. */
  441. struct dentry *target_dir, *nresult;
  442. /*
  443. * See if either the dentry we just got from the filesystem
  444. * or any alias for it is acceptable. This is always true
  445. * if this filesystem is exported without the subtreecheck
  446. * option. If the filesystem is exported with the subtree
  447. * check option there's a fair chance we need to look at
  448. * the parent directory in the file handle and make sure
  449. * it's connected to the filesystem root.
  450. */
  451. alias = find_acceptable_alias(result, acceptable, context);
  452. if (alias)
  453. return alias;
  454. /*
  455. * Try to extract a dentry for the parent directory from the
  456. * file handle. If this fails we'll have to give up.
  457. */
  458. err = -ESTALE;
  459. if (!nop->fh_to_parent)
  460. goto err_result;
  461. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  462. fh_len, fileid_type);
  463. if (!target_dir)
  464. goto err_result;
  465. err = PTR_ERR(target_dir);
  466. if (IS_ERR(target_dir))
  467. goto err_result;
  468. /*
  469. * And as usual we need to make sure the parent directory is
  470. * connected to the filesystem root. The VFS really doesn't
  471. * like disconnected directories..
  472. */
  473. err = reconnect_path(mnt, target_dir, nbuf);
  474. if (err) {
  475. dput(target_dir);
  476. goto err_result;
  477. }
  478. /*
  479. * Now that we've got both a well-connected parent and a
  480. * dentry for the inode we're after, make sure that our
  481. * inode is actually connected to the parent.
  482. */
  483. err = exportfs_get_name(mnt, target_dir, nbuf, result);
  484. if (err) {
  485. dput(target_dir);
  486. goto err_result;
  487. }
  488. nresult = lookup_one_unlocked(mnt_idmap(mnt), &QSTR(nbuf), target_dir);
  489. if (!IS_ERR(nresult)) {
  490. if (unlikely(nresult->d_inode != result->d_inode)) {
  491. dput(nresult);
  492. nresult = ERR_PTR(-ESTALE);
  493. }
  494. }
  495. /*
  496. * At this point we are done with the parent, but it's pinned
  497. * by the child dentry anyway.
  498. */
  499. dput(target_dir);
  500. if (IS_ERR(nresult)) {
  501. err = PTR_ERR(nresult);
  502. goto err_result;
  503. }
  504. dput(result);
  505. result = nresult;
  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_raw);
  522. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  523. int fh_len, int fileid_type,
  524. int (*acceptable)(void *, struct dentry *),
  525. void *context)
  526. {
  527. struct dentry *ret;
  528. ret = exportfs_decode_fh_raw(mnt, fid, fh_len, fileid_type, 0,
  529. acceptable, context);
  530. if (IS_ERR_OR_NULL(ret)) {
  531. if (ret == ERR_PTR(-ENOMEM))
  532. return ret;
  533. return ERR_PTR(-ESTALE);
  534. }
  535. return ret;
  536. }
  537. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  538. MODULE_DESCRIPTION("Code mapping from inodes to file handles");
  539. MODULE_LICENSE("GPL");