nodelist.c 29 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.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. 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. 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. 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. 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. 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->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
  74. 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. 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. 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. 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 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
  230. this->ofs, this->ofs+this->size);
  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. 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. 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. 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. 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, len;
  350. size_t retlen;
  351. BUG_ON(tn->csize == 0);
  352. if (!jffs2_is_writebuffered(c))
  353. goto adj_acc;
  354. /* Calculate how many bytes were already checked */
  355. ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
  356. len = ofs % c->wbuf_pagesize;
  357. if (likely(len))
  358. len = c->wbuf_pagesize - len;
  359. if (len >= tn->csize) {
  360. dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
  361. ref_offset(ref), tn->csize, ofs);
  362. goto adj_acc;
  363. }
  364. ofs += len;
  365. len = tn->csize - len;
  366. 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",
  367. ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
  368. #ifndef __ECOS
  369. /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
  370. * adding and jffs2_flash_read_end() interface. */
  371. if (c->mtd->point) {
  372. err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
  373. if (!err && retlen < tn->csize) {
  374. JFFS2_WARNING("MTD point returned len too short: %zu "
  375. "instead of %u.\n", retlen, tn->csize);
  376. c->mtd->unpoint(c->mtd, buffer, ofs, len);
  377. } else if (err)
  378. JFFS2_WARNING("MTD point failed: error code %d.\n", err);
  379. else
  380. pointed = 1; /* succefully pointed to device */
  381. }
  382. #endif
  383. if (!pointed) {
  384. buffer = kmalloc(len, GFP_KERNEL);
  385. if (unlikely(!buffer))
  386. return -ENOMEM;
  387. /* TODO: this is very frequent pattern, make it a separate
  388. * routine */
  389. err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
  390. if (err) {
  391. JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
  392. goto free_out;
  393. }
  394. if (retlen != len) {
  395. JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n",
  396. ofs, retlen, len);
  397. err = -EIO;
  398. goto free_out;
  399. }
  400. }
  401. /* Continue calculating CRC */
  402. crc = crc32(tn->partial_crc, buffer, len);
  403. if(!pointed)
  404. kfree(buffer);
  405. #ifndef __ECOS
  406. else
  407. c->mtd->unpoint(c->mtd, buffer, ofs, len);
  408. #endif
  409. if (crc != tn->data_crc) {
  410. JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
  411. ofs, tn->data_crc, crc);
  412. return 1;
  413. }
  414. adj_acc:
  415. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  416. len = ref_totlen(c, jeb, ref);
  417. /*
  418. * Mark the node as having been checked and fix the
  419. * accounting accordingly.
  420. */
  421. spin_lock(&c->erase_completion_lock);
  422. jeb->used_size += len;
  423. jeb->unchecked_size -= len;
  424. c->used_size += len;
  425. c->unchecked_size -= len;
  426. spin_unlock(&c->erase_completion_lock);
  427. return 0;
  428. free_out:
  429. if(!pointed)
  430. kfree(buffer);
  431. #ifndef __ECOS
  432. else
  433. c->mtd->unpoint(c->mtd, buffer, ofs, len);
  434. #endif
  435. return err;
  436. }
  437. /*
  438. * Helper function for jffs2_add_older_frag_to_fragtree().
  439. *
  440. * Checks the node if we are in the checking stage.
  441. */
  442. static int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn)
  443. {
  444. int ret;
  445. BUG_ON(ref_obsolete(tn->fn->raw));
  446. /* We only check the data CRC of unchecked nodes */
  447. if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
  448. return 0;
  449. dbg_fragtree2("check node %#04x-%#04x, phys offs %#08x.\n",
  450. tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
  451. ret = check_node_data(c, tn);
  452. if (unlikely(ret < 0)) {
  453. JFFS2_ERROR("check_node_data() returned error: %d.\n",
  454. ret);
  455. } else if (unlikely(ret > 0)) {
  456. dbg_fragtree2("CRC error, mark it obsolete.\n");
  457. jffs2_mark_node_obsolete(c, tn->fn->raw);
  458. }
  459. return ret;
  460. }
  461. /*
  462. * Helper function for jffs2_add_older_frag_to_fragtree().
  463. *
  464. * Called when the new fragment that is being inserted
  465. * splits a hole fragment.
  466. */
  467. static int split_hole(struct jffs2_sb_info *c, struct rb_root *root,
  468. struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole)
  469. {
  470. dbg_fragtree2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
  471. newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size);
  472. if (hole->ofs == newfrag->ofs) {
  473. /*
  474. * Well, the new fragment actually starts at the same offset as
  475. * the hole.
  476. */
  477. if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
  478. /*
  479. * We replace the overlapped left part of the hole by
  480. * the new node.
  481. */
  482. dbg_fragtree2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
  483. newfrag->ofs, newfrag->ofs + newfrag->size);
  484. rb_replace_node(&hole->rb, &newfrag->rb, root);
  485. hole->ofs += newfrag->size;
  486. hole->size -= newfrag->size;
  487. /*
  488. * We know that 'hole' should be the right hand
  489. * fragment.
  490. */
  491. jffs2_fragtree_insert(hole, newfrag);
  492. rb_insert_color(&hole->rb, root);
  493. } else {
  494. /*
  495. * Ah, the new fragment is of the same size as the hole.
  496. * Relace the hole by it.
  497. */
  498. dbg_fragtree2("insert fragment %#04x-%#04x and overwrite hole\n",
  499. newfrag->ofs, newfrag->ofs + newfrag->size);
  500. rb_replace_node(&hole->rb, &newfrag->rb, root);
  501. jffs2_free_node_frag(hole);
  502. }
  503. } else {
  504. /* The new fragment lefts some hole space at the left */
  505. struct jffs2_node_frag * newfrag2 = NULL;
  506. if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
  507. /* The new frag also lefts some space at the right */
  508. newfrag2 = new_fragment(NULL, newfrag->ofs +
  509. newfrag->size, hole->ofs + hole->size
  510. - newfrag->ofs - newfrag->size);
  511. if (unlikely(!newfrag2)) {
  512. jffs2_free_node_frag(newfrag);
  513. return -ENOMEM;
  514. }
  515. }
  516. hole->size = newfrag->ofs - hole->ofs;
  517. dbg_fragtree2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
  518. hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size);
  519. jffs2_fragtree_insert(newfrag, hole);
  520. rb_insert_color(&newfrag->rb, root);
  521. if (newfrag2) {
  522. dbg_fragtree2("left the hole %#04x-%#04x at the right\n",
  523. newfrag2->ofs, newfrag2->ofs + newfrag2->size);
  524. jffs2_fragtree_insert(newfrag2, newfrag);
  525. rb_insert_color(&newfrag2->rb, root);
  526. }
  527. }
  528. return 0;
  529. }
  530. /*
  531. * This function is used when we build inode. It expects the nodes are passed
  532. * in the decreasing version order. The whole point of this is to improve the
  533. * inodes checking on NAND: we check the nodes' data CRC only when they are not
  534. * obsoleted. Previously, add_frag_to_fragtree() function was used and
  535. * nodes were passed to it in the increasing version ordes and CRCs of all
  536. * nodes were checked.
  537. *
  538. * Note: tn->fn->size shouldn't be zero.
  539. *
  540. * Returns 0 if the node was inserted
  541. * 1 if it wasn't inserted (since it is obsolete)
  542. * < 0 an if error occured
  543. */
  544. int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
  545. struct jffs2_tmp_dnode_info *tn)
  546. {
  547. struct jffs2_node_frag *this, *newfrag;
  548. uint32_t lastend;
  549. struct jffs2_full_dnode *fn = tn->fn;
  550. struct rb_root *root = &f->fragtree;
  551. uint32_t fn_size = fn->size, fn_ofs = fn->ofs;
  552. int err, checked = 0;
  553. int ref_flag;
  554. dbg_fragtree("insert fragment %#04x-%#04x, ver %u\n", fn_ofs, fn_ofs + fn_size, tn->version);
  555. /* Skip all the nodes which are completed before this one starts */
  556. this = jffs2_lookup_node_frag(root, fn_ofs);
  557. if (this)
  558. dbg_fragtree2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole");
  559. if (this)
  560. lastend = this->ofs + this->size;
  561. else
  562. lastend = 0;
  563. /* Detect the preliminary type of node */
  564. if (fn->size >= PAGE_CACHE_SIZE)
  565. ref_flag = REF_PRISTINE;
  566. else
  567. ref_flag = REF_NORMAL;
  568. /* See if we ran off the end of the root */
  569. if (lastend <= fn_ofs) {
  570. /* We did */
  571. /*
  572. * We are going to insert the new node into the
  573. * fragment tree, so check it.
  574. */
  575. err = check_node(c, f, tn);
  576. if (err != 0)
  577. return err;
  578. fn->frags = 1;
  579. newfrag = new_fragment(fn, fn_ofs, fn_size);
  580. if (unlikely(!newfrag))
  581. return -ENOMEM;
  582. err = no_overlapping_node(c, root, newfrag, this, lastend);
  583. if (unlikely(err != 0)) {
  584. jffs2_free_node_frag(newfrag);
  585. return err;
  586. }
  587. goto out_ok;
  588. }
  589. fn->frags = 0;
  590. while (1) {
  591. /*
  592. * Here we have:
  593. * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
  594. *
  595. * Remember, 'this' has higher version, any non-hole node
  596. * which is already in the fragtree is newer then the newly
  597. * inserted.
  598. */
  599. if (!this->node) {
  600. /*
  601. * 'this' is the hole fragment, so at least the
  602. * beginning of the new fragment is valid.
  603. */
  604. /*
  605. * We are going to insert the new node into the
  606. * fragment tree, so check it.
  607. */
  608. if (!checked) {
  609. err = check_node(c, f, tn);
  610. if (unlikely(err != 0))
  611. return err;
  612. checked = 1;
  613. }
  614. if (this->ofs + this->size >= fn_ofs + fn_size) {
  615. /* We split the hole on two parts */
  616. fn->frags += 1;
  617. newfrag = new_fragment(fn, fn_ofs, fn_size);
  618. if (unlikely(!newfrag))
  619. return -ENOMEM;
  620. err = split_hole(c, root, newfrag, this);
  621. if (unlikely(err))
  622. return err;
  623. goto out_ok;
  624. }
  625. /*
  626. * The beginning of the new fragment is valid since it
  627. * overlaps the hole node.
  628. */
  629. ref_flag = REF_NORMAL;
  630. fn->frags += 1;
  631. newfrag = new_fragment(fn, fn_ofs,
  632. this->ofs + this->size - fn_ofs);
  633. if (unlikely(!newfrag))
  634. return -ENOMEM;
  635. if (fn_ofs == this->ofs) {
  636. /*
  637. * The new node starts at the same offset as
  638. * the hole and supersieds the hole.
  639. */
  640. dbg_fragtree2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
  641. fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
  642. rb_replace_node(&this->rb, &newfrag->rb, root);
  643. jffs2_free_node_frag(this);
  644. } else {
  645. /*
  646. * The hole becomes shorter as its right part
  647. * is supersieded by the new fragment.
  648. */
  649. dbg_fragtree2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
  650. this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size);
  651. dbg_fragtree2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs,
  652. fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
  653. this->size -= newfrag->size;
  654. jffs2_fragtree_insert(newfrag, this);
  655. rb_insert_color(&newfrag->rb, root);
  656. }
  657. fn_ofs += newfrag->size;
  658. fn_size -= newfrag->size;
  659. this = rb_entry(rb_next(&newfrag->rb),
  660. struct jffs2_node_frag, rb);
  661. dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
  662. this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
  663. }
  664. /*
  665. * 'This' node is not the hole so it obsoletes the new fragment
  666. * either fully or partially.
  667. */
  668. if (this->ofs + this->size >= fn_ofs + fn_size) {
  669. /* The new node is obsolete, drop it */
  670. if (fn->frags == 0) {
  671. dbg_fragtree2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size);
  672. ref_flag = REF_OBSOLETE;
  673. }
  674. goto out_ok;
  675. } else {
  676. struct jffs2_node_frag *new_this;
  677. /* 'This' node obsoletes the beginning of the new node */
  678. dbg_fragtree2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size);
  679. ref_flag = REF_NORMAL;
  680. fn_size -= this->ofs + this->size - fn_ofs;
  681. fn_ofs = this->ofs + this->size;
  682. dbg_fragtree2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
  683. new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb);
  684. if (!new_this) {
  685. /*
  686. * There is no next fragment. Add the rest of
  687. * the new node as the right-hand child.
  688. */
  689. if (!checked) {
  690. err = check_node(c, f, tn);
  691. if (unlikely(err != 0))
  692. return err;
  693. checked = 1;
  694. }
  695. fn->frags += 1;
  696. newfrag = new_fragment(fn, fn_ofs, fn_size);
  697. if (unlikely(!newfrag))
  698. return -ENOMEM;
  699. dbg_fragtree2("there are no more fragments, insert %#04x-%#04x\n",
  700. newfrag->ofs, newfrag->ofs + newfrag->size);
  701. rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
  702. rb_insert_color(&newfrag->rb, root);
  703. goto out_ok;
  704. } else {
  705. this = new_this;
  706. dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
  707. this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
  708. }
  709. }
  710. }
  711. out_ok:
  712. BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE);
  713. if (ref_flag == REF_OBSOLETE) {
  714. dbg_fragtree2("the node is obsolete now\n");
  715. /* jffs2_mark_node_obsolete() will adjust space accounting */
  716. jffs2_mark_node_obsolete(c, fn->raw);
  717. return 1;
  718. }
  719. dbg_fragtree2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE");
  720. /* Space accounting was adjusted at check_node_data() */
  721. spin_lock(&c->erase_completion_lock);
  722. fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag;
  723. spin_unlock(&c->erase_completion_lock);
  724. return 0;
  725. }
  726. void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
  727. {
  728. spin_lock(&c->inocache_lock);
  729. ic->state = state;
  730. wake_up(&c->inocache_wq);
  731. spin_unlock(&c->inocache_lock);
  732. }
  733. /* During mount, this needs no locking. During normal operation, its
  734. callers want to do other stuff while still holding the inocache_lock.
  735. Rather than introducing special case get_ino_cache functions or
  736. callbacks, we just let the caller do the locking itself. */
  737. struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  738. {
  739. struct jffs2_inode_cache *ret;
  740. ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
  741. while (ret && ret->ino < ino) {
  742. ret = ret->next;
  743. }
  744. if (ret && ret->ino != ino)
  745. ret = NULL;
  746. return ret;
  747. }
  748. void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
  749. {
  750. struct jffs2_inode_cache **prev;
  751. spin_lock(&c->inocache_lock);
  752. if (!new->ino)
  753. new->ino = ++c->highest_ino;
  754. dbg_inocache("add %p (ino #%u)\n", new, new->ino);
  755. prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
  756. while ((*prev) && (*prev)->ino < new->ino) {
  757. prev = &(*prev)->next;
  758. }
  759. new->next = *prev;
  760. *prev = new;
  761. spin_unlock(&c->inocache_lock);
  762. }
  763. void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
  764. {
  765. struct jffs2_inode_cache **prev;
  766. dbg_inocache("del %p (ino #%u)\n", old, old->ino);
  767. spin_lock(&c->inocache_lock);
  768. prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
  769. while ((*prev) && (*prev)->ino < old->ino) {
  770. prev = &(*prev)->next;
  771. }
  772. if ((*prev) == old) {
  773. *prev = old->next;
  774. }
  775. /* Free it now unless it's in READING or CLEARING state, which
  776. are the transitions upon read_inode() and clear_inode(). The
  777. rest of the time we know nobody else is looking at it, and
  778. if it's held by read_inode() or clear_inode() they'll free it
  779. for themselves. */
  780. if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
  781. jffs2_free_inode_cache(old);
  782. spin_unlock(&c->inocache_lock);
  783. }
  784. void jffs2_free_ino_caches(struct jffs2_sb_info *c)
  785. {
  786. int i;
  787. struct jffs2_inode_cache *this, *next;
  788. for (i=0; i<INOCACHE_HASHSIZE; i++) {
  789. this = c->inocache_list[i];
  790. while (this) {
  791. next = this->next;
  792. jffs2_free_inode_cache(this);
  793. this = next;
  794. }
  795. c->inocache_list[i] = NULL;
  796. }
  797. }
  798. void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
  799. {
  800. int i;
  801. struct jffs2_raw_node_ref *this, *next;
  802. for (i=0; i<c->nr_blocks; i++) {
  803. this = c->blocks[i].first_node;
  804. while(this) {
  805. next = this->next_phys;
  806. jffs2_free_raw_node_ref(this);
  807. this = next;
  808. }
  809. c->blocks[i].first_node = c->blocks[i].last_node = NULL;
  810. }
  811. }
  812. struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
  813. {
  814. /* The common case in lookup is that there will be a node
  815. which precisely matches. So we go looking for that first */
  816. struct rb_node *next;
  817. struct jffs2_node_frag *prev = NULL;
  818. struct jffs2_node_frag *frag = NULL;
  819. dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
  820. next = fragtree->rb_node;
  821. while(next) {
  822. frag = rb_entry(next, struct jffs2_node_frag, rb);
  823. if (frag->ofs + frag->size <= offset) {
  824. /* Remember the closest smaller match on the way down */
  825. if (!prev || frag->ofs > prev->ofs)
  826. prev = frag;
  827. next = frag->rb.rb_right;
  828. } else if (frag->ofs > offset) {
  829. next = frag->rb.rb_left;
  830. } else {
  831. return frag;
  832. }
  833. }
  834. /* Exact match not found. Go back up looking at each parent,
  835. and return the closest smaller one */
  836. if (prev)
  837. dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
  838. prev->ofs, prev->ofs+prev->size);
  839. else
  840. dbg_fragtree2("returning NULL, empty fragtree\n");
  841. return prev;
  842. }
  843. /* Pass 'c' argument to indicate that nodes should be marked obsolete as
  844. they're killed. */
  845. void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
  846. {
  847. struct jffs2_node_frag *frag;
  848. struct jffs2_node_frag *parent;
  849. if (!root->rb_node)
  850. return;
  851. dbg_fragtree("killing\n");
  852. frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
  853. while(frag) {
  854. if (frag->rb.rb_left) {
  855. frag = frag_left(frag);
  856. continue;
  857. }
  858. if (frag->rb.rb_right) {
  859. frag = frag_right(frag);
  860. continue;
  861. }
  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. }