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.


Cleanup and sanity-checking of before/after null-check and copy+paste errors (Coverit...
[palacios.git] / linux_module / iface-keyed-stream.c
index 75978ab..a6fb960 100644 (file)
@@ -13,7 +13,6 @@
 #include <linux/file.h>
 #include <linux/uaccess.h>
 #include <linux/namei.h>
-#include <linux/vmalloc.h>
 #include <linux/poll.h>
 #include <linux/anon_inodes.h>
 
@@ -168,7 +167,7 @@ static struct mem_stream *create_mem_stream_internal(uint64_t size)
     }
 
 
-    m->data = vmalloc(size);
+    m->data = palacios_valloc(size);
     
     if (!m->data) { 
        palacios_free(m);
@@ -193,7 +192,7 @@ static void destroy_mem_stream(struct mem_stream *m)
 {
     if (m) {
        if (m->data) {
-           vfree(m->data);
+           palacios_vfree(m->data);
        }
        m->data=0;
        palacios_free(m);
@@ -202,7 +201,7 @@ static void destroy_mem_stream(struct mem_stream *m)
     
 static int expand_mem_stream(struct mem_stream *m, uint32_t new_size)
 {
-    void *data = vmalloc(new_size);
+    void *data = palacios_valloc(new_size);
     uint32_t nc;
 
     if (!data) { 
@@ -213,7 +212,7 @@ static int expand_mem_stream(struct mem_stream *m, uint32_t new_size)
 
     memcpy(data,m->data,nc);
 
-    vfree(m->data);
+    palacios_vfree(m->data);
 
     m->data=data;
     m->size=new_size;
@@ -311,7 +310,7 @@ static v3_keyed_stream_t open_stream_mem(char *url,
                    return 0;
                }
 
-               strcpy(mykey,url+4);
+               strcpy(mykey,url+4); // will fit
                
                mks = (struct mem_keyed_stream *) palacios_alloc(sizeof(struct mem_keyed_stream));
 
@@ -383,7 +382,7 @@ static v3_keyed_stream_key_t open_key_mem(v3_keyed_stream_t stream,
            return 0;
        }
 
-       strcpy(mykey,key);
+       strcpy(mykey,key); // will fit
 
        m = create_mem_stream();
        
@@ -432,7 +431,7 @@ static void preallocate_hint_key_mem(v3_keyed_stream_t stream,
            return;
        }
        
-       strcpy(mykey,key);
+       strcpy(mykey,key); // will fit
        
        m = create_mem_stream_internal(size);
        
@@ -685,7 +684,7 @@ static v3_keyed_stream_t open_stream_file(char *url,
        return 0;
     }
     
-    strcpy(fks->path,url+5);
+    strcpy(fks->path,url+5); // will fit
     
     fks->stype=STREAM_FILE;
 
@@ -727,7 +726,7 @@ static v3_keyed_stream_t open_stream_file(char *url,
 
            de = lookup_create(&nd,1);
 
-           if (IS_ERR(de)) { 
+           if (!de || IS_ERR(de)) { 
                ERROR("cannot allocate dentry\n");
                goto fail_out;
            }
@@ -797,11 +796,12 @@ static v3_keyed_stream_key_t open_key_file(v3_keyed_stream_t stream,
        ERROR("cannot allocate file keyed stream for key %s\n",key);
        return 0;
     }
+    // this sequence will fit and terminate with a zero
     strcpy(path,fks->path);
     strcat(path,"/");
     strcat(path,key);
     
-    fs = (struct file_stream *) palacios_alloc(sizeof(struct file_stream *));
+    fs = (struct file_stream *) palacios_alloc(sizeof(struct file_stream));
     
     if (!fs) { 
        ERROR("cannot allocate file keyed stream for key %s\n",key);
@@ -811,9 +811,9 @@ static v3_keyed_stream_key_t open_key_file(v3_keyed_stream_t stream,
 
     fs->stype=STREAM_FILE;
 
-    fs->f = filp_open(path,O_RDWR|O_CREAT,0600);
-    
-    if (IS_ERR(fs->f)) {
+    fs->f = filp_open(path,O_RDWR|O_CREAT|O_LARGEFILE,0600);
+
+    if (!fs->f || IS_ERR(fs->f)) {
        ERROR("cannot open relevent file \"%s\" for stream \"file:%s\" and key \"%s\"\n",path,fks->path,key);
        palacios_free(fs);
        palacios_free(path);
@@ -1449,7 +1449,7 @@ static v3_keyed_stream_key_t open_key_textfile(v3_keyed_stream_t stream,
     }
 
 
-    if (token_scan(ms,tempkey,keylen+3," \t\r\n",4)) { 
+    if (token_scan(ms,tempkey,keylen+3,"\t\r\n",3)) { 
       ERROR("Cannot scan for token (key search)\n");
       close_key_file(mks,ms);
       mks->stype=STREAM_TEXTFILE;
@@ -1709,7 +1709,7 @@ static int do_request_to_response(struct user_keyed_stream *s, unsigned long *fl
     s->waiting = 1;
 
     // release the stream
-    spin_unlock_irqrestore(&(s->lock), *flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock), *flags);
 
     // wake up anyone waiting on it
     wake_up_interruptible(&(s->user_wait_queue));
@@ -1718,7 +1718,7 @@ static int do_request_to_response(struct user_keyed_stream *s, unsigned long *fl
     while (wait_event_interruptible(s->host_wait_queue, (s->waiting == 0)) != 0) {}
 
     // reacquire the lock for our called
-    spin_lock_irqsave(&(s->lock), *flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), *flags);
 
     return 0;
 }
@@ -1739,7 +1739,7 @@ static int do_response_to_request(struct user_keyed_stream *s, unsigned long *fl
     s->waiting = 0;
 
     // release the stream
-    spin_unlock_irqrestore(&(s->lock), *flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock), *flags);
 
     // wake up anyone waiting on it
     wake_up_interruptible(&(s->host_wait_queue));
@@ -1758,16 +1758,16 @@ static unsigned int keyed_stream_poll_user(struct file *filp, poll_table *wait)
         return POLLERR;
     }
     
-    spin_lock_irqsave(&(s->lock), flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), flags);
+
+    poll_wait(filp, &(s->user_wait_queue), wait);
 
     if (s->waiting) {
-       spin_unlock_irqrestore(&(s->lock), flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
        return POLLIN | POLLRDNORM;
     }
-
-    poll_wait(filp, &(s->user_wait_queue), wait);
     
-    spin_unlock_irqrestore(&(s->lock), flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
 
     return 0;
 }
@@ -1786,22 +1786,22 @@ static long keyed_stream_ioctl_user(struct file * filp, unsigned int ioctl, unsi
            
            // inform request size
            
-           spin_lock_irqsave(&(s->lock), flags);
+           palacios_spinlock_lock_irqsave(&(s->lock), flags);
            
            if (!(s->waiting)) {
-               spin_unlock_irqrestore(&(s->lock), flags);
+               palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
                return 0;
            }
 
            size =  sizeof(struct palacios_user_keyed_stream_op) + s->op->buf_len;
            
            if (copy_to_user((void * __user) argp, &size, sizeof(uint64_t))) {
-               spin_unlock_irqrestore(&(s->lock), flags);
+               palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
                ERROR("palacios user key size request failed to copy data\n");
                return -EFAULT;
            }
            
-           spin_unlock_irqrestore(&(s->lock), flags);
+           palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
            
            return 1;
            
@@ -1811,10 +1811,10 @@ static long keyed_stream_ioctl_user(struct file * filp, unsigned int ioctl, unsi
                
            // pull the request
            
-           spin_lock_irqsave(&(s->lock), flags);
+           palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
            if (!(s->waiting)) {
-               spin_unlock_irqrestore(&(s->lock), flags);
+               palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
                ERROR("palacios user key pull request when not waiting\n");
                return 0;
            }
@@ -1823,12 +1823,12 @@ static long keyed_stream_ioctl_user(struct file * filp, unsigned int ioctl, unsi
 
 
            if (copy_to_user((void __user *) argp, s->op, size)) {
-               spin_unlock_irqrestore(&(s->lock), flags);
+               palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
                ERROR("palacios user key pull request failed to copy data\n");
                return -EFAULT;
            }
 
-           spin_unlock_irqrestore(&(s->lock), flags);
+           palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
            
            return 1;
            
@@ -1839,28 +1839,28 @@ static long keyed_stream_ioctl_user(struct file * filp, unsigned int ioctl, unsi
 
         // push the response
 
-        spin_lock_irqsave(&(s->lock), flags);
+        palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
         if (!(s->waiting)) {
-            spin_unlock_irqrestore(&(s->lock), flags);
+            palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
            ERROR("palacios user key push response when not waiting\n");
             return 0;
         }
        
         if (copy_from_user(&size, (void __user *) argp, sizeof(uint64_t))) {
            ERROR("palacios user key push response failed to copy size\n");
-            spin_unlock_irqrestore(&(s->lock), flags);
+            palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
             return -EFAULT;
         }
 
        if (resize_op(&(s->op),size-sizeof(struct palacios_user_keyed_stream_op))) {
            ERROR("unable to resize op in user key push response\n");
-            spin_unlock_irqrestore(&(s->lock), flags);
+            palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
            return -EFAULT;
        }
 
         if (copy_from_user(s->op, (void __user *) argp, size)) {
-            spin_unlock_irqrestore(&(s->lock), flags);
+            palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
             return -EFAULT;
         }
 
@@ -1887,13 +1887,13 @@ static int keyed_stream_release_user(struct inode *inode, struct file *filp)
     struct user_keyed_stream *s = filp->private_data;
     unsigned long f1,f2;
 
-    spin_lock_irqsave(&(user_streams->lock),f1);
-    spin_lock_irqsave(&(s->lock), f2);
+    palacios_spinlock_lock_irqsave(&(user_streams->lock),f1);
+    palacios_spinlock_lock_irqsave(&(s->lock), f2);
 
     list_del(&(s->node));
 
-    spin_unlock_irqrestore(&(s->lock), f2);
-    spin_unlock_irqrestore(&(user_streams->lock), f1);
+    palacios_spinlock_unlock_irqrestore(&(s->lock), f2);
+    palacios_spinlock_unlock_irqrestore(&(user_streams->lock), f1);
     
     palacios_free(s->url);
     palacios_free(s);
@@ -1949,7 +1949,7 @@ int keyed_stream_connect_user(struct v3_guest *guest, unsigned int cmd, unsigned
        
     
     // Check for duplicate handler
-    spin_lock_irqsave(&(user_streams->lock), flags);
+    palacios_spinlock_lock_irqsave(&(user_streams->lock), flags);
     list_for_each_entry(s, &(user_streams->streams), node) {
         if (!strncasecmp(url, s->url, len)) {
             ERROR("user keyed stream connection with url \"%s\" already exists\n", url);
@@ -1957,7 +1957,7 @@ int keyed_stream_connect_user(struct v3_guest *guest, unsigned int cmd, unsigned
             return -1;
         }
     }
-    spin_unlock_irqrestore(&(user_streams->lock), flags);
+    palacios_spinlock_unlock_irqrestore(&(user_streams->lock), flags);
     
     // Create connection
     s = palacios_alloc(sizeof(struct user_keyed_stream));
@@ -1988,9 +1988,9 @@ int keyed_stream_connect_user(struct v3_guest *guest, unsigned int cmd, unsigned
     init_waitqueue_head(&(s->host_wait_queue));
     
     // Insert connection into list
-    spin_lock_irqsave(&(user_streams->lock), flags);
+    palacios_spinlock_lock_irqsave(&(user_streams->lock), flags);
     list_add(&(s->node), &(user_streams->streams));
-    spin_unlock_irqrestore(&(user_streams->lock), flags);
+    palacios_spinlock_unlock_irqrestore(&(user_streams->lock), flags);
     
     return fd;
 }
@@ -2005,15 +2005,15 @@ static struct user_keyed_stream *keyed_stream_user_find(char *url)
        return NULL;
     }
     
-    spin_lock_irqsave(&(user_streams->lock), flags);
+    palacios_spinlock_lock_irqsave(&(user_streams->lock), flags);
     list_for_each_entry(s, &(user_streams->streams), node) {
         if (!strcasecmp(url, s->url)) {
-            spin_unlock_irqrestore(&(user_streams->lock), flags);
+            palacios_spinlock_unlock_irqrestore(&(user_streams->lock), flags);
             return s;
         }
     }
     
-    spin_unlock_irqrestore(&(user_streams->lock), flags);
+    palacios_spinlock_unlock_irqrestore(&(user_streams->lock), flags);
     
     return NULL;
 }
@@ -2031,17 +2031,17 @@ static v3_keyed_stream_t open_stream_user(char *url, v3_keyed_stream_open_t ot)
         return NULL;
     }
 
-    spin_lock_irqsave(&(s->lock), flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
     if (s->waiting) {
-        spin_unlock_irqrestore(&(s->lock), flags);
+        palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
        ERROR("cannot open user stream %s as it is already in waiting state\n",url);
         return NULL;
     }
     
     s->otype = ot==V3_KS_WR_ONLY_CREATE ? V3_KS_WR_ONLY : ot;
     
-    spin_unlock_irqrestore(&(s->lock), flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock), flags);
     
     return s;
     
@@ -2072,22 +2072,22 @@ static v3_keyed_stream_key_t open_key_user(v3_keyed_stream_t stream, char *key)
     uint64_t   len = strlen(key)+1;
     void *user_key;
 
-    spin_lock_irqsave(&(s->lock), flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
 
     if (resize_op(&(s->op),len)) {
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("cannot resize op in opening key %s on user keyed stream %s\n",key,s->url);
        return NULL;
     }
 
     s->op->type = PALACIOS_KSTREAM_OPEN_KEY;
     s->op->buf_len = len;
-    strncpy(s->op->buf,key,len);
+    strncpy(s->op->buf,key,len); // will terminate buffer
 
     // enter with it locked
     if (do_request_to_response(s,&flags)) { 
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("request/response handling failed\n");
        return NULL;
     }
@@ -2095,7 +2095,7 @@ static v3_keyed_stream_key_t open_key_user(v3_keyed_stream_t stream, char *key)
 
     user_key=s->op->user_key;
 
-    spin_unlock_irqrestore(&(s->lock),flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
 
     return user_key;
 }
@@ -2106,10 +2106,10 @@ static void close_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t key)
     uint64_t   len = 0;
     unsigned long flags;
     
-    spin_lock_irqsave(&(s->lock), flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
     if (resize_op(&(s->op),len)) {
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("cannot resize op in closing key 0x%p on user keyed stream %s\n",key,s->url);
        return;
     }
@@ -2120,13 +2120,13 @@ static void close_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t key)
 
     // enter with it locked
     if (do_request_to_response(s,&flags)) { 
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("request/response handling failed\n");
        return;
     }
     // return with it locked
 
-    spin_unlock_irqrestore(&(s->lock),flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
 
     return;
 }
@@ -2144,16 +2144,16 @@ static sint64_t read_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t ke
     sint64_t   xfer;
     unsigned long flags;
 
-    spin_lock_irqsave(&(s->lock), flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
     if (s->otype != V3_KS_RD_ONLY) { 
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("attempt to read key from stream that is not in read state on %s\n",s->url);
     }  
 
 
     if (resize_op(&(s->op),len)) {
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("cannot resize op in reading key 0x%p on user keyed stream %s\n",key,s->url);
        return -1;
     }
@@ -2168,7 +2168,7 @@ static sint64_t read_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t ke
 
     // enter with it locked
     if (do_request_to_response(s,&flags)) { 
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("request/response handling failed\n");
        return -1;
     }
@@ -2182,7 +2182,7 @@ static sint64_t read_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t ke
 
     xfer=s->op->xfer;
 
-    spin_unlock_irqrestore(&(s->lock),flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
 
     return xfer;
 }
@@ -2200,15 +2200,15 @@ static sint64_t write_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t k
     unsigned long flags;
 
 
-    spin_lock_irqsave(&(s->lock), flags);
+    palacios_spinlock_lock_irqsave(&(s->lock), flags);
 
     if (s->otype != V3_KS_WR_ONLY) { 
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("attempt to write key on stream that is not in write state on %s\n",s->url);
     }  
 
     if (resize_op(&(s->op),len)) {
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("cannot resize op in reading key 0x%p on user keyed stream %s\n",key,s->url);
        return -1;
     }
@@ -2224,7 +2224,7 @@ static sint64_t write_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t k
 
     // enter with it locked
     if (do_request_to_response(s,&flags)) { 
-       spin_unlock_irqrestore(&(s->lock),flags);
+       palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
        ERROR("request/response handling failed\n");
        return -1;
     }
@@ -2234,7 +2234,7 @@ static sint64_t write_key_user(v3_keyed_stream_t stream, v3_keyed_stream_key_t k
 
     xfer=s->op->xfer;
 
-    spin_unlock_irqrestore(&(s->lock),flags);
+    palacios_spinlock_unlock_irqrestore(&(s->lock),flags);
 
     return xfer;
 }
@@ -2780,7 +2780,7 @@ static sint64_t write_key_net(v3_keyed_stream_t stream, v3_keyed_stream_key_t ke
            ERROR("Could not send tag length in write_key_net\n");
            return -1;
        } 
-       if (send_msg(nks->ns,tag,taglen)!=len) { 
+       if (send_msg(nks->ns,tag,taglen)!=taglen) { 
            ERROR("Could not send tag in write_key_net\n");
            return -1;
        }
@@ -3145,7 +3145,7 @@ static int init_keyed_streams( void )
 
     INIT_LIST_HEAD(&(user_streams->streams));
     
-    spin_lock_init(&(user_streams->lock));
+    palacios_spinlock_init(&(user_streams->lock));
 
     V3_Init_Keyed_Streams(&hooks);
 
@@ -3157,6 +3157,8 @@ static int deinit_keyed_streams( void )
 {
     palacios_free_htable(mem_streams,1,1);
 
+    palacios_spinlock_deinit(&(user_streams->lock));
+
     palacios_free(user_streams);
 
     WARNING("Deinit of Palacios Keyed Streams likely leaked memory\n");
@@ -3176,6 +3178,7 @@ static int guest_init_keyed_streams(struct v3_guest * guest, void ** vm_data )
 
 static int guest_deinit_keyed_streams(struct v3_guest * guest, void * vm_data)
 {
+    remove_guest_ctrl(guest, V3_VM_KSTREAM_USER_CONNECT);
 
     return 0;
 }