More clean up
[yaffs2.git] / direct / yaffsfs.c
index 0b231338ad0dfaa8a8562b24e28cf685a317dea1..eaf30808862179807c9540445e3d62ad6fdcc07b 100644 (file)
@@ -43,7 +43,7 @@ static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(
                                struct yaffs_obj *reldir, const YCHAR *dirname);
 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirent);
 
-unsigned int yaffs_wr_attempts;
+int yaffs_wr_attempts;
 
 /*
  * Handle management.
@@ -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;
 
@@ -782,7 +783,7 @@ int yaffs_dup(int handle)
 
 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
 {
-       int current_objects = dev->n_obj - dev->n_deleted_files;
+       u32 current_objects = dev->n_obj - dev->n_deleted_files;
 
        if (dev->param.max_objects && current_objects > dev->param.max_objects)
                return 1;
@@ -1077,7 +1078,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 +1120,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;
        }
@@ -1138,7 +1139,7 @@ static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
        Y_LOFF_T startPos = 0;
        Y_LOFF_T endPos = 0;
        int nRead = 0;
-       int nToRead = 0;
+       unsigned int nToRead = 0;
        int totalRead = 0;
        Y_LOFF_T maxRead;
        u8 *buf = (u8 *) vbuf;
@@ -1160,9 +1161,6 @@ static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
                /* Not a reading handle */
                yaffsfs_SetError(-EINVAL);
                totalRead = -1;
-       } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
-               yaffsfs_SetError(-EINVAL);
-               totalRead = -1;
        } else {
                if (isPread)
                        startPos = offset;
@@ -1176,7 +1174,7 @@ static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
                else
                        maxRead = 0;
 
-               if (nbyte > maxRead)
+               if ((Y_LOFF_T)nbyte > maxRead)
                        nbyte = maxRead;
 
                yaffsfs_GetHandle(handle);
@@ -1184,7 +1182,6 @@ static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
                endPos = pos + nbyte;
 
                if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
-                   nbyte > YAFFS_MAX_FILE_SIZE ||
                    endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
                        totalRead = -1;
                        nbyte = 0;
@@ -1212,7 +1209,7 @@ static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
                                buf += nRead;
                        }
 
-                       if (nRead == nToRead)
+                       if (nRead == (int)nToRead)
                                nbyte -= nRead;
                        else
                                nbyte = 0;      /* no more to read */
@@ -1262,7 +1259,7 @@ static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
        int nWritten = 0;
        int totalWritten = 0;
        int write_trhrough = 0;
-       int nToWrite = 0;
+       unsigned int nToWrite = 0;
        const u8 *buf = (const u8 *)vbuf;
 
        if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
@@ -1297,7 +1294,6 @@ static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
                endPos = pos + nbyte;
 
                if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
-                   nbyte > YAFFS_MAX_FILE_SIZE ||
                    endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
                        totalWritten = -1;
                        nbyte = 0;
@@ -1327,7 +1323,7 @@ static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
                                buf += nWritten;
                        }
 
-                       if (nWritten == nToWrite)
+                       if (nWritten == (int)nToWrite)
                                nbyte -= nToWrite;
                        else
                                nbyte = 0;
@@ -1713,7 +1709,7 @@ static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
        obj = yaffs_get_equivalent_obj(obj);
 
        if (obj && buf) {
-               buf->st_dev = (int)obj->my_dev->os_context;
+               buf->st_dev = 0;
                buf->st_ino = obj->obj_id;
                buf->st_mode = obj->yst_mode & ~S_IFMT;
 
@@ -1854,9 +1850,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,19 +1858,27 @@ static int yaffsfs_DoUtime(struct yaffs_obj *obj,
                return -1;
        }
 
-       if (!buf) {
-               local.actime = Y_CURRENT_TIME;
-               local.modtime = local.actime;
-               buf = &local;
-       }
+#if !CONFIG_YAFFS_WINCE
+       {
+               struct yaffs_utimbuf local;
 
-       if (obj) {
-               obj->yst_atime = buf->actime;
-               obj->yst_mtime = buf->modtime;
-               obj->dirty = 1;
-               result = yaffs_flush_file(obj, 0, 0);
-               retVal = result == YAFFS_OK ? 0 : -1;
+               if (!buf) {
+                       local.actime = Y_CURRENT_TIME;
+                       local.modtime = local.actime;
+                       buf = &local;
+               }
+
+               if (obj) {
+                       int result;
+
+                       obj->yst_atime = buf->actime;
+                       obj->yst_mtime = buf->modtime;
+                       obj->dirty = 1;
+                       result = yaffs_flush_file(obj, 0, 0, 0);
+                       retVal = result == YAFFS_OK ? 0 : -1;
+               }
        }
+#endif
 
        return retVal;
 }
@@ -2460,7 +2461,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 +2484,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;
@@ -2866,7 +2867,7 @@ int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
                        yaffsfs_SetError(-EROFS);
                else {
 
-                       yaffs_flush_whole_cache(dev);
+                       yaffs_flush_whole_cache(dev, 0);
                        yaffs_checkpoint_save(dev);
                        retVal = 0;
 
@@ -2888,6 +2889,57 @@ int yaffs_sync(const YCHAR *path)
        return yaffs_sync_common(NULL, path);
 }
 
+
+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)
 {
        int i;
@@ -2922,7 +2974,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 +3025,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 +3098,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) {
@@ -3410,6 +3462,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);
@@ -3428,7 +3486,7 @@ struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
                if (dsc->nextReturn) {
                        dsc->de.d_ino =
                            yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
-                       dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
+                       dsc->de.d_dont_use = dsc->nextReturn;
                        dsc->de.d_off = dsc->offset++;
                        yaffs_get_obj_name(dsc->nextReturn,
                                           dsc->de.d_name, NAME_MAX);