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 = palacios_alloc(strlen(path) + 1);
54 ERROR("Cannot allocate in mkdir recursive\n");
58 memset(tmp_str, 0, strlen(path) + 1);
59 strncpy(tmp_str, path, strlen(path));
61 dirname_ptr = tmp_str;
64 // parse path string, call palacios_file_mkdir recursively.
67 while (dirname_ptr != NULL) {
70 while ((*tmp_iter != '/') &&
71 (*tmp_iter != '\0')) {
73 if ( (!isprint(*tmp_iter))) {
74 ERROR("Invalid character in path name (%d)\n", *tmp_iter);
75 palacios_free(tmp_str);
82 if (*tmp_iter == '/') {
88 // Ignore empty directories
89 if ((tmp_iter - dirname_ptr) > 1) {
90 if (palacios_file_mkdir(tmp_str, perms, 0) != 0) {
91 ERROR("Could not create directory (%s)\n", tmp_str);
92 palacios_free(tmp_str);
105 dirname_ptr = tmp_iter;
108 palacios_free(tmp_str);
113 static int palacios_file_mkdir(const char * pathname, unsigned short perms, int recurse) {
114 /* Welcome to the jungle... */
116 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,41)
117 /* DO NOT REFERENCE THIS VARIABLE */
118 /* It only exists to provide version compatibility */
119 struct path tmp_path;
122 struct path * path_ptr = NULL;
123 struct dentry * dentry;
129 return mkdir_recursive(pathname, perms);
132 /* Before Linux 3.1 this was somewhat more difficult */
133 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,41)
137 // I'm not 100% sure about the version here, but it was around this time that the API changed
138 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,38)
139 ret = kern_path_parent(pathname, &nd);
142 if (path_lookup(pathname, LOOKUP_DIRECTORY | LOOKUP_FOLLOW, &nd) == 0) {
146 if (path_lookup(pathname, LOOKUP_PARENT | LOOKUP_FOLLOW, &nd) != 0) {
152 ERROR("%s:%d - Error: kern_path_parent() returned error for (%s)\n", __FILE__, __LINE__,
157 dentry = lookup_create(&nd, 1);
158 path_ptr = &(nd.path);
162 dentry = kern_path_create(AT_FDCWD, pathname, &tmp_path, 1);
164 if (!dentry || IS_ERR(dentry)) {
168 path_ptr = &tmp_path;
173 if (!(!dentry || IS_ERR(dentry))) {
174 ret = vfs_mkdir(path_ptr->dentry->d_inode, dentry, perms);
177 mutex_unlock(&(path_ptr->dentry->d_inode->i_mutex));
184 static void * palacios_file_open(const char * path, int mode, void * private_data) {
185 struct v3_guest * guest = (struct v3_guest *)private_data;
186 struct palacios_file * pfile = NULL;
187 struct vm_file_state * vm_state = NULL;
190 vm_state = get_vm_ext_data(guest, "FILE_INTERFACE");
192 if (vm_state == NULL) {
193 ERROR("ERROR: Could not locate vm file state for extension FILE_INTERFACE\n");
198 pfile = palacios_alloc(sizeof(struct palacios_file));
200 ERROR("Cannot allocate in file open\n");
203 memset(pfile, 0, sizeof(struct palacios_file));
205 if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) {
206 pfile->mode = O_RDWR;
207 } else if (mode & FILE_OPEN_MODE_READ) {
208 pfile->mode = O_RDONLY;
209 } else if (mode & FILE_OPEN_MODE_WRITE) {
210 pfile->mode = O_WRONLY;
213 if (mode & FILE_OPEN_MODE_CREATE) {
214 pfile->mode |= O_CREAT;
218 pfile->mode |= O_LARGEFILE;
221 pfile->filp = filp_open(path, pfile->mode, 0);
223 if (!pfile->filp || IS_ERR(pfile->filp)) {
224 ERROR("Cannot open file: %s\n", path);
225 palacios_free(pfile);
229 pfile->path = palacios_alloc(strlen(path));
232 ERROR("Cannot allocate in file open\n");
233 filp_close(pfile->filp,NULL);
234 palacios_free(pfile);
237 strncpy(pfile->path, path, strlen(path));
238 pfile->guest = guest;
240 palacios_spinlock_init(&(pfile->lock));
243 list_add(&(pfile->file_node), &(global_files));
245 list_add(&(pfile->file_node), &(vm_state->open_files));
252 static int palacios_file_close(void * file_ptr) {
253 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
259 filp_close(pfile->filp, NULL);
261 list_del(&(pfile->file_node));
263 palacios_spinlock_deinit(&(pfile->lock));
265 palacios_free(pfile->path);
266 palacios_free(pfile);
271 static unsigned long long palacios_file_size(void * file_ptr) {
272 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
273 struct file * filp = pfile->filp;
277 ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
280 ERROR("Failed to fstat file\n");
287 static unsigned long long palacios_file_read(void * file_ptr, void * buffer, unsigned long long length, unsigned long long offset){
288 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
289 struct file * filp = pfile->filp;
296 ret = vfs_read(filp, buffer, length, &offset);
301 ERROR("sys_read of %p for %lld bytes at offset %llu failed (ret=%ld)\n", filp, length, offset, ret);
308 static unsigned long long palacios_file_write(void * file_ptr, void * buffer, unsigned long long length, unsigned long long offset) {
309 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
310 struct file * filp = pfile->filp;
317 ret = vfs_write(filp, buffer, length, &offset);
323 ERROR("sys_write for %llu bytes at offset %llu failed (ret=%ld)\n", length, offset, ret);
330 static struct v3_file_hooks palacios_file_hooks = {
331 .open = palacios_file_open,
332 .close = palacios_file_close,
333 .read = palacios_file_read,
334 .write = palacios_file_write,
335 .size = palacios_file_size,
336 .mkdir = palacios_file_mkdir,
341 static int file_init( void ) {
342 INIT_LIST_HEAD(&(global_files));
344 V3_Init_File(&palacios_file_hooks);
350 static int file_deinit( void ) {
351 struct palacios_file * pfile = NULL;
352 struct palacios_file * tmp = NULL;
354 list_for_each_entry_safe(pfile, tmp, &(global_files), file_node) {
355 filp_close(pfile->filp, NULL);
356 list_del(&(pfile->file_node));
357 palacios_free(pfile->path);
358 palacios_free(pfile);
364 static int guest_file_init(struct v3_guest * guest, void ** vm_data) {
365 struct vm_file_state * state = palacios_alloc(sizeof(struct vm_file_state));
368 ERROR("Cannot allocate when intializing file services for guest\n");
373 INIT_LIST_HEAD(&(state->open_files));
382 static int guest_file_deinit(struct v3_guest * guest, void * vm_data) {
383 struct vm_file_state * state = (struct vm_file_state *)vm_data;
384 struct palacios_file * pfile = NULL;
385 struct palacios_file * tmp = NULL;
387 list_for_each_entry_safe(pfile, tmp, &(state->open_files), file_node) {
388 filp_close(pfile->filp, NULL);
389 list_del(&(pfile->file_node));
390 palacios_free(pfile->path);
391 palacios_free(pfile);
394 palacios_free(state);
399 static struct linux_ext file_ext = {
400 .name = "FILE_INTERFACE",
402 .deinit = file_deinit,
403 .guest_init = guest_file_init,
404 .guest_deinit = guest_file_deinit
408 register_extension(&file_ext);