shithub: lwext4

Download patch

ref: 31e09211a04626c08219c545a1e8885a34376750
parent: 6b8e5b91d54162a369f8962b09bbf07fb45eae6f
author: gkostka <kostka.grzegorz@gmail.com>
date: Thu Oct 10 16:15:09 EDT 2013

Add mount point stats.
Last char of mount point should be '/'

--- a/src/demos/generic/main.c
+++ b/src/demos/generic/main.c
@@ -121,7 +121,28 @@
 	ext4_dir_close(&d);
 }
 
+static void mp_stats(void)
+{
+    struct ext4_mount_stats stats;
+    ext4_mount_point_stats("/mp/", &stats);
 
+    printf("**********************************************\n");
+    printf("ext4_mount_point_stats\n");
+    printf("inodes_count        = %d\n", stats.inodes_count);
+    printf("free_inodes_count   = %d\n", stats.free_inodes_count);
+    printf("blocks_count        = %d\n", stats.blocks_count);
+    printf("free_blocks_count   = %d\n", stats.free_blocks_count);
+    printf("block_size          = %d\n", stats.block_size);
+    printf("block_group_count   = %d\n", stats.block_group_count);
+    printf("blocks_per_group    = %d\n", stats.blocks_per_group);
+    printf("inodes_per_group    = %d\n", stats.inodes_per_group);
+    printf("volume_name         = %s\n", stats.volume_name);
+
+    printf("**********************************************\n");
+
+}
+
+
 int main(int argc, char **argv)
 {
 	int option_index = 0;
@@ -196,7 +217,7 @@
 		return EXIT_FAILURE;
 	}
 
-	r = ext4_mount("ext4_filesim", "/mp");
+	r = ext4_mount("ext4_filesim", "/mp/");
 	if(r != EOK){
 		printf("ext4_mount ERROR = %d\n", r);
 		return EXIT_FAILURE;
@@ -203,9 +224,17 @@
 	}
 
 
+	ext4_fremove("/mp/hello.txt");
 	ext4_fremove("/mp/test1");
+	mp_stats();
 	dir_ls("/mp/");
 
+    /*Add hello world file.*/
+    r = ext4_fopen(&f, "/mp/hello.txt", "wb");
+    r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);
+    r = ext4_fclose(&f);
+
+
 	printf("ext4_fopen: test1\n");
 
 	r = ext4_fopen(&f, "/mp/test1", "wb");
@@ -264,9 +293,10 @@
 	r = ext4_fclose(&f);
 
 
+	mp_stats();
 	dir_ls("/mp/");
 
-	r = ext4_umount("/mp");
+	r = ext4_umount("/mp/");
 
 	printf("Test finish: OK\n");
     return EXIT_SUCCESS;
--- a/src/ext4.h
+++ b/src/ext4.h
@@ -169,8 +169,9 @@
 /**@brief	Mount a block device with EXT4 partition to the mountpoint.
  * @param	dev_name block device name (@ref ext4_device_register)
  * @param	mount_point pount point, for example
- * 			-	/my_partition
- * 			-	/my_second_partition
+ *          -   /
+ *          -   /my_partition/
+ *          -   /my_second_partition/
  *
  * @return standard error code */
 int	ext4_mount(const char * dev_name,  char *mount_point);
@@ -180,10 +181,29 @@
  * @return  standard error code */
 int	ext4_umount(char *mount_point);
 
+
+/**@brief   Some of the filesystem params.*/
+struct ext4_mount_stats {
+    uint32_t inodes_count;
+    uint32_t free_inodes_count;
+    uint64_t blocks_count;
+    uint64_t free_blocks_count;
+
+    uint32_t block_size;
+    uint32_t block_group_count;
+    uint32_t blocks_per_group;
+    uint32_t inodes_per_group;
+
+    char volume_name[16];
+};
+
+int ext4_mount_point_stats(const char *mount_point,
+    struct ext4_mount_stats *stats);
+
 /********************************FILE OPERATIONS*****************************/
 
 /**@brief	*/
