1 /* Palacios file interface
7 #include <linux/namei.h>
8 #include <linux/version.h>
9 #include <linux/file.h>
10 #include <linux/spinlock.h>
11 #include <linux/uaccess.h>
12 #include <linux/module.h>
15 #include "linux-exts.h"
17 #include <interfaces/vmm_file.h>
19 static struct list_head global_files;
21 #define isprint(a) ((a >= ' ') && (a <= '~'))
23 struct palacios_file {
31 struct v3_guest * guest;
34 struct list_head file_node;
38 // Currently this just holds the list of open files
39 struct vm_file_state {
40 struct list_head open_files;
45 static int palacios_file_mkdir(const char * pathname, unsigned short perms, int recurse);
47 static int mkdir_recursive(const char * path, unsigned short perms) {
48 char * tmp_str = NULL;
52 tmp_str = palacios_alloc(strlen(path) + 1);
54 ERROR("Cannot allocate in mkdir recursive\n");
58 memset(tmp_str, 0, strlen(path) + 1);
59 strncpy(tmp_str, path, strlen(path));
61 dirname_ptr = tmp_str;
64 // parse path string, call palacios_file_mkdir recursively.
67 while (dirname_ptr != NULL) {
70 while ((*tmp_iter != '/') &&
71 (*tmp_iter != '\0')) {
73 if ( (!isprint(*tmp_iter))) {
74 ERROR("Invalid character in path name (%d)\n", *tmp_iter);
75 palacios_free(tmp_str);
82 if (*tmp_iter == '/') {
88 // Ignore empty directories
89 if ((tmp_iter - dirname_ptr) > 1) {
90 if (palacios_file_mkdir(tmp_str, perms, 0) != 0) {
91 ERROR("Could not create directory (%s)\n", tmp_str);
92 palacios_free(tmp_str);
105 dirname_ptr = tmp_iter;
108 palacios_free(tmp_str);
113 static int palacios_file_mkdir(const char * pathname, unsigned short perms, int recurse) {
114 /* Welcome to the jungle... */
116 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,41)
117 /* DO NOT REFERENCE THIS VARIABLE */
118 /* It only exists to provide version compatibility */
119 struct path tmp_path;
122 struct path * path_ptr = NULL;
123 struct dentry * dentry;
129 return mkdir_recursive(pathname, perms);
132 /* Before Linux 3.1 this was somewhat more difficult */
133 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,41)
137 // I'm not 100% sure about the version here, but it was around this time that the API changed
138 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,38)
139 ret = kern_path_parent(pathname, &nd);
142 if (path_lookup(pathname, LOOKUP_DIRECTORY | LOOKUP_FOLLOW, &nd) == 0) {
146 if (path_lookup(pathname, LOOKUP_PARENT | LOOKUP_FOLLOW, &nd) != 0) {
152 ERROR("%s:%d - Error: kern_path_parent() returned error for (%s)\n", __FILE__, __LINE__,
157 dentry = lookup_create(&nd, 1);
158 path_ptr = &(nd.path);
162 dentry = kern_path_create(AT_FDCWD, pathname, &tmp_path, 1);
164 if (IS_ERR(dentry)) {
168 path_ptr = &tmp_path;
173 if (!IS_ERR(dentry)) {
174 ret = vfs_mkdir(path_ptr->dentry->d_inode, dentry, perms);
177 mutex_unlock(&(path_ptr->dentry->d_inode->i_mutex));
184 static void * palacios_file_open(const char * path, int mode, void * private_data) {
185 struct v3_guest * guest = (struct v3_guest *)private_data;
186 struct palacios_file * pfile = NULL;
187 struct vm_file_state * vm_state = NULL;
190 vm_state = get_vm_ext_data(guest, "FILE_INTERFACE");
192 if (vm_state == NULL) {
193 ERROR("ERROR: Could not locate vm file state for extension FILE_INTERFACE\n");
198 pfile = palacios_alloc(sizeof(struct palacios_file));
200 ERROR("Cannot allocate in file open\n");
203 memset(pfile, 0, sizeof(struct palacios_file));
205 if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) {
206 pfile->mode = O_RDWR;
207 } else if (mode & FILE_OPEN_MODE_READ) {
208 pfile->mode = O_RDONLY;
209 } else if (mode & FILE_OPEN_MODE_WRITE) {
210 pfile->mode = O_WRONLY;
213 if (mode & FILE_OPEN_MODE_CREATE) {
214 pfile->mode |= O_CREAT;
218 pfile->mode |= O_LARGEFILE;
221 pfile->filp = filp_open(path, pfile->mode, 0);
223 if (IS_ERR(pfile->filp)) {
224 ERROR("Cannot open file: %s\n", path);
225 palacios_free(pfile);
229 pfile->path = palacios_alloc(strlen(path));
232 ERROR("Cannot allocate in file open\n");
233 filp_close(pfile->filp,NULL);
234 palacios_free(pfile);
237 strncpy(pfile->path, path, strlen(path));
238 pfile->guest = guest;
240 spin_lock_init(&(pfile->lock));
243 list_add(&(pfile->file_node), &(global_files));
245 list_add(&(pfile->file_node), &(vm_state->open_files));
252 static int palacios_file_close(void * file_ptr) {
253 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
255 filp_close(pfile->filp, NULL);
257 list_del(&(pfile->file_node));
259 palacios_free(pfile->path);
260 palacios_free(pfile);
265 static unsigned long long palacios_file_size(void * file_ptr) {
266 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
267 struct file * filp = pfile->filp;
271 ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
274 ERROR("Failed to fstat file\n");
281 static unsigned long long palacios_file_read(void * file_ptr, void * buffer, unsigned long long length, unsigned long long offset){
282 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
283 struct file * filp = pfile->filp;
290 ret = vfs_read(filp, buffer, length, &offset);
295 ERROR("sys_read of %p for %lld bytes at offset %llu failed (ret=%ld)\n", filp, length, offset, ret);
302 static unsigned long long palacios_file_write(void * file_ptr, void * buffer, unsigned long long length, unsigned long long offset) {
303 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
304 struct file * filp = pfile->filp;
311 ret = vfs_write(filp, buffer, length, &offset);
317 ERROR("sys_write for %llu bytes at offset %llu failed (ret=%ld)\n", length, offset, ret);
324 static struct v3_file_hooks palacios_file_hooks = {
325 .open = palacios_file_open,
326 .close = palacios_file_close,
327 .read = palacios_file_read,
328 .write = palacios_file_write,
329 .size = palacios_file_size,
330 .mkdir = palacios_file_mkdir,
335 static int file_init( void ) {
336 INIT_LIST_HEAD(&(global_files));
338 V3_Init_File(&palacios_file_hooks);
344 static int file_deinit( void ) {
345 struct palacios_file * pfile = NULL;
346 struct palacios_file * tmp = NULL;
348 list_for_each_entry_safe(pfile, tmp, &(global_files), file_node) {
349 filp_close(pfile->filp, NULL);
350 list_del(&(pfile->file_node));
351 palacios_free(pfile->path);
352 palacios_free(pfile);
358 static int guest_file_init(struct v3_guest * guest, void ** vm_data) {
359 struct vm_file_state * state = palacios_alloc(sizeof(struct vm_file_state));
362 ERROR("Cannot allocate when intializing file services for guest\n");
367 INIT_LIST_HEAD(&(state->open_files));
376 static int guest_file_deinit(struct v3_guest * guest, void * vm_data) {
377 struct vm_file_state * state = (struct vm_file_state *)vm_data;
378 struct palacios_file * pfile = NULL;
379 struct palacios_file * tmp = NULL;
381 list_for_each_entry_safe(pfile, tmp, &(state->open_files), file_node) {
382 filp_close(pfile->filp, NULL);
383 list_del(&(pfile->file_node));
384 palacios_free(pfile->path);
385 palacios_free(pfile);
388 palacios_free(state);
393 static struct linux_ext file_ext = {
394 .name = "FILE_INTERFACE",
396 .deinit = file_deinit,
397 .guest_init = guest_file_init,
398 .guest_deinit = guest_file_deinit
402 register_extension(&file_ext);