Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | /* * JFFS2 -- Journalling Flash File System, Version 2. * * Copyright (C) 2001, 2002 Red Hat, Inc. * * Created by David Woodhouse <dwmw2@cambridge.redhat.com> * * The original JFFS, from which the design for JFFS2 was derived, * was designed and implemented by Axis Communications AB. * * The contents of this file are subject to the Red Hat eCos Public * License Version 1.1 (the "Licence"); you may not use this file * except in compliance with the Licence. You may obtain a copy of * the Licence at http://www.redhat.com/ * * Software distributed under the Licence is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. * See the Licence for the specific language governing rights and * limitations under the Licence. * * The Original Code is JFFS2 - Journalling Flash File System, version 2 * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License version 2 (the "GPL"), in * which case the provisions of the GPL are applicable instead of the * above. If you wish to allow the use of your version of this file * only under the terms of the GPL and not to allow others to use your * version of this file under the RHEPL, indicate your decision by * deleting the provisions above and replace them with the notice and * other provisions required by the GPL. If you do not delete the * provisions above, a recipient may use your version of this file * under either the RHEPL or the GPL. * * $Id: write.c,v 1.52 2002/03/08 11:01:43 dwmw2 Exp $ * */ #include <linux/kernel.h> #include <linux/fs.h> #include <linux/crc32.h> #include <linux/slab.h> #include <linux/mtd/mtd.h> #include "nodelist.h" int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri) { struct jffs2_inode_cache *ic; ic = jffs2_alloc_inode_cache(); if (!ic) { return -ENOMEM; } memset(ic, 0, sizeof(*ic)); init_MUTEX_LOCKED(&f->sem); f->inocache = ic; f->inocache->nlink = 1; f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache; f->inocache->ino = ri->ino = ++c->highest_ino; D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", ri->ino)); jffs2_add_ino_cache(c, f->inocache); ri->magic = JFFS2_MAGIC_BITMASK; ri->nodetype = JFFS2_NODETYPE_INODE; ri->totlen = PAD(sizeof(*ri)); ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4); ri->mode = mode; f->highest_version = ri->version = 1; return 0; } static void writecheck(struct jffs2_sb_info *c, uint32_t ofs) { unsigned char buf[16]; size_t retlen; int ret, i; ret = jffs2_flash_read(c, ofs, 16, &retlen, buf); if (ret || (retlen != 16)) { D1(printk(KERN_DEBUG "read failed or short in writecheck(). ret %d, retlen %d\n", ret, retlen)); return; } ret = 0; for (i=0; i<16; i++) { if (buf[i] != 0xff) ret = 1; } if (ret) { printk(KERN_WARNING "ARGH. About to write node to 0x%08x on flash, but there's data already there:\n", ofs); printk(KERN_WARNING "0x%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", ofs, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]); } } /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it, write it to the flash, link it into the existing inode/fragment list */ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const unsigned char *data, uint32_t datalen, uint32_t flash_ofs, uint32_t *writelen) { struct jffs2_raw_node_ref *raw; struct jffs2_full_dnode *fn; size_t retlen; struct iovec vecs[2]; int ret; unsigned long cnt = 2; D1(if(ri->hdr_crc != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) { printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n"); BUG(); } ); vecs[0].iov_base = ri; vecs[0].iov_len = sizeof(*ri); vecs[1].iov_base = (unsigned char *)data; vecs[1].iov_len = datalen; writecheck(c, flash_ofs); if (ri->totlen != sizeof(*ri) + datalen) { printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08x) + datalen (0x%08x)\n", ri->totlen, sizeof(*ri), datalen); } raw = jffs2_alloc_raw_node_ref(); if (!raw) return ERR_PTR(-ENOMEM); fn = jffs2_alloc_full_dnode(); if (!fn) { jffs2_free_raw_node_ref(raw); return ERR_PTR(-ENOMEM); } raw->flash_offset = flash_ofs; raw->totlen = PAD(ri->totlen); raw->next_phys = NULL; fn->ofs = ri->offset; fn->size = ri->dsize; fn->frags = 0; fn->raw = raw; /* check number of valid vecs */ if (!datalen || !data) cnt = 1; ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen); if (ret || (retlen != sizeof(*ri) + datalen)) { printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %d\n", sizeof(*ri)+datalen, flash_ofs, ret, retlen); /* Mark the space as dirtied */ if (retlen) { /* Doesn't belong to any inode */ raw->next_in_ino = NULL; /* Don't change raw->size to match retlen. We may have written the node header already, and only the data will seem corrupted, in which case the scan would skip over any node we write before the original intended end of this node */ jffs2_add_physical_node_ref(c, raw, sizeof(*ri)+datalen, 1); jffs2_mark_node_obsolete(c, raw); } else { printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset); jffs2_free_raw_node_ref(raw); } /* Release the full_dnode which is now useless, and return */ jffs2_free_full_dnode(fn); if (writelen) *writelen = retlen; return ERR_PTR(ret?ret:-EIO); } /* Mark the space used */ jffs2_add_physical_node_ref(c, raw, retlen, 0); /* Link into per-inode list */ raw->next_in_ino = f->inocache->nodes; f->inocache->nodes = raw; D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n", flash_ofs, ri->dsize, ri->csize, ri->node_crc, ri->data_crc, ri->totlen)); if (writelen) *writelen = retlen; f->inocache->nodes = raw; return fn; } struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_dirent *rd, const unsigned char *name, uint32_t namelen, uint32_t flash_ofs, uint32_t *writelen) { struct jffs2_raw_node_ref *raw; struct jffs2_full_dirent *fd; size_t retlen; struct iovec vecs[2]; int ret; D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n", rd->pino, name, name, rd->ino, rd->name_crc)); writecheck(c, flash_ofs); D1(if(rd->hdr_crc != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) { printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n"); BUG(); } ); vecs[0].iov_base = rd; vecs[0].iov_len = sizeof(*rd); vecs[1].iov_base = (unsigned char *)name; vecs[1].iov_len = namelen; raw = jffs2_alloc_raw_node_ref(); if (!raw) return ERR_PTR(-ENOMEM); fd = jffs2_alloc_full_dirent(namelen+1); if (!fd) { jffs2_free_raw_node_ref(raw); return ERR_PTR(-ENOMEM); } raw->flash_offset = flash_ofs; raw->totlen = PAD(rd->totlen); raw->next_in_ino = f->inocache->nodes; f->inocache->nodes = raw; raw->next_phys = NULL; fd->version = rd->version; fd->ino = rd->ino; fd->nhash = full_name_hash(name, strlen(name)); fd->type = rd->type; memcpy(fd->name, name, namelen); fd->name[namelen]=0; fd->raw = raw; ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen); if (ret || (retlen != sizeof(*rd) + namelen)) { printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %d\n", sizeof(*rd)+namelen, flash_ofs, ret, retlen); /* Mark the space as dirtied */ if (retlen) { jffs2_add_physical_node_ref(c, raw, sizeof(*rd)+namelen, 1); jffs2_mark_node_obsolete(c, raw); } else { printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset); jffs2_free_raw_node_ref(raw); } /* Release the full_dnode which is now useless, and return */ jffs2_free_full_dirent(fd); if (writelen) *writelen = retlen; return ERR_PTR(ret?ret:-EIO); } /* Mark the space used */ jffs2_add_physical_node_ref(c, raw, retlen, 0); if (writelen) *writelen = retlen; f->inocache->nodes = raw; return fd; } /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that we don't have to go digging in struct inode or its equivalent. It should set: mode, uid, gid, (starting)isize, atime, ctime, mtime */ int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, unsigned char *buf, uint32_t offset, uint32_t writelen, uint32_t *retlen) { int ret = 0; uint32_t writtenlen = 0; D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n", f->inocache->ino, offset, writelen)); while(writelen) { struct jffs2_full_dnode *fn; unsigned char *comprbuf = NULL; unsigned char comprtype = JFFS2_COMPR_NONE; uint32_t phys_ofs, alloclen; uint32_t datalen, cdatalen; D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset)); ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL); if (ret) { D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret)); break; } down(&f->sem); datalen = writelen; cdatalen = min(alloclen - sizeof(*ri), writelen); comprbuf = kmalloc(cdatalen, GFP_KERNEL); if (comprbuf) { comprtype = jffs2_compress(buf, comprbuf, &datalen, &cdatalen); } if (comprtype == JFFS2_COMPR_NONE) { /* Either compression failed, or the allocation of comprbuf failed */ if (comprbuf) kfree(comprbuf); comprbuf = buf; datalen = cdatalen; } /* Now comprbuf points to the data to be written, be it compressed or not. comprtype holds the compression type, and comprtype == JFFS2_COMPR_NONE means that the comprbuf doesn't need to be kfree()d. */ ri->magic = JFFS2_MAGIC_BITMASK; ri->nodetype = JFFS2_NODETYPE_INODE; ri->totlen = sizeof(*ri) + cdatalen; ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4); ri->ino = f->inocache->ino; ri->version = ++f->highest_version; ri->isize = max(ri->isize, offset + datalen); ri->offset = offset; ri->csize = cdatalen; ri->dsize = datalen; ri->compr = comprtype; ri->node_crc = crc32(0, ri, sizeof(*ri)-8); ri->data_crc = crc32(0, comprbuf, cdatalen); fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, NULL); if (comprtype != JFFS2_COMPR_NONE) kfree(comprbuf); if (IS_ERR(fn)) { ret = PTR_ERR(fn); up(&f->sem); jffs2_complete_reservation(c); break; } ret = jffs2_add_full_dnode_to_inode(c, f, fn); if (f->metadata) { jffs2_mark_node_obsolete(c, f->metadata->raw); jffs2_free_full_dnode(f->metadata); f->metadata = NULL; } if (ret) { /* Eep */ D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret)); jffs2_mark_node_obsolete(c, fn->raw); jffs2_free_full_dnode(fn); up(&f->sem); jffs2_complete_reservation(c); break; } up(&f->sem); jffs2_complete_reservation(c); if (!datalen) { printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n"); ret = -EIO; break; } D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen)); writtenlen += datalen; offset += datalen; writelen -= datalen; buf += datalen; } *retlen = writtenlen; return ret; } int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const char *name, int namelen) { struct jffs2_raw_dirent *rd; struct jffs2_full_dnode *fn; struct jffs2_full_dirent *fd; uint32_t alloclen, phys_ofs; uint32_t writtenlen; int ret; /* Try to reserve enough space for both node and dirent. * Just the node will do for now, though */ ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL); D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen)); if (ret) return ret; ri->data_crc = 0; ri->node_crc = crc32(0, ri, sizeof(*ri)-8); fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, &writtenlen); D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n", ri->mode)); if (IS_ERR(fn)) { D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n")); /* Eeek. Wave bye bye */ up(&f->sem); jffs2_complete_reservation(c); return PTR_ERR(fn); } /* No data here. Only a metadata node, which will be obsoleted by the first data write */ f->metadata = fn; /* Work out where to put the dirent node now. */ writtenlen = PAD(writtenlen); phys_ofs += writtenlen; alloclen -= writtenlen; up(&f->sem); if (alloclen < sizeof(*rd)+namelen) { /* Not enough space left in this chunk. Get some more */ jffs2_complete_reservation(c); ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL); if (ret) { /* Eep. */ D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n")); return ret; } } rd = jffs2_alloc_raw_dirent(); if (!rd) { /* Argh. Now we treat it like a normal delete */ jffs2_complete_reservation(c); return -ENOMEM; } down(&dir_f->sem); rd->magic = JFFS2_MAGIC_BITMASK; rd->nodetype = JFFS2_NODETYPE_DIRENT; rd->totlen = sizeof(*rd) + namelen; rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4); rd->pino = dir_f->inocache->ino; rd->version = ++dir_f->highest_version; rd->ino = ri->ino; rd->mctime = ri->ctime; rd->nsize = namelen; rd->type = DT_REG; rd->node_crc = crc32(0, rd, sizeof(*rd)-8); rd->name_crc = crc32(0, name, namelen); fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, &writtenlen); jffs2_free_raw_dirent(rd); if (IS_ERR(fd)) { /* dirent failed to write. Delete the inode normally as if it were the final unlink() */ jffs2_complete_reservation(c); up(&dir_f->sem); return PTR_ERR(fd); } /* Link the fd into the inode's list, obsoleting an old one if necessary. */ jffs2_add_fd_to_list(c, fd, &dir_f->dents); jffs2_complete_reservation(c); up(&dir_f->sem); return 0; } int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, const char *name, int namelen, struct jffs2_inode_info *dead_f) { struct jffs2_raw_dirent *rd; struct jffs2_full_dirent *fd; uint32_t alloclen, phys_ofs; int ret; rd = jffs2_alloc_raw_dirent(); if (!rd) return -ENOMEM; ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_DELETION); if (ret) { jffs2_free_raw_dirent(rd); return ret; } down(&dir_f->sem); /* Build a deletion node */ rd->magic = JFFS2_MAGIC_BITMASK; rd->nodetype = JFFS2_NODETYPE_DIRENT; rd->totlen = sizeof(*rd) + namelen; rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4); rd->pino = dir_f->inocache->ino; rd->version = ++dir_f->highest_version; rd->ino = 0; rd->mctime = CURRENT_TIME; rd->nsize = namelen; rd->type = DT_UNKNOWN; rd->node_crc = crc32(0, rd, sizeof(*rd)-8); rd->name_crc = crc32(0, name, namelen); fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, NULL); jffs2_free_raw_dirent(rd); if (IS_ERR(fd)) { jffs2_complete_reservation(c); up(&dir_f->sem); return PTR_ERR(fd); } /* File it. This will mark the old one obsolete. */ jffs2_add_fd_to_list(c, fd, &dir_f->dents); jffs2_complete_reservation(c); up(&dir_f->sem); if (dead_f) { /* Null if this was a rename not a real unlink */ down(&dead_f->sem); while (dead_f->dents) { /* There can be only deleted ones */ fd = dead_f->dents; dead_f->dents = fd->next; if (fd->ino) { printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n", dead_f->inocache->ino, fd->name, fd->ino); } else { D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n", fd->name, dead_f->inocache->ino)); } jffs2_mark_node_obsolete(c, fd->raw); jffs2_free_full_dirent(fd); } dead_f->inocache->nlink--; /* NB: Caller must set inode nlink if appropriate */ up(&dead_f->sem); } return 0; } int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino, uint8_t type, const char *name, int namelen) { struct jffs2_raw_dirent *rd; struct jffs2_full_dirent *fd; uint32_t alloclen, phys_ofs; int ret; rd = jffs2_alloc_raw_dirent(); if (!rd) return -ENOMEM; ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL); if (ret) { jffs2_free_raw_dirent(rd); return ret; } down(&dir_f->sem); /* Build a deletion node */ rd->magic = JFFS2_MAGIC_BITMASK; rd->nodetype = JFFS2_NODETYPE_DIRENT; rd->totlen = sizeof(*rd) + namelen; rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4); rd->pino = dir_f->inocache->ino; rd->version = ++dir_f->highest_version; rd->ino = ino; rd->mctime = CURRENT_TIME; rd->nsize = namelen; rd->type = type; rd->node_crc = crc32(0, rd, sizeof(*rd)-8); rd->name_crc = crc32(0, name, namelen); fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, NULL); jffs2_free_raw_dirent(rd); if (IS_ERR(fd)) { jffs2_complete_reservation(c); up(&dir_f->sem); return PTR_ERR(fd); } /* File it. This will mark the old one obsolete. */ jffs2_add_fd_to_list(c, fd, &dir_f->dents); jffs2_complete_reservation(c); up(&dir_f->sem); return 0; } |