1 /* 2 * JFFS2 -- Journalling Flash File System, Version 2. 3 * 4 * Copyright © 2001-2007 Red Hat, Inc. 5 * 6 * Created by David Woodhouse <dwmw2@infradead.org> 7 * 8 * For licensing information, see the file 'LICENCE' in this directory. 9 * 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/fs.h> 14 #include <linux/crc32.h> 15 #include <linux/pagemap.h> 16 #include <linux/mtd/mtd.h> 17 #include "nodelist.h" 18 #include "compr.h" 19 20 21 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 22 uint32_t mode, struct jffs2_raw_inode *ri) 23 { 24 struct jffs2_inode_cache *ic; 25 26 ic = jffs2_alloc_inode_cache(); 27 if (!ic) { 28 return -ENOMEM; 29 } 30 31 memset(ic, 0, sizeof(*ic)); 32 33 f->inocache = ic; 34 f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */ 35 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache; 36 f->inocache->state = INO_STATE_PRESENT; 37 38 jffs2_add_ino_cache(c, f->inocache); 39 jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino); 40 ri->ino = cpu_to_je32(f->inocache->ino); 41 42 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 43 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); 44 ri->totlen = cpu_to_je32(PAD(sizeof(*ri))); 45 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); 46 ri->mode = cpu_to_jemode(mode); 47 48 f->highest_version = 1; 49 ri->version = cpu_to_je32(f->highest_version); 50 51 return 0; 52 } 53 54 /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it, 55 write it to the flash, link it into the existing inode/fragment list */ 56 57 struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 58 struct jffs2_raw_inode *ri, const unsigned char *data, 59 uint32_t datalen, int alloc_mode) 60 61 { 62 struct jffs2_full_dnode *fn; 63 size_t retlen; 64 uint32_t flash_ofs; 65 struct kvec vecs[2]; 66 int ret; 67 int retried = 0; 68 unsigned long cnt = 2; 69 70 D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) { 71 pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n"); 72 BUG(); 73 } 74 ); 75 vecs[0].iov_base = ri; 76 vecs[0].iov_len = sizeof(*ri); 77 vecs[1].iov_base = (unsigned char *)data; 78 vecs[1].iov_len = datalen; 79 80 if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) { 81 pr_warn("%s(): ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n", 82 __func__, je32_to_cpu(ri->totlen), 83 sizeof(*ri), datalen); 84 } 85 86 fn = jffs2_alloc_full_dnode(); 87 if (!fn) 88 return ERR_PTR(-ENOMEM); 89 90 /* check number of valid vecs */ 91 if (!datalen || !data) 92 cnt = 1; 93 retry: 94 flash_ofs = write_ofs(c); 95 96 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len); 97 98 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) { 99 BUG_ON(!retried); 100 jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n", 101 __func__, 102 je32_to_cpu(ri->version), f->highest_version); 103 ri->version = cpu_to_je32(++f->highest_version); 104 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 105 } 106 107 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen, 108 (alloc_mode==ALLOC_GC)?0:f->inocache->ino); 109 110 if (ret || (retlen != sizeof(*ri) + datalen)) { 111 pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", 112 sizeof(*ri) + datalen, flash_ofs, ret, retlen); 113 114 /* Mark the space as dirtied */ 115 if (retlen) { 116 /* Don't change raw->size to match retlen. We may have 117 written the node header already, and only the data will 118 seem corrupted, in which case the scan would skip over 119 any node we write before the original intended end of 120 this node */ 121 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL); 122 } else { 123 pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", 124 flash_ofs); 125 } 126 if (!retried && alloc_mode != ALLOC_NORETRY) { 127 /* Try to reallocate space and retry */ 128 uint32_t dummy; 129 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size]; 130 131 retried = 1; 132 133 jffs2_dbg(1, "Retrying failed write.\n"); 134 135 jffs2_dbg_acct_sanity_check(c,jeb); 136 jffs2_dbg_acct_paranoia_check(c, jeb); 137 138 if (alloc_mode == ALLOC_GC) { 139 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy, 140 JFFS2_SUMMARY_INODE_SIZE); 141 } else { 142 /* Locking pain */ 143 mutex_unlock(&f->sem); 144 jffs2_complete_reservation(c); 145 146 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy, 147 alloc_mode, JFFS2_SUMMARY_INODE_SIZE); 148 mutex_lock(&f->sem); 149 } 150 151 if (!ret) { 152 flash_ofs = write_ofs(c); 153 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n", 154 flash_ofs); 155 156 jffs2_dbg_acct_sanity_check(c,jeb); 157 jffs2_dbg_acct_paranoia_check(c, jeb); 158 159 goto retry; 160 } 161 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n", 162 ret); 163 } 164 /* Release the full_dnode which is now useless, and return */ 165 jffs2_free_full_dnode(fn); 166 return ERR_PTR(ret?ret:-EIO); 167 } 168 /* Mark the space used */ 169 /* If node covers at least a whole page, or if it starts at the 170 beginning of a page and runs to the end of the file, or if 171 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL. 172 */ 173 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) || 174 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) && 175 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) { 176 flash_ofs |= REF_PRISTINE; 177 } else { 178 flash_ofs |= REF_NORMAL; 179 } 180 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache); 181 if (IS_ERR(fn->raw)) { 182 void *hold_err = fn->raw; 183 /* Release the full_dnode which is now useless, and return */ 184 jffs2_free_full_dnode(fn); 185 return ERR_CAST(hold_err); 186 } 187 fn->ofs = je32_to_cpu(ri->offset); 188 fn->size = je32_to_cpu(ri->dsize); 189 fn->frags = 0; 190 191 jffs2_dbg(1, "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n", 192 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize), 193 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc), 194 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)); 195 196 if (retried) { 197 jffs2_dbg_acct_sanity_check(c,NULL); 198 } 199 200 return fn; 201 } 202 203 struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 204 struct jffs2_raw_dirent *rd, const unsigned char *name, 205 uint32_t namelen, int alloc_mode) 206 { 207 struct jffs2_full_dirent *fd; 208 size_t retlen; 209 struct kvec vecs[2]; 210 uint32_t flash_ofs; 211 int retried = 0; 212 int ret; 213 214 jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n", 215 __func__, 216 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino), 217 je32_to_cpu(rd->name_crc)); 218 219 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) { 220 pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n"); 221 BUG(); 222 }); 223 224 if (strnlen(name, namelen) != namelen) { 225 /* This should never happen, but seems to have done on at least one 226 occasion: https://dev.laptop.org/ticket/4184 */ 227 pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n"); 228 pr_crit("Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n", 229 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino), 230 je32_to_cpu(rd->name_crc)); 231 WARN_ON(1); 232 return ERR_PTR(-EIO); 233 } 234 235 vecs[0].iov_base = rd; 236 vecs[0].iov_len = sizeof(*rd); 237 vecs[1].iov_base = (unsigned char *)name; 238 vecs[1].iov_len = namelen; 239 240 fd = jffs2_alloc_full_dirent(namelen+1); 241 if (!fd) 242 return ERR_PTR(-ENOMEM); 243 244 fd->version = je32_to_cpu(rd->version); 245 fd->ino = je32_to_cpu(rd->ino); 246 fd->nhash = full_name_hash(name, namelen); 247 fd->type = rd->type; 248 memcpy(fd->name, name, namelen); 249 fd->name[namelen]=0; 250 251 retry: 252 flash_ofs = write_ofs(c); 253 254 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len); 255 256 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) { 257 BUG_ON(!retried); 258 jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n", 259 __func__, 260 je32_to_cpu(rd->version), f->highest_version); 261 rd->version = cpu_to_je32(++f->highest_version); 262 fd->version = je32_to_cpu(rd->version); 263 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 264 } 265 266 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen, 267 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino)); 268 if (ret || (retlen != sizeof(*rd) + namelen)) { 269 pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", 270 sizeof(*rd) + namelen, flash_ofs, ret, retlen); 271 /* Mark the space as dirtied */ 272 if (retlen) { 273 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL); 274 } else { 275 pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", 276 flash_ofs); 277 } 278 if (!retried) { 279 /* Try to reallocate space and retry */ 280 uint32_t dummy; 281 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size]; 282 283 retried = 1; 284 285 jffs2_dbg(1, "Retrying failed write.\n"); 286 287 jffs2_dbg_acct_sanity_check(c,jeb); 288 jffs2_dbg_acct_paranoia_check(c, jeb); 289 290 if (alloc_mode == ALLOC_GC) { 291 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy, 292 JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 293 } else { 294 /* Locking pain */ 295 mutex_unlock(&f->sem); 296 jffs2_complete_reservation(c); 297 298 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy, 299 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 300 mutex_lock(&f->sem); 301 } 302 303 if (!ret) { 304 flash_ofs = write_ofs(c); 305 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n", 306 flash_ofs); 307 jffs2_dbg_acct_sanity_check(c,jeb); 308 jffs2_dbg_acct_paranoia_check(c, jeb); 309 goto retry; 310 } 311 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n", 312 ret); 313 } 314 /* Release the full_dnode which is now useless, and return */ 315 jffs2_free_full_dirent(fd); 316 return ERR_PTR(ret?ret:-EIO); 317 } 318 /* Mark the space used */ 319 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd), 320 PAD(sizeof(*rd)+namelen), f->inocache); 321 if (IS_ERR(fd->raw)) { 322 void *hold_err = fd->raw; 323 /* Release the full_dirent which is now useless, and return */ 324 jffs2_free_full_dirent(fd); 325 return ERR_CAST(hold_err); 326 } 327 328 if (retried) { 329 jffs2_dbg_acct_sanity_check(c,NULL); 330 } 331 332 return fd; 333 } 334 335 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that 336 we don't have to go digging in struct inode or its equivalent. It should set: 337 mode, uid, gid, (starting)isize, atime, ctime, mtime */ 338 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 339 struct jffs2_raw_inode *ri, unsigned char *buf, 340 uint32_t offset, uint32_t writelen, uint32_t *retlen) 341 { 342 int ret = 0; 343 uint32_t writtenlen = 0; 344 345 jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n", 346 __func__, f->inocache->ino, offset, writelen); 347 348 while(writelen) { 349 struct jffs2_full_dnode *fn; 350 unsigned char *comprbuf = NULL; 351 uint16_t comprtype = JFFS2_COMPR_NONE; 352 uint32_t alloclen; 353 uint32_t datalen, cdatalen; 354 int retried = 0; 355 356 retry: 357 jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", 358 writelen, offset); 359 360 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, 361 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 362 if (ret) { 363 jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret); 364 break; 365 } 366 mutex_lock(&f->sem); 367 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1))); 368 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen); 369 370 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen); 371 372 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 373 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE); 374 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen); 375 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)); 376 377 ri->ino = cpu_to_je32(f->inocache->ino); 378 ri->version = cpu_to_je32(++f->highest_version); 379 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen)); 380 ri->offset = cpu_to_je32(offset); 381 ri->csize = cpu_to_je32(cdatalen); 382 ri->dsize = cpu_to_je32(datalen); 383 ri->compr = comprtype & 0xff; 384 ri->usercompr = (comprtype >> 8 ) & 0xff; 385 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 386 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen)); 387 388 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY); 389 390 jffs2_free_comprbuf(comprbuf, buf); 391 392 if (IS_ERR(fn)) { 393 ret = PTR_ERR(fn); 394 mutex_unlock(&f->sem); 395 jffs2_complete_reservation(c); 396 if (!retried) { 397 /* Write error to be retried */ 398 retried = 1; 399 jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n"); 400 goto retry; 401 } 402 break; 403 } 404 ret = jffs2_add_full_dnode_to_inode(c, f, fn); 405 if (f->metadata) { 406 jffs2_mark_node_obsolete(c, f->metadata->raw); 407 jffs2_free_full_dnode(f->metadata); 408 f->metadata = NULL; 409 } 410 if (ret) { 411 /* Eep */ 412 jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", 413 ret); 414 jffs2_mark_node_obsolete(c, fn->raw); 415 jffs2_free_full_dnode(fn); 416 417 mutex_unlock(&f->sem); 418 jffs2_complete_reservation(c); 419 break; 420 } 421 mutex_unlock(&f->sem); 422 jffs2_complete_reservation(c); 423 if (!datalen) { 424 pr_warn("Eep. We didn't actually write any data in jffs2_write_inode_range()\n"); 425 ret = -EIO; 426 break; 427 } 428 jffs2_dbg(1, "increasing writtenlen by %d\n", datalen); 429 writtenlen += datalen; 430 offset += datalen; 431 writelen -= datalen; 432 buf += datalen; 433 } 434 *retlen = writtenlen; 435 return ret; 436 } 437 438 int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, 439 struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, 440 const struct qstr *qstr) 441 { 442 struct jffs2_raw_dirent *rd; 443 struct jffs2_full_dnode *fn; 444 struct jffs2_full_dirent *fd; 445 uint32_t alloclen; 446 int ret; 447 448 /* Try to reserve enough space for both node and dirent. 449 * Just the node will do for now, though 450 */ 451 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, 452 JFFS2_SUMMARY_INODE_SIZE); 453 jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen); 454 if (ret) 455 return ret; 456 457 mutex_lock(&f->sem); 458 459 ri->data_crc = cpu_to_je32(0); 460 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 461 462 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL); 463 464 jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n", 465 jemode_to_cpu(ri->mode)); 466 467 if (IS_ERR(fn)) { 468 jffs2_dbg(1, "jffs2_write_dnode() failed\n"); 469 /* Eeek. Wave bye bye */ 470 mutex_unlock(&f->sem); 471 jffs2_complete_reservation(c); 472 return PTR_ERR(fn); 473 } 474 /* No data here. Only a metadata node, which will be 475 obsoleted by the first data write 476 */ 477 f->metadata = fn; 478 479 mutex_unlock(&f->sem); 480 jffs2_complete_reservation(c); 481 482 ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr); 483 if (ret) 484 return ret; 485 ret = jffs2_init_acl_post(&f->vfs_inode); 486 if (ret) 487 return ret; 488 489 ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen, 490 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len)); 491 492 if (ret) { 493 /* Eep. */ 494 jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n"); 495 return ret; 496 } 497 498 rd = jffs2_alloc_raw_dirent(); 499 if (!rd) { 500 /* Argh. Now we treat it like a normal delete */ 501 jffs2_complete_reservation(c); 502 return -ENOMEM; 503 } 504 505 mutex_lock(&dir_f->sem); 506 507 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 508 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 509 rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len); 510 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 511 512 rd->pino = cpu_to_je32(dir_f->inocache->ino); 513 rd->version = cpu_to_je32(++dir_f->highest_version); 514 rd->ino = ri->ino; 515 rd->mctime = ri->ctime; 516 rd->nsize = qstr->len; 517 rd->type = DT_REG; 518 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 519 rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len)); 520 521 fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL); 522 523 jffs2_free_raw_dirent(rd); 524 525 if (IS_ERR(fd)) { 526 /* dirent failed to write. Delete the inode normally 527 as if it were the final unlink() */ 528 jffs2_complete_reservation(c); 529 mutex_unlock(&dir_f->sem); 530 return PTR_ERR(fd); 531 } 532 533 /* Link the fd into the inode's list, obsoleting an old 534 one if necessary. */ 535 jffs2_add_fd_to_list(c, fd, &dir_f->dents); 536 537 jffs2_complete_reservation(c); 538 mutex_unlock(&dir_f->sem); 539 540 return 0; 541 } 542 543 544 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, 545 const char *name, int namelen, struct jffs2_inode_info *dead_f, 546 uint32_t time) 547 { 548 struct jffs2_raw_dirent *rd; 549 struct jffs2_full_dirent *fd; 550 uint32_t alloclen; 551 int ret; 552 553 if (!jffs2_can_mark_obsolete(c)) { 554 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */ 555 556 rd = jffs2_alloc_raw_dirent(); 557 if (!rd) 558 return -ENOMEM; 559 560 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 561 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 562 if (ret) { 563 jffs2_free_raw_dirent(rd); 564 return ret; 565 } 566 567 mutex_lock(&dir_f->sem); 568 569 /* Build a deletion node */ 570 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 571 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 572 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 573 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 574 575 rd->pino = cpu_to_je32(dir_f->inocache->ino); 576 rd->version = cpu_to_je32(++dir_f->highest_version); 577 rd->ino = cpu_to_je32(0); 578 rd->mctime = cpu_to_je32(time); 579 rd->nsize = namelen; 580 rd->type = DT_UNKNOWN; 581 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 582 rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); 583 584 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION); 585 586 jffs2_free_raw_dirent(rd); 587 588 if (IS_ERR(fd)) { 589 jffs2_complete_reservation(c); 590 mutex_unlock(&dir_f->sem); 591 return PTR_ERR(fd); 592 } 593 594 /* File it. This will mark the old one obsolete. */ 595 jffs2_add_fd_to_list(c, fd, &dir_f->dents); 596 mutex_unlock(&dir_f->sem); 597 } else { 598 uint32_t nhash = full_name_hash(name, namelen); 599 600 fd = dir_f->dents; 601 /* We don't actually want to reserve any space, but we do 602 want to be holding the alloc_sem when we write to flash */ 603 mutex_lock(&c->alloc_sem); 604 mutex_lock(&dir_f->sem); 605 606 for (fd = dir_f->dents; fd; fd = fd->next) { 607 if (fd->nhash == nhash && 608 !memcmp(fd->name, name, namelen) && 609 !fd->name[namelen]) { 610 611 jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n", 612 fd->ino, ref_offset(fd->raw)); 613 jffs2_mark_node_obsolete(c, fd->raw); 614 /* We don't want to remove it from the list immediately, 615 because that screws up getdents()/seek() semantics even 616 more than they're screwed already. Turn it into a 617 node-less deletion dirent instead -- a placeholder */ 618 fd->raw = NULL; 619 fd->ino = 0; 620 break; 621 } 622 } 623 mutex_unlock(&dir_f->sem); 624 } 625 626 /* dead_f is NULL if this was a rename not a real unlink */ 627 /* Also catch the !f->inocache case, where there was a dirent 628 pointing to an inode which didn't exist. */ 629 if (dead_f && dead_f->inocache) { 630 631 mutex_lock(&dead_f->sem); 632 633 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) { 634 while (dead_f->dents) { 635 /* There can be only deleted ones */ 636 fd = dead_f->dents; 637 638 dead_f->dents = fd->next; 639 640 if (fd->ino) { 641 pr_warn("Deleting inode #%u with active dentry \"%s\"->ino #%u\n", 642 dead_f->inocache->ino, 643 fd->name, fd->ino); 644 } else { 645 jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n", 646 fd->name, 647 dead_f->inocache->ino); 648 } 649 if (fd->raw) 650 jffs2_mark_node_obsolete(c, fd->raw); 651 jffs2_free_full_dirent(fd); 652 } 653 dead_f->inocache->pino_nlink = 0; 654 } else 655 dead_f->inocache->pino_nlink--; 656 /* NB: Caller must set inode nlink if appropriate */ 657 mutex_unlock(&dead_f->sem); 658 } 659 660 jffs2_complete_reservation(c); 661 662 return 0; 663 } 664 665 666 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, uint32_t time) 667 { 668 struct jffs2_raw_dirent *rd; 669 struct jffs2_full_dirent *fd; 670 uint32_t alloclen; 671 int ret; 672 673 rd = jffs2_alloc_raw_dirent(); 674 if (!rd) 675 return -ENOMEM; 676 677 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen, 678 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 679 if (ret) { 680 jffs2_free_raw_dirent(rd); 681 return ret; 682 } 683 684 mutex_lock(&dir_f->sem); 685 686 /* Build a deletion node */ 687 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 688 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT); 689 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen); 690 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)); 691 692 rd->pino = cpu_to_je32(dir_f->inocache->ino); 693 rd->version = cpu_to_je32(++dir_f->highest_version); 694 rd->ino = cpu_to_je32(ino); 695 rd->mctime = cpu_to_je32(time); 696 rd->nsize = namelen; 697 698 rd->type = type; 699 700 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 701 rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); 702 703 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL); 704 705 jffs2_free_raw_dirent(rd); 706 707 if (IS_ERR(fd)) { 708 jffs2_complete_reservation(c); 709 mutex_unlock(&dir_f->sem); 710 return PTR_ERR(fd); 711 } 712 713 /* File it. This will mark the old one obsolete. */ 714 jffs2_add_fd_to_list(c, fd, &dir_f->dents); 715 716 jffs2_complete_reservation(c); 717 mutex_unlock(&dir_f->sem); 718 719 return 0; 720 } 721