embox

Форк
0
/
nodemgmt.c 
705 строк · 24.3 Кб
1
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001-2003 Red Hat, Inc.
5
 *
6
 * Created by David Woodhouse <dwmw2@infradead.org>
7
 *
8
 * For licensing information, see the file 'LICENCE' in this directory.
9
 *
10
 * $Id: nodemgmt.c,v 1.124 2005/07/20 15:32:28 dedekind Exp $
11
 *
12
 */
13

14
#include <linux/kernel.h>
15
#include <linux/slab.h>
16
#include <linux/mtd/mtd.h>
17
#include <linux/compiler.h>
18
#include <linux/sched.h> /* For cond_resched() */
19
#include "nodelist.h"
20

21
/**
22
 *	jffs2_reserve_space - request physical space to write nodes to flash
23
 *	@c: superblock info
24
 *	@minsize: Minimum acceptable size of allocation
25
 *	@ofs: Returned value of node offset
26
 *	@len: Returned value of allocation length
27
 *	@prio: Allocation type - ALLOC_{NORMAL,DELETION}
28
 *
29
 *	Requests a block of physical space on the flash. Returns zero for success
30
 *	and puts 'ofs' and 'len' into the appriopriate place, or returns -ENOSPC
31
 *	or other error if appropriate.
32
 *
33
 *	If it returns zero, jffs2_reserve_space() also downs the per-filesystem
34
 *	allocation semaphore, to prevent more than one allocation from being
35
 *	active at any time. The semaphore is later released by jffs2_commit_allocation()
36
 *
37
 *	jffs2_reserve_space() may trigger garbage collection in order to make room
38
 *	for the requested allocation.
39
 */
40

41
static int jffs2_do_reserve_space(struct jffs2_sb_info *c,
42
			uint32_t minsize, uint32_t *ofs, uint32_t *len);
43

44
int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
45
		uint32_t *ofs, uint32_t *len, int prio) {
46
	int ret = -EAGAIN;
47
	int blocksneeded = c->resv_blocks_write;
48
	/* align it */
49
	minsize = PAD(minsize);
50

51
	D1(printk( "jffs2_reserve_space(): Requested 0x%x bytes\n", minsize));
52
	down(&c->alloc_sem);
53

54
	D1(printk( "jffs2_reserve_space(): alloc sem got\n"));
55

56
	spin_lock(&c->erase_completion_lock);
57

58
	/* this needs a little more thought (true <tglx> :)) */
59
	while(ret == -EAGAIN) {
60
		while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
61
			int ret;
62
			uint32_t dirty, avail;
63

64
			/* calculate real dirty size
65
			 * dirty_size contains blocks on erase_pending_list
66
			 * those blocks are counted in c->nr_erasing_blocks.
67
			 * If one block is actually erased, it is not longer counted as dirty_space
68
			 * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
69
			 * with c->nr_erasing_blocks * c->sector_size again.
70
			 * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
71
			 * This helps us to force gc and pick eventually a clean block to spread the load.
72
			 * We add unchecked_size here, as we hopefully will find some space to use.
73
			 * This will affect the sum only once, as gc first finishes checking
74
			 * of nodes.
75
			 */
76
			dirty = c->dirty_size + c->erasing_size -
77
					c->nr_erasing_blocks * c->sector_size + c->unchecked_size;
78
			if (dirty < c->nospc_dirty_size) {
79
				if (prio == ALLOC_DELETION && c->nr_free_blocks +
80
						c->nr_erasing_blocks >= c->resv_blocks_deletion) {
81
					D1(printk(KERN_NOTICE "jffs2_reserve_space(): Low on dirty space to GC, but it's a deletion. Allowing...\n"));
82
					break;
83
				}
84
				D1(printk( "dirty size 0x%08x + unchecked_size 0x%08x < nospc_dirty_size 0x%08x, returning -ENOSPC\n",
85
					  dirty, c->unchecked_size, c->sector_size));
86

87
				spin_unlock(&c->erase_completion_lock);
88
				up(&c->alloc_sem);
89
				return -ENOSPC;
90
			}
91

92
			/* Calc possibly available space. Possibly available means that we
93
			 * don't know, if unchecked size contains obsoleted nodes, which could give us some
94
			 * more usable space. This will affect the sum only once, as gc first finishes checking
95
			 * of nodes.
96
			 + Return -ENOSPC, if the maximum possibly available space is less or equal than
97
			 * blocksneeded * sector_size.
98
			 * This blocks endless gc looping on a filesystem, which is nearly full, even if
99
			 * the check above passes.
100
			 */
101
			avail = c->free_size + c->dirty_size +
102
					c->erasing_size + c->unchecked_size;
103
			if ( (avail / c->sector_size) <= blocksneeded) {
104
				if (prio == ALLOC_DELETION && c->nr_free_blocks +
105
						c->nr_erasing_blocks >= c->resv_blocks_deletion) {
106
					D1(printk(KERN_NOTICE "jffs2_reserve_space(): Low on possibly available space, but it's a deletion. Allowing...\n"));
107
					break;
108
				}
109

110
				D1(printk( "max. available size 0x%08x  < blocksneeded * sector_size 0x%08x, returning -ENOSPC\n",
111
					  avail, blocksneeded * c->sector_size));
112
				spin_unlock(&c->erase_completion_lock);
113
				up(&c->alloc_sem);
114
				return -ENOSPC;
115
			}
116

117
			up(&c->alloc_sem);
118

119
			D1(printk( "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",
120
				  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,
121
				  c->free_size + c->dirty_size + c->wasted_size + c->used_size + c->erasing_size + c->bad_size, c->flash_size));
122
			spin_unlock(&c->erase_completion_lock);
123

124
			ret = jffs2_garbage_collect_pass(c);
125
			if (ret) {
126
				return ret;
127
			}
128

129
			cond_resched();
130

131
			if (signal_pending(current)) {
132
				return -EINTR;
133
			}
134

135
			down(&c->alloc_sem);
136
			spin_lock(&c->erase_completion_lock);
137
		}
138

139
		ret = jffs2_do_reserve_space(c, minsize, ofs, len);
140
		if (ret) {
141
			D1(printk( "jffs2_reserve_space: ret is %d\n", ret));
142
		}
143
	}
