Some changes to get VxWorks wrapper working
[yaffs2.git] / direct / yaffsfs.c
index 0b231338ad0dfaa8a8562b24e28cf685a317dea1..05cd22977720acd42e1bc925f9fc119e05a81584 100644 (file)
@@ -78,8 +78,8 @@ struct yaffsfs_DirSearchContext {
        struct yaffs_obj *dirObj;       /* ptr to directory being searched */
        struct yaffs_obj *nextReturn;   /* obj  returned by next readddir */
        struct list_head others;
-       int offset:20;
-       unsigned inUse:1;
+       s32 offset:20;
+       u8 inUse:1;
 };
 
 struct yaffsfs_FileDes {
@@ -89,8 +89,8 @@ struct yaffsfs_FileDes {
        u8 append:1;
        u8 shareRead:1;
        u8 shareWrite:1;
-       int inodeId:12;         /* Index to corresponding yaffsfs_Inode */
-       int handleCount:10;     /* Number of handles for this fd */
+       s32 inodeId:12;         /* Index to corresponding yaffsfs_Inode */
+       s32 handleCount:10;     /* Number of handles for this fd */
        union {
                Y_LOFF_T position;      /* current position in file */
                yaffs_DIR *dir;
@@ -398,7 +398,7 @@ static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
  *  Stuff to handle names.
  */
 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
-
+#ifndef CONFIG_YAFFS_WINCE
 static int yaffs_toupper(YCHAR a)
 {
        if (a >= 'a' && a <= 'z')
@@ -406,6 +406,7 @@ static int yaffs_toupper(YCHAR a)
        else
                return a;
 }
+#endif
 
 static int yaffsfs_Match(YCHAR a, YCHAR b)
 {
@@ -432,7 +433,7 @@ static int yaffsfs_IsPathDivider(YCHAR ch)
        return 0;
 }
 
-static int yaffsfs_CheckNameLength(const char *name)
+static int yaffsfs_CheckNameLength(const YCHAR *name)
 {
        int retVal = 0;
 
@@ -514,17 +515,17 @@ static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
                thisMatchLength = 0;
                matching = 1;
 
+
                if(!p)
                        continue;
 
-               while (matching && *p && *leftOver) {
-                       /* Skip over any /s */
-                       while (yaffsfs_IsPathDivider(*p))
-                               p++;
+               /* Skip over any leading  /s */
+               while (yaffsfs_IsPathDivider(*p))
+                       p++;
+               while (yaffsfs_IsPathDivider(*leftOver))
+                       leftOver++;
 
-                       /* Skip over any /s */
-                       while (yaffsfs_IsPathDivider(*leftOver))
-                               leftOver++;
+               while (matching && *p && *leftOver) {
 
                        /* Now match the text part */
                        while (matching &&
@@ -538,6 +539,16 @@ static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
                                        matching = 0;
                                }
                        }
+
+                       if ((*p && !yaffsfs_IsPathDivider(*p)) ||
+                           (*leftOver && !yaffsfs_IsPathDivider(*leftOver)))
+                               matching = 0;
+                       else {
+                               while (yaffsfs_IsPathDivider(*p))
+                                       p++;
+                               while (yaffsfs_IsPathDivider(*leftOver))
+                                       leftOver++;
+                       }
                }
 
                /* Skip over any /s in leftOver */
@@ -558,7 +569,6 @@ static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
                        retval = dev;
                        longestMatch = thisMatchLength;
                }
-
        }
        return retval;
 }
@@ -737,7 +747,20 @@ static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
        if (dirOut)
                *dirOut = dir;
 
-       if (dir && *name)
+       /* At this stage we have looked up directory part and have the name part
+        * in name if there is one.
+        *
+        *  eg /nand/x/ will give us a name of ""
+        *     /nand/x will give us a name of "x"
+        *
+        * Since the name part might be "." or ".." which need to be fixed.
+        */
+       if (dir && (yaffs_strcmp(name, _Y("..")) == 0)) {
+               dir = dir->parent;
+               obj = dir;
+       } else if (dir && (yaffs_strcmp(name, _Y(".")) == 0))
+               obj = dir;
+       else if (dir && *name)
                obj = yaffs_find_by_name(dir, name);
        else
                obj = dir;
@@ -830,7 +853,7 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                oflag &= ~(O_EXCL);
 
        /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
-       if ((oflag & O_CREAT) & (oflag & O_EXCL))
+       if ((oflag & O_CREAT) && (oflag & O_EXCL))
                oflag &= ~(O_TRUNC);
 
        /* Todo: Are there any more flag combos to sanitise ? */
@@ -896,10 +919,10 @@ int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
                        }
 
                        /* Check file permissions */
-                       if (readRequested && !(obj->yst_mode & S_IREAD))
+                       if (readRequested && !(obj->yst_mode & S_IRUSR))
                                openDenied = 1;
 
