Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [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 © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>, |
| 5 | * Zoltan Sogor <weth@inf.u-szeged.hu>, |
| 6 | * Patrik Kluba <pajko@halom.u-szeged.hu>, |
| 7 | * University of Szeged, Hungary |
| 8 | * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com> |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 9 | * |
| 10 | * For licensing information, see the file 'LICENCE' in this directory. |
| 11 | * |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/mtd/mtd.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/crc32.h> |
| 19 | #include <linux/compiler.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | #include "nodelist.h" |
| 22 | #include "debug.h" |
| 23 | |
| 24 | int jffs2_sum_init(struct jffs2_sb_info *c) |
| 25 | { |
Yan Burman | 3d375d9 | 2006-12-04 15:03:01 -0800 | [diff] [blame] | 26 | c->summary = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 27 | |
| 28 | if (!c->summary) { |
| 29 | JFFS2_WARNING("Can't allocate memory for summary information!\n"); |
| 30 | return -ENOMEM; |
| 31 | } |
| 32 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 33 | c->summary->sum_buf = vmalloc(c->sector_size); |
| 34 | |
| 35 | if (!c->summary->sum_buf) { |
| 36 | JFFS2_WARNING("Can't allocate buffer for writing out summary information!\n"); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 37 | kfree(c->summary); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 38 | return -ENOMEM; |
| 39 | } |
| 40 | |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 41 | dbg_summary("returned successfully\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | void jffs2_sum_exit(struct jffs2_sb_info *c) |
| 47 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 48 | dbg_summary("called\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 49 | |
| 50 | jffs2_sum_disable_collecting(c->summary); |
| 51 | |
| 52 | vfree(c->summary->sum_buf); |
| 53 | c->summary->sum_buf = NULL; |
| 54 | |
| 55 | kfree(c->summary); |
| 56 | c->summary = NULL; |
| 57 | } |
| 58 | |
| 59 | static int jffs2_sum_add_mem(struct jffs2_summary *s, union jffs2_sum_mem *item) |
| 60 | { |
| 61 | if (!s->sum_list_head) |
| 62 | s->sum_list_head = (union jffs2_sum_mem *) item; |
| 63 | if (s->sum_list_tail) |
| 64 | s->sum_list_tail->u.next = (union jffs2_sum_mem *) item; |
| 65 | s->sum_list_tail = (union jffs2_sum_mem *) item; |
| 66 | |
| 67 | switch (je16_to_cpu(item->u.nodetype)) { |
| 68 | case JFFS2_NODETYPE_INODE: |
| 69 | s->sum_size += JFFS2_SUMMARY_INODE_SIZE; |
| 70 | s->sum_num++; |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 71 | dbg_summary("inode (%u) added to summary\n", |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 72 | je32_to_cpu(item->i.inode)); |
| 73 | break; |
| 74 | case JFFS2_NODETYPE_DIRENT: |
| 75 | s->sum_size += JFFS2_SUMMARY_DIRENT_SIZE(item->d.nsize); |
| 76 | s->sum_num++; |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 77 | dbg_summary("dirent (%u) added to summary\n", |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 78 | je32_to_cpu(item->d.ino)); |
| 79 | break; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 80 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 81 | case JFFS2_NODETYPE_XATTR: |
| 82 | s->sum_size += JFFS2_SUMMARY_XATTR_SIZE; |
| 83 | s->sum_num++; |
| 84 | dbg_summary("xattr (xid=%u, version=%u) added to summary\n", |
| 85 | je32_to_cpu(item->x.xid), je32_to_cpu(item->x.version)); |
| 86 | break; |
| 87 | case JFFS2_NODETYPE_XREF: |
| 88 | s->sum_size += JFFS2_SUMMARY_XREF_SIZE; |
| 89 | s->sum_num++; |
| 90 | dbg_summary("xref added to summary\n"); |
| 91 | break; |
| 92 | #endif |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 93 | default: |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 94 | JFFS2_WARNING("UNKNOWN node type %u\n", |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 95 | je16_to_cpu(item->u.nodetype)); |
| 96 | return 1; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* The following 3 functions are called from scan.c to collect summary info for not closed jeb */ |
| 103 | |
| 104 | int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size) |
| 105 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 106 | dbg_summary("called with %u\n", size); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 107 | s->sum_padded += size; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int jffs2_sum_add_inode_mem(struct jffs2_summary *s, struct jffs2_raw_inode *ri, |
| 112 | uint32_t ofs) |
| 113 | { |
| 114 | struct jffs2_sum_inode_mem *temp = kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL); |
| 115 | |
| 116 | if (!temp) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | temp->nodetype = ri->nodetype; |
| 120 | temp->inode = ri->ino; |
| 121 | temp->version = ri->version; |
| 122 | temp->offset = cpu_to_je32(ofs); /* relative offset from the begining of the jeb */ |
| 123 | temp->totlen = ri->totlen; |
| 124 | temp->next = NULL; |
| 125 | |
| 126 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); |
| 127 | } |
| 128 | |
| 129 | int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *rd, |
| 130 | uint32_t ofs) |
| 131 | { |
| 132 | struct jffs2_sum_dirent_mem *temp = |
| 133 | kmalloc(sizeof(struct jffs2_sum_dirent_mem) + rd->nsize, GFP_KERNEL); |
| 134 | |
| 135 | if (!temp) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | temp->nodetype = rd->nodetype; |
| 139 | temp->totlen = rd->totlen; |
| 140 | temp->offset = cpu_to_je32(ofs); /* relative from the begining of the jeb */ |
| 141 | temp->pino = rd->pino; |
| 142 | temp->version = rd->version; |
| 143 | temp->ino = rd->ino; |
| 144 | temp->nsize = rd->nsize; |
| 145 | temp->type = rd->type; |
| 146 | temp->next = NULL; |
| 147 | |
| 148 | memcpy(temp->name, rd->name, rd->nsize); |
| 149 | |
| 150 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); |
| 151 | } |
| 152 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 153 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 154 | int jffs2_sum_add_xattr_mem(struct jffs2_summary *s, struct jffs2_raw_xattr *rx, uint32_t ofs) |
| 155 | { |
| 156 | struct jffs2_sum_xattr_mem *temp; |
| 157 | |
| 158 | temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL); |
| 159 | if (!temp) |
| 160 | return -ENOMEM; |
| 161 | |
| 162 | temp->nodetype = rx->nodetype; |
| 163 | temp->xid = rx->xid; |
| 164 | temp->version = rx->version; |
| 165 | temp->offset = cpu_to_je32(ofs); |
| 166 | temp->totlen = rx->totlen; |
| 167 | temp->next = NULL; |
| 168 | |
| 169 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); |
| 170 | } |
| 171 | |
| 172 | int jffs2_sum_add_xref_mem(struct jffs2_summary *s, struct jffs2_raw_xref *rr, uint32_t ofs) |
| 173 | { |
| 174 | struct jffs2_sum_xref_mem *temp; |
| 175 | |
| 176 | temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL); |
| 177 | if (!temp) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | temp->nodetype = rr->nodetype; |
| 181 | temp->offset = cpu_to_je32(ofs); |
| 182 | temp->next = NULL; |
| 183 | |
| 184 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); |
| 185 | } |
| 186 | #endif |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 187 | /* Cleanup every collected summary information */ |
| 188 | |
| 189 | static void jffs2_sum_clean_collected(struct jffs2_summary *s) |
| 190 | { |
| 191 | union jffs2_sum_mem *temp; |
| 192 | |
| 193 | if (!s->sum_list_head) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 194 | dbg_summary("already empty\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 195 | } |
| 196 | while (s->sum_list_head) { |
| 197 | temp = s->sum_list_head; |
| 198 | s->sum_list_head = s->sum_list_head->u.next; |
| 199 | kfree(temp); |
| 200 | } |
| 201 | s->sum_list_tail = NULL; |
| 202 | s->sum_padded = 0; |
| 203 | s->sum_num = 0; |
| 204 | } |
| 205 | |
| 206 | void jffs2_sum_reset_collected(struct jffs2_summary *s) |
| 207 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 208 | dbg_summary("called\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 209 | jffs2_sum_clean_collected(s); |
| 210 | s->sum_size = 0; |
| 211 | } |
| 212 | |
| 213 | void jffs2_sum_disable_collecting(struct jffs2_summary *s) |
| 214 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 215 | dbg_summary("called\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 216 | jffs2_sum_clean_collected(s); |
| 217 | s->sum_size = JFFS2_SUMMARY_NOSUM_SIZE; |
| 218 | } |
| 219 | |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 220 | int jffs2_sum_is_disabled(struct jffs2_summary *s) |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 221 | { |
| 222 | return (s->sum_size == JFFS2_SUMMARY_NOSUM_SIZE); |
| 223 | } |
| 224 | |
| 225 | /* Move the collected summary information into sb (called from scan.c) */ |
| 226 | |
| 227 | void jffs2_sum_move_collected(struct jffs2_sb_info *c, struct jffs2_summary *s) |
| 228 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 229 | dbg_summary("oldsize=0x%x oldnum=%u => newsize=0x%x newnum=%u\n", |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 230 | c->summary->sum_size, c->summary->sum_num, |
| 231 | s->sum_size, s->sum_num); |
| 232 | |
| 233 | c->summary->sum_size = s->sum_size; |
| 234 | c->summary->sum_num = s->sum_num; |
| 235 | c->summary->sum_padded = s->sum_padded; |
| 236 | c->summary->sum_list_head = s->sum_list_head; |
| 237 | c->summary->sum_list_tail = s->sum_list_tail; |
| 238 | |
| 239 | s->sum_list_head = s->sum_list_tail = NULL; |
| 240 | } |
| 241 | |
| 242 | /* Called from wbuf.c to collect writed node info */ |
| 243 | |
| 244 | int jffs2_sum_add_kvec(struct jffs2_sb_info *c, const struct kvec *invecs, |
| 245 | unsigned long count, uint32_t ofs) |
| 246 | { |
| 247 | union jffs2_node_union *node; |
| 248 | struct jffs2_eraseblock *jeb; |
| 249 | |
Zoltan Sogor | 27bea32 | 2006-09-16 12:15:59 -0700 | [diff] [blame] | 250 | if (c->summary->sum_size == JFFS2_SUMMARY_NOSUM_SIZE) { |
| 251 | dbg_summary("Summary is disabled for this jeb! Skipping summary info!\n"); |
| 252 | return 0; |
| 253 | } |
| 254 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 255 | node = invecs[0].iov_base; |
| 256 | jeb = &c->blocks[ofs / c->sector_size]; |
| 257 | ofs -= jeb->offset; |
| 258 | |
| 259 | switch (je16_to_cpu(node->u.nodetype)) { |
| 260 | case JFFS2_NODETYPE_INODE: { |
| 261 | struct jffs2_sum_inode_mem *temp = |
| 262 | kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL); |
| 263 | |
| 264 | if (!temp) |
| 265 | goto no_mem; |
| 266 | |
| 267 | temp->nodetype = node->i.nodetype; |
| 268 | temp->inode = node->i.ino; |
| 269 | temp->version = node->i.version; |
| 270 | temp->offset = cpu_to_je32(ofs); |
| 271 | temp->totlen = node->i.totlen; |
| 272 | temp->next = NULL; |
| 273 | |
| 274 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); |
| 275 | } |
| 276 | |
| 277 | case JFFS2_NODETYPE_DIRENT: { |
| 278 | struct jffs2_sum_dirent_mem *temp = |
| 279 | kmalloc(sizeof(struct jffs2_sum_dirent_mem) + node->d.nsize, GFP_KERNEL); |
| 280 | |
| 281 | if (!temp) |
| 282 | goto no_mem; |
| 283 | |
| 284 | temp->nodetype = node->d.nodetype; |
| 285 | temp->totlen = node->d.totlen; |
| 286 | temp->offset = cpu_to_je32(ofs); |
| 287 | temp->pino = node->d.pino; |
| 288 | temp->version = node->d.version; |
| 289 | temp->ino = node->d.ino; |
| 290 | temp->nsize = node->d.nsize; |
| 291 | temp->type = node->d.type; |
| 292 | temp->next = NULL; |
| 293 | |
| 294 | switch (count) { |
| 295 | case 1: |
| 296 | memcpy(temp->name,node->d.name,node->d.nsize); |
| 297 | break; |
| 298 | |
| 299 | case 2: |
| 300 | memcpy(temp->name,invecs[1].iov_base,node->d.nsize); |
| 301 | break; |
| 302 | |
| 303 | default: |
| 304 | BUG(); /* impossible count value */ |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); |
| 309 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 310 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 311 | case JFFS2_NODETYPE_XATTR: { |
| 312 | struct jffs2_sum_xattr_mem *temp; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 313 | temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL); |
| 314 | if (!temp) |
| 315 | goto no_mem; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 316 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 317 | temp->nodetype = node->x.nodetype; |
| 318 | temp->xid = node->x.xid; |
| 319 | temp->version = node->x.version; |
| 320 | temp->totlen = node->x.totlen; |
| 321 | temp->offset = cpu_to_je32(ofs); |
| 322 | temp->next = NULL; |
| 323 | |
| 324 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); |
| 325 | } |
| 326 | case JFFS2_NODETYPE_XREF: { |
| 327 | struct jffs2_sum_xref_mem *temp; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 328 | temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL); |
| 329 | if (!temp) |
| 330 | goto no_mem; |
| 331 | temp->nodetype = node->r.nodetype; |
| 332 | temp->offset = cpu_to_je32(ofs); |
| 333 | temp->next = NULL; |
| 334 | |
| 335 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); |
| 336 | } |
| 337 | #endif |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 338 | case JFFS2_NODETYPE_PADDING: |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 339 | dbg_summary("node PADDING\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 340 | c->summary->sum_padded += je32_to_cpu(node->u.totlen); |
| 341 | break; |
| 342 | |
| 343 | case JFFS2_NODETYPE_CLEANMARKER: |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 344 | dbg_summary("node CLEANMARKER\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 345 | break; |
| 346 | |
| 347 | case JFFS2_NODETYPE_SUMMARY: |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 348 | dbg_summary("node SUMMARY\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | default: |
| 352 | /* If you implement a new node type you should also implement |
| 353 | summary support for it or disable summary. |
| 354 | */ |
| 355 | BUG(); |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | |
| 361 | no_mem: |
| 362 | JFFS2_WARNING("MEMORY ALLOCATION ERROR!"); |
| 363 | return -ENOMEM; |
| 364 | } |
| 365 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 366 | static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c, |
| 367 | struct jffs2_eraseblock *jeb, |
| 368 | uint32_t ofs, uint32_t len, |
| 369 | struct jffs2_inode_cache *ic) |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 370 | { |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 371 | /* If there was a gap, mark it dirty */ |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 372 | if ((ofs & ~3) > c->sector_size - jeb->free_size) { |
| 373 | /* Ew. Summary doesn't actually tell us explicitly about dirty space */ |
| 374 | jffs2_scan_dirty_space(c, jeb, (ofs & ~3) - (c->sector_size - jeb->free_size)); |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 375 | } |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 376 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 377 | return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic); |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 378 | } |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 379 | |
| 380 | /* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */ |
| 381 | |
| 382 | static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
Ferenc Havasi | 2bc9764 | 2005-09-26 12:37:25 +0100 | [diff] [blame] | 383 | struct jffs2_raw_summary *summary, uint32_t *pseudo_random) |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 384 | { |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 385 | struct jffs2_inode_cache *ic; |
| 386 | struct jffs2_full_dirent *fd; |
| 387 | void *sp; |
| 388 | int i, ino; |
David Woodhouse | 6827099 | 2006-05-21 03:46:05 +0100 | [diff] [blame] | 389 | int err; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 390 | |
| 391 | sp = summary->sum; |
| 392 | |
| 393 | for (i=0; i<je32_to_cpu(summary->sum_num); i++) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 394 | dbg_summary("processing summary index %d\n", i); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 395 | |
Artem Bityutskiy | a2166b9 | 2006-12-28 12:01:41 +0200 | [diff] [blame] | 396 | cond_resched(); |
| 397 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 398 | /* Make sure there's a spare ref for dirty space */ |
David Woodhouse | 046b8b9 | 2006-05-25 01:50:35 +0100 | [diff] [blame] | 399 | err = jffs2_prealloc_raw_node_refs(c, jeb, 2); |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 400 | if (err) |
| 401 | return err; |
| 402 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 403 | switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) { |
| 404 | case JFFS2_NODETYPE_INODE: { |
| 405 | struct jffs2_sum_inode_flash *spi; |
| 406 | spi = sp; |
| 407 | |
| 408 | ino = je32_to_cpu(spi->inode); |
| 409 | |
David Woodhouse | 9167e0f | 2006-05-21 13:13:45 +0100 | [diff] [blame] | 410 | dbg_summary("Inode at 0x%08x-0x%08x\n", |
| 411 | jeb->offset + je32_to_cpu(spi->offset), |
David Woodhouse | 8b9e9fe | 2006-05-25 01:53:09 +0100 | [diff] [blame] | 412 | jeb->offset + je32_to_cpu(spi->offset) + je32_to_cpu(spi->totlen)); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 413 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 414 | ic = jffs2_scan_make_ino_cache(c, ino); |
| 415 | if (!ic) { |
| 416 | JFFS2_NOTICE("scan_make_ino_cache failed\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 417 | return -ENOMEM; |
| 418 | } |
| 419 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 420 | sum_link_node_ref(c, jeb, je32_to_cpu(spi->offset) | REF_UNCHECKED, |
| 421 | PAD(je32_to_cpu(spi->totlen)), ic); |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 422 | |
| 423 | *pseudo_random += je32_to_cpu(spi->version); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 424 | |
| 425 | sp += JFFS2_SUMMARY_INODE_SIZE; |
| 426 | |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | case JFFS2_NODETYPE_DIRENT: { |
| 431 | struct jffs2_sum_dirent_flash *spd; |
| 432 | spd = sp; |
| 433 | |
David Woodhouse | 8b9e9fe | 2006-05-25 01:53:09 +0100 | [diff] [blame] | 434 | dbg_summary("Dirent at 0x%08x-0x%08x\n", |
David Woodhouse | 9167e0f | 2006-05-21 13:13:45 +0100 | [diff] [blame] | 435 | jeb->offset + je32_to_cpu(spd->offset), |
| 436 | jeb->offset + je32_to_cpu(spd->offset) + je32_to_cpu(spd->totlen)); |
| 437 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 438 | |
| 439 | fd = jffs2_alloc_full_dirent(spd->nsize+1); |
David Woodhouse | 9641b78 | 2006-05-20 16:13:34 +0100 | [diff] [blame] | 440 | if (!fd) |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 441 | return -ENOMEM; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 442 | |
| 443 | memcpy(&fd->name, spd->name, spd->nsize); |
| 444 | fd->name[spd->nsize] = 0; |
| 445 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 446 | ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino)); |
| 447 | if (!ic) { |
| 448 | jffs2_free_full_dirent(fd); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 449 | return -ENOMEM; |
| 450 | } |
| 451 | |
David Woodhouse | 1046d88 | 2006-06-18 22:44:21 +0100 | [diff] [blame] | 452 | fd->raw = sum_link_node_ref(c, jeb, je32_to_cpu(spd->offset) | REF_UNCHECKED, |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 453 | PAD(je32_to_cpu(spd->totlen)), ic); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 454 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 455 | fd->next = NULL; |
| 456 | fd->version = je32_to_cpu(spd->version); |
| 457 | fd->ino = je32_to_cpu(spd->ino); |
| 458 | fd->nhash = full_name_hash(fd->name, spd->nsize); |
| 459 | fd->type = spd->type; |
David Woodhouse | f1f9671 | 2006-05-20 19:45:26 +0100 | [diff] [blame] | 460 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 461 | jffs2_add_fd_to_list(c, fd, &ic->scan_dents); |
| 462 | |
| 463 | *pseudo_random += je32_to_cpu(spd->version); |
| 464 | |
| 465 | sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize); |
| 466 | |
| 467 | break; |
| 468 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 469 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 470 | case JFFS2_NODETYPE_XATTR: { |
| 471 | struct jffs2_xattr_datum *xd; |
| 472 | struct jffs2_sum_xattr_flash *spx; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 473 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 474 | spx = (struct jffs2_sum_xattr_flash *)sp; |
David Woodhouse | 9167e0f | 2006-05-21 13:13:45 +0100 | [diff] [blame] | 475 | dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n", |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 476 | jeb->offset + je32_to_cpu(spx->offset), |
David Woodhouse | 9167e0f | 2006-05-21 13:13:45 +0100 | [diff] [blame] | 477 | jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen), |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 478 | je32_to_cpu(spx->xid), je32_to_cpu(spx->version)); |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 479 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 480 | xd = jffs2_setup_xattr_datum(c, je32_to_cpu(spx->xid), |
| 481 | je32_to_cpu(spx->version)); |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 482 | if (IS_ERR(xd)) |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 483 | return PTR_ERR(xd); |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 484 | if (xd->version > je32_to_cpu(spx->version)) { |
| 485 | /* node is not the newest one */ |
| 486 | struct jffs2_raw_node_ref *raw |
| 487 | = sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED, |
| 488 | PAD(je32_to_cpu(spx->totlen)), NULL); |
| 489 | raw->next_in_ino = xd->node->next_in_ino; |
| 490 | xd->node->next_in_ino = raw; |
| 491 | } else { |
| 492 | xd->version = je32_to_cpu(spx->version); |
| 493 | sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED, |
| 494 | PAD(je32_to_cpu(spx->totlen)), (void *)xd); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 495 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 496 | *pseudo_random += je32_to_cpu(spx->xid); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 497 | sp += JFFS2_SUMMARY_XATTR_SIZE; |
| 498 | |
| 499 | break; |
| 500 | } |
| 501 | case JFFS2_NODETYPE_XREF: { |
| 502 | struct jffs2_xattr_ref *ref; |
| 503 | struct jffs2_sum_xref_flash *spr; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 504 | |
| 505 | spr = (struct jffs2_sum_xref_flash *)sp; |
David Woodhouse | 9167e0f | 2006-05-21 13:13:45 +0100 | [diff] [blame] | 506 | dbg_summary("xref at %#08x-%#08x\n", |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 507 | jeb->offset + je32_to_cpu(spr->offset), |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 508 | jeb->offset + je32_to_cpu(spr->offset) + |
| 509 | (uint32_t)PAD(sizeof(struct jffs2_raw_xref))); |
David Woodhouse | 9167e0f | 2006-05-21 13:13:45 +0100 | [diff] [blame] | 510 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 511 | ref = jffs2_alloc_xattr_ref(); |
| 512 | if (!ref) { |
| 513 | JFFS2_NOTICE("allocation of xattr_datum failed\n"); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 514 | return -ENOMEM; |
| 515 | } |
KaiGai Kohei | 8f2b6f4 | 2006-05-13 15:15:07 +0900 | [diff] [blame] | 516 | ref->next = c->xref_temp; |
| 517 | c->xref_temp = ref; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 518 | |
KaiGai Kohei | c9f700f | 2006-06-11 10:35:15 +0900 | [diff] [blame] | 519 | sum_link_node_ref(c, jeb, je32_to_cpu(spr->offset) | REF_UNCHECKED, |
| 520 | PAD(sizeof(struct jffs2_raw_xref)), (void *)ref); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 521 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 522 | *pseudo_random += ref->node->flash_offset; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 523 | sp += JFFS2_SUMMARY_XREF_SIZE; |
| 524 | |
| 525 | break; |
| 526 | } |
| 527 | #endif |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 528 | default : { |
David Woodhouse | 7807ef7 | 2006-05-21 03:45:27 +0100 | [diff] [blame] | 529 | uint16_t nodetype = je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype); |
| 530 | JFFS2_WARNING("Unsupported node type %x found in summary! Exiting...\n", nodetype); |
| 531 | if ((nodetype & JFFS2_COMPAT_MASK) == JFFS2_FEATURE_INCOMPAT) |
| 532 | return -EIO; |
| 533 | |
| 534 | /* For compatible node types, just fall back to the full scan */ |
| 535 | c->wasted_size -= jeb->wasted_size; |
| 536 | c->free_size += c->sector_size - jeb->free_size; |
| 537 | c->used_size -= jeb->used_size; |
| 538 | c->dirty_size -= jeb->dirty_size; |
| 539 | jeb->wasted_size = jeb->used_size = jeb->dirty_size = 0; |
| 540 | jeb->free_size = c->sector_size; |
| 541 | |
David Woodhouse | c38c1b6 | 2006-05-25 01:38:27 +0100 | [diff] [blame] | 542 | jffs2_free_jeb_node_refs(c, jeb); |
David Woodhouse | 7807ef7 | 2006-05-21 03:45:27 +0100 | [diff] [blame] | 543 | return -ENOTRECOVERABLE; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* Process the summary node - called from jffs2_scan_eraseblock() */ |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 551 | int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
David Woodhouse | 9641b78 | 2006-05-20 16:13:34 +0100 | [diff] [blame] | 552 | struct jffs2_raw_summary *summary, uint32_t sumsize, |
| 553 | uint32_t *pseudo_random) |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 554 | { |
| 555 | struct jffs2_unknown_node crcnode; |
David Woodhouse | 9641b78 | 2006-05-20 16:13:34 +0100 | [diff] [blame] | 556 | int ret, ofs; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 557 | uint32_t crc; |
| 558 | |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 559 | ofs = c->sector_size - sumsize; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 560 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 561 | dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n", |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 562 | jeb->offset, jeb->offset + ofs, sumsize); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 563 | |
| 564 | /* OK, now check for node validity and CRC */ |
| 565 | crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 566 | crcnode.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY); |
| 567 | crcnode.totlen = summary->totlen; |
| 568 | crc = crc32(0, &crcnode, sizeof(crcnode)-4); |
| 569 | |
| 570 | if (je32_to_cpu(summary->hdr_crc) != crc) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 571 | dbg_summary("Summary node header is corrupt (bad CRC or " |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 572 | "no summary at all)\n"); |
| 573 | goto crc_err; |
| 574 | } |
| 575 | |
| 576 | if (je32_to_cpu(summary->totlen) != sumsize) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 577 | dbg_summary("Summary node is corrupt (wrong erasesize?)\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 578 | goto crc_err; |
| 579 | } |
| 580 | |
Ferenc Havasi | 2bc9764 | 2005-09-26 12:37:25 +0100 | [diff] [blame] | 581 | crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 582 | |
| 583 | if (je32_to_cpu(summary->node_crc) != crc) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 584 | dbg_summary("Summary node is corrupt (bad CRC)\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 585 | goto crc_err; |
| 586 | } |
| 587 | |
Ferenc Havasi | 2bc9764 | 2005-09-26 12:37:25 +0100 | [diff] [blame] | 588 | crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary)); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 589 | |
| 590 | if (je32_to_cpu(summary->sum_crc) != crc) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 591 | dbg_summary("Summary node data is corrupt (bad CRC)\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 592 | goto crc_err; |
| 593 | } |
| 594 | |
| 595 | if ( je32_to_cpu(summary->cln_mkr) ) { |
| 596 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 597 | dbg_summary("Summary : CLEANMARKER node \n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 598 | |
David Woodhouse | 098a198 | 2006-05-30 09:00:14 +0100 | [diff] [blame] | 599 | ret = jffs2_prealloc_raw_node_refs(c, jeb, 1); |
| 600 | if (ret) |
| 601 | return ret; |
| 602 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 603 | if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 604 | dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n", |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 605 | je32_to_cpu(summary->cln_mkr), c->cleanmarker_size); |
David Woodhouse | 098a198 | 2006-05-30 09:00:14 +0100 | [diff] [blame] | 606 | if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr))))) |
| 607 | return ret; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 608 | } else if (jeb->first_node) { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 609 | dbg_summary("CLEANMARKER node not first node in block " |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 610 | "(0x%08x)\n", jeb->offset); |
David Woodhouse | 098a198 | 2006-05-30 09:00:14 +0100 | [diff] [blame] | 611 | if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr))))) |
| 612 | return ret; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 613 | } else { |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 614 | jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, |
| 615 | je32_to_cpu(summary->cln_mkr), NULL); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 619 | ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random); |
David Woodhouse | 7807ef7 | 2006-05-21 03:45:27 +0100 | [diff] [blame] | 620 | /* -ENOTRECOVERABLE isn't a fatal error -- it means we should do a full |
| 621 | scan of this eraseblock. So return zero */ |
| 622 | if (ret == -ENOTRECOVERABLE) |
| 623 | return 0; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 624 | if (ret) |
David Woodhouse | 7807ef7 | 2006-05-21 03:45:27 +0100 | [diff] [blame] | 625 | return ret; /* real error */ |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 626 | |
| 627 | /* for PARANOIA_CHECK */ |
David Woodhouse | 046b8b9 | 2006-05-25 01:50:35 +0100 | [diff] [blame] | 628 | ret = jffs2_prealloc_raw_node_refs(c, jeb, 2); |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 629 | if (ret) |
| 630 | return ret; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 631 | |
David Woodhouse | f61579c3 | 2006-05-25 01:42:40 +0100 | [diff] [blame] | 632 | sum_link_node_ref(c, jeb, ofs | REF_NORMAL, sumsize, NULL); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 633 | |
David Woodhouse | 49f11d4 | 2006-05-21 04:00:01 +0100 | [diff] [blame] | 634 | if (unlikely(jeb->free_size)) { |
| 635 | JFFS2_WARNING("Free size 0x%x bytes in eraseblock @0x%08x with summary?\n", |
| 636 | jeb->free_size, jeb->offset); |
| 637 | jeb->wasted_size += jeb->free_size; |
| 638 | c->wasted_size += jeb->free_size; |
| 639 | c->free_size -= jeb->free_size; |
| 640 | jeb->free_size = 0; |
| 641 | } |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 642 | |
| 643 | return jffs2_scan_classify_jeb(c, jeb); |
| 644 | |
| 645 | crc_err: |
| 646 | JFFS2_WARNING("Summary node crc error, skipping summary information.\n"); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* Write summary data to flash - helper function for jffs2_sum_write_sumnode() */ |
| 652 | |
| 653 | static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
| 654 | uint32_t infosize, uint32_t datasize, int padsize) |
| 655 | { |
Ferenc Havasi | 2bc9764 | 2005-09-26 12:37:25 +0100 | [diff] [blame] | 656 | struct jffs2_raw_summary isum; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 657 | union jffs2_sum_mem *temp; |
| 658 | struct jffs2_sum_marker *sm; |
| 659 | struct kvec vecs[2]; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 660 | uint32_t sum_ofs; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 661 | void *wpage; |
| 662 | int ret; |
| 663 | size_t retlen; |
| 664 | |
| 665 | memset(c->summary->sum_buf, 0xff, datasize); |
| 666 | memset(&isum, 0, sizeof(isum)); |
| 667 | |
| 668 | isum.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 669 | isum.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY); |
| 670 | isum.totlen = cpu_to_je32(infosize); |
| 671 | isum.hdr_crc = cpu_to_je32(crc32(0, &isum, sizeof(struct jffs2_unknown_node) - 4)); |
| 672 | isum.padded = cpu_to_je32(c->summary->sum_padded); |
| 673 | isum.cln_mkr = cpu_to_je32(c->cleanmarker_size); |
| 674 | isum.sum_num = cpu_to_je32(c->summary->sum_num); |
| 675 | wpage = c->summary->sum_buf; |
| 676 | |
| 677 | while (c->summary->sum_num) { |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 678 | temp = c->summary->sum_list_head; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 679 | |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 680 | switch (je16_to_cpu(temp->u.nodetype)) { |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 681 | case JFFS2_NODETYPE_INODE: { |
| 682 | struct jffs2_sum_inode_flash *sino_ptr = wpage; |
| 683 | |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 684 | sino_ptr->nodetype = temp->i.nodetype; |
| 685 | sino_ptr->inode = temp->i.inode; |
| 686 | sino_ptr->version = temp->i.version; |
| 687 | sino_ptr->offset = temp->i.offset; |
| 688 | sino_ptr->totlen = temp->i.totlen; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 689 | |
| 690 | wpage += JFFS2_SUMMARY_INODE_SIZE; |
| 691 | |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | case JFFS2_NODETYPE_DIRENT: { |
| 696 | struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage; |
| 697 | |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 698 | sdrnt_ptr->nodetype = temp->d.nodetype; |
| 699 | sdrnt_ptr->totlen = temp->d.totlen; |
| 700 | sdrnt_ptr->offset = temp->d.offset; |
| 701 | sdrnt_ptr->pino = temp->d.pino; |
| 702 | sdrnt_ptr->version = temp->d.version; |
| 703 | sdrnt_ptr->ino = temp->d.ino; |
| 704 | sdrnt_ptr->nsize = temp->d.nsize; |
| 705 | sdrnt_ptr->type = temp->d.type; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 706 | |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 707 | memcpy(sdrnt_ptr->name, temp->d.name, |
| 708 | temp->d.nsize); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 709 | |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 710 | wpage += JFFS2_SUMMARY_DIRENT_SIZE(temp->d.nsize); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 711 | |
| 712 | break; |
| 713 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 714 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 715 | case JFFS2_NODETYPE_XATTR: { |
| 716 | struct jffs2_sum_xattr_flash *sxattr_ptr = wpage; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 717 | |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 718 | temp = c->summary->sum_list_head; |
| 719 | sxattr_ptr->nodetype = temp->x.nodetype; |
| 720 | sxattr_ptr->xid = temp->x.xid; |
| 721 | sxattr_ptr->version = temp->x.version; |
| 722 | sxattr_ptr->offset = temp->x.offset; |
| 723 | sxattr_ptr->totlen = temp->x.totlen; |
| 724 | |
| 725 | wpage += JFFS2_SUMMARY_XATTR_SIZE; |
| 726 | break; |
| 727 | } |
| 728 | case JFFS2_NODETYPE_XREF: { |
| 729 | struct jffs2_sum_xref_flash *sxref_ptr = wpage; |
| 730 | |
| 731 | temp = c->summary->sum_list_head; |
| 732 | sxref_ptr->nodetype = temp->r.nodetype; |
| 733 | sxref_ptr->offset = temp->r.offset; |
| 734 | |
| 735 | wpage += JFFS2_SUMMARY_XREF_SIZE; |
| 736 | break; |
| 737 | } |
| 738 | #endif |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 739 | default : { |
David Woodhouse | 6171586 | 2006-05-21 00:02:06 +0100 | [diff] [blame] | 740 | if ((je16_to_cpu(temp->u.nodetype) & JFFS2_COMPAT_MASK) |
| 741 | == JFFS2_FEATURE_RWCOMPAT_COPY) { |
| 742 | dbg_summary("Writing unknown RWCOMPAT_COPY node type %x\n", |
| 743 | je16_to_cpu(temp->u.nodetype)); |
| 744 | jffs2_sum_disable_collecting(c->summary); |
| 745 | } else { |
| 746 | BUG(); /* unknown node in summary information */ |
| 747 | } |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Jesper Juhl | 20ffdcb | 2006-05-12 11:55:51 +0100 | [diff] [blame] | 751 | c->summary->sum_list_head = temp->u.next; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 752 | kfree(temp); |
| 753 | |
| 754 | c->summary->sum_num--; |
| 755 | } |
| 756 | |
| 757 | jffs2_sum_reset_collected(c->summary); |
| 758 | |
| 759 | wpage += padsize; |
| 760 | |
| 761 | sm = wpage; |
| 762 | sm->offset = cpu_to_je32(c->sector_size - jeb->free_size); |
| 763 | sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC); |
| 764 | |
| 765 | isum.sum_crc = cpu_to_je32(crc32(0, c->summary->sum_buf, datasize)); |
| 766 | isum.node_crc = cpu_to_je32(crc32(0, &isum, sizeof(isum) - 8)); |
| 767 | |
| 768 | vecs[0].iov_base = &isum; |
| 769 | vecs[0].iov_len = sizeof(isum); |
| 770 | vecs[1].iov_base = c->summary->sum_buf; |
| 771 | vecs[1].iov_len = datasize; |
| 772 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 773 | sum_ofs = jeb->offset + c->sector_size - jeb->free_size; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 774 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 775 | dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n", |
| 776 | sum_ofs); |
| 777 | |
| 778 | ret = jffs2_flash_writev(c, vecs, 2, sum_ofs, &retlen, 0); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 779 | |
| 780 | if (ret || (retlen != infosize)) { |
David Woodhouse | 010b06d | 2006-05-21 13:15:59 +0100 | [diff] [blame] | 781 | |
David Woodhouse | c41ff6e | 2006-05-16 17:05:33 +0100 | [diff] [blame] | 782 | JFFS2_WARNING("Write of %u bytes at 0x%08x failed. returned %d, retlen %zd\n", |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 783 | infosize, sum_ofs, ret, retlen); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 784 | |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 785 | if (retlen) { |
| 786 | /* Waste remaining space */ |
| 787 | spin_lock(&c->erase_completion_lock); |
| 788 | jffs2_link_node_ref(c, jeb, sum_ofs | REF_OBSOLETE, infosize, NULL); |
| 789 | spin_unlock(&c->erase_completion_lock); |
| 790 | } |
David Woodhouse | 010b06d | 2006-05-21 13:15:59 +0100 | [diff] [blame] | 791 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 792 | c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 793 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 794 | return 0; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 795 | } |
| 796 | |
David Woodhouse | 010b06d | 2006-05-21 13:15:59 +0100 | [diff] [blame] | 797 | spin_lock(&c->erase_completion_lock); |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 798 | jffs2_link_node_ref(c, jeb, sum_ofs | REF_NORMAL, infosize, NULL); |
| 799 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | 010b06d | 2006-05-21 13:15:59 +0100 | [diff] [blame] | 800 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /* Write out summary information - called from jffs2_do_reserve_space */ |
| 805 | |
| 806 | int jffs2_sum_write_sumnode(struct jffs2_sb_info *c) |
| 807 | { |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 808 | int datasize, infosize, padsize; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 809 | struct jffs2_eraseblock *jeb; |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 810 | int ret; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 811 | |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 812 | dbg_summary("called\n"); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 813 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 814 | spin_unlock(&c->erase_completion_lock); |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 815 | |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 816 | jeb = c->nextblock; |
David Woodhouse | 046b8b9 | 2006-05-25 01:50:35 +0100 | [diff] [blame] | 817 | jffs2_prealloc_raw_node_refs(c, jeb, 1); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 818 | |
| 819 | if (!c->summary->sum_num || !c->summary->sum_list_head) { |
| 820 | JFFS2_WARNING("Empty summary info!!!\n"); |
| 821 | BUG(); |
| 822 | } |
| 823 | |
| 824 | datasize = c->summary->sum_size + sizeof(struct jffs2_sum_marker); |
Ferenc Havasi | 2bc9764 | 2005-09-26 12:37:25 +0100 | [diff] [blame] | 825 | infosize = sizeof(struct jffs2_raw_summary) + datasize; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 826 | padsize = jeb->free_size - infosize; |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 827 | infosize += padsize; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 828 | datasize += padsize; |
| 829 | |
| 830 | /* Is there enough space for summary? */ |
| 831 | if (padsize < 0) { |
| 832 | /* don't try to write out summary for this jeb */ |
| 833 | jffs2_sum_disable_collecting(c->summary); |
| 834 | |
| 835 | JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize); |
David Woodhouse | 9bfeb69 | 2006-05-26 21:19:05 +0100 | [diff] [blame] | 836 | spin_lock(&c->erase_completion_lock); |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize); |
David Woodhouse | 010b06d | 2006-05-21 13:15:59 +0100 | [diff] [blame] | 841 | spin_lock(&c->erase_completion_lock); |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 842 | return ret; |
Ferenc Havasi | e631ddb | 2005-09-07 09:35:26 +0100 | [diff] [blame] | 843 | } |