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