expfs.c 14 KB

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