File: vmfs_bitmap.c

package info (click to toggle)
vmfs6-tools 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 548 kB
  • sloc: ansic: 6,959; sh: 80; makefile: 9
file content (563 lines) | stat: -rw-r--r-- 15,484 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 * vmfs-tools - Tools to access VMFS filesystems
 * Copyright (C) 2009 Christophe Fillot <cf@utc.fr>
 * Copyright (C) 2009 Mike Hommey <mh@glandium.org>
 * Copyright (C) 2018 Weafon Tsao <weafon.tsao@accelstor.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/* 
 * VMFS bitmaps.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>

#include "utils.h"
#include "vmfs.h"

/* Read a bitmap header */
int vmfs_bmh_read(vmfs_bitmap_header_t *bmh,const u_char *buf)
{
   bmh->items_per_bitmap_entry = read_le32(buf,0x0);
   bmh->bmp_entries_per_area   = read_le32(buf,0x4);
   bmh->hdr_size               = read_le32(buf,0x8);
   bmh->data_size              = read_le32(buf,0xc);
   bmh->area_size              = read_le32(buf,0x10);
   bmh->total_items            = read_le32(buf,0x14);
   bmh->area_count             = read_le32(buf,0x18);
   return(0);
}

/* Write a bitmap header */
int vmfs_bmh_write(const vmfs_bitmap_header_t *bmh,u_char *buf)
{
   write_le32(buf,0x0,bmh->items_per_bitmap_entry);
   write_le32(buf,0x4,bmh->bmp_entries_per_area);
   write_le32(buf,0x8,bmh->hdr_size);
   write_le32(buf,0xc,bmh->data_size);
   write_le32(buf,0x10,bmh->area_size);
   write_le32(buf,0x14,bmh->total_items);
   write_le32(buf,0x18,bmh->area_count);
   return(0);
}

/* Read a bitmap entry */
int vmfs_bme_read(vmfs_bitmap_entry_t *bme,const u_char *buf,int copy_bitmap)
{
   vmfs_metadata_hdr_read(&bme->mdh,buf);
   bme->id     = read_le32(buf,VMFS_BME_OFS_ID);
   bme->total  = read_le32(buf,VMFS_BME_OFS_TOTAL);
   bme->free   = read_le32(buf,VMFS_BME_OFS_FREE);
   bme->ffree  = read_le32(buf,VMFS_BME_OFS_FFREE);

   if (copy_bitmap) {
      memcpy(bme->bitmap,&buf[VMFS_BME_OFS_BITMAP],(bme->total+7)/8);
   }

   return(0);
}

/* Write a bitmap entry */
int vmfs_bme_write(const vmfs_bitmap_entry_t *bme,u_char *buf)
{
   vmfs_metadata_hdr_write(&bme->mdh,buf);
   write_le32(buf,VMFS_BME_OFS_ID,bme->id);
   write_le32(buf,VMFS_BME_OFS_TOTAL,bme->total);
   write_le32(buf,VMFS_BME_OFS_FREE,bme->free);
   write_le32(buf,VMFS_BME_OFS_FFREE,bme->ffree);
   memcpy(&buf[VMFS_BME_OFS_BITMAP],bme->bitmap,(bme->total+7)/8);
   return(0);
}

/* Update a bitmap entry on disk */
int vmfs_bme_update(const vmfs_fs_t *fs,const vmfs_bitmap_entry_t *bme)
{
   DECL_ALIGNED_BUFFER(buf,VMFS_BITMAP_ENTRY_SIZE);

   memset(buf,0,VMFS_BITMAP_ENTRY_SIZE);
   vmfs_bme_write(bme,buf);

   if (vmfs_device_write(fs->dev,bme->mdh.pos,buf,buf_len) != buf_len)
      return(-1);

   return(0);
}

