nodelist.c 17 KB

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