^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2018 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/falloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "data_mgmt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct backing_file_context *incfs_alloc_bfc(struct mount_info *mi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct file *backing_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct backing_file_context *result = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) result = kzalloc(sizeof(*result), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) result->bc_file = get_file(backing_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) result->bc_cred = mi->mi_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) mutex_init(&result->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void incfs_free_bfc(struct backing_file_context *bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (bfc->bc_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) fput(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) mutex_destroy(&bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) kfree(bfc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static loff_t incfs_get_end_offset(struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * This function assumes that file size and the end-offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * are the same. This is not always true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return i_size_read(file_inode(f));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Truncate the tail of the file to the given length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * Used to rollback partially successful multistep writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int truncate_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) loff_t new_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct dentry *dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) loff_t old_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct iattr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!bfc->bc_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) old_end = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (old_end == new_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (old_end < new_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) inode = bfc->bc_file->f_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dentry = bfc->bc_file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) attr.ia_size = new_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) attr.ia_valid = ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) result = notify_change(dentry, &attr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int write_to_bf(struct backing_file_context *bfc, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) size_t count, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ssize_t res = incfs_kwrite(bfc, buf, count, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (res != count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int append_zeros_no_fallocate(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) size_t file_size, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u8 buffer[256] = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) for (i = 0; i < len; i += sizeof(buffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int to_write = len - i > sizeof(buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ? sizeof(buffer) : len - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int err = write_to_bf(bfc, buffer, to_write, file_size + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Append a given number of zero bytes to the end of the backing file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int append_zeros(struct backing_file_context *bfc, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) loff_t file_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) loff_t new_last_byte_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Allocate only one byte at the new desired end of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * It will increase file size and create a zeroed area of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * a given size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) file_size = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) new_last_byte_offset = file_size + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) result = vfs_fallocate(bfc->bc_file, 0, new_last_byte_offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (result != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return append_zeros_no_fallocate(bfc, file_size, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Append a given metadata record to the backing file and update a previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * record to add the new record the the metadata list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int append_md_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct incfs_md_header *record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) loff_t record_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) loff_t file_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) __le64 new_md_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) size_t record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!bfc || !record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (bfc->bc_last_md_record_offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) record_size = le16_to_cpu(record->h_record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) file_pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) record->h_next_md_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* Write the metadata record to the end of the backing file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) record_offset = file_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) new_md_offset = cpu_to_le64(record_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) result = write_to_bf(bfc, record, record_size, file_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* Update next metadata offset in a previous record or a superblock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (bfc->bc_last_md_record_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Find a place in the previous md record where new record's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * offset needs to be saved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) file_pos = bfc->bc_last_md_record_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) offsetof(struct incfs_md_header, h_next_md_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * No metadata yet, file a place to update in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * file_header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) file_pos = offsetof(struct incfs_file_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fh_first_md_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) result = write_to_bf(bfc, &new_md_offset, sizeof(new_md_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) file_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) bfc->bc_last_md_record_offset = record_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * Reserve 0-filled space for the blockmap body, and append
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * incfs_blockmap metadata record pointing to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int incfs_write_blockmap_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) u32 block_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct incfs_blockmap blockmap = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) loff_t file_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) size_t map_size = block_count * sizeof(struct incfs_blockmap_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) blockmap.m_header.h_md_entry_type = INCFS_MD_BLOCK_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) blockmap.m_header.h_record_size = cpu_to_le16(sizeof(blockmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) blockmap.m_header.h_next_md_offset = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) blockmap.m_block_count = cpu_to_le32(block_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Reserve 0-filled space for the blockmap body in the backing file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) file_end = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) result = append_zeros(bfc, map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* Write blockmap metadata record pointing to the body written above. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) blockmap.m_base_offset = cpu_to_le64(file_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) result = append_md_to_backing_file(bfc, &blockmap.m_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* Error, rollback file changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) truncate_backing_file(bfc, file_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int incfs_write_signature_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct mem_range sig, u32 tree_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) loff_t *tree_offset, loff_t *sig_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct incfs_file_signature sg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) loff_t rollback_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) loff_t tree_area_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) size_t alignment = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) rollback_pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) sg.sg_header.h_md_entry_type = INCFS_MD_SIGNATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) sg.sg_header.h_record_size = cpu_to_le16(sizeof(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) sg.sg_header.h_next_md_offset = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (sig.data != NULL && sig.len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) sg.sg_sig_size = cpu_to_le32(sig.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) sg.sg_sig_offset = cpu_to_le64(rollback_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) result = write_to_bf(bfc, sig.data, sig.len, rollback_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) tree_area_pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (tree_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (tree_size > 5 * INCFS_DATA_FILE_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * If hash tree is big enough, it makes sense to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * align in the backing file for faster access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) loff_t offset = round_up(tree_area_pos, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) alignment = offset - tree_area_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) tree_area_pos = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * If root hash is not the only hash in the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * reserve 0-filled space for the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) result = append_zeros(bfc, tree_size + alignment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) sg.sg_hash_tree_size = cpu_to_le32(tree_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) sg.sg_hash_tree_offset = cpu_to_le64(tree_area_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* Write a hash tree metadata record pointing to the hash tree above. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) result = append_md_to_backing_file(bfc, &sg.sg_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Error, rollback file changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) truncate_backing_file(bfc, rollback_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (tree_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *tree_offset = tree_area_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (sig_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *sig_offset = rollback_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int write_new_status_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) u32 data_blocks_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) u32 hash_blocks_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) loff_t rollback_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct incfs_status is = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .is_header = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .h_md_entry_type = INCFS_MD_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .h_record_size = cpu_to_le16(sizeof(is)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .is_data_blocks_written = cpu_to_le32(data_blocks_written),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .is_hash_blocks_written = cpu_to_le32(hash_blocks_written),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) rollback_pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) result = append_md_to_backing_file(bfc, &is.is_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) truncate_backing_file(bfc, rollback_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int incfs_write_status_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) loff_t status_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) u32 data_blocks_written,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) u32 hash_blocks_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct incfs_status is;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (status_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return write_new_status_to_backing_file(bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) data_blocks_written, hash_blocks_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) result = incfs_kread(bfc, &is, sizeof(is), status_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (result != sizeof(is))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) is.is_data_blocks_written = cpu_to_le32(data_blocks_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) is.is_hash_blocks_written = cpu_to_le32(hash_blocks_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) result = incfs_kwrite(bfc, &is, sizeof(is), status_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (result != sizeof(is))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int incfs_write_verity_signature_to_backing_file(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct backing_file_context *bfc, struct mem_range signature,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct incfs_file_verity_signature vs = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /* No verity signature section is equivalent to an empty section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (signature.data == NULL || signature.len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) vs = (struct incfs_file_verity_signature) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) .vs_header = (struct incfs_md_header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) .h_md_entry_type = INCFS_MD_VERITY_SIGNATURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) .h_record_size = cpu_to_le16(sizeof(vs)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .h_next_md_offset = cpu_to_le64(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .vs_size = cpu_to_le32(signature.len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .vs_offset = cpu_to_le64(pos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) result = write_to_bf(bfc, signature.data, signature.len, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) result = append_md_to_backing_file(bfc, &vs.vs_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) *offset = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* Error, rollback file changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) truncate_backing_file(bfc, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * Write a backing file header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * It should always be called only on empty file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * fh.fh_first_md_offset is 0 for now, but will be updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * once first metadata record is added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int incfs_write_fh_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) incfs_uuid_t *uuid, u64 file_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct incfs_file_header fh = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) loff_t file_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) fh.fh_magic = cpu_to_le64(INCFS_MAGIC_NUMBER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) fh.fh_version = cpu_to_le64(INCFS_FORMAT_CURRENT_VER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) fh.fh_header_size = cpu_to_le16(sizeof(fh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) fh.fh_first_md_offset = cpu_to_le64(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) fh.fh_data_block_size = cpu_to_le16(INCFS_DATA_FILE_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) fh.fh_file_size = cpu_to_le64(file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) fh.fh_uuid = *uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) file_pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (file_pos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return write_to_bf(bfc, &fh, sizeof(fh), file_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * Write a backing file header for a mapping file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * It should always be called only on empty file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int incfs_write_mapping_fh_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) incfs_uuid_t *uuid, u64 file_size, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct incfs_file_header fh = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) loff_t file_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) fh.fh_magic = cpu_to_le64(INCFS_MAGIC_NUMBER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) fh.fh_version = cpu_to_le64(INCFS_FORMAT_CURRENT_VER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) fh.fh_header_size = cpu_to_le16(sizeof(fh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) fh.fh_original_offset = cpu_to_le64(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) fh.fh_data_block_size = cpu_to_le16(INCFS_DATA_FILE_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) fh.fh_mapped_file_size = cpu_to_le64(file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) fh.fh_original_uuid = *uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) fh.fh_flags = cpu_to_le32(INCFS_FILE_MAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) file_pos = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (file_pos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return write_to_bf(bfc, &fh, sizeof(fh), file_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* Write a given data block and update file's blockmap to point it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int incfs_write_data_block_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct mem_range block, int block_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) loff_t bm_base_off, u16 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct incfs_blockmap_entry bm_entry = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) loff_t data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) loff_t bm_entry_off =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) bm_base_off + sizeof(struct incfs_blockmap_entry) * block_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (block.len >= (1 << 16) || block_index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) data_offset = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (data_offset <= bm_entry_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /* Blockmap entry is beyond the file's end. It is not normal. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /* Write the block data at the end of the backing file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) result = write_to_bf(bfc, block.data, block.len, data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /* Update the blockmap to point to the newly written data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) bm_entry.me_data_offset_lo = cpu_to_le32((u32)data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) bm_entry.me_data_offset_hi = cpu_to_le16((u16)(data_offset >> 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) bm_entry.me_data_size = cpu_to_le16((u16)block.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) bm_entry.me_flags = cpu_to_le16(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return write_to_bf(bfc, &bm_entry, sizeof(bm_entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) bm_entry_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int incfs_write_hash_block_to_backing_file(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct mem_range block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int block_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) loff_t hash_area_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) loff_t bm_base_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) loff_t file_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct incfs_blockmap_entry bm_entry = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) loff_t data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) loff_t file_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) loff_t bm_entry_off =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) bm_base_off +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) sizeof(struct incfs_blockmap_entry) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) (block_index + get_blocks_count_for_size(file_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (!bfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) LOCK_REQUIRED(bfc->bc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) data_offset = hash_area_off + block_index * INCFS_DATA_FILE_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) file_end = incfs_get_end_offset(bfc->bc_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (data_offset + block.len > file_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Block is located beyond the file's end. It is not normal. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) result = write_to_bf(bfc, block.data, block.len, data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) bm_entry.me_data_offset_lo = cpu_to_le32((u32)data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) bm_entry.me_data_offset_hi = cpu_to_le16((u16)(data_offset >> 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) bm_entry.me_data_size = cpu_to_le16(INCFS_DATA_FILE_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return write_to_bf(bfc, &bm_entry, sizeof(bm_entry), bm_entry_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) int incfs_read_blockmap_entry(struct backing_file_context *bfc, int block_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) loff_t bm_base_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) struct incfs_blockmap_entry *bm_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) int error = incfs_read_blockmap_entries(bfc, bm_entry, block_index, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) bm_base_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (error != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) int incfs_read_blockmap_entries(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct incfs_blockmap_entry *entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int start_index, int blocks_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) loff_t bm_base_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) loff_t bm_entry_off =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) bm_base_off + sizeof(struct incfs_blockmap_entry) * start_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) const size_t bytes_to_read = sizeof(struct incfs_blockmap_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * blocks_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (!bfc || !entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (start_index < 0 || bm_base_off <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) result = incfs_kread(bfc, entries, bytes_to_read, bm_entry_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return result / sizeof(*entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) int incfs_read_file_header(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) loff_t *first_md_off, incfs_uuid_t *uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) u64 *file_size, u32 *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) ssize_t bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct incfs_file_header fh = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (!bfc || !first_md_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) bytes_read = incfs_kread(bfc, &fh, sizeof(fh), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (bytes_read < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (bytes_read < sizeof(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (le64_to_cpu(fh.fh_magic) != INCFS_MAGIC_NUMBER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (le64_to_cpu(fh.fh_version) > INCFS_FORMAT_CURRENT_VER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (le16_to_cpu(fh.fh_data_block_size) != INCFS_DATA_FILE_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (le16_to_cpu(fh.fh_header_size) != sizeof(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (first_md_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) *first_md_off = le64_to_cpu(fh.fh_first_md_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) *uuid = fh.fh_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (file_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) *file_size = le64_to_cpu(fh.fh_file_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) *flags = le32_to_cpu(fh.fh_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * Read through metadata records from the backing file one by one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * and call provided metadata handlers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) int incfs_read_next_metadata_record(struct backing_file_context *bfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) struct metadata_handler *handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) const ssize_t max_md_size = INCFS_MAX_METADATA_RECORD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ssize_t bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) size_t md_record_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) loff_t next_record = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) struct incfs_md_header *md_hdr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!bfc || !handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (handler->md_record_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) memset(&handler->md_buffer, 0, max_md_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) bytes_read = incfs_kread(bfc, &handler->md_buffer, max_md_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) handler->md_record_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (bytes_read < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) return bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (bytes_read < sizeof(*md_hdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) md_hdr = &handler->md_buffer.md_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) next_record = le64_to_cpu(md_hdr->h_next_md_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) md_record_size = le16_to_cpu(md_hdr->h_record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (md_record_size > max_md_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) pr_warn("incfs: The record is too large. Size: %zu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) md_record_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (bytes_read < md_record_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) pr_warn("incfs: The record hasn't been fully read.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (next_record <= handler->md_record_offset && next_record != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) pr_warn("incfs: Next record (%lld) points back in file.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) next_record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) switch (md_hdr->h_md_entry_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) case INCFS_MD_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) case INCFS_MD_BLOCK_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (handler->handle_blockmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) res = handler->handle_blockmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) &handler->md_buffer.blockmap, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) case INCFS_MD_FILE_ATTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * File attrs no longer supported, ignore section for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) case INCFS_MD_SIGNATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (handler->handle_signature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) res = handler->handle_signature(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) &handler->md_buffer.signature, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) case INCFS_MD_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (handler->handle_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) res = handler->handle_status(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) &handler->md_buffer.status, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) case INCFS_MD_VERITY_SIGNATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (handler->handle_verity_signature)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) res = handler->handle_verity_signature(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) &handler->md_buffer.verity_signature, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) res = -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (next_record == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * Zero offset for the next record means that the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * metadata record has just been processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) bfc->bc_last_md_record_offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) handler->md_record_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) handler->md_prev_record_offset = handler->md_record_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) handler->md_record_offset = next_record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) ssize_t incfs_kread(struct backing_file_context *bfc, void *buf, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) const struct cred *old_cred = override_creds(bfc->bc_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) int ret = kernel_read(bfc->bc_file, buf, size, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) ssize_t incfs_kwrite(struct backing_file_context *bfc, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) size_t size, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) const struct cred *old_cred = override_creds(bfc->bc_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) int ret = kernel_write(bfc->bc_file, buf, size, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }