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