Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 10 | * $Id: nodelist.c,v 1.115 2005/11/07 11:14:40 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 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 | |
| 24 | void 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 27 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 28 | dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 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. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 34 | dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 35 | (*prev)->name, (*prev)->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | jffs2_mark_node_obsolete(c, new->raw); |
| 37 | jffs2_free_full_dirent(new); |
| 38 | } else { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 39 | dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 40 | (*prev)->name, (*prev)->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | new->next = (*prev)->next; |
| 42 | jffs2_mark_node_obsolete(c, ((*prev)->raw)); |
| 43 | jffs2_free_full_dirent(*prev); |
| 44 | *prev = new; |
| 45 | } |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 46 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | prev = &((*prev)->next); |
| 49 | } |
| 50 | new->next = *prev; |
| 51 | *prev = new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Artem B. Bityutskiy | 1e90097 | 2005-07-31 09:20:48 +0100 | [diff] [blame] | 54 | void 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. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 58 | dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size); |
Artem B. Bityutskiy | 1e90097 | 2005-07-31 09:20:48 +0100 | [diff] [blame] | 59 | |
| 60 | /* We know frag->ofs <= size. That's what lookup does for us */ |
| 61 | if (frag && frag->ofs != size) { |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 62 | if (frag->ofs+frag->size > size) { |
Artem B. Bityutskiy | 1e90097 | 2005-07-31 09:20:48 +0100 | [diff] [blame] | 63 | 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. Bityutskiy | 1e90097 | 2005-07-31 09:20:48 +0100 | [diff] [blame] | 70 | frag_erase(frag, list); |
| 71 | jffs2_obsolete_node_frag(c, frag); |
| 72 | frag = next; |
| 73 | } |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 74 | |
| 75 | if (size == 0) |
| 76 | return; |
| 77 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 78 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 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); |
Artem B. Bityutskiy | f050753 | 2005-08-22 10:07:12 +0100 | [diff] [blame] | 83 | if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 84 | dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n", |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 85 | frag->ofs, frag->ofs + frag->size); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 86 | frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE; |
| 87 | } |
Artem B. Bityutskiy | 1e90097 | 2005-07-31 09:20:48 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 90 | void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 92 | 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. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 96 | dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 97 | ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 98 | jffs2_mark_node_obsolete(c, this->node->raw); |
| 99 | jffs2_free_full_dnode(this->node); |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 100 | } else { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 101 | dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 102 | ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 103 | mark_ref_normal(this->node->raw); |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 104 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 105 | |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 106 | } |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 107 | jffs2_free_node_frag(this); |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 110 | static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base) |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 111 | { |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 112 | struct rb_node *parent = &base->rb; |
| 113 | struct rb_node **link = &parent; |
| 114 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 115 | dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 116 | |
| 117 | while (*link) { |
| 118 | parent = *link; |
| 119 | base = rb_entry(parent, struct jffs2_node_frag, rb); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 120 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 121 | 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. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 126 | JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base); |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 127 | BUG(); |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 131 | rb_link_node(&newfrag->rb, &base->rb, link); |
Artem B. Bityutskiy | dae6227 | 2005-07-15 11:13:57 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 134 | /* |
| 135 | * Allocate and initializes a new fragment. |
| 136 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 137 | static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size) |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 138 | { |
| 139 | struct jffs2_node_frag *newfrag; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 140 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 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 | */ |
| 157 | static 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 172 | /* By definition, the 'this' node has no right-hand child, |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 173 | because there are no frags with offset greater than it. |
| 174 | So that's where we want to put the hole */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 175 | dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 176 | holefrag->ofs, holefrag->ofs + holefrag->size); |
| 177 | rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right); |
| 178 | } else { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 179 | dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 180 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 186 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 187 | if (this) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 188 | /* By definition, the 'this' node has no right-hand child, |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 189 | because there are no frags with offset greater than it. |
| 190 | So that's where we want to put new fragment */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 191 | dbg_fragtree2("add the new node at the right\n"); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 192 | rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 193 | } else { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 194 | dbg_fragtree2("insert the new node at the root of the tree\n"); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 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. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 202 | /* Doesn't set inode->i_size */ |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 203 | static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 205 | struct jffs2_node_frag *this; |
| 206 | uint32_t lastend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 208 | /* Skip all the nodes which are completed before this one starts */ |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 209 | this = jffs2_lookup_node_frag(root, newfrag->node->ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 211 | if (this) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 212 | dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 213 | this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 214 | lastend = this->ofs + this->size; |
| 215 | } else { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 216 | dbg_fragtree2("lookup gave no frag\n"); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 217 | lastend = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 219 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 220 | /* See if we ran off the end of the fragtree */ |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 221 | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 234 | return no_overlapping_node(c, root, newfrag, this, lastend); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 237 | if (this->node) |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 238 | dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 239 | this->ofs, this->ofs + this->size, |
| 240 | ref_offset(this->node->raw), ref_flags(this->node->raw)); |
| 241 | else |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 242 | dbg_fragtree2("dealing with hole frag %u-%u.\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 243 | this->ofs, this->ofs + this->size); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 244 | |
| 245 | /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes, |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 246 | * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 259 | struct jffs2_node_frag *newfrag2; |
| 260 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 261 | if (this->node) |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 262 | dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 263 | this->ofs, this->ofs+this->size, ref_offset(this->node->raw)); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 264 | else |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 265 | dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n", |
Artem B. Bityutskiy | 8d5df40 | 2005-08-17 15:13:48 +0100 | [diff] [blame] | 266 | this->ofs, this->ofs+this->size); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 267 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 268 | /* New second frag pointing to this's node */ |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 269 | 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. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 273 | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 286 | rb_insert_color(&newfrag->rb, root); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 287 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 288 | jffs2_fragtree_insert(newfrag2, newfrag); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 289 | rb_insert_color(&newfrag2->rb, root); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 290 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 298 | rb_insert_color(&newfrag->rb, root); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 299 | } else { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 300 | /* New frag starts at the same point as 'this' used to. Replace |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 301 | it in the tree without doing a delete and insertion */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 302 | dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 303 | newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 304 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 305 | rb_replace_node(&this->rb, &newfrag->rb, root); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 306 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 307 | if (newfrag->ofs + newfrag->size >= this->ofs+this->size) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 308 | dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 309 | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 315 | rb_insert_color(&this->rb, root); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | } |
| 319 | /* OK, now we have newfrag added in the correct place in the tree, but |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 320 | frag_next(newfrag) may be a fragment which is overlapped by it |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 321 | */ |
| 322 | while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) { |
| 323 | /* 'this' frag is obsoleted completely. */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 324 | dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 325 | this, this->ofs, this->ofs+this->size); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 326 | rb_erase(&this->rb, root); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 327 | jffs2_obsolete_node_frag(c, this); |
| 328 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 329 | /* Now we're pointing at the first frag which isn't totally obsoleted by |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 330 | the new frag */ |
| 331 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 332 | if (!this || newfrag->ofs + newfrag->size == this->ofs) |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 333 | return 0; |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 334 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 335 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 347 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 348 | * Given an inode, probably with existing tree of fragments, add the new node |
| 349 | * to the fragment tree. |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 350 | */ |
| 351 | int 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. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 356 | if (unlikely(!fn->size)) |
| 357 | return 0; |
| 358 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 359 | newfrag = new_fragment(fn, fn->ofs, fn->size); |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 360 | if (unlikely(!newfrag)) |
| 361 | return -ENOMEM; |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 362 | newfrag->node->frags = 1; |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 363 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 364 | dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 365 | fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 366 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 367 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 377 | /* If we don't start at zero there's _always_ a previous */ |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 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); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 384 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 392 | |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 396 | /* |
| 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 | */ |
| 403 | static 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 Nemoto | 0ef675d | 2006-03-09 17:33:38 -0800 | [diff] [blame] | 409 | uint32_t crc, ofs, len; |
| 410 | size_t retlen; |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 411 | |
| 412 | BUG_ON(tn->csize == 0); |
| 413 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 414 | if (!jffs2_is_writebuffered(c)) |
| 415 | goto adj_acc; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 416 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 417 | /* Calculate how many bytes were already checked */ |
| 418 | ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 419 | len = ofs % c->wbuf_pagesize; |
Artem B. Bityutskiy | 280562b | 2005-08-17 15:57:43 +0100 | [diff] [blame] | 420 | if (likely(len)) |
| 421 | len = c->wbuf_pagesize - len; |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 422 | |
| 423 | if (len >= tn->csize) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 424 | dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 425 | ref_offset(ref), tn->csize, ofs); |
| 426 | goto adj_acc; |
| 427 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 428 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 429 | ofs += len; |
| 430 | len = tn->csize - len; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 431 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 432 | 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. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 433 | ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 434 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 435 | #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 Morton | 184f565 | 2006-05-15 13:45:58 +0100 | [diff] [blame] | 441 | JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 442 | c->mtd->unpoint(c->mtd, buffer, ofs, len); |
| 443 | } else if (err) |
| 444 | JFFS2_WARNING("MTD point failed: error code %d.\n", err); |
| 445 | else |
| 446 | pointed = 1; /* succefully pointed to device */ |
| 447 | } |
| 448 | #endif |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 449 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 450 | if (!pointed) { |
| 451 | buffer = kmalloc(len, GFP_KERNEL); |
| 452 | if (unlikely(!buffer)) |
| 453 | return -ENOMEM; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 454 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 455 | /* TODO: this is very frequent pattern, make it a separate |
| 456 | * routine */ |
| 457 | err = jffs2_flash_read(c, ofs, len, &retlen, buffer); |
| 458 | if (err) { |
| 459 | JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err); |
| 460 | goto free_out; |
| 461 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 462 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 463 | if (retlen != len) { |
Andrew Morton | 184f565 | 2006-05-15 13:45:58 +0100 | [diff] [blame] | 464 | JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 465 | err = -EIO; |
| 466 | goto free_out; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Continue calculating CRC */ |
| 471 | crc = crc32(tn->partial_crc, buffer, len); |
| 472 | if(!pointed) |
| 473 | kfree(buffer); |
| 474 | #ifndef __ECOS |
| 475 | else |
| 476 | c->mtd->unpoint(c->mtd, buffer, ofs, len); |
| 477 | #endif |
| 478 | |
| 479 | if (crc != tn->data_crc) { |
Artem B. Bityutskiy | 3924350 | 2005-08-03 10:26:50 +0100 | [diff] [blame] | 480 | JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 481 | ofs, tn->data_crc, crc); |
| 482 | return 1; |
| 483 | } |
| 484 | |
| 485 | adj_acc: |
| 486 | jeb = &c->blocks[ref->flash_offset / c->sector_size]; |
| 487 | len = ref_totlen(c, jeb, ref); |
| 488 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 489 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 490 | * Mark the node as having been checked and fix the |
| 491 | * accounting accordingly. |
| 492 | */ |
| 493 | spin_lock(&c->erase_completion_lock); |
| 494 | jeb->used_size += len; |
| 495 | jeb->unchecked_size -= len; |
| 496 | c->used_size += len; |
| 497 | c->unchecked_size -= len; |
| 498 | spin_unlock(&c->erase_completion_lock); |
| 499 | |
| 500 | return 0; |
| 501 | |
| 502 | free_out: |
| 503 | if(!pointed) |
| 504 | kfree(buffer); |
| 505 | #ifndef __ECOS |
| 506 | else |
| 507 | c->mtd->unpoint(c->mtd, buffer, ofs, len); |
| 508 | #endif |
| 509 | return err; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Helper function for jffs2_add_older_frag_to_fragtree(). |
| 514 | * |
| 515 | * Checks the node if we are in the checking stage. |
| 516 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 517 | static int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn) |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 518 | { |
| 519 | int ret; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 520 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 521 | BUG_ON(ref_obsolete(tn->fn->raw)); |
| 522 | |
| 523 | /* We only check the data CRC of unchecked nodes */ |
| 524 | if (ref_flags(tn->fn->raw) != REF_UNCHECKED) |
| 525 | return 0; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 526 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 527 | dbg_fragtree2("check node %#04x-%#04x, phys offs %#08x.\n", |
Artem B. Bityutskiy | 3924350 | 2005-08-03 10:26:50 +0100 | [diff] [blame] | 528 | tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw)); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 529 | |
| 530 | ret = check_node_data(c, tn); |
| 531 | if (unlikely(ret < 0)) { |
| 532 | JFFS2_ERROR("check_node_data() returned error: %d.\n", |
| 533 | ret); |
| 534 | } else if (unlikely(ret > 0)) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 535 | dbg_fragtree2("CRC error, mark it obsolete.\n"); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 536 | jffs2_mark_node_obsolete(c, tn->fn->raw); |
| 537 | } |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 542 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 543 | * Helper function for jffs2_add_older_frag_to_fragtree(). |
| 544 | * |
| 545 | * Called when the new fragment that is being inserted |
| 546 | * splits a hole fragment. |
| 547 | */ |
| 548 | static int split_hole(struct jffs2_sb_info *c, struct rb_root *root, |
| 549 | struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole) |
| 550 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 551 | dbg_fragtree2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 552 | newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size); |
| 553 | |
| 554 | if (hole->ofs == newfrag->ofs) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 555 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 556 | * Well, the new fragment actually starts at the same offset as |
| 557 | * the hole. |
| 558 | */ |
| 559 | if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 560 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 561 | * We replace the overlapped left part of the hole by |
| 562 | * the new node. |
| 563 | */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 564 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 565 | dbg_fragtree2("insert fragment %#04x-%#04x and cut the left part of the hole\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 566 | newfrag->ofs, newfrag->ofs + newfrag->size); |
| 567 | rb_replace_node(&hole->rb, &newfrag->rb, root); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 568 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 569 | hole->ofs += newfrag->size; |
| 570 | hole->size -= newfrag->size; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 571 | |
| 572 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 573 | * We know that 'hole' should be the right hand |
| 574 | * fragment. |
| 575 | */ |
| 576 | jffs2_fragtree_insert(hole, newfrag); |
| 577 | rb_insert_color(&hole->rb, root); |
| 578 | } else { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 579 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 580 | * Ah, the new fragment is of the same size as the hole. |
| 581 | * Relace the hole by it. |
| 582 | */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 583 | dbg_fragtree2("insert fragment %#04x-%#04x and overwrite hole\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 584 | newfrag->ofs, newfrag->ofs + newfrag->size); |
| 585 | rb_replace_node(&hole->rb, &newfrag->rb, root); |
| 586 | jffs2_free_node_frag(hole); |
| 587 | } |
| 588 | } else { |
| 589 | /* The new fragment lefts some hole space at the left */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 590 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 591 | struct jffs2_node_frag * newfrag2 = NULL; |
| 592 | |
| 593 | if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) { |
| 594 | /* The new frag also lefts some space at the right */ |
| 595 | newfrag2 = new_fragment(NULL, newfrag->ofs + |
| 596 | newfrag->size, hole->ofs + hole->size |
| 597 | - newfrag->ofs - newfrag->size); |
| 598 | if (unlikely(!newfrag2)) { |
| 599 | jffs2_free_node_frag(newfrag); |
| 600 | return -ENOMEM; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | hole->size = newfrag->ofs - hole->ofs; |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 605 | dbg_fragtree2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 606 | hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size); |
| 607 | |
| 608 | jffs2_fragtree_insert(newfrag, hole); |
| 609 | rb_insert_color(&newfrag->rb, root); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 610 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 611 | if (newfrag2) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 612 | dbg_fragtree2("left the hole %#04x-%#04x at the right\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 613 | newfrag2->ofs, newfrag2->ofs + newfrag2->size); |
| 614 | jffs2_fragtree_insert(newfrag2, newfrag); |
| 615 | rb_insert_color(&newfrag2->rb, root); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * This function is used when we build inode. It expects the nodes are passed |
| 624 | * in the decreasing version order. The whole point of this is to improve the |
| 625 | * inodes checking on NAND: we check the nodes' data CRC only when they are not |
| 626 | * obsoleted. Previously, add_frag_to_fragtree() function was used and |
| 627 | * nodes were passed to it in the increasing version ordes and CRCs of all |
| 628 | * nodes were checked. |
| 629 | * |
| 630 | * Note: tn->fn->size shouldn't be zero. |
| 631 | * |
| 632 | * Returns 0 if the node was inserted |
| 633 | * 1 if it wasn't inserted (since it is obsolete) |
| 634 | * < 0 an if error occured |
| 635 | */ |
| 636 | int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f, |
| 637 | struct jffs2_tmp_dnode_info *tn) |
| 638 | { |
| 639 | struct jffs2_node_frag *this, *newfrag; |
| 640 | uint32_t lastend; |
| 641 | struct jffs2_full_dnode *fn = tn->fn; |
| 642 | struct rb_root *root = &f->fragtree; |
| 643 | uint32_t fn_size = fn->size, fn_ofs = fn->ofs; |
| 644 | int err, checked = 0; |
| 645 | int ref_flag; |
| 646 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 647 | dbg_fragtree("insert fragment %#04x-%#04x, ver %u\n", fn_ofs, fn_ofs + fn_size, tn->version); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 648 | |
| 649 | /* Skip all the nodes which are completed before this one starts */ |
| 650 | this = jffs2_lookup_node_frag(root, fn_ofs); |
| 651 | if (this) |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 652 | dbg_fragtree2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole"); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 653 | |
| 654 | if (this) |
| 655 | lastend = this->ofs + this->size; |
| 656 | else |
| 657 | lastend = 0; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 658 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 659 | /* Detect the preliminary type of node */ |
| 660 | if (fn->size >= PAGE_CACHE_SIZE) |
| 661 | ref_flag = REF_PRISTINE; |
| 662 | else |
| 663 | ref_flag = REF_NORMAL; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 664 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 665 | /* See if we ran off the end of the root */ |
| 666 | if (lastend <= fn_ofs) { |
| 667 | /* We did */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 668 | |
| 669 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 670 | * We are going to insert the new node into the |
| 671 | * fragment tree, so check it. |
| 672 | */ |
| 673 | err = check_node(c, f, tn); |
| 674 | if (err != 0) |
| 675 | return err; |
| 676 | |
| 677 | fn->frags = 1; |
| 678 | |
| 679 | newfrag = new_fragment(fn, fn_ofs, fn_size); |
| 680 | if (unlikely(!newfrag)) |
| 681 | return -ENOMEM; |
| 682 | |
| 683 | err = no_overlapping_node(c, root, newfrag, this, lastend); |
| 684 | if (unlikely(err != 0)) { |
| 685 | jffs2_free_node_frag(newfrag); |
| 686 | return err; |
| 687 | } |
| 688 | |
| 689 | goto out_ok; |
| 690 | } |
| 691 | |
| 692 | fn->frags = 0; |
| 693 | |
| 694 | while (1) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 695 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 696 | * Here we have: |
| 697 | * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs. |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 698 | * |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 699 | * Remember, 'this' has higher version, any non-hole node |
| 700 | * which is already in the fragtree is newer then the newly |
| 701 | * inserted. |
| 702 | */ |
| 703 | if (!this->node) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 704 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 705 | * 'this' is the hole fragment, so at least the |
| 706 | * beginning of the new fragment is valid. |
| 707 | */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 708 | |
| 709 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 710 | * We are going to insert the new node into the |
| 711 | * fragment tree, so check it. |
| 712 | */ |
| 713 | if (!checked) { |
| 714 | err = check_node(c, f, tn); |
| 715 | if (unlikely(err != 0)) |
| 716 | return err; |
| 717 | checked = 1; |
| 718 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 719 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 720 | if (this->ofs + this->size >= fn_ofs + fn_size) { |
| 721 | /* We split the hole on two parts */ |
| 722 | |
| 723 | fn->frags += 1; |
| 724 | newfrag = new_fragment(fn, fn_ofs, fn_size); |
| 725 | if (unlikely(!newfrag)) |
| 726 | return -ENOMEM; |
| 727 | |
| 728 | err = split_hole(c, root, newfrag, this); |
| 729 | if (unlikely(err)) |
| 730 | return err; |
| 731 | goto out_ok; |
| 732 | } |
| 733 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 734 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 735 | * The beginning of the new fragment is valid since it |
| 736 | * overlaps the hole node. |
| 737 | */ |
| 738 | |
| 739 | ref_flag = REF_NORMAL; |
| 740 | |
| 741 | fn->frags += 1; |
| 742 | newfrag = new_fragment(fn, fn_ofs, |
| 743 | this->ofs + this->size - fn_ofs); |
| 744 | if (unlikely(!newfrag)) |
| 745 | return -ENOMEM; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 746 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 747 | if (fn_ofs == this->ofs) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 748 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 749 | * The new node starts at the same offset as |
| 750 | * the hole and supersieds the hole. |
| 751 | */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 752 | dbg_fragtree2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 753 | fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags); |
| 754 | |
| 755 | rb_replace_node(&this->rb, &newfrag->rb, root); |
| 756 | jffs2_free_node_frag(this); |
| 757 | } else { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 758 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 759 | * The hole becomes shorter as its right part |
| 760 | * is supersieded by the new fragment. |
| 761 | */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 762 | dbg_fragtree2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 763 | this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 764 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 765 | dbg_fragtree2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs, |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 766 | fn_ofs + this->ofs + this->size - fn_ofs, fn->frags); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 767 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 768 | this->size -= newfrag->size; |
| 769 | jffs2_fragtree_insert(newfrag, this); |
| 770 | rb_insert_color(&newfrag->rb, root); |
| 771 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 772 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 773 | fn_ofs += newfrag->size; |
| 774 | fn_size -= newfrag->size; |
| 775 | this = rb_entry(rb_next(&newfrag->rb), |
| 776 | struct jffs2_node_frag, rb); |
| 777 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 778 | dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 779 | this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)"); |
| 780 | } |
| 781 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 782 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 783 | * 'This' node is not the hole so it obsoletes the new fragment |
| 784 | * either fully or partially. |
| 785 | */ |
| 786 | if (this->ofs + this->size >= fn_ofs + fn_size) { |
| 787 | /* The new node is obsolete, drop it */ |
| 788 | if (fn->frags == 0) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 789 | dbg_fragtree2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 790 | ref_flag = REF_OBSOLETE; |
| 791 | } |
| 792 | goto out_ok; |
| 793 | } else { |
| 794 | struct jffs2_node_frag *new_this; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 795 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 796 | /* 'This' node obsoletes the beginning of the new node */ |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 797 | dbg_fragtree2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 798 | |
| 799 | ref_flag = REF_NORMAL; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 800 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 801 | fn_size -= this->ofs + this->size - fn_ofs; |
| 802 | fn_ofs = this->ofs + this->size; |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 803 | dbg_fragtree2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 804 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 805 | new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb); |
| 806 | if (!new_this) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 807 | /* |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 808 | * There is no next fragment. Add the rest of |
| 809 | * the new node as the right-hand child. |
| 810 | */ |
| 811 | if (!checked) { |
| 812 | err = check_node(c, f, tn); |
| 813 | if (unlikely(err != 0)) |
| 814 | return err; |
| 815 | checked = 1; |
| 816 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 817 | |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 818 | fn->frags += 1; |
| 819 | newfrag = new_fragment(fn, fn_ofs, fn_size); |
| 820 | if (unlikely(!newfrag)) |
| 821 | return -ENOMEM; |
| 822 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 823 | dbg_fragtree2("there are no more fragments, insert %#04x-%#04x\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 824 | newfrag->ofs, newfrag->ofs + newfrag->size); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 825 | rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 826 | rb_insert_color(&newfrag->rb, root); |
| 827 | goto out_ok; |
| 828 | } else { |
| 829 | this = new_this; |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 830 | dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n", |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 831 | this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)"); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | out_ok: |
| 837 | BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE); |
| 838 | |
| 839 | if (ref_flag == REF_OBSOLETE) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 840 | dbg_fragtree2("the node is obsolete now\n"); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 841 | /* jffs2_mark_node_obsolete() will adjust space accounting */ |
| 842 | jffs2_mark_node_obsolete(c, fn->raw); |
| 843 | return 1; |
| 844 | } |
| 845 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 846 | dbg_fragtree2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE"); |
Artem B. Bityutskiy | 1e0da3c | 2005-08-01 13:05:22 +0100 | [diff] [blame] | 847 | |
| 848 | /* Space accounting was adjusted at check_node_data() */ |
| 849 | spin_lock(&c->erase_completion_lock); |
| 850 | fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag; |
| 851 | spin_unlock(&c->erase_completion_lock); |
| 852 | |
| 853 | return 0; |
| 854 | } |
Artem B. Bityutskiy | f97117d | 2005-07-27 15:46:14 +0100 | [diff] [blame] | 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state) |
| 857 | { |
| 858 | spin_lock(&c->inocache_lock); |
| 859 | ic->state = state; |
| 860 | wake_up(&c->inocache_wq); |
| 861 | spin_unlock(&c->inocache_lock); |
| 862 | } |
| 863 | |
| 864 | /* During mount, this needs no locking. During normal operation, its |
| 865 | callers want to do other stuff while still holding the inocache_lock. |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 866 | Rather than introducing special case get_ino_cache functions or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | callbacks, we just let the caller do the locking itself. */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino) |
| 870 | { |
| 871 | struct jffs2_inode_cache *ret; |
| 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | ret = c->inocache_list[ino % INOCACHE_HASHSIZE]; |
| 874 | while (ret && ret->ino < ino) { |
| 875 | ret = ret->next; |
| 876 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | if (ret && ret->ino != ino) |
| 879 | ret = NULL; |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new) |
| 885 | { |
| 886 | struct jffs2_inode_cache **prev; |
Thomas Gleixner | 7d27c81 | 2005-05-22 21:47:19 +0200 | [diff] [blame] | 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | spin_lock(&c->inocache_lock); |
Thomas Gleixner | 7d27c81 | 2005-05-22 21:47:19 +0200 | [diff] [blame] | 889 | if (!new->ino) |
| 890 | new->ino = ++c->highest_ino; |
| 891 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 892 | dbg_inocache("add %p (ino #%u)\n", new, new->ino); |
Thomas Gleixner | 7d27c81 | 2005-05-22 21:47:19 +0200 | [diff] [blame] | 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE]; |
| 895 | |
| 896 | while ((*prev) && (*prev)->ino < new->ino) { |
| 897 | prev = &(*prev)->next; |
| 898 | } |
| 899 | new->next = *prev; |
| 900 | *prev = new; |
| 901 | |
| 902 | spin_unlock(&c->inocache_lock); |
| 903 | } |
| 904 | |
| 905 | void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old) |
| 906 | { |
| 907 | struct jffs2_inode_cache **prev; |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 908 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 909 | dbg_inocache("del %p (ino #%u)\n", old, old->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | spin_lock(&c->inocache_lock); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE]; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | while ((*prev) && (*prev)->ino < old->ino) { |
| 915 | prev = &(*prev)->next; |
| 916 | } |
| 917 | if ((*prev) == old) { |
| 918 | *prev = old->next; |
| 919 | } |
| 920 | |
David Woodhouse | 67e345d | 2005-02-27 23:01:36 +0000 | [diff] [blame] | 921 | /* Free it now unless it's in READING or CLEARING state, which |
| 922 | are the transitions upon read_inode() and clear_inode(). The |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 923 | rest of the time we know nobody else is looking at it, and |
David Woodhouse | 67e345d | 2005-02-27 23:01:36 +0000 | [diff] [blame] | 924 | if it's held by read_inode() or clear_inode() they'll free it |
| 925 | for themselves. */ |
| 926 | if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING) |
| 927 | jffs2_free_inode_cache(old); |
| 928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | spin_unlock(&c->inocache_lock); |
| 930 | } |
| 931 | |
| 932 | void jffs2_free_ino_caches(struct jffs2_sb_info *c) |
| 933 | { |
| 934 | int i; |
| 935 | struct jffs2_inode_cache *this, *next; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | for (i=0; i<INOCACHE_HASHSIZE; i++) { |
| 938 | this = c->inocache_list[i]; |
| 939 | while (this) { |
| 940 | next = this->next; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 941 | jffs2_xattr_free_inode(c, this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | jffs2_free_inode_cache(this); |
| 943 | this = next; |
| 944 | } |
| 945 | c->inocache_list[i] = NULL; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | void jffs2_free_raw_node_refs(struct jffs2_sb_info *c) |
| 950 | { |
| 951 | int i; |
| 952 | struct jffs2_raw_node_ref *this, *next; |
| 953 | |
| 954 | for (i=0; i<c->nr_blocks; i++) { |
| 955 | this = c->blocks[i].first_node; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 956 | while (this) { |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 957 | if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE) |
| 958 | next = this[REFS_PER_BLOCK].next_in_ino; |
| 959 | else |
| 960 | next = NULL; |
| 961 | |
| 962 | jffs2_free_refblock(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | this = next; |
| 964 | } |
| 965 | c->blocks[i].first_node = c->blocks[i].last_node = NULL; |
| 966 | } |
| 967 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset) |
| 970 | { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 971 | /* The common case in lookup is that there will be a node |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | which precisely matches. So we go looking for that first */ |
| 973 | struct rb_node *next; |
| 974 | struct jffs2_node_frag *prev = NULL; |
| 975 | struct jffs2_node_frag *frag = NULL; |
| 976 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 977 | dbg_fragtree2("root %p, offset %d\n", fragtree, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | next = fragtree->rb_node; |
| 980 | |
| 981 | while(next) { |
| 982 | frag = rb_entry(next, struct jffs2_node_frag, rb); |
| 983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | if (frag->ofs + frag->size <= offset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | /* Remember the closest smaller match on the way down */ |
| 986 | if (!prev || frag->ofs > prev->ofs) |
| 987 | prev = frag; |
| 988 | next = frag->rb.rb_right; |
| 989 | } else if (frag->ofs > offset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | next = frag->rb.rb_left; |
| 991 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return frag; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /* Exact match not found. Go back up looking at each parent, |
| 997 | and return the closest smaller one */ |
| 998 | |
| 999 | if (prev) |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 1000 | dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n", |
Artem B. Bityutskiy | e0d6013 | 2005-07-28 15:46:43 +0100 | [diff] [blame] | 1001 | prev->ofs, prev->ofs+prev->size); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1002 | else |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 1003 | dbg_fragtree2("returning NULL, empty fragtree\n"); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return prev; |
| 1006 | } |
| 1007 | |
| 1008 | /* Pass 'c' argument to indicate that nodes should be marked obsolete as |
| 1009 | they're killed. */ |
| 1010 | void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c) |
| 1011 | { |
| 1012 | struct jffs2_node_frag *frag; |
| 1013 | struct jffs2_node_frag *parent; |
| 1014 | |
| 1015 | if (!root->rb_node) |
| 1016 | return; |
| 1017 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 1018 | dbg_fragtree("killing\n"); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | while(frag) { |
| 1022 | if (frag->rb.rb_left) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | frag = frag_left(frag); |
| 1024 | continue; |
| 1025 | } |
| 1026 | if (frag->rb.rb_right) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | frag = frag_right(frag); |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | if (frag->node && !(--frag->node->frags)) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1032 | /* Not a hole, and it's the final remaining frag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | of this node. Free the node */ |
| 1034 | if (c) |
| 1035 | jffs2_mark_node_obsolete(c, frag->node->raw); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | jffs2_free_full_dnode(frag->node); |
| 1038 | } |
| 1039 | parent = frag_parent(frag); |
| 1040 | if (parent) { |
| 1041 | if (frag_left(parent) == frag) |
| 1042 | parent->rb.rb_left = NULL; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1043 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | parent->rb.rb_right = NULL; |
| 1045 | } |
| 1046 | |
| 1047 | jffs2_free_node_frag(frag); |
| 1048 | frag = parent; |
| 1049 | |
| 1050 | cond_resched(); |
| 1051 | } |
| 1052 | } |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1053 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1054 | struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c, |
| 1055 | struct jffs2_eraseblock *jeb, |
| 1056 | uint32_t ofs, uint32_t len, |
| 1057 | struct jffs2_inode_cache *ic) |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1058 | { |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1059 | struct jffs2_raw_node_ref *ref; |
| 1060 | |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1061 | BUG_ON(!jeb->allocated_refs); |
| 1062 | jeb->allocated_refs--; |
| 1063 | |
| 1064 | ref = jeb->last_node; |
| 1065 | |
| 1066 | dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset, |
| 1067 | ref->next_in_ino); |
| 1068 | |
| 1069 | while (ref->flash_offset != REF_EMPTY_NODE) { |
| 1070 | if (ref->flash_offset == REF_LINK_NODE) |
| 1071 | ref = ref->next_in_ino; |
| 1072 | else |
| 1073 | ref++; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1076 | dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref, |
| 1077 | ref->flash_offset, ofs, ref->next_in_ino, len); |
| 1078 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1079 | ref->flash_offset = ofs; |
| 1080 | |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1081 | if (!jeb->first_node) { |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1082 | jeb->first_node = ref; |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1083 | BUG_ON(ref_offset(ref) != jeb->offset); |
| 1084 | } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) { |
| 1085 | uint32_t last_len = ref_totlen(c, jeb, jeb->last_node); |
| 1086 | |
| 1087 | JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n", |
| 1088 | ref, ref_offset(ref), ref_offset(ref)+len, |
| 1089 | ref_offset(jeb->last_node), |
| 1090 | ref_offset(jeb->last_node)+last_len); |
| 1091 | BUG(); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1092 | } |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1093 | jeb->last_node = ref; |
| 1094 | |
David Woodhouse | fcb7578 | 2006-05-22 15:23:10 +0100 | [diff] [blame] | 1095 | if (ic) { |
| 1096 | ref->next_in_ino = ic->nodes; |
| 1097 | ic->nodes = ref; |
| 1098 | } else { |
| 1099 | ref->next_in_ino = NULL; |
| 1100 | } |
| 1101 | |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1102 | switch(ref_flags(ref)) { |
| 1103 | case REF_UNCHECKED: |
| 1104 | c->unchecked_size += len; |
| 1105 | jeb->unchecked_size += len; |
| 1106 | break; |
| 1107 | |
| 1108 | case REF_NORMAL: |
| 1109 | case REF_PRISTINE: |
| 1110 | c->used_size += len; |
| 1111 | jeb->used_size += len; |
| 1112 | break; |
| 1113 | |
| 1114 | case REF_OBSOLETE: |
| 1115 | c->dirty_size += len; |
David Woodhouse | 3b79673 | 2006-05-22 12:15:47 +0100 | [diff] [blame] | 1116 | jeb->dirty_size += len; |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1117 | break; |
| 1118 | } |
| 1119 | c->free_size -= len; |
| 1120 | jeb->free_size -= len; |
| 1121 | |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1122 | #ifdef TEST_TOTLEN |
| 1123 | /* Set (and test) __totlen field... for now */ |
| 1124 | ref->__totlen = len; |
| 1125 | ref_totlen(c, jeb, ref); |
| 1126 | #endif |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1127 | return ref; |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 1128 | } |
David Woodhouse | 6827099 | 2006-05-21 03:46:05 +0100 | [diff] [blame] | 1129 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1130 | /* No locking, no reservation of 'ref'. Do not use on a live file system */ |
David Woodhouse | 6827099 | 2006-05-21 03:46:05 +0100 | [diff] [blame] | 1131 | int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
| 1132 | uint32_t size) |
| 1133 | { |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1134 | if (!size) |
| 1135 | return 0; |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1136 | if (unlikely(size > jeb->free_size)) { |
| 1137 | printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n", |
| 1138 | size, jeb->free_size, jeb->wasted_size); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1139 | BUG(); |
| 1140 | } |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1141 | /* REF_EMPTY_NODE is !obsolete, so that works OK */ |
David Woodhouse | 2ebf09c | 2006-05-28 22:13:25 +0100 | [diff] [blame] | 1142 | if (jeb->last_node && ref_obsolete(jeb->last_node)) { |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1143 | #ifdef TEST_TOTLEN |
| 1144 | jeb->last_node->__totlen += size; |
| 1145 | #endif |
| 1146 | c->dirty_size += size; |
| 1147 | c->free_size -= size; |
| 1148 | jeb->dirty_size += size; |
| 1149 | jeb->free_size -= size; |
| 1150 | } else { |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1151 | uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size; |
| 1152 | ofs |= REF_OBSOLETE; |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1153 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 1154 | jffs2_link_node_ref(c, jeb, ofs, size, NULL); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1155 | } |
David Woodhouse | 6827099 | 2006-05-21 03:46:05 +0100 | [diff] [blame] | 1156 | |
| 1157 | return 0; |
| 1158 | } |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1159 | |
| 1160 | /* Calculate totlen from surrounding nodes or eraseblock */ |
| 1161 | static inline uint32_t __ref_totlen(struct jffs2_sb_info *c, |
| 1162 | struct jffs2_eraseblock *jeb, |
| 1163 | struct jffs2_raw_node_ref *ref) |
| 1164 | { |
| 1165 | uint32_t ref_end; |
David Woodhouse | 99988f7 | 2006-05-24 09:04:17 +0100 | [diff] [blame] | 1166 | struct jffs2_raw_node_ref *next_ref = ref_next(ref); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1167 | |
David Woodhouse | 99988f7 | 2006-05-24 09:04:17 +0100 | [diff] [blame] | 1168 | if (next_ref) |
| 1169 | ref_end = ref_offset(next_ref); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1170 | else { |
| 1171 | if (!jeb) |
| 1172 | jeb = &c->blocks[ref->flash_offset / c->sector_size]; |
| 1173 | |
| 1174 | /* Last node in block. Use free_space */ |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1175 | if (unlikely(ref != jeb->last_node)) { |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1176 | printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n", |
| 1177 | ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0); |
| 1178 | BUG(); |
| 1179 | } |
| 1180 | ref_end = jeb->offset + c->sector_size - jeb->free_size; |
| 1181 | } |
| 1182 | return ref_end - ref_offset(ref); |
| 1183 | } |
| 1184 | |
| 1185 | uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
| 1186 | struct jffs2_raw_node_ref *ref) |
| 1187 | { |
| 1188 | uint32_t ret; |
| 1189 | |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1190 | ret = __ref_totlen(c, jeb, ref); |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1191 | |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1192 | #ifdef TEST_TOTLEN |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1193 | if (unlikely(ret != ref->__totlen)) { |
| 1194 | if (!jeb) |
| 1195 | jeb = &c->blocks[ref->flash_offset / c->sector_size]; |
| 1196 | |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1197 | printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n", |
| 1198 | ref, ref_offset(ref), ref_offset(ref)+ref->__totlen, |
| 1199 | ret, ref->__totlen); |
David Woodhouse | 99988f7 | 2006-05-24 09:04:17 +0100 | [diff] [blame] | 1200 | if (ref_next(ref)) { |
| 1201 | printk(KERN_CRIT "next %p (0x%08x-0x%08x)\n", ref_next(ref), ref_offset(ref_next(ref)), |
| 1202 | ref_offset(ref_next(ref))+ref->__totlen); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1203 | } else |
David Woodhouse | 99988f7 | 2006-05-24 09:04:17 +0100 | [diff] [blame] | 1204 | printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node); |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1205 | |
| 1206 | printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size); |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1207 | |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1208 | #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS) |
| 1209 | __jffs2_dbg_dump_node_refs_nolock(c, jeb); |
| 1210 | #endif |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1211 | |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1212 | WARN_ON(1); |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 1213 | |
| 1214 | ret = ref->__totlen; |
David Woodhouse | ca89a51 | 2006-05-21 13:29:11 +0100 | [diff] [blame] | 1215 | } |
| 1216 | #endif /* TEST_TOTLEN */ |
| 1217 | return ret; |
| 1218 | } |