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.


f7fe7ec8c699061025ebb54f55844e4048e79c5b
[palacios.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 <interfaces/vmm_file.h>
24 #include <palacios/vm_guest.h>
25
26 #ifndef V3_CONFIG_DEBUG_FILEDISK
27 #undef PrintDebug
28 #define PrintDebug(fmt, args...)
29 #endif
30
31 struct disk_state {
32     uint64_t capacity; // in bytes
33
34     v3_file_t fd;
35 };
36
37
38
39 static int write_all(v3_file_t fd, char * buf, int offset, int length) {
40     int bytes_written = 0;
41     
42     PrintDebug("Writing %d bytes\n", length - bytes_written);
43     while (bytes_written < length) {
44         int tmp_bytes = v3_file_write(fd, buf + bytes_written, length - bytes_written, offset + bytes_written);
45         PrintDebug("Wrote %d bytes\n", tmp_bytes);
46         
47         if (tmp_bytes <= 0 ) {
48             PrintError("Write failed\n");
49             return -1;
50         }
51         
52         bytes_written += tmp_bytes;
53     }
54     
55     return 0;
56 }
57
58
59 static int read_all(v3_file_t fd, char * buf, int offset, int length) {
60     int bytes_read = 0;
61     
62     PrintDebug("Reading %d bytes\n", length - bytes_read);
63     while (bytes_read < length) {
64         int tmp_bytes = v3_file_read(fd, buf + bytes_read, length - bytes_read, offset + bytes_read);
65         PrintDebug("Read %d bytes\n", tmp_bytes);
66         
67         if (tmp_bytes <= 0) {
68             PrintError("Read failed\n");
69             return -1;
70         }
71         
72         bytes_read += tmp_bytes;
73     }
74     
75     return 0;
76 }
77
78 static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
79     struct disk_state * disk = (struct disk_state *)private_data;
80
81     PrintDebug("Reading %d bytes from %p to %p\n", (uint32_t)num_bytes, (uint8_t *)(disk->disk_image + lba), buf);
82
83     return read_all(disk->fd, buf, lba, num_bytes);
84 }
85
86
87 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
88     struct disk_state * disk = (struct disk_state *)private_data;
89
90     PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes,  buf, (uint8_t *)(disk->disk_image + lba));
91
92     return write_all(disk->fd,  buf, lba, num_bytes);
93 }
94
95
96 static uint64_t get_capacity(void * private_data) {
97     struct disk_state * disk = (struct disk_state *)private_data;
98
99     PrintDebug("Querying RAMDISK capacity %d\n", 
100                (uint32_t)(disk->capacity));
101
102     return disk->capacity;
103 }
104
105 static struct v3_dev_blk_ops blk_ops = {
106     .read = read, 
107     .write = write,
108     .get_capacity = get_capacity,
109 };
110
111
112
113
114 static int disk_free(struct disk_state * disk) {
115     v3_file_close(disk->fd);
116     
117     V3_Free(disk);
118     return 0;
119 }
120
121 static struct v3_device_ops dev_ops = {
122     .free = (int (*)(void *))disk_free,
123 };
124
125
126
127
128 static int disk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
129     struct disk_state * disk = NULL;
130     char * path = v3_cfg_val(cfg, "path");
131     char * dev_id = v3_cfg_val(cfg, "ID");
132     char * writable = v3_cfg_val(cfg, "writable");
133     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
134     int flags = FILE_OPEN_MODE_READ;
135
136     if ( (writable) && (writable[0] == '1') ) {
137         flags |= FILE_OPEN_MODE_WRITE;
138     }
139
140     if (path == NULL) {
141         PrintError("Missing path (%s) for %s\n", path, dev_id);
142         return -1;
143     }
144
145     disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
146
147     if (disk == NULL) {
148         PrintError("Could not allocate disk\n");
149         return -1;
150     }
151
152     memset(disk, 0, sizeof(struct disk_state));
153
154     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, disk);
155
156     if (dev == NULL) {
157         PrintError("Could not attach device %s\n", dev_id);
158         V3_Free(disk);
159         return -1;
160     }
161
162
163     disk->fd = v3_file_open(vm, path, flags);
164
165     if (disk->fd == NULL) {
166         PrintError("Could not open file disk:%s\n", path);
167         v3_remove_device(dev);
168         return -1;
169     }
170
171     disk->capacity = v3_file_size(disk->fd);
172
173     PrintDebug("Registering FILEDISK %s (path=%s, fd=%lu, size=%lu)\n",
174                dev_id, path, file->fd, file->capacity);
175
176
177     if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), 
178                            &blk_ops, frontend_cfg, disk) == -1) {
179         PrintError("Could not connect %s to frontend %s\n", 
180                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
181         v3_remove_device(dev);
182         return -1;
183     }
184     
185
186     return 0;
187 }
188
189
190 device_register("FILEDISK", disk_init)