Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 17 | #include "ObbVolume.h" |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 18 | #include "Loop.h" |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 19 | #include "Utils.h" |
| 20 | #include "VoldUtil.h" |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 21 | #include "fs/Vfat.h" |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 25 | #include <cutils/fs.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/mount.h> |
| 31 | #include <sys/stat.h> |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 32 | #include <sys/sysmacros.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 33 | #include <sys/types.h> |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 34 | #include <sys/wait.h> |
| 35 | |
| 36 | using android::base::StringPrintf; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace vold { |
| 40 | |
Eric Biggers | 7e79a43 | 2022-03-01 21:19:18 +0000 | [diff] [blame^] | 41 | ObbVolume::ObbVolume(int id, const std::string& sourcePath, gid_t ownerGid) |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 42 | : VolumeBase(Type::kObb) { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 43 | setId(StringPrintf("obb:%d", id)); |
| 44 | mSourcePath = sourcePath; |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 45 | mOwnerGid = ownerGid; |
| 46 | } |
| 47 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 48 | ObbVolume::~ObbVolume() {} |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 49 | |
| 50 | status_t ObbVolume::doCreate() { |
| 51 | if (Loop::create(mSourcePath, mLoopPath)) { |
| 52 | PLOG(ERROR) << getId() << " failed to create loop"; |
| 53 | return -1; |
| 54 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 55 | return OK; |
| 56 | } |
| 57 | |
| 58 | status_t ObbVolume::doDestroy() { |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 59 | if (!mLoopPath.empty() && Loop::destroyByDevice(mLoopPath.c_str())) { |
| 60 | PLOG(WARNING) << getId() << " failed to destroy loop"; |
| 61 | } |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 62 | mLoopPath.clear(); |
| 63 | return OK; |
| 64 | } |
| 65 | |
| 66 | status_t ObbVolume::doMount() { |
| 67 | auto path = StringPrintf("/mnt/obb/%s", getId().c_str()); |
| 68 | setPath(path); |
| 69 | |
| 70 | if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 71 | PLOG(ERROR) << getId() << " failed to create mount point"; |
| 72 | return -1; |
| 73 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 74 | // clang-format off |
Eric Biggers | 7e79a43 | 2022-03-01 21:19:18 +0000 | [diff] [blame^] | 75 | if (android::vold::vfat::Mount(mLoopPath, path, true, false, true, |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 76 | 0, mOwnerGid, 0227, false)) { |
| 77 | // clang-format on |
Jeff Sharkey | 11c2d38 | 2017-09-11 10:32:01 -0600 | [diff] [blame] | 78 | PLOG(ERROR) << getId() << " failed to mount"; |
| 79 | return -1; |
| 80 | } |
| 81 | return OK; |
| 82 | } |
| 83 | |
| 84 | status_t ObbVolume::doUnmount() { |
| 85 | auto path = getPath(); |
| 86 | |
| 87 | KillProcessesUsingPath(path); |
| 88 | ForceUnmount(path); |
| 89 | rmdir(path.c_str()); |
| 90 | |
| 91 | return OK; |
| 92 | } |
| 93 | |
| 94 | } // namespace vold |
| 95 | } // namespace android |