Skip to content

Commit 8fa54f8

Browse files
author
Hongzhen Luo
committed
[EROFS]: clean up erofs_fs
Hide the parts related to liberofs in erofs_fs.h so that erofs_fs.h can be included. Signed-off-by: Hongzhen Luo <[email protected]>
1 parent 355b288 commit 8fa54f8

File tree

4 files changed

+90
-46
lines changed

4 files changed

+90
-46
lines changed

src/overlaybd/tar/erofs/erofs_common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
#include <map>
2424
#include <set>
2525

26+
/* block-related definitions */
2627
#define SECTOR_SIZE 512ULL
2728
#define SECTOR_BITS 9
2829

30+
/* address alignment operation */
2931
#define round_down_blk(addr) ((addr) & (~(SECTOR_SIZE - 1)))
3032
#define round_up_blk(addr) (round_down_blk((addr) + SECTOR_SIZE - 1))
3133
#define erofs_min(a, b) (a) < (b) ? (a) : (b)
@@ -43,6 +45,10 @@ struct liberofs_inmem_sector {
4345
char data[SECTOR_SIZE];
4446
};
4547

48+
/*
49+
* Internal cache of EROFS, used to accelerate
50+
* the read and write operations of an IFile.
51+
*/
4652
class ErofsCache {
4753
public:
4854
ErofsCache(photon::fs::IFile *file, unsigned long int capacity):
@@ -59,6 +65,10 @@ class ErofsCache {
5965
std::set<u64> dirty;
6066
};
6167

68+
/*
69+
* Encapsulation of IFile by liberofs,
70+
* including I/O operations and ErofsCache.
71+
*/
6272
struct liberofs_file {
6373
struct erofs_vfops ops;
6474
photon::fs::IFile *file;

src/overlaybd/tar/erofs/erofs_fs.cpp

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,32 @@
2121
#include <dirent.h>
2222
#include <vector>
2323

24+
struct ErofsFileSystem::ErofsFileSystemInt {
25+
struct erofs_sb_info sbi;
26+
struct liberofs_file target_file;
27+
};
28+
struct ErofsFile::ErofsFileInt {
29+
struct erofs_inode inode;
30+
};
31+
2432
// ErofsFile
2533
ErofsFile::ErofsFile(ErofsFileSystem *fs): fs(fs)
2634
{
27-
memset(&inode, 0, sizeof(struct erofs_inode));
35+
ErofsFileSystem::ErofsFileSystemInt *fs_private = fs->fs_private;
36+
37+
file_private = new ErofsFileInt;
38+
if (!file_private)
39+
LOG_ERROR("[erofs_fs] fail to prepare for file_private");
40+
41+
memset(&file_private->inode, 0, sizeof(struct erofs_inode));
42+
file_private->inode.sbi = &fs_private->sbi;
43+
}
44+
45+
ErofsFile::~ErofsFile() {
46+
delete file_private;
47+
file_private = NULL;
2848
}
49+
2950
photon::fs::IFileSystem *ErofsFile::filesystem() { return fs; }
3051

3152
struct liberofs_nameidata {
@@ -185,15 +206,16 @@ static int do_erofs_ilookup(const char *path, struct erofs_inode *vi)
185206

186207
int ErofsFile::fstat(struct stat *buf)
187208
{
188-
buf->st_mode = inode.i_mode;
189-
buf->st_nlink = inode.i_nlink;
190-
buf->st_size = inode.i_size;
191-
buf->st_blocks = roundup(inode.i_size, erofs_blksiz(inode.sbi)) >> 9;
192-
buf->st_uid = inode.i_uid;
193-
buf->st_gid = inode.i_gid;
194-
buf->st_ctime = inode.i_mtime;
195-
buf->st_mtime = inode.i_mtime;
196-
buf->st_atime = inode.i_mtime;
209+
buf->st_mode = file_private->inode.i_mode;
210+
buf->st_nlink = file_private->inode.i_nlink;
211+
buf->st_size = file_private->inode.i_size;
212+
buf->st_blocks = roundup(file_private->inode.i_size,
213+
erofs_blksiz(file_private->inode.sbi)) >> 9;
214+
buf->st_uid = file_private->inode.i_uid;
215+
buf->st_gid = file_private->inode.i_gid;
216+
buf->st_ctime = file_private->inode.i_mtime;
217+
buf->st_mtime = file_private->inode.i_mtime;
218+
buf->st_atime = file_private->inode.i_mtime;
197219
return 0;
198220
}
199221

@@ -207,8 +229,8 @@ int ErofsFile::fiemap(struct photon::fs::fiemap *map)
207229
erofs_map.index = UINT_MAX;
208230
erofs_map.m_la = 0;
209231

210-
while (erofs_map.m_la < inode.i_size) {
211-
err = erofs_map_blocks(&inode, &erofs_map, 0);
232+
while (erofs_map.m_la < file_private->inode.i_size) {
233+
err = erofs_map_blocks(&file_private->inode, &erofs_map, 0);
212234
if (err)
213235
LOG_ERROR_RETURN(err, err, "[erofs] Fail to map erofs blocks");
214236
ext_buf[map->fm_mapped_extents].fe_physical = erofs_map.m_pa;
@@ -245,38 +267,49 @@ EROFS_UNIMPLEMENTED_FUNC(int, ErofsFileSystem, syncfs(), -EROFS_UNIMPLEMENTED)
245267

246268
ErofsFileSystem::ErofsFileSystem(photon::fs::IFile *imgfile, uint64_t blksize)
247269
{
248-
target_file.ops.pread = erofs_target_pread;
249-
target_file.ops.pwrite = erofs_target_pwrite;
250-
target_file.ops.pread = erofs_target_pread;
251-
target_file.ops.pwrite = erofs_target_pwrite;
252-
target_file.ops.fsync = erofs_target_fsync;
253-
target_file.ops.fallocate = erofs_target_fallocate;
254-
target_file.ops.ftruncate = erofs_target_ftruncate;
255-
target_file.ops.read = erofs_target_read;
256-
target_file.ops.lseek = erofs_target_lseek;
257-
target_file.file = imgfile;
258-
target_file.cache = new ErofsCache(target_file.file, 128);
259-
260-
memset(&sbi, 0, sizeof(struct erofs_sb_info));
261-
sbi.blkszbits = ilog2(blksize);
262-
sbi.bdev.ops = &target_file.ops;
263-
target_file.file->lseek(0,0);
264-
sbi.devsz = INT64_MAX;
265-
if (erofs_read_superblock(&sbi))
270+
fs_private = new ErofsFileSystemInt;
271+
272+
if (!fs_private)
273+
LOG_ERROR("[erofs] Fail to prepare fs_private: no mem.");
274+
275+
// init target_file
276+
fs_private->target_file.ops.pread = erofs_target_pread;
277+
fs_private->target_file.ops.pwrite = erofs_target_pwrite;
278+
fs_private->target_file.ops.pread = erofs_target_pread;
279+
fs_private->target_file.ops.pwrite = erofs_target_pwrite;
280+
fs_private->target_file.ops.fsync = erofs_target_fsync;
281+
fs_private->target_file.ops.fallocate = erofs_target_fallocate;
282+
fs_private->target_file.ops.ftruncate = erofs_target_ftruncate;
283+
fs_private->target_file.ops.read = erofs_target_read;
284+
fs_private->target_file.ops.lseek = erofs_target_lseek;
285+
fs_private->target_file.file = imgfile;
286+
fs_private->target_file.cache = new ErofsCache(imgfile, 128);
287+
if (!fs_private->target_file.cache)
288+
LOG_ERROR("[erofs] Fail to prepare target_file.cache: no mem.");
289+
290+
// init sbi
291+
memset(&fs_private->sbi, 0, sizeof(struct erofs_sb_info));
292+
fs_private->sbi.blkszbits = ilog2(blksize);
293+
fs_private->sbi.bdev.ops = &fs_private->target_file.ops;
294+
fs_private->target_file.file->lseek(0,0);
295+
fs_private->sbi.devsz = INT64_MAX;
296+
if (erofs_read_superblock(&fs_private->sbi))
266297
LOG_ERROR("[erofs] Fail to read_super_block");
267298
}
268299

269300
ErofsFileSystem::~ErofsFileSystem()
270301
{
271-
delete target_file.cache;
302+
delete fs_private->target_file.cache;
303+
delete fs_private;
304+
fs_private = NULL;
272305
}
273306

274307
int ErofsFileSystem::stat(const char *path, struct stat *buf)
275308
{
276309
struct erofs_inode vi;
277310
int err;
278311

279-
vi.sbi = &sbi;
312+
vi.sbi = &fs_private->sbi;
280313
err = do_erofs_ilookup(path, &vi);
281314
if (err)
282315
LOG_ERRNO_RETURN(err, err, "[erofs] Fail to lookup inode");
@@ -297,8 +330,7 @@ photon::fs::IFile* ErofsFileSystem::open(const char *pathname, int flags)
297330
ErofsFile *file = new ErofsFile(this);
298331
int err;
299332

300-
file->inode.sbi = &sbi;
301-
err = do_erofs_ilookup(pathname, &file->inode);
333+
err = do_erofs_ilookup(pathname, &file->file_private->inode);
302334
if (err) {
303335
delete file;
304336
LOG_ERROR_RETURN(-err, nullptr, "[erofs] Fail to lookup inode by path");
@@ -354,7 +386,7 @@ photon::fs::DIR* ErofsFileSystem::opendir(const char *name)
354386
{
355387
std::vector<::dirent> dirs;
356388

357-
auto ret = do_erofs_readdir(&sbi, name, &dirs);
389+
auto ret = do_erofs_readdir(&fs_private->sbi, name, &dirs);
358390
if (ret) {
359391
errno = -ret;
360392
return nullptr;

src/overlaybd/tar/erofs/erofs_fs.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@
2121
#include <photon/fs/virtual-file.h>
2222
#include <photon/common/alog.h>
2323
#include <vector>
24-
#include "erofs_common.h"
25-
#include "erofs/internal.h"
2624

2725
class ErofsFileSystem: public photon::fs::IFileSystem {
2826
public:
29-
struct erofs_sb_info sbi;
30-
struct liberofs_file target_file;
31-
3227
ErofsFileSystem(photon::fs::IFile *imgfile, uint64_t blksize);
3328
~ErofsFileSystem();
3429
photon::fs::IFile* open(const char *pathname, int flags);
@@ -56,17 +51,26 @@ class ErofsFileSystem: public photon::fs::IFileSystem {
5651
int mknod(const char *path, mode_t mode, dev_t dev);
5752
int syncfs();
5853
photon::fs::DIR* opendir(const char *name);
54+
private:
55+
struct ErofsFileSystemInt;
56+
struct ErofsFileSystemInt *fs_private;
57+
58+
friend class ErofsFile;
5959
};
6060

6161
class ErofsFile: public photon::fs::VirtualReadOnlyFile {
6262
public:
63-
ErofsFileSystem *fs;
64-
struct erofs_inode inode;
65-
6663
ErofsFile(ErofsFileSystem *fs);
64+
~ErofsFile();
6765
photon::fs::IFileSystem *filesystem();
6866
int fstat(struct stat *buf);
6967
int fiemap(struct photon::fs::fiemap *map);
68+
private:
69+
ErofsFileSystem *fs;
70+
struct ErofsFileInt;
71+
ErofsFileInt *file_private;
72+
73+
friend class ErofsFileSystem;
7074
};
7175

7276
class ErofsDir: public photon::fs::DIR {

src/tools/comm_func.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../overlaybd/registryfs/registryfs.h"
2424
#include "../image_service.h"
2525
#include "../image_file.h"
26+
#include "../overlaybd/tar/erofs/erofs_fs.h"
2627

2728

2829
using namespace std;
@@ -80,9 +81,6 @@ photon::fs::IFileSystem *create_ext4fs(photon::fs::IFile *imgfile, bool mkfs,
8081
return target;
8182
}
8283

83-
extern bool erofs_check_fs(const photon::fs::IFile *imgfile);
84-
extern photon::fs::IFileSystem *erofs_create_fs(photon::fs::IFile *imgfile, uint64_t blksz);
85-
8684
bool is_erofs_fs(const photon::fs::IFile *imgfile)
8785
{
8886
if (imgfile == nullptr)

0 commit comments

Comments
 (0)