yaffsfs: Add yaffs_format function
authorCharles Manning <cdhmanning@gmail.com>
Wed, 25 Jul 2012 01:04:11 +0000 (13:04 +1200)
committerCharles Manning <cdhmanning@gmail.com>
Wed, 25 Jul 2012 01:04:11 +0000 (13:04 +1200)
Allows the formatting of a yaffs device

Signed-off-by: Charles Manning <cdhmanning@gmail.com>
direct/basic-test/dtest.c
direct/u-boot/fs/yaffs2/Makefile
direct/u-boot/patch-u-boot.sh
direct/yaffsfs.c
direct/yaffsfs.h
yaffs_guts.c
yaffs_guts.h

index 153d274..9f2d2e6 100644 (file)
@@ -3081,6 +3081,32 @@ void readdir_test(const char *mountpt)
 
 }
 
+void format_test(const char *mountpt)
+{
+       int ret;
+
+       yaffs_start_up();
+
+       ret = yaffs_format(mountpt, 0, 0, 0);
+       printf("yaffs_format(...,0, 0, 0) of unmounted returned %d\n", ret);
+
+       yaffs_mount(mountpt);
+
+       ret = yaffs_format(mountpt, 0, 0, 0);
+       printf("yaffs_format(...,0, 0, 0) of mounted returned %d\n", ret);
+
+       ret = yaffs_format(mountpt, 1, 0, 0);
+       printf("yaffs_format(...,1, 0, 0) of mounted returned %d\n", ret);
+
+       ret = yaffs_mount(mountpt);
+       printf("mount should return 0 returned %d\n", ret);
+
+       ret = yaffs_format(mountpt, 1, 0, 1);
+       printf("yaffs_format(...,1, 0, 1) of mounted returned %d\n", ret);
+
+       ret = yaffs_mount(mountpt);
+       printf("mount should return -1 returned %d\n", ret);
+}
 
 int random_seed;
 int simulate_power_failure;
@@ -3149,7 +3175,10 @@ int main(int argc, char *argv[])
         // link_follow_test("/yaffs2");
         //basic_utime_test("/yaffs2");
 
-       max_files_test("/yaffs2");
+
+       format_test("/yaffs2");
+
+       //max_files_test("/yaffs2");
 
         //start_twice("/yaffs2");
 
index d2ae2bf..f45c19d 100644 (file)
@@ -35,6 +35,11 @@ YCFLAGS =  -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_SHORT_NAMES_IN_RAM
 YCFLAGS += -DCONFIG_YAFFS_YAFFS2 -DNO_Y_INLINE
 YCFLAGS += -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES
 
+#
+# To enable 32-bit loff_t uncomment the following 
+#
+YCFLAGS += -DY_LOFF_T=int
+
 CFLAGS += $(YCFLAGS)
 CPPFLAGS +=  $(YCFLAGS)
 
index 8caa2e8..fc17552 100755 (executable)
@@ -17,7 +17,7 @@
 #
 #  Inspired yaffs kernel patching script
 
-set -e -x
+#set -e -x
 
 UBOOTDIR=$1
 
index d208e25..705106e 100644 (file)
@@ -2648,6 +2648,67 @@ int yaffs_unmount(const YCHAR *path)
        return yaffs_unmount2(path, 0);
 }
 
+int yaffs_format(const YCHAR *path,
+               int unmount_flag,
+               int force_unmount_flag,
+               int remount_flag)
+{
+       int retVal = 0;
+       struct yaffs_dev *dev = NULL;
+       int result;
+
+       if (!path) {
+               yaffsfs_SetError(-EFAULT);
+               return -1;
+       }
+
+       if (yaffsfs_CheckPath(path) < 0) {
+               yaffsfs_SetError(-ENAMETOOLONG);
+               return -1;
+       }
+
+       yaffsfs_Lock();
+       dev = yaffsfs_FindMountPoint(path);
+
+       if (dev) {
+               int was_mounted = dev->is_mounted;
+
+               if (dev->is_mounted && unmount_flag) {
+                       int inUse;
+                       yaffs_flush_whole_cache(dev);
+                       yaffs_checkpoint_save(dev);
+                       inUse = yaffsfs_IsDevBusy(dev);
+                       if (!inUse || force_unmount_flag) {
+                               if (inUse)
+                                       yaffsfs_BreakDeviceHandles(dev);
+                               yaffs_deinitialise(dev);
+                       }
+               }
+
+               if(dev->is_mounted) {
+                               yaffsfs_SetError(-EBUSY);
+                               retVal = -1;
+               } else {
+                       yaffs_format_dev(dev);
+                       if(was_mounted && remount_flag) {
+                               result = yaffs_guts_initialise(dev);
+                               if (result == YAFFS_FAIL) {
+                                       yaffsfs_SetError(-ENOMEM);
+                                       retVal = -1;
+                               }
+                       }
+               }
+       } else {
+               yaffsfs_SetError(-ENODEV);
+               retVal = -1;
+       }
+
+       yaffsfs_Unlock();
+       return retVal;
+
+}
+
+
 Y_LOFF_T yaffs_freespace(const YCHAR *path)
 {
        Y_LOFF_T retVal = -1;
index ba3f87f..b863901 100644 (file)
@@ -171,6 +171,10 @@ int yaffs_unmount(const YCHAR *path) ;
 int yaffs_unmount2(const YCHAR *path, int force);
 int yaffs_remount(const YCHAR *path, int force, int read_only);
 
+int yaffs_format(const YCHAR *path,
+               int unmount_flag,
+               int force_unmount_flag,
+               int remount_flag);
 
 int yaffs_sync(const YCHAR *path) ;
 
index 1602c4b..b67055a 100644 (file)
@@ -4986,6 +4986,42 @@ int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
        return n_free;
 }
 
+
+int yaffs_format_dev(struct yaffs_dev *dev)
+{
+       int i;
+       enum yaffs_block_state state;
+       u32 dummy;
+
+       if(dev->is_mounted)
+               return YAFFS_FAIL;
+
+       /*
+       * The runtime variables might not have been set up,
+       * so set up what we need.
+       */
+       dev->internal_start_block = dev->param.start_block;
+       dev->internal_end_block = dev->param.end_block;
+       dev->block_offset = 0;
+       dev->chunk_offset = 0;
+
+       if (dev->param.start_block == 0) {
+               dev->internal_start_block = dev->param.start_block + 1;
+               dev->internal_end_block = dev->param.end_block + 1;
+               dev->block_offset = 1;
+               dev->chunk_offset = dev->param.chunks_per_block;
+       }
+
+       for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
+               yaffs_query_init_block_state(dev, i, &state, &dummy);
+               if (state != YAFFS_BLOCK_STATE_DEAD)
+                       yaffs_erase_block(dev, i);
+       }
+
+       return YAFFS_OK;
+}
+
+
 /*
  * Marshalling functions to get loff_t file sizes into and out of
  * object headers.
index 1aefb85..0e334bd 100644 (file)
@@ -961,6 +961,8 @@ u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
 
 int yaffs_is_non_empty_dir(struct yaffs_obj *obj);
 
+int yaffs_format_dev(struct yaffs_dev *dev);
+
 void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
                                int *chunk_out, u32 *offset_out);
 /*