qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [RFC] Block device size rounding


From: John Snow
Subject: Re: [Qemu-devel] [RFC] Block device size rounding
Date: Mon, 12 Oct 2015 11:56:04 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0


On 10/09/2015 11:01 PM, Peter Crosthwaite wrote:
> I have in interesting problem with SD cards, where if you pass a block
> device that is not multiple-of-512k size the last bit gets chopped off.
> The problem is the card can only report a 512kX size to the guest, so
> a significant rounding is needed one way or the other. The current
> round-down policy causes crashing boots because parts of my guest
> file-system are missing.
> 
> The below patch works around it, by changing to round-up and simply
> ignoring reads and writes past the end of the block device file.
> 
> What is the correct action here though? If the file is writeable should
> we just allow the device to extend its size? Is that possible already?
> Just zero-pad read-only?
> 

Read-only seems like an easy case of append zeroes.

Read-write ... well, we can't write-protect just half of a 512k block.
Forcibly extending the size might be the only viable solution.

I would almost suggest doing a sparse allocation for the remainder of
the block and don't extend the physical size until the first write to
that block, but then we have the strange situation where:

- QEMU may extend your image according to the device model, sometimes
- Or sometimes not.

People don't seem to like unpredictability much, so maybe just outright
extending the image is the best thing to do, because then it can be
documented.

I also suppose an interactive warning prompt wouldn't work either ("Hey,
this file is a pinch too small for this device, may I extend it y/n?"),
since there's no existing protocol for negotiating that sort of thing
and it might cause management layers to explode...

Probably just forcibly increasing the size on RW or refusing to use the
file altogether are probably the sane deterministic things we want.


> The same could be applied to pflash, where the device init barfs if the
> backing file is too small (the devices are inited of a constant size,
> not based on the block device size).
> 
> Requiring the user to pad files in a device dependent way is a little
> user-unfriendly.
> 
> Regards,
> Peter
> ---
>  hw/sd/sd.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index 3e2a451..539bb72 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -248,13 +248,18 @@ static const uint8_t sd_csd_rw_mask[16] = {
>      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
>  };
>  
> -static void sd_set_csd(SDState *sd, uint64_t size)
> +static uint64_t sd_set_csd(SDState *sd, uint64_t size)
>  {
> -    uint32_t csize = (size >> (CMULT_SHIFT + HWBLOCK_SHIFT)) - 1;
> -    uint32_t sectsize = (1 << (SECTOR_SHIFT + 1)) - 1;
> -    uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1;
> +    uint64_t actual_size;
>  
>      if (size <= 0x40000000) {        /* Standard Capacity SD */
> +        uint32_t sectsize = (1 << (SECTOR_SHIFT + 1)) - 1;
> +        uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1;
> +        uint32_t csize;
> +
> +        actual_size = ROUND_UP(size, 1 << (CMULT_SHIFT + HWBLOCK_SHIFT));
> +        csize = (actual_size >> (CMULT_SHIFT + HWBLOCK_SHIFT)) - 1;
> +
>          sd->csd[0] = 0x00;   /* CSD structure */
>          sd->csd[1] = 0x26;   /* Data read access-time-1 */
>          sd->csd[2] = 0x00;   /* Data read access-time-2 */
> @@ -281,7 +286,8 @@ static void sd_set_csd(SDState *sd, uint64_t size)
>          sd->csd[14] = 0x00;  /* File format group */
>          sd->csd[15] = (sd_crc7(sd->csd, 15) << 1) | 1;
>      } else {                 /* SDHC */
> -        size /= 512 * 1024;
> +        actual_size = ROUND_UP(size, 512 * 1024);
> +        size = actual_size / (512 * 1024);
>          size -= 1;
>          sd->csd[0] = 0x40;
>          sd->csd[1] = 0x0e;
> @@ -301,6 +307,7 @@ static void sd_set_csd(SDState *sd, uint64_t size)
>          sd->csd[15] = 0x00;
>          sd->ocr |= 1 << 30;     /* High Capacity SD Memory Card */
>      }
> +    return actual_size;
>  }
>  
>  static void sd_set_rca(SDState *sd)
> @@ -408,7 +415,7 @@ static void sd_reset(SDState *sd)
>      sd_set_ocr(sd);
>      sd_set_scr(sd);
>      sd_set_cid(sd);
> -    sd_set_csd(sd, size);
> +    size = sd_set_csd(sd, size);
>      sd_set_cardstatus(sd);
>      sd_set_sdstatus(sd);
>  
> 

-- 
—js



reply via email to

[Prev in Thread] Current Thread [Next in Thread]