block.c (04eeb8b6d68ed314746856c813ce6fa8bc1a95df) block.c (eda578e559879b1a6a85f924adf2942070ae7ec3)
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 128 unchanged lines hidden (view full) ---

137static void bdrv_register(BlockDriver *bdrv)
138{
139 if (!bdrv->bdrv_aio_read) {
140 /* add AIO emulation layer */
141 bdrv->bdrv_aio_read = bdrv_aio_read_em;
142 bdrv->bdrv_aio_write = bdrv_aio_write_em;
143 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
144 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 128 unchanged lines hidden (view full) ---

137static void bdrv_register(BlockDriver *bdrv)
138{
139 if (!bdrv->bdrv_aio_read) {
140 /* add AIO emulation layer */
141 bdrv->bdrv_aio_read = bdrv_aio_read_em;
142 bdrv->bdrv_aio_write = bdrv_aio_write_em;
143 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
144 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
145 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
145 } else if (!bdrv->bdrv_read) {
146 /* add synchronous IO emulation layer */
147 bdrv->bdrv_read = bdrv_read_em;
148 bdrv->bdrv_write = bdrv_write_em;
149 }
150 bdrv->next = first_drv;
151 first_drv = bdrv;
152}
153

--- 409 unchanged lines hidden (view full) ---

563{
564 BlockDriver *drv = bs->drv;
565
566 if (!drv)
567 return -ENOMEDIUM;
568 if (bdrv_check_request(bs, sector_num, nb_sectors))
569 return -EIO;
570
146 /* add synchronous IO emulation layer */
147 bdrv->bdrv_read = bdrv_read_em;
148 bdrv->bdrv_write = bdrv_write_em;
149 }
150 bdrv->next = first_drv;
151 first_drv = bdrv;
152}
153

--- 409 unchanged lines hidden (view full) ---

563{
564 BlockDriver *drv = bs->drv;
565
566 if (!drv)
567 return -ENOMEDIUM;
568 if (bdrv_check_request(bs, sector_num, nb_sectors))
569 return -EIO;
570
571 if (drv->bdrv_pread) {
572 int ret, len;
573 len = nb_sectors * 512;
574 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
575 if (ret < 0)
576 return ret;
577 else if (ret != len)
578 return -EINVAL;
579 else {
580 bs->rd_bytes += (unsigned) len;
581 bs->rd_ops ++;
582 return 0;
583 }
584 } else {
585 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
586 }
571 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
587}
588
589/* Return < 0 if error. Important errors are:
590 -EIO generic I/O error (may happen for all errors)
591 -ENOMEDIUM No media inserted.
592 -EINVAL Invalid sector number or nb_sectors
593 -EACCES Trying to write a read-only device
594*/
595int bdrv_write(BlockDriverState *bs, int64_t sector_num,
596 const uint8_t *buf, int nb_sectors)
597{
598 BlockDriver *drv = bs->drv;
599 if (!bs->drv)
600 return -ENOMEDIUM;
601 if (bs->read_only)
602 return -EACCES;
603 if (bdrv_check_request(bs, sector_num, nb_sectors))
604 return -EIO;
605
572}
573
574/* Return < 0 if error. Important errors are:
575 -EIO generic I/O error (may happen for all errors)
576 -ENOMEDIUM No media inserted.
577 -EINVAL Invalid sector number or nb_sectors
578 -EACCES Trying to write a read-only device
579*/
580int bdrv_write(BlockDriverState *bs, int64_t sector_num,
581 const uint8_t *buf, int nb_sectors)
582{
583 BlockDriver *drv = bs->drv;
584 if (!bs->drv)
585 return -ENOMEDIUM;
586 if (bs->read_only)
587 return -EACCES;
588 if (bdrv_check_request(bs, sector_num, nb_sectors))
589 return -EIO;
590
606 if (drv->bdrv_pwrite) {
607 int ret, len, count = 0;
608 len = nb_sectors * 512;
609 do {
610 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
611 if (ret < 0) {
612 printf("bdrv_write ret=%d\n", ret);
613 return ret;
614 }
615 count += ret;
616 buf += ret;
617 } while (count != len);
618 bs->wr_bytes += (unsigned) len;
619 bs->wr_ops ++;
620 return 0;
621 }
622 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
623}
624
591 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
592}
593
625static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
626 uint8_t *buf, int count1)
594int bdrv_pread(BlockDriverState *bs, int64_t offset,
595 void *buf, int count1)
627{
628 uint8_t tmp_buf[SECTOR_SIZE];
629 int len, nb_sectors, count;
630 int64_t sector_num;
631
632 count = count1;
633 /* first read to align to sector start */
634 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);

