From 2b429ebe1886836cb39a03741bda9472e9acdda1 Mon Sep 17 00:00:00 2001
From: Adwait Nadkarni <anadkarni@ncsu.edu>
Date: Sat, 26 Oct 2013 14:39:36 -0400
Subject: [PATCH] Changes for Aquifer CCS2013

---
 rootdir/ueventd.rc |    1 +
 sdcard/sdcard.c    |   73 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index 438cf0a..5e88127 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -7,6 +7,7 @@
 /dev/urandom              0666   root       root
 /dev/ashmem               0666   root       root
 /dev/binder               0666   root       root
+/dev/aquifer               0666   root       root
 
 # logger should be world writable (for logging) but not readable
 /dev/log/*                0662   root       log
diff --git a/sdcard/sdcard.c b/sdcard/sdcard.c
index 689cd2a..84766db 100644
--- a/sdcard/sdcard.c
+++ b/sdcard/sdcard.c
@@ -24,6 +24,7 @@
 #include <sys/stat.h>
 #include <sys/statfs.h>
 #include <sys/uio.h>
+#include <sys/xattr.h>
 #include <dirent.h>
 #include <ctype.h>
 
@@ -31,6 +32,30 @@
 
 #include "fuse.h"
 
+extern int setxattr (const char *__path, const char *__name,
+                      const void *__value, size_t __size, int __flags);
+extern int lsetxattr (const char *__path, const char *__name,
+                      const void *__value, size_t __size, int __flags);
+extern int fsetxattr (int __filedes, const char *__name,
+                      const void *__value, size_t __size, int __flags);
+
+extern ssize_t getxattr (const char *__path, const char *__name,
+                                void *__value, size_t __size);
+extern ssize_t lgetxattr (const char *__path, const char *__name,
+                                void *__value, size_t __size);
+extern ssize_t fgetxattr (int __filedes, const char *__name,
+                                void *__value, size_t __size);
+
+extern ssize_t listxattr (const char *__path, char *__list,
+                                size_t __size);
+extern ssize_t llistxattr (const char *__path, char *__list,
+                                size_t __size);
+extern ssize_t flistxattr (int __filedes, char *__list,
+                                size_t __size);
+
+extern int removexattr (const char *__path, const char *__name);
+extern int lremovexattr (const char *__path, const char *__name);
+extern int fremovexattr (int __filedes,   const char *__name);
 /* README
  *
  * What is this?
@@ -833,8 +858,48 @@ void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *da
         return;
     }
 //    case FUSE_FSYNC:
-//    case FUSE_SETXATTR:
-//    case FUSE_GETXATTR:
+	case FUSE_SETXATTR: {
+        struct fuse_setxattr_in *req = data;
+        char *path, buffer[PATH_BUFFER_SIZE], value[XATTR_SIZE_MAX], name[XATTR_NAME_MAX];
+        int res;
+
+        path = node_get_path(node, buffer, 0);
+
+        // data:
+        // struct fuse_setxattr_in
+        // char[] name (null-terminated string)
+        // byte[] value (req->size bytes)
+
+        strncpy(name, ((char*) data) + sizeof(*req), XATTR_NAME_MAX);
+        memcpy(value, ((char*) data) + sizeof(*req) + strlen(name) + 1, req->size);
+
+        res = setxattr(path, name, value, req->size, 0);
+
+        fuse_status(fuse, hdr->unique, res ? -errno : 0);
+        return;
+    }
+    case FUSE_GETXATTR: {
+        struct fuse_getxattr_in *req = data;
+        char *path, buffer[PATH_BUFFER_SIZE], value[XATTR_SIZE_MAX], name[XATTR_NAME_MAX];
+        int res;
+
+        path = node_get_path(node, buffer, 0);
+
+        // data:
+        // struct fuse_getxattr_in
+        // char[] name (null-terminated string)
+
+        strncpy(name, ((char*) data) + sizeof(*req), XATTR_NAME_MAX);
+
+        res = getxattr(path, name, value, req->size);
+        if (res < 0) {
+            fuse_status(fuse, hdr->unique, -errno);
+            return;
+        }
+
+        fuse_reply(fuse, hdr->unique, value, res);
+        return;
+    }
 //    case FUSE_LISTXATTR:
 //    case FUSE_REMOVEXATTR:
     case FUSE_FLUSH:
@@ -912,7 +977,7 @@ void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *da
         out.major = FUSE_KERNEL_VERSION;
         out.minor = FUSE_KERNEL_MINOR_VERSION;
         out.max_readahead = req->max_readahead;
-        out.flags = FUSE_ATOMIC_O_TRUNC;
+        out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
         out.max_background = 32;
         out.congestion_threshold = 32;
         out.max_write = 256 * 1024;
@@ -941,7 +1006,7 @@ void handle_fuse_requests(struct fuse *fuse)
     int len;
     
     for (;;) {
-        len = read(fuse->fd, req, 8192);
+        len = read(fuse->fd, req, sizeof(req));
         if (len < 0) {
             if (errno == EINTR)
                 continue;
-- 
1.7.4.1

