nodelist.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: nodelist.c,v 1.115 2005/11/07 11:14:40 gleixner Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/fs.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/crc32.h>
  19. #include <linux/slab.h>
  20. #include <linux/pagemap.h>
  21. #include "nodelist.h"
  22. static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
  23. struct jffs2_node_frag *this);
  24. void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
  25. {
  26. struct jffs2_full_dirent **prev = list;
  27. dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
  28. while ((*prev) && (*prev)->nhash <= new->nhash) {
  29. if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
  30. /* Duplicate. Free one */
  31. if (new->version < (*prev)->version) {
  32. dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
  33. (*prev)->name, (*prev)->ino);
  34. jffs2_mark_node_obsolete(c, new->raw);
  35. jffs2_free_full_dirent(new);
  36. } else {
  37. dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n",
  38. (*prev)->name, (*prev)->ino);
  39. new->next = (*prev)->next;
  40. jffs2_mark_node_obsolete(c, ((*prev)->raw));
  41. jffs2_free_full_dirent(*prev);
  42. *prev = new;
  43. }
  44. return;
  45. }
  46. prev = &((*prev)->next);
  47. }
  48. new->next = *prev;
  49. *prev = new;
  50. }
  51. void jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
  52. {
  53. struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
  54. dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
  55. /* We know frag->ofs <= size. That's what lookup does for us */
  56. if (frag && frag->ofs != size) {
  57. if (frag->ofs+frag->size > size) {
  58. frag->size = size - frag->ofs;
  59. }
  60. frag = frag_next(frag);
  61. }
  62. while (frag && frag->ofs >= size) {
  63. struct jffs2_node_frag *next = frag_next(frag);
  64. frag_erase(frag, list);
  65. jffs2_obsolete_node_frag(c, frag);
  66. frag = next;
  67. }
  68. if (size == 0)
  69. return;
  70. /*
  71. * If the last fragment starts at the RAM page boundary, it is
  72. * REF_PRISTINE irrespective of its size.
  73. */
  74. frag = frag_last(list);
  75. if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
  76. dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
  77. frag->ofs, frag->ofs + frag->size);
  78. frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
  79. }
  80. }
  81. static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
  82. struct jffs2_node_frag *this)
  83. {
  84. if (this->node) {
  85. this->node->frags--;
  86. if (!this->node->frags) {
  87. /* The node has no valid frags left. It's totally obsoleted */
  88. dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
  89. ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
  90. jffs2_mark_node_obsolete(c, this->node->raw);
  91. jffs2_free_full_dnode(this->node);
  92. } else {
  93. dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
  94. ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
  95. mark_ref_normal(this->node->raw);
  96. }
  97. }
  98. jffs2_free_node_frag(this);
  99. }
  100. static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
  101. {
  102. struct rb_node *parent = &base->rb;
  103. struct rb_node **link = &parent;
  104. dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
  105. while (*link) {
  106. parent = *link;
  107. base = rb_entry(parent, struct jffs2_node_frag, rb);
  108. if (newfrag->ofs > base->ofs)
  109. link = &base->rb.rb_right;
  110. else if (newfrag->ofs < base->ofs)
  111. link = &base->rb.rb_left;
  112. else {
  113. JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
  114. BUG();
  115. }
  116. }
  117. rb_link_node(&newfrag->rb, &base->rb, link);
  118. }
  119. /*
  120. * Allocate and initializes a new fragment.
  121. */
  122. static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
  123. {
  124. struct jffs2_node_frag *newfrag;
  125. newfrag = jffs2_alloc_node_frag();
  126. if (likely(newfrag)) {
  127. newfrag->ofs = ofs;
  128. newfrag->size = size;
  129. newfrag->node = fn;
  130. } else {
  131. JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
  132. }
  133. return newfrag;
  134. }
  135. /*
  136. * Called when there is no overlapping fragment exist. Inserts a hole before the new
  137. * fragment and inserts the new fragment to the fragtree.
  138. */
  139. static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
  140. struct jffs2_node_frag *newfrag,
  141. struct jffs2_node_frag *this, uint32_t lastend)
  142. {
  143. if (lastend < newfrag->node->ofs) {
  144. /* put a hole in before the new fragment */
  145. struct jffs2_node_frag *holefrag;
  146. holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
  147. if (unlikely(!holefrag)) {
  148. jffs2_free_node_frag(newfrag);
  149. return -ENOMEM;
  150. }
  151. if (this) {
  152. /* By definition, the 'this' node has no right-hand child,
  153. because there are no frags with offset greater than it.
  154. So that's where we want to put the hole */
  155. dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
  156. holefrag->ofs, holefrag->ofs + holefrag->size);
  157. rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
  158. } else {
  159. dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
  160. holefrag->ofs, holefrag->ofs + holefrag->size);
  161. rb_link_node(&holefrag->rb, NULL, &root->rb_node);
  162. }
  163. rb_insert_color(&holefrag->rb, root);
  164. this = holefrag;
  165. }
  166. if (this) {
  167. /* By definition, the 'this' node has no right-hand child,
  168. because there are no frags with offset greater than it.
  169. So that's where we want to put new fragment */
  170. dbg_fragtree2("add the new node at the right\n");
  171. rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
  172. } else {
  173. dbg_fragtree2("insert the new node at the root of the tree\n");
  174. rb_link_node(&newfrag->rb, NULL, &root->rb_node);
  175. }
  176. rb_insert_color(&newfrag->rb, root);
  177. return 0;
  178. }
  179. /* Doesn't set inode->i_size */
  180. static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
  181. {
  182. struct jffs2_node_frag *this;
  183. uint32_t lastend;
  184. /* Skip all the nodes which are completed before this one starts */
  185. this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
  186. if (this) {
  187. dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
  188. this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
  189. lastend = this->ofs + this->size;
  190. } else {
  191. dbg_fragtree2("lookup gave no frag\n");
  192. lastend = 0;
  193. }
  194. /* See if we ran off the end of the fragtree */
  195. if (lastend <= newfrag->ofs) {
  196. /* We did */
  197. /* Check if 'this' node was on the same page as the new node.
  198. If so, both 'this' and the new node get marked REF_NORMAL so
  199. the GC can take a look.
  200. */
  201. if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
  202. if (this->node)
  203. mark_ref_normal(this->node->raw);
  204. mark_ref_normal(newfrag->node->raw);
  205. }
  206. return no_overlapping_node(c, root, newfrag, this, lastend);
  207. }
  208. if (this->node)
  209. dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
  210. this->ofs, this->ofs + this->size,
  211. ref_offset(this->node->raw), ref_flags(this->node->raw));
  212. else
  213. dbg_fragtree2("dealing with hole frag %u-%u.\n",
  214. this->ofs, this->ofs + this->size);
  215. /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
  216. * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
  217. */
  218. if (newfrag->ofs > this->ofs) {
  219. /* This node isn't completely obsoleted. The start of it remains valid */
  220. /* Mark the new node and the partially covered node REF_NORMAL -- let
  221. the GC take a look at them */
  222. mark_ref_normal(newfrag->node->raw);
  223. if (this->node)
  224. mark_ref_normal(this->node->raw);
  225. if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
  226. /* The new node splits 'this' frag into two */
  227. struct jffs2_node_frag *newfrag2;
  228. if (this->node)
  229. dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
  230. this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
  231. else
  232. dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
  233. this->ofs, this->ofs+this->size);
  234. /* New second frag pointing to this's node */
  235. newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
  236. this->ofs + this->size - newfrag->ofs - newfrag->size);
  237. if (unlikely(!newfrag2))
  238. return -ENOMEM;
  239. if (this->node)
  240. this->node->frags++;
  241. /* Adjust size of original 'this' */
  242. this->size = newfrag->ofs - this->ofs;
  243. /* Now, we know there's no node with offset
  244. greater than this->ofs but smaller than
  245. newfrag2->ofs or newfrag->ofs, for obvious
  246. reasons. So we can do a tree insert from
  247. 'this' to insert newfrag, and a tree insert
  248. from newfrag to insert newfrag2. */
  249. jffs2_fragtree_insert(newfrag, this);
  250. rb_insert_color(&newfrag->rb, root);
  251. jffs2_fragtree_insert(newfrag2, newfrag);
  252. rb_insert_color(&newfrag2->rb, root);
  253. return 0;
  254. }
  255. /* New node just reduces 'this' frag in size, doesn't split it */
  256. this->size = newfrag->ofs - this->ofs;
  257. /* Again, we know it lives down here in the tree */
  258. jffs2_fragtree_insert(newfrag, this);
  259. rb_insert_color(&newfrag->rb, root);
  260. } else {
  261. /* New frag starts at the same point as 'this' used to. Replace
  262. it in the tree without doing a delete and insertion */
  263. dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
  264. newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
  265. rb_replace_node(&this->rb, &newfrag->rb, root);
  266. if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
  267. dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
  268. jffs2_obsolete_node_frag(c, this);
  269. } else {
  270. this->ofs += newfrag->size;
  271. this->size -= newfrag->size;
  272. jffs2_fragtree_insert(this, newfrag);
  273. rb_insert_color(&this->rb, root);
  274. return 0;
  275. }
  276. }
  277. /* OK, now we have newfrag added in the correct place in the tree, but
  278. frag_next(newfrag) may be a fragment which is overlapped by it
  279. */
  280. while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
  281. /* 'this' frag is obsoleted completely. */
  282. dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
  283. this, this->ofs, this->ofs+this->size);
  284. rb_erase(&this->rb, root);
  285. jffs2_obsolete_node_frag(c, this);
  286. }
  287. /* Now we're pointing at the first frag which isn't totally obsoleted by
  288. the new frag */
  289. if (!this || newfrag->ofs + newfrag->size == this->ofs)
  290. return 0;
  291. /* Still some overlap but we don't need to move it in the tree */
  292. this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
  293. this->ofs = newfrag->ofs + newfrag->size;
  294. /* And mark them REF_NORMAL so the GC takes a look at them */
  295. if (this->node)
  296. mark_ref_normal(this->node->raw);
  297. mark_ref_normal(newfrag->node->raw);
  298. return 0;
  299. }
  300. /*
  301. * Given an inode, probably with existing tree of fragments, add the new node
  302. * to the fragment tree.
  303. */
  304. int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
  305. {
  306. int ret;
  307. struct jffs2_node_frag *newfrag;
  308. if (unlikely(!fn->size))
  309. return 0;
  310. newfrag = new_fragment(fn, fn->ofs, fn->size);
  311. if (unlikely(!newfrag))
  312. return -ENOMEM;
  313. newfrag->node->frags = 1;
  314. dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
  315. fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
  316. ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
  317. if (unlikely(ret))
  318. return ret;
  319. /* If we now share a page with other nodes, mark either previous
  320. or next node REF_NORMAL, as appropriate. */
  321. if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
  322. struct jffs2_node_frag *prev = frag_prev(newfrag);
  323. mark_ref_normal(fn->raw);
  324. /* If we don't start at zero there's _always_ a previous */
  325. if (prev->node)
  326. mark_ref_normal(prev->node->raw);
  327. }
  328. if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
  329. struct jffs2_node_frag *next = frag_next(newfrag);
  330. if (next) {
  331. mark_ref_normal(fn->raw);
  332. if (next->node)
  333. mark_ref_normal(next->node->raw);
  334. }
  335. }
  336. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  337. return 0;
  338. }
  339. /*
  340. * Check the data CRC of the node.
  341. *
  342. * Returns: 0 if the data CRC is correct;
  343. * 1 - if incorrect;
  344. * error code if an error occured.
  345. */
  346. static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
  347. {
  348. struct jffs2_raw_node_ref *ref = tn->fn->raw;
  349. int err = 0, pointed = 0;
  350. struct jffs2_eraseblock *jeb;
  351. unsigned char *buffer;
  352. uint32_t crc, ofs, len;
  353. size_t retlen;
  354. BUG_ON(tn->csize == 0);
  355. if (!jffs2_is_writebuffered(c))
  356. goto adj_acc;
  357. /* Calculate how many bytes were already checked */
  358. ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
  359. len = ofs % c->wbuf_pagesize;
  360. if (likely(len))
  361. len = c->wbuf_pagesize - len;
  362. if (len >= tn->csize) {
  363. dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
  364. ref_offset(ref), tn->csize, ofs);
  365. goto adj_acc;
  366. }
  367. ofs += len;
  368. len = tn->csize - len;
  369. dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
  370. ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
  371. #ifndef __ECOS
  372. /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
  373. * adding and jffs2_flash_read_end() interface. */
  374. if (c->mtd->point) {
  375. err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
  376. if (!err && retlen < tn->csize) {
  377. JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
  378. c->mtd->unpoint(c->mtd, buffer, ofs, len);
  379. } else if (err)
  380. JFFS2_WARNING("MTD point failed: error code %d.\n", err);
  381. else
  382. pointed = 1; /* succefully pointed to device */
  383. }
  384. #endif
  385. if (!pointed) {
  386. buffer = kmalloc(len, GFP_KERNEL);
  387. if (unlikely(!buffer))
  388. return -ENOMEM;
  389. /* TODO: this is very frequent pattern, make it a separate
  390. * routine */
  391. err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
  392. if (err) {
  393. JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
  394. goto free_out;
  395. }
  396. if (retlen != len) {
  397. JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
  398. err = -EIO;
  399. goto free_out;
  400. }
  401. }
  402. /* Continue calculating CRC */
  403. crc = crc32(tn->partial_crc, buffer, len);
  404. if(!pointed)
  405. kfree(buffer);
  406. #ifndef __ECOS
  407. else
  408. c->mtd->unpoint(c->mtd, buffer, ofs, len);
  409. #endif
  410. if (crc != tn->data_crc) {
  411. JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
  412. ofs, tn->data_crc, crc);
  413. return 1;
  414. }
  415. adj_acc:
  416. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  417. len = ref_totlen(c, jeb, ref);
  418. /*
  419. * Mark the node as having been checked and fix the
  420. * accounting accordingly.
  421. */
  422. spin_lock(&c->erase_completion_lock);
  423. jeb->used_size += len;
  424. jeb->unchecked_size -= len;
  425. c->used_size += len;
  426. c->unchecked_size -= len;
  427. spin_unlock(&c->erase_completion_lock);
  428. return 0;
  429. free_out:
  430. if(!pointed)
  431. kfree(buffer);
  432. #ifndef __ECOS
  433. else
  434. c->mtd->unpoint(c->mtd, buffer, ofs, len);
  435. #endif
  436. return err;
  437. }
  438. /*
  439. * Helper function for jffs2_add_older_frag_to_fragtree().
  440. *
  441. * Checks the node if we are in the checking stage.
  442. */
  443. static int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn)
  444. {
  445. int ret;
  446. BUG_ON(ref_obsolete(tn->fn->raw));
  447. /* We only check the data CRC of unchecked nodes */
  448. if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
  449. return 0;
  450. dbg_fragtree2("check node %#04x-%#04x, phys offs %#08x.\n",
  451. tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
  452. ret = check_node_data(c, tn);
  453. if (unlikely(ret < 0)) {
  454. JFFS2_ERROR("check_node_data() returned error: %d.\n",
  455. ret);
  456. } else if (unlikely(ret > 0)) {
  457. dbg_fragtree2("CRC error, mark it obsolete.\n");
  458. jffs2_mark_node_obsolete(c, tn->fn->raw);
  459. }
  460. return ret;
  461. }
  462. /*
  463. * Helper function for jffs2_add_older_frag_to_fragtree().
  464. *
  465. * Called when the new fragment that is being inserted
  466. * splits a hole fragment.
  467. */
  468. static int split_hole(struct jffs2_sb_info *c, struct rb_root *root,
  469. struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole)
  470. {
  471. dbg_fragtree2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
  472. newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size);
  473. if (hole->ofs == newfrag->ofs) {
  474. /*
  475. * Well, the new fragment actually starts at the same offset as
  476. * the hole.
  477. */
  478. if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
  479. /*
  480. * We replace the overlapped left part of the hole by
  481. * the new node.
  482. */
  483. dbg_fragtree2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
  484. newfrag->ofs, newfrag->ofs + newfrag->size);
  485. rb_replace_node(&hole->rb, &newfrag->rb, root);
  486. hole->ofs += newfrag->size;
  487. hole->size -= newfrag->size;
  488. /*
  489. * We know that 'hole' should be the right hand
  490. * fragment.
  491. */
  492. jffs2_fragtree_insert(hole, newfrag);
  493. rb_insert_color(&hole->rb, root);
  494. } else {
  495. /*
  496. * Ah, the new fragment is of the same size as the hole.
  497. * Relace the hole by it.
  498. */
  499. dbg_fragtree2("insert fragment %#04x-%#04x and overwrite hole\n",
  500. newfrag->ofs, newfrag->ofs + newfrag->size);
  501. rb_replace_node(&hole->rb, &newfrag->rb, root);
  502. jffs2_free_node_frag(hole);
  503. }
  504. } else {
  505. /* The new fragment lefts some hole space at the left */
  506. struct jffs2_node_frag * newfrag2 = NULL;
  507. if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
  508. /* The new frag also lefts some space at the right */
  509. newfrag2 = new_fragment(NULL, newfrag->ofs +
  510. newfrag->size, hole->ofs + hole->size
  511. - newfrag->ofs - newfrag->size);
  512. if (unlikely(!newfrag2)) {
  513. jffs2_free_node_frag(newfrag);
  514. return -ENOMEM;
  515. }
  516. }
  517. hole->size = newfrag->ofs - hole->ofs;
  518. dbg_fragtree2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
  519. hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size);
  520. jffs2_fragtree_insert(newfrag, hole);
  521. rb_insert_color(&newfrag->rb, root);
  522. if (newfrag2) {
  523. dbg_fragtree2("left the hole %#04x-%#04x at the right\n",
  524. newfrag2->ofs, newfrag2->ofs + newfrag2->size);
  525. jffs2_fragtree_insert(newfrag2, newfrag);
  526. rb_insert_color(&newfrag2->rb, root);
  527. }
  528. }
  529. return 0;
  530. }
  531. /*
  532. * This function is used when we build inode. It expects the nodes are passed
  533. * in the decreasing version order. The whole point of this is to improve the
  534. * inodes checking on NAND: we check the nodes' data CRC only when they are not
  535. * obsoleted. Previously, add_frag_to_fragtree() function was used and
  536. * nodes were passed to it in the increasing version ordes and CRCs of all
  537. * nodes were checked.
  538. *
  539. * Note: tn->fn->size shouldn't be zero.
  540. *
  541. * Returns 0 if the node was inserted
  542. * 1 if it wasn't inserted (since it is obsolete)
  543. * < 0 an if error occured
  544. */
  545. int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  546. struct jffs2_tmp_dnode_info *tn)
  547. {
  548. struct jffs2_node_frag *this, *newfrag;
  549. uint32_t lastend;
  550. struct jffs2_full_dnode *fn = tn->fn;
  551. struct rb_root *root = &f->fragtree;
  552. uint32_t fn_size = fn->size, fn_ofs = fn->ofs;
  553. int err, checked = 0;
  554. int ref_flag;
  555. dbg_fragtree("insert fragment %#04x-%#04x, ver %u\n", fn_ofs, fn_ofs + fn_size, tn->version);
  556. /* Skip all the nodes which are completed before this one starts */
  557. this = jffs2_lookup_node_frag(root, fn_ofs);
  558. if (this)
  559. dbg_fragtree2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole");
  560. if (this)
  561. lastend = this->ofs + this->size;
  562. else
  563. lastend = 0;
  564. /* Detect the preliminary type of node */
  565. if (fn->size >= PAGE_CACHE_SIZE)
  566. ref_flag = REF_PRISTINE;
  567. else
  568. ref_flag = REF_NORMAL;
  569. /* See if we ran off the end of the root */
  570. if (lastend <= fn_ofs) {
  571. /* We did */
  572. /*
  573. * We are going to insert the new node into the
  574. * fragment tree, so check it.
  575. */
  576. err = check_node(c, f, tn);
  577. if (err != 0)
  578. return err;
  579. fn->frags = 1;
  580. newfrag = new_fragment(fn, fn_ofs, fn_size);
  581. if (unlikely(!newfrag))
  582. return -ENOMEM;
  583. err = no_overlapping_node(c, root, newfrag, this, lastend);
  584. if (unlikely(err != 0)) {
  585. jffs2_free_node_frag(newfrag);
  586. return err;
  587. }
  588. goto out_ok;
  589. }
  590. fn->frags = 0;
  591. while (1) {
  592. /*
  593. * Here we have:
  594. * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
  595. *
  596. * Remember, 'this' has higher version, any non-hole node
  597. * which is already in the fragtree is newer then the newly
  598. * inserted.
  599. */
  600. if (!this->node) {
  601. /*
  602. * 'this' is the hole fragment, so at least the
  603. * beginning of the new fragment is valid.
  604. */
  605. /*
  606. * We are going to insert the new node into the
  607. * fragment tree, so check it.
  608. */
  609. if (!checked) {
  610. err = check_node(c, f, tn);
  611. if (unlikely(err != 0))
  612. return err;
  613. checked = 1;
  614. }
  615. if (this->ofs + this->size >= fn_ofs + fn_size) {
  616. /* We split the hole on two parts */
  617. fn->frags += 1;
  618. newfrag = new_fragment(fn, fn_ofs, fn_size);
  619. if (unlikely(!newfrag))
  620. return -ENOMEM;
  621. err = split_hole(c, root, newfrag, this);
  622. if (unlikely(err))
  623. return err;
  624. goto out_ok;
  625. }
  626. /*
  627. * The beginning of the new fragment is valid since it
  628. * overlaps the hole node.
  629. */
  630. ref_flag = REF_NORMAL;
  631. fn->frags += 1;
  632. newfrag = new_fragment(fn, fn_ofs,
  633. this->ofs + this->size - fn_ofs);
  634. if (unlikely(!newfrag))
  635. return -ENOMEM;
  636. if (fn_ofs == this->ofs) {
  637. /*
  638. * The new node starts at the same offset as
  639. * the hole and supersieds the hole.
  640. */
  641. dbg_fragtree2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
  642. fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
  643. rb_replace_node(&this->rb, &newfrag->rb, root);
  644. jffs2_free_node_frag(this);
  645. } else {
  646. /*
  647. * The hole becomes shorter as its right part
  648. * is supersieded by the new fragment.
  649. */
  650. dbg_fragtree2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
  651. this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size);
  652. dbg_fragtree2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs,
  653. fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
  654. this->size -= newfrag->size;
  655. jffs2_fragtree_insert(newfrag, this);
  656. rb_insert_color(&newfrag->rb, root);
  657. }
  658. fn_ofs += newfrag->size;
  659. fn_size -= newfrag->size;
  660. this = rb_entry(rb_next(&newfrag->rb),
  661. struct jffs2_node_frag, rb);
  662. dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
  663. this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
  664. }
  665. /*
  666. * 'This' node is not the hole so it obsoletes the new fragment
  667. * either fully or partially.
  668. */
  669. if (this->ofs + this->size >= fn_ofs + fn_size) {
  670. /* The new node is obsolete, drop it */
  671. if (fn->frags == 0) {
  672. dbg_fragtree2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size);
  673. ref_flag = REF_OBSOLETE;
  674. }
  675. goto out_ok;
  676. } else {
  677. struct jffs2_node_frag *new_this;
  678. /* 'This' node obsoletes the beginning of the new node */
  679. dbg_fragtree2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size);
  680. ref_flag = REF_NORMAL;
  681. fn_size -= this->ofs + this->size - fn_ofs;
  682. fn_ofs = this->ofs + this->size;
  683. dbg_fragtree2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
  684. new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb);
  685. if (!new_this) {
  686. /*
  687. * There is no next fragment. Add the rest of
  688. * the new node as the right-hand child.
  689. */
  690. if (!checked) {
  691. err = check_node(c, f, tn);
  692. if (unlikely(err != 0))
  693. return err;
  694. checked = 1;
  695. }
  696. fn->frags += 1;
  697. newfrag = new_fragment(fn, fn_ofs, fn_size);
  698. if (unlikely(!newfrag))
  699. return -ENOMEM;
  700. dbg_fragtree2("there are no more fragments, insert %#04x-%#04x\n",
  701. newfrag->ofs, newfrag->ofs + newfrag->size);
  702. rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
  703. rb_insert_color(&newfrag->rb, root);
  704. goto out_ok;
  705. } else {
  706. this = new_this;
  707. dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
  708. this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
  709. }
  710. }
  711. }
  712. out_ok:
  713. BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE);
  714. if (ref_flag == REF_OBSOLETE) {
  715. dbg_fragtree2("the node is obsolete now\n");
  716. /* jffs2_mark_node_obsolete() will adjust space accounting */
  717. jffs2_mark_node_obsolete(c, fn->raw);
  718. return 1;
  719. }
  720. dbg_fragtree2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE");
  721. /* Space accounting was adjusted at check_node_data() */
  722. spin_lock(&c->erase_completion_lock);
  723. fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag;
  724. spin_unlock(&c->erase_completion_lock);
  725. return 0;
  726. }
  727. void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
  728. {
  729. spin_lock(&c->inocache_lock);
  730. ic->state = state;
  731. wake_up(&c->inocache_wq);
  732. spin_unlock(&c->inocache_lock);
  733. }
  734. /* During mount, this needs no locking. During normal operation, its
  735. callers want to do other stuff while still holding the inocache_lock.
  736. Rather than introducing special case get_ino_cache functions or
  737. callbacks, we just let the caller do the locking itself. */
  738. struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  739. {
  740. struct jffs2_inode_cache *ret;
  741. ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
  742. while (ret && ret->ino < ino) {
  743. ret = ret->next;
  744. }
  745. if (ret && ret->ino != ino)
  746. ret = NULL;
  747. return ret;
  748. }
  749. void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
  750. {
  751. struct jffs2_inode_cache **prev;
  752. spin_lock(&c->inocache_lock);
  753. if (!new->ino)
  754. new->ino = ++c->highest_ino;
  755. dbg_inocache("add %p (ino #%u)\n", new, new->ino);
  756. prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
  757. while ((*prev) && (*prev)->ino < new->ino) {
  758. prev = &(*prev)->next;
  759. }
  760. new->next = *prev;
  761. *prev = new;
  762. spin_unlock(&c->inocache_lock);
  763. }
  764. void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
  765. {
  766. struct jffs2_inode_cache **prev;
  767. #ifdef CONFIG_JFFS2_FS_XATTR
  768. BUG_ON(old->xref);
  769. #endif
  770. dbg_inocache("del %p (ino #%u)\n", old, old->ino);
  771. spin_lock(&c->inocache_lock);
  772. prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
  773. while ((*prev) && (*prev)->ino < old->ino) {
  774. prev = &(*prev)->next;
  775. }
  776. if ((*prev) == old) {
  777. *prev = old->next;
  778. }
  779. /* Free it now unless it's in READING or CLEARING state, which
  780. are the transitions upon read_inode() and clear_inode(). The
  781. rest of the time we know nobody else is looking at it, and
  782. if it's held by read_inode() or clear_inode() they'll free it
  783. for themselves. */
  784. if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
  785. jffs2_free_inode_cache(old);
  786. spin_unlock(&c->inocache_lock);
  787. }
  788. void jffs2_free_ino_caches(struct jffs2_sb_info *c)
  789. {
  790. int i;
  791. struct jffs2_inode_cache *this, *next;
  792. for (i=0; i<INOCACHE_HASHSIZE; i++) {
  793. this = c->inocache_list[i];
  794. while (this) {
  795. next = this->next;
  796. jffs2_xattr_free_inode(c, this);
  797. jffs2_free_inode_cache(this);
  798. this = next;
  799. }
  800. c->inocache_list[i] = NULL;
  801. }
  802. }
  803. void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
  804. {
  805. int i;
  806. struct jffs2_raw_node_ref *this, *next;
  807. for (i=0; i<c->nr_blocks; i++) {
  808. this = c->blocks[i].first_node;
  809. while (this) {
  810. if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
  811. next = this[REFS_PER_BLOCK].next_in_ino;
  812. else
  813. next = NULL;
  814. jffs2_free_refblock(this);
  815. this = next;
  816. }
  817. c->blocks[i].first_node = c->blocks[i].last_node = NULL;
  818. }
  819. }
  820. struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
  821. {
  822. /* The common case in lookup is that there will be a node
  823. which precisely matches. So we go looking for that first */
  824. struct rb_node *next;
  825. struct jffs2_node_frag *prev = NULL;
  826. struct jffs2_node_frag *frag = NULL;
  827. dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
  828. next = fragtree->rb_node;
  829. while(next) {
  830. frag = rb_entry(next, struct jffs2_node_frag, rb);
  831. if (frag->ofs + frag->size <= offset) {
  832. /* Remember the closest smaller match on the way down */
  833. if (!prev || frag->ofs > prev->ofs)
  834. prev = frag;
  835. next = frag->rb.rb_right;
  836. } else if (frag->ofs > offset) {
  837. next = frag->rb.rb_left;
  838. } else {
  839. return frag;
  840. }
  841. }
  842. /* Exact match not found. Go back up looking at each parent,
  843. and return the closest smaller one */
  844. if (prev)
  845. dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
  846. prev->ofs, prev->ofs+prev->size);
  847. else
  848. dbg_fragtree2("returning NULL, empty fragtree\n");
  849. return prev;
  850. }
  851. /* Pass 'c' argument to indicate that nodes should be marked obsolete as
  852. they're killed. */
  853. void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
  854. {
  855. struct jffs2_node_frag *frag;
  856. struct jffs2_node_frag *parent;
  857. if (!root->rb_node)
  858. return;
  859. dbg_fragtree("killing\n");
  860. frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
  861. while(frag) {
  862. if (frag->rb.rb_left) {
  863. frag = frag_left(frag);
  864. continue;
  865. }
  866. if (frag->rb.rb_right) {
  867. frag = frag_right(frag);
  868. continue;
  869. }
  870. if (frag->node && !(--frag->node->frags)) {
  871. /* Not a hole, and it's the final remaining frag
  872. of this node. Free the node */
  873. if (c)
  874. jffs2_mark_node_obsolete(c, frag->node->raw);
  875. jffs2_free_full_dnode(frag->node);
  876. }
  877. parent = frag_parent(frag);
  878. if (parent) {
  879. if (frag_left(parent) == frag)
  880. parent->rb.rb_left = NULL;
  881. else
  882. parent->rb.rb_right = NULL;
  883. }
  884. jffs2_free_node_frag(frag);
  885. frag = parent;
  886. cond_resched();
  887. }
  888. }
  889. struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
  890. struct jffs2_eraseblock *jeb,
  891. uint32_t ofs, uint32_t len,
  892. struct jffs2_inode_cache *ic)
  893. {
  894. struct jffs2_raw_node_ref *ref;
  895. BUG_ON(!jeb->allocated_refs);
  896. jeb->allocated_refs--;
  897. ref = jeb->last_node;
  898. dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
  899. ref->next_in_ino);
  900. while (ref->flash_offset != REF_EMPTY_NODE) {
  901. if (ref->flash_offset == REF_LINK_NODE)
  902. ref = ref->next_in_ino;
  903. else
  904. ref++;
  905. }
  906. dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
  907. ref->flash_offset, ofs, ref->next_in_ino, len);
  908. ref->flash_offset = ofs;
  909. if (!jeb->first_node) {
  910. jeb->first_node = ref;
  911. BUG_ON(ref_offset(ref) != jeb->offset);
  912. } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
  913. uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
  914. JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
  915. ref, ref_offset(ref), ref_offset(ref)+len,
  916. ref_offset(jeb->last_node),
  917. ref_offset(jeb->last_node)+last_len);
  918. BUG();
  919. }
  920. jeb->last_node = ref;
  921. if (ic) {
  922. ref->next_in_ino = ic->nodes;
  923. ic->nodes = ref;
  924. } else {
  925. ref->next_in_ino = NULL;
  926. }
  927. switch(ref_flags(ref)) {
  928. case REF_UNCHECKED:
  929. c->unchecked_size += len;
  930. jeb->unchecked_size += len;
  931. break;
  932. case REF_NORMAL:
  933. case REF_PRISTINE:
  934. c->used_size += len;
  935. jeb->used_size += len;
  936. break;
  937. case REF_OBSOLETE:
  938. c->dirty_size += len;
  939. jeb->dirty_size += len;
  940. break;
  941. }
  942. c->free_size -= len;
  943. jeb->free_size -= len;
  944. #ifdef TEST_TOTLEN
  945. /* Set (and test) __totlen field... for now */
  946. ref->__totlen = len;
  947. ref_totlen(c, jeb, ref);
  948. #endif
  949. return ref;
  950. }
  951. /* No locking, no reservation of 'ref'. Do not use on a live file system */
  952. int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  953. uint32_t size)
  954. {
  955. if (!size)
  956. return 0;
  957. if (unlikely(size > jeb->free_size)) {
  958. printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
  959. size, jeb->free_size, jeb->wasted_size);
  960. BUG();
  961. }
  962. /* REF_EMPTY_NODE is !obsolete, so that works OK */
  963. if (jeb->last_node && ref_obsolete(jeb->last_node)) {
  964. #ifdef TEST_TOTLEN
  965. jeb->last_node->__totlen += size;
  966. #endif
  967. c->dirty_size += size;
  968. c->free_size -= size;
  969. jeb->dirty_size += size;
  970. jeb->free_size -= size;
  971. } else {
  972. uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
  973. ofs |= REF_OBSOLETE;
  974. jffs2_link_node_ref(c, jeb, ofs, size, NULL);
  975. }
  976. return 0;
  977. }
  978. /* Calculate totlen from surrounding nodes or eraseblock */
  979. static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
  980. struct jffs2_eraseblock *jeb,
  981. struct jffs2_raw_node_ref *ref)
  982. {
  983. uint32_t ref_end;
  984. struct jffs2_raw_node_ref *next_ref = ref_next(ref);
  985. if (next_ref)
  986. ref_end = ref_offset(next_ref);
  987. else {
  988. if (!jeb)
  989. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  990. /* Last node in block. Use free_space */
  991. if (unlikely(ref != jeb->last_node)) {
  992. printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
  993. ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0);
  994. BUG();
  995. }
  996. ref_end = jeb->offset + c->sector_size - jeb->free_size;
  997. }
  998. return ref_end - ref_offset(ref);
  999. }
  1000. uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  1001. struct jffs2_raw_node_ref *ref)
  1002. {
  1003. uint32_t ret;
  1004. ret = __ref_totlen(c, jeb, ref);
  1005. #ifdef TEST_TOTLEN
  1006. if (unlikely(ret != ref->__totlen)) {
  1007. if (!jeb)
  1008. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  1009. printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
  1010. ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
  1011. ret, ref->__totlen);
  1012. if (ref_next(ref)) {
  1013. printk(KERN_CRIT "next %p (0x%08x-0x%08x)\n", ref_next(ref), ref_offset(ref_next(ref)),
  1014. ref_offset(ref_next(ref))+ref->__totlen);
  1015. } else
  1016. printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node);
  1017. printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size);
  1018. #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
  1019. __jffs2_dbg_dump_node_refs_nolock(c, jeb);
  1020. #endif
  1021. WARN_ON(1);
  1022. ret = ref->__totlen;
  1023. }
  1024. #endif /* TEST_TOTLEN */
  1025. return ret;
  1026. }