1 /* 2 * JFFS2 -- Journalling Flash File System, Version 2. 3 * 4 * Copyright © 2001-2007 Red Hat, Inc. 5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> 6 * 7 * Created by David Woodhouse <dwmw2@infradead.org> 8 * 9 * For licensing information, see the file 'LICENCE' in this directory. 10 * 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/mtd/mtd.h> 15 #include <linux/slab.h> 16 #include <linux/pagemap.h> 17 #include <linux/crc32.h> 18 #include <linux/compiler.h> 19 #include <linux/stat.h> 20 #include "nodelist.h" 21 #include "compr.h" 22 23 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, 24 struct jffs2_inode_cache *ic, 25 struct jffs2_raw_node_ref *raw); 26 static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 27 struct jffs2_inode_info *f, struct jffs2_full_dnode *fd); 28 static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 29 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd); 30 static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 31 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd); 32 static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 33 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn, 34 uint32_t start, uint32_t end); 35 static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 36 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn, 37 uint32_t start, uint32_t end); 38 static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 39 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f); 40 41 /* Called with erase_completion_lock held */ 42 static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c) 43 { 44 struct jffs2_eraseblock *ret; 45 struct list_head *nextlist = NULL; 46 int n = jiffies % 128; 47 48 /* Pick an eraseblock to garbage collect next. This is where we'll 49 put the clever wear-levelling algorithms. Eventually. */ 50 /* We possibly want to favour the dirtier blocks more when the 51 number of free blocks is low. */ 52 again: 53 if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) { 54 jffs2_dbg(1, "Picking block from bad_used_list to GC next\n"); 55 nextlist = &c->bad_used_list; 56 } else if (n < 50 && !list_empty(&c->erasable_list)) { 57 /* Note that most of them will have gone directly to be erased. 58 So don't favour the erasable_list _too_ much. */ 59 jffs2_dbg(1, "Picking block from erasable_list to GC next\n"); 60 nextlist = &c->erasable_list; 61 } else if (n < 110 && !list_empty(&c->very_dirty_list)) { 62 /* Most of the time, pick one off the very_dirty list */ 63 jffs2_dbg(1, "Picking block from very_dirty_list to GC next\n"); 64 nextlist = &c->very_dirty_list; 65 } else if (n < 126 && !list_empty(&c->dirty_list)) { 66 jffs2_dbg(1, "Picking block from dirty_list to GC next\n"); 67 nextlist = &c->dirty_list; 68 } else if (!list_empty(&c->clean_list)) { 69 jffs2_dbg(1, "Picking block from clean_list to GC next\n"); 70 nextlist = &c->clean_list; 71 } else if (!list_empty(&c->dirty_list)) { 72 jffs2_dbg(1, "Picking block from dirty_list to GC next (clean_list was empty)\n"); 73 74 nextlist = &c->dirty_list; 75 } else if (!list_empty(&c->very_dirty_list)) { 76 jffs2_dbg(1, "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n"); 77 nextlist = &c->very_dirty_list; 78 } else if (!list_empty(&c->erasable_list)) { 79 jffs2_dbg(1, "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n"); 80 81 nextlist = &c->erasable_list; 82 } else if (!list_empty(&c->erasable_pending_wbuf_list)) { 83 /* There are blocks are wating for the wbuf sync */ 84 jffs2_dbg(1, "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n"); 85 spin_unlock(&c->erase_completion_lock); 86 jffs2_flush_wbuf_pad(c); 87 spin_lock(&c->erase_completion_lock); 88 goto again; 89 } else { 90 /* Eep. All were empty */ 91 jffs2_dbg(1, "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n"); 92 return NULL; 93 } 94 95 ret = list_entry(nextlist->next, struct jffs2_eraseblock, list); 96 list_del(&ret->list); 97 c->gcblock = ret; 98 ret->gc_node = ret->first_node; 99 if (!ret->gc_node) { 100 printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset); 101 BUG(); 102 } 103 104 /* Have we accidentally picked a clean block with wasted space ? */ 105 if (ret->wasted_size) { 106 jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n", 107 ret->wasted_size); 108 ret->dirty_size += ret->wasted_size; 109 c->wasted_size -= ret->wasted_size; 110 c->dirty_size += ret->wasted_size; 111 ret->wasted_size = 0; 112 } 113 114 return ret; 115 } 116 117 /* jffs2_garbage_collect_pass 118 * Make a single attempt to progress GC. Move one node, and possibly 119 * start erasing one eraseblock. 120 */ 121 int jffs2_garbage_collect_pass(struct jffs2_sb_info *c) 122 { 123 struct jffs2_inode_info *f; 124 struct jffs2_inode_cache *ic; 125 struct jffs2_eraseblock *jeb; 126 struct jffs2_raw_node_ref *raw; 127 uint32_t gcblock_dirty; 128 int ret = 0, inum, nlink; 129 int xattr = 0; 130 131 if (mutex_lock_interruptible(&c->alloc_sem)) 132 return -EINTR; 133 134 for (;;) { 135 spin_lock(&c->erase_completion_lock); 136 if (!c->unchecked_size) 137 break; 138 139 /* We can't start doing GC yet. We haven't finished checking 140 the node CRCs etc. Do it now. */ 141 142 /* checked_ino is protected by the alloc_sem */ 143 if (c->checked_ino > c->highest_ino && xattr) { 144 printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n", 145 c->unchecked_size); 146 jffs2_dbg_dump_block_lists_nolock(c); 147 spin_unlock(&c->erase_completion_lock); 148 mutex_unlock(&c->alloc_sem); 149 return -ENOSPC; 150 } 151 152 spin_unlock(&c->erase_completion_lock); 153 154 if (!xattr) 155 xattr = jffs2_verify_xattr(c); 156 157 spin_lock(&c->inocache_lock); 158 159 ic = jffs2_get_ino_cache(c, c->checked_ino++); 160 161 if (!ic) { 162 spin_unlock(&c->inocache_lock); 163 continue; 164 } 165 166 if (!ic->pino_nlink) { 167 jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n", 168 ic->ino); 169 spin_unlock(&c->inocache_lock); 170 jffs2_xattr_delete_inode(c, ic); 171 continue; 172 } 173 switch(ic->state) { 174 case INO_STATE_CHECKEDABSENT: 175 case INO_STATE_PRESENT: 176 jffs2_dbg(1, "Skipping ino #%u already checked\n", 177 ic->ino); 178 spin_unlock(&c->inocache_lock); 179 continue; 180 181 case INO_STATE_GC: 182 case INO_STATE_CHECKING: 183 printk(KERN_WARNING "Inode #%u is in state %d during CRC check phase!\n", ic->ino, ic->state); 184 spin_unlock(&c->inocache_lock); 185 BUG(); 186 187 case INO_STATE_READING: 188 /* We need to wait for it to finish, lest we move on 189 and trigger the BUG() above while we haven't yet 190 finished checking all its nodes */ 191 jffs2_dbg(1, "Waiting for ino #%u to finish reading\n", 192 ic->ino); 193 /* We need to come back again for the _same_ inode. We've 194 made no progress in this case, but that should be OK */ 195 c->checked_ino--; 196 197 mutex_unlock(&c->alloc_sem); 198 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock); 199 return 0; 200 201 default: 202 BUG(); 203 204 case INO_STATE_UNCHECKED: 205 ; 206 } 207 ic->state = INO_STATE_CHECKING; 208 spin_unlock(&c->inocache_lock); 209 210 jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n", 211 __func__, ic->ino); 212 213 ret = jffs2_do_crccheck_inode(c, ic); 214 if (ret) 215 printk(KERN_WARNING "Returned error for crccheck of ino #%u. Expect badness...\n", ic->ino); 216 217 jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT); 218 mutex_unlock(&c->alloc_sem); 219 return ret; 220 } 221 222 /* If there are any blocks which need erasing, erase them now */ 223 if (!list_empty(&c->erase_complete_list) || 224 !list_empty(&c->erase_pending_list)) { 225 spin_unlock(&c->erase_completion_lock); 226 mutex_unlock(&c->alloc_sem); 227 jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__); 228 if (jffs2_erase_pending_blocks(c, 1)) 229 return 0; 230 231 jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n"); 232 spin_lock(&c->erase_completion_lock); 233 mutex_lock(&c->alloc_sem); 234 } 235 236 /* First, work out which block we're garbage-collecting */ 237 jeb = c->gcblock; 238 239 if (!jeb) 240 jeb = jffs2_find_gc_block(c); 241 242 if (!jeb) { 243 /* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */ 244 if (c->nr_erasing_blocks) { 245 spin_unlock(&c->erase_completion_lock); 246 mutex_unlock(&c->alloc_sem); 247 return -EAGAIN; 248 } 249 jffs2_dbg(1, "jffs2: Couldn't find erase block to garbage collect!\n"); 250 spin_unlock(&c->erase_completion_lock); 251 mutex_unlock(&c->alloc_sem); 252 return -EIO; 253 } 254 255 jffs2_dbg(1, "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n", 256 jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size); 257 D1(if (c->nextblock) 258 printk(KERN_DEBUG "Nextblock at %08x, used_size %08x, dirty_size %08x, wasted_size %08x, free_size %08x\n", c->nextblock->offset, c->nextblock->used_size, c->nextblock->dirty_size, c->nextblock->wasted_size, c->nextblock->free_size)); 259 260 if (!jeb->used_size) { 261 mutex_unlock(&c->alloc_sem); 262 goto eraseit; 263 } 264 265 raw = jeb->gc_node; 266 gcblock_dirty = jeb->dirty_size; 267 268 while(ref_obsolete(raw)) { 269 jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n", 270 ref_offset(raw)); 271 raw = ref_next(raw); 272 if (unlikely(!raw)) { 273 printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n"); 274 printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", 275 jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size); 276 jeb->gc_node = raw; 277 spin_unlock(&c->erase_completion_lock); 278 mutex_unlock(&c->alloc_sem); 279 BUG(); 280 } 281 } 282 jeb->gc_node = raw; 283 284 jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n", 285 ref_offset(raw)); 286 287 if (!raw->next_in_ino) { 288 /* Inode-less node. Clean marker, snapshot or something like that */ 289 spin_unlock(&c->erase_completion_lock); 290 if (ref_flags(raw) == REF_PRISTINE) { 291 /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */ 292 jffs2_garbage_collect_pristine(c, NULL, raw); 293 } else { 294 /* Just mark it obsolete */ 295 jffs2_mark_node_obsolete(c, raw); 296 } 297 mutex_unlock(&c->alloc_sem); 298 goto eraseit_lock; 299 } 300 301 ic = jffs2_raw_ref_to_ic(raw); 302 303 #ifdef CONFIG_JFFS2_FS_XATTR 304 /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr. 305 * We can decide whether this node is inode or xattr by ic->class. */ 306 if (ic->class == RAWNODE_CLASS_XATTR_DATUM 307 || ic->class == RAWNODE_CLASS_XATTR_REF) { 308 spin_unlock(&c->erase_completion_lock); 309 310 if (ic->class == RAWNODE_CLASS_XATTR_DATUM) { 311 ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw); 312 } else { 313 ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw); 314 } 315 goto test_gcnode; 316 } 317 #endif 318 319 /* We need to hold the inocache. Either the erase_completion_lock or 320 the inocache_lock are sufficient; we trade down since the inocache_lock 321 causes less contention. */ 322 spin_lock(&c->inocache_lock); 323 324 spin_unlock(&c->erase_completion_lock); 325 326 jffs2_dbg(1, "%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n", 327 __func__, jeb->offset, ref_offset(raw), ref_flags(raw), 328 ic->ino); 329 330 /* Three possibilities: 331 1. Inode is already in-core. We must iget it and do proper 332 updating to its fragtree, etc. 333 2. Inode is not in-core, node is REF_PRISTINE. We lock the 334 inocache to prevent a read_inode(), copy the node intact. 335 3. Inode is not in-core, node is not pristine. We must iget() 336 and take the slow path. 337 */ 338 339 switch(ic->state) { 340 case INO_STATE_CHECKEDABSENT: 341 /* It's been checked, but it's not currently in-core. 342 We can just copy any pristine nodes, but have 343 to prevent anyone else from doing read_inode() while 344 we're at it, so we set the state accordingly */ 345 if (ref_flags(raw) == REF_PRISTINE) 346 ic->state = INO_STATE_GC; 347 else { 348 jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n", 349 ic->ino); 350 } 351 break; 352 353 case INO_STATE_PRESENT: 354 /* It's in-core. GC must iget() it. */ 355 break; 356 357 case INO_STATE_UNCHECKED: 358 case INO_STATE_CHECKING: 359 case INO_STATE_GC: 360 /* Should never happen. We should have finished checking 361 by the time we actually start doing any GC, and since 362 we're holding the alloc_sem, no other garbage collection 363 can happen. 364 */ 365 printk(KERN_CRIT "Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n", 366 ic->ino, ic->state); 367 mutex_unlock(&c->alloc_sem); 368 spin_unlock(&c->inocache_lock); 369 BUG(); 370 371 case INO_STATE_READING: 372 /* Someone's currently trying to read it. We must wait for 373 them to finish and then go through the full iget() route 374 to do the GC. However, sometimes read_inode() needs to get 375 the alloc_sem() (for marking nodes invalid) so we must 376 drop the alloc_sem before sleeping. */ 377 378 mutex_unlock(&c->alloc_sem); 379 jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n", 380 __func__, ic->ino, ic->state); 381 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock); 382 /* And because we dropped the alloc_sem we must start again from the 383 beginning. Ponder chance of livelock here -- we're returning success 384 without actually making any progress. 385 386 Q: What are the chances that the inode is back in INO_STATE_READING 387 again by the time we next enter this function? And that this happens 388 enough times to cause a real delay? 389 390 A: Small enough that I don't care :) 391 */ 392 return 0; 393 } 394 395 /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the 396 node intact, and we don't have to muck about with the fragtree etc. 397 because we know it's not in-core. If it _was_ in-core, we go through 398 all the iget() crap anyway */ 399 400 if (ic->state == INO_STATE_GC) { 401 spin_unlock(&c->inocache_lock); 402 403 ret = jffs2_garbage_collect_pristine(c, ic, raw); 404 405 spin_lock(&c->inocache_lock); 406 ic->state = INO_STATE_CHECKEDABSENT; 407 wake_up(&c->inocache_wq); 408 409 if (ret != -EBADFD) { 410 spin_unlock(&c->inocache_lock); 411 goto test_gcnode; 412 } 413 414 /* Fall through if it wanted us to, with inocache_lock held */ 415 } 416 417 /* Prevent the fairly unlikely race where the gcblock is 418 entirely obsoleted by the final close of a file which had 419 the only valid nodes in the block, followed by erasure, 420 followed by freeing of the ic because the erased block(s) 421 held _all_ the nodes of that inode.... never been seen but 422 it's vaguely possible. */ 423 424 inum = ic->ino; 425 nlink = ic->pino_nlink; 426 spin_unlock(&c->inocache_lock); 427 428 f = jffs2_gc_fetch_inode(c, inum, !nlink); 429 if (IS_ERR(f)) { 430 ret = PTR_ERR(f); 431 goto release_sem; 432 } 433 if (!f) { 434 ret = 0; 435 goto release_sem; 436 } 437 438 ret = jffs2_garbage_collect_live(c, jeb, raw, f); 439 440 jffs2_gc_release_inode(c, f); 441 442 test_gcnode: 443 if (jeb->dirty_size == gcblock_dirty && !ref_obsolete(jeb->gc_node)) { 444 /* Eep. This really should never happen. GC is broken */ 445 printk(KERN_ERR "Error garbage collecting node at %08x!\n", ref_offset(jeb->gc_node)); 446 ret = -ENOSPC; 447 } 448 release_sem: 449 mutex_unlock(&c->alloc_sem); 450 451 eraseit_lock: 452 /* If we've finished this block, start it erasing */ 453 spin_lock(&c->erase_completion_lock); 454 455 eraseit: 456 if (c->gcblock && !c->gcblock->used_size) { 457 jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n", 458 c->gcblock->offset); 459 /* We're GC'ing an empty block? */ 460 list_add_tail(&c->gcblock->list, &c->erase_pending_list); 461 c->gcblock = NULL; 462 c->nr_erasing_blocks++; 463 jffs2_garbage_collect_trigger(c); 464 } 465 spin_unlock(&c->erase_completion_lock); 466 467 return ret; 468 } 469 470 static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 471 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f) 472 { 473 struct jffs2_node_frag *frag; 474 struct jffs2_full_dnode *fn = NULL; 475 struct jffs2_full_dirent *fd; 476 uint32_t start = 0, end = 0, nrfrags = 0; 477 int ret = 0; 478 479 mutex_lock(&f->sem); 480 481 /* Now we have the lock for this inode. Check that it's still the one at the head 482 of the list. */ 483 484 spin_lock(&c->erase_completion_lock); 485 486 if (c->gcblock != jeb) { 487 spin_unlock(&c->erase_completion_lock); 488 jffs2_dbg(1, "GC block is no longer gcblock. Restart\n"); 489 goto upnout; 490 } 491 if (ref_obsolete(raw)) { 492 spin_unlock(&c->erase_completion_lock); 493 jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n"); 494 /* They'll call again */ 495 goto upnout; 496 } 497 spin_unlock(&c->erase_completion_lock); 498 499 /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */ 500 if (f->metadata && f->metadata->raw == raw) { 501 fn = f->metadata; 502 ret = jffs2_garbage_collect_metadata(c, jeb, f, fn); 503 goto upnout; 504 } 505 506 /* FIXME. Read node and do lookup? */ 507 for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) { 508 if (frag->node && frag->node->raw == raw) { 509 fn = frag->node; 510 end = frag->ofs + frag->size; 511 if (!nrfrags++) 512 start = frag->ofs; 513 if (nrfrags == frag->node->frags) 514 break; /* We've found them all */ 515 } 516 } 517 if (fn) { 518 if (ref_flags(raw) == REF_PRISTINE) { 519 ret = jffs2_garbage_collect_pristine(c, f->inocache, raw); 520 if (!ret) { 521 /* Urgh. Return it sensibly. */ 522 frag->node->raw = f->inocache->nodes; 523 } 524 if (ret != -EBADFD) 525 goto upnout; 526 } 527 /* We found a datanode. Do the GC */ 528 if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) { 529 /* It crosses a page boundary. Therefore, it must be a hole. */ 530 ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end); 531 } else { 532 /* It could still be a hole. But we GC the page this way anyway */ 533 ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end); 534 } 535 goto upnout; 536 } 537 538 /* Wasn't a dnode. Try dirent */ 539 for (fd = f->dents; fd; fd=fd->next) { 540 if (fd->raw == raw) 541 break; 542 } 543 544 if (fd && fd->ino) { 545 ret = jffs2_garbage_collect_dirent(c, jeb, f, fd); 546 } else if (fd) { 547 ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd); 548 } else { 549 printk(KERN_WARNING "Raw node at 0x%08x wasn't in node lists for ino #%u\n", 550 ref_offset(raw), f->inocache->ino); 551 if (ref_obsolete(raw)) { 552 printk(KERN_WARNING "But it's obsolete so we don't mind too much\n"); 553 } else { 554 jffs2_dbg_dump_node(c, ref_offset(raw)); 555 BUG(); 556 } 557 } 558 upnout: 559 mutex_unlock(&f->sem); 560 561 return ret; 562 } 563 564 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, 565 struct jffs2_inode_cache *ic, 566 struct jffs2_raw_node_ref *raw) 567 { 568 union jffs2_node_union *node; 569 size_t retlen; 570 int ret; 571 uint32_t phys_ofs, alloclen; 572 uint32_t crc, rawlen; 573 int retried = 0; 574 575 jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n", 576 ref_offset(raw)); 577 578 alloclen = rawlen = ref_totlen(c, c->gcblock, raw); 579 580 /* Ask for a small amount of space (or the totlen if smaller) because we 581 don't want to force wastage of the end of a block if splitting would 582 work. */ 583 if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN) 584 alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN; 585 586 ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen); 587 /* 'rawlen' is not the exact summary size; it is only an upper estimation */ 588 589 if (ret) 590 return ret; 591 592 if (alloclen < rawlen) { 593 /* Doesn't fit untouched. We'll go the old route and split it */ 594 return -EBADFD; 595 } 596 597 node = kmalloc(rawlen, GFP_KERNEL); 598 if (!node) 599 return -ENOMEM; 600 601 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node); 602 if (!ret && retlen != rawlen) 603 ret = -EIO; 604 if (ret) 605 goto out_node; 606 607 crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4); 608 if (je32_to_cpu(node->u.hdr_crc) != crc) { 609 printk(KERN_WARNING "Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 610 ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc); 611 goto bail; 612 } 613 614 switch(je16_to_cpu(node->u.nodetype)) { 615 case JFFS2_NODETYPE_INODE: 616 crc = crc32(0, node, sizeof(node->i)-8); 617 if (je32_to_cpu(node->i.node_crc) != crc) { 618 printk(KERN_WARNING "Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 619 ref_offset(raw), je32_to_cpu(node->i.node_crc), crc); 620 goto bail; 621 } 622 623 if (je32_to_cpu(node->i.dsize)) { 624 crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize)); 625 if (je32_to_cpu(node->i.data_crc) != crc) { 626 printk(KERN_WARNING "Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 627 ref_offset(raw), je32_to_cpu(node->i.data_crc), crc); 628 goto bail; 629 } 630 } 631 break; 632 633 case JFFS2_NODETYPE_DIRENT: 634 crc = crc32(0, node, sizeof(node->d)-8); 635 if (je32_to_cpu(node->d.node_crc) != crc) { 636 printk(KERN_WARNING "Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 637 ref_offset(raw), je32_to_cpu(node->d.node_crc), crc); 638 goto bail; 639 } 640 641 if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) { 642 printk(KERN_WARNING "Name in dirent node at 0x%08x contains zeroes\n", ref_offset(raw)); 643 goto bail; 644 } 645 646 if (node->d.nsize) { 647 crc = crc32(0, node->d.name, node->d.nsize); 648 if (je32_to_cpu(node->d.name_crc) != crc) { 649 printk(KERN_WARNING "Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 650 ref_offset(raw), je32_to_cpu(node->d.name_crc), crc); 651 goto bail; 652 } 653 } 654 break; 655 default: 656 /* If it's inode-less, we don't _know_ what it is. Just copy it intact */ 657 if (ic) { 658 printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n", 659 ref_offset(raw), je16_to_cpu(node->u.nodetype)); 660 goto bail; 661 } 662 } 663 664 /* OK, all the CRCs are good; this node can just be copied as-is. */ 665 retry: 666 phys_ofs = write_ofs(c); 667 668 ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node); 669 670 if (ret || (retlen != rawlen)) { 671 printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n", 672 rawlen, phys_ofs, ret, retlen); 673 if (retlen) { 674 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL); 675 } else { 676 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", phys_ofs); 677 } 678 if (!retried) { 679 /* Try to reallocate space and retry */ 680 uint32_t dummy; 681 struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size]; 682 683 retried = 1; 684 685 jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n"); 686 687 jffs2_dbg_acct_sanity_check(c,jeb); 688 jffs2_dbg_acct_paranoia_check(c, jeb); 689 690 ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen); 691 /* this is not the exact summary size of it, 692 it is only an upper estimation */ 693 694 if (!ret) { 695 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n", 696 phys_ofs); 697 698 jffs2_dbg_acct_sanity_check(c,jeb); 699 jffs2_dbg_acct_paranoia_check(c, jeb); 700 701 goto retry; 702 } 703 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n", 704 ret); 705 } 706 707 if (!ret) 708 ret = -EIO; 709 goto out_node; 710 } 711 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic); 712 713 jffs2_mark_node_obsolete(c, raw); 714 jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", 715 ref_offset(raw)); 716 717 out_node: 718 kfree(node); 719 return ret; 720 bail: 721 ret = -EBADFD; 722 goto out_node; 723 } 724 725 static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 726 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn) 727 { 728 struct jffs2_full_dnode *new_fn; 729 struct jffs2_raw_inode ri; 730 struct jffs2_node_frag *last_frag; 731 union jffs2_device_node dev; 732 char *mdata = NULL; 733 int mdatalen = 0; 734 uint32_t alloclen, ilen; 735 int ret; 736 737 if (S_ISBLK(JFFS2_F_I_MODE(f)) || 738 S_ISCHR(JFFS2_F_I_MODE(f)) ) { 739 /* For these, we don't actually need to read the old node */ 740 mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f)); 741 mdata = (char *)&dev; 742 jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n", 743 __func__, mdatalen); 744 } else if (S_ISLNK(JFFS2_F_I_MODE(f))) { 745 mdatalen = fn->size; 746 mdata = kmalloc(fn->size, GFP_KERNEL); 747 if (!mdata) { 748 printk(KERN_WARNING "kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n"); 749 return -ENOMEM; 750 } 751 ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen); 752 if (ret) { 753 printk(KERN_WARNING "read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n", ret); 754 kfree(mdata); 755 return ret; 756 } 757 jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n", 758 __func__, mdatalen); 759 760 } 761 762 ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen, 763 JFFS2_SUMMARY_INODE_SIZE); 764 if (ret) { 765 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n", 766 sizeof(ri)+ mdatalen, ret); 767 goto out; 768 } 769 770 last_frag = frag_last(&f->fragtree); 771 if (last_frag) 772 /* Fetch the inode length from the fragtree rather then 773 * from i_size since i_size may have not been updated yet */ 774 ilen = last_frag->ofs + last_frag->size; 775 else 776 ilen = JFFS2_F_I_SIZE(f); 777 778 memset(&ri, 0, sizeof(ri)); 779 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 780 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); 781 ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen); 782 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4)); 783 784 ri.ino = cpu_to_je32(f->inocache->ino); 785 ri.version = cpu_to_je32(++f->highest_version); 786 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f)); 787 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f)); 788 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f)); 789 ri.isize = cpu_to_je32(ilen); 790 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f)); 791 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f)); 792 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f)); 793 ri.offset = cpu_to_je32(0); 794 ri.csize = cpu_to_je32(mdatalen); 795 ri.dsize = cpu_to_je32(mdatalen); 796 ri.compr = JFFS2_COMPR_NONE; 797 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 798 ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen)); 799 800 new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC); 801 802 if (IS_ERR(new_fn)) { 803 printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn)); 804 ret = PTR_ERR(new_fn); 805 goto out; 806 } 807 jffs2_mark_node_obsolete(c, fn->raw); 808 jffs2_free_full_dnode(fn); 809 f->metadata = new_fn; 810 out: 811 if (S_ISLNK(JFFS2_F_I_MODE(f))) 812 kfree(mdata); 813 return ret; 814 } 815 816 static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 817 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd) 818 { 819 struct jffs2_full_dirent *new_fd; 820 struct jffs2_raw_dirent rd; 821 uint32_t alloclen; 822 int ret; 823 824 rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 825 rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 826 rd.nsize = strlen(fd->name); 827 rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize); 828 rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4)); 829 830 rd.pino = cpu_to_je32(f->inocache->ino); 831 rd.version = cpu_to_je32(++f->highest_version); 832 rd.ino = cpu_to_je32(fd->ino); 833 /* If the times on this inode were set by explicit utime() they can be different, 834 so refrain from splatting them. */ 835 if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f)) 836 rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f)); 837 else 838 rd.mctime = cpu_to_je32(0); 839 rd.type = fd->type; 840 rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8)); 841 rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize)); 842 843 ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen, 844 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize)); 845 if (ret) { 846 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n", 847 sizeof(rd)+rd.nsize, ret); 848 return ret; 849 } 850 new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC); 851 852 if (IS_ERR(new_fd)) { 853 printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd)); 854 return PTR_ERR(new_fd); 855 } 856 jffs2_add_fd_to_list(c, new_fd, &f->dents); 857 return 0; 858 } 859 860 static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 861 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd) 862 { 863 struct jffs2_full_dirent **fdp = &f->dents; 864 int found = 0; 865 866 /* On a medium where we can't actually mark nodes obsolete 867 pernamently, such as NAND flash, we need to work out 868 whether this deletion dirent is still needed to actively 869 delete a 'real' dirent with the same name that's still 870 somewhere else on the flash. */ 871 if (!jffs2_can_mark_obsolete(c)) { 872 struct jffs2_raw_dirent *rd; 873 struct jffs2_raw_node_ref *raw; 874 int ret; 875 size_t retlen; 876 int name_len = strlen(fd->name); 877 uint32_t name_crc = crc32(0, fd->name, name_len); 878 uint32_t rawlen = ref_totlen(c, jeb, fd->raw); 879 880 rd = kmalloc(rawlen, GFP_KERNEL); 881 if (!rd) 882 return -ENOMEM; 883 884 /* Prevent the erase code from nicking the obsolete node refs while 885 we're looking at them. I really don't like this extra lock but 886 can't see any alternative. Suggestions on a postcard to... */ 887 mutex_lock(&c->erase_free_sem); 888 889 for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) { 890 891 cond_resched(); 892 893 /* We only care about obsolete ones */ 894 if (!(ref_obsolete(raw))) 895 continue; 896 897 /* Any dirent with the same name is going to have the same length... */ 898 if (ref_totlen(c, NULL, raw) != rawlen) 899 continue; 900 901 /* Doesn't matter if there's one in the same erase block. We're going to 902 delete it too at the same time. */ 903 if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset)) 904 continue; 905 906 jffs2_dbg(1, "Check potential deletion dirent at %08x\n", 907 ref_offset(raw)); 908 909 /* This is an obsolete node belonging to the same directory, and it's of the right 910 length. We need to take a closer look...*/ 911 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd); 912 if (ret) { 913 printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Read error (%d) reading obsolete node at %08x\n", ret, ref_offset(raw)); 914 /* If we can't read it, we don't need to continue to obsolete it. Continue */ 915 continue; 916 } 917 if (retlen != rawlen) { 918 printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Short read (%zd not %u) reading header from obsolete node at %08x\n", 919 retlen, rawlen, ref_offset(raw)); 920 continue; 921 } 922 923 if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT) 924 continue; 925 926 /* If the name CRC doesn't match, skip */ 927 if (je32_to_cpu(rd->name_crc) != name_crc) 928 continue; 929 930 /* If the name length doesn't match, or it's another deletion dirent, skip */ 931 if (rd->nsize != name_len || !je32_to_cpu(rd->ino)) 932 continue; 933 934 /* OK, check the actual name now */ 935 if (memcmp(rd->name, fd->name, name_len)) 936 continue; 937 938 /* OK. The name really does match. There really is still an older node on 939 the flash which our deletion dirent obsoletes. So we have to write out 940 a new deletion dirent to replace it */ 941 mutex_unlock(&c->erase_free_sem); 942 943 jffs2_dbg(1, "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n", 944 ref_offset(fd->raw), fd->name, 945 ref_offset(raw), je32_to_cpu(rd->ino)); 946 kfree(rd); 947 948 return jffs2_garbage_collect_dirent(c, jeb, f, fd); 949 } 950 951 mutex_unlock(&c->erase_free_sem); 952 kfree(rd); 953 } 954 955 /* FIXME: If we're deleting a dirent which contains the current mtime and ctime, 956 we should update the metadata node with those times accordingly */ 957 958 /* No need for it any more. Just mark it obsolete and remove it from the list */ 959 while (*fdp) { 960 if ((*fdp) == fd) { 961 found = 1; 962 *fdp = fd->next; 963 break; 964 } 965 fdp = &(*fdp)->next; 966 } 967 if (!found) { 968 printk(KERN_WARNING "Deletion dirent \"%s\" not found in list for ino #%u\n", fd->name, f->inocache->ino); 969 } 970 jffs2_mark_node_obsolete(c, fd->raw); 971 jffs2_free_full_dirent(fd); 972 return 0; 973 } 974 975 static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 976 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn, 977 uint32_t start, uint32_t end) 978 { 979 struct jffs2_raw_inode ri; 980 struct jffs2_node_frag *frag; 981 struct jffs2_full_dnode *new_fn; 982 uint32_t alloclen, ilen; 983 int ret; 984 985 jffs2_dbg(1, "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n", 986 f->inocache->ino, start, end); 987 988 memset(&ri, 0, sizeof(ri)); 989 990 if(fn->frags > 1) { 991 size_t readlen; 992 uint32_t crc; 993 /* It's partially obsoleted by a later write. So we have to 994 write it out again with the _same_ version as before */ 995 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri); 996 if (readlen != sizeof(ri) || ret) { 997 printk(KERN_WARNING "Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n", ret, readlen); 998 goto fill; 999 } 1000 if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) { 1001 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n", 1002 ref_offset(fn->raw), 1003 je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE); 1004 return -EIO; 1005 } 1006 if (je32_to_cpu(ri.totlen) != sizeof(ri)) { 1007 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n", 1008 ref_offset(fn->raw), 1009 je32_to_cpu(ri.totlen), sizeof(ri)); 1010 return -EIO; 1011 } 1012 crc = crc32(0, &ri, sizeof(ri)-8); 1013 if (crc != je32_to_cpu(ri.node_crc)) { 1014 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n", 1015 ref_offset(fn->raw), 1016 je32_to_cpu(ri.node_crc), crc); 1017 /* FIXME: We could possibly deal with this by writing new holes for each frag */ 1018 printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", 1019 start, end, f->inocache->ino); 1020 goto fill; 1021 } 1022 if (ri.compr != JFFS2_COMPR_ZERO) { 1023 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!\n", ref_offset(fn->raw)); 1024 printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", 1025 start, end, f->inocache->ino); 1026 goto fill; 1027 } 1028 } else { 1029 fill: 1030 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 1031 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); 1032 ri.totlen = cpu_to_je32(sizeof(ri)); 1033 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4)); 1034 1035 ri.ino = cpu_to_je32(f->inocache->ino); 1036 ri.version = cpu_to_je32(++f->highest_version); 1037 ri.offset = cpu_to_je32(start); 1038 ri.dsize = cpu_to_je32(end - start); 1039 ri.csize = cpu_to_je32(0); 1040 ri.compr = JFFS2_COMPR_ZERO; 1041 } 1042 1043 frag = frag_last(&f->fragtree); 1044 if (frag) 1045 /* Fetch the inode length from the fragtree rather then 1046 * from i_size since i_size may have not been updated yet */ 1047 ilen = frag->ofs + frag->size; 1048 else 1049 ilen = JFFS2_F_I_SIZE(f); 1050 1051 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f)); 1052 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f)); 1053 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f)); 1054 ri.isize = cpu_to_je32(ilen); 1055 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f)); 1056 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f)); 1057 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f)); 1058 ri.data_crc = cpu_to_je32(0); 1059 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 1060 1061 ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen, 1062 JFFS2_SUMMARY_INODE_SIZE); 1063 if (ret) { 1064 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n", 1065 sizeof(ri), ret); 1066 return ret; 1067 } 1068 new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC); 1069 1070 if (IS_ERR(new_fn)) { 1071 printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn)); 1072 return PTR_ERR(new_fn); 1073 } 1074 if (je32_to_cpu(ri.version) == f->highest_version) { 1075 jffs2_add_full_dnode_to_inode(c, f, new_fn); 1076 if (f->metadata) { 1077 jffs2_mark_node_obsolete(c, f->metadata->raw); 1078 jffs2_free_full_dnode(f->metadata); 1079 f->metadata = NULL; 1080 } 1081 return 0; 1082 } 1083 1084 /* 1085 * We should only get here in the case where the node we are 1086 * replacing had more than one frag, so we kept the same version 1087 * number as before. (Except in case of error -- see 'goto fill;' 1088 * above.) 1089 */ 1090 D1(if(unlikely(fn->frags <= 1)) { 1091 printk(KERN_WARNING "jffs2_garbage_collect_hole: Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n", 1092 fn->frags, je32_to_cpu(ri.version), f->highest_version, 1093 je32_to_cpu(ri.ino)); 1094 }); 1095 1096 /* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */ 1097 mark_ref_normal(new_fn->raw); 1098 1099 for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs); 1100 frag; frag = frag_next(frag)) { 1101 if (frag->ofs > fn->size + fn->ofs) 1102 break; 1103 if (frag->node == fn) { 1104 frag->node = new_fn; 1105 new_fn->frags++; 1106 fn->frags--; 1107 } 1108 } 1109 if (fn->frags) { 1110 printk(KERN_WARNING "jffs2_garbage_collect_hole: Old node still has frags!\n"); 1111 BUG(); 1112 } 1113 if (!new_fn->frags) { 1114 printk(KERN_WARNING "jffs2_garbage_collect_hole: New node has no frags!\n"); 1115 BUG(); 1116 } 1117 1118 jffs2_mark_node_obsolete(c, fn->raw); 1119 jffs2_free_full_dnode(fn); 1120 1121 return 0; 1122 } 1123 1124 static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb, 1125 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn, 1126 uint32_t start, uint32_t end) 1127 { 1128 struct jffs2_full_dnode *new_fn; 1129 struct jffs2_raw_inode ri; 1130 uint32_t alloclen, offset, orig_end, orig_start; 1131 int ret = 0; 1132 unsigned char *comprbuf = NULL, *writebuf; 1133 unsigned long pg; 1134 unsigned char *pg_ptr; 1135 1136 memset(&ri, 0, sizeof(ri)); 1137 1138 jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n", 1139 f->inocache->ino, start, end); 1140 1141 orig_end = end; 1142 orig_start = start; 1143 1144 if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) { 1145 /* Attempt to do some merging. But only expand to cover logically 1146 adjacent frags if the block containing them is already considered 1147 to be dirty. Otherwise we end up with GC just going round in 1148 circles dirtying the nodes it already wrote out, especially 1149 on NAND where we have small eraseblocks and hence a much higher 1150 chance of nodes having to be split to cross boundaries. */ 1151 1152 struct jffs2_node_frag *frag; 1153 uint32_t min, max; 1154 1155 min = start & ~(PAGE_CACHE_SIZE-1); 1156 max = min + PAGE_CACHE_SIZE; 1157 1158 frag = jffs2_lookup_node_frag(&f->fragtree, start); 1159 1160 /* BUG_ON(!frag) but that'll happen anyway... */ 1161 1162 BUG_ON(frag->ofs != start); 1163 1164 /* First grow down... */ 1165 while((frag = frag_prev(frag)) && frag->ofs >= min) { 1166 1167 /* If the previous frag doesn't even reach the beginning, there's 1168 excessive fragmentation. Just merge. */ 1169 if (frag->ofs > min) { 1170 jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n", 1171 frag->ofs, frag->ofs+frag->size); 1172 start = frag->ofs; 1173 continue; 1174 } 1175 /* OK. This frag holds the first byte of the page. */ 1176 if (!frag->node || !frag->node->raw) { 1177 jffs2_dbg(1, "First frag in page is hole (0x%x-0x%x). Not expanding down.\n", 1178 frag->ofs, frag->ofs+frag->size); 1179 break; 1180 } else { 1181 1182 /* OK, it's a frag which extends to the beginning of the page. Does it live 1183 in a block which is still considered clean? If so, don't obsolete it. 1184 If not, cover it anyway. */ 1185 1186 struct jffs2_raw_node_ref *raw = frag->node->raw; 1187 struct jffs2_eraseblock *jeb; 1188 1189 jeb = &c->blocks[raw->flash_offset / c->sector_size]; 1190 1191 if (jeb == c->gcblock) { 1192 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n", 1193 frag->ofs, 1194 frag->ofs + frag->size, 1195 ref_offset(raw)); 1196 start = frag->ofs; 1197 break; 1198 } 1199 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) { 1200 jffs2_dbg(1, "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n", 1201 frag->ofs, 1202 frag->ofs + frag->size, 1203 jeb->offset); 1204 break; 1205 } 1206 1207 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n", 1208 frag->ofs, 1209 frag->ofs + frag->size, 1210 jeb->offset); 1211 start = frag->ofs; 1212 break; 1213 } 1214 } 1215 1216 /* ... then up */ 1217 1218 /* Find last frag which is actually part of the node we're to GC. */ 1219 frag = jffs2_lookup_node_frag(&f->fragtree, end-1); 1220 1221 while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) { 1222 1223 /* If the previous frag doesn't even reach the beginning, there's lots 1224 of fragmentation. Just merge. */ 1225 if (frag->ofs+frag->size < max) { 1226 jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n", 1227 frag->ofs, frag->ofs+frag->size); 1228 end = frag->ofs + frag->size; 1229 continue; 1230 } 1231 1232 if (!frag->node || !frag->node->raw) { 1233 jffs2_dbg(1, "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n", 1234 frag->ofs, frag->ofs+frag->size); 1235 break; 1236 } else { 1237 1238 /* OK, it's a frag which extends to the beginning of the page. Does it live 1239 in a block which is still considered clean? If so, don't obsolete it. 1240 If not, cover it anyway. */ 1241 1242 struct jffs2_raw_node_ref *raw = frag->node->raw; 1243 struct jffs2_eraseblock *jeb; 1244 1245 jeb = &c->blocks[raw->flash_offset / c->sector_size]; 1246 1247 if (jeb == c->gcblock) { 1248 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n", 1249 frag->ofs, 1250 frag->ofs + frag->size, 1251 ref_offset(raw)); 1252 end = frag->ofs + frag->size; 1253 break; 1254 } 1255 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) { 1256 jffs2_dbg(1, "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n", 1257 frag->ofs, 1258 frag->ofs + frag->size, 1259 jeb->offset); 1260 break; 1261 } 1262 1263 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n", 1264 frag->ofs, 1265 frag->ofs + frag->size, 1266 jeb->offset); 1267 end = frag->ofs + frag->size; 1268 break; 1269 } 1270 } 1271 jffs2_dbg(1, "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n", 1272 orig_start, orig_end, start, end); 1273 1274 D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size)); 1275 BUG_ON(end < orig_end); 1276 BUG_ON(start > orig_start); 1277 } 1278 1279 /* First, use readpage() to read the appropriate page into the page cache */ 1280 /* Q: What happens if we actually try to GC the _same_ page for which commit_write() 1281 * triggered garbage collection in the first place? 1282 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the 1283 * page OK. We'll actually write it out again in commit_write, which is a little 1284 * suboptimal, but at least we're correct. 1285 */ 1286 pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg); 1287 1288 if (IS_ERR(pg_ptr)) { 1289 printk(KERN_WARNING "read_cache_page() returned error: %ld\n", PTR_ERR(pg_ptr)); 1290 return PTR_ERR(pg_ptr); 1291 } 1292 1293 offset = start; 1294 while(offset < orig_end) { 1295 uint32_t datalen; 1296 uint32_t cdatalen; 1297 uint16_t comprtype = JFFS2_COMPR_NONE; 1298 1299 ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, 1300 &alloclen, JFFS2_SUMMARY_INODE_SIZE); 1301 1302 if (ret) { 1303 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n", 1304 sizeof(ri)+ JFFS2_MIN_DATA_LEN, ret); 1305 break; 1306 } 1307 cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset); 1308 datalen = end - offset; 1309 1310 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1)); 1311 1312 comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen); 1313 1314 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 1315 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); 1316 ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen); 1317 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4)); 1318 1319 ri.ino = cpu_to_je32(f->inocache->ino); 1320 ri.version = cpu_to_je32(++f->highest_version); 1321 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f)); 1322 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f)); 1323 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f)); 1324 ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f)); 1325 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f)); 1326 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f)); 1327 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f)); 1328 ri.offset = cpu_to_je32(offset); 1329 ri.csize = cpu_to_je32(cdatalen); 1330 ri.dsize = cpu_to_je32(datalen); 1331 ri.compr = comprtype & 0xff; 1332 ri.usercompr = (comprtype >> 8) & 0xff; 1333 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 1334 ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen)); 1335 1336 new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC); 1337 1338 jffs2_free_comprbuf(comprbuf, writebuf); 1339 1340 if (IS_ERR(new_fn)) { 1341 printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn)); 1342 ret = PTR_ERR(new_fn); 1343 break; 1344 } 1345 ret = jffs2_add_full_dnode_to_inode(c, f, new_fn); 1346 offset += datalen; 1347 if (f->metadata) { 1348 jffs2_mark_node_obsolete(c, f->metadata->raw); 1349 jffs2_free_full_dnode(f->metadata); 1350 f->metadata = NULL; 1351 } 1352 } 1353 1354 jffs2_gc_release_page(c, pg_ptr, &pg); 1355 return ret; 1356 } 1357