blob: fd21f109fbd22fedb8adf9725ebf102717478dd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Artem B. Bityutskiy280562b2005-08-17 15:57:43 +010010 * $Id: nodelist.c,v 1.111 2005/08/17 14:57:39 dedekind Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/fs.h>
17#include <linux/mtd/mtd.h>
18#include <linux/rbtree.h>
19#include <linux/crc32.h>
20#include <linux/slab.h>
21#include <linux/pagemap.h>
22#include "nodelist.h"
23
24void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
25{
26 struct jffs2_full_dirent **prev = list;
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010027
28 JFFS2_DBG_DENTLIST("add dirent \"%s\", ino #%u\n", new->name, new->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 while ((*prev) && (*prev)->nhash <= new->nhash) {
31 if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
32 /* Duplicate. Free one */
33 if (new->version < (*prev)->version) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010034 JFFS2_DBG_DENTLIST("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
35 (*prev)->name, (*prev)->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 jffs2_mark_node_obsolete(c, new->raw);
37 jffs2_free_full_dirent(new);
38 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010039 JFFS2_DBG_DENTLIST("marking old dirent \"%s\", ino #%u bsolete\n",
40 (*prev)->name, (*prev)->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 new->next = (*prev)->next;
42 jffs2_mark_node_obsolete(c, ((*prev)->raw));
43 jffs2_free_full_dirent(*prev);
44 *prev = new;
45 }
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 prev = &((*prev)->next);
49 }
50 new->next = *prev;
51 *prev = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010054void jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
55{
56 struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
57
58 JFFS2_DBG_FRAGTREE("truncating fragtree to 0x%08x bytes\n", size);
59
60 /* We know frag->ofs <= size. That's what lookup does for us */
61 if (frag && frag->ofs != size) {
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +010062 if (frag->ofs+frag->size > size) {
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010063 frag->size = size - frag->ofs;
64 }
65 frag = frag_next(frag);
66 }
67 while (frag && frag->ofs >= size) {
68 struct jffs2_node_frag *next = frag_next(frag);
69
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010070 frag_erase(frag, list);
71 jffs2_obsolete_node_frag(c, frag);
72 frag = next;
73 }
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +010074
75 if (size == 0)
76 return;
77
78 /*
79 * If the last fragment starts at the RAM page boundary, it is
80 * REF_PRISTINE irrespective of its size.
81 */
82 frag = frag_last(list);
83 if ((frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
84 JFFS2_DBG_FRAGTREE2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
85 frag->ofs, frag->ofs + frag->size);
86 frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
87 }
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010088}
89
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010090void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010092 if (this->node) {
93 this->node->frags--;
94 if (!this->node->frags) {
95 /* The node has no valid frags left. It's totally obsoleted */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010096 JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
97 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010098 jffs2_mark_node_obsolete(c, this->node->raw);
99 jffs2_free_full_dnode(this->node);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100100 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100101 JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
102 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100103 mark_ref_normal(this->node->raw);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100104 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100105
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100106 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100107 jffs2_free_node_frag(this);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100108}
109
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100110static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100111{
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100112 struct rb_node *parent = &base->rb;
113 struct rb_node **link = &parent;
114
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100115 JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100116
117 while (*link) {
118 parent = *link;
119 base = rb_entry(parent, struct jffs2_node_frag, rb);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100120
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100121 if (newfrag->ofs > base->ofs)
122 link = &base->rb.rb_right;
123 else if (newfrag->ofs < base->ofs)
124 link = &base->rb.rb_left;
125 else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100126 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100127 BUG();
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100128 }
129 }
130
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100131 rb_link_node(&newfrag->rb, &base->rb, link);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100132}
133
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100134/*
135 * Allocate and initializes a new fragment.
136 */
137static inline struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
138{
139 struct jffs2_node_frag *newfrag;
140
141 newfrag = jffs2_alloc_node_frag();
142 if (likely(newfrag)) {
143 newfrag->ofs = ofs;
144 newfrag->size = size;
145 newfrag->node = fn;
146 } else {
147 JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
148 }
149
150 return newfrag;
151}
152
153/*
154 * Called when there is no overlapping fragment exist. Inserts a hole before the new
155 * fragment and inserts the new fragment to the fragtree.
156 */
157static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
158 struct jffs2_node_frag *newfrag,
159 struct jffs2_node_frag *this, uint32_t lastend)
160{
161 if (lastend < newfrag->node->ofs) {
162 /* put a hole in before the new fragment */
163 struct jffs2_node_frag *holefrag;
164
165 holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
166 if (unlikely(!holefrag)) {
167 jffs2_free_node_frag(newfrag);
168 return -ENOMEM;
169 }
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 the hole */
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100175 JFFS2_DBG_FRAGTREE2("add hole frag %#04x-%#04x on the right of the new frag.\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100176 holefrag->ofs, holefrag->ofs + holefrag->size);
177 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
178 } else {
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100179 JFFS2_DBG_FRAGTREE2("Add hole frag %#04x-%#04x to the root of the tree.\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100180 holefrag->ofs, holefrag->ofs + holefrag->size);
181 rb_link_node(&holefrag->rb, NULL, &root->rb_node);
182 }
183 rb_insert_color(&holefrag->rb, root);
184 this = holefrag;
185 }
186
187 if (this) {
188 /* By definition, the 'this' node has no right-hand child,
189 because there are no frags with offset greater than it.
190 So that's where we want to put new fragment */
191 JFFS2_DBG_FRAGTREE2("add the new node at the right\n");
192 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
193 } else {
194 JFFS2_DBG_FRAGTREE2("insert the new node at the root of the tree\n");
195 rb_link_node(&newfrag->rb, NULL, &root->rb_node);
196 }
197 rb_insert_color(&newfrag->rb, root);
198
199 return 0;
200}
201
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100202/* Doesn't set inode->i_size */
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100203static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100205 struct jffs2_node_frag *this;
206 uint32_t lastend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100208 /* Skip all the nodes which are completed before this one starts */
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100209 this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100211 if (this) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100212 JFFS2_DBG_FRAGTREE2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
213 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100214 lastend = this->ofs + this->size;
215 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100216 JFFS2_DBG_FRAGTREE2("lookup gave no frag\n");
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100217 lastend = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100219
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100220 /* See if we ran off the end of the fragtree */
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100221 if (lastend <= newfrag->ofs) {
222 /* We did */
223
224 /* Check if 'this' node was on the same page as the new node.
225 If so, both 'this' and the new node get marked REF_NORMAL so
226 the GC can take a look.
227 */
228 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
229 if (this->node)
230 mark_ref_normal(this->node->raw);
231 mark_ref_normal(newfrag->node->raw);
232 }
233
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100234 return no_overlapping_node(c, root, newfrag, this, lastend);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100235 }
236
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100237 if (this->node)
238 JFFS2_DBG_FRAGTREE2("dealing with frag %u-%u, phys %#08x(%d).\n",
239 this->ofs, this->ofs + this->size,
240 ref_offset(this->node->raw), ref_flags(this->node->raw));
241 else
242 JFFS2_DBG_FRAGTREE2("dealing with hole frag %u-%u.\n",
243 this->ofs, this->ofs + this->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100244
245 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
246 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
247 */
248 if (newfrag->ofs > this->ofs) {
249 /* This node isn't completely obsoleted. The start of it remains valid */
250
251 /* Mark the new node and the partially covered node REF_NORMAL -- let
252 the GC take a look at them */
253 mark_ref_normal(newfrag->node->raw);
254 if (this->node)
255 mark_ref_normal(this->node->raw);
256
257 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
258 /* The new node splits 'this' frag into two */
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100259 struct jffs2_node_frag *newfrag2;
260
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100261 if (this->node)
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100262 JFFS2_DBG_FRAGTREE2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
263 this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100264 else
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100265 JFFS2_DBG_FRAGTREE2("split old hole frag 0x%04x-0x%04x\n",
Artem B. Bityutskiy8d5df402005-08-17 15:13:48 +0100266 this->ofs, this->ofs+this->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100267
268 /* New second frag pointing to this's node */
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100269 newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
270 this->ofs + this->size - newfrag->ofs - newfrag->size);
271 if (unlikely(!newfrag2))
272 return -ENOMEM;
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100273 if (this->node)
274 this->node->frags++;
275
276 /* Adjust size of original 'this' */
277 this->size = newfrag->ofs - this->ofs;
278
279 /* Now, we know there's no node with offset
280 greater than this->ofs but smaller than
281 newfrag2->ofs or newfrag->ofs, for obvious
282 reasons. So we can do a tree insert from
283 'this' to insert newfrag, and a tree insert
284 from newfrag to insert newfrag2. */
285 jffs2_fragtree_insert(newfrag, this);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100286 rb_insert_color(&newfrag->rb, root);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100287
288 jffs2_fragtree_insert(newfrag2, newfrag);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100289 rb_insert_color(&newfrag2->rb, root);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100290
291 return 0;
292 }
293 /* New node just reduces 'this' frag in size, doesn't split it */
294 this->size = newfrag->ofs - this->ofs;
295
296 /* Again, we know it lives down here in the tree */
297 jffs2_fragtree_insert(newfrag, this);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100298 rb_insert_color(&newfrag->rb, root);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100299 } else {
300 /* New frag starts at the same point as 'this' used to. Replace
301 it in the tree without doing a delete and insertion */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100302 JFFS2_DBG_FRAGTREE2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
303 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100304
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100305 rb_replace_node(&this->rb, &newfrag->rb, root);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100306
307 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100308 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100309 jffs2_obsolete_node_frag(c, this);
310 } else {
311 this->ofs += newfrag->size;
312 this->size -= newfrag->size;
313
314 jffs2_fragtree_insert(this, newfrag);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100315 rb_insert_color(&this->rb, root);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100316 return 0;
317 }
318 }
319 /* OK, now we have newfrag added in the correct place in the tree, but
320 frag_next(newfrag) may be a fragment which is overlapped by it
321 */
322 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
323 /* 'this' frag is obsoleted completely. */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100324 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n",
325 this, this->ofs, this->ofs+this->size);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100326 rb_erase(&this->rb, root);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100327 jffs2_obsolete_node_frag(c, this);
328 }
329 /* Now we're pointing at the first frag which isn't totally obsoleted by
330 the new frag */
331
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100332 if (!this || newfrag->ofs + newfrag->size == this->ofs)
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100333 return 0;
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100334
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100335 /* Still some overlap but we don't need to move it in the tree */
336 this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
337 this->ofs = newfrag->ofs + newfrag->size;
338
339 /* And mark them REF_NORMAL so the GC takes a look at them */
340 if (this->node)
341 mark_ref_normal(this->node->raw);
342 mark_ref_normal(newfrag->node->raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100347/*
348 * Given an inode, probably with existing tree of fragments, add the new node
349 * to the fragment tree.
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100350 */
351int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
352{
353 int ret;
354 struct jffs2_node_frag *newfrag;
355
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100356 if (unlikely(!fn->size))
357 return 0;
358
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100359 newfrag = new_fragment(fn, fn->ofs, fn->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100360 if (unlikely(!newfrag))
361 return -ENOMEM;
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100362 newfrag->node->frags = 1;
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100363
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100364 JFFS2_DBG_FRAGTREE("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
365 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100366
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100367 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
368 if (unlikely(ret))
369 return ret;
370
371 /* If we now share a page with other nodes, mark either previous
372 or next node REF_NORMAL, as appropriate. */
373 if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
374 struct jffs2_node_frag *prev = frag_prev(newfrag);
375
376 mark_ref_normal(fn->raw);
377 /* If we don't start at zero there's _always_ a previous */
378 if (prev->node)
379 mark_ref_normal(prev->node->raw);
380 }
381
382 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
383 struct jffs2_node_frag *next = frag_next(newfrag);
384
385 if (next) {
386 mark_ref_normal(fn->raw);
387 if (next->node)
388 mark_ref_normal(next->node->raw);
389 }
390 }
391 jffs2_dbg_fragtree_paranoia_check_nolock(f);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100392
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100393 return 0;
394}
395
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100396/*
397 * Check the data CRC of the node.
398 *
399 * Returns: 0 if the data CRC is correct;
400 * 1 - if incorrect;
401 * error code if an error occured.
402 */
403static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
404{
405 struct jffs2_raw_node_ref *ref = tn->fn->raw;
406 int err = 0, pointed = 0;
407 struct jffs2_eraseblock *jeb;
408 unsigned char *buffer;
409 uint32_t crc, ofs, retlen, len;
410
411 BUG_ON(tn->csize == 0);
412
413 /* Calculate how many bytes were already checked */
414 ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
Artem B. Bityutskiy3c091332005-08-04 12:40:02 +0100415 len = ofs & (c->wbuf_pagesize - 1);
Artem B. Bityutskiy280562b2005-08-17 15:57:43 +0100416 if (likely(len))
417 len = c->wbuf_pagesize - len;
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100418
419 if (len >= tn->csize) {
420 JFFS2_DBG_READINODE("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
421 ref_offset(ref), tn->csize, ofs);
422 goto adj_acc;
423 }
424
425 ofs += len;
426 len = tn->csize - len;
427
428 JFFS2_DBG_READINODE("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
429 ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
430
431#ifndef __ECOS
432 /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
433 * adding and jffs2_flash_read_end() interface. */
434 if (c->mtd->point) {
435 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
436 if (!err && retlen < tn->csize) {
437 JFFS2_WARNING("MTD point returned len too short: %u instead of %u.\n", retlen, tn->csize);
438 c->mtd->unpoint(c->mtd, buffer, ofs, len);
439 } else if (err)
440 JFFS2_WARNING("MTD point failed: error code %d.\n", err);
441 else
442 pointed = 1; /* succefully pointed to device */
443 }
444#endif
445
446 if (!pointed) {
447 buffer = kmalloc(len, GFP_KERNEL);
448 if (unlikely(!buffer))
449 return -ENOMEM;
450
451 /* TODO: this is very frequent pattern, make it a separate
452 * routine */
453 err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
454 if (err) {
455 JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
456 goto free_out;
457 }
458
459 if (retlen != len) {
460 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ofs, retlen, len);
461 err = -EIO;
462 goto free_out;
463 }
464 }
465
466 /* Continue calculating CRC */
467 crc = crc32(tn->partial_crc, buffer, len);
468 if(!pointed)
469 kfree(buffer);
470#ifndef __ECOS
471 else
472 c->mtd->unpoint(c->mtd, buffer, ofs, len);
473#endif
474
475 if (crc != tn->data_crc) {
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100476 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100477 ofs, tn->data_crc, crc);
478 return 1;
479 }
480
481adj_acc:
482 jeb = &c->blocks[ref->flash_offset / c->sector_size];
483 len = ref_totlen(c, jeb, ref);
484
485 /*
486 * Mark the node as having been checked and fix the
487 * accounting accordingly.
488 */
489 spin_lock(&c->erase_completion_lock);
490 jeb->used_size += len;
491 jeb->unchecked_size -= len;
492 c->used_size += len;
493 c->unchecked_size -= len;
494 spin_unlock(&c->erase_completion_lock);
495
496 return 0;
497
498free_out:
499 if(!pointed)
500 kfree(buffer);
501#ifndef __ECOS
502 else
503 c->mtd->unpoint(c->mtd, buffer, ofs, len);
504#endif
505 return err;
506}
507
508/*
509 * Helper function for jffs2_add_older_frag_to_fragtree().
510 *
511 * Checks the node if we are in the checking stage.
512 */
513static inline int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn)
514{
515 int ret;
516
517 BUG_ON(ref_obsolete(tn->fn->raw));
518
519 /* We only check the data CRC of unchecked nodes */
520 if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
521 return 0;
522
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100523 JFFS2_DBG_FRAGTREE2("check node %#04x-%#04x, phys offs %#08x.\n",
524 tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100525
526 ret = check_node_data(c, tn);
527 if (unlikely(ret < 0)) {
528 JFFS2_ERROR("check_node_data() returned error: %d.\n",
529 ret);
530 } else if (unlikely(ret > 0)) {
531 JFFS2_DBG_FRAGTREE2("CRC error, mark it obsolete.\n");
532 jffs2_mark_node_obsolete(c, tn->fn->raw);
533 }
534
535 return ret;
536}
537
538/*
539 * Helper function for jffs2_add_older_frag_to_fragtree().
540 *
541 * Called when the new fragment that is being inserted
542 * splits a hole fragment.
543 */
544static int split_hole(struct jffs2_sb_info *c, struct rb_root *root,
545 struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole)
546{
547 JFFS2_DBG_FRAGTREE2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
548 newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size);
549
550 if (hole->ofs == newfrag->ofs) {
551 /*
552 * Well, the new fragment actually starts at the same offset as
553 * the hole.
554 */
555 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
556 /*
557 * We replace the overlapped left part of the hole by
558 * the new node.
559 */
560
561 JFFS2_DBG_FRAGTREE2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
562 newfrag->ofs, newfrag->ofs + newfrag->size);
563 rb_replace_node(&hole->rb, &newfrag->rb, root);
564
565 hole->ofs += newfrag->size;
566 hole->size -= newfrag->size;
567
568 /*
569 * We know that 'hole' should be the right hand
570 * fragment.
571 */
572 jffs2_fragtree_insert(hole, newfrag);
573 rb_insert_color(&hole->rb, root);
574 } else {
575 /*
576 * Ah, the new fragment is of the same size as the hole.
577 * Relace the hole by it.
578 */
579 JFFS2_DBG_FRAGTREE2("insert fragment %#04x-%#04x and overwrite hole\n",
580 newfrag->ofs, newfrag->ofs + newfrag->size);
581 rb_replace_node(&hole->rb, &newfrag->rb, root);
582 jffs2_free_node_frag(hole);
583 }
584 } else {
585 /* The new fragment lefts some hole space at the left */
586
587 struct jffs2_node_frag * newfrag2 = NULL;
588
589 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
590 /* The new frag also lefts some space at the right */
591 newfrag2 = new_fragment(NULL, newfrag->ofs +
592 newfrag->size, hole->ofs + hole->size
593 - newfrag->ofs - newfrag->size);
594 if (unlikely(!newfrag2)) {
595 jffs2_free_node_frag(newfrag);
596 return -ENOMEM;
597 }
598 }
599
600 hole->size = newfrag->ofs - hole->ofs;
601 JFFS2_DBG_FRAGTREE2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
602 hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size);
603
604 jffs2_fragtree_insert(newfrag, hole);
605 rb_insert_color(&newfrag->rb, root);
606
607 if (newfrag2) {
608 JFFS2_DBG_FRAGTREE2("left the hole %#04x-%#04x at the right\n",
609 newfrag2->ofs, newfrag2->ofs + newfrag2->size);
610 jffs2_fragtree_insert(newfrag2, newfrag);
611 rb_insert_color(&newfrag2->rb, root);
612 }
613 }
614
615 return 0;
616}
617
618/*
619 * This function is used when we build inode. It expects the nodes are passed
620 * in the decreasing version order. The whole point of this is to improve the
621 * inodes checking on NAND: we check the nodes' data CRC only when they are not
622 * obsoleted. Previously, add_frag_to_fragtree() function was used and
623 * nodes were passed to it in the increasing version ordes and CRCs of all
624 * nodes were checked.
625 *
626 * Note: tn->fn->size shouldn't be zero.
627 *
628 * Returns 0 if the node was inserted
629 * 1 if it wasn't inserted (since it is obsolete)
630 * < 0 an if error occured
631 */
632int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
633 struct jffs2_tmp_dnode_info *tn)
634{
635 struct jffs2_node_frag *this, *newfrag;
636 uint32_t lastend;
637 struct jffs2_full_dnode *fn = tn->fn;
638 struct rb_root *root = &f->fragtree;
639 uint32_t fn_size = fn->size, fn_ofs = fn->ofs;
640 int err, checked = 0;
641 int ref_flag;
642
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100643 JFFS2_DBG_FRAGTREE("insert fragment %#04x-%#04x, ver %u\n", fn_ofs, fn_ofs + fn_size, tn->version);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100644
645 /* Skip all the nodes which are completed before this one starts */
646 this = jffs2_lookup_node_frag(root, fn_ofs);
647 if (this)
648 JFFS2_DBG_FRAGTREE2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole");
649
650 if (this)
651 lastend = this->ofs + this->size;
652 else
653 lastend = 0;
654
655 /* Detect the preliminary type of node */
656 if (fn->size >= PAGE_CACHE_SIZE)
657 ref_flag = REF_PRISTINE;
658 else
659 ref_flag = REF_NORMAL;
660
661 /* See if we ran off the end of the root */
662 if (lastend <= fn_ofs) {
663 /* We did */
664
665 /*
666 * We are going to insert the new node into the
667 * fragment tree, so check it.
668 */
669 err = check_node(c, f, tn);
670 if (err != 0)
671 return err;
672
673 fn->frags = 1;
674
675 newfrag = new_fragment(fn, fn_ofs, fn_size);
676 if (unlikely(!newfrag))
677 return -ENOMEM;
678
679 err = no_overlapping_node(c, root, newfrag, this, lastend);
680 if (unlikely(err != 0)) {
681 jffs2_free_node_frag(newfrag);
682 return err;
683 }
684
685 goto out_ok;
686 }
687
688 fn->frags = 0;
689
690 while (1) {
691 /*
692 * Here we have:
693 * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
694 *
695 * Remember, 'this' has higher version, any non-hole node
696 * which is already in the fragtree is newer then the newly
697 * inserted.
698 */
699 if (!this->node) {
700 /*
701 * 'this' is the hole fragment, so at least the
702 * beginning of the new fragment is valid.
703 */
704
705 /*
706 * We are going to insert the new node into the
707 * fragment tree, so check it.
708 */
709 if (!checked) {
710 err = check_node(c, f, tn);
711 if (unlikely(err != 0))
712 return err;
713 checked = 1;
714 }
715
716 if (this->ofs + this->size >= fn_ofs + fn_size) {
717 /* We split the hole on two parts */
718
719 fn->frags += 1;
720 newfrag = new_fragment(fn, fn_ofs, fn_size);
721 if (unlikely(!newfrag))
722 return -ENOMEM;
723
724 err = split_hole(c, root, newfrag, this);
725 if (unlikely(err))
726 return err;
727 goto out_ok;
728 }
729
730 /*
731 * The beginning of the new fragment is valid since it
732 * overlaps the hole node.
733 */
734
735 ref_flag = REF_NORMAL;
736
737 fn->frags += 1;
738 newfrag = new_fragment(fn, fn_ofs,
739 this->ofs + this->size - fn_ofs);
740 if (unlikely(!newfrag))
741 return -ENOMEM;
742
743 if (fn_ofs == this->ofs) {
744 /*
745 * The new node starts at the same offset as
746 * the hole and supersieds the hole.
747 */
748 JFFS2_DBG_FRAGTREE2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
749 fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
750
751 rb_replace_node(&this->rb, &newfrag->rb, root);
752 jffs2_free_node_frag(this);
753 } else {
754 /*
755 * The hole becomes shorter as its right part
756 * is supersieded by the new fragment.
757 */
758 JFFS2_DBG_FRAGTREE2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
759 this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size);
760
761 JFFS2_DBG_FRAGTREE2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs,
762 fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
763
764 this->size -= newfrag->size;
765 jffs2_fragtree_insert(newfrag, this);
766 rb_insert_color(&newfrag->rb, root);
767 }
768
769 fn_ofs += newfrag->size;
770 fn_size -= newfrag->size;
771 this = rb_entry(rb_next(&newfrag->rb),
772 struct jffs2_node_frag, rb);
773
774 JFFS2_DBG_FRAGTREE2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
775 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
776 }
777
778 /*
779 * 'This' node is not the hole so it obsoletes the new fragment
780 * either fully or partially.
781 */
782 if (this->ofs + this->size >= fn_ofs + fn_size) {
783 /* The new node is obsolete, drop it */
784 if (fn->frags == 0) {
785 JFFS2_DBG_FRAGTREE2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size);
786 ref_flag = REF_OBSOLETE;
787 }
788 goto out_ok;
789 } else {
790 struct jffs2_node_frag *new_this;
791
792 /* 'This' node obsoletes the beginning of the new node */
793 JFFS2_DBG_FRAGTREE2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size);
794
795 ref_flag = REF_NORMAL;
796
797 fn_size -= this->ofs + this->size - fn_ofs;
798 fn_ofs = this->ofs + this->size;
799 JFFS2_DBG_FRAGTREE2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
800
801 new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb);
802 if (!new_this) {
803 /*
804 * There is no next fragment. Add the rest of
805 * the new node as the right-hand child.
806 */
807 if (!checked) {
808 err = check_node(c, f, tn);
809 if (unlikely(err != 0))
810 return err;
811 checked = 1;
812 }
813
814 fn->frags += 1;
815 newfrag = new_fragment(fn, fn_ofs, fn_size);
816 if (unlikely(!newfrag))
817 return -ENOMEM;
818
819 JFFS2_DBG_FRAGTREE2("there are no more fragments, insert %#04x-%#04x\n",
820 newfrag->ofs, newfrag->ofs + newfrag->size);
821 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
822 rb_insert_color(&newfrag->rb, root);
823 goto out_ok;
824 } else {
825 this = new_this;
826 JFFS2_DBG_FRAGTREE2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
827 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
828 }
829 }
830 }
831
832out_ok:
833 BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE);
834
835 if (ref_flag == REF_OBSOLETE) {
836 JFFS2_DBG_FRAGTREE2("the node is obsolete now\n");
837 /* jffs2_mark_node_obsolete() will adjust space accounting */
838 jffs2_mark_node_obsolete(c, fn->raw);
839 return 1;
840 }
841
842 JFFS2_DBG_FRAGTREE2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE");
843
844 /* Space accounting was adjusted at check_node_data() */
845 spin_lock(&c->erase_completion_lock);
846 fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag;
847 spin_unlock(&c->erase_completion_lock);
848
849 return 0;
850}
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
853{
854 spin_lock(&c->inocache_lock);
855 ic->state = state;
856 wake_up(&c->inocache_wq);
857 spin_unlock(&c->inocache_lock);
858}
859
860/* During mount, this needs no locking. During normal operation, its
861 callers want to do other stuff while still holding the inocache_lock.
862 Rather than introducing special case get_ino_cache functions or
863 callbacks, we just let the caller do the locking itself. */
864
865struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
866{
867 struct jffs2_inode_cache *ret;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
870 while (ret && ret->ino < ino) {
871 ret = ret->next;
872 }
873
874 if (ret && ret->ino != ino)
875 ret = NULL;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return ret;
878}
879
880void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
881{
882 struct jffs2_inode_cache **prev;
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 spin_lock(&c->inocache_lock);
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200885 if (!new->ino)
886 new->ino = ++c->highest_ino;
887
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100888 JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino);
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
891
892 while ((*prev) && (*prev)->ino < new->ino) {
893 prev = &(*prev)->next;
894 }
895 new->next = *prev;
896 *prev = new;
897
898 spin_unlock(&c->inocache_lock);
899}
900
901void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
902{
903 struct jffs2_inode_cache **prev;
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100904
905 JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 spin_lock(&c->inocache_lock);
907
908 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
909
910 while ((*prev) && (*prev)->ino < old->ino) {
911 prev = &(*prev)->next;
912 }
913 if ((*prev) == old) {
914 *prev = old->next;
915 }
916
David Woodhouse67e345d2005-02-27 23:01:36 +0000917 /* Free it now unless it's in READING or CLEARING state, which
918 are the transitions upon read_inode() and clear_inode(). The
919 rest of the time we know nobody else is looking at it, and
920 if it's held by read_inode() or clear_inode() they'll free it
921 for themselves. */
922 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
923 jffs2_free_inode_cache(old);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 spin_unlock(&c->inocache_lock);
926}
927
928void jffs2_free_ino_caches(struct jffs2_sb_info *c)
929{
930 int i;
931 struct jffs2_inode_cache *this, *next;
932
933 for (i=0; i<INOCACHE_HASHSIZE; i++) {
934 this = c->inocache_list[i];
935 while (this) {
936 next = this->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 jffs2_free_inode_cache(this);
938 this = next;
939 }
940 c->inocache_list[i] = NULL;
941 }
942}
943
944void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
945{
946 int i;
947 struct jffs2_raw_node_ref *this, *next;
948
949 for (i=0; i<c->nr_blocks; i++) {
950 this = c->blocks[i].first_node;
951 while(this) {
952 next = this->next_phys;
953 jffs2_free_raw_node_ref(this);
954 this = next;
955 }
956 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
957 }
958}
959
960struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
961{
962 /* The common case in lookup is that there will be a node
963 which precisely matches. So we go looking for that first */
964 struct rb_node *next;
965 struct jffs2_node_frag *prev = NULL;
966 struct jffs2_node_frag *frag = NULL;
967
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100968 JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 next = fragtree->rb_node;
971
972 while(next) {
973 frag = rb_entry(next, struct jffs2_node_frag, rb);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (frag->ofs + frag->size <= offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Remember the closest smaller match on the way down */
977 if (!prev || frag->ofs > prev->ofs)
978 prev = frag;
979 next = frag->rb.rb_right;
980 } else if (frag->ofs > offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 next = frag->rb.rb_left;
982 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return frag;
984 }
985 }
986
987 /* Exact match not found. Go back up looking at each parent,
988 and return the closest smaller one */
989
990 if (prev)
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100991 JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n",
992 prev->ofs, prev->ofs+prev->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 else
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100994 JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 return prev;
997}
998
999/* Pass 'c' argument to indicate that nodes should be marked obsolete as
1000 they're killed. */
1001void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
1002{
1003 struct jffs2_node_frag *frag;
1004 struct jffs2_node_frag *parent;
1005
1006 if (!root->rb_node)
1007 return;
1008
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +01001009 JFFS2_DBG_FRAGTREE("killing\n");
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 while(frag) {
1013 if (frag->rb.rb_left) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 frag = frag_left(frag);
1015 continue;
1016 }
1017 if (frag->rb.rb_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 frag = frag_right(frag);
1019 continue;
1020 }
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (frag->node && !(--frag->node->frags)) {
1023 /* Not a hole, and it's the final remaining frag
1024 of this node. Free the node */
1025 if (c)
1026 jffs2_mark_node_obsolete(c, frag->node->raw);
1027
1028 jffs2_free_full_dnode(frag->node);
1029 }
1030 parent = frag_parent(frag);
1031 if (parent) {
1032 if (frag_left(parent) == frag)
1033 parent->rb.rb_left = NULL;
1034 else
1035 parent->rb.rb_right = NULL;
1036 }
1037
1038 jffs2_free_node_frag(frag);
1039 frag = parent;
1040
1041 cond_resched();
1042 }
1043}