Skip to contentSkip to conversation

Add some comments about const-ness around iterate API

PATCH Superseded Kelvin Zhang via Linux-erofs 3 participants 14 messages Dec 21, 2021 – Jan 5, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Add some comments about const-ness around iterate API

Change-Id I297a56ba14a37ef5eced95330a5b09109378ca44
---
 include/erofs/dir.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/erofs/dir.h +8 -0
index 77656ca..59bd40d 100644
@@ -39,6 +39,14 @@ typedef int (*erofs_readdir_cb)(struct erofs_dir_context *);
  * the callback context. |de_namelen| is the exact dirent name length.
  */
 struct erofs_dir_context {
+	/* During execution of |erofs_iterate_dir|, the function needs
+	 * to read the values inside |erofs_inode* dir|. So it is important
+	 * that the callback function does not modify stuct pointed by
+	 * |dir|. It is OK to repoint |dir| to other objects.
+	 * Unfortunately, it's not possible to enforce this restriction
+	 * with const keyword, as |erofs_iterate_dir| needs to modify
+	 * struct pointed by |dir|.
+	 */
 	struct erofs_inode *dir;
 	erofs_readdir_cb cb;
 	erofs_nid_t pnid;		/* optional */
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#1 lore raw

PATCH 2/2 Add API to get on disk size of an inode

Change-Id I60fa9346737b14418bd3fa1d12f760aaf0a0cca5
---
 dump/main.c              |  4 ++--
 include/erofs/internal.h |  2 ++
 lib/data.c               | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
dump/main.c +2 -2
index 71b44b4..cdde561 100644
@@ -175,7 +175,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 	return 0;
 }
 
-static int erofs_get_occupied_size(struct erofs_inode *inode,
+static int dump_get_occupied_size(struct erofs_inode *inode,
 		erofs_off_t *size)
 {
 	*size = 0;
@@ -291,7 +291,7 @@ static int erofs_read_dirent(struct erofs_dirent *de,
 		return err;
 	}
 
-	err = erofs_get_occupied_size(&inode, &occupied_size);
+	err = dump_get_occupied_size(&inode, &occupied_size);
 	if (err) {
 		erofs_err("get file size failed\n");
 		return err;
include/erofs/internal.h +2 -0
index 947304f..8f13e69 100644
@@ -320,6 +320,8 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+			    erofs_off_t *size);
 /* zmap.c */
 int z_erofs_fill_inode(struct erofs_inode *vi);
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
lib/data.c +21 -0
index 27710f9..92e54b5 100644
@@ -320,3 +320,24 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 	}
 	return -EINVAL;
 }
+
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+			    erofs_off_t *size)
+{
+	*size = 0;
+	switch (inode->datalayout) {
+	case EROFS_INODE_FLAT_INLINE:
+	case EROFS_INODE_FLAT_PLAIN:
+	case EROFS_INODE_CHUNK_BASED:
+		*size = inode->i_size;
+		break;
+	case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+	case EROFS_INODE_FLAT_COMPRESSION:
+		*size = inode->u.i_blocks * EROFS_BLKSIZ;
+		break;
+	default:
+		erofs_err("unknown datalayout");
+		return -1;
+	}
+	return 0;
+}
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#2 lore raw

Re: [PATCH v1 2/2] Add API to get on disk size of an inode

Quoted 2 lines · Kelvin Zhang
Change-Id I60fa9346737b14418bd3fa1d12f760aaf0a0cca5

The same to the previous patch.

Also could we remove dump_get_occupied_size() entirely?

Thanks,
Gao Xiang

Quoted 72 lines
---
 dump/main.c              |  4 ++--
 include/erofs/internal.h |  2 ++
 lib/data.c               | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
dump/main.c +2 -2
index 71b44b4..cdde561 100644
@@ -175,7 +175,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 	return 0;
 }
 
-static int erofs_get_occupied_size(struct erofs_inode *inode,
+static int dump_get_occupied_size(struct erofs_inode *inode,
 		erofs_off_t *size)
 {
 	*size = 0;
@@ -291,7 +291,7 @@ static int erofs_read_dirent(struct erofs_dirent *de,
 		return err;
 	}
 
-	err = erofs_get_occupied_size(&inode, &occupied_size);
+	err = dump_get_occupied_size(&inode, &occupied_size);
 	if (err) {
 		erofs_err("get file size failed\n");
 		return err;
include/erofs/internal.h +2 -0
index 947304f..8f13e69 100644
@@ -320,6 +320,8 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+			    erofs_off_t *size);
 /* zmap.c */
 int z_erofs_fill_inode(struct erofs_inode *vi);
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
lib/data.c +21 -0
index 27710f9..92e54b5 100644
@@ -320,3 +320,24 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 	}
 	return -EINVAL;
 }
