13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/kernel.h>
17 #include <linux/slab.h>
20 #include <linux/compiler.h>
21 #include <linux/stat.h>
56 jffs2_dbg(1,
"Picking block from bad_used_list to GC next\n");
61 jffs2_dbg(1,
"Picking block from erasable_list to GC next\n");
65 jffs2_dbg(1,
"Picking block from very_dirty_list to GC next\n");
67 }
else if (n < 126 && !list_empty(&c->
dirty_list)) {
68 jffs2_dbg(1,
"Picking block from dirty_list to GC next\n");
71 jffs2_dbg(1,
"Picking block from clean_list to GC next\n");
74 jffs2_dbg(1,
"Picking block from dirty_list to GC next (clean_list was empty)\n");
78 jffs2_dbg(1,
"Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n");
81 jffs2_dbg(1,
"Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n");
86 jffs2_dbg(1,
"Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n");
93 jffs2_dbg(1,
"No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n");
102 pr_warn(
"Eep. ret->gc_node for block at 0x%08x is NULL\n",
109 jffs2_dbg(1,
"Converting wasted_size %08x to dirty_size\n",
147 pr_crit(
"Checked all inodes but still 0x%x bytes of unchecked space?\n",
170 jffs2_dbg(1,
"Skipping check of ino #%d with nlink/pino zero\n",
179 jffs2_dbg(1,
"Skipping ino #%u already checked\n",
186 pr_warn(
"Inode #%u is in state %d during CRC check phase!\n",
195 jffs2_dbg(1,
"Waiting for ino #%u to finish reading\n",
214 jffs2_dbg(1,
"%s(): triggering inode scan of ino#%u\n",
219 pr_warn(
"Returned error for crccheck of ino #%u. Expect badness...\n",
232 jffs2_dbg(1,
"%s(): erasing pending blocks\n", __func__);
236 jffs2_dbg(1,
"No progress from erasing block; doing GC anyway\n");
245 jeb = jffs2_find_gc_block(c);
254 jffs2_dbg(1,
"Couldn't find erase block to garbage collect!\n");
260 jffs2_dbg(1,
"GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n",
274 jffs2_dbg(1,
"Node at 0x%08x is obsolete... skipping\n",
278 pr_warn(
"eep. End of raw list while still supposedly nodes to GC\n");
279 pr_warn(
"erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
290 jffs2_dbg(1,
"Going to garbage collect node at 0x%08x\n",
298 jffs2_garbage_collect_pristine(c,
NULL, raw);
307 ic = jffs2_raw_ref_to_ic(raw);
309 #ifdef CONFIG_JFFS2_FS_XATTR
332 jffs2_dbg(1,
"%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n",
354 jffs2_dbg(1,
"Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
371 pr_crit(
"Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
385 jffs2_dbg(1,
"%s(): waiting for ino #%u in state %d\n",
409 ret = jffs2_garbage_collect_pristine(c, ic, raw);
444 ret = jffs2_garbage_collect_live(c, jeb, raw, f);
451 pr_err(
"Error garbage collecting node at %08x!\n",
464 jffs2_dbg(1,
"Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n",
495 jffs2_dbg(1,
"GC block is no longer gcblock. Restart\n");
500 jffs2_dbg(1,
"node to be GC'd was obsoleted in the meantime.\n");
509 ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
515 if (frag->
node && frag->
node->raw == raw) {
520 if (nrfrags == frag->
node->frags)
526 ret = jffs2_garbage_collect_pristine(c, f->
inocache, raw);
537 ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start,
end);
540 ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start,
end);
552 ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
554 ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
556 pr_warn(
"Raw node at 0x%08x wasn't in node lists for ino #%u\n",
559 pr_warn(
"But it's obsolete so we don't mind too much\n");
571 static int jffs2_garbage_collect_pristine(
struct jffs2_sb_info *c,
582 jffs2_dbg(1,
"Going to GC REF_PRISTINE node at 0x%08x\n",
599 if (alloclen < rawlen) {
609 if (!ret && retlen != rawlen)
616 pr_warn(
"Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
623 crc =
crc32(0, node,
sizeof(node->
i)-8);
625 pr_warn(
"Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
634 pr_warn(
"Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
643 crc =
crc32(0, node,
sizeof(node->
d)-8);
645 pr_warn(
"Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
651 if (
strnlen(node->
d.name, node->
d.nsize) != node->
d.nsize) {
652 pr_warn(
"Name in dirent node at 0x%08x contains zeroes\n",
658 crc =
crc32(0, node->
d.name, node->
d.nsize);
660 pr_warn(
"Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
670 pr_warn(
"Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
682 if (ret || (retlen != rawlen)) {
683 pr_notice(
"Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
684 rawlen, phys_ofs, ret, retlen);
688 pr_notice(
"Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
698 jffs2_dbg(1,
"Retrying failed write of REF_PRISTINE node.\n");
708 jffs2_dbg(1,
"Allocated space at 0x%08x to retry failed write.\n",
716 jffs2_dbg(1,
"Failed to allocate space to retry failed write: %d!\n",
727 jffs2_dbg(1,
"WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n",
754 mdata = (
char *)&
dev;
755 jffs2_dbg(1,
"%s(): Writing %d bytes of kdev_t\n",
761 pr_warn(
"kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
766 pr_warn(
"read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
771 jffs2_dbg(1,
"%s(): Writing %d bites of symlink target\n",
779 pr_warn(
"jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
780 sizeof(ri) + mdatalen, ret);
784 last_frag = frag_last(&f->
fragtree);
788 ilen = last_frag->
ofs + last_frag->
size;
792 memset(&ri, 0,
sizeof(ri));
816 if (IS_ERR(new_fn)) {
817 pr_warn(
"Error writing new dnode: %ld\n", PTR_ERR(new_fn));
818 ret = PTR_ERR(new_fn);
860 pr_warn(
"jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
861 sizeof(
rd)+
rd.nsize, ret);
866 if (IS_ERR(new_fd)) {
867 pr_warn(
"jffs2_write_dirent in garbage_collect_dirent failed: %ld\n",
869 return PTR_ERR(new_fd);
921 jffs2_dbg(1,
"Check potential deletion dirent at %08x\n",
928 pr_warn(
"%s(): Read error (%d) reading obsolete node at %08x\n",
933 if (retlen != rawlen) {
934 pr_warn(
"%s(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
935 __func__, retlen, rawlen,
960 jffs2_dbg(1,
"Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
965 return jffs2_garbage_collect_dirent(c, jeb, f, fd);
985 pr_warn(
"Deletion dirent \"%s\" not found in list for ino #%u\n",
1003 jffs2_dbg(1,
"Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
1006 memset(&ri, 0,
sizeof(ri));
1014 if (readlen !=
sizeof(ri) || ret) {
1015 pr_warn(
"Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n",
1020 pr_warn(
"%s(): Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
1026 pr_warn(
"%s(): Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
1031 crc =
crc32(0, &ri,
sizeof(ri)-8);
1033 pr_warn(
"%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
1037 pr_warn(
"Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1042 pr_warn(
"%s(): Node 0x%08x wasn't a hole node!\n",
1044 pr_warn(
"Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1067 ilen = frag->
ofs + frag->
size;
1084 pr_warn(
"jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1090 if (IS_ERR(new_fn)) {
1091 pr_warn(
"Error writing new hole node: %ld\n", PTR_ERR(new_fn));
1092 return PTR_ERR(new_fn);
1111 pr_warn(
"%s(): Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1123 if (frag->
node == fn) {
1124 frag->
node = new_fn;
1130 pr_warn(
"%s(): Old node still has frags!\n", __func__);
1133 if (!new_fn->
frags) {
1134 pr_warn(
"%s(): New node has no frags!\n", __func__);
1152 unsigned char *comprbuf =
NULL, *writebuf;
1154 unsigned char *pg_ptr;
1156 memset(&ri, 0,
sizeof(ri));
1158 jffs2_dbg(1,
"Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
1189 if (frag->
ofs > min) {
1190 jffs2_dbg(1,
"Expanding down to cover partial frag (0x%x-0x%x)\n",
1196 if (!frag->
node || !frag->
node->raw) {
1197 jffs2_dbg(1,
"First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
1212 jffs2_dbg(1,
"Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1220 jffs2_dbg(1,
"Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
1227 jffs2_dbg(1,
"Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
1245 if (frag->
ofs+frag->
size < max) {
1246 jffs2_dbg(1,
"Expanding up to cover partial frag (0x%x-0x%x)\n",
1252 if (!frag->
node || !frag->
node->raw) {
1253 jffs2_dbg(1,
"Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
1268 jffs2_dbg(1,
"Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1276 jffs2_dbg(1,
"Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
1283 jffs2_dbg(1,
"Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
1291 jffs2_dbg(1,
"Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
1292 orig_start, orig_end, start, end);
1296 BUG_ON(start > orig_start);
1308 if (IS_ERR(pg_ptr)) {
1309 pr_warn(
"read_cache_page() returned error: %ld\n",
1311 return PTR_ERR(pg_ptr);
1315 while(offset < orig_end) {
1324 pr_warn(
"jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
1328 cdatalen =
min_t(
uint32_t, alloclen -
sizeof(ri), end - offset);
1331 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
1333 comprtype =
jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
1352 ri.compr = comprtype & 0xff;
1353 ri.usercompr = (comprtype >> 8) & 0xff;
1361 if (IS_ERR(new_fn)) {
1362 pr_warn(
"Error writing new dnode: %ld\n",
1364 ret = PTR_ERR(new_fn);