1    | /* disk_testkit.c
2    |    --------------
3    | 
4    |    Disk access functions used under Stewart Smith's Testkit.
5    | 
6    |    $Id: disk_testkit.c,v 1.7 2003/10/20 07:18:11 stewart Exp $
7    | 
8    |    (C)2003 Stewart Smith
9    |    Distributed under the GNU Public License
10   | 
11   | */
12   | 
13   | #include <stdio.h>
14   | #include <stdlib.h>
15   | #include "testkit/block_dev.h"
16   | #include "disk.h"
17   | 
18   | /* disk_new
19   |    --------
20   |    new disk object from testkit block device.
21   | */
22   | struct fcfs_disk* disk_new(struct block_device *bdev)
23   | {
24   |   struct fcfs_disk* disk;
25   | 
26   |   disk = (struct fcfs_disk*)malloc(sizeof(struct fcfs_disk));
27   |   disk->os_private = bdev;
28   |   disk->bsize = bdev->block_size;
29   |   disk->blocksnr = bdev->num_blocks;
30   |   disk->sb = NULL;
31   |   return disk;
32   | }
33   | 
34   | /* disk_free
35   |    ---------
36   |    Close and free the disk.
37   |  */
38   | struct fcfs_disk* disk_free(struct fcfs_disk* disk)
39   | {
40   |   block_dev_close(disk->os_private);
41   |   free(disk);
42   | }
43   | 
44   | /* Return an empty disk_block for the block. Does not read from disk */
45   | struct fcfs_disk_block* disk_newblock(struct fcfs_disk *disk, sector_t block)
46   | {
47   |   struct fcfs_disk_block* disk_block;
48   | 
49   |   disk_block = (struct fcfs_disk_block*)malloc(sizeof(struct fcfs_disk_block));
50   |   if(disk_block==NULL)
51   |     {
52   |       fprintf(stderr,"Unable to allocate disk_block\n");
53   |       abort();
54   |     }
55   |   disk_block->os_private = bnew(disk->os_private,block,disk->bsize);
56   |   disk_block->blocknr = block;
57   |   disk_block->bsize = disk->bsize;
58   |   disk_block->data = ((struct buffer_head*)(disk_block->os_private))->b_data;
59   |   ((struct buffer_head*)(disk_block->os_private))->b_count++;
60   |   disk_block->disk = disk;
61   |   
62   |   return disk_block;
63   | }
64   | 
65   | /* Allocate and return a disk block */
66   | struct fcfs_disk_block* disk_getblock(struct fcfs_disk *disk,sector_t block)
67   | {
68   |   struct fcfs_disk_block* disk_block;
69   |   
70   |   disk_block = (struct fcfs_disk_block*)malloc(sizeof(struct fcfs_disk_block));
71   |   if(disk_block==NULL)
72   |     {
73   |       fprintf(stderr,"Unable to allocate disk_block\n");
74   |       abort();
75   |     }
76   | 
77   |   //  fprintf(stderr,"DISK_GETBLOCK: 0x%llx\n",block);
78   | 
79   |   disk_block->os_private = bread(disk->os_private,block,disk->bsize);
80   |   disk_block->blocknr = block;
81   |   disk_block->bsize = disk->bsize;
82   |   disk_block->data = ((struct buffer_head*)(disk_block->os_private))->b_data;
83   |   ((struct buffer_head*)(disk_block->os_private))->b_count++;
84   |   disk_block->disk = disk;
85   | 
86   |   return disk_block;
87   | }
88   |