Skip to contentSkip to conversation

erofs: use Z_EROFS_LCLUSTER_TYPE_MAX to simplify switches

PATCH RESEND Superseded Hongzhen Luo 2 participants 3 messages Feb 7, 2025 EROFS file system

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 erofs: use Z_EROFS_LCLUSTER_TYPE_MAX to simplify switches

There's no need to enumerate each type. No logic changes.

---
 fs/erofs/zmap.c | 59 ++++++++++++++++++-------------------------------
 1 file changed, 22 insertions(+), 37 deletions(-)
fs/erofs/zmap.c +22 -37
index 689437e99a5a..0ee78413bfd5 100644
@@ -265,24 +265,20 @@ static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
 		if (err)
 			return err;
 
-		switch (m->type) {
-		case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
+		if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
 			lookback_distance = m->delta[0];
 			if (!lookback_distance)
 				goto err_bogus;
 			continue;
-		case Z_EROFS_LCLUSTER_TYPE_PLAIN:
-		case Z_EROFS_LCLUSTER_TYPE_HEAD1:
-		case Z_EROFS_LCLUSTER_TYPE_HEAD2:
+		} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
 			m->headtype = m->type;
 			m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
 			return 0;
-		default:
-			erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
-				  m->type, lcn, vi->nid);
-			DBG_BUGON(1);
-			return -EOPNOTSUPP;
 		}
+		erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
+			  m->type, lcn, vi->nid);
+		DBG_BUGON(1);
+		return -EOPNOTSUPP;
 	}
 err_bogus:
 	erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",
@@ -329,35 +325,28 @@ static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
 	DBG_BUGON(lcn == initial_lcn &&
 		  m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
 
-	switch (m->type) {
-	case Z_EROFS_LCLUSTER_TYPE_PLAIN:
-	case Z_EROFS_LCLUSTER_TYPE_HEAD1:
-	case Z_EROFS_LCLUSTER_TYPE_HEAD2:
+	if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
+		if (m->delta[0] != 1) {
+			erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
+			DBG_BUGON(1);
+			return -EFSCORRUPTED;
+		}
+		if (m->compressedblks)
+			goto out;
+	} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
 		/*
 		 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
 		 * rather than CBLKCNT, it's a 1 block-sized pcluster.
 		 */
 		m->compressedblks = 1;
-		break;
-	case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
-		if (m->delta[0] != 1)
-			goto err_bonus_cblkcnt;
-		if (m->compressedblks)
-			break;
-		fallthrough;
-	default:
-		erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn,
-			  vi->nid);
-		DBG_BUGON(1);
-		return -EFSCORRUPTED;
+		goto out;
 	}
+	erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
 out:
 	m->map->m_plen = erofs_pos(sb, m->compressedblks);
 	return 0;
-err_bonus_cblkcnt:
-	erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
-	DBG_BUGON(1);
-	return -EFSCORRUPTED;
 }
 
 static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
@@ -386,9 +375,7 @@ static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
 				m->delta[1] = 1;
 				DBG_BUGON(1);
 			}
-		} else if (m->type == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
-			   m->type == Z_EROFS_LCLUSTER_TYPE_HEAD1 ||
-			   m->type == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
+		} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
 			if (lcn != headlcn)
 				break;	/* ends at the next HEAD lcluster */
 			m->delta[1] = 1;
@@ -452,8 +439,7 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 		}
 		/* m.lcn should be >= 1 if endoff < m.clusterofs */
 		if (!m.lcn) {
-			erofs_err(inode->i_sb,
-				  "invalid logical cluster 0 at nid %llu",
+			erofs_err(inode->i_sb, "invalid logical cluster 0 at nid %llu",
 				  vi->nid);
 			err = -EFSCORRUPTED;
 			goto unmap_out;
@@ -469,8 +455,7 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 			goto unmap_out;
 		break;
 	default:
-		erofs_err(inode->i_sb,
-			  "unknown type %u @ offset %llu of nid %llu",
+		erofs_err(inode->i_sb, "unknown type %u @ offset %llu of nid %llu",
 			  m.type, ofs, vi->nid);
 		err = -EOPNOTSUPP;
 		goto unmap_out;
Signature · 1 line
2.43.5
#1 lore raw
Quoted 29 lines · Hongzhen Luo

There's no need to enumerate each type. No logic changes.

---
  fs/erofs/zmap.c | 59 ++++++++++++++++++-------------------------------
  1 file changed, 22 insertions(+), 37 deletions(-)
fs/erofs/zmap.c +2 -5
index 689437e99a5a..0ee78413bfd5 100644
@@ -265,24 +265,20 @@ static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,
  		if (err)
  			return err;
 
-		switch (m->type) {
-		case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
+		if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
  			lookback_distance = m->delta[0];
  			if (!lookback_distance)
  				goto err_bogus;
  			continue;
-		case Z_EROFS_LCLUSTER_TYPE_PLAIN:
-		case Z_EROFS_LCLUSTER_TYPE_HEAD1:
-		case Z_EROFS_LCLUSTER_TYPE_HEAD2:
+		} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
  			m->headtype = m->type;
  			m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
  			return 0;

fs/erofs/zmap.c static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m, · line 281

Quoted 10 lines
fs/erofs/zmap.c +4 -5
-		default:
-			erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
-				  m->type, lcn, vi->nid);
-			DBG_BUGON(1);
-			return -EOPNOTSUPP;
  		}
+		erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
+			  m->type, lcn, vi->nid);
+		DBG_BUGON(1);
+		return -EOPNOTSUPP;

