1 /* Palacios file interface
7 #include <linux/file.h>
8 #include <linux/spinlock.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
14 #include <interfaces/vmm_file.h>
16 static struct list_head global_files;
18 struct palacios_file {
26 struct v3_guest * guest;
29 struct list_head file_node;
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;
38 pfile = kmalloc(sizeof(struct palacios_file), GFP_KERNEL);
39 memset(pfile, 0, sizeof(struct palacios_file));
41 if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) {
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;
49 pfile->filp = filp_open(path, pfile->mode, 0);
51 if (pfile->filp == NULL) {
52 printk("Cannot open file: %s\n", path);
56 pfile->path = kmalloc(strlen(path) + 1, GFP_KERNEL);
57 strncpy(pfile->path, path, strlen(path));
60 spin_lock_init(&(pfile->lock));
63 list_add(&(pfile->file_node), &(global_files));
65 list_add(&(pfile->file_node), &(guest->files));
72 static int palacios_file_close(void * file_ptr) {
73 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
75 filp_close(pfile->filp, NULL);
77 list_del(&(pfile->file_node));
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;
91 ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
94 printk("Failed to fstat file\n");
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;
110 ret = vfs_read(filp, buffer, length, &offset);
115 printk("sys_read of %p for %lld bytes failed\n", filp, length);
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;
131 ret = vfs_write(filp, buffer, length, &offset);
137 printk("sys_write failed\n");
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,
153 int palacios_file_init( void ) {
154 INIT_LIST_HEAD(&(global_files));
156 V3_Init_File(&palacios_file_hooks);
162 int palacios_file_deinit( void ) {
163 if (!list_empty(&(global_files))) {
164 printk("Error removing module with open files\n");