expfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. struct inode *inode;
  40. if (acceptable(context, result))
  41. return result;
  42. inode = result->d_inode;
  43. spin_lock(&inode->i_lock);
  44. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  45. dget(dentry);
  46. spin_unlock(&inode->i_lock);
  47. if (toput)
  48. dput(toput);
  49. if (dentry != result && acceptable(context, dentry)) {
  50. dput(result);
  51. return dentry;
  52. }
  53. spin_lock(&inode->i_lock);
  54. toput = dentry;
  55. }
  56. spin_unlock(&inode->i_lock);
  57. if (toput)
  58. dput(toput);
  59. return NULL;
  60. }
  61. /*
  62. * Find root of a disconnected subtree and return a reference to it.
  63. */
  64. static struct dentry *
  65. find_disconnected_root(struct dentry *dentry)
  66. {
  67. dget(dentry);
  68. while (!IS_ROOT(dentry)) {
  69. struct dentry *parent = dget_parent(dentry);
  70. if (!(parent->d_flags & DCACHE_DISCONNECTED)) {
  71. dput(parent);
  72. break;
  73. }
  74. dput(dentry);
  75. dentry = parent;
  76. }
  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 (connectable && (len < 4)) {
  291. *max_len = 4;
  292. return 255;
  293. } else if (len < 2) {
  294. *max_len = 2;
  295. return 255;
  296. }
  297. len = 2;
  298. fid->i32.ino = inode->i_ino;
  299. fid->i32.gen = inode->i_generation;
  300. if (connectable && !S_ISDIR(inode->i_mode)) {
  301. struct inode *parent;
  302. spin_lock(&dentry->d_lock);
  303. parent = dentry->d_parent->d_inode;
  304. fid->i32.parent_ino = parent->i_ino;
  305. fid->i32.parent_gen = parent->i_generation;
  306. spin_unlock(&dentry->d_lock);
  307. len = 4;
  308. type = FILEID_INO32_GEN_PARENT;
  309. }
  310. *max_len = len;
  311. return type;
  312. }
  313. int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
  314. int connectable)
  315. {
  316. const struct export_operations *nop = dentry->d_sb->s_export_op;
  317. int error;
  318. if (nop->encode_fh)
  319. error = nop->encode_fh(dentry, fid->raw, max_len, connectable);
  320. else
  321. error = export_encode_fh(dentry, fid, max_len, connectable);
  322. return error;
  323. }
  324. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  325. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
  326. int fh_len, int fileid_type,
  327. int (*acceptable)(void *, struct dentry *), void *context)
  328. {
  329. const struct export_operations *nop = mnt->mnt_sb->s_export_op;
  330. struct dentry *result, *alias;
  331. char nbuf[NAME_MAX+1];
  332. int err;
  333. /*
  334. * Try to get any dentry for the given file handle from the filesystem.
  335. */
  336. if (!nop || !nop->fh_to_dentry)
  337. return ERR_PTR(-ESTALE);
  338. result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
  339. if (!result)
  340. result = ERR_PTR(-ESTALE);
  341. if (IS_ERR(result))
  342. return result;
  343. if (S_ISDIR(result->d_inode->i_mode)) {
  344. /*
  345. * This request is for a directory.
  346. *
  347. * On the positive side there is only one dentry for each
  348. * directory inode. On the negative side this implies that we
  349. * to ensure our dentry is connected all the way up to the
  350. * filesystem root.
  351. */
  352. if (result->d_flags & DCACHE_DISCONNECTED) {
  353. err = reconnect_path(mnt, result, nbuf);
  354. if (err)
  355. goto err_result;
  356. }
  357. if (!acceptable(context, result)) {
  358. err = -EACCES;
  359. goto err_result;
  360. }
  361. return result;
  362. } else {
  363. /*
  364. * It's not a directory. Life is a little more complicated.
  365. */
  366. struct dentry *target_dir, *nresult;
  367. /*
  368. * See if either the dentry we just got from the filesystem
  369. * or any alias for it is acceptable. This is always true
  370. * if this filesystem is exported without the subtreecheck
  371. * option. If the filesystem is exported with the subtree
  372. * check option there's a fair chance we need to look at
  373. * the parent directory in the file handle and make sure
  374. * it's connected to the filesystem root.
  375. */
  376. alias = find_acceptable_alias(result, acceptable, context);
  377. if (alias)
  378. return alias;
  379. /*
  380. * Try to extract a dentry for the parent directory from the
  381. * file handle. If this fails we'll have to give up.
  382. */
  383. err = -ESTALE;
  384. if (!nop->fh_to_parent)
  385. goto err_result;
  386. target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
  387. fh_len, fileid_type);
  388. if (!target_dir)
  389. goto err_result;
  390. err = PTR_ERR(target_dir);
  391. if (IS_ERR(target_dir))
  392. goto err_result;
  393. /*
  394. * And as usual we need to make sure the parent directory is
  395. * connected to the filesystem root. The VFS really doesn't
  396. * like disconnected directories..
  397. */
  398. err = reconnect_path(mnt, target_dir, nbuf);
  399. if (err) {
  400. dput(target_dir);
  401. goto err_result;
  402. }
  403. /*
  404. * Now that we've got both a well-connected parent and a
  405. * dentry for the inode we're after, make sure that our
  406. * inode is actually connected to the parent.
  407. */
  408. err = exportfs_get_name(mnt, target_dir, nbuf, result);
  409. if (!err) {
  410. mutex_lock(&target_dir->d_inode->i_mutex);
  411. nresult = lookup_one_len(nbuf, target_dir,
  412. strlen(nbuf));
  413. mutex_unlock(&target_dir->d_inode->i_mutex);
  414. if (!IS_ERR(nresult)) {
  415. if (nresult->d_inode) {
  416. dput(result);
  417. result = nresult;
  418. } else
  419. dput(nresult);
  420. }
  421. }
  422. /*
  423. * At this point we are done with the parent, but it's pinned
  424. * by the child dentry anyway.
  425. */
  426. dput(target_dir);
  427. /*
  428. * And finally make sure the dentry is actually acceptable
  429. * to NFSD.
  430. */
  431. alias = find_acceptable_alias(result, acceptable, context);
  432. if (!alias) {
  433. err = -EACCES;
  434. goto err_result;
  435. }
  436. return alias;
  437. }
  438. err_result:
  439. dput(result);
  440. return ERR_PTR(err);
  441. }
  442. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  443. MODULE_LICENSE("GPL");