expfs.c 12 KB

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