From nobody@FreeBSD.org  Tue Feb 18 03:31:59 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 8D5F086F
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 18 Feb 2014 03:31:59 +0000 (UTC)
Received: from newred.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.freebsd.org (Postfix) with ESMTPS id 7935E147B
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 18 Feb 2014 03:31:59 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by newred.freebsd.org (8.14.7/8.14.7) with ESMTP id s1I3VxYa074240
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 18 Feb 2014 03:31:59 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.7/8.14.7/Submit) id s1I3VxSI074225;
	Tue, 18 Feb 2014 03:31:59 GMT
	(envelope-from nobody)
Message-Id: <201402180331.s1I3VxSI074225@cgiserv.freebsd.org>
Date: Tue, 18 Feb 2014 03:31:59 GMT
From: Zheng Liu <gnehzuil@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: mount a ext4 file system with uninit_bg and flex_bg in read-only mode
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         186854
>Category:       kern
>Synopsis:       [ext2fs] [patch] allow mounting an ext4 file system with uninit_bg and flex_bg in read-only mode
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    pfg
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Tue Feb 18 03:40:00 UTC 2014
>Closed-Date:    Thu Apr 17 01:13:41 UTC 2014
>Last-Modified:  Thu Apr 17 01:13:41 UTC 2014
>Originator:     Zheng Liu
>Release:        current
>Organization:
>Environment:
FreeBSD lambda 11.0-CURRENT FreeBSD 11.0-CURRENT #2 ab3f62f(master): Fri Feb 14 09:03:24 CST 2014     root@lambda:/usr/obj/home/wenqing/projects/freebsd/sys/GENERIC  amd64
>Description:
Currently ext2fs driver has ability to access a ext4 file system in
read-only mode under some specific features.  But it couldn't mount a
ext4 file system that is created from scratch because uninit_bg and
flex_bg features will be enabled by default.  Hence the user would be
trouble with this if he/she wants to access a ext4 file system with
default features in read-only mode.

This patch tries to fix this issue.  In the patch, all we need to do
is to define a new flag set called 'EXT4F_RO_INCOMPAT_SUPP' in order
to prevent the user from trying to mount a ext4 file system in read-
write mode and do some sanity check.

uninit_bg is a read-only compatible feature in ext4.  It is used to
mark which block group is uninitialised.  This feature can save the
time when a user creates a ext4 file system because 'mkfs' doesn't try
to zero out inode/block bitmap and inode table.  Meanwhile 'fsck' will
skip uninitialised block group for the same purpose.

flex_bg is an incompatible feature in ext4.  It tries to put multiple
block groups tie together as one logical block group [1].  After enabling
this feature we will get more continuous disk space.

1. https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Flexible_Block_Groups
>How-To-Repeat:
mkfs.ext4 ${DEV}
mount -t ext2fs -ro ${DEV} ${MNT}

The user will get an error message when he/she tries to mount a ext4 file system due to some unsupported features.
>Fix:
The attached patch tries to fix this problem.

Patch attached with submission follows:

From 94c91af45638cbaba0991f9baacf1483c5be18c4 Mon Sep 17 00:00:00 2001
From: Zheng Liu <gnehzuil@gmail.com>
Date: Tue, 18 Feb 2014 03:08:34 +0800
Subject: [PATCH] patch ext4fs-uninitbg-flexbg

