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/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "nodelist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * Check whether the user is allowed to write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int jffs2_rp_can_write(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	uint32_t avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct jffs2_mount_opts *opts = &c->mount_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	avail = c->dirty_size + c->free_size + c->unchecked_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		c->erasing_size - c->resv_blocks_write * c->sector_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		- c->nospc_dirty_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (avail < 2 * opts->rp_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		jffs2_dbg(1, "rpsize %u, dirty_size %u, free_size %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 			  "erasing_size %u, unchecked_size %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			  "nr_erasing_blocks %u, avail %u, resrv %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			  opts->rp_size, c->dirty_size, c->free_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			  c->erasing_size, c->unchecked_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			  c->nr_erasing_blocks, avail, c->nospc_dirty_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (avail > opts->rp_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	/* Always allow root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (capable(CAP_SYS_RESOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	jffs2_dbg(1, "forbid writing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *	jffs2_reserve_space - request physical space to write nodes to flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *	@c: superblock info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *	@minsize: Minimum acceptable size of allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  *	@len: Returned value of allocation length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *	@prio: Allocation type - ALLOC_{NORMAL,DELETION}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *	Requests a block of physical space on the flash. Returns zero for success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *	and puts 'len' into the appropriate place, or returns -ENOSPC or other 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *	error if appropriate. Doesn't return len since that's 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  *	If it returns zero, jffs2_reserve_space() also downs the per-filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *	allocation semaphore, to prevent more than one allocation from being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  *	active at any time. The semaphore is later released by jffs2_commit_allocation()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *	jffs2_reserve_space() may trigger garbage collection in order to make room
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *	for the requested allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int jffs2_do_reserve_space(struct jffs2_sb_info *c,  uint32_t minsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				  uint32_t *len, uint32_t sumsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			uint32_t *len, int prio, uint32_t sumsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int blocksneeded = c->resv_blocks_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* align it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	minsize = PAD(minsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	jffs2_dbg(1, "%s(): Requested 0x%x bytes\n", __func__, minsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	mutex_lock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	jffs2_dbg(1, "%s(): alloc sem got\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * Check if the free space is greater then size of the reserved pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * If not, only allow root to proceed with writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (prio != ALLOC_DELETION && !jffs2_rp_can_write(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/* this needs a little more thought (true <tglx> :)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	while(ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			uint32_t dirty, avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			/* calculate real dirty size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			 * dirty_size contains blocks on erase_pending_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			 * those blocks are counted in c->nr_erasing_blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			 * If one block is actually erased, it is not longer counted as dirty_space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			 * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			 * with c->nr_erasing_blocks * c->sector_size again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			 * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			 * This helps us to force gc and pick eventually a clean block to spread the load.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			 * We add unchecked_size here, as we hopefully will find some space to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			 * This will affect the sum only once, as gc first finishes checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			 * of nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size + c->unchecked_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			if (dirty < c->nospc_dirty_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 					jffs2_dbg(1, "%s(): Low on dirty space to GC, but it's a deletion. Allowing...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 						  __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				jffs2_dbg(1, "dirty size 0x%08x + unchecked_size 0x%08x < nospc_dirty_size 0x%08x, returning -ENOSPC\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 					  dirty, c->unchecked_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 					  c->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				mutex_unlock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			/* Calc possibly available space. Possibly available means that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			 * don't know, if unchecked size contains obsoleted nodes, which could give us some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			 * more usable space. This will affect the sum only once, as gc first finishes checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 * of nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 + Return -ENOSPC, if the maximum possibly available space is less or equal than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			 * blocksneeded * sector_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			 * This blocks endless gc looping on a filesystem, which is nearly full, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			 * the check above passes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			avail = c->free_size + c->dirty_size + c->erasing_size + c->unchecked_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			if ( (avail / c->sector_size) <= blocksneeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 				if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 					jffs2_dbg(1, "%s(): Low on possibly available space, but it's a deletion. Allowing...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 						  __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				jffs2_dbg(1, "max. available size 0x%08x  < blocksneeded * sector_size 0x%08x, returning -ENOSPC\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 					  avail, blocksneeded * c->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				mutex_unlock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			mutex_unlock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			jffs2_dbg(1, "Triggering GC pass. nr_free_blocks %d, nr_erasing_blocks %d, free_size 0x%08x, dirty_size 0x%08x, wasted_size 0x%08x, used_size 0x%08x, erasing_size 0x%08x, bad_size 0x%08x (total 0x%08x of 0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				  c->nr_free_blocks, c->nr_erasing_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				  c->free_size, c->dirty_size, c->wasted_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				  c->used_size, c->erasing_size, c->bad_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				  c->free_size + c->dirty_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				  c->wasted_size + c->used_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				  c->erasing_size + c->bad_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				  c->flash_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			ret = jffs2_garbage_collect_pass(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				if (c->nr_erasing_blocks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				    list_empty(&c->erase_pending_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				    list_empty(&c->erase_complete_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 					set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 					add_wait_queue(&c->erase_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 					jffs2_dbg(1, "%s waiting for erase to complete\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 						  __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 					spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 					remove_wait_queue(&c->erase_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 					spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			} else if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			mutex_lock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		ret = jffs2_do_reserve_space(c, minsize, len, sumsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			jffs2_dbg(1, "%s(): ret is %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		mutex_unlock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			   uint32_t *len, uint32_t sumsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	minsize = PAD(minsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	jffs2_dbg(1, "%s(): Requested 0x%x bytes\n", __func__, minsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		ret = jffs2_do_reserve_space(c, minsize, len, sumsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			jffs2_dbg(1, "%s(): looping, ret is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				  __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* Classify nextblock (clean, dirty of verydirty) and force to select an other one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static void jffs2_close_nextblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
^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) 	if (c->nextblock == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		jffs2_dbg(1, "%s(): Erase block at 0x%08x has already been placed in a list\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			  __func__, jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	/* Check, if we have a dirty block now, or if it was dirty already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (ISDIRTY (jeb->wasted_size + jeb->dirty_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		c->dirty_size += jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		c->wasted_size -= jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		jeb->dirty_size += jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		jeb->wasted_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		if (VERYDIRTY(c, jeb->dirty_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			jffs2_dbg(1, "Adding full erase block at 0x%08x to very_dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				  jeb->offset, jeb->free_size, jeb->dirty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				  jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			list_add_tail(&jeb->list, &c->very_dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			jffs2_dbg(1, "Adding full erase block at 0x%08x to dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 				  jeb->offset, jeb->free_size, jeb->dirty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				  jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			list_add_tail(&jeb->list, &c->dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		jffs2_dbg(1, "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			  jeb->offset, jeb->free_size, jeb->dirty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			  jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		list_add_tail(&jeb->list, &c->clean_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	c->nextblock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* Select a new jeb for nextblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int jffs2_find_nextblock(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct list_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	/* Take the next block off the 'free' list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (list_empty(&c->free_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (!c->nr_erasing_blocks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			!list_empty(&c->erasable_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			struct jffs2_eraseblock *ejeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			ejeb = list_entry(c->erasable_list.next, struct jffs2_eraseblock, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			list_move_tail(&ejeb->list, &c->erase_pending_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			c->nr_erasing_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			jffs2_garbage_collect_trigger(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			jffs2_dbg(1, "%s(): Triggering erase of erasable block at 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 				  __func__, ejeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (!c->nr_erasing_blocks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			!list_empty(&c->erasable_pending_wbuf_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			jffs2_dbg(1, "%s(): Flushing write buffer\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				  __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			/* c->nextblock is NULL, no update to c->nextblock allowed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			jffs2_flush_wbuf_pad(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			/* Have another go. It'll be on the erasable_list now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (!c->nr_erasing_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			/* Ouch. We're in GC, or we wouldn't have got here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			   And there's no space left. At all. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			pr_crit("Argh. No free space left for GC. nr_erasing_blocks is %d. nr_free_blocks is %d. (erasableempty: %s, erasingempty: %s, erasependingempty: %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				c->nr_erasing_blocks, c->nr_free_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				list_empty(&c->erasable_list) ? "yes" : "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 				list_empty(&c->erasing_list) ? "yes" : "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				list_empty(&c->erase_pending_list) ? "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		/* Don't wait for it; just erase one right now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		jffs2_erase_pending_blocks(c, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		/* An erase may have failed, decreasing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		   amount of free space available. So we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		   restart from the beginning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	next = c->free_list.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	list_del(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	c->nextblock = list_entry(next, struct jffs2_eraseblock, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	c->nr_free_blocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	jffs2_sum_reset_collected(c->summary); /* reset collected summary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	/* adjust write buffer offset, else we get a non contiguous write bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (!(c->wbuf_ofs % c->sector_size) && !c->wbuf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		c->wbuf_ofs = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		  __func__, c->nextblock->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* Called with alloc sem _and_ erase_completion_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 				  uint32_t *len, uint32_t sumsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct jffs2_eraseblock *jeb = c->nextblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	uint32_t reserved_size;				/* for summary information at the end of the jeb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	reserved_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (jffs2_sum_active() && (sumsize != JFFS2_SUMMARY_NOSUM_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 							/* NOSUM_SIZE means not to generate summary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		if (jeb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			dbg_summary("minsize=%d , jeb->free=%d ,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 						"summary->size=%d , sumsize=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 						minsize, jeb->free_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 						c->summary->sum_size, sumsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		/* Is there enough space for writing out the current node, or we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		   write out summary information now, close this jeb and select new nextblock? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		if (jeb && (PAD(minsize) + PAD(c->summary->sum_size + sumsize +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 					JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			/* Has summary been disabled for this jeb? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			if (jffs2_sum_is_disabled(c->summary)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				sumsize = JFFS2_SUMMARY_NOSUM_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			/* Writing out the collected summary information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			dbg_summary("generating summary for 0x%08x.\n", jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			ret = jffs2_sum_write_sumnode(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			if (jffs2_sum_is_disabled(c->summary)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				/* jffs2_write_sumnode() couldn't write out the summary information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				   diabling summary for this jeb and free the collected information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 				sumsize = JFFS2_SUMMARY_NOSUM_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 				goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			jffs2_close_nextblock(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			jeb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			/* keep always valid value in reserved_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		if (jeb && minsize > jeb->free_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			uint32_t waste;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			/* Skip the end of this block and file it as having some dirty space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			/* If there's a pending write to it, flush now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			if (jffs2_wbuf_dirty(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 				spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				jffs2_dbg(1, "%s(): Flushing write buffer\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 					  __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				jffs2_flush_wbuf_pad(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 				spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 				jeb = c->nextblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 				goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			/* Just lock it again and continue. Nothing much can change because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			   we hold c->alloc_sem anyway. In fact, it's not entirely clear why
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			   we hold c->erase_completion_lock in the majority of this function...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			   but that's a question for another (more caffeine-rich) day. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			waste = jeb->free_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			jffs2_link_node_ref(c, jeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 					    (jeb->offset + c->sector_size - waste) | REF_OBSOLETE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 					    waste, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			/* FIXME: that made it count as dirty. Convert to wasted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			jeb->dirty_size -= waste;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			c->dirty_size -= waste;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			jeb->wasted_size += waste;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			c->wasted_size += waste;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			jffs2_close_nextblock(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			jeb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (!jeb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		ret = jffs2_find_nextblock(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		jeb = c->nextblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		if (jeb->free_size != c->sector_size - c->cleanmarker_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			pr_warn("Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 				jeb->offset, jeb->free_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	/* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	   enough space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	*len = jeb->free_size - reserved_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	    !jeb->first_node->next_in_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		/* Only node in it beforehand was a CLEANMARKER node (we think).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		   So mark it obsolete now that there's going to be another node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		   in the block. This will reduce used_size to zero but We've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		   already set c->nextblock so that jffs2_mark_node_obsolete()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		   won't try to refile it to the dirty_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		jffs2_mark_node_obsolete(c, jeb->first_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	jffs2_dbg(1, "%s(): Giving 0x%x bytes at 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		  __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		  *len, jeb->offset + (c->sector_size - jeb->free_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  *	jffs2_add_physical_node_ref - add a physical node reference to the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  *	@c: superblock info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  *	@new: new node reference to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  *	@len: length of this physical node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  *	Should only be used to report nodes for which space has been allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  *	by jffs2_reserve_space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  *	Must be called with the alloc_sem held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct jffs2_raw_node_ref *jffs2_add_physical_node_ref(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 						       uint32_t ofs, uint32_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 						       struct jffs2_inode_cache *ic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	struct jffs2_eraseblock *jeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	struct jffs2_raw_node_ref *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	jeb = &c->blocks[ofs / c->sector_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	jffs2_dbg(1, "%s(): Node at 0x%x(%d), size 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		  __func__, ofs & ~3, ofs & 3, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) #if 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/* Allow non-obsolete nodes only to be added at the end of c->nextblock, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	   if c->nextblock is set. Note that wbuf.c will file obsolete nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	   even after refiling c->nextblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	    && (jeb != c->nextblock || (ofs & ~3) != jeb->offset + (c->sector_size - jeb->free_size))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		pr_warn("argh. node added in wrong place at 0x%08x(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			ofs & ~3, ofs & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		if (c->nextblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			pr_warn("nextblock 0x%08x", c->nextblock->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			pr_warn("No nextblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		pr_cont(", expected at %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			jeb->offset + (c->sector_size - jeb->free_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	new = jffs2_link_node_ref(c, jeb, ofs, len, ic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		/* If it lives on the dirty_list, jffs2_reserve_space will put it there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		jffs2_dbg(1, "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			  jeb->offset, jeb->free_size, jeb->dirty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			  jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		if (jffs2_wbuf_dirty(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			/* Flush the last write in the block if it's outstanding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			jffs2_flush_wbuf_pad(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		list_add_tail(&jeb->list, &c->clean_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		c->nextblock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	jffs2_dbg_acct_sanity_check_nolock(c,jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) void jffs2_complete_reservation(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	jffs2_dbg(1, "jffs2_complete_reservation()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	jffs2_garbage_collect_trigger(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	mutex_unlock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static inline int on_list(struct list_head *obj, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	struct list_head *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	list_for_each(this, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		if (this == obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 			jffs2_dbg(1, "%p is on list at %p\n", obj, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	struct jffs2_eraseblock *jeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	int blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	struct jffs2_unknown_node n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	int ret, addedsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	uint32_t freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if(unlikely(!ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		pr_notice("EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	if (ref_obsolete(ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		jffs2_dbg(1, "%s(): called with already obsolete node at 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			  __func__, ref_offset(ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	blocknr = ref->flash_offset / c->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	if (blocknr >= c->nr_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		pr_notice("raw node at 0x%08x is off the end of device!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 			  ref->flash_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	jeb = &c->blocks[blocknr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (jffs2_can_mark_obsolete(c) && !jffs2_is_readonly(c) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	    !(c->flags & (JFFS2_SB_FLAG_SCANNING | JFFS2_SB_FLAG_BUILDING))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		/* Hm. This may confuse static lock analysis. If any of the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		   three conditions is false, we're going to return from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		   function without actually obliterating any nodes or freeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		   any jffs2_raw_node_refs. So we don't need to stop erases from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		   happening, or protect against people holding an obsolete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		   jffs2_raw_node_ref without the erase_completion_lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		mutex_lock(&c->erase_free_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	freed_len = ref_totlen(c, jeb, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (ref_flags(ref) == REF_UNCHECKED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		D1(if (unlikely(jeb->unchecked_size < freed_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 				pr_notice("raw unchecked node of size 0x%08x freed from erase block %d at 0x%08x, but unchecked_size was already 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 					  freed_len, blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 					  ref->flash_offset, jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 			jffs2_dbg(1, "Obsoleting previously unchecked node at 0x%08x of len %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 				  ref_offset(ref), freed_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		jeb->unchecked_size -= freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		c->unchecked_size -= freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		D1(if (unlikely(jeb->used_size < freed_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 				pr_notice("raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 					  freed_len, blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 					  ref->flash_offset, jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			jffs2_dbg(1, "Obsoleting node at 0x%08x of len %#x: ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 				  ref_offset(ref), freed_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		jeb->used_size -= freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		c->used_size -= freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	// Take care, that wasted size is taken into concern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + freed_len)) && jeb != c->nextblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		jffs2_dbg(1, "Dirtying\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		addedsize = freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		jeb->dirty_size += freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		c->dirty_size += freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		/* Convert wasted space to dirty, if not a bad block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		if (jeb->wasted_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			if (on_list(&jeb->list, &c->bad_used_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 				jffs2_dbg(1, "Leaving block at %08x on the bad_used_list\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 					  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 				addedsize = 0; /* To fool the refiling code later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 				jffs2_dbg(1, "Converting %d bytes of wasted space to dirty in block at %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 					  jeb->wasted_size, jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 				addedsize += jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 				jeb->dirty_size += jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 				c->dirty_size += jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 				c->wasted_size -= jeb->wasted_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 				jeb->wasted_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		jffs2_dbg(1, "Wasting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		addedsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		jeb->wasted_size += freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		c->wasted_size += freed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	if (c->flags & JFFS2_SB_FLAG_SCANNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		/* Flash scanning is in progress. Don't muck about with the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		   lists because they're not ready yet, and don't actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		   obliterate nodes that look obsolete. If they weren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		   marked obsolete on the flash at the time they _became_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		   obsolete, there was probably a reason for that. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		/* We didn't lock the erase_free_sem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	if (jeb == c->nextblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		jffs2_dbg(2, "Not moving nextblock 0x%08x to dirty/erase_pending list\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 			  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	} else if (!jeb->used_size && !jeb->unchecked_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		if (jeb == c->gcblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 			jffs2_dbg(1, "gcblock at 0x%08x completely dirtied. Clearing gcblock...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 				  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 			c->gcblock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 			jffs2_dbg(1, "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 				  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			list_del(&jeb->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		if (jffs2_wbuf_dirty(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 			jffs2_dbg(1, "...and adding to erasable_pending_wbuf_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 			list_add_tail(&jeb->list, &c->erasable_pending_wbuf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			if (jiffies & 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 				/* Most of the time, we just erase it immediately. Otherwise we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 				   spend ages scanning it on mount, etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 				jffs2_dbg(1, "...and adding to erase_pending_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 				list_add_tail(&jeb->list, &c->erase_pending_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 				c->nr_erasing_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 				jffs2_garbage_collect_trigger(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 				/* Sometimes, however, we leave it elsewhere so it doesn't get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 				   immediately reused, and we spread the load a bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 				jffs2_dbg(1, "...and adding to erasable_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 				list_add_tail(&jeb->list, &c->erasable_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		jffs2_dbg(1, "Done OK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	} else if (jeb == c->gcblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		jffs2_dbg(2, "Not moving gcblock 0x%08x to dirty_list\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 			  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	} else if (ISDIRTY(jeb->dirty_size) && !ISDIRTY(jeb->dirty_size - addedsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		jffs2_dbg(1, "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 			  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		list_del(&jeb->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		jffs2_dbg(1, "...and adding to dirty_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		list_add_tail(&jeb->list, &c->dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	} else if (VERYDIRTY(c, jeb->dirty_size) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		   !VERYDIRTY(c, jeb->dirty_size - addedsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		jffs2_dbg(1, "Eraseblock at 0x%08x is now very dirty. Removing from dirty list...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 			  jeb->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		list_del(&jeb->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		jffs2_dbg(1, "...and adding to very_dirty_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		list_add_tail(&jeb->list, &c->very_dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		jffs2_dbg(1, "Eraseblock at 0x%08x not moved anywhere. (free 0x%08x, dirty 0x%08x, used 0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			  jeb->offset, jeb->free_size, jeb->dirty_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 			  jeb->used_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	if (!jffs2_can_mark_obsolete(c) || jffs2_is_readonly(c) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		(c->flags & JFFS2_SB_FLAG_BUILDING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 		/* We didn't lock the erase_free_sem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	/* The erase_free_sem is locked, and has been since before we marked the node obsolete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	   and potentially put its eraseblock onto the erase_pending_list. Thus, we know that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	   the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	   by jffs2_free_jeb_node_refs() in erase.c. Which is nice. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	jffs2_dbg(1, "obliterating obsoleted node at 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		  ref_offset(ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		pr_warn("Read error reading from obsoleted node at 0x%08x: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 			ref_offset(ref), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		goto out_erase_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	if (retlen != sizeof(n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		pr_warn("Short read from obsoleted node at 0x%08x: %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 			ref_offset(ref), retlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 		goto out_erase_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	if (PAD(je32_to_cpu(n.totlen)) != PAD(freed_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		pr_warn("Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 			je32_to_cpu(n.totlen), freed_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 		goto out_erase_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 		jffs2_dbg(1, "Node at 0x%08x was already marked obsolete (nodetype 0x%04x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 			  ref_offset(ref), je16_to_cpu(n.nodetype));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		goto out_erase_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	/* XXX FIXME: This is ugly now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		pr_warn("Write error in obliterating obsoleted node at 0x%08x: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 			ref_offset(ref), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		goto out_erase_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	if (retlen != sizeof(n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 		pr_warn("Short write in obliterating obsoleted node at 0x%08x: %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 			ref_offset(ref), retlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		goto out_erase_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	/* Nodes which have been marked obsolete no longer need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	   associated with any inode. Remove them from the per-inode list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	   Note we can't do this for NAND at the moment because we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	   obsolete dirent nodes to stay on the lists, because of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	   horridness in jffs2_garbage_collect_deletion_dirent(). Also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	   because we delete the inocache, and on NAND we need that to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	   stay around until all the nodes are actually erased, in order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	   to stop us from giving the same inode number to another newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	   created inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	if (ref->next_in_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		struct jffs2_inode_cache *ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		struct jffs2_raw_node_ref **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		spin_lock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 		ic = jffs2_raw_ref_to_ic(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		for (p = &ic->nodes; (*p) != ref; p = &((*p)->next_in_ino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		*p = ref->next_in_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		ref->next_in_ino = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 		switch (ic->class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) #ifdef CONFIG_JFFS2_FS_XATTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 			case RAWNODE_CLASS_XATTR_DATUM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 				jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 			case RAWNODE_CLASS_XATTR_REF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 				jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 				if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 					jffs2_del_ino_cache(c, ic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 		spin_unlock(&c->erase_completion_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)  out_erase_sem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	mutex_unlock(&c->erase_free_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) int jffs2_thread_should_wake(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	uint32_t dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	int nr_very_dirty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	struct jffs2_eraseblock *jeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	if (!list_empty(&c->erase_complete_list) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	    !list_empty(&c->erase_pending_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	if (c->unchecked_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, check_ino #%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 			  c->unchecked_size, c->check_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	/* dirty_size contains blocks on erase_pending_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	 * those blocks are counted in c->nr_erasing_blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	 * If one block is actually erased, it is not longer counted as dirty_space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	 * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	 * with c->nr_erasing_blocks * c->sector_size again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	 * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	 * This helps us to force gc and pick eventually a clean block to spread the load.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 			(dirty > c->nospc_dirty_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	list_for_each_entry(jeb, &c->very_dirty_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 		nr_very_dirty++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 		if (nr_very_dirty == c->vdirty_blocks_gctrigger) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 			/* In debug mode, actually go through and count them all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 			D1(continue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	jffs2_dbg(1, "%s(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x, vdirty_blocks %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		  __func__, c->nr_free_blocks, c->nr_erasing_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		  c->dirty_size, nr_very_dirty, ret ? "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }