expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. #include <linux/cred.h>
  19. #define dprintk(fmt, args...) do{}while(0)
  20. static int get_name(const struct path *path, char *name, struct dentry *child);
  21. static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir,
  22. char *name, struct dentry *child)
  23. {
  24. const struct export_operations *nop = dir->d_sb->s_export_op;
  25. struct path path = {.mnt = mnt, .dentry = dir};
  26. if (nop->get_name)
  27. return nop->get_name(dir, name, child);
  28. else
  29. return get_name(&path, name, child);
  30. }
  31. /*
  32. * Check if the dentry or any of it's aliases is acceptable.
  33. */
  34. static struct dentry *
  35. find_acceptable_alias(struct dentry *result,
  36. int (*acceptable)(void *context, struct dentry *dentry),
  37. void *context)
  38. {
  39. struct dentry *dentry, *toput = NULL;
  40. struct inode *inode;
  41. if (acceptable(context, result))
  42. return result;
  43. inode = result->d_inode;
  44. spin_lock(&inode->i_lock);
  45. hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
  46. dget(dentry);
  47. spin_unlock(&inode->i_lock);
  48. if (toput)
  49. dput(toput);
  50. if (dentry != result && acceptable(context, dentry)) {
  51. dput(result);
  52. return dentry;
  53. }
  54. spin_lock(&inode->i_lock);
  55. toput = dentry;
  56. }
  57. spin_unlock(&inode->i_lock);
  58. if (toput)
  59. dput(toput);
  60. return NULL;
  61. }
  62. static bool dentry_connected(struct dentry *dentry)
  63. {
  64. dget(dentry);
  65. while (dentry->d_flags & DCACHE_DISCONNECTED) {
  66. struct dentry *parent = dget_parent(dentry);
  67. dput(dentry);
  68. if (dentry == parent) {
  69. dput(parent);
  70. return false;
  71. }
  72. dentry = parent;
  73. }
  74. dput(dentry);
  75. return true;
  76. }
  77. static void clear_disconnected(struct dentry *dentry)
  78. {
  79. dget(dentry);
  80. while (dentry->d_flags & DCACHE_DISCONNECTED) {
  81. struct dentry *parent = dget_parent(dentry);
  82. WARN_ON_ONCE(IS_ROOT(dentry));
  83. spin_lock(&dentry->d_lock);
  84. dentry->d_flags &= ~DCACHE_DISCONNECTED;
  85. spin_unlock(&dentry->d_lock);
  86. dput(dentry);
  87. dentry = parent;
  88. }
  89. dput(dentry);
  90. }
  91. /*
  92. * Reconnect a directory dentry with its parent.
  93. *
  94. * This can return a dentry, or NULL, or an error.
  95. *
  96. * In the first case the returned dentry is the parent of the given
  97. * dentry, and may itself need to be reconnected to its parent.
  98. *
  99. * In the NULL case, a concurrent VFS operation has either renamed or
  100. * removed this directory. The concurrent operation has reconnected our
  101. * dentry, so we no longer need to.
  102. */
  103. static struct dentry *reconnect_one(struct vfsmount *mnt,
  104. struct dentry *dentry, char *nbuf)
  105. {
  106. struct dentry *parent;
  107. struct dentry *tmp;
  108. int err;
  109. parent = ERR_PTR(-EACCES);
  110. inode_lock(dentry->d_inode);
  111. if (mnt->mnt_sb->s_export_op->get_parent)
  112. parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
  113. inode_unlock(dentry->d_inode);
  114. if (IS_ERR(parent)) {
  115. dprintk("%s: get_parent of %ld failed, err %d\n",
  116. __func__, dentry->d_inode->i_ino, PTR_ERR(parent));
  117. return parent;
  118. }
  119. dprintk("%s: find name of %lu in %lu\n", __func__,
  120. dentry->d_inode->i_ino, parent->d_inode->i_ino);
  121. err = exportfs_get_name(mnt, parent, nbuf, dentry);
  122. if (err == -ENOENT)
  123. goto out_reconnected;
  124. if (err)
  125. goto out_err;
  126. dprintk("%s: found name: %s\n", __func__, nbuf);
  127. tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
  128. if (IS_ERR(tmp)) {
  129. dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
  130. err = PTR_ERR(tmp);
  131. goto out_err;
  132. }
  133. if (tmp != dentry) {
  134. /*
  135. * Somebody has renamed it since exportfs_get_name();
  136. * great, since it could've only been renamed if it
  137. * got looked up and thus connected, and it would
  138. * remain connected afterwards. We are done.
  139. */
  140. dput(tmp);
  141. goto out_reconnected;
  142. }
  143. dput(tmp);
  144. if (IS_ROOT(dentry)) {
  145. err = -ESTALE;
  146. goto out_err;
  147. }
  148. return parent;
  149. out_err:
  150. dput(parent);
  151. return ERR_PTR(err);
  152. out_reconnected:
  153. dput(parent);
  154. /*
  155. * Someone must have renamed our entry into another parent, in
  156. * which case it has been reconnected by the rename.
  157. *
  158. * Or someone removed it entirely, in which case filehandle
  159. * lookup will succeed but the directory is now IS_DEAD and
  160. * subsequent operations on it will fail.
  161. *
  162. * Alternatively, maybe there was no race at all, and the
  163. * filesystem is just corrupt and gave us a parent that doesn't
  164. * actually contain any entry pointing to this inode. So,
  165. * double check that this worked and return -ESTALE if not:
  166. */
  167. if (!dentry_connected(dentry))
  168. return ERR_PTR(-ESTALE);
  169. return NULL;
  170. }
  171. /*
  172. * Make sure target_dir is fully connected to the dentry tree.
  173. *
  174. * On successful return, DCACHE_DISCONNECTED will be cleared on
  175. * target_dir, and target_dir->d_parent->...->d_parent will reach the
  176. * root of the filesystem.
  177. *
  178. * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
  179. * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
  180. * set but already be connected. In that case we'll verify the
  181. * connection to root and then clear the flag.
  182. *
  183. * Note that target_dir could be removed by a concurrent operation. In
  184. * that case reconnect_path may still succeed with target_dir fully
  185. * connected, but further operations using the filehandle will fail when
  186. * necessary (due to S_DEAD being set on the directory).
  187. */
  188. static int
  189. reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
  190. {
  191. struct dentry *dentry, *parent;
  192. dentry = dget(target_dir);
  193. while (dentry->d_flags & DCACHE_DISCONNECTED) {
  194. BUG_ON(dentry == mnt->mnt_sb->s_root);
  195. if (IS_ROOT(dentry))
  196. parent = reconnect_one(mnt, dentry, nbuf);
  197. else
  198. parent = dget_parent(dentry);
  199. if (!parent)
  200. break;
  201. dput(dentry);
  202. if (IS_ERR(parent))
  203. return PTR_ERR(parent);
  204. dentry = parent;
  205. }
  206. dput(dentry);
  207. clear_disconnected(target_dir);
  208. return 0;
  209. }
  210. struct getdents_callback {
  211. struct dir_context ctx;
  212. char *name; /* name that was found. It already points to a
  213. buffer NAME_MAX+1 is size */
  214. u64 ino; /* the inum we are looking for */
  215. int found; /* inode matched? */
  216. int sequence; /* sequence counter */
  217. };
  218. /*
  219. * A rather strange filldir function to capture
  220. * the name matching the specified inode number.
  221. */
  222. static int filldir_one(struct dir_context *ctx, const char *name, int len,
  223. loff_t pos, u64 ino, unsigned int d_type)
  224. {
  225. struct getdents_callback *buf =
  226. container_of(ctx, struct getdents_callback, ctx);
  227. int result = 0;
  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. result = -1;
  234. }
  235. return result;
  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 *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  372. int fh_len, int fileid_type,
  373. int (*acceptable)(void *, struct dentry *), void *context)
  374. {
  375. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  376. struct dentry *result, *alias;
  377. char nbuf[NAME_MAX+1];
  378. int err;
  379. /*
  380. * Try to get any dentry for the given file handle from the filesystem.
  381. */
  382. if (!nop || !nop->fh_to_dentry)
  383. return ERR_PTR(-ESTALE);
  384. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  385. if (PTR_ERR(result) == -ENOMEM)
  386. return ERR_CAST(result);
  387. if (IS_ERR_OR_NULL(result))
  388. return ERR_PTR(-ESTALE);
  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_len(nbuf, target_dir, strlen(nbuf));
  469. if (!IS_ERR(nresult)) {
  470. if (unlikely(nresult->d_inode != result->d_inode)) {
  471. dput(nresult);
  472. nresult = ERR_PTR(-ESTALE);
  473. }
  474. }
  475. inode_unlock(target_dir->d_inode);
  476. /*
  477. * At this point we are done with the parent, but it's pinned
  478. * by the child dentry anyway.
  479. */
  480. dput(target_dir);
  481. if (IS_ERR(nresult)) {
  482. err = PTR_ERR(nresult);
  483. goto err_result;
  484. }
  485. dput(result);
  486. result = nresult;
  487. /*
  488. * And finally make sure the dentry is actually acceptable
  489. * to NFSD.
  490. */
  491. alias = find_acceptable_alias(result, acceptable, context);
  492. if (!alias) {
  493. err = -EACCES;
  494. goto err_result;
  495. }
  496. return alias;
  497. }
  498. err_result:
  499. dput(result);
  500. if (err != -ENOMEM)
  501. err = -ESTALE;
  502. return ERR_PTR(err);
  503. }
  504. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  505. MODULE_LICENSE("GPL");