Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
David Woodhouse | c00c310 | 2007-04-25 14:16:47 +0100 | [diff] [blame] | 4 | * Copyright © 2001-2007 Red Hat, Inc. |
David Woodhouse | 6088c05 | 2010-08-08 14:15:22 +0100 | [diff] [blame] | 5 | * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Created by David Woodhouse <dwmw2@infradead.org> |
| 8 | * |
| 9 | * For licensing information, see the file 'LICENCE' in this directory. |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 23 | static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | struct jffs2_inode_cache *ic, |
| 25 | struct jffs2_raw_node_ref *raw); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 26 | static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct jffs2_inode_info *f, struct jffs2_full_dnode *fd); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 28 | static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | struct jffs2_inode_info *f, struct jffs2_full_dirent *fd); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 30 | static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | struct jffs2_inode_info *f, struct jffs2_full_dirent *fd); |
| 32 | static 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); |
| 35 | static 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); |
| 38 | static 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 */ |
| 42 | static 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. Bityuckiy | a42163d | 2005-03-20 17:45:29 +0000 | [diff] [blame] | 52 | again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 54 | jffs2_dbg(1, "Picking block from bad_used_list to GC next\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | nextlist = &c->bad_used_list; |
| 56 | } else if (n < 50 && !list_empty(&c->erasable_list)) { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 57 | /* Note that most of them will have gone directly to be erased. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | So don't favour the erasable_list _too_ much. */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 59 | jffs2_dbg(1, "Picking block from erasable_list to GC next\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 63 | jffs2_dbg(1, "Picking block from very_dirty_list to GC next\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | nextlist = &c->very_dirty_list; |
| 65 | } else if (n < 126 && !list_empty(&c->dirty_list)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 66 | jffs2_dbg(1, "Picking block from dirty_list to GC next\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | nextlist = &c->dirty_list; |
| 68 | } else if (!list_empty(&c->clean_list)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 69 | jffs2_dbg(1, "Picking block from clean_list to GC next\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | nextlist = &c->clean_list; |
| 71 | } else if (!list_empty(&c->dirty_list)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 72 | jffs2_dbg(1, "Picking block from dirty_list to GC next (clean_list was empty)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | nextlist = &c->dirty_list; |
| 75 | } else if (!list_empty(&c->very_dirty_list)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 76 | jffs2_dbg(1, "Picking block from very_dirty_list to GC next (clean_list and dirty_list were empty)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | nextlist = &c->very_dirty_list; |
| 78 | } else if (!list_empty(&c->erasable_list)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 79 | jffs2_dbg(1, "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | nextlist = &c->erasable_list; |
Artem B. Bityuckiy | a42163d | 2005-03-20 17:45:29 +0000 | [diff] [blame] | 82 | } else if (!list_empty(&c->erasable_pending_wbuf_list)) { |
| 83 | /* There are blocks are wating for the wbuf sync */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 84 | jffs2_dbg(1, "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n"); |
Artem B. Bityuckiy | 3cceb9f | 2005-03-20 21:43:26 +0000 | [diff] [blame] | 85 | spin_unlock(&c->erase_completion_lock); |
Artem B. Bityuckiy | a42163d | 2005-03-20 17:45:29 +0000 | [diff] [blame] | 86 | jffs2_flush_wbuf_pad(c); |
Artem B. Bityuckiy | 3cceb9f | 2005-03-20 21:43:26 +0000 | [diff] [blame] | 87 | spin_lock(&c->erase_completion_lock); |
Artem B. Bityuckiy | a42163d | 2005-03-20 17:45:29 +0000 | [diff] [blame] | 88 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } else { |
| 90 | /* Eep. All were empty */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 91 | jffs2_dbg(1, "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* Have we accidentally picked a clean block with wasted space ? */ |
| 105 | if (ret->wasted_size) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 106 | jffs2_dbg(1, "Converting wasted_size %08x to dirty_size\n", |
| 107 | ret->wasted_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | 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 | */ |
| 121 | int 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 Woodhouse | 2665ea8 | 2007-10-13 11:31:23 +0100 | [diff] [blame] | 127 | uint32_t gcblock_dirty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int ret = 0, inum, nlink; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 129 | int xattr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 131 | if (mutex_lock_interruptible(&c->alloc_sem)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* checked_ino is protected by the alloc_sem */ |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 143 | if (c->checked_ino > c->highest_ino && xattr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n", |
| 145 | c->unchecked_size); |
Artem B. Bityutskiy | e0c8e42 | 2005-07-24 16:14:17 +0100 | [diff] [blame] | 146 | jffs2_dbg_dump_block_lists_nolock(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 148 | mutex_unlock(&c->alloc_sem); |
David Woodhouse | 44b998e | 2007-04-23 12:11:46 +0100 | [diff] [blame] | 149 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | spin_unlock(&c->erase_completion_lock); |
| 153 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 154 | if (!xattr) |
| 155 | xattr = jffs2_verify_xattr(c); |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | 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 Woodhouse | 27c72b0 | 2008-05-01 18:47:17 +0100 | [diff] [blame] | 166 | if (!ic->pino_nlink) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 167 | jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n", |
| 168 | ic->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | spin_unlock(&c->inocache_lock); |
KaiGai Kohei | 355ed4e | 2006-06-24 09:15:36 +0900 | [diff] [blame] | 170 | jffs2_xattr_delete_inode(c, ic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
| 173 | switch(ic->state) { |
| 174 | case INO_STATE_CHECKEDABSENT: |
| 175 | case INO_STATE_PRESENT: |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 176 | jffs2_dbg(1, "Skipping ino #%u already checked\n", |
| 177 | ic->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 189 | and trigger the BUG() above while we haven't yet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | finished checking all its nodes */ |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 191 | jffs2_dbg(1, "Waiting for ino #%u to finish reading\n", |
| 192 | ic->ino); |
David Woodhouse | d96fb99 | 2006-04-17 00:19:48 +0100 | [diff] [blame] | 193 | /* 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 197 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 210 | jffs2_dbg(1, "%s(): triggering inode scan of ino#%u\n", |
| 211 | __func__, ic->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 218 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
David Woodhouse | 0717bf8 | 2010-05-19 16:37:13 +0100 | [diff] [blame] | 222 | /* 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 Tjernlund | 81cfc9f | 2010-10-07 18:01:44 +0200 | [diff] [blame] | 226 | mutex_unlock(&c->alloc_sem); |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 227 | jffs2_dbg(1, "%s(): erasing pending blocks\n", __func__); |
Joakim Tjernlund | 81cfc9f | 2010-10-07 18:01:44 +0200 | [diff] [blame] | 228 | if (jffs2_erase_pending_blocks(c, 1)) |
David Woodhouse | 0717bf8 | 2010-05-19 16:37:13 +0100 | [diff] [blame] | 229 | return 0; |
Joakim Tjernlund | 81cfc9f | 2010-10-07 18:01:44 +0200 | [diff] [blame] | 230 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 231 | jffs2_dbg(1, "No progress from erasing block; doing GC anyway\n"); |
David Woodhouse | 0717bf8 | 2010-05-19 16:37:13 +0100 | [diff] [blame] | 232 | spin_lock(&c->erase_completion_lock); |
Joakim Tjernlund | 81cfc9f | 2010-10-07 18:01:44 +0200 | [diff] [blame] | 233 | mutex_lock(&c->alloc_sem); |
David Woodhouse | 0717bf8 | 2010-05-19 16:37:13 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* 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 Woodhouse | 422b120 | 2008-04-23 15:40:52 +0100 | [diff] [blame] | 243 | /* Couldn't find a free block. But maybe we can just erase one and make 'progress'? */ |
David Woodhouse | 0717bf8 | 2010-05-19 16:37:13 +0100 | [diff] [blame] | 244 | if (c->nr_erasing_blocks) { |
David Woodhouse | 422b120 | 2008-04-23 15:40:52 +0100 | [diff] [blame] | 245 | spin_unlock(&c->erase_completion_lock); |
| 246 | mutex_unlock(&c->alloc_sem); |
| 247 | return -EAGAIN; |
| 248 | } |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 249 | jffs2_dbg(1, "jffs2: Couldn't find erase block to garbage collect!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 251 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return -EIO; |
| 253 | } |
| 254 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 255 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 261 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | goto eraseit; |
| 263 | } |
| 264 | |
| 265 | raw = jeb->gc_node; |
David Woodhouse | 2665ea8 | 2007-10-13 11:31:23 +0100 | [diff] [blame] | 266 | gcblock_dirty = jeb->dirty_size; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | while(ref_obsolete(raw)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 269 | jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n", |
| 270 | ref_offset(raw)); |
David Woodhouse | 99988f7 | 2006-05-24 09:04:17 +0100 | [diff] [blame] | 271 | raw = ref_next(raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (unlikely(!raw)) { |
| 273 | printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n"); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 274 | printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size); |
| 276 | jeb->gc_node = raw; |
| 277 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 278 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | BUG(); |
| 280 | } |
| 281 | } |
| 282 | jeb->gc_node = raw; |
| 283 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 284 | jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n", |
| 285 | ref_offset(raw)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | if (!raw->next_in_ino) { |
| 288 | /* Inode-less node. Clean marker, snapshot or something like that */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | 6171586 | 2006-05-21 00:02:06 +0100 | [diff] [blame] | 290 | 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 297 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | goto eraseit_lock; |
| 299 | } |
| 300 | |
| 301 | ic = jffs2_raw_ref_to_ic(raw); |
| 302 | |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 303 | #ifdef CONFIG_JFFS2_FS_XATTR |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 304 | /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr. |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 305 | * 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 Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 308 | spin_unlock(&c->erase_completion_lock); |
| 309 | |
| 310 | if (ic->class == RAWNODE_CLASS_XATTR_DATUM) { |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 311 | ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 312 | } else { |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 313 | ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw); |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 314 | } |
David Woodhouse | 2665ea8 | 2007-10-13 11:31:23 +0100 | [diff] [blame] | 315 | goto test_gcnode; |
KaiGai Kohei | 084702e | 2006-05-13 15:16:13 +0900 | [diff] [blame] | 316 | } |
| 317 | #endif |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /* We need to hold the inocache. Either the erase_completion_lock or |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 320 | the inocache_lock are sufficient; we trade down since the inocache_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | causes less contention. */ |
| 322 | spin_lock(&c->inocache_lock); |
| 323 | |
| 324 | spin_unlock(&c->erase_completion_lock); |
| 325 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 326 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 341 | /* It's been checked, but it's not currently in-core. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 348 | jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n", |
| 349 | ic->ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 361 | by the time we actually start doing any GC, and since |
| 362 | we're holding the alloc_sem, no other garbage collection |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 367 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 378 | mutex_unlock(&c->alloc_sem); |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 379 | jffs2_dbg(1, "%s(): waiting for ino #%u in state %d\n", |
| 380 | __func__, ic->ino, ic->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 382 | /* And because we dropped the alloc_sem we must start again from the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | beginning. Ponder chance of livelock here -- we're returning success |
| 384 | without actually making any progress. |
| 385 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 386 | Q: What are the chances that the inode is back in INO_STATE_READING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | again by the time we next enter this function? And that this happens |
| 388 | enough times to cause a real delay? |
| 389 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 390 | A: Small enough that I don't care :) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 396 | node intact, and we don't have to muck about with the fragtree etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | 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 Woodhouse | 2665ea8 | 2007-10-13 11:31:23 +0100 | [diff] [blame] | 411 | goto test_gcnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 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 Woodhouse | 27c72b0 | 2008-05-01 18:47:17 +0100 | [diff] [blame] | 425 | nlink = ic->pino_nlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | spin_unlock(&c->inocache_lock); |
| 427 | |
David Woodhouse | 1b690b4 | 2008-05-01 16:59:24 +0100 | [diff] [blame] | 428 | f = jffs2_gc_fetch_inode(c, inum, !nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | 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 Woodhouse | 2665ea8 | 2007-10-13 11:31:23 +0100 | [diff] [blame] | 442 | 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 Woodhouse | 4fc8a60 | 2007-10-13 14:29:39 +0100 | [diff] [blame] | 447 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | release_sem: |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 449 | mutex_unlock(&c->alloc_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 457 | jffs2_dbg(1, "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n", |
| 458 | c->gcblock->offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /* 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 Woodhouse | ae3b6ba | 2010-05-19 17:05:14 +0100 | [diff] [blame] | 463 | jffs2_garbage_collect_trigger(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | spin_unlock(&c->erase_completion_lock); |
| 466 | |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 479 | mutex_lock(&f->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 488 | jffs2_dbg(1, "GC block is no longer gcblock. Restart\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | goto upnout; |
| 490 | } |
| 491 | if (ref_obsolete(raw)) { |
| 492 | spin_unlock(&c->erase_completion_lock); |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 493 | jffs2_dbg(1, "node to be GC'd was obsoleted in the meantime.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | /* 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | /* 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. Bityutskiy | e0c8e42 | 2005-07-24 16:14:17 +0100 | [diff] [blame] | 554 | jffs2_dbg_dump_node(c, ref_offset(raw)); |
| 555 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | upnout: |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 559 | mutex_unlock(&f->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | return ret; |
| 562 | } |
| 563 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 564 | static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | struct jffs2_inode_cache *ic, |
| 566 | struct jffs2_raw_node_ref *raw) |
| 567 | { |
| 568 | union jffs2_node_union *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | size_t retlen; |
| 570 | int ret; |
| 571 | uint32_t phys_ofs, alloclen; |
| 572 | uint32_t crc, rawlen; |
| 573 | int retried = 0; |
| 574 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 575 | jffs2_dbg(1, "Going to GC REF_PRISTINE node at 0x%08x\n", |
| 576 | ref_offset(raw)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
David Woodhouse | 6171586 | 2006-05-21 00:02:06 +0100 | [diff] [blame] | 578 | alloclen = rawlen = ref_totlen(c, c->gcblock, raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
| 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 Woodhouse | 6171586 | 2006-05-21 00:02:06 +0100 | [diff] [blame] | 583 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 586 | ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen); |
David Woodhouse | 6171586 | 2006-05-21 00:02:06 +0100 | [diff] [blame] | 587 | /* 'rawlen' is not the exact summary size; it is only an upper estimation */ |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | 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 Woodhouse | ef53cb0 | 2007-07-10 10:01:22 +0100 | [diff] [blame] | 599 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 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 Woodhouse | b534e70 | 2007-10-13 11:35:58 +0100 | [diff] [blame] | 641 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | 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 Woodhouse | b534e70 | 2007-10-13 11:35:58 +0100 | [diff] [blame] | 649 | printk(KERN_WARNING "Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | ref_offset(raw), je32_to_cpu(node->d.name_crc), crc); |
| 651 | goto bail; |
| 652 | } |
| 653 | } |
| 654 | break; |
| 655 | default: |
David Woodhouse | 6171586 | 2006-05-21 00:02:06 +0100 | [diff] [blame] | 656 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /* OK, all the CRCs are good; this node can just be copied as-is. */ |
| 665 | retry: |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 666 | phys_ofs = write_ofs(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 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 Woodhouse | ef53cb0 | 2007-07-10 10:01:22 +0100 | [diff] [blame] | 672 | rawlen, phys_ofs, ret, retlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (retlen) { |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 674 | jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } else { |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 676 | printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", phys_ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 678 | if (!retried) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | /* 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 685 | jffs2_dbg(1, "Retrying failed write of REF_PRISTINE node.\n"); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 686 | |
Artem B. Bityutskiy | 730554d | 2005-07-17 07:56:26 +0100 | [diff] [blame] | 687 | jffs2_dbg_acct_sanity_check(c,jeb); |
| 688 | jffs2_dbg_acct_paranoia_check(c, jeb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 690 | ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 691 | /* this is not the exact summary size of it, |
| 692 | it is only an upper estimation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | if (!ret) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 695 | jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n", |
| 696 | phys_ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Artem B. Bityutskiy | 730554d | 2005-07-17 07:56:26 +0100 | [diff] [blame] | 698 | jffs2_dbg_acct_sanity_check(c,jeb); |
| 699 | jffs2_dbg_acct_paranoia_check(c, jeb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | goto retry; |
| 702 | } |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 703 | jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n", |
| 704 | ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (!ret) |
| 708 | ret = -EIO; |
| 709 | goto out_node; |
| 710 | } |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 711 | jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | jffs2_mark_node_obsolete(c, raw); |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 714 | jffs2_dbg(1, "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", |
| 715 | ref_offset(raw)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | out_node: |
| 718 | kfree(node); |
| 719 | return ret; |
| 720 | bail: |
| 721 | ret = -EBADFD; |
| 722 | goto out_node; |
| 723 | } |
| 724 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 725 | static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | 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. Bityuckiy | 8557fd5 | 2005-04-09 11:47:03 +0100 | [diff] [blame] | 730 | struct jffs2_node_frag *last_frag; |
David Woodhouse | aef9ab4 | 2006-05-19 00:28:49 +0100 | [diff] [blame] | 731 | union jffs2_device_node dev; |
David Woodhouse | 2e16cfc | 2009-12-16 03:27:20 +0000 | [diff] [blame] | 732 | char *mdata = NULL; |
| 733 | int mdatalen = 0; |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 734 | uint32_t alloclen, ilen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | 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 Woodhouse | aef9ab4 | 2006-05-19 00:28:49 +0100 | [diff] [blame] | 740 | mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | mdata = (char *)&dev; |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 742 | jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n", |
| 743 | __func__, mdatalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 757 | jffs2_dbg(1, "%s(): Writing %d bites of symlink target\n", |
| 758 | __func__, mdatalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 761 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 762 | ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen, |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 763 | JFFS2_SUMMARY_INODE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 769 | |
Artem B. Bityuckiy | 8557fd5 | 2005-04-09 11:47:03 +0100 | [diff] [blame] | 770 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | 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. Bityuckiy | 8557fd5 | 2005-04-09 11:47:03 +0100 | [diff] [blame] | 789 | ri.isize = cpu_to_je32(ilen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 800 | new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 816 | static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 821 | uint32_t alloclen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | 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. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 833 | /* 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 837 | else |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 838 | rd.mctime = cpu_to_je32(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 842 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 843 | ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen, |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 844 | JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 850 | new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 860 | static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 887 | mutex_lock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | for (raw = f->inocache->nodes; raw != (void *)f->inocache; raw = raw->next_in_ino) { |
| 890 | |
Artem Bityutskiy | aba54da | 2006-12-19 15:45:23 +0200 | [diff] [blame] | 891 | cond_resched(); |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | /* 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 901 | /* Doesn't matter if there's one in the same erase block. We're going to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | delete it too at the same time. */ |
Andrew Victor | 3be3667 | 2005-02-09 09:09:05 +0000 | [diff] [blame] | 903 | if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | continue; |
| 905 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 906 | jffs2_dbg(1, "Check potential deletion dirent at %08x\n", |
| 907 | ref_offset(raw)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
| 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 Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 941 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 943 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | kfree(rd); |
| 947 | |
| 948 | return jffs2_garbage_collect_dirent(c, jeb, f, fd); |
| 949 | } |
| 950 | |
David Woodhouse | ced2207 | 2008-04-22 15:13:40 +0100 | [diff] [blame] | 951 | mutex_unlock(&c->erase_free_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | kfree(rd); |
| 953 | } |
| 954 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 955 | /* FIXME: If we're deleting a dirent which contains the current mtime and ctime, |
Artem B. Bityutskiy | 3a69e0c | 2005-08-17 14:46:26 +0100 | [diff] [blame] | 956 | we should update the metadata node with those times accordingly */ |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* 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 | |
| 975 | static 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 982 | uint32_t alloclen, ilen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | int ret; |
| 984 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 985 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | memset(&ri, 0, sizeof(ri)); |
| 989 | |
| 990 | if(fn->frags > 1) { |
| 991 | size_t readlen; |
| 992 | uint32_t crc; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 993 | /* It's partially obsoleted by a later write. So we have to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1015 | ref_offset(fn->raw), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | je32_to_cpu(ri.node_crc), crc); |
| 1017 | /* FIXME: We could possibly deal with this by writing new holes for each frag */ |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1018 | printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1024 | printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1042 | |
Artem B. Bityuckiy | 8557fd5 | 2005-04-09 11:47:03 +0100 | [diff] [blame] | 1043 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | 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. Bityuckiy | 8557fd5 | 2005-04-09 11:47:03 +0100 | [diff] [blame] | 1054 | ri.isize = cpu_to_je32(ilen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1061 | ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen, |
| 1062 | JFFS2_SUMMARY_INODE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1068 | new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1084 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | * 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1087 | * number as before. (Except in case of error -- see 'goto fill;' |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | * 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1099 | for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | jffs2_mark_node_obsolete(c, fn->raw); |
| 1119 | jffs2_free_full_dnode(fn); |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
David Woodhouse | 25dc30b | 2008-04-22 12:12:25 +0100 | [diff] [blame] | 1124 | static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *orig_jeb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1130 | uint32_t alloclen, offset, orig_end, orig_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | int ret = 0; |
| 1132 | unsigned char *comprbuf = NULL, *writebuf; |
| 1133 | unsigned long pg; |
| 1134 | unsigned char *pg_ptr; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | memset(&ri, 0, sizeof(ri)); |
| 1137 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1138 | jffs2_dbg(1, "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n", |
| 1139 | f->inocache->ino, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1147 | to be dirty. Otherwise we end up with GC just going round in |
| 1148 | circles dirtying the nodes it already wrote out, especially |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1170 | jffs2_dbg(1, "Expanding down to cover partial frag (0x%x-0x%x)\n", |
| 1171 | frag->ofs, frag->ofs+frag->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1177 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | break; |
| 1180 | } else { |
| 1181 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1182 | /* OK, it's a frag which extends to the beginning of the page. Does it live |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1192 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | start = frag->ofs; |
| 1197 | break; |
| 1198 | } |
| 1199 | if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1200 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | } |
| 1206 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1207 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1226 | jffs2_dbg(1, "Expanding up to cover partial frag (0x%x-0x%x)\n", |
| 1227 | frag->ofs, frag->ofs+frag->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | end = frag->ofs + frag->size; |
| 1229 | continue; |
| 1230 | } |
| 1231 | |
| 1232 | if (!frag->node || !frag->node->raw) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1233 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | break; |
| 1236 | } else { |
| 1237 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1238 | /* OK, it's a frag which extends to the beginning of the page. Does it live |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | 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 Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1248 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | end = frag->ofs + frag->size; |
| 1253 | break; |
| 1254 | } |
| 1255 | if (!ISDIRTY(jeb->dirty_size + jeb->wasted_size)) { |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1256 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | break; |
| 1261 | } |
| 1262 | |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1263 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | end = frag->ofs + frag->size; |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
Joe Perches | 9c261b3 | 2012-02-15 15:56:43 -0800 | [diff] [blame^] | 1271 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
Artem B. Bityuckiy | 8557fd5 | 2005-04-09 11:47:03 +0100 | [diff] [blame] | 1274 | D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | BUG_ON(end < orig_end); |
| 1276 | BUG_ON(start > orig_start); |
| 1277 | } |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | /* 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 Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1299 | ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 1300 | &alloclen, JFFS2_SUMMARY_INODE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | |
| 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 Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 1335 | |
David Woodhouse | 9fe4854 | 2006-05-23 00:38:06 +0100 | [diff] [blame] | 1336 | new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 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 | } |