cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
6717ab
diff -up util-linux-2.23.2/libblkid/src/superblocks/xfs.c.kzak util-linux-2.23.2/libblkid/src/superblocks/xfs.c
6717ab
--- util-linux-2.23.2/libblkid/src/superblocks/xfs.c.kzak	2014-09-24 10:59:39.548315524 +0200
6717ab
+++ util-linux-2.23.2/libblkid/src/superblocks/xfs.c	2014-09-24 11:02:55.595186026 +0200
6717ab
@@ -20,20 +20,143 @@
6717ab
 #include "superblocks.h"
6717ab
 
6717ab
 struct xfs_super_block {
6717ab
-	unsigned char	xs_magic[4];
6717ab
-	uint32_t	xs_blocksize;
6717ab
-	uint64_t	xs_dblocks;
6717ab
-	uint64_t	xs_rblocks;
6717ab
-	uint32_t	xs_dummy1[2];
6717ab
-	unsigned char	xs_uuid[16];
6717ab
-	uint32_t	xs_dummy2[15];
6717ab
-	char		xs_fname[12];
6717ab
-	uint32_t	xs_dummy3[2];
6717ab
-	uint64_t	xs_icount;
6717ab
-	uint64_t	xs_ifree;
6717ab
-	uint64_t	xs_fdblocks;
6717ab
+	uint32_t	sb_magicnum;	/* magic number == XFS_SB_MAGIC */
6717ab
+	uint32_t	sb_blocksize;	/* logical block size, bytes */
6717ab
+	uint64_t	sb_dblocks;	/* number of data blocks */
6717ab
+	uint64_t	sb_rblocks;	/* number of realtime blocks */
6717ab
+	uint64_t	sb_rextents;	/* number of realtime extents */
6717ab
+	unsigned char	sb_uuid[16];	/* file system unique id */
6717ab
+	uint64_t	sb_logstart;	/* starting block of log if internal */
6717ab
+	uint64_t	sb_rootino;	/* root inode number */
6717ab
+	uint64_t	sb_rbmino;	/* bitmap inode for realtime extents */
6717ab
+	uint64_t	sb_rsumino;	/* summary inode for rt bitmap */
6717ab
+	uint32_t	sb_rextsize;	/* realtime extent size, blocks */
6717ab
+	uint32_t	sb_agblocks;	/* size of an allocation group */
6717ab
+	uint32_t	sb_agcount;	/* number of allocation groups */
6717ab
+	uint32_t	sb_rbmblocks;	/* number of rt bitmap blocks */
6717ab
+	uint32_t	sb_logblocks;	/* number of log blocks */
6717ab
+
6717ab
+	uint16_t	sb_versionnum;	/* header version == XFS_SB_VERSION */
6717ab
+	uint16_t	sb_sectsize;	/* volume sector size, bytes */
6717ab
+	uint16_t	sb_inodesize;	/* inode size, bytes */
6717ab
+	uint16_t	sb_inopblock;	/* inodes per block */
6717ab
+	char		sb_fname[12];	/* file system name */
6717ab
+	uint8_t		sb_blocklog;	/* log2 of sb_blocksize */
6717ab
+	uint8_t		sb_sectlog;	/* log2 of sb_sectsize */
6717ab
+	uint8_t		sb_inodelog;	/* log2 of sb_inodesize */
6717ab
+	uint8_t		sb_inopblog;	/* log2 of sb_inopblock */
6717ab
+	uint8_t		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
6717ab
+	uint8_t		sb_rextslog;	/* log2 of sb_rextents */
6717ab
+	uint8_t		sb_inprogress;	/* mkfs is in progress, don't mount */
6717ab
+	uint8_t		sb_imax_pct;	/* max % of fs for inode space */
6717ab
+					/* statistics */
6717ab
+	uint64_t	sb_icount;	/* allocated inodes */
6717ab
+	uint64_t	sb_ifree;	/* free inodes */
6717ab
+	uint64_t	sb_fdblocks;	/* free data blocks */
6717ab
+	uint64_t	sb_frextents;	/* free realtime extents */
6717ab
+
6717ab
+	/* this is not all... but enough for libblkid */
6717ab
+
6717ab
 } __attribute__((packed));
6717ab
 
