expfs.c 13 KB

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