VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/fs/extvfs.cpp@ 94291

Last change on this file since 94291 was 94291, checked in by vboxsync, 3 years ago

IPRT,Storage: Adding RTVfsQueryLabel and internally a generic pfnQueryInfoEx method to the RTVFSOBJOPS function table. Untested implementation of the latter for iso/udf. bugref:9781

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 108.4 KB
Line 
1/* $Id: extvfs.cpp 94291 2022-03-17 13:29:52Z vboxsync $ */
2/** @file
3 * IPRT - Ext2/3/4 Virtual Filesystem.
4 */
5
6/*
7 * Copyright (C) 2012-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_FS
32#include <iprt/fsvfs.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/avl.h>
37#include <iprt/file.h>
38#include <iprt/err.h>
39#include <iprt/list.h>
40#include <iprt/log.h>
41#include <iprt/mem.h>
42#include <iprt/string.h>
43#include <iprt/vfs.h>
44#include <iprt/vfslowlevel.h>
45#include <iprt/formats/ext.h>
46
47
48/*********************************************************************************************************************************
49* Defined Constants And Macros *
50*********************************************************************************************************************************/
51/** The maximum block group cache size (in bytes). */
52#if ARCH_BITS >= 64
53# define RTFSEXT_MAX_BLOCK_GROUP_CACHE_SIZE _512K
54#else
55# define RTFSEXT_MAX_BLOCK_GROUP_CACHE_SIZE _128K
56#endif
57/** The maximum inode cache size (in bytes). */
58#if ARCH_BITS >= 64
59# define RTFSEXT_MAX_INODE_CACHE_SIZE _512K
60#else
61# define RTFSEXT_MAX_INODE_CACHE_SIZE _128K
62#endif
63/** The maximum extent/block map cache size (in bytes). */
64#if ARCH_BITS >= 64
65# define RTFSEXT_MAX_BLOCK_CACHE_SIZE _512K
66#else
67# define RTFSEXT_MAX_BLOCK_CACHE_SIZE _128K
68#endif
69
70/** All supported incompatible features. */
71#define RTFSEXT_INCOMPAT_FEATURES_SUPP ( EXT_SB_FEAT_INCOMPAT_DIR_FILETYPE | EXT_SB_FEAT_INCOMPAT_EXTENTS | EXT_SB_FEAT_INCOMPAT_64BIT \
72 | EXT_SB_FEAT_INCOMPAT_FLEX_BG)
73
74
75/*********************************************************************************************************************************
76* Structures and Typedefs *
77*********************************************************************************************************************************/
78/** Pointer to the ext filesystem data. */
79typedef struct RTFSEXTVOL *PRTFSEXTVOL;
80
81
82/**
83 * Cached block group descriptor data.
84 */
85typedef struct RTFSEXTBLKGRP
86{
87 /** AVL tree node, indexed by the block group number. */
88 AVLU32NODECORE Core;
89 /** List node for the LRU list used for eviction. */
90 RTLISTNODE NdLru;
91 /** Reference counter. */
92 volatile uint32_t cRefs;
93 /** Block number where the inode table is store. */
94 uint64_t iBlockInodeTbl;
95 /** Pointer to the inode bitmap. */
96 uint8_t *pabInodeBitmap;
97 /** Block bitmap - variable in size (depends on the block size
98 * and number of blocks per group). */
99 uint8_t abBlockBitmap[1];
100} RTFSEXTBLKGRP;
101/** Pointer to block group descriptor data. */
102typedef RTFSEXTBLKGRP *PRTFSEXTBLKGRP;
103
104
105/**
106 * In-memory inode.
107 */
108typedef struct RTFSEXTINODE
109{
110 /** AVL tree node, indexed by the inode number. */
111 AVLU32NODECORE Core;
112 /** List node for the inode LRU list used for eviction. */
113 RTLISTNODE NdLru;
114 /** Reference counter. */
115 volatile uint32_t cRefs;
116 /** Byte offset in the backing file where the inode is stored.. */
117 uint64_t offInode;
118 /** Inode data. */
119 RTFSOBJINFO ObjInfo;
120 /** Inode flags (copied from the on disk inode). */
121 uint32_t fFlags;
122 /** Copy of the block map/extent tree. */
123 uint32_t aiBlocks[EXT_INODE_BLOCK_ENTRIES];
124} RTFSEXTINODE;
125/** Pointer to an in-memory inode. */
126typedef RTFSEXTINODE *PRTFSEXTINODE;
127
128
129/**
130 * Block cache entry.
131 */
132typedef struct RTFSEXTBLOCKENTRY
133{
134 /** AVL tree node, indexed by the filesystem block number. */
135 AVLU64NODECORE Core;
136 /** List node for the inode LRU list used for eviction. */
137 RTLISTNODE NdLru;
138 /** Reference counter. */
139 volatile uint32_t cRefs;
140 /** The block data. */
141 uint8_t abData[1];
142} RTFSEXTBLOCKENTRY;
143/** Pointer to a block cache entry. */
144typedef RTFSEXTBLOCKENTRY *PRTFSEXTBLOCKENTRY;
145
146
147/**
148 * Open directory instance.
149 */
150typedef struct RTFSEXTDIR
151{
152 /** Volume this directory belongs to. */
153 PRTFSEXTVOL pVol;
154 /** The underlying inode structure. */
155 PRTFSEXTINODE pInode;
156 /** Set if we've reached the end of the directory enumeration. */
157 bool fNoMoreFiles;
158 /** Current offset into the directory where the next entry should be read. */
159 uint64_t offEntry;
160 /** Next entry index (for logging purposes). */
161 uint32_t idxEntry;
162} RTFSEXTDIR;
163/** Pointer to an open directory instance. */
164typedef RTFSEXTDIR *PRTFSEXTDIR;
165
166
167/**
168 * Open file instance.
169 */
170typedef struct RTFSEXTFILE
171{
172 /** Volume this directory belongs to. */
173 PRTFSEXTVOL pVol;
174 /** The underlying inode structure. */
175 PRTFSEXTINODE pInode;
176 /** Current offset into the file for I/O. */
177 RTFOFF offFile;
178} RTFSEXTFILE;
179/** Pointer to an open file instance. */
180typedef RTFSEXTFILE *PRTFSEXTFILE;
181
182
183/**
184 * Ext2/3/4 filesystem volume.
185 */
186typedef struct RTFSEXTVOL
187{
188 /** Handle to itself. */
189 RTVFS hVfsSelf;
190 /** The file, partition, or whatever backing the ext volume. */
191 RTVFSFILE hVfsBacking;
192 /** The size of the backing thingy. */
193 uint64_t cbBacking;
194
195 /** RTVFSMNT_F_XXX. */
196 uint32_t fMntFlags;
197 /** RTFSEXTVFS_F_XXX (currently none defined). */
198 uint32_t fExtFlags;
199
200 /** Flag whether the filesystem is 64bit. */
201 bool f64Bit;
202 /** Size of one block. */
203 size_t cbBlock;
204 /** Number of bits to shift left for fast conversion of block numbers to offsets. */
205 uint32_t cBlockShift;
206 /** Number of blocks in one group. */
207 uint32_t cBlocksPerGroup;
208 /** Number of inodes in each block group. */
209 uint32_t cInodesPerGroup;
210 /** Number of blocks groups in the volume. */
211 uint32_t cBlockGroups;
212 /** Size of the block bitmap. */
213 size_t cbBlockBitmap;
214 /** Size of the inode bitmap. */
215 size_t cbInodeBitmap;
216 /** Size of block group descriptor. */
217 size_t cbBlkGrpDesc;
218 /** Size of an inode. */
219 size_t cbInode;
220
221 /** Incompatible features selected for this filesystem. */
222 uint32_t fFeaturesIncompat;
223
224 /** @name Block group cache.
225 * @{ */
226 /** LRU list anchor. */
227 RTLISTANCHOR LstBlockGroupLru;
228 /** Root of the cached block group tree. */
229 AVLU32TREE BlockGroupRoot;
230 /** Size of the cached block groups. */
231 size_t cbBlockGroups;
232 /** @} */
233
234 /** @name Inode cache.
235 * @{ */
236 /** LRU list anchor for the inode cache. */
237 RTLISTANCHOR LstInodeLru;
238 /** Root of the cached inode tree. */
239 AVLU32TREE InodeRoot;
240 /** Size of the cached inodes. */
241 size_t cbInodes;
242 /** @} */
243
244 /** @name Block cache.
245 * @{ */
246 /** LRU list anchor for the block cache. */
247 RTLISTANCHOR LstBlockLru;
248 /** Root of the cached block tree. */
249 AVLU64TREE BlockRoot;
250 /** Size of cached blocks. */
251 size_t cbBlocks;
252 /** @} */
253} RTFSEXTVOL;
254
255
256
257/*********************************************************************************************************************************
258* Internal Functions *
259*********************************************************************************************************************************/
260static int rtFsExtVol_OpenDirByInode(PRTFSEXTVOL pThis, uint32_t iInode, PRTVFSDIR phVfsDir);
261
262#ifdef LOG_ENABLED
263/**
264 * Logs the ext filesystem superblock.
265 *
266 * @returns nothing.
267 * @param pSb Pointer to the superblock.
268 */
269static void rtFsExtSb_Log(PCEXTSUPERBLOCK pSb)
270{
271 if (LogIs2Enabled())
272 {
273 RTTIMESPEC Spec;
274 char sz[80];
275
276 Log2(("EXT: Superblock:\n"));
277 Log2(("EXT: cInodesTotal %RU32\n", RT_LE2H_U32(pSb->cInodesTotal)));
278 Log2(("EXT: cBlocksTotalLow %RU32\n", RT_LE2H_U32(pSb->cBlocksTotalLow)));
279 Log2(("EXT: cBlocksRsvdForSuperUserLow %RU32\n", RT_LE2H_U32(pSb->cBlocksRsvdForSuperUserLow)));
280 Log2(("EXT: cBlocksFreeLow %RU32\n", RT_LE2H_U32(pSb->cBlocksFreeLow)));
281 Log2(("EXT: cInodesFree %RU32\n", RT_LE2H_U32(pSb->cInodesFree)));
282 Log2(("EXT: iBlockOfSuperblock %RU32\n", RT_LE2H_U32(pSb->iBlockOfSuperblock)));
283 Log2(("EXT: cLogBlockSize %RU32\n", RT_LE2H_U32(pSb->cLogBlockSize)));
284 Log2(("EXT: cLogClusterSize %RU32\n", RT_LE2H_U32(pSb->cLogClusterSize)));
285 Log2(("EXT: cBlocksPerGroup %RU32\n", RT_LE2H_U32(pSb->cBlocksPerGroup)));
286 Log2(("EXT: cClustersPerBlockGroup %RU32\n", RT_LE2H_U32(pSb->cClustersPerBlockGroup)));
287 Log2(("EXT: cInodesPerBlockGroup %RU32\n", RT_LE2H_U32(pSb->cInodesPerBlockGroup)));
288 Log2(("EXT: u32LastMountTime %#RX32 %s\n", RT_LE2H_U32(pSb->u32LastMountTime),
289 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pSb->u32LastMountTime)), sz, sizeof(sz))));
290 Log2(("EXT: u32LastWrittenTime %#RX32 %s\n", RT_LE2H_U32(pSb->u32LastWrittenTime),
291 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pSb->u32LastWrittenTime)), sz, sizeof(sz))));
292 Log2(("EXT: cMountsSinceLastCheck %RU16\n", RT_LE2H_U32(pSb->cMountsSinceLastCheck)));
293 Log2(("EXT: cMaxMountsUntilCheck %RU16\n", RT_LE2H_U32(pSb->cMaxMountsUntilCheck)));
294 Log2(("EXT: u16Signature %#RX16\n", RT_LE2H_U32(pSb->u16Signature)));
295 Log2(("EXT: u16FilesystemState %#RX16\n", RT_LE2H_U32(pSb->u16FilesystemState)));
296 Log2(("EXT: u16ActionOnError %#RX16\n", RT_LE2H_U32(pSb->u16ActionOnError)));
297 Log2(("EXT: u16RevLvlMinor %#RX16\n", RT_LE2H_U32(pSb->u16RevLvlMinor)));
298 Log2(("EXT: u32LastCheckTime %#RX32 %s\n", RT_LE2H_U32(pSb->u32LastCheckTime),
299 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pSb->u32LastCheckTime)), sz, sizeof(sz))));
300 Log2(("EXT: u32CheckInterval %RU32\n", RT_LE2H_U32(pSb->u32CheckInterval)));
301 Log2(("EXT: u32OsIdCreator %#RX32\n", RT_LE2H_U32(pSb->u32OsIdCreator)));
302 Log2(("EXT: u32RevLvl %#RX32\n", RT_LE2H_U32(pSb->u32RevLvl)));
303 Log2(("EXT: u16UidReservedBlocks %#RX16\n", RT_LE2H_U32(pSb->u16UidReservedBlocks)));
304 Log2(("EXT: u16GidReservedBlocks %#RX16\n", RT_LE2H_U32(pSb->u16GidReservedBlocks)));
305 if (RT_LE2H_U32(pSb->u32RevLvl) == EXT_SB_REV_V2_DYN_INODE_SZ)
306 {
307 Log2(("EXT: iFirstInodeNonRsvd %#RX32\n", RT_LE2H_U32(pSb->iFirstInodeNonRsvd)));
308 Log2(("EXT: cbInode %#RX16\n", RT_LE2H_U32(pSb->cbInode)));
309 Log2(("EXT: iBlkGrpSb %#RX16\n", RT_LE2H_U32(pSb->iBlkGrpSb)));
310 Log2(("EXT: fFeaturesCompat %#RX32%s%s%s%s%s%s%s%s%s%s\n", RT_LE2H_U32(pSb->fFeaturesCompat),
311 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_DIR_PREALLOC ? " dir-prealloc" : "",
312 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_IMAGIC_INODES ? " imagic-inode" : "",
313 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_HAS_JOURNAL ? " has-journal" : "",
314 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_EXT_ATTR ? " ext-attrs" : "",
315 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_RESIZE_INODE ? " resize-inode" : "",
316 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_DIR_INDEX ? " dir-index" : "",
317 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_LAZY_BG ? " lazy-bg" : "",
318 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_EXCLUDE_INODE ? " excl-inode" : "",
319 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_EXCLUDE_BITMAP ? " excl-bitmap" : "",
320 RT_LE2H_U32(pSb->fFeaturesCompat) & EXT_SB_FEAT_COMPAT_SPARSE_SUPER2 ? " sparse-super2" : ""));
321 Log2(("EXT: fFeaturesIncompat %#RX32%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", RT_LE2H_U32(pSb->fFeaturesIncompat),
322 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_COMPRESSION ? " compression" : "",
323 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_DIR_FILETYPE ? " dir-filetype" : "",
324 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_RECOVER ? " recovery" : "",
325 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_JOURNAL_DEV ? " journal-dev" : "",
326 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_META_BG ? " meta-bg" : "",
327 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_EXTENTS ? " extents" : "",
328 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_64BIT ? " 64bit" : "",
329 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_MMP ? " mmp" : "",
330 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_FLEX_BG ? " flex-bg" : "",
331 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_EXT_ATTR_INODE ? " extattr-inode" : "",
332 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_DIRDATA ? " dir-data" : "",
333 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_CSUM_SEED ? " csum-seed" : "",
334 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_LARGE_DIR ? " large-dir" : "",
335 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_INLINE_DATA ? " inline-data" : "",
336 RT_LE2H_U32(pSb->fFeaturesIncompat) & EXT_SB_FEAT_INCOMPAT_ENCRYPT ? " encrypt" : ""));
337 Log2(("EXT: fFeaturesCompatRo %#RX32%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", RT_LE2H_U32(pSb->fFeaturesCompatRo),
338 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_SPARSE_SUPER ? " sparse-super" : "",
339 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_LARGE_FILE ? " large-file" : "",
340 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_BTREE_DIR ? " btree-dir" : "",
341 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_HUGE_FILE ? " huge-file" : "",
342 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_GDT_CHSKUM ? " gdt-chksum" : "",
343 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_DIR_NLINK ? " dir-nlink" : "",
344 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_EXTRA_INODE_SZ ? " extra-inode" : "",
345 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_HAS_SNAPSHOTS ? " snapshots" : "",
346 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_QUOTA ? " quota" : "",
347 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_BIGALLOC ? " big-alloc" : "",
348 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_METADATA_CHKSUM ? " meta-chksum" : "",
349 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_REPLICA ? " replica" : "",
350 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_READONLY ? " ro" : "",
351 RT_LE2H_U32(pSb->fFeaturesCompatRo) & EXT_SB_FEAT_COMPAT_RO_PROJECT ? " project" : ""));
352 Log2(("EXT: au8Uuid <todo>\n"));
353 Log2(("EXT: achVolumeName %16s\n", &pSb->achVolumeName[0]));
354 Log2(("EXT: achLastMounted %64s\n", &pSb->achLastMounted[0]));
355 Log2(("EXT: u32AlgoUsageBitmap %#RX32\n", RT_LE2H_U32(pSb->u32AlgoUsageBitmap)));
356 Log2(("EXT: cBlocksPrealloc %RU8\n", pSb->cBlocksPrealloc));
357 Log2(("EXT: cBlocksPreallocDirectory %RU8\n", pSb->cBlocksPreallocDirectory));
358 Log2(("EXT: cGdtEntriesRsvd %RU16\n", pSb->cGdtEntriesRsvd));
359 Log2(("EXT: au8JournalUuid <todo>\n"));
360 Log2(("EXT: iJournalInode %#RX32\n", RT_LE2H_U32(pSb->iJournalInode)));
361 Log2(("EXT: u32JournalDev %#RX32\n", RT_LE2H_U32(pSb->u32JournalDev)));
362 Log2(("EXT: u32LastOrphan %#RX32\n", RT_LE2H_U32(pSb->u32LastOrphan)));
363 Log2(("EXT: au32HashSeedHtree[0] %#RX32\n", RT_LE2H_U32(pSb->au32HashSeedHtree[0])));
364 Log2(("EXT: au32HashSeedHtree[1] %#RX32\n", RT_LE2H_U32(pSb->au32HashSeedHtree[1])));
365 Log2(("EXT: au32HashSeedHtree[2] %#RX32\n", RT_LE2H_U32(pSb->au32HashSeedHtree[2])));
366 Log2(("EXT: au32HashSeedHtree[3] %#RX32\n", RT_LE2H_U32(pSb->au32HashSeedHtree[3])));
367 Log2(("EXT: u8HashVersionDef %#RX8\n", pSb->u8HashVersionDef));
368 Log2(("EXT: u8JnlBackupType %#RX8\n", pSb->u8JnlBackupType));
369 Log2(("EXT: cbGroupDesc %RU16\n", RT_LE2H_U16(pSb->cbGroupDesc)));
370 Log2(("EXT: fMntOptsDef %#RX32\n", RT_LE2H_U32(pSb->fMntOptsDef)));
371 Log2(("EXT: iFirstMetaBg %#RX32\n", RT_LE2H_U32(pSb->iFirstMetaBg)));
372 Log2(("EXT: u32TimeFsCreation %#RX32 %s\n", RT_LE2H_U32(pSb->u32TimeFsCreation),
373 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pSb->u32TimeFsCreation)), sz, sizeof(sz))));
374 for (unsigned i = 0; i < RT_ELEMENTS(pSb->au32JnlBlocks); i++)
375 Log2(("EXT: au32JnlBlocks[%u] %#RX32\n", i, RT_LE2H_U32(pSb->au32JnlBlocks[i])));
376 Log2(("EXT: cBlocksTotalHigh %#RX32\n", RT_LE2H_U32(pSb->cBlocksTotalHigh)));
377 Log2(("EXT: cBlocksRsvdForSuperUserHigh %#RX32\n", RT_LE2H_U32(pSb->cBlocksRsvdForSuperUserHigh)));
378 Log2(("EXT: cBlocksFreeHigh %#RX32\n", RT_LE2H_U32(pSb->cBlocksFreeHigh)));
379 Log2(("EXT: cbInodesExtraMin %#RX16\n", RT_LE2H_U16(pSb->cbInodesExtraMin)));
380 Log2(("EXT: cbNewInodesRsv %#RX16\n", RT_LE2H_U16(pSb->cbInodesExtraMin)));
381 Log2(("EXT: fFlags %#RX32\n", RT_LE2H_U32(pSb->fFlags)));
382 Log2(("EXT: cRaidStride %RU16\n", RT_LE2H_U16(pSb->cRaidStride)));
383 Log2(("EXT: cSecMmpInterval %RU16\n", RT_LE2H_U16(pSb->cSecMmpInterval)));
384 Log2(("EXT: iMmpBlock %#RX64\n", RT_LE2H_U64(pSb->iMmpBlock)));
385 Log2(("EXT: cRaidStrideWidth %#RX32\n", RT_LE2H_U32(pSb->cRaidStrideWidth)));
386 Log2(("EXT: cLogGroupsPerFlex %RU8\n", pSb->cLogGroupsPerFlex));
387 Log2(("EXT: u8ChksumType %RX8\n", pSb->u8ChksumType));
388 Log2(("EXT: cKbWritten %#RX64\n", RT_LE2H_U64(pSb->cKbWritten)));
389 Log2(("EXT: iSnapshotInode %#RX32\n", RT_LE2H_U32(pSb->iSnapshotInode)));
390 Log2(("EXT: iSnapshotId %#RX32\n", RT_LE2H_U32(pSb->iSnapshotId)));
391 Log2(("EXT: cSnapshotRsvdBlocks %#RX64\n", RT_LE2H_U64(pSb->cSnapshotRsvdBlocks)));
392 Log2(("EXT: iSnapshotListInode %#RX32\n", RT_LE2H_U32(pSb->iSnapshotListInode)));
393 Log2(("EXT: cErrorsSeen %#RX32\n", RT_LE2H_U32(pSb->cErrorsSeen)));
394 Log2(("EXT: [...]\n")); /** @todo Missing fields if becoming interesting. */
395 Log2(("EXT: iInodeLostFound %#RX32\n", RT_LE2H_U32(pSb->iInodeLostFound)));
396 Log2(("EXT: iInodeProjQuota %#RX32\n", RT_LE2H_U32(pSb->iInodeProjQuota)));
397 Log2(("EXT: u32ChksumSeed %#RX32\n", RT_LE2H_U32(pSb->u32ChksumSeed)));
398 Log2(("EXT: [...]\n")); /** @todo Missing fields if becoming interesting. */
399 Log2(("EXT: u32Chksum %#RX32\n", RT_LE2H_U32(pSb->u32Chksum)));
400 }
401 }
402}
403
404
405/**
406 * Logs a ext filesystem block group descriptor.
407 *
408 * @returns nothing.
409 * @param pThis The ext volume instance.
410 * @param iBlockGroup Block group number.
411 * @param pBlockGroup Pointer to the block group.
412 */
413static void rtFsExtBlockGroup_Log(PRTFSEXTVOL pThis, uint32_t iBlockGroup, PCEXTBLOCKGROUPDESC pBlockGroup)
414{
415 if (LogIs2Enabled())
416 {
417 uint64_t iBlockStart = (uint64_t)iBlockGroup * pThis->cBlocksPerGroup;
418 Log2(("EXT: Block group %#RX32 (blocks %#RX64 to %#RX64):\n",
419 iBlockGroup, iBlockStart, iBlockStart + pThis->cBlocksPerGroup - 1));
420 Log2(("EXT: offBlockBitmapLow %#RX32\n", RT_LE2H_U32(pBlockGroup->v32.offBlockBitmapLow)));
421 Log2(("EXT: offInodeBitmapLow %#RX32\n", RT_LE2H_U32(pBlockGroup->v32.offInodeBitmapLow)));
422 Log2(("EXT: offInodeTableLow %#RX32\n", RT_LE2H_U32(pBlockGroup->v32.offInodeTableLow)));
423 Log2(("EXT: cBlocksFreeLow %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.cBlocksFreeLow)));
424 Log2(("EXT: cInodesFreeLow %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.cInodesFreeLow)));
425 Log2(("EXT: cDirectoriesLow %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.cDirectoriesLow)));
426 Log2(("EXT: fFlags %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.fFlags)));
427 Log2(("EXT: offSnapshotExclBitmapLow %#RX32\n", RT_LE2H_U32(pBlockGroup->v32.offSnapshotExclBitmapLow)));
428 Log2(("EXT: u16ChksumBlockBitmapLow %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.u16ChksumBlockBitmapLow)));
429 Log2(("EXT: u16ChksumInodeBitmapLow %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.u16ChksumInodeBitmapLow)));
430 Log2(("EXT: cInodeTblUnusedLow %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.cInodeTblUnusedLow)));
431 Log2(("EXT: u16Chksum %#RX16\n", RT_LE2H_U16(pBlockGroup->v32.u16Chksum)));
432 if (pThis->cbBlkGrpDesc == sizeof(EXTBLOCKGROUPDESC64))
433 {
434 Log2(("EXT: offBlockBitmapHigh %#RX32\n", RT_LE2H_U32(pBlockGroup->v64.offBlockBitmapHigh)));
435 Log2(("EXT: offInodeBitmapHigh %#RX32\n", RT_LE2H_U32(pBlockGroup->v64.offInodeBitmapHigh)));
436 Log2(("EXT: offInodeTableHigh %#RX32\n", RT_LE2H_U32(pBlockGroup->v64.offInodeTableHigh)));
437 Log2(("EXT: cBlocksFreeHigh %#RX16\n", RT_LE2H_U16(pBlockGroup->v64.cBlocksFreeHigh)));
438 Log2(("EXT: cInodesFreeHigh %#RX16\n", RT_LE2H_U16(pBlockGroup->v64.cInodesFreeHigh)));
439 Log2(("EXT: cDirectoriesHigh %#RX16\n", RT_LE2H_U16(pBlockGroup->v64.cDirectoriesHigh)));
440 Log2(("EXT: cInodeTblUnusedHigh %#RX16\n", RT_LE2H_U16(pBlockGroup->v64.cInodeTblUnusedHigh)));
441 Log2(("EXT: offSnapshotExclBitmapHigh %#RX32\n", RT_LE2H_U32(pBlockGroup->v64.offSnapshotExclBitmapHigh)));
442 Log2(("EXT: u16ChksumBlockBitmapHigh %#RX16\n", RT_LE2H_U16(pBlockGroup->v64.u16ChksumBlockBitmapHigh)));
443 Log2(("EXT: u16ChksumInodeBitmapHigh %#RX16\n", RT_LE2H_U16(pBlockGroup->v64.u16ChksumInodeBitmapHigh)));
444 }
445 }
446}
447
448
449/**
450 * Logs a ext filesystem inode.
451 *
452 * @returns nothing.
453 * @param pThis The ext volume instance.
454 * @param iInode Inode number.
455 * @param pInode Pointer to the inode.
456 */
457static void rtFsExtInode_Log(PRTFSEXTVOL pThis, uint32_t iInode, PCEXTINODECOMB pInode)
458{
459 if (LogIs2Enabled())
460 {
461 RTTIMESPEC Spec;
462 char sz[80];
463
464 Log2(("EXT: Inode %#RX32:\n", iInode));
465 Log2(("EXT: fMode %#RX16\n", RT_LE2H_U16(pInode->Core.fMode)));
466 Log2(("EXT: uUidLow %#RX16\n", RT_LE2H_U16(pInode->Core.uUidLow)));
467 Log2(("EXT: cbSizeLow %#RX32\n", RT_LE2H_U32(pInode->Core.cbSizeLow)));
468 Log2(("EXT: u32TimeLastAccess %#RX32 %s\n", RT_LE2H_U32(pInode->Core.u32TimeLastAccess),
469 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pInode->Core.u32TimeLastAccess)), sz, sizeof(sz))));
470 Log2(("EXT: u32TimeLastChange %#RX32 %s\n", RT_LE2H_U32(pInode->Core.u32TimeLastChange),
471 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pInode->Core.u32TimeLastChange)), sz, sizeof(sz))));
472 Log2(("EXT: u32TimeLastModification %#RX32 %s\n", RT_LE2H_U32(pInode->Core.u32TimeLastModification),
473 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pInode->Core.u32TimeLastModification)), sz, sizeof(sz))));
474 Log2(("EXT: u32TimeDeletion %#RX32 %s\n", RT_LE2H_U32(pInode->Core.u32TimeDeletion),
475 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pInode->Core.u32TimeDeletion)), sz, sizeof(sz))));
476 Log2(("EXT: uGidLow %#RX16\n", RT_LE2H_U16(pInode->Core.uGidLow)));
477 Log2(("EXT: cHardLinks %#RU16\n", RT_LE2H_U16(pInode->Core.cHardLinks)));
478 Log2(("EXT: cBlocksLow %#RX32\n", RT_LE2H_U32(pInode->Core.cBlocksLow)));
479 Log2(("EXT: fFlags %#RX32\n", RT_LE2H_U32(pInode->Core.fFlags)));
480 Log2(("EXT: Osd1.u32LnxVersion %#RX32\n", RT_LE2H_U32(pInode->Core.Osd1.u32LnxVersion)));
481 for (unsigned i = 0; i < RT_ELEMENTS(pInode->Core.au32Block); i++)
482 Log2(("EXT: au32Block[%u] %#RX32\n", i, RT_LE2H_U32(pInode->Core.au32Block[i])));
483 Log2(("EXT: u32Version %#RX32\n", RT_LE2H_U32(pInode->Core.u32Version)));
484 Log2(("EXT: offExtAttrLow %#RX32\n", RT_LE2H_U32(pInode->Core.offExtAttrLow)));
485 Log2(("EXT: cbSizeHigh %#RX32\n", RT_LE2H_U32(pInode->Core.cbSizeHigh)));
486 Log2(("EXT: u32FragmentAddrObs %#RX32\n", RT_LE2H_U32(pInode->Core.u32FragmentAddrObs)));
487 Log2(("EXT: Osd2.Lnx.cBlocksHigh %#RX32\n", RT_LE2H_U32(pInode->Core.Osd2.Lnx.cBlocksHigh)));
488 Log2(("EXT: Osd2.Lnx.offExtAttrHigh %#RX32\n", RT_LE2H_U32(pInode->Core.Osd2.Lnx.offExtAttrHigh)));
489 Log2(("EXT: Osd2.Lnx.uUidHigh %#RX16\n", RT_LE2H_U16(pInode->Core.Osd2.Lnx.uUidHigh)));
490 Log2(("EXT: Osd2.Lnx.uGidHigh %#RX16\n", RT_LE2H_U16(pInode->Core.Osd2.Lnx.uGidHigh)));
491 Log2(("EXT: Osd2.Lnx.u16ChksumLow %#RX16\n", RT_LE2H_U16(pInode->Core.Osd2.Lnx.u16ChksumLow)));
492
493 if (pThis->cbInode >= sizeof(EXTINODECOMB))
494 {
495 Log2(("EXT: cbInodeExtra %#RU16\n", RT_LE2H_U16(pInode->Extra.cbInodeExtra)));
496 Log2(("EXT: u16ChksumHigh %#RX16\n", RT_LE2H_U16(pInode->Extra.u16ChksumHigh)));
497 Log2(("EXT: u32ExtraTimeLastChange %#RX32\n", RT_LE2H_U16(pInode->Extra.u32ExtraTimeLastChange)));
498 Log2(("EXT: u32ExtraTimeLastModification %#RX32\n", RT_LE2H_U16(pInode->Extra.u32ExtraTimeLastModification)));
499 Log2(("EXT: u32ExtraTimeLastAccess %#RX32\n", RT_LE2H_U16(pInode->Extra.u32ExtraTimeLastAccess)));
500 Log2(("EXT: u32TimeCreation %#RX32 %s\n", RT_LE2H_U32(pInode->Extra.u32TimeCreation),
501 RTTimeSpecToString(RTTimeSpecSetSeconds(&Spec, RT_LE2H_U32(pInode->Extra.u32TimeCreation)), sz, sizeof(sz))));
502 Log2(("EXT: u32ExtraTimeCreation %#RX32\n", RT_LE2H_U16(pInode->Extra.u32ExtraTimeCreation)));
503 Log2(("EXT: u32VersionHigh %#RX32\n", RT_LE2H_U16(pInode->Extra.u32VersionHigh)));
504 Log2(("EXT: u32ProjectId %#RX32\n", RT_LE2H_U16(pInode->Extra.u32ProjectId)));
505 }
506 }
507}
508
509
510/**
511 * Logs a ext filesystem directory entry.
512 *
513 * @returns nothing.
514 * @param pThis The ext volume instance.
515 * @param idxDirEntry Directory entry index number.
516 * @param pDirEntry The directory entry.
517 */
518static void rtFsExtDirEntry_Log(PRTFSEXTVOL pThis, uint32_t idxDirEntry, PCEXTDIRENTRYEX pDirEntry)
519{
520 if (LogIs2Enabled())
521 {
522 int cbName = 0;
523
524 Log2(("EXT: Directory entry %#RX32:\n", idxDirEntry));
525 Log2(("EXT: iInodeRef %#RX32\n", RT_LE2H_U32(pDirEntry->Core.iInodeRef)));
526 Log2(("EXT: cbRecord %#RX32\n", RT_LE2H_U32(pDirEntry->Core.cbRecord)));
527 if (pThis->fFeaturesIncompat & EXT_SB_FEAT_INCOMPAT_DIR_FILETYPE)
528 {
529 Log2(("EXT: cbName %#RU8\n", pDirEntry->Core.u.v2.cbName));
530 Log2(("EXT: uType %#RX8\n", pDirEntry->Core.u.v2.uType));
531 cbName = pDirEntry->Core.u.v2.cbName;
532 }
533 else
534 {
535 Log2(("EXT: cbName %#RU16\n", RT_LE2H_U16(pDirEntry->Core.u.v1.cbName)));
536 cbName = RT_LE2H_U16(pDirEntry->Core.u.v1.cbName);
537 }
538 Log2(("EXT: achName %*s\n", cbName, &pDirEntry->Core.achName[0]));
539 }
540}
541
542
543/**
544 * Logs an extent header.
545 *
546 * @returns nothing.
547 * @param pExtentHdr The extent header node.
548 */
549static void rtFsExtExtentHdr_Log(PCEXTEXTENTHDR pExtentHdr)
550{
551 if (LogIs2Enabled())
552 {
553 Log2(("EXT: Extent header:\n"));
554 Log2(("EXT: u16Magic %#RX16\n", RT_LE2H_U32(pExtentHdr->u16Magic)));
555 Log2(("EXT: cEntries %#RX16\n", RT_LE2H_U32(pExtentHdr->cEntries)));
556 Log2(("EXT: cMax %#RX16\n", RT_LE2H_U32(pExtentHdr->cMax)));
557 Log2(("EXT: uDepth %#RX16\n", RT_LE2H_U32(pExtentHdr->uDepth)));
558 Log2(("EXT: cGeneration %#RX32\n", RT_LE2H_U32(pExtentHdr->cGeneration)));
559 }
560}
561
562
563/**
564 * Logs an extent index node.
565 *
566 * @returns nothing.
567 * @param pExtentIdx The extent index node.
568 */
569static void rtFsExtExtentIdx_Log(PCEXTEXTENTIDX pExtentIdx)
570{
571 if (LogIs2Enabled())
572 {
573 Log2(("EXT: Extent index node:\n"));
574 Log2(("EXT: iBlock %#RX32\n", RT_LE2H_U32(pExtentIdx->iBlock)));
575 Log2(("EXT: offChildLow %#RX32\n", RT_LE2H_U32(pExtentIdx->offChildLow)));
576 Log2(("EXT: offChildHigh %#RX16\n", RT_LE2H_U16(pExtentIdx->offChildHigh)));
577 }
578}
579
580
581/**
582 * Logs an extent.
583 *
584 * @returns nothing.
585 * @param pExtent The extent.
586 */
587static void rtFsExtExtent_Log(PCEXTEXTENT pExtent)
588{
589 if (LogIs2Enabled())
590 {
591 Log2(("EXT: Extent:\n"));
592 Log2(("EXT: iBlock %#RX32\n", RT_LE2H_U32(pExtent->iBlock)));
593 Log2(("EXT: cBlocks %#RX16\n", RT_LE2H_U16(pExtent->cBlocks)));
594 Log2(("EXT: offStartHigh %#RX16\n", RT_LE2H_U32(pExtent->offStartHigh)));
595 Log2(("EXT: offStartLow %#RX16\n", RT_LE2H_U16(pExtent->offStartLow)));
596 }
597}
598#endif
599
600
601/**
602 * Converts a block number to a byte offset.
603 *
604 * @returns Offset in bytes for the given block number.
605 * @param pThis The ext volume instance.
606 * @param iBlock The block number to convert.
607 */
608DECLINLINE(uint64_t) rtFsExtBlockIdxToDiskOffset(PRTFSEXTVOL pThis, uint64_t iBlock)
609{
610 return iBlock << pThis->cBlockShift;
611}
612
613
614/**
615 * Converts a byte offset to a block number.
616 *
617 * @returns Block number.
618 * @param pThis The ext volume instance.
619 * @param iBlock The offset to convert.
620 */
621DECLINLINE(uint64_t) rtFsExtDiskOffsetToBlockIdx(PRTFSEXTVOL pThis, uint64_t off)
622{
623 return off >> pThis->cBlockShift;
624}
625
626
627/**
628 * Creates the proper block number from the given low and high parts in case a 64bit
629 * filesystem is used.
630 *
631 * @returns 64bit block number.
632 * @param pThis The ext volume instance.
633 * @param uLow The lower 32bit part.
634 * @param uHigh The upper 32bit part.
635 */
636DECLINLINE(uint64_t) rtFsExtBlockFromLowHigh(PRTFSEXTVOL pThis, uint32_t uLow, uint32_t uHigh)
637{
638 return pThis->f64Bit ? RT_MAKE_U64(uLow, uHigh): uLow;
639}
640
641
642/**
643 * Converts the given high and low parts of the block number to a byte offset.
644 *
645 * @returns Offset in bytes for the given block number.
646 * @param uLow The lower 32bit part of the block number.
647 * @param uHigh The upper 32bit part of the block number.
648 */
649DECLINLINE(uint64_t) rtFsExtBlockIdxLowHighToDiskOffset(PRTFSEXTVOL pThis, uint32_t uLow, uint32_t uHigh)
650{
651 uint64_t iBlock = rtFsExtBlockFromLowHigh(pThis, uLow, uHigh);
652 return rtFsExtBlockIdxToDiskOffset(pThis, iBlock);
653}
654
655
656/**
657 * Allocates a new block group.
658 *
659 * @returns Pointer to the new block group descriptor or NULL if out of memory.
660 * @param pThis The ext volume instance.
661 * @param cbAlloc How much to allocate.
662 * @param iBlockGroup Block group number.
663 */
664static PRTFSEXTBLOCKENTRY rtFsExtVol_BlockAlloc(PRTFSEXTVOL pThis, size_t cbAlloc, uint64_t iBlock)
665{
666 PRTFSEXTBLOCKENTRY pBlock = (PRTFSEXTBLOCKENTRY)RTMemAllocZ(cbAlloc);
667 if (RT_LIKELY(pBlock))
668 {
669 pBlock->Core.Key = iBlock;
670 pBlock->cRefs = 0;
671 pThis->cbBlocks += cbAlloc;
672 }
673
674 return pBlock;
675}
676
677
678/**
679 * Returns a new block entry utilizing the cache if possible.
680 *
681 * @returns Pointer to the new block entry or NULL if out of memory.
682 * @param pThis The ext volume instance.
683 * @param iBlock Block number.
684 */
685static PRTFSEXTBLOCKENTRY rtFsExtVol_BlockGetNew(PRTFSEXTVOL pThis, uint64_t iBlock)
686{
687 PRTFSEXTBLOCKENTRY pBlock = NULL;
688 size_t cbAlloc = RT_UOFFSETOF_DYN(RTFSEXTBLOCKENTRY, abData[pThis->cbBlock]);
689 if (pThis->cbBlocks + cbAlloc <= RTFSEXT_MAX_BLOCK_CACHE_SIZE)
690 pBlock = rtFsExtVol_BlockAlloc(pThis, cbAlloc, iBlock);
691 else
692 {
693 pBlock = RTListRemoveLast(&pThis->LstBlockLru, RTFSEXTBLOCKENTRY, NdLru);
694 if (!pBlock)
695 pBlock = rtFsExtVol_BlockAlloc(pThis, cbAlloc, iBlock);
696 else
697 {
698 /* Remove the block group from the tree because it gets a new key. */
699 PAVLU64NODECORE pCore = RTAvlU64Remove(&pThis->BlockRoot, pBlock->Core.Key);
700 Assert(pCore == &pBlock->Core); RT_NOREF(pCore);
701 }
702 }
703
704 Assert(!pBlock->cRefs);
705 pBlock->Core.Key = iBlock;
706 pBlock->cRefs = 1;
707
708 return pBlock;
709}
710
711
712/**
713 * Frees the given block.
714 *
715 * @returns nothing.
716 * @param pThis The ext volume instance.
717 * @param pBlock The block to free.
718 */
719static void rtFsExtVol_BlockFree(PRTFSEXTVOL pThis, PRTFSEXTBLOCKENTRY pBlock)
720{
721 Assert(!pBlock->cRefs);
722
723 /*
724 * Put it into the cache if the limit wasn't exceeded, otherwise the block group
725 * is freed right away.
726 */
727 if (pThis->cbBlocks <= RTFSEXT_MAX_BLOCK_CACHE_SIZE)
728 {
729 /* Put onto the LRU list. */
730 RTListPrepend(&pThis->LstBlockLru, &pBlock->NdLru);
731 }
732 else
733 {
734 /* Remove from the tree and free memory. */
735 PAVLU64NODECORE pCore = RTAvlU64Remove(&pThis->BlockRoot, pBlock->Core.Key);
736 Assert(pCore == &pBlock->Core); RT_NOREF(pCore);
737 RTMemFree(pBlock);
738 pThis->cbBlocks -= RT_UOFFSETOF_DYN(RTFSEXTBLOCKENTRY, abData[pThis->cbBlock]);
739 }
740}
741
742
743/**
744 * Gets the specified block data from the volume.
745 *
746 * @returns IPRT status code.
747 * @param pThis The ext volume instance.
748 * @param iBlock The filesystem block to load.
749 * @param ppBlock Where to return the pointer to the block entry on success.
750 * @param ppvData Where to return the pointer to the block data on success.
751 */
752static int rtFsExtVol_BlockLoad(PRTFSEXTVOL pThis, uint64_t iBlock, PRTFSEXTBLOCKENTRY *ppBlock, void **ppvData)
753{
754 int rc = VINF_SUCCESS;
755
756 /* Try to fetch the block group from the cache first. */
757 PRTFSEXTBLOCKENTRY pBlock = (PRTFSEXTBLOCKENTRY)RTAvlU64Get(&pThis->BlockRoot, iBlock);
758 if (!pBlock)
759 {
760 /* Slow path, load from disk. */
761 pBlock = rtFsExtVol_BlockGetNew(pThis, iBlock);
762 if (RT_LIKELY(pBlock))
763 {
764 uint64_t offRead = rtFsExtBlockIdxToDiskOffset(pThis, iBlock);
765 rc = RTVfsFileReadAt(pThis->hVfsBacking, offRead, &pBlock->abData[0], pThis->cbBlock, NULL);
766 if (RT_SUCCESS(rc))
767 {
768 bool fIns = RTAvlU64Insert(&pThis->BlockRoot, &pBlock->Core);
769 Assert(fIns); RT_NOREF(fIns);
770 }
771 }
772 else
773 rc = VERR_NO_MEMORY;
774 }
775 else
776 {
777 /* Remove from current LRU list position and add to the beginning. */
778 uint32_t cRefs = ASMAtomicIncU32(&pBlock->cRefs);
779 if (cRefs == 1) /* Blocks get removed from the LRU list if they are referenced. */
780 RTListNodeRemove(&pBlock->NdLru);
781 }
782
783 if (RT_SUCCESS(rc))
784 {
785 *ppBlock = pBlock;
786 *ppvData = &pBlock->abData[0];
787 }
788 else if (pBlock)
789 {
790 ASMAtomicDecU32(&pBlock->cRefs);
791 rtFsExtVol_BlockFree(pThis, pBlock); /* Free the block. */
792 }
793
794 return rc;
795}
796
797
798/**
799 * Releases a reference of the given block.
800 *
801 * @returns nothing.
802 * @param pThis The ext volume instance.
803 * @param pBlock The block to release.
804 */
805static void rtFsExtVol_BlockRelease(PRTFSEXTVOL pThis, PRTFSEXTBLOCKENTRY pBlock)
806{
807 uint32_t cRefs = ASMAtomicDecU32(&pBlock->cRefs);
808 if (!cRefs)
809 rtFsExtVol_BlockFree(pThis, pBlock);
810}
811
812
813/**
814 * Allocates a new block group.
815 *
816 * @returns Pointer to the new block group descriptor or NULL if out of memory.
817 * @param pThis The ext volume instance.
818 * @param cbAlloc How much to allocate.
819 * @param iBlockGroup Block group number.
820 */
821static PRTFSEXTBLKGRP rtFsExtBlockGroupAlloc(PRTFSEXTVOL pThis, size_t cbAlloc, uint32_t iBlockGroup)
822{
823 PRTFSEXTBLKGRP pBlockGroup = (PRTFSEXTBLKGRP)RTMemAllocZ(cbAlloc);
824 if (RT_LIKELY(pBlockGroup))
825 {
826 pBlockGroup->Core.Key = iBlockGroup;
827 pBlockGroup->cRefs = 0;
828 pBlockGroup->pabInodeBitmap = &pBlockGroup->abBlockBitmap[pThis->cbBlockBitmap];
829 pThis->cbBlockGroups += cbAlloc;
830 }
831
832 return pBlockGroup;
833}
834
835
836/**
837 * Frees the given block group.
838 *
839 * @returns nothing.
840 * @param pThis The ext volume instance.
841 * @param pBlockGroup The block group to free.
842 */
843static void rtFsExtBlockGroupFree(PRTFSEXTVOL pThis, PRTFSEXTBLKGRP pBlockGroup)
844{
845 Assert(!pBlockGroup->cRefs);
846
847 /*
848 * Put it into the cache if the limit wasn't exceeded, otherwise the block group
849 * is freed right away.
850 */
851 if (pThis->cbBlockGroups <= RTFSEXT_MAX_BLOCK_GROUP_CACHE_SIZE)
852 {
853 /* Put onto the LRU list. */
854 RTListPrepend(&pThis->LstBlockGroupLru, &pBlockGroup->NdLru);
855 }
856 else
857 {
858 /* Remove from the tree and free memory. */
859 PAVLU32NODECORE pCore = RTAvlU32Remove(&pThis->BlockGroupRoot, pBlockGroup->Core.Key);
860 Assert(pCore == &pBlockGroup->Core); RT_NOREF(pCore);
861 RTMemFree(pBlockGroup);
862 pThis->cbBlockGroups -= sizeof(RTFSEXTBLKGRP) + pThis->cbBlockBitmap + pThis->cbInodeBitmap;
863 }
864}
865
866
867/**
868 * Returns a new block group utilizing the cache if possible.
869 *
870 * @returns Pointer to the new block group descriptor or NULL if out of memory.
871 * @param pThis The ext volume instance.
872 * @param iBlockGroup Block group number.
873 */
874static PRTFSEXTBLKGRP rtFsExtBlockGroupGetNew(PRTFSEXTVOL pThis, uint32_t iBlockGroup)
875{
876 PRTFSEXTBLKGRP pBlockGroup = NULL;
877 size_t cbAlloc = sizeof(RTFSEXTBLKGRP) + pThis->cbBlockBitmap + pThis->cbInodeBitmap;
878 if (pThis->cbBlockGroups + cbAlloc <= RTFSEXT_MAX_BLOCK_GROUP_CACHE_SIZE)
879 pBlockGroup = rtFsExtBlockGroupAlloc(pThis, cbAlloc, iBlockGroup);
880 else
881 {
882 pBlockGroup = RTListRemoveLast(&pThis->LstBlockGroupLru, RTFSEXTBLKGRP, NdLru);
883 if (!pBlockGroup)
884 pBlockGroup = rtFsExtBlockGroupAlloc(pThis, cbAlloc, iBlockGroup);
885 else
886 {
887 /* Remove the block group from the tree because it gets a new key. */
888 PAVLU32NODECORE pCore = RTAvlU32Remove(&pThis->BlockGroupRoot, pBlockGroup->Core.Key);
889 Assert(pCore == &pBlockGroup->Core); RT_NOREF(pCore);
890 }
891 }
892
893 Assert(!pBlockGroup->cRefs);
894 pBlockGroup->Core.Key = iBlockGroup;
895 pBlockGroup->cRefs = 1;
896
897 return pBlockGroup;
898}
899
900
901/**
902 * Loads the given block group number and returns it on success.
903 *
904 * @returns IPRT status code.
905 * @param pThis The ext volume instance.
906 * @param iBlockGroup The block group to load.
907 * @param ppBlockGroup Where to store the block group on success.
908 */
909static int rtFsExtBlockGroupLoad(PRTFSEXTVOL pThis, uint32_t iBlockGroup, PRTFSEXTBLKGRP *ppBlockGroup)
910{
911 int rc = VINF_SUCCESS;
912
913 /* Try to fetch the block group from the cache first. */
914 PRTFSEXTBLKGRP pBlockGroup = (PRTFSEXTBLKGRP)RTAvlU32Get(&pThis->BlockGroupRoot, iBlockGroup);
915 if (!pBlockGroup)
916 {
917 /* Slow path, load from disk. */
918 pBlockGroup = rtFsExtBlockGroupGetNew(pThis, iBlockGroup);
919 if (RT_LIKELY(pBlockGroup))
920 {
921 uint64_t offRead = rtFsExtBlockIdxToDiskOffset(pThis, pThis->cbBlock == _1K ? 2 : 1)
922 + (uint64_t)iBlockGroup * pThis->cbBlkGrpDesc;
923 EXTBLOCKGROUPDESC BlockGroupDesc;
924 rc = RTVfsFileReadAt(pThis->hVfsBacking, offRead, &BlockGroupDesc, pThis->cbBlkGrpDesc, NULL);
925 if (RT_SUCCESS(rc))
926 {
927#ifdef LOG_ENABLED
928 rtFsExtBlockGroup_Log(pThis, iBlockGroup, &BlockGroupDesc);
929#endif
930 pBlockGroup->iBlockInodeTbl = RT_LE2H_U32(BlockGroupDesc.v32.offInodeTableLow)
931 | ((pThis->cbBlkGrpDesc == sizeof(EXTBLOCKGROUPDESC64))
932 ? (uint64_t)RT_LE2H_U32(BlockGroupDesc.v64.offInodeTableHigh) << 32
933 : 0);
934
935 offRead = rtFsExtBlockIdxLowHighToDiskOffset(pThis, RT_LE2H_U32(BlockGroupDesc.v32.offBlockBitmapLow),
936 RT_LE2H_U32(BlockGroupDesc.v64.offBlockBitmapHigh));
937 rc = RTVfsFileReadAt(pThis->hVfsBacking, offRead, &pBlockGroup->abBlockBitmap[0], pThis->cbBlockBitmap, NULL);
938 if (RT_SUCCESS(rc))
939 {
940 offRead = rtFsExtBlockIdxLowHighToDiskOffset(pThis, RT_LE2H_U32(BlockGroupDesc.v32.offInodeBitmapLow),
941 RT_LE2H_U32(BlockGroupDesc.v64.offInodeBitmapHigh));
942 rc = RTVfsFileReadAt(pThis->hVfsBacking, offRead, &pBlockGroup->pabInodeBitmap[0], pThis->cbInodeBitmap, NULL);
943 if (RT_SUCCESS(rc))
944 {
945 bool fIns = RTAvlU32Insert(&pThis->BlockGroupRoot, &pBlockGroup->Core);
946 Assert(fIns); RT_NOREF(fIns);
947 }
948 }
949 }
950 }
951 else
952 rc = VERR_NO_MEMORY;
953 }
954 else
955 {
956 /* Remove from current LRU list position and add to the beginning. */
957 uint32_t cRefs = ASMAtomicIncU32(&pBlockGroup->cRefs);
958 if (cRefs == 1) /* Block groups get removed from the LRU list if they are referenced. */
959 RTListNodeRemove(&pBlockGroup->NdLru);
960 }
961
962 if (RT_SUCCESS(rc))
963 *ppBlockGroup = pBlockGroup;
964 else if (pBlockGroup)
965 {
966 ASMAtomicDecU32(&pBlockGroup->cRefs);
967 rtFsExtBlockGroupFree(pThis, pBlockGroup); /* Free the block group. */
968 }
969
970 return rc;
971}
972
973
974/**
975 * Releases a reference of the given block group.
976 *
977 * @returns nothing.
978 * @param pThis The ext volume instance.
979 * @param pBlockGroup The block group to release.
980 */
981static void rtFsExtBlockGroupRelease(PRTFSEXTVOL pThis, PRTFSEXTBLKGRP pBlockGroup)
982{
983 uint32_t cRefs = ASMAtomicDecU32(&pBlockGroup->cRefs);
984 if (!cRefs)
985 rtFsExtBlockGroupFree(pThis, pBlockGroup);
986}
987
988
989/**
990 * Allocates a new inode.
991 *
992 * @returns Pointer to the new inode or NULL if out of memory.
993 * @param pThis The ext volume instance.
994 * @param iInode Inode number.
995 */
996static PRTFSEXTINODE rtFsExtInodeAlloc(PRTFSEXTVOL pThis, uint32_t iInode)
997{
998 PRTFSEXTINODE pInode = (PRTFSEXTINODE)RTMemAllocZ(sizeof(RTFSEXTINODE));
999 if (RT_LIKELY(pInode))
1000 {
1001 pInode->Core.Key = iInode;
1002 pInode->cRefs = 0;
1003 pThis->cbInodes += sizeof(RTFSEXTINODE);
1004 }
1005
1006 return pInode;
1007}
1008
1009
1010/**
1011 * Frees the given inode.
1012 *
1013 * @returns nothing.
1014 * @param pThis The ext volume instance.
1015 * @param pInode The inode to free.
1016 */
1017static void rtFsExtInodeFree(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode)
1018{
1019 Assert(!pInode->cRefs);
1020
1021 /*
1022 * Put it into the cache if the limit wasn't exceeded, otherwise the inode
1023 * is freed right away.
1024 */
1025 if (pThis->cbInodes <= RTFSEXT_MAX_INODE_CACHE_SIZE)
1026 {
1027 /* Put onto the LRU list. */
1028 RTListPrepend(&pThis->LstInodeLru, &pInode->NdLru);
1029 }
1030 else
1031 {
1032 /* Remove from the tree and free memory. */
1033 PAVLU32NODECORE pCore = RTAvlU32Remove(&pThis->InodeRoot, pInode->Core.Key);
1034 Assert(pCore == &pInode->Core); RT_NOREF(pCore);
1035 RTMemFree(pInode);
1036 pThis->cbInodes -= sizeof(RTFSEXTINODE);
1037 }
1038}
1039
1040
1041/**
1042 * Returns a new inodep utilizing the cache if possible.
1043 *
1044 * @returns Pointer to the new inode or NULL if out of memory.
1045 * @param pThis The ext volume instance.
1046 * @param iInode Inode number.
1047 */
1048static PRTFSEXTINODE rtFsExtInodeGetNew(PRTFSEXTVOL pThis, uint32_t iInode)
1049{
1050 PRTFSEXTINODE pInode = NULL;
1051 if (pThis->cbInodes + sizeof(RTFSEXTINODE) <= RTFSEXT_MAX_INODE_CACHE_SIZE)
1052 pInode = rtFsExtInodeAlloc(pThis, iInode);
1053 else
1054 {
1055 pInode = RTListRemoveLast(&pThis->LstInodeLru, RTFSEXTINODE, NdLru);
1056 if (!pInode)
1057 pInode = rtFsExtInodeAlloc(pThis, iInode);
1058 else
1059 {
1060 /* Remove the block group from the tree because it gets a new key. */
1061 PAVLU32NODECORE pCore = RTAvlU32Remove(&pThis->InodeRoot, pInode->Core.Key);
1062 Assert(pCore == &pInode->Core); RT_NOREF(pCore);
1063 }
1064 }
1065
1066 Assert(!pInode->cRefs);
1067 pInode->Core.Key = iInode;
1068 pInode->cRefs = 1;
1069
1070 return pInode;
1071}
1072
1073
1074/**
1075 * Loads the given inode number and returns it on success.
1076 *
1077 * @returns IPRT status code.
1078 * @param pThis The ext volume instance.
1079 * @param iInode The inode to load.
1080 * @param ppInode Where to store the inode on success.
1081 */
1082static int rtFsExtInodeLoad(PRTFSEXTVOL pThis, uint32_t iInode, PRTFSEXTINODE *ppInode)
1083{
1084 int rc = VINF_SUCCESS;
1085
1086 /* Try to fetch the inode from the cache first. */
1087 PRTFSEXTINODE pInode = (PRTFSEXTINODE)RTAvlU32Get(&pThis->InodeRoot, iInode);
1088 if (!pInode)
1089 {
1090 /* Slow path, load from disk. */
1091 pInode = rtFsExtInodeGetNew(pThis, iInode);
1092 if (RT_LIKELY(pInode))
1093 {
1094 /* Calculate the block group and load that one first to get at the inode table location. */
1095 PRTFSEXTBLKGRP pBlockGroup = NULL;
1096 rc = rtFsExtBlockGroupLoad(pThis, (iInode - 1) / pThis->cInodesPerGroup, &pBlockGroup);
1097 if (RT_SUCCESS(rc))
1098 {
1099 uint32_t idxInodeInTbl = (iInode - 1) % pThis->cInodesPerGroup;
1100 uint64_t offRead = rtFsExtBlockIdxToDiskOffset(pThis, pBlockGroup->iBlockInodeTbl)
1101 + idxInodeInTbl * pThis->cbInode;
1102
1103 /* Release block group here already as it is not required. */
1104 rtFsExtBlockGroupRelease(pThis, pBlockGroup);
1105
1106 EXTINODECOMB Inode;
1107 rc = RTVfsFileReadAt(pThis->hVfsBacking, offRead, &Inode, RT_MIN(sizeof(Inode), pThis->cbInode), NULL);
1108 if (RT_SUCCESS(rc))
1109 {
1110#ifdef LOG_ENABLED
1111 rtFsExtInode_Log(pThis, iInode, &Inode);
1112#endif
1113 pInode->offInode = offRead;
1114 pInode->fFlags = RT_LE2H_U32(Inode.Core.fFlags);
1115 pInode->ObjInfo.cbObject = (uint64_t)RT_LE2H_U32(Inode.Core.cbSizeHigh) << 32
1116 | (uint64_t)RT_LE2H_U32(Inode.Core.cbSizeLow);
1117 pInode->ObjInfo.cbAllocated = ( (uint64_t)RT_LE2H_U16(Inode.Core.Osd2.Lnx.cBlocksHigh) << 32
1118 | (uint64_t)RT_LE2H_U32(Inode.Core.cBlocksLow)) * pThis->cbBlock;
1119 RTTimeSpecSetSeconds(&pInode->ObjInfo.AccessTime, RT_LE2H_U32(Inode.Core.u32TimeLastAccess));
1120 RTTimeSpecSetSeconds(&pInode->ObjInfo.ModificationTime, RT_LE2H_U32(Inode.Core.u32TimeLastModification));
1121 RTTimeSpecSetSeconds(&pInode->ObjInfo.ChangeTime, RT_LE2H_U32(Inode.Core.u32TimeLastChange));
1122 pInode->ObjInfo.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
1123 pInode->ObjInfo.Attr.u.Unix.uid = (uint32_t)RT_LE2H_U16(Inode.Core.Osd2.Lnx.uUidHigh) << 16
1124 | (uint32_t)RT_LE2H_U16(Inode.Core.uUidLow);
1125 pInode->ObjInfo.Attr.u.Unix.gid = (uint32_t)RT_LE2H_U16(Inode.Core.Osd2.Lnx.uGidHigh) << 16
1126 | (uint32_t)RT_LE2H_U16(Inode.Core.uGidLow);
1127 pInode->ObjInfo.Attr.u.Unix.cHardlinks = RT_LE2H_U16(Inode.Core.cHardLinks);
1128 pInode->ObjInfo.Attr.u.Unix.INodeIdDevice = 0;
1129 pInode->ObjInfo.Attr.u.Unix.INodeId = iInode;
1130 pInode->ObjInfo.Attr.u.Unix.fFlags = 0;
1131 pInode->ObjInfo.Attr.u.Unix.GenerationId = RT_LE2H_U32(Inode.Core.u32Version);
1132 pInode->ObjInfo.Attr.u.Unix.Device = 0;
1133 if (pThis->cbInode >= sizeof(EXTINODECOMB))
1134 RTTimeSpecSetSeconds(&pInode->ObjInfo.BirthTime, RT_LE2H_U32(Inode.Extra.u32TimeCreation));
1135 else
1136 RTTimeSpecSetSeconds(&pInode->ObjInfo.BirthTime, RT_LE2H_U32(Inode.Core.u32TimeLastChange));
1137 for (unsigned i = 0; i < RT_ELEMENTS(pInode->aiBlocks); i++)
1138 pInode->aiBlocks[i] = RT_LE2H_U32(Inode.Core.au32Block[i]);
1139
1140 /* Fill in the mode. */
1141 pInode->ObjInfo.Attr.fMode = 0;
1142 uint32_t fInodeMode = RT_LE2H_U32(Inode.Core.fMode);
1143 switch (EXT_INODE_MODE_TYPE_GET_TYPE(fInodeMode))
1144 {
1145 case EXT_INODE_MODE_TYPE_FIFO:
1146 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_FIFO;
1147 break;
1148 case EXT_INODE_MODE_TYPE_CHAR:
1149 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_DEV_CHAR;
1150 break;
1151 case EXT_INODE_MODE_TYPE_DIR:
1152 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_DIRECTORY;
1153 break;
1154 case EXT_INODE_MODE_TYPE_BLOCK:
1155 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_DEV_BLOCK;
1156 break;
1157 case EXT_INODE_MODE_TYPE_REGULAR:
1158 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_FILE;
1159 break;
1160 case EXT_INODE_MODE_TYPE_SYMLINK:
1161 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_SYMLINK;
1162 break;
1163 case EXT_INODE_MODE_TYPE_SOCKET:
1164 pInode->ObjInfo.Attr.fMode |= RTFS_TYPE_SOCKET;
1165 break;
1166 default:
1167 rc = VERR_VFS_BOGUS_FORMAT;
1168 }
1169 if (fInodeMode & EXT_INODE_MODE_EXEC_OTHER)
1170 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IXOTH;
1171 if (fInodeMode & EXT_INODE_MODE_WRITE_OTHER)
1172 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IWOTH;
1173 if (fInodeMode & EXT_INODE_MODE_READ_OTHER)
1174 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IROTH;
1175 if (fInodeMode & EXT_INODE_MODE_EXEC_GROUP)
1176 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IXGRP;
1177 if (fInodeMode & EXT_INODE_MODE_WRITE_GROUP)
1178 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IWGRP;
1179 if (fInodeMode & EXT_INODE_MODE_READ_GROUP)
1180 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IRGRP;
1181 if (fInodeMode & EXT_INODE_MODE_EXEC_OWNER)
1182 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IXUSR;
1183 if (fInodeMode & EXT_INODE_MODE_WRITE_OWNER)
1184 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IWUSR;
1185 if (fInodeMode & EXT_INODE_MODE_READ_OWNER)
1186 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_IRUSR;
1187 if (fInodeMode & EXT_INODE_MODE_STICKY)
1188 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_ISTXT;
1189 if (fInodeMode & EXT_INODE_MODE_SET_GROUP_ID)
1190 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_ISGID;
1191 if (fInodeMode & EXT_INODE_MODE_SET_USER_ID)
1192 pInode->ObjInfo.Attr.fMode |= RTFS_UNIX_ISUID;
1193 }
1194 }
1195
1196 if (RT_SUCCESS(rc))
1197 {
1198 bool fIns = RTAvlU32Insert(&pThis->InodeRoot, &pInode->Core);
1199 Assert(fIns); RT_NOREF(fIns);
1200 }
1201 }
1202 else
1203 rc = VERR_NO_MEMORY;
1204 }
1205 else
1206 {
1207 /* Remove from current LRU list position and add to the beginning. */
1208 uint32_t cRefs = ASMAtomicIncU32(&pInode->cRefs);
1209 if (cRefs == 1) /* Inodes get removed from the LRU list if they are referenced. */
1210 RTListNodeRemove(&pInode->NdLru);
1211 }
1212
1213 if (RT_SUCCESS(rc))
1214 *ppInode = pInode;
1215 else if (pInode)
1216 {
1217 ASMAtomicDecU32(&pInode->cRefs);
1218 rtFsExtInodeFree(pThis, pInode); /* Free the inode. */
1219 }
1220
1221 return rc;
1222}
1223
1224
1225/**
1226 * Releases a reference of the given inode.
1227 *
1228 * @returns nothing.
1229 * @param pThis The ext volume instance.
1230 * @param pInode The inode to release.
1231 */
1232static void rtFsExtInodeRelease(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode)
1233{
1234 uint32_t cRefs = ASMAtomicDecU32(&pInode->cRefs);
1235 if (!cRefs)
1236 rtFsExtInodeFree(pThis, pInode);
1237}
1238
1239
1240/**
1241 * Worker for various QueryInfo methods.
1242 *
1243 * @returns IPRT status code.
1244 * @param pInode The inode structure to return info for.
1245 * @param pObjInfo Where to return object info.
1246 * @param enmAddAttr What additional info to return.
1247 */
1248static int rtFsExtInode_QueryInfo(PRTFSEXTINODE pInode, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1249{
1250 RT_ZERO(*pObjInfo);
1251
1252 pObjInfo->cbObject = pInode->ObjInfo.cbObject;
1253 pObjInfo->cbAllocated = pInode->ObjInfo.cbAllocated;
1254 pObjInfo->AccessTime = pInode->ObjInfo.AccessTime;
1255 pObjInfo->ModificationTime = pInode->ObjInfo.ModificationTime;
1256 pObjInfo->ChangeTime = pInode->ObjInfo.ChangeTime;
1257 pObjInfo->BirthTime = pInode->ObjInfo.BirthTime;
1258 pObjInfo->Attr.fMode = pInode->ObjInfo.Attr.fMode;
1259 pObjInfo->Attr.enmAdditional = enmAddAttr;
1260 switch (enmAddAttr)
1261 {
1262 case RTFSOBJATTRADD_UNIX:
1263 memcpy(&pObjInfo->Attr.u.Unix, &pInode->ObjInfo.Attr.u.Unix, sizeof(pInode->ObjInfo.Attr.u.Unix));
1264 break;
1265
1266 case RTFSOBJATTRADD_UNIX_OWNER:
1267 pObjInfo->Attr.u.UnixOwner.uid = pInode->ObjInfo.Attr.u.Unix.uid;
1268 break;
1269
1270 case RTFSOBJATTRADD_UNIX_GROUP:
1271 pObjInfo->Attr.u.UnixGroup.gid = pInode->ObjInfo.Attr.u.Unix.gid;
1272 break;
1273
1274 default:
1275 break;
1276 }
1277
1278 return VINF_SUCCESS;
1279}
1280
1281
1282/**
1283 * Validates a given extent header.
1284 *
1285 * @returns Flag whether the extent header appears to be valid.
1286 * @param pExtentHdr The extent header to validate.
1287 */
1288DECLINLINE(bool) rtFsExtInode_ExtentHdrValidate(PCEXTEXTENTHDR pExtentHdr)
1289{
1290 return RT_LE2H_U16(pExtentHdr->u16Magic) == EXT_EXTENT_HDR_MAGIC
1291 && RT_LE2H_U16(pExtentHdr->cEntries) <= RT_LE2H_U16(pExtentHdr->cMax)
1292 && RT_LE2H_U16(pExtentHdr->uDepth) <= EXT_EXTENT_HDR_DEPTH_MAX;
1293}
1294
1295
1296/**
1297 * Parses the given extent, checking whether it intersects with the given block.
1298 *
1299 * @returns Flag whether the extent maps the given range (at least partly).
1300 * @param pExtent The extent to parse.
1301 * @param iBlock The starting inode block to map.
1302 * @param cBlocks Number of blocks requested.
1303 * @param piBlockFs Where to store the filesystem block on success.
1304 * @param pcBlocks Where to store the number of contiguous blocks on success.
1305 * @param pfSparse Where to store the sparse flag on success.
1306 */
1307DECLINLINE(bool) rtFsExtInode_ExtentParse(PCEXTEXTENT pExtent, uint64_t iBlock, size_t cBlocks,
1308 uint64_t *piBlockFs, size_t *pcBlocks, bool *pfSparse)
1309{
1310#ifdef LOG_ENABLED
1311 rtFsExtExtent_Log(pExtent);
1312#endif
1313
1314 uint32_t iExtentBlock = RT_LE2H_U32(pExtent->iBlock);
1315 uint16_t cExtentLength = RT_LE2H_U16(pExtent->cBlocks);
1316
1317 /* Length over EXT_EXTENT_LENGTH_LIMIT blocks indicate a sparse extent. */
1318 if (cExtentLength > EXT_EXTENT_LENGTH_LIMIT)
1319 {
1320 *pfSparse = true;
1321 cExtentLength -= EXT_EXTENT_LENGTH_LIMIT;
1322 }
1323 else
1324 *pfSparse = false;
1325
1326 if ( iExtentBlock <= iBlock
1327 && iExtentBlock + cExtentLength > iBlock)
1328 {
1329 uint32_t iBlockRel = iBlock - iExtentBlock;
1330 *pcBlocks = RT_MIN(cBlocks, cExtentLength - iBlockRel);
1331 *piBlockFs = ( ((uint64_t)RT_LE2H_U16(pExtent->offStartHigh)) << 32
1332 | ((uint64_t)RT_LE2H_U32(pExtent->offStartLow))) + iBlockRel;
1333 return true;
1334 }
1335
1336 return false;
1337}
1338
1339
1340/**
1341 * Locates the location of the next level in the extent tree mapping the given block.
1342 *
1343 * @returns Filesystem block number where the next level of the extent is stored.
1344 * @param paExtentIdx Pointer to the array of extent index nodes.
1345 * @param cEntries Number of entries in the extent index node array.
1346 * @param iBlock The block to resolve.
1347 */
1348DECLINLINE(uint64_t) rtFsExtInode_ExtentIndexLocateNextLvl(PCEXTEXTENTIDX paExtentIdx, uint16_t cEntries, uint64_t iBlock)
1349{
1350 for (uint32_t i = 1; i < cEntries; i++)
1351 {
1352 PCEXTEXTENTIDX pPrev = &paExtentIdx[i - 1];
1353 PCEXTEXTENTIDX pCur = &paExtentIdx[i];
1354
1355#ifdef LOG_ENABLED
1356 rtFsExtExtentIdx_Log(pPrev);
1357#endif
1358
1359 if ( RT_LE2H_U32(pPrev->iBlock) <= iBlock
1360 && RT_LE2H_U32(pCur->iBlock) > iBlock)
1361 return (uint64_t)RT_LE2H_U16(pPrev->offChildHigh) << 32
1362 | (uint64_t)RT_LE2H_U32(pPrev->offChildLow);
1363 }
1364
1365 /* Nothing found so far, the blast extent index must cover the block as the array is sorted. */
1366 PCEXTEXTENTIDX pLast = &paExtentIdx[cEntries - 1];
1367#ifdef LOG_ENABLED
1368 rtFsExtExtentIdx_Log(pLast);
1369#endif
1370
1371 return (uint64_t)RT_LE2H_U16(pLast->offChildHigh) << 32
1372 | (uint64_t)RT_LE2H_U32(pLast->offChildLow);
1373}
1374
1375
1376/**
1377 * Maps the given inode block to the destination filesystem block using the embedded extent tree.
1378 *
1379 * @returns IPRT status code.
1380 * @param pThis The ext volume instance.
1381 * @param pInode The inode structure to read from.
1382 * @param iBlock The starting inode block to map.
1383 * @param cBlocks Number of blocks requested.
1384 * @param piBlockFs Where to store the filesystem block on success.
1385 * @param pcBlocks Where to store the number of contiguous blocks on success.
1386 * @param pfSparse Where to store the sparse flag on success.
1387 *
1388 * @todo Optimize
1389 */
1390static int rtFsExtInode_MapBlockToFsViaExtent(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode, uint64_t iBlock, size_t cBlocks,
1391 uint64_t *piBlockFs, size_t *pcBlocks, bool *pfSparse)
1392{
1393 int rc = VINF_SUCCESS;
1394
1395 /* The root of the extent tree is located in the block data of the inode. */
1396 PCEXTEXTENTHDR pExtentHdr = (PCEXTEXTENTHDR)&pInode->aiBlocks[0];
1397
1398#ifdef LOG_ENABLED
1399 rtFsExtExtentHdr_Log(pExtentHdr);
1400#endif
1401
1402 /*
1403 * Some validation, the top level is located inside the inode block data
1404 * and has a maxmimum of 4 entries.
1405 */
1406 if ( rtFsExtInode_ExtentHdrValidate(pExtentHdr)
1407 && RT_LE2H_U16(pExtentHdr->cMax) <= 4)
1408 {
1409 uint16_t uDepthCur = RT_LE2H_U16(pExtentHdr->uDepth);
1410 if (!uDepthCur)
1411 {
1412 PCEXTEXTENT pExtent = (PCEXTEXTENT)(pExtentHdr + 1);
1413
1414 rc = VERR_VFS_BOGUS_FORMAT;
1415 for (uint32_t i = 0; i < RT_LE2H_U16(pExtentHdr->cEntries); i++)
1416 {
1417 /* Check whether the extent intersects with the block. */
1418 if (rtFsExtInode_ExtentParse(pExtent, iBlock, cBlocks, piBlockFs, pcBlocks, pfSparse))
1419 {
1420 rc = VINF_SUCCESS;
1421 break;
1422 }
1423 pExtent++;
1424 }
1425 }
1426 else
1427 {
1428 uint8_t *pbExtent = NULL;
1429 PRTFSEXTBLOCKENTRY pBlock = NULL;
1430 uint64_t iBlockNext = 0;
1431 PCEXTEXTENTIDX paExtentIdx = (PCEXTEXTENTIDX)(pExtentHdr + 1);
1432 uint16_t cEntries = RT_LE2H_U16(pExtentHdr->cEntries);
1433
1434 /* Descend the tree until we reached the leaf nodes. */
1435 do
1436 {
1437 iBlockNext = rtFsExtInode_ExtentIndexLocateNextLvl(paExtentIdx, cEntries, iBlock);
1438 /* Read in the full block. */
1439 rc = rtFsExtVol_BlockLoad(pThis, iBlockNext, &pBlock, (void **)&pbExtent);
1440 if (RT_SUCCESS(rc))
1441 {
1442 pExtentHdr = (PCEXTEXTENTHDR)pbExtent;
1443
1444#ifdef LOG_ENABLED
1445 rtFsExtExtentHdr_Log(pExtentHdr);
1446#endif
1447
1448 if ( rtFsExtInode_ExtentHdrValidate(pExtentHdr)
1449 && RT_LE2H_U16(pExtentHdr->cMax) <= (pThis->cbBlock - sizeof(EXTEXTENTHDR)) / sizeof(EXTEXTENTIDX)
1450 && RT_LE2H_U16(pExtentHdr->uDepth) == uDepthCur - 1)
1451 {
1452 uDepthCur--;
1453 cEntries = RT_LE2H_U16(pExtentHdr->cEntries);
1454 paExtentIdx = (PCEXTEXTENTIDX)(pExtentHdr + 1);
1455 if (uDepthCur)
1456 rtFsExtVol_BlockRelease(pThis, pBlock);
1457 }
1458 else
1459 rc = VERR_VFS_BOGUS_FORMAT;
1460 }
1461 }
1462 while ( uDepthCur > 0
1463 && RT_SUCCESS(rc));
1464
1465 if (RT_SUCCESS(rc))
1466 {
1467 Assert(!uDepthCur);
1468
1469 /* We reached the leaf nodes. */
1470 PCEXTEXTENT pExtent = (PCEXTEXTENT)(pExtentHdr + 1);
1471 for (uint32_t i = 0; i < RT_LE2H_U16(pExtentHdr->cEntries); i++)
1472 {
1473 /* Check whether the extent intersects with the block. */
1474 if (rtFsExtInode_ExtentParse(pExtent, iBlock, cBlocks, piBlockFs, pcBlocks, pfSparse))
1475 {
1476 rc = VINF_SUCCESS;
1477 break;
1478 }
1479 pExtent++;
1480 }
1481 }
1482
1483 if (pBlock)
1484 rtFsExtVol_BlockRelease(pThis, pBlock);
1485 }
1486 }
1487 else
1488 rc = VERR_VFS_BOGUS_FORMAT;
1489
1490 return rc;
1491}
1492
1493
1494/**
1495 * Maps the given inode block to the destination filesystem block using the original block mapping scheme.
1496 *
1497 * @returns IPRT status code.
1498 * @param pThis The ext volume instance.
1499 * @param pInode The inode structure to read from.
1500 * @param iBlock The inode block to map.
1501 * @param cBlocks Number of blocks requested.
1502 * @param piBlockFs Where to store the filesystem block on success.
1503 * @param pcBlocks Where to store the number of contiguous blocks on success.
1504 * @param pfSparse Where to store the sparse flag on success.
1505 *
1506 * @todo Optimize and handle sparse files.
1507 */
1508static int rtFsExtInode_MapBlockToFsViaBlockMap(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode, uint64_t iBlock, size_t cBlocks,
1509 uint64_t *piBlockFs, size_t *pcBlocks, bool *pfSparse)
1510{
1511 int rc = VINF_SUCCESS;
1512 RT_NOREF(cBlocks);
1513
1514 *pfSparse = false;
1515 *pcBlocks = 1;
1516
1517 /* The first 12 inode blocks are directly mapped from the inode. */
1518 if (iBlock <= 11)
1519 *piBlockFs = pInode->aiBlocks[iBlock];
1520 else
1521 {
1522 uint32_t cEntriesPerBlockMap = (uint32_t)(pThis->cbBlock >> sizeof(uint32_t));
1523
1524 if (iBlock <= cEntriesPerBlockMap + 11)
1525 {
1526 /* Indirect block. */
1527 PRTFSEXTBLOCKENTRY pBlock = NULL;
1528 uint32_t *paBlockMap = NULL;
1529 rc = rtFsExtVol_BlockLoad(pThis, pInode->aiBlocks[12], &pBlock, (void **)&paBlockMap);
1530 if (RT_SUCCESS(rc))
1531 {
1532 *piBlockFs = RT_LE2H_U32(paBlockMap[iBlock - 12]);
1533 rtFsExtVol_BlockRelease(pThis, pBlock);
1534 }
1535 }
1536 else if (iBlock <= cEntriesPerBlockMap * cEntriesPerBlockMap + cEntriesPerBlockMap + 11)
1537 {
1538 /* Double indirect block. */
1539 PRTFSEXTBLOCKENTRY pBlock = NULL;
1540 uint32_t *paBlockMap = NULL;
1541
1542 iBlock -= 12 + cEntriesPerBlockMap;
1543 rc = rtFsExtVol_BlockLoad(pThis, pInode->aiBlocks[13], &pBlock, (void **)&paBlockMap);
1544 if (RT_SUCCESS(rc))
1545 {
1546 uint32_t idxBlockL2 = iBlock / cEntriesPerBlockMap;
1547 uint32_t idxBlockL1 = iBlock % cEntriesPerBlockMap;
1548 uint32_t iBlockNext = RT_LE2H_U32(paBlockMap[idxBlockL2]);
1549
1550 rtFsExtVol_BlockRelease(pThis, pBlock);
1551 rc = rtFsExtVol_BlockLoad(pThis, iBlockNext, &pBlock, (void **)&paBlockMap);
1552 if (RT_SUCCESS(rc))
1553 {
1554 *piBlockFs = RT_LE2H_U32(paBlockMap[idxBlockL1]);
1555 rtFsExtVol_BlockRelease(pThis, pBlock);
1556 }
1557 }
1558 }
1559 else
1560 {
1561 /* Triple indirect block. */
1562 PRTFSEXTBLOCKENTRY pBlock = NULL;
1563 uint32_t *paBlockMap = NULL;
1564
1565 iBlock -= 12 + cEntriesPerBlockMap * cEntriesPerBlockMap + cEntriesPerBlockMap;
1566 rc = rtFsExtVol_BlockLoad(pThis, pInode->aiBlocks[14], &pBlock, (void **)&paBlockMap);
1567 if (RT_SUCCESS(rc))
1568 {
1569 uint32_t idxBlockL3 = iBlock / (cEntriesPerBlockMap * cEntriesPerBlockMap);
1570 uint32_t iBlockNext = RT_LE2H_U32(paBlockMap[idxBlockL3]);
1571
1572 rtFsExtVol_BlockRelease(pThis, pBlock);
1573 rc = rtFsExtVol_BlockLoad(pThis, iBlockNext, &pBlock, (void **)&paBlockMap);
1574 if (RT_SUCCESS(rc))
1575 {
1576 uint32_t idxBlockL2 = (iBlock % (cEntriesPerBlockMap * cEntriesPerBlockMap)) / cEntriesPerBlockMap;
1577 uint32_t idxBlockL1 = iBlock % cEntriesPerBlockMap;
1578 iBlockNext = RT_LE2H_U32(paBlockMap[idxBlockL2]);
1579
1580 rtFsExtVol_BlockRelease(pThis, pBlock);
1581 rc = rtFsExtVol_BlockLoad(pThis, iBlockNext, &pBlock, (void **)&paBlockMap);
1582 if (RT_SUCCESS(rc))
1583 {
1584 *piBlockFs = RT_LE2H_U32(paBlockMap[idxBlockL1]);
1585 rtFsExtVol_BlockRelease(pThis, pBlock);
1586 }
1587 }
1588 }
1589 }
1590 }
1591
1592 return rc;
1593}
1594
1595
1596/**
1597 * Maps the given inode block to the destination filesystem block.
1598 *
1599 * @returns IPRT status code.
1600 * @param pThis The ext volume instance.
1601 * @param pInode The inode structure to read from.
1602 * @param iBlock The inode block to map.
1603 * @param cBlocks Number of blocks requested.
1604 * @param piBlockFs Where to store the filesystem block on success.
1605 * @param pcBlocks Where to store the number of contiguous blocks on success.
1606 * @param pfSparse Where to store the sparse flag on success.
1607 *
1608 * @todo Optimize
1609 */
1610static int rtFsExtInode_MapBlockToFs(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode, uint64_t iBlock, size_t cBlocks,
1611 uint64_t *piBlockFs, size_t *pcBlocks, bool *pfSparse)
1612{
1613 if (pInode->fFlags & EXT_INODE_F_EXTENTS)
1614 return rtFsExtInode_MapBlockToFsViaExtent(pThis, pInode, iBlock, cBlocks, piBlockFs, pcBlocks, pfSparse);
1615 else
1616 return rtFsExtInode_MapBlockToFsViaBlockMap(pThis, pInode, iBlock, cBlocks, piBlockFs, pcBlocks, pfSparse);
1617}
1618
1619
1620/**
1621 * Reads data from the given inode at the given byte offset.
1622 *
1623 * @returns IPRT status code.
1624 * @param pThis The ext volume instance.
1625 * @param pInode The inode structure to read from.
1626 * @param off The byte offset to start reading from.
1627 * @param pvBuf Where to store the read data to.
1628 * @param pcbRead Where to return the amount of data read.
1629 */
1630static int rtFsExtInode_Read(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode, uint64_t off, void *pvBuf, size_t cbRead, size_t *pcbRead)
1631{
1632 int rc = VINF_SUCCESS;
1633 uint8_t *pbBuf = (uint8_t *)pvBuf;
1634
1635 if (((uint64_t)pInode->ObjInfo.cbObject < off + cbRead))
1636 {
1637 if (!pcbRead)
1638 return VERR_EOF;
1639 else
1640 cbRead = (uint64_t)pInode->ObjInfo.cbObject - off;
1641 }
1642
1643 while ( cbRead
1644 && RT_SUCCESS(rc))
1645 {
1646 uint64_t iBlockStart = rtFsExtDiskOffsetToBlockIdx(pThis, off);
1647 uint32_t offBlockStart = off % pThis->cbBlock;
1648
1649 /* Resolve the inode block to the proper filesystem block. */
1650 uint64_t iBlockFs = 0;
1651 size_t cBlocks = 0;
1652 bool fSparse = false;
1653 rc = rtFsExtInode_MapBlockToFs(pThis, pInode, iBlockStart, 1, &iBlockFs, &cBlocks, &fSparse);
1654 if (RT_SUCCESS(rc))
1655 {
1656 Assert(cBlocks == 1);
1657
1658 size_t cbThisRead = RT_MIN(cbRead, pThis->cbBlock - offBlockStart);
1659
1660 if (!fSparse)
1661 {
1662 uint64_t offRead = rtFsExtBlockIdxToDiskOffset(pThis, iBlockFs);
1663 rc = RTVfsFileReadAt(pThis->hVfsBacking, offRead + offBlockStart, pbBuf, cbThisRead, NULL);
1664 }
1665 else
1666 memset(pbBuf, 0, cbThisRead);
1667
1668 if (RT_SUCCESS(rc))
1669 {
1670 pbBuf += cbThisRead;
1671 cbRead -= cbThisRead;
1672 off += cbThisRead;
1673 if (pcbRead)
1674 *pcbRead += cbThisRead;
1675 }
1676 }
1677 }
1678
1679 return rc;
1680}
1681
1682
1683
1684/*
1685 *
1686 * File operations.
1687 * File operations.
1688 * File operations.
1689 *
1690 */
1691
1692/**
1693 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
1694 */
1695static DECLCALLBACK(int) rtFsExtFile_Close(void *pvThis)
1696{
1697 PRTFSEXTFILE pThis = (PRTFSEXTFILE)pvThis;
1698 LogFlow(("rtFsExtFile_Close(%p/%p)\n", pThis, pThis->pInode));
1699
1700 rtFsExtInodeRelease(pThis->pVol, pThis->pInode);
1701 pThis->pInode = NULL;
1702 pThis->pVol = NULL;
1703 return VINF_SUCCESS;
1704}
1705
1706
1707/**
1708 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
1709 */
1710static DECLCALLBACK(int) rtFsExtFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1711{
1712 PRTFSEXTFILE pThis = (PRTFSEXTFILE)pvThis;
1713 return rtFsExtInode_QueryInfo(pThis->pInode, pObjInfo, enmAddAttr);
1714}
1715
1716
1717/**
1718 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
1719 */
1720static DECLCALLBACK(int) rtFsExtFile_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
1721{
1722 PRTFSEXTFILE pThis = (PRTFSEXTFILE)pvThis;
1723 AssertReturn(pSgBuf->cSegs == 1, VERR_INTERNAL_ERROR_3);
1724 RT_NOREF(fBlocking);
1725
1726 if (off == -1)
1727 off = pThis->offFile;
1728 else
1729 AssertReturn(off >= 0, VERR_INTERNAL_ERROR_3);
1730
1731 int rc;
1732 size_t cbRead = pSgBuf->paSegs[0].cbSeg;
1733 if (!pcbRead)
1734 {
1735 rc = rtFsExtInode_Read(pThis->pVol, pThis->pInode, (uint64_t)off, pSgBuf->paSegs[0].pvSeg, cbRead, NULL);
1736 if (RT_SUCCESS(rc))
1737 pThis->offFile = off + cbRead;
1738 Log6(("rtFsExtFile_Read: off=%#RX64 cbSeg=%#x -> %Rrc\n", off, pSgBuf->paSegs[0].cbSeg, rc));
1739 }
1740 else
1741 {
1742 PRTFSEXTINODE pInode = pThis->pInode;
1743 if (off >= pInode->ObjInfo.cbObject)
1744 {
1745 *pcbRead = 0;
1746 rc = VINF_EOF;
1747 }
1748 else
1749 {
1750 if ((uint64_t)off + cbRead <= (uint64_t)pInode->ObjInfo.cbObject)
1751 rc = rtFsExtInode_Read(pThis->pVol, pThis->pInode, (uint64_t)off, pSgBuf->paSegs[0].pvSeg, cbRead, NULL);
1752 else
1753 {
1754 /* Return VINF_EOF if beyond end-of-file. */
1755 cbRead = (size_t)(pInode->ObjInfo.cbObject - off);
1756 rc = rtFsExtInode_Read(pThis->pVol, pThis->pInode, off, pSgBuf->paSegs[0].pvSeg, cbRead, NULL);
1757 if (RT_SUCCESS(rc))
1758 rc = VINF_EOF;
1759 }
1760 if (RT_SUCCESS(rc))
1761 {
1762 pThis->offFile = off + cbRead;
1763 *pcbRead = cbRead;
1764 }
1765 else
1766 *pcbRead = 0;
1767 }
1768 Log6(("rtFsExtFile_Read: off=%#RX64 cbSeg=%#x -> %Rrc *pcbRead=%#x\n", off, pSgBuf->paSegs[0].cbSeg, rc, *pcbRead));
1769 }
1770
1771 return rc;
1772}
1773
1774
1775/**
1776 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
1777 */
1778static DECLCALLBACK(int) rtFsExtFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
1779{
1780 RT_NOREF(pvThis, off, pSgBuf, fBlocking, pcbWritten);
1781 return VERR_WRITE_PROTECT;
1782}
1783
1784
1785/**
1786 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
1787 */
1788static DECLCALLBACK(int) rtFsExtFile_Flush(void *pvThis)
1789{
1790 RT_NOREF(pvThis);
1791 return VINF_SUCCESS;
1792}
1793
1794
1795/**
1796 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
1797 */
1798static DECLCALLBACK(int) rtFsExtFile_Tell(void *pvThis, PRTFOFF poffActual)
1799{
1800 PRTFSEXTFILE pThis = (PRTFSEXTFILE)pvThis;
1801 *poffActual = pThis->offFile;
1802 return VINF_SUCCESS;
1803}
1804
1805
1806/**
1807 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
1808 */
1809static DECLCALLBACK(int) rtFsExtFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
1810{
1811 RT_NOREF(pvThis, fMode, fMask);
1812 return VERR_WRITE_PROTECT;
1813}
1814
1815
1816/**
1817 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
1818 */
1819static DECLCALLBACK(int) rtFsExtFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
1820 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
1821{
1822 RT_NOREF(pvThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
1823 return VERR_WRITE_PROTECT;
1824}
1825
1826
1827/**
1828 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
1829 */
1830static DECLCALLBACK(int) rtFsExtFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
1831{
1832 RT_NOREF(pvThis, uid, gid);
1833 return VERR_WRITE_PROTECT;
1834}
1835
1836
1837/**
1838 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
1839 */
1840static DECLCALLBACK(int) rtFsExtFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
1841{
1842 PRTFSEXTFILE pThis = (PRTFSEXTFILE)pvThis;
1843 RTFOFF offNew;
1844 switch (uMethod)
1845 {
1846 case RTFILE_SEEK_BEGIN:
1847 offNew = offSeek;
1848 break;
1849 case RTFILE_SEEK_END:
1850 offNew = pThis->pInode->ObjInfo.cbObject + offSeek;
1851 break;
1852 case RTFILE_SEEK_CURRENT:
1853 offNew = (RTFOFF)pThis->offFile + offSeek;
1854 break;
1855 default:
1856 return VERR_INVALID_PARAMETER;
1857 }
1858 if (offNew >= 0)
1859 {
1860 pThis->offFile = offNew;
1861 *poffActual = offNew;
1862 return VINF_SUCCESS;
1863 }
1864 return VERR_NEGATIVE_SEEK;
1865}
1866
1867
1868/**
1869 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
1870 */
1871static DECLCALLBACK(int) rtFsExtFile_QuerySize(void *pvThis, uint64_t *pcbFile)
1872{
1873 PRTFSEXTFILE pThis = (PRTFSEXTFILE)pvThis;
1874 *pcbFile = (uint64_t)pThis->pInode->ObjInfo.cbObject;
1875 return VINF_SUCCESS;
1876}
1877
1878
1879/**
1880 * @interface_method_impl{RTVFSFILEOPS,pfnSetSize}
1881 */
1882static DECLCALLBACK(int) rtFsExtFile_SetSize(void *pvThis, uint64_t cbFile, uint32_t fFlags)
1883{
1884 RT_NOREF(pvThis, cbFile, fFlags);
1885 return VERR_WRITE_PROTECT;
1886}
1887
1888
1889/**
1890 * @interface_method_impl{RTVFSFILEOPS,pfnQueryMaxSize}
1891 */
1892static DECLCALLBACK(int) rtFsExtFile_QueryMaxSize(void *pvThis, uint64_t *pcbMax)
1893{
1894 RT_NOREF(pvThis);
1895 *pcbMax = INT64_MAX; /** @todo */
1896 return VINF_SUCCESS;
1897}
1898
1899
1900/**
1901 * EXT file operations.
1902 */
1903static const RTVFSFILEOPS g_rtFsExtFileOps =
1904{
1905 { /* Stream */
1906 { /* Obj */
1907 RTVFSOBJOPS_VERSION,
1908 RTVFSOBJTYPE_FILE,
1909 "EXT File",
1910 rtFsExtFile_Close,
1911 rtFsExtFile_QueryInfo,
1912 NULL,
1913 RTVFSOBJOPS_VERSION
1914 },
1915 RTVFSIOSTREAMOPS_VERSION,
1916 RTVFSIOSTREAMOPS_FEAT_NO_SG,
1917 rtFsExtFile_Read,
1918 rtFsExtFile_Write,
1919 rtFsExtFile_Flush,
1920 NULL /*PollOne*/,
1921 rtFsExtFile_Tell,
1922 NULL /*pfnSkip*/,
1923 NULL /*pfnZeroFill*/,
1924 RTVFSIOSTREAMOPS_VERSION,
1925 },
1926 RTVFSFILEOPS_VERSION,
1927 0,
1928 { /* ObjSet */
1929 RTVFSOBJSETOPS_VERSION,
1930 RT_UOFFSETOF(RTVFSFILEOPS, ObjSet) - RT_UOFFSETOF(RTVFSFILEOPS, Stream.Obj),
1931 rtFsExtFile_SetMode,
1932 rtFsExtFile_SetTimes,
1933 rtFsExtFile_SetOwner,
1934 RTVFSOBJSETOPS_VERSION
1935 },
1936 rtFsExtFile_Seek,
1937 rtFsExtFile_QuerySize,
1938 rtFsExtFile_SetSize,
1939 rtFsExtFile_QueryMaxSize,
1940 RTVFSFILEOPS_VERSION
1941};
1942
1943
1944/**
1945 * Creates a new VFS file from the given regular file inode.
1946 *
1947 * @returns IPRT status code.
1948 * @param pThis The ext volume instance.
1949 * @param fOpen Open flags passed.
1950 * @param iInode The inode for the file.
1951 * @param phVfsFile Where to store the VFS file handle on success.
1952 * @param pErrInfo Where to record additional error information on error, optional.
1953 * @param pszWhat Logging prefix.
1954 */
1955static int rtFsExtVol_NewFile(PRTFSEXTVOL pThis, uint64_t fOpen, uint32_t iInode,
1956 PRTVFSFILE phVfsFile, PRTERRINFO pErrInfo, const char *pszWhat)
1957{
1958 /*
1959 * Load the inode and check that it really is a file.
1960 */
1961 PRTFSEXTINODE pInode = NULL;
1962 int rc = rtFsExtInodeLoad(pThis, iInode, &pInode);
1963 if (RT_SUCCESS(rc))
1964 {
1965 if (RTFS_IS_FILE(pInode->ObjInfo.Attr.fMode))
1966 {
1967 PRTFSEXTFILE pNewFile;
1968 rc = RTVfsNewFile(&g_rtFsExtFileOps, sizeof(*pNewFile), fOpen, pThis->hVfsSelf, NIL_RTVFSLOCK,
1969 phVfsFile, (void **)&pNewFile);
1970 if (RT_SUCCESS(rc))
1971 {
1972 pNewFile->pVol = pThis;
1973 pNewFile->pInode = pInode;
1974 pNewFile->offFile = 0;
1975 }
1976 }
1977 else
1978 rc = RTERRINFO_LOG_SET_F(pErrInfo, VERR_NOT_A_FILE, "%s: fMode=%#RX32", pszWhat, pInode->ObjInfo.Attr.fMode);
1979
1980 if (RT_FAILURE(rc))
1981 rtFsExtInodeRelease(pThis, pInode);
1982 }
1983
1984 return rc;
1985}
1986
1987
1988
1989/*
1990 *
1991 * EXT directory code.
1992 * EXT directory code.
1993 * EXT directory code.
1994 *
1995 */
1996
1997/**
1998 * Looks up an entry in the given directory inode.
1999 *
2000 * @returns IPRT status code.
2001 * @param pThis The ext volume instance.
2002 * @param pInode The directory inode structure to.
2003 * @param pszEntry The entry to lookup.
2004 * @param piInode Where to store the inode number if the entry was found.
2005 */
2006static int rtFsExtDir_Lookup(PRTFSEXTVOL pThis, PRTFSEXTINODE pInode, const char *pszEntry, uint32_t *piInode)
2007{
2008 uint64_t offEntry = 0;
2009 int rc = VERR_FILE_NOT_FOUND;
2010 uint32_t idxDirEntry = 0;
2011 size_t cchEntry = strlen(pszEntry);
2012
2013 if (cchEntry > 255)
2014 return VERR_FILENAME_TOO_LONG;
2015
2016 while (offEntry < (uint64_t)pInode->ObjInfo.cbObject)
2017 {
2018 EXTDIRENTRYEX DirEntry;
2019 size_t cbThis = RT_MIN(sizeof(DirEntry), (uint64_t)pInode->ObjInfo.cbObject - offEntry);
2020 int rc2 = rtFsExtInode_Read(pThis, pInode, offEntry, &DirEntry, cbThis, NULL);
2021 if (RT_SUCCESS(rc2))
2022 {
2023#ifdef LOG_ENABLED
2024 rtFsExtDirEntry_Log(pThis, idxDirEntry, &DirEntry);
2025#endif
2026
2027 uint16_t cbName = pThis->fFeaturesIncompat & EXT_SB_FEAT_INCOMPAT_DIR_FILETYPE
2028 ? DirEntry.Core.u.v2.cbName
2029 : RT_LE2H_U16(DirEntry.Core.u.v1.cbName);
2030 if ( cchEntry == cbName
2031 && !memcmp(pszEntry, &DirEntry.Core.achName[0], cchEntry))
2032 {
2033 *piInode = RT_LE2H_U32(DirEntry.Core.iInodeRef);
2034 rc = VINF_SUCCESS;
2035 break;
2036 }
2037
2038 offEntry += RT_LE2H_U16(DirEntry.Core.cbRecord);
2039 idxDirEntry++;
2040 }
2041 else
2042 {
2043 rc = rc2;
2044 break;
2045 }
2046 }
2047
2048 return rc;
2049}
2050
2051
2052
2053/*
2054 *
2055 * Directory instance methods
2056 * Directory instance methods
2057 * Directory instance methods
2058 *
2059 */
2060
2061/**
2062 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
2063 */
2064static DECLCALLBACK(int) rtFsExtDir_Close(void *pvThis)
2065{
2066 PRTFSEXTDIR pThis = (PRTFSEXTDIR)pvThis;
2067 LogFlowFunc(("pThis=%p\n", pThis));
2068 rtFsExtInodeRelease(pThis->pVol, pThis->pInode);
2069 pThis->pInode = NULL;
2070 return VINF_SUCCESS;
2071}
2072
2073
2074/**
2075 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
2076 */
2077static DECLCALLBACK(int) rtFsExtDir_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2078{
2079 PRTFSEXTDIR pThis = (PRTFSEXTDIR)pvThis;
2080 LogFlowFunc(("\n"));
2081 return rtFsExtInode_QueryInfo(pThis->pInode, pObjInfo, enmAddAttr);
2082}
2083
2084
2085/**
2086 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
2087 */
2088static DECLCALLBACK(int) rtFsExtDir_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
2089{
2090 LogFlowFunc(("\n"));
2091 RT_NOREF(pvThis, fMode, fMask);
2092 return VERR_WRITE_PROTECT;
2093}
2094
2095
2096/**
2097 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
2098 */
2099static DECLCALLBACK(int) rtFsExtDir_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
2100 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
2101{
2102 LogFlowFunc(("\n"));
2103 RT_NOREF(pvThis, pAccessTime, pModificationTime, pChangeTime, pBirthTime);
2104 return VERR_WRITE_PROTECT;
2105}
2106
2107
2108/**
2109 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
2110 */
2111static DECLCALLBACK(int) rtFsExtDir_SetOwner(void *pvThis, RTUID uid, RTGID gid)
2112{
2113 LogFlowFunc(("\n"));
2114 RT_NOREF(pvThis, uid, gid);
2115 return VERR_WRITE_PROTECT;
2116}
2117
2118
2119/**
2120 * @interface_method_impl{RTVFSDIROPS,pfnOpen}
2121 */
2122static DECLCALLBACK(int) rtFsExtDir_Open(void *pvThis, const char *pszEntry, uint64_t fOpen,
2123 uint32_t fFlags, PRTVFSOBJ phVfsObj)
2124{
2125 LogFlowFunc(("pszEntry='%s' fOpen=%#RX64 fFlags=%#x\n", pszEntry, fOpen, fFlags));
2126 PRTFSEXTDIR pThis = (PRTFSEXTDIR)pvThis;
2127 PRTFSEXTVOL pVol = pThis->pVol;
2128 int rc = VINF_SUCCESS;
2129
2130 RT_NOREF(fFlags);
2131
2132 /*
2133 * We cannot create or replace anything, just open stuff.
2134 */
2135 if ( (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN
2136 || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN_CREATE)
2137 { /* likely */ }
2138 else
2139 return VERR_WRITE_PROTECT;
2140
2141 /*
2142 * Lookup the entry.
2143 */
2144 uint32_t iInode = 0;
2145 rc = rtFsExtDir_Lookup(pVol, pThis->pInode, pszEntry, &iInode);
2146 if (RT_SUCCESS(rc))
2147 {
2148 PRTFSEXTINODE pInode = NULL;
2149 rc = rtFsExtInodeLoad(pVol, iInode, &pInode);
2150 if (RT_SUCCESS(rc))
2151 {
2152 if (RTFS_IS_DIRECTORY(pInode->ObjInfo.Attr.fMode))
2153 {
2154 RTVFSDIR hVfsDir;
2155 rc = rtFsExtVol_OpenDirByInode(pVol, iInode, &hVfsDir);
2156 if (RT_SUCCESS(rc))
2157 {
2158 *phVfsObj = RTVfsObjFromDir(hVfsDir);
2159 RTVfsDirRelease(hVfsDir);
2160 AssertStmt(*phVfsObj != NIL_RTVFSOBJ, rc = VERR_INTERNAL_ERROR_3);
2161 }
2162 }
2163 else if (RTFS_IS_FILE(pInode->ObjInfo.Attr.fMode))
2164 {
2165 RTVFSFILE hVfsFile;
2166 rc = rtFsExtVol_NewFile(pVol, fOpen, iInode, &hVfsFile, NULL, pszEntry);
2167 if (RT_SUCCESS(rc))
2168 {
2169 *phVfsObj = RTVfsObjFromFile(hVfsFile);
2170 RTVfsFileRelease(hVfsFile);
2171 AssertStmt(*phVfsObj != NIL_RTVFSOBJ, rc = VERR_INTERNAL_ERROR_3);
2172 }
2173 }
2174 else
2175 rc = VERR_NOT_SUPPORTED;
2176 }
2177 }
2178
2179 LogFlow(("rtFsExtDir_Open(%s): returns %Rrc\n", pszEntry, rc));
2180 return rc;
2181}
2182
2183
2184/**
2185 * @interface_method_impl{RTVFSDIROPS,pfnCreateDir}
2186 */
2187static DECLCALLBACK(int) rtFsExtDir_CreateDir(void *pvThis, const char *pszSubDir, RTFMODE fMode, PRTVFSDIR phVfsDir)
2188{
2189 RT_NOREF(pvThis, pszSubDir, fMode, phVfsDir);
2190 LogFlowFunc(("\n"));
2191 return VERR_WRITE_PROTECT;
2192}
2193
2194
2195/**
2196 * @interface_method_impl{RTVFSDIROPS,pfnOpenSymlink}
2197 */
2198static DECLCALLBACK(int) rtFsExtDir_OpenSymlink(void *pvThis, const char *pszSymlink, PRTVFSSYMLINK phVfsSymlink)
2199{
2200 RT_NOREF(pvThis, pszSymlink, phVfsSymlink);
2201 LogFlowFunc(("\n"));
2202 return VERR_NOT_SUPPORTED;
2203}
2204
2205
2206/**
2207 * @interface_method_impl{RTVFSDIROPS,pfnCreateSymlink}
2208 */
2209static DECLCALLBACK(int) rtFsExtDir_CreateSymlink(void *pvThis, const char *pszSymlink, const char *pszTarget,
2210 RTSYMLINKTYPE enmType, PRTVFSSYMLINK phVfsSymlink)
2211{
2212 RT_NOREF(pvThis, pszSymlink, pszTarget, enmType, phVfsSymlink);
2213 LogFlowFunc(("\n"));
2214 return VERR_WRITE_PROTECT;
2215}
2216
2217
2218/**
2219 * @interface_method_impl{RTVFSDIROPS,pfnUnlinkEntry}
2220 */
2221static DECLCALLBACK(int) rtFsExtDir_UnlinkEntry(void *pvThis, const char *pszEntry, RTFMODE fType)
2222{
2223 RT_NOREF(pvThis, pszEntry, fType);
2224 LogFlowFunc(("\n"));
2225 return VERR_WRITE_PROTECT;
2226}
2227
2228
2229/**
2230 * @interface_method_impl{RTVFSDIROPS,pfnRenameEntry}
2231 */
2232static DECLCALLBACK(int) rtFsExtDir_RenameEntry(void *pvThis, const char *pszEntry, RTFMODE fType, const char *pszNewName)
2233{
2234 RT_NOREF(pvThis, pszEntry, fType, pszNewName);
2235 LogFlowFunc(("\n"));
2236 return VERR_WRITE_PROTECT;
2237}
2238
2239
2240/**
2241 * @interface_method_impl{RTVFSDIROPS,pfnRewindDir}
2242 */
2243static DECLCALLBACK(int) rtFsExtDir_RewindDir(void *pvThis)
2244{
2245 PRTFSEXTDIR pThis = (PRTFSEXTDIR)pvThis;
2246 LogFlowFunc(("\n"));
2247
2248 pThis->fNoMoreFiles = false;
2249 pThis->offEntry = 0;
2250 pThis->idxEntry = 0;
2251 return VINF_SUCCESS;
2252}
2253
2254
2255/**
2256 * @interface_method_impl{RTVFSDIROPS,pfnReadDir}
2257 */
2258static DECLCALLBACK(int) rtFsExtDir_ReadDir(void *pvThis, PRTDIRENTRYEX pDirEntry, size_t *pcbDirEntry,
2259 RTFSOBJATTRADD enmAddAttr)
2260{
2261 PRTFSEXTDIR pThis = (PRTFSEXTDIR)pvThis;
2262 PRTFSEXTINODE pInode = pThis->pInode;
2263 LogFlowFunc(("\n"));
2264
2265 if (pThis->fNoMoreFiles)
2266 return VERR_NO_MORE_FILES;
2267
2268 EXTDIRENTRYEX DirEntry;
2269 size_t cbThis = RT_MIN(sizeof(DirEntry), (uint64_t)pInode->ObjInfo.cbObject - pThis->offEntry);
2270 int rc = rtFsExtInode_Read(pThis->pVol, pInode, pThis->offEntry, &DirEntry, cbThis, NULL);
2271 if (RT_SUCCESS(rc))
2272 {
2273#ifdef LOG_ENABLED
2274 rtFsExtDirEntry_Log(pThis->pVol, pThis->idxEntry, &DirEntry);
2275#endif
2276
2277 /* 0 inode entry means unused entry. */
2278 /** @todo Can there be unused entries somewhere in the middle? */
2279 uint32_t iInodeRef = RT_LE2H_U32(DirEntry.Core.iInodeRef);
2280 if (iInodeRef != 0)
2281 {
2282 uint16_t cbName = pThis->pVol->fFeaturesIncompat & EXT_SB_FEAT_INCOMPAT_DIR_FILETYPE
2283 ? DirEntry.Core.u.v2.cbName
2284 : RT_LE2H_U16(DirEntry.Core.u.v1.cbName);
2285
2286 if (cbName <= 255)
2287 {
2288 size_t const cbDirEntry = *pcbDirEntry;
2289
2290 *pcbDirEntry = RT_UOFFSETOF_DYN(RTDIRENTRYEX, szName[cbName + 2]);
2291 if (*pcbDirEntry <= cbDirEntry)
2292 {
2293 /* Load the referenced inode. */
2294 PRTFSEXTINODE pInodeRef;
2295 rc = rtFsExtInodeLoad(pThis->pVol, iInodeRef, &pInodeRef);
2296 if (RT_SUCCESS(rc))
2297 {
2298 memcpy(&pDirEntry->szName[0], &DirEntry.Core.achName[0], cbName);
2299 pDirEntry->szName[cbName] = '\0';
2300 pDirEntry->cbName = cbName;
2301 rc = rtFsExtInode_QueryInfo(pInodeRef, &pDirEntry->Info, enmAddAttr);
2302 if (RT_SUCCESS(rc))
2303 {
2304 pThis->offEntry += RT_LE2H_U16(DirEntry.Core.cbRecord);
2305 pThis->idxEntry++;
2306 rtFsExtInodeRelease(pThis->pVol, pInodeRef);
2307 return VINF_SUCCESS;
2308 }
2309 rtFsExtInodeRelease(pThis->pVol, pInodeRef);
2310 }
2311 }
2312 else
2313 rc = VERR_BUFFER_OVERFLOW;
2314 }
2315 else
2316 rc = VERR_FILENAME_TOO_LONG;
2317 }
2318 else
2319 {
2320 rc = VERR_NO_MORE_FILES;
2321 LogFlowFunc(("no more files\n"));
2322 pThis->fNoMoreFiles = true;
2323 }
2324 }
2325
2326 return rc;
2327}
2328
2329
2330/**
2331 * EXT directory operations.
2332 */
2333static const RTVFSDIROPS g_rtFsExtDirOps =
2334{
2335 { /* Obj */
2336 RTVFSOBJOPS_VERSION,
2337 RTVFSOBJTYPE_DIR,
2338 "EXT Dir",
2339 rtFsExtDir_Close,
2340 rtFsExtDir_QueryInfo,
2341 NULL,
2342 RTVFSOBJOPS_VERSION
2343 },
2344 RTVFSDIROPS_VERSION,
2345 0,
2346 { /* ObjSet */
2347 RTVFSOBJSETOPS_VERSION,
2348 RT_UOFFSETOF(RTVFSDIROPS, ObjSet) - RT_UOFFSETOF(RTVFSDIROPS, Obj),
2349 rtFsExtDir_SetMode,
2350 rtFsExtDir_SetTimes,
2351 rtFsExtDir_SetOwner,
2352 RTVFSOBJSETOPS_VERSION
2353 },
2354 rtFsExtDir_Open,
2355 NULL /* pfnFollowAbsoluteSymlink */,
2356 NULL /* pfnOpenFile */,
2357 NULL /* pfnOpenDir */,
2358 rtFsExtDir_CreateDir,
2359 rtFsExtDir_OpenSymlink,
2360 rtFsExtDir_CreateSymlink,
2361 NULL /* pfnQueryEntryInfo */,
2362 rtFsExtDir_UnlinkEntry,
2363 rtFsExtDir_RenameEntry,
2364 rtFsExtDir_RewindDir,
2365 rtFsExtDir_ReadDir,
2366 RTVFSDIROPS_VERSION,
2367};
2368
2369
2370/**
2371 * Opens a directory by the given inode.
2372 *
2373 * @returns IPRT status code.
2374 * @param pThis The ext volume instance.
2375 * @param iInode The inode to open.
2376 * @param phVfsDir Where to store the handle to the VFS directory on success.
2377 */
2378static int rtFsExtVol_OpenDirByInode(PRTFSEXTVOL pThis, uint32_t iInode, PRTVFSDIR phVfsDir)
2379{
2380 PRTFSEXTINODE pInode = NULL;
2381 int rc = rtFsExtInodeLoad(pThis, iInode, &pInode);
2382 if (RT_SUCCESS(rc))
2383 {
2384 if (RTFS_IS_DIRECTORY(pInode->ObjInfo.Attr.fMode))
2385 {
2386 PRTFSEXTDIR pNewDir;
2387 rc = RTVfsNewDir(&g_rtFsExtDirOps, sizeof(*pNewDir), 0 /*fFlags*/, pThis->hVfsSelf, NIL_RTVFSLOCK,
2388 phVfsDir, (void **)&pNewDir);
2389 if (RT_SUCCESS(rc))
2390 {
2391 pNewDir->fNoMoreFiles = false;
2392 pNewDir->pVol = pThis;
2393 pNewDir->pInode = pInode;
2394 }
2395 }
2396 else
2397 rc = VERR_VFS_BOGUS_FORMAT;
2398
2399 if (RT_FAILURE(rc))
2400 rtFsExtInodeRelease(pThis, pInode);
2401 }
2402
2403 return rc;
2404}
2405
2406
2407
2408/*
2409 *
2410 * Volume level code.
2411 * Volume level code.
2412 * Volume level code.
2413 *
2414 */
2415
2416/**
2417 * Checks whether the block range in the given block group is in use by checking the
2418 * block bitmap.
2419 *
2420 * @returns Flag whether the range is in use.
2421 * @param pBlkGrpDesc The block group to check for.
2422 * @param iBlockStart The starting block to check relative from the beginning of the block group.
2423 * @param cBlocks How many blocks to check.
2424 */
2425static bool rtFsExtIsBlockRangeInUse(PRTFSEXTBLKGRP pBlkGrpDesc, uint64_t iBlockStart, size_t cBlocks)
2426{
2427 /** @todo Optimize with ASMBitFirstSet(). */
2428 while (cBlocks)
2429 {
2430 uint32_t idxByte = iBlockStart / 8;
2431 uint32_t iBit = iBlockStart % 8;
2432
2433 if (pBlkGrpDesc->abBlockBitmap[idxByte] & RT_BIT(iBit))
2434 return true;
2435
2436 cBlocks--;
2437 iBlockStart++;
2438 }
2439
2440 return false;
2441}
2442
2443
2444static DECLCALLBACK(int) rtFsExtVolBlockGroupTreeDestroy(PAVLU32NODECORE pCore, void *pvUser)
2445{
2446 RT_NOREF(pvUser);
2447
2448 PRTFSEXTBLKGRP pBlockGroup = (PRTFSEXTBLKGRP)pCore;
2449 Assert(!pBlockGroup->cRefs);
2450 RTMemFree(pBlockGroup);
2451 return VINF_SUCCESS;
2452}
2453
2454
2455static DECLCALLBACK(int) rtFsExtVolInodeTreeDestroy(PAVLU32NODECORE pCore, void *pvUser)
2456{
2457 RT_NOREF(pvUser);
2458
2459 PRTFSEXTINODE pInode = (PRTFSEXTINODE)pCore;
2460 Assert(!pInode->cRefs);
2461 RTMemFree(pInode);
2462 return VINF_SUCCESS;
2463}
2464
2465
2466static DECLCALLBACK(int) rtFsExtVolBlockTreeDestroy(PAVLU64NODECORE pCore, void *pvUser)
2467{
2468 RT_NOREF(pvUser);
2469
2470 PRTFSEXTBLOCKENTRY pBlock = (PRTFSEXTBLOCKENTRY)pCore;
2471 Assert(!pBlock->cRefs);
2472 RTMemFree(pBlock);
2473 return VINF_SUCCESS;
2474}
2475
2476
2477/**
2478 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnClose}
2479 */
2480static DECLCALLBACK(int) rtFsExtVol_Close(void *pvThis)
2481{
2482 PRTFSEXTVOL pThis = (PRTFSEXTVOL)pvThis;
2483
2484 /* Destroy the block group tree. */
2485 RTAvlU32Destroy(&pThis->BlockGroupRoot, rtFsExtVolBlockGroupTreeDestroy, pThis);
2486 pThis->BlockGroupRoot = NULL;
2487 RTListInit(&pThis->LstBlockGroupLru);
2488
2489 /* Destroy the inode tree. */
2490 RTAvlU32Destroy(&pThis->InodeRoot, rtFsExtVolInodeTreeDestroy, pThis);
2491 pThis->InodeRoot = NULL;
2492 RTListInit(&pThis->LstInodeLru);
2493
2494 /* Destroy the block cache tree. */
2495 RTAvlU64Destroy(&pThis->BlockRoot, rtFsExtVolBlockTreeDestroy, pThis);
2496 pThis->BlockRoot = NULL;
2497 RTListInit(&pThis->LstBlockLru);
2498
2499 /*
2500 * Backing file and handles.
2501 */
2502 RTVfsFileRelease(pThis->hVfsBacking);
2503 pThis->hVfsBacking = NIL_RTVFSFILE;
2504 pThis->hVfsSelf = NIL_RTVFS;
2505
2506 return VINF_SUCCESS;
2507}
2508
2509
2510/**
2511 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnQueryInfo}
2512 */
2513static DECLCALLBACK(int) rtFsExtVol_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
2514{
2515 RT_NOREF(pvThis, pObjInfo, enmAddAttr);
2516 return VERR_WRONG_TYPE;
2517}
2518
2519
2520/**
2521 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnOpenRoot}
2522 */
2523static DECLCALLBACK(int) rtFsExtVol_OpenRoot(void *pvThis, PRTVFSDIR phVfsDir)
2524{
2525 PRTFSEXTVOL pThis = (PRTFSEXTVOL)pvThis;
2526 int rc = rtFsExtVol_OpenDirByInode(pThis, EXT_INODE_NR_ROOT_DIR, phVfsDir);
2527 LogFlowFunc(("returns %Rrc\n", rc));
2528 return rc;
2529}
2530
2531
2532/**
2533 * @interface_method_impl{RTVFSOBJOPS::Obj,pfnQueryRangeState}
2534 */
2535static DECLCALLBACK(int) rtFsExtVol_QueryRangeState(void *pvThis, uint64_t off, size_t cb, bool *pfUsed)
2536{
2537 int rc = VINF_SUCCESS;
2538 PRTFSEXTVOL pThis = (PRTFSEXTVOL)pvThis;
2539
2540 *pfUsed = false;
2541
2542 uint64_t iBlock = rtFsExtDiskOffsetToBlockIdx(pThis, off);
2543 uint64_t cBlocks = rtFsExtDiskOffsetToBlockIdx(pThis, cb) + (cb % pThis->cbBlock ? 1 : 0);
2544 while (cBlocks > 0)
2545 {
2546 uint32_t const iBlockGroup = iBlock / pThis->cBlocksPerGroup;
2547 uint32_t const iBlockRelStart = iBlock - iBlockGroup * pThis->cBlocksPerGroup;
2548 PRTFSEXTBLKGRP pBlockGroup = NULL;
2549
2550 rc = rtFsExtBlockGroupLoad(pThis, iBlockGroup, &pBlockGroup);
2551 if (RT_FAILURE(rc))
2552 break;
2553
2554 uint64_t cBlocksThis = RT_MIN(cBlocks, iBlockRelStart - pThis->cBlocksPerGroup);
2555 if (rtFsExtIsBlockRangeInUse(pBlockGroup, iBlockRelStart, cBlocksThis))
2556 {
2557 *pfUsed = true;
2558 break;
2559 }
2560
2561 rtFsExtBlockGroupRelease(pThis, pBlockGroup);
2562 cBlocks -= cBlocksThis;
2563 iBlock += cBlocksThis;
2564 }
2565
2566 return rc;
2567}
2568
2569
2570DECL_HIDDEN_CONST(const RTVFSOPS) g_rtFsExtVolOps =
2571{
2572 /* .Obj = */
2573 {
2574 /* .uVersion = */ RTVFSOBJOPS_VERSION,
2575 /* .enmType = */ RTVFSOBJTYPE_VFS,
2576 /* .pszName = */ "ExtVol",
2577 /* .pfnClose = */ rtFsExtVol_Close,
2578 /* .pfnQueryInfo = */ rtFsExtVol_QueryInfo,
2579 /* .pfnQueryInfoEx = */ NULL,
2580 /* .uEndMarker = */ RTVFSOBJOPS_VERSION
2581 },
2582 /* .uVersion = */ RTVFSOPS_VERSION,
2583 /* .fFeatures = */ 0,
2584 /* .pfnOpenRoot = */ rtFsExtVol_OpenRoot,
2585 /* .pfnQueryRangeState = */ rtFsExtVol_QueryRangeState,
2586 /* .uEndMarker = */ RTVFSOPS_VERSION
2587};
2588
2589
2590
2591/**
2592 * Loads the parameters from the given original ext2 format superblock (EXT_SB_REV_ORIG).
2593 *
2594 * @returns IPRT status code.
2595 * @param pThis The ext volume instance.
2596 * @param pSb The superblock to load.
2597 * @param pErrInfo Where to return additional error info.
2598 */
2599static int rtFsExtVolLoadAndParseSuperBlockV0(PRTFSEXTVOL pThis, PCEXTSUPERBLOCK pSb, PRTERRINFO pErrInfo)
2600{
2601 RT_NOREF(pErrInfo);
2602
2603 /*
2604 * Linux never supported a differing cluster (also called fragment) size for
2605 * the original ext2 layout so we reject such filesystems as it is not clear what
2606 * the purpose is really.
2607 */
2608 if (RT_LE2H_U32(pSb->cLogBlockSize) != RT_LE2H_U32(pSb->cLogClusterSize))
2609 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "EXT filesystem cluster and block size differ");
2610
2611 pThis->f64Bit = false;
2612 pThis->cBlockShift = 10 + RT_LE2H_U32(pSb->cLogBlockSize);
2613 pThis->cbBlock = UINT64_C(1) << pThis->cBlockShift;
2614 pThis->cbInode = sizeof(EXTINODE);
2615 pThis->cbBlkGrpDesc = sizeof(EXTBLOCKGROUPDESC32);
2616 pThis->cBlocksPerGroup = RT_LE2H_U32(pSb->cBlocksPerGroup);
2617 pThis->cInodesPerGroup = RT_LE2H_U32(pSb->cInodesPerBlockGroup);
2618 pThis->cBlockGroups = RT_LE2H_U32(pSb->cBlocksTotalLow) / pThis->cBlocksPerGroup;
2619 pThis->cbBlockBitmap = pThis->cBlocksPerGroup / 8;
2620 if (pThis->cBlocksPerGroup % 8)
2621 pThis->cbBlockBitmap++;
2622 pThis->cbInodeBitmap = pThis->cInodesPerGroup / 8;
2623 if (pThis->cInodesPerGroup % 8)
2624 pThis->cbInodeBitmap++;
2625
2626 return VINF_SUCCESS;
2627}
2628
2629
2630/**
2631 * Loads the parameters from the given ext superblock (EXT_SB_REV_V2_DYN_INODE_SZ).
2632 *
2633 * @returns IPRT status code.
2634 * @param pThis The ext volume instance.
2635 * @param pSb The superblock to load.
2636 * @param pErrInfo Where to return additional error info.
2637 */
2638static int rtFsExtVolLoadAndParseSuperBlockV1(PRTFSEXTVOL pThis, PCEXTSUPERBLOCK pSb, PRTERRINFO pErrInfo)
2639{
2640 if ((RT_LE2H_U32(pSb->fFeaturesIncompat) & ~RTFSEXT_INCOMPAT_FEATURES_SUPP) != 0)
2641 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "EXT filesystem contains unsupported incompatible features: %RX32",
2642 RT_LE2H_U32(pSb->fFeaturesIncompat) & ~RTFSEXT_INCOMPAT_FEATURES_SUPP);
2643 if ( RT_LE2H_U32(pSb->fFeaturesCompatRo) != 0
2644 && !(pThis->fMntFlags & RTVFSMNT_F_READ_ONLY))
2645 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "EXT filesystem contains unsupported readonly features: %RX32",
2646 RT_LE2H_U32(pSb->fFeaturesCompatRo));
2647
2648 pThis->fFeaturesIncompat = RT_LE2H_U32(pSb->fFeaturesIncompat);
2649 pThis->f64Bit = RT_BOOL(pThis->fFeaturesIncompat & EXT_SB_FEAT_INCOMPAT_64BIT);
2650 pThis->cBlockShift = 10 + RT_LE2H_U32(pSb->cLogBlockSize);
2651 pThis->cbBlock = UINT64_C(1) << pThis->cBlockShift;
2652 pThis->cbInode = RT_LE2H_U16(pSb->cbInode);
2653 pThis->cbBlkGrpDesc = pThis->f64Bit ? RT_LE2H_U16(pSb->cbGroupDesc) : sizeof(EXTBLOCKGROUPDESC32);
2654 pThis->cBlocksPerGroup = RT_LE2H_U32(pSb->cBlocksPerGroup);
2655 pThis->cInodesPerGroup = RT_LE2H_U32(pSb->cInodesPerBlockGroup);
2656 pThis->cBlockGroups = RT_LE2H_U32(pSb->cBlocksTotalLow) / pThis->cBlocksPerGroup;
2657 pThis->cbBlockBitmap = pThis->cBlocksPerGroup / 8;
2658 if (pThis->cBlocksPerGroup % 8)
2659 pThis->cbBlockBitmap++;
2660 pThis->cbInodeBitmap = pThis->cInodesPerGroup / 8;
2661 if (pThis->cInodesPerGroup % 8)
2662 pThis->cbInodeBitmap++;
2663
2664 return VINF_SUCCESS;
2665}
2666
2667
2668/**
2669 * Loads and parses the superblock of the filesystem.
2670 *
2671 * @returns IPRT status code.
2672 * @param pThis The ext volume instance.
2673 * @param pErrInfo Where to return additional error info.
2674 */
2675static int rtFsExtVolLoadAndParseSuperblock(PRTFSEXTVOL pThis, PRTERRINFO pErrInfo)
2676{
2677 int rc = VINF_SUCCESS;
2678 EXTSUPERBLOCK Sb;
2679 rc = RTVfsFileReadAt(pThis->hVfsBacking, EXT_SB_OFFSET, &Sb, sizeof(EXTSUPERBLOCK), NULL);
2680 if (RT_FAILURE(rc))
2681 return RTERRINFO_LOG_SET(pErrInfo, rc, "Error reading super block");
2682
2683 /* Validate the superblock. */
2684 if (RT_LE2H_U16(Sb.u16Signature) != EXT_SB_SIGNATURE)
2685 return RTERRINFO_LOG_SET_F(pErrInfo, VERR_VFS_UNKNOWN_FORMAT, "Not EXT - Signature mismatch: %RX16", RT_LE2H_U16(Sb.u16Signature));
2686
2687#ifdef LOG_ENABLED
2688 rtFsExtSb_Log(&Sb);
2689#endif
2690
2691 if (RT_LE2H_U16(Sb.u16FilesystemState) == EXT_SB_STATE_ERRORS)
2692 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "EXT filesystem contains errors");
2693
2694 if (RT_LE2H_U32(Sb.u32RevLvl) == EXT_SB_REV_ORIG)
2695 rc = rtFsExtVolLoadAndParseSuperBlockV0(pThis, &Sb, pErrInfo);
2696 else
2697 rc = rtFsExtVolLoadAndParseSuperBlockV1(pThis, &Sb, pErrInfo);
2698
2699 return rc;
2700}
2701
2702
2703RTDECL(int) RTFsExtVolOpen(RTVFSFILE hVfsFileIn, uint32_t fMntFlags, uint32_t fExtFlags, PRTVFS phVfs, PRTERRINFO pErrInfo)
2704{
2705 AssertPtrReturn(phVfs, VERR_INVALID_POINTER);
2706 AssertReturn(!(fMntFlags & ~RTVFSMNT_F_VALID_MASK), VERR_INVALID_FLAGS);
2707 AssertReturn(!fExtFlags, VERR_INVALID_FLAGS);
2708
2709 uint32_t cRefs = RTVfsFileRetain(hVfsFileIn);
2710 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
2711
2712 /*
2713 * Create a VFS instance and initialize the data so rtFsExtVol_Close works.
2714 */
2715 RTVFS hVfs;
2716 PRTFSEXTVOL pThis;
2717 int rc = RTVfsNew(&g_rtFsExtVolOps, sizeof(*pThis), NIL_RTVFS, RTVFSLOCK_CREATE_RW, &hVfs, (void **)&pThis);
2718 if (RT_SUCCESS(rc))
2719 {
2720 pThis->hVfsBacking = hVfsFileIn;
2721 pThis->hVfsSelf = hVfs;
2722 pThis->fMntFlags = fMntFlags;
2723 pThis->fExtFlags = fExtFlags;
2724 pThis->BlockGroupRoot = NULL;
2725 pThis->InodeRoot = NULL;
2726 pThis->BlockRoot = NULL;
2727 pThis->cbBlockGroups = 0;
2728 pThis->cbInodes = 0;
2729 pThis->cbBlocks = 0;
2730 RTListInit(&pThis->LstBlockGroupLru);
2731 RTListInit(&pThis->LstInodeLru);
2732 RTListInit(&pThis->LstBlockLru);
2733
2734 rc = RTVfsFileQuerySize(pThis->hVfsBacking, &pThis->cbBacking);
2735 if (RT_SUCCESS(rc))
2736 {
2737 rc = rtFsExtVolLoadAndParseSuperblock(pThis, pErrInfo);
2738 if (RT_SUCCESS(rc))
2739 {
2740 *phVfs = hVfs;
2741 return VINF_SUCCESS;
2742 }
2743 }
2744
2745 RTVfsRelease(hVfs);
2746 *phVfs = NIL_RTVFS;
2747 }
2748 else
2749 RTVfsFileRelease(hVfsFileIn);
2750
2751 return rc;
2752}
2753
2754
2755/**
2756 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
2757 */
2758static DECLCALLBACK(int) rtVfsChainExtVol_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
2759 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
2760{
2761 RT_NOREF(pProviderReg);
2762
2763 /*
2764 * Basic checks.
2765 */
2766 if (pElement->enmTypeIn != RTVFSOBJTYPE_FILE)
2767 return pElement->enmTypeIn == RTVFSOBJTYPE_INVALID ? VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT : VERR_VFS_CHAIN_TAKES_FILE;
2768 if ( pElement->enmType != RTVFSOBJTYPE_VFS
2769 && pElement->enmType != RTVFSOBJTYPE_DIR)
2770 return VERR_VFS_CHAIN_ONLY_DIR_OR_VFS;
2771 if (pElement->cArgs > 1)
2772 return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
2773
2774 /*
2775 * Parse the flag if present, save in pElement->uProvider.
2776 */
2777 bool fReadOnly = (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ;
2778 if (pElement->cArgs > 0)
2779 {
2780 const char *psz = pElement->paArgs[0].psz;
2781 if (*psz)
2782 {
2783 if (!strcmp(psz, "ro"))
2784 fReadOnly = true;
2785 else if (!strcmp(psz, "rw"))
2786 fReadOnly = false;
2787 else
2788 {
2789 *poffError = pElement->paArgs[0].offSpec;
2790 return RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected 'ro' or 'rw' as argument");
2791 }
2792 }
2793 }
2794
2795 pElement->uProvider = fReadOnly ? RTVFSMNT_F_READ_ONLY : 0;
2796 return VINF_SUCCESS;
2797}
2798
2799
2800/**
2801 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
2802 */
2803static DECLCALLBACK(int) rtVfsChainExtVol_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
2804 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
2805 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
2806{
2807 RT_NOREF(pProviderReg, pSpec, poffError);
2808
2809 int rc;
2810 RTVFSFILE hVfsFileIn = RTVfsObjToFile(hPrevVfsObj);
2811 if (hVfsFileIn != NIL_RTVFSFILE)
2812 {
2813 RTVFS hVfs;
2814 rc = RTFsExtVolOpen(hVfsFileIn, (uint32_t)pElement->uProvider, (uint32_t)(pElement->uProvider >> 32), &hVfs, pErrInfo);
2815 RTVfsFileRelease(hVfsFileIn);
2816 if (RT_SUCCESS(rc))
2817 {
2818 *phVfsObj = RTVfsObjFromVfs(hVfs);
2819 RTVfsRelease(hVfs);
2820 if (*phVfsObj != NIL_RTVFSOBJ)
2821 return VINF_SUCCESS;
2822 rc = VERR_VFS_CHAIN_CAST_FAILED;
2823 }
2824 }
2825 else
2826 rc = VERR_VFS_CHAIN_CAST_FAILED;
2827 return rc;
2828}
2829
2830
2831/**
2832 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
2833 */
2834static DECLCALLBACK(bool) rtVfsChainExtVol_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
2835 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
2836 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
2837{
2838 RT_NOREF(pProviderReg, pSpec, pReuseSpec);
2839 if ( pElement->paArgs[0].uProvider == pReuseElement->paArgs[0].uProvider
2840 || !pReuseElement->paArgs[0].uProvider)
2841 return true;
2842 return false;
2843}
2844
2845
2846/** VFS chain element 'ext'. */
2847static RTVFSCHAINELEMENTREG g_rtVfsChainExtVolReg =
2848{
2849 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
2850 /* fReserved = */ 0,
2851 /* pszName = */ "ext",
2852 /* ListEntry = */ { NULL, NULL },
2853 /* pszHelp = */ "Open a EXT file system, requires a file object on the left side.\n"
2854 "First argument is an optional 'ro' (read-only) or 'rw' (read-write) flag.\n",
2855 /* pfnValidate = */ rtVfsChainExtVol_Validate,
2856 /* pfnInstantiate = */ rtVfsChainExtVol_Instantiate,
2857 /* pfnCanReuseElement = */ rtVfsChainExtVol_CanReuseElement,
2858 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
2859};
2860
2861RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainExtVolReg, rtVfsChainExtVolReg);
2862
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette