nodelist.c 21 KB

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