expfs.c 14 KB

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