2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: readinode.c,v 1.132 2005/07/28 14:46:40 dedekind Exp $
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/crc32.h>
18 #include <linux/pagemap.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/compiler.h>
23 void jffs2_truncate_fragtree (struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
25 struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
27 JFFS2_DBG_FRAGTREE("truncating fragtree to 0x%08x bytes\n", size);
29 /* We know frag->ofs <= size. That's what lookup does for us */
30 if (frag && frag->ofs != size) {
31 if (frag->ofs+frag->size >= size) {
32 JFFS2_DBG_FRAGTREE2("truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
33 frag->size = size - frag->ofs;
35 frag = frag_next(frag);
37 while (frag && frag->ofs >= size) {
38 struct jffs2_node_frag *next = frag_next(frag);
40 JFFS2_DBG_FRAGTREE("removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
41 frag_erase(frag, list);
42 jffs2_obsolete_node_frag(c, frag);
48 * Put a new tmp_dnode_info into the temporaty RB-tree, keeping the list in
49 * order of increasing version.
51 static void jffs2_add_tn_to_tree(struct jffs2_tmp_dnode_info *tn, struct rb_root *list)
53 struct rb_node **p = &list->rb_node;
54 struct rb_node * parent = NULL;
55 struct jffs2_tmp_dnode_info *this;
59 this = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
61 /* There may actually be a collision here, but it doesn't
62 actually matter. As long as the two nodes with the same
63 version are together, it's all fine. */
64 if (tn->version < this->version)
70 rb_link_node(&tn->rb, parent, p);
71 rb_insert_color(&tn->rb, list);
74 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
77 struct jffs2_tmp_dnode_info *tn;
81 /* Now at bottom of tree */
85 else if (this->rb_right)
86 this = this->rb_right;
88 tn = rb_entry(this, struct jffs2_tmp_dnode_info, rb);
89 jffs2_free_full_dnode(tn->fn);
90 jffs2_free_tmp_dnode_info(tn);
92 this = this->rb_parent;
96 if (this->rb_left == &tn->rb)
98 else if (this->rb_right == &tn->rb)
99 this->rb_right = NULL;
103 list->rb_node = NULL;
106 static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
108 struct jffs2_full_dirent *next;
112 jffs2_free_full_dirent(fd);
117 /* Returns first valid node after 'ref'. May return 'ref' */
118 static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
120 while (ref && ref->next_in_ino) {
121 if (!ref_obsolete(ref))
123 JFFS2_DBG_NODEREF("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
124 ref = ref->next_in_ino;
130 * Helper function for jffs2_get_inode_nodes().
131 * It is called every time an directory entry node is found.
133 * Returns: 0 on succes;
134 * 1 if the node should be marked obsolete;
135 * negative error code on failure.
138 read_direntry(struct jffs2_sb_info *c,
139 struct jffs2_raw_node_ref *ref,
140 struct jffs2_raw_dirent *rd,
142 struct jffs2_full_dirent **fdp,
143 int32_t *latest_mctime,
144 uint32_t *mctime_ver)
146 struct jffs2_full_dirent *fd;
148 /* The direntry nodes are checked during the flash scanning */
149 BUG_ON(ref_flags(ref) == REF_UNCHECKED);
150 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
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);
165 fd->version = je32_to_cpu(rd->version);
166 fd->ino = je32_to_cpu(rd->ino);
169 /* Pick out the mctime of the latest dirent */
170 if(fd->version > *mctime_ver) {
171 *mctime_ver = fd->version;
172 *latest_mctime = je32_to_cpu(rd->mctime);
176 * Copy as much of the name as possible from the raw
177 * dirent we've already read from the flash.
179 if (read > sizeof(*rd))
180 memcpy(&fd->name[0], &rd->name[0],
181 min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
183 /* Do we need to copy any more of the name directly from the flash? */
184 if (rd->nsize + sizeof(*rd) > read) {
187 int already = read - sizeof(*rd);
189 err = jffs2_flash_read(c, (ref_offset(ref)) + read,
190 rd->nsize - already, &read, &fd->name[already]);
191 if (unlikely(read != rd->nsize - already) && likely(!err))
195 JFFS2_ERROR("read remainder of name: error %d\n", err);
196 jffs2_free_full_dirent(fd);
201 fd->nhash = full_name_hash(fd->name, rd->nsize);
203 fd->name[rd->nsize] = '\0';
206 * Wheee. We now have a complete jffs2_full_dirent structure, with
207 * the name in it and everything. Link it into the list
209 jffs2_add_fd_to_list(c, fd, fdp);
215 * Helper function for jffs2_get_inode_nodes().
216 * It is called every time an inode node is found.
218 * Returns: 0 on succes;
219 * 1 if the node should be marked obsolete;
220 * negative error code on failure.
223 read_dnode(struct jffs2_sb_info *c,
224 struct jffs2_raw_node_ref *ref,
225 struct jffs2_raw_inode *rd,
228 int32_t *latest_mctime,
229 uint32_t *mctime_ver)
231 struct jffs2_eraseblock *jeb;
232 struct jffs2_tmp_dnode_info *tn;
234 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
235 BUG_ON(ref_obsolete(ref));
237 /* If we've never checked the CRCs on this node, check them now */
238 if (ref_flags(ref) == REF_UNCHECKED) {
241 crc = crc32(0, rd, sizeof(*rd) - 8);
242 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
243 JFFS2_NOTICE("header CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
244 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
249 if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
250 unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
251 JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
252 __jffs2_dbg_dump_node(c, ref_offset(ref));
256 if (rd->compr != JFFS2_COMPR_ZERO && je32_to_cpu(rd->csize)) {
257 unsigned char *buf = NULL;
258 uint32_t pointed = 0;
262 err = c->mtd->point (c->mtd, ref_offset(ref) + sizeof(*rd), je32_to_cpu(rd->csize),
264 if (unlikely(read < je32_to_cpu(rd->csize)) && likely(!err)) {
265 JFFS2_ERROR("MTD point returned len too short: 0x%zx\n", read);
266 c->mtd->unpoint(c->mtd, buf, ref_offset(ref) + sizeof(*rd),
267 je32_to_cpu(rd->csize));
268 } else if (unlikely(err)){
269 JFFS2_ERROR("MTD point failed %d\n", err);
271 pointed = 1; /* succefully pointed to device */
275 buf = kmalloc(je32_to_cpu(rd->csize), GFP_KERNEL);
279 err = jffs2_flash_read(c, ref_offset(ref) + sizeof(*rd), je32_to_cpu(rd->csize),
281 if (unlikely(read != je32_to_cpu(rd->csize)) && likely(!err))
288 crc = crc32(0, buf, je32_to_cpu(rd->csize));
293 c->mtd->unpoint(c->mtd, buf, ref_offset(ref) + sizeof(*rd), je32_to_cpu(rd->csize));
296 if (crc != je32_to_cpu(rd->data_crc)) {
297 JFFS2_NOTICE("data CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
298 ref_offset(ref), je32_to_cpu(rd->data_crc), crc);
304 /* Mark the node as having been checked and fix the accounting accordingly */
305 jeb = &c->blocks[ref->flash_offset / c->sector_size];
306 len = ref_totlen(c, jeb, ref);
308 spin_lock(&c->erase_completion_lock);
309 jeb->used_size += len;
310 jeb->unchecked_size -= len;
312 c->unchecked_size -= len;
314 /* If node covers at least a whole page, or if it starts at the
315 beginning of a page and runs to the end of the file, or if
316 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
318 If it's actually overlapped, it'll get made NORMAL (or OBSOLETE)
319 when the overlapping node(s) get added to the tree anyway.
321 if ((je32_to_cpu(rd->dsize) >= PAGE_CACHE_SIZE) ||
322 ( ((je32_to_cpu(rd->offset) & (PAGE_CACHE_SIZE-1))==0) &&
323 (je32_to_cpu(rd->dsize) + je32_to_cpu(rd->offset) == je32_to_cpu(rd->isize)))) {
324 JFFS2_DBG_READINODE("marking node at %#08x REF_PRISTINE\n", ref_offset(ref));
325 ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
327 JFFS2_DBG_READINODE("marking node at %#08x REF_NORMAL\n", ref_offset(ref));
328 ref->flash_offset = ref_offset(ref) | REF_NORMAL;
330 spin_unlock(&c->erase_completion_lock);
333 tn = jffs2_alloc_tmp_dnode_info();
335 JFFS2_ERROR("alloc tn failed\n");
339 tn->fn = jffs2_alloc_full_dnode();
341 JFFS2_ERROR("alloc fn failed\n");
342 jffs2_free_tmp_dnode_info(tn);
346 tn->version = je32_to_cpu(rd->version);
347 tn->fn->ofs = je32_to_cpu(rd->offset);
350 /* There was a bug where we wrote hole nodes out with
351 csize/dsize swapped. Deal with it */
352 if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && je32_to_cpu(rd->csize))
353 tn->fn->size = je32_to_cpu(rd->csize);
354 else // normal case...
355 tn->fn->size = je32_to_cpu(rd->dsize);
357 JFFS2_DBG_READINODE("dnode @%08x: ver %u, offset %#04x, dsize %#04x\n",
358 ref_offset(ref), je32_to_cpu(rd->version), je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize));
360 jffs2_add_tn_to_tree(tn, tnp);
366 * Helper function for jffs2_get_inode_nodes().
367 * It is called every time an unknown node is found.
369 * Returns: 0 on succes;
370 * 1 if the node should be marked obsolete;
371 * negative error code on failure.
374 read_unknown(struct jffs2_sb_info *c,
375 struct jffs2_raw_node_ref *ref,
376 struct jffs2_unknown_node *un,
379 /* We don't mark unknown nodes as REF_UNCHECKED */
380 BUG_ON(ref_flags(ref) == REF_UNCHECKED);
382 un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
384 if (crc32(0, un, sizeof(struct jffs2_unknown_node) - 4) != je32_to_cpu(un->hdr_crc)) {
385 /* Hmmm. This should have been caught at scan time. */
386 JFFS2_NOTICE("node header CRC failed at %#08x. But it must have been OK earlier.\n", ref_offset(ref));
387 __jffs2_dbg_dump_node(c, ref_offset(ref));
390 switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
392 case JFFS2_FEATURE_INCOMPAT:
393 JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
394 je16_to_cpu(un->nodetype), ref_offset(ref));
399 case JFFS2_FEATURE_ROCOMPAT:
400 JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
401 je16_to_cpu(un->nodetype), ref_offset(ref));
402 BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
405 case JFFS2_FEATURE_RWCOMPAT_COPY:
406 JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
407 je16_to_cpu(un->nodetype), ref_offset(ref));
410 case JFFS2_FEATURE_RWCOMPAT_DELETE:
411 JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
412 je16_to_cpu(un->nodetype), ref_offset(ref));
420 /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
421 with this ino, returning the former in order of version */
423 static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
424 struct rb_root *tnp, struct jffs2_full_dirent **fdp,
425 uint32_t *highest_version, uint32_t *latest_mctime,
426 uint32_t *mctime_ver)
428 struct jffs2_raw_node_ref *ref, *valid_ref;
429 struct rb_root ret_tn = RB_ROOT;
430 struct jffs2_full_dirent *ret_fd = NULL;
431 union jffs2_node_union node;
437 JFFS2_DBG_READINODE("ino #%u\n", f->inocache->ino);
439 spin_lock(&c->erase_completion_lock);
441 valid_ref = jffs2_first_valid_node(f->inocache->nodes);
443 if (!valid_ref && (f->inocache->ino != 1))
444 JFFS2_WARNING("no valid nodes for ino #%u\n", f->inocache->ino);
447 /* We can hold a pointer to a non-obsolete node without the spinlock,
448 but _obsolete_ nodes may disappear at any time, if the block
449 they're in gets erased. So if we mark 'ref' obsolete while we're
450 not holding the lock, it can go away immediately. For that reason,
451 we find the next valid node first, before processing 'ref'.
454 valid_ref = jffs2_first_valid_node(ref->next_in_ino);
455 spin_unlock(&c->erase_completion_lock);
460 err = jffs2_flash_read(c, (ref_offset(ref)),
461 min_t(uint32_t, ref_totlen(c, NULL, ref), sizeof(node)),
462 &retlen, (void *)&node);
464 JFFS2_ERROR("error %d reading node at 0x%08x in get_inode_nodes()\n", err, ref_offset(ref));
468 switch (je16_to_cpu(node.u.nodetype)) {
470 case JFFS2_NODETYPE_DIRENT:
471 JFFS2_DBG_READINODE("node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref));
473 if (retlen < sizeof(node.d)) {
474 JFFS2_ERROR("short read dirent at %#08x\n", ref_offset(ref));
479 err = read_direntry(c, ref, &node.d, retlen, &ret_fd, latest_mctime, mctime_ver);
481 jffs2_mark_node_obsolete(c, ref);
483 } else if (unlikely(err))
486 if (je32_to_cpu(node.d.version) > *highest_version)
487 *highest_version = je32_to_cpu(node.d.version);
491 case JFFS2_NODETYPE_INODE:
492 JFFS2_DBG_READINODE("node at %08x (%d) is a data node\n", ref_offset(ref), ref_flags(ref));
494 if (retlen < sizeof(node.i)) {
495 JFFS2_ERROR("short read dnode at %#08x\n", ref_offset(ref));
500 err = read_dnode(c, ref, &node.i, retlen, &ret_tn, latest_mctime, mctime_ver);
502 jffs2_mark_node_obsolete(c, ref);
504 } else if (unlikely(err))
507 if (je32_to_cpu(node.i.version) > *highest_version)
508 *highest_version = je32_to_cpu(node.i.version);
510 JFFS2_DBG_READINODE("version %d, highest_version now %d\n",
511 je32_to_cpu(node.i.version), *highest_version);
516 /* Check we've managed to read at least the common node header */
517 if (retlen < sizeof(struct jffs2_unknown_node)) {
518 JFFS2_ERROR("short read unknown node at %#08x\n", ref_offset(ref));
522 err = read_unknown(c, ref, &node.u, retlen);
524 jffs2_mark_node_obsolete(c, ref);
526 } else if (unlikely(err))
530 spin_lock(&c->erase_completion_lock);
533 spin_unlock(&c->erase_completion_lock);
540 jffs2_free_tmp_dnode_info_list(&ret_tn);
541 jffs2_free_full_dirent_list(ret_fd);
545 static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
546 struct jffs2_inode_info *f,
547 struct jffs2_raw_inode *latest_node)
549 struct jffs2_tmp_dnode_info *tn = NULL;
550 struct rb_root tn_list;
551 struct rb_node *rb, *repl_rb;
552 struct jffs2_full_dirent *fd_list;
553 struct jffs2_full_dnode *fn = NULL;
555 uint32_t latest_mctime, mctime_ver;
556 uint32_t mdata_ver = 0;
560 JFFS2_DBG_READINODE("ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink);
562 /* Grab all nodes relevant to this ino */
563 ret = jffs2_get_inode_nodes(c, f, &tn_list, &fd_list, &f->highest_version, &latest_mctime, &mctime_ver);
566 JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
567 if (f->inocache->state == INO_STATE_READING)
568 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
573 rb = rb_first(&tn_list);
576 tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb);
580 if (likely(tn->version >= mdata_ver)) {
581 JFFS2_DBG_READINODE("obsoleting old metadata at 0x%08x\n", ref_offset(f->metadata->raw));
582 jffs2_mark_node_obsolete(c, f->metadata->raw);
583 jffs2_free_full_dnode(f->metadata);
588 /* This should never happen. */
589 JFFS2_ERROR("Er. New metadata at 0x%08x with ver %d is actually older than previous ver %d at 0x%08x\n",
590 ref_offset(fn->raw), tn->version, mdata_ver, ref_offset(f->metadata->raw));
591 jffs2_mark_node_obsolete(c, fn->raw);
592 jffs2_free_full_dnode(fn);
593 /* Fill in latest_node from the metadata, not this one we're about to free... */
600 jffs2_add_full_dnode_to_inode(c, f, fn);
602 /* Zero-sized node at end of version list. Just a metadata update */
603 JFFS2_DBG_READINODE("metadata @%08x: ver %d\n", ref_offset(fn->raw), tn->version);
605 mdata_ver = tn->version;
609 if (rb->rb_parent && rb->rb_parent->rb_left == rb) {
610 /* We were then left-hand child of our parent. We need
611 to move our own right-hand child into our place. */
612 repl_rb = rb->rb_right;
614 repl_rb->rb_parent = rb->rb_parent;
620 /* Remove the spent tn from the tree; don't bother rebalancing
621 but put our right-hand child in our own place. */
622 if (tn->rb.rb_parent) {
623 if (tn->rb.rb_parent->rb_left == &tn->rb)
624 tn->rb.rb_parent->rb_left = repl_rb;
625 else if (tn->rb.rb_parent->rb_right == &tn->rb)
626 tn->rb.rb_parent->rb_right = repl_rb;
628 } else if (tn->rb.rb_right)
629 tn->rb.rb_right->rb_parent = NULL;
631 jffs2_free_tmp_dnode_info(tn);
633 jffs2_dbg_fragtree_paranoia_check_nolock(f);
636 /* No data nodes for this inode. */
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, f->inocache, INO_STATE_CHECKEDABSENT);
644 JFFS2_NOTICE("but it has children so we fake some modes for it\n");
646 latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
647 latest_node->version = cpu_to_je32(0);
648 latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
649 latest_node->isize = cpu_to_je32(0);
650 latest_node->gid = cpu_to_je16(0);
651 latest_node->uid = cpu_to_je16(0);
652 if (f->inocache->state == INO_STATE_READING)
653 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
657 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(*latest_node), &retlen, (void *)latest_node);
658 if (ret || retlen != sizeof(*latest_node)) {
659 JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
660 ret, retlen, sizeof(*latest_node));
661 /* FIXME: If this fails, there seems to be a memory leak. Find it. */
663 jffs2_do_clear_inode(c, f);
667 crc = crc32(0, latest_node, sizeof(*latest_node)-8);
668 if (crc != je32_to_cpu(latest_node->node_crc)) {
669 JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
670 f->inocache->ino, ref_offset(fn->raw));
672 jffs2_do_clear_inode(c, f);
676 switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
678 if (mctime_ver > je32_to_cpu(latest_node->version)) {
679 /* The times in the latest_node are actually older than
680 mctime in the latest dirent. Cheat. */
681 latest_node->ctime = latest_node->mtime = cpu_to_je32(latest_mctime);
687 /* If it was a regular file, truncate it to the latest node's isize */
688 jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
692 /* Hack to work around broken isize in old symlink code.
693 Remove this when dwmw2 comes to his senses and stops
694 symlinks from being an entirely gratuitous special
696 if (!je32_to_cpu(latest_node->isize))
697 latest_node->isize = latest_node->dsize;
699 if (f->inocache->state != INO_STATE_CHECKING) {
700 /* Symlink's inode data is the target path. Read it and
701 * keep in RAM to facilitate quick follow symlink
703 f->target = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
705 JFFS2_ERROR("can't allocate %d bytes of memory for the symlink target path cache\n", je32_to_cpu(latest_node->csize));
707 jffs2_do_clear_inode(c, f);
711 ret = jffs2_flash_read(c, ref_offset(fn->raw) + sizeof(*latest_node),
712 je32_to_cpu(latest_node->csize), &retlen, (char *)f->target);
714 if (ret || retlen != je32_to_cpu(latest_node->csize)) {
715 if (retlen != je32_to_cpu(latest_node->csize))
720 jffs2_do_clear_inode(c, f);
724 f->target[je32_to_cpu(latest_node->csize)] = '\0';
725 JFFS2_DBG_READINODE("symlink's target '%s' cached\n", f->target);
728 /* fall through... */
732 /* Certain inode types should have only one data node, and it's
733 kept as the metadata node */
735 JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
736 f->inocache->ino, jemode_to_cpu(latest_node->mode));
738 jffs2_do_clear_inode(c, f);
741 if (!frag_first(&f->fragtree)) {
742 JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
743 f->inocache->ino, jemode_to_cpu(latest_node->mode));
745 jffs2_do_clear_inode(c, f);
748 /* ASSERT: f->fraglist != NULL */
749 if (frag_next(frag_first(&f->fragtree))) {
750 JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
751 f->inocache->ino, jemode_to_cpu(latest_node->mode));
752 /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
754 jffs2_do_clear_inode(c, f);
757 /* OK. We're happy */
758 f->metadata = frag_first(&f->fragtree)->node;
759 jffs2_free_node_frag(frag_first(&f->fragtree));
760 f->fragtree = RB_ROOT;
763 if (f->inocache->state == INO_STATE_READING)
764 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
769 /* Scan the list of all nodes present for this ino, build map of versions, etc. */
770 int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
771 uint32_t ino, struct jffs2_raw_inode *latest_node)
773 JFFS2_DBG_READINODE("read inode #%u\n", ino);
776 spin_lock(&c->inocache_lock);
777 f->inocache = jffs2_get_ino_cache(c, ino);
780 /* Check its state. We may need to wait before we can use it */
781 switch(f->inocache->state) {
782 case INO_STATE_UNCHECKED:
783 case INO_STATE_CHECKEDABSENT:
784 f->inocache->state = INO_STATE_READING;
787 case INO_STATE_CHECKING:
789 /* If it's in either of these states, we need
790 to wait for whoever's got it to finish and
792 JFFS2_DBG_READINODE("waiting for ino #%u in state %d\n", ino, f->inocache->state);
793 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
796 case INO_STATE_READING:
797 case INO_STATE_PRESENT:
798 /* Eep. This should never happen. It can
799 happen if Linux calls read_inode() again
800 before clear_inode() has finished though. */
801 JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
802 /* Fail. That's probably better than allowing it to succeed */
810 spin_unlock(&c->inocache_lock);
812 if (!f->inocache && ino == 1) {
813 /* Special case - no root inode on medium */
814 f->inocache = jffs2_alloc_inode_cache();
816 JFFS2_ERROR("cannot allocate inocache for root inode\n");
819 JFFS2_DBG_READINODE("creating inocache for root inode\n");
820 memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
821 f->inocache->ino = f->inocache->nlink = 1;
822 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
823 f->inocache->state = INO_STATE_READING;
824 jffs2_add_ino_cache(c, f->inocache);
827 JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
831 return jffs2_do_read_inode_internal(c, f, latest_node);
834 int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
836 struct jffs2_raw_inode n;
837 struct jffs2_inode_info *f = kmalloc(sizeof(*f), GFP_KERNEL);
843 memset(f, 0, sizeof(*f));
844 init_MUTEX_LOCKED(&f->sem);
847 ret = jffs2_do_read_inode_internal(c, f, &n);
850 jffs2_do_clear_inode(c, f);
856 void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
858 struct jffs2_full_dirent *fd, *fds;
862 deleted = f->inocache && !f->inocache->nlink;
864 if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
865 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
869 jffs2_mark_node_obsolete(c, f->metadata->raw);
870 jffs2_free_full_dnode(f->metadata);
873 jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
884 jffs2_free_full_dirent(fd);
887 if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
888 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
889 if (f->inocache->nodes == (void *)f->inocache)
890 jffs2_del_ino_cache(c, f->inocache);