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.


Linux kernel compatability enhancements (through 3.19)
[palacios.git] / linux_module / iface-file.c
index 46d4a28..880c4d9 100644 (file)
@@ -20,6 +20,12 @@ static struct list_head global_files;
 
 #define isprint(a) ((a >= ' ') && (a <= '~'))
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
+#define PAL_VFS_GETATTR(path, kstat) vfs_getattr((path)->mnt, (path)->dentry, kstat)
+#else
+#define PAL_VFS_GETATTR(path, kstat) vfs_getattr(path, kstat)
+#endif
+
 struct palacios_file {
     struct file * filp;
 
@@ -56,6 +62,7 @@ static int mkdir_recursive(const char * path, unsigned short perms) {
     }
 
     memset(tmp_str, 0, strlen(path) + 1);
+    // will terminate tmp_str
     strncpy(tmp_str, path, strlen(path));
 
     dirname_ptr = tmp_str;
@@ -118,6 +125,9 @@ static int palacios_file_mkdir(const char * pathname, unsigned short perms, int
     /* It only exists to provide version compatibility */
     struct path tmp_path; 
 #endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,41)
+    struct nameidata nd;
+#endif
 
     struct path * path_ptr = NULL;
     struct dentry * dentry;
@@ -132,8 +142,6 @@ static int palacios_file_mkdir(const char * pathname, unsigned short perms, int
     /* Before Linux 3.1 this was somewhat more difficult */
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,41)
     {
-       struct nameidata nd;
-
        // I'm not 100% sure about the version here, but it was around this time that the API changed
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,38) 
        ret = kern_path_parent(pathname, &nd);
@@ -161,7 +169,7 @@ static int palacios_file_mkdir(const char * pathname, unsigned short perms, int
     {
        dentry = kern_path_create(AT_FDCWD, pathname, &tmp_path, 1);
        
-       if (IS_ERR(dentry)) {
+       if (!dentry || IS_ERR(dentry)) {
            return 0;
        }
        
@@ -170,7 +178,7 @@ static int palacios_file_mkdir(const char * pathname, unsigned short perms, int
 #endif    
 
 
-    if (!IS_ERR(dentry)) {
+    if (!(!dentry || IS_ERR(dentry))) {
        ret = vfs_mkdir(path_ptr->dentry->d_inode, dentry, perms);
     }
 
@@ -218,15 +226,15 @@ static void * palacios_file_open(const char * path, int mode, void * private_dat
     pfile->mode |= O_LARGEFILE;
 
 
-    pfile->filp = filp_open(path, pfile->mode, 0);
+    pfile->filp = filp_open(path, pfile->mode, 0600);  // rw------- to start
     
-    if (IS_ERR(pfile->filp)) {
+    if (!pfile->filp || IS_ERR(pfile->filp)) {
        ERROR("Cannot open file: %s\n", path);
        palacios_free(pfile);
        return NULL;
     }
 
-    pfile->path = palacios_alloc(strlen(path));
+    pfile->path = palacios_alloc(strlen(path) + 1);
     
     if (!pfile->path) { 
        ERROR("Cannot allocate in file open\n");
@@ -234,10 +242,10 @@ static void * palacios_file_open(const char * path, int mode, void * private_dat
        palacios_free(pfile);
        return NULL;
     }
-    strncpy(pfile->path, path, strlen(path));
+    strncpy(pfile->path, path, strlen(path)); // will terminate pfile->path
     pfile->guest = guest;
     
-    spin_lock_init(&(pfile->lock));
+    palacios_spinlock_init(&(pfile->lock));
 
     if (guest == NULL) {
        list_add(&(pfile->file_node), &(global_files));
@@ -252,10 +260,16 @@ static void * palacios_file_open(const char * path, int mode, void * private_dat
 static int palacios_file_close(void * file_ptr) {
     struct palacios_file * pfile = (struct palacios_file *)file_ptr;
 
+    if (!pfile) {
+        return -1;
+    }
+
     filp_close(pfile->filp, NULL);
     
     list_del(&(pfile->file_node));
 
+    palacios_spinlock_deinit(&(pfile->lock));
+
     palacios_free(pfile->path);    
     palacios_free(pfile);
 
@@ -268,7 +282,7 @@ static unsigned long long palacios_file_size(void * file_ptr) {
     struct kstat s;
     int ret;
     
-    ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
+    ret = PAL_VFS_GETATTR(&(filp->f_path), &s);
 
     if (ret != 0) {
        ERROR("Failed to fstat file\n");
@@ -342,8 +356,14 @@ 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;
@@ -368,9 +388,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;
     
-    palacios_free(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;
 }