expfs.c 12 KB

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