expfs.c 14 KB

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