yaffs Add function to get number of open handles
[yaffs2.git] / direct / yaffsfs.c
index a494e34ca67612b093ea97899d4d05215324b477..b709d93fed1edf91332a66f756b3db6bae7ef813 100644 (file)
 #define YAFFSFS_RW_SHIFT (13)
 #define YAFFSFS_RW_SIZE  (1<<YAFFSFS_RW_SHIFT)
 
-
-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;
-
-
 /* Some forward references */
 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
@@ -69,21 +62,23 @@ typedef struct{
 
 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)
@@ -115,31 +110,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)
@@ -152,6 +162,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;
@@ -253,7 +274,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)
@@ -264,6 +285,10 @@ int yaffsfs_IsPathDivider(YCHAR ch)
        return 0;
 }
 
+
+
+YLIST_HEAD(yaffsfs_deviceList);
+
 /*
  * yaffsfs_FindDevice
  * yaffsfs_FindRoot
@@ -273,10 +298,11 @@ int yaffsfs_IsPathDivider(YCHAR ch)
  */
 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
 {
-       yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
+       struct ylist_head *cfg;
        const YCHAR *leftOver;
        const YCHAR *p;
        yaffs_Device *retval = NULL;
+       yaffs_Device *dev = NULL;
        int thisMatchLength;
        int longestMatch = -1;
        int matching;
@@ -286,9 +312,10 @@ static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
         * 1) Actually matches a prefix (ie /a amd /abc will not match
         * 2) Matches the longest.
         */
