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.


bind launch thread to local core
[palacios.git] / linux_module / palacios-file.c
1 /* Palacios file interface 
2  * (c) Jack Lange, 2010
3  */
4
5
6 #include <linux/fs.h>
7 #include <linux/file.h>
8 #include <linux/spinlock.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11
12 #include "palacios.h"
13
14 #include <interfaces/vmm_file.h>
15
16 static struct list_head global_files;
17
18 struct palacios_file {
19     struct file * filp;
20
21     char * path;
22     int mode;
23     
24     spinlock_t lock;
25
26     struct v3_guest * guest;
27     
28
29     struct list_head file_node;
30 };
31
32
33
34 static void * palacios_file_open(const char * path, int mode, void * private_data) {
35     struct v3_guest * guest = (struct v3_guest *)private_data;
36     struct palacios_file * pfile = NULL;
37     
38     pfile = kmalloc(sizeof(struct palacios_file), GFP_KERNEL);
39     memset(pfile, 0, sizeof(struct palacios_file));
40
41     if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) { 
42         pfile->mode = O_RDWR;
43     } else if (mode & FILE_OPEN_MODE_READ) { 
44         pfile->mode = O_RDONLY;
45     } else if (mode & FILE_OPEN_MODE_WRITE) { 
46         pfile->mode = O_WRONLY;
47     } 
48
49     pfile->filp = filp_open(path, pfile->mode, 0);
50     
51     if (pfile->filp == NULL) {
52         printk("Cannot open file: %s\n", path);
53         return NULL;
54     }
55
56     pfile->path = kmalloc(strlen(path) + 1, GFP_KERNEL);
57     strncpy(pfile->path, path, strlen(path));
58     pfile->guest = guest;
59     
60     spin_lock_init(&(pfile->lock));
61
62     if (guest == NULL) {
63         list_add(&(pfile->file_node), &(global_files));
64     } else {
65         list_add(&(pfile->file_node), &(guest->files));
66     } 
67
68
69     return pfile;
70 }
71
72 static int palacios_file_close(void * file_ptr) {
73     struct palacios_file * pfile = (struct palacios_file *)file_ptr;
74
75     filp_close(pfile->filp, NULL);
76     
77     list_del(&(pfile->file_node));
78
79     kfree(pfile->path);    
80     kfree(pfile);
81
82     return 0;
83 }
84
85 static long long palacios_file_size(void * file_ptr) {
86     struct palacios_file * pfile = (struct palacios_file *)file_ptr;
87     struct file * filp = pfile->filp;
88     struct kstat s;
89     int ret;
90     
91     ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
92
93     if (ret != 0) {
94         printk("Failed to fstat file\n");
95         return -1;
96     }
97
98     return s.size;
99 }
100
101 static long long palacios_file_read(void * file_ptr, void * buffer, long long length, long long offset){
102     struct palacios_file * pfile = (struct palacios_file *)file_ptr;
103     struct file * filp = pfile->filp;
104     ssize_t ret;
105     mm_segment_t old_fs;
106         
107     old_fs = get_fs();
108     set_fs(get_ds());
109         
110     ret = vfs_read(filp, buffer, length, &offset);
111         
112     set_fs(old_fs);
113         
114     if (ret <= 0) {
115         printk("sys_read of %p for %lld bytes failed\n", filp, length);         
116     }
117         
118     return ret;
119 }
120
121
122 static long long palacios_file_write(void * file_ptr, void * buffer, long long length, long long offset) {
123     struct palacios_file * pfile = (struct palacios_file *)file_ptr;
124     struct file * filp = pfile->filp;
125     mm_segment_t old_fs;
126     ssize_t ret;
127
128     old_fs = get_fs();
129     set_fs(get_ds());
130
131     ret = vfs_write(filp, buffer, length, &offset);
132         
133     set_fs(old_fs);
134
135  
136     if (ret <= 0) {
137         printk("sys_write failed\n");           
138     }
139         
140     return ret;
141 }
142
143
144 static struct v3_file_hooks palacios_file_hooks = {
145         .open           = palacios_file_open,
146         .close          = palacios_file_close,
147         .read           = palacios_file_read,
148         .write          = palacios_file_write,
149         .size           = palacios_file_size,
150 };
151
152
153 int palacios_file_init( void ) {
154     INIT_LIST_HEAD(&(global_files));
155
156     V3_Init_File(&palacios_file_hooks);
157
158     return 0;
159 }
160
161
162 int palacios_file_deinit( void ) {
163     if (!list_empty(&(global_files))) {
164         printk("Error removing module with open files\n");
165     }
166
167     return 0;
168 }