blob: 1d46677afd172b5742a4ccd2472b4d59f82a2800 [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 *
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000010 * $Id: nodelist.c,v 1.115 2005/11/07 11:14:40 gleixner 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;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000027
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010028 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. Bityutskiy733802d2005-09-22 12:25:00 +010034 dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010035 (*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. Bityutskiy733802d2005-09-22 12:25:00 +010039 dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010040 (*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
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010058 dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010059
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
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000078 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +010079 * 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);
Artem B. Bityutskiyf0507532005-08-22 10:07:12 +010083 if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010084 dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000085 frag->ofs, frag->ofs + frag->size);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +010086 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. Bityutskiy733802d2005-09-22 12:25:00 +010096 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010097 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. Bityutskiy733802d2005-09-22 12:25:00 +0100101 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100102 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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000105
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. Bityutskiy733802d2005-09-22 12:25:00 +0100115 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);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000120
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 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800137static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100138{
139 struct jffs2_node_frag *newfrag;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000140
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100141 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) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000172 /* By definition, the 'this' node has no right-hand child,
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100173 because there are no frags with offset greater than it.
174 So that's where we want to put the hole */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100175 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. Bityutskiy733802d2005-09-22 12:25:00 +0100179 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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000186
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100187 if (this) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000188 /* By definition, the 'this' node has no right-hand child,
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100189 because there are no frags with offset greater than it.
190 So that's where we want to put new fragment */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100191 dbg_fragtree2("add the new node at the right\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000192 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100193 } else {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100194 dbg_fragtree2("insert the new node at the root of the tree\n");
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100195 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. Bityutskiy733802d2005-09-22 12:25:00 +0100212 dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100213 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. Bityutskiy733802d2005-09-22 12:25:00 +0100216 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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000219
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)
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100238 dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100239 this->ofs, this->ofs + this->size,
240 ref_offset(this->node->raw), ref_flags(this->node->raw));
241 else
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100242 dbg_fragtree2("dealing with hole frag %u-%u.\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100243 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,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000246 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100247 */
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. Bityutskiy733802d2005-09-22 12:25:00 +0100262 dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100263 this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000264 else
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100265 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);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000267
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100268 /* 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);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000287
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100288 jffs2_fragtree_insert(newfrag2, newfrag);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100289 rb_insert_color(&newfrag2->rb, root);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000290
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100291 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 {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000300 /* New frag starts at the same point as 'this' used to. Replace
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100301 it in the tree without doing a delete and insertion */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100302 dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100303 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000304
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100305 rb_replace_node(&this->rb, &newfrag->rb, root);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000306
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100307 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100308 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
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000320 frag_next(newfrag) may be a fragment which is overlapped by it
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100321 */
322 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
323 /* 'this' frag is obsoleted completely. */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100324 dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100325 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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000329 /* Now we're pointing at the first frag which isn't totally obsoleted by
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100330 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
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000347/*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100348 * 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. Bityutskiy733802d2005-09-22 12:25:00 +0100364 dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100365 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000366
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);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000377 /* If we don't start at zero there's _always_ a previous */
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100378 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);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000384
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100385 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;
Atsushi Nemoto0ef675d2006-03-09 17:33:38 -0800409 uint32_t crc, ofs, len;
410 size_t retlen;
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100411
412 BUG_ON(tn->csize == 0);
413
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100414 if (!jffs2_is_writebuffered(c))
415 goto adj_acc;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000416
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100417 /* Calculate how many bytes were already checked */
418 ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100419 len = ofs % c->wbuf_pagesize;
Artem B. Bityutskiy280562b2005-08-17 15:57:43 +0100420 if (likely(len))
421 len = c->wbuf_pagesize - len;
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100422
423 if (len >= tn->csize) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100424 dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100425 ref_offset(ref), tn->csize, ofs);
426 goto adj_acc;
427 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000428
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100429 ofs += len;
430 len = tn->csize - len;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000431
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100432 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",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100433 ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000434
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100435#ifndef __ECOS
436 /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
437 * adding and jffs2_flash_read_end() interface. */
438 if (c->mtd->point) {
439 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
440 if (!err && retlen < tn->csize) {
Andrew Morton194a61b2006-05-15 09:44:42 -0700441 JFFS2_WARNING("MTD point returned len too short: %zu "
442 "instead of %u.\n", retlen, tn->csize);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100443 c->mtd->unpoint(c->mtd, buffer, ofs, len);
444 } else if (err)
445 JFFS2_WARNING("MTD point failed: error code %d.\n", err);
446 else
447 pointed = 1; /* succefully pointed to device */
448 }
449#endif
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000450
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100451 if (!pointed) {
452 buffer = kmalloc(len, GFP_KERNEL);
453 if (unlikely(!buffer))
454 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000455
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100456 /* TODO: this is very frequent pattern, make it a separate
457 * routine */
458 err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
459 if (err) {
460 JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
461 goto free_out;
462 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000463
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100464 if (retlen != len) {
Andrew Morton194a61b2006-05-15 09:44:42 -0700465 JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n",
466 ofs, retlen, len);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100467 err = -EIO;
468 goto free_out;
469 }
470 }
471
472 /* Continue calculating CRC */
473 crc = crc32(tn->partial_crc, buffer, len);
474 if(!pointed)
475 kfree(buffer);
476#ifndef __ECOS
477 else
478 c->mtd->unpoint(c->mtd, buffer, ofs, len);
479#endif
480
481 if (crc != tn->data_crc) {
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100482 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100483 ofs, tn->data_crc, crc);
484 return 1;
485 }
486
487adj_acc:
488 jeb = &c->blocks[ref->flash_offset / c->sector_size];
489 len = ref_totlen(c, jeb, ref);
490
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000491 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100492 * Mark the node as having been checked and fix the
493 * accounting accordingly.
494 */
495 spin_lock(&c->erase_completion_lock);
496 jeb->used_size += len;
497 jeb->unchecked_size -= len;
498 c->used_size += len;
499 c->unchecked_size -= len;
500 spin_unlock(&c->erase_completion_lock);
501
502 return 0;
503
504free_out:
505 if(!pointed)
506 kfree(buffer);
507#ifndef __ECOS
508 else
509 c->mtd->unpoint(c->mtd, buffer, ofs, len);
510#endif
511 return err;
512}
513
514/*
515 * Helper function for jffs2_add_older_frag_to_fragtree().
516 *
517 * Checks the node if we are in the checking stage.
518 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800519static int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn)
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100520{
521 int ret;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000522
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100523 BUG_ON(ref_obsolete(tn->fn->raw));
524
525 /* We only check the data CRC of unchecked nodes */
526 if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
527 return 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000528
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100529 dbg_fragtree2("check node %#04x-%#04x, phys offs %#08x.\n",
Artem B. Bityutskiy39243502005-08-03 10:26:50 +0100530 tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100531
532 ret = check_node_data(c, tn);
533 if (unlikely(ret < 0)) {
534 JFFS2_ERROR("check_node_data() returned error: %d.\n",
535 ret);
536 } else if (unlikely(ret > 0)) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100537 dbg_fragtree2("CRC error, mark it obsolete.\n");
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100538 jffs2_mark_node_obsolete(c, tn->fn->raw);
539 }
540
541 return ret;
542}
543
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000544/*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100545 * Helper function for jffs2_add_older_frag_to_fragtree().
546 *
547 * Called when the new fragment that is being inserted
548 * splits a hole fragment.
549 */
550static int split_hole(struct jffs2_sb_info *c, struct rb_root *root,
551 struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole)
552{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100553 dbg_fragtree2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100554 newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size);
555
556 if (hole->ofs == newfrag->ofs) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000557 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100558 * Well, the new fragment actually starts at the same offset as
559 * the hole.
560 */
561 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000562 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100563 * We replace the overlapped left part of the hole by
564 * the new node.
565 */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000566
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100567 dbg_fragtree2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100568 newfrag->ofs, newfrag->ofs + newfrag->size);
569 rb_replace_node(&hole->rb, &newfrag->rb, root);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000570
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100571 hole->ofs += newfrag->size;
572 hole->size -= newfrag->size;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000573
574 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100575 * We know that 'hole' should be the right hand
576 * fragment.
577 */
578 jffs2_fragtree_insert(hole, newfrag);
579 rb_insert_color(&hole->rb, root);
580 } else {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000581 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100582 * Ah, the new fragment is of the same size as the hole.
583 * Relace the hole by it.
584 */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100585 dbg_fragtree2("insert fragment %#04x-%#04x and overwrite hole\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100586 newfrag->ofs, newfrag->ofs + newfrag->size);
587 rb_replace_node(&hole->rb, &newfrag->rb, root);
588 jffs2_free_node_frag(hole);
589 }
590 } else {
591 /* The new fragment lefts some hole space at the left */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000592
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100593 struct jffs2_node_frag * newfrag2 = NULL;
594
595 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
596 /* The new frag also lefts some space at the right */
597 newfrag2 = new_fragment(NULL, newfrag->ofs +
598 newfrag->size, hole->ofs + hole->size
599 - newfrag->ofs - newfrag->size);
600 if (unlikely(!newfrag2)) {
601 jffs2_free_node_frag(newfrag);
602 return -ENOMEM;
603 }
604 }
605
606 hole->size = newfrag->ofs - hole->ofs;
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100607 dbg_fragtree2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100608 hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size);
609
610 jffs2_fragtree_insert(newfrag, hole);
611 rb_insert_color(&newfrag->rb, root);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000612
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100613 if (newfrag2) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100614 dbg_fragtree2("left the hole %#04x-%#04x at the right\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100615 newfrag2->ofs, newfrag2->ofs + newfrag2->size);
616 jffs2_fragtree_insert(newfrag2, newfrag);
617 rb_insert_color(&newfrag2->rb, root);
618 }
619 }
620
621 return 0;
622}
623
624/*
625 * This function is used when we build inode. It expects the nodes are passed
626 * in the decreasing version order. The whole point of this is to improve the
627 * inodes checking on NAND: we check the nodes' data CRC only when they are not
628 * obsoleted. Previously, add_frag_to_fragtree() function was used and
629 * nodes were passed to it in the increasing version ordes and CRCs of all
630 * nodes were checked.
631 *
632 * Note: tn->fn->size shouldn't be zero.
633 *
634 * Returns 0 if the node was inserted
635 * 1 if it wasn't inserted (since it is obsolete)
636 * < 0 an if error occured
637 */
638int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
639 struct jffs2_tmp_dnode_info *tn)
640{
641 struct jffs2_node_frag *this, *newfrag;
642 uint32_t lastend;
643 struct jffs2_full_dnode *fn = tn->fn;
644 struct rb_root *root = &f->fragtree;
645 uint32_t fn_size = fn->size, fn_ofs = fn->ofs;
646 int err, checked = 0;
647 int ref_flag;
648
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100649 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 +0100650
651 /* Skip all the nodes which are completed before this one starts */
652 this = jffs2_lookup_node_frag(root, fn_ofs);
653 if (this)
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100654 dbg_fragtree2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole");
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100655
656 if (this)
657 lastend = this->ofs + this->size;
658 else
659 lastend = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000660
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100661 /* Detect the preliminary type of node */
662 if (fn->size >= PAGE_CACHE_SIZE)
663 ref_flag = REF_PRISTINE;
664 else
665 ref_flag = REF_NORMAL;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000666
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100667 /* See if we ran off the end of the root */
668 if (lastend <= fn_ofs) {
669 /* We did */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000670
671 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100672 * We are going to insert the new node into the
673 * fragment tree, so check it.
674 */
675 err = check_node(c, f, tn);
676 if (err != 0)
677 return err;
678
679 fn->frags = 1;
680
681 newfrag = new_fragment(fn, fn_ofs, fn_size);
682 if (unlikely(!newfrag))
683 return -ENOMEM;
684
685 err = no_overlapping_node(c, root, newfrag, this, lastend);
686 if (unlikely(err != 0)) {
687 jffs2_free_node_frag(newfrag);
688 return err;
689 }
690
691 goto out_ok;
692 }
693
694 fn->frags = 0;
695
696 while (1) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000697 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100698 * Here we have:
699 * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000700 *
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100701 * Remember, 'this' has higher version, any non-hole node
702 * which is already in the fragtree is newer then the newly
703 * inserted.
704 */
705 if (!this->node) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000706 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100707 * 'this' is the hole fragment, so at least the
708 * beginning of the new fragment is valid.
709 */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000710
711 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100712 * We are going to insert the new node into the
713 * fragment tree, so check it.
714 */
715 if (!checked) {
716 err = check_node(c, f, tn);
717 if (unlikely(err != 0))
718 return err;
719 checked = 1;
720 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000721
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100722 if (this->ofs + this->size >= fn_ofs + fn_size) {
723 /* We split the hole on two parts */
724
725 fn->frags += 1;
726 newfrag = new_fragment(fn, fn_ofs, fn_size);
727 if (unlikely(!newfrag))
728 return -ENOMEM;
729
730 err = split_hole(c, root, newfrag, this);
731 if (unlikely(err))
732 return err;
733 goto out_ok;
734 }
735
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000736 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100737 * The beginning of the new fragment is valid since it
738 * overlaps the hole node.
739 */
740
741 ref_flag = REF_NORMAL;
742
743 fn->frags += 1;
744 newfrag = new_fragment(fn, fn_ofs,
745 this->ofs + this->size - fn_ofs);
746 if (unlikely(!newfrag))
747 return -ENOMEM;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000748
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100749 if (fn_ofs == this->ofs) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000750 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100751 * The new node starts at the same offset as
752 * the hole and supersieds the hole.
753 */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100754 dbg_fragtree2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100755 fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
756
757 rb_replace_node(&this->rb, &newfrag->rb, root);
758 jffs2_free_node_frag(this);
759 } else {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000760 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100761 * The hole becomes shorter as its right part
762 * is supersieded by the new fragment.
763 */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100764 dbg_fragtree2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100765 this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000766
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100767 dbg_fragtree2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs,
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100768 fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000769
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100770 this->size -= newfrag->size;
771 jffs2_fragtree_insert(newfrag, this);
772 rb_insert_color(&newfrag->rb, root);
773 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000774
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100775 fn_ofs += newfrag->size;
776 fn_size -= newfrag->size;
777 this = rb_entry(rb_next(&newfrag->rb),
778 struct jffs2_node_frag, rb);
779
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100780 dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100781 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
782 }
783
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000784 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100785 * 'This' node is not the hole so it obsoletes the new fragment
786 * either fully or partially.
787 */
788 if (this->ofs + this->size >= fn_ofs + fn_size) {
789 /* The new node is obsolete, drop it */
790 if (fn->frags == 0) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100791 dbg_fragtree2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100792 ref_flag = REF_OBSOLETE;
793 }
794 goto out_ok;
795 } else {
796 struct jffs2_node_frag *new_this;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000797
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100798 /* 'This' node obsoletes the beginning of the new node */
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100799 dbg_fragtree2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100800
801 ref_flag = REF_NORMAL;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000802
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100803 fn_size -= this->ofs + this->size - fn_ofs;
804 fn_ofs = this->ofs + this->size;
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100805 dbg_fragtree2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000806
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100807 new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb);
808 if (!new_this) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000809 /*
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100810 * There is no next fragment. Add the rest of
811 * the new node as the right-hand child.
812 */
813 if (!checked) {
814 err = check_node(c, f, tn);
815 if (unlikely(err != 0))
816 return err;
817 checked = 1;
818 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000819
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100820 fn->frags += 1;
821 newfrag = new_fragment(fn, fn_ofs, fn_size);
822 if (unlikely(!newfrag))
823 return -ENOMEM;
824
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100825 dbg_fragtree2("there are no more fragments, insert %#04x-%#04x\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100826 newfrag->ofs, newfrag->ofs + newfrag->size);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000827 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100828 rb_insert_color(&newfrag->rb, root);
829 goto out_ok;
830 } else {
831 this = new_this;
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100832 dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100833 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
834 }
835 }
836 }
837
838out_ok:
839 BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE);
840
841 if (ref_flag == REF_OBSOLETE) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100842 dbg_fragtree2("the node is obsolete now\n");
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100843 /* jffs2_mark_node_obsolete() will adjust space accounting */
844 jffs2_mark_node_obsolete(c, fn->raw);
845 return 1;
846 }
847
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100848 dbg_fragtree2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE");
Artem B. Bityutskiy1e0da3c2005-08-01 13:05:22 +0100849
850 /* Space accounting was adjusted at check_node_data() */
851 spin_lock(&c->erase_completion_lock);
852 fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag;
853 spin_unlock(&c->erase_completion_lock);
854
855 return 0;
856}
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
859{
860 spin_lock(&c->inocache_lock);
861 ic->state = state;
862 wake_up(&c->inocache_wq);
863 spin_unlock(&c->inocache_lock);
864}
865
866/* During mount, this needs no locking. During normal operation, its
867 callers want to do other stuff while still holding the inocache_lock.
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000868 Rather than introducing special case get_ino_cache functions or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 callbacks, we just let the caller do the locking itself. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
872{
873 struct jffs2_inode_cache *ret;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
876 while (ret && ret->ino < ino) {
877 ret = ret->next;
878 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (ret && ret->ino != ino)
881 ret = NULL;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return ret;
884}
885
886void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
887{
888 struct jffs2_inode_cache **prev;
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 spin_lock(&c->inocache_lock);
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200891 if (!new->ino)
892 new->ino = ++c->highest_ino;
893
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100894 dbg_inocache("add %p (ino #%u)\n", new, new->ino);
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
897
898 while ((*prev) && (*prev)->ino < new->ino) {
899 prev = &(*prev)->next;
900 }
901 new->next = *prev;
902 *prev = new;
903
904 spin_unlock(&c->inocache_lock);
905}
906
907void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
908{
909 struct jffs2_inode_cache **prev;
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100910
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100911 dbg_inocache("del %p (ino #%u)\n", old, old->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_lock(&c->inocache_lock);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 while ((*prev) && (*prev)->ino < old->ino) {
917 prev = &(*prev)->next;
918 }
919 if ((*prev) == old) {
920 *prev = old->next;
921 }
922
David Woodhouse67e345d2005-02-27 23:01:36 +0000923 /* Free it now unless it's in READING or CLEARING state, which
924 are the transitions upon read_inode() and clear_inode(). The
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000925 rest of the time we know nobody else is looking at it, and
David Woodhouse67e345d2005-02-27 23:01:36 +0000926 if it's held by read_inode() or clear_inode() they'll free it
927 for themselves. */
928 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
929 jffs2_free_inode_cache(old);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 spin_unlock(&c->inocache_lock);
932}
933
934void jffs2_free_ino_caches(struct jffs2_sb_info *c)
935{
936 int i;
937 struct jffs2_inode_cache *this, *next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 for (i=0; i<INOCACHE_HASHSIZE; i++) {
940 this = c->inocache_list[i];
941 while (this) {
942 next = this->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 jffs2_free_inode_cache(this);
944 this = next;
945 }
946 c->inocache_list[i] = NULL;
947 }
948}
949
950void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
951{
952 int i;
953 struct jffs2_raw_node_ref *this, *next;
954
955 for (i=0; i<c->nr_blocks; i++) {
956 this = c->blocks[i].first_node;
957 while(this) {
958 next = this->next_phys;
959 jffs2_free_raw_node_ref(this);
960 this = next;
961 }
962 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
963 }
964}
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
967{
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000968 /* The common case in lookup is that there will be a node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 which precisely matches. So we go looking for that first */
970 struct rb_node *next;
971 struct jffs2_node_frag *prev = NULL;
972 struct jffs2_node_frag *frag = NULL;
973
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100974 dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 next = fragtree->rb_node;
977
978 while(next) {
979 frag = rb_entry(next, struct jffs2_node_frag, rb);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (frag->ofs + frag->size <= offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* Remember the closest smaller match on the way down */
983 if (!prev || frag->ofs > prev->ofs)
984 prev = frag;
985 next = frag->rb.rb_right;
986 } else if (frag->ofs > offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 next = frag->rb.rb_left;
988 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return frag;
990 }
991 }
992
993 /* Exact match not found. Go back up looking at each parent,
994 and return the closest smaller one */
995
996 if (prev)
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100997 dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100998 prev->ofs, prev->ofs+prev->size);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000999 else
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +01001000 dbg_fragtree2("returning NULL, empty fragtree\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return prev;
1003}
1004
1005/* Pass 'c' argument to indicate that nodes should be marked obsolete as
1006 they're killed. */
1007void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
1008{
1009 struct jffs2_node_frag *frag;
1010 struct jffs2_node_frag *parent;
1011
1012 if (!root->rb_node)
1013 return;
1014
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +01001015 dbg_fragtree("killing\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 while(frag) {
1019 if (frag->rb.rb_left) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 frag = frag_left(frag);
1021 continue;
1022 }
1023 if (frag->rb.rb_right) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 frag = frag_right(frag);
1025 continue;
1026 }
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (frag->node && !(--frag->node->frags)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001029 /* Not a hole, and it's the final remaining frag
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 of this node. Free the node */
1031 if (c)
1032 jffs2_mark_node_obsolete(c, frag->node->raw);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 jffs2_free_full_dnode(frag->node);
1035 }
1036 parent = frag_parent(frag);
1037 if (parent) {
1038 if (frag_left(parent) == frag)
1039 parent->rb.rb_left = NULL;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001040 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 parent->rb.rb_right = NULL;
1042 }
1043
1044 jffs2_free_node_frag(frag);
1045 frag = parent;
1046
1047 cond_resched();
1048 }
1049}