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