blob: 85e703a29361df4d1d47261af692bb6efff3305b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
David Woodhouse6088c052010-08-08 14:15:22 +01005 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Created by David Woodhouse <dwmw2@infradead.org>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/kernel.h>
14#include <linux/mtd/mtd.h>
15#include <linux/slab.h>
16#include <linux/pagemap.h>
17#include <linux/crc32.h>
18#include <linux/compiler.h>
19#include <linux/stat.h>
20#include "nodelist.h"
21#include "compr.h"
22
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000023static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 struct jffs2_inode_cache *ic,
25 struct jffs2_raw_node_ref *raw);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000026static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000028static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000030static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
32static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
33 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
34 uint32_t start, uint32_t end);
35static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
36 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
37 uint32_t start, uint32_t end);
38static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
39 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f);
40
41/* Called with erase_completion_lock held */
42static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
43{
44 struct jffs2_eraseblock *ret;
45 struct list_head *nextlist = NULL;
46 int n = jiffies % 128;
47
48 /* Pick an eraseblock to garbage collect next. This is where we'll
49 put the clever wear-levelling algorithms. Eventually. */
50 /* We possibly want to favour the dirtier blocks more when the
51 number of free blocks is low. */
Artem B. Bityuckiya42163d2005-03-20 17:45:29 +000052again:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
Joe Perches9c261b32012-02-15 15:56:43 -080054 jffs2_dbg(1, "Picking block from bad_used_list to GC next\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 nextlist = &c->bad_used_list;
56 } else if (n < 50 && !list_empty(&c->erasable_list)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000057 /* Note that most of them will have gone directly to be erased.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 So don't favour the erasable_list _too_ much. */
Joe Perches9c261b32012-02-15 15:56:43 -080059 jffs2_dbg(1, "Picking block from erasable_list to GC next\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 nextlist = &c->erasable_list;
61 } else if (n < 110 && !list_empty(&c->very_dirty_list)) {
62 /* Most of the time, pick one off the very_dirty list */
Joe Perches9c261b32012-02-15 15:56:43 -080063 jffs2_dbg(1, "Picking block from very_dirty_list to GC next\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 nextlist = &c->very_dirty_list;
65 } else if (n < 126 && !list_empty(&c->dirty_list)) {
Joe Perches9c261b32012-02-15 15:56:43 -080066 jffs2_dbg(1, "Picking block from dirty_list to GC next\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 nextlist = &c->dirty_list;
68 } else if (!list_empty(&c->clean_list)) {
Joe Perches9c261b32012-02-15 15:56:43 -080069 jffs2_dbg(1, "Picking block from clean_list to GC next\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 nextlist = &c->clean_list;
71 } else if (!list_empty(&c->dirty_list)) {
Joe Perches9c261b32012-02-15 15:56:43 -080072 jffs2_dbg(1, "Picking block from dirty_list to GC next (clean_list was empty)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 nextlist = &c->dirty_list;
75 } else if (!list_empty(&c->very_dirty_list)) {
Joe Perches9c261b32012-02-15 15:56:43 -080076 jffs2_dbg(1, "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 nextlist = &c->very_dirty_list;
78 } else if (!list_empty(&c->erasable_list)) {
Joe Perches9c261b32012-02-15 15:56:43 -080079 jffs2_dbg(1, "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 nextlist = &c->erasable_list;
Artem B. Bityuckiya42163d2005-03-20 17:45:29 +000082 } else if (!list_empty(&c->erasable_pending_wbuf_list)) {
83 /* There are blocks are wating for the wbuf sync */
Joe Perches9c261b32012-02-15 15:56:43 -080084 jffs2_dbg(1, "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n");
Artem B. Bityuckiy3cceb9f2005-03-20 21:43:26 +000085 spin_unlock(&c->erase_completion_lock);
Artem B. Bityuckiya42163d2005-03-20 17:45:29 +000086 jffs2_flush_wbuf_pad(c);
Artem B. Bityuckiy3cceb9f2005-03-20 21:43:26 +000087 spin_lock(&c->erase_completion_lock);
Artem B. Bityuckiya42163d2005-03-20 17:45:29 +000088 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 } else {
90 /* Eep. All were empty */
Joe Perches9c261b32012-02-15 15:56:43 -080091 jffs2_dbg(1, "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return NULL;
93 }
94
95 ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
96 list_del(&ret->list);
97 c->gcblock = ret;
98 ret->gc_node = ret->first_node;
99 if (!ret->gc_node) {
100 printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset);
101 BUG();
102 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* Have we accidentally picked a clean block with wasted space ? */
105 if (ret->wasted_size) {
Joe Perches9c261b32012-02-15 15:56:43 -0800106 jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n",
107 ret->wasted_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 ret->dirty_size += ret->wasted_size;
109 c->wasted_size -= ret->wasted_size;
110 c->dirty_size += ret->wasted_size;
111 ret->wasted_size = 0;
112 }
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return ret;
115}
116
117/* jffs2_garbage_collect_pass
118 * Make a single attempt to progress GC. Move one node, and possibly
119 * start erasing one eraseblock.
120 */
121int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
122{
123 struct jffs2_inode_info *f;
124 struct jffs2_inode_cache *ic;
125 struct jffs2_eraseblock *jeb;
126 struct jffs2_raw_node_ref *raw;
David Woodhouse2665ea82007-10-13 11:31:23 +0100127 uint32_t gcblock_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int ret = 0, inum, nlink;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900129 int xattr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David Woodhouseced22072008-04-22 15:13:40 +0100131 if (mutex_lock_interruptible(&c->alloc_sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EINTR;
133
134 for (;;) {
135 spin_lock(&c->erase_completion_lock);
136 if (!c->unchecked_size)
137 break;
138
139 /* We can't start doing GC yet. We haven't finished checking
140 the node CRCs etc. Do it now. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* checked_ino is protected by the alloc_sem */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900143 if (c->checked_ino > c->highest_ino && xattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n",
145 c->unchecked_size);
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100146 jffs2_dbg_dump_block_lists_nolock(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100148 mutex_unlock(&c->alloc_sem);
David Woodhouse44b998e2007-04-23 12:11:46 +0100149 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152 spin_unlock(&c->erase_completion_lock);
153
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900154 if (!xattr)
155 xattr = jffs2_verify_xattr(c);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 spin_lock(&c->inocache_lock);
158
159 ic = jffs2_get_ino_cache(c, c->checked_ino++);
160
161 if (!ic) {
162 spin_unlock(&c->inocache_lock);
163 continue;
164 }
165
David Woodhouse27c72b02008-05-01 18:47:17 +0100166 if (!ic->pino_nlink) {
Joe Perches9c261b32012-02-15 15:56:43 -0800167 jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
168 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 spin_unlock(&c->inocache_lock);
KaiGai Kohei355ed4e2006-06-24 09:15:36 +0900170 jffs2_xattr_delete_inode(c, ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 continue;
172 }
173 switch(ic->state) {
174 case INO_STATE_CHECKEDABSENT:
175 case INO_STATE_PRESENT:
Joe Perches9c261b32012-02-15 15:56:43 -0800176 jffs2_dbg(1, "Skipping ino #%u already checked\n",
177 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_unlock(&c->inocache_lock);
179 continue;
180
181 case INO_STATE_GC:
182 case INO_STATE_CHECKING:
183 printk(KERN_WARNING "Inode #%u is in state %d during CRC check phase!\n", ic->ino, ic->state);
184 spin_unlock(&c->inocache_lock);
185 BUG();
186
187 case INO_STATE_READING:
188 /* We need to wait for it to finish, lest we move on
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000189 and trigger the BUG() above while we haven't yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 finished checking all its nodes */
Joe Perches9c261b32012-02-15 15:56:43 -0800191 jffs2_dbg(1, "Waiting for ino #%u to finish reading\n",
192 ic->ino);
David Woodhoused96fb992006-04-17 00:19:48 +0100193 /* We need to come back again for the _same_ inode. We've
194 made no progress in this case, but that should be OK */
195 c->checked_ino--;
196
David Woodhouseced22072008-04-22 15:13:40 +0100197 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
199 return 0;
200
201 default:
202 BUG();
203
204 case INO_STATE_UNCHECKED:
205 ;
206 }
207 ic->state = INO_STATE_CHECKING;
208 spin_unlock(&c->inocache_lock);
209
Joe Perches9c261b32012-02-15 15:56:43 -0800210 jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n",
211 __func__, ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 ret = jffs2_do_crccheck_inode(c, ic);
214 if (ret)
215 printk(KERN_WARNING "Returned error for crccheck of ino #%u. Expect badness...\n", ic->ino);
216
217 jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
David Woodhouseced22072008-04-22 15:13:40 +0100218 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return ret;
220 }
221
David Woodhouse0717bf82010-05-19 16:37:13 +0100222 /* If there are any blocks which need erasing, erase them now */
223 if (!list_empty(&c->erase_complete_list) ||
224 !list_empty(&c->erase_pending_list)) {
225 spin_unlock(&c->erase_completion_lock);
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200226 mutex_unlock(&c->alloc_sem);
Joe Perches9c261b32012-02-15 15:56:43 -0800227 jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__);
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200228 if (jffs2_erase_pending_blocks(c, 1))
David Woodhouse0717bf82010-05-19 16:37:13 +0100229 return 0;
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200230
Joe Perches9c261b32012-02-15 15:56:43 -0800231 jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n");
David Woodhouse0717bf82010-05-19 16:37:13 +0100232 spin_lock(&c->erase_completion_lock);
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200233 mutex_lock(&c->alloc_sem);
David Woodhouse0717bf82010-05-19 16:37:13 +0100234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* First, work out which block we're garbage-collecting */
237 jeb = c->gcblock;
238
239 if (!jeb)
240 jeb = jffs2_find_gc_block(c);
241
242 if (!jeb) {
David Woodhouse422b1202008-04-23 15:40:52 +0100243 /* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */
David Woodhouse0717bf82010-05-19 16:37:13 +0100244 if (c->nr_erasing_blocks) {
David Woodhouse422b1202008-04-23 15:40:52 +0100245 spin_unlock(&c->erase_completion_lock);
246 mutex_unlock(&c->alloc_sem);
247 return -EAGAIN;
248 }
Joe Perches9c261b32012-02-15 15:56:43 -0800249 jffs2_dbg(1, "jffs2: Couldn't find erase block to garbage collect!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100251 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -EIO;
253 }
254
Joe Perches9c261b32012-02-15 15:56:43 -0800255 jffs2_dbg(1, "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n",
256 jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 D1(if (c->nextblock)
258 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));
259
260 if (!jeb->used_size) {
David Woodhouseced22072008-04-22 15:13:40 +0100261 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto eraseit;
263 }
264
265 raw = jeb->gc_node;
David Woodhouse2665ea82007-10-13 11:31:23 +0100266 gcblock_dirty = jeb->dirty_size;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 while(ref_obsolete(raw)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800269 jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
270 ref_offset(raw));
David Woodhouse99988f72006-05-24 09:04:17 +0100271 raw = ref_next(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (unlikely(!raw)) {
273 printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000274 printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size);
276 jeb->gc_node = raw;
277 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100278 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 BUG();
280 }
281 }
282 jeb->gc_node = raw;
283
Joe Perches9c261b32012-02-15 15:56:43 -0800284 jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
285 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!raw->next_in_ino) {
288 /* Inode-less node. Clean marker, snapshot or something like that */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 spin_unlock(&c->erase_completion_lock);
David Woodhouse61715862006-05-21 00:02:06 +0100290 if (ref_flags(raw) == REF_PRISTINE) {
291 /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
292 jffs2_garbage_collect_pristine(c, NULL, raw);
293 } else {
294 /* Just mark it obsolete */
295 jffs2_mark_node_obsolete(c, raw);
296 }
David Woodhouseced22072008-04-22 15:13:40 +0100297 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto eraseit_lock;
299 }
300
301 ic = jffs2_raw_ref_to_ic(raw);
302
KaiGai Kohei084702e2006-05-13 15:16:13 +0900303#ifdef CONFIG_JFFS2_FS_XATTR
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900304 /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
KaiGai Kohei084702e2006-05-13 15:16:13 +0900305 * We can decide whether this node is inode or xattr by ic->class. */
306 if (ic->class == RAWNODE_CLASS_XATTR_DATUM
307 || ic->class == RAWNODE_CLASS_XATTR_REF) {
KaiGai Kohei084702e2006-05-13 15:16:13 +0900308 spin_unlock(&c->erase_completion_lock);
309
310 if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900311 ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +0900312 } else {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900313 ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +0900314 }
David Woodhouse2665ea82007-10-13 11:31:23 +0100315 goto test_gcnode;
KaiGai Kohei084702e2006-05-13 15:16:13 +0900316 }
317#endif
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* We need to hold the inocache. Either the erase_completion_lock or
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000320 the inocache_lock are sufficient; we trade down since the inocache_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 causes less contention. */
322 spin_lock(&c->inocache_lock);
323
324 spin_unlock(&c->erase_completion_lock);
325
Joe Perches9c261b32012-02-15 15:56:43 -0800326 jffs2_dbg(1, "%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n",
327 __func__, jeb->offset, ref_offset(raw), ref_flags(raw),
328 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Three possibilities:
331 1. Inode is already in-core. We must iget it and do proper
332 updating to its fragtree, etc.
333 2. Inode is not in-core, node is REF_PRISTINE. We lock the
334 inocache to prevent a read_inode(), copy the node intact.
335 3. Inode is not in-core, node is not pristine. We must iget()
336 and take the slow path.
337 */
338
339 switch(ic->state) {
340 case INO_STATE_CHECKEDABSENT:
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000341 /* It's been checked, but it's not currently in-core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 We can just copy any pristine nodes, but have
343 to prevent anyone else from doing read_inode() while
344 we're at it, so we set the state accordingly */
345 if (ref_flags(raw) == REF_PRISTINE)
346 ic->state = INO_STATE_GC;
347 else {
Joe Perches9c261b32012-02-15 15:56:43 -0800348 jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
349 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 break;
352
353 case INO_STATE_PRESENT:
354 /* It's in-core. GC must iget() it. */
355 break;
356
357 case INO_STATE_UNCHECKED:
358 case INO_STATE_CHECKING:
359 case INO_STATE_GC:
360 /* Should never happen. We should have finished checking
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000361 by the time we actually start doing any GC, and since
362 we're holding the alloc_sem, no other garbage collection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 can happen.
364 */
365 printk(KERN_CRIT "Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
366 ic->ino, ic->state);
David Woodhouseced22072008-04-22 15:13:40 +0100367 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_unlock(&c->inocache_lock);
369 BUG();
370
371 case INO_STATE_READING:
372 /* Someone's currently trying to read it. We must wait for
373 them to finish and then go through the full iget() route
374 to do the GC. However, sometimes read_inode() needs to get
375 the alloc_sem() (for marking nodes invalid) so we must
376 drop the alloc_sem before sleeping. */
377
David Woodhouseced22072008-04-22 15:13:40 +0100378 mutex_unlock(&c->alloc_sem);
Joe Perches9c261b32012-02-15 15:56:43 -0800379 jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n",
380 __func__, ic->ino, ic->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000382 /* And because we dropped the alloc_sem we must start again from the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 beginning. Ponder chance of livelock here -- we're returning success
384 without actually making any progress.
385
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000386 Q: What are the chances that the inode is back in INO_STATE_READING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 again by the time we next enter this function? And that this happens
388 enough times to cause a real delay?
389
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000390 A: Small enough that I don't care :)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392 return 0;
393 }
394
395 /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000396 node intact, and we don't have to muck about with the fragtree etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 because we know it's not in-core. If it _was_ in-core, we go through
398 all the iget() crap anyway */
399
400 if (ic->state == INO_STATE_GC) {
401 spin_unlock(&c->inocache_lock);
402
403 ret = jffs2_garbage_collect_pristine(c, ic, raw);
404
405 spin_lock(&c->inocache_lock);
406 ic->state = INO_STATE_CHECKEDABSENT;
407 wake_up(&c->inocache_wq);
408
409 if (ret != -EBADFD) {
410 spin_unlock(&c->inocache_lock);
David Woodhouse2665ea82007-10-13 11:31:23 +0100411 goto test_gcnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 /* Fall through if it wanted us to, with inocache_lock held */
415 }
416
417 /* Prevent the fairly unlikely race where the gcblock is
418 entirely obsoleted by the final close of a file which had
419 the only valid nodes in the block, followed by erasure,
420 followed by freeing of the ic because the erased block(s)
421 held _all_ the nodes of that inode.... never been seen but
422 it's vaguely possible. */
423
424 inum = ic->ino;
David Woodhouse27c72b02008-05-01 18:47:17 +0100425 nlink = ic->pino_nlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 spin_unlock(&c->inocache_lock);
427
David Woodhouse1b690b42008-05-01 16:59:24 +0100428 f = jffs2_gc_fetch_inode(c, inum, !nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (IS_ERR(f)) {
430 ret = PTR_ERR(f);
431 goto release_sem;
432 }
433 if (!f) {
434 ret = 0;
435 goto release_sem;
436 }
437
438 ret = jffs2_garbage_collect_live(c, jeb, raw, f);
439
440 jffs2_gc_release_inode(c, f);
441
David Woodhouse2665ea82007-10-13 11:31:23 +0100442 test_gcnode:
443 if (jeb->dirty_size == gcblock_dirty && !ref_obsolete(jeb->gc_node)) {
444 /* Eep. This really should never happen. GC is broken */
445 printk(KERN_ERR "Error garbage collecting node at %08x!\n", ref_offset(jeb->gc_node));
446 ret = -ENOSPC;
David Woodhouse4fc8a602007-10-13 14:29:39 +0100447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 release_sem:
David Woodhouseced22072008-04-22 15:13:40 +0100449 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 eraseit_lock:
452 /* If we've finished this block, start it erasing */
453 spin_lock(&c->erase_completion_lock);
454
455 eraseit:
456 if (c->gcblock && !c->gcblock->used_size) {
Joe Perches9c261b32012-02-15 15:56:43 -0800457 jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n",
458 c->gcblock->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* We're GC'ing an empty block? */
460 list_add_tail(&c->gcblock->list, &c->erase_pending_list);
461 c->gcblock = NULL;
462 c->nr_erasing_blocks++;
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100463 jffs2_garbage_collect_trigger(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 spin_unlock(&c->erase_completion_lock);
466
467 return ret;
468}
469
470static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
471 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
472{
473 struct jffs2_node_frag *frag;
474 struct jffs2_full_dnode *fn = NULL;
475 struct jffs2_full_dirent *fd;
476 uint32_t start = 0, end = 0, nrfrags = 0;
477 int ret = 0;
478
David Woodhouseced22072008-04-22 15:13:40 +0100479 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* Now we have the lock for this inode. Check that it's still the one at the head
482 of the list. */
483
484 spin_lock(&c->erase_completion_lock);
485
486 if (c->gcblock != jeb) {
487 spin_unlock(&c->erase_completion_lock);
Joe Perches9c261b32012-02-15 15:56:43 -0800488 jffs2_dbg(1, "GC block is no longer gcblock. Restart\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto upnout;
490 }
491 if (ref_obsolete(raw)) {
492 spin_unlock(&c->erase_completion_lock);
Joe Perches9c261b32012-02-15 15:56:43 -0800493 jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* They'll call again */
495 goto upnout;
496 }
497 spin_unlock(&c->erase_completion_lock);
498
499 /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
500 if (f->metadata && f->metadata->raw == raw) {
501 fn = f->metadata;
502 ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
503 goto upnout;
504 }
505
506 /* FIXME. Read node and do lookup? */
507 for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
508 if (frag->node && frag->node->raw == raw) {
509 fn = frag->node;
510 end = frag->ofs + frag->size;
511 if (!nrfrags++)
512 start = frag->ofs;
513 if (nrfrags == frag->node->frags)
514 break; /* We've found them all */
515 }
516 }
517 if (fn) {
518 if (ref_flags(raw) == REF_PRISTINE) {
519 ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
520 if (!ret) {
521 /* Urgh. Return it sensibly. */
522 frag->node->raw = f->inocache->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (ret != -EBADFD)
525 goto upnout;
526 }
527 /* We found a datanode. Do the GC */
528 if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
529 /* It crosses a page boundary. Therefore, it must be a hole. */
530 ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
531 } else {
532 /* It could still be a hole. But we GC the page this way anyway */
533 ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
534 }
535 goto upnout;
536 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Wasn't a dnode. Try dirent */
539 for (fd = f->dents; fd; fd=fd->next) {
540 if (fd->raw == raw)
541 break;
542 }
543
544 if (fd && fd->ino) {
545 ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
546 } else if (fd) {
547 ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
548 } else {
549 printk(KERN_WARNING "Raw node at 0x%08x wasn't in node lists for ino #%u\n",
550 ref_offset(raw), f->inocache->ino);
551 if (ref_obsolete(raw)) {
552 printk(KERN_WARNING "But it's obsolete so we don't mind too much\n");
553 } else {
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100554 jffs2_dbg_dump_node(c, ref_offset(raw));
555 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 }
558 upnout:
David Woodhouseced22072008-04-22 15:13:40 +0100559 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 return ret;
562}
563
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000564static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct jffs2_inode_cache *ic,
566 struct jffs2_raw_node_ref *raw)
567{
568 union jffs2_node_union *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 size_t retlen;
570 int ret;
571 uint32_t phys_ofs, alloclen;
572 uint32_t crc, rawlen;
573 int retried = 0;
574
Joe Perches9c261b32012-02-15 15:56:43 -0800575 jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n",
576 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
David Woodhouse61715862006-05-21 00:02:06 +0100578 alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* Ask for a small amount of space (or the totlen if smaller) because we
581 don't want to force wastage of the end of a block if splitting would
582 work. */
David Woodhouse61715862006-05-21 00:02:06 +0100583 if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
584 alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
585
David Woodhouse9fe48542006-05-23 00:38:06 +0100586 ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
David Woodhouse61715862006-05-21 00:02:06 +0100587 /* 'rawlen' is not the exact summary size; it is only an upper estimation */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (ret)
590 return ret;
591
592 if (alloclen < rawlen) {
593 /* Doesn't fit untouched. We'll go the old route and split it */
594 return -EBADFD;
595 }
596
597 node = kmalloc(rawlen, GFP_KERNEL);
598 if (!node)
David Woodhouseef53cb02007-07-10 10:01:22 +0100599 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
602 if (!ret && retlen != rawlen)
603 ret = -EIO;
604 if (ret)
605 goto out_node;
606
607 crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
608 if (je32_to_cpu(node->u.hdr_crc) != crc) {
609 printk(KERN_WARNING "Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
610 ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
611 goto bail;
612 }
613
614 switch(je16_to_cpu(node->u.nodetype)) {
615 case JFFS2_NODETYPE_INODE:
616 crc = crc32(0, node, sizeof(node->i)-8);
617 if (je32_to_cpu(node->i.node_crc) != crc) {
618 printk(KERN_WARNING "Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
619 ref_offset(raw), je32_to_cpu(node->i.node_crc), crc);
620 goto bail;
621 }
622
623 if (je32_to_cpu(node->i.dsize)) {
624 crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
625 if (je32_to_cpu(node->i.data_crc) != crc) {
626 printk(KERN_WARNING "Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
627 ref_offset(raw), je32_to_cpu(node->i.data_crc), crc);
628 goto bail;
629 }
630 }
631 break;
632
633 case JFFS2_NODETYPE_DIRENT:
634 crc = crc32(0, node, sizeof(node->d)-8);
635 if (je32_to_cpu(node->d.node_crc) != crc) {
636 printk(KERN_WARNING "Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
637 ref_offset(raw), je32_to_cpu(node->d.node_crc), crc);
638 goto bail;
639 }
640
David Woodhouseb534e702007-10-13 11:35:58 +0100641 if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
642 printk(KERN_WARNING "Name in dirent node at 0x%08x contains zeroes\n", ref_offset(raw));
643 goto bail;
644 }
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (node->d.nsize) {
647 crc = crc32(0, node->d.name, node->d.nsize);
648 if (je32_to_cpu(node->d.name_crc) != crc) {
David Woodhouseb534e702007-10-13 11:35:58 +0100649 printk(KERN_WARNING "Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 ref_offset(raw), je32_to_cpu(node->d.name_crc), crc);
651 goto bail;
652 }
653 }
654 break;
655 default:
David Woodhouse61715862006-05-21 00:02:06 +0100656 /* If it's inode-less, we don't _know_ what it is. Just copy it intact */
657 if (ic) {
658 printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
659 ref_offset(raw), je16_to_cpu(node->u.nodetype));
660 goto bail;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* OK, all the CRCs are good; this node can just be copied as-is. */
665 retry:
David Woodhouse2f785402006-05-24 02:04:45 +0100666 phys_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
669
670 if (ret || (retlen != rawlen)) {
671 printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
David Woodhouseef53cb02007-07-10 10:01:22 +0100672 rawlen, phys_ofs, ret, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (retlen) {
David Woodhouse2f785402006-05-24 02:04:45 +0100674 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100676 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", phys_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
David Woodhouse2f785402006-05-24 02:04:45 +0100678 if (!retried) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* Try to reallocate space and retry */
680 uint32_t dummy;
681 struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
682
683 retried = 1;
684
Joe Perches9c261b32012-02-15 15:56:43 -0800685 jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000686
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100687 jffs2_dbg_acct_sanity_check(c,jeb);
688 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
David Woodhouse9fe48542006-05-23 00:38:06 +0100690 ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100691 /* this is not the exact summary size of it,
692 it is only an upper estimation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (!ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800695 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
696 phys_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100698 jffs2_dbg_acct_sanity_check(c,jeb);
699 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 goto retry;
702 }
Joe Perches9c261b32012-02-15 15:56:43 -0800703 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
704 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!ret)
708 ret = -EIO;
709 goto out_node;
710 }
David Woodhouse2f785402006-05-24 02:04:45 +0100711 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 jffs2_mark_node_obsolete(c, raw);
Joe Perches9c261b32012-02-15 15:56:43 -0800714 jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n",
715 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 out_node:
718 kfree(node);
719 return ret;
720 bail:
721 ret = -EBADFD;
722 goto out_node;
723}
724
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000725static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
727{
728 struct jffs2_full_dnode *new_fn;
729 struct jffs2_raw_inode ri;
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +0100730 struct jffs2_node_frag *last_frag;
David Woodhouseaef9ab42006-05-19 00:28:49 +0100731 union jffs2_device_node dev;
David Woodhouse2e16cfc2009-12-16 03:27:20 +0000732 char *mdata = NULL;
733 int mdatalen = 0;
David Woodhouse9fe48542006-05-23 00:38:06 +0100734 uint32_t alloclen, ilen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int ret;
736
737 if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
738 S_ISCHR(JFFS2_F_I_MODE(f)) ) {
739 /* For these, we don't actually need to read the old node */
David Woodhouseaef9ab42006-05-19 00:28:49 +0100740 mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 mdata = (char *)&dev;
Joe Perches9c261b32012-02-15 15:56:43 -0800742 jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
743 __func__, mdatalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 } else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
745 mdatalen = fn->size;
746 mdata = kmalloc(fn->size, GFP_KERNEL);
747 if (!mdata) {
748 printk(KERN_WARNING "kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
749 return -ENOMEM;
750 }
751 ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
752 if (ret) {
753 printk(KERN_WARNING "read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n", ret);
754 kfree(mdata);
755 return ret;
756 }
Joe Perches9c261b32012-02-15 15:56:43 -0800757 jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n",
758 __func__, mdatalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000761
David Woodhouse9fe48542006-05-23 00:38:06 +0100762 ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100763 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (ret) {
765 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
766 sizeof(ri)+ mdatalen, ret);
767 goto out;
768 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000769
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +0100770 last_frag = frag_last(&f->fragtree);
771 if (last_frag)
772 /* Fetch the inode length from the fragtree rather then
773 * from i_size since i_size may have not been updated yet */
774 ilen = last_frag->ofs + last_frag->size;
775 else
776 ilen = JFFS2_F_I_SIZE(f);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 memset(&ri, 0, sizeof(ri));
779 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
780 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
781 ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
782 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
783
784 ri.ino = cpu_to_je32(f->inocache->ino);
785 ri.version = cpu_to_je32(++f->highest_version);
786 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
787 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
788 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +0100789 ri.isize = cpu_to_je32(ilen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
791 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
792 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
793 ri.offset = cpu_to_je32(0);
794 ri.csize = cpu_to_je32(mdatalen);
795 ri.dsize = cpu_to_je32(mdatalen);
796 ri.compr = JFFS2_COMPR_NONE;
797 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
798 ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
799
David Woodhouse9fe48542006-05-23 00:38:06 +0100800 new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (IS_ERR(new_fn)) {
803 printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
804 ret = PTR_ERR(new_fn);
805 goto out;
806 }
807 jffs2_mark_node_obsolete(c, fn->raw);
808 jffs2_free_full_dnode(fn);
809 f->metadata = new_fn;
810 out:
811 if (S_ISLNK(JFFS2_F_I_MODE(f)))
812 kfree(mdata);
813 return ret;
814}
815
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000816static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
818{
819 struct jffs2_full_dirent *new_fd;
820 struct jffs2_raw_dirent rd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100821 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int ret;
823
824 rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
825 rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
826 rd.nsize = strlen(fd->name);
827 rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
828 rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
829
830 rd.pino = cpu_to_je32(f->inocache->ino);
831 rd.version = cpu_to_je32(++f->highest_version);
832 rd.ino = cpu_to_je32(fd->ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100833 /* If the times on this inode were set by explicit utime() they can be different,
834 so refrain from splatting them. */
835 if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
836 rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000837 else
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100838 rd.mctime = cpu_to_je32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 rd.type = fd->type;
840 rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
841 rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000842
David Woodhouse9fe48542006-05-23 00:38:06 +0100843 ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100844 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (ret) {
846 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
847 sizeof(rd)+rd.nsize, ret);
848 return ret;
849 }
David Woodhouse9fe48542006-05-23 00:38:06 +0100850 new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (IS_ERR(new_fd)) {
853 printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd));
854 return PTR_ERR(new_fd);
855 }
856 jffs2_add_fd_to_list(c, new_fd, &f->dents);
857 return 0;
858}
859
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000860static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
862{
863 struct jffs2_full_dirent **fdp = &f->dents;
864 int found = 0;
865
866 /* On a medium where we can't actually mark nodes obsolete
867 pernamently, such as NAND flash, we need to work out
868 whether this deletion dirent is still needed to actively
869 delete a 'real' dirent with the same name that's still
870 somewhere else on the flash. */
871 if (!jffs2_can_mark_obsolete(c)) {
872 struct jffs2_raw_dirent *rd;
873 struct jffs2_raw_node_ref *raw;
874 int ret;
875 size_t retlen;
876 int name_len = strlen(fd->name);
877 uint32_t name_crc = crc32(0, fd->name, name_len);
878 uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
879
880 rd = kmalloc(rawlen, GFP_KERNEL);
881 if (!rd)
882 return -ENOMEM;
883
884 /* Prevent the erase code from nicking the obsolete node refs while
885 we're looking at them. I really don't like this extra lock but
886 can't see any alternative. Suggestions on a postcard to... */
David Woodhouseced22072008-04-22 15:13:40 +0100887 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
890
Artem Bityutskiyaba54da2006-12-19 15:45:23 +0200891 cond_resched();
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* We only care about obsolete ones */
894 if (!(ref_obsolete(raw)))
895 continue;
896
897 /* Any dirent with the same name is going to have the same length... */
898 if (ref_totlen(c, NULL, raw) != rawlen)
899 continue;
900
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000901 /* Doesn't matter if there's one in the same erase block. We're going to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 delete it too at the same time. */
Andrew Victor3be36672005-02-09 09:09:05 +0000903 if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 continue;
905
Joe Perches9c261b32012-02-15 15:56:43 -0800906 jffs2_dbg(1, "Check potential deletion dirent at %08x\n",
907 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* This is an obsolete node belonging to the same directory, and it's of the right
910 length. We need to take a closer look...*/
911 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
912 if (ret) {
913 printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Read error (%d) reading obsolete node at %08x\n", ret, ref_offset(raw));
914 /* If we can't read it, we don't need to continue to obsolete it. Continue */
915 continue;
916 }
917 if (retlen != rawlen) {
918 printk(KERN_WARNING "jffs2_g_c_deletion_dirent(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
919 retlen, rawlen, ref_offset(raw));
920 continue;
921 }
922
923 if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
924 continue;
925
926 /* If the name CRC doesn't match, skip */
927 if (je32_to_cpu(rd->name_crc) != name_crc)
928 continue;
929
930 /* If the name length doesn't match, or it's another deletion dirent, skip */
931 if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
932 continue;
933
934 /* OK, check the actual name now */
935 if (memcmp(rd->name, fd->name, name_len))
936 continue;
937
938 /* OK. The name really does match. There really is still an older node on
939 the flash which our deletion dirent obsoletes. So we have to write out
940 a new deletion dirent to replace it */
David Woodhouseced22072008-04-22 15:13:40 +0100941 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Joe Perches9c261b32012-02-15 15:56:43 -0800943 jffs2_dbg(1, "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
944 ref_offset(fd->raw), fd->name,
945 ref_offset(raw), je32_to_cpu(rd->ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 kfree(rd);
947
948 return jffs2_garbage_collect_dirent(c, jeb, f, fd);
949 }
950
David Woodhouseced22072008-04-22 15:13:40 +0100951 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 kfree(rd);
953 }
954
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000955 /* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100956 we should update the metadata node with those times accordingly */
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* No need for it any more. Just mark it obsolete and remove it from the list */
959 while (*fdp) {
960 if ((*fdp) == fd) {
961 found = 1;
962 *fdp = fd->next;
963 break;
964 }
965 fdp = &(*fdp)->next;
966 }
967 if (!found) {
968 printk(KERN_WARNING "Deletion dirent \"%s\" not found in list for ino #%u\n", fd->name, f->inocache->ino);
969 }
970 jffs2_mark_node_obsolete(c, fd->raw);
971 jffs2_free_full_dirent(fd);
972 return 0;
973}
974
975static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
976 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
977 uint32_t start, uint32_t end)
978{
979 struct jffs2_raw_inode ri;
980 struct jffs2_node_frag *frag;
981 struct jffs2_full_dnode *new_fn;
David Woodhouse9fe48542006-05-23 00:38:06 +0100982 uint32_t alloclen, ilen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int ret;
984
Joe Perches9c261b32012-02-15 15:56:43 -0800985 jffs2_dbg(1, "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
986 f->inocache->ino, start, end);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 memset(&ri, 0, sizeof(ri));
989
990 if(fn->frags > 1) {
991 size_t readlen;
992 uint32_t crc;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000993 /* It's partially obsoleted by a later write. So we have to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 write it out again with the _same_ version as before */
995 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
996 if (readlen != sizeof(ri) || ret) {
997 printk(KERN_WARNING "Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n", ret, readlen);
998 goto fill;
999 }
1000 if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
1001 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
1002 ref_offset(fn->raw),
1003 je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
1004 return -EIO;
1005 }
1006 if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
1007 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
1008 ref_offset(fn->raw),
1009 je32_to_cpu(ri.totlen), sizeof(ri));
1010 return -EIO;
1011 }
1012 crc = crc32(0, &ri, sizeof(ri)-8);
1013 if (crc != je32_to_cpu(ri.node_crc)) {
1014 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001015 ref_offset(fn->raw),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 je32_to_cpu(ri.node_crc), crc);
1017 /* FIXME: We could possibly deal with this by writing new holes for each frag */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001018 printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 start, end, f->inocache->ino);
1020 goto fill;
1021 }
1022 if (ri.compr != JFFS2_COMPR_ZERO) {
1023 printk(KERN_WARNING "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!\n", ref_offset(fn->raw));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001024 printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 start, end, f->inocache->ino);
1026 goto fill;
1027 }
1028 } else {
1029 fill:
1030 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1031 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1032 ri.totlen = cpu_to_je32(sizeof(ri));
1033 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1034
1035 ri.ino = cpu_to_je32(f->inocache->ino);
1036 ri.version = cpu_to_je32(++f->highest_version);
1037 ri.offset = cpu_to_je32(start);
1038 ri.dsize = cpu_to_je32(end - start);
1039 ri.csize = cpu_to_je32(0);
1040 ri.compr = JFFS2_COMPR_ZERO;
1041 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001042
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +01001043 frag = frag_last(&f->fragtree);
1044 if (frag)
1045 /* Fetch the inode length from the fragtree rather then
1046 * from i_size since i_size may have not been updated yet */
1047 ilen = frag->ofs + frag->size;
1048 else
1049 ilen = JFFS2_F_I_SIZE(f);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1052 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1053 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +01001054 ri.isize = cpu_to_je32(ilen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1056 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1057 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1058 ri.data_crc = cpu_to_je32(0);
1059 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1060
David Woodhouse9fe48542006-05-23 00:38:06 +01001061 ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
1062 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (ret) {
1064 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1065 sizeof(ri), ret);
1066 return ret;
1067 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001068 new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 if (IS_ERR(new_fn)) {
1071 printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn));
1072 return PTR_ERR(new_fn);
1073 }
1074 if (je32_to_cpu(ri.version) == f->highest_version) {
1075 jffs2_add_full_dnode_to_inode(c, f, new_fn);
1076 if (f->metadata) {
1077 jffs2_mark_node_obsolete(c, f->metadata->raw);
1078 jffs2_free_full_dnode(f->metadata);
1079 f->metadata = NULL;
1080 }
1081 return 0;
1082 }
1083
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001084 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * We should only get here in the case where the node we are
1086 * replacing had more than one frag, so we kept the same version
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001087 * number as before. (Except in case of error -- see 'goto fill;'
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * above.)
1089 */
1090 D1(if(unlikely(fn->frags <= 1)) {
1091 printk(KERN_WARNING "jffs2_garbage_collect_hole: Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1092 fn->frags, je32_to_cpu(ri.version), f->highest_version,
1093 je32_to_cpu(ri.ino));
1094 });
1095
1096 /* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
1097 mark_ref_normal(new_fn->raw);
1098
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001099 for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 frag; frag = frag_next(frag)) {
1101 if (frag->ofs > fn->size + fn->ofs)
1102 break;
1103 if (frag->node == fn) {
1104 frag->node = new_fn;
1105 new_fn->frags++;
1106 fn->frags--;
1107 }
1108 }
1109 if (fn->frags) {
1110 printk(KERN_WARNING "jffs2_garbage_collect_hole: Old node still has frags!\n");
1111 BUG();
1112 }
1113 if (!new_fn->frags) {
1114 printk(KERN_WARNING "jffs2_garbage_collect_hole: New node has no frags!\n");
1115 BUG();
1116 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 jffs2_mark_node_obsolete(c, fn->raw);
1119 jffs2_free_full_dnode(fn);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return 0;
1122}
1123
David Woodhouse25dc30b2008-04-22 12:12:25 +01001124static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1126 uint32_t start, uint32_t end)
1127{
1128 struct jffs2_full_dnode *new_fn;
1129 struct jffs2_raw_inode ri;
David Woodhouse9fe48542006-05-23 00:38:06 +01001130 uint32_t alloclen, offset, orig_end, orig_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int ret = 0;
1132 unsigned char *comprbuf = NULL, *writebuf;
1133 unsigned long pg;
1134 unsigned char *pg_ptr;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 memset(&ri, 0, sizeof(ri));
1137
Joe Perches9c261b32012-02-15 15:56:43 -08001138 jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
1139 f->inocache->ino, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 orig_end = end;
1142 orig_start = start;
1143
1144 if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
1145 /* Attempt to do some merging. But only expand to cover logically
1146 adjacent frags if the block containing them is already considered
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001147 to be dirty. Otherwise we end up with GC just going round in
1148 circles dirtying the nodes it already wrote out, especially
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 on NAND where we have small eraseblocks and hence a much higher
1150 chance of nodes having to be split to cross boundaries. */
1151
1152 struct jffs2_node_frag *frag;
1153 uint32_t min, max;
1154
1155 min = start & ~(PAGE_CACHE_SIZE-1);
1156 max = min + PAGE_CACHE_SIZE;
1157
1158 frag = jffs2_lookup_node_frag(&f->fragtree, start);
1159
1160 /* BUG_ON(!frag) but that'll happen anyway... */
1161
1162 BUG_ON(frag->ofs != start);
1163
1164 /* First grow down... */
1165 while((frag = frag_prev(frag)) && frag->ofs >= min) {
1166
1167 /* If the previous frag doesn't even reach the beginning, there's
1168 excessive fragmentation. Just merge. */
1169 if (frag->ofs > min) {
Joe Perches9c261b32012-02-15 15:56:43 -08001170 jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n",
1171 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 start = frag->ofs;
1173 continue;
1174 }
1175 /* OK. This frag holds the first byte of the page. */
1176 if (!frag->node || !frag->node->raw) {
Joe Perches9c261b32012-02-15 15:56:43 -08001177 jffs2_dbg(1, "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
1178 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180 } else {
1181
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001182 /* OK, it's a frag which extends to the beginning of the page. Does it live
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 in a block which is still considered clean? If so, don't obsolete it.
1184 If not, cover it anyway. */
1185
1186 struct jffs2_raw_node_ref *raw = frag->node->raw;
1187 struct jffs2_eraseblock *jeb;
1188
1189 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1190
1191 if (jeb == c->gcblock) {
Joe Perches9c261b32012-02-15 15:56:43 -08001192 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1193 frag->ofs,
1194 frag->ofs + frag->size,
1195 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 start = frag->ofs;
1197 break;
1198 }
1199 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
Joe Perches9c261b32012-02-15 15:56:43 -08001200 jffs2_dbg(1, "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
1201 frag->ofs,
1202 frag->ofs + frag->size,
1203 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 break;
1205 }
1206
Joe Perches9c261b32012-02-15 15:56:43 -08001207 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
1208 frag->ofs,
1209 frag->ofs + frag->size,
1210 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 start = frag->ofs;
1212 break;
1213 }
1214 }
1215
1216 /* ... then up */
1217
1218 /* Find last frag which is actually part of the node we're to GC. */
1219 frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
1220
1221 while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
1222
1223 /* If the previous frag doesn't even reach the beginning, there's lots
1224 of fragmentation. Just merge. */
1225 if (frag->ofs+frag->size < max) {
Joe Perches9c261b32012-02-15 15:56:43 -08001226 jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n",
1227 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 end = frag->ofs + frag->size;
1229 continue;
1230 }
1231
1232 if (!frag->node || !frag->node->raw) {
Joe Perches9c261b32012-02-15 15:56:43 -08001233 jffs2_dbg(1, "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
1234 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 } else {
1237
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001238 /* OK, it's a frag which extends to the beginning of the page. Does it live
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 in a block which is still considered clean? If so, don't obsolete it.
1240 If not, cover it anyway. */
1241
1242 struct jffs2_raw_node_ref *raw = frag->node->raw;
1243 struct jffs2_eraseblock *jeb;
1244
1245 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1246
1247 if (jeb == c->gcblock) {
Joe Perches9c261b32012-02-15 15:56:43 -08001248 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1249 frag->ofs,
1250 frag->ofs + frag->size,
1251 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 end = frag->ofs + frag->size;
1253 break;
1254 }
1255 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
Joe Perches9c261b32012-02-15 15:56:43 -08001256 jffs2_dbg(1, "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
1257 frag->ofs,
1258 frag->ofs + frag->size,
1259 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 }
1262
Joe Perches9c261b32012-02-15 15:56:43 -08001263 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
1264 frag->ofs,
1265 frag->ofs + frag->size,
1266 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 end = frag->ofs + frag->size;
1268 break;
1269 }
1270 }
Joe Perches9c261b32012-02-15 15:56:43 -08001271 jffs2_dbg(1, "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
1272 orig_start, orig_end, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +01001274 D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 BUG_ON(end < orig_end);
1276 BUG_ON(start > orig_start);
1277 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* First, use readpage() to read the appropriate page into the page cache */
1280 /* Q: What happens if we actually try to GC the _same_ page for which commit_write()
1281 * triggered garbage collection in the first place?
1282 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
1283 * page OK. We'll actually write it out again in commit_write, which is a little
1284 * suboptimal, but at least we're correct.
1285 */
1286 pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
1287
1288 if (IS_ERR(pg_ptr)) {
1289 printk(KERN_WARNING "read_cache_page() returned error: %ld\n", PTR_ERR(pg_ptr));
1290 return PTR_ERR(pg_ptr);
1291 }
1292
1293 offset = start;
1294 while(offset < orig_end) {
1295 uint32_t datalen;
1296 uint32_t cdatalen;
1297 uint16_t comprtype = JFFS2_COMPR_NONE;
1298
David Woodhouse9fe48542006-05-23 00:38:06 +01001299 ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001300 &alloclen, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (ret) {
1303 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
1304 sizeof(ri)+ JFFS2_MIN_DATA_LEN, ret);
1305 break;
1306 }
1307 cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
1308 datalen = end - offset;
1309
1310 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
1311
1312 comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
1313
1314 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1315 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1316 ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
1317 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1318
1319 ri.ino = cpu_to_je32(f->inocache->ino);
1320 ri.version = cpu_to_je32(++f->highest_version);
1321 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1322 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1323 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1324 ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
1325 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1326 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1327 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1328 ri.offset = cpu_to_je32(offset);
1329 ri.csize = cpu_to_je32(cdatalen);
1330 ri.dsize = cpu_to_je32(datalen);
1331 ri.compr = comprtype & 0xff;
1332 ri.usercompr = (comprtype >> 8) & 0xff;
1333 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1334 ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001335
David Woodhouse9fe48542006-05-23 00:38:06 +01001336 new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 jffs2_free_comprbuf(comprbuf, writebuf);
1339
1340 if (IS_ERR(new_fn)) {
1341 printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
1342 ret = PTR_ERR(new_fn);
1343 break;
1344 }
1345 ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
1346 offset += datalen;
1347 if (f->metadata) {
1348 jffs2_mark_node_obsolete(c, f->metadata->raw);
1349 jffs2_free_full_dnode(f->metadata);
1350 f->metadata = NULL;
1351 }
1352 }
1353
1354 jffs2_gc_release_page(c, pg_ptr, &pg);
1355 return ret;
1356}