May be it would be easier to understand if you put the exception branch at the beginning. Such as:

if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
// return -EOPNOTSUPP;
}

if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
// do something
}

// do something for other cases..

This is also useful for other places. :)

Thanks,
Hongbo

Quoted 83 lines
fs/erofs/zmap.c +16 -27
  	}
  err_bogus:
  	erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",
@@ -329,35 +325,28 @@ static int z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,
  	DBG_BUGON(lcn == initial_lcn &&
  		  m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
 
-	switch (m->type) {
-	case Z_EROFS_LCLUSTER_TYPE_PLAIN:
-	case Z_EROFS_LCLUSTER_TYPE_HEAD1:
-	case Z_EROFS_LCLUSTER_TYPE_HEAD2:
+	if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
+		if (m->delta[0] != 1) {
+			erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
+			DBG_BUGON(1);
+			return -EFSCORRUPTED;
+		}
+		if (m->compressedblks)
+			goto out;
+	} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
  		/*
  		 * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
  		 * rather than CBLKCNT, it's a 1 block-sized pcluster.
  		 */
  		m->compressedblks = 1;
-		break;
-	case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
-		if (m->delta[0] != 1)
-			goto err_bonus_cblkcnt;
-		if (m->compressedblks)
-			break;
-		fallthrough;
-	default:
-		erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn,
-			  vi->nid);
-		DBG_BUGON(1);
-		return -EFSCORRUPTED;
+		goto out;
  	}
+	erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
+	DBG_BUGON(1);
+	return -EFSCORRUPTED;
  out:
  	m->map->m_plen = erofs_pos(sb, m->compressedblks);
  	return 0;
-err_bonus_cblkcnt:
-	erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
-	DBG_BUGON(1);
-	return -EFSCORRUPTED;
  }
 
  static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
@@ -386,9 +375,7 @@ static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
  				m->delta[1] = 1;
  				DBG_BUGON(1);
  			}
-		} else if (m->type == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
-			   m->type == Z_EROFS_LCLUSTER_TYPE_HEAD1 ||
-			   m->type == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
+		} else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
  			if (lcn != headlcn)
  				break;	/* ends at the next HEAD lcluster */
  			m->delta[1] = 1;
