12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
20 #include <linux/compiler.h>
25 #define DEFAULT_EMPTY_SCAN_SIZE 256
27 #define noisy_printk(noise, fmt, ...) \
30 pr_notice(fmt, ##__VA_ARGS__); \
33 pr_notice("Further such events for this erase block will not be printed\n"); \
54 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
94 uint32_t empty_blocks = 0, bad_blocks = 0;
95 unsigned char *flashbuf =
NULL;
99 size_t pointlen, try_size;
102 (
void **)&flashbuf,
NULL);
103 if (!ret && pointlen < c->mtd->size) {
105 jffs2_dbg(1,
"MTD point returned len too short: 0x%zx\n",
111 jffs2_dbg(1,
"MTD point failed %d\n", ret);
121 jffs2_dbg(1,
"Trying to allocate readbuf of %zu "
122 "bytes\n", try_size);
128 jffs2_dbg(1,
"Allocated readbuf of %zu bytes\n",
151 ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->
offset),
182 jffs2_dbg(1,
"Adding all-dirty block at 0x%08x to erase_pending_list\n",
210 jffs2_dbg(1,
"%s(): new nextblock = 0x%08x\n",
214 ret = file_dirty(c, jeb);
223 jffs2_dbg(1,
"Erase block at 0x%08x is not formatted. It will be erased\n",
237 pr_warn(
"%s(): unknown block state\n", __func__);
249 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
257 jffs2_dbg(1,
"%s(): Skipping %d bytes in nextblock to ensure page alignment\n",
265 pr_notice(
"Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
266 pr_notice(
"empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",
295 jffs2_dbg(1,
"mtd->read(0x%x bytes from 0x%x) returned %d\n",
300 jffs2_dbg(1,
"Read at 0x%x gave only 0x%zx bytes\n",
326 #ifdef CONFIG_JFFS2_FS_XATTR
337 JFFS2_WARNING(
"node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
350 JFFS2_WARNING(
"node length mismatch at %#08x, read=%u, calc=%u\n",
378 dbg_xattr(
"scanning xdatum at %#08x (xid=%u, version=%u)\n",
391 crc =
crc32(0, rr,
sizeof(*rr) - 4);
393 JFFS2_WARNING(
"node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
401 JFFS2_WARNING(
"node length mismatch at %#08x, read=%u, calc=%zd\n",
409 ref = jffs2_alloc_xattr_ref();
425 if (ref->
xseqno > c->highest_xseqno)
427 ref->
next = c->xref_temp;
434 dbg_xattr(
"scan xref at %#08x (xid=%u, ino=%u)\n",
452 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
453 int cleanmarkerfound = 0;
457 prevofs = jeb->
offset - 1;
459 jffs2_dbg(1,
"%s(): Scanning block at 0x%x\n", __func__, ofs);
461 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
469 jffs2_dbg(2,
"jffs_check_nand_cleanmarker returned %d\n", ret);
475 case 0: cleanmarkerfound = 1;
break;
499 buf_len =
sizeof(*sm);
502 err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
508 sm = (
void *)buf + buf_size -
sizeof(*sm);
511 sumptr = buf + buf_size - sumlen;
514 if (sumlen > buf_size) {
519 memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
521 if (buf_len < sumlen) {
523 err = jffs2_fill_scan_buf(c, sumptr,
536 if (buf_size && sumlen > buf_size)
554 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
563 while(ofs < max_ofs && *(
uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
566 if (ofs == max_ofs) {
567 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
571 jffs2_dbg(2,
"jffs2_check_oob_empty returned %d\n",
580 jffs2_dbg(1,
"Block at 0x%08x is empty (erased)\n",
601 dbg_summary(
"no summary found in jeb 0x%08x. Apply original scan.\n",jeb->
offset);
616 pr_warn(
"Eep. ofs 0x%08x not word-aligned!\n", ofs);
620 if (ofs == prevofs) {
621 pr_warn(
"ofs 0x%08x has already been seen. Skipping\n",
631 jffs2_dbg(1,
"Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n",
640 if (buf_ofs + buf_len < ofs +
sizeof(*node)) {
642 jffs2_dbg(1,
"Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
645 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
653 if (*(
uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
661 jffs2_dbg(1,
"Found empty flash at 0x%08x\n", ofs);
663 inbuf_ofs = ofs - buf_ofs;
664 while (inbuf_ofs < scan_end) {
666 pr_warn(
"Empty flash at 0x%08x ends at 0x%08x\n",
677 jffs2_dbg(1,
"Empty flash to end of buffer at 0x%08x\n",
684 jffs2_dbg(1,
"%d bytes at start of block seems clean... assuming all clean\n",
688 if (!buf_size && (scan_end != buf_len)) {
698 jffs2_dbg(1,
"Empty flash at %08x runs to end of block. Treating as free_space\n",
704 jffs2_dbg(1,
"Reading another 0x%x at 0x%08x\n",
706 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
714 pr_warn(
"Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n",
722 jffs2_dbg(1,
"Dirty bitmask at 0x%08x\n", ofs);
729 pr_warn(
"Old JFFS2 bitmask found at 0x%08x\n", ofs);
730 pr_warn(
"You cannot use older JFFS2 filesystems with newer kernels\n");
738 noisy_printk(&noise,
"%s(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
748 crcnode.magic = node->
magic;
750 crcnode.totlen = node->
totlen;
751 hdr_crc =
crc32(0, &crcnode,
sizeof(crcnode)-4);
754 noisy_printk(&noise,
"%s(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
769 pr_warn(
"Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
771 pr_warn(
"Perhaps the file system was created with the wrong erase size?\n");
780 jffs2_dbg(2,
"Node at 0x%08x is obsolete. Skipping\n",
792 jffs2_dbg(1,
"Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
795 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
801 err = jffs2_scan_inode_node(c, jeb, (
void *)node, ofs, s);
809 jffs2_dbg(1,
"Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
812 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
818 err = jffs2_scan_dirent_node(c, jeb, (
void *)node, ofs, s);
823 #ifdef CONFIG_JFFS2_FS_XATTR
827 jffs2_dbg(1,
"Fewer than %d bytes (xattr node) left to end of buf. Reading 0x%x at 0x%08x\n",
830 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
836 err = jffs2_scan_xattr_node(c, jeb, (
void *)node, ofs, s);
844 jffs2_dbg(1,
"Fewer than %d bytes (xref node) left to end of buf. Reading 0x%x at 0x%08x\n",
847 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
853 err = jffs2_scan_xref_node(c, jeb, (
void *)node, ofs, s);
861 jffs2_dbg(1,
"CLEANMARKER node found at 0x%08x\n", ofs);
863 pr_notice(
"CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
870 pr_notice(
"CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n",
893 pr_notice(
"Read-only compatible feature node (0x%04x) found at offset 0x%08x\n",
904 pr_notice(
"Incompatible feature node (0x%04x) found at offset 0x%08x\n",
909 jffs2_dbg(1,
"Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
917 jffs2_dbg(1,
"Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n",
934 "summary information, disabling for this jeb!\n");
939 jffs2_dbg(1,
"Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
967 pr_notice(
"%s(): allocation of inode cache failed\n", __func__);
970 memset(ic, 0,
sizeof(*ic));
973 ic->
nodes = (
void *)ic;
986 jffs2_dbg(1,
"%s(): Node at 0x%08x\n", __func__, ofs);
998 crc =
crc32(0, ri,
sizeof(*ri)-8);
1000 pr_notice(
"%s(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1020 jffs2_dbg(1,
"Node is ino #%u, version %d. Range 0x%x-0x%x\n",
1043 jffs2_dbg(1,
"%s(): Node at 0x%08x\n", __func__, ofs);
1047 crc =
crc32(0, rd,
sizeof(*rd)-8);
1050 pr_notice(
"%s(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1062 if (checkedlen < rd->
nsize) {
1063 pr_err(
"Dirent at %08x has zeroes in name. Truncating to %d chars\n",
1071 fd->
name[checkedlen] = 0;
1075 pr_notice(
"%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1077 jffs2_dbg(1,
"Name for which CRC failed is (now) '%s', ino #%d\n",
1140 rotateby = pseudo_random %
x;
1146 rotateby = pseudo_random %
x;
1152 rotateby = pseudo_random %
x;
1158 rotateby = pseudo_random %
x;