nodelist.c 29 KB

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