/* Get number of items per area */
static inline u_int
vmfs_bitmap_get_items_per_area(const vmfs_bitmap_header_t *bmh)
{
   return(bmh->bmp_entries_per_area * bmh->items_per_bitmap_entry);
}

/* Get address of a given area (pointing to bitmap array) */
static inline off_t
vmfs_bitmap_get_area_addr(const vmfs_bitmap_header_t *bmh,u_int area)
{
   return(bmh->hdr_size + (area * bmh->area_size));
}

/* Read a bitmap entry given a block id */
int vmfs_bitmap_get_entry(vmfs_bitmap_t *b,uint32_t entry,uint32_t item,
                          vmfs_bitmap_entry_t *bmp_entry)
{   
   DECL_ALIGNED_BUFFER(buf,VMFS_BITMAP_ENTRY_SIZE);
   uint32_t items_per_area;
   u_int entry_idx,area;
   off_t addr;

   addr = (entry * b->bmh.items_per_bitmap_entry) + item;

   items_per_area = vmfs_bitmap_get_items_per_area(&b->bmh);
   area = addr / items_per_area;

   entry_idx = (addr % items_per_area) / b->bmh.items_per_bitmap_entry;

   addr = vmfs_bitmap_get_area_addr(&b->bmh,area);
   addr += entry_idx * VMFS_BITMAP_ENTRY_SIZE;

   if (vmfs_file_pread(b->f,buf,buf_len,addr) != buf_len)
      return(-1);

   vmfs_bme_read(bmp_entry,buf,1);
   return(0);
}

/* Get position of an item */
off_t vmfs_bitmap_get_item_pos(vmfs_bitmap_t *b,uint32_t entry,uint32_t item)
{
   off_t pos;
   uint32_t addr;
   uint32_t items_per_area;
   u_int area;

   addr = (entry * b->bmh.items_per_bitmap_entry) + item;

   items_per_area = vmfs_bitmap_get_items_per_area(&b->bmh);
   area = addr / items_per_area;

   pos  = b->bmh.hdr_size + (area * b->bmh.area_size);
   pos += b->bmh.bmp_entries_per_area * VMFS_BITMAP_ENTRY_SIZE; 
   pos += (addr % items_per_area) * b->bmh.data_size;

   return(pos);
}

/* Read a bitmap item from its entry and item numbers */
bool vmfs_bitmap_get_item(vmfs_bitmap_t *b, uint32_t entry, uint32_t item,
                          u_char *buf)
{
   off_t pos = vmfs_bitmap_get_item_pos(b,entry,item);
   dprintf("called data_size 0x%x pos 0x%08lx\n", b->bmh.data_size, pos);   
   return(vmfs_file_pread(b->f,buf,b->bmh.data_size,pos) == b->bmh.data_size);
}

/* Write a bitmap given its entry and item numbers */
bool vmfs_bitmap_set_item(vmfs_bitmap_t *b,uint32_t entry,uint32_t item,
                          u_char *buf)
{
   off_t pos = vmfs_bitmap_get_item_pos(b,entry,item);
   return(vmfs_file_pwrite(b->f,buf,b->bmh.data_size,pos) == b->bmh.data_size);
}

/* Get offset of an item in a bitmap entry */
static void 
vmfs_bitmap_get_item_offset(const vmfs_bitmap_header_t *bmh,u_int addr,
                            u_int *array_idx,u_int *bit_idx)
{
   u_int idx;

   idx = addr % bmh->items_per_bitmap_entry;
   *array_idx = idx >> 3;
   *bit_idx   = idx & 0x07;
}

/* Update the first free item field */
static void vmfs_bitmap_update_ffree(vmfs_bitmap_entry_t *entry)
{
   u_int array_idx,bit_idx;
   int i;

   entry->ffree = 0;

   for(i=0;i<entry->total;i++) {
      array_idx = i >> 3;
      bit_idx   = i & 0x07;

      if (entry->bitmap[array_idx] & (1 << bit_idx)) {
         entry->ffree = i;
         break;
      }
   }
}

