1 /* $NetBSD: chfs_scan.c,v 1.4 2012/10/19 12:44:39 ttoth Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Department of Software Engineering, 5 * University of Szeged, Hungary 6 * Copyright (c) 2010 David Tengeri <dtengeri@inf.u-szeged.hu> 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by the Department of Software Engineering, University of Szeged, Hungary 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "chfs.h" 35 36 /* 37 * chfs_scan_make_vnode_cache - makes a new vnode cache during scan 38 * This function returns a vnode cache belonging to @vno. 39 */ 40 struct chfs_vnode_cache * 41 chfs_scan_make_vnode_cache(struct chfs_mount *chmp, ino_t vno) 42 { 43 struct chfs_vnode_cache *vc; 44 45 KASSERT(mutex_owned(&chmp->chm_lock_vnocache)); 46 47 /* vnode cache already exists */ 48 vc = chfs_vnode_cache_get(chmp, vno); 49 if (vc) { 50 return vc; 51 } 52 53 /* update max vnode number if needed */ 54 if (vno > chmp->chm_max_vno) { 55 chmp->chm_max_vno = vno; 56 } 57 58 /* create new vnode cache */ 59 vc = chfs_vnode_cache_alloc(vno); 60 61 chfs_vnode_cache_add(chmp, vc); 62 63 if (vno == CHFS_ROOTINO) { 64 vc->nlink = 2; 65 vc->pvno = CHFS_ROOTINO; 66 vc->state = VNO_STATE_CHECKEDABSENT; 67 } 68 69 return vc; 70 } 71 72 /* 73 * chfs_scan_check_node_hdr - checks node magic and crc 74 * Returns 0 if everything is OK, error code otherwise. 75 */ 76 int 77 chfs_scan_check_node_hdr(struct chfs_flash_node_hdr *nhdr) 78 { 79 uint16_t magic; 80 uint32_t crc, hdr_crc; 81 82 magic = le16toh(nhdr->magic); 83 84 if (magic != CHFS_FS_MAGIC_BITMASK) { 85 dbg("bad magic\n"); 86 return CHFS_NODE_BADMAGIC; 87 } 88 89 hdr_crc = le32toh(nhdr->hdr_crc); 90 crc = crc32(0, (uint8_t *)nhdr, CHFS_NODE_HDR_SIZE - 4); 91 92 if (crc != hdr_crc) { 93 dbg("bad crc\n"); 94 return CHFS_NODE_BADCRC; 95 } 96 97 return CHFS_NODE_OK; 98 } 99 100 /* chfs_scan_check_vnode - check vnode crc and add it to vnode cache */ 101 int 102 chfs_scan_check_vnode(struct chfs_mount *chmp, 103 struct chfs_eraseblock *cheb, void *buf, off_t ofs) 104 { 105 KASSERT(mutex_owned(&chmp->chm_lock_mountfields)); 106 struct chfs_vnode_cache *vc; 107 struct chfs_flash_vnode *vnode = buf; 108 struct chfs_node_ref *nref; 109 int err; 110 uint32_t crc; 111 ino_t vno; 112 113 crc = crc32(0, (uint8_t *)vnode, 114 sizeof(struct chfs_flash_vnode) - 4); 115 116 /* check node crc */ 117 if (crc != le32toh(vnode->node_crc)) { 118 err = chfs_update_eb_dirty(chmp, 119 cheb, le32toh(vnode->length)); 120 if (err) { 121 return err; 122 } 123 124 return CHFS_NODE_BADCRC; 125 } 126 127 vno = le64toh(vnode->vno); 128 129 /* find the corresponding vnode cache */ 130 mutex_enter(&chmp->chm_lock_vnocache); 131 vc = chfs_vnode_cache_get(chmp, vno); 132 if (!vc) { 133 vc = chfs_scan_make_vnode_cache(chmp, vno); 134 if (!vc) { 135 mutex_exit(&chmp->chm_lock_vnocache); 136 return ENOMEM; 137 } 138 } 139 140 nref = chfs_alloc_node_ref(cheb); 141 142 nref->nref_offset = ofs; 143 144 KASSERT(nref->nref_lnr == cheb->lnr); 145 146 /* check version of vnode */ 147 if ((struct chfs_vnode_cache *)vc->v != vc) { 148 if (le64toh(vnode->version) > *vc->vno_version) { 149 *vc->vno_version = le64toh(vnode->version); 150 chfs_add_vnode_ref_to_vc(chmp, vc, nref); 151 } else { 152 err = chfs_update_eb_dirty(chmp, cheb, 153 sizeof(struct chfs_flash_vnode)); 154 return CHFS_NODE_OK; 155 } 156 } else { 157 vc->vno_version = kmem_alloc(sizeof(uint64_t), KM_SLEEP); 158 if (!vc->vno_version) 159 return ENOMEM; 160 *vc->vno_version = le64toh(vnode->version); 161 chfs_add_vnode_ref_to_vc(chmp, vc, nref); 162 } 163 mutex_exit(&chmp->chm_lock_vnocache); 164 165 /* update sizes */ 166 mutex_enter(&chmp->chm_lock_sizes); 167 chfs_change_size_free(chmp, cheb, -le32toh(vnode->length)); 168 chfs_change_size_used(chmp, cheb, le32toh(vnode->length)); 169 mutex_exit(&chmp->chm_lock_sizes); 170 171 KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size); 172 173 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 174 175 return CHFS_NODE_OK; 176 } 177 178 /* chfs_scan_mark_dirent_obsolete - marks a directory entry "obsolete" */ 179 int 180 chfs_scan_mark_dirent_obsolete(struct chfs_mount *chmp, 181 struct chfs_vnode_cache *vc, struct chfs_dirent *fd) 182 { 183 struct chfs_eraseblock *cheb; 184 struct chfs_node_ref *prev, *nref; 185 186 nref = fd->nref; 187 cheb = &chmp->chm_blocks[fd->nref->nref_lnr]; 188 189 /* remove dirent's node ref from vnode cache */ 190 prev = vc->dirents; 191 if (prev && prev == nref) { 192 vc->dirents = prev->nref_next; 193 } else if (prev && prev != (void *)vc) { 194 while (prev->nref_next && prev->nref_next != (void *)vc) { 195 if (prev->nref_next == nref) { 196 prev->nref_next = nref->nref_next; 197 break; 198 } 199 prev = prev->nref_next; 200 } 201 } 202 203 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + 204 cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 205 206 return 0; 207 } 208 209 /* chfs_add_fd_to_list - adds a directory entry to its parent's vnode cache */ 210 void 211 chfs_add_fd_to_list(struct chfs_mount *chmp, 212 struct chfs_dirent *new, struct chfs_vnode_cache *pvc) 213 { 214 KASSERT(mutex_owned(&chmp->chm_lock_mountfields)); 215 int size; 216 struct chfs_eraseblock *cheb, *oldcheb; 217 struct chfs_dirent *fd, *tmpfd; 218 219 dbg("adding fd to list: %s\n", new->name); 220 221 /* update highest version if needed */ 222 if ((new->version > pvc->highest_version)) 223 pvc->highest_version = new->version; 224 225 size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) + 226 new->nsize); 227 cheb = &chmp->chm_blocks[new->nref->nref_lnr]; 228 229 mutex_enter(&chmp->chm_lock_sizes); 230 TAILQ_FOREACH_SAFE(fd, &pvc->scan_dirents, fds, tmpfd) { 231 if (fd->nhash > new->nhash) { 232 /* insert new before fd */ 233 TAILQ_INSERT_BEFORE(fd, new, fds); 234 goto out; 235 } else if (fd->nhash == new->nhash && 236 !strcmp(fd->name, new->name)) { 237 if (new->version > fd->version) { 238 /* replace fd with new */ 239 TAILQ_INSERT_BEFORE(fd, new, fds); 240 chfs_change_size_free(chmp, cheb, -size); 241 chfs_change_size_used(chmp, cheb, size); 242 243 TAILQ_REMOVE(&pvc->scan_dirents, fd, fds); 244 if (fd->nref) { 245 size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) + fd->nsize); 246 chfs_scan_mark_dirent_obsolete(chmp, pvc, fd); 247 oldcheb = &chmp->chm_blocks[fd->nref->nref_lnr]; 248 chfs_change_size_used(chmp, oldcheb, -size); 249 chfs_change_size_dirty(chmp, oldcheb, size); 250 } 251 chfs_free_dirent(fd); 252 } else { 253 /* new dirent is older */ 254 chfs_scan_mark_dirent_obsolete(chmp, pvc, new); 255 chfs_change_size_free(chmp, cheb, -size); 256 chfs_change_size_dirty(chmp, cheb, size); 257 chfs_free_dirent(new); 258 } 259 mutex_exit(&chmp->chm_lock_sizes); 260 return; 261 } 262 } 263 /* if we couldnt fit it elsewhere, lets add to the end */ 264 TAILQ_INSERT_TAIL(&pvc->scan_dirents, new, fds); 265 266 out: 267 /* update sizes */ 268 chfs_change_size_free(chmp, cheb, -size); 269 chfs_change_size_used(chmp, cheb, size); 270 mutex_exit(&chmp->chm_lock_sizes); 271 272 KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size); 273 274 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 275 } 276 277 /* chfs_scan_check_dirent_node - check vnode crc and add to vnode cache */ 278 int 279 chfs_scan_check_dirent_node(struct chfs_mount *chmp, 280 struct chfs_eraseblock *cheb, void *buf, off_t ofs) 281 { 282 int err, namelen; 283 uint32_t crc; 284 struct chfs_dirent *fd; 285 struct chfs_vnode_cache *parentvc; 286 struct chfs_flash_dirent_node *dirent = buf; 287 288 /* check crc */ 289 crc = crc32(0, (uint8_t *)dirent, sizeof(*dirent) - 4); 290 if (crc != le32toh(dirent->node_crc)) { 291 err = chfs_update_eb_dirty(chmp, cheb, le32toh(dirent->length)); 292 if (err) 293 return err; 294 return CHFS_NODE_BADCRC; 295 } 296 297 /* allocate space for name */ 298 namelen = dirent->nsize; 299 300 fd = chfs_alloc_dirent(namelen + 1); 301 if (!fd) 302 return ENOMEM; 303 304 /* allocate an nref */ 305 fd->nref = chfs_alloc_node_ref(cheb); 306 if (!fd->nref) 307 return ENOMEM; 308 309 KASSERT(fd->nref->nref_lnr == cheb->lnr); 310 311 memcpy(&fd->name, dirent->name, namelen); 312 fd->nsize = namelen; 313 fd->name[namelen] = 0; 314 crc = crc32(0, fd->name, dirent->nsize); 315 if (crc != le32toh(dirent->name_crc)) { 316 chfs_err("Directory entry's name has bad crc: read: 0x%x, " 317 "calculated: 0x%x\n", le32toh(dirent->name_crc), crc); 318 chfs_free_dirent(fd); 319 err = chfs_update_eb_dirty(chmp, cheb, le32toh(dirent->length)); 320 if (err) 321 return err; 322 return CHFS_NODE_BADNAMECRC; 323 } 324 325 /* check vnode_cache of parent node */ 326 mutex_enter(&chmp->chm_lock_vnocache); 327 parentvc = chfs_scan_make_vnode_cache(chmp, le64toh(dirent->pvno)); 328 if (!parentvc) { 329 chfs_free_dirent(fd); 330 return ENOMEM; 331 } 332 333 fd->nref->nref_offset = ofs; 334 335 dbg("add dirent to #%llu\n", (unsigned long long)parentvc->vno); 336 chfs_add_node_to_list(chmp, parentvc, fd->nref, &parentvc->dirents); 337 mutex_exit(&chmp->chm_lock_vnocache); 338 339 fd->vno = le64toh(dirent->vno); 340 fd->version = le64toh(dirent->version); 341 fd->nhash = hash32_buf(fd->name, namelen, HASH32_BUF_INIT); 342 fd->type = dirent->dtype; 343 344 chfs_add_fd_to_list(chmp, fd, parentvc); 345 346 return CHFS_NODE_OK; 347 } 348 349 /* chfs_scan_check_data_node - check vnode crc and add to vnode cache */ 350 int 351 chfs_scan_check_data_node(struct chfs_mount *chmp, 352 struct chfs_eraseblock *cheb, void *buf, off_t ofs) 353 { 354 KASSERT(mutex_owned(&chmp->chm_lock_mountfields)); 355 int err; 356 uint32_t crc, vno; 357 struct chfs_node_ref *nref; 358 struct chfs_vnode_cache *vc; 359 struct chfs_flash_data_node *dnode = buf; 360 361 /* check crc */ 362 crc = crc32(0, (uint8_t *)dnode, sizeof(struct chfs_flash_data_node) - 4); 363 if (crc != le32toh(dnode->node_crc)) { 364 err = chfs_update_eb_dirty(chmp, cheb, le32toh(dnode->length)); 365 if (err) 366 return err; 367 return CHFS_NODE_BADCRC; 368 } 369 /* 370 * Don't check data nodes crc and version here, it will be done in 371 * the background GC thread. 372 */ 373 nref = chfs_alloc_node_ref(cheb); 374 if (!nref) 375 return ENOMEM; 376 377 nref->nref_offset = CHFS_GET_OFS(ofs) | CHFS_UNCHECKED_NODE_MASK; 378 379 KASSERT(nref->nref_lnr == cheb->lnr); 380 381 vno = le64toh(dnode->vno); 382 mutex_enter(&chmp->chm_lock_vnocache); 383 vc = chfs_vnode_cache_get(chmp, vno); 384 if (!vc) { 385 vc = chfs_scan_make_vnode_cache(chmp, vno); 386 if (!vc) 387 return ENOMEM; 388 } 389 chfs_add_node_to_list(chmp, vc, nref, &vc->dnode); 390 mutex_exit(&chmp->chm_lock_vnocache); 391 392 dbg("chmpfree: %u, chebfree: %u, dnode: %u\n", chmp->chm_free_size, cheb->free_size, dnode->length); 393 394 /* update sizes */ 395 mutex_enter(&chmp->chm_lock_sizes); 396 chfs_change_size_free(chmp, cheb, -dnode->length); 397 chfs_change_size_unchecked(chmp, cheb, dnode->length); 398 mutex_exit(&chmp->chm_lock_sizes); 399 return CHFS_NODE_OK; 400 } 401 402 /* chfs_scan_classify_cheb - determine eraseblock's state */ 403 int 404 chfs_scan_classify_cheb(struct chfs_mount *chmp, 405 struct chfs_eraseblock *cheb) 406 { 407 if (cheb->free_size == chmp->chm_ebh->eb_size) 408 return CHFS_BLK_STATE_FREE; 409 else if (cheb->dirty_size < MAX_DIRTY_TO_CLEAN) 410 return CHFS_BLK_STATE_CLEAN; 411 else if (cheb->used_size || cheb->unchecked_size) 412 return CHFS_BLK_STATE_PARTDIRTY; 413 else 414 return CHFS_BLK_STATE_ALLDIRTY; 415 } 416 417 418 /* 419 * chfs_scan_eraseblock - scans an eraseblock and looking for nodes 420 * 421 * This function scans a whole eraseblock, checks the nodes on it and add them 422 * to the vnode cache. 423 * Returns eraseblock state on success, error code if fails. 424 */ 425 int 426 chfs_scan_eraseblock(struct chfs_mount *chmp, 427 struct chfs_eraseblock *cheb) 428 { 429 int err; 430 size_t len, retlen; 431 off_t ofs = 0; 432 int lnr = cheb->lnr; 433 u_char *buf; 434 struct chfs_flash_node_hdr *nhdr; 435 int read_free = 0; 436 struct chfs_node_ref *nref; 437 438 dbg("scanning eraseblock content: %d free_size: %d\n", cheb->lnr, cheb->free_size); 439 dbg("scanned physical block: %d\n", chmp->chm_ebh->lmap[lnr]); 440 buf = kmem_alloc(CHFS_MAX_NODE_SIZE, KM_SLEEP); 441 442 while((ofs + CHFS_NODE_HDR_SIZE) < chmp->chm_ebh->eb_size) { 443 memset(buf, 0 , CHFS_MAX_NODE_SIZE); 444 err = chfs_read_leb(chmp, 445 lnr, buf, ofs, CHFS_NODE_HDR_SIZE, &retlen); 446 if (err) { 447 return err; 448 } 449 450 if (retlen != CHFS_NODE_HDR_SIZE) { 451 chfs_err("Error reading node header: " 452 "read: %zu instead of: %zu\n", 453 CHFS_NODE_HDR_SIZE, retlen); 454 return EIO; 455 } 456 457 /* first we check if the buffer we read is full with 0xff, if yes maybe 458 * the blocks remaining area is free. We increase read_free and if it 459 * reaches MAX_READ_FREE we stop reading the block */ 460 if (check_pattern(buf, 0xff, 0, CHFS_NODE_HDR_SIZE)) { 461 read_free += CHFS_NODE_HDR_SIZE; 462 if (read_free >= MAX_READ_FREE(chmp)) { 463 dbg("rest of the block is free. Size: %d\n", cheb->free_size); 464 return chfs_scan_classify_cheb(chmp, cheb); 465 } 466 ofs += CHFS_NODE_HDR_SIZE; 467 continue; 468 } else { 469 chfs_update_eb_dirty(chmp, cheb, read_free); 470 read_free = 0; 471 } 472 473 nhdr = (struct chfs_flash_node_hdr *)buf; 474 475 err = chfs_scan_check_node_hdr(nhdr); 476 if (err) { 477 dbg("node hdr error\n"); 478 err = chfs_update_eb_dirty(chmp, cheb, 4); 479 if (err) { 480 return err; 481 } 482 483 ofs += 4; 484 continue; 485 } 486 ofs += CHFS_NODE_HDR_SIZE; 487 if (ofs > chmp->chm_ebh->eb_size) { 488 chfs_err("Second part of node is on the next eraseblock.\n"); 489 return EIO; 490 } 491 switch (le16toh(nhdr->type)) { 492 case CHFS_NODETYPE_VNODE: 493 /* vnode information */ 494 /* read up the node */ 495 len = le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE; 496 err = chfs_read_leb(chmp, 497 lnr, buf + CHFS_NODE_HDR_SIZE, 498 ofs, len, &retlen); 499 if (err) { 500 return err; 501 } 502 503 if (retlen != len) { 504 chfs_err("Error reading vnode: read: %zu instead of: %zu\n", 505 len, retlen); 506 return EIO; 507 } 508 KASSERT(lnr == cheb->lnr); 509 err = chfs_scan_check_vnode(chmp, 510 cheb, buf, ofs - CHFS_NODE_HDR_SIZE); 511 if (err) { 512 return err; 513 } 514 515 break; 516 case CHFS_NODETYPE_DIRENT: 517 /* directory entry */ 518 /* read up the node */ 519 len = le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE; 520 521 err = chfs_read_leb(chmp, 522 lnr, buf + CHFS_NODE_HDR_SIZE, 523 ofs, len, &retlen); 524 if (err) { 525 return err; 526 } 527 528 if (retlen != len) { 529 chfs_err("Error reading dirent node: read: %zu " 530 "instead of: %zu\n", len, retlen); 531 return EIO; 532 } 533 534 KASSERT(lnr == cheb->lnr); 535 536 err = chfs_scan_check_dirent_node(chmp, 537 cheb, buf, ofs - CHFS_NODE_HDR_SIZE); 538 if (err) { 539 return err; 540 } 541 542 break; 543 case CHFS_NODETYPE_DATA: 544 /* data node */ 545 len = sizeof(struct chfs_flash_data_node) - 546 CHFS_NODE_HDR_SIZE; 547 err = chfs_read_leb(chmp, 548 lnr, buf + CHFS_NODE_HDR_SIZE, 549 ofs, len, &retlen); 550 if (err) { 551 return err; 552 } 553 554 if (retlen != len) { 555 chfs_err("Error reading data node: read: %zu " 556 "instead of: %zu\n", len, retlen); 557 return EIO; 558 } 559 KASSERT(lnr == cheb->lnr); 560 err = chfs_scan_check_data_node(chmp, 561 cheb, buf, ofs - CHFS_NODE_HDR_SIZE); 562 if (err) 563 return err; 564 565 break; 566 case CHFS_NODETYPE_PADDING: 567 /* padding node, set size and update dirty */ 568 nref = chfs_alloc_node_ref(cheb); 569 nref->nref_offset = ofs - CHFS_NODE_HDR_SIZE; 570 nref->nref_offset = CHFS_GET_OFS(nref->nref_offset) | 571 CHFS_OBSOLETE_NODE_MASK; 572 573 err = chfs_update_eb_dirty(chmp, cheb, 574 le32toh(nhdr->length)); 575 if (err) 576 return err; 577 578 break; 579 default: 580 /* unknown node type, update dirty and skip */ 581 err = chfs_update_eb_dirty(chmp, cheb, 582 le32toh(nhdr->length)); 583 if (err) 584 return err; 585 586 break; 587 } 588 ofs += le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE; 589 } 590 591 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + 592 cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 593 594 return chfs_scan_classify_cheb(chmp, cheb); 595 } 596