+
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+			    erofs_off_t *size)
+{
+	*size = 0;
+	switch (inode->datalayout) {
+	case EROFS_INODE_FLAT_INLINE:
+	case EROFS_INODE_FLAT_PLAIN:
+	case EROFS_INODE_CHUNK_BASED:
+		*size = inode->i_size;
+		break;
+	case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+	case EROFS_INODE_FLAT_COMPRESSION:
+		*size = inode->u.i_blocks * EROFS_BLKSIZ;
+		break;
+	default:
+		erofs_err("unknown datalayout");
+		return -1;
+	}
+	return 0;
+}
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#3 lore raw

Re: [PATCH v1 2/2] Add API to get on disk size of an inode

Not really, dump_get_occupied_size() contains some custom logic for counting stats.

Quoted 84 lines · Gao Xiang
Quoted 2 lines · Kelvin Zhang
Change-Id I60fa9346737b14418bd3fa1d12f760aaf0a0cca5

The same to the previous patch.

Also could we remove dump_get_occupied_size() entirely?

Thanks,
Gao Xiang

Quoted 72 lines
---
 dump/main.c              |  4 ++--
 include/erofs/internal.h |  2 ++
 lib/data.c               | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
dump/main.c +2 -2
index 71b44b4..cdde561 100644
@@ -175,7 +175,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
      return 0;
 }
 
-static int erofs_get_occupied_size(struct erofs_inode *inode,
+static int dump_get_occupied_size(struct erofs_inode *inode,
              erofs_off_t *size)
 {
      *size = 0;
@@ -291,7 +291,7 @@ static int erofs_read_dirent(struct erofs_dirent *de,
              return err;
      }
 
-     err = erofs_get_occupied_size(&inode, &occupied_size);
+     err = dump_get_occupied_size(&inode, &occupied_size);
      if (err) {
              erofs_err("get file size failed\n");
              return err;
include/erofs/internal.h +2 -0
index 947304f..8f13e69 100644
@@ -320,6 +320,8 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 int erofs_map_blocks(struct erofs_inode *inode,
              struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+                         erofs_off_t *size);
 /* zmap.c */
 int z_erofs_fill_inode(struct erofs_inode *vi);
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
lib/data.c +21 -1
index 27710f9..92e54b5 100644
@@ -320,3 +320,24 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
      }
      return -EINVAL;
 }
+
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+                         erofs_off_t *size)
+{
+     *size = 0;
+     switch (inode->datalayout) {
+     case EROFS_INODE_FLAT_INLINE:
+     case EROFS_INODE_FLAT_PLAIN:
+     case EROFS_INODE_CHUNK_BASED:
+             *size = inode->i_size;
+             break;
+     case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+     case EROFS_INODE_FLAT_COMPRESSION:
+             *size = inode->u.i_blocks * EROFS_BLKSIZ;
+             break;
+     default:
+             erofs_err("unknown datalayout");
+             return -1;
+     }
+     return 0;
+}
--

2.34.1.307.g9b7440fafd-goog

Signature · 3 lines
Sincerely,

Kelvin Zhang
#4 lore raw

Re: [PATCH v1 2/2] Add API to get on disk size of an inode

Quoted 3 lines · Kelvin Zhang

Not really, dump_get_occupied_size() contains some custom logic for counting stats.

Ok, thanks. maybe it would be better to rename as erofsdump_get_occupied_size().
Also we could "static inline" such API since it's simple.

Thanks,
Gao Xiang

#5 lore raw

PATCH v3 1/2 erofs-utils: lib: Add some comments about const-ness around iterate API

The new iterate dir API has non-trivial const correctness requirements. Document them in comment.

---
 include/erofs/dir.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/erofs/dir.h +8 -0
