12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
21 #include <linux/compiler.h>
34 int err = 0, pointed = 0;
51 if (adj >= tn->
csize) {
52 dbg_readinode(
"no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
61 dbg_readinode(
"check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
68 if (!err && retlen < len) {
69 JFFS2_WARNING(
"MTD point returned len too short: %zu instead of %u.\n", retlen, tn->
csize);
87 JFFS2_ERROR(
"can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
92 JFFS2_ERROR(
"short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
108 JFFS2_NOTICE(
"wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
163 ret = check_node_data(c, tn);
165 JFFS2_ERROR(
"check_node_data() returned error: %d.\n",
187 if (tn->
fn->ofs < offset)
188 next = tn->
rb.rb_right;
189 else if (tn->
fn->ofs >= offset)
190 next = tn->
rb.rb_left;
238 dbg_readinode(
"kill new mdata with ver %d (older than existing %d\n",
240 jffs2_kill_tn(c, tn);
250 this = jffs2_lookup_tn(&rii->
tn_root, tn->
fn->ofs);
266 dbg_readinode(
"'this' found %#04x-%#04x (%s)\n", this->
fn->ofs, this->fn->ofs + this->fn->size, this->fn ?
"data" :
"hole");
270 if (this->
fn->ofs > fn_end)
273 this->
version, this->
fn->ofs, this->fn->size);
278 if (!check_tn_node(c,
this)) {
281 jffs2_kill_tn(c, tn);
287 jffs2_kill_tn(c,
this);
292 if (this->version < tn->
version &&
293 this->
fn->ofs >= tn->
fn->ofs &&
294 this->fn->ofs + this->fn->size <= fn_end) {
296 if (check_tn_node(c, tn)) {
298 jffs2_kill_tn(c, tn);
302 while (
this && this->
fn->ofs + this->fn->size <= fn_end) {
304 if (this->version < tn->
version) {
307 this->version, this->
fn->ofs,
308 this->fn->ofs+this->fn->size);
309 jffs2_kill_tn(c,
this);
317 this->fn->ofs <= tn->
fn->ofs &&
318 this->fn->ofs+this->fn->size >= fn_end) {
320 if (!check_tn_node(c,
this)) {
322 jffs2_kill_tn(c, tn);
328 jffs2_kill_tn(c,
this);
345 if (tn->
fn->ofs > insert_point->
fn->ofs)
346 link = &insert_point->
rb.rb_right;
347 else if (tn->
fn->ofs < insert_point->
fn->ofs ||
348 tn->
fn->size < insert_point->
fn->size)
349 link = &insert_point->
rb.rb_left;
351 link = &insert_point->
rb.rb_right;
353 rb_link_node(&tn->
rb, &insert_point->
rb, link);
361 if (this->
fn->ofs + this->fn->size > tn->
fn->ofs) {
362 dbg_readinode(
"Node is overlapped by %p (v %d, 0x%x-0x%x)\n",
364 this->fn->ofs+this->fn->size);
386 while (
this && this->
fn->ofs < fn_end) {
390 this->fn->ofs+this->fn->size);
412 else if (node == parent->
rb_left)
439 dbg_readinode(
"Link new node at %p (root is %p)\n", link, ver_root);
440 rb_link_node(&tn->
rb, parent, link);
451 static int jffs2_build_inode_fragtree(
struct jffs2_sb_info *c,
464 #ifdef JFFS2_DBG_READINODE_MESSAGES
468 this->fn->ofs+this->fn->size, this->overlapped);
473 while ((last = pen)) {
477 ver_insert(&ver_root, last);
499 eat_last(&ver_root, &this->
rb);
500 if (check_tn_node(c,
this)) {
503 this->fn->ofs+this->fn->size);
504 jffs2_kill_tn(c,
this);
506 if (this->
version > high_ver) {
514 dbg_readinode(
"Add %p (v %d, 0x%x-0x%x, ov %d) to fragtree\n",
516 this->fn->ofs+this->fn->size, this->overlapped);
525 if (check_tn_node(c,
this))
532 eat_last(&ver_root, &vers_next->
rb);
544 static void jffs2_free_tmp_dnode_info_list(
struct rb_root *
list)
616 crc =
crc32(0, rd,
sizeof(*rd) - 8);
618 JFFS2_NOTICE(
"header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
631 JFFS2_ERROR(
"illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
671 if (read >
sizeof(*rd))
676 if (rd->
nsize +
sizeof(*rd) > read) {
679 int already = read -
sizeof(*rd);
682 rd->nsize - already, &read, &fd->
name[already]);
687 JFFS2_ERROR(
"read remainder of name: error %d\n", err);
695 fd->
name[rd->nsize] =
'\0';
725 crc =
crc32(0, rd,
sizeof(*rd) - 8);
727 JFFS2_NOTICE(
"node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
735 JFFS2_ERROR(
"failed to allocate tn (%zu bytes).\n",
sizeof(*tn));
793 buf = (
unsigned char *)rd +
sizeof(*rd);
803 JFFS2_NOTICE(
"wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
809 }
else if (csize == 0) {
852 tn->
fn->size = csize;
856 dbg_readinode2(
"dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
860 ret = jffs2_add_tn_to_tree(c, rii, tn);
868 #ifdef JFFS2_DBG_READINODE2_MESSAGES
892 JFFS2_ERROR(
"REF_UNCHECKED but unknown node at %#08x\n",
894 JFFS2_ERROR(
"Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
906 JFFS2_ERROR(
"unknown INCOMPAT nodetype %#04X at %#08x\n",
913 JFFS2_ERROR(
"unknown ROCOMPAT nodetype %#04X at %#08x\n",
919 JFFS2_NOTICE(
"unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
924 JFFS2_NOTICE(
"unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
941 int needed_len,
int *rdlen,
unsigned char *buf)
943 int err, to_read = needed_len - *rdlen;
962 "error code: %d.\n", to_read, offs, err);
966 if (retlen < to_read) {
967 JFFS2_ERROR(
"short read at %#08x: %zu instead of %d.\n",
968 offs, retlen, to_read);
985 unsigned char *buf =
NULL;
1002 valid_ref = jffs2_first_valid_node(f->
inocache->nodes);
1003 if (!valid_ref && f->
inocache->ino != 1)
1013 valid_ref = jffs2_first_valid_node(ref->
next_in_ino);
1061 JFFS2_NOTICE(
"Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
1072 JFFS2_NOTICE(
"Wrong magic bitmask 0x%04x in node header at %#08x.\n",
1089 err = read_direntry(c, ref, &node->
d, retlen, rii);
1104 err = read_dnode(c, ref, &node->
i, len, rii);
1118 err = read_unknown(c, ref, &node->
u);
1132 dbg_readinode(
"nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
1138 jffs2_free_tmp_dnode_info_list(&rii->
tn_root);
1139 jffs2_free_full_dirent_list(rii->
fds);
1145 static int jffs2_do_read_inode_internal(
struct jffs2_sb_info *c,
1157 memset(&rii, 0,
sizeof(rii));
1160 ret = jffs2_get_inode_nodes(c, f, &rii);
1163 JFFS2_ERROR(
"cannot read nodes for ino %u, returned error is %d\n", f->
inocache->ino, ret);
1169 ret = jffs2_build_inode_fragtree(c, f, &rii);
1171 JFFS2_ERROR(
"Failed to build final fragtree for inode #%u: error %d\n",
1175 jffs2_free_tmp_dnode_info_list(&rii.
tn_root);
1208 JFFS2_NOTICE(
"but it has children so we fake some modes for it\n");
1222 if (ret || retlen !=
sizeof(*latest_node)) {
1223 JFFS2_ERROR(
"failed to read from flash: error %d, %zd of %zd bytes read\n",
1224 ret, retlen,
sizeof(*latest_node));
1228 return ret?ret:-
EIO;
1231 crc =
crc32(0, latest_node,
sizeof(*latest_node)-8);
1233 JFFS2_ERROR(
"CRC failed for read_inode of inode %u at physical location 0x%x\n",
1254 JFFS2_WARNING(
"Truncating ino #%u to %d bytes failed because it only had %d bytes to start with!\n",
1280 JFFS2_ERROR(
"can't allocate %u bytes of memory for the symlink target path cache\n", csize);
1287 csize, &retlen, (
char *)f->
target);
1289 if (ret || retlen != csize) {
1290 if (retlen != csize)
1310 JFFS2_ERROR(
"Argh. Special inode #%u with mode 0%o had metadata node\n",
1317 JFFS2_ERROR(
"Argh. Special inode #%u with mode 0%o has no fragments\n",
1325 JFFS2_ERROR(
"Argh. Special inode #%u with mode 0x%x had more than one node\n",
1369 goto retry_inocache;
1376 JFFS2_ERROR(
"Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->
inocache->state);
1391 JFFS2_ERROR(
"cannot allocate inocache for root inode\n");
1402 JFFS2_ERROR(
"requestied to read an nonexistent ino %u\n", ino);
1406 return jffs2_do_read_inode_internal(c, f, latest_node);
1422 ret = jffs2_do_read_inode_internal(c, f, &n);