6717ab
+#define XFS_MIN_BLOCKSIZE_LOG	9	/* i.e. 512 bytes */
6717ab
+#define XFS_MAX_BLOCKSIZE_LOG	16	/* i.e. 65536 bytes */
6717ab
+#define XFS_MIN_BLOCKSIZE	(1 << XFS_MIN_BLOCKSIZE_LOG)
6717ab
+#define XFS_MAX_BLOCKSIZE	(1 << XFS_MAX_BLOCKSIZE_LOG)
6717ab
+#define XFS_MIN_SECTORSIZE_LOG	9	/* i.e. 512 bytes */
6717ab
+#define XFS_MAX_SECTORSIZE_LOG	15	/* i.e. 32768 bytes */
6717ab
+#define XFS_MIN_SECTORSIZE	(1 << XFS_MIN_SECTORSIZE_LOG)
6717ab
+#define XFS_MAX_SECTORSIZE	(1 << XFS_MAX_SECTORSIZE_LOG)
6717ab
+
6717ab
+#define	XFS_DINODE_MIN_LOG	8
6717ab
+#define	XFS_DINODE_MAX_LOG	11
6717ab
+#define	XFS_DINODE_MIN_SIZE	(1 << XFS_DINODE_MIN_LOG)
6717ab
+#define	XFS_DINODE_MAX_SIZE	(1 << XFS_DINODE_MAX_LOG)
6717ab
+
6717ab
+#define	XFS_MAX_RTEXTSIZE	(1024 * 1024 * 1024)	/* 1GB */
6717ab
+#define	XFS_DFL_RTEXTSIZE	(64 * 1024)	        /* 64kB */
6717ab
+#define	XFS_MIN_RTEXTSIZE	(4 * 1024)		/* 4kB */
6717ab
+
6717ab
+#define XFS_MIN_AG_BLOCKS	64
6717ab
+#define XFS_MAX_DBLOCKS(s) ((uint64_t)(s)->sb_agcount * (s)->sb_agblocks)
6717ab
+#define XFS_MIN_DBLOCKS(s) ((uint64_t)((s)->sb_agcount - 1) *	\
6717ab
+			 (s)->sb_agblocks + XFS_MIN_AG_BLOCKS)
6717ab
+
6717ab
+
6717ab
+static void sb_from_disk(struct xfs_super_block *from,
6717ab
+			 struct xfs_super_block *to)
6717ab
+{
6717ab
+
6717ab
+	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
6717ab
+	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
6717ab
+	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
6717ab
+	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
6717ab
+	to->sb_rextents = be64_to_cpu(from->sb_rextents);
6717ab
+	to->sb_logstart = be64_to_cpu(from->sb_logstart);
6717ab
+	to->sb_rootino = be64_to_cpu(from->sb_rootino);
6717ab
+	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
6717ab
+	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
6717ab
+	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
6717ab
+	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
6717ab
+	to->sb_agcount = be32_to_cpu(from->sb_agcount);
6717ab
+	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
6717ab
+	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
6717ab
+	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
6717ab
+	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
6717ab
+	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
6717ab
+	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
6717ab
+	to->sb_blocklog = from->sb_blocklog;
6717ab
+	to->sb_sectlog = from->sb_sectlog;
6717ab
+	to->sb_inodelog = from->sb_inodelog;
6717ab
+	to->sb_inopblog = from->sb_inopblog;
6717ab
+	to->sb_agblklog = from->sb_agblklog;
6717ab
+	to->sb_rextslog = from->sb_rextslog;
6717ab
+	to->sb_inprogress = from->sb_inprogress;
6717ab
+	to->sb_imax_pct = from->sb_imax_pct;
6717ab
+	to->sb_icount = be64_to_cpu(from->sb_icount);
6717ab
+	to->sb_ifree = be64_to_cpu(from->sb_ifree);
6717ab
+	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
6717ab
+	to->sb_frextents = be64_to_cpu(from->sb_frextents);
6717ab
+}
6717ab
+
6717ab
+static int xfs_verify_sb(struct xfs_super_block *ondisk)
6717ab
+{
6717ab
+	struct xfs_super_block sb, *sbp = &sb;
6717ab
+
6717ab
+	/* beXX_to_cpu(), but don't convert UUID and fsname! */
6717ab
+	sb_from_disk(ondisk, sbp);
6717ab
+
6717ab
+	/* sanity checks, we don't want to rely on magic string only */
6717ab
+	if (sbp->sb_agcount <= 0					||
6717ab
+	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
6717ab
+	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
6717ab
+	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
6717ab
+	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
6717ab
+	    sbp->sb_sectsize != (1 << sbp->sb_sectlog)			||
6717ab
+	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
6717ab
+	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
6717ab
+	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
6717ab
+	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
6717ab
+	    sbp->sb_blocksize != (1 << sbp->sb_blocklog)		||
6717ab
+	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
6717ab
+	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
6717ab
+	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
6717ab
+	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
6717ab
+	    sbp->sb_inodesize != (1 << sbp->sb_inodelog)		||
6717ab
+	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
6717ab
+	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
6717ab
+	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
6717ab
+	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)	||
6717ab
+	    sbp->sb_dblocks == 0					||
6717ab
+	    sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)			||
6717ab
+	    sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))
6717ab
+		return 0;
6717ab
+
6717ab
+	/* TODO: version 5 has also checksum CRC32, maybe we can check it too */
6717ab
+
6717ab
+	return 1;
6717ab
+}
6717ab
+
6717ab
 static int probe_xfs(blkid_probe pr, const struct blkid_idmag *mag)
6717ab
 {
6717ab
 	struct xfs_super_block *xs;
6717ab
@@ -42,10 +165,13 @@ static int probe_xfs(blkid_probe pr, con
6717ab
 	if (!xs)
6717ab
 		return errno ? -errno : 1;
6717ab
 
6717ab
-	if (strlen(xs->xs_fname))
6717ab
-		blkid_probe_set_label(pr, (unsigned char *) xs->xs_fname,
6717ab
-				sizeof(xs->xs_fname));
6717ab
-	blkid_probe_set_uuid(pr, xs->xs_uuid);
6717ab
+	if (!xfs_verify_sb(xs))
6717ab
+		return 1;
6717ab
+
6717ab
+	if (strlen(xs->sb_fname))
6717ab
+		blkid_probe_set_label(pr, (unsigned char *) xs->sb_fname,
6717ab
+				sizeof(xs->sb_fname));
6717ab
+	blkid_probe_set_uuid(pr, xs->sb_uuid);
6717ab
 	return 0;
6717ab
 }
6717ab