14
#include <linux/kernel.h>
15
#include <linux/mtd/mtd.h>
16
#include <linux/slab.h>
17
#include <linux/pagemap.h>
18
#include <linux/crc32.h>
19
#include <linux/compiler.h>
20
#include <linux/stat.h>
24
#define jiffies ((unsigned long) clock_sys_ticks())
26
static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
27
struct jffs2_inode_cache *ic,
28
struct jffs2_raw_node_ref *raw);
29
static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c,
30
struct jffs2_eraseblock *jeb,
31
struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
32
static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c,
33
struct jffs2_eraseblock *jeb,
34
struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
35
static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c,
36
struct jffs2_eraseblock *jeb,
37
struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
38
static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c,
39
struct jffs2_eraseblock *jeb,
40
struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
41
uint32_t start, uint32_t end);
42
static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c,
43
struct jffs2_eraseblock *jeb,
44
struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
45
uint32_t start, uint32_t end);
46
static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,
47
struct jffs2_eraseblock *jeb,
48
struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
51
static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c) {
52
struct jffs2_eraseblock *ret;
53
struct list_head *nextlist = NULL;
54
int n = jiffies % 128;
62
if (!list_empty(&c->bad_used_list) &&
63
c->nr_free_blocks > c->resv_blocks_gcbad) {
64
D1(printk( "Picking block from bad_used_list to GC next\n"));
65
nextlist = &c->bad_used_list;
66
} else if (n < 50 && !list_empty(&c->erasable_list)) {
70
D1(printk( "Picking block from erasable_list to GC next\n"));
71
nextlist = &c->erasable_list;
72
} else if (n < 110 && !list_empty(&c->very_dirty_list)) {
74
D1(printk( "Picking block from very_dirty_list to GC next\n"));
75
nextlist = &c->very_dirty_list;
76
} else if (n < 126 && !list_empty(&c->dirty_list)) {
77
D1(printk( "Picking block from dirty_list to GC next\n"));
78
nextlist = &c->dirty_list;
79
} else if (!list_empty(&c->clean_list)) {
80
D1(printk( "Picking block from clean_list to GC next\n"));
81
nextlist = &c->clean_list;
82
} else if (!list_empty(&c->dirty_list)) {
83
D1(printk( "Picking block from dirty_list to GC next (clean_list was empty)\n"));
85
nextlist = &c->dirty_list;
86
} else if (!list_empty(&c->very_dirty_list)) {
87
D1(printk( "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n"));
88
nextlist = &c->very_dirty_list;
89
} else if (!list_empty(&c->erasable_list)) {
90
D1(printk( "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n"));
92
nextlist = &c->erasable_list;
93
} else if (!list_empty(&c->erasable_pending_wbuf_list)) {
95
D1(printk( "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n"));
96
spin_unlock(&c->erase_completion_lock);
97
jffs2_flush_wbuf_pad(c);
98
spin_lock(&c->erase_completion_lock);
102
D1(printk(KERN_NOTICE "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n"));
106
ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
107
list_del(&ret->list);
109
ret->gc_node = ret->first_node;
111
printk( "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset);
116
if (ret->wasted_size) {
117
D1(printk( "Converting wasted_size %08x to dirty_size\n", ret->wasted_size));
118
ret->dirty_size += ret->wasted_size;
119
c->wasted_size -= ret->wasted_size;
120
c->dirty_size += ret->wasted_size;
121
ret->wasted_size = 0;
131
int jffs2_garbage_collect_pass(struct jffs2_sb_info *c) {
132
struct jffs2_inode_info *f;
133
struct jffs2_inode_cache *ic;
134
struct jffs2_eraseblock *jeb;
135
struct jffs2_raw_node_ref *raw;
136
int ret = 0, inum, nlink;
138
if (down_interruptible(&c->alloc_sem)) {
143
spin_lock(&c->erase_completion_lock);
144
if (!c->unchecked_size) {
153
if (c->checked_ino > c->highest_ino) {
154
printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n",
156
jffs2_dbg_dump_block_lists_nolock(c);
157
spin_unlock(&c->erase_completion_lock);
161
spin_unlock(&c->erase_completion_lock);
163
spin_lock(&c->inocache_lock);
165
ic = jffs2_get_ino_cache(c, c->checked_ino++);
168
spin_unlock(&c->inocache_lock);
173
D1(printk( "Skipping check of ino #%d with nlink zero\n",
175
spin_unlock(&c->inocache_lock);
179
case INO_STATE_CHECKEDABSENT:
180
case INO_STATE_PRESENT:
181
D1(printk( "Skipping ino #%u already checked\n", ic->ino));
182
spin_unlock(&c->inocache_lock);
186
case INO_STATE_CHECKING:
187
printk( "Inode #%u is in state %d during CRC check phase!\n", ic->ino, ic->state);
188
spin_unlock(&c->inocache_lock);
191
case INO_STATE_READING:
196
D1(printk( "Waiting for ino #%u to finish reading\n", ic->ino));
198
sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
204
case INO_STATE_UNCHECKED:
207
ic->state = INO_STATE_CHECKING;
208
spin_unlock(&c->inocache_lock);
210
D1(printk( "jffs2_garbage_collect_pass() triggering inode scan of ino#%u\n", ic->ino));
212
ret = jffs2_do_crccheck_inode(c, ic);
214
printk( "Returned error for crccheck of ino #%u. Expect badness...\n", ic->ino);
217
jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
226
jeb = jffs2_find_gc_block(c);
230
D1 (printk(KERN_NOTICE "jffs2: Couldn't find erase block to garbage collect!\n"));
231
spin_unlock(&c->erase_completion_lock);
236
D1(printk( "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n", jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size));
238
printk( "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));
240
if (!jeb->used_size) {
247
while(ref_obsolete(raw)) {
248
D1(printk( "Node at 0x%08x is obsolete... skipping\n", ref_offset(raw)));
249
raw = raw->next_phys;
250
if (unlikely(!raw)) {
251
printk( "eep. End of raw list while still supposedly nodes to GC\n");
252
printk( "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
253
jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size);
255
spin_unlock(&c->erase_completion_lock);
262
D1(printk( "Going to garbage collect node at 0x%08x\n", ref_offset(raw)));
264
if (!raw->next_in_ino) {
269
spin_unlock(&c->erase_completion_lock);
270
jffs2_mark_node_obsolete(c, raw);
275
ic = jffs2_raw_ref_to_ic(raw);
281
spin_lock(&c->inocache_lock);
283
spin_unlock(&c->erase_completion_lock);
285
D1(printk( "jffs2_garbage_collect_pass collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n", jeb->offset, ref_offset(raw), ref_flags(raw), ic->ino));
296
case INO_STATE_CHECKEDABSENT:
302
if (ref_flags(raw) == REF_PRISTINE) {
303
ic->state = INO_STATE_GC;
305
D1(printk( "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
310
case INO_STATE_PRESENT:
314
case INO_STATE_UNCHECKED:
315
case INO_STATE_CHECKING:
322
printk(KERN_CRIT "Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
325
spin_unlock(&c->inocache_lock);
328
case INO_STATE_READING:
337
D1(printk( "jffs2_garbage_collect_pass() waiting for ino #%u in state %d\n",
338
ic->ino, ic->state));
339
sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
357
if (ic->state == INO_STATE_GC) {
358
spin_unlock(&c->inocache_lock);
360
ret = jffs2_garbage_collect_pristine(c, ic, raw);
362
spin_lock(&c->inocache_lock);
363
ic->state = INO_STATE_CHECKEDABSENT;
364
wake_up(&c->inocache_wq);
366
if (ret != -EBADFD) {
367
spin_unlock(&c->inocache_lock);
384
spin_unlock(&c->inocache_lock);
386
f = jffs2_gc_fetch_inode(c, inum, nlink);
396
ret = jffs2_garbage_collect_live(c, jeb, raw, f);
398
jffs2_gc_release_inode(c, f);
405
spin_lock(&c->erase_completion_lock);
408
if (c->gcblock && !c->gcblock->used_size) {
409
D1(printk( "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n", c->gcblock->offset));
411
list_add_tail(&c->gcblock->list, &c->erase_pending_list);
413
c->nr_erasing_blocks++;
414
jffs2_erase_pending_trigger(c);
416
spin_unlock(&c->erase_completion_lock);
421
static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,
422
struct jffs2_eraseblock *jeb,
423
struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f) {
424
struct jffs2_node_frag *frag;
425
struct jffs2_full_dnode *fn = NULL;
426
struct jffs2_full_dirent *fd;
427
uint32_t start = 0, end = 0, nrfrags = 0;
435
spin_lock(&c->erase_completion_lock);
437
if (c->gcblock != jeb) {
438
spin_unlock(&c->erase_completion_lock);
439
D1(printk( "GC block is no longer gcblock. Restart\n"));
442
if (ref_obsolete(raw)) {
443
spin_unlock(&c->erase_completion_lock);
444
D1(printk( "node to be GC'd was obsoleted in the meantime.\n"));
448
spin_unlock(&c->erase_completion_lock);
451
if (f->metadata && f->metadata->raw == raw) {
453
ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
458
for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
459
if (frag->node && frag->node->raw == raw) {
461
end = frag->ofs + frag->size;
465
if (nrfrags == frag->node->frags) {
471
if (ref_flags(raw) == REF_PRISTINE) {
472
ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
475
frag->node->raw = f->inocache->nodes;
477
if (ret != -EBADFD) {
482
if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
484
ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
487
ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
493
for (fd = f->dents; fd; fd=fd->next) {
494
if (fd->raw == raw) {
500
ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
502
ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
504
printk( "Raw node at 0x%08x wasn't in node lists for ino #%u\n",
505
ref_offset(raw), f->inocache->ino);
506
if (ref_obsolete(raw)) {
507
printk( "But it's obsolete so we don't mind too much\n");
509
jffs2_dbg_dump_node(c, ref_offset(raw));
519
static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
520
struct jffs2_inode_cache *ic,
521
struct jffs2_raw_node_ref *raw) {
522
union jffs2_node_union *node;
523
struct jffs2_raw_node_ref *nraw;
526
uint32_t phys_ofs, alloclen;
527
uint32_t crc, rawlen;
530
D1(printk( "Going to GC REF_PRISTINE node at 0x%08x\n", ref_offset(raw)));
532
rawlen = ref_totlen(c, c->gcblock, raw);
538
ret = jffs2_reserve_space_gc(c, min_t(uint32_t,
539
sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN,
540
rawlen), &phys_ofs, &alloclen);
545
if (alloclen < rawlen) {
550
node = kmalloc(rawlen, GFP_KERNEL);
555
ret = jffs2_flash_read(c, ref_offset(raw),
556
rawlen, &retlen, (unsigned char *)node);
557
if (!ret && retlen != rawlen) {
564
crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
565
if (je32_to_cpu(node->u.hdr_crc) != crc) {
566
printk( "Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
567
ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
571
switch(je16_to_cpu(node->u.nodetype)) {
572
case JFFS2_NODETYPE_INODE:
573
crc = crc32(0, node, sizeof(node->i)-8);
574
if (je32_to_cpu(node->i.node_crc) != crc) {
575
printk( "Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
576
ref_offset(raw), je32_to_cpu(node->i.node_crc), crc);
580
if (je32_to_cpu(node->i.dsize)) {
581
crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
582
if (je32_to_cpu(node->i.data_crc) != crc) {
583
printk( "Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
584
ref_offset(raw), je32_to_cpu(node->i.data_crc), crc);
590
case JFFS2_NODETYPE_DIRENT:
591
crc = crc32(0, node, sizeof(node->d)-8);
592
if (je32_to_cpu(node->d.node_crc) != crc) {
593
printk( "Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
594
ref_offset(raw), je32_to_cpu(node->d.node_crc), crc);
599
crc = crc32(0, node->d.name, node->d.nsize);
600
if (je32_to_cpu(node->d.name_crc) != crc) {
601
printk( "Name CRC failed on REF_PRISTINE dirent ode at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
602
ref_offset(raw), je32_to_cpu(node->d.name_crc), crc);
608
printk( "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
609
ref_offset(raw), je16_to_cpu(node->u.nodetype));
613
nraw = jffs2_alloc_raw_node_ref();
621
nraw->flash_offset = phys_ofs;
622
nraw->__totlen = rawlen;
623
nraw->next_phys = NULL;
625
ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (unsigned char *)node);
627
if (ret || (retlen != rawlen)) {
628
printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
629
rawlen, phys_ofs, ret, retlen);
632
nraw->next_in_ino = NULL;
634
nraw->flash_offset |= REF_OBSOLETE;
635
jffs2_add_physical_node_ref(c, nraw);
636
jffs2_mark_node_obsolete(c, nraw);
638
printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", nraw->flash_offset);
639
jffs2_free_raw_node_ref(nraw);
641
if (!retried && (nraw = jffs2_alloc_raw_node_ref())) {
644
struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
648
D1(printk( "Retrying failed write of REF_PRISTINE node.\n"));
650
jffs2_dbg_acct_sanity_check(c,jeb);
651
jffs2_dbg_acct_paranoia_check(c, jeb);
653
ret = jffs2_reserve_space_gc(c, rawlen, &phys_ofs, &dummy);
656
D1(printk( "Allocated space at 0x%08x to retry failed write.\n", phys_ofs));
658
jffs2_dbg_acct_sanity_check(c,jeb);
659
jffs2_dbg_acct_paranoia_check(c, jeb);
663
D1(printk( "Failed to allocate space to retry failed write: %d!\n", ret));
664
jffs2_free_raw_node_ref(nraw);
667
jffs2_free_raw_node_ref(nraw);
673
nraw->flash_offset |= REF_PRISTINE;
674
jffs2_add_physical_node_ref(c, nraw);
681
spin_lock(&c->erase_completion_lock);
682
nraw->next_in_ino = ic->nodes;
684
spin_unlock(&c->erase_completion_lock);
686
jffs2_mark_node_obsolete(c, raw);
687
D1(printk( "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", ref_offset(raw)));
697
static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c,
698
struct jffs2_eraseblock *jeb,
699
struct jffs2_inode_info *f, struct jffs2_full_dnode *fn) {
700
struct jffs2_full_dnode *new_fn;
701
struct jffs2_raw_inode ri;
702
struct jffs2_node_frag *last_frag;
704
char *mdata = NULL, mdatalen = 0;
705
uint32_t alloclen, phys_ofs, ilen;
708
if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
709
S_ISCHR(JFFS2_F_I_MODE(f)) ) {
712
dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) |
713
JFFS2_F_I_RDEV_MIN(f)));
714
mdata = (char *)&dev;
715
mdatalen = sizeof(dev);
716
D1(printk( "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen));
717
} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
719
mdata = kmalloc(fn->size, GFP_KERNEL);
721
printk( "kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
724
ret = jffs2_read_dnode(c, f, fn, (unsigned char *) mdata, 0, mdatalen);
726
printk( "read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n", ret);
730
D1(printk( "jffs2_garbage_collect_metadata(): Writing %d bites of symlink target\n", mdatalen));
734
ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen,
735
&phys_ofs, &alloclen);
737
printk( "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
738
sizeof(ri)+ mdatalen, ret);
742
last_frag = frag_last(&f->fragtree);
746
ilen = last_frag->ofs + last_frag->size;
748
ilen = JFFS2_F_I_SIZE(f);
751
memset(&ri, 0, sizeof(ri));
752
ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
753
ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
754
ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
755
ri.hdr_crc = cpu_to_je32(crc32(0, &ri,
756
sizeof(struct jffs2_unknown_node) - 4));
758
ri.ino = cpu_to_je32(f->inocache->ino);
759
ri.version = cpu_to_je32(++f->highest_version);
760
ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
761
ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
762
ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
763
ri.isize = cpu_to_je32(ilen);
764
ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
765
ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
766
ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
767
ri.offset = cpu_to_je32(0);
768
ri.csize = cpu_to_je32(mdatalen);
769
ri.dsize = cpu_to_je32(mdatalen);
770
ri.compr = JFFS2_COMPR_NONE;
771
ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
772
ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
774
new_fn = jffs2_write_dnode(c, f, &ri,
775
(const unsigned char *)mdata, mdatalen, phys_ofs, ALLOC_GC);
777
if (IS_ERR(new_fn)) {
778
printk( "Error writing new dnode: %lu\n", PTR_ERR(new_fn));
779
ret = PTR_ERR(new_fn);
782
jffs2_mark_node_obsolete(c, fn->raw);
783
jffs2_free_full_dnode(fn);
784
f->metadata = new_fn;
786
if (S_ISLNK(JFFS2_F_I_MODE(f))) {
792
static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
793
struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
795
struct jffs2_full_dirent *new_fd;
796
struct jffs2_raw_dirent rd;
797
uint32_t alloclen, phys_ofs;
800
rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
801
rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
802
rd.nsize = strlen((const char *)fd->name);
803
rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
804
rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
806
rd.pino = cpu_to_je32(f->inocache->ino);
807
rd.version = cpu_to_je32(++f->highest_version);
808
rd.ino = cpu_to_je32(fd->ino);
809
rd.mctime = cpu_to_je32(max(JFFS2_F_I_MTIME(f), JFFS2_F_I_CTIME(f)));
811
rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
812
rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
814
ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &phys_ofs, &alloclen);
816
printk( "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
817
sizeof(rd)+rd.nsize, ret);
820
new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, phys_ofs, ALLOC_GC);
822
if (IS_ERR(new_fd)) {
823
printk( "jffs2_write_dirent in garbage_collect_dirent failed: %lu\n", PTR_ERR(new_fd));
824
return PTR_ERR(new_fd);
826
jffs2_add_fd_to_list(c, new_fd, &f->dents);
830
static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
831
struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
833
struct jffs2_full_dirent **fdp = &f->dents;
841
if (!jffs2_can_mark_obsolete(c)) {
842
struct jffs2_raw_dirent *rd;
843
struct jffs2_raw_node_ref *raw;
846
int name_len = strlen((const char *) fd->name);
847
uint32_t name_crc = crc32(0, fd->name, name_len);
848
uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
850
rd = kmalloc(rawlen, GFP_KERNEL);
857
down(&c->erase_free_sem);
859
for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
862
if (!(ref_obsolete(raw)))
866
if (ref_totlen(c, NULL, raw) != rawlen)
871
if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
874
D1(printk( "Check potential deletion dirent at %08x\n", ref_offset(raw)));
878
ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (unsigned char *)rd);
880
printk( "jffs2_g_c_deletion_dirent(): Read error (%d) reading obsolete node at %08x\n", ret, ref_offset(raw));
884
if (retlen != rawlen) {
885
printk( "jffs2_g_c_deletion_dirent(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
886
retlen, rawlen, ref_offset(raw));
890
if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
894
if (je32_to_cpu(rd->name_crc) != name_crc)
898
if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
902
if (memcmp(rd->name, fd->name, name_len))
908
up(&c->erase_free_sem);
910
D1(printk( "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
911
ref_offset(fd->raw), fd->name, ref_offset(raw), je32_to_cpu(rd->ino)));
914
return jffs2_garbage_collect_dirent(c, jeb, f, fd);
917
up(&c->erase_free_sem);
931
printk( "Deletion dirent \"%s\" not found in list for ino #%u\n", fd->name, f->inocache->ino);
933
jffs2_mark_node_obsolete(c, fd->raw);
934
jffs2_free_full_dirent(fd);
938
static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
939
struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
940
uint32_t start, uint32_t end)
942
struct jffs2_raw_inode ri;
943
struct jffs2_node_frag *frag;
944
struct jffs2_full_dnode *new_fn;
945
uint32_t alloclen, phys_ofs, ilen;
948
D1(printk( "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
949
f->inocache->ino, start, end));
951
memset(&ri, 0, sizeof(ri));
958
ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (unsigned char *)&ri);
959
if (readlen != sizeof(ri) || ret) {
960
printk( "Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n", ret, readlen);
963
if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
964
printk( "jffs2_garbage_collect_hole: Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
966
je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
969
if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
970
printk( "jffs2_garbage_collect_hole: Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
972
je32_to_cpu(ri.totlen), sizeof(ri));
975
crc = crc32(0, &ri, sizeof(ri)-8);
976
if (crc != je32_to_cpu(ri.node_crc)) {
977
printk( "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
979
je32_to_cpu(ri.node_crc), crc);
981
printk( "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
982
start, end, f->inocache->ino);
985
if (ri.compr != JFFS2_COMPR_ZERO) {
986
printk( "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!\n", ref_offset(fn->raw));
987
printk( "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
988
start, end, f->inocache->ino);
993
ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
994
ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
995
ri.totlen = cpu_to_je32(sizeof(ri));
996
ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
998
ri.ino = cpu_to_je32(f->inocache->ino);
999
ri.version = cpu_to_je32(++f->highest_version);
1000
ri.offset = cpu_to_je32(start);
1001
ri.dsize = cpu_to_je32(end - start);
1002
ri.csize = cpu_to_je32(0);
1003
ri.compr = JFFS2_COMPR_ZERO;
1006
frag = frag_last(&f->fragtree);
1010
ilen = frag->ofs + frag->size;
1012
ilen = JFFS2_F_I_SIZE(f);
1014
ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1015
ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1016
ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1017
ri.isize = cpu_to_je32(ilen);
1018
ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1019
ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1020
ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1021
ri.data_crc = cpu_to_je32(0);
1022
ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1024
ret = jffs2_reserve_space_gc(c, sizeof(ri), &phys_ofs, &alloclen);
1026
printk( "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1030
new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_GC);
1032
if (IS_ERR(new_fn)) {
1033
printk( "Error writing new hole node: %lu\n", PTR_ERR(new_fn));
1034
return PTR_ERR(new_fn);
1036
if (je32_to_cpu(ri.version) == f->highest_version) {
1037
jffs2_add_full_dnode_to_inode(c, f, new_fn);
1039
jffs2_mark_node_obsolete(c, f->metadata->raw);
1040
jffs2_free_full_dnode(f->metadata);
1052
D1(if(unlikely(fn->frags <= 1)) {
1053
printk( "jffs2_garbage_collect_hole: Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1054
fn->frags, je32_to_cpu(ri.version), f->highest_version,
1055
je32_to_cpu(ri.ino));
1059
mark_ref_normal(new_fn->raw);
1061
for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
1062
frag; frag = frag_next(frag)) {
1063
if (frag->ofs > fn->size + fn->ofs)
1065
if (frag->node == fn) {
1066
frag->node = new_fn;
1072
printk( "jffs2_garbage_collect_hole: Old node still has frags!\n");
1075
if (!new_fn->frags) {
1076
printk( "jffs2_garbage_collect_hole: New node has no frags!\n");
1080
jffs2_mark_node_obsolete(c, fn->raw);
1081
jffs2_free_full_dnode(fn);
1086
static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1087
struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1088
uint32_t start, uint32_t end)
1090
struct jffs2_full_dnode *new_fn;
1091
struct jffs2_raw_inode ri;
1092
uint32_t alloclen, phys_ofs, offset, orig_end, orig_start;
1094
unsigned char *comprbuf = NULL, *writebuf;
1096
unsigned char *pg_ptr;
1098
memset(&ri, 0, sizeof(ri));
1100
D1(printk( "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
1101
f->inocache->ino, start, end));
1106
if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
1114
struct jffs2_node_frag *frag;
1117
min = start & ~(PAGE_CACHE_SIZE-1);
1118
max = min + PAGE_CACHE_SIZE;
1120
frag = jffs2_lookup_node_frag(&f->fragtree, start);
1124
BUG_ON(frag->ofs != start);
1127
while((frag = frag_prev(frag)) && frag->ofs >= min) {
1131
if (frag->ofs > min) {
1132
D1(printk( "Expanding down to cover partial frag (0x%x-0x%x)\n",
1133
frag->ofs, frag->ofs+frag->size));
1138
if (!frag->node || !frag->node->raw) {
1139
D1(printk( "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
1140
frag->ofs, frag->ofs+frag->size));
1148
struct jffs2_raw_node_ref *raw = frag->node->raw;
1149
struct jffs2_eraseblock *jeb;
1151
jeb = &c->blocks[raw->flash_offset / c->sector_size];
1153
if (jeb == c->gcblock) {
1154
D1(printk( "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1155
frag->ofs, frag->ofs+frag->size, ref_offset(raw)));
1159
if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
1160
D1(printk( "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
1161
frag->ofs, frag->ofs+frag->size, jeb->offset));
1165
D1(printk( "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
1166
frag->ofs, frag->ofs+frag->size, jeb->offset));
1175
frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
1177
while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
1181
if (frag->ofs+frag->size < max) {
1182
D1(printk( "Expanding up to cover partial frag (0x%x-0x%x)\n",
1183
frag->ofs, frag->ofs+frag->size));
1184
end = frag->ofs + frag->size;
1188
if (!frag->node || !frag->node->raw) {
1189
D1(printk( "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
1190
frag->ofs, frag->ofs+frag->size));
1198
struct jffs2_raw_node_ref *raw = frag->node->raw;
1199
struct jffs2_eraseblock *jeb;
1201
jeb = &c->blocks[raw->flash_offset / c->sector_size];
1203
if (jeb == c->gcblock) {
1204
D1(printk( "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1205
frag->ofs, frag->ofs+frag->size, ref_offset(raw)));
1206
end = frag->ofs + frag->size;
1209
if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
1210
D1(printk( "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
1211
frag->ofs, frag->ofs+frag->size, jeb->offset));
1215
D1(printk( "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
1216
frag->ofs, frag->ofs+frag->size, jeb->offset));
1217
end = frag->ofs + frag->size;
1221
D1(printk( "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
1222
orig_start, orig_end, start, end));
1224
D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
1225
BUG_ON(end < orig_end);
1226
BUG_ON(start > orig_start);
1236
pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
1238
if (IS_ERR(pg_ptr)) {
1239
printk( "read_cache_page() returned error: %lu\n", PTR_ERR(pg_ptr));
1240
return PTR_ERR(pg_ptr);
1244
while(offset < orig_end) {
1247
uint16_t comprtype = JFFS2_COMPR_NONE;
1249
ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen);
1252
printk( "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
1253
sizeof(ri)+ JFFS2_MIN_DATA_LEN, ret);
1256
cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
1257
datalen = end - offset;
1259
writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
1261
comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
1263
ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1264
ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1265
ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
1266
ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1268
ri.ino = cpu_to_je32(f->inocache->ino);
1269
ri.version = cpu_to_je32(++f->highest_version);
1270
ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1271
ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1272
ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1273
ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
1274
ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1275
ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1276
ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1277
ri.offset = cpu_to_je32(offset);
1278
ri.csize = cpu_to_je32(cdatalen);
1279
ri.dsize = cpu_to_je32(datalen);
1280
ri.compr = comprtype & 0xff;
1281
ri.usercompr = (comprtype >> 8) & 0xff;
1282
ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1283
ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
1285
new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, phys_ofs, ALLOC_GC);
1287
jffs2_free_comprbuf(comprbuf, writebuf);
1289
if (IS_ERR(new_fn)) {
1290
printk( "Error writing new dnode: %lu\n", PTR_ERR(new_fn));
1291
ret = PTR_ERR(new_fn);
1294
ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
1297
jffs2_mark_node_obsolete(c, f->metadata->raw);
1298
jffs2_free_full_dnode(f->metadata);
1303
jffs2_gc_release_page(c, pg_ptr, &pg);