Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Functional file backend
[palacios-OLD.git] / palacios / src / devices / filedisk.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
22
23 #include <palacios/vmm_file.h>
24
25 #ifndef CONFIG_DEBUG_FILEDISK
26 #undef PrintDebug
27 #define PrintDebug(fmt, args...)
28 #endif
29
30 struct disk_state {
31     uint64_t capacity; // in bytes
32
33     int fd;
34 };
35
36
37
38 static int write_all(int fd, char * buf, int offset, int length) {
39     int bytes_written = 0;
40     
41     PrintDebug("Writing %d bytes\n", length - bytes_written);
42     while (bytes_written < length) {
43         int tmp_bytes = V3_FileWrite(fd, offset+bytes_written, buf + bytes_written, length - bytes_written);
44         PrintDebug("Wrote %d bytes\n", tmp_bytes);
45         
46         if (tmp_bytes <= 0 ) {
47             PrintError("Write failed\n");
48             return -1;
49         }
50         
51         bytes_written += tmp_bytes;
52     }
53     
54     return 0;
55 }
56
57
58 static int read_all(int fd, char * buf, int offset, int length) {
59     int bytes_read = 0;
60     
61     PrintDebug("Reading %d bytes\n", length - bytes_read);
62     while (bytes_read < length) {
63         int tmp_bytes = V3_FileRead(fd, offset+bytes_read, buf + bytes_read, length - bytes_read);
64         PrintDebug("Read %d bytes\n", tmp_bytes);
65         
66         if (tmp_bytes <= 0) {
67             PrintError("Read failed\n");
68             return -1;
69         }
70         
71         bytes_read += tmp_bytes;
72     }
73     
74     return 0;
75 }
76
77 static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
78     struct disk_state * disk = (struct disk_state *)private_data;
79
80     PrintDebug("Reading %d bytes from %p to %p\n", (uint32_t)num_bytes, (uint8_t *)(disk->disk_image + lba), buf);
81
82     return read_all(disk->fd, buf, lba, num_bytes);
83 }
84
85
86 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
87     struct disk_state * disk = (struct disk_state *)private_data;
88
89     PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes,  buf, (uint8_t *)(disk->disk_image + lba));
90
91     return write_all(disk->fd,  buf, lba, num_bytes);
92 }
93
94
95 static uint64_t get_capacity(void * private_data) {
96     struct disk_state * disk = (struct disk_state *)private_data;
97
98     PrintDebug("Querying RAMDISK capacity %d\n", 
99                (uint32_t)(disk->capacity));
100
101     return disk->capacity;
102 }
103
104 static struct v3_dev_blk_ops blk_ops = {
105     .read = read, 
106     .write = write,
107     .get_capacity = get_capacity,
108 };
109
110
111
112
113 static int disk_free(struct vm_device * dev) {
114     return 0;
115 }
116
117 static struct v3_device_ops dev_ops = {
118     .free = disk_free,
119     .reset = NULL,
120     .start = NULL,
121     .stop = NULL,
122 };
123
124
125
126
127 static int disk_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
128     struct disk_state * disk = NULL;
129     char * path = v3_cfg_val(cfg, "path");
130     char * dev_id = v3_cfg_val(cfg, "ID");
131     char * filename = v3_cfg_val(cfg, "file");
132
133
134     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
135
136     disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
137     memset(disk, 0, sizeof(struct disk_state));
138
139
140     if (!path) {
141
142         PrintError("Missing path (%s) for %s\n", path, dev_id);
143         return -1;
144     }
145     
146     disk->fd = V3_FileOpen(path, 0);
147     disk->capacity = V3_FileSize(disk->fd);
148
149     PrintDebug("Registering FILEDISK %s (path=%s, fd=%lu, size=%lu)\n",
150                name, path, file->fd, file->capacity);
151
152     struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, disk);
153
154     if (v3_attach_device(vm, dev) == -1) {
155         PrintError("Could not attach device %s\n", dev_id);
156         return -1;
157     }
158
159     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
160                            &blk_ops, frontend_cfg, disk) == -1) {
161         PrintError("Could not connect %s to frontend %s\n", 
162                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
163         return -1;
164     }
165     
166
167     return 0;
168 }
169
170
171 device_register("FILEDISK", disk_init)