1 /* 2 * JFFS2 -- Journalling Flash File System, Version 2. 3 * 4 * Copyright (C) 2001-2003 Red Hat, Inc. 5 * 6 * Created by David Woodhouse <dwmw2@infradead.org> 7 * 8 * For licensing information, see the file 'LICENCE' in this directory. 9 * 10 * $Id: nodelist.c,v 1.110 2005/08/17 14:13:45 dedekind Exp $ 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; 27 28 JFFS2_DBG_DENTLIST("add dirent \"%s\", ino #%u\n", new->name, new->ino); 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) { 34 JFFS2_DBG_DENTLIST("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n", 35 (*prev)->name, (*prev)->ino); 36 jffs2_mark_node_obsolete(c, new->raw); 37 jffs2_free_full_dirent(new); 38 } else { 39 JFFS2_DBG_DENTLIST("marking old dirent \"%s\", ino #%u bsolete\n", 40 (*prev)->name, (*prev)->ino); 41 new->next = (*prev)->next; 42 jffs2_mark_node_obsolete(c, ((*prev)->raw)); 43 jffs2_free_full_dirent(*prev); 44 *prev = new; 45 } 46 return; 47 } 48 prev = &((*prev)->next); 49 } 50 new->next = *prev; 51 *prev = new; 52 } 53 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 58 JFFS2_DBG_FRAGTREE("truncating fragtree to 0x%08x bytes\n", size); 59 60 /* We know frag->ofs <= size. That's what lookup does for us */ 61 if (frag && frag->ofs != size) { 62 if (frag->ofs+frag->size > size) { 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 70 frag_erase(frag, list); 71 jffs2_obsolete_node_frag(c, frag); 72 frag = next; 73 } 74 75 if (size == 0) 76 return; 77 78 /* 79 * If the last fragment starts at the RAM page boundary, it is 80 * REF_PRISTINE irrespective of its size. 81 */ 82 frag = frag_last(list); 83 if ((frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) { 84 JFFS2_DBG_FRAGTREE2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n", 85 frag->ofs, frag->ofs + frag->size); 86 frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE; 87 } 88 } 89 90 void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this) 91 { 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 */ 96 JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n", 97 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size); 98 jffs2_mark_node_obsolete(c, this->node->raw); 99 jffs2_free_full_dnode(this->node); 100 } else { 101 JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n", 102 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags); 103 mark_ref_normal(this->node->raw); 104 } 105 106 } 107 jffs2_free_node_frag(this); 108 } 109 110 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base) 111 { 112 struct rb_node *parent = &base->rb; 113 struct rb_node **link = &parent; 114 115 JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size); 116 117 while (*link) { 118 parent = *link; 119 base = rb_entry(parent, struct jffs2_node_frag, rb); 120 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 { 126 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base); 127 BUG(); 128 } 129 } 130 131 rb_link_node(&newfrag->rb, &base->rb, link); 132 } 133 134 /* 135 * Allocate and initializes a new fragment. 136 */ 137 static inline struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size) 138 { 139 struct jffs2_node_frag *newfrag; 140 141 newfrag = jffs2_alloc_node_frag(); 142 if (likely(newfrag)) { 143 newfrag->ofs = ofs; 144 newfrag->size = size; 145 newfrag->node = fn; 146 } else { 147 JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n"); 148 } 149 150 return newfrag; 151 } 152 153 /* 154 * Called when there is no overlapping fragment exist. Inserts a hole before the new 155 * fragment and inserts the new fragment to the fragtree. 156 */ 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) { 172 /* By definition, the 'this' node has no right-hand child, 173 because there are no frags with offset greater than it. 174 So that's where we want to put the hole */ 175 JFFS2_DBG_FRAGTREE2("add hole frag %#04x-%#04x on the right of the new frag.\n", 176 holefrag->ofs, holefrag->ofs + holefrag->size); 177 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right); 178 } else { 179 JFFS2_DBG_FRAGTREE2("Add hole frag %#04x-%#04x to the root of the tree.\n", 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 } 186 187 if (this) { 188 /* By definition, the 'this' node has no right-hand child, 189 because there are no frags with offset greater than it. 190 So that's where we want to put new fragment */ 191 JFFS2_DBG_FRAGTREE2("add the new node at the right\n"); 192 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right); 193 } else { 194 JFFS2_DBG_FRAGTREE2("insert the new node at the root of the tree\n"); 195 rb_link_node(&newfrag->rb, NULL, &root->rb_node); 196 } 197 rb_insert_color(&newfrag->rb, root); 198 199 return 0; 200 } 201 202 /* Doesn't set inode->i_size */ 203 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag) 204 { 205 struct jffs2_node_frag *this; 206 uint32_t lastend; 207 208 /* Skip all the nodes which are completed before this one starts */ 209 this = jffs2_lookup_node_frag(root, newfrag->node->ofs); 210 211 if (this) { 212 JFFS2_DBG_FRAGTREE2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n", 213 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this); 214 lastend = this->ofs + this->size; 215 } else { 216 JFFS2_DBG_FRAGTREE2("lookup gave no frag\n"); 217 lastend = 0; 218 } 219 220 /* See if we ran off the end of the fragtree */ 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 234 return no_overlapping_node(c, root, newfrag, this, lastend); 235 } 236 237 if (this->node) 238 JFFS2_DBG_FRAGTREE2("dealing with frag %u-%u, phys %#08x(%d).\n", 239 this->ofs, this->ofs + this->size, 240 ref_offset(this->node->raw), ref_flags(this->node->raw)); 241 else 242 JFFS2_DBG_FRAGTREE2("dealing with hole frag %u-%u.\n", 243 this->ofs, this->ofs + this->size); 244 245 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes, 246 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs 247 */ 248 if (newfrag->ofs > this->ofs) { 249 /* This node isn't completely obsoleted. The start of it remains valid */ 250 251 /* Mark the new node and the partially covered node REF_NORMAL -- let 252 the GC take a look at them */ 253 mark_ref_normal(newfrag->node->raw); 254 if (this->node) 255 mark_ref_normal(this->node->raw); 256 257 if (this->ofs + this->size > newfrag->ofs + newfrag->size) { 258 /* The new node splits 'this' frag into two */ 259 struct jffs2_node_frag *newfrag2; 260 261 if (this->node) 262 JFFS2_DBG_FRAGTREE2("split old frag 0x%04x-0x%04x, phys 0x%08x\n", 263 this->ofs, this->ofs+this->size, ref_offset(this->node->raw)); 264 else 265 JFFS2_DBG_FRAGTREE2("split old hole frag 0x%04x-0x%04x\n", 266 this->ofs, this->ofs+this->size); 267 268 /* New second frag pointing to this's node */ 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; 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); 286 rb_insert_color(&newfrag->rb, root); 287 288 jffs2_fragtree_insert(newfrag2, newfrag); 289 rb_insert_color(&newfrag2->rb, root); 290 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); 298 rb_insert_color(&newfrag->rb, root); 299 } else { 300 /* New frag starts at the same point as 'this' used to. Replace 301 it in the tree without doing a delete and insertion */ 302 JFFS2_DBG_FRAGTREE2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n", 303 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size); 304 305 rb_replace_node(&this->rb, &newfrag->rb, root); 306 307 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) { 308 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size); 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); 315 rb_insert_color(&this->rb, root); 316 return 0; 317 } 318 } 319 /* OK, now we have newfrag added in the correct place in the tree, but 320 frag_next(newfrag) may be a fragment which is overlapped by it 321 */ 322 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) { 323 /* 'this' frag is obsoleted completely. */ 324 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n", 325 this, this->ofs, this->ofs+this->size); 326 rb_erase(&this->rb, root); 327 jffs2_obsolete_node_frag(c, this); 328 } 329 /* Now we're pointing at the first frag which isn't totally obsoleted by 330 the new frag */ 331 332 if (!this || newfrag->ofs + newfrag->size == this->ofs) 333 return 0; 334 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); 343 344 return 0; 345 } 346 347 /* 348 * Given an inode, probably with existing tree of fragments, add the new node 349 * to the fragment tree. 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 356 if (unlikely(!fn->size)) 357 return 0; 358 359 newfrag = new_fragment(fn, fn->ofs, fn->size); 360 if (unlikely(!newfrag)) 361 return -ENOMEM; 362 newfrag->node->frags = 1; 363 364 JFFS2_DBG_FRAGTREE("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n", 365 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag); 366 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); 377 /* If we don't start at zero there's _always_ a previous */ 378 if (prev->node) 379 mark_ref_normal(prev->node->raw); 380 } 381 382 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) { 383 struct jffs2_node_frag *next = frag_next(newfrag); 384 385 if (next) { 386 mark_ref_normal(fn->raw); 387 if (next->node) 388 mark_ref_normal(next->node->raw); 389 } 390 } 391 jffs2_dbg_fragtree_paranoia_check_nolock(f); 392 393 return 0; 394 } 395 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; 409 uint32_t crc, ofs, retlen, len; 410 411 BUG_ON(tn->csize == 0); 412 413 /* Calculate how many bytes were already checked */ 414 ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode); 415 len = ofs & (c->wbuf_pagesize - 1); 416 len = c->wbuf_pagesize - len; 417 418 if (len >= tn->csize) { 419 JFFS2_DBG_READINODE("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n", 420 ref_offset(ref), tn->csize, ofs); 421 goto adj_acc; 422 } 423 424 ofs += len; 425 len = tn->csize - len; 426 427 JFFS2_DBG_READINODE("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n", 428 ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len); 429 430 #ifndef __ECOS 431 /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(), 432 * adding and jffs2_flash_read_end() interface. */ 433 if (c->mtd->point) { 434 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer); 435 if (!err && retlen < tn->csize) { 436 JFFS2_WARNING("MTD point returned len too short: %u instead of %u.\n", retlen, tn->csize); 437 c->mtd->unpoint(c->mtd, buffer, ofs, len); 438 } else if (err) 439 JFFS2_WARNING("MTD point failed: error code %d.\n", err); 440 else 441 pointed = 1; /* succefully pointed to device */ 442 } 443 #endif 444 445 if (!pointed) { 446 buffer = kmalloc(len, GFP_KERNEL); 447 if (unlikely(!buffer)) 448 return -ENOMEM; 449 450 /* TODO: this is very frequent pattern, make it a separate 451 * routine */ 452 err = jffs2_flash_read(c, ofs, len, &retlen, buffer); 453 if (err) { 454 JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err); 455 goto free_out; 456 } 457 458 if (retlen != len) { 459 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ofs, retlen, len); 460 err = -EIO; 461 goto free_out; 462 } 463 } 464 465 /* Continue calculating CRC */ 466 crc = crc32(tn->partial_crc, buffer, len); 467 if(!pointed) 468 kfree(buffer); 469 #ifndef __ECOS 470 else 471 c->mtd->unpoint(c->mtd, buffer, ofs, len); 472 #endif 473 474 if (crc != tn->data_crc) { 475 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n", 476 ofs, tn->data_crc, crc); 477 return 1; 478 } 479 480 adj_acc: 481 jeb = &c->blocks[ref->flash_offset / c->sector_size]; 482 len = ref_totlen(c, jeb, ref); 483 484 /* 485 * Mark the node as having been checked and fix the 486 * accounting accordingly. 487 */ 488 spin_lock(&c->erase_completion_lock); 489 jeb->used_size += len; 490 jeb->unchecked_size -= len; 491 c->used_size += len; 492 c->unchecked_size -= len; 493 spin_unlock(&c->erase_completion_lock); 494 495 return 0; 496 497 free_out: 498 if(!pointed) 499 kfree(buffer); 500 #ifndef __ECOS 501 else 502 c->mtd->unpoint(c->mtd, buffer, ofs, len); 503 #endif 504 return err; 505 } 506 507 /* 508 * Helper function for jffs2_add_older_frag_to_fragtree(). 509 * 510 * Checks the node if we are in the checking stage. 511 */ 512 static inline int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn) 513 { 514 int ret; 515 516 BUG_ON(ref_obsolete(tn->fn->raw)); 517 518 /* We only check the data CRC of unchecked nodes */ 519 if (ref_flags(tn->fn->raw) != REF_UNCHECKED) 520 return 0; 521 522 JFFS2_DBG_FRAGTREE2("check node %#04x-%#04x, phys offs %#08x.\n", 523 tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw)); 524 525 ret = check_node_data(c, tn); 526 if (unlikely(ret < 0)) { 527 JFFS2_ERROR("check_node_data() returned error: %d.\n", 528 ret); 529 } else if (unlikely(ret > 0)) { 530 JFFS2_DBG_FRAGTREE2("CRC error, mark it obsolete.\n"); 531 jffs2_mark_node_obsolete(c, tn->fn->raw); 532 } 533 534 return ret; 535 } 536 537 /* 538 * Helper function for jffs2_add_older_frag_to_fragtree(). 539 * 540 * Called when the new fragment that is being inserted 541 * splits a hole fragment. 542 */ 543 static int split_hole(struct jffs2_sb_info *c, struct rb_root *root, 544 struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole) 545 { 546 JFFS2_DBG_FRAGTREE2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n", 547 newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size); 548 549 if (hole->ofs == newfrag->ofs) { 550 /* 551 * Well, the new fragment actually starts at the same offset as 552 * the hole. 553 */ 554 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) { 555 /* 556 * We replace the overlapped left part of the hole by 557 * the new node. 558 */ 559 560 JFFS2_DBG_FRAGTREE2("insert fragment %#04x-%#04x and cut the left part of the hole\n", 561 newfrag->ofs, newfrag->ofs + newfrag->size); 562 rb_replace_node(&hole->rb, &newfrag->rb, root); 563 564 hole->ofs += newfrag->size; 565 hole->size -= newfrag->size; 566 567 /* 568 * We know that 'hole' should be the right hand 569 * fragment. 570 */ 571 jffs2_fragtree_insert(hole, newfrag); 572 rb_insert_color(&hole->rb, root); 573 } else { 574 /* 575 * Ah, the new fragment is of the same size as the hole. 576 * Relace the hole by it. 577 */ 578 JFFS2_DBG_FRAGTREE2("insert fragment %#04x-%#04x and overwrite hole\n", 579 newfrag->ofs, newfrag->ofs + newfrag->size); 580 rb_replace_node(&hole->rb, &newfrag->rb, root); 581 jffs2_free_node_frag(hole); 582 } 583 } else { 584 /* The new fragment lefts some hole space at the left */ 585 586 struct jffs2_node_frag * newfrag2 = NULL; 587 588 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) { 589 /* The new frag also lefts some space at the right */ 590 newfrag2 = new_fragment(NULL, newfrag->ofs + 591 newfrag->size, hole->ofs + hole->size 592 - newfrag->ofs - newfrag->size); 593 if (unlikely(!newfrag2)) { 594 jffs2_free_node_frag(newfrag); 595 return -ENOMEM; 596 } 597 } 598 599 hole->size = newfrag->ofs - hole->ofs; 600 JFFS2_DBG_FRAGTREE2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n", 601 hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size); 602 603 jffs2_fragtree_insert(newfrag, hole); 604 rb_insert_color(&newfrag->rb, root); 605 606 if (newfrag2) { 607 JFFS2_DBG_FRAGTREE2("left the hole %#04x-%#04x at the right\n", 608 newfrag2->ofs, newfrag2->ofs + newfrag2->size); 609 jffs2_fragtree_insert(newfrag2, newfrag); 610 rb_insert_color(&newfrag2->rb, root); 611 } 612 } 613 614 return 0; 615 } 616 617 /* 618 * This function is used when we build inode. It expects the nodes are passed 619 * in the decreasing version order. The whole point of this is to improve the 620 * inodes checking on NAND: we check the nodes' data CRC only when they are not 621 * obsoleted. Previously, add_frag_to_fragtree() function was used and 622 * nodes were passed to it in the increasing version ordes and CRCs of all 623 * nodes were checked. 624 * 625 * Note: tn->fn->size shouldn't be zero. 626 * 627 * Returns 0 if the node was inserted 628 * 1 if it wasn't inserted (since it is obsolete) 629 * < 0 an if error occured 630 */ 631 int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 632 struct jffs2_tmp_dnode_info *tn) 633 { 634 struct jffs2_node_frag *this, *newfrag; 635 uint32_t lastend; 636 struct jffs2_full_dnode *fn = tn->fn; 637 struct rb_root *root = &f->fragtree; 638 uint32_t fn_size = fn->size, fn_ofs = fn->ofs; 639 int err, checked = 0; 640 int ref_flag; 641 642 JFFS2_DBG_FRAGTREE("insert fragment %#04x-%#04x, ver %u\n", fn_ofs, fn_ofs + fn_size, tn->version); 643 644 /* Skip all the nodes which are completed before this one starts */ 645 this = jffs2_lookup_node_frag(root, fn_ofs); 646 if (this) 647 JFFS2_DBG_FRAGTREE2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole"); 648 649 if (this) 650 lastend = this->ofs + this->size; 651 else 652 lastend = 0; 653 654 /* Detect the preliminary type of node */ 655 if (fn->size >= PAGE_CACHE_SIZE) 656 ref_flag = REF_PRISTINE; 657 else 658 ref_flag = REF_NORMAL; 659 660 /* See if we ran off the end of the root */ 661 if (lastend <= fn_ofs) { 662 /* We did */ 663 664 /* 665 * We are going to insert the new node into the 666 * fragment tree, so check it. 667 */ 668 err = check_node(c, f, tn); 669 if (err != 0) 670 return err; 671 672 fn->frags = 1; 673 674 newfrag = new_fragment(fn, fn_ofs, fn_size); 675 if (unlikely(!newfrag)) 676 return -ENOMEM; 677 678 err = no_overlapping_node(c, root, newfrag, this, lastend); 679 if (unlikely(err != 0)) { 680 jffs2_free_node_frag(newfrag); 681 return err; 682 } 683 684 goto out_ok; 685 } 686 687 fn->frags = 0; 688 689 while (1) { 690 /* 691 * Here we have: 692 * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs. 693 * 694 * Remember, 'this' has higher version, any non-hole node 695 * which is already in the fragtree is newer then the newly 696 * inserted. 697 */ 698 if (!this->node) { 699 /* 700 * 'this' is the hole fragment, so at least the 701 * beginning of the new fragment is valid. 702 */ 703 704 /* 705 * We are going to insert the new node into the 706 * fragment tree, so check it. 707 */ 708 if (!checked) { 709 err = check_node(c, f, tn); 710 if (unlikely(err != 0)) 711 return err; 712 checked = 1; 713 } 714 715 if (this->ofs + this->size >= fn_ofs + fn_size) { 716 /* We split the hole on two parts */ 717 718 fn->frags += 1; 719 newfrag = new_fragment(fn, fn_ofs, fn_size); 720 if (unlikely(!newfrag)) 721 return -ENOMEM; 722 723 err = split_hole(c, root, newfrag, this); 724 if (unlikely(err)) 725 return err; 726 goto out_ok; 727 } 728 729 /* 730 * The beginning of the new fragment is valid since it 731 * overlaps the hole node. 732 */ 733 734 ref_flag = REF_NORMAL; 735 736 fn->frags += 1; 737 newfrag = new_fragment(fn, fn_ofs, 738 this->ofs + this->size - fn_ofs); 739 if (unlikely(!newfrag)) 740 return -ENOMEM; 741 742 if (fn_ofs == this->ofs) { 743 /* 744 * The new node starts at the same offset as 745 * the hole and supersieds the hole. 746 */ 747 JFFS2_DBG_FRAGTREE2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n", 748 fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags); 749 750 rb_replace_node(&this->rb, &newfrag->rb, root); 751 jffs2_free_node_frag(this); 752 } else { 753 /* 754 * The hole becomes shorter as its right part 755 * is supersieded by the new fragment. 756 */ 757 JFFS2_DBG_FRAGTREE2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n", 758 this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size); 759 760 JFFS2_DBG_FRAGTREE2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs, 761 fn_ofs + this->ofs + this->size - fn_ofs, fn->frags); 762 763 this->size -= newfrag->size; 764 jffs2_fragtree_insert(newfrag, this); 765 rb_insert_color(&newfrag->rb, root); 766 } 767 768 fn_ofs += newfrag->size; 769 fn_size -= newfrag->size; 770 this = rb_entry(rb_next(&newfrag->rb), 771 struct jffs2_node_frag, rb); 772 773 JFFS2_DBG_FRAGTREE2("switch to the next 'this' fragment: %#04x-%#04x %s\n", 774 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)"); 775 } 776 777 /* 778 * 'This' node is not the hole so it obsoletes the new fragment 779 * either fully or partially. 780 */ 781 if (this->ofs + this->size >= fn_ofs + fn_size) { 782 /* The new node is obsolete, drop it */ 783 if (fn->frags == 0) { 784 JFFS2_DBG_FRAGTREE2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size); 785 ref_flag = REF_OBSOLETE; 786 } 787 goto out_ok; 788 } else { 789 struct jffs2_node_frag *new_this; 790 791 /* 'This' node obsoletes the beginning of the new node */ 792 JFFS2_DBG_FRAGTREE2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size); 793 794 ref_flag = REF_NORMAL; 795 796 fn_size -= this->ofs + this->size - fn_ofs; 797 fn_ofs = this->ofs + this->size; 798 JFFS2_DBG_FRAGTREE2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size); 799 800 new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb); 801 if (!new_this) { 802 /* 803 * There is no next fragment. Add the rest of 804 * the new node as the right-hand child. 805 */ 806 if (!checked) { 807 err = check_node(c, f, tn); 808 if (unlikely(err != 0)) 809 return err; 810 checked = 1; 811 } 812 813 fn->frags += 1; 814 newfrag = new_fragment(fn, fn_ofs, fn_size); 815 if (unlikely(!newfrag)) 816 return -ENOMEM; 817 818 JFFS2_DBG_FRAGTREE2("there are no more fragments, insert %#04x-%#04x\n", 819 newfrag->ofs, newfrag->ofs + newfrag->size); 820 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right); 821 rb_insert_color(&newfrag->rb, root); 822 goto out_ok; 823 } else { 824 this = new_this; 825 JFFS2_DBG_FRAGTREE2("switch to the next 'this' fragment: %#04x-%#04x %s\n", 826 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)"); 827 } 828 } 829 } 830 831 out_ok: 832 BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE); 833 834 if (ref_flag == REF_OBSOLETE) { 835 JFFS2_DBG_FRAGTREE2("the node is obsolete now\n"); 836 /* jffs2_mark_node_obsolete() will adjust space accounting */ 837 jffs2_mark_node_obsolete(c, fn->raw); 838 return 1; 839 } 840 841 JFFS2_DBG_FRAGTREE2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE"); 842 843 /* Space accounting was adjusted at check_node_data() */ 844 spin_lock(&c->erase_completion_lock); 845 fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag; 846 spin_unlock(&c->erase_completion_lock); 847 848 return 0; 849 } 850 851 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state) 852 { 853 spin_lock(&c->inocache_lock); 854 ic->state = state; 855 wake_up(&c->inocache_wq); 856 spin_unlock(&c->inocache_lock); 857 } 858 859 /* During mount, this needs no locking. During normal operation, its 860 callers want to do other stuff while still holding the inocache_lock. 861 Rather than introducing special case get_ino_cache functions or 862 callbacks, we just let the caller do the locking itself. */ 863 864 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino) 865 { 866 struct jffs2_inode_cache *ret; 867 868 ret = c->inocache_list[ino % INOCACHE_HASHSIZE]; 869 while (ret && ret->ino < ino) { 870 ret = ret->next; 871 } 872 873 if (ret && ret->ino != ino) 874 ret = NULL; 875 876 return ret; 877 } 878 879 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new) 880 { 881 struct jffs2_inode_cache **prev; 882 883 spin_lock(&c->inocache_lock); 884 if (!new->ino) 885 new->ino = ++c->highest_ino; 886 887 JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino); 888 889 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE]; 890 891 while ((*prev) && (*prev)->ino < new->ino) { 892 prev = &(*prev)->next; 893 } 894 new->next = *prev; 895 *prev = new; 896 897 spin_unlock(&c->inocache_lock); 898 } 899 900 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old) 901 { 902 struct jffs2_inode_cache **prev; 903 904 JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino); 905 spin_lock(&c->inocache_lock); 906 907 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE]; 908 909 while ((*prev) && (*prev)->ino < old->ino) { 910 prev = &(*prev)->next; 911 } 912 if ((*prev) == old) { 913 *prev = old->next; 914 } 915 916 /* Free it now unless it's in READING or CLEARING state, which 917 are the transitions upon read_inode() and clear_inode(). The 918 rest of the time we know nobody else is looking at it, and 919 if it's held by read_inode() or clear_inode() they'll free it 920 for themselves. */ 921 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING) 922 jffs2_free_inode_cache(old); 923 924 spin_unlock(&c->inocache_lock); 925 } 926 927 void jffs2_free_ino_caches(struct jffs2_sb_info *c) 928 { 929 int i; 930 struct jffs2_inode_cache *this, *next; 931 932 for (i=0; i<INOCACHE_HASHSIZE; i++) { 933 this = c->inocache_list[i]; 934 while (this) { 935 next = this->next; 936 jffs2_free_inode_cache(this); 937 this = next; 938 } 939 c->inocache_list[i] = NULL; 940 } 941 } 942 943 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c) 944 { 945 int i; 946 struct jffs2_raw_node_ref *this, *next; 947 948 for (i=0; i<c->nr_blocks; i++) { 949 this = c->blocks[i].first_node; 950 while(this) { 951 next = this->next_phys; 952 jffs2_free_raw_node_ref(this); 953 this = next; 954 } 955 c->blocks[i].first_node = c->blocks[i].last_node = NULL; 956 } 957 } 958 959 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset) 960 { 961 /* The common case in lookup is that there will be a node 962 which precisely matches. So we go looking for that first */ 963 struct rb_node *next; 964 struct jffs2_node_frag *prev = NULL; 965 struct jffs2_node_frag *frag = NULL; 966 967 JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset); 968 969 next = fragtree->rb_node; 970 971 while(next) { 972 frag = rb_entry(next, struct jffs2_node_frag, rb); 973 974 if (frag->ofs + frag->size <= offset) { 975 /* Remember the closest smaller match on the way down */ 976 if (!prev || frag->ofs > prev->ofs) 977 prev = frag; 978 next = frag->rb.rb_right; 979 } else if (frag->ofs > offset) { 980 next = frag->rb.rb_left; 981 } else { 982 return frag; 983 } 984 } 985 986 /* Exact match not found. Go back up looking at each parent, 987 and return the closest smaller one */ 988 989 if (prev) 990 JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n", 991 prev->ofs, prev->ofs+prev->size); 992 else 993 JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n"); 994 995 return prev; 996 } 997 998 /* Pass 'c' argument to indicate that nodes should be marked obsolete as 999 they're killed. */ 1000 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c) 1001 { 1002 struct jffs2_node_frag *frag; 1003 struct jffs2_node_frag *parent; 1004 1005 if (!root->rb_node) 1006 return; 1007 1008 JFFS2_DBG_FRAGTREE("killing\n"); 1009 1010 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb)); 1011 while(frag) { 1012 if (frag->rb.rb_left) { 1013 frag = frag_left(frag); 1014 continue; 1015 } 1016 if (frag->rb.rb_right) { 1017 frag = frag_right(frag); 1018 continue; 1019 } 1020 1021 if (frag->node && !(--frag->node->frags)) { 1022 /* Not a hole, and it's the final remaining frag 1023 of this node. Free the node */ 1024 if (c) 1025 jffs2_mark_node_obsolete(c, frag->node->raw); 1026 1027 jffs2_free_full_dnode(frag->node); 1028 } 1029 parent = frag_parent(frag); 1030 if (parent) { 1031 if (frag_left(parent) == frag) 1032 parent->rb.rb_left = NULL; 1033 else 1034 parent->rb.rb_right = NULL; 1035 } 1036 1037 jffs2_free_node_frag(frag); 1038 frag = parent; 1039 1040 cond_resched(); 1041 } 1042 } 1043