1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * erofs-utils/include/erofs/cache.h
4 *
5 * Copyright (C) 2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Miao Xie <miaoxie@huawei.com>
8 * with heavy changes by Gao Xiang <gaoxiang25@huawei.com>
9 */
10 #ifndef __EROFS_CACHE_H
11 #define __EROFS_CACHE_H
12
13 #include "internal.h"
14
15 struct erofs_buffer_head;
16 struct erofs_buffer_block;
17
18 #define DATA 0
19 #define META 1
20 /* including inline xattrs, extent */
21 #define INODE 2
22 /* shared xattrs */
23 #define XATTR 3
24
25 struct erofs_bhops {
26 bool (*preflush)(struct erofs_buffer_head *bh);
27 bool (*flush)(struct erofs_buffer_head *bh);
28 };
29
30 struct erofs_buffer_head {
31 struct list_head list;
32 struct erofs_buffer_block *block;
33
34 erofs_off_t off;
35 struct erofs_bhops *op;
36
37 void *fsprivate;
38 };
39
40 struct erofs_buffer_block {
41 struct list_head list;
42
43 erofs_blk_t blkaddr;
44 int type;
45
46 struct erofs_buffer_head buffers;
47 };
48
get_alignsize(int type,int * type_ret)49 static inline const int get_alignsize(int type, int *type_ret)
50 {
51 if (type == DATA)
52 return EROFS_BLKSIZ;
53
54 if (type == INODE) {
55 *type_ret = META;
56 return sizeof(struct erofs_inode_compact);
57 } else if (type == XATTR) {
58 *type_ret = META;
59 return sizeof(struct erofs_xattr_entry);
60 }
61
62 if (type == META)
63 return 1;
64 return -EINVAL;
65 }
66
67 extern struct erofs_bhops erofs_drop_directly_bhops;
68 extern struct erofs_bhops erofs_skip_write_bhops;
69 extern struct erofs_bhops erofs_buf_write_bhops;
70
erofs_btell(struct erofs_buffer_head * bh,bool end)71 static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
72 {
73 const struct erofs_buffer_block *bb = bh->block;
74
75 if (bb->blkaddr == NULL_ADDR)
76 return NULL_ADDR_UL;
77
78 return blknr_to_addr(bb->blkaddr) +
79 (end ? list_next_entry(bh, list)->off : bh->off);
80 }
81
erofs_bh_flush_generic_end(struct erofs_buffer_head * bh)82 static inline bool erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
83 {
84 list_del(&bh->list);
85 free(bh);
86 return true;
87 }
88
89 struct erofs_buffer_head *erofs_buffer_init(void);
90 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
91
92 struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
93 unsigned int required_ext,
94 unsigned int inline_ext);
95 struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
96 int type, unsigned int size);
97
98 erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb, bool end);
99 bool erofs_bflush(struct erofs_buffer_block *bb);
100
101 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
102
103 #endif
104
105