--- 26 unchanged lines hidden (view full) ---

661 if (count > 0) {
662 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
663 return -EIO;
664 memcpy(buf, tmp_buf, count);
665 }
666 return count1;
667}
668
596{
597 uint8_t tmp_buf[SECTOR_SIZE];
598 int len, nb_sectors, count;
599 int64_t sector_num;
600
601 count = count1;
602 /* first read to align to sector start */
603 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);

--- 26 unchanged lines hidden (view full) ---

630 if (count > 0) {
631 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
632 return -EIO;
633 memcpy(buf, tmp_buf, count);
634 }
635 return count1;
636}
637
669static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
670 const uint8_t *buf, int count1)
638int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
639 const void *buf, int count1)
671{
672 uint8_t tmp_buf[SECTOR_SIZE];
673 int len, nb_sectors, count;
674 int64_t sector_num;
675
676 count = count1;
677 /* first write to align to sector start */
678 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);

--- 31 unchanged lines hidden (view full) ---

710 memcpy(tmp_buf, buf, count);
711 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
712 return -EIO;
713 }
714 return count1;
715}
716
717/**
640{
641 uint8_t tmp_buf[SECTOR_SIZE];
642 int len, nb_sectors, count;
643 int64_t sector_num;
644
645 count = count1;
646 /* first write to align to sector start */
647 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);

--- 31 unchanged lines hidden (view full) ---

679 memcpy(tmp_buf, buf, count);
680 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
681 return -EIO;
682 }
683 return count1;
684}
685
686/**
718 * Read with byte offsets (needed only for file protocols)
719 */
720int bdrv_pread(BlockDriverState *bs, int64_t offset,
721 void *buf1, int count1)
722{
723 BlockDriver *drv = bs->drv;
724
725 if (!drv)
726 return -ENOMEDIUM;
727 if (bdrv_check_byte_request(bs, offset, count1))
728 return -EIO;
729
730 if (!drv->bdrv_pread)
731 return bdrv_pread_em(bs, offset, buf1, count1);
732 return drv->bdrv_pread(bs, offset, buf1, count1);
733}
734
735/**
736 * Write with byte offsets (needed only for file protocols)
737 */
738int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
739 const void *buf1, int count1)
740{
741 BlockDriver *drv = bs->drv;
742
743 if (!drv)
744 return -ENOMEDIUM;
745 if (bdrv_check_byte_request(bs, offset, count1))
746 return -EIO;
747
748 if (!drv->bdrv_pwrite)
749 return bdrv_pwrite_em(bs, offset, buf1, count1);
750 return drv->bdrv_pwrite(bs, offset, buf1, count1);
751}
752
753/**
754 * Truncate file to 'offset' bytes (needed only for file protocols)
755 */
756int bdrv_truncate(BlockDriverState *bs, int64_t offset)
757{
758 BlockDriver *drv = bs->drv;
759 if (!drv)
760 return -ENOMEDIUM;
761 if (!drv->bdrv_truncate)

--- 938 unchanged lines hidden ---
687 * Truncate file to 'offset' bytes (needed only for file protocols)
688 */
689int bdrv_truncate(BlockDriverState *bs, int64_t offset)
690{
691 BlockDriver *drv = bs->drv;
692 if (!drv)
693 return -ENOMEDIUM;
694 if (!drv->bdrv_truncate)

--- 938 unchanged lines hidden ---