Blame view

fs/jffs2/nodemgmt.c 27.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * JFFS2 -- Journalling Flash File System, Version 2.
   *
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
4
   * Copyright © 2001-2007 Red Hat, Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
   *
   * Created by David Woodhouse <dwmw2@infradead.org>
   *
   * For licensing information, see the file 'LICENCE' in this directory.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
   */
  
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
  #include <linux/mtd/mtd.h>
  #include <linux/compiler.h>
  #include <linux/sched.h> /* For cond_resched() */
  #include "nodelist.h"
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
17
  #include "debug.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
  
  /**
   *	jffs2_reserve_space - request physical space to write nodes to flash
   *	@c: superblock info
   *	@minsize: Minimum acceptable size of allocation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
   *	@len: Returned value of allocation length
   *	@prio: Allocation type - ALLOC_{NORMAL,DELETION}
   *
   *	Requests a block of physical space on the flash. Returns zero for success
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
27
28
   *	and puts 'len' into the appropriate place, or returns -ENOSPC or other 
   *	error if appropriate. Doesn't return len since that's 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
32
33
34
35
36
   *
   *	If it returns zero, jffs2_reserve_space() also downs the per-filesystem
   *	allocation semaphore, to prevent more than one allocation from being
   *	active at any time. The semaphore is later released by jffs2_commit_allocation()
   *
   *	jffs2_reserve_space() may trigger garbage collection in order to make room
   *	for the requested allocation.
   */
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
37
  static int jffs2_do_reserve_space(struct jffs2_sb_info *c,  uint32_t minsize,
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
38
  				  uint32_t *len, uint32_t sumsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
40
  int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
41
  			uint32_t *len, int prio, uint32_t sumsize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
45
46
47
48
49
  {
  	int ret = -EAGAIN;
  	int blocksneeded = c->resv_blocks_write;
  	/* align it */
  	minsize = PAD(minsize);
  
  	D1(printk(KERN_DEBUG "jffs2_reserve_space(): Requested 0x%x bytes
  ", minsize));
ced220703   David Woodhouse   [JFFS2] semaphore...
50
  	mutex_lock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
57
58
59
  
  	D1(printk(KERN_DEBUG "jffs2_reserve_space(): alloc sem got
  "));
  
  	spin_lock(&c->erase_completion_lock);
  
  	/* this needs a little more thought (true <tglx> :)) */
  	while(ret == -EAGAIN) {
  		while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  			uint32_t dirty, avail;
  
  			/* calculate real dirty size
  			 * dirty_size contains blocks on erase_pending_list
  			 * those blocks are counted in c->nr_erasing_blocks.
  			 * If one block is actually erased, it is not longer counted as dirty_space
  			 * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
  			 * with c->nr_erasing_blocks * c->sector_size again.
  			 * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
  			 * This helps us to force gc and pick eventually a clean block to spread the load.
  			 * We add unchecked_size here, as we hopefully will find some space to use.
  			 * This will affect the sum only once, as gc first finishes checking
  			 * of nodes.
  			 */
  			dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size + c->unchecked_size;
  			if (dirty < c->nospc_dirty_size) {
  				if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
4132ace8d   Artem B. Bityuckiy   [JFFS2] Suppress ...
77
78
  					D1(printk(KERN_NOTICE "jffs2_reserve_space(): Low on dirty space to GC, but it's a deletion. Allowing...
  "));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
81
82
83
84
85
  					break;
  				}
  				D1(printk(KERN_DEBUG "dirty size 0x%08x + unchecked_size 0x%08x < nospc_dirty_size 0x%08x, returning -ENOSPC
  ",
  					  dirty, c->unchecked_size, c->sector_size));
  
  				spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
86
  				mutex_unlock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
  				return -ENOSPC;
  			}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
89

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
  			/* Calc possibly available space. Possibly available means that we
  			 * don't know, if unchecked size contains obsoleted nodes, which could give us some
  			 * more usable space. This will affect the sum only once, as gc first finishes checking
  			 * of nodes.
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
94
  			 + Return -ENOSPC, if the maximum possibly available space is less or equal than
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
101
  			 * blocksneeded * sector_size.
  			 * This blocks endless gc looping on a filesystem, which is nearly full, even if
  			 * the check above passes.
  			 */
  			avail = c->free_size + c->dirty_size + c->erasing_size + c->unchecked_size;
  			if ( (avail / c->sector_size) <= blocksneeded) {
  				if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
4132ace8d   Artem B. Bityuckiy   [JFFS2] Suppress ...
102
103
  					D1(printk(KERN_NOTICE "jffs2_reserve_space(): Low on possibly available space, but it's a deletion. Allowing...
  "));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
109
110
  					break;
  				}
  
  				D1(printk(KERN_DEBUG "max. available size 0x%08x  < blocksneeded * sector_size 0x%08x, returning -ENOSPC
  ",
  					  avail, blocksneeded * c->sector_size));
  				spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
111
  				mutex_unlock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
  				return -ENOSPC;
  			}
ced220703   David Woodhouse   [JFFS2] semaphore...
114
  			mutex_unlock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
119
120
  
  			D1(printk(KERN_DEBUG "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)
  ",
  				  c->nr_free_blocks, c->nr_erasing_blocks, c->free_size, c->dirty_size, c->wasted_size, c->used_size, c->erasing_size, c->bad_size,
  				  c->free_size + c->dirty_size + c->wasted_size + c->used_size + c->erasing_size + c->bad_size, c->flash_size));
  			spin_unlock(&c->erase_completion_lock);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
121

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  			ret = jffs2_garbage_collect_pass(c);
422b12023   David Woodhouse   [JFFS2] Fix jffs2...
123

0717bf841   David Woodhouse   jffs2: Erase pend...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  			if (ret == -EAGAIN) {
  				spin_lock(&c->erase_completion_lock);
  				if (c->nr_erasing_blocks &&
  				    list_empty(&c->erase_pending_list) &&
  				    list_empty(&c->erase_complete_list)) {
  					DECLARE_WAITQUEUE(wait, current);
  					set_current_state(TASK_UNINTERRUPTIBLE);
  					add_wait_queue(&c->erase_wait, &wait);
  					D1(printk(KERN_DEBUG "%s waiting for erase to complete
  ", __func__));
  					spin_unlock(&c->erase_completion_lock);
  
  					schedule();
  				} else
  					spin_unlock(&c->erase_completion_lock);
  			} else if (ret)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
143
144
145
  				return ret;
  
  			cond_resched();
  
  			if (signal_pending(current))
  				return -EINTR;
ced220703   David Woodhouse   [JFFS2] semaphore...
146
  			mutex_lock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  			spin_lock(&c->erase_completion_lock);
  		}
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
149
  		ret = jffs2_do_reserve_space(c, minsize, len, sumsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
154
155
  		if (ret) {
  			D1(printk(KERN_DEBUG "jffs2_reserve_space: ret is %d
  ", ret));
  		}
  	}
  	spin_unlock(&c->erase_completion_lock);
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
156
  	if (!ret)
046b8b980   David Woodhouse   [JFFS2] Add 'jeb'...
157
  		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
  	if (ret)
ced220703   David Woodhouse   [JFFS2] semaphore...
159
  		mutex_unlock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
  	return ret;
  }
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
162
163
  int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize,
  			   uint32_t *len, uint32_t sumsize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
166
167
168
169
170
171
172
  {
  	int ret = -EAGAIN;
  	minsize = PAD(minsize);
  
  	D1(printk(KERN_DEBUG "jffs2_reserve_space_gc(): Requested 0x%x bytes
  ", minsize));
  
  	spin_lock(&c->erase_completion_lock);
  	while(ret == -EAGAIN) {
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
173
  		ret = jffs2_do_reserve_space(c, minsize, len, sumsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  		if (ret) {
ef53cb02f   David Woodhouse   [JFFS2] Whitespac...
175
176
  			D1(printk(KERN_DEBUG "jffs2_reserve_space_gc: looping, ret is %d
  ", ret));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
  		}
  	}
  	spin_unlock(&c->erase_completion_lock);
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
180
  	if (!ret)
046b8b980   David Woodhouse   [JFFS2] Add 'jeb'...
181
  		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
182

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
  	return ret;
  }
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
185
186
187
188
  
  /* Classify nextblock (clean, dirty of verydirty) and force to select an other one */
  
  static void jffs2_close_nextblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
  {
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
190

99c2594f0   Adrian Hunter   [JFFS2] Prevent l...
191
192
193
194
195
196
  	if (c->nextblock == NULL) {
  		D1(printk(KERN_DEBUG "jffs2_close_nextblock: Erase block at 0x%08x has already been placed in a list
  ",
  		  jeb->offset));
  		return;
  	}
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  	/* Check, if we have a dirty block now, or if it was dirty already */
  	if (ISDIRTY (jeb->wasted_size + jeb->dirty_size)) {
  		c->dirty_size += jeb->wasted_size;
  		c->wasted_size -= jeb->wasted_size;
  		jeb->dirty_size += jeb->wasted_size;
  		jeb->wasted_size = 0;
  		if (VERYDIRTY(c, jeb->dirty_size)) {
  			D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to very_dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x
  ",
  			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  			list_add_tail(&jeb->list, &c->very_dirty_list);
  		} else {
  			D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x
  ",
  			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  			list_add_tail(&jeb->list, &c->dirty_list);
  		}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
214
  	} else {
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  		D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x
  ",
  		  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  		list_add_tail(&jeb->list, &c->clean_list);
  	}
  	c->nextblock = NULL;
  
  }
  
  /* Select a new jeb for nextblock */
  
  static int jffs2_find_nextblock(struct jffs2_sb_info *c)
  {
  	struct list_head *next;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
229

e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
230
231
232
233
234
235
236
237
238
  	/* Take the next block off the 'free' list */
  
  	if (list_empty(&c->free_list)) {
  
  		if (!c->nr_erasing_blocks &&
  			!list_empty(&c->erasable_list)) {
  			struct jffs2_eraseblock *ejeb;
  
  			ejeb = list_entry(c->erasable_list.next, struct jffs2_eraseblock, list);
f116629d0   Akinobu Mita   [PATCH] fs: use l...
239
  			list_move_tail(&ejeb->list, &c->erase_pending_list);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
240
  			c->nr_erasing_blocks++;
ae3b6ba06   David Woodhouse   jffs2: Use jffs2_...
241
  			jffs2_garbage_collect_trigger(c);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
242
243
244
245
246
247
248
249
250
251
  			D1(printk(KERN_DEBUG "jffs2_find_nextblock: Triggering erase of erasable block at 0x%08x
  ",
  				  ejeb->offset));
  		}
  
  		if (!c->nr_erasing_blocks &&
  			!list_empty(&c->erasable_pending_wbuf_list)) {
  			D1(printk(KERN_DEBUG "jffs2_find_nextblock: Flushing write buffer
  "));
  			/* c->nextblock is NULL, no update to c->nextblock allowed */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
  			spin_unlock(&c->erase_completion_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
  			jffs2_flush_wbuf_pad(c);
  			spin_lock(&c->erase_completion_lock);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
255
256
  			/* Have another go. It'll be on the erasable_list now */
  			return -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
  		}
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
258
259
260
261
  
  		if (!c->nr_erasing_blocks) {
  			/* Ouch. We're in GC, or we wouldn't have got here.
  			   And there's no space left. At all. */
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
262
263
264
  			printk(KERN_CRIT "Argh. No free space left for GC. nr_erasing_blocks is %d. nr_free_blocks is %d. (erasableempty: %s, erasingempty: %s, erasependingempty: %s)
  ",
  				   c->nr_erasing_blocks, c->nr_free_blocks, list_empty(&c->erasable_list)?"yes":"no",
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
265
266
  				   list_empty(&c->erasing_list)?"yes":"no", list_empty(&c->erase_pending_list)?"yes":"no");
  			return -ENOSPC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  		}
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
268
269
270
271
272
273
274
275
276
277
  
  		spin_unlock(&c->erase_completion_lock);
  		/* Don't wait for it; just erase one right now */
  		jffs2_erase_pending_blocks(c, 1);
  		spin_lock(&c->erase_completion_lock);
  
  		/* An erase may have failed, decreasing the
  		   amount of free space available. So we must
  		   restart from the beginning */
  		return -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
  	}
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
279
280
281
282
283
  
  	next = c->free_list.next;
  	list_del(next);
  	c->nextblock = list_entry(next, struct jffs2_eraseblock, list);
  	c->nr_free_blocks--;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
284

e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
285
  	jffs2_sum_reset_collected(c->summary); /* reset collected summary */
f04de505e   Steve Glendinning   [JFFS2] Fix build...
286
  #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
5bf172372   Alexander Belyakov   [JFFS2] Write buf...
287
288
289
  	/* adjust write buffer offset, else we get a non contiguous write bug */
  	if (!(c->wbuf_ofs % c->sector_size) && !c->wbuf_len)
  		c->wbuf_ofs = 0xffffffff;
f04de505e   Steve Glendinning   [JFFS2] Fix build...
290
  #endif
5bf172372   Alexander Belyakov   [JFFS2] Write buf...
291

e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
292
293
294
295
296
297
298
  	D1(printk(KERN_DEBUG "jffs2_find_nextblock(): new nextblock = 0x%08x
  ", c->nextblock->offset));
  
  	return 0;
  }
  
  /* Called with alloc sem _and_ erase_completion_lock */
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
299
300
  static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
  				  uint32_t *len, uint32_t sumsize)
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
301
302
  {
  	struct jffs2_eraseblock *jeb = c->nextblock;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
303
  	uint32_t reserved_size;				/* for summary information at the end of the jeb */
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
304
305
306
307
308
309
310
311
312
313
  	int ret;
  
   restart:
  	reserved_size = 0;
  
  	if (jffs2_sum_active() && (sumsize != JFFS2_SUMMARY_NOSUM_SIZE)) {
  							/* NOSUM_SIZE means not to generate summary */
  
  		if (jeb) {
  			reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
733802d97   Artem B. Bityutskiy   [JFFS2] Debug cod...
314
  			dbg_summary("minsize=%d , jeb->free=%d ,"
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  						"summary->size=%d , sumsize=%d
  ",
  						minsize, jeb->free_size,
  						c->summary->sum_size, sumsize);
  		}
  
  		/* Is there enough space for writing out the current node, or we have to
  		   write out summary information now, close this jeb and select new nextblock? */
  		if (jeb && (PAD(minsize) + PAD(c->summary->sum_size + sumsize +
  					JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size)) {
  
  			/* Has summary been disabled for this jeb? */
  			if (jffs2_sum_is_disabled(c->summary)) {
  				sumsize = JFFS2_SUMMARY_NOSUM_SIZE;
  				goto restart;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  			}
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
331
  			/* Writing out the collected summary information */
733802d97   Artem B. Bityutskiy   [JFFS2] Debug cod...
332
333
  			dbg_summary("generating summary for 0x%08x.
  ", jeb->offset);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  			ret = jffs2_sum_write_sumnode(c);
  
  			if (ret)
  				return ret;
  
  			if (jffs2_sum_is_disabled(c->summary)) {
  				/* jffs2_write_sumnode() couldn't write out the summary information
  				   diabling summary for this jeb and free the collected information
  				 */
  				sumsize = JFFS2_SUMMARY_NOSUM_SIZE;
  				goto restart;
  			}
  
  			jffs2_close_nextblock(c, jeb);
  			jeb = NULL;
34c0e9067   Ferenc Havasi   [JFFS2] Account s...
349
350
  			/* keep always valid value in reserved_size */
  			reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
351
352
353
  		}
  	} else {
  		if (jeb && minsize > jeb->free_size) {
fc6612f62   David Woodhouse   [JFFS2] When reti...
354
  			uint32_t waste;
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
355
356
357
358
  			/* Skip the end of this block and file it as having some dirty space */
  			/* If there's a pending write to it, flush now */
  
  			if (jffs2_wbuf_dirty(c)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
  				spin_unlock(&c->erase_completion_lock);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
360
361
  				D1(printk(KERN_DEBUG "jffs2_do_reserve_space: Flushing write buffer
  "));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
363
  				jffs2_flush_wbuf_pad(c);
  				spin_lock(&c->erase_completion_lock);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
364
365
  				jeb = c->nextblock;
  				goto restart;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
  			}
fc6612f62   David Woodhouse   [JFFS2] When reti...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  			spin_unlock(&c->erase_completion_lock);
  
  			ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
  			if (ret)
  				return ret;
  			/* Just lock it again and continue. Nothing much can change because
  			   we hold c->alloc_sem anyway. In fact, it's not entirely clear why
  			   we hold c->erase_completion_lock in the majority of this function...
  			   but that's a question for another (more caffeine-rich) day. */
  			spin_lock(&c->erase_completion_lock);
  
  			waste = jeb->free_size;
  			jffs2_link_node_ref(c, jeb,
  					    (jeb->offset + c->sector_size - waste) | REF_OBSOLETE,
  					    waste, NULL);
  			/* FIXME: that made it count as dirty. Convert to wasted */
  			jeb->dirty_size -= waste;
  			c->dirty_size -= waste;
  			jeb->wasted_size += waste;
  			c->wasted_size += waste;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387

e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
388
389
  			jffs2_close_nextblock(c, jeb);
  			jeb = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
  		}
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
391
392
393
394
395
396
397
  	}
  
  	if (!jeb) {
  
  		ret = jffs2_find_nextblock(c);
  		if (ret)
  			return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398

e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
399
  		jeb = c->nextblock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
402
403
404
405
406
407
408
  
  		if (jeb->free_size != c->sector_size - c->cleanmarker_size) {
  			printk(KERN_WARNING "Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!
  ", jeb->offset, jeb->free_size);
  			goto restart;
  		}
  	}
  	/* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
  	   enough space */
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
409
  	*len = jeb->free_size - reserved_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
411
412
  
  	if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
  	    !jeb->first_node->next_in_ino) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
413
  		/* Only node in it beforehand was a CLEANMARKER node (we think).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
  		   So mark it obsolete now that there's going to be another node
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
415
  		   in the block. This will reduce used_size to zero but We've
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
417
418
419
420
421
422
  		   already set c->nextblock so that jffs2_mark_node_obsolete()
  		   won't try to refile it to the dirty_list.
  		*/
  		spin_unlock(&c->erase_completion_lock);
  		jffs2_mark_node_obsolete(c, jeb->first_node);
  		spin_lock(&c->erase_completion_lock);
  	}
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
423
424
425
  	D1(printk(KERN_DEBUG "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x
  ",
  		  *len, jeb->offset + (c->sector_size - jeb->free_size)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
427
428
429
430
431
432
433
  	return 0;
  }
  
  /**
   *	jffs2_add_physical_node_ref - add a physical node reference to the list
   *	@c: superblock info
   *	@new: new node reference to add
   *	@len: length of this physical node
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
   *
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
435
   *	Should only be used to report nodes for which space has been allocated
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
437
438
439
   *	by jffs2_reserve_space.
   *
   *	Must be called with the alloc_sem held.
   */
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
440

2f785402f   David Woodhouse   [JFFS2] Reduce vi...
441
442
443
  struct jffs2_raw_node_ref *jffs2_add_physical_node_ref(struct jffs2_sb_info *c,
  						       uint32_t ofs, uint32_t len,
  						       struct jffs2_inode_cache *ic)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
  {
  	struct jffs2_eraseblock *jeb;
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
446
  	struct jffs2_raw_node_ref *new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447

2f785402f   David Woodhouse   [JFFS2] Reduce vi...
448
  	jeb = &c->blocks[ofs / c->sector_size];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449

2f785402f   David Woodhouse   [JFFS2] Reduce vi...
450
451
452
  	D1(printk(KERN_DEBUG "jffs2_add_physical_node_ref(): Node at 0x%x(%d), size 0x%x
  ",
  		  ofs & ~3, ofs & 3, len));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
  #if 1
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
454
455
456
457
458
  	/* Allow non-obsolete nodes only to be added at the end of c->nextblock, 
  	   if c->nextblock is set. Note that wbuf.c will file obsolete nodes
  	   even after refiling c->nextblock */
  	if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
  	    && (jeb != c->nextblock || (ofs & ~3) != jeb->offset + (c->sector_size - jeb->free_size))) {
66bfaeaa9   David Woodhouse   [JFFS2] Improve d...
459
460
461
462
463
464
465
466
  		printk(KERN_WARNING "argh. node added in wrong place at 0x%08x(%d)
  ", ofs & ~3, ofs & 3);
  		if (c->nextblock)
  			printk(KERN_WARNING "nextblock 0x%08x", c->nextblock->offset);
  		else
  			printk(KERN_WARNING "No nextblock");
  		printk(", expected at %08x
  ", jeb->offset + (c->sector_size - jeb->free_size));
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
467
  		return ERR_PTR(-EINVAL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
470
  	}
  #endif
  	spin_lock(&c->erase_completion_lock);
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
471
  	new = jffs2_link_node_ref(c, jeb, ofs, len, ic);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472

9b88f4739   Estelle Hammache   [JFFS2] Code clea...
473
  	if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  		/* If it lives on the dirty_list, jffs2_reserve_space will put it there */
  		D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x
  ",
  			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  		if (jffs2_wbuf_dirty(c)) {
  			/* Flush the last write in the block if it's outstanding */
  			spin_unlock(&c->erase_completion_lock);
  			jffs2_flush_wbuf_pad(c);
  			spin_lock(&c->erase_completion_lock);
  		}
  
  		list_add_tail(&jeb->list, &c->clean_list);
  		c->nextblock = NULL;
  	}
e0c8e42f8   Artem B. Bityutskiy   [JFFS2] Debug cod...
488
489
  	jffs2_dbg_acct_sanity_check_nolock(c,jeb);
  	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
  
  	spin_unlock(&c->erase_completion_lock);
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
492
  	return new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
494
495
496
497
498
499
  }
  
  
  void jffs2_complete_reservation(struct jffs2_sb_info *c)
  {
  	D1(printk(KERN_DEBUG "jffs2_complete_reservation()
  "));
acb64a43e   David Woodhouse   jffs2: Require jf...
500
  	spin_lock(&c->erase_completion_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
  	jffs2_garbage_collect_trigger(c);
acb64a43e   David Woodhouse   jffs2: Require jf...
502
  	spin_unlock(&c->erase_completion_lock);
ced220703   David Woodhouse   [JFFS2] semaphore...
503
  	mutex_unlock(&c->alloc_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  }
  
  static inline int on_list(struct list_head *obj, struct list_head *head)
  {
  	struct list_head *this;
  
  	list_for_each(this, head) {
  		if (this == obj) {
  			D1(printk("%p is on list at %p
  ", obj, head));
  			return 1;
  
  		}
  	}
  	return 0;
  }
  
  void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref)
  {
  	struct jffs2_eraseblock *jeb;
  	int blocknr;
  	struct jffs2_unknown_node n;
  	int ret, addedsize;
  	size_t retlen;
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
528
  	uint32_t freed_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529

9bfeb691e   David Woodhouse   [JFFS2] Switch to...
530
  	if(unlikely(!ref)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  		printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node
  ");
  		return;
  	}
  	if (ref_obsolete(ref)) {
  		D1(printk(KERN_DEBUG "jffs2_mark_node_obsolete called with already obsolete node at 0x%08x
  ", ref_offset(ref)));
  		return;
  	}
  	blocknr = ref->flash_offset / c->sector_size;
  	if (blocknr >= c->nr_blocks) {
  		printk(KERN_NOTICE "raw node at 0x%08x is off the end of device!
  ", ref->flash_offset);
  		BUG();
  	}
  	jeb = &c->blocks[blocknr];
  
  	if (jffs2_can_mark_obsolete(c) && !jffs2_is_readonly(c) &&
31fbdf7aa   Artem B. Bityuckiy   [JFFS2] Fix NOR s...
549
  	    !(c->flags & (JFFS2_SB_FLAG_SCANNING | JFFS2_SB_FLAG_BUILDING))) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
550
551
  		/* Hm. This may confuse static lock analysis. If any of the above
  		   three conditions is false, we're going to return from this
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
  		   function without actually obliterating any nodes or freeing
  		   any jffs2_raw_node_refs. So we don't need to stop erases from
  		   happening, or protect against people holding an obsolete
  		   jffs2_raw_node_ref without the erase_completion_lock. */
ced220703   David Woodhouse   [JFFS2] semaphore...
556
  		mutex_lock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
558
559
  	}
  
  	spin_lock(&c->erase_completion_lock);
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
560
  	freed_len = ref_totlen(c, jeb, ref);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
  	if (ref_flags(ref) == REF_UNCHECKED) {
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
562
  		D1(if (unlikely(jeb->unchecked_size < freed_len)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
  			printk(KERN_NOTICE "raw unchecked node of size 0x%08x freed from erase block %d at 0x%08x, but unchecked_size was already 0x%08x
  ",
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
565
  			       freed_len, blocknr, ref->flash_offset, jeb->used_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
567
  			BUG();
  		})
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
568
569
570
  		D1(printk(KERN_DEBUG "Obsoleting previously unchecked node at 0x%08x of len %x: ", ref_offset(ref), freed_len));
  		jeb->unchecked_size -= freed_len;
  		c->unchecked_size -= freed_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
  	} else {
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
572
  		D1(if (unlikely(jeb->used_size < freed_len)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
574
  			printk(KERN_NOTICE "raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x
  ",
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
575
  			       freed_len, blocknr, ref->flash_offset, jeb->used_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
577
  			BUG();
  		})
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
578
579
580
  		D1(printk(KERN_DEBUG "Obsoleting node at 0x%08x of len %#x: ", ref_offset(ref), freed_len));
  		jeb->used_size -= freed_len;
  		c->used_size -= freed_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
581
582
583
  	}
  
  	// Take care, that wasted size is taken into concern
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
584
  	if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + freed_len)) && jeb != c->nextblock) {
c7c16c8e7   David Woodhouse   [JFFS2] Revert Ar...
585
586
  		D1(printk("Dirtying
  "));
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
587
588
589
  		addedsize = freed_len;
  		jeb->dirty_size += freed_len;
  		c->dirty_size += freed_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
  
  		/* Convert wasted space to dirty, if not a bad block */
  		if (jeb->wasted_size) {
  			if (on_list(&jeb->list, &c->bad_used_list)) {
  				D1(printk(KERN_DEBUG "Leaving block at %08x on the bad_used_list
  ",
  					  jeb->offset));
  				addedsize = 0; /* To fool the refiling code later */
  			} else {
  				D1(printk(KERN_DEBUG "Converting %d bytes of wasted space to dirty in block at %08x
  ",
  					  jeb->wasted_size, jeb->offset));
  				addedsize += jeb->wasted_size;
  				jeb->dirty_size += jeb->wasted_size;
  				c->dirty_size += jeb->wasted_size;
  				c->wasted_size -= jeb->wasted_size;
  				jeb->wasted_size = 0;
  			}
  		}
  	} else {
c7c16c8e7   David Woodhouse   [JFFS2] Revert Ar...
610
611
  		D1(printk("Wasting
  "));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
  		addedsize = 0;
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
613
614
  		jeb->wasted_size += freed_len;
  		c->wasted_size += freed_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
615
616
  	}
  	ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
617

e0c8e42f8   Artem B. Bityutskiy   [JFFS2] Debug cod...
618
619
  	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
  	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620

31fbdf7aa   Artem B. Bityuckiy   [JFFS2] Fix NOR s...
621
622
  	if (c->flags & JFFS2_SB_FLAG_SCANNING) {
  		/* Flash scanning is in progress. Don't muck about with the block
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
  		   lists because they're not ready yet, and don't actually
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
624
  		   obliterate nodes that look obsolete. If they weren't
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  		   marked obsolete on the flash at the time they _became_
  		   obsolete, there was probably a reason for that. */
  		spin_unlock(&c->erase_completion_lock);
  		/* We didn't lock the erase_free_sem */
  		return;
  	}
  
  	if (jeb == c->nextblock) {
  		D2(printk(KERN_DEBUG "Not moving nextblock 0x%08x to dirty/erase_pending list
  ", jeb->offset));
  	} else if (!jeb->used_size && !jeb->unchecked_size) {
  		if (jeb == c->gcblock) {
  			D1(printk(KERN_DEBUG "gcblock at 0x%08x completely dirtied. Clearing gcblock...
  ", jeb->offset));
  			c->gcblock = NULL;
  		} else {
  			D1(printk(KERN_DEBUG "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...
  ", jeb->offset));
  			list_del(&jeb->list);
  		}
  		if (jffs2_wbuf_dirty(c)) {
  			D1(printk(KERN_DEBUG "...and adding to erasable_pending_wbuf_list
  "));
  			list_add_tail(&jeb->list, &c->erasable_pending_wbuf_list);
  		} else {
  			if (jiffies & 127) {
  				/* Most of the time, we just erase it immediately. Otherwise we
  				   spend ages scanning it on mount, etc. */
  				D1(printk(KERN_DEBUG "...and adding to erase_pending_list
  "));
  				list_add_tail(&jeb->list, &c->erase_pending_list);
  				c->nr_erasing_blocks++;
ae3b6ba06   David Woodhouse   jffs2: Use jffs2_...
657
  				jffs2_garbage_collect_trigger(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
659
660
661
662
663
  			} else {
  				/* Sometimes, however, we leave it elsewhere so it doesn't get
  				   immediately reused, and we spread the load a bit. */
  				D1(printk(KERN_DEBUG "...and adding to erasable_list
  "));
  				list_add_tail(&jeb->list, &c->erasable_list);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
664
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  		}
  		D1(printk(KERN_DEBUG "Done OK
  "));
  	} else if (jeb == c->gcblock) {
  		D2(printk(KERN_DEBUG "Not moving gcblock 0x%08x to dirty_list
  ", jeb->offset));
  	} else if (ISDIRTY(jeb->dirty_size) && !ISDIRTY(jeb->dirty_size - addedsize)) {
  		D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...
  ", jeb->offset));
  		list_del(&jeb->list);
  		D1(printk(KERN_DEBUG "...and adding to dirty_list
  "));
  		list_add_tail(&jeb->list, &c->dirty_list);
  	} else if (VERYDIRTY(c, jeb->dirty_size) &&
  		   !VERYDIRTY(c, jeb->dirty_size - addedsize)) {
  		D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is now very dirty. Removing from dirty list...
  ", jeb->offset));
  		list_del(&jeb->list);
  		D1(printk(KERN_DEBUG "...and adding to very_dirty_list
  "));
  		list_add_tail(&jeb->list, &c->very_dirty_list);
  	} else {
  		D1(printk(KERN_DEBUG "Eraseblock at 0x%08x not moved anywhere. (free 0x%08x, dirty 0x%08x, used 0x%08x)
  ",
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
689
690
  			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
691
692
  
  	spin_unlock(&c->erase_completion_lock);
31fbdf7aa   Artem B. Bityuckiy   [JFFS2] Fix NOR s...
693
694
  	if (!jffs2_can_mark_obsolete(c) || jffs2_is_readonly(c) ||
  		(c->flags & JFFS2_SB_FLAG_BUILDING)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
696
697
698
699
700
701
  		/* We didn't lock the erase_free_sem */
  		return;
  	}
  
  	/* The erase_free_sem is locked, and has been since before we marked the node obsolete
  	   and potentially put its eraseblock onto the erase_pending_list. Thus, we know that
  	   the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet
c38c1b613   David Woodhouse   [JFFS2] jffs2_fre...
702
  	   by jffs2_free_jeb_node_refs() in erase.c. Which is nice. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
705
706
707
708
709
710
711
712
713
714
715
716
  
  	D1(printk(KERN_DEBUG "obliterating obsoleted node at 0x%08x
  ", ref_offset(ref)));
  	ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
  	if (ret) {
  		printk(KERN_WARNING "Read error reading from obsoleted node at 0x%08x: %d
  ", ref_offset(ref), ret);
  		goto out_erase_sem;
  	}
  	if (retlen != sizeof(n)) {
  		printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %zd
  ", ref_offset(ref), retlen);
  		goto out_erase_sem;
  	}
1417fc44e   David Woodhouse   [JFFS2] Reduce ca...
717
718
719
  	if (PAD(je32_to_cpu(n.totlen)) != PAD(freed_len)) {
  		printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)
  ", je32_to_cpu(n.totlen), freed_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
  		goto out_erase_sem;
  	}
  	if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
  		D1(printk(KERN_DEBUG "Node at 0x%08x was already marked obsolete (nodetype 0x%04x)
  ", ref_offset(ref), je16_to_cpu(n.nodetype)));
  		goto out_erase_sem;
  	}
  	/* XXX FIXME: This is ugly now */
  	n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
  	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
  	if (ret) {
  		printk(KERN_WARNING "Write error in obliterating obsoleted node at 0x%08x: %d
  ", ref_offset(ref), ret);
  		goto out_erase_sem;
  	}
  	if (retlen != sizeof(n)) {
  		printk(KERN_WARNING "Short write in obliterating obsoleted node at 0x%08x: %zd
  ", ref_offset(ref), retlen);
  		goto out_erase_sem;
  	}
  
  	/* Nodes which have been marked obsolete no longer need to be
  	   associated with any inode. Remove them from the per-inode list.
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
743
744
  
  	   Note we can't do this for NAND at the moment because we need
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
746
  	   obsolete dirent nodes to stay on the lists, because of the
  	   horridness in jffs2_garbage_collect_deletion_dirent(). Also
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
747
  	   because we delete the inocache, and on NAND we need that to
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
  	   stay around until all the nodes are actually erased, in order
  	   to stop us from giving the same inode number to another newly
  	   created inode. */
  	if (ref->next_in_ino) {
  		struct jffs2_inode_cache *ic;
  		struct jffs2_raw_node_ref **p;
  
  		spin_lock(&c->erase_completion_lock);
  
  		ic = jffs2_raw_ref_to_ic(ref);
  		for (p = &ic->nodes; (*p) != ref; p = &((*p)->next_in_ino))
  			;
  
  		*p = ref->next_in_ino;
  		ref->next_in_ino = NULL;
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
763
764
765
766
767
768
769
770
771
772
  		switch (ic->class) {
  #ifdef CONFIG_JFFS2_FS_XATTR
  			case RAWNODE_CLASS_XATTR_DATUM:
  				jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
  				break;
  			case RAWNODE_CLASS_XATTR_REF:
  				jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
  				break;
  #endif
  			default:
27c72b040   David Woodhouse   [JFFS2] Track par...
773
  				if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
774
775
776
  					jffs2_del_ino_cache(c, ic);
  				break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
  		spin_unlock(&c->erase_completion_lock);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
779
   out_erase_sem:
ced220703   David Woodhouse   [JFFS2] semaphore...
780
  	mutex_unlock(&c->erase_free_sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
784
785
  int jffs2_thread_should_wake(struct jffs2_sb_info *c)
  {
  	int ret = 0;
  	uint32_t dirty;
8fb870df5   David Woodhouse   [JFFS2] Trigger g...
786
787
  	int nr_very_dirty = 0;
  	struct jffs2_eraseblock *jeb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788

d6ce17106   Joakim Tjernlund   jffs2: Wake GC th...
789
790
791
  	if (!list_empty(&c->erase_complete_list) ||
  	    !list_empty(&c->erase_pending_list))
  		return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  	if (c->unchecked_size) {
  		D1(printk(KERN_DEBUG "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d
  ",
  			  c->unchecked_size, c->checked_ino));
  		return 1;
  	}
  
  	/* dirty_size contains blocks on erase_pending_list
  	 * those blocks are counted in c->nr_erasing_blocks.
  	 * If one block is actually erased, it is not longer counted as dirty_space
  	 * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
  	 * with c->nr_erasing_blocks * c->sector_size again.
  	 * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
  	 * This helps us to force gc and pick eventually a clean block to spread the load.
  	 */
  	dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
808
809
  	if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger &&
  			(dirty > c->nospc_dirty_size))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
  		ret = 1;
8fb870df5   David Woodhouse   [JFFS2] Trigger g...
811
812
813
814
  	list_for_each_entry(jeb, &c->very_dirty_list, list) {
  		nr_very_dirty++;
  		if (nr_very_dirty == c->vdirty_blocks_gctrigger) {
  			ret = 1;
a8c68f326   David Woodhouse   [JFFS2] Don't cou...
815
816
817
  			/* In debug mode, actually go through and count them all */
  			D1(continue);
  			break;
8fb870df5   David Woodhouse   [JFFS2] Trigger g...
818
819
820
821
822
823
  		}
  	}
  
  	D1(printk(KERN_DEBUG "jffs2_thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x, vdirty_blocks %d: %s
  ",
  		  c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size, nr_very_dirty, ret?"yes":"no"));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
825
826
  
  	return ret;
  }