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 = kmalloc(strlen(path) + 1, GFP_KERNEL);
53 memset(tmp_str, 0, strlen(path) + 1);
54 strncpy(tmp_str, path, strlen(path));
56 dirname_ptr = tmp_str;
59 // parse path string, call palacios_file_mkdir recursively.
62 while (dirname_ptr != NULL) {
65 while ((*tmp_iter != '/') &&
66 (*tmp_iter != '\0')) {
68 if ( (!isprint(*tmp_iter))) {
69 printk("Invalid character in path name (%d)\n", *tmp_iter);
76 if (*tmp_iter == '/') {
82 // Ignore empty directories
83 if ((tmp_iter - dirname_ptr) > 1) {
84 if (palacios_file_mkdir(tmp_str, perms, 0) != 0) {
85 printk("Could not create directory (%s)\n", tmp_str);
98 dirname_ptr = tmp_iter;
106 static int palacios_file_mkdir(const char * pathname, unsigned short perms, int recurse) {
107 /* Welcome to the jungle... */
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
110 /* DO NOT REFERENCE THIS VARIABLE */
111 /* It only exists to provide version compatibility */
112 struct path tmp_path;
115 struct path * path_ptr = NULL;
116 struct dentry * dentry;
122 return mkdir_recursive(pathname, perms);
125 /* Before Linux 3.1 this was somewhat more difficult */
126 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)
130 // I'm not 100% sure about the version here, but it was around this time that the API changed
131 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,37)
132 ret = kern_path_parent(pathname, &nd);
135 if (path_lookup(pathname, LOOKUP_DIRECTORY | LOOKUP_FOLLOW, &nd) == 0) {
139 if (path_lookup(pathname, LOOKUP_PARENT | LOOKUP_FOLLOW, &nd) != 0) {
145 printk("%s:%d - Error: kern_path_parent() returned error for (%s)\n", __FILE__, __LINE__,
150 dentry = lookup_create(&nd, 1);
151 path_ptr = &(nd.path);
155 dentry = kern_path_create(AT_FDCWD, pathname, &tmp_path, 1);
157 if (IS_ERR(dentry)) {
161 path_ptr = &tmp_path;
166 if (!IS_ERR(dentry)) {
167 ret = vfs_mkdir(path_ptr->dentry->d_inode, dentry, perms);
170 mutex_unlock(&(path_ptr->dentry->d_inode->i_mutex));
177 static void * palacios_file_open(const char * path, int mode, void * private_data) {
178 struct v3_guest * guest = (struct v3_guest *)private_data;
179 struct palacios_file * pfile = NULL;
180 struct vm_file_state * vm_state = NULL;
183 vm_state = get_vm_ext_data(guest, "FILE_INTERFACE");
185 if (vm_state == NULL) {
186 printk("ERROR: Could not locate vm file state for extension FILE_INTERFACE\n");
191 pfile = kmalloc(sizeof(struct palacios_file), GFP_KERNEL);
192 memset(pfile, 0, sizeof(struct palacios_file));
194 if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) {
195 pfile->mode = O_RDWR;
196 } else if (mode & FILE_OPEN_MODE_READ) {
197 pfile->mode = O_RDONLY;
198 } else if (mode & FILE_OPEN_MODE_WRITE) {
199 pfile->mode = O_WRONLY;
202 if (mode & FILE_OPEN_MODE_CREATE) {
203 pfile->mode |= O_CREAT;
207 pfile->mode |= O_LARGEFILE;
210 pfile->filp = filp_open(path, pfile->mode, 0);
212 if (IS_ERR(pfile->filp)) {
213 printk("Cannot open file: %s\n", path);
217 pfile->path = kmalloc(strlen(path) + 1, GFP_KERNEL);
218 strncpy(pfile->path, path, strlen(path));
219 pfile->guest = guest;
221 spin_lock_init(&(pfile->lock));
224 list_add(&(pfile->file_node), &(global_files));
226 list_add(&(pfile->file_node), &(vm_state->open_files));
233 static int palacios_file_close(void * file_ptr) {
234 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
236 filp_close(pfile->filp, NULL);
238 list_del(&(pfile->file_node));
246 static long long palacios_file_size(void * file_ptr) {
247 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
248 struct file * filp = pfile->filp;
252 ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
255 printk("Failed to fstat file\n");
262 static long long palacios_file_read(void * file_ptr, void * buffer, long long length, long long offset){
263 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
264 struct file * filp = pfile->filp;
271 ret = vfs_read(filp, buffer, length, &offset);
276 printk("sys_read of %p for %lld bytes failed\n", filp, length);
283 static long long palacios_file_write(void * file_ptr, void * buffer, long long length, long long offset) {
284 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
285 struct file * filp = pfile->filp;
292 ret = vfs_write(filp, buffer, length, &offset);
298 printk("sys_write failed\n");
305 static struct v3_file_hooks palacios_file_hooks = {
306 .open = palacios_file_open,
307 .close = palacios_file_close,
308 .read = palacios_file_read,
309 .write = palacios_file_write,
310 .size = palacios_file_size,
311 .mkdir = palacios_file_mkdir,
316 static int file_init( void ) {
317 INIT_LIST_HEAD(&(global_files));
319 V3_Init_File(&palacios_file_hooks);
325 static int file_deinit( void ) {
326 if (!list_empty(&(global_files))) {
327 printk("Error removing module with open files\n");
333 static int guest_file_init(struct v3_guest * guest, void ** vm_data) {
334 struct vm_file_state * state = kmalloc(sizeof(struct vm_file_state), GFP_KERNEL);
336 INIT_LIST_HEAD(&(state->open_files));
345 static int guest_file_deinit(struct v3_guest * guest, void * vm_data) {
351 static struct linux_ext file_ext = {
352 .name = "FILE_INTERFACE",
354 .deinit = file_deinit,
355 .guest_init = guest_file_init,
356 .guest_deinit = guest_file_deinit
360 register_extension(&file_ext);