fs_mgr.c revision 5bc31a2632f453e03edac714b865773970bba608
1/* 2 * Copyright (C) 2012 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 <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20#include <unistd.h> 21#include <fcntl.h> 22#include <ctype.h> 23#include <sys/mount.h> 24#include <sys/stat.h> 25#include <errno.h> 26#include <sys/types.h> 27#include <sys/wait.h> 28#include <libgen.h> 29#include <time.h> 30#include <sys/swap.h> 31/* XXX These need to be obtained from kernel headers. See b/9336527 */ 32#define SWAP_FLAG_PREFER 0x8000 33#define SWAP_FLAG_PRIO_MASK 0x7fff 34#define SWAP_FLAG_PRIO_SHIFT 0 35#define SWAP_FLAG_DISCARD 0x10000 36 37#include <private/android_filesystem_config.h> 38#include <cutils/partition_utils.h> 39#include <cutils/properties.h> 40#include <logwrap/logwrap.h> 41 42#include "fs_mgr_priv.h" 43 44#define KEY_LOC_PROP "ro.crypto.keyfile.userdata" 45#define KEY_IN_FOOTER "footer" 46 47#define E2FSCK_BIN "/system/bin/e2fsck" 48#define MKSWAP_BIN "/system/bin/mkswap" 49 50#define ZRAM_CONF_DEV "/sys/block/zram0/disksize" 51 52#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) 53 54struct flag_list { 55 const char *name; 56 unsigned flag; 57}; 58 59static struct flag_list mount_flags[] = { 60 { "noatime", MS_NOATIME }, 61 { "noexec", MS_NOEXEC }, 62 { "nosuid", MS_NOSUID }, 63 { "nodev", MS_NODEV }, 64 { "nodiratime", MS_NODIRATIME }, 65 { "ro", MS_RDONLY }, 66 { "rw", 0 }, 67 { "remount", MS_REMOUNT }, 68 { "bind", MS_BIND }, 69 { "rec", MS_REC }, 70 { "unbindable", MS_UNBINDABLE }, 71 { "private", MS_PRIVATE }, 72 { "slave", MS_SLAVE }, 73 { "shared", MS_SHARED }, 74 { "defaults", 0 }, 75 { 0, 0 }, 76}; 77 78static struct flag_list fs_mgr_flags[] = { 79 { "wait", MF_WAIT }, 80 { "check", MF_CHECK }, 81 { "encryptable=",MF_CRYPT }, 82 { "nonremovable",MF_NONREMOVABLE }, 83 { "voldmanaged=",MF_VOLDMANAGED}, 84 { "length=", MF_LENGTH }, 85 { "recoveryonly",MF_RECOVERYONLY }, 86 { "swapprio=", MF_SWAPPRIO }, 87 { "zramsize=", MF_ZRAMSIZE }, 88 { "defaults", 0 }, 89 { 0, 0 }, 90}; 91 92struct fs_mgr_flag_values { 93 char *key_loc; 94 long long part_length; 95 char *label; 96 int partnum; 97 int swap_prio; 98 unsigned int zram_size; 99}; 100 101/* 102 * gettime() - returns the time in seconds of the system's monotonic clock or 103 * zero on error. 104 */ 105static time_t gettime(void) 106{ 107 struct timespec ts; 108 int ret; 109 110 ret = clock_gettime(CLOCK_MONOTONIC, &ts); 111 if (ret < 0) { 112 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno)); 113 return 0; 114 } 115 116 return ts.tv_sec; 117} 118 119static int wait_for_file(const char *filename, int timeout) 120{ 121 struct stat info; 122 time_t timeout_time = gettime() + timeout; 123 int ret = -1; 124 125 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0)) 126 usleep(10000); 127 128 return ret; 129} 130 131static int parse_flags(char *flags, struct flag_list *fl, 132 struct fs_mgr_flag_values *flag_vals, 133 char *fs_options, int fs_options_len) 134{ 135 int f = 0; 136 int i; 137 char *p; 138 char *savep; 139 140 /* initialize flag values. If we find a relevant flag, we'll 141 * update the value */ 142 if (flag_vals) { 143 memset(flag_vals, 0, sizeof(*flag_vals)); 144 flag_vals->partnum = -1; 145 flag_vals->swap_prio = -1; /* negative means it wasn't specified. */ 146 } 147 148 /* initialize fs_options to the null string */ 149 if (fs_options && (fs_options_len > 0)) { 150 fs_options[0] = '\0'; 151 } 152 153 p = strtok_r(flags, ",", &savep); 154 while (p) { 155 /* Look for the flag "p" in the flag list "fl" 156 * If not found, the loop exits with fl[i].name being null. 157 */ 158 for (i = 0; fl[i].name; i++) { 159 if (!strncmp(p, fl[i].name, strlen(fl[i].name))) { 160 f |= fl[i].flag; 161 if ((fl[i].flag == MF_CRYPT) && flag_vals) { 162 /* The encryptable flag is followed by an = and the 163 * location of the keys. Get it and return it. 164 */ 165 flag_vals->key_loc = strdup(strchr(p, '=') + 1); 166 } else if ((fl[i].flag == MF_LENGTH) && flag_vals) { 167 /* The length flag is followed by an = and the 168 * size of the partition. Get it and return it. 169 */ 170 flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0); 171 } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) { 172 /* The voldmanaged flag is followed by an = and the 173 * label, a colon and the partition number or the 174 * word "auto", e.g. 175 * voldmanaged=sdcard:3 176 * Get and return them. 177 */ 178 char *label_start; 179 char *label_end; 180 char *part_start; 181 182 label_start = strchr(p, '=') + 1; 183 label_end = strchr(p, ':'); 184 if (label_end) { 185 flag_vals->label = strndup(label_start, 186 (int) (label_end - label_start)); 187 part_start = strchr(p, ':') + 1; 188 if (!strcmp(part_start, "auto")) { 189 flag_vals->partnum = -1; 190 } else { 191 flag_vals->partnum = strtol(part_start, NULL, 0); 192 } 193 } else { 194 ERROR("Warning: voldmanaged= flag malformed\n"); 195 } 196 } else if ((fl[i].flag == MF_SWAPPRIO) && flag_vals) { 197 flag_vals->swap_prio = strtoll(strchr(p, '=') + 1, NULL, 0); 198 } else if ((fl[i].flag == MF_ZRAMSIZE) && flag_vals) { 199 flag_vals->zram_size = strtoll(strchr(p, '=') + 1, NULL, 0); 200 } 201 break; 202 } 203 } 204 205 if (!fl[i].name) { 206 if (fs_options) { 207 /* It's not a known flag, so it must be a filesystem specific 208 * option. Add it to fs_options if it was passed in. 209 */ 210 strlcat(fs_options, p, fs_options_len); 211 strlcat(fs_options, ",", fs_options_len); 212 } else { 213 /* fs_options was not passed in, so if the flag is unknown 214 * it's an error. 215 */ 216 ERROR("Warning: unknown flag %s\n", p); 217 } 218 } 219 p = strtok_r(NULL, ",", &savep); 220 } 221 222out: 223 if (fs_options && fs_options[0]) { 224 /* remove the last trailing comma from the list of options */ 225 fs_options[strlen(fs_options) - 1] = '\0'; 226 } 227 228 return f; 229} 230 231/* Read a line of text till the next newline character. 232 * If no newline is found before the buffer is full, continue reading till a new line is seen, 233 * then return an empty buffer. This effectively ignores lines that are too long. 234 * On EOF, return null. 235 */ 236static char *fs_getline(char *buf, int size, FILE *file) 237{ 238 int cnt = 0; 239 int eof = 0; 240 int eol = 0; 241 int c; 242 243 if (size < 1) { 244 return NULL; 245 } 246 247 while (cnt < (size - 1)) { 248 c = getc(file); 249 if (c == EOF) { 250 eof = 1; 251 break; 252 } 253 254 *(buf + cnt) = c; 255 cnt++; 256 257 if (c == '\n') { 258 eol = 1; 259 break; 260 } 261 } 262 263 /* Null terminate what we've read */ 264 *(buf + cnt) = '\0'; 265 266 if (eof) { 267 if (cnt) { 268 return buf; 269 } else { 270 return NULL; 271 } 272 } else if (eol) { 273 return buf; 274 } else { 275 /* The line is too long. Read till a newline or EOF. 276 * If EOF, return null, if newline, return an empty buffer. 277 */ 278 while(1) { 279 c = getc(file); 280 if (c == EOF) { 281 return NULL; 282 } else if (c == '\n') { 283 *buf = '\0'; 284 return buf; 285 } 286 } 287 } 288} 289 290struct fstab *fs_mgr_read_fstab(const char *fstab_path) 291{ 292 FILE *fstab_file; 293 int cnt, entries; 294 int len; 295 char line[256]; 296 const char *delim = " \t"; 297 char *save_ptr, *p; 298 struct fstab *fstab; 299 struct fstab_rec *recs; 300 struct fs_mgr_flag_values flag_vals; 301#define FS_OPTIONS_LEN 1024 302 char tmp_fs_options[FS_OPTIONS_LEN]; 303 304 fstab_file = fopen(fstab_path, "r"); 305 if (!fstab_file) { 306 ERROR("Cannot open file %s\n", fstab_path); 307 return 0; 308 } 309 310 entries = 0; 311 while (fs_getline(line, sizeof(line), fstab_file)) { 312 /* if the last character is a newline, shorten the string by 1 byte */ 313 len = strlen(line); 314 if (line[len - 1] == '\n') { 315 line[len - 1] = '\0'; 316 } 317 /* Skip any leading whitespace */ 318 p = line; 319 while (isspace(*p)) { 320 p++; 321 } 322 /* ignore comments or empty lines */ 323 if (*p == '#' || *p == '\0') 324 continue; 325 entries++; 326 } 327 328 if (!entries) { 329 ERROR("No entries found in fstab\n"); 330 return 0; 331 } 332 333 /* Allocate and init the fstab structure */ 334 fstab = calloc(1, sizeof(struct fstab)); 335 fstab->num_entries = entries; 336 fstab->fstab_filename = strdup(fstab_path); 337 fstab->recs = calloc(fstab->num_entries, sizeof(struct fstab_rec)); 338 339 fseek(fstab_file, 0, SEEK_SET); 340 341 cnt = 0; 342 while (fs_getline(line, sizeof(line), fstab_file)) { 343 /* if the last character is a newline, shorten the string by 1 byte */ 344 len = strlen(line); 345 if (line[len - 1] == '\n') { 346 line[len - 1] = '\0'; 347 } 348 349 /* Skip any leading whitespace */ 350 p = line; 351 while (isspace(*p)) { 352 p++; 353 } 354 /* ignore comments or empty lines */ 355 if (*p == '#' || *p == '\0') 356 continue; 357 358 /* If a non-comment entry is greater than the size we allocated, give an 359 * error and quit. This can happen in the unlikely case the file changes 360 * between the two reads. 361 */ 362 if (cnt >= entries) { 363 ERROR("Tried to process more entries than counted\n"); 364 break; 365 } 366 367 if (!(p = strtok_r(line, delim, &save_ptr))) { 368 ERROR("Error parsing mount source\n"); 369 return 0; 370 } 371 fstab->recs[cnt].blk_device = strdup(p); 372 373 if (!(p = strtok_r(NULL, delim, &save_ptr))) { 374 ERROR("Error parsing mount_point\n"); 375 return 0; 376 } 377 fstab->recs[cnt].mount_point = strdup(p); 378 379 if (!(p = strtok_r(NULL, delim, &save_ptr))) { 380 ERROR("Error parsing fs_type\n"); 381 return 0; 382 } 383 fstab->recs[cnt].fs_type = strdup(p); 384 385 if (!(p = strtok_r(NULL, delim, &save_ptr))) { 386 ERROR("Error parsing mount_flags\n"); 387 return 0; 388 } 389 tmp_fs_options[0] = '\0'; 390 fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL, 391 tmp_fs_options, FS_OPTIONS_LEN); 392 393 /* fs_options are optional */ 394 if (tmp_fs_options[0]) { 395 fstab->recs[cnt].fs_options = strdup(tmp_fs_options); 396 } else { 397 fstab->recs[cnt].fs_options = NULL; 398 } 399 400 if (!(p = strtok_r(NULL, delim, &save_ptr))) { 401 ERROR("Error parsing fs_mgr_options\n"); 402 return 0; 403 } 404 fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags, 405 &flag_vals, NULL, 0); 406 fstab->recs[cnt].key_loc = flag_vals.key_loc; 407 fstab->recs[cnt].length = flag_vals.part_length; 408 fstab->recs[cnt].label = flag_vals.label; 409 fstab->recs[cnt].partnum = flag_vals.partnum; 410 fstab->recs[cnt].swap_prio = flag_vals.swap_prio; 411 fstab->recs[cnt].zram_size = flag_vals.zram_size; 412 cnt++; 413 } 414 fclose(fstab_file); 415 416 return fstab; 417} 418 419void fs_mgr_free_fstab(struct fstab *fstab) 420{ 421 int i; 422 423 for (i = 0; i < fstab->num_entries; i++) { 424 /* Free the pointers return by strdup(3) */ 425 free(fstab->recs[i].blk_device); 426 free(fstab->recs[i].mount_point); 427 free(fstab->recs[i].fs_type); 428 free(fstab->recs[i].fs_options); 429 free(fstab->recs[i].key_loc); 430 free(fstab->recs[i].label); 431 i++; 432 } 433 434 /* Free the fstab_recs array created by calloc(3) */ 435 free(fstab->recs); 436 437 /* Free the fstab filename */ 438 free(fstab->fstab_filename); 439 440 /* Free fstab */ 441 free(fstab); 442} 443 444static void check_fs(char *blk_device, char *fs_type, char *target) 445{ 446 int status; 447 int ret; 448 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; 449 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro"; 450 char *e2fsck_argv[] = { 451 E2FSCK_BIN, 452 "-y", 453 blk_device 454 }; 455 456 /* Check for the types of filesystems we know how to check */ 457 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) { 458 /* 459 * First try to mount and unmount the filesystem. We do this because 460 * the kernel is more efficient than e2fsck in running the journal and 461 * processing orphaned inodes, and on at least one device with a 462 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes 463 * to do what the kernel does in about a second. 464 * 465 * After mounting and unmounting the filesystem, run e2fsck, and if an 466 * error is recorded in the filesystem superblock, e2fsck will do a full 467 * check. Otherwise, it does nothing. If the kernel cannot mount the 468 * filesytsem due to an error, e2fsck is still run to do a full check 469 * fix the filesystem. 470 */ 471 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts); 472 if (!ret) { 473 umount(target); 474 } 475 476 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device); 477 478 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, 479 &status, true, LOG_KLOG, true); 480 481 if (ret < 0) { 482 /* No need to check for error in fork, we can't really handle it now */ 483 ERROR("Failed trying to run %s\n", E2FSCK_BIN); 484 } 485 } 486 487 return; 488} 489 490static void remove_trailing_slashes(char *n) 491{ 492 int len; 493 494 len = strlen(n) - 1; 495 while ((*(n + len) == '/') && len) { 496 *(n + len) = '\0'; 497 len--; 498 } 499} 500 501/* 502 * Mark the given block device as read-only, using the BLKROSET ioctl. 503 * Return 0 on success, and -1 on error. 504 */ 505static void fs_set_blk_ro(const char *blockdev) 506{ 507 int fd; 508 int ON = 1; 509 510 fd = open(blockdev, O_RDONLY); 511 if (fd < 0) { 512 // should never happen 513 return; 514 } 515 516 ioctl(fd, BLKROSET, &ON); 517 close(fd); 518} 519 520/* 521 * __mount(): wrapper around the mount() system call which also 522 * sets the underlying block device to read-only if the mount is read-only. 523 * See "man 2 mount" for return values. 524 */ 525static int __mount(const char *source, const char *target, 526 const char *filesystemtype, unsigned long mountflags, 527 const void *data) 528{ 529 int ret = mount(source, target, filesystemtype, mountflags, data); 530 531 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) { 532 fs_set_blk_ro(source); 533 } 534 535 return ret; 536} 537 538static int fs_match(char *in1, char *in2) 539{ 540 char *n1; 541 char *n2; 542 int ret; 543 544 n1 = strdup(in1); 545 n2 = strdup(in2); 546 547 remove_trailing_slashes(n1); 548 remove_trailing_slashes(n2); 549 550 ret = !strcmp(n1, n2); 551 552 free(n1); 553 free(n2); 554 555 return ret; 556} 557 558int fs_mgr_mount_all(struct fstab *fstab) 559{ 560 int i = 0; 561 int encrypted = 0; 562 int ret = -1; 563 int mret; 564 565 if (!fstab) { 566 return ret; 567 } 568 569 for (i = 0; i < fstab->num_entries; i++) { 570 /* Don't mount entries that are managed by vold */ 571 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) { 572 continue; 573 } 574 575 /* Skip swap and raw partition entries such as boot, recovery, etc */ 576 if (!strcmp(fstab->recs[i].fs_type, "swap") || 577 !strcmp(fstab->recs[i].fs_type, "emmc") || 578 !strcmp(fstab->recs[i].fs_type, "mtd")) { 579 continue; 580 } 581 582 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { 583 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); 584 } 585 586 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { 587 check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type, 588 fstab->recs[i].mount_point); 589 } 590 591 mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, 592 fstab->recs[i].fs_type, fstab->recs[i].flags, 593 fstab->recs[i].fs_options); 594 if (!mret) { 595 /* Success! Go get the next one */ 596 continue; 597 } 598 599 /* mount(2) returned an error, check if it's encrypted and deal with it */ 600 if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) && 601 !partition_wiped(fstab->recs[i].blk_device)) { 602 /* Need to mount a tmpfs at this mountpoint for now, and set 603 * properties that vold will query later for decrypting 604 */ 605 if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs", 606 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) { 607 ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n", 608 fstab->recs[i].mount_point); 609 goto out; 610 } 611 encrypted = 1; 612 } else { 613 ERROR("Cannot mount filesystem on %s at %s\n", 614 fstab->recs[i].blk_device, fstab->recs[i].mount_point); 615 goto out; 616 } 617 } 618 619 if (encrypted) { 620 ret = 1; 621 } else { 622 ret = 0; 623 } 624 625out: 626 return ret; 627} 628 629/* If tmp_mount_point is non-null, mount the filesystem there. This is for the 630 * tmp mount we do to check the user password 631 */ 632int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, 633 char *tmp_mount_point) 634{ 635 int i = 0; 636 int ret = -1; 637 char *m; 638 639 if (!fstab) { 640 return ret; 641 } 642 643 for (i = 0; i < fstab->num_entries; i++) { 644 if (!fs_match(fstab->recs[i].mount_point, n_name)) { 645 continue; 646 } 647 648 /* We found our match */ 649 /* If this swap or a raw partition, report an error */ 650 if (!strcmp(fstab->recs[i].fs_type, "swap") || 651 !strcmp(fstab->recs[i].fs_type, "emmc") || 652 !strcmp(fstab->recs[i].fs_type, "mtd")) { 653 ERROR("Cannot mount filesystem of type %s on %s\n", 654 fstab->recs[i].fs_type, n_blk_device); 655 goto out; 656 } 657 658 /* First check the filesystem if requested */ 659 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { 660 wait_for_file(n_blk_device, WAIT_TIMEOUT); 661 } 662 663 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { 664 check_fs(n_blk_device, fstab->recs[i].fs_type, 665 fstab->recs[i].mount_point); 666 } 667 668 /* Now mount it where requested */ 669 if (tmp_mount_point) { 670 m = tmp_mount_point; 671 } else { 672 m = fstab->recs[i].mount_point; 673 } 674 if (__mount(n_blk_device, m, fstab->recs[i].fs_type, 675 fstab->recs[i].flags, fstab->recs[i].fs_options)) { 676 ERROR("Cannot mount filesystem on %s at %s\n", 677 n_blk_device, m); 678 goto out; 679 } else { 680 ret = 0; 681 goto out; 682 } 683 } 684 685 /* We didn't find a match, say so and return an error */ 686 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point); 687 688out: 689 return ret; 690} 691 692/* 693 * mount a tmpfs filesystem at the given point. 694 * return 0 on success, non-zero on failure. 695 */ 696int fs_mgr_do_tmpfs_mount(char *n_name) 697{ 698 int ret; 699 700 ret = mount("tmpfs", n_name, "tmpfs", 701 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS); 702 if (ret < 0) { 703 ERROR("Cannot mount tmpfs filesystem at %s\n", n_name); 704 return -1; 705 } 706 707 /* Success */ 708 return 0; 709} 710 711int fs_mgr_unmount_all(struct fstab *fstab) 712{ 713 int i = 0; 714 int ret = 0; 715 716 if (!fstab) { 717 return -1; 718 } 719 720 while (fstab->recs[i].blk_device) { 721 if (umount(fstab->recs[i].mount_point)) { 722 ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point); 723 ret = -1; 724 } 725 i++; 726 } 727 728 return ret; 729} 730 731/* This must be called after mount_all, because the mkswap command needs to be 732 * available. 733 */ 734int fs_mgr_swapon_all(struct fstab *fstab) 735{ 736 int i = 0; 737 int flags = 0; 738 int err = 0; 739 int ret = 0; 740 int status; 741 char *mkswap_argv[2] = { 742 MKSWAP_BIN, 743 NULL 744 }; 745 746 if (!fstab) { 747 return -1; 748 } 749 750 for (i = 0; i < fstab->num_entries; i++) { 751 /* Skip non-swap entries */ 752 if (strcmp(fstab->recs[i].fs_type, "swap")) { 753 continue; 754 } 755 756 if (fstab->recs[i].zram_size > 0) { 757 /* A zram_size was specified, so we need to configure the 758 * device. There is no point in having multiple zram devices 759 * on a system (all the memory comes from the same pool) so 760 * we can assume the device number is 0. 761 */ 762 FILE *zram_fp; 763 764 zram_fp = fopen(ZRAM_CONF_DEV, "r+"); 765 if (zram_fp == NULL) { 766 ERROR("Unable to open zram conf device " ZRAM_CONF_DEV); 767 ret = -1; 768 continue; 769 } 770 fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size); 771 fclose(zram_fp); 772 } 773 774 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { 775 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); 776 } 777 778 /* Initialize the swap area */ 779 mkswap_argv[1] = fstab->recs[i].blk_device; 780 err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv, 781 &status, true, LOG_KLOG, false); 782 if (err) { 783 ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device); 784 ret = -1; 785 continue; 786 } 787 788 /* If -1, then no priority was specified in fstab, so don't set 789 * SWAP_FLAG_PREFER or encode the priority */ 790 if (fstab->recs[i].swap_prio >= 0) { 791 flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) & 792 SWAP_FLAG_PRIO_MASK; 793 flags |= SWAP_FLAG_PREFER; 794 } else { 795 flags = 0; 796 } 797 err = swapon(fstab->recs[i].blk_device, flags); 798 if (err) { 799 ERROR("swapon failed for %s\n", fstab->recs[i].blk_device); 800 ret = -1; 801 } 802 } 803 804 return ret; 805} 806 807/* 808 * key_loc must be at least PROPERTY_VALUE_MAX bytes long 809 * 810 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long 811 */ 812int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size) 813{ 814 int i = 0; 815 816 if (!fstab) { 817 return -1; 818 } 819 /* Initialize return values to null strings */ 820 if (key_loc) { 821 *key_loc = '\0'; 822 } 823 if (real_blk_device) { 824 *real_blk_device = '\0'; 825 } 826 827 /* Look for the encryptable partition to find the data */ 828 for (i = 0; i < fstab->num_entries; i++) { 829 /* Don't deal with vold managed enryptable partitions here */ 830 if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) { 831 continue; 832 } 833 if (!(fstab->recs[i].fs_mgr_flags & MF_CRYPT)) { 834 continue; 835 } 836 837 /* We found a match */ 838 if (key_loc) { 839 strlcpy(key_loc, fstab->recs[i].key_loc, size); 840 } 841 if (real_blk_device) { 842 strlcpy(real_blk_device, fstab->recs[i].blk_device, size); 843 } 844 break; 845 } 846 847 return 0; 848} 849 850/* Add an entry to the fstab, and return 0 on success or -1 on error */ 851int fs_mgr_add_entry(struct fstab *fstab, 852 const char *mount_point, const char *fs_type, 853 const char *blk_device, long long length) 854{ 855 struct fstab_rec *new_fstab_recs; 856 int n = fstab->num_entries; 857 858 new_fstab_recs = (struct fstab_rec *) 859 realloc(fstab->recs, sizeof(struct fstab_rec) * (n + 1)); 860 861 if (!new_fstab_recs) { 862 return -1; 863 } 864 865 /* A new entry was added, so initialize it */ 866 memset(&new_fstab_recs[n], 0, sizeof(struct fstab_rec)); 867 new_fstab_recs[n].mount_point = strdup(mount_point); 868 new_fstab_recs[n].fs_type = strdup(fs_type); 869 new_fstab_recs[n].blk_device = strdup(blk_device); 870 new_fstab_recs[n].length = 0; 871 872 /* Update the fstab struct */ 873 fstab->recs = new_fstab_recs; 874 fstab->num_entries++; 875 876 return 0; 877} 878 879struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path) 880{ 881 int i; 882 883 if (!fstab) { 884 return NULL; 885 } 886 887 for (i = 0; i < fstab->num_entries; i++) { 888 int len = strlen(fstab->recs[i].mount_point); 889 if (strncmp(path, fstab->recs[i].mount_point, len) == 0 && 890 (path[len] == '\0' || path[len] == '/')) { 891 return &fstab->recs[i]; 892 } 893 } 894 895 return NULL; 896} 897 898int fs_mgr_is_voldmanaged(struct fstab_rec *fstab) 899{ 900 return fstab->fs_mgr_flags & MF_VOLDMANAGED; 901} 902 903int fs_mgr_is_nonremovable(struct fstab_rec *fstab) 904{ 905 return fstab->fs_mgr_flags & MF_NONREMOVABLE; 906} 907 908int fs_mgr_is_encryptable(struct fstab_rec *fstab) 909{ 910 return fstab->fs_mgr_flags & MF_CRYPT; 911} 912 913