@@ -452,8 +439,7 @@ static int z_erofs_do_map_blocks(struct inode *inode,
  		}
  		/* m.lcn should be >= 1 if endoff < m.clusterofs */
  		if (!m.lcn) {
-			erofs_err(inode->i_sb,
-				  "invalid logical cluster 0 at nid %llu",
+			erofs_err(inode->i_sb, "invalid logical cluster 0 at nid %llu",
  				  vi->nid);
  			err = -EFSCORRUPTED;
  			goto unmap_out;
@@ -469,8 +455,7 @@ static int z_erofs_do_map_blocks(struct inode *inode,
  			goto unmap_out;
  		break;
  	default:
-		erofs_err(inode->i_sb,
-			  "unknown type %u @ offset %llu of nid %llu",
+		erofs_err(inode->i_sb, "unknown type %u @ offset %llu of nid %llu",
  			  m.type, ofs, vi->nid);
  		err = -EOPNOTSUPP;
  		goto unmap_out;
#2 lore raw
Quoted 53 lines · Hongbo Li
Quoted 29 lines · Hongzhen Luo

There's no need to enumerate each type. No logic changes.

---
  fs/erofs/zmap.c | 59 ++++++++++++++++++-------------------------------

  1 file changed, 22 insertions(+), 37 deletions(-)
fs/erofs/zmap.c +2 -4
index 689437e99a5a..0ee78413bfd5 100644
@@ -265,24 +265,20 @@ static int z_erofs_extent_lookback(struct 
 z_erofs_maprecorder *m,
           if (err)
               return err;
   -        switch (m->type) {
-        case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
+        if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
               lookback_distance = m->delta[0];
               if (!lookback_distance)
                   goto err_bogus;
               continue;
-        case Z_EROFS_LCLUSTER_TYPE_PLAIN:
-        case Z_EROFS_LCLUSTER_TYPE_HEAD1:
-        case Z_EROFS_LCLUSTER_TYPE_HEAD2:
+        } else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
               m->headtype = m->type;
               m->map->m_la = (lcn << lclusterbits) | m->clusterofs;
               return 0;

fs/erofs/zmap.c static int z_erofs_extent_lookback(struct · line 282

Quoted 10 lines
fs/erofs/zmap.c +4 -5
-        default:
-            erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
-                  m->type, lcn, vi->nid);
-            DBG_BUGON(1);
-            return -EOPNOTSUPP;
           }
+        erofs_err(sb, "unknown type %u @ lcn %lu of nid %llu",
+              m->type, lcn, vi->nid);
+        DBG_BUGON(1);
+        return -EOPNOTSUPP;

May be it would be easier to understand if you put the exception branch at the beginning. Such as:

if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
  // return -EOPNOTSUPP;
}

if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
  // do something
}

Quoted 7 lines

// do something for other cases..

This is also useful for other places. :)

Thanks,
Hongbo

Okay, I will send an improved version later.

Thanks,

Hongzhen

Quoted 90 lines
Quoted 90 lines
fs/erofs/zmap.c +0 -0
       }

  err_bogus:
      erofs_err(sb, "bogus lookback distance %u @ lcn %lu of nid %llu",

fs/erofs/zmap.c +0 -0
@@ -329,35 +325,28 @@ static int 
 z_erofs_get_extent_compressedlen(struct z_erofs_maprecorder *m,

      DBG_BUGON(lcn == initial_lcn &&
            m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD);
  -    switch (m->type) {
-    case Z_EROFS_LCLUSTER_TYPE_PLAIN:
-    case Z_EROFS_LCLUSTER_TYPE_HEAD1:
-    case Z_EROFS_LCLUSTER_TYPE_HEAD2:
+    if (m->type == Z_EROFS_LCLUSTER_TYPE_NONHEAD) {
+        if (m->delta[0] != 1) {
+            erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
+            DBG_BUGON(1);
+            return -EFSCORRUPTED;
+        }
+        if (m->compressedblks)
+            goto out;
+    } else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
          /*
           * if the 1st NONHEAD lcluster is actually PLAIN or HEAD type
           * rather than CBLKCNT, it's a 1 block-sized pcluster.
           */
          m->compressedblks = 1;
-        break;
-    case Z_EROFS_LCLUSTER_TYPE_NONHEAD:
-        if (m->delta[0] != 1)
-            goto err_bonus_cblkcnt;
-        if (m->compressedblks)
-            break;
-        fallthrough;
-    default:
-        erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn,
-              vi->nid);
-        DBG_BUGON(1);
-        return -EFSCORRUPTED;
+        goto out;
      }
+    erofs_err(sb, "cannot found CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid);
+    DBG_BUGON(1);
+    return -EFSCORRUPTED;
  out:
      m->map->m_plen = erofs_pos(sb, m->compressedblks);
      return 0;
-err_bonus_cblkcnt:
-    erofs_err(sb, "bogus CBLKCNT @ lcn %lu of nid %llu", lcn, vi->nid); -    DBG_BUGON(1);
-    return -EFSCORRUPTED;
  }
    static int z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)

fs/erofs/zmap.c +1 -3
@@ -386,9 +375,7 @@ static int 
 z_erofs_get_extent_decompressedlen(struct z_erofs_maprecorder *m)
                   m->delta[1] = 1;
                   DBG_BUGON(1);
               }
-        } else if (m->type == Z_EROFS_LCLUSTER_TYPE_PLAIN ||
-               m->type == Z_EROFS_LCLUSTER_TYPE_HEAD1 ||
-               m->type == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
+        } else if (m->type < Z_EROFS_LCLUSTER_TYPE_MAX) {
               if (lcn != headlcn)

                  break;    /* ends at the next HEAD lcluster */ m->delta[1] = 1;

fs/erofs/zmap.c +1 -2
@@ -452,8 +439,7 @@ static int z_erofs_do_map_blocks(struct inode 
 *inode,
           }
           /* m.lcn should be >= 1 if endoff < m.clusterofs */
           if (!m.lcn) {
-            erofs_err(inode->i_sb,
-                  "invalid logical cluster 0 at nid %llu",
+            erofs_err(inode->i_sb, "invalid logical cluster 0 at nid 
 %llu",
                     vi->nid);

              err = -EFSCORRUPTED;
              goto unmap_out;

fs/erofs/zmap.c +0 -0
@@ -469,8 +455,7 @@ static int z_erofs_do_map_blocks(struct inode 
 *inode,
               goto unmap_out;
           break;

      default:
-        erofs_err(inode->i_sb,
-              "unknown type %u @ offset %llu of nid %llu", +        erofs_err(inode->i_sb, "unknown type %u @ offset %llu of nid %llu",
                m.type, ofs, vi->nid);
          err = -EOPNOTSUPP;
          goto unmap_out;