expfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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(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. while (!IS_ROOT(dentry)) {
  67. struct dentry *parent = dget_parent(dentry);
  68. if (!(parent->d_flags & DCACHE_DISCONNECTED)) {
  69. dput(parent);
  70. break;
  71. }
  72. dput(dentry);
  73. dentry = parent;
  74. }
  75. return dentry;
  76. }
  77. /*
  78. * Make sure target_dir is fully connected to the dentry tree.
  79. *
  80. * It may already be, as the flag isn't always updated when connection happens.
  81. */
  82. static int
  83. reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
  84. {
  85. int noprogress = 0;
  86. int err = -ESTALE;
  87. /*
  88. * It is possible that a confused file system might not let us complete
  89. * the path to the root. For example, if get_parent returns a directory
  90. * in which we cannot find a name for the child. While this implies a
  91. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  92. * the noprogress counter. If we go through the loop 10 times (2 is
  93. * probably enough) without getting anywhere, we just give up
  94. */
  95. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  96. struct dentry *pd = find_disconnected_root(target_dir);
  97. if (!IS_ROOT(pd)) {
  98. /* must have found a connected parent - great */
  99. spin_lock(&pd->d_lock);
  100. pd->d_flags &= ~DCACHE_DISCONNECTED;
  101. spin_unlock(&pd->d_lock);
  102. noprogress = 0;
  103. } else if (pd == mnt->mnt_sb->s_root) {
  104. printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
  105. spin_lock(&pd->d_lock);
  106. pd->d_flags &= ~DCACHE_DISCONNECTED;
  107. spin_unlock(&pd->d_lock);
  108. noprogress = 0;
  109. } else {
  110. /*
  111. * We have hit the top of a disconnected path, try to
  112. * find parent and connect.
  113. *
  114. * Racing with some other process renaming a directory
  115. * isn't much of a problem here. If someone renames
  116. * the directory, it will end up properly connected,
  117. * which is what we want
  118. *
  119. * Getting the parent can't be supported generically,
  120. * the locking is too icky.
  121. *
  122. * Instead we just return EACCES. If server reboots
  123. * or inodes get flushed, you lose
  124. */
  125. struct dentry *ppd = ERR_PTR(-EACCES);
  126. struct dentry *npd;
  127. mutex_lock(&pd->d_inode->i_mutex);
  128. if (mnt->mnt_sb->s_export_op->get_parent)
  129. ppd = mnt->mnt_sb->s_export_op->get_parent(pd);
  130. mutex_unlock(&pd->d_inode->i_mutex);
  131. if (IS_ERR(ppd)) {
  132. err = PTR_ERR(ppd);
  133. dprintk("%s: get_parent of %ld failed, err %d\n",
  134. __func__, pd->d_inode->i_ino, err);
  135. dput(pd);
  136. break;
  137. }
  138. dprintk("%s: find name of %lu in %lu\n", __func__,
  139. pd->d_inode->i_ino, ppd->d_inode->i_ino);
  140. err = exportfs_get_name(mnt, ppd, nbuf, pd);
  141. if (err) {
  142. dput(ppd);
  143. dput(pd);
  144. if (err == -ENOENT)
  145. /* some race between get_parent and
  146. * get_name? just try again
  147. */
  148. continue;
  149. break;
  150. }
  151. dprintk("%s: found name: %s\n", __func__, nbuf);
  152. mutex_lock(&ppd->d_inode->i_mutex);
  153. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  154. mutex_unlock(&ppd->d_inode->i_mutex);
  155. if (IS_ERR(npd)) {
  156. err = PTR_ERR(npd);
  157. dprintk("%s: lookup failed: %d\n",
  158. __func__, err);
  159. dput(ppd);
  160. dput(pd);
  161. break;
  162. }
  163. /* we didn't really want npd, we really wanted
  164. * a side-effect of the lookup.
  165. * hopefully, npd == pd, though it isn't really
  166. * a problem if it isn't
  167. */
  168. if (npd == pd)
  169. noprogress = 0;
  170. else
  171. printk("%s: npd != pd\n", __func__);
  172. dput(npd);
  173. dput(ppd);
  174. if (IS_ROOT(pd)) {
  175. /* something went wrong, we have to give up */
  176. dput(pd);
  177. break;
  178. }
  179. }
  180. dput(pd);
  181. }
  182. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  183. /* something went wrong - oh-well */
  184. if (!err)
  185. err = -ESTALE;
  186. return err;
  187. }
  188. return 0;
  189. }
  190. struct getdents_callback {
  191. char *name; /* name that was found. It already points to a
  192. buffer NAME_MAX+1 is size */
  193. unsigned long ino; /* the inum we are looking for */
  194. int found; /* inode matched? */
  195. int sequence; /* sequence counter */
  196. };
  197. /*
  198. * A rather strange filldir function to capture
  199. * the name matching the specified inode number.
  200. */
  201. static int filldir_one(void * __buf, const char * name, int len,
  202. loff_t pos, u64 ino, unsigned int d_type)
  203. {
  204. struct getdents_callback *buf = __buf;
  205. int result = 0;
  206. buf->sequence++;
  207. if (buf->ino == ino) {
  208. memcpy(buf->name, name, len);
  209. buf->name[len] = '\0';
  210. buf->found = 1;
  211. result = -1;
  212. }
  213. return result;
  214. }
  215. /**
  216. * get_name - default export_operations->get_name function
  217. * @dentry: the directory in which to find a name
  218. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  219. * @child: the dentry for the child directory.
  220. *
  221. * calls readdir on the parent until it finds an entry with
  222. * the same inode number as the child, and returns that.
  223. */
  224. static int get_name(struct vfsmount *mnt, struct dentry *dentry,
  225. char *name, struct dentry *child)
  226. {
  227. const struct cred *cred = current_cred();
  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, cred);
  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 (!result)
  331. result = ERR_PTR(-ESTALE);
  332. if (IS_ERR(result))
  333. return result;
  334. if (S_ISDIR(result->d_inode->i_mode)) {
  335. /*
  336. * This request is for a directory.
  337. *
  338. * On the positive side there is only one dentry for each
  339. * directory inode. On the negative side this implies that we
  340. * to ensure our dentry is connected all the way up to the
  341. * filesystem root.
  342. */
  343. if (result->d_flags & DCACHE_DISCONNECTED) {
  344. err = reconnect_path(mnt, result, nbuf);
  345. if (err)
  346. goto err_result;
  347. }
  348. if (!acceptable(context, result)) {
  349. err = -EACCES;
  350. goto err_result;
  351. }
  352. return result;
  353. } else {
  354. /*
  355. * It's not a directory. Life is a little more complicated.
  356. */
  357. struct dentry *target_dir, *nresult;
  358. /*
  359. * See if either the dentry we just got from the filesystem
  360. * or any alias for it is acceptable. This is always true
  361. * if this filesystem is exported without the subtreecheck
  362. * option. If the filesystem is exported with the subtree
  363. * check option there's a fair chance we need to look at
  364. * the parent directory in the file handle and make sure
  365. * it's connected to the filesystem root.
  366. */
  367. alias = find_acceptable_alias(result, acceptable, context);
  368. if (alias)
  369. return alias;
  370. /*
  371. * Try to extract a dentry for the parent directory from the
  372. * file handle. If this fails we'll have to give up.
  373. */
  374. err = -ESTALE;
  375. if (!nop->fh_to_parent)
  376. goto err_result;
  377. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  378. fh_len, fileid_type);
  379. if (!target_dir)
  380. goto err_result;
  381. err = PTR_ERR(target_dir);
  382. if (IS_ERR(target_dir))
  383. goto err_result;
  384. /*
  385. * And as usual we need to make sure the parent directory is
  386. * connected to the filesystem root. The VFS really doesn't
  387. * like disconnected directories..
  388. */
  389. err = reconnect_path(mnt, target_dir, nbuf);
  390. if (err) {
  391. dput(target_dir);
  392. goto err_result;
  393. }
  394. /*
  395. * Now that we've got both a well-connected parent and a
  396. * dentry for the inode we're after, make sure that our
  397. * inode is actually connected to the parent.
  398. */
  399. err = exportfs_get_name(mnt, target_dir, nbuf, result);
  400. if (!err) {
  401. mutex_lock(&target_dir->d_inode->i_mutex);
  402. nresult = lookup_one_len(nbuf, target_dir,
  403. strlen(nbuf));
  404. mutex_unlock(&target_dir->d_inode->i_mutex);
  405. if (!IS_ERR(nresult)) {
  406. if (nresult->d_inode) {
  407. dput(result);
  408. result = nresult;
  409. } else
  410. dput(nresult);
  411. }
  412. }
  413. /*
  414. * At this point we are done with the parent, but it's pinned
  415. * by the child dentry anyway.
  416. */
  417. dput(target_dir);
  418. /*
  419. * And finally make sure the dentry is actually acceptable
  420. * to NFSD.
  421. */
  422. alias = find_acceptable_alias(result, acceptable, context);
  423. if (!alias) {
  424. err = -EACCES;
  425. goto err_result;
  426. }
  427. return alias;
  428. }
  429. err_result:
  430. dput(result);
  431. return ERR_PTR(err);
  432. }
  433. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  434. MODULE_LICENSE("GPL");