/* Mark an item as free or allocated */
int vmfs_bitmap_set_item_status(const vmfs_bitmap_header_t *bmh,
                                vmfs_bitmap_entry_t *bmp_entry,
                                uint32_t entry,uint32_t item,
                                int status)
{
   u_int array_idx,bit_idx;
   u_int bit_mask;
   uint32_t addr;

   addr = (entry * bmh->items_per_bitmap_entry) + item;

   vmfs_bitmap_get_item_offset(bmh,addr,&array_idx,&bit_idx);
   bit_mask = 1 << bit_idx;

   if (status == 0) {
      /* item is already freed */
      if (bmp_entry->bitmap[array_idx] & bit_mask)
         return(-1);

      bmp_entry->bitmap[array_idx] |= bit_mask;
      bmp_entry->free++;
   } else {
      /* item is already allocated */
      if (!(bmp_entry->bitmap[array_idx] & bit_mask))
         return(-1);
      
      bmp_entry->bitmap[array_idx] &= ~bit_mask;
      bmp_entry->free--;
   }

   vmfs_bitmap_update_ffree(bmp_entry);
   return(0);
}

/* Get the status of an item (0=free,1=allocated) */
int vmfs_bitmap_get_item_status(const vmfs_bitmap_header_t *bmh,
                                vmfs_bitmap_entry_t *bmp_entry,
                                uint32_t entry,uint32_t item)
{
   u_int array_idx,bit_idx;
   u_int bit_mask;
   uint32_t addr;

   addr = (entry * bmh->items_per_bitmap_entry) + item;

   vmfs_bitmap_get_item_offset(bmh,addr,&array_idx,&bit_idx);
   bit_mask = 1 << bit_idx;

   return((bmp_entry->bitmap[array_idx] & bit_mask) ? 0 : 1);
}

/* Find a free item in a bitmap entry and mark it allocated */
int vmfs_bitmap_alloc_item(vmfs_bitmap_entry_t *bmp_entry,uint32_t *item)
{
   u_int array_idx,bit_idx;
   int i;

   /* TODO: use first free field as a hint */

   for(i=0;i<bmp_entry->total;i++) {
      array_idx = i >> 3;
      bit_idx   = i & 0x07;

      if (bmp_entry->bitmap[array_idx] & (1 << bit_idx)) {
         *item = i;
         bmp_entry->bitmap[array_idx] &= ~(1 << bit_idx);
         bmp_entry->free--;
         vmfs_bitmap_update_ffree(bmp_entry);
         return(0);
      }
   }

   return(-1);
}

/* Find a bitmap entry with at least "num_items" free in the specified area */
int vmfs_bitmap_area_find_free_items(vmfs_bitmap_t *b,
                                     u_int area,u_int num_items,
                                     vmfs_bitmap_entry_t *entry)
{
   vmfs_fs_t *fs;
   u_char *buf,*ptr;
   size_t buf_len;
   off_t pos;
   int res = -1;
   int i;

   fs = (vmfs_fs_t *)vmfs_file_get_fs(b->f);
   pos = vmfs_bitmap_get_area_addr(&b->bmh,area);
   buf_len = b->bmh.bmp_entries_per_area * VMFS_BITMAP_ENTRY_SIZE;

   if (!(buf = iobuffer_alloc(buf_len)))
      return(-1);

   if (vmfs_file_pread(b->f,buf,buf_len,pos) != buf_len)
      goto done;

   for(i=0;i<b->bmh.bmp_entries_per_area;i++) {
      ptr = buf + (i * VMFS_BITMAP_ENTRY_SIZE);
      vmfs_bme_read(entry,ptr,1);

      if (vmfs_metadata_is_locked(&entry->mdh) || (entry->free < num_items))
         continue;

      /* We now have to re-read the bitmap entry with the reservation taken */
      if (!vmfs_metadata_lock(fs,entry->mdh.pos,
                              ptr,VMFS_BITMAP_ENTRY_SIZE,
                              &entry->mdh))
      {      
         vmfs_bme_read(entry,ptr,1);

         if (entry->free < num_items) {
            vmfs_metadata_unlock(fs,&entry->mdh);
            continue;
         }

         res = 0;
         break;
      }
   }

 done:
   iobuffer_free(buf);
   return(res);
}

