blob: 09f16fb45cee42deef5ccaca15a68fccffb61163 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6 *
7 * Created by David Woodhouse <dwmw2@infradead.org>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/kernel.h>
16#include <linux/mtd/mtd.h>
17#include <linux/slab.h>
18#include <linux/pagemap.h>
19#include <linux/crc32.h>
20#include <linux/compiler.h>
21#include <linux/stat.h>
22#include "nodelist.h"
23#include "compr.h"
24
25extern void jffs2_close_nextblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
26extern int jffs2_find_nextblock(struct jffs2_sb_info *c);
27
28static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
29 struct jffs2_inode_cache *ic,
30 struct jffs2_raw_node_ref *raw);
31static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
32 struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
33static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
34 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
35static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
36 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
37static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
38 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
39 uint32_t start, uint32_t end);
40static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
41 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
42 uint32_t start, uint32_t end);
43static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
44 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
45
46/* Called with erase_completion_lock held */
47static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
48{
49 struct jffs2_eraseblock *ret;
50 struct list_head *nextlist = NULL;
51 int n = jiffies % 128;
52
53 if(c->flags & JFFS2_SB_FLAG_QUICK_GC)
54 n = 1;
55 /* Pick an eraseblock to garbage collect next. This is where we'll
56 put the clever wear-levelling algorithms. Eventually. */
57 /* We possibly want to favour the dirtier blocks more when the
58 number of free blocks is low. */
59again:
60 if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
61 jffs2_dbg(1, "Picking block from bad_used_list to GC next\n");
62 nextlist = &c->bad_used_list;
63 } else if (n < 50 && !list_empty(&c->erasable_list)) {
64 /* Note that most of them will have gone directly to be erased.
65 So don't favour the erasable_list _too_ much. */
66 jffs2_dbg(1, "Picking block from erasable_list to GC next\n");
67 nextlist = &c->erasable_list;
68 } else if (n < 110 && !list_empty(&c->very_dirty_list)) {
69 /* Most of the time, pick one off the very_dirty list */
70 jffs2_dbg(1, "Picking block from very_dirty_list to GC next\n");
71 nextlist = &c->very_dirty_list;
72 } else if (n < 126 && !list_empty(&c->dirty_list)) {
73 jffs2_dbg(1, "Picking block from dirty_list to GC next\n");
74 nextlist = &c->dirty_list;
75 } else if ((!(c->flags & JFFS2_SB_FLAG_QUICK_GC)) && (!list_empty(&c->clean_list))) {
76 jffs2_dbg(1, "Picking block from clean_list to GC next\n");
77 nextlist = &c->clean_list;
78 } else if (!list_empty(&c->dirty_list)) {
79 jffs2_dbg(1, "Picking block from dirty_list to GC next (clean_list was empty)\n");
80
81 nextlist = &c->dirty_list;
82 } else if (!list_empty(&c->very_dirty_list)) {
83 jffs2_dbg(1, "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n");
84 nextlist = &c->very_dirty_list;
85 } else if (!list_empty(&c->erasable_list)) {
86 jffs2_dbg(1, "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n");
87
88 nextlist = &c->erasable_list;
89 } else if (!list_empty(&c->erasable_pending_wbuf_list)) {
90 /* There are blocks are wating for the wbuf sync */
91 jffs2_dbg(1, "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n");
92 spin_unlock(&c->erase_completion_lock);
93 jffs2_flush_wbuf_pad(c);
94 spin_lock(&c->erase_completion_lock);
95 goto again;
96 } else {
97 /* Eep. All were empty */
98 jffs2_dbg(1, "No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n");
99 return NULL;
100 }
101
102 ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
103 list_del(&ret->list);
104 c->gcblock = ret;
105 ret->gc_node = ret->first_node;
106 if (!ret->gc_node) {
107 pr_warn("Eep. ret->gc_node for block at 0x%08x is NULL\n",
108 ret->offset);
109 BUG();
110 }
111
112 /* Have we accidentally picked a clean block with wasted space ? */
113 if (ret->wasted_size) {
114 jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n",
115 ret->wasted_size);
116 ret->dirty_size += ret->wasted_size;
117 c->wasted_size -= ret->wasted_size;
118 c->dirty_size += ret->wasted_size;
119 ret->wasted_size = 0;
120 }
121
122 return ret;
123}
124
125/* jffs2_garbage_collect_pass
126 * Make a single attempt to progress GC. Move one node, and possibly
127 * start erasing one eraseblock.
128 */
129int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
130{
131 struct jffs2_inode_info *f;
132 struct jffs2_inode_cache *ic;
133 struct jffs2_eraseblock *jeb;
134 struct jffs2_raw_node_ref *raw;
135 uint32_t gcblock_dirty;
136 int ret = 0, inum, nlink;
137 int xattr = 0;
138
139 if (mutex_lock_interruptible(&c->alloc_sem))
140 return -EINTR;
141
142 for (;;) {
143 spin_lock(&c->erase_completion_lock);
144 if (!c->unchecked_size)
145 break;
146
147 /* We can't start doing GC yet. We haven't finished checking
148 the node CRCs etc. Do it now. */
149
150 /* checked_ino is protected by the alloc_sem */
151 if (c->checked_ino > c->highest_ino && xattr) {
152 pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
153 c->unchecked_size);
154 jffs2_dbg_dump_block_lists_nolock(c);
155 spin_unlock(&c->erase_completion_lock);
156 mutex_unlock(&c->alloc_sem);
157 return -ENOSPC;
158 }
159
160 spin_unlock(&c->erase_completion_lock);
161
162 if (!xattr)
163 xattr = jffs2_verify_xattr(c);
164
165 spin_lock(&c->inocache_lock);
166
167 ic = jffs2_get_ino_cache(c, c->checked_ino++);
168
169 if (!ic) {
170 spin_unlock(&c->inocache_lock);
171 continue;
172 }
173
174 if (!ic->pino_nlink) {
175 jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
176 ic->ino);
177 spin_unlock(&c->inocache_lock);
178 jffs2_xattr_delete_inode(c, ic);
179 continue;
180 }
181 switch(ic->state) {
182 case INO_STATE_CHECKEDABSENT:
183 case INO_STATE_PRESENT:
184 jffs2_dbg(1, "Skipping ino #%u already checked\n",
185 ic->ino);
186 spin_unlock(&c->inocache_lock);
187 continue;
188
189 case INO_STATE_GC:
190 case INO_STATE_CHECKING:
191 pr_warn("Inode #%u is in state %d during CRC check phase!\n",
192 ic->ino, ic->state);
193 spin_unlock(&c->inocache_lock);
194 BUG();
195
196 case INO_STATE_READING:
197 /* We need to wait for it to finish, lest we move on
198 and trigger the BUG() above while we haven't yet
199 finished checking all its nodes */
200 jffs2_dbg(1, "Waiting for ino #%u to finish reading\n",
201 ic->ino);
202 /* We need to come back again for the _same_ inode. We've
203 made no progress in this case, but that should be OK */
204 c->checked_ino--;
205
206 mutex_unlock(&c->alloc_sem);
207 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
208 return 0;
209
210 default:
211 BUG();
212
213 case INO_STATE_UNCHECKED:
214 ;
215 }
216 ic->state = INO_STATE_CHECKING;
217 spin_unlock(&c->inocache_lock);
218
219 jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n",
220 __func__, ic->ino);
221
222 ret = jffs2_do_crccheck_inode(c, ic);
223 if (ret)
224 pr_warn("Returned error for crccheck of ino #%u. Expect badness...\n",
225 ic->ino);
226
227 jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
228 mutex_unlock(&c->alloc_sem);
229 return ret;
230 }
231
232 /* If there are any blocks which need erasing, erase them now */
233 if (!list_empty(&c->erase_complete_list) ||
234 !list_empty(&c->erase_pending_list)) {
235 spin_unlock(&c->erase_completion_lock);
236 mutex_unlock(&c->alloc_sem);
237 jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__);
238 if (jffs2_erase_pending_blocks(c, 1))
239 return 0;
240
241 jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n");
242 mutex_lock(&c->alloc_sem);
243 spin_lock(&c->erase_completion_lock);
244 }
245
246 /* First, work out which block we're garbage-collecting */
247 jeb = c->gcblock;
248
249 if (!jeb)
250 jeb = jffs2_find_gc_block(c);
251
252 if (!jeb) {
253 /* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */
254 if (c->nr_erasing_blocks) {
255 spin_unlock(&c->erase_completion_lock);
256 mutex_unlock(&c->alloc_sem);
257 return -EAGAIN;
258 }
259 jffs2_dbg(1, "Couldn't find erase block to garbage collect!\n");
260 spin_unlock(&c->erase_completion_lock);
261 mutex_unlock(&c->alloc_sem);
262 return -EIO;
263 }
264
265 jffs2_dbg(1, "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n",
266 jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size);
267 D1(if (c->nextblock)
268 printk(KERN_DEBUG "Nextblock at %08x, used_size %08x, dirty_size %08x, wasted_size %08x, free_size %08x\n", c->nextblock->offset, c->nextblock->used_size, c->nextblock->dirty_size, c->nextblock->wasted_size, c->nextblock->free_size));
269
270 if (!jeb->used_size) {
271 mutex_unlock(&c->alloc_sem);
272 goto eraseit;
273 }
274
275 raw = jeb->gc_node;
276 gcblock_dirty = jeb->dirty_size;
277
278 while(ref_obsolete(raw)) {
279 jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
280 ref_offset(raw));
281 raw = ref_next(raw);
282 if (unlikely(!raw)) {
283 pr_warn("eep. End of raw list while still supposedly nodes to GC\n");
284 pr_warn("erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
285 jeb->offset, jeb->free_size,
286 jeb->dirty_size, jeb->used_size);
287 jeb->gc_node = raw;
288 spin_unlock(&c->erase_completion_lock);
289 mutex_unlock(&c->alloc_sem);
290 BUG();
291 }
292 }
293 jeb->gc_node = raw;
294
295 jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
296 ref_offset(raw));
297
298 if (!raw->next_in_ino) {
299 /* Inode-less node. Clean marker, snapshot or something like that */
300 spin_unlock(&c->erase_completion_lock);
301 if (ref_flags(raw) == REF_PRISTINE) {
302 /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
303 jffs2_garbage_collect_pristine(c, NULL, raw);
304 } else {
305 /* Just mark it obsolete */
306 jffs2_mark_node_obsolete(c, raw);
307 }
308 mutex_unlock(&c->alloc_sem);
309 goto eraseit_lock;
310 }
311
312 ic = jffs2_raw_ref_to_ic(raw);
313
314#ifdef CONFIG_JFFS2_FS_XATTR
315 /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
316 * We can decide whether this node is inode or xattr by ic->class. */
317 if (ic->class == RAWNODE_CLASS_XATTR_DATUM
318 || ic->class == RAWNODE_CLASS_XATTR_REF) {
319 spin_unlock(&c->erase_completion_lock);
320
321 if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
322 ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
323 } else {
324 ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
325 }
326 goto test_gcnode;
327 }
328#endif
329
330 /* We need to hold the inocache. Either the erase_completion_lock or
331 the inocache_lock are sufficient; we trade down since the inocache_lock
332 causes less contention. */
333 spin_lock(&c->inocache_lock);
334
335 spin_unlock(&c->erase_completion_lock);
336
337 jffs2_dbg(1, "%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n",
338 __func__, jeb->offset, ref_offset(raw), ref_flags(raw),
339 ic->ino);
340
341 /* Three possibilities:
342 1. Inode is already in-core. We must iget it and do proper
343 updating to its fragtree, etc.
344 2. Inode is not in-core, node is REF_PRISTINE. We lock the
345 inocache to prevent a read_inode(), copy the node intact.
346 3. Inode is not in-core, node is not pristine. We must iget()
347 and take the slow path.
348 */
349
350 switch(ic->state) {
351 case INO_STATE_CHECKEDABSENT:
352 /* It's been checked, but it's not currently in-core.
353 We can just copy any pristine nodes, but have
354 to prevent anyone else from doing read_inode() while
355 we're at it, so we set the state accordingly */
356 if (ref_flags(raw) == REF_PRISTINE)
357 ic->state = INO_STATE_GC;
358 else {
359 jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
360 ic->ino);
361 }
362 break;
363
364 case INO_STATE_PRESENT:
365 /* It's in-core. GC must iget() it. */
366 break;
367
368 case INO_STATE_UNCHECKED:
369 case INO_STATE_CHECKING:
370 case INO_STATE_GC:
371 /* Should never happen. We should have finished checking
372 by the time we actually start doing any GC, and since
373 we're holding the alloc_sem, no other garbage collection
374 can happen.
375 */
376 pr_crit("Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
377 ic->ino, ic->state);
378 mutex_unlock(&c->alloc_sem);
379 spin_unlock(&c->inocache_lock);
380 BUG();
381
382 case INO_STATE_READING:
383 /* Someone's currently trying to read it. We must wait for
384 them to finish and then go through the full iget() route
385 to do the GC. However, sometimes read_inode() needs to get
386 the alloc_sem() (for marking nodes invalid) so we must
387 drop the alloc_sem before sleeping. */
388
389 mutex_unlock(&c->alloc_sem);
390 jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n",
391 __func__, ic->ino, ic->state);
392 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
393 /* And because we dropped the alloc_sem we must start again from the
394 beginning. Ponder chance of livelock here -- we're returning success
395 without actually making any progress.
396
397 Q: What are the chances that the inode is back in INO_STATE_READING
398 again by the time we next enter this function? And that this happens
399 enough times to cause a real delay?
400
401 A: Small enough that I don't care :)
402 */
403 return 0;
404 }
405
406 /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
407 node intact, and we don't have to muck about with the fragtree etc.
408 because we know it's not in-core. If it _was_ in-core, we go through
409 all the iget() crap anyway */
410
411 if (ic->state == INO_STATE_GC) {
412 spin_unlock(&c->inocache_lock);
413
414 ret = jffs2_garbage_collect_pristine(c, ic, raw);
415
416 spin_lock(&c->inocache_lock);
417 ic->state = INO_STATE_CHECKEDABSENT;
418 wake_up(&c->inocache_wq);
419
420 if (ret != -EBADFD) {
421 spin_unlock(&c->inocache_lock);
422 goto test_gcnode;
423 }
424
425 /* Fall through if it wanted us to, with inocache_lock held */
426 }
427
428 /* Prevent the fairly unlikely race where the gcblock is
429 entirely obsoleted by the final close of a file which had
430 the only valid nodes in the block, followed by erasure,
431 followed by freeing of the ic because the erased block(s)
432 held _all_ the nodes of that inode.... never been seen but
433 it's vaguely possible. */
434
435 inum = ic->ino;
436 nlink = ic->pino_nlink;
437 spin_unlock(&c->inocache_lock);
438
439 f = jffs2_gc_fetch_inode(c, inum, !nlink);
440 if (IS_ERR(f)) {
441 ret = PTR_ERR(f);
442 goto release_sem;
443 }
444 if (!f) {
445 ret = 0;
446 goto release_sem;
447 }
448
449 ret = jffs2_garbage_collect_live(c, jeb, raw, f);
450
451 jffs2_gc_release_inode(c, f);
452
453 test_gcnode:
454 if (jeb->dirty_size == gcblock_dirty && !ref_obsolete(jeb->gc_node)) {
455 /* Eep. This really should never happen. GC is broken */
456 pr_err("Error garbage collecting node at %08x!\n",
457 ref_offset(jeb->gc_node));
458 ret = -ENOSPC;
459 }
460 release_sem:
461 mutex_unlock(&c->alloc_sem);
462
463 eraseit_lock:
464 /* If we've finished this block, start it erasing */
465 spin_lock(&c->erase_completion_lock);
466
467 eraseit:
468 if (c->gcblock && !c->gcblock->used_size) {
469 jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n",
470 c->gcblock->offset);
471 /* We're GC'ing an empty block? */
472 list_add_tail(&c->gcblock->list, &c->erase_pending_list);
473 c->gcblock = NULL;
474 c->nr_erasing_blocks++;
475 jffs2_garbage_collect_trigger(c);
476 }
477 spin_unlock(&c->erase_completion_lock);
478
479 return ret;
480}
481
482static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
483 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
484{
485 struct jffs2_node_frag *frag;
486 struct jffs2_full_dnode *fn = NULL;
487 struct jffs2_full_dirent *fd;
488 uint32_t start = 0, end = 0, nrfrags = 0;
489 int ret = 0;
490
491 mutex_lock(&f->sem);
492
493 /* Now we have the lock for this inode. Check that it's still the one at the head
494 of the list. */
495
496 spin_lock(&c->erase_completion_lock);
497
498 if (c->gcblock != jeb) {
499 spin_unlock(&c->erase_completion_lock);
500 jffs2_dbg(1, "GC block is no longer gcblock. Restart\n");
501 goto upnout;
502 }
503 if (ref_obsolete(raw)) {
504 spin_unlock(&c->erase_completion_lock);
505 jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n");
506 /* They'll call again */
507 goto upnout;
508 }
509 spin_unlock(&c->erase_completion_lock);
510
511 /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
512 if (f->metadata && f->metadata->raw == raw) {
513 fn = f->metadata;
514 ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
515 goto upnout;
516 }
517
518 /* FIXME. Read node and do lookup? */
519 for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
520 if (frag->node && frag->node->raw == raw) {
521 fn = frag->node;
522 end = frag->ofs + frag->size;
523 if (!nrfrags++)
524 start = frag->ofs;
525 if (nrfrags == frag->node->frags)
526 break; /* We've found them all */
527 }
528 }
529 if (fn) {
530 if (ref_flags(raw) == REF_PRISTINE) {
531 ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
532 if (!ret) {
533 /* Urgh. Return it sensibly. */
534 frag->node->raw = f->inocache->nodes;
535 }
536 if (ret != -EBADFD)
537 goto upnout;
538 }
539 /* We found a datanode. Do the GC */
540 if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
541 /* It crosses a page boundary. Therefore, it must be a hole. */
542 ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
543 } else {
544 /* It could still be a hole. But we GC the page this way anyway */
545 ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
546 }
547 goto upnout;
548 }
549
550 /* Wasn't a dnode. Try dirent */
551 for (fd = f->dents; fd; fd=fd->next) {
552 if (fd->raw == raw)
553 break;
554 }
555
556 if (fd && fd->ino) {
557 ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
558 } else if (fd) {
559 ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
560 } else {
561 pr_warn("Raw node at 0x%08x wasn't in node lists for ino #%u\n",
562 ref_offset(raw), f->inocache->ino);
563 if (ref_obsolete(raw)) {
564 pr_warn("But it's obsolete so we don't mind too much\n");
565 } else {
566 jffs2_dbg_dump_node(c, ref_offset(raw));
567 BUG();
568 }
569 }
570 upnout:
571 mutex_unlock(&f->sem);
572
573 return ret;
574}
575
576static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
577 struct jffs2_inode_cache *ic,
578 struct jffs2_raw_node_ref *raw)
579{
580 union jffs2_node_union *node;
581 size_t retlen;
582 int ret;
583 uint32_t phys_ofs, alloclen;
584 uint32_t crc, rawlen;
585 int retried = 0;
586
587 jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n",
588 ref_offset(raw));
589
590 alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
591
592 /* Ask for a small amount of space (or the totlen if smaller) because we
593 don't want to force wastage of the end of a block if splitting would
594 work. */
595 if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
596 alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
597
598 ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
599 /* 'rawlen' is not the exact summary size; it is only an upper estimation */
600
601 if (ret)
602 return ret;
603
604 if (alloclen < rawlen) {
605 /* Doesn't fit untouched. We'll go the old route and split it */
606 return -EBADFD;
607 }
608
609 node = kmalloc(rawlen, GFP_KERNEL);
610 if (!node)
611 return -ENOMEM;
612
613 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
614 if (!ret && retlen != rawlen)
615 ret = -EIO;
616 if (ret)
617 goto out_node;
618
619 crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
620 if (je32_to_cpu(node->u.hdr_crc) != crc) {
621 pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
622 ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
623 goto bail;
624 }
625
626 switch(je16_to_cpu(node->u.nodetype)) {
627 case JFFS2_NODETYPE_INODE:
628 crc = crc32(0, node, sizeof(node->i)-8);
629 if (je32_to_cpu(node->i.node_crc) != crc) {
630 pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
631 ref_offset(raw), je32_to_cpu(node->i.node_crc),
632 crc);
633 goto bail;
634 }
635
636 if (je32_to_cpu(node->i.dsize)) {
637 crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
638 if (je32_to_cpu(node->i.data_crc) != crc) {
639 pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
640 ref_offset(raw),
641 je32_to_cpu(node->i.data_crc), crc);
642 goto bail;
643 }
644 }
645 break;
646
647 case JFFS2_NODETYPE_DIRENT:
648 crc = crc32(0, node, sizeof(node->d)-8);
649 if (je32_to_cpu(node->d.node_crc) != crc) {
650 pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
651 ref_offset(raw),
652 je32_to_cpu(node->d.node_crc), crc);
653 goto bail;
654 }
655
656 if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
657 pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
658 ref_offset(raw));
659 goto bail;
660 }
661
662 if (node->d.nsize) {
663 crc = crc32(0, node->d.name, node->d.nsize);
664 if (je32_to_cpu(node->d.name_crc) != crc) {
665 pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
666 ref_offset(raw),
667 je32_to_cpu(node->d.name_crc), crc);
668 goto bail;
669 }
670 }
671 break;
672 default:
673 /* If it's inode-less, we don't _know_ what it is. Just copy it intact */
674 if (ic) {
675 pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
676 ref_offset(raw), je16_to_cpu(node->u.nodetype));
677 goto bail;
678 }
679 }
680
681 /* OK, all the CRCs are good; this node can just be copied as-is. */
682 retry:
683 phys_ofs = write_ofs(c);
684
685 ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
686
687 if (ret || (retlen != rawlen)) {
688 pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
689 rawlen, phys_ofs, ret, retlen);
690 if (retlen) {
691 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
692 } else {
693 pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
694 phys_ofs);
695 }
696 if (!retried) {
697 /* Try to reallocate space and retry */
698 uint32_t dummy;
699 struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
700
701 retried = 1;
702
703 jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n");
704
705 jffs2_dbg_acct_sanity_check(c,jeb);
706 jffs2_dbg_acct_paranoia_check(c, jeb);
707
708 ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
709 /* this is not the exact summary size of it,
710 it is only an upper estimation */
711
712 if (!ret) {
713 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
714 phys_ofs);
715
716 jffs2_dbg_acct_sanity_check(c,jeb);
717 jffs2_dbg_acct_paranoia_check(c, jeb);
718
719 goto retry;
720 }
721 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
722 ret);
723 }
724
725 if (!ret)
726 ret = -EIO;
727 goto out_node;
728 }
729 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
730
731 jffs2_mark_node_obsolete(c, raw);
732 jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n",
733 ref_offset(raw));
734
735 out_node:
736 kfree(node);
737 return ret;
738 bail:
739 ret = -EBADFD;
740 goto out_node;
741}
742
743static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
744 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
745{
746 struct jffs2_full_dnode *new_fn;
747 struct jffs2_raw_inode ri;
748 struct jffs2_node_frag *last_frag;
749 union jffs2_device_node dev;
750 char *mdata = NULL;
751 int mdatalen = 0;
752 uint32_t alloclen, ilen;
753 int ret;
754
755 if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
756 S_ISCHR(JFFS2_F_I_MODE(f)) ) {
757 /* For these, we don't actually need to read the old node */
758 mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
759 mdata = (char *)&dev;
760 jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
761 __func__, mdatalen);
762 } else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
763 mdatalen = fn->size;
764 mdata = kmalloc(fn->size, GFP_KERNEL);
765 if (!mdata) {
766 pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
767 return -ENOMEM;
768 }
769 ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
770 if (ret) {
771 pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
772 ret);
773 kfree(mdata);
774 return ret;
775 }
776 jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n",
777 __func__, mdatalen);
778
779 }
780
781 ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
782 JFFS2_SUMMARY_INODE_SIZE);
783 if (ret) {
784 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
785 sizeof(ri) + mdatalen, ret);
786 goto out;
787 }
788
789 last_frag = frag_last(&f->fragtree);
790 if (last_frag)
791 /* Fetch the inode length from the fragtree rather then
792 * from i_size since i_size may have not been updated yet */
793 ilen = last_frag->ofs + last_frag->size;
794 else
795 ilen = JFFS2_F_I_SIZE(f);
796
797 memset(&ri, 0, sizeof(ri));
798 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
799 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
800 ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
801 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
802
803 ri.ino = cpu_to_je32(f->inocache->ino);
804 ri.version = cpu_to_je32(++f->highest_version);
805 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
806 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
807 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
808 ri.isize = cpu_to_je32(ilen);
809 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
810 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
811 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
812 ri.offset = cpu_to_je32(0);
813 ri.csize = cpu_to_je32(mdatalen);
814 ri.dsize = cpu_to_je32(mdatalen);
815 ri.compr = JFFS2_COMPR_NONE;
816 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
817 ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
818
819 new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
820
821 if (IS_ERR(new_fn)) {
822 pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
823 ret = PTR_ERR(new_fn);
824 goto out;
825 }
826 jffs2_mark_node_obsolete(c, fn->raw);
827 jffs2_free_full_dnode(fn);
828 f->metadata = new_fn;
829 out:
830 if (S_ISLNK(JFFS2_F_I_MODE(f)))
831 kfree(mdata);
832 return ret;
833}
834
835static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
836 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
837{
838 struct jffs2_full_dirent *new_fd;
839 struct jffs2_raw_dirent rd;
840 uint32_t alloclen;
841 int ret;
842
843 rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
844 rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
845 rd.nsize = strlen(fd->name);
846 rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
847 rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
848
849 rd.pino = cpu_to_je32(f->inocache->ino);
850 rd.version = cpu_to_je32(++f->highest_version);
851 rd.ino = cpu_to_je32(fd->ino);
852 /* If the times on this inode were set by explicit utime() they can be different,
853 so refrain from splatting them. */
854 if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
855 rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
856 else
857 rd.mctime = cpu_to_je32(0);
858 rd.type = fd->type;
859 rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
860 rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
861
862 ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
863 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
864 if (ret) {
865 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
866 sizeof(rd)+rd.nsize, ret);
867 return ret;
868 }
869 new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
870
871 if (IS_ERR(new_fd)) {
872 pr_warn("jffs2_write_dirent in garbage_collect_dirent failed: %ld\n",
873 PTR_ERR(new_fd));
874 return PTR_ERR(new_fd);
875 }
876 jffs2_add_fd_to_list(c, new_fd, &f->dents);
877 return 0;
878}
879
880static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
881 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
882{
883 struct jffs2_full_dirent **fdp = &f->dents;
884 int found = 0;
885
886 /* On a medium where we can't actually mark nodes obsolete
887 pernamently, such as NAND flash, we need to work out
888 whether this deletion dirent is still needed to actively
889 delete a 'real' dirent with the same name that's still
890 somewhere else on the flash. */
891 if (!jffs2_can_mark_obsolete(c)) {
892 struct jffs2_raw_dirent *rd;
893 struct jffs2_raw_node_ref *raw;
894 int ret;
895 size_t retlen;
896 int name_len = strlen(fd->name);
897 uint32_t name_crc = crc32(0, fd->name, name_len);
898 uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
899
900 rd = kmalloc(rawlen, GFP_KERNEL);
901 if (!rd)
902 return -ENOMEM;
903
904 /* Prevent the erase code from nicking the obsolete node refs while
905 we're looking at them. I really don't like this extra lock but
906 can't see any alternative. Suggestions on a postcard to... */
907 mutex_lock(&c->erase_free_sem);
908
909 for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
910
911 cond_resched();
912
913 /* We only care about obsolete ones */
914 if (!(ref_obsolete(raw)))
915 continue;
916
917 /* Any dirent with the same name is going to have the same length... */
918 if (ref_totlen(c, NULL, raw) != rawlen)
919 continue;
920
921 /* Doesn't matter if there's one in the same erase block. We're going to
922 delete it too at the same time. */
923 if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
924 continue;
925
926 jffs2_dbg(1, "Check potential deletion dirent at %08x\n",
927 ref_offset(raw));
928
929 /* This is an obsolete node belonging to the same directory, and it's of the right
930 length. We need to take a closer look...*/
931 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
932 if (ret) {
933 pr_warn("%s(): Read error (%d) reading obsolete node at %08x\n",
934 __func__, ret, ref_offset(raw));
935 /* If we can't read it, we don't need to continue to obsolete it. Continue */
936 continue;
937 }
938 if (retlen != rawlen) {
939 pr_warn("%s(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
940 __func__, retlen, rawlen,
941 ref_offset(raw));
942 continue;
943 }
944
945 if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
946 continue;
947
948 /* If the name CRC doesn't match, skip */
949 if (je32_to_cpu(rd->name_crc) != name_crc)
950 continue;
951
952 /* If the name length doesn't match, or it's another deletion dirent, skip */
953 if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
954 continue;
955
956 /* OK, check the actual name now */
957 if (memcmp(rd->name, fd->name, name_len))
958 continue;
959
960 /* OK. The name really does match. There really is still an older node on
961 the flash which our deletion dirent obsoletes. So we have to write out
962 a new deletion dirent to replace it */
963 mutex_unlock(&c->erase_free_sem);
964
965 jffs2_dbg(1, "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
966 ref_offset(fd->raw), fd->name,
967 ref_offset(raw), je32_to_cpu(rd->ino));
968 kfree(rd);
969
970 return jffs2_garbage_collect_dirent(c, jeb, f, fd);
971 }
972
973 mutex_unlock(&c->erase_free_sem);
974 kfree(rd);
975 }
976
977 /* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
978 we should update the metadata node with those times accordingly */
979
980 /* No need for it any more. Just mark it obsolete and remove it from the list */
981 while (*fdp) {
982 if ((*fdp) == fd) {
983 found = 1;
984 *fdp = fd->next;
985 break;
986 }
987 fdp = &(*fdp)->next;
988 }
989 if (!found) {
990 pr_warn("Deletion dirent \"%s\" not found in list for ino #%u\n",
991 fd->name, f->inocache->ino);
992 }
993 jffs2_mark_node_obsolete(c, fd->raw);
994 jffs2_free_full_dirent(fd);
995 return 0;
996}
997
998static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
999 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1000 uint32_t start, uint32_t end)
1001{
1002 struct jffs2_raw_inode ri;
1003 struct jffs2_node_frag *frag;
1004 struct jffs2_full_dnode *new_fn;
1005 uint32_t alloclen, ilen;
1006 int ret;
1007
1008 jffs2_dbg(1, "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
1009 f->inocache->ino, start, end);
1010
1011 memset(&ri, 0, sizeof(ri));
1012
1013 if(fn->frags > 1) {
1014 size_t readlen;
1015 uint32_t crc;
1016 /* It's partially obsoleted by a later write. So we have to
1017 write it out again with the _same_ version as before */
1018 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
1019 if (readlen != sizeof(ri) || ret) {
1020 pr_warn("Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n",
1021 ret, readlen);
1022 goto fill;
1023 }
1024 if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
1025 pr_warn("%s(): Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
1026 __func__, ref_offset(fn->raw),
1027 je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
1028 return -EIO;
1029 }
1030 if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
1031 pr_warn("%s(): Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
1032 __func__, ref_offset(fn->raw),
1033 je32_to_cpu(ri.totlen), sizeof(ri));
1034 return -EIO;
1035 }
1036 crc = crc32(0, &ri, sizeof(ri)-8);
1037 if (crc != je32_to_cpu(ri.node_crc)) {
1038 pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
1039 __func__, ref_offset(fn->raw),
1040 je32_to_cpu(ri.node_crc), crc);
1041 /* FIXME: We could possibly deal with this by writing new holes for each frag */
1042 pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1043 start, end, f->inocache->ino);
1044 goto fill;
1045 }
1046 if (ri.compr != JFFS2_COMPR_ZERO) {
1047 pr_warn("%s(): Node 0x%08x wasn't a hole node!\n",
1048 __func__, ref_offset(fn->raw));
1049 pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1050 start, end, f->inocache->ino);
1051 goto fill;
1052 }
1053 } else {
1054 fill:
1055 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1056 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1057 ri.totlen = cpu_to_je32(sizeof(ri));
1058 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1059
1060 ri.ino = cpu_to_je32(f->inocache->ino);
1061 ri.version = cpu_to_je32(++f->highest_version);
1062 ri.offset = cpu_to_je32(start);
1063 ri.dsize = cpu_to_je32(end - start);
1064 ri.csize = cpu_to_je32(0);
1065 ri.compr = JFFS2_COMPR_ZERO;
1066 }
1067
1068 frag = frag_last(&f->fragtree);
1069 if (frag)
1070 /* Fetch the inode length from the fragtree rather then
1071 * from i_size since i_size may have not been updated yet */
1072 ilen = frag->ofs + frag->size;
1073 else
1074 ilen = JFFS2_F_I_SIZE(f);
1075
1076 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1077 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1078 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1079 ri.isize = cpu_to_je32(ilen);
1080 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1081 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1082 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1083 ri.data_crc = cpu_to_je32(0);
1084 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1085
1086 ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
1087 JFFS2_SUMMARY_INODE_SIZE);
1088 if (ret) {
1089 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1090 sizeof(ri), ret);
1091 return ret;
1092 }
1093 new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
1094
1095 if (IS_ERR(new_fn)) {
1096 pr_warn("Error writing new hole node: %ld\n", PTR_ERR(new_fn));
1097 return PTR_ERR(new_fn);
1098 }
1099 if (je32_to_cpu(ri.version) == f->highest_version) {
1100 jffs2_add_full_dnode_to_inode(c, f, new_fn);
1101 if (f->metadata) {
1102 jffs2_mark_node_obsolete(c, f->metadata->raw);
1103 jffs2_free_full_dnode(f->metadata);
1104 f->metadata = NULL;
1105 }
1106 return 0;
1107 }
1108
1109 /*
1110 * We should only get here in the case where the node we are
1111 * replacing had more than one frag, so we kept the same version
1112 * number as before. (Except in case of error -- see 'goto fill;'
1113 * above.)
1114 */
1115 D1(if(unlikely(fn->frags <= 1)) {
1116 pr_warn("%s(): Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1117 __func__, fn->frags, je32_to_cpu(ri.version),
1118 f->highest_version, je32_to_cpu(ri.ino));
1119 });
1120
1121 /* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
1122 mark_ref_normal(new_fn->raw);
1123
1124 for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
1125 frag; frag = frag_next(frag)) {
1126 if (frag->ofs > fn->size + fn->ofs)
1127 break;
1128 if (frag->node == fn) {
1129 frag->node = new_fn;
1130 new_fn->frags++;
1131 fn->frags--;
1132 }
1133 }
1134 if (fn->frags) {
1135 pr_warn("%s(): Old node still has frags!\n", __func__);
1136 BUG();
1137 }
1138 if (!new_fn->frags) {
1139 pr_warn("%s(): New node has no frags!\n", __func__);
1140 BUG();
1141 }
1142
1143 jffs2_mark_node_obsolete(c, fn->raw);
1144 jffs2_free_full_dnode(fn);
1145
1146 return 0;
1147}
1148
1149static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb,
1150 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1151 uint32_t start, uint32_t end)
1152{
1153 struct jffs2_full_dnode *new_fn;
1154 struct jffs2_raw_inode ri;
1155 uint32_t alloclen, offset, orig_end, orig_start;
1156 int ret = 0;
1157 unsigned char *comprbuf = NULL, *writebuf;
1158 unsigned long pg;
1159 unsigned char *pg_ptr;
1160
1161 memset(&ri, 0, sizeof(ri));
1162
1163 jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
1164 f->inocache->ino, start, end);
1165
1166 orig_end = end;
1167 orig_start = start;
1168
1169 if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
1170 /* Attempt to do some merging. But only expand to cover logically
1171 adjacent frags if the block containing them is already considered
1172 to be dirty. Otherwise we end up with GC just going round in
1173 circles dirtying the nodes it already wrote out, especially
1174 on NAND where we have small eraseblocks and hence a much higher
1175 chance of nodes having to be split to cross boundaries. */
1176
1177 struct jffs2_node_frag *frag;
1178 uint32_t min, max;
1179
1180 min = start & ~(PAGE_CACHE_SIZE-1);
1181 max = min + PAGE_CACHE_SIZE;
1182
1183 frag = jffs2_lookup_node_frag(&f->fragtree, start);
1184
1185 /* BUG_ON(!frag) but that'll happen anyway... */
1186
1187 BUG_ON(frag->ofs != start);
1188
1189 /* First grow down... */
1190 while((frag = frag_prev(frag)) && frag->ofs >= min) {
1191
1192 /* If the previous frag doesn't even reach the beginning, there's
1193 excessive fragmentation. Just merge. */
1194 if (frag->ofs > min) {
1195 jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n",
1196 frag->ofs, frag->ofs+frag->size);
1197 start = frag->ofs;
1198 continue;
1199 }
1200 /* OK. This frag holds the first byte of the page. */
1201 if (!frag->node || !frag->node->raw) {
1202 jffs2_dbg(1, "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
1203 frag->ofs, frag->ofs+frag->size);
1204 break;
1205 } else {
1206
1207 /* OK, it's a frag which extends to the beginning of the page. Does it live
1208 in a block which is still considered clean? If so, don't obsolete it.
1209 If not, cover it anyway. */
1210
1211 struct jffs2_raw_node_ref *raw = frag->node->raw;
1212 struct jffs2_eraseblock *jeb;
1213
1214 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1215
1216 if (jeb == c->gcblock) {
1217 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1218 frag->ofs,
1219 frag->ofs + frag->size,
1220 ref_offset(raw));
1221 start = frag->ofs;
1222 break;
1223 }
1224 if (!ISDIRTY(c->flags, jeb->dirty_size + jeb->wasted_size)) {
1225 jffs2_dbg(1, "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
1226 frag->ofs,
1227 frag->ofs + frag->size,
1228 jeb->offset);
1229 break;
1230 }
1231
1232 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
1233 frag->ofs,
1234 frag->ofs + frag->size,
1235 jeb->offset);
1236 start = frag->ofs;
1237 break;
1238 }
1239 }
1240
1241 /* ... then up */
1242
1243 /* Find last frag which is actually part of the node we're to GC. */
1244 frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
1245
1246 while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
1247
1248 /* If the previous frag doesn't even reach the beginning, there's lots
1249 of fragmentation. Just merge. */
1250 if (frag->ofs+frag->size < max) {
1251 jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n",
1252 frag->ofs, frag->ofs+frag->size);
1253 end = frag->ofs + frag->size;
1254 continue;
1255 }
1256
1257 if (!frag->node || !frag->node->raw) {
1258 jffs2_dbg(1, "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
1259 frag->ofs, frag->ofs+frag->size);
1260 break;
1261 } else {
1262
1263 /* OK, it's a frag which extends to the beginning of the page. Does it live
1264 in a block which is still considered clean? If so, don't obsolete it.
1265 If not, cover it anyway. */
1266
1267 struct jffs2_raw_node_ref *raw = frag->node->raw;
1268 struct jffs2_eraseblock *jeb;
1269
1270 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1271
1272 if (jeb == c->gcblock) {
1273 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1274 frag->ofs,
1275 frag->ofs + frag->size,
1276 ref_offset(raw));
1277 end = frag->ofs + frag->size;
1278 break;
1279 }
1280 if (!ISDIRTY(c->flags, jeb->dirty_size + jeb->wasted_size)) {
1281 jffs2_dbg(1, "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
1282 frag->ofs,
1283 frag->ofs + frag->size,
1284 jeb->offset);
1285 break;
1286 }
1287
1288 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
1289 frag->ofs,
1290 frag->ofs + frag->size,
1291 jeb->offset);
1292 end = frag->ofs + frag->size;
1293 break;
1294 }
1295 }
1296 jffs2_dbg(1, "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
1297 orig_start, orig_end, start, end);
1298
1299 D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
1300 BUG_ON(end < orig_end);
1301 BUG_ON(start > orig_start);
1302 }
1303
1304 /* First, use readpage() to read the appropriate page into the page cache */
1305 /* Q: What happens if we actually try to GC the _same_ page for which commit_write()
1306 * triggered garbage collection in the first place?
1307 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
1308 * page OK. We'll actually write it out again in commit_write, which is a little
1309 * suboptimal, but at least we're correct.
1310 */
1311 mutex_unlock(&f->sem);
1312 pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
1313 mutex_lock(&f->sem);
1314
1315 if (IS_ERR(pg_ptr)) {
1316 pr_warn("read_cache_page() returned error: %ld\n",
1317 PTR_ERR(pg_ptr));
1318 return PTR_ERR(pg_ptr);
1319 }
1320
1321 offset = start;
1322 while(offset < orig_end) {
1323 uint32_t datalen;
1324 uint32_t cdatalen;
1325 uint16_t comprtype = JFFS2_COMPR_NONE;
1326
1327 ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
1328 &alloclen, JFFS2_SUMMARY_INODE_SIZE);
1329
1330 if (ret) {
1331 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
1332 sizeof(ri) + JFFS2_MIN_DATA_LEN, ret);
1333 break;
1334 }
1335 cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
1336 datalen = end - offset;
1337
1338 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
1339
1340 comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
1341
1342 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1343 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1344 ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
1345 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1346
1347 ri.ino = cpu_to_je32(f->inocache->ino);
1348 ri.version = cpu_to_je32(++f->highest_version);
1349 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1350 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1351 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1352 ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
1353 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1354 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1355 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1356 ri.offset = cpu_to_je32(offset);
1357 ri.csize = cpu_to_je32(cdatalen);
1358 ri.dsize = cpu_to_je32(datalen);
1359 ri.compr = comprtype & 0xff;
1360 ri.usercompr = (comprtype >> 8) & 0xff;
1361 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1362 ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
1363
1364 new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
1365
1366 jffs2_free_comprbuf(comprbuf, writebuf);
1367
1368 if (IS_ERR(new_fn)) {
1369 pr_warn("Error writing new dnode: %ld\n",
1370 PTR_ERR(new_fn));
1371 ret = PTR_ERR(new_fn);
1372 break;
1373 }
1374 ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
1375 offset += datalen;
1376 if (f->metadata) {
1377 jffs2_mark_node_obsolete(c, f->metadata->raw);
1378 jffs2_free_full_dnode(f->metadata);
1379 f->metadata = NULL;
1380 }
1381 }
1382
1383 jffs2_gc_release_page(c, pg_ptr, &pg);
1384 return ret;
1385}