expfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include <linux/exportfs.h>
  2. #include <linux/fs.h>
  3. #include <linux/file.h>
  4. #include <linux/module.h>
  5. #include <linux/mount.h>
  6. #include <linux/namei.h>
  7. #define dprintk(fmt, args...) do{}while(0)
  8. static int get_name(struct dentry *dentry, char *name,
  9. struct dentry *child);
  10. static int exportfs_get_name(struct dentry *dir, char *name,
  11. struct dentry *child)
  12. {
  13. const struct export_operations *nop = dir->d_sb->s_export_op;
  14. if (nop->get_name)
  15. return nop->get_name(dir, name, child);
  16. else
  17. return get_name(dir, name, child);
  18. }
  19. /*
  20. * Check if the dentry or any of it's aliases is acceptable.
  21. */
  22. static struct dentry *
  23. find_acceptable_alias(struct dentry *result,
  24. int (*acceptable)(void *context, struct dentry *dentry),
  25. void *context)
  26. {
  27. struct dentry *dentry, *toput = NULL;
  28. if (acceptable(context, result))
  29. return result;
  30. spin_lock(&dcache_lock);
  31. list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
  32. dget_locked(dentry);
  33. spin_unlock(&dcache_lock);
  34. if (toput)
  35. dput(toput);
  36. if (dentry != result && acceptable(context, dentry)) {
  37. dput(result);
  38. return dentry;
  39. }
  40. spin_lock(&dcache_lock);
  41. toput = dentry;
  42. }
  43. spin_unlock(&dcache_lock);
  44. if (toput)
  45. dput(toput);
  46. return NULL;
  47. }
  48. /*
  49. * Find root of a disconnected subtree and return a reference to it.
  50. */
  51. static struct dentry *
  52. find_disconnected_root(struct dentry *dentry)
  53. {
  54. dget(dentry);
  55. spin_lock(&dentry->d_lock);
  56. while (!IS_ROOT(dentry) &&
  57. (dentry->d_parent->d_flags & DCACHE_DISCONNECTED)) {
  58. struct dentry *parent = dentry->d_parent;
  59. dget(parent);
  60. spin_unlock(&dentry->d_lock);
  61. dput(dentry);
  62. dentry = parent;
  63. spin_lock(&dentry->d_lock);
  64. }
  65. spin_unlock(&dentry->d_lock);
  66. return dentry;
  67. }
  68. /*
  69. * Make sure target_dir is fully connected to the dentry tree.
  70. *
  71. * It may already be, as the flag isn't always updated when connection happens.
  72. */
  73. static int
  74. reconnect_path(struct super_block *sb, struct dentry *target_dir)
  75. {
  76. char nbuf[NAME_MAX+1];
  77. int noprogress = 0;
  78. int err = -ESTALE;
  79. /*
  80. * It is possible that a confused file system might not let us complete
  81. * the path to the root. For example, if get_parent returns a directory
  82. * in which we cannot find a name for the child. While this implies a
  83. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  84. * the noprogress counter. If we go through the loop 10 times (2 is
  85. * probably enough) without getting anywhere, we just give up
  86. */
  87. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  88. struct dentry *pd = find_disconnected_root(target_dir);
  89. if (!IS_ROOT(pd)) {
  90. /* must have found a connected parent - great */
  91. spin_lock(&pd->d_lock);
  92. pd->d_flags &= ~DCACHE_DISCONNECTED;
  93. spin_unlock(&pd->d_lock);
  94. noprogress = 0;
  95. } else if (pd == sb->s_root) {
  96. printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
  97. spin_lock(&pd->d_lock);
  98. pd->d_flags &= ~DCACHE_DISCONNECTED;
  99. spin_unlock(&pd->d_lock);
  100. noprogress = 0;
  101. } else {
  102. /*
  103. * We have hit the top of a disconnected path, try to
  104. * find parent and connect.
  105. *
  106. * Racing with some other process renaming a directory
  107. * isn't much of a problem here. If someone renames
  108. * the directory, it will end up properly connected,
  109. * which is what we want
  110. *
  111. * Getting the parent can't be supported generically,
  112. * the locking is too icky.
  113. *
  114. * Instead we just return EACCES. If server reboots
  115. * or inodes get flushed, you lose
  116. */
  117. struct dentry *ppd = ERR_PTR(-EACCES);
  118. struct dentry *npd;
  119. mutex_lock(&pd->d_inode->i_mutex);
  120. if (sb->s_export_op->get_parent)
  121. ppd = sb->s_export_op->get_parent(pd);
  122. mutex_unlock(&pd->d_inode->i_mutex);
  123. if (IS_ERR(ppd)) {
  124. err = PTR_ERR(ppd);
  125. dprintk("%s: get_parent of %ld failed, err %d\n",
  126. __FUNCTION__, pd->d_inode->i_ino, err);
  127. dput(pd);
  128. break;
  129. }
  130. dprintk("%s: find name of %lu in %lu\n", __FUNCTION__,
  131. pd->d_inode->i_ino, ppd->d_inode->i_ino);
  132. err = exportfs_get_name(ppd, nbuf, pd);
  133. if (err) {
  134. dput(ppd);
  135. dput(pd);
  136. if (err == -ENOENT)
  137. /* some race between get_parent and
  138. * get_name? just try again
  139. */
  140. continue;
  141. break;
  142. }
  143. dprintk("%s: found name: %s\n", __FUNCTION__, nbuf);
  144. mutex_lock(&ppd->d_inode->i_mutex);
  145. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  146. mutex_unlock(&ppd->d_inode->i_mutex);
  147. if (IS_ERR(npd)) {
  148. err = PTR_ERR(npd);
  149. dprintk("%s: lookup failed: %d\n",
  150. __FUNCTION__, err);
  151. dput(ppd);
  152. dput(pd);
  153. break;
  154. }
  155. /* we didn't really want npd, we really wanted
  156. * a side-effect of the lookup.
  157. * hopefully, npd == pd, though it isn't really
  158. * a problem if it isn't
  159. */
  160. if (npd == pd)
  161. noprogress = 0;
  162. else
  163. printk("%s: npd != pd\n", __FUNCTION__);
  164. dput(npd);
  165. dput(ppd);
  166. if (IS_ROOT(pd)) {
  167. /* something went wrong, we have to give up */
  168. dput(pd);
  169. break;
  170. }
  171. }
  172. dput(pd);
  173. }
  174. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  175. /* something went wrong - oh-well */
  176. if (!err)
  177. err = -ESTALE;
  178. return err;
  179. }
  180. return 0;
  181. }
  182. struct getdents_callback {
  183. char *name; /* name that was found. It already points to a
  184. buffer NAME_MAX+1 is size */
  185. unsigned long ino; /* the inum we are looking for */
  186. int found; /* inode matched? */
  187. int sequence; /* sequence counter */
  188. };
  189. /*
  190. * A rather strange filldir function to capture
  191. * the name matching the specified inode number.
  192. */
  193. static int filldir_one(void * __buf, const char * name, int len,
  194. loff_t pos, u64 ino, unsigned int d_type)
  195. {
  196. struct getdents_callback *buf = __buf;
  197. int result = 0;
  198. buf->sequence++;
  199. if (buf->ino == ino) {
  200. memcpy(buf->name, name, len);
  201. buf->name[len] = '\0';
  202. buf->found = 1;
  203. result = -1;
  204. }
  205. return result;
  206. }
  207. /**
  208. * get_name - default export_operations->get_name function
  209. * @dentry: the directory in which to find a name
  210. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  211. * @child: the dentry for the child directory.
  212. *
  213. * calls readdir on the parent until it finds an entry with
  214. * the same inode number as the child, and returns that.
  215. */
  216. static int get_name(struct dentry *dentry, char *name,
  217. struct dentry *child)
  218. {
  219. struct inode *dir = dentry->d_inode;
  220. int error;
  221. struct file *file;
  222. struct getdents_callback buffer;
  223. error = -ENOTDIR;
  224. if (!dir || !S_ISDIR(dir->i_mode))
  225. goto out;
  226. error = -EINVAL;
  227. if (!dir->i_fop)
  228. goto out;
  229. /*
  230. * Open the directory ...
  231. */
  232. file = dentry_open(dget(dentry), NULL, O_RDONLY);
  233. error = PTR_ERR(file);
  234. if (IS_ERR(file))
  235. goto out;
  236. error = -EINVAL;
  237. if (!file->f_op->readdir)
  238. goto out_close;
  239. buffer.name = name;
  240. buffer.ino = child->d_inode->i_ino;
  241. buffer.found = 0;
  242. buffer.sequence = 0;
  243. while (1) {
  244. int old_seq = buffer.sequence;
  245. error = vfs_readdir(file, filldir_one, &buffer);
  246. if (error < 0)
  247. break;
  248. error = 0;
  249. if (buffer.found)
  250. break;
  251. error = -ENOENT;
  252. if (old_seq == buffer.sequence)
  253. break;
  254. }
  255. out_close:
  256. fput(file);
  257. out:
  258. return error;
  259. }
  260. /**
  261. * export_encode_fh - default export_operations->encode_fh function
  262. * @dentry: the dentry to encode
  263. * @fh: where to store the file handle fragment
  264. * @max_len: maximum length to store there
  265. * @connectable: whether to store parent information
  266. *
  267. * This default encode_fh function assumes that the 32 inode number
  268. * is suitable for locating an inode, and that the generation number
  269. * can be used to check that it is still valid. It places them in the
  270. * filehandle fragment where export_decode_fh expects to find them.
  271. */
  272. static int export_encode_fh(struct dentry *dentry, struct fid *fid,
  273. int *max_len, int connectable)
  274. {
  275. struct inode * inode = dentry->d_inode;
  276. int len = *max_len;
  277. int type = FILEID_INO32_GEN;
  278. if (len < 2 || (connectable && len < 4))
  279. return 255;
  280. len = 2;
  281. fid->i32.ino = inode->i_ino;
  282. fid->i32.gen = inode->i_generation;
  283. if (connectable && !S_ISDIR(inode->i_mode)) {
  284. struct inode *parent;
  285. spin_lock(&dentry->d_lock);
  286. parent = dentry->d_parent->d_inode;
  287. fid->i32.parent_ino = parent->i_ino;
  288. fid->i32.parent_gen = parent->i_generation;
  289. spin_unlock(&dentry->d_lock);
  290. len = 4;
  291. type = FILEID_INO32_GEN_PARENT;
  292. }
  293. *max_len = len;
  294. return type;
  295. }
  296. int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
  297. int connectable)
  298. {
  299. const struct export_operations *nop = dentry->d_sb->s_export_op;
  300. int error;
  301. if (nop->encode_fh)
  302. error = nop->encode_fh(dentry, fid->raw, max_len, connectable);
  303. else
  304. error = export_encode_fh(dentry, fid, max_len, connectable);
  305. return error;
  306. }
  307. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  308. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  309. int fh_len, int fileid_type,
  310. int (*acceptable)(void *, struct dentry *), void *context)
  311. {
  312. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  313. struct dentry *result, *alias;
  314. int err;
  315. /*
  316. * Try to get any dentry for the given file handle from the filesystem.
  317. */
  318. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  319. if (!result)
  320. result = ERR_PTR(-ESTALE);
  321. if (IS_ERR(result))
  322. return result;
  323. if (S_ISDIR(result->d_inode->i_mode)) {
  324. /*
  325. * This request is for a directory.
  326. *
  327. * On the positive side there is only one dentry for each
  328. * directory inode. On the negative side this implies that we
  329. * to ensure our dentry is connected all the way up to the
  330. * filesystem root.
  331. */
  332. if (result->d_flags & DCACHE_DISCONNECTED) {
  333. err = reconnect_path(mnt->mnt_sb, result);
  334. if (err)
  335. goto err_result;
  336. }
  337. if (!acceptable(context, result)) {
  338. err = -EACCES;
  339. goto err_result;
  340. }
  341. return result;
  342. } else {
  343. /*
  344. * It's not a directory. Life is a little more complicated.
  345. */
  346. struct dentry *target_dir, *nresult;
  347. char nbuf[NAME_MAX+1];
  348. /*
  349. * See if either the dentry we just got from the filesystem
  350. * or any alias for it is acceptable. This is always true
  351. * if this filesystem is exported without the subtreecheck
  352. * option. If the filesystem is exported with the subtree
  353. * check option there's a fair chance we need to look at
  354. * the parent directory in the file handle and make sure
  355. * it's connected to the filesystem root.
  356. */
  357. alias = find_acceptable_alias(result, acceptable, context);
  358. if (alias)
  359. return alias;
  360. /*
  361. * Try to extract a dentry for the parent directory from the
  362. * file handle. If this fails we'll have to give up.
  363. */
  364. err = -ESTALE;
  365. if (!nop->fh_to_parent)
  366. goto err_result;
  367. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  368. fh_len, fileid_type);
  369. if (!target_dir)
  370. goto err_result;
  371. err = PTR_ERR(target_dir);
  372. if (IS_ERR(target_dir))
  373. goto err_result;
  374. /*
  375. * And as usual we need to make sure the parent directory is
  376. * connected to the filesystem root. The VFS really doesn't
  377. * like disconnected directories..
  378. */
  379. err = reconnect_path(mnt->mnt_sb, target_dir);
  380. if (err) {
  381. dput(target_dir);
  382. goto err_result;
  383. }
  384. /*
  385. * Now that we've got both a well-connected parent and a
  386. * dentry for the inode we're after, make sure that our
  387. * inode is actually connected to the parent.
  388. */
  389. err = exportfs_get_name(target_dir, nbuf, result);
  390. if (!err) {
  391. mutex_lock(&target_dir->d_inode->i_mutex);
  392. nresult = lookup_one_len(nbuf, target_dir,
  393. strlen(nbuf));
  394. mutex_unlock(&target_dir->d_inode->i_mutex);
  395. if (!IS_ERR(nresult)) {
  396. if (nresult->d_inode) {
  397. dput(result);
  398. result = nresult;
  399. } else
  400. dput(nresult);
  401. }
  402. }
  403. /*
  404. * At this point we are done with the parent, but it's pinned
  405. * by the child dentry anyway.
  406. */
  407. dput(target_dir);
  408. /*
  409. * And finally make sure the dentry is actually acceptable
  410. * to NFSD.
  411. */
  412. alias = find_acceptable_alias(result, acceptable, context);
  413. if (!alias) {
  414. err = -EACCES;
  415. goto err_result;
  416. }
  417. return alias;
  418. }
  419. err_result:
  420. dput(result);
  421. return ERR_PTR(err);
  422. }
  423. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  424. MODULE_LICENSE("GPL");