-int	    ext4_fremove(const char *path);
+int ext4_fremove(const char *path);
 
 /**@brief	File open function.
  * @param	filename, (has to start from mountpoint)
@@ -204,19 +224,19 @@
  *  |---------------------------------------------------------------|
  *
  * @return	standard error code*/
-int	ext4_fopen (ext4_file *f, const char *path, const char *flags);
+int ext4_fopen (ext4_file *f, const char *path, const char *flags);
 
 /**@brief	*/
-int	ext4_fclose(ext4_file *f);
+int ext4_fclose(ext4_file *f);
 
 /**@brief	*/
-int	ext4_fread (ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);
+int ext4_fread (ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);
 
 /**@brief	*/
-int	ext4_fwrite(ext4_file *f, void *buf, uint32_t size, uint32_t *wcnt);
+int ext4_fwrite(ext4_file *f, void *buf, uint32_t size, uint32_t *wcnt);
 
 /**@brief	*/
-int	ext4_fseek (ext4_file *f, uint64_t offset, uint32_t origin);
+int ext4_fseek (ext4_file *f, uint64_t offset, uint32_t origin);
 
 /**@brief	*/
 uint64_t ext4_ftell (ext4_file *f);
--- a/src/lwext4/ext4.c
+++ b/src/lwext4/ext4.c
@@ -71,7 +71,7 @@
     struct ext4_fs	 fs;
 
     /**@brief	Dynamic alocation cache flag.*/
-    bool			cache_dynamic;
+    bool            cache_dynamic;
 };
 
 /**@brief	Block devices descriptor.*/
@@ -315,6 +315,9 @@
     struct ext4_bcache		*bc = 0;
     struct ext4_mountpoint	*mp = 0;
 
+    if(mount_point[strlen(mount_point) - 1] != '/')
+        return ENOTSUP;
+
     for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
         if(_bdevices[i].name){
             if(!strcmp(dev_name, _bdevices[i].name)){
@@ -418,7 +421,41 @@
     return ext4_block_fini(mp->fs.bdev);
 }
 
+int ext4_mount_point_stats(const char *mount_point,
+    struct ext4_mount_stats *stats)
+{
+    uint32_t i;
+    struct ext4_mountpoint    *mp = 0;
 
+    for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
+        if(_mp[i].name){
+            if(!strcmp(_mp[i].name, mount_point))
+                mp = &_mp[i];
+            break;
+        }
+    }
+
+    if(!mp)
+        return ENOENT;
+
+
+    EXT4_MP_LOCK(mp);
+    stats->inodes_count      = ext4_get32(&mp->fs.sb, inodes_count);
+    stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
+    stats->blocks_count      = ext4_sb_get_blocks_cnt(&mp->fs.sb);
+    stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
+    stats->block_size        = ext4_sb_get_block_size(&mp->fs.sb);
+
+    stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
+    stats->blocks_per_group  = ext4_get32(&mp->fs.sb, blocks_per_group);
+    stats->inodes_per_group  = ext4_get32(&mp->fs.sb, inodes_per_group);
+
+    memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
+    EXT4_MP_UNLOCK(mp);
+
+    return EOK;
+}
+
 /********************************FILE OPERATIONS*****************************/
 
 static struct ext4_mountpoint*  ext4_get_mount(const char *path)
@@ -517,12 +554,6 @@
     /*Skip mount point*/
     path += strlen(mp->name);
 
-    /*Skip first /*/
-    if(*path != '/')
-        return ENOENT;
-
-    path++;
-
     EXT4_MP_LOCK(mp);
 
     /*Load root*/
@@ -676,12 +707,6 @@
 
     /*Skip mount point*/
     path += strlen(mp->name);
-
-    /*Skip first /*/
-    if(*path != '/')
-        return ENOENT;
-
-    path++;
 
     /*Lock mountpoint*/
     EXT4_MP_LOCK(mp);