Fix problem caused by void ptr and clean up code paths
[yaffs2.git] / direct / yaffsfs.c
index e082dfd95f3f700c6860d5649179faa87dbb96c5..7e2fc39908df33f1903be17c45333a89fac3a8f4 100644 (file)
@@ -31,7 +31,7 @@
 #define YAFFSFS_RW_SIZE  (1<<YAFFSFS_RW_SHIFT)
 
 
-const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.32 2010-02-05 03:59:04 charles Exp $";
+const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.35 2010-02-25 22:38:03 charles Exp $";
 
 // configurationList is the list of devices that are supported
 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
@@ -581,7 +581,10 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
                } else if((oflag & O_CREAT)) {
                        // Let's see if we can create this file
                        dir = yaffsfs_FindDirectory(NULL,path,&name,0);
-                       if(dir)
+                       if(dir  && dir->myDev->readOnly){
+                               yaffsfs_SetError(-EINVAL);
+                               errorReported = 1;
+                       } else if(dir)
                                obj = yaffs_MknodFile(dir,name,mode,0,0);
                        else {
                                yaffsfs_SetError(-ENOTDIR);
@@ -690,7 +693,7 @@ int yaffs_close(int fd)
 
 
 
-int yaffsfs_do_read(int fd, void *buf, unsigned int nbyte, int isPread, int offset)
+int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
 {
        yaffsfs_Handle *h = NULL;
        yaffs_Object *obj = NULL;
@@ -700,6 +703,7 @@ int yaffsfs_do_read(int fd, void *buf, unsigned int nbyte, int isPread, int offs
        int nToRead = 0;
        int totalRead = 0;
        unsigned int maxRead;
+       __u8 *buf = (__u8 *)vbuf;
 
        yaffsfs_Lock();
        h = yaffsfs_GetHandlePointer(fd);
@@ -714,7 +718,9 @@ int yaffsfs_do_read(int fd, void *buf, unsigned int nbyte, int isPread, int offs
                        startPos = offset;
                else
                        startPos = h->position;
-                       
+
+               pos = startPos;
+                                       
                if(yaffs_GetObjectFileLength(obj) > pos)
                        maxRead = yaffs_GetObjectFileLength(obj) - pos;
                else
@@ -724,46 +730,42 @@ int yaffsfs_do_read(int fd, void *buf, unsigned int nbyte, int isPread, int offs
                        nbyte = maxRead;
 
 
-               if(nbyte > 0) {
-                       yaffsfs_GetHandle(fd);
-                       pos = startPos;
-                       
-                       while(nbyte > 0) {
-                               nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
-                               if(nToRead > nbyte)
-                                       nToRead = nbyte;
+               yaffsfs_GetHandle(fd);
 
-                               nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
+               while(nbyte > 0) {
+                       nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
+                       if(nToRead > nbyte)
+                               nToRead = nbyte;
+                       nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
 
-                               if(nRead > 0){
-                                       totalRead += nRead;
-                                       pos += nRead;
-                                       buf += nRead;
-                               }
+                       if(nRead > 0){
+                               totalRead += nRead;
+                               pos += nRead;
+                               buf += nRead;
+                       }
 
-                               if(nRead == nToRead)
-                                       nbyte-=nRead;
-                               else
-                                       nbyte = 0; /* no more to read */
+                       if(nRead == nToRead)
+                               nbyte-=nRead;
+                       else
+                               nbyte = 0; /* no more to read */
                                        
                                        
-                               if(nbyte > 0){
-                                       yaffsfs_Unlock();
-                                       yaffsfs_Lock();
-                               }
-
+                       if(nbyte > 0){
+                               yaffsfs_Unlock();
+                               yaffsfs_Lock();
                        }
 
-                       yaffsfs_PutHandle(fd);
-                       if(!isPread) {
-                               if(totalRead >= 0)
-                                       h->position = startPos + totalRead;
-                               else {
+               }
+
+               yaffsfs_PutHandle(fd);
+
+               if(!isPread) {
+                       if(totalRead >= 0)
+                               h->position = startPos + totalRead;
+                       else {
                                        //todo error
-                               }
                        }
-               } else
-                       totalRead = 0;
+               }
 
        }
 
@@ -783,7 +785,7 @@ int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
        return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
 }
 
-int yaffsfs_do_write(int fd, const void *buf, unsigned int nbyte, int isPwrite, int offset)
+int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
 {
        yaffsfs_Handle *h = NULL;
        yaffs_Object *obj = NULL;
@@ -793,6 +795,7 @@ int yaffsfs_do_write(int fd, const void *buf, unsigned int nbyte, int isPwrite,
        int totalWritten = 0;
        int writeThrough = 0;
        int nToWrite = 0;
+       const __u8 *buf = (const __u8 *)vbuf;
 
        yaffsfs_Lock();
        h = yaffsfs_GetHandlePointer(fd);
@@ -802,7 +805,7 @@ int yaffsfs_do_write(int fd, const void *buf, unsigned int nbyte, int isPwrite,
                // bad handle
                yaffsfs_SetError(-EBADF);
                totalWritten = -1;
-       } else if( h && obj && h->readOnly){
+       } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){
                yaffsfs_SetError(-EINVAL);
                totalWritten=-1;
        } else if( h && obj){
@@ -812,55 +815,51 @@ int yaffsfs_do_write(int fd, const void *buf, unsigned int nbyte, int isPwrite,
                        startPos = yaffs_GetObjectFileLength(obj);
                else
                        startPos = h->position;
-               if( nbyte > 0){
-                       yaffsfs_GetHandle(fd);
-                       pos = startPos;
-                       while(nbyte > 0) {
-                               nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
-                               if(nToWrite > nbyte)
-                                       nToWrite = nbyte;
-                               
-                               nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
-                               if(nWritten > 0){
-                                       totalWritten += nWritten;
-                                       pos += nWritten;
-                                       buf += nWritten;
-                               }
 
-                               if(nWritten == nToWrite)
-                                       nbyte -= nToWrite;
-                               else
-                                       nbyte = 0;
-                               
-                               if(nWritten < 1 && totalWritten < 1){
-                                       yaffsfs_SetError(-ENOSPC);
-                                       totalWritten = -1;
-                               }
-
-                               if(nbyte > 0){
-                                       yaffsfs_Unlock();
-                                       yaffsfs_Lock();
-                               }
+               yaffsfs_GetHandle(fd);
+               pos = startPos;
+               while(nbyte > 0) {
+                       nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
+                       if(nToWrite > nbyte)
+                               nToWrite = nbyte;
+
+                       nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
+                       if(nWritten > 0){
+                               totalWritten += nWritten;
+                               pos += nWritten;
+                               buf += nWritten;
                        }
 
-                       yaffsfs_PutHandle(fd);
+                       if(nWritten == nToWrite)
+                               nbyte -= nToWrite;
+                       else
+                               nbyte = 0;
 
-                       if(!isPwrite){
-                               if(totalWritten > 0)
-                                       h->position = startPos + totalWritten;
-                               else {
-                                       //todo error
-                               }
+                       if(nWritten < 1 && totalWritten < 1){
+                               yaffsfs_SetError(-ENOSPC);
+                               totalWritten = -1;
                        }
-               } else
-                       totalWritten = 0;
 
+                       if(nbyte > 0){
+                               yaffsfs_Unlock();
+                               yaffsfs_Lock();
+                       }
+               }
+
+               yaffsfs_PutHandle(fd);
+
+               if(!isPwrite){
+                       if(totalWritten > 0)
+                               h->position = startPos + totalWritten;
+                       else {
+                               //todo error
+                       }
+               }
        }
 
        yaffsfs_Unlock();
 
        return (totalWritten >= 0) ? totalWritten : -1;
-
 }
 
 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
@@ -889,6 +888,8 @@ int yaffs_truncate(const YCHAR *path,off_t newSize)
                yaffsfs_SetError(-ENOENT);
        else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
                yaffsfs_SetError(-EISDIR);
+       else if(obj->myDev->readOnly)
+               yaffsfs_SetError(-EINVAL);
        else
                result = yaffs_ResizeFile(obj,newSize);
 
@@ -911,6 +912,8 @@ int yaffs_ftruncate(int fd, off_t newSize)
        if(!h || !obj)
                // bad handle
                yaffsfs_SetError(-EBADF);
+       else if(obj->myDev->readOnly)
+               yaffsfs_SetError(-EINVAL);
        else
                // resize the file
                result = yaffs_ResizeFile(obj,newSize);
@@ -977,6 +980,8 @@ int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
                yaffsfs_SetError(-ENOTDIR);
        else if(!obj)
                yaffsfs_SetError(-ENOENT);
+       else if(obj->myDev->readOnly)
+               yaffsfs_SetError(-EINVAL);
        else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
                yaffsfs_SetError(-EISDIR);
        else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
@@ -1026,6 +1031,9 @@ int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
                // bad file
                yaffsfs_SetError(-EBADF);
                renameAllowed = 0;
+       } else if(obj->myDev->readOnly){
+               yaffsfs_SetError(-EINVAL);
+               renameAllowed = 0;
        } else if(olddir->myDev != newdir->myDev) {
                // oops must be on same device
                // todo error
@@ -1064,7 +1072,7 @@ static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
                obj = yaffs_GetEquivalentObject(obj);
 
        if(obj && buf){
-               buf->st_dev = (int)obj->myDev->genericDevice;
+               buf->st_dev = (int)obj->myDev->context;
                buf->st_ino = obj->objectId;
                buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
 
@@ -1294,11 +1302,12 @@ int yaffs_chmod(const YCHAR *path, mode_t mode)
        yaffsfs_Lock();
        obj = yaffsfs_FindObject(NULL,path,0);
 
-       if(obj)
-               retVal = yaffsfs_DoChMod(obj,mode);
-       else
-               // todo error not found
+       if(!obj)
                yaffsfs_SetError(-ENOENT);
+       else if(obj->myDev->readOnly)
+               yaffsfs_SetError(-EINVAL);
+       else
+               retVal = yaffsfs_DoChMod(obj,mode);
 
        yaffsfs_Unlock();
 
@@ -1316,11 +1325,12 @@ int yaffs_fchmod(int fd, mode_t mode)
        yaffsfs_Lock();
        obj = yaffsfs_GetHandleObject(fd);
 
-       if(obj)
-               retVal = yaffsfs_DoChMod(obj,mode);
+       if(!obj)
+               yaffsfs_SetError(-ENOENT);
+       else if(obj->myDev->readOnly)
+               yaffsfs_SetError(-EINVAL);
        else
-               // bad handle
-               yaffsfs_SetError(-EBADF);
+               retVal = yaffsfs_DoChMod(obj,mode);
 
        yaffsfs_Unlock();
 
@@ -1337,18 +1347,22 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode)
 
        yaffsfs_Lock();
        parent = yaffsfs_FindDirectory(NULL,path,&name,0);
-       if(parent)
-               dir = yaffs_MknodDirectory(parent,name,mode,0,0);
-       if(dir)
-               retVal = 0;
-       else {
-               if(!parent)
-                       yaffsfs_SetError(-ENOENT); // missing path
-               else if (yaffs_FindObjectByName(parent,name))
-                       yaffsfs_SetError(-EEXIST); // the name already exists
-               else
-                       yaffsfs_SetError(-ENOSPC); // just assume no space
-               retVal = -1;
+       if(parent && parent->myDev->readOnly){
+               yaffsfs_SetError(-EINVAL);
+       } else {
+               if(parent)
+                       dir = yaffs_MknodDirectory(parent,name,mode,0,0);
+               if(dir)
+                       retVal = 0;
+               else {
+                       if(!parent)
+                               yaffsfs_SetError(-ENOENT); // missing path
+                       else if (yaffs_FindObjectByName(parent,name))
+                               yaffsfs_SetError(-EEXIST); // the name already exists
+                       else
+                               yaffsfs_SetError(-ENOSPC); // just assume no space
+                       retVal = -1;
+               }
        }
 
        yaffsfs_Unlock();
@@ -1356,7 +1370,7 @@ int yaffs_mkdir(const YCHAR *path, mode_t mode)
        return retVal;
 }
 
-int yaffs_mount(const YCHAR *path)
+int yaffs_mount2(const YCHAR *path,int readOnly)
 {
        int retVal=-1;
        int result=YAFFS_FAIL;
@@ -1369,6 +1383,7 @@ int yaffs_mount(const YCHAR *path)
        dev = yaffsfs_FindDevice(path,&dummy);
        if(dev){
                if(!dev->isMounted){
+                       dev->readOnly = readOnly ? 1 : 0;
                        result = yaffs_GutsInitialise(dev);
                        if(result == YAFFS_FAIL)
                                // todo error - mount failed
@@ -1388,6 +1403,11 @@ int yaffs_mount(const YCHAR *path)
 
 }
 
+int yaffs_mount(const YCHAR *path)
+{
+       return yaffs_mount2(path,0);
+}
+
 int yaffs_sync(const YCHAR *path)
 {
         int retVal=-1;
@@ -1416,7 +1436,47 @@ int yaffs_sync(const YCHAR *path)
 }
 
 
-int yaffs_unmount(const YCHAR *path)
+int yaffs_remount(const YCHAR *path, int force, int readOnly)
+{
+        int retVal=-1;
+       yaffs_Device *dev=NULL;
+       YCHAR *dummy;
+
+       yaffsfs_Lock();
+       dev = yaffsfs_FindDevice(path,&dummy);
+       if(dev){
+               if(dev->isMounted){
+                       int i;
+                       int inUse;
+
+                       yaffs_FlushEntireDeviceCache(dev);
+
+                       for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
+                               if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
+                                       inUse = 1; // the device is in use, can't unmount
+                       }
+
+                       if(!inUse || force){
+                               if(readOnly)
+                                       yaffs_CheckpointSave(dev);
+                               dev->readOnly =  readOnly ? 1 : 0;
+                               retVal = 0;
+                       } else
+                               yaffsfs_SetError(-EBUSY);
+
+               } else
+                       yaffsfs_SetError(-EINVAL);
+
+       }
+       else
+               yaffsfs_SetError(-ENODEV);
+
+       yaffsfs_Unlock();
+       return retVal;
+
+}
+
+int yaffs_unmount2(const YCHAR *path, int force)
 {
         int retVal=-1;
        yaffs_Device *dev=NULL;
@@ -1437,7 +1497,7 @@ int yaffs_unmount(const YCHAR *path)
                                        inUse = 1; // the device is in use, can't unmount
                        }
 
-                       if(!inUse){
+                       if(!inUse || force){
                                yaffs_Deinitialise(dev);
 
                                retVal = 0;
@@ -1459,6 +1519,11 @@ int yaffs_unmount(const YCHAR *path)
 
 }
 
+int yaffs_unmount(const YCHAR *path)
+{
+       return yaffs_unmount2(path,0);
+}
+
 loff_t yaffs_freespace(const YCHAR *path)
 {
        loff_t retVal=-1;
@@ -1487,8 +1552,8 @@ loff_t yaffs_totalspace(const YCHAR *path)
        yaffsfs_Lock();
        dev = yaffsfs_FindDevice(path,&dummy);
        if(dev  && dev->isMounted){
-               retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
-               retVal *= dev->nChunksPerBlock;
+               retVal = (dev->param.endBlock - dev->param.startBlock + 1) - dev->param.nReservedBlocks;
+               retVal *= dev->param.nChunksPerBlock;
                retVal *= dev->nDataBytesPerChunk;
 
        } else
@@ -1534,7 +1599,7 @@ void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
 
        while(cfg && cfg->prefix && cfg->dev){
                cfg->dev->isMounted = 0;
-               cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
+               cfg->dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
                cfg++;
        }
 
@@ -1738,7 +1803,9 @@ int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
 
        yaffsfs_Lock();
        parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
-       if(parent){
+       if(parent && parent->myDev->readOnly)
+               yaffsfs_SetError(-EINVAL);
+       else if(parent){
                obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
                if(obj)
                        retVal = 0;
@@ -1800,6 +1867,9 @@ int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
        if(!obj) {
                yaffsfs_SetError(-ENOENT);
                retVal = -1;
+       } else if(obj->myDev->readOnly){
+               yaffsfs_SetError(-EINVAL);
+               retVal= -1;
        } else if(target) {
                yaffsfs_SetError(-EEXIST);
                retVal = -1;