blob: 87c59a551655de31402d0f468058a8ad8cb5ab5e [file] [log] [blame]
Paul Crowleyf71ace32016-06-02 11:01:19 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "EncryptInplace.h"
18
Paul Crowleyf71ace32016-06-02 11:01:19 -070019#include <ext4_utils/ext4.h>
20#include <ext4_utils/ext4_utils.h>
21#include <f2fs_sparseblock.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070022#include <fcntl.h>
23#include <inttypes.h>
24#include <stdint.h>
25#include <stdio.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070029
30#include <algorithm>
31
Paul Crowley772cc852018-02-01 09:53:27 -080032#include <android-base/logging.h>
33#include <android-base/properties.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070034
35// HORRIBLE HACK, FIXME
36#include "cryptfs.h"
37
38// FIXME horrible cut-and-paste code
Paul Crowley14c8c072018-09-18 13:30:21 -070039static inline int unix_read(int fd, void* buff, int len) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070040 return TEMP_FAILURE_RETRY(read(fd, buff, len));
41}
42
Paul Crowley14c8c072018-09-18 13:30:21 -070043static inline int unix_write(int fd, const void* buff, int len) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070044 return TEMP_FAILURE_RETRY(write(fd, buff, len));
45}
46
47#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
48
49/* aligned 32K writes tends to make flash happy.
50 * SD card association recommends it.
51 */
52#ifndef CONFIG_HW_DISK_ENCRYPTION
53#define BLOCKS_AT_A_TIME 8
54#else
55#define BLOCKS_AT_A_TIME 1024
56#endif
57
Paul Crowley14c8c072018-09-18 13:30:21 -070058struct encryptGroupsData {
Paul Crowleyf71ace32016-06-02 11:01:19 -070059 int realfd;
60 int cryptofd;
61 off64_t numblocks;
62 off64_t one_pct, cur_pct, new_pct;
63 off64_t blocks_already_done, tot_numblocks;
64 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrencef376a012019-06-25 14:44:33 -070065 const char* real_blkdev;
66 const char* crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -070067 int count;
68 off64_t offset;
69 char* buffer;
70 off64_t last_written_sector;
71 int completed;
72 time_t time_started;
73 int remaining_time;
Paul Crowley0fd26262018-01-30 09:48:19 -080074 bool set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -070075};
76
Paul Crowley14c8c072018-09-18 13:30:21 -070077static void update_progress(struct encryptGroupsData* data, int is_used) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070078 data->blocks_already_done++;
79
80 if (is_used) {
81 data->used_blocks_already_done++;
82 }
83 if (data->tot_used_blocks) {
84 data->new_pct = data->used_blocks_already_done / data->one_pct;
85 } else {
86 data->new_pct = data->blocks_already_done / data->one_pct;
87 }
88
Paul Crowley0fd26262018-01-30 09:48:19 -080089 if (!data->set_progress_properties) return;
90
Paul Crowleyf71ace32016-06-02 11:01:19 -070091 if (data->new_pct > data->cur_pct) {
92 char buf[8];
93 data->cur_pct = data->new_pct;
94 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Crowley772cc852018-02-01 09:53:27 -080095 android::base::SetProperty("vold.encrypt_progress", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -070096 }
97
98 if (data->cur_pct >= 5) {
99 struct timespec time_now;
100 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800101 LOG(WARNING) << "Error getting time";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700102 } else {
103 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
Paul Crowley14c8c072018-09-18 13:30:21 -0700104 off64_t remaining_blocks = data->tot_used_blocks - data->used_blocks_already_done;
105 int remaining_time =
106 (int)(elapsed_time * remaining_blocks / data->used_blocks_already_done);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700107
108 // Change time only if not yet set, lower, or a lot higher for
109 // best user experience
Paul Crowley14c8c072018-09-18 13:30:21 -0700110 if (data->remaining_time == -1 || remaining_time < data->remaining_time ||
111 remaining_time > data->remaining_time + 60) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700112 char buf[8];
113 snprintf(buf, sizeof(buf), "%d", remaining_time);
Paul Crowley772cc852018-02-01 09:53:27 -0800114 android::base::SetProperty("vold.encrypt_time_remaining", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700115 data->remaining_time = remaining_time;
116 }
117 }
118 }
119}
120
Paul Crowley14c8c072018-09-18 13:30:21 -0700121static void log_progress(struct encryptGroupsData const* data, bool completed) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700122 // Precondition - if completed data = 0 else data != 0
123
124 // Track progress so we can skip logging blocks
125 static off64_t offset = -1;
126
127 // Need to close existing 'Encrypting from' log?
128 if (completed || (offset != -1 && data->offset != offset)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800129 LOG(INFO) << "Encrypted to sector " << offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700130 offset = -1;
131 }
132
133 // Need to start new 'Encrypting from' log?
134 if (!completed && offset != data->offset) {
Paul Crowley772cc852018-02-01 09:53:27 -0800135 LOG(INFO) << "Encrypting from sector " << data->offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700136 }
137
138 // Update offset
139 if (!completed) {
140 offset = data->offset + (off64_t)data->count * info.block_size;
141 }
142}
143
Paul Crowley14c8c072018-09-18 13:30:21 -0700144static int flush_outstanding_data(struct encryptGroupsData* data) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700145 if (data->count == 0) {
146 return 0;
147 }
148
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700149 LOG(DEBUG) << "Copying " << data->count << " blocks at offset " << data->offset;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700150
Paul Crowley772cc852018-02-01 09:53:27 -0800151 if (pread64(data->realfd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
152 LOG(ERROR) << "Error reading real_blkdev " << data->real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700153 return -1;
154 }
155
Paul Crowley772cc852018-02-01 09:53:27 -0800156 if (pwrite64(data->cryptofd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
157 LOG(ERROR) << "Error writing crypto_blkdev " << data->crypto_blkdev
158 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700159 return -1;
160 } else {
Paul Crowley14c8c072018-09-18 13:30:21 -0700161 log_progress(data, false);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700162 }
163
164 data->count = 0;
Paul Crowley14c8c072018-09-18 13:30:21 -0700165 data->last_written_sector =
166 (data->offset + data->count) / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700167 return 0;
168}
169
Paul Crowley14c8c072018-09-18 13:30:21 -0700170static int encrypt_groups(struct encryptGroupsData* data) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700171 unsigned int i;
Paul Crowley14c8c072018-09-18 13:30:21 -0700172 u8* block_bitmap = 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700173 unsigned int block;
174 off64_t ret;
175 int rc = -1;
176
Paul Crowley14c8c072018-09-18 13:30:21 -0700177 data->buffer = (char*)malloc(info.block_size * BLOCKS_AT_A_TIME);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700178 if (!data->buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800179 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700180 goto errout;
181 }
182
Paul Crowley14c8c072018-09-18 13:30:21 -0700183 block_bitmap = (u8*)malloc(info.block_size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700184 if (!block_bitmap) {
Paul Crowley772cc852018-02-01 09:53:27 -0800185 LOG(ERROR) << "failed to allocate block bitmap";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700186 goto errout;
187 }
188
189 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley772cc852018-02-01 09:53:27 -0800190 LOG(INFO) << "Encrypting group " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700191
192 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
Paul Crowley14c8c072018-09-18 13:30:21 -0700193 u32 block_count = std::min(info.blocks_per_group, (u32)(aux_info.len_blocks - first_block));
Paul Crowleyf71ace32016-06-02 11:01:19 -0700194
Paul Crowley14c8c072018-09-18 13:30:21 -0700195 off64_t offset = (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700196
197 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
198 if (ret != (int)info.block_size) {
Paul Crowley772cc852018-02-01 09:53:27 -0800199 LOG(ERROR) << "failed to read all of block group bitmap " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700200 goto errout;
201 }
202
203 offset = (u64)info.block_size * first_block;
204
205 data->count = 0;
206
207 for (block = 0; block < block_count; block++) {
Will Shiu3aa4dc72020-07-29 17:03:17 +0800208 int used;
209
210 if (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
211 // In block groups with an uninitialized block bitmap, we only
212 // need to encrypt the backup superblock (if one is present).
213 used = (ext4_bg_has_super_block(i) && block < 1 + aux_info.bg_desc_blocks);
214 } else {
215 used = bitmap_get_bit(block_bitmap, block);
216 }
217
Paul Crowleyf71ace32016-06-02 11:01:19 -0700218 update_progress(data, used);
219 if (used) {
220 if (data->count == 0) {
221 data->offset = offset;
222 }
223 data->count++;
224 } else {
225 if (flush_outstanding_data(data)) {
226 goto errout;
227 }
228 }
229
230 offset += info.block_size;
231
232 /* Write data if we are aligned or buffer size reached */
Paul Crowley14c8c072018-09-18 13:30:21 -0700233 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0 ||
234 data->count == BLOCKS_AT_A_TIME) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700235 if (flush_outstanding_data(data)) {
236 goto errout;
237 }
238 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700239 }
240 if (flush_outstanding_data(data)) {
241 goto errout;
242 }
243 }
244
245 data->completed = 1;
246 rc = 0;
247
248errout:
249 log_progress(0, true);
250 free(data->buffer);
251 free(block_bitmap);
252 return rc;
253}
254
Paul Lawrencef376a012019-06-25 14:44:33 -0700255static int cryptfs_enable_inplace_ext4(const char* crypto_blkdev, const char* real_blkdev,
256 off64_t size, off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800257 off64_t previously_encrypted_upto,
258 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700259 u32 i;
260 struct encryptGroupsData data;
Paul Crowley14c8c072018-09-18 13:30:21 -0700261 int rc; // Can't initialize without causing warning -Wclobbered
Paul Crowleyf71ace32016-06-02 11:01:19 -0700262 int retries = RETRY_MOUNT_ATTEMPTS;
263 struct timespec time_started = {0};
264
265 if (previously_encrypted_upto > *size_already_done) {
Paul Crowley772cc852018-02-01 09:53:27 -0800266 LOG(DEBUG) << "Not fast encrypting since resuming part way through";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700267 return -1;
268 }
269
270 memset(&data, 0, sizeof(data));
271 data.real_blkdev = real_blkdev;
272 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800273 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700274
Paul Crowley0fd26262018-01-30 09:48:19 -0800275 LOG(DEBUG) << "Opening" << real_blkdev;
Paul Crowley14c8c072018-09-18 13:30:21 -0700276 if ((data.realfd = open(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800277 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700278 rc = -1;
279 goto errout;
280 }
281
Paul Crowley0fd26262018-01-30 09:48:19 -0800282 LOG(DEBUG) << "Opening" << crypto_blkdev;
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530283 if (!strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev)))
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530284 data.cryptofd = data.realfd;
285 else {
286 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
287 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
288 if (--retries) {
289 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
290 << " for ext4 inplace encrypt. err=" << errno
291 << "(" << strerror(errno) << "), retrying";
292 sleep(RETRY_MOUNT_DELAY_SECONDS);
293 } else {
294 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
295 << " for ext4 inplace encrypt. err=" << errno
296 << "(" << strerror(errno) << "), retrying";
297 rc = ENABLE_INPLACE_ERR_DEV;
298 goto errout;
299 }
300 }
301 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700302
Paul Crowley14c8c072018-09-18 13:30:21 -0700303 if (setjmp(setjmp_env)) { // NOLINT
Paul Crowley772cc852018-02-01 09:53:27 -0800304 LOG(ERROR) << "Reading ext4 extent caused an exception";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700305 rc = -1;
306 goto errout;
307 }
308
309 if (read_ext(data.realfd, 0) != 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800310 LOG(ERROR) << "Failed to read ext4 extent";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700311 rc = -1;
312 goto errout;
313 }
314
315 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
316 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
317 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
318
Paul Crowley772cc852018-02-01 09:53:27 -0800319 LOG(INFO) << "Encrypting ext4 filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700320
321 data.tot_used_blocks = data.numblocks;
322 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700323 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700324 }
325
326 data.one_pct = data.tot_used_blocks / 100;
327 data.cur_pct = 0;
328
329 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800330 LOG(WARNING) << "Error getting time at start";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700331 // Note - continue anyway - we'll run with 0
332 }
333 data.time_started = time_started.tv_sec;
334 data.remaining_time = -1;
335
336 rc = encrypt_groups(&data);
337 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800338 LOG(ERROR) << "Error encrypting groups";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700339 goto errout;
340 }
341
342 *size_already_done += data.completed ? size : data.last_written_sector;
343 rc = 0;
344
345errout:
346 close(data.realfd);
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530347 if (strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev)))
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530348 close(data.cryptofd);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700349
350 return rc;
351}
352
Paul Crowley14c8c072018-09-18 13:30:21 -0700353static void log_progress_f2fs(u64 block, bool completed) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700354 // Precondition - if completed data = 0 else data != 0
355
356 // Track progress so we can skip logging blocks
357 static u64 last_block = (u64)-1;
358
359 // Need to close existing 'Encrypting from' log?
360 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800361 LOG(INFO) << "Encrypted to block " << last_block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700362 last_block = -1;
363 }
364
365 // Need to start new 'Encrypting from' log?
366 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800367 LOG(INFO) << "Encrypting from block " << block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700368 }
369
370 // Update offset
371 if (!completed) {
372 last_block = block;
373 }
374}
375
Paul Crowley14c8c072018-09-18 13:30:21 -0700376static int encrypt_one_block_f2fs(u64 pos, void* data) {
377 struct encryptGroupsData* priv_dat = (struct encryptGroupsData*)data;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700378
379 priv_dat->blocks_already_done = pos - 1;
380 update_progress(priv_dat, 1);
381
382 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
383
384 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800385 LOG(ERROR) << "Error reading real_blkdev " << priv_dat->crypto_blkdev
386 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700387 return -1;
388 }
389
390 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800391 LOG(ERROR) << "Error writing crypto_blkdev " << priv_dat->crypto_blkdev
392 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700393 return -1;
394 } else {
395 log_progress_f2fs(pos, false);
396 }
397
398 return 0;
399}
400
Paul Lawrencef376a012019-06-25 14:44:33 -0700401static int cryptfs_enable_inplace_f2fs(const char* crypto_blkdev, const char* real_blkdev,
402 off64_t size, off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800403 off64_t previously_encrypted_upto,
404 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700405 struct encryptGroupsData data;
Paul Crowley14c8c072018-09-18 13:30:21 -0700406 struct f2fs_info* f2fs_info = NULL;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700407 int rc = ENABLE_INPLACE_ERR_OTHER;
Denis Hsu1740eff2019-12-26 16:06:37 +0800408 struct timespec time_started = {0};
409
Paul Crowleyf71ace32016-06-02 11:01:19 -0700410 if (previously_encrypted_upto > *size_already_done) {
Paul Crowley772cc852018-02-01 09:53:27 -0800411 LOG(DEBUG) << "Not fast encrypting since resuming part way through";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700412 return ENABLE_INPLACE_ERR_OTHER;
413 }
414 memset(&data, 0, sizeof(data));
415 data.real_blkdev = real_blkdev;
416 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800417 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700418 data.realfd = -1;
419 data.cryptofd = -1;
Paul Crowley14c8c072018-09-18 13:30:21 -0700420 if ((data.realfd = open64(real_blkdev, O_RDWR | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800421 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700422 goto errout;
423 }
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530424 if (!strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev)))
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530425 data.cryptofd = data.realfd;
426 else {
427 if ((data.cryptofd = open64(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
428 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
429 << " for f2fs inplace encrypt. err=" << errno
430 << "(" << strerror(errno) << "), retrying";
431 rc = ENABLE_INPLACE_ERR_DEV;
432 goto errout;
433 }
434 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700435
436 f2fs_info = generate_f2fs_info(data.realfd);
Paul Crowley14c8c072018-09-18 13:30:21 -0700437 if (!f2fs_info) goto errout;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700438
439 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
440 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
441 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
442
443 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
444
445 data.one_pct = data.tot_used_blocks / 100;
446 data.cur_pct = 0;
Denis Hsu1740eff2019-12-26 16:06:37 +0800447 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
448 LOG(WARNING) << "Error getting time at start";
449 // Note - continue anyway - we'll run with 0
450 }
451 data.time_started = time_started.tv_sec;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700452 data.remaining_time = -1;
453
Denis Hsu1740eff2019-12-26 16:06:37 +0800454
Paul Crowley14c8c072018-09-18 13:30:21 -0700455 data.buffer = (char*)malloc(f2fs_info->block_size);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700456 if (!data.buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800457 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700458 goto errout;
459 }
460
461 data.count = 0;
462
463 /* Currently, this either runs to completion, or hits a nonrecoverable error */
464 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
465
466 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800467 LOG(ERROR) << "Error in running over f2fs blocks";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700468 rc = ENABLE_INPLACE_ERR_OTHER;
469 goto errout;
470 }
471
472 *size_already_done += size;
473 rc = 0;
474
475errout:
Paul Crowley772cc852018-02-01 09:53:27 -0800476 if (rc) LOG(ERROR) << "Failed to encrypt f2fs filesystem on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700477
478 log_progress_f2fs(0, true);
479 free(f2fs_info);
480 free(data.buffer);
481 close(data.realfd);
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530482 if (strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev)))
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530483 close(data.cryptofd);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700484
485 return rc;
486}
487
Paul Lawrencef376a012019-06-25 14:44:33 -0700488static int cryptfs_enable_inplace_full(const char* crypto_blkdev, const char* real_blkdev,
489 off64_t size, off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800490 off64_t previously_encrypted_upto,
491 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700492 int realfd, cryptofd;
Paul Crowley14c8c072018-09-18 13:30:21 -0700493 char* buf[CRYPT_INPLACE_BUFSIZE];
Paul Crowleyf71ace32016-06-02 11:01:19 -0700494 int rc = ENABLE_INPLACE_ERR_OTHER;
495 off64_t numblocks, i, remainder;
496 off64_t one_pct, cur_pct, new_pct;
497 off64_t blocks_already_done, tot_numblocks;
498
Paul Crowley14c8c072018-09-18 13:30:21 -0700499 if ((realfd = open(real_blkdev, O_RDONLY | O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800500 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700501 return ENABLE_INPLACE_ERR_OTHER;
502 }
503
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530504 if (!strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev)))
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530505 cryptofd = realfd;
506 else {
507 if ((cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
508 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
509 << " for inplace encrypt. err=" << errno
510 << "(" << strerror(errno) << "), retrying";
511 close(realfd);
512 return ENABLE_INPLACE_ERR_DEV;
513 }
514 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700515
516 /* This is pretty much a simple loop of reading 4K, and writing 4K.
517 * The size passed in is the number of 512 byte sectors in the filesystem.
518 * So compute the number of whole 4K blocks we should read/write,
519 * and the remainder.
520 */
521 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
522 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
523 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
524 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
525
Paul Crowley772cc852018-02-01 09:53:27 -0800526 LOG(ERROR) << "Encrypting filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700527
528 i = previously_encrypted_upto + 1 - *size_already_done;
529
530 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800531 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700532 goto errout;
533 }
534
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530535 if (strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev))) {
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530536 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
537 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << crypto_blkdev;
538 goto errout;
539 }
540 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700541
Paul Crowley14c8c072018-09-18 13:30:21 -0700542 for (; i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700543 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800544 PLOG(ERROR) << "Error reading initial sectors from real_blkdev " << real_blkdev
545 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700546 goto errout;
547 }
548 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800549 PLOG(ERROR) << "Error writing initial sectors to crypto_blkdev " << crypto_blkdev
550 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700551 goto errout;
552 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800553 LOG(INFO) << "Encrypted 1 block at " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700554 }
555 }
556
557 one_pct = tot_numblocks / 100;
558 cur_pct = 0;
559 /* process the majority of the filesystem in blocks */
Paul Crowley14c8c072018-09-18 13:30:21 -0700560 for (i /= CRYPT_SECTORS_PER_BUFSIZE; i < numblocks; i++) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700561 new_pct = (i + blocks_already_done) / one_pct;
Paul Crowley0fd26262018-01-30 09:48:19 -0800562 if (set_progress_properties && new_pct > cur_pct) {
Greg Kaiser110acfb2018-12-18 12:34:07 -0800563 char property_buf[8];
Paul Crowleyf71ace32016-06-02 11:01:19 -0700564
565 cur_pct = new_pct;
Greg Kaiser110acfb2018-12-18 12:34:07 -0800566 snprintf(property_buf, sizeof(property_buf), "%" PRId64, cur_pct);
567 android::base::SetProperty("vold.encrypt_progress", property_buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700568 }
569 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800570 PLOG(ERROR) << "Error reading real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700571 goto errout;
572 }
573 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800574 PLOG(ERROR) << "Error writing crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700575 goto errout;
576 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800577 LOG(DEBUG) << "Encrypted " << CRYPT_SECTORS_PER_BUFSIZE << " block at "
578 << i * CRYPT_SECTORS_PER_BUFSIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700579 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700580 }
581
582 /* Do any remaining sectors */
Paul Crowley14c8c072018-09-18 13:30:21 -0700583 for (i = 0; i < remainder; i++) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700584 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800585 LOG(ERROR) << "Error reading final sectors from real_blkdev " << real_blkdev
586 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700587 goto errout;
588 }
589 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800590 LOG(ERROR) << "Error writing final sectors to crypto_blkdev " << crypto_blkdev
591 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700592 goto errout;
593 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800594 LOG(INFO) << "Encrypted 1 block at next location";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700595 }
596 }
597
598 *size_already_done += size;
599 rc = 0;
600
601errout:
602 close(realfd);
Neeraj Soni2d8016b2019-02-04 10:17:02 +0530603 if (strncmp(real_blkdev, crypto_blkdev, strlen(real_blkdev)))
AnilKumar Chimatad08106a2018-02-11 17:11:24 +0530604 close(cryptofd);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700605
606 return rc;
607}
608
609/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrencef376a012019-06-25 14:44:33 -0700610int cryptfs_enable_inplace(const char* crypto_blkdev, const char* real_blkdev, off64_t size,
Paul Crowley772cc852018-02-01 09:53:27 -0800611 off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800612 off64_t previously_encrypted_upto, bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700613 int rc_ext4, rc_f2fs, rc_full;
Paul Crowley0fd26262018-01-30 09:48:19 -0800614 LOG(DEBUG) << "cryptfs_enable_inplace(" << crypto_blkdev << ", " << real_blkdev << ", " << size
615 << ", " << size_already_done << ", " << tot_size << ", " << previously_encrypted_upto
616 << ", " << set_progress_properties << ")";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700617 if (previously_encrypted_upto) {
Paul Crowley772cc852018-02-01 09:53:27 -0800618 LOG(DEBUG) << "Continuing encryption from " << previously_encrypted_upto;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700619 }
620
621 if (*size_already_done + size < previously_encrypted_upto) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800622 LOG(DEBUG) << "cryptfs_enable_inplace already done";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700623 *size_already_done += size;
624 return 0;
625 }
626
627 /* TODO: identify filesystem type.
628 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
629 * then we will drop down to cryptfs_enable_inplace_f2fs.
630 * */
Paul Crowley0fd26262018-01-30 09:48:19 -0800631 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, size, size_already_done,
632 tot_size, previously_encrypted_upto,
633 set_progress_properties)) == 0) {
634 LOG(DEBUG) << "cryptfs_enable_inplace_ext4 success";
635 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700636 }
Paul Crowley772cc852018-02-01 09:53:27 -0800637 LOG(DEBUG) << "cryptfs_enable_inplace_ext4()=" << rc_ext4;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700638
Paul Crowley0fd26262018-01-30 09:48:19 -0800639 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, size, size_already_done,
640 tot_size, previously_encrypted_upto,
641 set_progress_properties)) == 0) {
642 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs success";
643 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700644 }
Paul Crowley772cc852018-02-01 09:53:27 -0800645 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs()=" << rc_f2fs;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700646
Paul Crowley0fd26262018-01-30 09:48:19 -0800647 rc_full =
648 cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, size, size_already_done, tot_size,
649 previously_encrypted_upto, set_progress_properties);
Paul Crowley772cc852018-02-01 09:53:27 -0800650 LOG(DEBUG) << "cryptfs_enable_inplace_full()=" << rc_full;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700651
652 /* Hack for b/17898962, the following is the symptom... */
Paul Crowley14c8c072018-09-18 13:30:21 -0700653 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV && rc_f2fs == ENABLE_INPLACE_ERR_DEV &&
654 rc_full == ENABLE_INPLACE_ERR_DEV) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800655 LOG(DEBUG) << "ENABLE_INPLACE_ERR_DEV";
656 return ENABLE_INPLACE_ERR_DEV;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700657 }
658 return rc_full;
659}