/* Find a bitmap entry with at least "num_items" free (scan all areas) */
int vmfs_bitmap_find_free_items(vmfs_bitmap_t *b,u_int num_items,
                                vmfs_bitmap_entry_t *entry)
{
   u_int i;

   for(i=0;i<b->bmh.area_count;i++)
      if (!vmfs_bitmap_area_find_free_items(b,i,num_items,entry))
         return(0);

   return(-1);
}

/* Count the total number of allocated items in a bitmap area */
uint32_t vmfs_bitmap_area_allocated_items(vmfs_bitmap_t *b,u_int area)
{
   u_char buf[VMFS_BITMAP_ENTRY_SIZE];
   vmfs_bitmap_entry_t entry;
   uint32_t count;
   off_t pos;
   int i;

   pos = vmfs_bitmap_get_area_addr(&b->bmh,area);

   for(i=0,count=0;i<b->bmh.bmp_entries_per_area;i++) {
      if (vmfs_file_pread(b->f,buf,sizeof(buf),pos) != sizeof(buf))
         break;

      vmfs_bme_read(&entry,buf,0);
      count += entry.total - entry.free;
      pos += sizeof(buf);
   }

   return count;
}

/* Count the total number of allocated items in a bitmap */
uint32_t vmfs_bitmap_allocated_items(vmfs_bitmap_t *b)
{
   uint32_t count;
   u_int i;
   
   for(i=0,count=0;i<b->bmh.area_count;i++)
      count += vmfs_bitmap_area_allocated_items(b,i);

   return(count);
}

/* Call a user function for each allocated item in a bitmap */
void vmfs_bitmap_area_foreach(vmfs_bitmap_t *b,u_int area,
                              vmfs_bitmap_foreach_cbk_t cbk,
                              void *opt_arg)
{
   DECL_ALIGNED_BUFFER(buf,VMFS_BITMAP_ENTRY_SIZE);
   vmfs_bitmap_entry_t entry;
   off_t pos;
   uint32_t addr;
   u_int array_idx,bit_idx;
   u_int i,j;

   pos = vmfs_bitmap_get_area_addr(&b->bmh,area);

   for(i=0;i<b->bmh.bmp_entries_per_area;i++) {
      if (vmfs_file_pread(b->f,buf,buf_len,pos) != buf_len)
         break;

      vmfs_bme_read(&entry,buf,1);

      for(j=0;j<entry.total;j++) {
         array_idx = j >> 3;
         bit_idx   = j & 0x07;
         
         addr =  area * vmfs_bitmap_get_items_per_area(&b->bmh);
         addr += i * b->bmh.items_per_bitmap_entry;
         addr += j;

         if (!(entry.bitmap[array_idx] & (1 << bit_idx)))
            cbk(b,addr,opt_arg);
      }

      pos += buf_len;
   }
}

/* Call a user function for each allocated item in a bitmap */
void vmfs_bitmap_foreach(vmfs_bitmap_t *b,vmfs_bitmap_foreach_cbk_t cbk,
                         void *opt_arg)
{
   u_int i;
   
   for(i=0;i<b->bmh.area_count;i++)
      vmfs_bitmap_area_foreach(b,i,cbk,opt_arg);
}

