14
#include <linux/kernel.h>
15
#include <linux/sched.h>
16
#include <linux/slab.h>
18
#include <linux/crc32.h>
19
#include <linux/pagemap.h>
20
#include <linux/mtd/mtd.h>
21
#include <linux/compiler.h>
24
void jffs2_truncate_fragtree (struct jffs2_sb_info *c,
25
struct rb_root *list, uint32_t size) {
26
struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
28
JFFS2_DBG_FRAGTREE("truncating fragtree to 0x%08x bytes\n", size);
31
if (frag && frag->ofs != size) {
32
if (frag->ofs+frag->size >= size) {
33
JFFS2_DBG_FRAGTREE2("truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
34
frag->size = size - frag->ofs;
36
frag = frag_next(frag);
38
while (frag && frag->ofs >= size) {
39
struct jffs2_node_frag *next = frag_next(frag);
41
JFFS2_DBG_FRAGTREE("removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
42
frag_erase(frag, list);
43
jffs2_obsolete_node_frag(c, frag);
52
static void jffs2_add_tn_to_tree(struct jffs2_tmp_dnode_info *tn, struct rb_root *list)
54
struct rb_node **p = &list->rb_node;
55
struct rb_node * parent = NULL;
56
struct jffs2_tmp_dnode_info *this;
60
this = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
66
if (tn->version < this->version) {
73
rb_link_node(&tn->rb, parent, p);
74
rb_insert_color(&tn->rb, list);
77
static void jffs2_free_tmp_dnode_info_list(struct rb_root *list) {
79
struct jffs2_tmp_dnode_info *tn;
87
} else if (this->rb_right) {
88
this = this->rb_right;
90
tn = rb_entry(this, struct jffs2_tmp_dnode_info, rb);
91
jffs2_free_full_dnode(tn->fn);
92
jffs2_free_tmp_dnode_info(tn);
94
this = this->rb_parent;
99
if (this->rb_left == &tn->rb) {
100
this->rb_left = NULL;
101
} else if (this->rb_right == &tn->rb) {
102
this->rb_right = NULL;
108
list->rb_node = NULL;
111
static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd) {
112
struct jffs2_full_dirent *next;
116
jffs2_free_full_dirent(fd);
122
static struct jffs2_raw_node_ref
123
*jffs2_first_valid_node(struct jffs2_raw_node_ref *ref) {
124
while (ref && ref->next_in_ino) {
125
if (!ref_obsolete(ref)) {
128
JFFS2_DBG_NODEREF("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
129
ref = ref->next_in_ino;
142
static inline int read_direntry(struct jffs2_sb_info *c,
143
struct jffs2_raw_node_ref *ref, struct jffs2_raw_dirent *rd,
144
uint32_t read, struct jffs2_full_dirent **fdp,
145
uint32_t *latest_mctime, uint32_t *mctime_ver) {
146
struct jffs2_full_dirent *fd;
149
BUG_ON(ref_flags(ref) == REF_UNCHECKED);
151
BUG_ON(ref_obsolete(ref));
154
if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
155
JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
156
ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
160
fd = jffs2_alloc_full_dirent(rd->nsize + 1);
166
fd->version = je32_to_cpu(rd->version);
167
fd->ino = je32_to_cpu(rd->ino);
171
if(fd->version > *mctime_ver) {
172
*mctime_ver = fd->version;
173
*latest_mctime = je32_to_cpu(rd->mctime);
180
if (read > sizeof(*rd)) {
181
memcpy(&fd->name[0], &rd->name[0],
182
min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
186
if (rd->nsize + sizeof(*rd) > read) {
189
int already = read - sizeof(*rd);
191
err = jffs2_flash_read(c, (ref_offset(ref)) + read,
192
rd->nsize - already, &read, &fd->name[already]);
193
if (unlikely(read != rd->nsize - already) && likely(!err)) {
197
JFFS2_ERROR("read remainder of name: error %d\n", err);
198
jffs2_free_full_dirent(fd);
203
fd->nhash = full_name_hash(fd->name, rd->nsize);
205
fd->name[rd->nsize] = '\0';
211
jffs2_add_fd_to_list(c, fd, fdp);
224
static inline int read_dnode(struct jffs2_sb_info *c,
225
struct jffs2_raw_node_ref *ref, struct jffs2_raw_inode *rd,
226
uint32_t read, struct rb_root *tnp, uint32_t *latest_mctime,
227
uint32_t *mctime_ver) {
228
struct jffs2_eraseblock *jeb;
229
struct jffs2_tmp_dnode_info *tn;
232
BUG_ON(ref_obsolete(ref));
235
if (ref_flags(ref) == REF_UNCHECKED) {
238
crc = crc32(0, rd, sizeof(*rd) - 8);
239
if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
240
JFFS2_NOTICE("header CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
241
ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
246
if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
247
unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
248
JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
249
jffs2_dbg_dump_node(c, ref_offset(ref));
253
if (rd->compr != JFFS2_COMPR_ZERO && je32_to_cpu(rd->csize)) {
254
unsigned char *buf = NULL;
255
uint32_t pointed = 0;
258
buf = kmalloc(je32_to_cpu(rd->csize), GFP_KERNEL);
263
err = jffs2_flash_read(c, ref_offset(ref) + sizeof(*rd), je32_to_cpu(rd->csize),
265
if (unlikely(read != je32_to_cpu(rd->csize)) && likely(!err)) {
273
crc = crc32(0, buf, je32_to_cpu(rd->csize));
278
if (crc != je32_to_cpu(rd->data_crc)) {
279
JFFS2_NOTICE("data CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
280
ref_offset(ref), je32_to_cpu(rd->data_crc), crc);
287
jeb = &c->blocks[ref->flash_offset / c->sector_size];
288
len = ref_totlen(c, jeb, ref);
290
spin_lock(&c->erase_completion_lock);
291
jeb->used_size += len;
292
jeb->unchecked_size -= len;
294
c->unchecked_size -= len;
303
if ((je32_to_cpu(rd->dsize) >= PAGE_CACHE_SIZE) ||
304
( ((je32_to_cpu(rd->offset) & (PAGE_CACHE_SIZE-1)) == 0) &&
305
(je32_to_cpu(rd->dsize) + je32_to_cpu(rd->offset) ==
306
je32_to_cpu(rd->isize)))) {
307
JFFS2_DBG_READINODE("marking node at %#08x REF_PRISTINE\n", ref_offset(ref));
308
ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
310
JFFS2_DBG_READINODE("marking node at %#08x REF_NORMAL\n", ref_offset(ref));
311
ref->flash_offset = ref_offset(ref) | REF_NORMAL;
313
spin_unlock(&c->erase_completion_lock);
316
tn = jffs2_alloc_tmp_dnode_info();
318
JFFS2_ERROR("alloc tn failed\n");
322
tn->fn = jffs2_alloc_full_dnode();
324
JFFS2_ERROR("alloc fn failed\n");
325
jffs2_free_tmp_dnode_info(tn);
329
tn->version = je32_to_cpu(rd->version);
330
tn->fn->ofs = je32_to_cpu(rd->offset);
336
if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize)
337
&& je32_to_cpu(rd->csize)) {
338
tn->fn->size = je32_to_cpu(rd->csize);
340
tn->fn->size = je32_to_cpu(rd->dsize);
343
JFFS2_DBG_READINODE("dnode @%08x: ver %u, offset %#04x, dsize %#04x\n",
344
ref_offset(ref), je32_to_cpu(rd->version), je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize));
346
jffs2_add_tn_to_tree(tn, tnp);
359
static inline int read_unknown(struct jffs2_sb_info *c,
360
struct jffs2_raw_node_ref *ref,
361
struct jffs2_unknown_node *un, uint32_t read) {
363
BUG_ON(ref_flags(ref) == REF_UNCHECKED);
365
un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE |
366
je16_to_cpu(un->nodetype));
368
if (crc32(0, un, sizeof(struct jffs2_unknown_node) - 4) !=
369
je32_to_cpu(un->hdr_crc)) {
371
JFFS2_NOTICE("node header CRC failed at %#08x. But it must have been OK earlier.\n", ref_offset(ref));
372
jffs2_dbg_dump_node(c, ref_offset(ref));
375
switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
377
case JFFS2_FEATURE_INCOMPAT:
378
JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
379
je16_to_cpu(un->nodetype), ref_offset(ref));
384
case JFFS2_FEATURE_ROCOMPAT:
385
JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
386
je16_to_cpu(un->nodetype), ref_offset(ref));
387
BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
390
case JFFS2_FEATURE_RWCOMPAT_COPY:
391
JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
392
je16_to_cpu(un->nodetype), ref_offset(ref));
395
case JFFS2_FEATURE_RWCOMPAT_DELETE:
396
JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
397
je16_to_cpu(un->nodetype), ref_offset(ref));
409
static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
410
struct rb_root *tnp, struct jffs2_full_dirent **fdp,
411
uint32_t *highest_version, uint32_t *latest_mctime,
412
uint32_t *mctime_ver) {
413
struct jffs2_raw_node_ref *ref, *valid_ref;
414
struct rb_root ret_tn = RB_ROOT;
415
struct jffs2_full_dirent *ret_fd = NULL;
416
union jffs2_node_union node;
422
JFFS2_DBG_READINODE("ino #%u\n", f->inocache->ino);
424
spin_lock(&c->erase_completion_lock);
426
valid_ref = jffs2_first_valid_node(f->inocache->nodes);
428
if (!valid_ref && (f->inocache->ino != 1)) {
429
JFFS2_WARNING("no valid nodes for ino #%u\n", f->inocache->ino);
440
valid_ref = jffs2_first_valid_node(ref->next_in_ino);
441
spin_unlock(&c->erase_completion_lock);
446
err = jffs2_flash_read(c, (ref_offset(ref)),
447
min_t(uint32_t, ref_totlen(c, NULL, ref), sizeof(node)),
448
&retlen, (void *)&node);
450
JFFS2_ERROR("error %d reading node at 0x%08x in get_inode_nodes()\n", err, ref_offset(ref));
454
switch (je16_to_cpu(node.u.nodetype)) {
456
case JFFS2_NODETYPE_DIRENT:
457
JFFS2_DBG_READINODE("node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref));
459
if (retlen < sizeof(node.d)) {
460
JFFS2_ERROR("short read dirent at %#08x\n", ref_offset(ref));
465
err = read_direntry(c, ref, &node.d, retlen, &ret_fd,
466
latest_mctime, mctime_ver);
468
jffs2_mark_node_obsolete(c, ref);
470
} else if (unlikely(err)) {
474
if (je32_to_cpu(node.d.version) > *highest_version) {
475
*highest_version = je32_to_cpu(node.d.version);
480
case JFFS2_NODETYPE_INODE:
481
JFFS2_DBG_READINODE("node at %08x (%d) is a data node\n",
482
ref_offset(ref), ref_flags(ref));
484
if (retlen < sizeof(node.i)) {
485
JFFS2_ERROR("short read dnode at %#08x\n", ref_offset(ref));
490
err = read_dnode(c, ref, &node.i, retlen, &ret_tn,
491
latest_mctime, mctime_ver);
493
jffs2_mark_node_obsolete(c, ref);
495
} else if (unlikely(err)) {
499
if (je32_to_cpu(node.i.version) > *highest_version) {
500
*highest_version = je32_to_cpu(node.i.version);
503
JFFS2_DBG_READINODE("version %d, highest_version now %d\n",
504
je32_to_cpu(node.i.version), *highest_version);
510
if (retlen < sizeof(struct jffs2_unknown_node)) {
511
JFFS2_ERROR("short read unknown node at %#08x\n", ref_offset(ref));
515
err = read_unknown(c, ref, &node.u, retlen);
517
jffs2_mark_node_obsolete(c, ref);
519
} else if (unlikely(err)) {
524
spin_lock(&c->erase_completion_lock);
527
spin_unlock(&c->erase_completion_lock);
534
jffs2_free_tmp_dnode_info_list(&ret_tn);
535
jffs2_free_full_dirent_list(ret_fd);
539
static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
540
struct jffs2_inode_info *f,
541
struct jffs2_raw_inode *latest_node) {
542
struct jffs2_tmp_dnode_info *tn = NULL;
543
struct rb_root tn_list;
544
struct rb_node *rb, *repl_rb;
545
struct jffs2_full_dirent *fd_list = NULL;
546
struct jffs2_full_dnode *fn = NULL;
548
uint32_t latest_mctime = 0, mctime_ver;
549
uint32_t mdata_ver = 0;
553
JFFS2_DBG_READINODE("ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink);
556
ret = jffs2_get_inode_nodes(c, f, &tn_list,
557
&fd_list, &f->highest_version, &latest_mctime, &mctime_ver);
560
JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
561
if (f->inocache->state == INO_STATE_READING) {
562
jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
568
rb = rb_first(&tn_list);
571
tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb);
575
if (likely(tn->version >= mdata_ver)) {
576
JFFS2_DBG_READINODE("obsoleting old metadata at 0x%08x\n", ref_offset(f->metadata->raw));
577
jffs2_mark_node_obsolete(c, f->metadata->raw);
578
jffs2_free_full_dnode(f->metadata);
584
JFFS2_ERROR("Er. New metadata at 0x%08x with ver %d is actually older than previous ver %d at 0x%08x\n",
585
ref_offset(fn->raw), tn->version, mdata_ver, ref_offset(f->metadata->raw));
586
jffs2_mark_node_obsolete(c, fn->raw);
587
jffs2_free_full_dnode(fn);
595
jffs2_add_full_dnode_to_inode(c, f, fn);
598
JFFS2_DBG_READINODE("metadata @%08x: ver %d\n", ref_offset(fn->raw), tn->version);
600
mdata_ver = tn->version;
604
if (rb->rb_parent && rb->rb_parent->rb_left == rb) {
607
repl_rb = rb->rb_right;
609
repl_rb->rb_parent = rb->rb_parent;
619
if (tn->rb.rb_parent) {
620
if (tn->rb.rb_parent->rb_left == &tn->rb) {
621
tn->rb.rb_parent->rb_left = repl_rb;
622
} else if (tn->rb.rb_parent->rb_right == &tn->rb) {
623
tn->rb.rb_parent->rb_right = repl_rb;
627
} else if (tn->rb.rb_right) {
628
tn->rb.rb_right->rb_parent = NULL;
631
jffs2_free_tmp_dnode_info(tn);
633
jffs2_dbg_fragtree_paranoia_check_nolock(f);
637
if (f->inocache->ino != 1) {
638
JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
640
if (f->inocache->state == INO_STATE_READING) {
641
jffs2_set_inocache_state(c,
642
f->inocache, INO_STATE_CHECKEDABSENT);
646
JFFS2_NOTICE("but it has children so we fake some modes for it\n");
648
latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
649
latest_node->version = cpu_to_je32(0);
650
latest_node->atime = latest_node->ctime =
651
latest_node->mtime = cpu_to_je32(0);
652
latest_node->isize = cpu_to_je32(0);
653
latest_node->gid = cpu_to_je16(0);
654
latest_node->uid = cpu_to_je16(0);
655
if (f->inocache->state == INO_STATE_READING) {
656
jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
661
ret = jffs2_flash_read(c, ref_offset(fn->raw),
662
sizeof(*latest_node), &retlen, (void *)latest_node);
663
if (ret || retlen != sizeof(*latest_node)) {
664
JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
665
ret, retlen, sizeof(*latest_node));
668
jffs2_do_clear_inode(c, f);
672
crc = crc32(0, latest_node, sizeof(*latest_node)-8);
673
if (crc != je32_to_cpu(latest_node->node_crc)) {
674
JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
675
f->inocache->ino, ref_offset(fn->raw));
677
jffs2_do_clear_inode(c, f);
681
switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
683
if (mctime_ver > je32_to_cpu(latest_node->version)) {
687
latest_node->ctime = latest_node->mtime = cpu_to_je32(latest_mctime);
694
jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
703
if (!je32_to_cpu(latest_node->isize)) {
704
latest_node->isize = latest_node->dsize;
707
if (f->inocache->state != INO_STATE_CHECKING) {
711
f->target = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
713
JFFS2_ERROR("can't allocate %d bytes of memory for the symlink target path cache\n", je32_to_cpu(latest_node->csize));
715
jffs2_do_clear_inode(c, f);
719
ret = jffs2_flash_read(c, ref_offset(fn->raw) +
720
sizeof(*latest_node), je32_to_cpu(latest_node->csize),
721
&retlen, (unsigned char *)f->target);
723
if (ret || retlen != je32_to_cpu(latest_node->csize)) {
724
if (retlen != je32_to_cpu(latest_node->csize)) {
730
jffs2_do_clear_inode(c, f);
734
f->target[je32_to_cpu(latest_node->csize)] = '\0';
735
JFFS2_DBG_READINODE("symlink's target '%s' cached\n", f->target);
745
JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
746
f->inocache->ino, jemode_to_cpu(latest_node->mode));
748
jffs2_do_clear_inode(c, f);
751
if (!frag_first(&f->fragtree)) {
752
JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
753
f->inocache->ino, jemode_to_cpu(latest_node->mode));
755
jffs2_do_clear_inode(c, f);
759
if (frag_next(frag_first(&f->fragtree))) {
760
JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
761
f->inocache->ino, jemode_to_cpu(latest_node->mode));
764
jffs2_do_clear_inode(c, f);
768
f->metadata = frag_first(&f->fragtree)->node;
769
jffs2_free_node_frag(frag_first(&f->fragtree));
770
f->fragtree = RB_ROOT;
773
if (f->inocache->state == INO_STATE_READING) {
774
jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
781
int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
782
uint32_t ino, struct jffs2_raw_inode *latest_node) {
783
JFFS2_DBG_READINODE("read inode #%u\n", ino);
786
spin_lock(&c->inocache_lock);
787
f->inocache = jffs2_get_ino_cache(c, ino);
791
switch(f->inocache->state) {
792
case INO_STATE_UNCHECKED:
793
case INO_STATE_CHECKEDABSENT:
794
f->inocache->state = INO_STATE_READING;
797
case INO_STATE_CHECKING:
803
JFFS2_DBG_READINODE("waiting for ino #%u in state %d\n", ino, f->inocache->state);
804
sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
807
case INO_STATE_READING:
808
case INO_STATE_PRESENT:
812
JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
822
spin_unlock(&c->inocache_lock);
824
if (!f->inocache && ino == 1) {
826
f->inocache = jffs2_alloc_inode_cache();
828
JFFS2_ERROR("cannot allocate inocache for root inode\n");
831
JFFS2_DBG_READINODE("creating inocache for root inode\n");
832
memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
833
f->inocache->ino = f->inocache->nlink = 1;
834
f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
835
f->inocache->state = INO_STATE_READING;
836
jffs2_add_ino_cache(c, f->inocache);
839
JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
843
return jffs2_do_read_inode_internal(c, f, latest_node);
846
int jffs2_do_crccheck_inode(struct jffs2_sb_info *c,
847
struct jffs2_inode_cache *ic) {
848
struct jffs2_raw_inode n;
849
struct jffs2_inode_info *f = kmalloc(sizeof(*f), GFP_KERNEL);
856
memset(f, 0, sizeof(*f));
857
init_MUTEX_LOCKED(&f->sem);
860
ret = jffs2_do_read_inode_internal(c, f, &n);
863
jffs2_do_clear_inode(c, f);
869
void jffs2_do_clear_inode(struct jffs2_sb_info *c,
870
struct jffs2_inode_info *f) {
871
struct jffs2_full_dirent *fd, *fds;
875
deleted = f->inocache && !f->inocache->nlink;
877
if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
878
jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
883
jffs2_mark_node_obsolete(c, f->metadata->raw);
885
jffs2_free_full_dnode(f->metadata);
888
jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
899
jffs2_free_full_dirent(fd);
902
if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
903
jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
904
if (f->inocache->nodes == (void *)f->inocache) {
905
jffs2_del_ino_cache(c, f->inocache);