nodelist.c 30 KB

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