/* Check coherency of a bitmap file */
int vmfs_bitmap_check(vmfs_bitmap_t *b)
{  
   u_char buf[VMFS_BITMAP_ENTRY_SIZE];
   vmfs_bitmap_entry_t entry;
   uint32_t total_items;
   uint32_t magic;
   uint32_t entry_id;
   int i,j,k,errors;
   int bmap_size;
   int bmap_count;
   off_t pos;

   errors      = 0;
   total_items = 0;
   magic       = 0;
   entry_id    = 0;

   for(i=0;i<b->bmh.area_count;i++) {
      pos = vmfs_bitmap_get_area_addr(&b->bmh,i);

      for(j=0;j<b->bmh.bmp_entries_per_area;j++) {
         if (vmfs_file_pread(b->f,buf,sizeof(buf),pos) != sizeof(buf))
            break;

         vmfs_bme_read(&entry,buf,0);

         if (entry.mdh.magic == 0)
            goto done;

         /* check the entry ID */
         if (entry.id != entry_id) {
            printf("Entry 0x%x has incorrect ID 0x%x\n",entry_id,entry.id);
            errors++;
         }
         
         /* check the magic number */
         if (magic == 0) {
            magic = entry.mdh.magic;
         } else {
            if (entry.mdh.magic != magic) {
               printf("Entry 0x%x has an incorrect magic id (0x%x)\n",
                      entry_id,entry.mdh.magic);
               errors++;
            }
         }
         
         /* check the number of items */
         if (entry.total > b->bmh.items_per_bitmap_entry) {
            printf("Entry 0x%x has an incorrect total of 0x%2.2x items\n",
                   entry_id,entry.total);
            errors++;
         }

         /* check the bitmap array */
         bmap_size = (entry.total + 7) / 8;
         bmap_count = 0;

         for(k=0;k<bmap_size;k++) {
            bmap_count += bit_count(buf[VMFS_BME_OFS_BITMAP+k]);
         }

         if (bmap_count != entry.free) {
            printf("Entry 0x%x has an incorrect bitmap array "
                   "(bmap_count=0x%x instead of 0x%x)\n",
                   entry_id,bmap_count,entry.free);
            errors++;
         }

         total_items += entry.total;
         entry_id++;
         pos += sizeof(buf);
      }
   }

 done:
   if (total_items != b->bmh.total_items) {
      printf("Total number of items (0x%x) doesn't match header info (0x%x)\n",
             total_items,b->bmh.total_items);
      errors++;
   }

   return(errors);
}


/* Open a bitmap file */
static inline vmfs_bitmap_t *vmfs_bitmap_open_from_file(vmfs_file_t *f)
{
   DECL_ALIGNED_BUFFER(buf,512);
   vmfs_bitmap_t *b;

   if (!f)
      return NULL;
   dprintf("called to file_pread\n");
   if (vmfs_file_pread(f,buf,buf_len,0) != buf_len) {
      dprintf("fail pread\n");
      vmfs_file_close(f);
      return NULL;
   }

   if (!(b = calloc(1, sizeof(vmfs_bitmap_t)))) {
      vmfs_file_close(f);
      return NULL;
   }
   //hexdump(buf, buf_len);
   vmfs_bmh_read(&b->bmh, buf);
   b->f = f;
   dprintf("leave\n");   
   return b;
}

vmfs_bitmap_t *vmfs_bitmap_open_at(vmfs_dir_t *d,const char *name)
{
   return vmfs_bitmap_open_from_file(vmfs_file_open_at(d, name));
}

vmfs_bitmap_t *vmfs_bitmap_open_from_inode(const vmfs_inode_t *inode)
{
   return vmfs_bitmap_open_from_file(vmfs_file_open_from_inode(inode));
}

vmfs_bitmap_t *vmfs_bitmap_open_from_host(const char *path)
{
   return vmfs_bitmap_open_from_file(vmfs_file_open_from_host(path));
}

/* Close a bitmap file */
void vmfs_bitmap_close(vmfs_bitmap_t *b)
{
   if (b != NULL) {
      vmfs_file_close(b->f);
      free(b);
   }
}