Revision 5fafdf24 block-cow.c

b/block-cow.c
1 1
/*
2 2
 * Block driver for the COW format
3
 * 
3
 *
4 4
 * Copyright (c) 2004 Fabrice Bellard
5
 * 
5
 *
6 6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 7
 * of this software and associated documentation files (the "Software"), to deal
8 8
 * in the Software without restriction, including without limitation the rights
......
56 56

  
57 57
    if (buf_size >= sizeof(struct cow_header_v2) &&
58 58
        be32_to_cpu(cow_header->magic) == COW_MAGIC &&
59
        be32_to_cpu(cow_header->version) == COW_VERSION) 
59
        be32_to_cpu(cow_header->version) == COW_VERSION)
60 60
        return 100;
61 61
    else
62 62
        return 0;
......
85 85
        be32_to_cpu(cow_header.version) != COW_VERSION) {
86 86
        goto fail;
87 87
    }
88
        
88
       
89 89
    /* cow image found */
90 90
    size = be64_to_cpu(cow_header.size);
91 91
    bs->total_sectors = size / 512;
92 92

  
93
    pstrcpy(bs->backing_file, sizeof(bs->backing_file), 
93
    pstrcpy(bs->backing_file, sizeof(bs->backing_file),
94 94
            cow_header.backing_file);
95
    
95
   
96 96
    /* mmap the bitmap */
97 97
    s->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
98
    s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size), 
99
                              s->cow_bitmap_size, 
98
    s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size),
99
                              s->cow_bitmap_size,
100 100
                              PROT_READ | PROT_WRITE,
101 101
                              MAP_SHARED, s->fd, 0);
102 102
    if (s->cow_bitmap_addr == MAP_FAILED)
......
143 143
    return changed;
144 144
}
145 145

  
146
static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num, 
146
static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num,
147 147
                            int nb_sectors, int *pnum)
148 148
{
149 149
    BDRVCowState *s = bs->opaque;
150 150
    return is_changed(s->cow_bitmap, sector_num, nb_sectors, pnum);
151 151
}
152 152

  
153
static int cow_read(BlockDriverState *bs, int64_t sector_num, 
153
static int cow_read(BlockDriverState *bs, int64_t sector_num,
154 154
                    uint8_t *buf, int nb_sectors)
155 155
{
156 156
    BDRVCowState *s = bs->opaque;
157 157
    int ret, n;
158
    
158
   
159 159
    while (nb_sectors > 0) {
160 160
        if (is_changed(s->cow_bitmap, sector_num, nb_sectors, &n)) {
161 161
            lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
162 162
            ret = read(s->fd, buf, n * 512);
163
            if (ret != n * 512) 
163
            if (ret != n * 512)
164 164
                return -1;
165 165
        } else {
166 166
            if (bs->backing_hd) {
......
179 179
    return 0;
180 180
}
181 181

  
182
static int cow_write(BlockDriverState *bs, int64_t sector_num, 
182
static int cow_write(BlockDriverState *bs, int64_t sector_num,
183 183
                     const uint8_t *buf, int nb_sectors)
184 184
{
185 185
    BDRVCowState *s = bs->opaque;
186 186
    int ret, i;
187
    
187
   
188 188
    lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
189 189
    ret = write(s->fd, buf, nb_sectors * 512);
190
    if (ret != nb_sectors * 512) 
190
    if (ret != nb_sectors * 512)
191 191
        return -1;
192 192
    for (i = 0; i < nb_sectors; i++)
193 193
        cow_set_bit(s->cow_bitmap, sector_num + i);
......
211 211
    if (flags)
212 212
        return -ENOTSUP;
213 213

  
214
    cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 
214
    cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
215 215
              0644);
216 216
    if (cow_fd < 0)
217 217
        return -1;

Also available in: Unified diff