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