c1c9242f0046150566414e1f222cb667e03e605evboxsync * IPRT Filesystem API (FileSys) - ext2/3 format.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Copyright (C) 2012 Oracle Corporation
c1c9242f0046150566414e1f222cb667e03e605evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c1c9242f0046150566414e1f222cb667e03e605evboxsync * available from http://www.virtualbox.org. This file is free software;
c1c9242f0046150566414e1f222cb667e03e605evboxsync * you can redistribute it and/or modify it under the terms of the GNU
c1c9242f0046150566414e1f222cb667e03e605evboxsync * General Public License (GPL) as published by the Free Software
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * The contents of this file may alternatively be used under the terms
c1c9242f0046150566414e1f222cb667e03e605evboxsync * of the Common Development and Distribution License Version 1.0
c1c9242f0046150566414e1f222cb667e03e605evboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * VirtualBox OSE distribution, in which case the provisions of the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * CDDL are applicable instead of those of the GPL.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * You may elect to license modified versions of this file under the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * terms and conditions of either the GPL or the CDDL or both.
c1c9242f0046150566414e1f222cb667e03e605evboxsync/*******************************************************************************
c1c9242f0046150566414e1f222cb667e03e605evboxsync* Header Files *
c1c9242f0046150566414e1f222cb667e03e605evboxsync*******************************************************************************/
c1c9242f0046150566414e1f222cb667e03e605evboxsync/*******************************************************************************
c1c9242f0046150566414e1f222cb667e03e605evboxsync* Structures and Typedefs *
c1c9242f0046150566414e1f222cb667e03e605evboxsync*******************************************************************************/
c1c9242f0046150566414e1f222cb667e03e605evboxsync * The filesystem structures are from http://wiki.osdev.org/Ext2 and
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Ext superblock.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Everything is stored little endian on the disk.
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Total number of inodes in the filesystem. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Total number of blocks in the filesystem. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of blocks reserved for the super user. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Total number of unallocated blocks. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Total number of unallocated inodes. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Block number of block containing the superblock. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of bits to shift 1024 to the left to get the block size */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of bits to shift 1024 to the left to get the fragment size */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of blocks in each block group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of fragments in each block group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of inodes in each block group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Last mount time. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Last written time. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of times the volume was mounted since the last check. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of mounts allowed before a consistency check. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Signature to identify a ext2 volume. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** State of the filesystem (clean/errors) */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** What to do on an error. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Minor version field. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Time of last check. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Interval between consistency checks. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Operating system ID of the filesystem creator. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Major version field. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** User ID that is allowed to use reserved blocks. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Group ID that is allowed to use reserved blocks. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Reserved fields. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Pointer to an ext super block. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Ext2 signature. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Clean filesystem state. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Error filesystem state. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Block group descriptor.
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Block address of the block bitmap. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Block address of the inode bitmap. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Start block address of the inode table. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of unallocated blocks in group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of unallocated inodes in group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of directories in the group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Padding. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Reserved. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Pointer to an ext block group descriptor. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Cached block group descriptor data.
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Start offset (in bytes and from the start of the disk). */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Last offset in the block group (inclusive). */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Block bitmap - variable in size (depends on the block size
c1c9242f0046150566414e1f222cb667e03e605evboxsync * and number of blocks per group). */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Pointer to block group descriptor data. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsynctypedef RTFILESYSTEMEXTBLKGRP *PRTFILESYSTEMEXTBLKGRP;
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Ext2/3 filesystem data.
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** VFS file handle. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Block number of the superblock. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Size of one block. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of blocks in one group. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Number of blocks groups in the volume. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** Cached block group descriptor data. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** Pointer to the ext filesystem data. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/*******************************************************************************
c1c9242f0046150566414e1f222cb667e03e605evboxsync*******************************************************************************/
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Loads the block descriptor of the given block group from the medium.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * @returns IPRT status code.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * @param pThis EXT filesystem instance data.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * @param iBlkGrp Block group number to load.
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic int rtFsExtLoadBlkGrpDesc(PRTFILESYSTEMEXT pThis, uint32_t iBlkGrp)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync PRTFILESYSTEMEXTBLKGRP pBlkGrpDesc = pThis->pBlkGrpDesc;
c1c9242f0046150566414e1f222cb667e03e605evboxsync uint64_t offRead = (pThis->iSbBlock + 1) * pThis->cbBlock;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync size_t cbBlkDesc = RT_OFFSETOF(RTFILESYSTEMEXTBLKGRP, abBlockBitmap[cbBlockBitmap]);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync pBlkGrpDesc = (PRTFILESYSTEMEXTBLKGRP)RTMemAllocZ(cbBlkDesc);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = RTVfsFileReadAt(pThis->hVfsFile, offRead, &BlkDesc, sizeof(BlkDesc), NULL);
c1c9242f0046150566414e1f222cb667e03e605evboxsync pBlkGrpDesc->offStart = pThis->iSbBlock + (uint64_t)iBlkGrp * pThis->cBlocksPerGroup * pThis->cbBlock;
c1c9242f0046150566414e1f222cb667e03e605evboxsync pBlkGrpDesc->offLast = pBlkGrpDesc->offStart + pThis->cBlocksPerGroup * pThis->cbBlock;
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = RTVfsFileReadAt(pThis->hVfsFile, BlkDesc.offBlockBitmap * pThis->cbBlock,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync &pBlkGrpDesc->abBlockBitmap[0], cbBlockBitmap, NULL);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic bool rtFsExtIsBlockRangeInUse(PRTFILESYSTEMEXTBLKGRP pBlkGrpDesc,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync if (pBlkGrpDesc->abBlockBitmap[idxByte] & RT_BIT(iBit))
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) rtFsExtProbe(RTVFSFILE hVfsFile, uint32_t *puScore)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = RTVfsFileReadAt(hVfsFile, 1024, &SuperBlock, sizeof(ExtSuperBlock), NULL);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** @todo: Convert to host endianess. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync if (SuperBlock.u16Signature == RTFILESYSTEM_EXT2_SIGNATURE)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) rtFsExtInit(void *pvThis, RTVFSFILE hVfsFile)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = RTVfsFileReadAt(hVfsFile, 1024, &SuperBlock, sizeof(ExtSuperBlock), NULL);
c1c9242f0046150566414e1f222cb667e03e605evboxsync /** @todo: Convert to host endianess. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync if (SuperBlock.u16FilesystemState == RTFILESYSTEM_EXT2_STATE_ERRORS)
c1c9242f0046150566414e1f222cb667e03e605evboxsync pThis->cbBlock = 1024 << SuperBlock.cBitsShiftLeftBlockSize;
c1c9242f0046150566414e1f222cb667e03e605evboxsync pThis->cBlocksPerGroup = SuperBlock.cBlocksPerGroup;
c1c9242f0046150566414e1f222cb667e03e605evboxsync pThis->cBlockGroups = SuperBlock.cBlocksTotal / pThis->cBlocksPerGroup;
c1c9242f0046150566414e1f222cb667e03e605evboxsync /* Load first block group descriptor. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(void) rtFsExtDestroy(void *pvThis)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) rtFsExtOpenRoot(void *pvThis, PRTVFSDIR phVfsDir)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) rtFsExtIsRangeInUse(void *pvThis, RTFOFF off, size_t cb,
c1c9242f0046150566414e1f222cb667e03e605evboxsync while (cb > 0)
c1c9242f0046150566414e1f222cb667e03e605evboxsync uint32_t offBlockStart = (uint32_t)(offStart / pThis->cbBlock);
c1c9242f0046150566414e1f222cb667e03e605evboxsync uint32_t iBlockGroup = (offBlockStart - pThis->iSbBlock) / pThis->cBlocksPerGroup;
c1c9242f0046150566414e1f222cb667e03e605evboxsync uint32_t offBlockRelStart = offBlockStart - iBlockGroup * pThis->cBlocksPerGroup;
c1c9242f0046150566414e1f222cb667e03e605evboxsync /* Load new block descriptor. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync cbThis = RT_MIN(cb, pThis->pBlkGrpDesc->offLast - offStart + 1);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync fUsed = rtFsExtIsBlockRangeInUse(pThis->pBlkGrpDesc, offBlockRelStart,
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncDECL_HIDDEN_CONST(RTFILESYSTEMDESC) const g_rtFsExt =
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** cbFs */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** VfsOps */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** uVersion. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** fFeatures */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** pszName */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync "ExtVfsOps",
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** pfnDestroy */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** pfnOpenRoot */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** pfnIsRangeInUse */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** uEndMarker */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** pfnProbe */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /** pfnInit */