• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum LOG_RECORD_TYPE {
24 	FULL,
25 	SAME_FILE,
26 	SAME_FILE_NEXT_BLOCK,
27 	SAME_FILE_NEXT_BLOCK_SHORT,
28 };
29 
30 struct full_record {
31 	enum LOG_RECORD_TYPE type : 2; /* FULL */
32 	u32 block_index : 30;
33 	incfs_uuid_t file_id;
34 	u64 absolute_ts_us;
35 } __packed; /* 28 bytes */
36 
37 struct same_file_record {
38 	enum LOG_RECORD_TYPE type : 2; /* SAME_FILE */
39 	u32 block_index : 30;
40 	u32 relative_ts_us; /* max 2^32 us ~= 1 hour (1:11:30) */
41 } __packed; /* 12 bytes */
42 
43 struct same_file_next_block {
44 	enum LOG_RECORD_TYPE type : 2; /* SAME_FILE_NEXT_BLOCK */
45 	u32 relative_ts_us : 30; /* max 2^30 us ~= 15 min (17:50) */
46 } __packed; /* 4 bytes */
47 
48 struct same_file_next_block_short {
49 	enum LOG_RECORD_TYPE type : 2; /* SAME_FILE_NEXT_BLOCK_SHORT */
50 	u16 relative_ts_us : 14; /* max 2^14 us ~= 16 ms */
51 } __packed; /* 2 bytes */
52 
53 union log_record {
54 	struct full_record full_record;
55 	struct same_file_record same_file_record;
56 	struct same_file_next_block same_file_next_block;
57 	struct same_file_next_block_short same_file_next_block_short;
58 };
59 
60 struct read_log_state {
61 	/* Log buffer generation id, incremented on configuration changes */
62 	u32 generation_id;
63 
64 	/* Offset in rl_ring_buf to write into. */
65 	u32 next_offset;
66 
67 	/* Current number of writer passes over rl_ring_buf */
68 	u32 current_pass_no;
69 
70 	/* Current full_record to diff against */
71 	struct full_record base_record;
72 
73 	/* Current record number counting from configuration change */
74 	u64 current_record_no;
75 };
76 
77 /* A ring buffer to save records about data blocks which were recently read. */
78 struct read_log {
79 	void *rl_ring_buf;
80 
81 	int rl_size;
82 
83 	struct read_log_state rl_head;
84 
85 	struct read_log_state rl_tail;
86 
87 	/* A lock to protect the above fields */
88 	spinlock_t rl_lock;
89 
90 	/* A queue of waiters who want to be notified about reads */
91 	wait_queue_head_t ml_notif_wq;
92 
93 	/* A work item to wake up those waiters without slowing down readers */
94 	struct delayed_work ml_wakeup_work;
95 };
96 
97 struct mount_options {
98 	unsigned int read_timeout_ms;
99 	unsigned int readahead_pages;
100 	unsigned int read_log_pages;
101 	unsigned int read_log_wakeup_count;
102 	bool no_backing_file_cache;
103 	bool no_backing_file_readahead;
104 };
105 
106 struct mount_info {
107 	struct super_block *mi_sb;
108 
109 	struct path mi_backing_dir_path;
110 
111 	struct dentry *mi_index_dir;
112 	/* For stacking mounts, if true, this indicates if the index dir needs
113 	 * to be freed for this SB otherwise it was created by lower level SB */
114 	bool mi_index_free;
115 
116 	const struct cred *mi_owner;
117 
118 	struct mount_options mi_options;
119 
120 	/* This mutex is to be taken before create, rename, delete */
121 	struct mutex mi_dir_struct_mutex;
122 
123 	/*
124 	 * A queue of waiters who want to be notified about new pending reads.
125 	 */
126 	wait_queue_head_t mi_pending_reads_notif_wq;
127 
128 	/*
129 	 * Protects:
130 	 *  - reads_list_head
131 	 *  - mi_pending_reads_count
132 	 *  - mi_last_pending_read_number
133 	 *  - data_file_segment.reads_list_head
134 	 */
135 	struct mutex mi_pending_reads_mutex;
136 
137 	/* List of active pending_read objects */
138 	struct list_head mi_reads_list_head;
139 
140 	/* Total number of items in reads_list_head */
141 	int mi_pending_reads_count;
142 
143 	/*
144 	 * Last serial number that was assigned to a pending read.
145 	 * 0 means no pending reads have been seen yet.
146 	 */
147 	int mi_last_pending_read_number;
148 
149 	/* Temporary buffer for read logger. */
150 	struct read_log mi_log;
151 
152 	void *log_xattr;
153 	size_t log_xattr_size;
154 
155 	void *pending_read_xattr;
156 	size_t pending_read_xattr_size;
157 };
158 
159 struct data_file_block {
160 	loff_t db_backing_file_data_offset;
161 
162 	size_t db_stored_size;
163 
164 	enum incfs_compression_alg db_comp_alg;
165 };
166 
167 struct pending_read {
168 	incfs_uuid_t file_id;
169 
170 	s64 timestamp_us;
171 
172 	atomic_t done;
173 
174 	int block_index;
175 
176 	int serial_number;
177 
178 	struct list_head mi_reads_list;
179 
180 	struct list_head segment_reads_list;
181 };
182 
183 struct data_file_segment {
184 	wait_queue_head_t new_data_arrival_wq;
185 
186 	/* Protects reads and writes from the blockmap */
187 	/* Good candidate for read/write mutex */
188 	struct mutex blockmap_mutex;
189 
190 	/* List of active pending_read objects belonging to this segment */
191 	/* Protected by mount_info.pending_reads_mutex */
192 	struct list_head reads_list_head;
193 };
194 
195 /*
196  * Extra info associated with a file. Just a few bytes set by a user.
197  */
198 struct file_attr {
199 	loff_t fa_value_offset;
200 
201 	size_t fa_value_size;
202 
203 	u32 fa_crc;
204 };
205 
206 
207 struct data_file {
208 	struct backing_file_context *df_backing_file_context;
209 
210 	struct mount_info *df_mount_info;
211 
212 	incfs_uuid_t df_id;
213 
214 	/*
215 	 * Array of segments used to reduce lock contention for the file.
216 	 * Segment is chosen for a block depends on the block's index.
217 	 */
218 	struct data_file_segment df_segments[SEGMENTS_PER_FILE];
219 
220 	/* Base offset of the first metadata record. */
221 	loff_t df_metadata_off;
222 
223 	/* Base offset of the block map. */
224 	loff_t df_blockmap_off;
225 
226 	/* File size in bytes */
227 	loff_t df_size;
228 
229 	/* File header flags */
230 	u32 df_header_flags;
231 
232 	/* File size in DATA_FILE_BLOCK_SIZE blocks */
233 	int df_data_block_count;
234 
235 	/* Total number of blocks, data + hash */
236 	int df_total_block_count;
237 
238 	struct file_attr n_attr;
239 
240 	struct mtree *df_hash_tree;
241 
242 	struct incfs_df_signature *df_signature;
243 };
244 
245 struct dir_file {
246 	struct mount_info *mount_info;
247 
248 	struct file *backing_dir;
249 };
250 
251 struct inode_info {
252 	struct mount_info *n_mount_info; /* A mount, this file belongs to */
253 
254 	struct inode *n_backing_inode;
255 
256 	struct data_file *n_file;
257 
258 	struct inode n_vfs_inode;
259 };
260 
261 struct dentry_info {
262 	struct path backing_path;
263 };
264 
265 struct mount_info *incfs_alloc_mount_info(struct super_block *sb,
266 					  struct mount_options *options,
267 					  struct path *backing_dir_path);
268 
269 int incfs_realloc_mount_info(struct mount_info *mi,
270 			     struct mount_options *options);
271 
272 void incfs_free_mount_info(struct mount_info *mi);
273 
274 struct data_file *incfs_open_data_file(struct mount_info *mi, struct file *bf);
275 void incfs_free_data_file(struct data_file *df);
276 
277 int incfs_scan_metadata_chain(struct data_file *df);
278 
279 struct dir_file *incfs_open_dir_file(struct mount_info *mi, struct file *bf);
280 void incfs_free_dir_file(struct dir_file *dir);
281 
282 ssize_t incfs_read_data_file_block(struct mem_range dst, struct file *f,
283 				   int index, int timeout_ms,
284 				   struct mem_range tmp);
285 
286 int incfs_get_filled_blocks(struct data_file *df,
287 			    struct incfs_get_filled_blocks_args *arg);
288 
289 int incfs_read_file_signature(struct data_file *df, struct mem_range dst);
290 
291 int incfs_process_new_data_block(struct data_file *df,
292 				 struct incfs_fill_block *block, u8 *data);
293 
294 int incfs_process_new_hash_block(struct data_file *df,
295 				 struct incfs_fill_block *block, u8 *data);
296 
297 bool incfs_fresh_pending_reads_exist(struct mount_info *mi, int last_number);
298 
299 /*
300  * Collects pending reads and saves them into the array (reads/reads_size).
301  * Only reads with serial_number > sn_lowerbound are reported.
302  * Returns how many reads were saved into the array.
303  */
304 int incfs_collect_pending_reads(struct mount_info *mi, int sn_lowerbound,
305 				struct incfs_pending_read_info *reads,
306 				int reads_size);
307 
308 int incfs_collect_logged_reads(struct mount_info *mi,
309 			       struct read_log_state *start_state,
310 			       struct incfs_pending_read_info *reads,
311 			       int reads_size);
312 struct read_log_state incfs_get_log_state(struct mount_info *mi);
313 int incfs_get_uncollected_logs_count(struct mount_info *mi,
314 				     const struct read_log_state *state);
315 
get_incfs_node(struct inode * inode)316 static inline struct inode_info *get_incfs_node(struct inode *inode)
317 {
318 	if (!inode)
319 		return NULL;
320 
321 	if (inode->i_sb->s_magic != (long) INCFS_MAGIC_NUMBER) {
322 		/* This inode doesn't belong to us. */
323 		pr_warn_once("incfs: %s on an alien inode.", __func__);
324 		return NULL;
325 	}
326 
327 	return container_of(inode, struct inode_info, n_vfs_inode);
328 }
329 
get_incfs_data_file(struct file * f)330 static inline struct data_file *get_incfs_data_file(struct file *f)
331 {
332 	struct inode_info *node = NULL;
333 
334 	if (!f)
335 		return NULL;
336 
337 	if (!S_ISREG(f->f_inode->i_mode))
338 		return NULL;
339 
340 	node = get_incfs_node(f->f_inode);
341 	if (!node)
342 		return NULL;
343 
344 	return node->n_file;
345 }
346 
get_incfs_dir_file(struct file * f)347 static inline struct dir_file *get_incfs_dir_file(struct file *f)
348 {
349 	if (!f)
350 		return NULL;
351 
352 	if (!S_ISDIR(f->f_inode->i_mode))
353 		return NULL;
354 
355 	return (struct dir_file *)f->private_data;
356 }
357 
358 /*
359  * Make sure that inode_info.n_file is initialized and inode can be used
360  * for reading and writing data from/to the backing file.
361  */
362 int make_inode_ready_for_data_ops(struct mount_info *mi,
363 				struct inode *inode,
364 				struct file *backing_file);
365 
get_incfs_dentry(const struct dentry * d)366 static inline struct dentry_info *get_incfs_dentry(const struct dentry *d)
367 {
368 	if (!d)
369 		return NULL;
370 
371 	return (struct dentry_info *)d->d_fsdata;
372 }
373 
get_incfs_backing_path(const struct dentry * d,struct path * path)374 static inline void get_incfs_backing_path(const struct dentry *d,
375 					  struct path *path)
376 {
377 	struct dentry_info *di = get_incfs_dentry(d);
378 
379 	if (!di) {
380 		*path = (struct path) {};
381 		return;
382 	}
383 
384 	*path = di->backing_path;
385 	path_get(path);
386 }
387 
get_blocks_count_for_size(u64 size)388 static inline int get_blocks_count_for_size(u64 size)
389 {
390 	if (size == 0)
391 		return 0;
392 	return 1 + (size - 1) / INCFS_DATA_FILE_BLOCK_SIZE;
393 }
394 
395 bool incfs_equal_ranges(struct mem_range lhs, struct mem_range rhs);
396 
397 #endif /* _INCFS_DATA_MGMT_H */
398