Changeset 3a43785 in mainline for uspace/srv/bd/vbd/disk.c
- Timestamp:
- 2015-10-09T18:56:36Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 6a0db524
- Parents:
- 0ecfc62
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/bd/vbd/disk.c
r0ecfc62 r3a43785 54 54 static category_id_t part_cid; 55 55 56 static int vbds_disk_parts_add(vbds_disk_t *, label_t *); 57 static int vbds_disk_parts_remove(vbds_disk_t *); 58 56 59 static int vbds_bd_open(bd_srvs_t *, bd_srv_t *); 57 60 static int vbds_bd_close(bd_srv_t *); … … 242 245 list_append(&part->lparts, &vbds_parts); 243 246 247 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_part_add -> %p", part); 248 244 249 if (rpart != NULL) 245 250 *rpart = part; … … 259 264 lpart = part->lpart; 260 265 261 if (part->open_cnt > 0) 266 if (part->open_cnt > 0) { 267 log_msg(LOG_DEFAULT, LVL_NOTE, "part->open_cnt = %d", 268 part->open_cnt); 262 269 return EBUSY; 270 } 263 271 264 272 if (part->svc_id != 0) { … … 277 285 } 278 286 279 static void vbds_disk_cat_change_cb(void) 280 { 281 (void) vbds_disks_check_new(); 282 } 283 284 int vbds_disk_discovery_start(void) 285 { 286 int rc; 287 288 rc = loc_register_cat_change_cb(vbds_disk_cat_change_cb); 289 if (rc != EOK) { 290 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback " 291 "for disk discovery (%d).", rc); 292 return rc; 293 } 294 295 return vbds_disks_check_new(); 296 } 297 298 int vbds_disk_add(service_id_t sid) 299 { 300 label_t *label = NULL; 287 /** Remove all disk partitions from our inventory leaving only the underlying 288 * liblabel partition structures. */ 289 static int vbds_disk_parts_add(vbds_disk_t *disk, label_t *label) 290 { 301 291 label_part_t *part; 302 vbds_disk_t *disk = NULL; 303 bool block_inited = false; 304 size_t block_size; 305 int rc; 306 307 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_disk_add(%zu)", sid); 308 309 /* Check for duplicates */ 310 rc = vbds_disk_by_svcid(sid, &disk); 311 if (rc == EOK) 312 return EEXISTS; 313 314 disk = calloc(1, sizeof(vbds_disk_t)); 315 if (disk == NULL) 316 return ENOMEM; 317 318 rc = loc_service_get_name(sid, &disk->svc_name); 319 if (rc != EOK) { 320 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting disk service name."); 321 rc = EIO; 322 goto error; 323 } 324 325 log_msg(LOG_DEFAULT, LVL_NOTE, "block_init(%zu)", sid); 326 rc = block_init(EXCHANGE_SERIALIZE, sid, 2048); 327 if (rc != EOK) { 328 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening block device %s.", 329 disk->svc_name); 330 rc = EIO; 331 goto error; 332 } 333 334 rc = block_get_bsize(sid, &block_size); 335 if (rc != EOK) { 336 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting block size of %s.", 337 disk->svc_name); 338 rc = EIO; 339 goto error; 340 } 341 342 block_inited = true; 343 344 rc = label_open(sid, &label); 345 if (rc != EOK) { 346 log_msg(LOG_DEFAULT, LVL_NOTE, "Label in disk %s not recognized.", 347 disk->svc_name); 348 rc = EIO; 349 goto error; 350 } 351 352 disk->svc_id = sid; 353 disk->label = label; 354 disk->block_size = block_size; 355 356 list_initialize(&disk->parts); 357 list_append(&disk->ldisks, &vbds_disks); 358 359 log_msg(LOG_DEFAULT, LVL_NOTE, "Recognized disk label. Adding partitions."); 292 int rc; 360 293 361 294 part = label_part_first(label); … … 365 298 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding partition " 366 299 "(disk %s)", disk->svc_name); 300 return rc; 367 301 } 368 302 … … 370 304 } 371 305 306 return EOK; 307 } 308 309 /** Remove all disk partitions from our inventory leaving only the underlying 310 * liblabel partition structures. */ 311 static int vbds_disk_parts_remove(vbds_disk_t *disk) 312 { 313 link_t *link; 314 vbds_part_t *part; 315 int rc; 316 317 link = list_first(&disk->parts); 318 while (link != NULL) { 319 part = list_get_instance(link, vbds_part_t, ldisk); 320 rc = vbds_part_remove(part, NULL); 321 if (rc != EOK) 322 return rc; 323 324 link = list_first(&disk->parts); 325 } 326 327 return EOK; 328 } 329 330 static void vbds_disk_cat_change_cb(void) 331 { 332 (void) vbds_disks_check_new(); 333 } 334 335 int vbds_disk_discovery_start(void) 336 { 337 int rc; 338 339 rc = loc_register_cat_change_cb(vbds_disk_cat_change_cb); 340 if (rc != EOK) { 341 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback " 342 "for disk discovery (%d).", rc); 343 return rc; 344 } 345 346 return vbds_disks_check_new(); 347 } 348 349 int vbds_disk_add(service_id_t sid) 350 { 351 label_t *label = NULL; 352 vbds_disk_t *disk = NULL; 353 bool block_inited = false; 354 size_t block_size; 355 int rc; 356 357 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_disk_add(%zu)", sid); 358 359 /* Check for duplicates */ 360 rc = vbds_disk_by_svcid(sid, &disk); 361 if (rc == EOK) 362 return EEXISTS; 363 364 disk = calloc(1, sizeof(vbds_disk_t)); 365 if (disk == NULL) 366 return ENOMEM; 367 368 rc = loc_service_get_name(sid, &disk->svc_name); 369 if (rc != EOK) { 370 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting disk service name."); 371 rc = EIO; 372 goto error; 373 } 374 375 log_msg(LOG_DEFAULT, LVL_NOTE, "block_init(%zu)", sid); 376 rc = block_init(EXCHANGE_SERIALIZE, sid, 2048); 377 if (rc != EOK) { 378 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening block device %s.", 379 disk->svc_name); 380 rc = EIO; 381 goto error; 382 } 383 384 rc = block_get_bsize(sid, &block_size); 385 if (rc != EOK) { 386 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting block size of %s.", 387 disk->svc_name); 388 rc = EIO; 389 goto error; 390 } 391 392 block_inited = true; 393 394 rc = label_open(sid, &label); 395 if (rc != EOK) { 396 log_msg(LOG_DEFAULT, LVL_NOTE, "Failed to open label in disk %s.", 397 disk->svc_name); 398 rc = EIO; 399 goto error; 400 } 401 402 disk->svc_id = sid; 403 disk->label = label; 404 disk->block_size = block_size; 405 406 list_initialize(&disk->parts); 407 list_append(&disk->ldisks, &vbds_disks); 408 409 log_msg(LOG_DEFAULT, LVL_NOTE, "Recognized disk label. Adding partitions."); 410 411 (void) vbds_disk_parts_add(disk, label); 372 412 return EOK; 373 413 error: … … 393 433 if (rc != EOK) 394 434 return rc; 435 436 rc = vbds_disk_parts_remove(disk); 437 if (rc != EOK) { 438 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed removing disk."); 439 return rc; 440 } 395 441 396 442 list_remove(&disk->ldisks); … … 485 531 } 486 532 487 488 533 int vbds_label_create(service_id_t sid, label_type_t ltype) 489 534 { 490 535 label_t *label; 491 536 vbds_disk_t *disk; 492 bool block_inited = false;493 size_t block_size;494 537 int rc; 495 538 496 539 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu)", sid); 497 540 498 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu) - chkdup", sid); 499 500 /* Check for duplicates */ 541 /* Find disk */ 501 542 rc = vbds_disk_by_svcid(sid, &disk); 502 if (rc == EOK) 503 return EEXISTS; 504 505 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu) - alloc", sid); 506 507 disk = calloc(1, sizeof(vbds_disk_t)); 508 if (disk == NULL) 509 return ENOMEM; 510 511 rc = loc_service_get_name(sid, &disk->svc_name); 512 if (rc != EOK) { 513 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting disk service name."); 514 rc = EIO; 515 goto error; 516 } 517 518 log_msg(LOG_DEFAULT, LVL_NOTE, "block_init(%zu)", sid); 519 rc = block_init(EXCHANGE_SERIALIZE, sid, 2048); 520 if (rc != EOK) { 521 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening block device %s.", 522 disk->svc_name); 523 rc = EIO; 524 goto error; 525 } 526 527 rc = block_get_bsize(sid, &block_size); 528 if (rc != EOK) { 529 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting block size of %s.", 530 disk->svc_name); 531 rc = EIO; 532 goto error; 533 } 534 535 block_inited = true; 543 if (rc != EOK) 544 return rc; 545 546 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu) - label_close", sid); 547 548 /* Close dummy label first */ 549 rc = vbds_disk_parts_remove(disk); 550 if (rc != EOK) { 551 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed removing disk."); 552 return rc; 553 } 554 555 label_close(disk->label); 556 disk->label = NULL; 536 557 537 558 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu) - label_create", sid); … … 541 562 goto error; 542 563 543 disk->svc_id = sid;564 (void) vbds_disk_parts_add(disk, label); 544 565 disk->label = label; 545 disk->block_size = block_size;546 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create: block_size=%zu",547 block_size);548 list_initialize(&disk->parts);549 550 list_append(&disk->ldisks, &vbds_disks);551 566 552 567 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu) - success", sid); … … 554 569 error: 555 570 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_label_create(%zu) - failure", sid); 556 if (block_inited) { 557 log_msg(LOG_DEFAULT, LVL_NOTE, "block_fini(%zu)", sid); 558 block_fini(sid); 559 } 560 if (disk != NULL) 561 free(disk->svc_name); 562 free(disk); 571 if (disk->label == NULL) { 572 rc = label_open(sid, &label); 573 if (rc != EOK) { 574 log_msg(LOG_DEFAULT, LVL_NOTE, "Failed to open label in disk %s.", 575 disk->svc_name); 576 } 577 } 578 563 579 return rc; 564 580 } … … 567 583 { 568 584 vbds_disk_t *disk; 585 label_t *label; 569 586 int rc; 570 587 … … 575 592 return rc; 576 593 594 rc = vbds_disk_parts_remove(disk); 595 if (rc != EOK) { 596 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed deleting label."); 597 return rc; 598 } 599 577 600 rc = label_destroy(disk->label); 578 601 if (rc != EOK) { … … 581 604 } 582 605 583 list_remove(&disk->ldisks); 584 log_msg(LOG_DEFAULT, LVL_NOTE, "block_fini(%zu)", sid); 585 block_fini(sid); 586 free(disk); 606 disk->label = NULL; 607 608 rc = label_open(disk->svc_id, &label); 609 if (rc != EOK) { 610 log_msg(LOG_DEFAULT, LVL_NOTE, "Failed to open label in disk %s.", 611 disk->svc_name); 612 return EIO; 613 } 614 615 (void) vbds_disk_parts_add(disk, label); 616 disk->label = label; 587 617 return EOK; 588 618 } … … 897 927 { 898 928 int rc; 929 930 log_msg(LOG_DEFAULT, LVL_NOTE, "vbds_part_svc_unregister(" 931 "disk->svc_name='%s', id=%zu)", part->disk->svc_name, part->svc_id); 899 932 900 933 rc = loc_service_unregister(part->svc_id);
Note:
See TracChangeset
for help on using the changeset viewer.