• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 Google LLC
4  */
5 #include <linux/crc32.h>
6 #include <linux/file.h>
7 #include <linux/gfp.h>
8 #include <linux/ktime.h>
9 #include <linux/lz4.h>
10 #include <linux/mm.h>
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/workqueue.h>
15 
16 #include "data_mgmt.h"
17 #include "format.h"
18 #include "integrity.h"
19 
log_wake_up_all(struct work_struct * work)20 static void log_wake_up_all(struct work_struct *work)
21 {
22 	struct delayed_work *dw = container_of(work, struct delayed_work, work);
23 	struct read_log *rl = container_of(dw, struct read_log, ml_wakeup_work);
24 	wake_up_all(&rl->ml_notif_wq);
25 }
26 
incfs_alloc_mount_info(struct super_block * sb,struct mount_options * options,struct path * backing_dir_path)27 struct mount_info *incfs_alloc_mount_info(struct super_block *sb,
28 					  struct mount_options *options,
29 					  struct path *backing_dir_path)
30 {
31 	struct mount_info *mi = NULL;
32 	int error = 0;
33 
34 	mi = kzalloc(sizeof(*mi), GFP_NOFS);
35 	if (!mi)
36 		return ERR_PTR(-ENOMEM);
37 
38 	mi->mi_sb = sb;
39 	mi->mi_backing_dir_path = *backing_dir_path;
40 	mi->mi_owner = get_current_cred();
41 	path_get(&mi->mi_backing_dir_path);
42 	mutex_init(&mi->mi_dir_struct_mutex);
43 	mutex_init(&mi->mi_pending_reads_mutex);
44 	init_waitqueue_head(&mi->mi_pending_reads_notif_wq);
45 	init_waitqueue_head(&mi->mi_log.ml_notif_wq);
46 	INIT_DELAYED_WORK(&mi->mi_log.ml_wakeup_work, log_wake_up_all);
47 	spin_lock_init(&mi->mi_log.rl_lock);
48 	INIT_LIST_HEAD(&mi->mi_reads_list_head);
49 
50 	error = incfs_realloc_mount_info(mi, options);
51 	if (error)
52 		goto err;
53 
54 	return mi;
55 
56 err:
57 	incfs_free_mount_info(mi);
58 	return ERR_PTR(error);
59 }
60 
incfs_realloc_mount_info(struct mount_info * mi,struct mount_options * options)61 int incfs_realloc_mount_info(struct mount_info *mi,
62 			     struct mount_options *options)
63 {
64 	void *new_buffer = NULL;
65 	void *old_buffer;
66 	size_t new_buffer_size = 0;
67 
68 	if (options->read_log_pages != mi->mi_options.read_log_pages) {
69 		struct read_log_state log_state;
70 		/*
71 		 * Even though having two buffers allocated at once isn't
72 		 * usually good, allocating a multipage buffer under a spinlock
73 		 * is even worse, so let's optimize for the shorter lock
74 		 * duration. It's not end of the world if we fail to increase
75 		 * the buffer size anyway.
76 		 */
77 		if (options->read_log_pages > 0) {
78 			new_buffer_size = PAGE_SIZE * options->read_log_pages;
79 			new_buffer = kzalloc(new_buffer_size, GFP_NOFS);
80 			if (!new_buffer)
81 				return -ENOMEM;
82 		}
83 
84 		spin_lock(&mi->mi_log.rl_lock);
85 		old_buffer = mi->mi_log.rl_ring_buf;
86 		mi->mi_log.rl_ring_buf = new_buffer;
87 		mi->mi_log.rl_size = new_buffer_size;
88 		log_state = (struct read_log_state){
89 			.generation_id = mi->mi_log.rl_head.generation_id + 1,
90 		};
91 		mi->mi_log.rl_head = log_state;
92 		mi->mi_log.rl_tail = log_state;
93 		spin_unlock(&mi->mi_log.rl_lock);
94 
95 		kfree(old_buffer);
96 	}
97 
98 	mi->mi_options = *options;
99 	return 0;
100 }
101 
incfs_free_mount_info(struct mount_info * mi)102 void incfs_free_mount_info(struct mount_info *mi)
103 {
104 	if (!mi)
105 		return;
106 
107 	flush_delayed_work(&mi->mi_log.ml_wakeup_work);
108 
109 	dput(mi->mi_index_dir);
110 	path_put(&mi->mi_backing_dir_path);
111 	mutex_destroy(&mi->mi_dir_struct_mutex);
112 	mutex_destroy(&mi->mi_pending_reads_mutex);
113 	put_cred(mi->mi_owner);
114 	kfree(mi->mi_log.rl_ring_buf);
115 	kfree(mi->log_xattr);
116 	kfree(mi->pending_read_xattr);
117 	kfree(mi);
118 }
119 
data_file_segment_init(struct data_file_segment * segment)120 static void data_file_segment_init(struct data_file_segment *segment)
121 {
122 	init_waitqueue_head(&segment->new_data_arrival_wq);
123 	mutex_init(&segment->blockmap_mutex);
124 	INIT_LIST_HEAD(&segment->reads_list_head);
125 }
126 
data_file_segment_destroy(struct data_file_segment * segment)127 static void data_file_segment_destroy(struct data_file_segment *segment)
128 {
129 	mutex_destroy(&segment->blockmap_mutex);
130 }
131 
incfs_open_data_file(struct mount_info * mi,struct file * bf)132 struct data_file *incfs_open_data_file(struct mount_info *mi, struct file *bf)
133 {
134 	struct data_file *df = NULL;
135 	struct backing_file_context *bfc = NULL;
136 	int md_records;
137 	u64 size;
138 	int error = 0;
139 	int i;
140 
141 	if (!bf || !mi)
142 		return ERR_PTR(-EFAULT);
143 
144 	if (!S_ISREG(bf->f_inode->i_mode))
145 		return ERR_PTR(-EBADF);
146 
147 	bfc = incfs_alloc_bfc(mi, bf);
148 	if (IS_ERR(bfc))
149 		return ERR_CAST(bfc);
150 
151 	df = kzalloc(sizeof(*df), GFP_NOFS);
152 	if (!df) {
153 		error = -ENOMEM;
154 		goto out;
155 	}
156 
157 	df->df_backing_file_context = bfc;
158 	df->df_mount_info = mi;
159 	for (i = 0; i < ARRAY_SIZE(df->df_segments); i++)
160 		data_file_segment_init(&df->df_segments[i]);
161 
162 	error = mutex_lock_interruptible(&bfc->bc_mutex);
163 	if (error)
164 		goto out;
165 	error = incfs_read_file_header(bfc, &df->df_metadata_off, &df->df_id,
166 				       &size, &df->df_header_flags);
167 	mutex_unlock(&bfc->bc_mutex);
168 
169 	if (error)
170 		goto out;
171 
172 	df->df_size = size;
173 	if (size > 0)
174 		df->df_data_block_count = get_blocks_count_for_size(size);
175 
176 	md_records = incfs_scan_metadata_chain(df);
177 	if (md_records < 0)
178 		error = md_records;
179 
180 out:
181 	if (error) {
182 		incfs_free_bfc(bfc);
183 		if (df)
184 			df->df_backing_file_context = NULL;
185 		incfs_free_data_file(df);
186 		return ERR_PTR(error);
187 	}
188 	return df;
189 }
190 
incfs_free_data_file(struct data_file * df)191 void incfs_free_data_file(struct data_file *df)
192 {
193 	int i;
194 
195 	if (!df)
196 		return;
197 
198 	incfs_free_mtree(df->df_hash_tree);
199 	for (i = 0; i < ARRAY_SIZE(df->df_segments); i++)
200 		data_file_segment_destroy(&df->df_segments[i]);
201 	incfs_free_bfc(df->df_backing_file_context);
202 	kfree(df->df_signature);
203 	kfree(df);
204 }
205 
make_inode_ready_for_data_ops(struct mount_info * mi,struct inode * inode,struct file * backing_file)206 int make_inode_ready_for_data_ops(struct mount_info *mi,
207 				struct inode *inode,
208 				struct file *backing_file)
209 {
210 	struct inode_info *node = get_incfs_node(inode);
211 	struct data_file *df = NULL;
212 	int err = 0;
213 
214 	inode_lock(inode);
215 	if (S_ISREG(inode->i_mode)) {
216 		if (!node->n_file) {
217 			df = incfs_open_data_file(mi, backing_file);
218 
219 			if (IS_ERR(df))
220 				err = PTR_ERR(df);
221 			else
222 				node->n_file = df;
223 		}
224 	} else
225 		err = -EBADF;
226 	inode_unlock(inode);
227 	return err;
228 }
229 
incfs_open_dir_file(struct mount_info * mi,struct file * bf)230 struct dir_file *incfs_open_dir_file(struct mount_info *mi, struct file *bf)
231 {
232 	struct dir_file *dir = NULL;
233 
234 	if (!S_ISDIR(bf->f_inode->i_mode))
235 		return ERR_PTR(-EBADF);
236 
237 	dir = kzalloc(sizeof(*dir), GFP_NOFS);
238 	if (!dir)
239 		return ERR_PTR(-ENOMEM);
240 
241 	dir->backing_dir = get_file(bf);
242 	dir->mount_info = mi;
243 	return dir;
244 }
245 
incfs_free_dir_file(struct dir_file * dir)246 void incfs_free_dir_file(struct dir_file *dir)
247 {
248 	if (!dir)
249 		return;
250 	if (dir->backing_dir)
251 		fput(dir->backing_dir);
252 	kfree(dir);
253 }
254 
decompress(struct mem_range src,struct mem_range dst)255 static ssize_t decompress(struct mem_range src, struct mem_range dst)
256 {
257 	int result = LZ4_decompress_safe(src.data, dst.data, src.len, dst.len);
258 
259 	if (result < 0)
260 		return -EBADMSG;
261 
262 	return result;
263 }
264 
log_read_one_record(struct read_log * rl,struct read_log_state * rs)265 static void log_read_one_record(struct read_log *rl, struct read_log_state *rs)
266 {
267 	union log_record *record =
268 		(union log_record *)((u8 *)rl->rl_ring_buf + rs->next_offset);
269 	size_t record_size;
270 
271 	switch (record->full_record.type) {
272 	case FULL:
273 		rs->base_record = record->full_record;
274 		record_size = sizeof(record->full_record);
275 		break;
276 
277 	case SAME_FILE:
278 		rs->base_record.block_index =
279 			record->same_file_record.block_index;
280 		rs->base_record.absolute_ts_us +=
281 			record->same_file_record.relative_ts_us;
282 		record_size = sizeof(record->same_file_record);
283 		break;
284 
285 	case SAME_FILE_NEXT_BLOCK:
286 		++rs->base_record.block_index;
287 		rs->base_record.absolute_ts_us +=
288 			record->same_file_next_block.relative_ts_us;
289 		record_size = sizeof(record->same_file_next_block);
290 		break;
291 
292 	case SAME_FILE_NEXT_BLOCK_SHORT:
293 		++rs->base_record.block_index;
294 		rs->base_record.absolute_ts_us +=
295 			record->same_file_next_block_short.relative_ts_us;
296 		record_size = sizeof(record->same_file_next_block_short);
297 		break;
298 	}
299 
300 	rs->next_offset += record_size;
301 	if (rs->next_offset > rl->rl_size - sizeof(*record)) {
302 		rs->next_offset = 0;
303 		++rs->current_pass_no;
304 	}
305 	++rs->current_record_no;
306 }
307 
log_block_read(struct mount_info * mi,incfs_uuid_t * id,int block_index)308 static void log_block_read(struct mount_info *mi, incfs_uuid_t *id,
309 			   int block_index)
310 {
311 	struct read_log *log = &mi->mi_log;
312 	struct read_log_state *head, *tail;
313 	s64 now_us;
314 	s64 relative_us;
315 	union log_record record;
316 	size_t record_size;
317 
318 	/*
319 	 * This may read the old value, but it's OK to delay the logging start
320 	 * right after the configuration update.
321 	 */
322 	if (READ_ONCE(log->rl_size) == 0)
323 		return;
324 
325 	now_us = ktime_to_us(ktime_get());
326 
327 	spin_lock(&log->rl_lock);
328 	if (log->rl_size == 0) {
329 		spin_unlock(&log->rl_lock);
330 		return;
331 	}
332 
333 	head = &log->rl_head;
334 	tail = &log->rl_tail;
335 	relative_us = now_us - head->base_record.absolute_ts_us;
336 
337 	if (memcmp(id, &head->base_record.file_id, sizeof(incfs_uuid_t)) ||
338 	    relative_us >= 1ll << 32) {
339 		record.full_record = (struct full_record){
340 			.type = FULL,
341 			.block_index = block_index,
342 			.file_id = *id,
343 			.absolute_ts_us = now_us,
344 		};
345 		head->base_record.file_id = *id;
346 		record_size = sizeof(struct full_record);
347 	} else if (block_index != head->base_record.block_index + 1 ||
348 		   relative_us >= 1 << 30) {
349 		record.same_file_record = (struct same_file_record){
350 			.type = SAME_FILE,
351 			.block_index = block_index,
352 			.relative_ts_us = relative_us,
353 		};
354 		record_size = sizeof(struct same_file_record);
355 	} else if (relative_us >= 1 << 14) {
356 		record.same_file_next_block = (struct same_file_next_block){
357 			.type = SAME_FILE_NEXT_BLOCK,
358 			.relative_ts_us = relative_us,
359 		};
360 		record_size = sizeof(struct same_file_next_block);
361 	} else {
362 		record.same_file_next_block_short =
363 			(struct same_file_next_block_short){
364 				.type = SAME_FILE_NEXT_BLOCK_SHORT,
365 				.relative_ts_us = relative_us,
366 			};
367 		record_size = sizeof(struct same_file_next_block_short);
368 	}
369 
370 	head->base_record.block_index = block_index;
371 	head->base_record.absolute_ts_us = now_us;
372 
373 	/* Advance tail beyond area we are going to overwrite */
374 	while (tail->current_pass_no < head->current_pass_no &&
375 	       tail->next_offset < head->next_offset + record_size)
376 		log_read_one_record(log, tail);
377 
378 	memcpy(((u8 *)log->rl_ring_buf) + head->next_offset, &record,
379 	       record_size);
380 	head->next_offset += record_size;
381 	if (head->next_offset > log->rl_size - sizeof(record)) {
382 		head->next_offset = 0;
383 		++head->current_pass_no;
384 	}
385 	++head->current_record_no;
386 
387 	spin_unlock(&log->rl_lock);
388 	schedule_delayed_work(&log->ml_wakeup_work, msecs_to_jiffies(16));
389 }
390 
validate_hash_tree(struct backing_file_context * bfc,struct file * f,int block_index,struct mem_range data,u8 * buf)391 static int validate_hash_tree(struct backing_file_context *bfc, struct file *f,
392 			      int block_index, struct mem_range data, u8 *buf)
393 {
394 	struct data_file *df = get_incfs_data_file(f);
395 	u8 stored_digest[INCFS_MAX_HASH_SIZE] = {};
396 	u8 calculated_digest[INCFS_MAX_HASH_SIZE] = {};
397 	struct mtree *tree = NULL;
398 	struct incfs_df_signature *sig = NULL;
399 	int digest_size;
400 	int hash_block_index = block_index;
401 	int lvl;
402 	int res;
403 	loff_t hash_block_offset[INCFS_MAX_MTREE_LEVELS];
404 	size_t hash_offset_in_block[INCFS_MAX_MTREE_LEVELS];
405 	int hash_per_block;
406 	pgoff_t file_pages;
407 
408 	tree = df->df_hash_tree;
409 	sig = df->df_signature;
410 	if (!tree || !sig)
411 		return 0;
412 
413 	digest_size = tree->alg->digest_size;
414 	hash_per_block = INCFS_DATA_FILE_BLOCK_SIZE / digest_size;
415 	for (lvl = 0; lvl < tree->depth; lvl++) {
416 		loff_t lvl_off = tree->hash_level_suboffset[lvl];
417 
418 		hash_block_offset[lvl] =
419 			lvl_off + round_down(hash_block_index * digest_size,
420 					     INCFS_DATA_FILE_BLOCK_SIZE);
421 		hash_offset_in_block[lvl] = hash_block_index * digest_size %
422 					    INCFS_DATA_FILE_BLOCK_SIZE;
423 		hash_block_index /= hash_per_block;
424 	}
425 
426 	memcpy(stored_digest, tree->root_hash, digest_size);
427 
428 	file_pages = DIV_ROUND_UP(df->df_size, INCFS_DATA_FILE_BLOCK_SIZE);
429 	for (lvl = tree->depth - 1; lvl >= 0; lvl--) {
430 		pgoff_t hash_page =
431 			file_pages +
432 			hash_block_offset[lvl] / INCFS_DATA_FILE_BLOCK_SIZE;
433 		struct page *page = find_get_page_flags(
434 			f->f_inode->i_mapping, hash_page, FGP_ACCESSED);
435 
436 		if (page && PageChecked(page)) {
437 			u8 *addr = kmap_atomic(page);
438 
439 			memcpy(stored_digest, addr + hash_offset_in_block[lvl],
440 			       digest_size);
441 			kunmap_atomic(addr);
442 			put_page(page);
443 			continue;
444 		}
445 
446 		if (page)
447 			put_page(page);
448 
449 		res = incfs_kread(bfc, buf, INCFS_DATA_FILE_BLOCK_SIZE,
450 				  hash_block_offset[lvl] + sig->hash_offset);
451 		if (res < 0)
452 			return res;
453 		if (res != INCFS_DATA_FILE_BLOCK_SIZE)
454 			return -EIO;
455 		res = incfs_calc_digest(tree->alg,
456 					range(buf, INCFS_DATA_FILE_BLOCK_SIZE),
457 					range(calculated_digest, digest_size));
458 		if (res)
459 			return res;
460 
461 		if (memcmp(stored_digest, calculated_digest, digest_size)) {
462 			int i;
463 			bool zero = true;
464 
465 			pr_debug("incfs: Hash mismatch lvl:%d blk:%d\n",
466 				lvl, block_index);
467 			for (i = 0; i < digest_size; i++)
468 				if (stored_digest[i]) {
469 					zero = false;
470 					break;
471 				}
472 
473 			if (zero)
474 				pr_debug("incfs: Note saved_digest all zero - did you forget to load the hashes?\n");
475 			return -EBADMSG;
476 		}
477 
478 		memcpy(stored_digest, buf + hash_offset_in_block[lvl],
479 		       digest_size);
480 
481 		page = grab_cache_page(f->f_inode->i_mapping, hash_page);
482 		if (page) {
483 			u8 *addr = kmap_atomic(page);
484 
485 			memcpy(addr, buf, INCFS_DATA_FILE_BLOCK_SIZE);
486 			kunmap_atomic(addr);
487 			SetPageChecked(page);
488 			unlock_page(page);
489 			put_page(page);
490 		}
491 	}
492 
493 	res = incfs_calc_digest(tree->alg, data,
494 				range(calculated_digest, digest_size));
495 	if (res)
496 		return res;
497 
498 	if (memcmp(stored_digest, calculated_digest, digest_size)) {
499 		pr_debug("incfs: Leaf hash mismatch blk:%d\n", block_index);
500 		return -EBADMSG;
501 	}
502 
503 	return 0;
504 }
505 
get_file_segment(struct data_file * df,int block_index)506 static struct data_file_segment *get_file_segment(struct data_file *df,
507 						  int block_index)
508 {
509 	int seg_idx = block_index % ARRAY_SIZE(df->df_segments);
510 
511 	return &df->df_segments[seg_idx];
512 }
513 
is_data_block_present(struct data_file_block * block)514 static bool is_data_block_present(struct data_file_block *block)
515 {
516 	return (block->db_backing_file_data_offset != 0) &&
517 	       (block->db_stored_size != 0);
518 }
519 
convert_data_file_block(struct incfs_blockmap_entry * bme,struct data_file_block * res_block)520 static void convert_data_file_block(struct incfs_blockmap_entry *bme,
521 				    struct data_file_block *res_block)
522 {
523 	u16 flags = le16_to_cpu(bme->me_flags);
524 
525 	res_block->db_backing_file_data_offset =
526 		le16_to_cpu(bme->me_data_offset_hi);
527 	res_block->db_backing_file_data_offset <<= 32;
528 	res_block->db_backing_file_data_offset |=
529 		le32_to_cpu(bme->me_data_offset_lo);
530 	res_block->db_stored_size = le16_to_cpu(bme->me_data_size);
531 	res_block->db_comp_alg = (flags & INCFS_BLOCK_COMPRESSED_LZ4) ?
532 					 COMPRESSION_LZ4 :
533 					 COMPRESSION_NONE;
534 }
535 
get_data_file_block(struct data_file * df,int index,struct data_file_block * res_block)536 static int get_data_file_block(struct data_file *df, int index,
537 			       struct data_file_block *res_block)
538 {
539 	struct incfs_blockmap_entry bme = {};
540 	struct backing_file_context *bfc = NULL;
541 	loff_t blockmap_off = 0;
542 	int error = 0;
543 
544 	if (!df || !res_block)
545 		return -EFAULT;
546 
547 	blockmap_off = df->df_blockmap_off;
548 	bfc = df->df_backing_file_context;
549 
550 	if (index < 0 || blockmap_off == 0)
551 		return -EINVAL;
552 
553 	error = incfs_read_blockmap_entry(bfc, index, blockmap_off, &bme);
554 	if (error)
555 		return error;
556 
557 	convert_data_file_block(&bme, res_block);
558 	return 0;
559 }
560 
check_room_for_one_range(u32 size,u32 size_out)561 static int check_room_for_one_range(u32 size, u32 size_out)
562 {
563 	if (size_out + sizeof(struct incfs_filled_range) > size)
564 		return -ERANGE;
565 	return 0;
566 }
567 
copy_one_range(struct incfs_filled_range * range,void __user * buffer,u32 size,u32 * size_out)568 static int copy_one_range(struct incfs_filled_range *range, void __user *buffer,
569 			  u32 size, u32 *size_out)
570 {
571 	int error = check_room_for_one_range(size, *size_out);
572 	if (error)
573 		return error;
574 
575 	if (copy_to_user(((char __user *)buffer) + *size_out, range,
576 				sizeof(*range)))
577 		return -EFAULT;
578 
579 	*size_out += sizeof(*range);
580 	return 0;
581 }
582 
update_file_header_flags(struct data_file * df,u32 bits_to_reset,u32 bits_to_set)583 static int update_file_header_flags(struct data_file *df, u32 bits_to_reset,
584 				    u32 bits_to_set)
585 {
586 	int result;
587 	u32 new_flags;
588 	struct backing_file_context *bfc;
589 
590 	if (!df)
591 		return -EFAULT;
592 	bfc = df->df_backing_file_context;
593 	if (!bfc)
594 		return -EFAULT;
595 
596 	result = mutex_lock_interruptible(&bfc->bc_mutex);
597 	if (result)
598 		return result;
599 
600 	new_flags = (df->df_header_flags & ~bits_to_reset) | bits_to_set;
601 	if (new_flags != df->df_header_flags) {
602 		df->df_header_flags = new_flags;
603 		result = incfs_write_file_header_flags(bfc, new_flags);
604 	}
605 
606 	mutex_unlock(&bfc->bc_mutex);
607 
608 	return result;
609 }
610 
611 #define READ_BLOCKMAP_ENTRIES 512
incfs_get_filled_blocks(struct data_file * df,struct incfs_get_filled_blocks_args * arg)612 int incfs_get_filled_blocks(struct data_file *df,
613 			    struct incfs_get_filled_blocks_args *arg)
614 {
615 	int error = 0;
616 	bool in_range = false;
617 	struct incfs_filled_range range;
618 	void __user *buffer = u64_to_user_ptr(arg->range_buffer);
619 	u32 size = arg->range_buffer_size;
620 	u32 end_index =
621 		arg->end_index ? arg->end_index : df->df_total_block_count;
622 	u32 *size_out = &arg->range_buffer_size_out;
623 	int i = READ_BLOCKMAP_ENTRIES - 1;
624 	int entries_read = 0;
625 	struct incfs_blockmap_entry *bme;
626 
627 	*size_out = 0;
628 	if (end_index > df->df_total_block_count)
629 		end_index = df->df_total_block_count;
630 	arg->total_blocks_out = df->df_total_block_count;
631 	arg->data_blocks_out = df->df_data_block_count;
632 
633 	if (df->df_header_flags & INCFS_FILE_COMPLETE) {
634 		pr_debug("File marked full, fast get_filled_blocks");
635 		if (arg->start_index > end_index) {
636 			arg->index_out = arg->start_index;
637 			return 0;
638 		}
639 		arg->index_out = arg->start_index;
640 
641 		error = check_room_for_one_range(size, *size_out);
642 		if (error)
643 			return error;
644 
645 		range = (struct incfs_filled_range){
646 			.begin = arg->start_index,
647 			.end = end_index,
648 		};
649 
650 		error = copy_one_range(&range, buffer, size, size_out);
651 		if (error)
652 			return error;
653 		arg->index_out = end_index;
654 		return 0;
655 	}
656 
657 	bme = kzalloc(sizeof(*bme) * READ_BLOCKMAP_ENTRIES,
658 		      GFP_NOFS | __GFP_COMP);
659 	if (!bme)
660 		return -ENOMEM;
661 
662 	for (arg->index_out = arg->start_index; arg->index_out < end_index;
663 	     ++arg->index_out) {
664 		struct data_file_block dfb;
665 
666 		if (++i == READ_BLOCKMAP_ENTRIES) {
667 			entries_read = incfs_read_blockmap_entries(
668 				df->df_backing_file_context, bme,
669 				arg->index_out, READ_BLOCKMAP_ENTRIES,
670 				df->df_blockmap_off);
671 			if (entries_read < 0) {
672 				error = entries_read;
673 				break;
674 			}
675 
676 			i = 0;
677 		}
678 
679 		if (i >= entries_read) {
680 			error = -EIO;
681 			break;
682 		}
683 
684 		convert_data_file_block(bme + i, &dfb);
685 
686 		if (is_data_block_present(&dfb) == in_range)
687 			continue;
688 
689 		if (!in_range) {
690 			error = check_room_for_one_range(size, *size_out);
691 			if (error)
692 				break;
693 			in_range = true;
694 			range.begin = arg->index_out;
695 		} else {
696 			range.end = arg->index_out;
697 			error = copy_one_range(&range, buffer, size, size_out);
698 			if (error) {
699 				/* there will be another try out of the loop,
700 				 * it will reset the index_out if it fails too
701 				 */
702 				break;
703 			}
704 			in_range = false;
705 		}
706 	}
707 
708 	if (in_range) {
709 		range.end = arg->index_out;
710 		error = copy_one_range(&range, buffer, size, size_out);
711 		if (error)
712 			arg->index_out = range.begin;
713 	}
714 
715 	if (!error && in_range && arg->start_index == 0 &&
716 	    end_index == df->df_total_block_count &&
717 	    *size_out == sizeof(struct incfs_filled_range)) {
718 		int result =
719 			update_file_header_flags(df, 0, INCFS_FILE_COMPLETE);
720 		/* Log failure only, since it's just a failed optimization */
721 		pr_debug("Marked file full with result %d", result);
722 	}
723 
724 	kfree(bme);
725 	return error;
726 }
727 
is_read_done(struct pending_read * read)728 static bool is_read_done(struct pending_read *read)
729 {
730 	return atomic_read_acquire(&read->done) != 0;
731 }
732 
set_read_done(struct pending_read * read)733 static void set_read_done(struct pending_read *read)
734 {
735 	atomic_set_release(&read->done, 1);
736 }
737 
738 /*
739  * Notifies a given data file about pending read from a given block.
740  * Returns a new pending read entry.
741  */
add_pending_read(struct data_file * df,int block_index)742 static struct pending_read *add_pending_read(struct data_file *df,
743 					     int block_index)
744 {
745 	struct pending_read *result = NULL;
746 	struct data_file_segment *segment = NULL;
747 	struct mount_info *mi = NULL;
748 
749 	segment = get_file_segment(df, block_index);
750 	mi = df->df_mount_info;
751 
752 	result = kzalloc(sizeof(*result), GFP_NOFS);
753 	if (!result)
754 		return NULL;
755 
756 	result->file_id = df->df_id;
757 	result->block_index = block_index;
758 	result->timestamp_us = ktime_to_us(ktime_get());
759 
760 	mutex_lock(&mi->mi_pending_reads_mutex);
761 
762 	result->serial_number = ++mi->mi_last_pending_read_number;
763 	mi->mi_pending_reads_count++;
764 
765 	list_add(&result->mi_reads_list, &mi->mi_reads_list_head);
766 	list_add(&result->segment_reads_list, &segment->reads_list_head);
767 	mutex_unlock(&mi->mi_pending_reads_mutex);
768 
769 	wake_up_all(&mi->mi_pending_reads_notif_wq);
770 	return result;
771 }
772 
773 /* Notifies a given data file that pending read is completed. */
remove_pending_read(struct data_file * df,struct pending_read * read)774 static void remove_pending_read(struct data_file *df, struct pending_read *read)
775 {
776 	struct mount_info *mi = NULL;
777 
778 	if (!df || !read) {
779 		WARN_ON(!df);
780 		WARN_ON(!read);
781 		return;
782 	}
783 
784 	mi = df->df_mount_info;
785 
786 	mutex_lock(&mi->mi_pending_reads_mutex);
787 	list_del(&read->mi_reads_list);
788 	list_del(&read->segment_reads_list);
789 
790 	mi->mi_pending_reads_count--;
791 	mutex_unlock(&mi->mi_pending_reads_mutex);
792 
793 	kfree(read);
794 }
795 
notify_pending_reads(struct mount_info * mi,struct data_file_segment * segment,int index)796 static void notify_pending_reads(struct mount_info *mi,
797 		struct data_file_segment *segment,
798 		int index)
799 {
800 	struct pending_read *entry = NULL;
801 
802 	/* Notify pending reads waiting for this block. */
803 	mutex_lock(&mi->mi_pending_reads_mutex);
804 	list_for_each_entry(entry, &segment->reads_list_head,
805 						segment_reads_list) {
806 		if (entry->block_index == index)
807 			set_read_done(entry);
808 	}
809 	mutex_unlock(&mi->mi_pending_reads_mutex);
810 	wake_up_all(&segment->new_data_arrival_wq);
811 }
812 
wait_for_data_block(struct data_file * df,int block_index,int timeout_ms,struct data_file_block * res_block)813 static int wait_for_data_block(struct data_file *df, int block_index,
814 			       int timeout_ms,
815 			       struct data_file_block *res_block)
816 {
817 	struct data_file_block block = {};
818 	struct data_file_segment *segment = NULL;
819 	struct pending_read *read = NULL;
820 	struct mount_info *mi = NULL;
821 	int error = 0;
822 	int wait_res = 0;
823 
824 	if (!df || !res_block)
825 		return -EFAULT;
826 
827 	if (block_index < 0 || block_index >= df->df_data_block_count)
828 		return -EINVAL;
829 
830 	if (df->df_blockmap_off <= 0)
831 		return -ENODATA;
832 
833 	segment = get_file_segment(df, block_index);
834 	error = mutex_lock_interruptible(&segment->blockmap_mutex);
835 	if (error)
836 		return error;
837 
838 	/* Look up the given block */
839 	error = get_data_file_block(df, block_index, &block);
840 
841 	/* If it's not found, create a pending read */
842 	if (!error && !is_data_block_present(&block) && timeout_ms != 0)
843 		read = add_pending_read(df, block_index);
844 
845 	mutex_unlock(&segment->blockmap_mutex);
846 	if (error)
847 		return error;
848 
849 	/* If the block was found, just return it. No need to wait. */
850 	if (is_data_block_present(&block)) {
851 		*res_block = block;
852 		return 0;
853 	}
854 
855 	mi = df->df_mount_info;
856 
857 	if (timeout_ms == 0) {
858 		log_block_read(mi, &df->df_id, block_index);
859 		return -ETIME;
860 	}
861 
862 	if (!read)
863 		return -ENOMEM;
864 
865 	/* Wait for notifications about block's arrival */
866 	wait_res =
867 		wait_event_interruptible_timeout(segment->new_data_arrival_wq,
868 						 (is_read_done(read)),
869 						 msecs_to_jiffies(timeout_ms));
870 
871 	/* Woke up, the pending read is no longer needed. */
872 	remove_pending_read(df, read);
873 	read = NULL;
874 
875 	if (wait_res == 0) {
876 		/* Wait has timed out */
877 		log_block_read(mi, &df->df_id, block_index);
878 		return -ETIME;
879 	}
880 	if (wait_res < 0) {
881 		/*
882 		 * Only ERESTARTSYS is really expected here when a signal
883 		 * comes while we wait.
884 		 */
885 		return wait_res;
886 	}
887 
888 	error = mutex_lock_interruptible(&segment->blockmap_mutex);
889 	if (error)
890 		return error;
891 
892 	/*
893 	 * Re-read block's info now, it has just arrived and
894 	 * should be available.
895 	 */
896 	error = get_data_file_block(df, block_index, &block);
897 	if (!error) {
898 		if (is_data_block_present(&block))
899 			*res_block = block;
900 		else {
901 			/*
902 			 * Somehow wait finished successfully bug block still
903 			 * can't be found. It's not normal.
904 			 */
905 			pr_warn("incfs:Wait succeeded, but block not found.\n");
906 			error = -ENODATA;
907 		}
908 	}
909 
910 	mutex_unlock(&segment->blockmap_mutex);
911 	return error;
912 }
913 
incfs_read_data_file_block(struct mem_range dst,struct file * f,int index,int timeout_ms,struct mem_range tmp)914 ssize_t incfs_read_data_file_block(struct mem_range dst, struct file *f,
915 				   int index, int timeout_ms,
916 				   struct mem_range tmp)
917 {
918 	loff_t pos;
919 	ssize_t result;
920 	size_t bytes_to_read;
921 	struct mount_info *mi = NULL;
922 	struct backing_file_context *bfc = NULL;
923 	struct data_file_block block = {};
924 	struct data_file *df = get_incfs_data_file(f);
925 
926 	if (!dst.data || !df)
927 		return -EFAULT;
928 
929 	if (tmp.len < 2 * INCFS_DATA_FILE_BLOCK_SIZE)
930 		return -ERANGE;
931 
932 	mi = df->df_mount_info;
933 	bfc = df->df_backing_file_context;
934 
935 	result = wait_for_data_block(df, index, timeout_ms, &block);
936 	if (result < 0)
937 		goto out;
938 
939 	pos = block.db_backing_file_data_offset;
940 	if (block.db_comp_alg == COMPRESSION_NONE) {
941 		bytes_to_read = min(dst.len, block.db_stored_size);
942 		result = incfs_kread(bfc, dst.data, bytes_to_read, pos);
943 
944 		/* Some data was read, but not enough */
945 		if (result >= 0 && result != bytes_to_read)
946 			result = -EIO;
947 	} else {
948 		bytes_to_read = min(tmp.len, block.db_stored_size);
949 		result = incfs_kread(bfc, tmp.data, bytes_to_read, pos);
950 		if (result == bytes_to_read) {
951 			result =
952 				decompress(range(tmp.data, bytes_to_read), dst);
953 			if (result < 0) {
954 				const char *name =
955 				    bfc->bc_file->f_path.dentry->d_name.name;
956 
957 				pr_warn_once("incfs: Decompression error. %s",
958 					     name);
959 			}
960 		} else if (result >= 0) {
961 			/* Some data was read, but not enough */
962 			result = -EIO;
963 		}
964 	}
965 
966 	if (result > 0) {
967 		int err = validate_hash_tree(bfc, f, index, dst, tmp.data);
968 
969 		if (err < 0)
970 			result = err;
971 	}
972 
973 	if (result >= 0)
974 		log_block_read(mi, &df->df_id, index);
975 
976 out:
977 	return result;
978 }
979 
incfs_process_new_data_block(struct data_file * df,struct incfs_fill_block * block,u8 * data)980 int incfs_process_new_data_block(struct data_file *df,
981 				 struct incfs_fill_block *block, u8 *data)
982 {
983 	struct mount_info *mi = NULL;
984 	struct backing_file_context *bfc = NULL;
985 	struct data_file_segment *segment = NULL;
986 	struct data_file_block existing_block = {};
987 	u16 flags = 0;
988 	int error = 0;
989 
990 	if (!df || !block)
991 		return -EFAULT;
992 
993 	bfc = df->df_backing_file_context;
994 	mi = df->df_mount_info;
995 
996 	if (block->block_index >= df->df_data_block_count)
997 		return -ERANGE;
998 
999 	segment = get_file_segment(df, block->block_index);
1000 	if (!segment)
1001 		return -EFAULT;
1002 	if (block->compression == COMPRESSION_LZ4)
1003 		flags |= INCFS_BLOCK_COMPRESSED_LZ4;
1004 
1005 	error = mutex_lock_interruptible(&segment->blockmap_mutex);
1006 	if (error)
1007 		return error;
1008 
1009 	error = get_data_file_block(df, block->block_index, &existing_block);
1010 	if (error)
1011 		goto unlock;
1012 	if (is_data_block_present(&existing_block)) {
1013 		/* Block is already present, nothing to do here */
1014 		goto unlock;
1015 	}
1016 
1017 	error = mutex_lock_interruptible(&bfc->bc_mutex);
1018 	if (!error) {
1019 		error = incfs_write_data_block_to_backing_file(
1020 			bfc, range(data, block->data_len), block->block_index,
1021 			df->df_blockmap_off, flags);
1022 		mutex_unlock(&bfc->bc_mutex);
1023 	}
1024 	if (!error)
1025 		notify_pending_reads(mi, segment, block->block_index);
1026 
1027 unlock:
1028 	mutex_unlock(&segment->blockmap_mutex);
1029 	if (error)
1030 		pr_debug("%d error: %d\n", block->block_index, error);
1031 	return error;
1032 }
1033 
incfs_read_file_signature(struct data_file * df,struct mem_range dst)1034 int incfs_read_file_signature(struct data_file *df, struct mem_range dst)
1035 {
1036 	struct backing_file_context *bfc = df->df_backing_file_context;
1037 	struct incfs_df_signature *sig;
1038 	int read_res = 0;
1039 
1040 	if (!dst.data)
1041 		return -EFAULT;
1042 
1043 	sig = df->df_signature;
1044 	if (!sig)
1045 		return 0;
1046 
1047 	if (dst.len < sig->sig_size)
1048 		return -E2BIG;
1049 
1050 	read_res = incfs_kread(bfc, dst.data, sig->sig_size, sig->sig_offset);
1051 
1052 	if (read_res < 0)
1053 		return read_res;
1054 
1055 	if (read_res != sig->sig_size)
1056 		return -EIO;
1057 
1058 	return read_res;
1059 }
1060 
incfs_process_new_hash_block(struct data_file * df,struct incfs_fill_block * block,u8 * data)1061 int incfs_process_new_hash_block(struct data_file *df,
1062 				 struct incfs_fill_block *block, u8 *data)
1063 {
1064 	struct backing_file_context *bfc = NULL;
1065 	struct mount_info *mi = NULL;
1066 	struct mtree *hash_tree = NULL;
1067 	struct incfs_df_signature *sig = NULL;
1068 	loff_t hash_area_base = 0;
1069 	loff_t hash_area_size = 0;
1070 	int error = 0;
1071 
1072 	if (!df || !block)
1073 		return -EFAULT;
1074 
1075 	if (!(block->flags & INCFS_BLOCK_FLAGS_HASH))
1076 		return -EINVAL;
1077 
1078 	bfc = df->df_backing_file_context;
1079 	mi = df->df_mount_info;
1080 
1081 	if (!df)
1082 		return -ENOENT;
1083 
1084 	hash_tree = df->df_hash_tree;
1085 	sig = df->df_signature;
1086 	if (!hash_tree || !sig || sig->hash_offset == 0)
1087 		return -ENOTSUPP;
1088 
1089 	hash_area_base = sig->hash_offset;
1090 	hash_area_size = sig->hash_size;
1091 	if (hash_area_size < block->block_index * INCFS_DATA_FILE_BLOCK_SIZE
1092 				+ block->data_len) {
1093 		/* Hash block goes beyond dedicated hash area of this file. */
1094 		return -ERANGE;
1095 	}
1096 
1097 	error = mutex_lock_interruptible(&bfc->bc_mutex);
1098 	if (!error) {
1099 		error = incfs_write_hash_block_to_backing_file(
1100 			bfc, range(data, block->data_len), block->block_index,
1101 			hash_area_base, df->df_blockmap_off, df->df_size);
1102 		mutex_unlock(&bfc->bc_mutex);
1103 	}
1104 	return error;
1105 }
1106 
process_blockmap_md(struct incfs_blockmap * bm,struct metadata_handler * handler)1107 static int process_blockmap_md(struct incfs_blockmap *bm,
1108 			       struct metadata_handler *handler)
1109 {
1110 	struct data_file *df = handler->context;
1111 	int error = 0;
1112 	loff_t base_off = le64_to_cpu(bm->m_base_offset);
1113 	u32 block_count = le32_to_cpu(bm->m_block_count);
1114 
1115 	if (!df)
1116 		return -EFAULT;
1117 
1118 	if (df->df_data_block_count > block_count)
1119 		return -EBADMSG;
1120 
1121 	df->df_total_block_count = block_count;
1122 	df->df_blockmap_off = base_off;
1123 	return error;
1124 }
1125 
process_file_attr_md(struct incfs_file_attr * fa,struct metadata_handler * handler)1126 static int process_file_attr_md(struct incfs_file_attr *fa,
1127 				struct metadata_handler *handler)
1128 {
1129 	struct data_file *df = handler->context;
1130 	u16 attr_size = le16_to_cpu(fa->fa_size);
1131 
1132 	if (!df)
1133 		return -EFAULT;
1134 
1135 	if (attr_size > INCFS_MAX_FILE_ATTR_SIZE)
1136 		return -E2BIG;
1137 
1138 	df->n_attr.fa_value_offset = le64_to_cpu(fa->fa_offset);
1139 	df->n_attr.fa_value_size = attr_size;
1140 	df->n_attr.fa_crc = le32_to_cpu(fa->fa_crc);
1141 
1142 	return 0;
1143 }
1144 
process_file_signature_md(struct incfs_file_signature * sg,struct metadata_handler * handler)1145 static int process_file_signature_md(struct incfs_file_signature *sg,
1146 				struct metadata_handler *handler)
1147 {
1148 	struct data_file *df = handler->context;
1149 	struct mtree *hash_tree = NULL;
1150 	int error = 0;
1151 	struct incfs_df_signature *signature =
1152 		kzalloc(sizeof(*signature), GFP_NOFS);
1153 	void *buf = NULL;
1154 	ssize_t read;
1155 
1156 	if (!signature)
1157 		return -ENOMEM;
1158 
1159 	if (!df || !df->df_backing_file_context ||
1160 	    !df->df_backing_file_context->bc_file) {
1161 		error = -ENOENT;
1162 		goto out;
1163 	}
1164 
1165 	signature->hash_offset = le64_to_cpu(sg->sg_hash_tree_offset);
1166 	signature->hash_size = le32_to_cpu(sg->sg_hash_tree_size);
1167 	signature->sig_offset = le64_to_cpu(sg->sg_sig_offset);
1168 	signature->sig_size = le32_to_cpu(sg->sg_sig_size);
1169 
1170 	buf = kzalloc(signature->sig_size, GFP_NOFS);
1171 	if (!buf) {
1172 		error = -ENOMEM;
1173 		goto out;
1174 	}
1175 
1176 	read = incfs_kread(df->df_backing_file_context, buf,
1177 			   signature->sig_size, signature->sig_offset);
1178 	if (read < 0) {
1179 		error = read;
1180 		goto out;
1181 	}
1182 
1183 	if (read != signature->sig_size) {
1184 		error = -EINVAL;
1185 		goto out;
1186 	}
1187 
1188 	hash_tree = incfs_alloc_mtree(range(buf, signature->sig_size),
1189 				      df->df_data_block_count);
1190 	if (IS_ERR(hash_tree)) {
1191 		error = PTR_ERR(hash_tree);
1192 		hash_tree = NULL;
1193 		goto out;
1194 	}
1195 	if (hash_tree->hash_tree_area_size != signature->hash_size) {
1196 		error = -EINVAL;
1197 		goto out;
1198 	}
1199 	if (signature->hash_size > 0 &&
1200 	    handler->md_record_offset <= signature->hash_offset) {
1201 		error = -EINVAL;
1202 		goto out;
1203 	}
1204 	if (handler->md_record_offset <= signature->sig_offset) {
1205 		error = -EINVAL;
1206 		goto out;
1207 	}
1208 	df->df_hash_tree = hash_tree;
1209 	hash_tree = NULL;
1210 	df->df_signature = signature;
1211 	signature = NULL;
1212 out:
1213 	incfs_free_mtree(hash_tree);
1214 	kfree(signature);
1215 	kfree(buf);
1216 
1217 	return error;
1218 }
1219 
incfs_scan_metadata_chain(struct data_file * df)1220 int incfs_scan_metadata_chain(struct data_file *df)
1221 {
1222 	struct metadata_handler *handler = NULL;
1223 	int result = 0;
1224 	int records_count = 0;
1225 	int error = 0;
1226 	struct backing_file_context *bfc = NULL;
1227 
1228 	if (!df || !df->df_backing_file_context)
1229 		return -EFAULT;
1230 
1231 	bfc = df->df_backing_file_context;
1232 
1233 	handler = kzalloc(sizeof(*handler), GFP_NOFS);
1234 	if (!handler)
1235 		return -ENOMEM;
1236 
1237 	/* No writing to the backing file while it's being scanned. */
1238 	error = mutex_lock_interruptible(&bfc->bc_mutex);
1239 	if (error)
1240 		goto out;
1241 
1242 	/* Reading superblock */
1243 	handler->md_record_offset = df->df_metadata_off;
1244 	handler->context = df;
1245 	handler->handle_blockmap = process_blockmap_md;
1246 	handler->handle_file_attr = process_file_attr_md;
1247 	handler->handle_signature = process_file_signature_md;
1248 
1249 	pr_debug("incfs: Starting reading incfs-metadata records at offset %lld\n",
1250 		 handler->md_record_offset);
1251 	while (handler->md_record_offset > 0) {
1252 		error = incfs_read_next_metadata_record(bfc, handler);
1253 		if (error) {
1254 			pr_warn("incfs: Error during reading incfs-metadata record. Offset: %lld Record #%d Error code: %d\n",
1255 				handler->md_record_offset, records_count + 1,
1256 				-error);
1257 			break;
1258 		}
1259 		records_count++;
1260 	}
1261 	if (error) {
1262 		pr_debug("incfs: Error %d after reading %d incfs-metadata records.\n",
1263 			 -error, records_count);
1264 		result = error;
1265 	} else {
1266 		pr_debug("incfs: Finished reading %d incfs-metadata records.\n",
1267 			 records_count);
1268 		result = records_count;
1269 	}
1270 	mutex_unlock(&bfc->bc_mutex);
1271 
1272 	if (df->df_hash_tree) {
1273 		int hash_block_count = get_blocks_count_for_size(
1274 			df->df_hash_tree->hash_tree_area_size);
1275 
1276 		if (df->df_data_block_count + hash_block_count !=
1277 		    df->df_total_block_count)
1278 			result = -EINVAL;
1279 	} else if (df->df_data_block_count != df->df_total_block_count)
1280 		result = -EINVAL;
1281 
1282 out:
1283 	kfree(handler);
1284 	return result;
1285 }
1286 
1287 /*
1288  * Quickly checks if there are pending reads with a serial number larger
1289  * than a given one.
1290  */
incfs_fresh_pending_reads_exist(struct mount_info * mi,int last_number)1291 bool incfs_fresh_pending_reads_exist(struct mount_info *mi, int last_number)
1292 {
1293 	bool result = false;
1294 
1295 	mutex_lock(&mi->mi_pending_reads_mutex);
1296 	result = (mi->mi_last_pending_read_number > last_number) &&
1297 		 (mi->mi_pending_reads_count > 0);
1298 	mutex_unlock(&mi->mi_pending_reads_mutex);
1299 	return result;
1300 }
1301 
incfs_collect_pending_reads(struct mount_info * mi,int sn_lowerbound,struct incfs_pending_read_info * reads,int reads_size)1302 int incfs_collect_pending_reads(struct mount_info *mi, int sn_lowerbound,
1303 				struct incfs_pending_read_info *reads,
1304 				int reads_size)
1305 {
1306 	int reported_reads = 0;
1307 	struct pending_read *entry = NULL;
1308 
1309 	if (!mi)
1310 		return -EFAULT;
1311 
1312 	if (reads_size <= 0)
1313 		return 0;
1314 
1315 	mutex_lock(&mi->mi_pending_reads_mutex);
1316 
1317 	if (mi->mi_last_pending_read_number <= sn_lowerbound
1318 	    || mi->mi_pending_reads_count == 0)
1319 		goto unlock;
1320 
1321 	list_for_each_entry(entry, &mi->mi_reads_list_head, mi_reads_list) {
1322 		if (entry->serial_number <= sn_lowerbound)
1323 			continue;
1324 
1325 		reads[reported_reads].file_id = entry->file_id;
1326 		reads[reported_reads].block_index = entry->block_index;
1327 		reads[reported_reads].serial_number = entry->serial_number;
1328 		reads[reported_reads].timestamp_us = entry->timestamp_us;
1329 		/* reads[reported_reads].kind = INCFS_READ_KIND_PENDING; */
1330 
1331 		reported_reads++;
1332 		if (reported_reads >= reads_size)
1333 			break;
1334 	}
1335 
1336 unlock:
1337 	mutex_unlock(&mi->mi_pending_reads_mutex);
1338 
1339 	return reported_reads;
1340 }
1341 
incfs_get_log_state(struct mount_info * mi)1342 struct read_log_state incfs_get_log_state(struct mount_info *mi)
1343 {
1344 	struct read_log *log = &mi->mi_log;
1345 	struct read_log_state result;
1346 
1347 	spin_lock(&log->rl_lock);
1348 	result = log->rl_head;
1349 	spin_unlock(&log->rl_lock);
1350 	return result;
1351 }
1352 
incfs_get_uncollected_logs_count(struct mount_info * mi,const struct read_log_state * state)1353 int incfs_get_uncollected_logs_count(struct mount_info *mi,
1354 				     const struct read_log_state *state)
1355 {
1356 	struct read_log *log = &mi->mi_log;
1357 	u32 generation;
1358 	u64 head_no, tail_no;
1359 
1360 	spin_lock(&log->rl_lock);
1361 	tail_no = log->rl_tail.current_record_no;
1362 	head_no = log->rl_head.current_record_no;
1363 	generation = log->rl_head.generation_id;
1364 	spin_unlock(&log->rl_lock);
1365 
1366 	if (generation != state->generation_id)
1367 		return head_no - tail_no;
1368 	else
1369 		return head_no - max_t(u64, tail_no, state->current_record_no);
1370 }
1371 
incfs_collect_logged_reads(struct mount_info * mi,struct read_log_state * reader_state,struct incfs_pending_read_info * reads,int reads_size)1372 int incfs_collect_logged_reads(struct mount_info *mi,
1373 			       struct read_log_state *reader_state,
1374 			       struct incfs_pending_read_info *reads,
1375 			       int reads_size)
1376 {
1377 	int dst_idx;
1378 	struct read_log *log = &mi->mi_log;
1379 	struct read_log_state *head, *tail;
1380 
1381 	spin_lock(&log->rl_lock);
1382 	head = &log->rl_head;
1383 	tail = &log->rl_tail;
1384 
1385 	if (reader_state->generation_id != head->generation_id) {
1386 		pr_debug("read ptr is wrong generation: %u/%u",
1387 			 reader_state->generation_id, head->generation_id);
1388 
1389 		*reader_state = (struct read_log_state){
1390 			.generation_id = head->generation_id,
1391 		};
1392 	}
1393 
1394 	if (reader_state->current_record_no < tail->current_record_no) {
1395 		pr_debug("read ptr is behind, moving: %u/%u -> %u/%u\n",
1396 			 (u32)reader_state->next_offset,
1397 			 (u32)reader_state->current_pass_no,
1398 			 (u32)tail->next_offset, (u32)tail->current_pass_no);
1399 
1400 		*reader_state = *tail;
1401 	}
1402 
1403 	for (dst_idx = 0; dst_idx < reads_size; dst_idx++) {
1404 		if (reader_state->current_record_no == head->current_record_no)
1405 			break;
1406 
1407 		log_read_one_record(log, reader_state);
1408 
1409 		reads[dst_idx] = (struct incfs_pending_read_info){
1410 			.file_id = reader_state->base_record.file_id,
1411 			.block_index = reader_state->base_record.block_index,
1412 			.serial_number = reader_state->current_record_no,
1413 			.timestamp_us = reader_state->base_record.absolute_ts_us
1414 		};
1415 	}
1416 
1417 	spin_unlock(&log->rl_lock);
1418 	return dst_idx;
1419 }
1420 
incfs_equal_ranges(struct mem_range lhs,struct mem_range rhs)1421 bool incfs_equal_ranges(struct mem_range lhs, struct mem_range rhs)
1422 {
1423 	if (lhs.len != rhs.len)
1424 		return false;
1425 	return memcmp(lhs.data, rhs.data, lhs.len) == 0;
1426 }
1427