-       while(cfg && cfg->prefix && cfg->dev){
+       ylist_for_each(cfg, &yaffsfs_deviceList){
+               dev = ylist_entry(cfg, yaffs_Device, devList);
                leftOver = path;
-               p = cfg->prefix;
+               p = dev->param.name;
                thisMatchLength = 0;
                matching = 1;
 
@@ -319,16 +346,23 @@ static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
                /* Skip over any /s in leftOver */
                while(yaffsfs_IsPathDivider(*leftOver))
                      leftOver++;
-               
 
-               if( matching && (thisMatchLength > longestMatch)){
-                       /* Matched prefix */
+               // Skip over any /s in p
+               while(yaffsfs_IsPathDivider(*p))
+                     p++;
+
+               // p should now be at the end of the string (ie. fully matched)
+               if(*p)
+                       matching = 0;
+
+               if( matching && (thisMatchLength > longestMatch))
+               {
+                       // Matched prefix
                        *restOfPath = (YCHAR *)leftOver;
-                       retval = cfg->dev;
+                       retval = dev;
                        longestMatch = thisMatchLength;
                }
 
-               cfg++;
        }
        return retval;
 }
@@ -567,12 +601,21 @@ int yaffs_open(const YCHAR *path, int oflag, int mode)
                if(obj)
                        obj = yaffs_GetEquivalentObject(obj);
 
-               if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
+               if(obj &&
+                       obj->variantType != YAFFS_OBJECT_TYPE_FILE &&
+                       obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
                        obj = NULL;
 
                if(obj){
 
-                       /* The file already exists */
+                       /* The file already exists or it might be a directory */
+
+                       /* If it is a directory then we can't open it as a file */
+                       if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
+                               openDenied = 1;
+                               yaffsfs_SetError(-EISDIR);
+                               errorReported = 1;
+                       }
 
                        /* Open should fail if O_CREAT and O_EXCL are specified since
                         * the file exists
@@ -826,10 +869,10 @@ int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite,
                yaffsfs_SetError(-EINVAL);
                totalWritten=-1;
        } else if( h && obj){
-               if(isPwrite)
-                       startPos = offset;
                if(h->append)
                        startPos = yaffs_GetObjectFileLength(obj);
+               else if(isPwrite)
+                       startPos = offset;
                else
                        startPos = h->position;
 
@@ -1091,7 +1134,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->context;
+               buf->st_dev = (int)obj->myDev->osContext;
                buf->st_ino = obj->objectId;
                buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
 
@@ -1180,11 +1223,11 @@ int yaffs_fstat(int fd, struct yaffs_stat *buf)
        return retVal;
 }
 
-
+#ifndef CONFIG_YAFFS_WINCE
 /* xattrib functions */
 
 
-int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
+static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
 {
        yaffs_Object *obj;
 
@@ -1193,11 +1236,16 @@ int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int si
        yaffsfs_Lock();
        obj = yaffsfs_FindObject(NULL,path,0);
 
-       obj = yaffsfs_FollowLink(obj,0);
+       if(follow)
+               obj = yaffsfs_FollowLink(obj,0);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* todo error not found */
                yaffsfs_SetError(-ENOENT);
 
@@ -1207,6 +1255,18 @@ int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int si
 
 }
 
+int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
+{
+       return yaffs_do_setxattr(path, name, data, size, flags, 1);
+}
+
+int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
+{
+       return yaffs_do_setxattr(path, name, data, size, flags, 0);
+}
+
+
+
 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
 {
        yaffs_Object *obj;
@@ -1216,9 +1276,13 @@ int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int fl
        yaffsfs_Lock();
        obj = yaffsfs_GetHandleObject(fd);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* bad handle */
                yaffsfs_SetError(-EBADF);
 
@@ -1227,7 +1291,7 @@ int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int fl
        return retVal;
 }
 
-int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
+static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
 {
        yaffs_Object *obj;
 
@@ -1236,11 +1300,16 @@ int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
        yaffsfs_Lock();
        obj = yaffsfs_FindObject(NULL,path,0);
 
-       obj = yaffsfs_FollowLink(obj,0);
+       if(follow)
+               obj = yaffsfs_FollowLink(obj,0);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_GetXAttribute(obj,name,data,size);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* todo error not found */
                yaffsfs_SetError(-ENOENT);
 
@@ -1250,6 +1319,17 @@ int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
 
 }
 
+int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
+{
+       return yaffs_do_getxattr( path, name, data, size, 1);
+}
+int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
+{
+       return yaffs_do_getxattr( path, name, data, size, 0);
+}
+
+
+
 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
 {
        yaffs_Object *obj;
@@ -1259,9 +1339,13 @@ int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
        yaffsfs_Lock();
        obj = yaffsfs_GetHandleObject(fd);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_GetXAttribute(obj,name,data,size);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* bad handle */
                yaffsfs_SetError(-EBADF);
 
@@ -1270,7 +1354,7 @@ int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
        return retVal;
 }
 
-int yaffs_listxattr(const YCHAR *path, char *data, int size)
+static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
 {
        yaffs_Object *obj;
 
@@ -1279,11 +1363,16 @@ int yaffs_listxattr(const YCHAR *path, char *data, int size)
        yaffsfs_Lock();
        obj = yaffsfs_FindObject(NULL,path,0);
 
-       obj = yaffsfs_FollowLink(obj,0);
+       if(follow)
+               obj = yaffsfs_FollowLink(obj,0);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_ListXAttributes(obj, data,size);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* todo error not found */
                yaffsfs_SetError(-ENOENT);
 
@@ -1293,6 +1382,16 @@ int yaffs_listxattr(const YCHAR *path, char *data, int size)
 
 }
 
+int yaffs_listxattr(const YCHAR *path, char *data, int size)
+{
+       return yaffs_do_listxattr(path, data, size, 1);
+}
+
+int yaffs_llistxattr(const YCHAR *path, char *data, int size)
+{
+       return yaffs_do_listxattr(path, data, size, 0);
+}
+
 int yaffs_flistxattr(int fd, char *data, int size)
 {
        yaffs_Object *obj;
@@ -1302,9 +1401,13 @@ int yaffs_flistxattr(int fd, char *data, int size)
        yaffsfs_Lock();
        obj = yaffsfs_GetHandleObject(fd);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_ListXAttributes(obj,data,size);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* bad handle */
                yaffsfs_SetError(-EBADF);
 
@@ -1313,7 +1416,7 @@ int yaffs_flistxattr(int fd, char *data, int size)
        return retVal;
 }
 
-int yaffs_removexattr(const YCHAR *path, const char *name)
+static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
 {
        yaffs_Object *obj;
 
@@ -1322,11 +1425,16 @@ int yaffs_removexattr(const YCHAR *path, const char *name)
        yaffsfs_Lock();
        obj = yaffsfs_FindObject(NULL,path,0);
 
-       obj = yaffsfs_FollowLink(obj,0);
+       if(follow)
+               obj = yaffsfs_FollowLink(obj,0);
 
-       if(obj)
+       if(obj) {
                retVal = yaffs_RemoveXAttribute(obj,name);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       } else
                /* todo error not found */
                yaffsfs_SetError(-ENOENT);
 
@@ -1336,6 +1444,16 @@ int yaffs_removexattr(const YCHAR *path, const char *name)
 
 }
 
+int yaffs_removexattr(const YCHAR *path, const char *name)
+{
+       return yaffs_do_removexattr(path, name, 1);
+}
+
+int yaffs_lremovexattr(const YCHAR *path, const char *name)
+{
+       return yaffs_do_removexattr(path, name, 0);
+}
+
 int yaffs_fremovexattr(int fd, const char *name)
 {
        yaffs_Object *obj;
@@ -1345,9 +1463,13 @@ int yaffs_fremovexattr(int fd, const char *name)
        yaffsfs_Lock();
        obj = yaffsfs_GetHandleObject(fd);
 
-       if(obj)
+       if(obj){
                retVal = yaffs_RemoveXAttribute(obj,name);
-       else
+               if(retVal< 0){
+                       yaffsfs_SetError(retVal);
+                       retVal = -1;
+               }
+       }else
                /* bad handle */
                yaffsfs_SetError(-EBADF);
 
@@ -1355,6 +1477,7 @@ int yaffs_fremovexattr(int fd, const char *name)
 
        return retVal;
 }
+#endif
 
 #ifdef CONFIG_YAFFS_WINCE
 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
@@ -1542,7 +1665,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)
@@ -1565,6 +1691,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;
@@ -1575,6 +1709,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){
@@ -1616,7 +1753,7 @@ int yaffs_sync(const YCHAR *path)
                         
                         yaffs_FlushEntireDeviceCache(dev);
                         yaffs_CheckpointSave(dev);
-                        
+                        retVal = 0;
                         
                 } else
                         /* todo error - not mounted. */
