Ext4.cpp revision 6a74dcaa6e646fea8e00b7c04332fc60fe7e017c
1344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root/* 2344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * Copyright (C) 2012 The Android Open Source Project 3344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * 4344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * Licensed under the Apache License, Version 2.0 (the "License"); 5344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * you may not use this file except in compliance with the License. 6344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * You may obtain a copy of the License at 7344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * 8344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * http://www.apache.org/licenses/LICENSE-2.0 9344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * 10344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * Unless required by applicable law or agreed to in writing, software 11344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * distributed under the License is distributed on an "AS IS" BASIS, 12344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * See the License for the specific language governing permissions and 14344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root * limitations under the License. 15344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root */ 16344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 17344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <stdio.h> 18344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <stdlib.h> 19344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <fcntl.h> 20344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <unistd.h> 21344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <errno.h> 22344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <string.h> 23344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <dirent.h> 24344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <errno.h> 25344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <fcntl.h> 26344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 27344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/types.h> 28344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/stat.h> 29344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/types.h> 30344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mman.h> 31344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mount.h> 322ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand#include <sys/wait.h> 33344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 34344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <linux/kdev_t.h> 35344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 36344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#define LOG_TAG "Vold" 37344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 38344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <cutils/log.h> 39344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <cutils/properties.h> 40344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 412ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand#include <logwrap/logwrap.h> 422ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand 43344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include "Ext4.h" 445593c856f49684f149116a6f2f28ddf9338b8557Rom Lemarchand#include "VoldUtil.h" 45344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 46344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#define MKEXT4FS_PATH "/system/bin/make_ext4fs"; 47344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 48344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount, 49344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root bool executable) { 50344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root int rc; 51344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root unsigned long flags; 52344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 53344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC; 54344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 55344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root flags |= (executable ? 0 : MS_NOEXEC); 56344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root flags |= (ro ? MS_RDONLY : 0); 57344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root flags |= (remount ? MS_REMOUNT : 0); 58344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 59344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root rc = mount(fsPath, mountPoint, "ext4", flags, NULL); 60344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 61344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root if (rc && errno == EROFS) { 62344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath); 63344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root flags |= MS_RDONLY; 64344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root rc = mount(fsPath, mountPoint, "ext4", flags, NULL); 65344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root } 66344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 67344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root return rc; 68344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root} 69344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 706a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenbergint Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) { 71344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root int fd; 726a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg const char *args[7]; 73344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root int rc; 742ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand int status; 75344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 76344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root args[0] = MKEXT4FS_PATH; 77344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root args[1] = "-J"; 78a54e13a3dca8ad15141a9f1084b6e121caeddce5rpcraig args[2] = "-a"; 79a54e13a3dca8ad15141a9f1084b6e121caeddce5rpcraig args[3] = mountpoint; 806a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg if (numSectors) { 816a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg char tmp[32]; 826a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg snprintf(tmp, sizeof(tmp), "%u", numSectors * 512); 836a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg const char *size = tmp; 846a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg args[4] = "-l"; 856a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg args[5] = size; 866a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg args[6] = fsPath; 876a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false, true); 886a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg } else { 896a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg args[4] = fsPath; 906a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg rc = android_fork_execvp(5, (char **)args, &status, false, true); 916a74dcaa6e646fea8e00b7c04332fc60fe7e017cDaniel Rosenberg } 922ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false, 932ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand true); 942ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand if (rc != 0) { 952ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand SLOGE("Filesystem (ext4) format failed due to logwrap error"); 962ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand errno = EIO; 972ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand return -1; 982ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand } 992ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand 1002ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand if (!WIFEXITED(status)) { 1012ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand SLOGE("Filesystem (ext4) format did not exit properly"); 1022ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand errno = EIO; 1032ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand return -1; 1042ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand } 1052ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand 1062ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand status = WEXITSTATUS(status); 107344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root 1082ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand if (status == 0) { 109344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root SLOGI("Filesystem (ext4) formatted OK"); 110344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root return 0; 111344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root } else { 1122ba45aafc22ff2fed91f8cd191beb85578b7b9bcRom Lemarchand SLOGE("Format (ext4) failed (unknown exit code %d)", status); 113344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root errno = EIO; 114344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root return -1; 115344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root } 116344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root return 0; 117344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root} 118