1 /* $NetBSD: chfs_scan.c,v 1.7 2017/06/01 02:45:15 chs 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 *vc->vno_version = le64toh(vnode->version); 159 chfs_add_vnode_ref_to_vc(chmp, vc, nref); 160 } 161 mutex_exit(&chmp->chm_lock_vnocache); 162 163 /* update sizes */ 164 mutex_enter(&chmp->chm_lock_sizes); 165 chfs_change_size_free(chmp, cheb, -le32toh(vnode->length)); 166 chfs_change_size_used(chmp, cheb, le32toh(vnode->length)); 167 mutex_exit(&chmp->chm_lock_sizes); 168 169 KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size); 170 171 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 172 173 return CHFS_NODE_OK; 174 } 175 176 /* chfs_scan_mark_dirent_obsolete - marks a directory entry "obsolete" */ 177 int 178 chfs_scan_mark_dirent_obsolete(struct chfs_mount *chmp, 179 struct chfs_vnode_cache *vc, struct chfs_dirent *fd) 180 { 181 struct chfs_eraseblock *cheb; 182 struct chfs_node_ref *prev, *nref; 183 184 nref = fd->nref; 185 cheb = &chmp->chm_blocks[fd->nref->nref_lnr]; 186 187 /* remove dirent's node ref from vnode cache */ 188 prev = vc->dirents; 189 if (prev && prev == nref) { 190 vc->dirents = prev->nref_next; 191 } else if (prev && prev != (void *)vc) { 192 while (prev->nref_next && prev->nref_next != (void *)vc) { 193 if (prev->nref_next == nref) { 194 prev->nref_next = nref->nref_next; 195 break; 196 } 197 prev = prev->nref_next; 198 } 199 } 200 201 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + 202 cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 203 204 return 0; 205 } 206 207 /* chfs_add_fd_to_list - adds a directory entry to its parent's vnode cache */ 208 void 209 chfs_add_fd_to_list(struct chfs_mount *chmp, 210 struct chfs_dirent *new, struct chfs_vnode_cache *pvc) 211 { 212 KASSERT(mutex_owned(&chmp->chm_lock_mountfields)); 213 int size; 214 struct chfs_eraseblock *cheb, *oldcheb; 215 struct chfs_dirent *fd, *tmpfd; 216 217 dbg("adding fd to list: %s\n", new->name); 218 219 /* update highest version if needed */ 220 if ((new->version > pvc->highest_version)) 221 pvc->highest_version = new->version; 222 223 size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) + 224 new->nsize); 225 cheb = &chmp->chm_blocks[new->nref->nref_lnr]; 226 227 mutex_enter(&chmp->chm_lock_sizes); 228 TAILQ_FOREACH_SAFE(fd, &pvc->scan_dirents, fds, tmpfd) { 229 if (fd->nhash > new->nhash) { 230 /* insert new before fd */ 231 TAILQ_INSERT_BEFORE(fd, new, fds); 232 goto out; 233 } else if (fd->nhash == new->nhash && 234 !strcmp(fd->name, new->name)) { 235 if (new->version > fd->version) { 236 /* replace fd with new */ 237 TAILQ_INSERT_BEFORE(fd, new, fds); 238 chfs_change_size_free(chmp, cheb, -size); 239 chfs_change_size_used(chmp, cheb, size); 240 241 TAILQ_REMOVE(&pvc->scan_dirents, fd, fds); 242 if (fd->nref) { 243 size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) + fd->nsize); 244 chfs_scan_mark_dirent_obsolete(chmp, pvc, fd); 245 oldcheb = &chmp->chm_blocks[fd->nref->nref_lnr]; 246 chfs_change_size_used(chmp, oldcheb, -size); 247 chfs_change_size_dirty(chmp, oldcheb, size); 248 } 249 chfs_free_dirent(fd); 250 } else { 251 /* new dirent is older */ 252 chfs_scan_mark_dirent_obsolete(chmp, pvc, new); 253 chfs_change_size_free(chmp, cheb, -size); 254 chfs_change_size_dirty(chmp, cheb, size); 255 chfs_free_dirent(new); 256 } 257 mutex_exit(&chmp->chm_lock_sizes); 258 return; 259 } 260 } 261 /* if we couldnt fit it elsewhere, lets add to the end */ 262 TAILQ_INSERT_TAIL(&pvc->scan_dirents, new, fds); 263 264 out: 265 /* update sizes */ 266 chfs_change_size_free(chmp, cheb, -size); 267 chfs_change_size_used(chmp, cheb, size); 268 mutex_exit(&chmp->chm_lock_sizes); 269 270 KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size); 271 272 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 273 } 274 275 /* chfs_scan_check_dirent_node - check vnode crc and add to vnode cache */ 276 int 277 chfs_scan_check_dirent_node(struct chfs_mount *chmp, 278 struct chfs_eraseblock *cheb, void *buf, off_t ofs) 279 { 280 int err, namelen; 281 uint32_t crc; 282 struct chfs_dirent *fd; 283 struct chfs_vnode_cache *parentvc; 284 struct chfs_flash_dirent_node *dirent = buf; 285 286 /* check crc */ 287 crc = crc32(0, (uint8_t *)dirent, sizeof(*dirent) - 4); 288 if (crc != le32toh(dirent->node_crc)) { 289 err = chfs_update_eb_dirty(chmp, cheb, le32toh(dirent->length)); 290 if (err) 291 return err; 292 return CHFS_NODE_BADCRC; 293 } 294 295 /* allocate space for name */ 296 namelen = dirent->nsize; 297 298 fd = chfs_alloc_dirent(namelen + 1); 299 if (!fd) 300 return ENOMEM; 301 302 /* allocate an nref */ 303 fd->nref = chfs_alloc_node_ref(cheb); 304 if (!fd->nref) 305 return ENOMEM; 306 307 KASSERT(fd->nref->nref_lnr == cheb->lnr); 308 309 memcpy(&fd->name, dirent->name, namelen); 310 fd->nsize = namelen; 311 fd->name[namelen] = 0; 312 crc = crc32(0, fd->name, dirent->nsize); 313 if (crc != le32toh(dirent->name_crc)) { 314 chfs_err("Directory entry's name has bad crc: read: 0x%x, " 315 "calculated: 0x%x\n", le32toh(dirent->name_crc), crc); 316 chfs_free_dirent(fd); 317 err = chfs_update_eb_dirty(chmp, cheb, le32toh(dirent->length)); 318 if (err) 319 return err; 320 return CHFS_NODE_BADNAMECRC; 321 } 322 323 /* check vnode_cache of parent node */ 324 mutex_enter(&chmp->chm_lock_vnocache); 325 parentvc = chfs_scan_make_vnode_cache(chmp, le64toh(dirent->pvno)); 326 if (!parentvc) { 327 chfs_free_dirent(fd); 328 return ENOMEM; 329 } 330 331 fd->nref->nref_offset = ofs; 332 333 dbg("add dirent to #%llu\n", (unsigned long long)parentvc->vno); 334 chfs_add_node_to_list(chmp, parentvc, fd->nref, &parentvc->dirents); 335 mutex_exit(&chmp->chm_lock_vnocache); 336 337 fd->vno = le64toh(dirent->vno); 338 fd->version = le64toh(dirent->version); 339 fd->nhash = hash32_buf(fd->name, namelen, HASH32_BUF_INIT); 340 fd->type = dirent->dtype; 341 342 chfs_add_fd_to_list(chmp, fd, parentvc); 343 344 return CHFS_NODE_OK; 345 } 346 347 /* chfs_scan_check_data_node - check vnode crc and add to vnode cache */ 348 int 349 chfs_scan_check_data_node(struct chfs_mount *chmp, 350 struct chfs_eraseblock *cheb, void *buf, off_t ofs) 351 { 352 KASSERT(mutex_owned(&chmp->chm_lock_mountfields)); 353 int err; 354 uint32_t crc, vno; 355 struct chfs_node_ref *nref; 356 struct chfs_vnode_cache *vc; 357 struct chfs_flash_data_node *dnode = buf; 358 359 /* check crc */ 360 crc = crc32(0, (uint8_t *)dnode, sizeof(struct chfs_flash_data_node) - 4); 361 if (crc != le32toh(dnode->node_crc)) { 362 err = chfs_update_eb_dirty(chmp, cheb, le32toh(dnode->length)); 363 if (err) 364 return err; 365 return CHFS_NODE_BADCRC; 366 } 367 /* 368 * Don't check data nodes crc and version here, it will be done in 369 * the background GC thread. 370 */ 371 nref = chfs_alloc_node_ref(cheb); 372 if (!nref) 373 return ENOMEM; 374 375 nref->nref_offset = CHFS_GET_OFS(ofs) | CHFS_UNCHECKED_NODE_MASK; 376 377 KASSERT(nref->nref_lnr == cheb->lnr); 378 379 vno = le64toh(dnode->vno); 380 mutex_enter(&chmp->chm_lock_vnocache); 381 vc = chfs_vnode_cache_get(chmp, vno); 382 if (!vc) { 383 vc = chfs_scan_make_vnode_cache(chmp, vno); 384 if (!vc) 385 return ENOMEM; 386 } 387 chfs_add_node_to_list(chmp, vc, nref, &vc->dnode); 388 mutex_exit(&chmp->chm_lock_vnocache); 389 390 dbg("chmpfree: %u, chebfree: %u, dnode: %u\n", chmp->chm_free_size, cheb->free_size, dnode->length); 391 392 /* update sizes */ 393 mutex_enter(&chmp->chm_lock_sizes); 394 chfs_change_size_free(chmp, cheb, -dnode->length); 395 chfs_change_size_unchecked(chmp, cheb, dnode->length); 396 mutex_exit(&chmp->chm_lock_sizes); 397 return CHFS_NODE_OK; 398 } 399 400 /* chfs_scan_classify_cheb - determine eraseblock's state */ 401 int 402 chfs_scan_classify_cheb(struct chfs_mount *chmp, 403 struct chfs_eraseblock *cheb) 404 { 405 if (cheb->free_size == chmp->chm_ebh->eb_size) 406 return CHFS_BLK_STATE_FREE; 407 else if (cheb->dirty_size < MAX_DIRTY_TO_CLEAN) 408 return CHFS_BLK_STATE_CLEAN; 409 else if (cheb->used_size || cheb->unchecked_size) 410 return CHFS_BLK_STATE_PARTDIRTY; 411 else 412 return CHFS_BLK_STATE_ALLDIRTY; 413 } 414 415 416 /* 417 * chfs_scan_eraseblock - scans an eraseblock and looking for nodes 418 * 419 * This function scans a whole eraseblock, checks the nodes on it and add them 420 * to the vnode cache. 421 * Returns eraseblock state on success, error code if fails. 422 */ 423 int 424 chfs_scan_eraseblock(struct chfs_mount *chmp, 425 struct chfs_eraseblock *cheb) 426 { 427 int err; 428 size_t len, retlen; 429 off_t ofs = 0; 430 int lnr = cheb->lnr; 431 u_char *buf; 432 struct chfs_flash_node_hdr *nhdr; 433 int read_free = 0; 434 struct chfs_node_ref *nref; 435 436 dbg("scanning eraseblock content: %d free_size: %d\n", cheb->lnr, cheb->free_size); 437 dbg("scanned physical block: %d\n", chmp->chm_ebh->lmap[lnr]); 438 buf = kmem_alloc(CHFS_MAX_NODE_SIZE, KM_SLEEP); 439 440 while((ofs + CHFS_NODE_HDR_SIZE) < chmp->chm_ebh->eb_size) { 441 memset(buf, 0 , CHFS_MAX_NODE_SIZE); 442 err = chfs_read_leb(chmp, 443 lnr, buf, ofs, CHFS_NODE_HDR_SIZE, &retlen); 444 if (err) 445 goto err_return; 446 447 if (retlen != CHFS_NODE_HDR_SIZE) { 448 chfs_err("Error reading node header: " 449 "read: %zu instead of: %zu\n", 450 CHFS_NODE_HDR_SIZE, retlen); 451 err = EIO; 452 goto err_return; 453 } 454 455 /* first we check if the buffer we read is full with 0xff, if yes maybe 456 * the blocks remaining area is free. We increase read_free and if it 457 * reaches MAX_READ_FREE we stop reading the block */ 458 if (check_pattern(buf, 0xff, 0, CHFS_NODE_HDR_SIZE)) { 459 read_free += CHFS_NODE_HDR_SIZE; 460 if (read_free >= MAX_READ_FREE(chmp)) { 461 dbg("rest of the block is free. Size: %d\n", cheb->free_size); 462 kmem_free(buf, CHFS_MAX_NODE_SIZE); 463 return chfs_scan_classify_cheb(chmp, cheb); 464 } 465 ofs += CHFS_NODE_HDR_SIZE; 466 continue; 467 } else { 468 chfs_update_eb_dirty(chmp, cheb, read_free); 469 read_free = 0; 470 } 471 472 nhdr = (struct chfs_flash_node_hdr *)buf; 473 474 err = chfs_scan_check_node_hdr(nhdr); 475 if (err) { 476 dbg("node hdr error\n"); 477 err = chfs_update_eb_dirty(chmp, cheb, 4); 478 if (err) 479 goto err_return; 480 481 ofs += 4; 482 continue; 483 } 484 ofs += CHFS_NODE_HDR_SIZE; 485 if (ofs > chmp->chm_ebh->eb_size) { 486 chfs_err("Second part of node is on the next eraseblock.\n"); 487 err = EIO; 488 goto err_return; 489 } 490 switch (le16toh(nhdr->type)) { 491 case CHFS_NODETYPE_VNODE: 492 /* vnode information */ 493 /* read up the node */ 494 len = le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE; 495 err = chfs_read_leb(chmp, 496 lnr, buf + CHFS_NODE_HDR_SIZE, 497 ofs, len, &retlen); 498 if (err) 499 goto err_return; 500 501 if (retlen != len) { 502 chfs_err("Error reading vnode: read: %zu instead of: %zu\n", 503 len, retlen); 504 err = EIO; 505 goto err_return; 506 } 507 KASSERT(lnr == cheb->lnr); 508 err = chfs_scan_check_vnode(chmp, 509 cheb, buf, ofs - CHFS_NODE_HDR_SIZE); 510 if (err) 511 goto err_return; 512 513 break; 514 case CHFS_NODETYPE_DIRENT: 515 /* directory entry */ 516 /* read up the node */ 517 len = le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE; 518 519 err = chfs_read_leb(chmp, 520 lnr, buf + CHFS_NODE_HDR_SIZE, 521 ofs, len, &retlen); 522 if (err) 523 goto err_return; 524 525 if (retlen != len) { 526 chfs_err("Error reading dirent node: read: %zu " 527 "instead of: %zu\n", len, retlen); 528 err = EIO; 529 goto err_return; 530 } 531 532 KASSERT(lnr == cheb->lnr); 533 534 err = chfs_scan_check_dirent_node(chmp, 535 cheb, buf, ofs - CHFS_NODE_HDR_SIZE); 536 if (err) 537 goto err_return; 538 539 break; 540 case CHFS_NODETYPE_DATA: 541 /* data node */ 542 len = sizeof(struct chfs_flash_data_node) - 543 CHFS_NODE_HDR_SIZE; 544 err = chfs_read_leb(chmp, 545 lnr, buf + CHFS_NODE_HDR_SIZE, 546 ofs, len, &retlen); 547 if (err) 548 goto err_return; 549 550 if (retlen != len) { 551 chfs_err("Error reading data node: read: %zu " 552 "instead of: %zu\n", len, retlen); 553 err = EIO; 554 goto err_return; 555 } 556 KASSERT(lnr == cheb->lnr); 557 err = chfs_scan_check_data_node(chmp, 558 cheb, buf, ofs - CHFS_NODE_HDR_SIZE); 559 if (err) 560 goto err_return; 561 562 break; 563 case CHFS_NODETYPE_PADDING: 564 /* padding node, set size and update dirty */ 565 nref = chfs_alloc_node_ref(cheb); 566 nref->nref_offset = ofs - CHFS_NODE_HDR_SIZE; 567 nref->nref_offset = CHFS_GET_OFS(nref->nref_offset) | 568 CHFS_OBSOLETE_NODE_MASK; 569 570 err = chfs_update_eb_dirty(chmp, cheb, 571 le32toh(nhdr->length)); 572 if (err) 573 goto err_return; 574 575 break; 576 default: 577 /* unknown node type, update dirty and skip */ 578 err = chfs_update_eb_dirty(chmp, cheb, 579 le32toh(nhdr->length)); 580 if (err) 581 goto err_return; 582 583 break; 584 } 585 ofs += le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE; 586 } 587 588 KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + 589 cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size); 590 591 err = chfs_scan_classify_cheb(chmp, cheb); 592 /* FALLTHROUGH */ 593 err_return: 594 kmem_free(buf, CHFS_MAX_NODE_SIZE); 595 return err; 596 } 597