1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright 2019 Google LLC
4 */
5 #ifndef _INCFS_DATA_MGMT_H
6 #define _INCFS_DATA_MGMT_H
7
8 #include <linux/cred.h>
9 #include <linux/fs.h>
10 #include <linux/types.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/wait.h>
15 #include <crypto/hash.h>
16
17 #include <uapi/linux/incrementalfs.h>
18
19 #include "internal.h"
20
21 #define SEGMENTS_PER_FILE 3
22
23 struct read_log_record {
24 u32 block_index : 31;
25
26 u32 timed_out : 1;
27
28 u64 timestamp_us;
29
30 incfs_uuid_t file_id;
31 } __packed;
32
33 struct read_log_state {
34 /* Next slot in rl_ring_buf to write to. */
35 u32 next_index;
36
37 /* Current number of writer pass over rl_ring_buf */
38 u32 current_pass_no;
39 };
40
41 /* A ring buffer to save records about data blocks which were recently read. */
42 struct read_log {
43 struct read_log_record *rl_ring_buf;
44
45 struct read_log_state rl_state;
46
47 spinlock_t rl_writer_lock;
48
49 int rl_size;
50
51 /*
52 * A queue of waiters who want to be notified about reads.
53 */
54 wait_queue_head_t ml_notif_wq;
55 };
56
57 struct mount_options {
58 unsigned int read_timeout_ms;
59 unsigned int readahead_pages;
60 unsigned int read_log_pages;
61 unsigned int read_log_wakeup_count;
62 bool no_backing_file_cache;
63 bool no_backing_file_readahead;
64 };
65
66 struct mount_info {
67 struct super_block *mi_sb;
68
69 struct path mi_backing_dir_path;
70
71 struct dentry *mi_index_dir;
72
73 const struct cred *mi_owner;
74
75 struct mount_options mi_options;
76
77 /* This mutex is to be taken before create, rename, delete */
78 struct mutex mi_dir_struct_mutex;
79
80 /*
81 * A queue of waiters who want to be notified about new pending reads.
82 */
83 wait_queue_head_t mi_pending_reads_notif_wq;
84
85 /*
86 * Protects:
87 * - reads_list_head
88 * - mi_pending_reads_count
89 * - mi_last_pending_read_number
90 * - data_file_segment.reads_list_head
91 */
92 struct mutex mi_pending_reads_mutex;
93
94 /* List of active pending_read objects */
95 struct list_head mi_reads_list_head;
96
97 /* Total number of items in reads_list_head */
98 int mi_pending_reads_count;
99
100 /*
101 * Last serial number that was assigned to a pending read.
102 * 0 means no pending reads have been seen yet.
103 */
104 int mi_last_pending_read_number;
105
106 /* Temporary buffer for read logger. */
107 struct read_log mi_log;
108 };
109
110 struct data_file_block {
111 loff_t db_backing_file_data_offset;
112
113 size_t db_stored_size;
114
115 enum incfs_compression_alg db_comp_alg;
116 };
117
118 struct pending_read {
119 incfs_uuid_t file_id;
120
121 s64 timestamp_us;
122
123 atomic_t done;
124
125 int block_index;
126
127 int serial_number;
128
129 struct list_head mi_reads_list;
130
131 struct list_head segment_reads_list;
132 };
133
134 struct data_file_segment {
135 wait_queue_head_t new_data_arrival_wq;
136
137 /* Protects reads and writes from the blockmap */
138 /* Good candidate for read/write mutex */
139 struct mutex blockmap_mutex;
140
141 /* List of active pending_read objects belonging to this segment */
142 /* Protected by mount_info.pending_reads_mutex */
143 struct list_head reads_list_head;
144 };
145
146 /*
147 * Extra info associated with a file. Just a few bytes set by a user.
148 */
149 struct file_attr {
150 loff_t fa_value_offset;
151
152 size_t fa_value_size;
153
154 u32 fa_crc;
155 };
156
157
158 struct data_file {
159 struct backing_file_context *df_backing_file_context;
160
161 struct mount_info *df_mount_info;
162
163 incfs_uuid_t df_id;
164
165 /*
166 * Array of segments used to reduce lock contention for the file.
167 * Segment is chosen for a block depends on the block's index.
168 */
169 struct data_file_segment df_segments[SEGMENTS_PER_FILE];
170
171 /* Base offset of the first metadata record. */
172 loff_t df_metadata_off;
173
174 /* Base offset of the block map. */
175 loff_t df_blockmap_off;
176
177 /* File size in bytes */
178 loff_t df_size;
179
180 int df_block_count; /* File size in DATA_FILE_BLOCK_SIZE blocks */
181
182 struct file_attr n_attr;
183
184 struct mtree *df_hash_tree;
185
186 struct ondisk_signature *df_signature;
187
188 /* True, if file signature has already been validated. */
189 bool df_signature_validated;
190 };
191
192 struct dir_file {
193 struct mount_info *mount_info;
194
195 struct file *backing_dir;
196 };
197
198 struct inode_info {
199 struct mount_info *n_mount_info; /* A mount, this file belongs to */
200
201 struct inode *n_backing_inode;
202
203 struct data_file *n_file;
204
205 struct inode n_vfs_inode;
206 };
207
208 struct dentry_info {
209 struct path backing_path;
210 };
211
212 struct mount_info *incfs_alloc_mount_info(struct super_block *sb,
213 struct mount_options *options,
214 struct path *backing_dir_path);
215
216 void incfs_free_mount_info(struct mount_info *mi);
217
218 struct data_file *incfs_open_data_file(struct mount_info *mi, struct file *bf);
219 void incfs_free_data_file(struct data_file *df);
220
221 int incfs_scan_metadata_chain(struct data_file *df);
222
223 struct dir_file *incfs_open_dir_file(struct mount_info *mi, struct file *bf);
224 void incfs_free_dir_file(struct dir_file *dir);
225
226 ssize_t incfs_read_data_file_block(struct mem_range dst, struct data_file *df,
227 int index, int timeout_ms,
228 struct mem_range tmp);
229
230 int incfs_read_file_signature(struct data_file *df, struct mem_range dst);
231
232 int incfs_process_new_data_block(struct data_file *df,
233 struct incfs_new_data_block *block, u8 *data);
234
235 int incfs_process_new_hash_block(struct data_file *df,
236 struct incfs_new_data_block *block, u8 *data);
237
238
239 bool incfs_fresh_pending_reads_exist(struct mount_info *mi, int last_number);
240
241 /*
242 * Collects pending reads and saves them into the array (reads/reads_size).
243 * Only reads with serial_number > sn_lowerbound are reported.
244 * Returns how many reads were saved into the array.
245 */
246 int incfs_collect_pending_reads(struct mount_info *mi, int sn_lowerbound,
247 struct incfs_pending_read_info *reads,
248 int reads_size);
249
250 int incfs_collect_logged_reads(struct mount_info *mi,
251 struct read_log_state *start_state,
252 struct incfs_pending_read_info *reads,
253 int reads_size);
254 struct read_log_state incfs_get_log_state(struct mount_info *mi);
255 int incfs_get_uncollected_logs_count(struct mount_info *mi,
256 struct read_log_state state);
257
get_incfs_node(struct inode * inode)258 static inline struct inode_info *get_incfs_node(struct inode *inode)
259 {
260 if (!inode)
261 return NULL;
262
263 if (inode->i_sb->s_magic != INCFS_MAGIC_NUMBER) {
264 /* This inode doesn't belong to us. */
265 pr_warn_once("incfs: %s on an alien inode.", __func__);
266 return NULL;
267 }
268
269 return container_of(inode, struct inode_info, n_vfs_inode);
270 }
271
get_incfs_data_file(struct file * f)272 static inline struct data_file *get_incfs_data_file(struct file *f)
273 {
274 struct inode_info *node = NULL;
275
276 if (!f)
277 return NULL;
278
279 if (!S_ISREG(f->f_inode->i_mode))
280 return NULL;
281
282 node = get_incfs_node(f->f_inode);
283 if (!node)
284 return NULL;
285
286 return node->n_file;
287 }
288
get_incfs_dir_file(struct file * f)289 static inline struct dir_file *get_incfs_dir_file(struct file *f)
290 {
291 if (!f)
292 return NULL;
293
294 if (!S_ISDIR(f->f_inode->i_mode))
295 return NULL;
296
297 return (struct dir_file *)f->private_data;
298 }
299
300 /*
301 * Make sure that inode_info.n_file is initialized and inode can be used
302 * for reading and writing data from/to the backing file.
303 */
304 int make_inode_ready_for_data_ops(struct mount_info *mi,
305 struct inode *inode,
306 struct file *backing_file);
307
get_incfs_dentry(const struct dentry * d)308 static inline struct dentry_info *get_incfs_dentry(const struct dentry *d)
309 {
310 if (!d)
311 return NULL;
312
313 return (struct dentry_info *)d->d_fsdata;
314 }
315
get_incfs_backing_path(const struct dentry * d,struct path * path)316 static inline void get_incfs_backing_path(const struct dentry *d,
317 struct path *path)
318 {
319 struct dentry_info *di = get_incfs_dentry(d);
320
321 if (!di) {
322 *path = (struct path) {};
323 return;
324 }
325
326 *path = di->backing_path;
327 path_get(path);
328 }
329
get_blocks_count_for_size(u64 size)330 static inline int get_blocks_count_for_size(u64 size)
331 {
332 if (size == 0)
333 return 0;
334 return 1 + (size - 1) / INCFS_DATA_FILE_BLOCK_SIZE;
335 }
336
337 bool incfs_equal_ranges(struct mem_range lhs, struct mem_range rhs);
338
339 #endif /* _INCFS_DATA_MGMT_H */
340