blob: 2761545bb0c84a72eb6279017b4b478a346adf0b [file] [log] [blame]
Ken Sumrallc1bf8962012-01-06 19:09:42 -08001/*
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
Ken Sumrallc1bf8962012-01-06 19:09:42 -080017#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>
Ken Sumrall5bc31a22013-07-08 19:11:55 -070030#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
Ken Sumrallc1bf8962012-01-06 19:09:42 -080036
37#include <private/android_filesystem_config.h>
38#include <cutils/partition_utils.h>
39#include <cutils/properties.h>
Ken Sumrallbf021b42013-03-19 19:38:44 -070040#include <logwrap/logwrap.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080041
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"
Ken Sumrall5bc31a22013-07-08 19:11:55 -070048#define MKSWAP_BIN "/system/bin/mkswap"
49
50#define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080051
Ken Sumrallbf021b42013-03-19 19:38:44 -070052#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
53
Ken Sumrallc1bf8962012-01-06 19:09:42 -080054struct 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 },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -070068 { "bind", MS_BIND },
69 { "rec", MS_REC },
70 { "unbindable", MS_UNBINDABLE },
71 { "private", MS_PRIVATE },
72 { "slave", MS_SLAVE },
73 { "shared", MS_SHARED },
Ken Sumrallc1bf8962012-01-06 19:09:42 -080074 { "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 },
Ken Sumrallab6b8522013-02-13 12:58:40 -080082 { "nonremovable",MF_NONREMOVABLE },
83 { "voldmanaged=",MF_VOLDMANAGED},
84 { "length=", MF_LENGTH },
Ken Sumrall6c2c1212013-02-22 17:36:21 -080085 { "recoveryonly",MF_RECOVERYONLY },
Ken Sumrall5bc31a22013-07-08 19:11:55 -070086 { "swapprio=", MF_SWAPPRIO },
87 { "zramsize=", MF_ZRAMSIZE },
Ken Sumrallc1bf8962012-01-06 19:09:42 -080088 { "defaults", 0 },
89 { 0, 0 },
90};
91
Ken Sumrall5bc31a22013-07-08 19:11:55 -070092struct 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
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800101/*
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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800131static int parse_flags(char *flags, struct flag_list *fl,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700132 struct fs_mgr_flag_values *flag_vals,
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800133 char *fs_options, int fs_options_len)
134{
135 int f = 0;
136 int i;
137 char *p;
138 char *savep;
139
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700140 /* 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. */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800146 }
147
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800148 /* 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;
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700161 if ((fl[i].flag == MF_CRYPT) && flag_vals) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800162 /* The encryptable flag is followed by an = and the
163 * location of the keys. Get it and return it.
164 */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700165 flag_vals->key_loc = strdup(strchr(p, '=') + 1);
166 } else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800167 /* The length flag is followed by an = and the
168 * size of the partition. Get it and return it.
169 */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700170 flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0);
171 } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800172 /* 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) {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700185 flag_vals->label = strndup(label_start,
186 (int) (label_end - label_start));
Ken Sumrallab6b8522013-02-13 12:58:40 -0800187 part_start = strchr(p, ':') + 1;
188 if (!strcmp(part_start, "auto")) {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700189 flag_vals->partnum = -1;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800190 } else {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700191 flag_vals->partnum = strtol(part_start, NULL, 0);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800192 }
193 } else {
194 ERROR("Warning: voldmanaged= flag malformed\n");
195 }
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700196 } 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);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800200 }
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 */
Irina Tirdea295b82b2012-08-27 19:10:57 +0300236static char *fs_getline(char *buf, int size, FILE *file)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800237{
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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800290struct fstab *fs_mgr_read_fstab(const char *fstab_path)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800291{
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;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800298 struct fstab *fstab;
299 struct fstab_rec *recs;
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700300 struct fs_mgr_flag_values flag_vals;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800301#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;
Irina Tirdea295b82b2012-08-27 19:10:57 +0300311 while (fs_getline(line, sizeof(line), fstab_file)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800312 /* 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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800333 /* 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));
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800338
339 fseek(fstab_file, 0, SEEK_SET);
340
341 cnt = 0;
Irina Tirdea295b82b2012-08-27 19:10:57 +0300342 while (fs_getline(line, sizeof(line), fstab_file)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800343 /* 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 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800371 fstab->recs[cnt].blk_device = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800372
373 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800374 ERROR("Error parsing mount_point\n");
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800375 return 0;
376 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800377 fstab->recs[cnt].mount_point = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800378
379 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
380 ERROR("Error parsing fs_type\n");
381 return 0;
382 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800383 fstab->recs[cnt].fs_type = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800384
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';
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700390 fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL,
Ken Sumrallab6b8522013-02-13 12:58:40 -0800391 tmp_fs_options, FS_OPTIONS_LEN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800392
393 /* fs_options are optional */
394 if (tmp_fs_options[0]) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800395 fstab->recs[cnt].fs_options = strdup(tmp_fs_options);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800396 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800397 fstab->recs[cnt].fs_options = NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800398 }
399
400 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
401 ERROR("Error parsing fs_mgr_options\n");
402 return 0;
403 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800404 fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700405 &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;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800412 cnt++;
413 }
414 fclose(fstab_file);
415
416 return fstab;
417}
418
Ken Sumrallab6b8522013-02-13 12:58:40 -0800419void fs_mgr_free_fstab(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800420{
Ken Sumrallab6b8522013-02-13 12:58:40 -0800421 int i;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800422
Ken Sumrallab6b8522013-02-13 12:58:40 -0800423 for (i = 0; i < fstab->num_entries; i++) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800424 /* Free the pointers return by strdup(3) */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800425 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);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800431 i++;
432 }
433
Ken Sumrallab6b8522013-02-13 12:58:40 -0800434 /* 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 */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800441 free(fstab);
442}
443
Ken Sumrallab6b8522013-02-13 12:58:40 -0800444static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800445{
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800446 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700447 int ret;
448 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
449 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -0700450 char *e2fsck_argv[] = {
451 E2FSCK_BIN,
452 "-y",
453 blk_device
454 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800455
456 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800457 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700458 /*
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 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800471 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
472 if (!ret) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700473 umount(target);
474 }
475
Ken Sumrallab6b8522013-02-13 12:58:40 -0800476 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800477
Ken Sumrallbf021b42013-03-19 19:38:44 -0700478 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
479 &status, true, LOG_KLOG, true);
480
481 if (ret < 0) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800482 /* No need to check for error in fork, we can't really handle it now */
Ken Sumrallbf021b42013-03-19 19:38:44 -0700483 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800484 }
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
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700501/*
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
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800538static 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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800558int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800559{
560 int i = 0;
561 int encrypted = 0;
562 int ret = -1;
563 int mret;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800564
Ken Sumrallab6b8522013-02-13 12:58:40 -0800565 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800566 return ret;
567 }
568
Ken Sumrallab6b8522013-02-13 12:58:40 -0800569 for (i = 0; i < fstab->num_entries; i++) {
570 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800571 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800572 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800573 }
574
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700575 /* 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") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800578 !strcmp(fstab->recs[i].fs_type, "mtd")) {
579 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800580 }
581
Ken Sumrallab6b8522013-02-13 12:58:40 -0800582 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
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700591 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);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800594 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 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800600 if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) &&
601 !partition_wiped(fstab->recs[i].blk_device)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800602 /* Need to mount a tmpfs at this mountpoint for now, and set
603 * properties that vold will query later for decrypting
604 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800605 if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs",
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800606 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
607 ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800608 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800609 goto out;
610 }
611 encrypted = 1;
612 } else {
613 ERROR("Cannot mount filesystem on %s at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800614 fstab->recs[i].blk_device, fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800615 goto out;
616 }
617 }
618
619 if (encrypted) {
620 ret = 1;
621 } else {
622 ret = 0;
623 }
624
625out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800626 return ret;
627}
628
Ken Sumrallab6b8522013-02-13 12:58:40 -0800629/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800630 * tmp mount we do to check the user password
631 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800632int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
633 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800634{
635 int i = 0;
636 int ret = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800637 char *m;
638
Ken Sumrallab6b8522013-02-13 12:58:40 -0800639 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800640 return ret;
641 }
642
Ken Sumrallab6b8522013-02-13 12:58:40 -0800643 for (i = 0; i < fstab->num_entries; i++) {
644 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800645 continue;
646 }
647
648 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700649 /* 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") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800652 !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;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800656 }
657
Ken Sumrallab6b8522013-02-13 12:58:40 -0800658 /* 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);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800666 }
667
668 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800669 if (tmp_mount_point) {
670 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800671 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800672 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800673 }
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700674 if (__mount(n_blk_device, m, fstab->recs[i].fs_type,
675 fstab->recs[i].flags, fstab->recs[i].fs_options)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800676 ERROR("Cannot mount filesystem on %s at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800677 n_blk_device, m);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800678 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 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800686 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800687
688out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800689 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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800711int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800712{
713 int i = 0;
714 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800715
Ken Sumrallab6b8522013-02-13 12:58:40 -0800716 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800717 return -1;
718 }
719
Ken Sumrallab6b8522013-02-13 12:58:40 -0800720 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);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800723 ret = -1;
724 }
725 i++;
726 }
727
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800728 return ret;
729}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700730
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
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800807/*
808 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
809 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800810 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800811 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800812int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800813{
814 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800815
Ken Sumrallab6b8522013-02-13 12:58:40 -0800816 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800817 return -1;
818 }
819 /* Initialize return values to null strings */
820 if (key_loc) {
821 *key_loc = '\0';
822 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800823 if (real_blk_device) {
824 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800825 }
826
827 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800828 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)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800834 continue;
835 }
836
837 /* We found a match */
838 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800839 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800840 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800841 if (real_blk_device) {
842 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800843 }
844 break;
845 }
846
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800847 return 0;
848}
849
Ken Sumrallab6b8522013-02-13 12:58:40 -0800850/* 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