yaffs Add sharing control to yaffs direct
[yaffs2.git] / direct / yaffsfs.c
index 191c34f6bd1b6f79209c5642a4df244e1b79a85a..d87bc8073f476fb5e7fe38c3dc276cb6fa79d59e 100644 (file)
@@ -53,30 +53,35 @@ typedef struct {
 } yaffsfs_Inode;
 
 typedef struct{
-       __u8    readOnly:1;
+       __u8    reading:1;
+       __u8    writing:1;
        __u8    append:1;
-       int     inodeId:13;     /* Index to corresponding yaffsfs_Inode */
-       int     useCount:16;    /* Use count for this handle */
+       __u8    shareRead:1;
+       __u8    shareWrite:1;
+       int     inodeId:12;     /* Index to corresponding yaffsfs_Inode */
+       int     useCount:10;    /* Use count for this handle */
        __u32 position;         /* current position in file */
 }yaffsfs_Handle;
 
 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
+static int yaffsfs_handlesInitialised;
 
 /*
  * yaffsfs_InitHandle
  * Inilitalise handle management on start-up.
  */
 
-static int yaffsfs_InitHandles(void)
+static void yaffsfs_InitHandles(void)
 {
        int i;
+       if(yaffsfs_handlesInitialised)
+                return;
+
        memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
        memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
        for(i = 0; i < YAFFSFS_N_HANDLES; i++)
                yaffsfs_handle[i].inodeId = -1;
-
-       return 0;
 }
 
 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
@@ -108,31 +113,46 @@ yaffs_Object *yaffsfs_GetHandleObject(int handle)
 }
 
 /*
- * yaffsfs_GetInodeIdForObject
- * Grab an inode entry when opening a new inode.
+ * yaffsfs_FindInodeIdForObject
+ * Find the inode entry for an object, if it exists.
  */
 
-static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
+static int yaffsfs_FindInodeIdForObject(yaffs_Object *obj)
 {
        int i;
        int ret = -1;
-       yaffsfs_Inode *in = NULL;
        
        if(obj)
                obj = yaffs_GetEquivalentObject(obj);
 
-       /* Look for it. If we can't find it then make one */
+       /* Look for it in open inode table*/
        for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
                if(yaffsfs_inode[i].iObj == obj)
                        ret = i;
        }
+       return ret;
+}
+
+/*
+ * yaffsfs_GetInodeIdForObject
+ * Grab an inode entry when opening a new inode.
+ */
+static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
+{
+       int i;
+       int ret;
+       yaffsfs_Inode *in = NULL;
+       
+       if(obj)
+               obj = yaffs_GetEquivalentObject(obj);
+
+        ret = yaffsfs_FindInodeIdForObject(obj);
 
        for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
                if(!yaffsfs_inode[i].iObj)
                        ret = i;
        }
-       
-       
+
        if(ret>=0){
                in = &yaffsfs_inode[ret];
                if(!in->iObj)
@@ -145,6 +165,17 @@ static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
        return ret;
 }
 
