nodelist.c 22 KB

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