blob: d7936d068b2e54010ec3f629fa78b8e335a89ab7 [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) {
Joe Perchesda320f02012-02-15 15:56:44 -0800100 pr_warn("Eep. ret->gc_node for block at 0x%08x is NULL\n",
101 ret->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 BUG();
103 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* Have we accidentally picked a clean block with wasted space ? */
106 if (ret->wasted_size) {
Joe Perches9c261b32012-02-15 15:56:43 -0800107 jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n",
108 ret->wasted_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 ret->dirty_size += ret->wasted_size;
110 c->wasted_size -= ret->wasted_size;
111 c->dirty_size += ret->wasted_size;
112 ret->wasted_size = 0;
113 }
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ret;
116}
117
118/* jffs2_garbage_collect_pass
119 * Make a single attempt to progress GC. Move one node, and possibly
120 * start erasing one eraseblock.
121 */
122int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
123{
124 struct jffs2_inode_info *f;
125 struct jffs2_inode_cache *ic;
126 struct jffs2_eraseblock *jeb;
127 struct jffs2_raw_node_ref *raw;
David Woodhouse2665ea82007-10-13 11:31:23 +0100128 uint32_t gcblock_dirty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int ret = 0, inum, nlink;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900130 int xattr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Woodhouseced22072008-04-22 15:13:40 +0100132 if (mutex_lock_interruptible(&c->alloc_sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -EINTR;
134
135 for (;;) {
136 spin_lock(&c->erase_completion_lock);
137 if (!c->unchecked_size)
138 break;
139
140 /* We can't start doing GC yet. We haven't finished checking
141 the node CRCs etc. Do it now. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* checked_ino is protected by the alloc_sem */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900144 if (c->checked_ino > c->highest_ino && xattr) {
Joe Perchesda320f02012-02-15 15:56:44 -0800145 pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
146 c->unchecked_size);
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100147 jffs2_dbg_dump_block_lists_nolock(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100149 mutex_unlock(&c->alloc_sem);
David Woodhouse44b998e2007-04-23 12:11:46 +0100150 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 spin_unlock(&c->erase_completion_lock);
154
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900155 if (!xattr)
156 xattr = jffs2_verify_xattr(c);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 spin_lock(&c->inocache_lock);
159
160 ic = jffs2_get_ino_cache(c, c->checked_ino++);
161
162 if (!ic) {
163 spin_unlock(&c->inocache_lock);
164 continue;
165 }
166
David Woodhouse27c72b02008-05-01 18:47:17 +0100167 if (!ic->pino_nlink) {
Joe Perches9c261b32012-02-15 15:56:43 -0800168 jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
169 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_unlock(&c->inocache_lock);
KaiGai Kohei355ed4e2006-06-24 09:15:36 +0900171 jffs2_xattr_delete_inode(c, ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 continue;
173 }
174 switch(ic->state) {
175 case INO_STATE_CHECKEDABSENT:
176 case INO_STATE_PRESENT:
Joe Perches9c261b32012-02-15 15:56:43 -0800177 jffs2_dbg(1, "Skipping ino #%u already checked\n",
178 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 spin_unlock(&c->inocache_lock);
180 continue;
181
182 case INO_STATE_GC:
183 case INO_STATE_CHECKING:
Joe Perchesda320f02012-02-15 15:56:44 -0800184 pr_warn("Inode #%u is in state %d during CRC check phase!\n",
185 ic->ino, ic->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_unlock(&c->inocache_lock);
187 BUG();
188
189 case INO_STATE_READING:
190 /* We need to wait for it to finish, lest we move on
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000191 and trigger the BUG() above while we haven't yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 finished checking all its nodes */
Joe Perches9c261b32012-02-15 15:56:43 -0800193 jffs2_dbg(1, "Waiting for ino #%u to finish reading\n",
194 ic->ino);
David Woodhoused96fb992006-04-17 00:19:48 +0100195 /* We need to come back again for the _same_ inode. We've
196 made no progress in this case, but that should be OK */
197 c->checked_ino--;
198
David Woodhouseced22072008-04-22 15:13:40 +0100199 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
201 return 0;
202
203 default:
204 BUG();
205
206 case INO_STATE_UNCHECKED:
207 ;
208 }
209 ic->state = INO_STATE_CHECKING;
210 spin_unlock(&c->inocache_lock);
211
Joe Perches9c261b32012-02-15 15:56:43 -0800212 jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n",
213 __func__, ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 ret = jffs2_do_crccheck_inode(c, ic);
216 if (ret)
Joe Perchesda320f02012-02-15 15:56:44 -0800217 pr_warn("Returned error for crccheck of ino #%u. Expect badness...\n",
218 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 jffs2_set_inocache_state(c, ic, INO_STATE_CHECKEDABSENT);
David Woodhouseced22072008-04-22 15:13:40 +0100221 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ret;
223 }
224
David Woodhouse0717bf82010-05-19 16:37:13 +0100225 /* If there are any blocks which need erasing, erase them now */
226 if (!list_empty(&c->erase_complete_list) ||
227 !list_empty(&c->erase_pending_list)) {
228 spin_unlock(&c->erase_completion_lock);
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200229 mutex_unlock(&c->alloc_sem);
Joe Perches9c261b32012-02-15 15:56:43 -0800230 jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__);
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200231 if (jffs2_erase_pending_blocks(c, 1))
David Woodhouse0717bf82010-05-19 16:37:13 +0100232 return 0;
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200233
Joe Perches9c261b32012-02-15 15:56:43 -0800234 jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n");
David Woodhouse0717bf82010-05-19 16:37:13 +0100235 spin_lock(&c->erase_completion_lock);
Joakim Tjernlund81cfc9f2010-10-07 18:01:44 +0200236 mutex_lock(&c->alloc_sem);
David Woodhouse0717bf82010-05-19 16:37:13 +0100237 }
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* First, work out which block we're garbage-collecting */
240 jeb = c->gcblock;
241
242 if (!jeb)
243 jeb = jffs2_find_gc_block(c);
244
245 if (!jeb) {
David Woodhouse422b1202008-04-23 15:40:52 +0100246 /* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */
David Woodhouse0717bf82010-05-19 16:37:13 +0100247 if (c->nr_erasing_blocks) {
David Woodhouse422b1202008-04-23 15:40:52 +0100248 spin_unlock(&c->erase_completion_lock);
249 mutex_unlock(&c->alloc_sem);
250 return -EAGAIN;
251 }
Joe Perches9c261b32012-02-15 15:56:43 -0800252 jffs2_dbg(1, "jffs2: Couldn't find erase block to garbage collect!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100254 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EIO;
256 }
257
Joe Perches9c261b32012-02-15 15:56:43 -0800258 jffs2_dbg(1, "GC from block %08x, used_size %08x, dirty_size %08x, free_size %08x\n",
259 jeb->offset, jeb->used_size, jeb->dirty_size, jeb->free_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 D1(if (c->nextblock)
261 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));
262
263 if (!jeb->used_size) {
David Woodhouseced22072008-04-22 15:13:40 +0100264 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto eraseit;
266 }
267
268 raw = jeb->gc_node;
David Woodhouse2665ea82007-10-13 11:31:23 +0100269 gcblock_dirty = jeb->dirty_size;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 while(ref_obsolete(raw)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800272 jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
273 ref_offset(raw));
David Woodhouse99988f72006-05-24 09:04:17 +0100274 raw = ref_next(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (unlikely(!raw)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800276 pr_warn("eep. End of raw list while still supposedly nodes to GC\n");
277 pr_warn("erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
278 jeb->offset, jeb->free_size,
279 jeb->dirty_size, jeb->used_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 jeb->gc_node = raw;
281 spin_unlock(&c->erase_completion_lock);
David Woodhouseced22072008-04-22 15:13:40 +0100282 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 BUG();
284 }
285 }
286 jeb->gc_node = raw;
287
Joe Perches9c261b32012-02-15 15:56:43 -0800288 jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
289 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (!raw->next_in_ino) {
292 /* Inode-less node. Clean marker, snapshot or something like that */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 spin_unlock(&c->erase_completion_lock);
David Woodhouse61715862006-05-21 00:02:06 +0100294 if (ref_flags(raw) == REF_PRISTINE) {
295 /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
296 jffs2_garbage_collect_pristine(c, NULL, raw);
297 } else {
298 /* Just mark it obsolete */
299 jffs2_mark_node_obsolete(c, raw);
300 }
David Woodhouseced22072008-04-22 15:13:40 +0100301 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto eraseit_lock;
303 }
304
305 ic = jffs2_raw_ref_to_ic(raw);
306
KaiGai Kohei084702e2006-05-13 15:16:13 +0900307#ifdef CONFIG_JFFS2_FS_XATTR
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900308 /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
KaiGai Kohei084702e2006-05-13 15:16:13 +0900309 * We can decide whether this node is inode or xattr by ic->class. */
310 if (ic->class == RAWNODE_CLASS_XATTR_DATUM
311 || ic->class == RAWNODE_CLASS_XATTR_REF) {
KaiGai Kohei084702e2006-05-13 15:16:13 +0900312 spin_unlock(&c->erase_completion_lock);
313
314 if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900315 ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +0900316 } else {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900317 ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
KaiGai Kohei084702e2006-05-13 15:16:13 +0900318 }
David Woodhouse2665ea82007-10-13 11:31:23 +0100319 goto test_gcnode;
KaiGai Kohei084702e2006-05-13 15:16:13 +0900320 }
321#endif
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* We need to hold the inocache. Either the erase_completion_lock or
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000324 the inocache_lock are sufficient; we trade down since the inocache_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 causes less contention. */
326 spin_lock(&c->inocache_lock);
327
328 spin_unlock(&c->erase_completion_lock);
329
Joe Perches9c261b32012-02-15 15:56:43 -0800330 jffs2_dbg(1, "%s(): collecting from block @0x%08x. Node @0x%08x(%d), ino #%u\n",
331 __func__, jeb->offset, ref_offset(raw), ref_flags(raw),
332 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Three possibilities:
335 1. Inode is already in-core. We must iget it and do proper
336 updating to its fragtree, etc.
337 2. Inode is not in-core, node is REF_PRISTINE. We lock the
338 inocache to prevent a read_inode(), copy the node intact.
339 3. Inode is not in-core, node is not pristine. We must iget()
340 and take the slow path.
341 */
342
343 switch(ic->state) {
344 case INO_STATE_CHECKEDABSENT:
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000345 /* It's been checked, but it's not currently in-core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 We can just copy any pristine nodes, but have
347 to prevent anyone else from doing read_inode() while
348 we're at it, so we set the state accordingly */
349 if (ref_flags(raw) == REF_PRISTINE)
350 ic->state = INO_STATE_GC;
351 else {
Joe Perches9c261b32012-02-15 15:56:43 -0800352 jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
353 ic->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355 break;
356
357 case INO_STATE_PRESENT:
358 /* It's in-core. GC must iget() it. */
359 break;
360
361 case INO_STATE_UNCHECKED:
362 case INO_STATE_CHECKING:
363 case INO_STATE_GC:
364 /* Should never happen. We should have finished checking
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000365 by the time we actually start doing any GC, and since
366 we're holding the alloc_sem, no other garbage collection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 can happen.
368 */
Joe Perchesda320f02012-02-15 15:56:44 -0800369 pr_crit("Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
370 ic->ino, ic->state);
David Woodhouseced22072008-04-22 15:13:40 +0100371 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock(&c->inocache_lock);
373 BUG();
374
375 case INO_STATE_READING:
376 /* Someone's currently trying to read it. We must wait for
377 them to finish and then go through the full iget() route
378 to do the GC. However, sometimes read_inode() needs to get
379 the alloc_sem() (for marking nodes invalid) so we must
380 drop the alloc_sem before sleeping. */
381
David Woodhouseced22072008-04-22 15:13:40 +0100382 mutex_unlock(&c->alloc_sem);
Joe Perches9c261b32012-02-15 15:56:43 -0800383 jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n",
384 __func__, ic->ino, ic->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000386 /* And because we dropped the alloc_sem we must start again from the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 beginning. Ponder chance of livelock here -- we're returning success
388 without actually making any progress.
389
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000390 Q: What are the chances that the inode is back in INO_STATE_READING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 again by the time we next enter this function? And that this happens
392 enough times to cause a real delay?
393
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000394 A: Small enough that I don't care :)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396 return 0;
397 }
398
399 /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000400 node intact, and we don't have to muck about with the fragtree etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 because we know it's not in-core. If it _was_ in-core, we go through
402 all the iget() crap anyway */
403
404 if (ic->state == INO_STATE_GC) {
405 spin_unlock(&c->inocache_lock);
406
407 ret = jffs2_garbage_collect_pristine(c, ic, raw);
408
409 spin_lock(&c->inocache_lock);
410 ic->state = INO_STATE_CHECKEDABSENT;
411 wake_up(&c->inocache_wq);
412
413 if (ret != -EBADFD) {
414 spin_unlock(&c->inocache_lock);
David Woodhouse2665ea82007-10-13 11:31:23 +0100415 goto test_gcnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
418 /* Fall through if it wanted us to, with inocache_lock held */
419 }
420
421 /* Prevent the fairly unlikely race where the gcblock is
422 entirely obsoleted by the final close of a file which had
423 the only valid nodes in the block, followed by erasure,
424 followed by freeing of the ic because the erased block(s)
425 held _all_ the nodes of that inode.... never been seen but
426 it's vaguely possible. */
427
428 inum = ic->ino;
David Woodhouse27c72b02008-05-01 18:47:17 +0100429 nlink = ic->pino_nlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 spin_unlock(&c->inocache_lock);
431
David Woodhouse1b690b42008-05-01 16:59:24 +0100432 f = jffs2_gc_fetch_inode(c, inum, !nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (IS_ERR(f)) {
434 ret = PTR_ERR(f);
435 goto release_sem;
436 }
437 if (!f) {
438 ret = 0;
439 goto release_sem;
440 }
441
442 ret = jffs2_garbage_collect_live(c, jeb, raw, f);
443
444 jffs2_gc_release_inode(c, f);
445
David Woodhouse2665ea82007-10-13 11:31:23 +0100446 test_gcnode:
447 if (jeb->dirty_size == gcblock_dirty && !ref_obsolete(jeb->gc_node)) {
448 /* Eep. This really should never happen. GC is broken */
Joe Perchesda320f02012-02-15 15:56:44 -0800449 pr_err("Error garbage collecting node at %08x!\n",
450 ref_offset(jeb->gc_node));
David Woodhouse2665ea82007-10-13 11:31:23 +0100451 ret = -ENOSPC;
David Woodhouse4fc8a602007-10-13 14:29:39 +0100452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 release_sem:
David Woodhouseced22072008-04-22 15:13:40 +0100454 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 eraseit_lock:
457 /* If we've finished this block, start it erasing */
458 spin_lock(&c->erase_completion_lock);
459
460 eraseit:
461 if (c->gcblock && !c->gcblock->used_size) {
Joe Perches9c261b32012-02-15 15:56:43 -0800462 jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n",
463 c->gcblock->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* We're GC'ing an empty block? */
465 list_add_tail(&c->gcblock->list, &c->erase_pending_list);
466 c->gcblock = NULL;
467 c->nr_erasing_blocks++;
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100468 jffs2_garbage_collect_trigger(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 spin_unlock(&c->erase_completion_lock);
471
472 return ret;
473}
474
475static int jffs2_garbage_collect_live(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
476 struct jffs2_raw_node_ref *raw, struct jffs2_inode_info *f)
477{
478 struct jffs2_node_frag *frag;
479 struct jffs2_full_dnode *fn = NULL;
480 struct jffs2_full_dirent *fd;
481 uint32_t start = 0, end = 0, nrfrags = 0;
482 int ret = 0;
483
David Woodhouseced22072008-04-22 15:13:40 +0100484 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /* Now we have the lock for this inode. Check that it's still the one at the head
487 of the list. */
488
489 spin_lock(&c->erase_completion_lock);
490
491 if (c->gcblock != jeb) {
492 spin_unlock(&c->erase_completion_lock);
Joe Perches9c261b32012-02-15 15:56:43 -0800493 jffs2_dbg(1, "GC block is no longer gcblock. Restart\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto upnout;
495 }
496 if (ref_obsolete(raw)) {
497 spin_unlock(&c->erase_completion_lock);
Joe Perches9c261b32012-02-15 15:56:43 -0800498 jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* They'll call again */
500 goto upnout;
501 }
502 spin_unlock(&c->erase_completion_lock);
503
504 /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
505 if (f->metadata && f->metadata->raw == raw) {
506 fn = f->metadata;
507 ret = jffs2_garbage_collect_metadata(c, jeb, f, fn);
508 goto upnout;
509 }
510
511 /* FIXME. Read node and do lookup? */
512 for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
513 if (frag->node && frag->node->raw == raw) {
514 fn = frag->node;
515 end = frag->ofs + frag->size;
516 if (!nrfrags++)
517 start = frag->ofs;
518 if (nrfrags == frag->node->frags)
519 break; /* We've found them all */
520 }
521 }
522 if (fn) {
523 if (ref_flags(raw) == REF_PRISTINE) {
524 ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
525 if (!ret) {
526 /* Urgh. Return it sensibly. */
527 frag->node->raw = f->inocache->nodes;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (ret != -EBADFD)
530 goto upnout;
531 }
532 /* We found a datanode. Do the GC */
533 if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
534 /* It crosses a page boundary. Therefore, it must be a hole. */
535 ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
536 } else {
537 /* It could still be a hole. But we GC the page this way anyway */
538 ret = jffs2_garbage_collect_dnode(c, jeb, f, fn, start, end);
539 }
540 goto upnout;
541 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* Wasn't a dnode. Try dirent */
544 for (fd = f->dents; fd; fd=fd->next) {
545 if (fd->raw == raw)
546 break;
547 }
548
549 if (fd && fd->ino) {
550 ret = jffs2_garbage_collect_dirent(c, jeb, f, fd);
551 } else if (fd) {
552 ret = jffs2_garbage_collect_deletion_dirent(c, jeb, f, fd);
553 } else {
Joe Perchesda320f02012-02-15 15:56:44 -0800554 pr_warn("Raw node at 0x%08x wasn't in node lists for ino #%u\n",
555 ref_offset(raw), f->inocache->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (ref_obsolete(raw)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800557 pr_warn("But it's obsolete so we don't mind too much\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 } else {
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100559 jffs2_dbg_dump_node(c, ref_offset(raw));
560 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 }
563 upnout:
David Woodhouseced22072008-04-22 15:13:40 +0100564 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 return ret;
567}
568
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000569static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct jffs2_inode_cache *ic,
571 struct jffs2_raw_node_ref *raw)
572{
573 union jffs2_node_union *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 size_t retlen;
575 int ret;
576 uint32_t phys_ofs, alloclen;
577 uint32_t crc, rawlen;
578 int retried = 0;
579
Joe Perches9c261b32012-02-15 15:56:43 -0800580 jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n",
581 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Woodhouse61715862006-05-21 00:02:06 +0100583 alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* Ask for a small amount of space (or the totlen if smaller) because we
586 don't want to force wastage of the end of a block if splitting would
587 work. */
David Woodhouse61715862006-05-21 00:02:06 +0100588 if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
589 alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
590
David Woodhouse9fe48542006-05-23 00:38:06 +0100591 ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
David Woodhouse61715862006-05-21 00:02:06 +0100592 /* 'rawlen' is not the exact summary size; it is only an upper estimation */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (ret)
595 return ret;
596
597 if (alloclen < rawlen) {
598 /* Doesn't fit untouched. We'll go the old route and split it */
599 return -EBADFD;
600 }
601
602 node = kmalloc(rawlen, GFP_KERNEL);
603 if (!node)
David Woodhouseef53cb02007-07-10 10:01:22 +0100604 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)node);
607 if (!ret && retlen != rawlen)
608 ret = -EIO;
609 if (ret)
610 goto out_node;
611
612 crc = crc32(0, node, sizeof(struct jffs2_unknown_node)-4);
613 if (je32_to_cpu(node->u.hdr_crc) != crc) {
Joe Perchesda320f02012-02-15 15:56:44 -0800614 pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
615 ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto bail;
617 }
618
619 switch(je16_to_cpu(node->u.nodetype)) {
620 case JFFS2_NODETYPE_INODE:
621 crc = crc32(0, node, sizeof(node->i)-8);
622 if (je32_to_cpu(node->i.node_crc) != crc) {
Joe Perchesda320f02012-02-15 15:56:44 -0800623 pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
624 ref_offset(raw), je32_to_cpu(node->i.node_crc),
625 crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto bail;
627 }
628
629 if (je32_to_cpu(node->i.dsize)) {
630 crc = crc32(0, node->i.data, je32_to_cpu(node->i.csize));
631 if (je32_to_cpu(node->i.data_crc) != crc) {
Joe Perchesda320f02012-02-15 15:56:44 -0800632 pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
633 ref_offset(raw),
634 je32_to_cpu(node->i.data_crc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto bail;
636 }
637 }
638 break;
639
640 case JFFS2_NODETYPE_DIRENT:
641 crc = crc32(0, node, sizeof(node->d)-8);
642 if (je32_to_cpu(node->d.node_crc) != crc) {
Joe Perchesda320f02012-02-15 15:56:44 -0800643 pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
644 ref_offset(raw),
645 je32_to_cpu(node->d.node_crc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto bail;
647 }
648
David Woodhouseb534e702007-10-13 11:35:58 +0100649 if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
Joe Perchesda320f02012-02-15 15:56:44 -0800650 pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
651 ref_offset(raw));
David Woodhouseb534e702007-10-13 11:35:58 +0100652 goto bail;
653 }
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (node->d.nsize) {
656 crc = crc32(0, node->d.name, node->d.nsize);
657 if (je32_to_cpu(node->d.name_crc) != crc) {
Joe Perchesda320f02012-02-15 15:56:44 -0800658 pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
659 ref_offset(raw),
660 je32_to_cpu(node->d.name_crc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 goto bail;
662 }
663 }
664 break;
665 default:
David Woodhouse61715862006-05-21 00:02:06 +0100666 /* If it's inode-less, we don't _know_ what it is. Just copy it intact */
667 if (ic) {
Joe Perchesda320f02012-02-15 15:56:44 -0800668 pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
669 ref_offset(raw), je16_to_cpu(node->u.nodetype));
David Woodhouse61715862006-05-21 00:02:06 +0100670 goto bail;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* OK, all the CRCs are good; this node can just be copied as-is. */
675 retry:
David Woodhouse2f785402006-05-24 02:04:45 +0100676 phys_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
679
680 if (ret || (retlen != rawlen)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800681 pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
682 rawlen, phys_ofs, ret, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (retlen) {
David Woodhouse2f785402006-05-24 02:04:45 +0100684 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
Joe Perchesda320f02012-02-15 15:56:44 -0800686 pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
687 phys_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
David Woodhouse2f785402006-05-24 02:04:45 +0100689 if (!retried) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* Try to reallocate space and retry */
691 uint32_t dummy;
692 struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
693
694 retried = 1;
695
Joe Perches9c261b32012-02-15 15:56:43 -0800696 jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000697
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
David Woodhouse9fe48542006-05-23 00:38:06 +0100701 ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100702 /* this is not the exact summary size of it,
703 it is only an upper estimation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (!ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800706 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
707 phys_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100709 jffs2_dbg_acct_sanity_check(c,jeb);
710 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 goto retry;
713 }
Joe Perches9c261b32012-02-15 15:56:43 -0800714 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
715 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!ret)
719 ret = -EIO;
720 goto out_node;
721 }
David Woodhouse2f785402006-05-24 02:04:45 +0100722 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 jffs2_mark_node_obsolete(c, raw);
Joe Perches9c261b32012-02-15 15:56:43 -0800725 jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n",
726 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 out_node:
729 kfree(node);
730 return ret;
731 bail:
732 ret = -EBADFD;
733 goto out_node;
734}
735
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000736static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
738{
739 struct jffs2_full_dnode *new_fn;
740 struct jffs2_raw_inode ri;
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +0100741 struct jffs2_node_frag *last_frag;
David Woodhouseaef9ab42006-05-19 00:28:49 +0100742 union jffs2_device_node dev;
David Woodhouse2e16cfc2009-12-16 03:27:20 +0000743 char *mdata = NULL;
744 int mdatalen = 0;
David Woodhouse9fe48542006-05-23 00:38:06 +0100745 uint32_t alloclen, ilen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int ret;
747
748 if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
749 S_ISCHR(JFFS2_F_I_MODE(f)) ) {
750 /* For these, we don't actually need to read the old node */
David Woodhouseaef9ab42006-05-19 00:28:49 +0100751 mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 mdata = (char *)&dev;
Joe Perches9c261b32012-02-15 15:56:43 -0800753 jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
754 __func__, mdatalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
756 mdatalen = fn->size;
757 mdata = kmalloc(fn->size, GFP_KERNEL);
758 if (!mdata) {
Joe Perchesda320f02012-02-15 15:56:44 -0800759 pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -ENOMEM;
761 }
762 ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
763 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -0800764 pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
765 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 kfree(mdata);
767 return ret;
768 }
Joe Perches9c261b32012-02-15 15:56:43 -0800769 jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n",
770 __func__, mdatalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000773
David Woodhouse9fe48542006-05-23 00:38:06 +0100774 ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100775 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -0800777 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
778 sizeof(ri) + mdatalen, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto out;
780 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000781
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +0100782 last_frag = frag_last(&f->fragtree);
783 if (last_frag)
784 /* Fetch the inode length from the fragtree rather then
785 * from i_size since i_size may have not been updated yet */
786 ilen = last_frag->ofs + last_frag->size;
787 else
788 ilen = JFFS2_F_I_SIZE(f);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 memset(&ri, 0, sizeof(ri));
791 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
792 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
793 ri.totlen = cpu_to_je32(sizeof(ri) + mdatalen);
794 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
795
796 ri.ino = cpu_to_je32(f->inocache->ino);
797 ri.version = cpu_to_je32(++f->highest_version);
798 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
799 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
800 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +0100801 ri.isize = cpu_to_je32(ilen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
803 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
804 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
805 ri.offset = cpu_to_je32(0);
806 ri.csize = cpu_to_je32(mdatalen);
807 ri.dsize = cpu_to_je32(mdatalen);
808 ri.compr = JFFS2_COMPR_NONE;
809 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
810 ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
811
David Woodhouse9fe48542006-05-23 00:38:06 +0100812 new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (IS_ERR(new_fn)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800815 pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 ret = PTR_ERR(new_fn);
817 goto out;
818 }
819 jffs2_mark_node_obsolete(c, fn->raw);
820 jffs2_free_full_dnode(fn);
821 f->metadata = new_fn;
822 out:
823 if (S_ISLNK(JFFS2_F_I_MODE(f)))
824 kfree(mdata);
825 return ret;
826}
827
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000828static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
830{
831 struct jffs2_full_dirent *new_fd;
832 struct jffs2_raw_dirent rd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100833 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int ret;
835
836 rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
837 rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
838 rd.nsize = strlen(fd->name);
839 rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
840 rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
841
842 rd.pino = cpu_to_je32(f->inocache->ino);
843 rd.version = cpu_to_je32(++f->highest_version);
844 rd.ino = cpu_to_je32(fd->ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100845 /* If the times on this inode were set by explicit utime() they can be different,
846 so refrain from splatting them. */
847 if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
848 rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000849 else
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100850 rd.mctime = cpu_to_je32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 rd.type = fd->type;
852 rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
853 rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000854
David Woodhouse9fe48542006-05-23 00:38:06 +0100855 ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100856 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -0800858 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
859 sizeof(rd)+rd.nsize, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return ret;
861 }
David Woodhouse9fe48542006-05-23 00:38:06 +0100862 new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (IS_ERR(new_fd)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800865 pr_warn("jffs2_write_dirent in garbage_collect_dirent failed: %ld\n",
866 PTR_ERR(new_fd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return PTR_ERR(new_fd);
868 }
869 jffs2_add_fd_to_list(c, new_fd, &f->dents);
870 return 0;
871}
872
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000873static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
875{
876 struct jffs2_full_dirent **fdp = &f->dents;
877 int found = 0;
878
879 /* On a medium where we can't actually mark nodes obsolete
880 pernamently, such as NAND flash, we need to work out
881 whether this deletion dirent is still needed to actively
882 delete a 'real' dirent with the same name that's still
883 somewhere else on the flash. */
884 if (!jffs2_can_mark_obsolete(c)) {
885 struct jffs2_raw_dirent *rd;
886 struct jffs2_raw_node_ref *raw;
887 int ret;
888 size_t retlen;
889 int name_len = strlen(fd->name);
890 uint32_t name_crc = crc32(0, fd->name, name_len);
891 uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
892
893 rd = kmalloc(rawlen, GFP_KERNEL);
894 if (!rd)
895 return -ENOMEM;
896
897 /* Prevent the erase code from nicking the obsolete node refs while
898 we're looking at them. I really don't like this extra lock but
899 can't see any alternative. Suggestions on a postcard to... */
David Woodhouseced22072008-04-22 15:13:40 +0100900 mutex_lock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) {
903
Artem Bityutskiyaba54da2006-12-19 15:45:23 +0200904 cond_resched();
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /* We only care about obsolete ones */
907 if (!(ref_obsolete(raw)))
908 continue;
909
910 /* Any dirent with the same name is going to have the same length... */
911 if (ref_totlen(c, NULL, raw) != rawlen)
912 continue;
913
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000914 /* Doesn't matter if there's one in the same erase block. We're going to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 delete it too at the same time. */
Andrew Victor3be36672005-02-09 09:09:05 +0000916 if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 continue;
918
Joe Perches9c261b32012-02-15 15:56:43 -0800919 jffs2_dbg(1, "Check potential deletion dirent at %08x\n",
920 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* This is an obsolete node belonging to the same directory, and it's of the right
923 length. We need to take a closer look...*/
924 ret = jffs2_flash_read(c, ref_offset(raw), rawlen, &retlen, (char *)rd);
925 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -0800926 pr_warn("%s(): Read error (%d) reading obsolete node at %08x\n",
927 __func__, ret, ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* If we can't read it, we don't need to continue to obsolete it. Continue */
929 continue;
930 }
931 if (retlen != rawlen) {
Joe Perchesda320f02012-02-15 15:56:44 -0800932 pr_warn("%s(): Short read (%zd not %u) reading header from obsolete node at %08x\n",
933 __func__, retlen, rawlen,
934 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 continue;
936 }
937
938 if (je16_to_cpu(rd->nodetype) != JFFS2_NODETYPE_DIRENT)
939 continue;
940
941 /* If the name CRC doesn't match, skip */
942 if (je32_to_cpu(rd->name_crc) != name_crc)
943 continue;
944
945 /* If the name length doesn't match, or it's another deletion dirent, skip */
946 if (rd->nsize != name_len || !je32_to_cpu(rd->ino))
947 continue;
948
949 /* OK, check the actual name now */
950 if (memcmp(rd->name, fd->name, name_len))
951 continue;
952
953 /* OK. The name really does match. There really is still an older node on
954 the flash which our deletion dirent obsoletes. So we have to write out
955 a new deletion dirent to replace it */
David Woodhouseced22072008-04-22 15:13:40 +0100956 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Joe Perches9c261b32012-02-15 15:56:43 -0800958 jffs2_dbg(1, "Deletion dirent at %08x still obsoletes real dirent \"%s\" at %08x for ino #%u\n",
959 ref_offset(fd->raw), fd->name,
960 ref_offset(raw), je32_to_cpu(rd->ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 kfree(rd);
962
963 return jffs2_garbage_collect_dirent(c, jeb, f, fd);
964 }
965
David Woodhouseced22072008-04-22 15:13:40 +0100966 mutex_unlock(&c->erase_free_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree(rd);
968 }
969
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000970 /* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100971 we should update the metadata node with those times accordingly */
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* No need for it any more. Just mark it obsolete and remove it from the list */
974 while (*fdp) {
975 if ((*fdp) == fd) {
976 found = 1;
977 *fdp = fd->next;
978 break;
979 }
980 fdp = &(*fdp)->next;
981 }
982 if (!found) {
Joe Perchesda320f02012-02-15 15:56:44 -0800983 pr_warn("Deletion dirent \"%s\" not found in list for ino #%u\n",
984 fd->name, f->inocache->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 jffs2_mark_node_obsolete(c, fd->raw);
987 jffs2_free_full_dirent(fd);
988 return 0;
989}
990
991static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
992 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
993 uint32_t start, uint32_t end)
994{
995 struct jffs2_raw_inode ri;
996 struct jffs2_node_frag *frag;
997 struct jffs2_full_dnode *new_fn;
David Woodhouse9fe48542006-05-23 00:38:06 +0100998 uint32_t alloclen, ilen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int ret;
1000
Joe Perches9c261b32012-02-15 15:56:43 -08001001 jffs2_dbg(1, "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
1002 f->inocache->ino, start, end);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 memset(&ri, 0, sizeof(ri));
1005
1006 if(fn->frags > 1) {
1007 size_t readlen;
1008 uint32_t crc;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001009 /* It's partially obsoleted by a later write. So we have to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 write it out again with the _same_ version as before */
1011 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
1012 if (readlen != sizeof(ri) || ret) {
Joe Perchesda320f02012-02-15 15:56:44 -08001013 pr_warn("Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %zd. Data will be lost by writing new hole node\n",
1014 ret, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto fill;
1016 }
1017 if (je16_to_cpu(ri.nodetype) != JFFS2_NODETYPE_INODE) {
Joe Perchesda320f02012-02-15 15:56:44 -08001018 pr_warn("%s(): Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
1019 __func__, ref_offset(fn->raw),
1020 je16_to_cpu(ri.nodetype), JFFS2_NODETYPE_INODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EIO;
1022 }
1023 if (je32_to_cpu(ri.totlen) != sizeof(ri)) {
Joe Perchesda320f02012-02-15 15:56:44 -08001024 pr_warn("%s(): Node at 0x%08x had totlen 0x%x instead of expected 0x%zx\n",
1025 __func__, ref_offset(fn->raw),
1026 je32_to_cpu(ri.totlen), sizeof(ri));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EIO;
1028 }
1029 crc = crc32(0, &ri, sizeof(ri)-8);
1030 if (crc != je32_to_cpu(ri.node_crc)) {
Joe Perchesda320f02012-02-15 15:56:44 -08001031 pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
1032 __func__, ref_offset(fn->raw),
1033 je32_to_cpu(ri.node_crc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* FIXME: We could possibly deal with this by writing new holes for each frag */
Joe Perchesda320f02012-02-15 15:56:44 -08001035 pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
1036 start, end, f->inocache->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto fill;
1038 }
1039 if (ri.compr != JFFS2_COMPR_ZERO) {
Joe Perchesda320f02012-02-15 15:56:44 -08001040 pr_warn("%s(): Node 0x%08x wasn't a hole node!\n",
1041 __func__, ref_offset(fn->raw));
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto fill;
1045 }
1046 } else {
1047 fill:
1048 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1049 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1050 ri.totlen = cpu_to_je32(sizeof(ri));
1051 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1052
1053 ri.ino = cpu_to_je32(f->inocache->ino);
1054 ri.version = cpu_to_je32(++f->highest_version);
1055 ri.offset = cpu_to_je32(start);
1056 ri.dsize = cpu_to_je32(end - start);
1057 ri.csize = cpu_to_je32(0);
1058 ri.compr = JFFS2_COMPR_ZERO;
1059 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001060
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +01001061 frag = frag_last(&f->fragtree);
1062 if (frag)
1063 /* Fetch the inode length from the fragtree rather then
1064 * from i_size since i_size may have not been updated yet */
1065 ilen = frag->ofs + frag->size;
1066 else
1067 ilen = JFFS2_F_I_SIZE(f);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1070 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1071 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +01001072 ri.isize = cpu_to_je32(ilen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1074 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1075 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1076 ri.data_crc = cpu_to_je32(0);
1077 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1078
David Woodhouse9fe48542006-05-23 00:38:06 +01001079 ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
1080 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -08001082 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1083 sizeof(ri), ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return ret;
1085 }
David Woodhouse9fe48542006-05-23 00:38:06 +01001086 new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (IS_ERR(new_fn)) {
Joe Perchesda320f02012-02-15 15:56:44 -08001089 pr_warn("Error writing new hole node: %ld\n", PTR_ERR(new_fn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return PTR_ERR(new_fn);
1091 }
1092 if (je32_to_cpu(ri.version) == f->highest_version) {
1093 jffs2_add_full_dnode_to_inode(c, f, new_fn);
1094 if (f->metadata) {
1095 jffs2_mark_node_obsolete(c, f->metadata->raw);
1096 jffs2_free_full_dnode(f->metadata);
1097 f->metadata = NULL;
1098 }
1099 return 0;
1100 }
1101
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001102 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * We should only get here in the case where the node we are
1104 * replacing had more than one frag, so we kept the same version
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001105 * number as before. (Except in case of error -- see 'goto fill;'
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 * above.)
1107 */
1108 D1(if(unlikely(fn->frags <= 1)) {
Joe Perchesda320f02012-02-15 15:56:44 -08001109 pr_warn("%s(): Replacing fn with %d frag(s) but new ver %d != highest_version %d of ino #%d\n",
1110 __func__, fn->frags, je32_to_cpu(ri.version),
1111 f->highest_version, je32_to_cpu(ri.ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 });
1113
1114 /* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
1115 mark_ref_normal(new_fn->raw);
1116
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001117 for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 frag; frag = frag_next(frag)) {
1119 if (frag->ofs > fn->size + fn->ofs)
1120 break;
1121 if (frag->node == fn) {
1122 frag->node = new_fn;
1123 new_fn->frags++;
1124 fn->frags--;
1125 }
1126 }
1127 if (fn->frags) {
Joe Perchesda320f02012-02-15 15:56:44 -08001128 pr_warn("%s(): Old node still has frags!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 BUG();
1130 }
1131 if (!new_fn->frags) {
Joe Perchesda320f02012-02-15 15:56:44 -08001132 pr_warn("%s(): New node has no frags!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 BUG();
1134 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 jffs2_mark_node_obsolete(c, fn->raw);
1137 jffs2_free_full_dnode(fn);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
David Woodhouse25dc30b2008-04-22 12:12:25 +01001142static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
1144 uint32_t start, uint32_t end)
1145{
1146 struct jffs2_full_dnode *new_fn;
1147 struct jffs2_raw_inode ri;
David Woodhouse9fe48542006-05-23 00:38:06 +01001148 uint32_t alloclen, offset, orig_end, orig_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int ret = 0;
1150 unsigned char *comprbuf = NULL, *writebuf;
1151 unsigned long pg;
1152 unsigned char *pg_ptr;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 memset(&ri, 0, sizeof(ri));
1155
Joe Perches9c261b32012-02-15 15:56:43 -08001156 jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
1157 f->inocache->ino, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 orig_end = end;
1160 orig_start = start;
1161
1162 if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
1163 /* Attempt to do some merging. But only expand to cover logically
1164 adjacent frags if the block containing them is already considered
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001165 to be dirty. Otherwise we end up with GC just going round in
1166 circles dirtying the nodes it already wrote out, especially
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 on NAND where we have small eraseblocks and hence a much higher
1168 chance of nodes having to be split to cross boundaries. */
1169
1170 struct jffs2_node_frag *frag;
1171 uint32_t min, max;
1172
1173 min = start & ~(PAGE_CACHE_SIZE-1);
1174 max = min + PAGE_CACHE_SIZE;
1175
1176 frag = jffs2_lookup_node_frag(&f->fragtree, start);
1177
1178 /* BUG_ON(!frag) but that'll happen anyway... */
1179
1180 BUG_ON(frag->ofs != start);
1181
1182 /* First grow down... */
1183 while((frag = frag_prev(frag)) && frag->ofs >= min) {
1184
1185 /* If the previous frag doesn't even reach the beginning, there's
1186 excessive fragmentation. Just merge. */
1187 if (frag->ofs > min) {
Joe Perches9c261b32012-02-15 15:56:43 -08001188 jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n",
1189 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 start = frag->ofs;
1191 continue;
1192 }
1193 /* OK. This frag holds the first byte of the page. */
1194 if (!frag->node || !frag->node->raw) {
Joe Perches9c261b32012-02-15 15:56:43 -08001195 jffs2_dbg(1, "First frag in page is hole (0x%x-0x%x). Not expanding down.\n",
1196 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198 } else {
1199
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001200 /* OK, it's a frag which extends to the beginning of the page. Does it live
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 in a block which is still considered clean? If so, don't obsolete it.
1202 If not, cover it anyway. */
1203
1204 struct jffs2_raw_node_ref *raw = frag->node->raw;
1205 struct jffs2_eraseblock *jeb;
1206
1207 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1208
1209 if (jeb == c->gcblock) {
Joe Perches9c261b32012-02-15 15:56:43 -08001210 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1211 frag->ofs,
1212 frag->ofs + frag->size,
1213 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 start = frag->ofs;
1215 break;
1216 }
1217 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
Joe Perches9c261b32012-02-15 15:56:43 -08001218 jffs2_dbg(1, "Not expanding down to cover frag (0x%x-0x%x) in clean block %08x\n",
1219 frag->ofs,
1220 frag->ofs + frag->size,
1221 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 }
1224
Joe Perches9c261b32012-02-15 15:56:43 -08001225 jffs2_dbg(1, "Expanding down to cover frag (0x%x-0x%x) in dirty block %08x\n",
1226 frag->ofs,
1227 frag->ofs + frag->size,
1228 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 start = frag->ofs;
1230 break;
1231 }
1232 }
1233
1234 /* ... then up */
1235
1236 /* Find last frag which is actually part of the node we're to GC. */
1237 frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
1238
1239 while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
1240
1241 /* If the previous frag doesn't even reach the beginning, there's lots
1242 of fragmentation. Just merge. */
1243 if (frag->ofs+frag->size < max) {
Joe Perches9c261b32012-02-15 15:56:43 -08001244 jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n",
1245 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 end = frag->ofs + frag->size;
1247 continue;
1248 }
1249
1250 if (!frag->node || !frag->node->raw) {
Joe Perches9c261b32012-02-15 15:56:43 -08001251 jffs2_dbg(1, "Last frag in page is hole (0x%x-0x%x). Not expanding up.\n",
1252 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 } else {
1255
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001256 /* OK, it's a frag which extends to the beginning of the page. Does it live
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 in a block which is still considered clean? If so, don't obsolete it.
1258 If not, cover it anyway. */
1259
1260 struct jffs2_raw_node_ref *raw = frag->node->raw;
1261 struct jffs2_eraseblock *jeb;
1262
1263 jeb = &c->blocks[raw->flash_offset / c->sector_size];
1264
1265 if (jeb == c->gcblock) {
Joe Perches9c261b32012-02-15 15:56:43 -08001266 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in gcblock at %08x\n",
1267 frag->ofs,
1268 frag->ofs + frag->size,
1269 ref_offset(raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 end = frag->ofs + frag->size;
1271 break;
1272 }
1273 if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) {
Joe Perches9c261b32012-02-15 15:56:43 -08001274 jffs2_dbg(1, "Not expanding up to cover frag (0x%x-0x%x) in clean block %08x\n",
1275 frag->ofs,
1276 frag->ofs + frag->size,
1277 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 }
1280
Joe Perches9c261b32012-02-15 15:56:43 -08001281 jffs2_dbg(1, "Expanding up to cover frag (0x%x-0x%x) in dirty block %08x\n",
1282 frag->ofs,
1283 frag->ofs + frag->size,
1284 jeb->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 end = frag->ofs + frag->size;
1286 break;
1287 }
1288 }
Joe Perches9c261b32012-02-15 15:56:43 -08001289 jffs2_dbg(1, "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
1290 orig_start, orig_end, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Artem B. Bityuckiy8557fd52005-04-09 11:47:03 +01001292 D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 BUG_ON(end < orig_end);
1294 BUG_ON(start > orig_start);
1295 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* First, use readpage() to read the appropriate page into the page cache */
1298 /* Q: What happens if we actually try to GC the _same_ page for which commit_write()
1299 * triggered garbage collection in the first place?
1300 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
1301 * page OK. We'll actually write it out again in commit_write, which is a little
1302 * suboptimal, but at least we're correct.
1303 */
1304 pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
1305
1306 if (IS_ERR(pg_ptr)) {
Joe Perchesda320f02012-02-15 15:56:44 -08001307 pr_warn("read_cache_page() returned error: %ld\n",
1308 PTR_ERR(pg_ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return PTR_ERR(pg_ptr);
1310 }
1311
1312 offset = start;
1313 while(offset < orig_end) {
1314 uint32_t datalen;
1315 uint32_t cdatalen;
1316 uint16_t comprtype = JFFS2_COMPR_NONE;
1317
David Woodhouse9fe48542006-05-23 00:38:06 +01001318 ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001319 &alloclen, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (ret) {
Joe Perchesda320f02012-02-15 15:56:44 -08001322 pr_warn("jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
1323 sizeof(ri) + JFFS2_MIN_DATA_LEN, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 }
1326 cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
1327 datalen = end - offset;
1328
1329 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
1330
1331 comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
1332
1333 ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1334 ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
1335 ri.totlen = cpu_to_je32(sizeof(ri) + cdatalen);
1336 ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
1337
1338 ri.ino = cpu_to_je32(f->inocache->ino);
1339 ri.version = cpu_to_je32(++f->highest_version);
1340 ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
1341 ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
1342 ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
1343 ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
1344 ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
1345 ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
1346 ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
1347 ri.offset = cpu_to_je32(offset);
1348 ri.csize = cpu_to_je32(cdatalen);
1349 ri.dsize = cpu_to_je32(datalen);
1350 ri.compr = comprtype & 0xff;
1351 ri.usercompr = (comprtype >> 8) & 0xff;
1352 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1353 ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001354
David Woodhouse9fe48542006-05-23 00:38:06 +01001355 new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 jffs2_free_comprbuf(comprbuf, writebuf);
1358
1359 if (IS_ERR(new_fn)) {
Joe Perchesda320f02012-02-15 15:56:44 -08001360 pr_warn("Error writing new dnode: %ld\n",
1361 PTR_ERR(new_fn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ret = PTR_ERR(new_fn);
1363 break;
1364 }
1365 ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
1366 offset += datalen;
1367 if (f->metadata) {
1368 jffs2_mark_node_obsolete(c, f->metadata->raw);
1369 jffs2_free_full_dnode(f->metadata);
1370 f->metadata = NULL;
1371 }
1372 }
1373
1374 jffs2_gc_release_page(c, pg_ptr, &pg);
1375 return ret;
1376}