---
 sys/fs/ext2fs/ext2_vfsops.c | 14 +++++++++++---
 sys/fs/ext2fs/ext2fs.h      | 19 ++++++++++++-------
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/sys/fs/ext2fs/ext2_vfsops.c b/sys/fs/ext2fs/ext2_vfsops.c
index a18d5cc1..5c36be9 100644
--- a/sys/fs/ext2fs/ext2_vfsops.c
+++ b/sys/fs/ext2fs/ext2_vfsops.c
@@ -290,9 +290,10 @@ ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
 		return (1);
 	}
 	if (es->e2fs_rev > E2FS_REV0) {
-		if (es->e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) {
-			printf(
-"WARNING: mount of %s denied due to unsupported optional features\n",
+		if (!ronly &&
+		    (es->e2fs_features_incompat & EXT4F_RO_INCOMPAT_SUPP)) {
+			printf("WARNING: R/W mount of %s denied due to "
+			    "ext4fs only can be mounted with R/O\n",
 			    devtoname(dev));
 			return (1);
 		}
@@ -302,6 +303,13 @@ ext2_check_sb_compat(struct ext2fs *es, struct cdev *dev, int ronly)
 			    "unsupported optional features\n", devtoname(dev));
 			return (1);
 		}
+		if (es->e2fs_features_incompat &
+		    ~(EXT2F_INCOMPAT_SUPP | EXT4F_RO_INCOMPAT_SUPP)) {
+			printf("WARNING: mount of %s denied due to "
+			    "unsupported optional features\n",
+			    devtoname(dev));
+			return (1);
+		}
 	}
 	return (0);
 }
diff --git a/sys/fs/ext2fs/ext2fs.h b/sys/fs/ext2fs/ext2fs.h
index b562287..05ab23d 100644
--- a/sys/fs/ext2fs/ext2fs.h
+++ b/sys/fs/ext2fs/ext2fs.h
@@ -201,18 +201,23 @@ struct csum {
  * - EXT2F_ROCOMPAT_SPARSESUPER
  * - EXT2F_ROCOMPAT_LARGEFILE
  * - EXT2F_INCOMPAT_FTYPE
- *
- * We partially (read-only) support the following EXT4 features:
- * - EXT2F_ROCOMPAT_HUGE_FILE
- * - EXT2F_ROCOMPAT_EXTRA_ISIZE
- * - EXT2F_INCOMPAT_EXTENTS
  */
 #define EXT2F_COMPAT_SUPP		0x0000
 #define EXT2F_ROCOMPAT_SUPP		(EXT2F_ROCOMPAT_SPARSESUPER | \
 					 EXT2F_ROCOMPAT_LARGEFILE | \
 					 EXT2F_ROCOMPAT_EXTRA_ISIZE)
-#define EXT2F_INCOMPAT_SUPP		(EXT2F_INCOMPAT_FTYPE |	\
-					 EXT2F_INCOMPAT_EXTENTS)
+#define EXT2F_INCOMPAT_SUPP		(EXT2F_INCOMPAT_FTYPE)
+
+/*
+ * We partially (read-only) support the following EXT4 features:
+ * - EXT2F_ROCOMPAT_HUGE_FILE
+ * - EXT2F_ROCOMPAT_GDT_CSUM
+ * - EXT2F_ROCOMPAT_EXTRA_ISIZE
+ * - EXT2F_INCOMPAT_EXTENTS
+ * - EXT2F_INCOMPAT_FLEX_BG
+ */
+#define EXT4F_RO_INCOMPAT_SUPP		(EXT2F_INCOMPAT_EXTENTS | \
+					 EXT2F_INCOMPAT_FLEX_BG)
 
 /* Assume that user mode programs are passing in an ext2fs superblock, not
  * a kernel struct super_block.  This will allow us to call the feature-test
-- 
1.8.4.2



>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-fs 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Wed Apr 16 02:09:05 UTC 2014 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=186854 
State-Changed-From-To: open->closed 
State-Changed-By: pfg 
State-Changed-When: Thu Apr 17 01:10:51 UTC 2014 
State-Changed-Why:  
Variant committed as r262346 and MFC'd, 

Thank you for your contribution! 


Responsible-Changed-From-To: freebsd-fs->pfg 
Responsible-Changed-By: pfg 
Responsible-Changed-When: Thu Apr 17 01:10:51 UTC 2014 
Responsible-Changed-Why:  
Grab it as I committed a variant already. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=186854 
>Unformatted:
