1*33b1fccfSAndroid Build Coastguard Worker /* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
2*33b1fccfSAndroid Build Coastguard Worker /*
3*33b1fccfSAndroid Build Coastguard Worker * Copyright (C) 2018 HUAWEI, Inc.
4*33b1fccfSAndroid Build Coastguard Worker * http://www.huawei.com/
5*33b1fccfSAndroid Build Coastguard Worker * Created by Miao Xie <[email protected]>
6*33b1fccfSAndroid Build Coastguard Worker * with heavy changes by Gao Xiang <[email protected]>
7*33b1fccfSAndroid Build Coastguard Worker */
8*33b1fccfSAndroid Build Coastguard Worker #ifndef __EROFS_CACHE_H
9*33b1fccfSAndroid Build Coastguard Worker #define __EROFS_CACHE_H
10*33b1fccfSAndroid Build Coastguard Worker
11*33b1fccfSAndroid Build Coastguard Worker #ifdef __cplusplus
12*33b1fccfSAndroid Build Coastguard Worker extern "C"
13*33b1fccfSAndroid Build Coastguard Worker {
14*33b1fccfSAndroid Build Coastguard Worker #endif
15*33b1fccfSAndroid Build Coastguard Worker
16*33b1fccfSAndroid Build Coastguard Worker #include "internal.h"
17*33b1fccfSAndroid Build Coastguard Worker
18*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_head;
19*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block;
20*33b1fccfSAndroid Build Coastguard Worker
21*33b1fccfSAndroid Build Coastguard Worker #define DATA 0
22*33b1fccfSAndroid Build Coastguard Worker #define META 1
23*33b1fccfSAndroid Build Coastguard Worker /* including inline xattrs, extent */
24*33b1fccfSAndroid Build Coastguard Worker #define INODE 2
25*33b1fccfSAndroid Build Coastguard Worker /* directory data */
26*33b1fccfSAndroid Build Coastguard Worker #define DIRA 3
27*33b1fccfSAndroid Build Coastguard Worker /* shared xattrs */
28*33b1fccfSAndroid Build Coastguard Worker #define XATTR 4
29*33b1fccfSAndroid Build Coastguard Worker /* device table */
30*33b1fccfSAndroid Build Coastguard Worker #define DEVT 5
31*33b1fccfSAndroid Build Coastguard Worker
32*33b1fccfSAndroid Build Coastguard Worker struct erofs_bhops {
33*33b1fccfSAndroid Build Coastguard Worker int (*flush)(struct erofs_buffer_head *bh);
34*33b1fccfSAndroid Build Coastguard Worker };
35*33b1fccfSAndroid Build Coastguard Worker
36*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_head {
37*33b1fccfSAndroid Build Coastguard Worker struct list_head list;
38*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block *block;
39*33b1fccfSAndroid Build Coastguard Worker
40*33b1fccfSAndroid Build Coastguard Worker erofs_off_t off;
41*33b1fccfSAndroid Build Coastguard Worker const struct erofs_bhops *op;
42*33b1fccfSAndroid Build Coastguard Worker
43*33b1fccfSAndroid Build Coastguard Worker void *fsprivate;
44*33b1fccfSAndroid Build Coastguard Worker };
45*33b1fccfSAndroid Build Coastguard Worker
46*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block {
47*33b1fccfSAndroid Build Coastguard Worker struct list_head list;
48*33b1fccfSAndroid Build Coastguard Worker struct list_head mapped_list;
49*33b1fccfSAndroid Build Coastguard Worker
50*33b1fccfSAndroid Build Coastguard Worker erofs_blk_t blkaddr;
51*33b1fccfSAndroid Build Coastguard Worker int type;
52*33b1fccfSAndroid Build Coastguard Worker
53*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_head buffers;
54*33b1fccfSAndroid Build Coastguard Worker };
55*33b1fccfSAndroid Build Coastguard Worker
56*33b1fccfSAndroid Build Coastguard Worker struct erofs_bufmgr {
57*33b1fccfSAndroid Build Coastguard Worker struct erofs_sb_info *sbi;
58*33b1fccfSAndroid Build Coastguard Worker
59*33b1fccfSAndroid Build Coastguard Worker /* buckets for all mapped buffer blocks to boost up allocation */
60*33b1fccfSAndroid Build Coastguard Worker struct list_head mapped_buckets[META + 1][EROFS_MAX_BLOCK_SIZE];
61*33b1fccfSAndroid Build Coastguard Worker
62*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block blkh;
63*33b1fccfSAndroid Build Coastguard Worker erofs_blk_t tail_blkaddr, metablkcnt;
64*33b1fccfSAndroid Build Coastguard Worker
65*33b1fccfSAndroid Build Coastguard Worker /* last mapped buffer block to accelerate erofs_mapbh() */
66*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block *last_mapped_block;
67*33b1fccfSAndroid Build Coastguard Worker };
68*33b1fccfSAndroid Build Coastguard Worker
get_alignsize(struct erofs_sb_info * sbi,int type,int * type_ret)69*33b1fccfSAndroid Build Coastguard Worker static inline const int get_alignsize(struct erofs_sb_info *sbi, int type,
70*33b1fccfSAndroid Build Coastguard Worker int *type_ret)
71*33b1fccfSAndroid Build Coastguard Worker {
72*33b1fccfSAndroid Build Coastguard Worker if (type == DATA)
73*33b1fccfSAndroid Build Coastguard Worker return erofs_blksiz(sbi);
74*33b1fccfSAndroid Build Coastguard Worker
75*33b1fccfSAndroid Build Coastguard Worker if (type == INODE) {
76*33b1fccfSAndroid Build Coastguard Worker *type_ret = META;
77*33b1fccfSAndroid Build Coastguard Worker return sizeof(struct erofs_inode_compact);
78*33b1fccfSAndroid Build Coastguard Worker } else if (type == DIRA) {
79*33b1fccfSAndroid Build Coastguard Worker *type_ret = META;
80*33b1fccfSAndroid Build Coastguard Worker return erofs_blksiz(sbi);
81*33b1fccfSAndroid Build Coastguard Worker } else if (type == XATTR) {
82*33b1fccfSAndroid Build Coastguard Worker *type_ret = META;
83*33b1fccfSAndroid Build Coastguard Worker return sizeof(struct erofs_xattr_entry);
84*33b1fccfSAndroid Build Coastguard Worker } else if (type == DEVT) {
85*33b1fccfSAndroid Build Coastguard Worker *type_ret = META;
86*33b1fccfSAndroid Build Coastguard Worker return EROFS_DEVT_SLOT_SIZE;
87*33b1fccfSAndroid Build Coastguard Worker }
88*33b1fccfSAndroid Build Coastguard Worker
89*33b1fccfSAndroid Build Coastguard Worker if (type == META)
90*33b1fccfSAndroid Build Coastguard Worker return 1;
91*33b1fccfSAndroid Build Coastguard Worker return -EINVAL;
92*33b1fccfSAndroid Build Coastguard Worker }
93*33b1fccfSAndroid Build Coastguard Worker
94*33b1fccfSAndroid Build Coastguard Worker extern const struct erofs_bhops erofs_drop_directly_bhops;
95*33b1fccfSAndroid Build Coastguard Worker extern const struct erofs_bhops erofs_skip_write_bhops;
96*33b1fccfSAndroid Build Coastguard Worker
erofs_btell(struct erofs_buffer_head * bh,bool end)97*33b1fccfSAndroid Build Coastguard Worker static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
98*33b1fccfSAndroid Build Coastguard Worker {
99*33b1fccfSAndroid Build Coastguard Worker const struct erofs_buffer_block *bb = bh->block;
100*33b1fccfSAndroid Build Coastguard Worker struct erofs_bufmgr *bmgr =
101*33b1fccfSAndroid Build Coastguard Worker (struct erofs_bufmgr *)bb->buffers.fsprivate;
102*33b1fccfSAndroid Build Coastguard Worker
103*33b1fccfSAndroid Build Coastguard Worker if (bb->blkaddr == NULL_ADDR)
104*33b1fccfSAndroid Build Coastguard Worker return NULL_ADDR_UL;
105*33b1fccfSAndroid Build Coastguard Worker
106*33b1fccfSAndroid Build Coastguard Worker return erofs_pos(bmgr->sbi, bb->blkaddr) +
107*33b1fccfSAndroid Build Coastguard Worker (end ? list_next_entry(bh, list)->off : bh->off);
108*33b1fccfSAndroid Build Coastguard Worker }
109*33b1fccfSAndroid Build Coastguard Worker
erofs_bh_flush_generic_end(struct erofs_buffer_head * bh)110*33b1fccfSAndroid Build Coastguard Worker static inline int erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
111*33b1fccfSAndroid Build Coastguard Worker {
112*33b1fccfSAndroid Build Coastguard Worker list_del(&bh->list);
113*33b1fccfSAndroid Build Coastguard Worker free(bh);
114*33b1fccfSAndroid Build Coastguard Worker return 0;
115*33b1fccfSAndroid Build Coastguard Worker }
116*33b1fccfSAndroid Build Coastguard Worker
117*33b1fccfSAndroid Build Coastguard Worker struct erofs_bufmgr *erofs_buffer_init(struct erofs_sb_info *sbi,
118*33b1fccfSAndroid Build Coastguard Worker erofs_blk_t startblk);
119*33b1fccfSAndroid Build Coastguard Worker int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
120*33b1fccfSAndroid Build Coastguard Worker
121*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_head *erofs_balloc(struct erofs_bufmgr *bmgr,
122*33b1fccfSAndroid Build Coastguard Worker int type, erofs_off_t size,
123*33b1fccfSAndroid Build Coastguard Worker unsigned int required_ext,
124*33b1fccfSAndroid Build Coastguard Worker unsigned int inline_ext);
125*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
126*33b1fccfSAndroid Build Coastguard Worker int type, unsigned int size);
127*33b1fccfSAndroid Build Coastguard Worker
128*33b1fccfSAndroid Build Coastguard Worker erofs_blk_t erofs_mapbh(struct erofs_bufmgr *bmgr,
129*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block *bb);
130*33b1fccfSAndroid Build Coastguard Worker int erofs_bflush(struct erofs_bufmgr *bmgr,
131*33b1fccfSAndroid Build Coastguard Worker struct erofs_buffer_block *bb);
132*33b1fccfSAndroid Build Coastguard Worker
133*33b1fccfSAndroid Build Coastguard Worker void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
134*33b1fccfSAndroid Build Coastguard Worker erofs_blk_t erofs_total_metablocks(struct erofs_bufmgr *bmgr);
135*33b1fccfSAndroid Build Coastguard Worker void erofs_buffer_exit(struct erofs_bufmgr *bmgr);
136*33b1fccfSAndroid Build Coastguard Worker
137*33b1fccfSAndroid Build Coastguard Worker #ifdef __cplusplus
138*33b1fccfSAndroid Build Coastguard Worker }
139*33b1fccfSAndroid Build Coastguard Worker #endif
140*33b1fccfSAndroid Build Coastguard Worker
141*33b1fccfSAndroid Build Coastguard Worker #endif
142