Blame view

fs/jffs2/gc.c 42.5 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
13
14
15
16
17
18
19
20
   */
  
  #include <linux/kernel.h>
  #include <linux/mtd/mtd.h>
  #include <linux/slab.h>
  #include <linux/pagemap.h>
  #include <linux/crc32.h>
  #include <linux/compiler.h>
  #include <linux/stat.h>
  #include "nodelist.h"
  #include "compr.h"
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
21
  static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
  					  struct jffs2_inode_cache *ic,
  					  struct jffs2_raw_node_ref *raw);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
24
  static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
  					struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
26
  static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
28
  static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
  static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  				      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
  				      uint32_t start, uint32_t end);
  static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
  				       uint32_t start, uint32_t end);
  static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
  			       struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
  
  /* Called with erase_completion_lock held */
  static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
  {
  	struct jffs2_eraseblock *ret;
  	struct list_head *nextlist = NULL;
  	int n = jiffies % 128;
  
  	/* Pick an eraseblock to garbage collect next. This is where we'll
  	   put the clever wear-levelling algorithms. Eventually.  */
  	/* We possibly want to favour the dirtier blocks more when the
  	   number of free blocks is low. */
a42163d7c   Artem B. Bityuckiy   [JFFS2] Improve g...
50
  again:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
  	if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
  		D1(printk(KERN_DEBUG "Picking block from bad_used_list to GC next
  "));
  		nextlist = &c->bad_used_list;
  	} else if (n < 50 && !list_empty(&c->erasable_list)) {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
56
  		/* Note that most of them will have gone directly to be erased.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  		   So don't favour the erasable_list _too_ much. */
  		D1(printk(KERN_DEBUG "Picking block from erasable_list to GC next
  "));
  		nextlist = &c->erasable_list;
  	} else if (n < 110 && !list_empty(&c->very_dirty_list)) {
  		/* Most of the time, pick one off the very_dirty list */
  		D1(printk(KERN_DEBUG "Picking block from very_dirty_list to GC next
  "));
  		nextlist = &c->very_dirty_list;
  	} else if (n < 126 && !list_empty(&c->dirty_list)) {
  		D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next
  "));
  		nextlist = &c->dirty_list;
  	} else if (!list_empty(&c->clean_list)) {
  		D1(printk(KERN_DEBUG "Picking block from clean_list to GC next
  "));
  		nextlist = &c->clean_list;
  	} else if (!list_empty(&c->dirty_list)) {
  		D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next (clean_list was empty)
  "));
  
  		nextlist = &c->dirty_list;
  	} else if (!list_empty(&c->very_dirty_list)) {
  		D1(printk(KERN_DEBUG "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)
  "));
  		nextlist = &c->very_dirty_list;
  	} else if (!list_empty(&c->erasable_list)) {
  		D1(printk(KERN_DEBUG "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)
  "));
  
  		nextlist = &c->erasable_list;
a42163d7c   Artem B. Bityuckiy   [JFFS2] Improve g...
88
89
90
91
  	} else if (!list_empty(&c->erasable_pending_wbuf_list)) {
  		/* There are blocks are wating for the wbuf sync */
  		D1(printk(KERN_DEBUG "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks
  "));
3cceb9f6c   Artem B. Bityuckiy   [JFFS2] Prevent d...
92
  		spin_unlock(&c->erase_completion_lock);
a42163d7c   Artem B. Bityuckiy   [JFFS2] Improve g...
93
  		jffs2_flush_wbuf_pad(c);
3cceb9f6c   Artem B. Bityuckiy   [JFFS2] Prevent d...
94
  		spin_lock(&c->erase_completion_lock);
a42163d7c   Artem B. Bityuckiy   [JFFS2] Improve g...
95
  		goto again;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  	} else {
  		/* Eep. All were empty */
  		D1(printk(KERN_NOTICE "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?
  "));
  		return NULL;
  	}
  
  	ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
  	list_del(&ret->list);
  	c->gcblock = ret;
  	ret->gc_node = ret->first_node;
  	if (!ret->gc_node) {
  		printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL
  ", ret->offset);
  		BUG();
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
112

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
115
116
117
118
119
120
121
  	/* Have we accidentally picked a clean block with wasted space ? */
  	if (ret->wasted_size) {
  		D1(printk(KERN_DEBUG "Converting wasted_size %08x to dirty_size
  ", ret->wasted_size));
  		ret->dirty_size += ret->wasted_size;
  		c->wasted_size -= ret->wasted_size;
  		c->dirty_size += ret->wasted_size;
  		ret->wasted_size = 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  	return ret;
  }
  
  /* jffs2_garbage_collect_pass
   * Make a single attempt to progress GC. Move one node, and possibly
   * start erasing one eraseblock.
   */
  int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
  {
  	struct jffs2_inode_info *f;
  	struct jffs2_inode_cache *ic;
  	struct jffs2_eraseblock *jeb;
  	struct jffs2_raw_node_ref *raw;
  	int ret = 0, inum, nlink;
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
136
  	int xattr = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
139
140
141
142
143
144
145
146
147
  
  	if (down_interruptible(&c->alloc_sem))
  		return -EINTR;
  
  	for (;;) {
  		spin_lock(&c->erase_completion_lock);
  		if (!c->unchecked_size)
  			break;
  
  		/* We can't start doing GC yet. We haven't finished checking
  		   the node CRCs etc. Do it now. */
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
148

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  		/* checked_ino is protected by the alloc_sem */
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
150
  		if (c->checked_ino > c->highest_ino && xattr) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
153
  			printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?
  ",
  			       c->unchecked_size);
e0c8e42f8   Artem B. Bityutskiy   [JFFS2] Debug cod...
154
  			jffs2_dbg_dump_block_lists_nolock(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  			spin_unlock(&c->erase_completion_lock);
44b998e1e   David Woodhouse   [JFFS2] Improve f...
156
157
  			up(&c->alloc_sem);
  			return -ENOSPC;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
  		}
  
  		spin_unlock(&c->erase_completion_lock);
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
161
162
  		if (!xattr)
  			xattr = jffs2_verify_xattr(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  		spin_lock(&c->inocache_lock);
  
  		ic = jffs2_get_ino_cache(c, c->checked_ino++);
  
  		if (!ic) {
  			spin_unlock(&c->inocache_lock);
  			continue;
  		}
  
  		if (!ic->nlink) {
  			D1(printk(KERN_DEBUG "Skipping check of ino #%d with nlink zero
  ",
  				  ic->ino));
  			spin_unlock(&c->inocache_lock);
355ed4e14   KaiGai Kohei   [JFFS2][XATTR] Fi...
177
  			jffs2_xattr_delete_inode(c, ic);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  			continue;
  		}
  		switch(ic->state) {
  		case INO_STATE_CHECKEDABSENT:
  		case INO_STATE_PRESENT:
  			D1(printk(KERN_DEBUG "Skipping ino #%u already checked
  ", ic->ino));
  			spin_unlock(&c->inocache_lock);
  			continue;
  
  		case INO_STATE_GC:
  		case INO_STATE_CHECKING:
  			printk(KERN_WARNING "Inode #%u is in state %d during CRC check phase!
  ", ic->ino, ic->state);
  			spin_unlock(&c->inocache_lock);
  			BUG();
  
  		case INO_STATE_READING:
  			/* We need to wait for it to finish, lest we move on
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
197
  			   and trigger the BUG() above while we haven't yet
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
200
  			   finished checking all its nodes */
  			D1(printk(KERN_DEBUG "Waiting for ino #%u to finish reading
  ", ic->ino));
d96fb997c   David Woodhouse   [JFFS2] Fix race ...
201
202
203
  			/* We need to come back again for the _same_ inode. We've
  			 made no progress in this case, but that should be OK */
  			c->checked_ino--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  			up(&c->alloc_sem);
  			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
  			return 0;
  
  		default:
  			BUG();
  
  		case INO_STATE_UNCHECKED:
  			;
  		}
  		ic->state = INO_STATE_CHECKING;
  		spin_unlock(&c->inocache_lock);
  
  		D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass() triggering inode scan of ino#%u
  ", ic->ino));
  
  		ret = jffs2_do_crccheck_inode(c, ic);
  		if (ret)
  			printk(KERN_WARNING "Returned error for crccheck of ino #%u. Expect badness...
  ", ic->ino);
  
  		jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
  		up(&c->alloc_sem);
  		return ret;
  	}
  
  	/* First, work out which block we're garbage-collecting */
  	jeb = c->gcblock;
  
  	if (!jeb)
  		jeb = jffs2_find_gc_block(c);
  
  	if (!jeb) {
  		D1 (printk(KERN_NOTICE "jffs2: Couldn't find erase block to garbage collect!
  "));
  		spin_unlock(&c->erase_completion_lock);
  		up(&c->alloc_sem);
  		return -EIO;
  	}
  
  	D1(printk(KERN_DEBUG "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x
  ", jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size));
  	D1(if (c->nextblock)
  	   printk(KERN_DEBUG "Nextblock at  %08x, used_size %08x, dirty_size %08x, wasted_size %08x, free_size %08x
  ", c->nextblock->offset, c->nextblock->used_size, c->nextblock->dirty_size, c->nextblock->wasted_size, c->nextblock->free_size));
  
  	if (!jeb->used_size) {
  		up(&c->alloc_sem);
  		goto eraseit;
  	}
  
  	raw = jeb->gc_node;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
256

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
259
  	while(ref_obsolete(raw)) {
  		D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping
  ", ref_offset(raw)));
99988f7bb   David Woodhouse   [JFFS2] Introduce...
260
  		raw = ref_next(raw);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
263
  		if (unlikely(!raw)) {
  			printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC
  ");
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
264
265
  			printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  			       jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size);
  			jeb->gc_node = raw;
  			spin_unlock(&c->erase_completion_lock);
  			up(&c->alloc_sem);
  			BUG();
  		}
  	}
  	jeb->gc_node = raw;
  
  	D1(printk(KERN_DEBUG "Going to garbage collect node at 0x%08x
  ", ref_offset(raw)));
  
  	if (!raw->next_in_ino) {
  		/* Inode-less node. Clean marker, snapshot or something like that */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
  		spin_unlock(&c->erase_completion_lock);
6171586a7   David Woodhouse   [JFFS2] Correct h...
281
282
283
284
285
286
287
  		if (ref_flags(raw) == REF_PRISTINE) {
  			/* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
  			jffs2_garbage_collect_pristine(c, NULL, raw);
  		} else {
  			/* Just mark it obsolete */
  			jffs2_mark_node_obsolete(c, raw);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
290
291
292
  		up(&c->alloc_sem);
  		goto eraseit_lock;
  	}
  
  	ic = jffs2_raw_ref_to_ic(raw);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
293
  #ifdef CONFIG_JFFS2_FS_XATTR
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
294
  	/* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
295
296
297
  	 * We can decide whether this node is inode or xattr by ic->class.     */
  	if (ic->class == RAWNODE_CLASS_XATTR_DATUM
  	    || ic->class == RAWNODE_CLASS_XATTR_REF) {
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
298
299
300
  		spin_unlock(&c->erase_completion_lock);
  
  		if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
301
  			ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
302
  		} else {
c9f700f84   KaiGai Kohei   [JFFS2][XATTR] us...
303
  			ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
304
  		}
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
305
  		goto release_sem;
084702e00   KaiGai Kohei   [JFFS2][XATTR] Re...
306
307
  	}
  #endif
aa98d7cf5   KaiGai Kohei   [JFFS2][XATTR] XA...
308

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
  	/* We need to hold the inocache. Either the erase_completion_lock or
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
310
  	   the inocache_lock are sufficient; we trade down since the inocache_lock
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  	   causes less contention. */
  	spin_lock(&c->inocache_lock);
  
  	spin_unlock(&c->erase_completion_lock);
  
  	D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass collecting from block @0x%08x. Node @0x%08x(%d), ino #%u
  ", jeb->offset, ref_offset(raw), ref_flags(raw), ic->ino));
  
  	/* Three possibilities:
  	   1. Inode is already in-core. We must iget it and do proper
  	      updating to its fragtree, etc.
  	   2. Inode is not in-core, node is REF_PRISTINE. We lock the
  	      inocache to prevent a read_inode(), copy the node intact.
  	   3. Inode is not in-core, node is not pristine. We must iget()
  	      and take the slow path.
  	*/
  
  	switch(ic->state) {
  	case INO_STATE_CHECKEDABSENT:
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
330
  		/* It's been checked, but it's not currently in-core.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
331
332
333
334
335
336
  		   We can just copy any pristine nodes, but have
  		   to prevent anyone else from doing read_inode() while
  		   we're at it, so we set the state accordingly */
  		if (ref_flags(raw) == REF_PRISTINE)
  			ic->state = INO_STATE_GC;
  		else {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
337
338
  			D1(printk(KERN_DEBUG "Ino #%u is absent but node not REF_PRISTINE. Reading.
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
342
343
344
345
346
347
348
349
350
  				  ic->ino));
  		}
  		break;
  
  	case INO_STATE_PRESENT:
  		/* It's in-core. GC must iget() it. */
  		break;
  
  	case INO_STATE_UNCHECKED:
  	case INO_STATE_CHECKING:
  	case INO_STATE_GC:
  		/* Should never happen. We should have finished checking
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
351
352
  		   by the time we actually start doing any GC, and since
  		   we're holding the alloc_sem, no other garbage collection
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  		   can happen.
  		*/
  		printk(KERN_CRIT "Inode #%u already in state %d in jffs2_garbage_collect_pass()!
  ",
  		       ic->ino, ic->state);
  		up(&c->alloc_sem);
  		spin_unlock(&c->inocache_lock);
  		BUG();
  
  	case INO_STATE_READING:
  		/* Someone's currently trying to read it. We must wait for
  		   them to finish and then go through the full iget() route
  		   to do the GC. However, sometimes read_inode() needs to get
  		   the alloc_sem() (for marking nodes invalid) so we must
  		   drop the alloc_sem before sleeping. */
  
  		up(&c->alloc_sem);
  		D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass() waiting for ino #%u in state %d
  ",
  			  ic->ino, ic->state));
  		sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
374
  		/* And because we dropped the alloc_sem we must start again from the
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
  		   beginning. Ponder chance of livelock here -- we're returning success
  		   without actually making any progress.
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
377
  		   Q: What are the chances that the inode is back in INO_STATE_READING
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
379
  		   again by the time we next enter this function? And that this happens
  		   enough times to cause a real delay?
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
380
  		   A: Small enough that I don't care :)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
  		*/
  		return 0;
  	}
  
  	/* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
386
  	   node intact, and we don't have to muck about with the fragtree etc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  	   because we know it's not in-core. If it _was_ in-core, we go through
  	   all the iget() crap anyway */
  
  	if (ic->state == INO_STATE_GC) {
  		spin_unlock(&c->inocache_lock);
  
  		ret = jffs2_garbage_collect_pristine(c, ic, raw);
  
  		spin_lock(&c->inocache_lock);
  		ic->state = INO_STATE_CHECKEDABSENT;
  		wake_up(&c->inocache_wq);
  
  		if (ret != -EBADFD) {
  			spin_unlock(&c->inocache_lock);
  			goto release_sem;
  		}
  
  		/* Fall through if it wanted us to, with inocache_lock held */
  	}
  
  	/* Prevent the fairly unlikely race where the gcblock is
  	   entirely obsoleted by the final close of a file which had
  	   the only valid nodes in the block, followed by erasure,
  	   followed by freeing of the ic because the erased block(s)
  	   held _all_ the nodes of that inode.... never been seen but
  	   it's vaguely possible. */
  
  	inum = ic->ino;
  	nlink = ic->nlink;
  	spin_unlock(&c->inocache_lock);
  
  	f = jffs2_gc_fetch_inode(c, inum, nlink);
  	if (IS_ERR(f)) {
  		ret = PTR_ERR(f);
  		goto release_sem;
  	}
  	if (!f) {
  		ret = 0;
  		goto release_sem;
  	}
  
  	ret = jffs2_garbage_collect_live(c, jeb, raw, f);
  
  	jffs2_gc_release_inode(c, f);
  
   release_sem:
  	up(&c->alloc_sem);
  
   eraseit_lock:
  	/* If we've finished this block, start it erasing */
  	spin_lock(&c->erase_completion_lock);
  
   eraseit:
  	if (c->gcblock && !c->gcblock->used_size) {
  		D1(printk(KERN_DEBUG "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list
  ", c->gcblock->offset));
  		/* We're GC'ing an empty block? */
  		list_add_tail(&c->gcblock->list, &c->erase_pending_list);
  		c->gcblock = NULL;
  		c->nr_erasing_blocks++;
  		jffs2_erase_pending_trigger(c);
  	}
  	spin_unlock(&c->erase_completion_lock);
  
  	return ret;
  }
  
  static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_eraseblock *jeb,
  				      struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
  {
  	struct jffs2_node_frag *frag;
  	struct jffs2_full_dnode *fn = NULL;
  	struct jffs2_full_dirent *fd;
  	uint32_t start = 0, end = 0, nrfrags = 0;
  	int ret = 0;
  
  	down(&f->sem);
  
  	/* Now we have the lock for this inode. Check that it's still the one at the head
  	   of the list. */
  
  	spin_lock(&c->erase_completion_lock);
  
  	if (c->gcblock != jeb) {
  		spin_unlock(&c->erase_completion_lock);
  		D1(printk(KERN_DEBUG "GC block is no longer gcblock. Restart
  "));
  		goto upnout;
  	}
  	if (ref_obsolete(raw)) {
  		spin_unlock(&c->erase_completion_lock);
  		D1(printk(KERN_DEBUG "node to be GC'd was obsoleted in the meantime.
  "));
  		/* They'll call again */
  		goto upnout;
  	}
  	spin_unlock(&c->erase_completion_lock);
  
  	/* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
  	if (f->metadata && f->metadata->raw == raw) {
  		fn = f->metadata;
  		ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
  		goto upnout;
  	}
  
  	/* FIXME. Read node and do lookup? */
  	for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
  		if (frag->node && frag->node->raw == raw) {
  			fn = frag->node;
  			end = frag->ofs + frag->size;
  			if (!nrfrags++)
  				start = frag->ofs;
  			if (nrfrags == frag->node->frags)
  				break; /* We've found them all */
  		}
  	}
  	if (fn) {
  		if (ref_flags(raw) == REF_PRISTINE) {
  			ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
  			if (!ret) {
  				/* Urgh. Return it sensibly. */
  				frag->node->raw = f->inocache->nodes;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
509
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
513
514
515
516
517
518
519
520
521
522
  			if (ret != -EBADFD)
  				goto upnout;
  		}
  		/* We found a datanode. Do the GC */
  		if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
  			/* It crosses a page boundary. Therefore, it must be a hole. */
  			ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
  		} else {
  			/* It could still be a hole. But we GC the page this way anyway */
  			ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
  		}
  		goto upnout;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
523

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
  	/* Wasn't a dnode. Try dirent */
  	for (fd = f->dents; fd; fd=fd->next) {
  		if (fd->raw == raw)
  			break;
  	}
  
  	if (fd && fd->ino) {
  		ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
  	} else if (fd) {
  		ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
  	} else {
  		printk(KERN_WARNING "Raw node at 0x%08x wasn't in node lists for ino #%u
  ",
  		       ref_offset(raw), f->inocache->ino);
  		if (ref_obsolete(raw)) {
  			printk(KERN_WARNING "But it's obsolete so we don't mind too much
  ");
  		} else {
e0c8e42f8   Artem B. Bityutskiy   [JFFS2] Debug cod...
542
543
  			jffs2_dbg_dump_node(c, ref_offset(raw));
  			BUG();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
544
545
546
547
548
549
550
  		}
  	}
   upnout:
  	up(&f->sem);
  
  	return ret;
  }
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
551
  static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
  					  struct jffs2_inode_cache *ic,
  					  struct jffs2_raw_node_ref *raw)
  {
  	union jffs2_node_union *node;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
559
560
561
562
563
  	size_t retlen;
  	int ret;
  	uint32_t phys_ofs, alloclen;
  	uint32_t crc, rawlen;
  	int retried = 0;
  
  	D1(printk(KERN_DEBUG "Going to GC REF_PRISTINE node at 0x%08x
  ", ref_offset(raw)));
6171586a7   David Woodhouse   [JFFS2] Correct h...
564
  	alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
565
566
567
568
  
  	/* Ask for a small amount of space (or the totlen if smaller) because we
  	   don't want to force wastage of the end of a block if splitting would
  	   work. */
6171586a7   David Woodhouse   [JFFS2] Correct h...
569
570
  	if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
  		alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
571
  	ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
6171586a7   David Woodhouse   [JFFS2] Correct h...
572
  	/* 'rawlen' is not the exact summary size; it is only an upper estimation */
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
573

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
  	if (ret)
  		return ret;
  
  	if (alloclen < rawlen) {
  		/* Doesn't fit untouched. We'll go the old route and split it */
  		return -EBADFD;
  	}
  
  	node = kmalloc(rawlen, GFP_KERNEL);
  	if (!node)
                 return -ENOMEM;
  
  	ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
  	if (!ret && retlen != rawlen)
  		ret = -EIO;
  	if (ret)
  		goto out_node;
  
  	crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
  	if (je32_to_cpu(node->u.hdr_crc) != crc) {
  		printk(KERN_WARNING "Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x
  ",
  		       ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
  		goto bail;
  	}
  
  	switch(je16_to_cpu(node->u.nodetype)) {
  	case JFFS2_NODETYPE_INODE:
  		crc = crc32(0, node, sizeof(node->i)-8);
  		if (je32_to_cpu(node->i.node_crc) != crc) {
  			printk(KERN_WARNING "Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x
  ",
  			       ref_offset(raw), je32_to_cpu(node->i.node_crc), crc);
  			goto bail;
  		}
  
  		if (je32_to_cpu(node->i.dsize)) {
  			crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
  			if (je32_to_cpu(node->i.data_crc) != crc) {
  				printk(KERN_WARNING "Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x
  ",
  				       ref_offset(raw), je32_to_cpu(node->i.data_crc), crc);
  				goto bail;
  			}
  		}
  		break;
  
  	case JFFS2_NODETYPE_DIRENT:
  		crc = crc32(0, node, sizeof(node->d)-8);
  		if (je32_to_cpu(node->d.node_crc) != crc) {
  			printk(KERN_WARNING "Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x
  ",
  			       ref_offset(raw), je32_to_cpu(node->d.node_crc), crc);
  			goto bail;
  		}
  
  		if (node->d.nsize) {
  			crc = crc32(0, node->d.name, node->d.nsize);
  			if (je32_to_cpu(node->d.name_crc) != crc) {
  				printk(KERN_WARNING "Name CRC failed on REF_PRISTINE dirent ode at 0x%08x: Read 0x%08x, calculated 0x%08x
  ",
  				       ref_offset(raw), je32_to_cpu(node->d.name_crc), crc);
  				goto bail;
  			}
  		}
  		break;
  	default:
6171586a7   David Woodhouse   [JFFS2] Correct h...
641
642
643
644
645
646
647
  		/* If it's inode-less, we don't _know_ what it is. Just copy it intact */
  		if (ic) {
  			printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x
  ",
  			       ref_offset(raw), je16_to_cpu(node->u.nodetype));
  			goto bail;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
650
  	/* OK, all the CRCs are good; this node can just be copied as-is. */
   retry:
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
651
  	phys_ofs = write_ofs(c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
654
655
656
657
  
  	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
  
  	if (ret || (retlen != rawlen)) {
  		printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd
  ",
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
658
                         rawlen, phys_ofs, ret, retlen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
  		if (retlen) {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
660
  			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
  		} else {
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
662
663
  			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero
  ", phys_ofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
  		}
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
665
  		if (!retried) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
668
669
670
671
672
673
  			/* Try to reallocate space and retry */
  			uint32_t dummy;
  			struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
  
  			retried = 1;
  
  			D1(printk(KERN_DEBUG "Retrying failed write of REF_PRISTINE node.
  "));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
674

730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
675
676
  			jffs2_dbg_acct_sanity_check(c,jeb);
  			jffs2_dbg_acct_paranoia_check(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
678
  			ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
679
680
  						/* this is not the exact summary size of it,
  							it is only an upper estimation */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
681
682
683
684
  
  			if (!ret) {
  				D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.
  ", phys_ofs));
730554d94   Artem B. Bityutskiy   [JFFS2] Debug cod...
685
686
  				jffs2_dbg_acct_sanity_check(c,jeb);
  				jffs2_dbg_acct_paranoia_check(c, jeb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
689
690
691
  
  				goto retry;
  			}
  			D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!
  ", ret));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
694
695
696
  		if (!ret)
  			ret = -EIO;
  		goto out_node;
  	}
2f785402f   David Woodhouse   [JFFS2] Reduce vi...
697
  	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
700
701
702
703
704
705
706
707
708
709
  	jffs2_mark_node_obsolete(c, raw);
  	D1(printk(KERN_DEBUG "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded
  ", ref_offset(raw)));
  
   out_node:
  	kfree(node);
  	return ret;
   bail:
  	ret = -EBADFD;
  	goto out_node;
  }
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
710
  static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
714
  					struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
  {
  	struct jffs2_full_dnode *new_fn;
  	struct jffs2_raw_inode ri;
8557fd51c   Artem B. Bityuckiy   [JFFS2] Fix race ...
715
  	struct jffs2_node_frag *last_frag;
aef9ab478   David Woodhouse   [JFFS2] Support n...
716
  	union jffs2_device_node dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
  	char *mdata = NULL, mdatalen = 0;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
718
  	uint32_t alloclen, ilen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
720
721
722
723
  	int ret;
  
  	if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
  	    S_ISCHR(JFFS2_F_I_MODE(f)) ) {
  		/* For these, we don't actually need to read the old node */
aef9ab478   David Woodhouse   [JFFS2] Support n...
724
  		mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
  		mdata = (char *)&dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  		D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t
  ", mdatalen));
  	} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
  		mdatalen = fn->size;
  		mdata = kmalloc(fn->size, GFP_KERNEL);
  		if (!mdata) {
  			printk(KERN_WARNING "kmalloc of mdata failed in jffs2_garbage_collect_metadata()
  ");
  			return -ENOMEM;
  		}
  		ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
  		if (ret) {
  			printk(KERN_WARNING "read of old metadata failed in jffs2_garbage_collect_metadata(): %d
  ", ret);
  			kfree(mdata);
  			return ret;
  		}
  		D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bites of symlink target
  ", mdatalen));
  
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
747

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
748
  	ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
749
  				JFFS2_SUMMARY_INODE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
751
752
753
754
755
  	if (ret) {
  		printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d
  ",
  		       sizeof(ri)+ mdatalen, ret);
  		goto out;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
756

8557fd51c   Artem B. Bityuckiy   [JFFS2] Fix race ...
757
758
759
760
761
762
763
  	last_frag = frag_last(&f->fragtree);
  	if (last_frag)
  		/* Fetch the inode length from the fragtree rather then
  		 * from i_size since i_size may have not been updated yet */
  		ilen = last_frag->ofs + last_frag->size;
  	else
  		ilen = JFFS2_F_I_SIZE(f);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
764

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
766
767
768
769
770
771
772
773
774
775
  	memset(&ri, 0, sizeof(ri));
  	ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  	ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
  	ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
  
  	ri.ino = cpu_to_je32(f->inocache->ino);
  	ri.version = cpu_to_je32(++f->highest_version);
  	ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
  	ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
  	ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
8557fd51c   Artem B. Bityuckiy   [JFFS2] Fix race ...
776
  	ri.isize = cpu_to_je32(ilen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
779
780
781
782
783
784
785
  	ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
  	ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
  	ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
  	ri.offset = cpu_to_je32(0);
  	ri.csize = cpu_to_je32(mdatalen);
  	ri.dsize = cpu_to_je32(mdatalen);
  	ri.compr = JFFS2_COMPR_NONE;
  	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
  	ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
786
  	new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
  
  	if (IS_ERR(new_fn)) {
  		printk(KERN_WARNING "Error writing new dnode: %ld
  ", PTR_ERR(new_fn));
  		ret = PTR_ERR(new_fn);
  		goto out;
  	}
  	jffs2_mark_node_obsolete(c, fn->raw);
  	jffs2_free_full_dnode(fn);
  	f->metadata = new_fn;
   out:
  	if (S_ISLNK(JFFS2_F_I_MODE(f)))
  		kfree(mdata);
  	return ret;
  }
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
802
  static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
803
804
805
806
  					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
  {
  	struct jffs2_full_dirent *new_fd;
  	struct jffs2_raw_dirent rd;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
807
  	uint32_t alloclen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
809
810
811
812
813
814
815
816
817
818
  	int ret;
  
  	rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  	rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  	rd.nsize = strlen(fd->name);
  	rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
  	rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
  
  	rd.pino = cpu_to_je32(f->inocache->ino);
  	rd.version = cpu_to_je32(++f->highest_version);
  	rd.ino = cpu_to_je32(fd->ino);
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
819
820
821
822
  	/* If the times on this inode were set by explicit utime() they can be different,
  	   so refrain from splatting them. */
  	if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
  		rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
823
  	else
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
824
  		rd.mctime = cpu_to_je32(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
826
827
  	rd.type = fd->type;
  	rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
  	rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
828

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
829
  	ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
830
  				JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
831
832
833
834
835
836
  	if (ret) {
  		printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d
  ",
  		       sizeof(rd)+rd.nsize, ret);
  		return ret;
  	}
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
837
  	new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
838
839
840
841
842
843
844
845
846
  
  	if (IS_ERR(new_fd)) {
  		printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld
  ", PTR_ERR(new_fd));
  		return PTR_ERR(new_fd);
  	}
  	jffs2_add_fd_to_list(c, new_fd, &f->dents);
  	return 0;
  }
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
847
  static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  					struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
  {
  	struct jffs2_full_dirent **fdp = &f->dents;
  	int found = 0;
  
  	/* On a medium where we can't actually mark nodes obsolete
  	   pernamently, such as NAND flash, we need to work out
  	   whether this deletion dirent is still needed to actively
  	   delete a 'real' dirent with the same name that's still
  	   somewhere else on the flash. */
  	if (!jffs2_can_mark_obsolete(c)) {
  		struct jffs2_raw_dirent *rd;
  		struct jffs2_raw_node_ref *raw;
  		int ret;
  		size_t retlen;
  		int name_len = strlen(fd->name);
  		uint32_t name_crc = crc32(0, fd->name, name_len);
  		uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
  
  		rd = kmalloc(rawlen, GFP_KERNEL);
  		if (!rd)
  			return -ENOMEM;
  
  		/* Prevent the erase code from nicking the obsolete node refs while
  		   we're looking at them. I really don't like this extra lock but
  		   can't see any alternative. Suggestions on a postcard to... */
  		down(&c->erase_free_sem);
  
  		for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
aba54da3d   Artem Bityutskiy   [JFFS2] add cond_...
877
  			cond_resched();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
879
880
881
882
883
884
  			/* We only care about obsolete ones */
  			if (!(ref_obsolete(raw)))
  				continue;
  
  			/* Any dirent with the same name is going to have the same length... */
  			if (ref_totlen(c, NULL, raw) != rawlen)
  				continue;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
885
  			/* Doesn't matter if there's one in the same erase block. We're going to
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
886
  			   delete it too at the same time. */
3be36675d   Andrew Victor   [JFFS2] Core chan...
887
  			if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
  				continue;
  
  			D1(printk(KERN_DEBUG "Check potential deletion dirent at %08x
  ", ref_offset(raw)));
  
  			/* This is an obsolete node belonging to the same directory, and it's of the right
  			   length. We need to take a closer look...*/
  			ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
  			if (ret) {
  				printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Read error (%d) reading obsolete node at %08x
  ", ret, ref_offset(raw));
  				/* If we can't read it, we don't need to continue to obsolete it. Continue */
  				continue;
  			}
  			if (retlen != rawlen) {
  				printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Short read (%zd not %u) reading header from obsolete node at %08x
  ",
  				       retlen, rawlen, ref_offset(raw));
  				continue;
  			}
  
  			if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
  				continue;
  
  			/* If the name CRC doesn't match, skip */
  			if (je32_to_cpu(rd->name_crc) != name_crc)
  				continue;
  
  			/* If the name length doesn't match, or it's another deletion dirent, skip */
  			if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
  				continue;
  
  			/* OK, check the actual name now */
  			if (memcmp(rd->name, fd->name, name_len))
  				continue;
  
  			/* OK. The name really does match. There really is still an older node on
  			   the flash which our deletion dirent obsoletes. So we have to write out
  			   a new deletion dirent to replace it */
  			up(&c->erase_free_sem);
  
  			D1(printk(KERN_DEBUG "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u
  ",
  				  ref_offset(fd->raw), fd->name, ref_offset(raw), je32_to_cpu(rd->ino)));
  			kfree(rd);
  
  			return jffs2_garbage_collect_dirent(c, jeb, f, fd);
  		}
  
  		up(&c->erase_free_sem);
  		kfree(rd);
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
940
  	/* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
3a69e0cd2   Artem B. Bityutskiy   [JFFS2] Fix JFFS2...
941
  	   we should update the metadata node with those times accordingly */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
  	/* No need for it any more. Just mark it obsolete and remove it from the list */
  	while (*fdp) {
  		if ((*fdp) == fd) {
  			found = 1;
  			*fdp = fd->next;
  			break;
  		}
  		fdp = &(*fdp)->next;
  	}
  	if (!found) {
  		printk(KERN_WARNING "Deletion dirent \"%s\" not found in list for ino #%u
  ", fd->name, f->inocache->ino);
  	}
  	jffs2_mark_node_obsolete(c, fd->raw);
  	jffs2_free_full_dirent(fd);
  	return 0;
  }
  
  static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  				      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
  				      uint32_t start, uint32_t end)
  {
  	struct jffs2_raw_inode ri;
  	struct jffs2_node_frag *frag;
  	struct jffs2_full_dnode *new_fn;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
967
  	uint32_t alloclen, ilen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
969
970
971
972
  	int ret;
  
  	D1(printk(KERN_DEBUG "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x
  ",
  		  f->inocache->ino, start, end));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
973

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
975
976
977
978
  	memset(&ri, 0, sizeof(ri));
  
  	if(fn->frags > 1) {
  		size_t readlen;
  		uint32_t crc;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
979
  		/* It's partially obsoleted by a later write. So we have to
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  		   write it out again with the _same_ version as before */
  		ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
  		if (readlen != sizeof(ri) || ret) {
  			printk(KERN_WARNING "Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node
  ", ret, readlen);
  			goto fill;
  		}
  		if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
  			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)
  ",
  			       ref_offset(fn->raw),
  			       je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
  			return -EIO;
  		}
  		if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
  			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had totlen 0x%x instead of expected 0x%zx
  ",
  			       ref_offset(fn->raw),
  			       je32_to_cpu(ri.totlen), sizeof(ri));
  			return -EIO;
  		}
  		crc = crc32(0, &ri, sizeof(ri)-8);
  		if (crc != je32_to_cpu(ri.node_crc)) {
  			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x
  ",
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1005
  			       ref_offset(fn->raw),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1006
1007
  			       je32_to_cpu(ri.node_crc), crc);
  			/* FIXME: We could possibly deal with this by writing new holes for each frag */
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1008
1009
  			printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
1013
1014
1015
  			       start, end, f->inocache->ino);
  			goto fill;
  		}
  		if (ri.compr != JFFS2_COMPR_ZERO) {
  			printk(KERN_WARNING "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!
  ", ref_offset(fn->raw));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1016
1017
  			printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
  			       start, end, f->inocache->ino);
  			goto fill;
  		}
  	} else {
  	fill:
  		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  		ri.totlen = cpu_to_je32(sizeof(ri));
  		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
  
  		ri.ino = cpu_to_je32(f->inocache->ino);
  		ri.version = cpu_to_je32(++f->highest_version);
  		ri.offset = cpu_to_je32(start);
  		ri.dsize = cpu_to_je32(end - start);
  		ri.csize = cpu_to_je32(0);
  		ri.compr = JFFS2_COMPR_ZERO;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1035

8557fd51c   Artem B. Bityuckiy   [JFFS2] Fix race ...
1036
1037
1038
1039
1040
1041
1042
  	frag = frag_last(&f->fragtree);
  	if (frag)
  		/* Fetch the inode length from the fragtree rather then
  		 * from i_size since i_size may have not been updated yet */
  		ilen = frag->ofs + frag->size;
  	else
  		ilen = JFFS2_F_I_SIZE(f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1043
1044
1045
  	ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
  	ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
  	ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
8557fd51c   Artem B. Bityuckiy   [JFFS2] Fix race ...
1046
  	ri.isize = cpu_to_je32(ilen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1047
1048
1049
1050
1051
  	ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
  	ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
  	ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
  	ri.data_crc = cpu_to_je32(0);
  	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1052
1053
  	ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
  				     JFFS2_SUMMARY_INODE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
1055
1056
1057
1058
1059
  	if (ret) {
  		printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d
  ",
  		       sizeof(ri), ret);
  		return ret;
  	}
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1060
  	new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
  
  	if (IS_ERR(new_fn)) {
  		printk(KERN_WARNING "Error writing new hole node: %ld
  ", PTR_ERR(new_fn));
  		return PTR_ERR(new_fn);
  	}
  	if (je32_to_cpu(ri.version) == f->highest_version) {
  		jffs2_add_full_dnode_to_inode(c, f, new_fn);
  		if (f->metadata) {
  			jffs2_mark_node_obsolete(c, f->metadata->raw);
  			jffs2_free_full_dnode(f->metadata);
  			f->metadata = NULL;
  		}
  		return 0;
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1076
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1077
1078
  	 * We should only get here in the case where the node we are
  	 * replacing had more than one frag, so we kept the same version
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1079
  	 * number as before. (Except in case of error -- see 'goto fill;'
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
  	 * above.)
  	 */
  	D1(if(unlikely(fn->frags <= 1)) {
  		printk(KERN_WARNING "jffs2_garbage_collect_hole: Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d
  ",
  		       fn->frags, je32_to_cpu(ri.version), f->highest_version,
  		       je32_to_cpu(ri.ino));
  	});
  
  	/* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
  	mark_ref_normal(new_fn->raw);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1091
  	for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
  	     frag; frag = frag_next(frag)) {
  		if (frag->ofs > fn->size + fn->ofs)
  			break;
  		if (frag->node == fn) {
  			frag->node = new_fn;
  			new_fn->frags++;
  			fn->frags--;
  		}
  	}
  	if (fn->frags) {
  		printk(KERN_WARNING "jffs2_garbage_collect_hole: Old node still has frags!
  ");
  		BUG();
  	}
  	if (!new_fn->frags) {
  		printk(KERN_WARNING "jffs2_garbage_collect_hole: New node has no frags!
  ");
  		BUG();
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1111

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
1113
  	jffs2_mark_node_obsolete(c, fn->raw);
  	jffs2_free_full_dnode(fn);
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1114

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115
1116
1117
1118
1119
1120
1121
1122
1123
  	return 0;
  }
  
  static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
  				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
  				       uint32_t start, uint32_t end)
  {
  	struct jffs2_full_dnode *new_fn;
  	struct jffs2_raw_inode ri;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1124
  	uint32_t alloclen, offset, orig_end, orig_start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1125
1126
1127
1128
  	int ret = 0;
  	unsigned char *comprbuf = NULL, *writebuf;
  	unsigned long pg;
  	unsigned char *pg_ptr;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1129

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  	memset(&ri, 0, sizeof(ri));
  
  	D1(printk(KERN_DEBUG "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x
  ",
  		  f->inocache->ino, start, end));
  
  	orig_end = end;
  	orig_start = start;
  
  	if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
  		/* Attempt to do some merging. But only expand to cover logically
  		   adjacent frags if the block containing them is already considered
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1142
1143
  		   to be dirty. Otherwise we end up with GC just going round in
  		   circles dirtying the nodes it already wrote out, especially
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
  		   on NAND where we have small eraseblocks and hence a much higher
  		   chance of nodes having to be split to cross boundaries. */
  
  		struct jffs2_node_frag *frag;
  		uint32_t min, max;
  
  		min = start & ~(PAGE_CACHE_SIZE-1);
  		max = min + PAGE_CACHE_SIZE;
  
  		frag = jffs2_lookup_node_frag(&f->fragtree, start);
  
  		/* BUG_ON(!frag) but that'll happen anyway... */
  
  		BUG_ON(frag->ofs != start);
  
  		/* First grow down... */
  		while((frag = frag_prev(frag)) && frag->ofs >= min) {
  
  			/* If the previous frag doesn't even reach the beginning, there's
  			   excessive fragmentation. Just merge. */
  			if (frag->ofs > min) {
  				D1(printk(KERN_DEBUG "Expanding down to cover partial frag (0x%x-0x%x)
  ",
  					  frag->ofs, frag->ofs+frag->size));
  				start = frag->ofs;
  				continue;
  			}
  			/* OK. This frag holds the first byte of the page. */
  			if (!frag->node || !frag->node->raw) {
  				D1(printk(KERN_DEBUG "First frag in page is hole (0x%x-0x%x). Not expanding down.
  ",
  					  frag->ofs, frag->ofs+frag->size));
  				break;
  			} else {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1178
  				/* OK, it's a frag which extends to the beginning of the page. Does it live
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
  				   in a block which is still considered clean? If so, don't obsolete it.
  				   If not, cover it anyway. */
  
  				struct jffs2_raw_node_ref *raw = frag->node->raw;
  				struct jffs2_eraseblock *jeb;
  
  				jeb = &c->blocks[raw->flash_offset / c->sector_size];
  
  				if (jeb == c->gcblock) {
  					D1(printk(KERN_DEBUG "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x
  ",
  						  frag->ofs, frag->ofs+frag->size, ref_offset(raw)));
  					start = frag->ofs;
  					break;
  				}
  				if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
  					D1(printk(KERN_DEBUG "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x
  ",
  						  frag->ofs, frag->ofs+frag->size, jeb->offset));
  					break;
  				}
  
  				D1(printk(KERN_DEBUG "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x
  ",
  						  frag->ofs, frag->ofs+frag->size, jeb->offset));
  				start = frag->ofs;
  				break;
  			}
  		}
  
  		/* ... then up */
  
  		/* Find last frag which is actually part of the node we're to GC. */
  		frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
  
  		while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
  
  			/* If the previous frag doesn't even reach the beginning, there's lots
  			   of fragmentation. Just merge. */
  			if (frag->ofs+frag->size < max) {
  				D1(printk(KERN_DEBUG "Expanding up to cover partial frag (0x%x-0x%x)
  ",
  					  frag->ofs, frag->ofs+frag->size));
  				end = frag->ofs + frag->size;
  				continue;
  			}
  
  			if (!frag->node || !frag->node->raw) {
  				D1(printk(KERN_DEBUG "Last frag in page is hole (0x%x-0x%x). Not expanding up.
  ",
  					  frag->ofs, frag->ofs+frag->size));
  				break;
  			} else {
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1232
  				/* OK, it's a frag which extends to the beginning of the page. Does it live
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
  				   in a block which is still considered clean? If so, don't obsolete it.
  				   If not, cover it anyway. */
  
  				struct jffs2_raw_node_ref *raw = frag->node->raw;
  				struct jffs2_eraseblock *jeb;
  
  				jeb = &c->blocks[raw->flash_offset / c->sector_size];
  
  				if (jeb == c->gcblock) {
  					D1(printk(KERN_DEBUG "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x
  ",
  						  frag->ofs, frag->ofs+frag->size, ref_offset(raw)));
  					end = frag->ofs + frag->size;
  					break;
  				}
  				if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
  					D1(printk(KERN_DEBUG "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x
  ",
  						  frag->ofs, frag->ofs+frag->size, jeb->offset));
  					break;
  				}
  
  				D1(printk(KERN_DEBUG "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x
  ",
  						  frag->ofs, frag->ofs+frag->size, jeb->offset));
  				end = frag->ofs + frag->size;
  				break;
  			}
  		}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1262
1263
  		D1(printk(KERN_DEBUG "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
  			  orig_start, orig_end, start, end));
8557fd51c   Artem B. Bityuckiy   [JFFS2] Fix race ...
1265
  		D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
1267
1268
  		BUG_ON(end < orig_end);
  		BUG_ON(start > orig_start);
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1269

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
  	/* First, use readpage() to read the appropriate page into the page cache */
  	/* Q: What happens if we actually try to GC the _same_ page for which commit_write()
  	 *    triggered garbage collection in the first place?
  	 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
  	 *    page OK. We'll actually write it out again in commit_write, which is a little
  	 *    suboptimal, but at least we're correct.
  	 */
  	pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
  
  	if (IS_ERR(pg_ptr)) {
  		printk(KERN_WARNING "read_cache_page() returned error: %ld
  ", PTR_ERR(pg_ptr));
  		return PTR_ERR(pg_ptr);
  	}
  
  	offset = start;
  	while(offset < orig_end) {
  		uint32_t datalen;
  		uint32_t cdatalen;
  		uint16_t comprtype = JFFS2_COMPR_NONE;
9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1290
  		ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
e631ddba5   Ferenc Havasi   [JFFS2] Add erase...
1291
  					&alloclen, JFFS2_SUMMARY_INODE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
  
  		if (ret) {
  			printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d
  ",
  			       sizeof(ri)+ JFFS2_MIN_DATA_LEN, ret);
  			break;
  		}
  		cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
  		datalen = end - offset;
  
  		writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
  
  		comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
  
  		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  		ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
  		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
  
  		ri.ino = cpu_to_je32(f->inocache->ino);
  		ri.version = cpu_to_je32(++f->highest_version);
  		ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
  		ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
  		ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
  		ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
  		ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
  		ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
  		ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
  		ri.offset = cpu_to_je32(offset);
  		ri.csize = cpu_to_je32(cdatalen);
  		ri.dsize = cpu_to_je32(datalen);
  		ri.compr = comprtype & 0xff;
  		ri.usercompr = (comprtype >> 8) & 0xff;
  		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
  		ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
1327

9fe4854cd   David Woodhouse   [JFFS2] Remove fl...
1328
  		new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
  
  		jffs2_free_comprbuf(comprbuf, writebuf);
  
  		if (IS_ERR(new_fn)) {
  			printk(KERN_WARNING "Error writing new dnode: %ld
  ", PTR_ERR(new_fn));
  			ret = PTR_ERR(new_fn);
  			break;
  		}
  		ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
  		offset += datalen;
  		if (f->metadata) {
  			jffs2_mark_node_obsolete(c, f->metadata->raw);
  			jffs2_free_full_dnode(f->metadata);
  			f->metadata = NULL;
  		}
  	}
  
  	jffs2_gc_release_page(c, pg_ptr, &pg);
  	return ret;
  }