Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "QuotaUtils.h" |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <unordered_map> |
| 21 | |
| 22 | #include <sys/quota.h> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | |
| 26 | #include "utils.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace installd { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | std::recursive_mutex mMountsLock; |
| 34 | |
| 35 | /* Map of all quota mounts from target to source */ |
| 36 | std::unordered_map<std::string, std::string> mQuotaReverseMounts; |
| 37 | |
| 38 | std::string& FindQuotaDeviceForUuid(const std::string& uuid) { |
| 39 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 40 | auto path = create_data_path(uuid.empty() ? nullptr : uuid.c_str()); |
| 41 | return mQuotaReverseMounts[path]; |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | bool InvalidateQuotaMounts() { |
| 47 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 48 | |
| 49 | mQuotaReverseMounts.clear(); |
| 50 | |
| 51 | std::ifstream in("/proc/mounts"); |
| 52 | if (!in.is_open()) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | std::string source; |
| 57 | std::string target; |
| 58 | std::string ignored; |
| 59 | while (!in.eof()) { |
| 60 | std::getline(in, source, ' '); |
| 61 | std::getline(in, target, ' '); |
| 62 | std::getline(in, ignored); |
| 63 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame^] | 64 | if (target.compare(0, 13, "/data_mirror/") == 0) { |
| 65 | continue; |
| 66 | } |
| 67 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 68 | if (source.compare(0, 11, "/dev/block/") == 0) { |
| 69 | struct dqblk dq; |
| 70 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, |
| 71 | reinterpret_cast<char*>(&dq)) == 0) { |
| 72 | LOG(DEBUG) << "Found quota mount " << source << " at " << target; |
| 73 | mQuotaReverseMounts[target] = source; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool IsQuotaSupported(const std::string& uuid) { |
| 81 | return !FindQuotaDeviceForUuid(uuid).empty(); |
| 82 | } |
| 83 | |
| 84 | int64_t GetOccupiedSpaceForUid(const std::string& uuid, uid_t uid) { |
| 85 | const std::string device = FindQuotaDeviceForUuid(uuid); |
| 86 | if (device == "") { |
| 87 | return -1; |
| 88 | } |
| 89 | struct dqblk dq; |
| 90 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, |
| 91 | reinterpret_cast<char*>(&dq)) != 0) { |
| 92 | if (errno != ESRCH) { |
| 93 | PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; |
| 94 | } |
| 95 | return -1; |
| 96 | } else { |
| 97 | #if MEASURE_DEBUG |
| 98 | LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; |
| 99 | #endif |
| 100 | return dq.dqb_curspace; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | int64_t GetOccupiedSpaceForGid(const std::string& uuid, gid_t gid) { |
| 105 | const std::string device = FindQuotaDeviceForUuid(uuid); |
| 106 | if (device == "") { |
| 107 | return -1; |
| 108 | } |
| 109 | struct dqblk dq; |
| 110 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), gid, |
| 111 | reinterpret_cast<char*>(&dq)) != 0) { |
| 112 | if (errno != ESRCH) { |
| 113 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << gid; |
| 114 | } |
| 115 | return -1; |
| 116 | } else { |
| 117 | #if MEASURE_DEBUG |
| 118 | LOG(DEBUG) << "quotactl() for GID " << gid << " " << dq.dqb_curspace; |
| 119 | #endif |
| 120 | return dq.dqb_curspace; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
| 125 | } // namespace installd |
| 126 | } // namespace android |