expfs.c 14 KB

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