+
+static int yaffsfs_CountHandles(yaffs_Object *obj)
+{
+       int i = yaffsfs_FindInodeIdForObject(obj);
+
+       if(i >= 0)
+               return yaffsfs_inode[i].count;
+       else
+               return 0;
+}
+
 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
 {
        yaffs_Object *obj;
@@ -246,7 +277,7 @@ int yaffsfs_Match(YCHAR a, YCHAR b)
 
 int yaffsfs_IsPathDivider(YCHAR ch)
 {
-       YCHAR *str = YAFFS_PATH_DIVIDERS;
+       const YCHAR *str = YAFFS_PATH_DIVIDERS;
 
        while(*str){
                if(*str == ch)
@@ -533,7 +564,7 @@ int yaffs_dup(int fd)
 
 }
 
-int yaffs_open(const YCHAR *path, int oflag, int mode)
+int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
 {
        yaffs_Object *obj = NULL;
        yaffs_Object *dir = NULL;
@@ -543,6 +574,14 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
        int openDenied = 0;
        int symDepth = 0;
        int errorReported = 0;
+       __u8 shareRead = (sharing & YAFFS_SHARE_READ) ?  1 : 0;
+       __u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
+       __u8 sharedReadAllowed;
+       __u8 sharedWriteAllowed;
+       __u8 alreadyReading;
+       __u8 alreadyWriting;
+       __u8 readRequested;
+       __u8 writeRequested;
 
        /* O_EXCL only has meaning if O_CREAT is specified */
        if(!(oflag & O_CREAT))
@@ -563,7 +602,6 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
 
                h = yaffsfs_GetHandlePointer(handle);
 
-
                /* try to find the exisiting object */
                obj = yaffsfs_FindObject(NULL,path,0);
 
@@ -611,6 +649,43 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
                           !(obj->yst_mode & S_IWRITE))
                                openDenied = 1;
 
+                       /* Check sharing of an existing object. */
+                       {
+                               yaffsfs_Handle *h;
+                               int i;
+                               sharedReadAllowed = 1;
+                               sharedWriteAllowed = 1;
+                               alreadyReading = 0;
+                               alreadyWriting = 0;
+                               for( i = 0; i < YAFFSFS_N_HANDLES; i++){
+                                       h = &yaffsfs_handle[i];
+                                       if(h->useCount > 0 &&
+                                               h->inodeId >= 0 &&
+                                               yaffsfs_inode[h->inodeId].iObj == obj){
+                                               if(!h->shareRead)
+                                                       sharedReadAllowed = 0;
+                                               if(!h->shareWrite)
+                                                       sharedWriteAllowed = 0;
+                                               if(h->reading)
+                                                       alreadyReading = 1;
+                                               if(h->writing)
+                                                       alreadyWriting = 0;
+                                       }
+                               }
+
+                               readRequested = (oflag & (O_RDWR | O_RDONLY)) ? 1 : 0;
+                               writeRequested = (oflag & (O_RDWR | O_WRONLY)) ? 1 : 0;
+
+                               if((!sharedReadAllowed && readRequested)|| 
+                                       (!shareRead  && alreadyReading) ||
+                                       (!sharedWriteAllowed && writeRequested) ||
+                                       (!shareWrite && alreadyWriting)){
+                                       openDenied = 1;
+                                       yaffsfs_SetError(-EBUSY);
+                                       errorReported=1;
+                               }
+                       }
+
                } else if((oflag & O_CREAT)) {
                        /* Let's see if we can create this file */
                        dir = yaffsfs_FindDirectory(NULL,path,&name,0);
@@ -636,14 +711,17 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
                        }
                        
                        h->inodeId = inodeId;
-                       h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
+                       h->reading = (oflag & (O_RDONLY | O_RDWR)) ? 1 : 0;
+                       h->writing = (oflag & (O_WRONLY | O_RDWR)) ? 1 : 0;
                        h->append =  (oflag & O_APPEND) ? 1 : 0;
                        h->position = 0;
+                       h->shareRead = shareRead;
+                       h->shareWrite = shareWrite;
 
                        /* Hook inode to object */
                         obj->myInode = (void*) &yaffsfs_inode[inodeId];
 
-                        if((oflag & O_TRUNC) && !h->readOnly)
+                        if((oflag & O_TRUNC) && h->writing)
                                 yaffs_ResizeFile(obj,0);
                } else {
                        yaffsfs_PutHandle(handle);
@@ -660,6 +738,11 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
        return handle;
 }
 
+int yaffs_open(const YCHAR *path, int oflag, int mode)
+{
+       return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
+}
+
 int yaffs_Dofsync(int fd,int datasync)
 {
        yaffsfs_Handle *h = NULL;
@@ -837,7 +920,7 @@ int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite,
                /* bad handle */
                yaffsfs_SetError(-EBADF);
                totalWritten = -1;
-       } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){
+       } else if( h && obj && (!h->writing || obj->myDev->readOnly)){
                yaffsfs_SetError(-EINVAL);
                totalWritten=-1;
        } else if( h && obj){
@@ -1637,7 +1720,10 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode)
 
        yaffsfs_Lock();
        parent = yaffsfs_FindDirectory(NULL,path,&name,0);
-       if(parent && parent->myDev->readOnly){
+       if(parent && yaffs_strnlen(name,5) == 0){
+               /* Trying to make the root itself */
+               yaffsfs_SetError(-EEXIST);
+       } else if(parent && parent->myDev->readOnly){
                yaffsfs_SetError(-EINVAL);
        } else {
                if(parent)
@@ -1660,6 +1746,14 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode)
        return retVal;
 }
 
+void * yaffs_getdev(const YCHAR *path)
+{
+       yaffs_Device *dev=NULL;
+       YCHAR *dummy;
+       dev = yaffsfs_FindDevice(path,&dummy);
+       return (void *)dev;
+}
+
 int yaffs_mount2(const YCHAR *path,int readOnly)
 {
        int retVal=-1;
@@ -1670,6 +1764,9 @@ int yaffs_mount2(const YCHAR *path,int readOnly)
        T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
 
        yaffsfs_Lock();
+
+       yaffsfs_InitHandles();
+
        dev = yaffsfs_FindDevice(path,&dummy);
        if(dev){
                if(!dev->isMounted){
@@ -1711,7 +1808,7 @@ int yaffs_sync(const YCHAR *path)
                         
                         yaffs_FlushEntireDeviceCache(dev);
                         yaffs_CheckpointSave(dev);
-                        
+                        retVal = 0;
                         
                 } else
                         /* todo error - not mounted. */
@@ -2207,6 +2304,23 @@ int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
        return -1;
 }
 
+
+
+/*
+ * yaffs_n_handles()
+ * Returns number of handles attached to the object
+ */
+int yaffs_n_handles(const YCHAR *path)
+{
+       yaffs_Object *obj;
+
+       obj = yaffsfs_FindObject(NULL,path,0);
+       if(obj)
+               obj = yaffs_GetEquivalentObject(obj);
+
+       return yaffsfs_CountHandles(obj);
+}
+
 int yaffs_DumpDevStruct(const YCHAR *path)
 {
 #if 0