xref: /OK3568_Linux_fs/kernel/fs/incfs/data_mgmt.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2019 Google LLC
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #ifndef _INCFS_DATA_MGMT_H
6*4882a593Smuzhiyun #define _INCFS_DATA_MGMT_H
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/cred.h>
9*4882a593Smuzhiyun #include <linux/fs.h>
10*4882a593Smuzhiyun #include <linux/types.h>
11*4882a593Smuzhiyun #include <linux/mutex.h>
12*4882a593Smuzhiyun #include <linux/spinlock.h>
13*4882a593Smuzhiyun #include <linux/rcupdate.h>
14*4882a593Smuzhiyun #include <linux/completion.h>
15*4882a593Smuzhiyun #include <linux/wait.h>
16*4882a593Smuzhiyun #include <linux/zstd.h>
17*4882a593Smuzhiyun #include <crypto/hash.h>
18*4882a593Smuzhiyun #include <linux/rwsem.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <uapi/linux/incrementalfs.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "internal.h"
23*4882a593Smuzhiyun #include "pseudo_files.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define SEGMENTS_PER_FILE 3
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun enum LOG_RECORD_TYPE {
28*4882a593Smuzhiyun 	FULL,
29*4882a593Smuzhiyun 	SAME_FILE,
30*4882a593Smuzhiyun 	SAME_FILE_CLOSE_BLOCK,
31*4882a593Smuzhiyun 	SAME_FILE_CLOSE_BLOCK_SHORT,
32*4882a593Smuzhiyun 	SAME_FILE_NEXT_BLOCK,
33*4882a593Smuzhiyun 	SAME_FILE_NEXT_BLOCK_SHORT,
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun struct full_record {
37*4882a593Smuzhiyun 	enum LOG_RECORD_TYPE type : 3; /* FULL */
38*4882a593Smuzhiyun 	u32 block_index : 29;
39*4882a593Smuzhiyun 	incfs_uuid_t file_id;
40*4882a593Smuzhiyun 	u64 absolute_ts_us;
41*4882a593Smuzhiyun 	uid_t uid;
42*4882a593Smuzhiyun } __packed; /* 32 bytes */
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct same_file {
45*4882a593Smuzhiyun 	enum LOG_RECORD_TYPE type : 3; /* SAME_FILE */
46*4882a593Smuzhiyun 	u32 block_index : 29;
47*4882a593Smuzhiyun 	uid_t uid;
48*4882a593Smuzhiyun 	u16 relative_ts_us; /* max 2^16 us ~= 64 ms */
49*4882a593Smuzhiyun } __packed; /* 10 bytes */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun struct same_file_close_block {
52*4882a593Smuzhiyun 	enum LOG_RECORD_TYPE type : 3; /* SAME_FILE_CLOSE_BLOCK */
53*4882a593Smuzhiyun 	u16 relative_ts_us : 13; /* max 2^13 us ~= 8 ms */
54*4882a593Smuzhiyun 	s16 block_index_delta;
55*4882a593Smuzhiyun } __packed; /* 4 bytes */
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun struct same_file_close_block_short {
58*4882a593Smuzhiyun 	enum LOG_RECORD_TYPE type : 3; /* SAME_FILE_CLOSE_BLOCK_SHORT */
59*4882a593Smuzhiyun 	u8 relative_ts_tens_us : 5; /* max 2^5*10 us ~= 320 us */
60*4882a593Smuzhiyun 	s8 block_index_delta;
61*4882a593Smuzhiyun } __packed; /* 2 bytes */
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun struct same_file_next_block {
64*4882a593Smuzhiyun 	enum LOG_RECORD_TYPE type : 3; /* SAME_FILE_NEXT_BLOCK */
65*4882a593Smuzhiyun 	u16 relative_ts_us : 13; /* max 2^13 us ~= 8 ms */
66*4882a593Smuzhiyun } __packed; /* 2 bytes */
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun struct same_file_next_block_short {
69*4882a593Smuzhiyun 	enum LOG_RECORD_TYPE type : 3; /* SAME_FILE_NEXT_BLOCK_SHORT */
70*4882a593Smuzhiyun 	u8 relative_ts_tens_us : 5; /* max 2^5*10 us ~= 320 us */
71*4882a593Smuzhiyun } __packed; /* 1 byte */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun union log_record {
74*4882a593Smuzhiyun 	struct full_record full_record;
75*4882a593Smuzhiyun 	struct same_file same_file;
76*4882a593Smuzhiyun 	struct same_file_close_block same_file_close_block;
77*4882a593Smuzhiyun 	struct same_file_close_block_short same_file_close_block_short;
78*4882a593Smuzhiyun 	struct same_file_next_block same_file_next_block;
79*4882a593Smuzhiyun 	struct same_file_next_block_short same_file_next_block_short;
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct read_log_state {
83*4882a593Smuzhiyun 	/* Log buffer generation id, incremented on configuration changes */
84*4882a593Smuzhiyun 	u32 generation_id;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/* Offset in rl_ring_buf to write into. */
87*4882a593Smuzhiyun 	u32 next_offset;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* Current number of writer passes over rl_ring_buf */
90*4882a593Smuzhiyun 	u32 current_pass_no;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* Current full_record to diff against */
93*4882a593Smuzhiyun 	struct full_record base_record;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* Current record number counting from configuration change */
96*4882a593Smuzhiyun 	u64 current_record_no;
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* A ring buffer to save records about data blocks which were recently read. */
100*4882a593Smuzhiyun struct read_log {
101*4882a593Smuzhiyun 	void *rl_ring_buf;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	int rl_size;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	struct read_log_state rl_head;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	struct read_log_state rl_tail;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* A lock to protect the above fields */
110*4882a593Smuzhiyun 	spinlock_t rl_lock;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* A queue of waiters who want to be notified about reads */
113*4882a593Smuzhiyun 	wait_queue_head_t ml_notif_wq;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/* A work item to wake up those waiters without slowing down readers */
116*4882a593Smuzhiyun 	struct delayed_work ml_wakeup_work;
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun struct mount_options {
120*4882a593Smuzhiyun 	unsigned int read_timeout_ms;
121*4882a593Smuzhiyun 	unsigned int readahead_pages;
122*4882a593Smuzhiyun 	unsigned int read_log_pages;
123*4882a593Smuzhiyun 	unsigned int read_log_wakeup_count;
124*4882a593Smuzhiyun 	bool report_uid;
125*4882a593Smuzhiyun 	char *sysfs_name;
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun struct mount_info {
129*4882a593Smuzhiyun 	struct super_block *mi_sb;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	struct path mi_backing_dir_path;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	struct dentry *mi_index_dir;
134*4882a593Smuzhiyun 	/* For stacking mounts, if true, this indicates if the index dir needs
135*4882a593Smuzhiyun 	 * to be freed for this SB otherwise it was created by lower level SB */
136*4882a593Smuzhiyun 	bool mi_index_free;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	struct dentry *mi_incomplete_dir;
139*4882a593Smuzhiyun 	/* For stacking mounts, if true, this indicates if the incomplete dir
140*4882a593Smuzhiyun 	 * needs to be freed for this SB. Similar to mi_index_free */
141*4882a593Smuzhiyun 	bool mi_incomplete_free;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	const struct cred *mi_owner;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	struct mount_options mi_options;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* This mutex is to be taken before create, rename, delete */
148*4882a593Smuzhiyun 	struct mutex mi_dir_struct_mutex;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/*
151*4882a593Smuzhiyun 	 * A queue of waiters who want to be notified about new pending reads.
152*4882a593Smuzhiyun 	 */
153*4882a593Smuzhiyun 	wait_queue_head_t mi_pending_reads_notif_wq;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/*
156*4882a593Smuzhiyun 	 * Protects - RCU safe:
157*4882a593Smuzhiyun 	 *  - reads_list_head
158*4882a593Smuzhiyun 	 *  - mi_pending_reads_count
159*4882a593Smuzhiyun 	 *  - mi_last_pending_read_number
160*4882a593Smuzhiyun 	 *  - data_file_segment.reads_list_head
161*4882a593Smuzhiyun 	 */
162*4882a593Smuzhiyun 	spinlock_t pending_read_lock;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* List of active pending_read objects */
165*4882a593Smuzhiyun 	struct list_head mi_reads_list_head;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* Total number of items in reads_list_head */
168*4882a593Smuzhiyun 	int mi_pending_reads_count;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	/*
171*4882a593Smuzhiyun 	 * Last serial number that was assigned to a pending read.
172*4882a593Smuzhiyun 	 * 0 means no pending reads have been seen yet.
173*4882a593Smuzhiyun 	 */
174*4882a593Smuzhiyun 	int mi_last_pending_read_number;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* Temporary buffer for read logger. */
177*4882a593Smuzhiyun 	struct read_log mi_log;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* SELinux needs special xattrs on our pseudo files */
180*4882a593Smuzhiyun 	struct mem_range pseudo_file_xattr[PSEUDO_FILE_COUNT];
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* A queue of waiters who want to be notified about blocks_written */
183*4882a593Smuzhiyun 	wait_queue_head_t mi_blocks_written_notif_wq;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	/* Number of blocks written since mount */
186*4882a593Smuzhiyun 	atomic_t mi_blocks_written;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* Per UID read timeouts */
189*4882a593Smuzhiyun 	spinlock_t mi_per_uid_read_timeouts_lock;
190*4882a593Smuzhiyun 	struct incfs_per_uid_read_timeouts *mi_per_uid_read_timeouts;
191*4882a593Smuzhiyun 	int mi_per_uid_read_timeouts_size;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/* zstd workspace */
194*4882a593Smuzhiyun 	struct mutex mi_zstd_workspace_mutex;
195*4882a593Smuzhiyun 	void *mi_zstd_workspace;
196*4882a593Smuzhiyun 	ZSTD_DStream *mi_zstd_stream;
197*4882a593Smuzhiyun 	struct delayed_work mi_zstd_cleanup_work;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	/* sysfs node */
200*4882a593Smuzhiyun 	struct incfs_sysfs_node *mi_sysfs_node;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* Last error information */
203*4882a593Smuzhiyun 	struct mutex	mi_le_mutex;
204*4882a593Smuzhiyun 	incfs_uuid_t	mi_le_file_id;
205*4882a593Smuzhiyun 	u64		mi_le_time_us;
206*4882a593Smuzhiyun 	u32		mi_le_page;
207*4882a593Smuzhiyun 	u32		mi_le_errno;
208*4882a593Smuzhiyun 	uid_t		mi_le_uid;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* Number of reads timed out */
211*4882a593Smuzhiyun 	u32 mi_reads_failed_timed_out;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* Number of reads failed because hash verification failed */
214*4882a593Smuzhiyun 	u32 mi_reads_failed_hash_verification;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/* Number of reads failed for another reason */
217*4882a593Smuzhiyun 	u32 mi_reads_failed_other;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	/* Number of reads delayed because page had to be fetched */
220*4882a593Smuzhiyun 	u32 mi_reads_delayed_pending;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* Total time waiting for pages to be fetched */
223*4882a593Smuzhiyun 	u64 mi_reads_delayed_pending_us;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/*
226*4882a593Smuzhiyun 	 * Number of reads delayed because of per-uid min_time_us or
227*4882a593Smuzhiyun 	 * min_pending_time_us settings
228*4882a593Smuzhiyun 	 */
229*4882a593Smuzhiyun 	u32 mi_reads_delayed_min;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* Total time waiting because of per-uid min_time_us or
232*4882a593Smuzhiyun 	 * min_pending_time_us settings.
233*4882a593Smuzhiyun 	 *
234*4882a593Smuzhiyun 	 * Note that if a read is initially delayed because we have to wait for
235*4882a593Smuzhiyun 	 * the page, then further delayed because of min_pending_time_us
236*4882a593Smuzhiyun 	 * setting, this counter gets incremented by only the further delay
237*4882a593Smuzhiyun 	 * time.
238*4882a593Smuzhiyun 	 */
239*4882a593Smuzhiyun 	u64 mi_reads_delayed_min_us;
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun struct data_file_block {
243*4882a593Smuzhiyun 	loff_t db_backing_file_data_offset;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	size_t db_stored_size;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	enum incfs_compression_alg db_comp_alg;
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun struct pending_read {
251*4882a593Smuzhiyun 	incfs_uuid_t file_id;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	s64 timestamp_us;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	atomic_t done;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	int block_index;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	int serial_number;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	uid_t uid;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	struct list_head mi_reads_list;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	struct list_head segment_reads_list;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	struct rcu_head rcu;
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun struct data_file_segment {
271*4882a593Smuzhiyun 	wait_queue_head_t new_data_arrival_wq;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	/* Protects reads and writes from the blockmap */
274*4882a593Smuzhiyun 	struct rw_semaphore rwsem;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/* List of active pending_read objects belonging to this segment */
277*4882a593Smuzhiyun 	/* Protected by mount_info.pending_reads_mutex */
278*4882a593Smuzhiyun 	struct list_head reads_list_head;
279*4882a593Smuzhiyun };
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun /*
282*4882a593Smuzhiyun  * Extra info associated with a file. Just a few bytes set by a user.
283*4882a593Smuzhiyun  */
284*4882a593Smuzhiyun struct file_attr {
285*4882a593Smuzhiyun 	loff_t fa_value_offset;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	size_t fa_value_size;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	u32 fa_crc;
290*4882a593Smuzhiyun };
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun struct data_file {
294*4882a593Smuzhiyun 	struct backing_file_context *df_backing_file_context;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	struct mount_info *df_mount_info;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	incfs_uuid_t df_id;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/*
301*4882a593Smuzhiyun 	 * Array of segments used to reduce lock contention for the file.
302*4882a593Smuzhiyun 	 * Segment is chosen for a block depends on the block's index.
303*4882a593Smuzhiyun 	 */
304*4882a593Smuzhiyun 	struct data_file_segment df_segments[SEGMENTS_PER_FILE];
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	/* Base offset of the first metadata record. */
307*4882a593Smuzhiyun 	loff_t df_metadata_off;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/* Base offset of the block map. */
310*4882a593Smuzhiyun 	loff_t df_blockmap_off;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	/* File size in bytes */
313*4882a593Smuzhiyun 	loff_t df_size;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	/* File header flags */
316*4882a593Smuzhiyun 	u32 df_header_flags;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* File size in DATA_FILE_BLOCK_SIZE blocks */
319*4882a593Smuzhiyun 	int df_data_block_count;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	/* Total number of blocks, data + hash */
322*4882a593Smuzhiyun 	int df_total_block_count;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* For mapped files, the offset into the actual file */
325*4882a593Smuzhiyun 	loff_t df_mapped_offset;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/* Number of data blocks written to file */
328*4882a593Smuzhiyun 	atomic_t df_data_blocks_written;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/* Number of data blocks in the status block */
331*4882a593Smuzhiyun 	u32 df_initial_data_blocks_written;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* Number of hash blocks written to file */
334*4882a593Smuzhiyun 	atomic_t df_hash_blocks_written;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	/* Number of hash blocks in the status block */
337*4882a593Smuzhiyun 	u32 df_initial_hash_blocks_written;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* Offset to status metadata header */
340*4882a593Smuzhiyun 	loff_t df_status_offset;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	/*
343*4882a593Smuzhiyun 	 * Mutex acquired while enabling verity. Note that df_hash_tree is set
344*4882a593Smuzhiyun 	 * by enable verity.
345*4882a593Smuzhiyun 	 *
346*4882a593Smuzhiyun 	 * The backing file mutex bc_mutex  may be taken while this mutex is
347*4882a593Smuzhiyun 	 * held.
348*4882a593Smuzhiyun 	 */
349*4882a593Smuzhiyun 	struct mutex df_enable_verity;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/*
352*4882a593Smuzhiyun 	 * Set either at construction time or during enabling verity. In the
353*4882a593Smuzhiyun 	 * latter case, set via smp_store_release, so use smp_load_acquire to
354*4882a593Smuzhiyun 	 * read it.
355*4882a593Smuzhiyun 	 */
356*4882a593Smuzhiyun 	struct mtree *df_hash_tree;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/* Guaranteed set if df_hash_tree is set. */
359*4882a593Smuzhiyun 	struct incfs_df_signature *df_signature;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	/*
362*4882a593Smuzhiyun 	 * The verity file digest, set when verity is enabled and the file has
363*4882a593Smuzhiyun 	 * been opened
364*4882a593Smuzhiyun 	 */
365*4882a593Smuzhiyun 	struct mem_range df_verity_file_digest;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	struct incfs_df_verity_signature *df_verity_signature;
368*4882a593Smuzhiyun };
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun struct dir_file {
371*4882a593Smuzhiyun 	struct mount_info *mount_info;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	struct file *backing_dir;
374*4882a593Smuzhiyun };
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun struct inode_info {
377*4882a593Smuzhiyun 	struct mount_info *n_mount_info; /* A mount, this file belongs to */
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	struct inode *n_backing_inode;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	struct data_file *n_file;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	struct inode n_vfs_inode;
384*4882a593Smuzhiyun };
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun struct dentry_info {
387*4882a593Smuzhiyun 	struct path backing_path;
388*4882a593Smuzhiyun };
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun enum FILL_PERMISSION {
391*4882a593Smuzhiyun 	CANT_FILL = 0,
392*4882a593Smuzhiyun 	CAN_FILL = 1,
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun struct incfs_file_data {
396*4882a593Smuzhiyun 	/* Does this file handle have INCFS_IOC_FILL_BLOCKS permission */
397*4882a593Smuzhiyun 	enum FILL_PERMISSION fd_fill_permission;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* If INCFS_IOC_GET_FILLED_BLOCKS has been called, where are we */
400*4882a593Smuzhiyun 	int fd_get_block_pos;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	/* And how many filled blocks are there up to that point */
403*4882a593Smuzhiyun 	int fd_filled_data_blocks;
404*4882a593Smuzhiyun 	int fd_filled_hash_blocks;
405*4882a593Smuzhiyun };
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun struct mount_info *incfs_alloc_mount_info(struct super_block *sb,
408*4882a593Smuzhiyun 					  struct mount_options *options,
409*4882a593Smuzhiyun 					  struct path *backing_dir_path);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun int incfs_realloc_mount_info(struct mount_info *mi,
412*4882a593Smuzhiyun 			     struct mount_options *options);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun void incfs_free_mount_info(struct mount_info *mi);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun char *file_id_to_str(incfs_uuid_t id);
417*4882a593Smuzhiyun struct dentry *incfs_lookup_dentry(struct dentry *parent, const char *name);
418*4882a593Smuzhiyun struct data_file *incfs_open_data_file(struct mount_info *mi, struct file *bf);
419*4882a593Smuzhiyun void incfs_free_data_file(struct data_file *df);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun struct dir_file *incfs_open_dir_file(struct mount_info *mi, struct file *bf);
422*4882a593Smuzhiyun void incfs_free_dir_file(struct dir_file *dir);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun struct incfs_read_data_file_timeouts {
425*4882a593Smuzhiyun 	u32 min_time_us;
426*4882a593Smuzhiyun 	u32 min_pending_time_us;
427*4882a593Smuzhiyun 	u32 max_pending_time_us;
428*4882a593Smuzhiyun };
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun ssize_t incfs_read_data_file_block(struct mem_range dst, struct file *f,
431*4882a593Smuzhiyun 			int index, struct mem_range tmp,
432*4882a593Smuzhiyun 			struct incfs_read_data_file_timeouts *timeouts);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun ssize_t incfs_read_merkle_tree_blocks(struct mem_range dst,
435*4882a593Smuzhiyun 				      struct data_file *df, size_t offset);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun int incfs_get_filled_blocks(struct data_file *df,
438*4882a593Smuzhiyun 			    struct incfs_file_data *fd,
439*4882a593Smuzhiyun 			    struct incfs_get_filled_blocks_args *arg);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun int incfs_read_file_signature(struct data_file *df, struct mem_range dst);
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun int incfs_process_new_data_block(struct data_file *df,
444*4882a593Smuzhiyun 				 struct incfs_fill_block *block, u8 *data);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun int incfs_process_new_hash_block(struct data_file *df,
447*4882a593Smuzhiyun 				 struct incfs_fill_block *block, u8 *data);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun bool incfs_fresh_pending_reads_exist(struct mount_info *mi, int last_number);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /*
452*4882a593Smuzhiyun  * Collects pending reads and saves them into the array (reads/reads_size).
453*4882a593Smuzhiyun  * Only reads with serial_number > sn_lowerbound are reported.
454*4882a593Smuzhiyun  * Returns how many reads were saved into the array.
455*4882a593Smuzhiyun  */
456*4882a593Smuzhiyun int incfs_collect_pending_reads(struct mount_info *mi, int sn_lowerbound,
457*4882a593Smuzhiyun 				struct incfs_pending_read_info *reads,
458*4882a593Smuzhiyun 				struct incfs_pending_read_info2 *reads2,
459*4882a593Smuzhiyun 				int reads_size, int *new_max_sn);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun int incfs_collect_logged_reads(struct mount_info *mi,
462*4882a593Smuzhiyun 			       struct read_log_state *start_state,
463*4882a593Smuzhiyun 			       struct incfs_pending_read_info *reads,
464*4882a593Smuzhiyun 			       struct incfs_pending_read_info2 *reads2,
465*4882a593Smuzhiyun 			       int reads_size);
466*4882a593Smuzhiyun struct read_log_state incfs_get_log_state(struct mount_info *mi);
467*4882a593Smuzhiyun int incfs_get_uncollected_logs_count(struct mount_info *mi,
468*4882a593Smuzhiyun 				     const struct read_log_state *state);
469*4882a593Smuzhiyun 
get_incfs_node(struct inode * inode)470*4882a593Smuzhiyun static inline struct inode_info *get_incfs_node(struct inode *inode)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	if (!inode)
473*4882a593Smuzhiyun 		return NULL;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	if (inode->i_sb->s_magic != INCFS_MAGIC_NUMBER) {
476*4882a593Smuzhiyun 		/* This inode doesn't belong to us. */
477*4882a593Smuzhiyun 		pr_warn_once("incfs: %s on an alien inode.", __func__);
478*4882a593Smuzhiyun 		return NULL;
479*4882a593Smuzhiyun 	}
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	return container_of(inode, struct inode_info, n_vfs_inode);
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
get_incfs_data_file(struct file * f)484*4882a593Smuzhiyun static inline struct data_file *get_incfs_data_file(struct file *f)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun 	struct inode_info *node = NULL;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	if (!f)
489*4882a593Smuzhiyun 		return NULL;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	if (!S_ISREG(f->f_inode->i_mode))
492*4882a593Smuzhiyun 		return NULL;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	node = get_incfs_node(f->f_inode);
495*4882a593Smuzhiyun 	if (!node)
496*4882a593Smuzhiyun 		return NULL;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	return node->n_file;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
get_incfs_dir_file(struct file * f)501*4882a593Smuzhiyun static inline struct dir_file *get_incfs_dir_file(struct file *f)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun 	if (!f)
504*4882a593Smuzhiyun 		return NULL;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (!S_ISDIR(f->f_inode->i_mode))
507*4882a593Smuzhiyun 		return NULL;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	return (struct dir_file *)f->private_data;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun /*
513*4882a593Smuzhiyun  * Make sure that inode_info.n_file is initialized and inode can be used
514*4882a593Smuzhiyun  * for reading and writing data from/to the backing file.
515*4882a593Smuzhiyun  */
516*4882a593Smuzhiyun int make_inode_ready_for_data_ops(struct mount_info *mi,
517*4882a593Smuzhiyun 				struct inode *inode,
518*4882a593Smuzhiyun 				struct file *backing_file);
519*4882a593Smuzhiyun 
get_incfs_dentry(const struct dentry * d)520*4882a593Smuzhiyun static inline struct dentry_info *get_incfs_dentry(const struct dentry *d)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	if (!d)
523*4882a593Smuzhiyun 		return NULL;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	return (struct dentry_info *)d->d_fsdata;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
get_incfs_backing_path(const struct dentry * d,struct path * path)528*4882a593Smuzhiyun static inline void get_incfs_backing_path(const struct dentry *d,
529*4882a593Smuzhiyun 					  struct path *path)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	struct dentry_info *di = get_incfs_dentry(d);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	if (!di) {
534*4882a593Smuzhiyun 		*path = (struct path) {};
535*4882a593Smuzhiyun 		return;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	*path = di->backing_path;
539*4882a593Smuzhiyun 	path_get(path);
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun 
get_blocks_count_for_size(u64 size)542*4882a593Smuzhiyun static inline int get_blocks_count_for_size(u64 size)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun 	if (size == 0)
545*4882a593Smuzhiyun 		return 0;
546*4882a593Smuzhiyun 	return 1 + (size - 1) / INCFS_DATA_FILE_BLOCK_SIZE;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun #endif /* _INCFS_DATA_MGMT_H */
550