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