Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * JFFS2 -- Journalling Flash File System, Version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright © 2001-2007 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Created by David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * For licensing information, see the file 'LICENCE' in this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "nodelist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 				     struct jffs2_node_frag *this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct jffs2_full_dirent **prev = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	while ((*prev) && (*prev)->nhash <= new->nhash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 			/* Duplicate. Free one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 			if (new->version < (*prev)->version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 				dbg_dentlist("Eep! Marking new dirent node obsolete, old is \"%s\", ino #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 					(*prev)->name, (*prev)->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 				jffs2_mark_node_obsolete(c, new->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 				jffs2_free_full_dirent(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 				dbg_dentlist("marking old dirent \"%s\", ino #%u obsolete\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 					(*prev)->name, (*prev)->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				new->next = (*prev)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 				/* It may have been a 'placeholder' deletion dirent, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 				   if jffs2_can_mark_obsolete() (see jffs2_do_unlink()) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 				if ((*prev)->raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 					jffs2_mark_node_obsolete(c, ((*prev)->raw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				jffs2_free_full_dirent(*prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 				*prev = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		prev = &((*prev)->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	new->next = *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	*prev = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) uint32_t jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/* We know frag->ofs <= size. That's what lookup does for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (frag && frag->ofs != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (frag->ofs+frag->size > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			frag->size = size - frag->ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		frag = frag_next(frag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	while (frag && frag->ofs >= size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		struct jffs2_node_frag *next = frag_next(frag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		frag_erase(frag, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		jffs2_obsolete_node_frag(c, frag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		frag = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	frag = frag_last(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* Sanity check for truncation to longer than we started with... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (!frag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (frag->ofs + frag->size < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return frag->ofs + frag->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/* If the last fragment starts at the RAM page boundary, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * REF_PRISTINE irrespective of its size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (frag->node && (frag->ofs & (PAGE_SIZE - 1)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			frag->ofs, frag->ofs + frag->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				     struct jffs2_node_frag *this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (this->node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		this->node->frags--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		if (!this->node->frags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			/* The node has no valid frags left. It's totally obsoleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			jffs2_mark_node_obsolete(c, this->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			jffs2_free_full_dnode(this->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			mark_ref_normal(this->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	jffs2_free_node_frag(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct rb_node *parent = &base->rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct rb_node **link = &parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	while (*link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		parent = *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		base = rb_entry(parent, struct jffs2_node_frag, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (newfrag->ofs > base->ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			link = &base->rb.rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		else if (newfrag->ofs < base->ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			link = &base->rb.rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	rb_link_node(&newfrag->rb, &base->rb, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * Allocate and initializes a new fragment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct jffs2_node_frag *newfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	newfrag = jffs2_alloc_node_frag();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (likely(newfrag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		newfrag->ofs = ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		newfrag->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		newfrag->node = fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return newfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * Called when there is no overlapping fragment exist. Inserts a hole before the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * fragment and inserts the new fragment to the fragtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		 	       struct jffs2_node_frag *newfrag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			       struct jffs2_node_frag *this, uint32_t lastend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (lastend < newfrag->node->ofs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		/* put a hole in before the new fragment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		struct jffs2_node_frag *holefrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (unlikely(!holefrag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			jffs2_free_node_frag(newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			/* By definition, the 'this' node has no right-hand child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			   because there are no frags with offset greater than it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			   So that's where we want to put the hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				holefrag->ofs, holefrag->ofs + holefrag->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				holefrag->ofs, holefrag->ofs + holefrag->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			rb_link_node(&holefrag->rb, NULL, &root->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		rb_insert_color(&holefrag->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		this = holefrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		/* By definition, the 'this' node has no right-hand child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		   because there are no frags with offset greater than it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		   So that's where we want to put new fragment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		dbg_fragtree2("add the new node at the right\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		dbg_fragtree2("insert the new node at the root of the tree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		rb_link_node(&newfrag->rb, NULL, &root->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	rb_insert_color(&newfrag->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* Doesn't set inode->i_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct jffs2_node_frag *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	uint32_t lastend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/* Skip all the nodes which are completed before this one starts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			  this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		lastend = this->ofs + this->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		dbg_fragtree2("lookup gave no frag\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		lastend = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* See if we ran off the end of the fragtree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (lastend <= newfrag->ofs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		/* We did */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		/* Check if 'this' node was on the same page as the new node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		   If so, both 'this' and the new node get marked REF_NORMAL so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		   the GC can take a look.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (lastend && (lastend-1) >> PAGE_SHIFT == newfrag->ofs >> PAGE_SHIFT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			if (this->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				mark_ref_normal(this->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			mark_ref_normal(newfrag->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return no_overlapping_node(c, root, newfrag, this, lastend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (this->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		this->ofs, this->ofs + this->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		ref_offset(this->node->raw), ref_flags(this->node->raw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		dbg_fragtree2("dealing with hole frag %u-%u.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		this->ofs, this->ofs + this->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	/* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (newfrag->ofs > this->ofs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		/* This node isn't completely obsoleted. The start of it remains valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		/* Mark the new node and the partially covered node REF_NORMAL -- let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		   the GC take a look at them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		mark_ref_normal(newfrag->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (this->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			mark_ref_normal(this->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			/* The new node splits 'this' frag into two */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			struct jffs2_node_frag *newfrag2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			if (this->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 					this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 					this->ofs, this->ofs+this->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			/* New second frag pointing to this's node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 						this->ofs + this->size - newfrag->ofs - newfrag->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			if (unlikely(!newfrag2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			if (this->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				this->node->frags++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			/* Adjust size of original 'this' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			this->size = newfrag->ofs - this->ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			/* Now, we know there's no node with offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			   greater than this->ofs but smaller than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			   newfrag2->ofs or newfrag->ofs, for obvious
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			   reasons. So we can do a tree insert from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			   'this' to insert newfrag, and a tree insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			   from newfrag to insert newfrag2. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			jffs2_fragtree_insert(newfrag, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			rb_insert_color(&newfrag->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			jffs2_fragtree_insert(newfrag2, newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			rb_insert_color(&newfrag2->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		/* New node just reduces 'this' frag in size, doesn't split it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		this->size = newfrag->ofs - this->ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		/* Again, we know it lives down here in the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		jffs2_fragtree_insert(newfrag, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		rb_insert_color(&newfrag->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		/* New frag starts at the same point as 'this' used to. Replace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		   it in the tree without doing a delete and insertion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			  newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		rb_replace_node(&this->rb, &newfrag->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			jffs2_obsolete_node_frag(c, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			this->ofs += newfrag->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			this->size -= newfrag->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			jffs2_fragtree_insert(this, newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			rb_insert_color(&this->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	/* OK, now we have newfrag added in the correct place in the tree, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	   frag_next(newfrag) may be a fragment which is overlapped by it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		/* 'this' frag is obsoleted completely. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			this, this->ofs, this->ofs+this->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		rb_erase(&this->rb, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		jffs2_obsolete_node_frag(c, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/* Now we're pointing at the first frag which isn't totally obsoleted by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	   the new frag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (!this || newfrag->ofs + newfrag->size == this->ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	/* Still some overlap but we don't need to move it in the tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	this->ofs = newfrag->ofs + newfrag->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	/* And mark them REF_NORMAL so the GC takes a look at them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (this->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		mark_ref_normal(this->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	mark_ref_normal(newfrag->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  * Given an inode, probably with existing tree of fragments, add the new node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * to the fragment tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct jffs2_node_frag *newfrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (unlikely(!fn->size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	newfrag = new_fragment(fn, fn->ofs, fn->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (unlikely(!newfrag))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	newfrag->node->frags = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		  fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* If we now share a page with other nodes, mark either previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	   or next node REF_NORMAL, as appropriate.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (newfrag->ofs & (PAGE_SIZE-1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		struct jffs2_node_frag *prev = frag_prev(newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		mark_ref_normal(fn->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		/* If we don't start at zero there's _always_ a previous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		if (prev->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			mark_ref_normal(prev->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if ((newfrag->ofs+newfrag->size) & (PAGE_SIZE-1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		struct jffs2_node_frag *next = frag_next(newfrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		if (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			mark_ref_normal(fn->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			if (next->node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 				mark_ref_normal(next->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	jffs2_dbg_fragtree_paranoia_check_nolock(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	spin_lock(&c->inocache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	ic->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	wake_up(&c->inocache_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	spin_unlock(&c->inocache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* During mount, this needs no locking. During normal operation, its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)    callers want to do other stuff while still holding the inocache_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)    Rather than introducing special case get_ino_cache functions or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)    callbacks, we just let the caller do the locking itself. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct jffs2_inode_cache *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	ret = c->inocache_list[ino % c->inocache_hashsize];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	while (ret && ret->ino < ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		ret = ret->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (ret && ret->ino != ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	struct jffs2_inode_cache **prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	spin_lock(&c->inocache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (!new->ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		new->ino = ++c->highest_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	dbg_inocache("add %p (ino #%u)\n", new, new->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	prev = &c->inocache_list[new->ino % c->inocache_hashsize];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	while ((*prev) && (*prev)->ino < new->ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		prev = &(*prev)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	new->next = *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	*prev = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	spin_unlock(&c->inocache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct jffs2_inode_cache **prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) #ifdef CONFIG_JFFS2_FS_XATTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	BUG_ON(old->xref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	dbg_inocache("del %p (ino #%u)\n", old, old->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	spin_lock(&c->inocache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	prev = &c->inocache_list[old->ino % c->inocache_hashsize];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	while ((*prev) && (*prev)->ino < old->ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		prev = &(*prev)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if ((*prev) == old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		*prev = old->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	/* Free it now unless it's in READING or CLEARING state, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	   are the transitions upon read_inode() and clear_inode(). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	   rest of the time we know nobody else is looking at it, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	   if it's held by read_inode() or clear_inode() they'll free it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	   for themselves. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		jffs2_free_inode_cache(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	spin_unlock(&c->inocache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) void jffs2_free_ino_caches(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	struct jffs2_inode_cache *this, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	for (i=0; i < c->inocache_hashsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		this = c->inocache_list[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		while (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			next = this->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			jffs2_xattr_free_inode(c, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			jffs2_free_inode_cache(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			this = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		c->inocache_list[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct jffs2_raw_node_ref *this, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	for (i=0; i<c->nr_blocks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		this = c->blocks[i].first_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		while (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 				next = this[REFS_PER_BLOCK].next_in_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 				next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			jffs2_free_refblock(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			this = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		c->blocks[i].first_node = c->blocks[i].last_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	/* The common case in lookup is that there will be a node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	   which precisely matches. So we go looking for that first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	struct rb_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	struct jffs2_node_frag *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	struct jffs2_node_frag *frag = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	next = fragtree->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	while(next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		frag = rb_entry(next, struct jffs2_node_frag, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		if (frag->ofs + frag->size <= offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			/* Remember the closest smaller match on the way down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			if (!prev || frag->ofs > prev->ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				prev = frag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			next = frag->rb.rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		} else if (frag->ofs > offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			next = frag->rb.rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			return frag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	/* Exact match not found. Go back up looking at each parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	   and return the closest smaller one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	if (prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			  prev->ofs, prev->ofs+prev->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		dbg_fragtree2("returning NULL, empty fragtree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	return prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /* Pass 'c' argument to indicate that nodes should be marked obsolete as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)    they're killed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	struct jffs2_node_frag *frag, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	dbg_fragtree("killing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	rbtree_postorder_for_each_entry_safe(frag, next, root, rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		if (frag->node && !(--frag->node->frags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			/* Not a hole, and it's the final remaining frag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			   of this node. Free the node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 				jffs2_mark_node_obsolete(c, frag->node->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			jffs2_free_full_dnode(frag->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		jffs2_free_node_frag(frag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 					       struct jffs2_eraseblock *jeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 					       uint32_t ofs, uint32_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 					       struct jffs2_inode_cache *ic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	struct jffs2_raw_node_ref *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	BUG_ON(!jeb->allocated_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	jeb->allocated_refs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	ref = jeb->last_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		    ref->next_in_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	while (ref->flash_offset != REF_EMPTY_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		if (ref->flash_offset == REF_LINK_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			ref = ref->next_in_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			ref++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		    ref->flash_offset, ofs, ref->next_in_ino, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	ref->flash_offset = ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (!jeb->first_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		jeb->first_node = ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		BUG_ON(ref_offset(ref) != jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	} else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			    ref, ref_offset(ref), ref_offset(ref)+len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			    ref_offset(jeb->last_node), 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 			    ref_offset(jeb->last_node)+last_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	jeb->last_node = ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	if (ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		ref->next_in_ino = ic->nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		ic->nodes = ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		ref->next_in_ino = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	switch(ref_flags(ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	case REF_UNCHECKED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		c->unchecked_size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		jeb->unchecked_size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	case REF_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	case REF_PRISTINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		c->used_size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		jeb->used_size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	case REF_OBSOLETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		c->dirty_size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		jeb->dirty_size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	c->free_size -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	jeb->free_size -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) #ifdef TEST_TOTLEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	/* Set (and test) __totlen field... for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	ref->__totlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	ref_totlen(c, jeb, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	return ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /* No locking, no reservation of 'ref'. Do not use on a live file system */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 			   uint32_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (unlikely(size > jeb->free_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		pr_crit("Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			size, jeb->free_size, jeb->wasted_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	/* REF_EMPTY_NODE is !obsolete, so that works OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (jeb->last_node && ref_obsolete(jeb->last_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #ifdef TEST_TOTLEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		jeb->last_node->__totlen += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		c->dirty_size += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		c->free_size -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		jeb->dirty_size += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		jeb->free_size -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		ofs |= REF_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		jffs2_link_node_ref(c, jeb, ofs, size, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /* Calculate totlen from surrounding nodes or eraseblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 				    struct jffs2_eraseblock *jeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 				    struct jffs2_raw_node_ref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	uint32_t ref_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	struct jffs2_raw_node_ref *next_ref = ref_next(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	if (next_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		ref_end = ref_offset(next_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		if (!jeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 			jeb = &c->blocks[ref->flash_offset / c->sector_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		/* Last node in block. Use free_space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		if (unlikely(ref != jeb->last_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 			pr_crit("ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 				ref, ref_offset(ref), jeb->last_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 				jeb->last_node ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 				ref_offset(jeb->last_node) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		ref_end = jeb->offset + c->sector_size - jeb->free_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	return ref_end - ref_offset(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 			    struct jffs2_raw_node_ref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	uint32_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	ret = __ref_totlen(c, jeb, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) #ifdef TEST_TOTLEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	if (unlikely(ret != ref->__totlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		if (!jeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 			jeb = &c->blocks[ref->flash_offset / c->sector_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		pr_crit("Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 			ref, ref_offset(ref), ref_offset(ref) + ref->__totlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 			ret, ref->__totlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		if (ref_next(ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 			pr_crit("next %p (0x%08x-0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 				ref_next(ref), ref_offset(ref_next(ref)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 				ref_offset(ref_next(ref)) + ref->__totlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		} else 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 			pr_crit("No next ref. jeb->last_node is %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 				jeb->last_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		pr_crit("jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 			jeb->wasted_size, jeb->dirty_size, jeb->used_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 			jeb->free_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		__jffs2_dbg_dump_node_refs_nolock(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		ret = ref->__totlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) #endif /* TEST_TOTLEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }