expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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_len_unlocked(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 int 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. int result = 0;
  229. buf->sequence++;
  230. if (buf->ino == ino && len <= NAME_MAX) {
  231. memcpy(buf->name, name, len);
  232. buf->name[len] = '\0';
  233. buf->found = 1;
  234. result = -1;
  235. }
  236. return result;
  237. }
  238. /**
  239. * get_name - default export_operations->get_name function
  240. * @path: the directory in which to find a name
  241. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  242. * @child: the dentry for the child directory.
  243. *
  244. * calls readdir on the parent until it finds an entry with
  245. * the same inode number as the child, and returns that.
  246. */
  247. static int get_name(const struct path *path, char *name, struct dentry *child)
  248. {
  249. const struct cred *cred = current_cred();
  250. struct inode *dir = path->dentry->d_inode;
  251. int error;
  252. struct file *file;
  253. struct kstat stat;
  254. struct path child_path = {
  255. .mnt = path->mnt,
  256. .dentry = child,
  257. };
  258. struct getdents_callback buffer = {
  259. .ctx.actor = filldir_one,
  260. .name = name,
  261. };
  262. error = -ENOTDIR;
  263. if (!dir || !S_ISDIR(dir->i_mode))
  264. goto out;
  265. error = -EINVAL;
  266. if (!dir->i_fop)
  267. goto out;
  268. /*
  269. * inode->i_ino is unsigned long, kstat->ino is u64, so the
  270. * former would be insufficient on 32-bit hosts when the
  271. * filesystem supports 64-bit inode numbers. So we need to
  272. * actually call ->getattr, not just read i_ino:
  273. */
  274. error = vfs_getattr_nosec(&child_path, &stat,
  275. STATX_INO, AT_STATX_SYNC_AS_STAT);
  276. if (error)
  277. return error;
  278. buffer.ino = stat.ino;
  279. /*
  280. * Open the directory ...
  281. */
  282. file = dentry_open(path, O_RDONLY, cred);
  283. error = PTR_ERR(file);
  284. if (IS_ERR(file))
  285. goto out;
  286. error = -EINVAL;
  287. if (!file->f_op->iterate && !file->f_op->iterate_shared)
  288. goto out_close;
  289. buffer.sequence = 0;
  290. while (1) {
  291. int old_seq = buffer.sequence;
  292. error = iterate_dir(file, &buffer.ctx);
  293. if (buffer.found) {
  294. error = 0;
  295. break;
  296. }
  297. if (error < 0)
  298. break;
  299. error = -ENOENT;
  300. if (old_seq == buffer.sequence)
  301. break;
  302. }
  303. out_close:
  304. fput(file);
  305. out:
  306. return error;
  307. }
  308. /**
  309. * export_encode_fh - default export_operations->encode_fh function
  310. * @inode: the object to encode
  311. * @fid: where to store the file handle fragment
  312. * @max_len: maximum length to store there
  313. * @parent: parent directory inode, if wanted
  314. *
  315. * This default encode_fh function assumes that the 32 inode number
  316. * is suitable for locating an inode, and that the generation number
  317. * can be used to check that it is still valid. It places them in the
  318. * filehandle fragment where export_decode_fh expects to find them.
  319. */
  320. static int export_encode_fh(struct inode *inode, struct fid *fid,
  321. int *max_len, struct inode *parent)
  322. {
  323. int len = *max_len;
  324. int type = FILEID_INO32_GEN;
  325. if (parent && (len < 4)) {
  326. *max_len = 4;
  327. return FILEID_INVALID;
  328. } else if (len < 2) {
  329. *max_len = 2;
  330. return FILEID_INVALID;
  331. }
  332. len = 2;
  333. fid->i32.ino = inode->i_ino;
  334. fid->i32.gen = inode->i_generation;
  335. if (parent) {
  336. fid->i32.parent_ino = parent->i_ino;
  337. fid->i32.parent_gen = parent->i_generation;
  338. len = 4;
  339. type = FILEID_INO32_GEN_PARENT;
  340. }
  341. *max_len = len;
  342. return type;
  343. }
  344. int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
  345. int *max_len, struct inode *parent)
  346. {
  347. const struct export_operations *nop = inode->i_sb->s_export_op;
  348. if (nop && nop->encode_fh)
  349. return nop->encode_fh(inode, fid->raw, max_len, parent);
  350. return export_encode_fh(inode, fid, max_len, parent);
  351. }
  352. EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
  353. int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
  354. int connectable)
  355. {
  356. int error;
  357. struct dentry *p = NULL;
  358. struct inode *inode = dentry->d_inode, *parent = NULL;
  359. if (connectable && !S_ISDIR(inode->i_mode)) {
  360. p = dget_parent(dentry);
  361. /*
  362. * note that while p might've ceased to be our parent already,
  363. * it's still pinned by and still positive.
  364. */
  365. parent = p->d_inode;
  366. }
  367. error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
  368. dput(p);
  369. return error;
  370. }
  371. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  372. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  373. int fh_len, int fileid_type,
  374. int (*acceptable)(void *, struct dentry *), void *context)
  375. {
  376. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  377. struct dentry *result, *alias;
  378. char nbuf[NAME_MAX+1];
  379. int err;
  380. /*
  381. * Try to get any dentry for the given file handle from the filesystem.
  382. */
  383. if (!nop || !nop->fh_to_dentry)
  384. return ERR_PTR(-ESTALE);
  385. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  386. if (PTR_ERR(result) == -ENOMEM)
  387. return ERR_CAST(result);
  388. if (IS_ERR_OR_NULL(result))
  389. return ERR_PTR(-ESTALE);
  390. /*
  391. * If no acceptance criteria was specified by caller, a disconnected
  392. * dentry is also accepatable. Callers may use this mode to query if
  393. * file handle is stale or to get a reference to an inode without
  394. * risking the high overhead caused by directory reconnect.
  395. */
  396. if (!acceptable)
  397. return result;
  398. if (d_is_dir(result)) {
  399. /*
  400. * This request is for a directory.
  401. *
  402. * On the positive side there is only one dentry for each
  403. * directory inode. On the negative side this implies that we
  404. * to ensure our dentry is connected all the way up to the
  405. * filesystem root.
  406. */
  407. if (result->d_flags & DCACHE_DISCONNECTED) {
  408. err = reconnect_path(mnt, result, nbuf);
  409. if (err)
  410. goto err_result;
  411. }
  412. if (!acceptable(context, result)) {
  413. err = -EACCES;
  414. goto err_result;
  415. }
  416. return result;
  417. } else {
  418. /*
  419. * It's not a directory. Life is a little more complicated.
  420. */
  421. struct dentry *target_dir, *nresult;
  422. /*
  423. * See if either the dentry we just got from the filesystem
  424. * or any alias for it is acceptable. This is always true
  425. * if this filesystem is exported without the subtreecheck
  426. * option. If the filesystem is exported with the subtree
  427. * check option there's a fair chance we need to look at
  428. * the parent directory in the file handle and make sure
  429. * it's connected to the filesystem root.
  430. */
  431. alias = find_acceptable_alias(result, acceptable, context);
  432. if (alias)
  433. return alias;
  434. /*
  435. * Try to extract a dentry for the parent directory from the
  436. * file handle. If this fails we'll have to give up.
  437. */
  438. err = -ESTALE;
  439. if (!nop->fh_to_parent)
  440. goto err_result;
  441. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  442. fh_len, fileid_type);
  443. if (!target_dir)
  444. goto err_result;
  445. err = PTR_ERR(target_dir);
  446. if (IS_ERR(target_dir))
  447. goto err_result;
  448. /*
  449. * And as usual we need to make sure the parent directory is
  450. * connected to the filesystem root. The VFS really doesn't
  451. * like disconnected directories..
  452. */
  453. err = reconnect_path(mnt, target_dir, nbuf);
  454. if (err) {
  455. dput(target_dir);
  456. goto err_result;
  457. }
  458. /*
  459. * Now that we've got both a well-connected parent and a
  460. * dentry for the inode we're after, make sure that our
  461. * inode is actually connected to the parent.
  462. */
  463. err = exportfs_get_name(mnt, target_dir, nbuf, result);
  464. if (err) {
  465. dput(target_dir);
  466. goto err_result;
  467. }
  468. inode_lock(target_dir->d_inode);
  469. nresult = lookup_one_len(nbuf, 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. if (err != -ENOMEM)
  502. err = -ESTALE;
  503. return ERR_PTR(err);
  504. }
  505. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  506. MODULE_LICENSE("GPL");