blob: ec3d267f8e30652123ed50ba721839e56bbb168f [file] [log] [blame]
Jeff Sharkey11c2d382017-09-11 10:32:01 -06001/*
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 Crowley14c8c072018-09-18 13:30:21 -070017#include "ObbVolume.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060018#include "Devmapper.h"
19#include "Loop.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060020#include "Utils.h"
21#include "VoldUtil.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070022#include "fs/Vfat.h"
Jeff Sharkey11c2d382017-09-11 10:32:01 -060023
24#include <android-base/logging.h>
25#include <android-base/stringprintf.h>
26#include <android-base/unique_fd.h>
27#include <cutils/fs.h>
28#include <private/android_filesystem_config.h>
29
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/stat.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060034#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070035#include <sys/types.h>
Jeff Sharkey11c2d382017-09-11 10:32:01 -060036#include <sys/wait.h>
37
38using android::base::StringPrintf;
39using android::base::unique_fd;
40
41namespace android {
42namespace vold {
43
44ObbVolume::ObbVolume(int id, const std::string& sourcePath, const std::string& sourceKey,
Paul Crowley14c8c072018-09-18 13:30:21 -070045 gid_t ownerGid)
46 : VolumeBase(Type::kObb) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060047 setId(StringPrintf("obb:%d", id));
48 mSourcePath = sourcePath;
49 mSourceKey = sourceKey;
50 mOwnerGid = ownerGid;
51}
52
Paul Crowley14c8c072018-09-18 13:30:21 -070053ObbVolume::~ObbVolume() {}
Jeff Sharkey11c2d382017-09-11 10:32:01 -060054
55status_t ObbVolume::doCreate() {
56 if (Loop::create(mSourcePath, mLoopPath)) {
57 PLOG(ERROR) << getId() << " failed to create loop";
58 return -1;
59 }
60
61 if (!mSourceKey.empty()) {
62 unsigned long nr_sec = 0;
63 {
64 unique_fd loop_fd(open(mLoopPath.c_str(), O_RDWR | O_CLOEXEC));
65 if (loop_fd.get() == -1) {
66 PLOG(ERROR) << getId() << " failed to open loop";
67 return -1;
68 }
69
70 get_blkdev_size(loop_fd.get(), &nr_sec);
71 if (nr_sec == 0) {
72 PLOG(ERROR) << getId() << " failed to get loop size";
73 return -1;
74 }
75 }
76
77 char tmp[PATH_MAX];
Paul Crowley14c8c072018-09-18 13:30:21 -070078 if (Devmapper::create(getId().c_str(), mLoopPath.c_str(), mSourceKey.c_str(), nr_sec, tmp,
79 PATH_MAX)) {
Jeff Sharkey11c2d382017-09-11 10:32:01 -060080 PLOG(ERROR) << getId() << " failed to create dm";
81 return -1;
82 }
83 mDmPath = tmp;
84 mMountPath = mDmPath;
85 } else {
86 mMountPath = mLoopPath;
87 }
88 return OK;
89}
90
91status_t ObbVolume::doDestroy() {
92 if (!mDmPath.empty() && Devmapper::destroy(getId().c_str())) {
93 PLOG(WARNING) << getId() << " failed to destroy dm";
94 }
95 if (!mLoopPath.empty() && Loop::destroyByDevice(mLoopPath.c_str())) {
96 PLOG(WARNING) << getId() << " failed to destroy loop";
97 }
98 mDmPath.clear();
99 mLoopPath.clear();
100 return OK;
101}
102
103status_t ObbVolume::doMount() {
104 auto path = StringPrintf("/mnt/obb/%s", getId().c_str());
105 setPath(path);
106
107 if (fs_prepare_dir(path.c_str(), 0700, AID_ROOT, AID_ROOT)) {
108 PLOG(ERROR) << getId() << " failed to create mount point";
109 return -1;
110 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700111 // clang-format off
112 if (android::vold::vfat::Mount(mMountPath, path, true, false, true,
113 0, mOwnerGid, 0227, false)) {
114 // clang-format on
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600115 PLOG(ERROR) << getId() << " failed to mount";
116 return -1;
117 }
118 return OK;
119}
120
121status_t ObbVolume::doUnmount() {
122 auto path = getPath();
123
124 KillProcessesUsingPath(path);
125 ForceUnmount(path);
126 rmdir(path.c_str());
127
128 return OK;
129}
130
131} // namespace vold
132} // namespace android