blob: d46d23ed91deb6dac34d3acdb2d3698b9bc537eb [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
19#include <stdio.h>
20#include <stdint.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070021#include <inttypes.h>
22#include <time.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <ext4_utils/ext4.h>
27#include <ext4_utils/ext4_utils.h>
28#include <f2fs_sparseblock.h>
29
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
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +053035#ifdef CONFIG_HW_DISK_ENCRYPTION
36#include "cryptfs_hw.h"
37#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -070038// HORRIBLE HACK, FIXME
39#include "cryptfs.h"
40
41// FIXME horrible cut-and-paste code
42static inline int unix_read(int fd, void* buff, int len)
43{
44 return TEMP_FAILURE_RETRY(read(fd, buff, len));
45}
46
47static inline int unix_write(int fd, const void* buff, int len)
48{
49 return TEMP_FAILURE_RETRY(write(fd, buff, len));
50}
51
52#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
53
54/* aligned 32K writes tends to make flash happy.
55 * SD card association recommends it.
56 */
57#ifndef CONFIG_HW_DISK_ENCRYPTION
58#define BLOCKS_AT_A_TIME 8
59#else
60#define BLOCKS_AT_A_TIME 1024
61#endif
62
63struct encryptGroupsData
64{
65 int realfd;
66 int cryptofd;
67 off64_t numblocks;
68 off64_t one_pct, cur_pct, new_pct;
69 off64_t blocks_already_done, tot_numblocks;
70 off64_t used_blocks_already_done, tot_used_blocks;
71 char* real_blkdev, * crypto_blkdev;
72 int count;
73 off64_t offset;
74 char* buffer;
75 off64_t last_written_sector;
76 int completed;
77 time_t time_started;
78 int remaining_time;
Paul Crowley0fd26262018-01-30 09:48:19 -080079 bool set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -070080};
81
82static void update_progress(struct encryptGroupsData* data, int is_used)
83{
84 data->blocks_already_done++;
85
86 if (is_used) {
87 data->used_blocks_already_done++;
88 }
89 if (data->tot_used_blocks) {
90 data->new_pct = data->used_blocks_already_done / data->one_pct;
91 } else {
92 data->new_pct = data->blocks_already_done / data->one_pct;
93 }
94
Paul Crowley0fd26262018-01-30 09:48:19 -080095 if (!data->set_progress_properties) return;
96
Paul Crowleyf71ace32016-06-02 11:01:19 -070097 if (data->new_pct > data->cur_pct) {
98 char buf[8];
99 data->cur_pct = data->new_pct;
100 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Crowley772cc852018-02-01 09:53:27 -0800101 android::base::SetProperty("vold.encrypt_progress", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700102 }
103
104 if (data->cur_pct >= 5) {
105 struct timespec time_now;
106 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800107 LOG(WARNING) << "Error getting time";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700108 } else {
109 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
110 off64_t remaining_blocks = data->tot_used_blocks
111 - data->used_blocks_already_done;
112 int remaining_time = (int)(elapsed_time * remaining_blocks
113 / data->used_blocks_already_done);
114
115 // Change time only if not yet set, lower, or a lot higher for
116 // best user experience
117 if (data->remaining_time == -1
118 || remaining_time < data->remaining_time
119 || remaining_time > data->remaining_time + 60) {
120 char buf[8];
121 snprintf(buf, sizeof(buf), "%d", remaining_time);
Paul Crowley772cc852018-02-01 09:53:27 -0800122 android::base::SetProperty("vold.encrypt_time_remaining", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700123 data->remaining_time = remaining_time;
124 }
125 }
126 }
127}
128
129static void log_progress(struct encryptGroupsData const* data, bool completed)
130{
131 // Precondition - if completed data = 0 else data != 0
132
133 // Track progress so we can skip logging blocks
134 static off64_t offset = -1;
135
136 // Need to close existing 'Encrypting from' log?
137 if (completed || (offset != -1 && data->offset != offset)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800138 LOG(INFO) << "Encrypted to sector " << offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700139 offset = -1;
140 }
141
142 // Need to start new 'Encrypting from' log?
143 if (!completed && offset != data->offset) {
Paul Crowley772cc852018-02-01 09:53:27 -0800144 LOG(INFO) << "Encrypting from sector " << data->offset / info.block_size * CRYPT_SECTOR_SIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700145 }
146
147 // Update offset
148 if (!completed) {
149 offset = data->offset + (off64_t)data->count * info.block_size;
150 }
151}
152
153static int flush_outstanding_data(struct encryptGroupsData* data)
154{
155 if (data->count == 0) {
156 return 0;
157 }
158
Paul Crowley772cc852018-02-01 09:53:27 -0800159 LOG(VERBOSE) << "Copying " << data->count << " blocks at offset " << data->offset;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700160
Paul Crowley772cc852018-02-01 09:53:27 -0800161 if (pread64(data->realfd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
162 LOG(ERROR) << "Error reading real_blkdev " << data->real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700163 return -1;
164 }
165
Paul Crowley772cc852018-02-01 09:53:27 -0800166 if (pwrite64(data->cryptofd, data->buffer, info.block_size * data->count, data->offset) <= 0) {
167 LOG(ERROR) << "Error writing crypto_blkdev " << data->crypto_blkdev
168 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700169 return -1;
170 } else {
171 log_progress(data, false);
172 }
173
174 data->count = 0;
175 data->last_written_sector = (data->offset + data->count)
176 / info.block_size * CRYPT_SECTOR_SIZE - 1;
177 return 0;
178}
179
180static int encrypt_groups(struct encryptGroupsData* data)
181{
182 unsigned int i;
183 u8 *block_bitmap = 0;
184 unsigned int block;
185 off64_t ret;
186 int rc = -1;
187
188 data->buffer = (char*) malloc(info.block_size * BLOCKS_AT_A_TIME);
189 if (!data->buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800190 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700191 goto errout;
192 }
193
194 block_bitmap = (u8*) malloc(info.block_size);
195 if (!block_bitmap) {
Paul Crowley772cc852018-02-01 09:53:27 -0800196 LOG(ERROR) << "failed to allocate block bitmap";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700197 goto errout;
198 }
199
200 for (i = 0; i < aux_info.groups; ++i) {
Paul Crowley772cc852018-02-01 09:53:27 -0800201 LOG(INFO) << "Encrypting group " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700202
203 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
204 u32 block_count = std::min(info.blocks_per_group,
205 (u32)(aux_info.len_blocks - first_block));
206
207 off64_t offset = (u64)info.block_size
208 * aux_info.bg_desc[i].bg_block_bitmap;
209
210 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
211 if (ret != (int)info.block_size) {
Paul Crowley772cc852018-02-01 09:53:27 -0800212 LOG(ERROR) << "failed to read all of block group bitmap " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700213 goto errout;
214 }
215
216 offset = (u64)info.block_size * first_block;
217
218 data->count = 0;
219
220 for (block = 0; block < block_count; block++) {
221 int used = (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) ?
222 0 : bitmap_get_bit(block_bitmap, block);
223 update_progress(data, used);
224 if (used) {
225 if (data->count == 0) {
226 data->offset = offset;
227 }
228 data->count++;
229 } else {
230 if (flush_outstanding_data(data)) {
231 goto errout;
232 }
233 }
234
235 offset += info.block_size;
236
237 /* Write data if we are aligned or buffer size reached */
238 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
239 || data->count == BLOCKS_AT_A_TIME) {
240 if (flush_outstanding_data(data)) {
241 goto errout;
242 }
243 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700244 }
245 if (flush_outstanding_data(data)) {
246 goto errout;
247 }
248 }
249
250 data->completed = 1;
251 rc = 0;
252
253errout:
254 log_progress(0, true);
255 free(data->buffer);
256 free(block_bitmap);
257 return rc;
258}
259
Paul Crowley772cc852018-02-01 09:53:27 -0800260static int cryptfs_enable_inplace_ext4(char* crypto_blkdev, char* real_blkdev, off64_t size,
261 off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800262 off64_t previously_encrypted_upto,
263 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700264 u32 i;
265 struct encryptGroupsData data;
266 int rc; // Can't initialize without causing warning -Wclobbered
267 int retries = RETRY_MOUNT_ATTEMPTS;
268 struct timespec time_started = {0};
269
270 if (previously_encrypted_upto > *size_already_done) {
Paul Crowley772cc852018-02-01 09:53:27 -0800271 LOG(DEBUG) << "Not fast encrypting since resuming part way through";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700272 return -1;
273 }
274
275 memset(&data, 0, sizeof(data));
276 data.real_blkdev = real_blkdev;
277 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800278 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700279
Paul Crowley0fd26262018-01-30 09:48:19 -0800280 LOG(DEBUG) << "Opening" << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700281 if ( (data.realfd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800282 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700283 rc = -1;
284 goto errout;
285 }
286
Paul Crowley0fd26262018-01-30 09:48:19 -0800287 LOG(DEBUG) << "Opening" << crypto_blkdev;
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530288#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
289 if (is_ice_enabled())
290 data.cryptofd = data.realfd;
291 else {
292 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
293 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
294 if (--retries) {
295 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
296 << " for ext4 inplace encrypt. err=" << errno
297 << "(" << strerror(errno) << "), retrying";
298 sleep(RETRY_MOUNT_DELAY_SECONDS);
299 } else {
300 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
301 << " for ext4 inplace encrypt. err=" << errno
302 << "(" << strerror(errno) << "), retrying";
303 rc = ENABLE_INPLACE_ERR_DEV;
304 goto errout;
305 }
306 }
307 }
308#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700309 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
310 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
311 if (--retries) {
Paul Crowley772cc852018-02-01 09:53:27 -0800312 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
313 << " for ext4 inplace encrypt, retrying";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700314 sleep(RETRY_MOUNT_DELAY_SECONDS);
315 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800316 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
317 << " for ext4 inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700318 rc = ENABLE_INPLACE_ERR_DEV;
319 goto errout;
320 }
321 }
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530322#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700323
324 if (setjmp(setjmp_env)) { // NOLINT
Paul Crowley772cc852018-02-01 09:53:27 -0800325 LOG(ERROR) << "Reading ext4 extent caused an exception";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700326 rc = -1;
327 goto errout;
328 }
329
330 if (read_ext(data.realfd, 0) != 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800331 LOG(ERROR) << "Failed to read ext4 extent";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700332 rc = -1;
333 goto errout;
334 }
335
336 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
337 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
338 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
339
Paul Crowley772cc852018-02-01 09:53:27 -0800340 LOG(INFO) << "Encrypting ext4 filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700341
342 data.tot_used_blocks = data.numblocks;
343 for (i = 0; i < aux_info.groups; ++i) {
344 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
345 }
346
347 data.one_pct = data.tot_used_blocks / 100;
348 data.cur_pct = 0;
349
350 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800351 LOG(WARNING) << "Error getting time at start";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700352 // Note - continue anyway - we'll run with 0
353 }
354 data.time_started = time_started.tv_sec;
355 data.remaining_time = -1;
356
357 rc = encrypt_groups(&data);
358 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800359 LOG(ERROR) << "Error encrypting groups";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700360 goto errout;
361 }
362
363 *size_already_done += data.completed ? size : data.last_written_sector;
364 rc = 0;
365
366errout:
367 close(data.realfd);
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530368#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
369 if (!is_ice_enabled())
370 close(data.cryptofd);
371#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700372 close(data.cryptofd);
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530373#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700374
375 return rc;
376}
377
378static void log_progress_f2fs(u64 block, bool completed)
379{
380 // Precondition - if completed data = 0 else data != 0
381
382 // Track progress so we can skip logging blocks
383 static u64 last_block = (u64)-1;
384
385 // Need to close existing 'Encrypting from' log?
386 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800387 LOG(INFO) << "Encrypted to block " << last_block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700388 last_block = -1;
389 }
390
391 // Need to start new 'Encrypting from' log?
392 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
Paul Crowley772cc852018-02-01 09:53:27 -0800393 LOG(INFO) << "Encrypting from block " << block;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700394 }
395
396 // Update offset
397 if (!completed) {
398 last_block = block;
399 }
400}
401
402static int encrypt_one_block_f2fs(u64 pos, void *data)
403{
404 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
405
406 priv_dat->blocks_already_done = pos - 1;
407 update_progress(priv_dat, 1);
408
409 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
410
411 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800412 LOG(ERROR) << "Error reading real_blkdev " << priv_dat->crypto_blkdev
413 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700414 return -1;
415 }
416
417 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800418 LOG(ERROR) << "Error writing crypto_blkdev " << priv_dat->crypto_blkdev
419 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700420 return -1;
421 } else {
422 log_progress_f2fs(pos, false);
423 }
424
425 return 0;
426}
427
Paul Crowley772cc852018-02-01 09:53:27 -0800428static int cryptfs_enable_inplace_f2fs(char* crypto_blkdev, char* real_blkdev, off64_t size,
429 off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800430 off64_t previously_encrypted_upto,
431 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700432 struct encryptGroupsData data;
433 struct f2fs_info *f2fs_info = NULL;
434 int rc = ENABLE_INPLACE_ERR_OTHER;
435 if (previously_encrypted_upto > *size_already_done) {
Paul Crowley772cc852018-02-01 09:53:27 -0800436 LOG(DEBUG) << "Not fast encrypting since resuming part way through";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700437 return ENABLE_INPLACE_ERR_OTHER;
438 }
439 memset(&data, 0, sizeof(data));
440 data.real_blkdev = real_blkdev;
441 data.crypto_blkdev = crypto_blkdev;
Paul Crowley0fd26262018-01-30 09:48:19 -0800442 data.set_progress_properties = set_progress_properties;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700443 data.realfd = -1;
444 data.cryptofd = -1;
445 if ( (data.realfd = open64(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800446 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700447 goto errout;
448 }
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530449#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
450 if (is_ice_enabled())
451 data.cryptofd = data.realfd;
452 else {
453 if ((data.cryptofd = open64(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
454 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
455 << " for f2fs inplace encrypt. err=" << errno
456 << "(" << strerror(errno) << "), retrying";
457 rc = ENABLE_INPLACE_ERR_DEV;
458 goto errout;
459 }
460 }
461#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700462 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800463 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
464 << " for f2fs inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700465 rc = ENABLE_INPLACE_ERR_DEV;
466 goto errout;
467 }
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530468#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700469
470 f2fs_info = generate_f2fs_info(data.realfd);
471 if (!f2fs_info)
472 goto errout;
473
474 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
475 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
476 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
477
478 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
479
480 data.one_pct = data.tot_used_blocks / 100;
481 data.cur_pct = 0;
482 data.time_started = time(NULL);
483 data.remaining_time = -1;
484
485 data.buffer = (char*) malloc(f2fs_info->block_size);
486 if (!data.buffer) {
Paul Crowley772cc852018-02-01 09:53:27 -0800487 LOG(ERROR) << "Failed to allocate crypto buffer";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700488 goto errout;
489 }
490
491 data.count = 0;
492
493 /* Currently, this either runs to completion, or hits a nonrecoverable error */
494 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
495
496 if (rc) {
Paul Crowley772cc852018-02-01 09:53:27 -0800497 LOG(ERROR) << "Error in running over f2fs blocks";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700498 rc = ENABLE_INPLACE_ERR_OTHER;
499 goto errout;
500 }
501
502 *size_already_done += size;
503 rc = 0;
504
505errout:
Paul Crowley772cc852018-02-01 09:53:27 -0800506 if (rc) LOG(ERROR) << "Failed to encrypt f2fs filesystem on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700507
508 log_progress_f2fs(0, true);
509 free(f2fs_info);
510 free(data.buffer);
511 close(data.realfd);
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530512#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
513 if (!is_ice_enabled())
514 close(data.cryptofd);
515#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700516 close(data.cryptofd);
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530517#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700518
519 return rc;
520}
521
Paul Crowley772cc852018-02-01 09:53:27 -0800522static int cryptfs_enable_inplace_full(char* crypto_blkdev, char* real_blkdev, off64_t size,
523 off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800524 off64_t previously_encrypted_upto,
525 bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700526 int realfd, cryptofd;
527 char *buf[CRYPT_INPLACE_BUFSIZE];
528 int rc = ENABLE_INPLACE_ERR_OTHER;
529 off64_t numblocks, i, remainder;
530 off64_t one_pct, cur_pct, new_pct;
531 off64_t blocks_already_done, tot_numblocks;
532
533 if ( (realfd = open(real_blkdev, O_RDONLY|O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800534 PLOG(ERROR) << "Error opening real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700535 return ENABLE_INPLACE_ERR_OTHER;
536 }
537
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530538#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
539 if (is_ice_enabled())
540 cryptofd = realfd;
541 else {
542 if ((cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
543 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev
544 << " for inplace encrypt. err=" << errno
545 << "(" << strerror(errno) << "), retrying";
546 close(realfd);
547 return ENABLE_INPLACE_ERR_DEV;
548 }
549 }
550#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700551 if ( (cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800552 PLOG(ERROR) << "Error opening crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700553 close(realfd);
554 return ENABLE_INPLACE_ERR_DEV;
555 }
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530556#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700557
558 /* This is pretty much a simple loop of reading 4K, and writing 4K.
559 * The size passed in is the number of 512 byte sectors in the filesystem.
560 * So compute the number of whole 4K blocks we should read/write,
561 * and the remainder.
562 */
563 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
564 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
565 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
566 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
567
Paul Crowley772cc852018-02-01 09:53:27 -0800568 LOG(ERROR) << "Encrypting filesystem in place...";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700569
570 i = previously_encrypted_upto + 1 - *size_already_done;
571
572 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800573 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << real_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700574 goto errout;
575 }
576
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530577#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
578 if (!is_ice_enabled()) {
579 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
580 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << crypto_blkdev;
581 goto errout;
582 }
583 }
584#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700585 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800586 PLOG(ERROR) << "Cannot seek to previously encrypted point on " << crypto_blkdev;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700587 goto errout;
588 }
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530589#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700590
591 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
592 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800593 PLOG(ERROR) << "Error reading initial sectors from real_blkdev " << real_blkdev
594 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700595 goto errout;
596 }
597 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800598 PLOG(ERROR) << "Error writing initial sectors to crypto_blkdev " << crypto_blkdev
599 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700600 goto errout;
601 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800602 LOG(INFO) << "Encrypted 1 block at " << i;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700603 }
604 }
605
606 one_pct = tot_numblocks / 100;
607 cur_pct = 0;
608 /* process the majority of the filesystem in blocks */
609 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
610 new_pct = (i + blocks_already_done) / one_pct;
Paul Crowley0fd26262018-01-30 09:48:19 -0800611 if (set_progress_properties && new_pct > cur_pct) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700612 char buf[8];
613
614 cur_pct = new_pct;
615 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Paul Crowley772cc852018-02-01 09:53:27 -0800616 android::base::SetProperty("vold.encrypt_progress", buf);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700617 }
618 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800619 PLOG(ERROR) << "Error reading real_blkdev " << real_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700620 goto errout;
621 }
622 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800623 PLOG(ERROR) << "Error writing crypto_blkdev " << crypto_blkdev << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700624 goto errout;
625 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800626 LOG(DEBUG) << "Encrypted " << CRYPT_SECTORS_PER_BUFSIZE << " block at "
627 << i * CRYPT_SECTORS_PER_BUFSIZE;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700628 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700629 }
630
631 /* Do any remaining sectors */
632 for (i=0; i<remainder; i++) {
633 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800634 LOG(ERROR) << "Error reading final sectors from real_blkdev " << real_blkdev
635 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700636 goto errout;
637 }
638 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
Paul Crowley772cc852018-02-01 09:53:27 -0800639 LOG(ERROR) << "Error writing final sectors to crypto_blkdev " << crypto_blkdev
640 << " for inplace encrypt";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700641 goto errout;
642 } else {
Paul Crowley772cc852018-02-01 09:53:27 -0800643 LOG(INFO) << "Encrypted 1 block at next location";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700644 }
645 }
646
647 *size_already_done += size;
648 rc = 0;
649
650errout:
651 close(realfd);
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530652#if defined(CONFIG_HW_DISK_ENCRYPTION) && defined(CONFIG_HW_DISK_ENCRYPT_PERF)
653 if (!is_ice_enabled())
654 close(cryptofd);
655#else
Paul Crowleyf71ace32016-06-02 11:01:19 -0700656 close(cryptofd);
AnilKumar Chimata4d404ad2018-02-11 17:11:24 +0530657#endif
Paul Crowleyf71ace32016-06-02 11:01:19 -0700658
659 return rc;
660}
661
662/* returns on of the ENABLE_INPLACE_* return codes */
Paul Crowley772cc852018-02-01 09:53:27 -0800663int cryptfs_enable_inplace(char* crypto_blkdev, char* real_blkdev, off64_t size,
664 off64_t* size_already_done, off64_t tot_size,
Paul Crowley0fd26262018-01-30 09:48:19 -0800665 off64_t previously_encrypted_upto, bool set_progress_properties) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700666 int rc_ext4, rc_f2fs, rc_full;
Paul Crowley0fd26262018-01-30 09:48:19 -0800667 LOG(DEBUG) << "cryptfs_enable_inplace(" << crypto_blkdev << ", " << real_blkdev << ", " << size
668 << ", " << size_already_done << ", " << tot_size << ", " << previously_encrypted_upto
669 << ", " << set_progress_properties << ")";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700670 if (previously_encrypted_upto) {
Paul Crowley772cc852018-02-01 09:53:27 -0800671 LOG(DEBUG) << "Continuing encryption from " << previously_encrypted_upto;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700672 }
673
674 if (*size_already_done + size < previously_encrypted_upto) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800675 LOG(DEBUG) << "cryptfs_enable_inplace already done";
Paul Crowleyf71ace32016-06-02 11:01:19 -0700676 *size_already_done += size;
677 return 0;
678 }
679
680 /* TODO: identify filesystem type.
681 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
682 * then we will drop down to cryptfs_enable_inplace_f2fs.
683 * */
Paul Crowley0fd26262018-01-30 09:48:19 -0800684 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, size, size_already_done,
685 tot_size, previously_encrypted_upto,
686 set_progress_properties)) == 0) {
687 LOG(DEBUG) << "cryptfs_enable_inplace_ext4 success";
688 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700689 }
Paul Crowley772cc852018-02-01 09:53:27 -0800690 LOG(DEBUG) << "cryptfs_enable_inplace_ext4()=" << rc_ext4;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700691
Paul Crowley0fd26262018-01-30 09:48:19 -0800692 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, size, size_already_done,
693 tot_size, previously_encrypted_upto,
694 set_progress_properties)) == 0) {
695 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs success";
696 return 0;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700697 }
Paul Crowley772cc852018-02-01 09:53:27 -0800698 LOG(DEBUG) << "cryptfs_enable_inplace_f2fs()=" << rc_f2fs;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700699
Paul Crowley0fd26262018-01-30 09:48:19 -0800700 rc_full =
701 cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, size, size_already_done, tot_size,
702 previously_encrypted_upto, set_progress_properties);
Paul Crowley772cc852018-02-01 09:53:27 -0800703 LOG(DEBUG) << "cryptfs_enable_inplace_full()=" << rc_full;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700704
705 /* Hack for b/17898962, the following is the symptom... */
706 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
707 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
708 && rc_full == ENABLE_INPLACE_ERR_DEV) {
Paul Crowley0fd26262018-01-30 09:48:19 -0800709 LOG(DEBUG) << "ENABLE_INPLACE_ERR_DEV";
710 return ENABLE_INPLACE_ERR_DEV;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700711 }
712 return rc_full;
713}