index 77656ca..59bd40d 100644
@@ -39,6 +39,14 @@ typedef int (*erofs_readdir_cb)(struct erofs_dir_context *);
  * the callback context. |de_namelen| is the exact dirent name length.
  */
 struct erofs_dir_context {
+	/* During execution of |erofs_iterate_dir|, the function needs
+	 * to read the values inside |erofs_inode* dir|. So it is important
+	 * that the callback function does not modify stuct pointed by
+	 * |dir|. It is OK to repoint |dir| to other objects.
+	 * Unfortunately, it's not possible to enforce this restriction
+	 * with const keyword, as |erofs_iterate_dir| needs to modify
+	 * struct pointed by |dir|.
+	 */
 	struct erofs_inode *dir;
 	erofs_readdir_cb cb;
 	erofs_nid_t pnid;		/* optional */
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#6 lore raw

PATCH v3 2/2 erofs-utils: lib: Add API to get on disk size of an inode

Marginally improve code re-use. It's quite common for users to query for compressed size of an inode.

---
 dump/main.c              |  4 ++--
 include/erofs/internal.h | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)
dump/main.c +2 -2
index 71b44b4..4c2c513 100644
@@ -175,7 +175,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 	return 0;
 }
 
-static int erofs_get_occupied_size(struct erofs_inode *inode,
+static int erofsdump_get_occupied_size(struct erofs_inode *inode,
 		erofs_off_t *size)
 {
 	*size = 0;
@@ -291,7 +291,7 @@ static int erofs_read_dirent(struct erofs_dirent *de,
 		return err;
 	}
 
-	err = erofs_get_occupied_size(&inode, &occupied_size);
+	err = erofsdump_get_occupied_size(&inode, &occupied_size);
 	if (err) {
 		erofs_err("get file size failed\n");
 		return err;
include/erofs/internal.h +23 -0
index 947304f..c64cf36 100644
@@ -19,6 +19,7 @@ typedef unsigned short umode_t;
 
 #define __packed __attribute__((__packed__))
 
+#include "erofs/print.h"
 #include "erofs_fs.h"
 #include <fcntl.h>
 
@@ -320,6 +321,28 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
+
+static inline int erofs_get_occupied_size(const struct erofs_inode *inode,
+					  erofs_off_t *size)
+{
+	*size = 0;
+	switch (inode->datalayout) {
+	case EROFS_INODE_FLAT_INLINE:
+	case EROFS_INODE_FLAT_PLAIN:
+	case EROFS_INODE_CHUNK_BASED:
+		*size = inode->i_size;
+		break;
+	case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+	case EROFS_INODE_FLAT_COMPRESSION:
+		*size = inode->u.i_blocks * EROFS_BLKSIZ;
+		break;
+	default:
+		erofs_err("unknown datalayout");
+		return -1;
+	}
+	return 0;
+}
+
 /* zmap.c */
 int z_erofs_fill_inode(struct erofs_inode *vi);
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#7 lore raw
In reply to Kelvin Zhang via Linux-erofs

Hi Kelvin,

Quoted 2 lines · Kelvin Zhang
Change-Id I297a56ba14a37ef5eced95330a5b09109378ca44

Would you mind add "erofs-utils: lib: " to the subject prefix, also drop the "Change-Id:" field so I could apply it.

Adding some commit message is better, even it's trivial and simple.

Thanks,
Gao Xiang

Quoted 25 lines
---
 include/erofs/dir.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/erofs/dir.h +8 -0
index 77656ca..59bd40d 100644
@@ -39,6 +39,14 @@ typedef int (*erofs_readdir_cb)(struct erofs_dir_context *);
  * the callback context. |de_namelen| is the exact dirent name length.
  */
 struct erofs_dir_context {
+	/* During execution of |erofs_iterate_dir|, the function needs
+	 * to read the values inside |erofs_inode* dir|. So it is important
+	 * that the callback function does not modify stuct pointed by
+	 * |dir|. It is OK to repoint |dir| to other objects.
+	 * Unfortunately, it's not possible to enforce this restriction
+	 * with const keyword, as |erofs_iterate_dir| needs to modify
+	 * struct pointed by |dir|.
+	 */
 	struct erofs_inode *dir;
 	erofs_readdir_cb cb;
 	erofs_nid_t pnid;		/* optional */
Signature · 1 line
2.34.1.307.g9b7440fafd-goog
#8 lore raw

PATCH v2 1/3 erofs-utils: lib: Add some comments about const-ness around iterate API

The new iterate dir API has non-trivial const correctness requirements. Document them in comment.

---
 include/erofs/dir.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/erofs/dir.h +8 -0
index 77656ca..59bd40d 100644
@@ -39,6 +39,14 @@ typedef int (*erofs_readdir_cb)(struct erofs_dir_context *);
  * the callback context. |de_namelen| is the exact dirent name length.
  */
 struct erofs_dir_context {
+	/* During execution of |erofs_iterate_dir|, the function needs
+	 * to read the values inside |erofs_inode* dir|. So it is important
+	 * that the callback function does not modify stuct pointed by
+	 * |dir|. It is OK to repoint |dir| to other objects.
+	 * Unfortunately, it's not possible to enforce this restriction
+	 * with const keyword, as |erofs_iterate_dir| needs to modify
+	 * struct pointed by |dir|.
+	 */
 	struct erofs_inode *dir;
 	erofs_readdir_cb cb;
 	erofs_nid_t pnid;		/* optional */
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#9 lore raw

PATCH v2 2/3 erofs-utils: lib: Add API to get on disk size of an inode

Marginally improve code re-use. It's quite common for users to query for compressed size of an inode.

---
 dump/main.c              |  4 ++--
 include/erofs/internal.h |  2 ++
 lib/data.c               | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
dump/main.c +2 -2
index 71b44b4..cdde561 100644
@@ -175,7 +175,7 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 	return 0;
 }
 
-static int erofs_get_occupied_size(struct erofs_inode *inode,
+static int dump_get_occupied_size(struct erofs_inode *inode,
 		erofs_off_t *size)
 {
 	*size = 0;
@@ -291,7 +291,7 @@ static int erofs_read_dirent(struct erofs_dirent *de,
 		return err;
 	}
 
-	err = erofs_get_occupied_size(&inode, &occupied_size);
+	err = dump_get_occupied_size(&inode, &occupied_size);
 	if (err) {
 		erofs_err("get file size failed\n");
 		return err;
include/erofs/internal.h +2 -0
index 947304f..8f13e69 100644
@@ -320,6 +320,8 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 int erofs_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags);
 int erofs_map_dev(struct erofs_sb_info *sbi, struct erofs_map_dev *map);
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+			    erofs_off_t *size);
 /* zmap.c */
 int z_erofs_fill_inode(struct erofs_inode *vi);
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
lib/data.c +21 -0
index 27710f9..92e54b5 100644
@@ -320,3 +320,24 @@ int erofs_pread(struct erofs_inode *inode, char *buf,
 	}
 	return -EINVAL;
 }
+
+int erofs_get_occupied_size(const struct erofs_inode *inode,
+			    erofs_off_t *size)
+{
+	*size = 0;
+	switch (inode->datalayout) {
+	case EROFS_INODE_FLAT_INLINE:
+	case EROFS_INODE_FLAT_PLAIN:
+	case EROFS_INODE_CHUNK_BASED:
+		*size = inode->i_size;
+		break;
+	case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+	case EROFS_INODE_FLAT_COMPRESSION:
+		*size = inode->u.i_blocks * EROFS_BLKSIZ;
+		break;
+	default:
+		erofs_err("unknown datalayout");
+		return -1;
+	}
+	return 0;
+}
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#10 lore raw

Re: [PATCH v2 1/3] erofs-utils: lib: Add some comments about const-ness around iterate API

In reply to Kelvin Zhang via Linux-erofs

friendly ping

Quoted 31 lines · Kelvin Zhang

The new iterate dir API has non-trivial const correctness requirements. Document them in comment.

---
 include/erofs/dir.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/erofs/dir.h +8 -1
index 77656ca..59bd40d 100644
@@ -39,6 +39,14 @@ typedef int (*erofs_readdir_cb)(struct erofs_dir_context *);
  * the callback context. |de_namelen| is the exact dirent name length.
  */
 struct erofs_dir_context {
+       /* During execution of |erofs_iterate_dir|, the function needs
+        * to read the values inside |erofs_inode* dir|. So it is important
+        * that the callback function does not modify stuct pointed by
+        * |dir|. It is OK to repoint |dir| to other objects.
+        * Unfortunately, it's not possible to enforce this restriction
+        * with const keyword, as |erofs_iterate_dir| needs to modify
+        * struct pointed by |dir|.
+        */
        struct erofs_inode *dir;
        erofs_readdir_cb cb;
        erofs_nid_t pnid;               /* optional */
--

2.34.1.448.ga2b2bfdf31-goog

Signature · 3 lines
Sincerely,

Kelvin Zhang