nodelist.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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.102 2005/07/28 12:45:10 dedekind 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. JFFS2_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((const char *)((*prev)->name), (const char *)new->name)) {
  28. /* Duplicate. Free one */
  29. if (new->version < (*prev)->version) {
  30. JFFS2_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. JFFS2_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_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
  50. {
  51. if (this->node) {
  52. this->node->frags--;
  53. if (!this->node->frags) {
  54. /* The node has no valid frags left. It's totally obsoleted */
  55. JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
  56. ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
  57. jffs2_mark_node_obsolete(c, this->node->raw);
  58. jffs2_free_full_dnode(this->node);
  59. } else {
  60. JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
  61. ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
  62. mark_ref_normal(this->node->raw);
  63. }
  64. }
  65. jffs2_free_node_frag(this);
  66. }
  67. static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
  68. {
  69. struct rb_node *parent = &base->rb;
  70. struct rb_node **link = &parent;
  71. JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
  72. while (*link) {
  73. parent = *link;
  74. base = rb_entry(parent, struct jffs2_node_frag, rb);
  75. JFFS2_DBG_FRAGTREE2("considering frag at 0x%08x\n", base->ofs);
  76. if (newfrag->ofs > base->ofs)
  77. link = &base->rb.rb_right;
  78. else if (newfrag->ofs < base->ofs)
  79. link = &base->rb.rb_left;
  80. else {
  81. JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
  82. BUG();
  83. }
  84. }
  85. rb_link_node(&newfrag->rb, &base->rb, link);
  86. }
  87. /* Doesn't set inode->i_size */
  88. static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag)
  89. {
  90. struct jffs2_node_frag *this;
  91. uint32_t lastend;
  92. /* Skip all the nodes which are completed before this one starts */
  93. this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
  94. if (this) {
  95. JFFS2_DBG_FRAGTREE2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
  96. this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
  97. lastend = this->ofs + this->size;
  98. } else {
  99. JFFS2_DBG_FRAGTREE2("lookup gave no frag\n");
  100. lastend = 0;
  101. }
  102. /* See if we ran off the end of the list */
  103. if (lastend <= newfrag->ofs) {
  104. /* We did */
  105. /* Check if 'this' node was on the same page as the new node.
  106. If so, both 'this' and the new node get marked REF_NORMAL so
  107. the GC can take a look.
  108. */
  109. if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
  110. if (this->node)
  111. mark_ref_normal(this->node->raw);
  112. mark_ref_normal(newfrag->node->raw);
  113. }
  114. if (lastend < newfrag->node->ofs) {
  115. /* ... and we need to put a hole in before the new node */
  116. struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag();
  117. if (!holefrag) {
  118. jffs2_free_node_frag(newfrag);
  119. return -ENOMEM;
  120. }
  121. holefrag->ofs = lastend;
  122. holefrag->size = newfrag->node->ofs - lastend;
  123. holefrag->node = NULL;
  124. if (this) {
  125. /* By definition, the 'this' node has no right-hand child,
  126. because there are no frags with offset greater than it.
  127. So that's where we want to put the hole */
  128. JFFS2_DBG_FRAGTREE2("adding hole frag (%p) on right of node at (%p)\n", holefrag, this);
  129. rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
  130. } else {
  131. JFFS2_DBG_FRAGTREE2("adding hole frag (%p) at root of tree\n", holefrag);
  132. rb_link_node(&holefrag->rb, NULL, &list->rb_node);
  133. }
  134. rb_insert_color(&holefrag->rb, list);
  135. this = holefrag;
  136. }
  137. if (this) {
  138. /* By definition, the 'this' node has no right-hand child,
  139. because there are no frags with offset greater than it.
  140. So that's where we want to put new fragment */
  141. JFFS2_DBG_FRAGTREE2("adding new frag (%p) on right of node at (%p)\n", newfrag, this);
  142. rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
  143. } else {
  144. JFFS2_DBG_FRAGTREE2("adding new frag (%p) at root of tree\n", newfrag);
  145. rb_link_node(&newfrag->rb, NULL, &list->rb_node);
  146. }
  147. rb_insert_color(&newfrag->rb, list);
  148. return 0;
  149. }
  150. JFFS2_DBG_FRAGTREE2("dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
  151. this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
  152. /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
  153. * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
  154. */
  155. if (newfrag->ofs > this->ofs) {
  156. /* This node isn't completely obsoleted. The start of it remains valid */
  157. /* Mark the new node and the partially covered node REF_NORMAL -- let
  158. the GC take a look at them */
  159. mark_ref_normal(newfrag->node->raw);
  160. if (this->node)
  161. mark_ref_normal(this->node->raw);
  162. if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
  163. /* The new node splits 'this' frag into two */
  164. struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag();
  165. if (!newfrag2) {
  166. jffs2_free_node_frag(newfrag);
  167. return -ENOMEM;
  168. }
  169. if (this->node)
  170. JFFS2_DBG_FRAGTREE2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
  171. this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
  172. else
  173. JFFS2_DBG_FRAGTREE2("split old hole frag 0x%04x-0x%04x\n",
  174. this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
  175. /* New second frag pointing to this's node */
  176. newfrag2->ofs = newfrag->ofs + newfrag->size;
  177. newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
  178. newfrag2->node = this->node;
  179. if (this->node)
  180. this->node->frags++;
  181. /* Adjust size of original 'this' */
  182. this->size = newfrag->ofs - this->ofs;
  183. /* Now, we know there's no node with offset
  184. greater than this->ofs but smaller than
  185. newfrag2->ofs or newfrag->ofs, for obvious
  186. reasons. So we can do a tree insert from
  187. 'this' to insert newfrag, and a tree insert
  188. from newfrag to insert newfrag2. */
  189. jffs2_fragtree_insert(newfrag, this);
  190. rb_insert_color(&newfrag->rb, list);
  191. jffs2_fragtree_insert(newfrag2, newfrag);
  192. rb_insert_color(&newfrag2->rb, list);
  193. return 0;
  194. }
  195. /* New node just reduces 'this' frag in size, doesn't split it */
  196. this->size = newfrag->ofs - this->ofs;
  197. /* Again, we know it lives down here in the tree */
  198. jffs2_fragtree_insert(newfrag, this);
  199. rb_insert_color(&newfrag->rb, list);
  200. } else {
  201. /* New frag starts at the same point as 'this' used to. Replace
  202. it in the tree without doing a delete and insertion */
  203. JFFS2_DBG_FRAGTREE2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
  204. newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
  205. rb_replace_node(&this->rb, &newfrag->rb, list);
  206. if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
  207. JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
  208. jffs2_obsolete_node_frag(c, this);
  209. } else {
  210. this->ofs += newfrag->size;
  211. this->size -= newfrag->size;
  212. jffs2_fragtree_insert(this, newfrag);
  213. rb_insert_color(&this->rb, list);
  214. return 0;
  215. }
  216. }
  217. /* OK, now we have newfrag added in the correct place in the tree, but
  218. frag_next(newfrag) may be a fragment which is overlapped by it
  219. */
  220. while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
  221. /* 'this' frag is obsoleted completely. */
  222. JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n",
  223. this, this->ofs, this->ofs+this->size);
  224. rb_erase(&this->rb, list);
  225. jffs2_obsolete_node_frag(c, this);
  226. }
  227. /* Now we're pointing at the first frag which isn't totally obsoleted by
  228. the new frag */
  229. if (!this || newfrag->ofs + newfrag->size == this->ofs) {
  230. return 0;
  231. }
  232. /* Still some overlap but we don't need to move it in the tree */
  233. this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
  234. this->ofs = newfrag->ofs + newfrag->size;
  235. /* And mark them REF_NORMAL so the GC takes a look at them */
  236. if (this->node)
  237. mark_ref_normal(this->node->raw);
  238. mark_ref_normal(newfrag->node->raw);
  239. return 0;
  240. }
  241. /* Given an inode, probably with existing list of fragments, add the new node
  242. * to the fragment list.
  243. */
  244. int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
  245. {
  246. int ret;
  247. struct jffs2_node_frag *newfrag;
  248. if (unlikely(!fn->size))
  249. return 0;
  250. newfrag = jffs2_alloc_node_frag();
  251. if (unlikely(!newfrag))
  252. return -ENOMEM;
  253. JFFS2_DBG_FRAGTREE("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
  254. fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
  255. newfrag->ofs = fn->ofs;
  256. newfrag->size = fn->size;
  257. newfrag->node = fn;
  258. newfrag->node->frags = 1;
  259. ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
  260. if (unlikely(ret))
  261. return ret;
  262. /* If we now share a page with other nodes, mark either previous
  263. or next node REF_NORMAL, as appropriate. */
  264. if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
  265. struct jffs2_node_frag *prev = frag_prev(newfrag);
  266. mark_ref_normal(fn->raw);
  267. /* If we don't start at zero there's _always_ a previous */
  268. if (prev->node)
  269. mark_ref_normal(prev->node->raw);
  270. }
  271. if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
  272. struct jffs2_node_frag *next = frag_next(newfrag);
  273. if (next) {
  274. mark_ref_normal(fn->raw);
  275. if (next->node)
  276. mark_ref_normal(next->node->raw);
  277. }
  278. }
  279. jffs2_dbg_fragtree_paranoia_check_nolock(f);
  280. jffs2_dbg_dump_fragtree_nolock(f);
  281. return 0;
  282. }
  283. void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
  284. {
  285. spin_lock(&c->inocache_lock);
  286. ic->state = state;
  287. wake_up(&c->inocache_wq);
  288. spin_unlock(&c->inocache_lock);
  289. }
  290. /* During mount, this needs no locking. During normal operation, its
  291. callers want to do other stuff while still holding the inocache_lock.
  292. Rather than introducing special case get_ino_cache functions or
  293. callbacks, we just let the caller do the locking itself. */
  294. struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
  295. {
  296. struct jffs2_inode_cache *ret;
  297. ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
  298. while (ret && ret->ino < ino) {
  299. ret = ret->next;
  300. }
  301. if (ret && ret->ino != ino)
  302. ret = NULL;
  303. return ret;
  304. }
  305. void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
  306. {
  307. struct jffs2_inode_cache **prev;
  308. spin_lock(&c->inocache_lock);
  309. if (!new->ino)
  310. new->ino = ++c->highest_ino;
  311. JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino);
  312. prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
  313. while ((*prev) && (*prev)->ino < new->ino) {
  314. prev = &(*prev)->next;
  315. }
  316. new->next = *prev;
  317. *prev = new;
  318. spin_unlock(&c->inocache_lock);
  319. }
  320. void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
  321. {
  322. struct jffs2_inode_cache **prev;
  323. JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino);
  324. spin_lock(&c->inocache_lock);
  325. prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
  326. while ((*prev) && (*prev)->ino < old->ino) {
  327. prev = &(*prev)->next;
  328. }
  329. if ((*prev) == old) {
  330. *prev = old->next;
  331. }
  332. /* Free it now unless it's in READING or CLEARING state, which
  333. are the transitions upon read_inode() and clear_inode(). The
  334. rest of the time we know nobody else is looking at it, and
  335. if it's held by read_inode() or clear_inode() they'll free it
  336. for themselves. */
  337. if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
  338. jffs2_free_inode_cache(old);
  339. spin_unlock(&c->inocache_lock);
  340. }
  341. void jffs2_free_ino_caches(struct jffs2_sb_info *c)
  342. {
  343. int i;
  344. struct jffs2_inode_cache *this, *next;
  345. for (i=0; i<INOCACHE_HASHSIZE; i++) {
  346. this = c->inocache_list[i];
  347. while (this) {
  348. next = this->next;
  349. jffs2_free_inode_cache(this);
  350. this = next;
  351. }
  352. c->inocache_list[i] = NULL;
  353. }
  354. }
  355. void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
  356. {
  357. int i;
  358. struct jffs2_raw_node_ref *this, *next;
  359. for (i=0; i<c->nr_blocks; i++) {
  360. this = c->blocks[i].first_node;
  361. while(this) {
  362. next = this->next_phys;
  363. jffs2_free_raw_node_ref(this);
  364. this = next;
  365. }
  366. c->blocks[i].first_node = c->blocks[i].last_node = NULL;
  367. }
  368. }
  369. struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
  370. {
  371. /* The common case in lookup is that there will be a node
  372. which precisely matches. So we go looking for that first */
  373. struct rb_node *next;
  374. struct jffs2_node_frag *prev = NULL;
  375. struct jffs2_node_frag *frag = NULL;
  376. JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset);
  377. next = fragtree->rb_node;
  378. while(next) {
  379. frag = rb_entry(next, struct jffs2_node_frag, rb);
  380. JFFS2_DBG_FRAGTREE2("considering frag %#04x-%#04x (%p). left %p, right %p\n",
  381. frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right);
  382. if (frag->ofs + frag->size <= offset) {
  383. JFFS2_DBG_FRAGTREE2("going right from frag %#04x-%#04x, before the region we care about\n",
  384. frag->ofs, frag->ofs+frag->size);
  385. /* Remember the closest smaller match on the way down */
  386. if (!prev || frag->ofs > prev->ofs)
  387. prev = frag;
  388. next = frag->rb.rb_right;
  389. } else if (frag->ofs > offset) {
  390. JFFS2_DBG_FRAGTREE2("going left from frag %#04x-%#04x, after the region we care about\n",
  391. frag->ofs, frag->ofs+frag->size);
  392. next = frag->rb.rb_left;
  393. } else {
  394. JFFS2_DBG_FRAGTREE2("returning frag %#04x-%#04x, matched\n",
  395. frag->ofs, frag->ofs+frag->size);
  396. return frag;
  397. }
  398. }
  399. /* Exact match not found. Go back up looking at each parent,
  400. and return the closest smaller one */
  401. if (prev)
  402. JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n",
  403. prev->ofs, prev->ofs+prev->size);
  404. else
  405. JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n");
  406. return prev;
  407. }
  408. /* Pass 'c' argument to indicate that nodes should be marked obsolete as
  409. they're killed. */
  410. void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
  411. {
  412. struct jffs2_node_frag *frag;
  413. struct jffs2_node_frag *parent;
  414. if (!root->rb_node)
  415. return;
  416. JFFS2_DBG_FRAGTREE("killing\n");
  417. frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
  418. while(frag) {
  419. if (frag->rb.rb_left) {
  420. JFFS2_DBG_FRAGTREE2("going left from frag (%p) %#04x-%#04x\n",
  421. frag, frag->ofs, frag->ofs+frag->size);
  422. frag = frag_left(frag);
  423. continue;
  424. }
  425. if (frag->rb.rb_right) {
  426. JFFS2_DBG_FRAGTREE2("going right from frag (%p) %#04x-%#04x\n",
  427. frag, frag->ofs, frag->ofs+frag->size);
  428. frag = frag_right(frag);
  429. continue;
  430. }
  431. JFFS2_DBG_FRAGTREE2("frag %#04x-%#04x: node %p, frags %d\n",
  432. frag->ofs, frag->ofs+frag->size, frag->node, frag->node?frag->node->frags:0);
  433. if (frag->node && !(--frag->node->frags)) {
  434. /* Not a hole, and it's the final remaining frag
  435. of this node. Free the node */
  436. if (c)
  437. jffs2_mark_node_obsolete(c, frag->node->raw);
  438. jffs2_free_full_dnode(frag->node);
  439. }
  440. parent = frag_parent(frag);
  441. if (parent) {
  442. if (frag_left(parent) == frag)
  443. parent->rb.rb_left = NULL;
  444. else
  445. parent->rb.rb_right = NULL;
  446. }
  447. jffs2_free_node_frag(frag);
  448. frag = parent;
  449. cond_resched();
  450. }
  451. }