expfs.c 15 KB

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