-                       if (writeRequested && !(obj->yst_mode & S_IWRITE))
+                       if (writeRequested && !(obj->yst_mode & S_IWUSR))
                                openDenied = 1;
 
                        if (!errorReported && writeRequested &&
@@ -1077,7 +1100,7 @@ static int yaffs_Dofsync(int handle, int datasync)
        else if (obj->my_dev->read_only)
                yaffsfs_SetError(-EROFS);
        else {
-               yaffs_flush_file(obj, 1, datasync);
+               yaffs_flush_file(obj, 1, datasync, 0);
                retVal = 0;
        }
 
@@ -1119,7 +1142,7 @@ int yaffs_close(int handle)
        else {
                /* clean up */
                if(!f->isDir)
-                       yaffs_flush_file(obj, 1, 0);
+                       yaffs_flush_file(obj, 1, 0, 1);
                yaffsfs_PutHandle(handle);
                retVal = 0;
        }
@@ -1566,6 +1589,35 @@ int yaffs_unlink(const YCHAR *path)
        return yaffs_unlink_reldir(NULL, path);
 }
 
+int yaffs_funlink(int fd)
+{
+       struct yaffs_obj *obj;
+       int retVal = -1;
+
+       yaffsfs_Lock();
+       obj = yaffsfs_HandleToObject(fd);
+
+       if (!obj)
+               yaffsfs_SetError(-EBADF);
+       else if (obj->my_dev->read_only)
+               yaffsfs_SetError(-EROFS);
+       else if (!isDirectory &&
+                obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
+               yaffsfs_SetError(-EISDIR);
+       else if (isDirectory &&
+                obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
+               yaffsfs_SetError(-ENOTDIR);
+       else if (isDirectory && obj == obj->my_dev->root_dir)
+               yaffsfs_SetError(-EBUSY);       /* Can't rmdir a root */
+       else if (yaffs_unlink_obj(oobj) == YAFFS_OK)
+                       retVal = 0;
+
+       yaffsfs_Unlock();
+
+       return retVal;
+}
+
+
 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
 {
        if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
@@ -1854,8 +1906,6 @@ static int yaffsfs_DoUtime(struct yaffs_obj *obj,
                           const struct yaffs_utimbuf *buf)
 {
        int retVal = -1;
-       int result;
-
        struct yaffs_utimbuf local;
 
        obj = yaffs_get_equivalent_obj(obj);
@@ -1865,6 +1915,7 @@ static int yaffsfs_DoUtime(struct yaffs_obj *obj,
                return -1;
        }
 
+#if !CONFIG_YAFFS_WINCE
        if (!buf) {
                local.actime = Y_CURRENT_TIME;
                local.modtime = local.actime;
@@ -1872,12 +1923,15 @@ static int yaffsfs_DoUtime(struct yaffs_obj *obj,
        }
 
        if (obj) {
+               int result;
+
                obj->yst_atime = buf->actime;
                obj->yst_mtime = buf->modtime;
                obj->dirty = 1;
-               result = yaffs_flush_file(obj, 0, 0);
+               result = yaffs_flush_file(obj, 0, 0, 0);
                retVal = result == YAFFS_OK ? 0 : -1;
        }
+#endif
 
        return retVal;
 }
@@ -2460,7 +2514,7 @@ int yaffs_set_wince_times(int fd,
                }
 
                obj->dirty = 1;
-               result = yaffs_flush_file(obj, 0, 0);
+               result = yaffs_flush_file(obj, 0, 0, 0);
                retVal = 0;
        } else
                /* bad handle */
@@ -2483,7 +2537,7 @@ static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
        if (obj) {
                obj->yst_mode = mode;
                obj->dirty = 1;
-               result = yaffs_flush_file(obj, 0, 0);
+               result = yaffs_flush_file(obj, 0, 0, 0);
        }
 
        return result == YAFFS_OK ? 0 : -1;
@@ -2528,11 +2582,11 @@ int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
        else {
                int access_ok = 1;
 
-               if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
+               if ((amode & R_OK) && !(obj->yst_mode & S_IRUSR))
                        access_ok = 0;
-               if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
+               if ((amode & W_OK) && !(obj->yst_mode & S_IWUSR))
                        access_ok = 0;
-               if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
+               if ((amode & X_OK) && !(obj->yst_mode & S_IXUSR))
                        access_ok = 0;
 
                if (!access_ok)
@@ -2838,7 +2892,9 @@ int yaffs_mount(const YCHAR *path)
        return yaffs_mount_common(NULL, path, 0, 0);
 }
 
-int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
+static int yaffs_sync_common(struct yaffs_dev *dev,
+                            const YCHAR *path,
+                            int do_checkpt)
 {
        int retVal = -1;
        YCHAR *dummy;
@@ -2866,8 +2922,9 @@ int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
                        yaffsfs_SetError(-EROFS);
                else {
 
-                       yaffs_flush_whole_cache(dev);
-                       yaffs_checkpoint_save(dev);
+                       yaffs_flush_whole_cache(dev, 0);
+                       if (do_checkpt)
+                               yaffs_checkpoint_save(dev);
                        retVal = 0;
 
                }
@@ -2878,14 +2935,75 @@ int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
        return retVal;
 }
 
+int yaffs_sync_files_reldev(struct yaffs_dev *dev)
+{
+       return yaffs_sync_common(dev, NULL, 0);
+}
+
+int yaffs_sync_files(const YCHAR *path)
+{
+       return yaffs_sync_common(NULL, path, 0);
+}
+
 int yaffs_sync_reldev(struct yaffs_dev *dev)
 {
-       return yaffs_sync_common(dev, NULL);
+       return yaffs_sync_common(dev, NULL, 1);
 }
 
 int yaffs_sync(const YCHAR *path)
 {
-       return yaffs_sync_common(NULL, path);
+       return yaffs_sync_common(NULL, path, 1);
+}
+
+
+static int yaffsfs_bg_gc_common(struct yaffs_dev *dev,
+                               const YCHAR *path,
+                               int urgency)
+{
+       int retVal = -1;
+       YCHAR *dummy;
+
+       if (!dev) {
+               if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
+                       yaffsfs_SetError(-EFAULT);
+                       return -1;
+               }
+
+               if (yaffsfs_CheckPath(path) < 0) {
+                       yaffsfs_SetError(-ENAMETOOLONG);
+                       return -1;
+               }
+       }
+
+       yaffsfs_Lock();
+       if (!dev)
+               dev = yaffsfs_FindDevice(path, &dummy);
+
+       if (dev) {
+               if (!dev->is_mounted)
+                       yaffsfs_SetError(-EINVAL);
+               else
+                       retVal = yaffs_bg_gc(dev, urgency);
+       } else
+               yaffsfs_SetError(-ENODEV);
+
+       yaffsfs_Unlock();
+       return retVal;
+}
+
+/* Background gc functions.
+ * These return 0 when bg done or greater than 0 when gc has been
+ * done and there is still a lot of garbage to be cleaned up.
+ */
+
+int yaffs_do_background_gc(const YCHAR *path, int urgency)
+{
+       return yaffsfs_bg_gc_common(NULL, path, urgency);
+}
+
+int yaffs_do_background_gc_reldev(struct yaffs_dev *dev, int urgency)
+{
+       return yaffsfs_bg_gc_common(dev, NULL, urgency);
 }
 
 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
@@ -2922,7 +3040,7 @@ int yaffs_remount_common(struct yaffs_dev *dev, const YCHAR *path,
 
        if (dev) {
                if (dev->is_mounted) {
-                       yaffs_flush_whole_cache(dev);
+                       yaffs_flush_whole_cache(dev, 0);
 
                        if (force || !yaffsfs_IsDevBusy(dev)) {
                                if (read_only)
@@ -2973,7 +3091,7 @@ int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
        if (dev) {
                if (dev->is_mounted) {
                        int inUse;
-                       yaffs_flush_whole_cache(dev);
+                       yaffs_flush_whole_cache(dev, 0);
                        yaffs_checkpoint_save(dev);
                        inUse = yaffsfs_IsDevBusy(dev);
                        if (!inUse || force) {
@@ -3046,7 +3164,7 @@ int yaffs_format_common(struct yaffs_dev *dev,
 
                if (dev->is_mounted && unmount_flag) {
                        int inUse;
-                       yaffs_flush_whole_cache(dev);
+                       yaffs_flush_whole_cache(dev, 0);
                        yaffs_checkpoint_save(dev);
                        inUse = yaffsfs_IsDevBusy(dev);
                        if (!inUse || force_unmount_flag) {
@@ -3365,6 +3483,7 @@ static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(struct yaffs_obj *reldir,
        }
 
        obj = yaffsfs_FindObject(reldir, dirname, 0, 1, NULL, &notDir, &loop);
+       obj = yaffsfs_FollowLink(obj, 0, &loop);
 
        if (!obj && notDir)
                yaffsfs_SetError(-ENOTDIR);
@@ -3410,6 +3529,12 @@ yaffs_DIR *yaffs_opendir_reldir(struct yaffs_obj *reldir, const YCHAR *dirname)
        yaffsfs_Unlock();
        return ret;
 }
+
+yaffs_DIR *yaffs_opendir_reldev(struct yaffs_dev *dev, const YCHAR *dirname)
+{
+       return yaffs_opendir_reldir(ROOT_DIR(dev), dirname);
+}
+
 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
 {
        return yaffs_opendir_reldir(NULL, dirname);
@@ -3518,6 +3643,7 @@ static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
 
        return 0;
 }
+
 int yaffs_closedir(yaffs_DIR *dirp)
 {
        int ret;
@@ -3764,6 +3890,11 @@ int yaffs_set_error(int error)
        return 0;
 }
 
+struct yaffs_obj * yaffs_get_obj_from_fd(int handle)
+{
+       return yaffsfs_HandleToObject(handle);
+}
+
 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
 {
 #if 1