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 // will terminate tmp_str
60 strncpy(tmp_str, path, strlen(path));
62 dirname_ptr = tmp_str;
65 // parse path string, call palacios_file_mkdir recursively.
68 while (dirname_ptr != NULL) {
71 while ((*tmp_iter != '/') &&
72 (*tmp_iter != '\0')) {
74 if ( (!isprint(*tmp_iter))) {
75 ERROR("Invalid character in path name (%d)\n", *tmp_iter);
76 palacios_free(tmp_str);
83 if (*tmp_iter == '/') {
89 // Ignore empty directories
90 if ((tmp_iter - dirname_ptr) > 1) {
91 if (palacios_file_mkdir(tmp_str, perms, 0) != 0) {
92 ERROR("Could not create directory (%s)\n", tmp_str);
93 palacios_free(tmp_str);
106 dirname_ptr = tmp_iter;
109 palacios_free(tmp_str);
114 static int palacios_file_mkdir(const char * pathname, unsigned short perms, int recurse) {
115 /* Welcome to the jungle... */
117 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,41)
118 /* DO NOT REFERENCE THIS VARIABLE */
119 /* It only exists to provide version compatibility */
120 struct path tmp_path;
122 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,41)
126 struct path * path_ptr = NULL;
127 struct dentry * dentry;
133 return mkdir_recursive(pathname, perms);
136 /* Before Linux 3.1 this was somewhat more difficult */
137 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,41)
139 // I'm not 100% sure about the version here, but it was around this time that the API changed
140 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,38)
141 ret = kern_path_parent(pathname, &nd);
144 if (path_lookup(pathname, LOOKUP_DIRECTORY | LOOKUP_FOLLOW, &nd) == 0) {
148 if (path_lookup(pathname, LOOKUP_PARENT | LOOKUP_FOLLOW, &nd) != 0) {
154 ERROR("%s:%d - Error: kern_path_parent() returned error for (%s)\n", __FILE__, __LINE__,
159 dentry = lookup_create(&nd, 1);
160 path_ptr = &(nd.path);
164 dentry = kern_path_create(AT_FDCWD, pathname, &tmp_path, 1);
166 if (!dentry || IS_ERR(dentry)) {
170 path_ptr = &tmp_path;
175 if (!(!dentry || IS_ERR(dentry))) {
176 ret = vfs_mkdir(path_ptr->dentry->d_inode, dentry, perms);
179 mutex_unlock(&(path_ptr->dentry->d_inode->i_mutex));
186 static void * palacios_file_open(const char * path, int mode, void * private_data) {
187 struct v3_guest * guest = (struct v3_guest *)private_data;
188 struct palacios_file * pfile = NULL;
189 struct vm_file_state * vm_state = NULL;
192 vm_state = get_vm_ext_data(guest, "FILE_INTERFACE");
194 if (vm_state == NULL) {
195 ERROR("ERROR: Could not locate vm file state for extension FILE_INTERFACE\n");
200 pfile = palacios_alloc(sizeof(struct palacios_file));
202 ERROR("Cannot allocate in file open\n");
205 memset(pfile, 0, sizeof(struct palacios_file));
207 if ((mode & FILE_OPEN_MODE_READ) && (mode & FILE_OPEN_MODE_WRITE)) {
208 pfile->mode = O_RDWR;
209 } else if (mode & FILE_OPEN_MODE_READ) {
210 pfile->mode = O_RDONLY;
211 } else if (mode & FILE_OPEN_MODE_WRITE) {
212 pfile->mode = O_WRONLY;
215 if (mode & FILE_OPEN_MODE_CREATE) {
216 pfile->mode |= O_CREAT;
220 pfile->mode |= O_LARGEFILE;
223 pfile->filp = filp_open(path, pfile->mode, 0600); // rw------- to start
225 if (!pfile->filp || IS_ERR(pfile->filp)) {
226 ERROR("Cannot open file: %s\n", path);
227 palacios_free(pfile);
231 pfile->path = palacios_alloc(strlen(path) + 1);
234 ERROR("Cannot allocate in file open\n");
235 filp_close(pfile->filp,NULL);
236 palacios_free(pfile);
239 strncpy(pfile->path, path, strlen(path)); // will terminate pfile->path
240 pfile->guest = guest;
242 palacios_spinlock_init(&(pfile->lock));
245 list_add(&(pfile->file_node), &(global_files));
247 list_add(&(pfile->file_node), &(vm_state->open_files));
254 static int palacios_file_close(void * file_ptr) {
255 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
261 filp_close(pfile->filp, NULL);
263 list_del(&(pfile->file_node));
265 palacios_spinlock_deinit(&(pfile->lock));
267 palacios_free(pfile->path);
268 palacios_free(pfile);
273 static unsigned long long palacios_file_size(void * file_ptr) {
274 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
275 struct file * filp = pfile->filp;
279 ret = vfs_getattr(filp->f_path.mnt, filp->f_path.dentry, &s);
282 ERROR("Failed to fstat file\n");
289 static unsigned long long palacios_file_read(void * file_ptr, void * buffer, unsigned long long length, unsigned long long offset){
290 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
291 struct file * filp = pfile->filp;
298 ret = vfs_read(filp, buffer, length, &offset);
303 ERROR("sys_read of %p for %lld bytes at offset %llu failed (ret=%ld)\n", filp, length, offset, ret);
310 static unsigned long long palacios_file_write(void * file_ptr, void * buffer, unsigned long long length, unsigned long long offset) {
311 struct palacios_file * pfile = (struct palacios_file *)file_ptr;
312 struct file * filp = pfile->filp;
319 ret = vfs_write(filp, buffer, length, &offset);
325 ERROR("sys_write for %llu bytes at offset %llu failed (ret=%ld)\n", length, offset, ret);
332 static struct v3_file_hooks palacios_file_hooks = {
333 .open = palacios_file_open,
334 .close = palacios_file_close,
335 .read = palacios_file_read,
336 .write = palacios_file_write,
337 .size = palacios_file_size,
338 .mkdir = palacios_file_mkdir,
343 static int file_init( void ) {
344 INIT_LIST_HEAD(&(global_files));
346 V3_Init_File(&palacios_file_hooks);
352 static int file_deinit( void ) {
353 struct palacios_file * pfile = NULL;
354 struct palacios_file * tmp = NULL;
356 list_for_each_entry_safe(pfile, tmp, &(global_files), file_node) {
357 filp_close(pfile->filp, NULL);
358 list_del(&(pfile->file_node));
359 palacios_free(pfile->path);
360 palacios_free(pfile);
366 static int guest_file_init(struct v3_guest * guest, void ** vm_data) {
367 struct vm_file_state * state = palacios_alloc(sizeof(struct vm_file_state));
370 ERROR("Cannot allocate when intializing file services for guest\n");
375 INIT_LIST_HEAD(&(state->open_files));
384 static int guest_file_deinit(struct v3_guest * guest, void * vm_data) {
385 struct vm_file_state * state = (struct vm_file_state *)vm_data;
386 struct palacios_file * pfile = NULL;
387 struct palacios_file * tmp = NULL;
389 list_for_each_entry_safe(pfile, tmp, &(state->open_files), file_node) {
390 filp_close(pfile->filp, NULL);
391 list_del(&(pfile->file_node));
392 palacios_free(pfile->path);
393 palacios_free(pfile);
396 palacios_free(state);
401 static struct linux_ext file_ext = {
402 .name = "FILE_INTERFACE",
404 .deinit = file_deinit,
405 .guest_init = guest_file_init,
406 .guest_deinit = guest_file_deinit
410 register_extension(&file_ext);