expfs.c 12 KB

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