int fs_write(const char *key, const void *buffer, unsigned int buf_len) { struct record record; unsigned int key_size = strlen(key) + 1; unsigned int new_record_size = key_size + sizeof(int) + buf_len; int no_error, fatal = 0; struct iter_state is; record_iter_init(&is, STORAGE_ADDRESS, STORAGE_SIZE); while((no_error = record_iter_next(&is, &record, &fatal))); if(fatal) goto fatal_error; if(STORAGE_SIZE - is.seek >= new_record_size) { write_at_offset(key, buffer, buf_len, is.seek); return 1; } if(!try_to_flush_duplicates(key, buf_len)) // storage is full, let's try to free some space up. return 0; // No duplicates found, cannot write the new key-value record: sector is full. // Now retrying to write, hoping enough flash was freed. record_iter_init(&is, STORAGE_ADDRESS, STORAGE_SIZE); while((no_error = record_iter_next(&is, &record, &fatal))); if(fatal) goto fatal_error; if(STORAGE_SIZE - is.seek >= new_record_size) { write_at_offset(key, buffer, buf_len, is.seek); return 1; // We eventually succeeded in writing the record } else return 0; // Storage is definitely full. fatal_error: log("fatal error: flash storage might be corrupted"); return 0; }
static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, bool encrypted, int status_fd) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { ALOGE("failed to remove the existing map file %s: %s", map_file, err.c_str()); return -1; } std::string tmp_map_file = std::string(map_file) + ".tmp"; unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); if (!mapfd) { ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno)); return -1; } // Make sure we can write to the status_file. if (!android::base::WriteStringToFd("0\n", status_fd)) { ALOGE("failed to update \"%s\"\n", STATUS_FILE.c_str()); return -1; } struct stat sb; if (stat(path, &sb) != 0) { ALOGE("failed to stat %s", path); return -1; } ALOGI(" block size: %ld bytes", static_cast<long>(sb.st_blksize)); int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; ALOGI(" file size: %" PRId64 " bytes, %d blocks", sb.st_size, blocks); std::vector<int> ranges; std::string s = android::base::StringPrintf("%s\n%" PRId64 " %ld\n", blk_dev, sb.st_size, static_cast<long>(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd.get())) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } std::vector<std::vector<unsigned char>> buffers; if (encrypted) { buffers.resize(WINDOW_SIZE, std::vector<unsigned char>(sb.st_blksize)); } int head_block = 0; int head = 0, tail = 0; unique_fd fd(open(path, O_RDONLY)); if (!fd) { ALOGE("failed to open %s for reading: %s", path, strerror(errno)); return -1; } unique_fd wfd(-1); if (encrypted) { wfd = open(blk_dev, O_WRONLY); if (!wfd) { ALOGE("failed to open fd for writing: %s", strerror(errno)); return -1; } } off64_t pos = 0; int last_progress = 0; while (pos < sb.st_size) { // Update the status file, progress must be between [0, 99]. int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size))); if (progress > last_progress) { last_progress = progress; android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd); } if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; if (ioctl(fd.get(), FIBMAP, &block) != 0) { ALOGE("failed to find block %d", head_block); return -1; } add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(), static_cast<off64_t>(sb.st_blksize) * block) != 0) { return -1; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } // read next block to tail if (encrypted) { size_t to_read = static_cast<size_t>( std::min(static_cast<off64_t>(sb.st_blksize), sb.st_size - pos)); if (!android::base::ReadFully(fd.get(), buffers[tail].data(), to_read)) { ALOGE("failed to read: %s", strerror(errno)); return -1; } pos += to_read; } else { // If we're not encrypting; we don't need to actually read // anything, just skip pos forward as if we'd read a // block. pos += sb.st_blksize; } tail = (tail+1) % WINDOW_SIZE; } while (head != tail) { // write out head buffer int block = head_block; if (ioctl(fd.get(), FIBMAP, &block) != 0) { ALOGE("failed to find block %d", head_block); return -1; } add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(), static_cast<off64_t>(sb.st_blksize) * block) != 0) { return -1; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } if (!android::base::WriteStringToFd( android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd.get())) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd.get())) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } } if (fsync(mapfd.get()) == -1) { ALOGE("failed to fsync \"%s\": %s", tmp_map_file.c_str(), strerror(errno)); return -1; } if (close(mapfd.get() == -1)) { ALOGE("failed to close %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } mapfd = -1; if (encrypted) { if (fsync(wfd.get()) == -1) { ALOGE("failed to fsync \"%s\": %s", blk_dev, strerror(errno)); return -1; } if (close(wfd.get()) == -1) { ALOGE("failed to close %s: %s", blk_dev, strerror(errno)); return -1; } wfd = -1; } if (rename(tmp_map_file.c_str(), map_file) == -1) { ALOGE("failed to rename %s to %s: %s", tmp_map_file.c_str(), map_file, strerror(errno)); return -1; } // Sync dir to make rename() result written to disk. std::string file_name = map_file; std::string dir_name = dirname(&file_name[0]); unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); if (!dfd) { ALOGE("failed to open dir %s: %s", dir_name.c_str(), strerror(errno)); return -1; } if (fsync(dfd.get()) == -1) { ALOGE("failed to fsync %s: %s", dir_name.c_str(), strerror(errno)); return -1; } if (close(dfd.get() == -1)) { ALOGE("failed to close %s: %s", dir_name.c_str(), strerror(errno)); return -1; } dfd = -1; return 0; }
static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, bool encrypted, int status_fd) { int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); if (mapfd == -1) { ALOGE("failed to open %s\n", map_file); return -1; } FILE* mapf = fdopen(mapfd, "w"); // Make sure we can write to the status_file. if (!android::base::WriteStringToFd("0\n", status_fd)) { ALOGE("failed to update \"%s\"\n", status_file.c_str()); return -1; } struct stat sb; int ret = stat(path, &sb); if (ret != 0) { ALOGE("failed to stat %s\n", path); return -1; } ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); int range_alloc = 1; int range_used = 1; int* ranges = reinterpret_cast<int*>(malloc(range_alloc * 2 * sizeof(int))); ranges[0] = -1; ranges[1] = -1; fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); unsigned char* buffers[WINDOW_SIZE]; if (encrypted) { for (size_t i = 0; i < WINDOW_SIZE; ++i) { buffers[i] = reinterpret_cast<unsigned char*>(malloc(sb.st_blksize)); } } int head_block = 0; int head = 0, tail = 0; size_t pos = 0; int fd = open(path, O_RDONLY); if (fd < 0) { ALOGE("failed to open fd for reading: %s\n", strerror(errno)); return -1; } int wfd = -1; if (encrypted) { wfd = open(blk_dev, O_WRONLY | O_SYNC); if (wfd < 0) { ALOGE("failed to open fd for writing: %s\n", strerror(errno)); return -1; } } int last_progress = 0; while (pos < sb.st_size) { // Update the status file, progress must be between [0, 99]. int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size))); if (progress > last_progress) { last_progress = progress; android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd); } if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; ret = ioctl(fd, FIBMAP, &block); if (ret != 0) { ALOGE("failed to find block %d\n", head_block); return -1; } add_block_to_ranges(&ranges, &range_alloc, &range_used, block); if (encrypted) { if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { return -1; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } // read next block to tail if (encrypted) { size_t so_far = 0; while (so_far < sb.st_blksize && pos < sb.st_size) { ssize_t this_read = TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far)); if (this_read == -1) { ALOGE("failed to read: %s\n", strerror(errno)); return -1; } so_far += this_read; pos += this_read; } } else { // If we're not encrypting; we don't need to actually read // anything, just skip pos forward as if we'd read a // block. pos += sb.st_blksize; } tail = (tail+1) % WINDOW_SIZE; } while (head != tail) { // write out head buffer int block = head_block; ret = ioctl(fd, FIBMAP, &block); if (ret != 0) { ALOGE("failed to find block %d\n", head_block); return -1; } add_block_to_ranges(&ranges, &range_alloc, &range_used, block); if (encrypted) { if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { return -1; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } fprintf(mapf, "%d\n", range_used); for (int i = 0; i < range_used; ++i) { fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); } if (fsync(mapfd) == -1) { ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno)); return -1; } fclose(mapf); close(fd); if (encrypted) { if (fsync(wfd) == -1) { ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno)); return -1; } close(wfd); } return 0; }
static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, bool encrypted, bool f2fs_fs, int socket) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; return kUncryptFileRemoveError; } std::string tmp_map_file = std::string(map_file) + ".tmp"; android::base::unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); if (mapfd == -1) { PLOG(ERROR) << "failed to open " << tmp_map_file; return kUncryptFileOpenError; } // Make sure we can write to the socket. if (!write_status_to_socket(0, socket)) { LOG(ERROR) << "failed to write to socket " << socket; return kUncryptSocketWriteError; } struct stat sb; if (stat(path, &sb) != 0) { LOG(ERROR) << "failed to stat " << path; return kUncryptFileStatError; } LOG(INFO) << " block size: " << sb.st_blksize << " bytes"; int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; LOG(INFO) << " file size: " << sb.st_size << " bytes, " << blocks << " blocks"; std::vector<int> ranges; std::string s = android::base::StringPrintf("%s\n%" PRId64 " %" PRId64 "\n", blk_dev, static_cast<int64_t>(sb.st_size), static_cast<int64_t>(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd)) { PLOG(ERROR) << "failed to write " << tmp_map_file; return kUncryptWriteError; } std::vector<std::vector<unsigned char>> buffers; if (encrypted) { buffers.resize(WINDOW_SIZE, std::vector<unsigned char>(sb.st_blksize)); } int head_block = 0; int head = 0, tail = 0; android::base::unique_fd fd(open(path, O_RDONLY)); if (fd == -1) { PLOG(ERROR) << "failed to open " << path << " for reading"; return kUncryptFileOpenError; } android::base::unique_fd wfd; if (encrypted) { wfd.reset(open(blk_dev, O_WRONLY)); if (wfd == -1) { PLOG(ERROR) << "failed to open " << blk_dev << " for writing"; return kUncryptBlockOpenError; } } // F2FS-specific ioctl // It requires the below kernel commit merged in v4.16-rc1. // 1ad71a27124c ("f2fs: add an ioctl to disable GC for specific file") // In android-4.4, // 56ee1e817908 ("f2fs: updates on v4.16-rc1") // In android-4.9, // 2f17e34672a8 ("f2fs: updates on v4.16-rc1") // In android-4.14, // ce767d9a55bc ("f2fs: updates on v4.16-rc1") #ifndef F2FS_IOC_SET_PIN_FILE #ifndef F2FS_IOCTL_MAGIC #define F2FS_IOCTL_MAGIC 0xf5 #endif #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32) #define F2FS_IOC_GET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 14, __u32) #endif if (f2fs_fs) { int error = ioctl(fd, F2FS_IOC_SET_PIN_FILE); // Don't break the old kernels which don't support it. if (error && errno != ENOTTY && errno != ENOTSUP) { PLOG(ERROR) << "Failed to set pin_file for f2fs: " << path << " on " << blk_dev; return kUncryptIoctlError; } } off64_t pos = 0; int last_progress = 0; while (pos < sb.st_size) { // Update the status file, progress must be between [0, 99]. int progress = static_cast<int>(100 * (double(pos) / double(sb.st_size))); if (progress > last_progress) { last_progress = progress; write_status_to_socket(progress, socket); } if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { PLOG(ERROR) << "failed to find block " << head_block; return kUncryptIoctlError; } if (block == 0) { LOG(ERROR) << "failed to find block " << head_block << ", retrying"; int error = retry_fibmap(fd, path, &block, head_block); if (error != kUncryptNoError) { return error; } } add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, static_cast<off64_t>(sb.st_blksize) * block) != 0) { return kUncryptWriteError; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } // read next block to tail if (encrypted) { size_t to_read = static_cast<size_t>( std::min(static_cast<off64_t>(sb.st_blksize), sb.st_size - pos)); if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) { PLOG(ERROR) << "failed to read " << path; return kUncryptReadError; } pos += to_read; } else { // If we're not encrypting; we don't need to actually read // anything, just skip pos forward as if we'd read a // block. pos += sb.st_blksize; } tail = (tail+1) % WINDOW_SIZE; } while (head != tail) { // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { PLOG(ERROR) << "failed to find block " << head_block; return kUncryptIoctlError; } if (block == 0) { LOG(ERROR) << "failed to find block " << head_block << ", retrying"; int error = retry_fibmap(fd, path, &block, head_block); if (error != kUncryptNoError) { return error; } } add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, static_cast<off64_t>(sb.st_blksize) * block) != 0) { return kUncryptWriteError; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } if (!android::base::WriteStringToFd( android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) { PLOG(ERROR) << "failed to write " << tmp_map_file; return kUncryptWriteError; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) { PLOG(ERROR) << "failed to write " << tmp_map_file; return kUncryptWriteError; } } if (fsync(mapfd) == -1) { PLOG(ERROR) << "failed to fsync \"" << tmp_map_file << "\""; return kUncryptFileSyncError; } if (close(mapfd.release()) == -1) { PLOG(ERROR) << "failed to close " << tmp_map_file; return kUncryptFileCloseError; } if (encrypted) { if (fsync(wfd) == -1) { PLOG(ERROR) << "failed to fsync \"" << blk_dev << "\""; return kUncryptFileSyncError; } if (close(wfd.release()) == -1) { PLOG(ERROR) << "failed to close " << blk_dev; return kUncryptFileCloseError; } } if (rename(tmp_map_file.c_str(), map_file) == -1) { PLOG(ERROR) << "failed to rename " << tmp_map_file << " to " << map_file; return kUncryptFileRenameError; } // Sync dir to make rename() result written to disk. std::string file_name = map_file; std::string dir_name = dirname(&file_name[0]); android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); if (dfd == -1) { PLOG(ERROR) << "failed to open dir " << dir_name; return kUncryptFileOpenError; } if (fsync(dfd) == -1) { PLOG(ERROR) << "failed to fsync " << dir_name; return kUncryptFileSyncError; } if (close(dfd.release()) == -1) { PLOG(ERROR) << "failed to close " << dir_name; return kUncryptFileCloseError; } return 0; }
int produce_block_map(const char* path, const char* map_file, const char* blk_dev, int encrypted) { struct stat sb; int ret; FILE* mapf = fopen(map_file, "w"); ret = stat(path, &sb); if (ret != 0) { ALOGE("failed to stat %s\n", path); return -1; } ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); int* ranges; int range_alloc = 1; int range_used = 1; ranges = malloc(range_alloc * 2 * sizeof(int)); ranges[0] = -1; ranges[1] = -1; fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); unsigned char* buffers[WINDOW_SIZE]; int i; if (encrypted) { for (i = 0; i < WINDOW_SIZE; ++i) { buffers[i] = malloc(sb.st_blksize); } } int head_block = 0; int head = 0, tail = 0; size_t pos = 0; int fd = open(path, O_RDONLY); if (fd < 0) { ALOGE("failed to open fd for reading: %s\n", strerror(errno)); return -1; } fsync(fd); int wfd = -1; if (encrypted) { wfd = open(blk_dev, O_WRONLY); if (wfd < 0) { ALOGE("failed to open fd for writing: %s\n", strerror(errno)); return -1; } } while (pos < sb.st_size) { if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; ret = ioctl(fd, FIBMAP, &block); if (ret != 0) { ALOGE("failed to find block %d\n", head_block); return -1; } add_block_to_ranges(&ranges, &range_alloc, &range_used, block); if (encrypted) { if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { return -1; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } // read next block to tail if (encrypted) { size_t so_far = 0; while (so_far < sb.st_blksize && pos < sb.st_size) { ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far); if (this_read < 0) { ALOGE("failed to read: %s\n", strerror(errno)); return -1; } so_far += this_read; pos += this_read; } } else { // If we're not encrypting; we don't need to actually read // anything, just skip pos forward as if we'd read a // block. pos += sb.st_blksize; } tail = (tail+1) % WINDOW_SIZE; } while (head != tail) { // write out head buffer int block = head_block; ret = ioctl(fd, FIBMAP, &block); if (ret != 0) { ALOGE("failed to find block %d\n", head_block); return -1; } add_block_to_ranges(&ranges, &range_alloc, &range_used, block); if (encrypted) { if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { return -1; } } head = (head + 1) % WINDOW_SIZE; ++head_block; } fprintf(mapf, "%d\n", range_used); for (i = 0; i < range_used; ++i) { fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); } fclose(mapf); close(fd); if (encrypted) { close(wfd); } return 0; }