0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013
0014 #include <linux/kernel.h>
0015 #include <linux/sched.h>
0016 #include <linux/slab.h>
0017 #include <linux/fs.h>
0018 #include <linux/crc32.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/mtd/mtd.h>
0021 #include <linux/compiler.h>
0022 #include "nodelist.h"
0023
0024
0025
0026
0027
0028
0029
0030
0031 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
0032 {
0033 struct jffs2_raw_node_ref *ref = tn->fn->raw;
0034 int err = 0, pointed = 0;
0035 struct jffs2_eraseblock *jeb;
0036 unsigned char *buffer;
0037 uint32_t crc, ofs, len;
0038 size_t retlen;
0039
0040 BUG_ON(tn->csize == 0);
0041
0042
0043 ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
0044 len = tn->csize;
0045
0046 if (jffs2_is_writebuffered(c)) {
0047 int adj = ofs % c->wbuf_pagesize;
0048 if (likely(adj))
0049 adj = c->wbuf_pagesize - adj;
0050
0051 if (adj >= tn->csize) {
0052 dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
0053 ref_offset(ref), tn->csize, ofs);
0054 goto adj_acc;
0055 }
0056
0057 ofs += adj;
0058 len -= adj;
0059 }
0060
0061 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",
0062 ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
0063
0064 #ifndef __ECOS
0065
0066
0067 err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
0068 if (!err && retlen < len) {
0069 JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
0070 mtd_unpoint(c->mtd, ofs, retlen);
0071 } else if (err) {
0072 if (err != -EOPNOTSUPP)
0073 JFFS2_WARNING("MTD point failed: error code %d.\n", err);
0074 } else
0075 pointed = 1;
0076 #endif
0077
0078 if (!pointed) {
0079 buffer = kmalloc(len, GFP_KERNEL);
0080 if (unlikely(!buffer))
0081 return -ENOMEM;
0082
0083
0084
0085 err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
0086 if (err) {
0087 JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
0088 goto free_out;
0089 }
0090
0091 if (retlen != len) {
0092 JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
0093 err = -EIO;
0094 goto free_out;
0095 }
0096 }
0097
0098
0099 crc = crc32(tn->partial_crc, buffer, len);
0100 if(!pointed)
0101 kfree(buffer);
0102 #ifndef __ECOS
0103 else
0104 mtd_unpoint(c->mtd, ofs, len);
0105 #endif
0106
0107 if (crc != tn->data_crc) {
0108 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
0109 ref_offset(ref), tn->data_crc, crc);
0110 return 1;
0111 }
0112
0113 adj_acc:
0114 jeb = &c->blocks[ref->flash_offset / c->sector_size];
0115 len = ref_totlen(c, jeb, ref);
0116
0117
0118
0119
0120 ref->flash_offset |= REF_PRISTINE;
0121
0122
0123
0124
0125 spin_lock(&c->erase_completion_lock);
0126 jeb->used_size += len;
0127 jeb->unchecked_size -= len;
0128 c->used_size += len;
0129 c->unchecked_size -= len;
0130 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
0131 spin_unlock(&c->erase_completion_lock);
0132
0133 return 0;
0134
0135 free_out:
0136 if(!pointed)
0137 kfree(buffer);
0138 #ifndef __ECOS
0139 else
0140 mtd_unpoint(c->mtd, ofs, len);
0141 #endif
0142 return err;
0143 }
0144
0145
0146
0147
0148
0149
0150 static int check_tn_node(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
0151 {
0152 int ret;
0153
0154 BUG_ON(ref_obsolete(tn->fn->raw));
0155
0156
0157 if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
0158 return 0;
0159
0160 dbg_readinode("check node %#04x-%#04x, phys offs %#08x\n",
0161 tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
0162
0163 ret = check_node_data(c, tn);
0164 if (unlikely(ret < 0)) {
0165 JFFS2_ERROR("check_node_data() returned error: %d.\n",
0166 ret);
0167 } else if (unlikely(ret > 0)) {
0168 dbg_readinode("CRC error, mark it obsolete.\n");
0169 jffs2_mark_node_obsolete(c, tn->fn->raw);
0170 }
0171
0172 return ret;
0173 }
0174
0175 static struct jffs2_tmp_dnode_info *jffs2_lookup_tn(struct rb_root *tn_root, uint32_t offset)
0176 {
0177 struct rb_node *next;
0178 struct jffs2_tmp_dnode_info *tn = NULL;
0179
0180 dbg_readinode("root %p, offset %d\n", tn_root, offset);
0181
0182 next = tn_root->rb_node;
0183
0184 while (next) {
0185 tn = rb_entry(next, struct jffs2_tmp_dnode_info, rb);
0186
0187 if (tn->fn->ofs < offset)
0188 next = tn->rb.rb_right;
0189 else if (tn->fn->ofs >= offset)
0190 next = tn->rb.rb_left;
0191 else
0192 break;
0193 }
0194
0195 return tn;
0196 }
0197
0198
0199 static void jffs2_kill_tn(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
0200 {
0201 jffs2_mark_node_obsolete(c, tn->fn->raw);
0202 jffs2_free_full_dnode(tn->fn);
0203 jffs2_free_tmp_dnode_info(tn);
0204 }
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 static int jffs2_add_tn_to_tree(struct jffs2_sb_info *c,
0219 struct jffs2_readinode_info *rii,
0220 struct jffs2_tmp_dnode_info *tn)
0221 {
0222 uint32_t fn_end = tn->fn->ofs + tn->fn->size;
0223 struct jffs2_tmp_dnode_info *this, *ptn;
0224
0225 dbg_readinode("insert fragment %#04x-%#04x, ver %u at %08x\n", tn->fn->ofs, fn_end, tn->version, ref_offset(tn->fn->raw));
0226
0227
0228
0229
0230
0231 if (!tn->fn->size) {
0232 if (rii->mdata_tn) {
0233 if (rii->mdata_tn->version < tn->version) {
0234
0235 dbg_readinode("kill old mdata with ver %d\n", rii->mdata_tn->version);
0236 jffs2_kill_tn(c, rii->mdata_tn);
0237 } else {
0238 dbg_readinode("kill new mdata with ver %d (older than existing %d\n",
0239 tn->version, rii->mdata_tn->version);
0240 jffs2_kill_tn(c, tn);
0241 return 0;
0242 }
0243 }
0244 rii->mdata_tn = tn;
0245 dbg_readinode("keep new mdata with ver %d\n", tn->version);
0246 return 0;
0247 }
0248
0249
0250 this = jffs2_lookup_tn(&rii->tn_root, tn->fn->ofs);
0251 if (this) {
0252
0253
0254 while (this->overlapped) {
0255 ptn = tn_prev(this);
0256 if (!ptn) {
0257
0258
0259
0260
0261 this->overlapped = 0;
0262 break;
0263 }
0264 this = ptn;
0265 }
0266 dbg_readinode("'this' found %#04x-%#04x (%s)\n", this->fn->ofs, this->fn->ofs + this->fn->size, this->fn ? "data" : "hole");
0267 }
0268
0269 while (this) {
0270 if (this->fn->ofs > fn_end)
0271 break;
0272 dbg_readinode("Ponder this ver %d, 0x%x-0x%x\n",
0273 this->version, this->fn->ofs, this->fn->size);
0274
0275 if (this->version == tn->version) {
0276
0277
0278 if (!check_tn_node(c, this)) {
0279
0280 dbg_readinode("Like old node. Throw away new\n");
0281 jffs2_kill_tn(c, tn);
0282 return 0;
0283 } else {
0284
0285 dbg_readinode("Like new node. Throw away old\n");
0286 rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
0287 jffs2_kill_tn(c, this);
0288
0289 return 0;
0290 }
0291 }
0292 if (this->version < tn->version &&
0293 this->fn->ofs >= tn->fn->ofs &&
0294 this->fn->ofs + this->fn->size <= fn_end) {
0295
0296 if (check_tn_node(c, tn)) {
0297 dbg_readinode("new node bad CRC\n");
0298 jffs2_kill_tn(c, tn);
0299 return 0;
0300 }
0301
0302 while (this && this->fn->ofs + this->fn->size <= fn_end) {
0303 struct jffs2_tmp_dnode_info *next = tn_next(this);
0304 if (this->version < tn->version) {
0305 tn_erase(this, &rii->tn_root);
0306 dbg_readinode("Kill overlapped ver %d, 0x%x-0x%x\n",
0307 this->version, this->fn->ofs,
0308 this->fn->ofs+this->fn->size);
0309 jffs2_kill_tn(c, this);
0310 }
0311 this = next;
0312 }
0313 dbg_readinode("Done killing overlapped nodes\n");
0314 continue;
0315 }
0316 if (this->version > tn->version &&
0317 this->fn->ofs <= tn->fn->ofs &&
0318 this->fn->ofs+this->fn->size >= fn_end) {
0319
0320 if (!check_tn_node(c, this)) {
0321 dbg_readinode("Good CRC on old node. Kill new\n");
0322 jffs2_kill_tn(c, tn);
0323 return 0;
0324 }
0325
0326 dbg_readinode("Bad CRC on old overlapping node. Kill it\n");
0327 tn_erase(this, &rii->tn_root);
0328 jffs2_kill_tn(c, this);
0329 break;
0330 }
0331
0332 this = tn_next(this);
0333 }
0334
0335
0336
0337 {
0338 struct rb_node *parent;
0339 struct rb_node **link = &rii->tn_root.rb_node;
0340 struct jffs2_tmp_dnode_info *insert_point = NULL;
0341
0342 while (*link) {
0343 parent = *link;
0344 insert_point = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
0345 if (tn->fn->ofs > insert_point->fn->ofs)
0346 link = &insert_point->rb.rb_right;
0347 else if (tn->fn->ofs < insert_point->fn->ofs ||
0348 tn->fn->size < insert_point->fn->size)
0349 link = &insert_point->rb.rb_left;
0350 else
0351 link = &insert_point->rb.rb_right;
0352 }
0353 rb_link_node(&tn->rb, &insert_point->rb, link);
0354 rb_insert_color(&tn->rb, &rii->tn_root);
0355 }
0356
0357
0358 this = tn_prev(tn);
0359 if (this) {
0360 while (1) {
0361 if (this->fn->ofs + this->fn->size > tn->fn->ofs) {
0362 dbg_readinode("Node is overlapped by %p (v %d, 0x%x-0x%x)\n",
0363 this, this->version, this->fn->ofs,
0364 this->fn->ofs+this->fn->size);
0365 tn->overlapped = 1;
0366 break;
0367 }
0368 if (!this->overlapped)
0369 break;
0370
0371 ptn = tn_prev(this);
0372 if (!ptn) {
0373
0374
0375
0376
0377 this->overlapped = 0;
0378 break;
0379 }
0380 this = ptn;
0381 }
0382 }
0383
0384
0385 this = tn_next(tn);
0386 while (this && this->fn->ofs < fn_end) {
0387 this->overlapped = 1;
0388 dbg_readinode("Node ver %d, 0x%x-0x%x is overlapped\n",
0389 this->version, this->fn->ofs,
0390 this->fn->ofs+this->fn->size);
0391 this = tn_next(this);
0392 }
0393 return 0;
0394 }
0395
0396
0397
0398
0399
0400
0401
0402 static void eat_last(struct rb_root *root, struct rb_node *node)
0403 {
0404 struct rb_node *parent = rb_parent(node);
0405 struct rb_node **link;
0406
0407
0408 BUG_ON(node->rb_right);
0409
0410 if (!parent)
0411 link = &root->rb_node;
0412 else if (node == parent->rb_left)
0413 link = &parent->rb_left;
0414 else
0415 link = &parent->rb_right;
0416
0417 *link = node->rb_left;
0418 if (node->rb_left)
0419 node->rb_left->__rb_parent_color = node->__rb_parent_color;
0420 }
0421
0422
0423
0424 static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn)
0425 {
0426 struct rb_node **link = &ver_root->rb_node;
0427 struct rb_node *parent = NULL;
0428 struct jffs2_tmp_dnode_info *this_tn;
0429
0430 while (*link) {
0431 parent = *link;
0432 this_tn = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
0433
0434 if (tn->version > this_tn->version)
0435 link = &parent->rb_left;
0436 else
0437 link = &parent->rb_right;
0438 }
0439 dbg_readinode("Link new node at %p (root is %p)\n", link, ver_root);
0440 rb_link_node(&tn->rb, parent, link);
0441 rb_insert_color(&tn->rb, ver_root);
0442 }
0443
0444
0445
0446
0447
0448
0449
0450
0451 static int jffs2_build_inode_fragtree(struct jffs2_sb_info *c,
0452 struct jffs2_inode_info *f,
0453 struct jffs2_readinode_info *rii)
0454 {
0455 struct jffs2_tmp_dnode_info *pen, *last, *this;
0456 struct rb_root ver_root = RB_ROOT;
0457 uint32_t high_ver = 0;
0458
0459 if (rii->mdata_tn) {
0460 dbg_readinode("potential mdata is ver %d at %p\n", rii->mdata_tn->version, rii->mdata_tn);
0461 high_ver = rii->mdata_tn->version;
0462 rii->latest_ref = rii->mdata_tn->fn->raw;
0463 }
0464 #ifdef JFFS2_DBG_READINODE_MESSAGES
0465 this = tn_last(&rii->tn_root);
0466 while (this) {
0467 dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
0468 this->fn->ofs+this->fn->size, this->overlapped);
0469 this = tn_prev(this);
0470 }
0471 #endif
0472 pen = tn_last(&rii->tn_root);
0473 while ((last = pen)) {
0474 pen = tn_prev(last);
0475
0476 eat_last(&rii->tn_root, &last->rb);
0477 ver_insert(&ver_root, last);
0478
0479 if (unlikely(last->overlapped)) {
0480 if (pen)
0481 continue;
0482
0483
0484
0485
0486 last->overlapped = 0;
0487 }
0488
0489
0490
0491
0492
0493 this = tn_last(&ver_root);
0494
0495 while (this) {
0496 struct jffs2_tmp_dnode_info *vers_next;
0497 int ret;
0498 vers_next = tn_prev(this);
0499 eat_last(&ver_root, &this->rb);
0500 if (check_tn_node(c, this)) {
0501 dbg_readinode("node ver %d, 0x%x-0x%x failed CRC\n",
0502 this->version, this->fn->ofs,
0503 this->fn->ofs+this->fn->size);
0504 jffs2_kill_tn(c, this);
0505 } else {
0506 if (this->version > high_ver) {
0507
0508
0509
0510
0511 high_ver = this->version;
0512 rii->latest_ref = this->fn->raw;
0513 }
0514 dbg_readinode("Add %p (v %d, 0x%x-0x%x, ov %d) to fragtree\n",
0515 this, this->version, this->fn->ofs,
0516 this->fn->ofs+this->fn->size, this->overlapped);
0517
0518 ret = jffs2_add_full_dnode_to_inode(c, f, this->fn);
0519 if (ret) {
0520
0521
0522 JFFS2_ERROR("Add node to tree failed %d\n", ret);
0523 while (1) {
0524 vers_next = tn_prev(this);
0525 if (check_tn_node(c, this))
0526 jffs2_mark_node_obsolete(c, this->fn->raw);
0527 jffs2_free_full_dnode(this->fn);
0528 jffs2_free_tmp_dnode_info(this);
0529 this = vers_next;
0530 if (!this)
0531 break;
0532 eat_last(&ver_root, &vers_next->rb);
0533 }
0534 return ret;
0535 }
0536 jffs2_free_tmp_dnode_info(this);
0537 }
0538 this = vers_next;
0539 }
0540 }
0541 return 0;
0542 }
0543
0544 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
0545 {
0546 struct jffs2_tmp_dnode_info *tn, *next;
0547
0548 rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
0549 jffs2_free_full_dnode(tn->fn);
0550 jffs2_free_tmp_dnode_info(tn);
0551 }
0552
0553 *list = RB_ROOT;
0554 }
0555
0556 static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
0557 {
0558 struct jffs2_full_dirent *next;
0559
0560 while (fd) {
0561 next = fd->next;
0562 jffs2_free_full_dirent(fd);
0563 fd = next;
0564 }
0565 }
0566
0567
0568 static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
0569 {
0570 while (ref && ref->next_in_ino) {
0571 if (!ref_obsolete(ref))
0572 return ref;
0573 dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
0574 ref = ref->next_in_ino;
0575 }
0576 return NULL;
0577 }
0578
0579
0580
0581
0582
0583
0584
0585
0586 static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
0587 struct jffs2_raw_dirent *rd, size_t read,
0588 struct jffs2_readinode_info *rii)
0589 {
0590 struct jffs2_full_dirent *fd;
0591 uint32_t crc;
0592
0593
0594 BUG_ON(ref_obsolete(ref));
0595
0596 crc = crc32(0, rd, sizeof(*rd) - 8);
0597 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
0598 JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
0599 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
0600 jffs2_mark_node_obsolete(c, ref);
0601 return 0;
0602 }
0603
0604
0605 if (ref_flags(ref) == REF_UNCHECKED) {
0606 struct jffs2_eraseblock *jeb;
0607 int len;
0608
0609
0610 if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
0611 JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
0612 ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
0613 jffs2_mark_node_obsolete(c, ref);
0614 return 0;
0615 }
0616
0617 jeb = &c->blocks[ref->flash_offset / c->sector_size];
0618 len = ref_totlen(c, jeb, ref);
0619
0620 spin_lock(&c->erase_completion_lock);
0621 jeb->used_size += len;
0622 jeb->unchecked_size -= len;
0623 c->used_size += len;
0624 c->unchecked_size -= len;
0625 ref->flash_offset = ref_offset(ref) | dirent_node_state(rd);
0626 spin_unlock(&c->erase_completion_lock);
0627 }
0628
0629 fd = jffs2_alloc_full_dirent(rd->nsize + 1);
0630 if (unlikely(!fd))
0631 return -ENOMEM;
0632
0633 fd->raw = ref;
0634 fd->version = je32_to_cpu(rd->version);
0635 fd->ino = je32_to_cpu(rd->ino);
0636 fd->type = rd->type;
0637
0638 if (fd->version > rii->highest_version)
0639 rii->highest_version = fd->version;
0640
0641
0642 if(fd->version > rii->mctime_ver && je32_to_cpu(rd->mctime)) {
0643 rii->mctime_ver = fd->version;
0644 rii->latest_mctime = je32_to_cpu(rd->mctime);
0645 }
0646
0647
0648
0649
0650
0651 if (read > sizeof(*rd))
0652 memcpy(&fd->name[0], &rd->name[0],
0653 min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
0654
0655
0656 if (rd->nsize + sizeof(*rd) > read) {
0657
0658 int err;
0659 int already = read - sizeof(*rd);
0660
0661 err = jffs2_flash_read(c, (ref_offset(ref)) + read,
0662 rd->nsize - already, &read, &fd->name[already]);
0663 if (unlikely(read != rd->nsize - already) && likely(!err)) {
0664 jffs2_free_full_dirent(fd);
0665 JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
0666 rd->nsize - already, read);
0667 return -EIO;
0668 }
0669
0670 if (unlikely(err)) {
0671 JFFS2_ERROR("read remainder of name: error %d\n", err);
0672 jffs2_free_full_dirent(fd);
0673 return -EIO;
0674 }
0675
0676 #ifdef CONFIG_JFFS2_SUMMARY
0677
0678
0679
0680
0681 crc = crc32(0, fd->name, rd->nsize);
0682 if (unlikely(crc != je32_to_cpu(rd->name_crc))) {
0683 JFFS2_NOTICE("name CRC failed on dirent node at"
0684 "%#08x: read %#08x,calculated %#08x\n",
0685 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
0686 jffs2_mark_node_obsolete(c, ref);
0687 jffs2_free_full_dirent(fd);
0688 return 0;
0689 }
0690 #endif
0691 }
0692
0693 fd->nhash = full_name_hash(NULL, fd->name, rd->nsize);
0694 fd->next = NULL;
0695 fd->name[rd->nsize] = '\0';
0696
0697
0698
0699
0700
0701 jffs2_add_fd_to_list(c, fd, &rii->fds);
0702
0703 return 0;
0704 }
0705
0706
0707
0708
0709
0710
0711
0712
0713 static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
0714 struct jffs2_raw_inode *rd, int rdlen,
0715 struct jffs2_readinode_info *rii)
0716 {
0717 struct jffs2_tmp_dnode_info *tn;
0718 uint32_t len, csize;
0719 int ret = 0;
0720 uint32_t crc;
0721
0722
0723 BUG_ON(ref_obsolete(ref));
0724
0725 crc = crc32(0, rd, sizeof(*rd) - 8);
0726 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
0727 JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
0728 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
0729 jffs2_mark_node_obsolete(c, ref);
0730 return 0;
0731 }
0732
0733 tn = jffs2_alloc_tmp_dnode_info();
0734 if (!tn) {
0735 JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
0736 return -ENOMEM;
0737 }
0738
0739 tn->partial_crc = 0;
0740 csize = je32_to_cpu(rd->csize);
0741
0742
0743 if (ref_flags(ref) == REF_UNCHECKED) {
0744
0745
0746 if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
0747 unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
0748 JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
0749 jffs2_dbg_dump_node(c, ref_offset(ref));
0750 jffs2_mark_node_obsolete(c, ref);
0751 goto free_out;
0752 }
0753
0754 if (jffs2_is_writebuffered(c) && csize != 0) {
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790 unsigned char *buf;
0791
0792
0793 buf = (unsigned char *)rd + sizeof(*rd);
0794
0795 len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
0796 tn->partial_crc = crc32(0, buf, len);
0797
0798 dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
0799
0800
0801
0802 if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
0803 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
0804 ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
0805 jffs2_mark_node_obsolete(c, ref);
0806 goto free_out;
0807 }
0808
0809 } else if (csize == 0) {
0810
0811
0812
0813
0814
0815
0816 struct jffs2_eraseblock *jeb;
0817
0818 dbg_readinode("the node has no data.\n");
0819 jeb = &c->blocks[ref->flash_offset / c->sector_size];
0820 len = ref_totlen(c, jeb, ref);
0821
0822 spin_lock(&c->erase_completion_lock);
0823 jeb->used_size += len;
0824 jeb->unchecked_size -= len;
0825 c->used_size += len;
0826 c->unchecked_size -= len;
0827 ref->flash_offset = ref_offset(ref) | REF_NORMAL;
0828 spin_unlock(&c->erase_completion_lock);
0829 }
0830 }
0831
0832 tn->fn = jffs2_alloc_full_dnode();
0833 if (!tn->fn) {
0834 JFFS2_ERROR("alloc fn failed\n");
0835 ret = -ENOMEM;
0836 goto free_out;
0837 }
0838
0839 tn->version = je32_to_cpu(rd->version);
0840 tn->fn->ofs = je32_to_cpu(rd->offset);
0841 tn->data_crc = je32_to_cpu(rd->data_crc);
0842 tn->csize = csize;
0843 tn->fn->raw = ref;
0844 tn->overlapped = 0;
0845
0846 if (tn->version > rii->highest_version)
0847 rii->highest_version = tn->version;
0848
0849
0850
0851 if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
0852 tn->fn->size = csize;
0853 else
0854 tn->fn->size = je32_to_cpu(rd->dsize);
0855
0856 dbg_readinode2("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
0857 ref_offset(ref), je32_to_cpu(rd->version),
0858 je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
0859
0860 ret = jffs2_add_tn_to_tree(c, rii, tn);
0861
0862 if (ret) {
0863 jffs2_free_full_dnode(tn->fn);
0864 free_out:
0865 jffs2_free_tmp_dnode_info(tn);
0866 return ret;
0867 }
0868 #ifdef JFFS2_DBG_READINODE2_MESSAGES
0869 dbg_readinode2("After adding ver %d:\n", je32_to_cpu(rd->version));
0870 tn = tn_first(&rii->tn_root);
0871 while (tn) {
0872 dbg_readinode2("%p: v %d r 0x%x-0x%x ov %d\n",
0873 tn, tn->version, tn->fn->ofs,
0874 tn->fn->ofs+tn->fn->size, tn->overlapped);
0875 tn = tn_next(tn);
0876 }
0877 #endif
0878 return 0;
0879 }
0880
0881
0882
0883
0884
0885
0886
0887
0888 static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
0889 {
0890
0891 if (ref_flags(ref) == REF_UNCHECKED) {
0892 JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
0893 ref_offset(ref));
0894 JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
0895 je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
0896 je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
0897 jffs2_mark_node_obsolete(c, ref);
0898 return 0;
0899 }
0900
0901 un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
0902
0903 switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
0904
0905 case JFFS2_FEATURE_INCOMPAT:
0906 JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
0907 je16_to_cpu(un->nodetype), ref_offset(ref));
0908
0909 BUG();
0910 break;
0911
0912 case JFFS2_FEATURE_ROCOMPAT:
0913 JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
0914 je16_to_cpu(un->nodetype), ref_offset(ref));
0915 BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
0916 break;
0917
0918 case JFFS2_FEATURE_RWCOMPAT_COPY:
0919 JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
0920 je16_to_cpu(un->nodetype), ref_offset(ref));
0921 break;
0922
0923 case JFFS2_FEATURE_RWCOMPAT_DELETE:
0924 JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
0925 je16_to_cpu(un->nodetype), ref_offset(ref));
0926 jffs2_mark_node_obsolete(c, ref);
0927 return 0;
0928 }
0929
0930 return 0;
0931 }
0932
0933
0934
0935
0936
0937
0938
0939
0940 static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
0941 int needed_len, int *rdlen, unsigned char *buf)
0942 {
0943 int err, to_read = needed_len - *rdlen;
0944 size_t retlen;
0945 uint32_t offs;
0946
0947 if (jffs2_is_writebuffered(c)) {
0948 int rem = to_read % c->wbuf_pagesize;
0949
0950 if (rem)
0951 to_read += c->wbuf_pagesize - rem;
0952 }
0953
0954
0955 offs = ref_offset(ref) + *rdlen;
0956
0957 dbg_readinode("read more %d bytes\n", to_read);
0958
0959 err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
0960 if (err) {
0961 JFFS2_ERROR("can not read %d bytes from 0x%08x, "
0962 "error code: %d.\n", to_read, offs, err);
0963 return err;
0964 }
0965
0966 if (retlen < to_read) {
0967 JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
0968 offs, retlen, to_read);
0969 return -EIO;
0970 }
0971
0972 *rdlen += to_read;
0973 return 0;
0974 }
0975
0976
0977
0978
0979
0980
0981 static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
0982 struct jffs2_readinode_info *rii)
0983 {
0984 struct jffs2_raw_node_ref *ref, *valid_ref;
0985 unsigned char *buf = NULL;
0986 union jffs2_node_union *node;
0987 size_t retlen;
0988 int len, err;
0989
0990 rii->mctime_ver = 0;
0991
0992 dbg_readinode("ino #%u\n", f->inocache->ino);
0993
0994
0995
0996 len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
0997 buf = kmalloc(len, GFP_KERNEL);
0998 if (!buf)
0999 return -ENOMEM;
1000
1001 spin_lock(&c->erase_completion_lock);
1002 valid_ref = jffs2_first_valid_node(f->inocache->nodes);
1003 if (!valid_ref && f->inocache->ino != 1)
1004 JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
1005 while (valid_ref) {
1006
1007
1008
1009
1010
1011
1012 ref = valid_ref;
1013 valid_ref = jffs2_first_valid_node(ref->next_in_ino);
1014 spin_unlock(&c->erase_completion_lock);
1015
1016 cond_resched();
1017
1018
1019
1020
1021
1022
1023
1024 len = JFFS2_MIN_NODE_HEADER;
1025 if (jffs2_is_writebuffered(c)) {
1026 int end, rem;
1027
1028
1029
1030
1031
1032
1033
1034
1035 end = ref_offset(ref) + len;
1036 rem = end % c->wbuf_pagesize;
1037 if (rem)
1038 end += c->wbuf_pagesize - rem;
1039 len = end - ref_offset(ref);
1040 }
1041
1042 dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
1043
1044
1045 err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
1046 if (err) {
1047 JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
1048 goto free_out;
1049 }
1050
1051 if (retlen < len) {
1052 JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
1053 err = -EIO;
1054 goto free_out;
1055 }
1056
1057 node = (union jffs2_node_union *)buf;
1058
1059
1060 if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
1061 JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
1062 ref_offset(ref), je16_to_cpu(node->u.magic),
1063 je16_to_cpu(node->u.nodetype),
1064 je32_to_cpu(node->u.totlen),
1065 je32_to_cpu(node->u.hdr_crc));
1066 jffs2_dbg_dump_node(c, ref_offset(ref));
1067 jffs2_mark_node_obsolete(c, ref);
1068 goto cont;
1069 }
1070 if (je16_to_cpu(node->u.magic) != JFFS2_MAGIC_BITMASK) {
1071
1072 JFFS2_NOTICE("Wrong magic bitmask 0x%04x in node header at %#08x.\n",
1073 je16_to_cpu(node->u.magic), ref_offset(ref));
1074 jffs2_mark_node_obsolete(c, ref);
1075 goto cont;
1076 }
1077
1078 switch (je16_to_cpu(node->u.nodetype)) {
1079
1080 case JFFS2_NODETYPE_DIRENT:
1081
1082 if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
1083 len < sizeof(struct jffs2_raw_dirent)) {
1084 err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
1085 if (unlikely(err))
1086 goto free_out;
1087 }
1088
1089 err = read_direntry(c, ref, &node->d, retlen, rii);
1090 if (unlikely(err))
1091 goto free_out;
1092
1093 break;
1094
1095 case JFFS2_NODETYPE_INODE:
1096
1097 if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
1098 len < sizeof(struct jffs2_raw_inode)) {
1099 err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
1100 if (unlikely(err))
1101 goto free_out;
1102 }
1103
1104 err = read_dnode(c, ref, &node->i, len, rii);
1105 if (unlikely(err))
1106 goto free_out;
1107
1108 break;
1109
1110 default:
1111 if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node) &&
1112 len < sizeof(struct jffs2_unknown_node)) {
1113 err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
1114 if (unlikely(err))
1115 goto free_out;
1116 }
1117
1118 err = read_unknown(c, ref, &node->u);
1119 if (unlikely(err))
1120 goto free_out;
1121
1122 }
1123 cont:
1124 spin_lock(&c->erase_completion_lock);
1125 }
1126
1127 spin_unlock(&c->erase_completion_lock);
1128 kfree(buf);
1129
1130 f->highest_version = rii->highest_version;
1131
1132 dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
1133 f->inocache->ino, rii->highest_version, rii->latest_mctime,
1134 rii->mctime_ver);
1135 return 0;
1136
1137 free_out:
1138 jffs2_free_tmp_dnode_info_list(&rii->tn_root);
1139 jffs2_free_full_dirent_list(rii->fds);
1140 rii->fds = NULL;
1141 kfree(buf);
1142 return err;
1143 }
1144
1145 static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
1146 struct jffs2_inode_info *f,
1147 struct jffs2_raw_inode *latest_node)
1148 {
1149 struct jffs2_readinode_info rii;
1150 uint32_t crc, new_size;
1151 size_t retlen;
1152 int ret;
1153
1154 dbg_readinode("ino #%u pino/nlink is %d\n", f->inocache->ino,
1155 f->inocache->pino_nlink);
1156
1157 memset(&rii, 0, sizeof(rii));
1158
1159
1160 ret = jffs2_get_inode_nodes(c, f, &rii);
1161
1162 if (ret) {
1163 JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
1164 if (f->inocache->state == INO_STATE_READING)
1165 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1166 return ret;
1167 }
1168
1169 ret = jffs2_build_inode_fragtree(c, f, &rii);
1170 if (ret) {
1171 JFFS2_ERROR("Failed to build final fragtree for inode #%u: error %d\n",
1172 f->inocache->ino, ret);
1173 if (f->inocache->state == INO_STATE_READING)
1174 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1175 jffs2_free_tmp_dnode_info_list(&rii.tn_root);
1176
1177 if (rii.mdata_tn) {
1178 jffs2_free_full_dnode(rii.mdata_tn->fn);
1179 jffs2_free_tmp_dnode_info(rii.mdata_tn);
1180 rii.mdata_tn = NULL;
1181 }
1182 return ret;
1183 }
1184
1185 if (rii.mdata_tn) {
1186 if (rii.mdata_tn->fn->raw == rii.latest_ref) {
1187 f->metadata = rii.mdata_tn->fn;
1188 jffs2_free_tmp_dnode_info(rii.mdata_tn);
1189 } else {
1190 jffs2_kill_tn(c, rii.mdata_tn);
1191 }
1192 rii.mdata_tn = NULL;
1193 }
1194
1195 f->dents = rii.fds;
1196
1197 jffs2_dbg_fragtree_paranoia_check_nolock(f);
1198
1199 if (unlikely(!rii.latest_ref)) {
1200
1201 if (f->inocache->ino != 1) {
1202 JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
1203 if (!rii.fds) {
1204 if (f->inocache->state == INO_STATE_READING)
1205 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1206 return -EIO;
1207 }
1208 JFFS2_NOTICE("but it has children so we fake some modes for it\n");
1209 }
1210 latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
1211 latest_node->version = cpu_to_je32(0);
1212 latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
1213 latest_node->isize = cpu_to_je32(0);
1214 latest_node->gid = cpu_to_je16(0);
1215 latest_node->uid = cpu_to_je16(0);
1216 if (f->inocache->state == INO_STATE_READING)
1217 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1218 return 0;
1219 }
1220
1221 ret = jffs2_flash_read(c, ref_offset(rii.latest_ref), sizeof(*latest_node), &retlen, (void *)latest_node);
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));
1225
1226 return ret ? ret : -EIO;
1227 }
1228
1229 crc = crc32(0, latest_node, sizeof(*latest_node)-8);
1230 if (crc != je32_to_cpu(latest_node->node_crc)) {
1231 JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
1232 f->inocache->ino, ref_offset(rii.latest_ref));
1233 return -EIO;
1234 }
1235
1236 switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
1237 case S_IFDIR:
1238 if (rii.mctime_ver > je32_to_cpu(latest_node->version)) {
1239
1240
1241 latest_node->ctime = latest_node->mtime = cpu_to_je32(rii.latest_mctime);
1242 }
1243 break;
1244
1245
1246 case S_IFREG:
1247
1248 new_size = jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
1249 if (new_size != je32_to_cpu(latest_node->isize)) {
1250 JFFS2_WARNING("Truncating ino #%u to %d bytes failed because it only had %d bytes to start with!\n",
1251 f->inocache->ino, je32_to_cpu(latest_node->isize), new_size);
1252 latest_node->isize = cpu_to_je32(new_size);
1253 }
1254 break;
1255
1256 case S_IFLNK:
1257
1258
1259
1260
1261 if (!je32_to_cpu(latest_node->isize))
1262 latest_node->isize = latest_node->dsize;
1263
1264 if (f->inocache->state != INO_STATE_CHECKING) {
1265
1266
1267
1268 uint32_t csize = je32_to_cpu(latest_node->csize);
1269 if (csize > JFFS2_MAX_NAME_LEN)
1270 return -ENAMETOOLONG;
1271 f->target = kmalloc(csize + 1, GFP_KERNEL);
1272 if (!f->target) {
1273 JFFS2_ERROR("can't allocate %u bytes of memory for the symlink target path cache\n", csize);
1274 return -ENOMEM;
1275 }
1276
1277 ret = jffs2_flash_read(c, ref_offset(rii.latest_ref) + sizeof(*latest_node),
1278 csize, &retlen, (char *)f->target);
1279
1280 if (ret || retlen != csize) {
1281 if (retlen != csize)
1282 ret = -EIO;
1283 kfree(f->target);
1284 f->target = NULL;
1285 return ret;
1286 }
1287
1288 f->target[csize] = '\0';
1289 dbg_readinode("symlink's target '%s' cached\n", f->target);
1290 }
1291
1292 fallthrough;
1293
1294 case S_IFBLK:
1295 case S_IFCHR:
1296
1297
1298 if (f->metadata) {
1299 JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
1300 f->inocache->ino, jemode_to_cpu(latest_node->mode));
1301 return -EIO;
1302 }
1303 if (!frag_first(&f->fragtree)) {
1304 JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
1305 f->inocache->ino, jemode_to_cpu(latest_node->mode));
1306 return -EIO;
1307 }
1308
1309 if (frag_next(frag_first(&f->fragtree))) {
1310 JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
1311 f->inocache->ino, jemode_to_cpu(latest_node->mode));
1312
1313 return -EIO;
1314 }
1315
1316 f->metadata = frag_first(&f->fragtree)->node;
1317 jffs2_free_node_frag(frag_first(&f->fragtree));
1318 f->fragtree = RB_ROOT;
1319 break;
1320 }
1321 if (f->inocache->state == INO_STATE_READING)
1322 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1323
1324 return 0;
1325 }
1326
1327
1328 int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
1329 uint32_t ino, struct jffs2_raw_inode *latest_node)
1330 {
1331 dbg_readinode("read inode #%u\n", ino);
1332
1333 retry_inocache:
1334 spin_lock(&c->inocache_lock);
1335 f->inocache = jffs2_get_ino_cache(c, ino);
1336
1337 if (f->inocache) {
1338
1339 switch(f->inocache->state) {
1340 case INO_STATE_UNCHECKED:
1341 case INO_STATE_CHECKEDABSENT:
1342 f->inocache->state = INO_STATE_READING;
1343 break;
1344
1345 case INO_STATE_CHECKING:
1346 case INO_STATE_GC:
1347
1348
1349
1350 dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
1351 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
1352 goto retry_inocache;
1353
1354 case INO_STATE_READING:
1355 case INO_STATE_PRESENT:
1356
1357
1358
1359 JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
1360
1361 f->inocache = NULL;
1362 break;
1363
1364 default:
1365 BUG();
1366 }
1367 }
1368 spin_unlock(&c->inocache_lock);
1369
1370 if (!f->inocache && ino == 1) {
1371
1372 f->inocache = jffs2_alloc_inode_cache();
1373 if (!f->inocache) {
1374 JFFS2_ERROR("cannot allocate inocache for root inode\n");
1375 return -ENOMEM;
1376 }
1377 dbg_readinode("creating inocache for root inode\n");
1378 memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
1379 f->inocache->ino = f->inocache->pino_nlink = 1;
1380 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
1381 f->inocache->state = INO_STATE_READING;
1382 jffs2_add_ino_cache(c, f->inocache);
1383 }
1384 if (!f->inocache) {
1385 JFFS2_ERROR("requested to read a nonexistent ino %u\n", ino);
1386 return -ENOENT;
1387 }
1388
1389 return jffs2_do_read_inode_internal(c, f, latest_node);
1390 }
1391
1392 int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
1393 {
1394 struct jffs2_raw_inode n;
1395 struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
1396 int ret;
1397
1398 if (!f)
1399 return -ENOMEM;
1400
1401 mutex_init(&f->sem);
1402 mutex_lock(&f->sem);
1403 f->inocache = ic;
1404
1405 ret = jffs2_do_read_inode_internal(c, f, &n);
1406 mutex_unlock(&f->sem);
1407 jffs2_do_clear_inode(c, f);
1408 jffs2_xattr_do_crccheck_inode(c, ic);
1409 kfree (f);
1410 return ret;
1411 }
1412
1413 void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
1414 {
1415 struct jffs2_full_dirent *fd, *fds;
1416 int deleted;
1417
1418 jffs2_xattr_delete_inode(c, f->inocache);
1419 mutex_lock(&f->sem);
1420 deleted = f->inocache && !f->inocache->pino_nlink;
1421
1422 if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
1423 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
1424
1425 if (f->metadata) {
1426 if (deleted)
1427 jffs2_mark_node_obsolete(c, f->metadata->raw);
1428 jffs2_free_full_dnode(f->metadata);
1429 }
1430
1431 jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
1432
1433 fds = f->dents;
1434 while(fds) {
1435 fd = fds;
1436 fds = fd->next;
1437 jffs2_free_full_dirent(fd);
1438 }
1439
1440 if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
1441 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1442 if (f->inocache->nodes == (void *)f->inocache)
1443 jffs2_del_ino_cache(c, f->inocache);
1444 }
1445
1446 mutex_unlock(&f->sem);
1447 }