nodelist.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 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. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/fs.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/crc32.h>
  17. #include <linux/slab.h>
  18. #include <linux/pagemap.h>
  19. #include "nodelist.h"
  20. static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
  21. struct jffs2_node_frag *this);
  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. uint32_t 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 0;
  68. frag = frag_last(list);
  69. /* Sanity check for truncation to longer than we started with... */
  70. if (!frag)
  71. return 0;
  72. if (frag->ofs + frag->size < size)
  73. return frag->ofs + frag->size;
  74. /* If the last fragment starts at the RAM page boundary, it is
  75. * REF_PRISTINE irrespective of its size. */
  76. if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
  77. dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
  78. frag->ofs, frag->ofs + frag->size);
  79. frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
  80. }
  81. return size;
  82. }
  83. static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
  84. struct jffs2_node_frag *this)
  85. {
  86. if (this->node) {
  87. this->node->frags--;
  88. if (!this->node->frags) {
  89. /* The node has no valid frags left. It's totally obsoleted */
  90. dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
  91. ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
  92. jffs2_mark_node_obsolete(c, this->node->raw);
  93. jffs2_free_full_dnode(this->node);
  94. } else {
  95. dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
  96. ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
  97. mark_ref_normal(this->node->raw);
  98. }
  99. }
  100. jffs2_free_node_frag(this);
  101. }
  102. static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
  103. {
  104. struct rb_node *parent = &base->rb;
  105. struct rb_node **link = &parent;
  106. dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
  107. while (*link) {
  108. parent = *link;
  109. base = rb_entry(parent, struct jffs2_node_frag, rb);
  110. if (newfrag->ofs > base->ofs)
  111. link = &base->rb.rb_right;
  112. else if (newfrag->ofs < base->ofs)
  113. link = &base->rb.rb_left;
  114. else {
  115. JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
  116. BUG();
  117. }
  118. }
  119. rb_link_node(&newfrag->rb, &base->rb, link);
  120. }
  121. /*
  122. * Allocate and initializes a new fragment.
  123. */
  124. static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
  125. {
  126. struct jffs2_node_frag *newfrag;
  127. newfrag = jffs2_alloc_node_frag();
  128. if (likely(newfrag)) {
  129. newfrag->ofs = ofs;
  130. newfrag->size = size;
  131. newfrag->node = fn;
  132. } else {
  133. JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
  134. }
  135. return newfrag;
  136. }
  137. /*
  138. * Called when there is no overlapping fragment exist. Inserts a hole before the new
  139. * fragment and inserts the new fragment to the fragtree.
  140. */
  141. static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
  142. struct jffs2_node_frag *newfrag,
  143. struct jffs2_node_frag *this, uint32_t lastend)
  144. {
  145. if (lastend < newfrag->node->ofs) {
  146. /* put a hole in before the new fragment */
  147. struct jffs2_node_frag *holefrag;
  148. holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
  149. if (unlikely(!holefrag)) {
  150. jffs2_free_node_frag(newfrag);
  151. return -ENOMEM;
  152. }
  153. if (this) {
  154. /* By definition, the 'this' node has no right-hand child,
  155. because there are no frags with offset greater than it.
  156. So that's where we want to put the hole */
  157. dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
  158. holefrag->ofs, holefrag->ofs + holefrag->size);
  159. rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
  160. } else {
  161. dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
  162. holefrag->ofs, holefrag->ofs + holefrag->size);
  163. rb_link_node(&holefrag->rb, NULL, &root->rb_node);
  164. }
  165. rb_insert_color(&holefrag->rb, root);
  166. this = holefrag;
  167. }
  168. if (this) {
  169. /* By definition, the 'this' node has no right-hand child,
  170. because there are no frags with offset greater than it.
  171. So that's where we want to put new fragment */
  172. dbg_fragtree2("add the new node at the right\n");
  173. rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
  174. } else {
  175. dbg_fragtree2("insert the new node at the root of the tree\n");
  176. rb_link_node(&newfrag->rb, NULL, &root->rb_node);
  177. }
  178. rb_insert_color(&newfrag->rb, root);
  179. return 0;
  180. }
  181. /* Doesn't set inode->i_size */
  182. static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
  183. {
  184. struct jffs2_node_frag *this;
  185. uint32_t lastend;
  186. /* Skip all the nodes which are completed before this one starts */
  187. this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
  188. if (this) {
  189. dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
  190. this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
  191. lastend = this->ofs + this->size;
  192. } else {
  193. dbg_fragtree2("lookup gave no frag\n");
  194. lastend = 0;
  195. }
  196. /* See if we ran off the end of the fragtree */
  197. if (lastend <= newfrag->ofs) {
  198. /* We did */
  199. /* Check if 'this' node was on the same page as the new node.
  200. If so, both 'this' and the new node get marked REF_NORMAL so
  201. the GC can take a look.
  202. */
  203. if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
  204. if (this->node)
  205. mark_ref_normal(this->node->raw);
  206. mark_ref_normal(newfrag->node->raw);
  207. }
  208. return no_overlapping_node(c, root, newfrag, this, lastend);
  209. }
  210. if (this->node)
  211. dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
  212. this->ofs, this->ofs + this->size,
  213. ref_offset(this->node->raw), ref_flags(this->node->raw));
  214. else
  215. dbg_fragtree2("dealing with hole frag %u-%u.\n",
  216. this->ofs, this->ofs + this->size);
  217. /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
  218. * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
  219. */
  220. if (newfrag->ofs > this->ofs) {
  221. /* This node isn't completely obsoleted. The start of it remains valid */
  222. /* Mark the new node and the partially covered node REF_NORMAL -- let
  223. the GC take a look at them */
  224. mark_ref_normal(newfrag->node->raw);
  225. if (this->node)
  226. mark_ref_normal(this->node->raw);
  227. if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
  228. /* The new node splits 'this' frag into two */
  229. struct jffs2_node_frag *newfrag2;
  230. if (this->node)
  231. dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
  232. this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
  233. else
  234. dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
  235. this->ofs, this->ofs+this->size);
  236. /* New second frag pointing to this's node */
  237. newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
  238. this->ofs + this->size - newfrag->ofs - newfrag->size);
  239. if (unlikely(!newfrag2))
  240. return -ENOMEM;
  241. if (this->node)
  242. this->node->frags++;
  243. /* Adjust size of original 'this' */
  244. this->size = newfrag->ofs - this->ofs;
  245. /* Now, we know there's no node with offset
  246. greater than this->ofs but smaller than
  247. newfrag2->ofs or newfrag->ofs, for obvious
  248. reasons. So we can do a tree insert from
  249. 'this' to insert newfrag, and a tree insert
  250. from newfrag to insert newfrag2. */
  251. jffs2_fragtree_insert(newfrag, this);
  252. rb_insert_color(&newfrag->rb, root);
  253. jffs2_fragtree_insert(newfrag2, newfrag);
  254. rb_insert_color(&newfrag2->rb, root);
  255. return 0;
  256. }
  257. /* New node just reduces 'this' frag in size, doesn't split it */
  258. this->size = newfrag->ofs - this->ofs;
  259. /* Again, we know it lives down here in the tree */
  260. jffs2_fragtree_insert(newfrag, this);
  261. rb_insert_color(&newfrag->rb, root);
  262. } else {
  263. /* New frag starts at the same point as 'this' used to. Replace
  264. it in the tree without doing a delete and insertion */
  265. dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
  266. newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
  267. rb_replace_node(&this->rb, &newfrag->rb, root);
  268. if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
  269. dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
  270. jffs2_obsolete_node_frag(c, this);
  271. } else {
  272. this->ofs += newfrag->size;
  273. this->size -= newfrag->size;
  274. jffs2_fragtree_insert(this, newfrag);
  275. rb_insert_color(&this->rb, root);
  276. return 0;
  277. }
  278. }
  279. /* OK, now we have newfrag added in the correct place in the tree, but
  280. frag_next(newfrag) may be a fragment which is overlapped by it
  281. */
  282. while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
  283. /* 'this' frag is obsoleted completely. */
  284. dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
  285. this, this->ofs, this->ofs+this->size);
  286. rb_erase(&this->rb, root);
  287. jffs2_obsolete_node_frag(c, this);
  288. }
  289. /* Now we're pointing at the first frag which isn't totally obsoleted by
  290. the new frag */
  291. if (!this || newfrag->ofs + newfrag->size == this->ofs)
  292. return 0;
  293. /* Still some overlap but we don't need to move it in the tree */
  294. this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
  295. this->ofs = newfrag->ofs + newfrag->size;
  296. /* And mark them REF_NORMAL so the GC takes a look at them */
  297. if (this->node)
  298. mark_ref_normal(this->node->raw);
  299. mark_ref_normal(newfrag->node->raw);
  300. return 0;
  301. }
  302. /*
  303. * Given an inode, probably with existing tree of fragments, add the new node
  304. * to the fragment tree.
  305. */
  306. int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
  307. {
  308. int ret;
  309. struct jffs2_node_frag *newfrag;
  310. if (unlikely(!fn->size))
  311. return 0;
  312. newfrag = new_fragment(fn, fn->ofs, fn->size);
  313. if (unlikely(!newfrag))
  314. return -ENOMEM;
  315. newfrag->node->frags = 1;
  316. dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
  317. fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
  318. ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
  319. if (unlikely(ret))
  320. return ret;
  321. /* If we now share a page with other nodes, mark either previous
  322. or next node REF_NORMAL, as appropriate. */
  323. if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
  324. struct jffs2_node_frag *prev = frag_prev(newfrag);
  325. mark_ref_normal(fn->raw);
  326. /* If we don't start at zero there's _always_ a previous */
  327. if (prev->node)
  328. mark_ref_normal(prev->node->raw);
  329. }
  330. if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
  331. struct jffs2_node_frag *next = frag_next(newfrag);
  332. if (next) {
  333. mark_ref_normal(fn->raw);
  334. if (next->node)
  335. mark_ref_normal(next->node->raw);
  336. }
  337. }
  338. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  339. return 0;
  340. }
  341. void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
  342. {
  343. spin_lock(&c->inocache_lock);
  344. ic->state = state;
  345. wake_up(&c->inocache_wq);
  346. spin_unlock(&c->inocache_lock);
  347. }
  348. /* During mount, this needs no locking. During normal operation, its
  349. callers want to do other stuff while still holding the inocache_lock.
  350. Rather than introducing special case get_ino_cache functions or
  351. callbacks, we just let the caller do the locking itself. */
  352. struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  353. {
  354. struct jffs2_inode_cache *ret;
  355. ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
  356. while (ret && ret->ino < ino) {
  357. ret = ret->next;
  358. }
  359. if (ret && ret->ino != ino)
  360. ret = NULL;
  361. return ret;
  362. }
  363. void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
  364. {
  365. struct jffs2_inode_cache **prev;
  366. spin_lock(&c->inocache_lock);
  367. if (!new->ino)
  368. new->ino = ++c->highest_ino;
  369. dbg_inocache("add %p (ino #%u)\n", new, new->ino);
  370. prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
  371. while ((*prev) && (*prev)->ino < new->ino) {
  372. prev = &(*prev)->next;
  373. }
  374. new->next = *prev;
  375. *prev = new;
  376. spin_unlock(&c->inocache_lock);
  377. }
  378. void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
  379. {
  380. struct jffs2_inode_cache **prev;
  381. #ifdef CONFIG_JFFS2_FS_XATTR
  382. BUG_ON(old->xref);
  383. #endif
  384. dbg_inocache("del %p (ino #%u)\n", old, old->ino);
  385. spin_lock(&c->inocache_lock);
  386. prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
  387. while ((*prev) && (*prev)->ino < old->ino) {
  388. prev = &(*prev)->next;
  389. }
  390. if ((*prev) == old) {
  391. *prev = old->next;
  392. }
  393. /* Free it now unless it's in READING or CLEARING state, which
  394. are the transitions upon read_inode() and clear_inode(). The
  395. rest of the time we know nobody else is looking at it, and
  396. if it's held by read_inode() or clear_inode() they'll free it
  397. for themselves. */
  398. if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
  399. jffs2_free_inode_cache(old);
  400. spin_unlock(&c->inocache_lock);
  401. }
  402. void jffs2_free_ino_caches(struct jffs2_sb_info *c)
  403. {
  404. int i;
  405. struct jffs2_inode_cache *this, *next;
  406. for (i=0; i<INOCACHE_HASHSIZE; i++) {
  407. this = c->inocache_list[i];
  408. while (this) {
  409. next = this->next;
  410. jffs2_xattr_free_inode(c, this);
  411. jffs2_free_inode_cache(this);
  412. this = next;
  413. }
  414. c->inocache_list[i] = NULL;
  415. }
  416. }
  417. void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
  418. {
  419. int i;
  420. struct jffs2_raw_node_ref *this, *next;
  421. for (i=0; i<c->nr_blocks; i++) {
  422. this = c->blocks[i].first_node;
  423. while (this) {
  424. if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
  425. next = this[REFS_PER_BLOCK].next_in_ino;
  426. else
  427. next = NULL;
  428. jffs2_free_refblock(this);
  429. this = next;
  430. }
  431. c->blocks[i].first_node = c->blocks[i].last_node = NULL;
  432. }
  433. }
  434. struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
  435. {
  436. /* The common case in lookup is that there will be a node
  437. which precisely matches. So we go looking for that first */
  438. struct rb_node *next;
  439. struct jffs2_node_frag *prev = NULL;
  440. struct jffs2_node_frag *frag = NULL;
  441. dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
  442. next = fragtree->rb_node;
  443. while(next) {
  444. frag = rb_entry(next, struct jffs2_node_frag, rb);
  445. if (frag->ofs + frag->size <= offset) {
  446. /* Remember the closest smaller match on the way down */
  447. if (!prev || frag->ofs > prev->ofs)
  448. prev = frag;
  449. next = frag->rb.rb_right;
  450. } else if (frag->ofs > offset) {
  451. next = frag->rb.rb_left;
  452. } else {
  453. return frag;
  454. }
  455. }
  456. /* Exact match not found. Go back up looking at each parent,
  457. and return the closest smaller one */
  458. if (prev)
  459. dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
  460. prev->ofs, prev->ofs+prev->size);
  461. else
  462. dbg_fragtree2("returning NULL, empty fragtree\n");
  463. return prev;
  464. }
  465. /* Pass 'c' argument to indicate that nodes should be marked obsolete as
  466. they're killed. */
  467. void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
  468. {
  469. struct jffs2_node_frag *frag;
  470. struct jffs2_node_frag *parent;
  471. if (!root->rb_node)
  472. return;
  473. dbg_fragtree("killing\n");
  474. frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
  475. while(frag) {
  476. if (frag->rb.rb_left) {
  477. frag = frag_left(frag);
  478. continue;
  479. }
  480. if (frag->rb.rb_right) {
  481. frag = frag_right(frag);
  482. continue;
  483. }
  484. if (frag->node && !(--frag->node->frags)) {
  485. /* Not a hole, and it's the final remaining frag
  486. of this node. Free the node */
  487. if (c)
  488. jffs2_mark_node_obsolete(c, frag->node->raw);
  489. jffs2_free_full_dnode(frag->node);
  490. }
  491. parent = frag_parent(frag);
  492. if (parent) {
  493. if (frag_left(parent) == frag)
  494. parent->rb.rb_left = NULL;
  495. else
  496. parent->rb.rb_right = NULL;
  497. }
  498. jffs2_free_node_frag(frag);
  499. frag = parent;
  500. cond_resched();
  501. }
  502. }
  503. struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
  504. struct jffs2_eraseblock *jeb,
  505. uint32_t ofs, uint32_t len,
  506. struct jffs2_inode_cache *ic)
  507. {
  508. struct jffs2_raw_node_ref *ref;
  509. BUG_ON(!jeb->allocated_refs);
  510. jeb->allocated_refs--;
  511. ref = jeb->last_node;
  512. dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
  513. ref->next_in_ino);
  514. while (ref->flash_offset != REF_EMPTY_NODE) {
  515. if (ref->flash_offset == REF_LINK_NODE)
  516. ref = ref->next_in_ino;
  517. else
  518. ref++;
  519. }
  520. dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
  521. ref->flash_offset, ofs, ref->next_in_ino, len);
  522. ref->flash_offset = ofs;
  523. if (!jeb->first_node) {
  524. jeb->first_node = ref;
  525. BUG_ON(ref_offset(ref) != jeb->offset);
  526. } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
  527. uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
  528. JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
  529. ref, ref_offset(ref), ref_offset(ref)+len,
  530. ref_offset(jeb->last_node),
  531. ref_offset(jeb->last_node)+last_len);
  532. BUG();
  533. }
  534. jeb->last_node = ref;
  535. if (ic) {
  536. ref->next_in_ino = ic->nodes;
  537. ic->nodes = ref;
  538. } else {
  539. ref->next_in_ino = NULL;
  540. }
  541. switch(ref_flags(ref)) {
  542. case REF_UNCHECKED:
  543. c->unchecked_size += len;
  544. jeb->unchecked_size += len;
  545. break;
  546. case REF_NORMAL:
  547. case REF_PRISTINE:
  548. c->used_size += len;
  549. jeb->used_size += len;
  550. break;
  551. case REF_OBSOLETE:
  552. c->dirty_size += len;
  553. jeb->dirty_size += len;
  554. break;
  555. }
  556. c->free_size -= len;
  557. jeb->free_size -= len;
  558. #ifdef TEST_TOTLEN
  559. /* Set (and test) __totlen field... for now */
  560. ref->__totlen = len;
  561. ref_totlen(c, jeb, ref);
  562. #endif
  563. return ref;
  564. }
  565. /* No locking, no reservation of 'ref'. Do not use on a live file system */
  566. int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  567. uint32_t size)
  568. {
  569. if (!size)
  570. return 0;
  571. if (unlikely(size > jeb->free_size)) {
  572. printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
  573. size, jeb->free_size, jeb->wasted_size);
  574. BUG();
  575. }
  576. /* REF_EMPTY_NODE is !obsolete, so that works OK */
  577. if (jeb->last_node && ref_obsolete(jeb->last_node)) {
  578. #ifdef TEST_TOTLEN
  579. jeb->last_node->__totlen += size;
  580. #endif
  581. c->dirty_size += size;
  582. c->free_size -= size;
  583. jeb->dirty_size += size;
  584. jeb->free_size -= size;
  585. } else {
  586. uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
  587. ofs |= REF_OBSOLETE;
  588. jffs2_link_node_ref(c, jeb, ofs, size, NULL);
  589. }
  590. return 0;
  591. }
  592. /* Calculate totlen from surrounding nodes or eraseblock */
  593. static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
  594. struct jffs2_eraseblock *jeb,
  595. struct jffs2_raw_node_ref *ref)
  596. {
  597. uint32_t ref_end;
  598. struct jffs2_raw_node_ref *next_ref = ref_next(ref);
  599. if (next_ref)
  600. ref_end = ref_offset(next_ref);
  601. else {
  602. if (!jeb)
  603. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  604. /* Last node in block. Use free_space */
  605. if (unlikely(ref != jeb->last_node)) {
  606. printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
  607. ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0);
  608. BUG();
  609. }
  610. ref_end = jeb->offset + c->sector_size - jeb->free_size;
  611. }
  612. return ref_end - ref_offset(ref);
  613. }
  614. uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  615. struct jffs2_raw_node_ref *ref)
  616. {
  617. uint32_t ret;
  618. ret = __ref_totlen(c, jeb, ref);
  619. #ifdef TEST_TOTLEN
  620. if (unlikely(ret != ref->__totlen)) {
  621. if (!jeb)
  622. jeb = &c->blocks[ref->flash_offset / c->sector_size];
  623. printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
  624. ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
  625. ret, ref->__totlen);
  626. if (ref_next(ref)) {
  627. printk(KERN_CRIT "next %p (0x%08x-0x%08x)\n", ref_next(ref), ref_offset(ref_next(ref)),
  628. ref_offset(ref_next(ref))+ref->__totlen);
  629. } else
  630. printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node);
  631. printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size);
  632. #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
  633. __jffs2_dbg_dump_node_refs_nolock(c, jeb);
  634. #endif
  635. WARN_ON(1);
  636. ret = ref->__totlen;
  637. }
  638. #endif /* TEST_TOTLEN */
  639. return ret;
  640. }