@@ -1767,7 +1904,7 @@ int yaffs_inodecount(const YCHAR *path)
        yaffsfs_Lock();
        dev = yaffsfs_FindDevice(path,&dummy);
        if(dev  && dev->isMounted) {
-          int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
+          int nObjects = dev->nObjects;
           if(nObjects > dev->nHardLinks)
                retVal = nObjects - dev->nHardLinks;
        }
@@ -1780,26 +1917,23 @@ int yaffs_inodecount(const YCHAR *path)
 }
 
 
-
-void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
+void yaffs_AddDevice(yaffs_Device *dev)
 {
+       dev->isMounted = 0;
+       dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
 
-       yaffsfs_DeviceConfiguration *cfg;
-
-       yaffsfs_configurationList = cfgList;
+       if(!dev->devList.next)
+               YINIT_LIST_HEAD(&dev->devList);
 
-       yaffsfs_InitHandles();
-
-       cfg = yaffsfs_configurationList;
+       ylist_add(&dev->devList,&yaffsfs_deviceList);
+}
 
-       while(cfg && cfg->prefix && cfg->dev){
-               cfg->dev->isMounted = 0;
-               cfg->dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
-               cfg++;
-       }
+void yaffs_RemoveDevice(yaffs_Device *dev)
+{
+       ylist_del_init(&dev->devList);
+}
 
 
-}
 
 
 /* Directory search stuff. */
@@ -2115,6 +2249,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