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.


PMU Host Interface and initial implementation for Linux and AMD/Intel
[palacios.git] / linux_module / iface-file.c
index 062a6d2..10a525d 100644 (file)
@@ -49,7 +49,12 @@ static int mkdir_recursive(const char * path, unsigned short perms) {
     char * dirname_ptr;
     char * tmp_iter;
 
-    tmp_str = kmalloc(strlen(path) + 1, GFP_KERNEL);
+    tmp_str = palacios_alloc(strlen(path) + 1);
+    if (!tmp_str) { 
+       ERROR("Cannot allocate in mkdir recursive\n");
+       return -1;
+    }
+
     memset(tmp_str, 0, strlen(path) + 1);
     strncpy(tmp_str, path, strlen(path));
 
@@ -67,6 +72,7 @@ static int mkdir_recursive(const char * path, unsigned short perms) {
 
            if ( (!isprint(*tmp_iter))) {
                ERROR("Invalid character in path name (%d)\n", *tmp_iter);
+               palacios_free(tmp_str);
                return -1;
            } else {
                tmp_iter++;
@@ -83,6 +89,7 @@ static int mkdir_recursive(const char * path, unsigned short perms) {
        if ((tmp_iter - dirname_ptr) > 1) {
            if (palacios_file_mkdir(tmp_str, perms, 0) != 0) {
                ERROR("Could not create directory (%s)\n", tmp_str);
+               palacios_free(tmp_str);
                return -1;
            }
        }
@@ -98,7 +105,7 @@ static int mkdir_recursive(const char * path, unsigned short perms) {
        dirname_ptr = tmp_iter;
     }
     
-    kfree(tmp_str);
+    palacios_free(tmp_str);
 
     return 0;
 }
@@ -188,7 +195,11 @@ static void * palacios_file_open(const char * path, int mode, void * private_dat
        }
     }
     
-    pfile = kmalloc(sizeof(struct palacios_file), GFP_KERNEL);
+    pfile = palacios_alloc(sizeof(struct palacios_file));
+    if (!pfile) { 
+       ERROR("Cannot allocate in file open\n");
+       return NULL;
+    }
     memset(pfile, 0, sizeof(struct palacios_file));
 
     if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) { 
@@ -211,10 +222,18 @@ static void * palacios_file_open(const char * path, int mode, void * private_dat
     
     if (IS_ERR(pfile->filp)) {
        ERROR("Cannot open file: %s\n", path);
+       palacios_free(pfile);
        return NULL;
     }
 
-    pfile->path = kmalloc(strlen(path) + 1, GFP_KERNEL);
+    pfile->path = palacios_alloc(strlen(path));
+    
+    if (!pfile->path) { 
+       ERROR("Cannot allocate in file open\n");
+       filp_close(pfile->filp,NULL);
+       palacios_free(pfile);
+       return NULL;
+    }
     strncpy(pfile->path, path, strlen(path));
     pfile->guest = guest;
     
@@ -237,8 +256,8 @@ static int palacios_file_close(void * file_ptr) {
     
     list_del(&(pfile->file_node));
 
-    kfree(pfile->path);    
-    kfree(pfile);
+    palacios_free(pfile->path);    
+    palacios_free(pfile);
 
     return 0;
 }
@@ -323,15 +342,27 @@ static int file_init( void ) {
 
 
 static int file_deinit( void ) {
-    if (!list_empty(&(global_files))) {
-       ERROR("Error removing module with open files\n");
+    struct palacios_file * pfile = NULL;
+    struct palacios_file * tmp = NULL;
+    
+    list_for_each_entry_safe(pfile, tmp, &(global_files), file_node) { 
+        filp_close(pfile->filp, NULL);
+        list_del(&(pfile->file_node));
+        palacios_free(pfile->path);    
+        palacios_free(pfile);
     }
 
     return 0;
 }
 
 static int guest_file_init(struct v3_guest * guest, void ** vm_data) {
-    struct vm_file_state * state = kmalloc(sizeof(struct vm_file_state), GFP_KERNEL);
+    struct vm_file_state * state = palacios_alloc(sizeof(struct vm_file_state));
+
+    if (!state) {
+       ERROR("Cannot allocate when intializing file services for guest\n");
+       return -1;
+    }
+       
     
     INIT_LIST_HEAD(&(state->open_files));
 
@@ -343,8 +374,18 @@ static int guest_file_init(struct v3_guest * guest, void ** vm_data) {
 
 
 static int guest_file_deinit(struct v3_guest * guest, void * vm_data) {
+    struct vm_file_state * state = (struct vm_file_state *)vm_data;
+    struct palacios_file * pfile = NULL;
+    struct palacios_file * tmp = NULL;
     
-    kfree(vm_data);
+    list_for_each_entry_safe(pfile, tmp, &(state->open_files), file_node) { 
+        filp_close(pfile->filp, NULL);
+        list_del(&(pfile->file_node));
+        palacios_free(pfile->path);    
+        palacios_free(pfile);
+    }
+
+    palacios_free(state);
     return 0;
 }