expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include <linux/exportfs.h>
  2. #include <linux/fs.h>
  3. #include <linux/file.h>
  4. #include <linux/module.h>
  5. #include <linux/mount.h>
  6. #include <linux/namei.h>
  7. #define dprintk(fmt, args...) do{}while(0)
  8. static int get_name(struct dentry *dentry, char *name,
  9. struct dentry *child);
  10. static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj)
  11. {
  12. struct dentry *result = ERR_PTR(-ESTALE);
  13. if (sb->s_export_op->get_dentry) {
  14. result = sb->s_export_op->get_dentry(sb, obj);
  15. if (!result)
  16. result = ERR_PTR(-ESTALE);
  17. }
  18. return result;
  19. }
  20. static int exportfs_get_name(struct dentry *dir, char *name,
  21. struct dentry *child)
  22. {
  23. struct export_operations *nop = dir->d_sb->s_export_op;
  24. if (nop->get_name)
  25. return nop->get_name(dir, name, child);
  26. else
  27. return get_name(dir, name, child);
  28. }
  29. /*
  30. * Check if the dentry or any of it's aliases is acceptable.
  31. */
  32. static struct dentry *
  33. find_acceptable_alias(struct dentry *result,
  34. int (*acceptable)(void *context, struct dentry *dentry),
  35. void *context)
  36. {
  37. struct dentry *dentry, *toput = NULL;
  38. if (acceptable(context, result))
  39. return result;
  40. spin_lock(&dcache_lock);
  41. list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
  42. dget_locked(dentry);
  43. spin_unlock(&dcache_lock);
  44. if (toput)
  45. dput(toput);
  46. if (dentry != result && acceptable(context, dentry)) {
  47. dput(result);
  48. return dentry;
  49. }
  50. spin_lock(&dcache_lock);
  51. toput = dentry;
  52. }
  53. spin_unlock(&dcache_lock);
  54. if (toput)
  55. dput(toput);
  56. return NULL;
  57. }
  58. /*
  59. * Find root of a disconnected subtree and return a reference to it.
  60. */
  61. static struct dentry *
  62. find_disconnected_root(struct dentry *dentry)
  63. {
  64. dget(dentry);
  65. spin_lock(&dentry->d_lock);
  66. while (!IS_ROOT(dentry) &&
  67. (dentry->d_parent->d_flags & DCACHE_DISCONNECTED)) {
  68. struct dentry *parent = dentry->d_parent;
  69. dget(parent);
  70. spin_unlock(&dentry->d_lock);
  71. dput(dentry);
  72. dentry = parent;
  73. spin_lock(&dentry->d_lock);
  74. }
  75. spin_unlock(&dentry->d_lock);
  76. return dentry;
  77. }
  78. /**
  79. * find_exported_dentry - helper routine to implement export_operations->decode_fh
  80. * @sb: The &super_block identifying the filesystem
  81. * @obj: An opaque identifier of the object to be found - passed to
  82. * get_inode
  83. * @parent: An optional opqaue identifier of the parent of the object.
  84. * @acceptable: A function used to test possible &dentries to see if they are
  85. * acceptable
  86. * @context: A parameter to @acceptable so that it knows on what basis to
  87. * judge.
  88. *
  89. * find_exported_dentry is the central helper routine to enable file systems
  90. * to provide the decode_fh() export_operation. It's main task is to take
  91. * an &inode, find or create an appropriate &dentry structure, and possibly
  92. * splice this into the dcache in the correct place.
  93. *
  94. * The decode_fh() operation provided by the filesystem should call
  95. * find_exported_dentry() with the same parameters that it received except
  96. * that instead of the file handle fragment, pointers to opaque identifiers
  97. * for the object and optionally its parent are passed. The default decode_fh
  98. * routine passes one pointer to the start of the filehandle fragment, and
  99. * one 8 bytes into the fragment. It is expected that most filesystems will
  100. * take this approach, though the offset to the parent identifier may well be
  101. * different.
  102. *
  103. * find_exported_dentry() will call get_dentry to get an dentry pointer from
  104. * the file system. If any &dentry in the d_alias list is acceptable, it will
  105. * be returned. Otherwise find_exported_dentry() will attempt to splice a new
  106. * &dentry into the dcache using get_name() and get_parent() to find the
  107. * appropriate place.
  108. */
  109. struct dentry *
  110. find_exported_dentry(struct super_block *sb, void *obj, void *parent,
  111. int (*acceptable)(void *context, struct dentry *de),
  112. void *context)
  113. {
  114. struct dentry *result = NULL;
  115. struct dentry *target_dir;
  116. int err = -ESTALE;
  117. struct export_operations *nops = sb->s_export_op;
  118. struct dentry *alias;
  119. int noprogress;
  120. char nbuf[NAME_MAX+1];
  121. /*
  122. * Attempt to find the inode.
  123. */
  124. result = exportfs_get_dentry(sb, obj);
  125. if (IS_ERR(result))
  126. return result;
  127. if (S_ISDIR(result->d_inode->i_mode)) {
  128. if (!(result->d_flags & DCACHE_DISCONNECTED)) {
  129. if (acceptable(context, result))
  130. return result;
  131. err = -EACCES;
  132. goto err_result;
  133. }
  134. target_dir = dget(result);
  135. } else {
  136. alias = find_acceptable_alias(result, acceptable, context);
  137. if (alias)
  138. return alias;
  139. if (parent == NULL)
  140. goto err_result;
  141. target_dir = exportfs_get_dentry(sb,parent);
  142. if (IS_ERR(target_dir)) {
  143. err = PTR_ERR(target_dir);
  144. goto err_result;
  145. }
  146. }
  147. /*
  148. * Now we need to make sure that target_dir is properly connected.
  149. * It may already be, as the flag isn't always updated when connection
  150. * happens.
  151. * So, we walk up parent links until we find a connected directory,
  152. * or we run out of directories. Then we find the parent, find
  153. * the name of the child in that parent, and do a lookup.
  154. * This should connect the child into the parent
  155. * We then repeat.
  156. */
  157. /* it is possible that a confused file system might not let us complete
  158. * the path to the root. For example, if get_parent returns a directory
  159. * in which we cannot find a name for the child. While this implies a
  160. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  161. * the noprogress counter. If we go through the loop 10 times (2 is
  162. * probably enough) without getting anywhere, we just give up
  163. */
  164. noprogress = 0;
  165. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  166. struct dentry *pd = find_disconnected_root(target_dir);
  167. if (!IS_ROOT(pd)) {
  168. /* must have found a connected parent - great */
  169. spin_lock(&pd->d_lock);
  170. pd->d_flags &= ~DCACHE_DISCONNECTED;
  171. spin_unlock(&pd->d_lock);
  172. noprogress = 0;
  173. } else if (pd == sb->s_root) {
  174. printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
  175. spin_lock(&pd->d_lock);
  176. pd->d_flags &= ~DCACHE_DISCONNECTED;
  177. spin_unlock(&pd->d_lock);
  178. noprogress = 0;
  179. } else {
  180. /*
  181. * We have hit the top of a disconnected path, try to
  182. * find parent and connect.
  183. *
  184. * Racing with some other process renaming a directory
  185. * isn't much of a problem here. If someone renames
  186. * the directory, it will end up properly connected,
  187. * which is what we want
  188. *
  189. * Getting the parent can't be supported generically,
  190. * the locking is too icky.
  191. *
  192. * Instead we just return EACCES. If server reboots
  193. * or inodes get flushed, you lose
  194. */
  195. struct dentry *ppd = ERR_PTR(-EACCES);
  196. struct dentry *npd;
  197. mutex_lock(&pd->d_inode->i_mutex);
  198. if (nops->get_parent)
  199. ppd = nops->get_parent(pd);
  200. mutex_unlock(&pd->d_inode->i_mutex);
  201. if (IS_ERR(ppd)) {
  202. err = PTR_ERR(ppd);
  203. dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
  204. pd->d_inode->i_ino, err);
  205. dput(pd);
  206. break;
  207. }
  208. dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
  209. err = exportfs_get_name(ppd, nbuf, pd);
  210. if (err) {
  211. dput(ppd);
  212. dput(pd);
  213. if (err == -ENOENT)
  214. /* some race between get_parent and
  215. * get_name? just try again
  216. */
  217. continue;
  218. break;
  219. }
  220. dprintk("find_exported_dentry: found name: %s\n", nbuf);
  221. mutex_lock(&ppd->d_inode->i_mutex);
  222. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  223. mutex_unlock(&ppd->d_inode->i_mutex);
  224. if (IS_ERR(npd)) {
  225. err = PTR_ERR(npd);
  226. dprintk("find_exported_dentry: lookup failed: %d\n", err);
  227. dput(ppd);
  228. dput(pd);
  229. break;
  230. }
  231. /* we didn't really want npd, we really wanted
  232. * a side-effect of the lookup.
  233. * hopefully, npd == pd, though it isn't really
  234. * a problem if it isn't
  235. */
  236. if (npd == pd)
  237. noprogress = 0;
  238. else
  239. printk("find_exported_dentry: npd != pd\n");
  240. dput(npd);
  241. dput(ppd);
  242. if (IS_ROOT(pd)) {
  243. /* something went wrong, we have to give up */
  244. dput(pd);
  245. break;
  246. }
  247. }
  248. dput(pd);
  249. }
  250. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  251. /* something went wrong - oh-well */
  252. if (!err)
  253. err = -ESTALE;
  254. goto err_target;
  255. }
  256. /* if we weren't after a directory, have one more step to go */
  257. if (result != target_dir) {
  258. struct dentry *nresult;
  259. err = exportfs_get_name(target_dir, nbuf, result);
  260. if (!err) {
  261. mutex_lock(&target_dir->d_inode->i_mutex);
  262. nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
  263. mutex_unlock(&target_dir->d_inode->i_mutex);
  264. if (!IS_ERR(nresult)) {
  265. if (nresult->d_inode) {
  266. dput(result);
  267. result = nresult;
  268. } else
  269. dput(nresult);
  270. }
  271. }
  272. }
  273. dput(target_dir);
  274. alias = find_acceptable_alias(result, acceptable, context);
  275. if (alias)
  276. return alias;
  277. /* drat - I just cannot find anything acceptable */
  278. dput(result);
  279. /* It might be justifiable to return ESTALE here,
  280. * but the filehandle at-least looks reasonable good
  281. * and it just be a permission problem, so returning
  282. * -EACCESS is safer
  283. */
  284. return ERR_PTR(-EACCES);
  285. err_target:
  286. dput(target_dir);
  287. err_result:
  288. dput(result);
  289. return ERR_PTR(err);
  290. }
  291. struct getdents_callback {
  292. char *name; /* name that was found. It already points to a
  293. buffer NAME_MAX+1 is size */
  294. unsigned long ino; /* the inum we are looking for */
  295. int found; /* inode matched? */
  296. int sequence; /* sequence counter */
  297. };
  298. /*
  299. * A rather strange filldir function to capture
  300. * the name matching the specified inode number.
  301. */
  302. static int filldir_one(void * __buf, const char * name, int len,
  303. loff_t pos, u64 ino, unsigned int d_type)
  304. {
  305. struct getdents_callback *buf = __buf;
  306. int result = 0;
  307. buf->sequence++;
  308. if (buf->ino == ino) {
  309. memcpy(buf->name, name, len);
  310. buf->name[len] = '\0';
  311. buf->found = 1;
  312. result = -1;
  313. }
  314. return result;
  315. }
  316. /**
  317. * get_name - default export_operations->get_name function
  318. * @dentry: the directory in which to find a name
  319. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  320. * @child: the dentry for the child directory.
  321. *
  322. * calls readdir on the parent until it finds an entry with
  323. * the same inode number as the child, and returns that.
  324. */
  325. static int get_name(struct dentry *dentry, char *name,
  326. struct dentry *child)
  327. {
  328. struct inode *dir = dentry->d_inode;
  329. int error;
  330. struct file *file;
  331. struct getdents_callback buffer;
  332. error = -ENOTDIR;
  333. if (!dir || !S_ISDIR(dir->i_mode))
  334. goto out;
  335. error = -EINVAL;
  336. if (!dir->i_fop)
  337. goto out;
  338. /*
  339. * Open the directory ...
  340. */
  341. file = dentry_open(dget(dentry), NULL, O_RDONLY);
  342. error = PTR_ERR(file);
  343. if (IS_ERR(file))
  344. goto out;
  345. error = -EINVAL;
  346. if (!file->f_op->readdir)
  347. goto out_close;
  348. buffer.name = name;
  349. buffer.ino = child->d_inode->i_ino;
  350. buffer.found = 0;
  351. buffer.sequence = 0;
  352. while (1) {
  353. int old_seq = buffer.sequence;
  354. error = vfs_readdir(file, filldir_one, &buffer);
  355. if (error < 0)
  356. break;
  357. error = 0;
  358. if (buffer.found)
  359. break;
  360. error = -ENOENT;
  361. if (old_seq == buffer.sequence)
  362. break;
  363. }
  364. out_close:
  365. fput(file);
  366. out:
  367. return error;
  368. }
  369. /**
  370. * export_encode_fh - default export_operations->encode_fh function
  371. * @dentry: the dentry to encode
  372. * @fh: where to store the file handle fragment
  373. * @max_len: maximum length to store there
  374. * @connectable: whether to store parent information
  375. *
  376. * This default encode_fh function assumes that the 32 inode number
  377. * is suitable for locating an inode, and that the generation number
  378. * can be used to check that it is still valid. It places them in the
  379. * filehandle fragment where export_decode_fh expects to find them.
  380. */
  381. static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
  382. int connectable)
  383. {
  384. struct inode * inode = dentry->d_inode;
  385. int len = *max_len;
  386. int type = 1;
  387. if (len < 2 || (connectable && len < 4))
  388. return 255;
  389. len = 2;
  390. fh[0] = inode->i_ino;
  391. fh[1] = inode->i_generation;
  392. if (connectable && !S_ISDIR(inode->i_mode)) {
  393. struct inode *parent;
  394. spin_lock(&dentry->d_lock);
  395. parent = dentry->d_parent->d_inode;
  396. fh[2] = parent->i_ino;
  397. fh[3] = parent->i_generation;
  398. spin_unlock(&dentry->d_lock);
  399. len = 4;
  400. type = 2;
  401. }
  402. *max_len = len;
  403. return type;
  404. }
  405. /**
  406. * export_decode_fh - default export_operations->decode_fh function
  407. * @sb: The superblock
  408. * @fh: pointer to the file handle fragment
  409. * @fh_len: length of file handle fragment
  410. * @acceptable: function for testing acceptability of dentrys
  411. * @context: context for @acceptable
  412. *
  413. * This is the default decode_fh() function.
  414. * a fileid_type of 1 indicates that the filehandlefragment
  415. * just contains an object identifier understood by get_dentry.
  416. * a fileid_type of 2 says that there is also a directory
  417. * identifier 8 bytes in to the filehandlefragement.
  418. */
  419. static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
  420. int fileid_type,
  421. int (*acceptable)(void *context, struct dentry *de),
  422. void *context)
  423. {
  424. __u32 parent[2];
  425. parent[0] = parent[1] = 0;
  426. if (fh_len < 2 || fileid_type > 2)
  427. return NULL;
  428. if (fileid_type == 2) {
  429. if (fh_len > 2) parent[0] = fh[2];
  430. if (fh_len > 3) parent[1] = fh[3];
  431. }
  432. return find_exported_dentry(sb, fh, parent,
  433. acceptable, context);
  434. }
  435. int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
  436. int connectable)
  437. {
  438. struct export_operations *nop = dentry->d_sb->s_export_op;
  439. int error;
  440. if (nop->encode_fh)
  441. error = nop->encode_fh(dentry, fh, max_len, connectable);
  442. else
  443. error = export_encode_fh(dentry, fh, max_len, connectable);
  444. return error;
  445. }
  446. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  447. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
  448. int fileid_type, int (*acceptable)(void *, struct dentry *),
  449. void *context)
  450. {
  451. struct export_operations *nop = mnt->mnt_sb->s_export_op;
  452. struct dentry *result;
  453. if (nop->decode_fh) {
  454. result = nop->decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
  455. acceptable, context);
  456. } else {
  457. result = export_decode_fh(mnt->mnt_sb, fh, fh_len, fileid_type,
  458. acceptable, context);
  459. }
  460. return result;
  461. }
  462. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  463. EXPORT_SYMBOL(find_exported_dentry);
  464. MODULE_LICENSE("GPL");