expfs.c 12 KB

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