expfs.c 14 KB

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