144
	spin_unlock(&c->erase_completion_lock);
145
	if (ret) {
146
		up(&c->alloc_sem);
147
	}
148
	return ret;
149
}
150

151
int jffs2_reserve_space_gc(struct jffs2_sb_info *c,
152
		uint32_t minsize, uint32_t *ofs, uint32_t *len) {
153
	int ret = -EAGAIN;
154
	minsize = PAD(minsize);
155

156
	D1(printk( "jffs2_reserve_space_gc(): Requested 0x%x bytes\n", minsize));
157

158
	spin_lock(&c->erase_completion_lock);
159
	while(ret == -EAGAIN) {
160
		ret = jffs2_do_reserve_space(c, minsize, ofs, len);
161
		if (ret) {
162
		        D1(printk( "jffs2_reserve_space_gc: looping, ret is %d\n", ret));
163
		}
164
	}
165
	spin_unlock(&c->erase_completion_lock);
166
	return ret;
167
}
168

169
/* Called with alloc sem _and_ erase_completion_lock */
170
static int jffs2_do_reserve_space(struct jffs2_sb_info *c,
171
		uint32_t minsize, uint32_t *ofs, uint32_t *len) {
172
	struct jffs2_eraseblock *jeb = c->nextblock;
173

174
 restart:
175
	if (jeb && minsize > jeb->free_size) {
176
		/* Skip the end of this block and file it as having some dirty space */
177
		/* If there's a pending write to it, flush now */
178
		if (jffs2_wbuf_dirty(c)) {
179
			spin_unlock(&c->erase_completion_lock);
180
			D1(printk( "jffs2_do_reserve_space: Flushing write buffer\n"));
181
			jffs2_flush_wbuf_pad(c);
182
			spin_lock(&c->erase_completion_lock);
183
			jeb = c->nextblock;
184
			goto restart;
185
		}
186
		c->wasted_size += jeb->free_size;
187
		c->free_size -= jeb->free_size;
188
		jeb->wasted_size += jeb->free_size;
189
		jeb->free_size = 0;
190

191
		/* Check, if we have a dirty block now, or if it was dirty already */
192
		if (ISDIRTY (jeb->wasted_size + jeb->dirty_size)) {
193
			c->dirty_size += jeb->wasted_size;
194
			c->wasted_size -= jeb->wasted_size;
195
			jeb->dirty_size += jeb->wasted_size;
196
			jeb->wasted_size = 0;
197
			if (VERYDIRTY(c, jeb->dirty_size)) {
198
				D1(printk( "Adding full erase block at 0x%08x to very_dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
199
				  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
200
				list_add_tail(&jeb->list, &c->very_dirty_list);
201
			} else {
202
				D1(printk( "Adding full erase block at 0x%08x to dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
203
				  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
204
				list_add_tail(&jeb->list, &c->dirty_list);
205
			}
206
		} else {
207
			D1(printk( "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
208
			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
209
			list_add_tail(&jeb->list, &c->clean_list);
210
		}
211
		c->nextblock = jeb = NULL;
212
	}
213

214
	if (!jeb) {
215
		struct list_head *next;
216
		/* Take the next block off the 'free' list */
217

218
		if (list_empty(&c->free_list)) {
219

220
			if (!c->nr_erasing_blocks &&
221
			    !list_empty(&c->erasable_list)) {
222
				struct jffs2_eraseblock *ejeb;
223

224
				ejeb = list_entry(c->erasable_list.next,
225
						struct jffs2_eraseblock, list);
226
				list_del(&ejeb->list);
227
				list_add_tail(&ejeb->list, &c->erase_pending_list);
228
				c->nr_erasing_blocks++;
229
				jffs2_erase_pending_trigger(c);
230
				D1(printk( "jffs2_do_reserve_space: Triggering erase of erasable block at 0x%08x\n",
231
					  ejeb->offset));
232
			}
233

234
			if (!c->nr_erasing_blocks &&
235
			    !list_empty(&c->erasable_pending_wbuf_list)) {
236
				D1(printk( "jffs2_do_reserve_space: Flushing write buffer\n"));
237
				/* c->nextblock is NULL, no update to c->nextblock allowed */
238
				spin_unlock(&c->erase_completion_lock);
239
				jffs2_flush_wbuf_pad(c);
240
				spin_lock(&c->erase_completion_lock);
241
				/* Have another go. It'll be on the erasable_list now */
242
				return -EAGAIN;
243
			}
244

245
			if (!c->nr_erasing_blocks) {
246
				/* Ouch. We're in GC, or we wouldn't have got here.
247
				 * And there's no space left. At all.
248
				 */
249
				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)\n",
250
				       c->nr_erasing_blocks, c->nr_free_blocks, list_empty(&c->erasable_list)?"yes":"no",
251
				       list_empty(&c->erasing_list)?"yes":"no", list_empty(&c->erase_pending_list)?"yes":"no");
252
				return -ENOSPC;
253
			}
254

255
			spin_unlock(&c->erase_completion_lock);
256
			/* Don't wait for it; just erase one right now */
257
			jffs2_erase_pending_blocks(c, 1);
258
			spin_lock(&c->erase_completion_lock);
259

260
			/* An erase may have failed, decreasing the
261
			 * amount of free space available. So we must
262
			 * restart from the beginning
263
			 */
264
			return -EAGAIN;
265
		}
266

267
		next = c->free_list.next;
268
		list_del(next);
269
		c->nextblock = jeb = list_entry(next, struct jffs2_eraseblock, list);
270
		c->nr_free_blocks--;
271

272
		if (jeb->free_size != c->sector_size - c->cleanmarker_size) {
273
			printk(KERN_WARNING "Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!\n", jeb->offset, jeb->free_size);
274
			goto restart;
275
		}
276
	}
277
	/* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
278
	   enough space */
279
	*ofs = jeb->offset + (c->sector_size - jeb->free_size);
280
	*len = jeb->free_size;
281

282
	if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
283
	    !jeb->first_node->next_in_ino) {
284
		/* Only node in it beforehand was a CLEANMARKER node (we think).
285
		 * So mark it obsolete now that there's going to be another node
286
		 * in the block. This will reduce used_size to zero but We've
287
		 * already set c->nextblock so that jffs2_mark_node_obsolete()
288
		 * won't try to refile it to the dirty_list.
289
		 */
290
		spin_unlock(&c->erase_completion_lock);
291
		jffs2_mark_node_obsolete(c, jeb->first_node);
292
		spin_lock(&c->erase_completion_lock);
293
	}
294

295
	D1(printk( "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x\n", *len, *ofs));
296
	return 0;
297
}
298

299
/**
300
 *	jffs2_add_physical_node_ref - add a physical node reference to the list
301
 *	@c: superblock info
302
 *	@new: new node reference to add
303
 *	@len: length of this physical node
304
 *	@dirty: dirty flag for new node
305
 *
306
 *	Should only be used to report nodes for which space has been allocated
307
 *	by jffs2_reserve_space.
308
 *
309
 *	Must be called with the alloc_sem held.
310
 */
311
int jffs2_add_physical_node_ref(struct jffs2_sb_info *c,
312
		struct jffs2_raw_node_ref *new) {
313
	struct jffs2_eraseblock *jeb;
314
	uint32_t len;
315

316
	jeb = &c->blocks[new->flash_offset / c->sector_size];
317
	len = ref_totlen(c, jeb, new);
318

319
	D1(printk( "jffs2_add_physical_node_ref(): Node at 0x%x(%d), size 0x%x\n", ref_offset(new), ref_flags(new), len));
320
#if 1
321
	/* we could get some obsolete nodes after nextblock was refiled in wbuf.c */
322
	if ((c->nextblock || !ref_obsolete(new)) &&
323
	    (jeb != c->nextblock || ref_offset(new) !=
324
	    		jeb->offset + (c->sector_size - jeb->free_size))) {
325
		printk(KERN_WARNING "argh. node added in wrong place\n");
326
		jffs2_free_raw_node_ref(new);
327
		return -EINVAL;
328
	}
329
#endif
330
	spin_lock(&c->erase_completion_lock);
331

332
	if (!jeb->first_node) {
333
		jeb->first_node = new;
334
	}
335
	if (jeb->last_node) {
336
		jeb->last_node->next_phys = new;
337
	}
338
	jeb->last_node = new;
339

340
	jeb->free_size -= len;
341
	c->free_size -= len;
342
	if (ref_obsolete(new)) {
343
		jeb->dirty_size += len;
344
		c->dirty_size += len;
345
	} else {
346
		jeb->used_size += len;
347
		c->used_size += len;
348
	}
349

350
	if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) {
351
		/* If it lives on the dirty_list, jffs2_reserve_space will put it there */
352
		D1(printk( "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
353
			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
354
		if (jffs2_wbuf_dirty(c)) {
355
			/* Flush the last write in the block if it's outstanding */
356
			spin_unlock(&c->erase_completion_lock);
357
			jffs2_flush_wbuf_pad(c);
358
			spin_lock(&c->erase_completion_lock);
359
		}
360

361
		list_add_tail(&jeb->list, &c->clean_list);
362
		c->nextblock = NULL;
363
	}
364
	jffs2_dbg_acct_sanity_check_nolock(c,jeb);
365
	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
366

367
	spin_unlock(&c->erase_completion_lock);
368

369
	return 0;
370
}
371

372

373
void jffs2_complete_reservation(struct jffs2_sb_info *c) {
374
	D1(printk( "jffs2_complete_reservation()\n"));
375
	jffs2_garbage_collect_trigger(c);
376
	up(&c->alloc_sem);
377
}
378

379
static inline int on_list(struct list_head *obj, struct list_head *head) {
380
	struct list_head *this;
381

382
	list_for_each(this, head) {
383
		if (this == obj) {
384
			D1(printk("%p is on list at %p\n", obj, head));
385
			return 1;
386
		}
387
	}
388
	return 0;
389
}
390

391
void jffs2_mark_node_obsolete(struct jffs2_sb_info *c,
392
						struct jffs2_raw_node_ref *ref) {
393
	struct jffs2_eraseblock *jeb;
394
	int blocknr;
395
	struct jffs2_unknown_node n;
396
	int ret, addedsize;
397
	size_t retlen;
398

399
	if(!ref) {
400
		printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
401
		return;
402
	}
403
	if (ref_obsolete(ref)) {
404
		D1(printk( "jffs2_mark_node_obsolete called with already obsolete node at 0x%08x\n", ref_offset(ref)));
405
		return;
406
	}
407
	blocknr = ref->flash_offset / c->sector_size;
408
	if (blocknr >= c->nr_blocks) {
409
		printk(KERN_NOTICE "raw node at 0x%08x is off the end of device!\n", ref->flash_offset);
410
		BUG();
411
	}
412
	jeb = &c->blocks[blocknr];
413

414
	if (jffs2_can_mark_obsolete(c) &&
415
	    !(c->flags & (JFFS2_SB_FLAG_SCANNING | JFFS2_SB_FLAG_BUILDING))) {
416
		/* Hm. This may confuse static lock analysis. If any of the above
417
		 * three conditions is false, we're going to return from this
418
		 * function without actually obliterating any nodes or freeing
419
		 * any jffs2_raw_node_refs. So we don't need to stop erases from
420
		 * happening, or protect against people holding an obsolete
421
		 * jffs2_raw_node_ref without the erase_completion_lock.
422
		 */
423
		down(&c->erase_free_sem);
424
	}
425

426
	spin_lock(&c->erase_completion_lock);
427

428
	if (ref_flags(ref) == REF_UNCHECKED) {
429
		D1(if (unlikely(jeb->unchecked_size < ref_totlen(c, jeb, ref))) {
430
			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\n",
431
			       ref_totlen(c, jeb, ref), blocknr, ref->flash_offset, jeb->used_size);
432
			BUG();
433
		})
434
		D1(printk( "Obsoleting previously unchecked node at 0x%08x of len %x: ", ref_offset(ref), ref_totlen(c, jeb, ref)));
435
		jeb->unchecked_size -= ref_totlen(c, jeb, ref);
436
		c->unchecked_size -= ref_totlen(c, jeb, ref);
437
	} else {
438
		D1(if (unlikely(jeb->used_size < ref_totlen(c, jeb, ref))) {
439
			printk(KERN_NOTICE "raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n",
440
			       ref_totlen(c, jeb, ref), blocknr, ref->flash_offset, jeb->used_size);
441
			BUG();
442
		})
443
		D1(printk( "Obsoleting node at 0x%08x of len %#x: ", ref_offset(ref), ref_totlen(c, jeb, ref)));
444
		jeb->used_size -= ref_totlen(c, jeb, ref);
445
		c->used_size -= ref_totlen(c, jeb, ref);
446
	}
447

448
	/* Take care, that wasted size is taken into concern */
449
	if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size +
450
			ref_totlen(c, jeb, ref))) && jeb != c->nextblock) {
451
		D1(printk( "Dirtying\n"));
452
		addedsize = ref_totlen(c, jeb, ref);
453
		jeb->dirty_size += ref_totlen(c, jeb, ref);
454
		c->dirty_size += ref_totlen(c, jeb, ref);
455

456
		/* Convert wasted space to dirty, if not a bad block */
457
		if (jeb->wasted_size) {
458
			if (on_list(&jeb->list, &c->bad_used_list)) {
459
				D1(printk( "Leaving block at %08x on the bad_used_list\n",
460
					  jeb->offset));
461
				addedsize = 0; /* To fool the refiling code later */
462
			} else {
463
				D1(printk( "Converting %d bytes of wasted space to dirty in block at %08x\n",
464
					  jeb->wasted_size, jeb->offset));
465
				addedsize += jeb->wasted_size;
466
				jeb->dirty_size += jeb->wasted_size;
467
				c->dirty_size += jeb->wasted_size;
468
				c->wasted_size -= jeb->wasted_size;
469
				jeb->wasted_size = 0;
470
			}
471
		}
472
	} else {
473
		D1(printk( "Wasting\n"));
474
		addedsize = 0;
475
		jeb->wasted_size += ref_totlen(c, jeb, ref);
476
		c->wasted_size += ref_totlen(c, jeb, ref);
477
	}
478
	ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
479

480
	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
481
	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
482

483
	if (c->flags & JFFS2_SB_FLAG_SCANNING) {
484
		/* Flash scanning is in progress. Don't muck about with the block
485
		 * lists because they're not ready yet, and don't actually
486
		 * obliterate nodes that look obsolete. If they weren't
487
		 * marked obsolete on the flash at the time they _became_
488
		 * obsolete, there was probably a reason for that.
489
		 */
490
		spin_unlock(&c->erase_completion_lock);
491
		/* We didn't lock the erase_free_sem */
492
		return;
493
	}
494

495
	if (jeb == c->nextblock) {
496
		D2(printk( "Not moving nextblock 0x%08x to dirty/erase_pending list\n", jeb->offset));
497
	} else if (!jeb->used_size && !jeb->unchecked_size) {
498
		if (jeb == c->gcblock) {
499
			D1(printk( "gcblock at 0x%08x completely dirtied. Clearing gcblock...\n", jeb->offset));
500
			c->gcblock = NULL;
501
		} else {
502
			D1(printk( "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...\n", jeb->offset));
503
			list_del(&jeb->list);
504
		}
505
		if (jffs2_wbuf_dirty(c)) {
506
			D1(printk( "...and adding to erasable_pending_wbuf_list\n"));
507
			list_add_tail(&jeb->list, &c->erasable_pending_wbuf_list);
508
		} else {
509
			int jiffies = 0;
510
			if (jiffies & 127) {
511
				/* Most of the time, we just erase it immediately. Otherwise we
512
				 * spend ages scanning it on mount, etc.
513
				 */
514
				D1(printk( "...and adding to erase_pending_list\n"));
515
				list_add_tail(&jeb->list, &c->erase_pending_list);
516
				c->nr_erasing_blocks++;
517
				jffs2_erase_pending_trigger(c);
518
			} else {
519
				/* Sometimes, however, we leave it elsewhere so it doesn't get
520
				 * immediately reused, and we spread the load a bit.
521
				 */
522
				D1(printk( "...and adding to erasable_list\n"));
523
				list_add_tail(&jeb->list, &c->erasable_list);
524
			}
525
		}
526
		D1(printk( "Done OK\n"));
527
	} else if (jeb == c->gcblock) {
528
		D2(printk( "Not moving gcblock 0x%08x to dirty_list\n", jeb->offset));
529
	} else if (ISDIRTY(jeb->dirty_size) && !ISDIRTY(jeb->dirty_size - addedsize)) {
530
		D1(printk( "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...\n", jeb->offset));
531
		list_del(&jeb->list);
532
		D1(printk( "...and adding to dirty_list\n"));
533
		list_add_tail(&jeb->list, &c->dirty_list);
534
	} else if (VERYDIRTY(c, jeb->dirty_size) &&
535
		   !VERYDIRTY(c, jeb->dirty_size - addedsize)) {
536
		D1(printk( "Eraseblock at 0x%08x is now very dirty. Removing from dirty list...\n", jeb->offset));
537
		list_del(&jeb->list);
538
		D1(printk( "...and adding to very_dirty_list\n"));
539
		list_add_tail(&jeb->list, &c->very_dirty_list);
540
	} else {
541
		D1(printk( "Eraseblock at 0x%08x not moved anywhere. (free 0x%08x, dirty 0x%08x, used 0x%08x)\n",
542
			  jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
543
	}
544

545
	spin_unlock(&c->erase_completion_lock);
546

547
	if (!jffs2_can_mark_obsolete(c) || (c->flags & JFFS2_SB_FLAG_BUILDING)) {
548
		/* We didn't lock the erase_free_sem */
549
		return;
550
	}
551

552
	/* The erase_free_sem is locked, and has been since before we marked the node obsolete
553
	 * and potentially put its eraseblock onto the erase_pending_list. Thus, we know that
554
	 * the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet
555
	 * by jffs2_free_all_node_refs() in erase.c. Which is nice.
556
	 */
557

558
	D1(printk( "obliterating obsoleted node at 0x%08x\n", ref_offset(ref)));
559
	ret = jffs2_flash_read(c, ref_offset(ref),
560
			sizeof(n), &retlen, (unsigned char *)&n);
561
	if (ret) {
562
		printk(KERN_WARNING "Read error reading from obsoleted node at 0x%08x: %d\n", ref_offset(ref), ret);
563
		goto out_erase_sem;
564
	}
565
	if (retlen != sizeof(n)) {
566
		printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
567
		goto out_erase_sem;
568
	}
569
	if (PAD(je32_to_cpu(n.totlen)) != PAD(ref_totlen(c, jeb, ref))) {
570
		printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n", je32_to_cpu(n.totlen), ref_totlen(c, jeb, ref));
571
		goto out_erase_sem;
572
	}
573
	if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
574
		D1(printk( "Node at 0x%08x was already marked obsolete (nodetype 0x%04x)\n", ref_offset(ref), je16_to_cpu(n.nodetype)));
575
		goto out_erase_sem;
576
	}
577
	/* XXX FIXME: This is ugly now */
578
	n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
579
	ret = jffs2_flash_write(c, ref_offset(ref),
580
			sizeof(n), &retlen, (unsigned char *)&n);
581
	if (ret) {
582
		printk(KERN_WARNING "Write error in obliterating obsoleted node at 0x%08x: %d\n", ref_offset(ref), ret);
583
		goto out_erase_sem;
584
	}
585
	if (retlen != sizeof(n)) {
586
		printk(KERN_WARNING "Short write in obliterating obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
587
		goto out_erase_sem;
588
	}
589

590
	/* Nodes which have been marked obsolete no longer need to be
591
	 * associated with any inode. Remove them from the per-inode list.
592
	 *
593
	 * Note we can't do this for NAND at the moment because we need
594
	 * obsolete dirent nodes to stay on the lists, because of the
595
	 * horridness in jffs2_garbage_collect_deletion_dirent(). Also
596
	 * because we delete the inocache, and on NAND we need that to
597
	 * stay around until all the nodes are actually erased, in order
598
	 * to stop us from giving the same inode number to another newly
599
	 * created inode.
600
	 */
601
	if (ref->next_in_ino) {
602
		struct jffs2_inode_cache *ic;
603
		struct jffs2_raw_node_ref **p;
604

605
		spin_lock(&c->erase_completion_lock);
606

607
		ic = jffs2_raw_ref_to_ic(ref);
608
		for (p = &ic->nodes; (*p) != ref; p = &((*p)->next_in_ino))
609
			;
610

611
		*p = ref->next_in_ino;
612
		ref->next_in_ino = NULL;
613

614
		if (ic->nodes == (void *)ic && ic->nlink == 0) {
615
			jffs2_del_ino_cache(c, ic);
616
		}
617

618
		spin_unlock(&c->erase_completion_lock);
619
	}
620

621

622
	/* Merge with the next node in the physical list, if there is one
623
	 * and if it's also obsolete and if it doesn't belong to any inode
624
	 */
625
	if (ref->next_phys && ref_obsolete(ref->next_phys) &&
626
	    !ref->next_phys->next_in_ino) {
627
		struct jffs2_raw_node_ref *n = ref->next_phys;
628

629
		spin_lock(&c->erase_completion_lock);
630

631
		ref->__totlen += n->__totlen;
632
		ref->next_phys = n->next_phys;
633
        if (jeb->last_node == n)  {
634
        	jeb->last_node = ref;
635
        }
636
		if (jeb->gc_node == n) {
637
			/* gc will be happy continuing gc on this node */
638
			jeb->gc_node=ref;
639
		}
640
		spin_unlock(&c->erase_completion_lock);
641

642
		jffs2_free_raw_node_ref(n);
643
	}
644

645
	/* Also merge with the previous node in the list, if there is one
646
	 * and that one is obsolete
647
	 */
648
	if (ref != jeb->first_node ) {
649
		struct jffs2_raw_node_ref *p = jeb->first_node;
650

651
		spin_lock(&c->erase_completion_lock);
652

653
		while (p->next_phys != ref) {
654
			p = p->next_phys;
655
		}
656

657
		if (ref_obsolete(p) && !ref->next_in_ino) {
658
			p->__totlen += ref->__totlen;
659
			if (jeb->last_node == ref) {
660
				jeb->last_node = p;
661
			}
662
			if (jeb->gc_node == ref) {
663
				/* gc will be happy continuing gc on this node */
664
				jeb->gc_node=p;
665
			}
666
			p->next_phys = ref->next_phys;
667
			jffs2_free_raw_node_ref(ref);
668
		}
669
		spin_unlock(&c->erase_completion_lock);
670
	}
671
 out_erase_sem:
672
	up(&c->erase_free_sem);
673
}
674

675
int jffs2_thread_should_wake(struct jffs2_sb_info *c) {
676
	int ret = 0;
677
	uint32_t dirty;
678

679
	if (c->unchecked_size) {
680
		D1(printk( "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
681
			  c->unchecked_size, c->checked_ino));
682
		return 1;
683
	}
684

685
	/* dirty_size contains blocks on erase_pending_list
686
	 * those blocks are counted in c->nr_erasing_blocks.
687
	 * If one block is actually erased, it is not longer counted as dirty_space
688
	 * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
689
	 * with c->nr_erasing_blocks * c->sector_size again.
690
	 * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
691
	 * This helps us to force gc and pick eventually a clean block to spread the load.
692
	 */
693
	dirty = c->dirty_size + c->erasing_size -
694
			c->nr_erasing_blocks * c->sector_size;
695

696
	if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger &&
697
			(dirty > c->nospc_dirty_size)) {
698
		ret = 1;
699
	}
700

701
	D1(printk( "jffs2_thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x: %s\n",
702
		  c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size, ret?"yes":"no"));
703

704
	return ret;
705
}
706

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.