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 
17 #include "fs/Vfat.h"
18 #include "Devmapper.h"
19 #include "Loop.h"
20 #include "ObbVolume.h"
21 #include "Utils.h"
22 #include "VoldUtil.h"
23 
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>
34 #include <sys/types.h>
35 #include <sys/sysmacros.h>
36 #include <sys/wait.h>
37 
38 using android::base::StringPrintf;
39 using android::base::unique_fd;
40 
41 namespace android {
42 namespace vold {
43 
ObbVolume(int id,const std::string & sourcePath,const std::string & sourceKey,gid_t ownerGid)44 ObbVolume::ObbVolume(int id, const std::string& sourcePath, const std::string& sourceKey,
45         gid_t ownerGid) : VolumeBase(Type::kObb) {
46     setId(StringPrintf("obb:%d", id));
47     mSourcePath = sourcePath;
48     mSourceKey = sourceKey;
49     mOwnerGid = ownerGid;
50 }
51 
~ObbVolume()52 ObbVolume::~ObbVolume() {
53 }
54 
doCreate()55 status_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];
78         if (Devmapper::create(getId().c_str(), mLoopPath.c_str(), mSourceKey.c_str(), nr_sec,
79                 tmp, PATH_MAX)) {
80             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 
doDestroy()91 status_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 
doMount()103 status_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     }
111     if (android::vold::vfat::Mount(mMountPath, path,
112             true, false, true, 0, mOwnerGid, 0227, false)) {
113         PLOG(ERROR) << getId() << " failed to mount";
114         return -1;
115     }
116     return OK;
117 }
118 
doUnmount()119 status_t ObbVolume::doUnmount() {
120     auto path = getPath();
121 
122     KillProcessesUsingPath(path);
123     ForceUnmount(path);
124     rmdir(path.c_str());
125 
126     return OK;
127 }
128 
129 }  // namespace vold
130 }  // namespace android
131