fs_mgr_overlayfs.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. #include <dirent.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <linux/fs.h>
  20. #include <selinux/selinux.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/mount.h>
  25. #include <sys/param.h>
  26. #include <sys/stat.h>
  27. #include <sys/statvfs.h>
  28. #include <sys/types.h>
  29. #include <sys/utsname.h>
  30. #include <sys/vfs.h>
  31. #include <unistd.h>
  32. #include <algorithm>
  33. #include <memory>
  34. #include <string>
  35. #include <vector>
  36. #include <android-base/file.h>
  37. #include <android-base/macros.h>
  38. #include <android-base/properties.h>
  39. #include <android-base/strings.h>
  40. #include <android-base/unique_fd.h>
  41. #include <ext4_utils/ext4_utils.h>
  42. #include <fs_mgr.h>
  43. #include <fs_mgr_dm_linear.h>
  44. #include <fs_mgr_overlayfs.h>
  45. #include <fstab/fstab.h>
  46. #include <libdm/dm.h>
  47. #include <libgsi/libgsi.h>
  48. #include <liblp/builder.h>
  49. #include <liblp/liblp.h>
  50. #include "fs_mgr_priv.h"
  51. using namespace std::literals;
  52. using namespace android::dm;
  53. using namespace android::fs_mgr;
  54. namespace {
  55. bool fs_mgr_access(const std::string& path) {
  56. auto save_errno = errno;
  57. auto ret = access(path.c_str(), F_OK) == 0;
  58. errno = save_errno;
  59. return ret;
  60. }
  61. // determine if a filesystem is available
  62. bool fs_mgr_overlayfs_filesystem_available(const std::string& filesystem) {
  63. std::string filesystems;
  64. if (!android::base::ReadFileToString("/proc/filesystems", &filesystems)) return false;
  65. return filesystems.find("\t" + filesystem + "\n") != std::string::npos;
  66. }
  67. } // namespace
  68. #if ALLOW_ADBD_DISABLE_VERITY == 0 // If we are a user build, provide stubs
  69. Fstab fs_mgr_overlayfs_candidate_list(const Fstab&) {
  70. return {};
  71. }
  72. bool fs_mgr_overlayfs_mount_all(Fstab*) {
  73. return false;
  74. }
  75. std::vector<std::string> fs_mgr_overlayfs_required_devices(Fstab*) {
  76. return {};
  77. }
  78. bool fs_mgr_overlayfs_setup(const char*, const char*, bool* change, bool) {
  79. if (change) *change = false;
  80. return false;
  81. }
  82. bool fs_mgr_overlayfs_teardown(const char*, bool* change) {
  83. if (change) *change = false;
  84. return false;
  85. }
  86. bool fs_mgr_overlayfs_is_setup() {
  87. return false;
  88. }
  89. #else // ALLOW_ADBD_DISABLE_VERITY == 0
  90. namespace {
  91. // list of acceptable overlayfs backing storage
  92. const auto kScratchMountPoint = "/mnt/scratch"s;
  93. const auto kCacheMountPoint = "/cache"s;
  94. const std::vector<const std::string> kOverlayMountPoints = {kScratchMountPoint, kCacheMountPoint};
  95. // Return true if everything is mounted, but before adb is started. Right
  96. // after 'trigger load_persist_props_action' is done.
  97. bool fs_mgr_boot_completed() {
  98. return android::base::GetBoolProperty("ro.persistent_properties.ready", false);
  99. }
  100. bool fs_mgr_is_dir(const std::string& path) {
  101. struct stat st;
  102. return !stat(path.c_str(), &st) && S_ISDIR(st.st_mode);
  103. }
  104. // Similar test as overlayfs workdir= validation in the kernel for read-write
  105. // validation, except we use fs_mgr_work. Covers space and storage issues.
  106. bool fs_mgr_dir_is_writable(const std::string& path) {
  107. auto test_directory = path + "/fs_mgr_work";
  108. rmdir(test_directory.c_str());
  109. auto ret = !mkdir(test_directory.c_str(), 0700);
  110. return ret | !rmdir(test_directory.c_str());
  111. }
  112. // At less than 1% free space return value of false,
  113. // means we will try to wrap with overlayfs.
  114. bool fs_mgr_filesystem_has_space(const std::string& mount_point) {
  115. // If we have access issues to find out space remaining, return true
  116. // to prevent us trying to override with overlayfs.
  117. struct statvfs vst;
  118. auto save_errno = errno;
  119. if (statvfs(mount_point.c_str(), &vst)) {
  120. errno = save_errno;
  121. return true;
  122. }
  123. static constexpr int kPercentThreshold = 1; // 1%
  124. return (vst.f_bfree >= (vst.f_blocks * kPercentThreshold / 100));
  125. }
  126. bool fs_mgr_overlayfs_enabled(FstabEntry* entry) {
  127. // readonly filesystem, can not be mount -o remount,rw
  128. // for squashfs, erofs or if free space is (near) zero making such a remount
  129. // virtually useless, or if there are shared blocks that prevent remount,rw
  130. if (!fs_mgr_filesystem_has_space(entry->mount_point)) {
  131. return true;
  132. }
  133. if (entry->fs_mgr_flags.logical) {
  134. fs_mgr_update_logical_partition(entry);
  135. }
  136. auto save_errno = errno;
  137. errno = 0;
  138. auto has_shared_blocks = fs_mgr_has_shared_blocks(entry->mount_point, entry->blk_device);
  139. if (!has_shared_blocks && (entry->mount_point == "/system")) {
  140. has_shared_blocks = fs_mgr_has_shared_blocks("/", entry->blk_device);
  141. }
  142. // special case for first stage init for system as root (taimen)
  143. if (!has_shared_blocks && (errno == ENOENT) && (entry->blk_device == "/dev/root")) {
  144. has_shared_blocks = true;
  145. }
  146. errno = save_errno;
  147. return has_shared_blocks;
  148. }
  149. bool fs_mgr_rm_all(const std::string& path, bool* change = nullptr, int level = 0) {
  150. auto save_errno = errno;
  151. std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
  152. if (!dir) {
  153. if (errno == ENOENT) {
  154. errno = save_errno;
  155. return true;
  156. }
  157. PERROR << "opendir " << path << " depth=" << level;
  158. if ((errno == EPERM) && (level != 0)) {
  159. errno = save_errno;
  160. return true;
  161. }
  162. return false;
  163. }
  164. dirent* entry;
  165. auto ret = true;
  166. while ((entry = readdir(dir.get()))) {
  167. if (("."s == entry->d_name) || (".."s == entry->d_name)) continue;
  168. auto file = path + "/" + entry->d_name;
  169. if (entry->d_type == DT_UNKNOWN) {
  170. struct stat st;
  171. save_errno = errno;
  172. if (!lstat(file.c_str(), &st) && (st.st_mode & S_IFDIR)) entry->d_type = DT_DIR;
  173. errno = save_errno;
  174. }
  175. if (entry->d_type == DT_DIR) {
  176. ret &= fs_mgr_rm_all(file, change, level + 1);
  177. if (!rmdir(file.c_str())) {
  178. if (change) *change = true;
  179. } else {
  180. if (errno != ENOENT) ret = false;
  181. PERROR << "rmdir " << file << " depth=" << level;
  182. }
  183. continue;
  184. }
  185. if (!unlink(file.c_str())) {
  186. if (change) *change = true;
  187. } else {
  188. if (errno != ENOENT) ret = false;
  189. PERROR << "rm " << file << " depth=" << level;
  190. }
  191. }
  192. return ret;
  193. }
  194. const auto kUpperName = "upper"s;
  195. const auto kWorkName = "work"s;
  196. const auto kOverlayTopDir = "/overlay"s;
  197. std::string fs_mgr_get_overlayfs_candidate(const std::string& mount_point) {
  198. if (!fs_mgr_is_dir(mount_point)) return "";
  199. const auto base = android::base::Basename(mount_point) + "/";
  200. for (const auto& overlay_mount_point : kOverlayMountPoints) {
  201. auto dir = overlay_mount_point + kOverlayTopDir + "/" + base;
  202. auto upper = dir + kUpperName;
  203. if (!fs_mgr_is_dir(upper)) continue;
  204. auto work = dir + kWorkName;
  205. if (!fs_mgr_is_dir(work)) continue;
  206. if (!fs_mgr_dir_is_writable(work)) continue;
  207. return dir;
  208. }
  209. return "";
  210. }
  211. const auto kLowerdirOption = "lowerdir="s;
  212. const auto kUpperdirOption = "upperdir="s;
  213. // default options for mount_point, returns empty string for none available.
  214. std::string fs_mgr_get_overlayfs_options(const std::string& mount_point) {
  215. auto candidate = fs_mgr_get_overlayfs_candidate(mount_point);
  216. if (candidate.empty()) return "";
  217. auto ret = kLowerdirOption + mount_point + "," + kUpperdirOption + candidate + kUpperName +
  218. ",workdir=" + candidate + kWorkName;
  219. if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kOverrideCredsRequired) {
  220. ret += ",override_creds=off";
  221. }
  222. return ret;
  223. }
  224. const std::string fs_mgr_mount_point(const std::string& mount_point) {
  225. if ("/"s != mount_point) return mount_point;
  226. return "/system";
  227. }
  228. bool fs_mgr_rw_access(const std::string& path) {
  229. if (path.empty()) return false;
  230. auto save_errno = errno;
  231. auto ret = access(path.c_str(), R_OK | W_OK) == 0;
  232. errno = save_errno;
  233. return ret;
  234. }
  235. bool fs_mgr_overlayfs_already_mounted(const std::string& mount_point, bool overlay_only = true) {
  236. Fstab fstab;
  237. auto save_errno = errno;
  238. if (!ReadFstabFromFile("/proc/mounts", &fstab)) {
  239. return false;
  240. }
  241. errno = save_errno;
  242. const auto lowerdir = kLowerdirOption + mount_point;
  243. for (const auto& entry : fstab) {
  244. if (overlay_only && "overlay" != entry.fs_type && "overlayfs" != entry.fs_type) continue;
  245. if (mount_point != entry.mount_point) continue;
  246. if (!overlay_only) return true;
  247. const auto options = android::base::Split(entry.fs_options, ",");
  248. for (const auto& opt : options) {
  249. if (opt == lowerdir) {
  250. return true;
  251. }
  252. }
  253. }
  254. return false;
  255. }
  256. bool fs_mgr_wants_overlayfs(FstabEntry* entry) {
  257. // Don't check entries that are managed by vold.
  258. if (entry->fs_mgr_flags.vold_managed || entry->fs_mgr_flags.recovery_only) return false;
  259. // *_other doesn't want overlayfs.
  260. if (entry->fs_mgr_flags.slot_select_other) return false;
  261. // Only concerned with readonly partitions.
  262. if (!(entry->flags & MS_RDONLY)) return false;
  263. // If unbindable, do not allow overlayfs as this could expose us to
  264. // security issues. On Android, this could also be used to turn off
  265. // the ability to overlay an otherwise acceptable filesystem since
  266. // /system and /vendor are never bound(sic) to.
  267. if (entry->flags & MS_UNBINDABLE) return false;
  268. if (!fs_mgr_overlayfs_enabled(entry)) return false;
  269. return true;
  270. }
  271. constexpr char kOverlayfsFileContext[] = "u:object_r:overlayfs_file:s0";
  272. bool fs_mgr_overlayfs_setup_dir(const std::string& dir, std::string* overlay, bool* change) {
  273. auto ret = true;
  274. auto top = dir + kOverlayTopDir;
  275. if (setfscreatecon(kOverlayfsFileContext)) {
  276. ret = false;
  277. PERROR << "setfscreatecon " << kOverlayfsFileContext;
  278. }
  279. auto save_errno = errno;
  280. if (!mkdir(top.c_str(), 0755)) {
  281. if (change) *change = true;
  282. } else if (errno != EEXIST) {
  283. ret = false;
  284. PERROR << "mkdir " << top;
  285. } else {
  286. errno = save_errno;
  287. }
  288. setfscreatecon(nullptr);
  289. if (overlay) *overlay = std::move(top);
  290. return ret;
  291. }
  292. bool fs_mgr_overlayfs_setup_one(const std::string& overlay, const std::string& mount_point,
  293. bool* change) {
  294. auto ret = true;
  295. if (fs_mgr_overlayfs_already_mounted(mount_point)) return ret;
  296. auto fsrec_mount_point = overlay + "/" + android::base::Basename(mount_point) + "/";
  297. if (setfscreatecon(kOverlayfsFileContext)) {
  298. ret = false;
  299. PERROR << "setfscreatecon " << kOverlayfsFileContext;
  300. }
  301. auto save_errno = errno;
  302. if (!mkdir(fsrec_mount_point.c_str(), 0755)) {
  303. if (change) *change = true;
  304. } else if (errno != EEXIST) {
  305. ret = false;
  306. PERROR << "mkdir " << fsrec_mount_point;
  307. } else {
  308. errno = save_errno;
  309. }
  310. save_errno = errno;
  311. if (!mkdir((fsrec_mount_point + kWorkName).c_str(), 0755)) {
  312. if (change) *change = true;
  313. } else if (errno != EEXIST) {
  314. ret = false;
  315. PERROR << "mkdir " << fsrec_mount_point << kWorkName;
  316. } else {
  317. errno = save_errno;
  318. }
  319. setfscreatecon(nullptr);
  320. auto new_context = fs_mgr_get_context(mount_point);
  321. if (!new_context.empty() && setfscreatecon(new_context.c_str())) {
  322. ret = false;
  323. PERROR << "setfscreatecon " << new_context;
  324. }
  325. auto upper = fsrec_mount_point + kUpperName;
  326. save_errno = errno;
  327. if (!mkdir(upper.c_str(), 0755)) {
  328. if (change) *change = true;
  329. } else if (errno != EEXIST) {
  330. ret = false;
  331. PERROR << "mkdir " << upper;
  332. } else {
  333. errno = save_errno;
  334. }
  335. if (!new_context.empty()) setfscreatecon(nullptr);
  336. return ret;
  337. }
  338. uint32_t fs_mgr_overlayfs_slot_number() {
  339. return SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix());
  340. }
  341. const auto kPhysicalDevice = "/dev/block/by-name/"s;
  342. std::string fs_mgr_overlayfs_super_device(uint32_t slot_number) {
  343. return kPhysicalDevice + fs_mgr_get_super_partition_name(slot_number);
  344. }
  345. bool fs_mgr_overlayfs_has_logical(const Fstab& fstab) {
  346. for (const auto& entry : fstab) {
  347. if (entry.fs_mgr_flags.logical) {
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. void fs_mgr_overlayfs_umount_scratch() {
  354. // Lazy umount will allow us to move on and possibly later
  355. // establish a new fresh mount without requiring a reboot should
  356. // the developer wish to restart. Old references should melt
  357. // away or have no data. Main goal is to shut the door on the
  358. // current overrides with an expectation of a subsequent reboot,
  359. // thus any errors here are ignored.
  360. umount2(kScratchMountPoint.c_str(), MNT_DETACH);
  361. LINFO << "umount(" << kScratchMountPoint << ")";
  362. rmdir(kScratchMountPoint.c_str());
  363. }
  364. // reduce 'DM_DEV_STATUS failed for scratch: No such device or address' noise
  365. std::string scratch_device_cache;
  366. bool fs_mgr_overlayfs_teardown_scratch(const std::string& overlay, bool* change) {
  367. // umount and delete kScratchMountPoint storage if we have logical partitions
  368. if (overlay != kScratchMountPoint) return true;
  369. scratch_device_cache.erase();
  370. auto slot_number = fs_mgr_overlayfs_slot_number();
  371. auto super_device = fs_mgr_overlayfs_super_device(slot_number);
  372. if (!fs_mgr_rw_access(super_device)) return true;
  373. auto save_errno = errno;
  374. if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) {
  375. fs_mgr_overlayfs_umount_scratch();
  376. }
  377. auto builder = MetadataBuilder::New(super_device, slot_number);
  378. if (!builder) {
  379. errno = save_errno;
  380. return true;
  381. }
  382. const auto partition_name = android::base::Basename(kScratchMountPoint);
  383. if (builder->FindPartition(partition_name) == nullptr) {
  384. errno = save_errno;
  385. return true;
  386. }
  387. builder->RemovePartition(partition_name);
  388. auto metadata = builder->Export();
  389. if (metadata && UpdatePartitionTable(super_device, *metadata.get(), slot_number)) {
  390. if (change) *change = true;
  391. if (!DestroyLogicalPartition(partition_name, 0s)) return false;
  392. } else {
  393. LERROR << "delete partition " << overlay;
  394. return false;
  395. }
  396. errno = save_errno;
  397. return true;
  398. }
  399. bool fs_mgr_overlayfs_teardown_one(const std::string& overlay, const std::string& mount_point,
  400. bool* change) {
  401. const auto top = overlay + kOverlayTopDir;
  402. if (!fs_mgr_access(top)) return fs_mgr_overlayfs_teardown_scratch(overlay, change);
  403. auto cleanup_all = mount_point.empty();
  404. const auto partition_name = android::base::Basename(mount_point);
  405. const auto oldpath = top + (cleanup_all ? "" : ("/" + partition_name));
  406. const auto newpath = cleanup_all ? overlay + "/." + kOverlayTopDir.substr(1) + ".teardown"
  407. : top + "/." + partition_name + ".teardown";
  408. auto ret = fs_mgr_rm_all(newpath);
  409. auto save_errno = errno;
  410. if (!rename(oldpath.c_str(), newpath.c_str())) {
  411. if (change) *change = true;
  412. } else if (errno != ENOENT) {
  413. ret = false;
  414. PERROR << "mv " << oldpath << " " << newpath;
  415. } else {
  416. errno = save_errno;
  417. }
  418. ret &= fs_mgr_rm_all(newpath, change);
  419. save_errno = errno;
  420. if (!rmdir(newpath.c_str())) {
  421. if (change) *change = true;
  422. } else if (errno != ENOENT) {
  423. ret = false;
  424. PERROR << "rmdir " << newpath;
  425. } else {
  426. errno = save_errno;
  427. }
  428. if (!cleanup_all) {
  429. save_errno = errno;
  430. if (!rmdir(top.c_str())) {
  431. if (change) *change = true;
  432. cleanup_all = true;
  433. } else if (errno == ENOTEMPTY) {
  434. cleanup_all = true;
  435. // cleanup all if the content is all hidden (leading .)
  436. std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(top.c_str()), closedir);
  437. if (!dir) {
  438. PERROR << "opendir " << top;
  439. } else {
  440. dirent* entry;
  441. while ((entry = readdir(dir.get()))) {
  442. if (entry->d_name[0] != '.') {
  443. cleanup_all = false;
  444. break;
  445. }
  446. }
  447. }
  448. errno = save_errno;
  449. } else if (errno == ENOENT) {
  450. cleanup_all = true;
  451. errno = save_errno;
  452. } else {
  453. ret = false;
  454. PERROR << "rmdir " << top;
  455. }
  456. }
  457. if (cleanup_all) ret &= fs_mgr_overlayfs_teardown_scratch(overlay, change);
  458. return ret;
  459. }
  460. bool fs_mgr_overlayfs_set_shared_mount(const std::string& mount_point, bool shared_flag) {
  461. auto ret = mount(nullptr, mount_point.c_str(), nullptr, shared_flag ? MS_SHARED : MS_PRIVATE,
  462. nullptr);
  463. if (ret) {
  464. PERROR << "__mount(target=" << mount_point
  465. << ",flag=" << (shared_flag ? "MS_SHARED" : "MS_PRIVATE") << ")=" << ret;
  466. return false;
  467. }
  468. return true;
  469. }
  470. bool fs_mgr_overlayfs_move_mount(const std::string& source, const std::string& target) {
  471. auto ret = mount(source.c_str(), target.c_str(), nullptr, MS_MOVE, nullptr);
  472. if (ret) {
  473. PERROR << "__mount(source=" << source << ",target=" << target << ",flag=MS_MOVE)=" << ret;
  474. return false;
  475. }
  476. return true;
  477. }
  478. struct mount_info {
  479. std::string mount_point;
  480. bool shared_flag;
  481. };
  482. std::vector<mount_info> ReadMountinfoFromFile(const std::string& path) {
  483. std::vector<mount_info> info;
  484. auto file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
  485. if (!file) {
  486. PERROR << __FUNCTION__ << "(): cannot open file: '" << path << "'";
  487. return info;
  488. }
  489. ssize_t len;
  490. size_t alloc_len = 0;
  491. char* line = nullptr;
  492. while ((len = getline(&line, &alloc_len, file.get())) != -1) {
  493. /* if the last character is a newline, shorten the string by 1 byte */
  494. if (line[len - 1] == '\n') {
  495. line[len - 1] = '\0';
  496. }
  497. static constexpr char delim[] = " \t";
  498. char* save_ptr;
  499. if (!strtok_r(line, delim, &save_ptr)) {
  500. LERROR << "Error parsing mount ID";
  501. break;
  502. }
  503. if (!strtok_r(nullptr, delim, &save_ptr)) {
  504. LERROR << "Error parsing parent ID";
  505. break;
  506. }
  507. if (!strtok_r(nullptr, delim, &save_ptr)) {
  508. LERROR << "Error parsing mount source";
  509. break;
  510. }
  511. if (!strtok_r(nullptr, delim, &save_ptr)) {
  512. LERROR << "Error parsing root";
  513. break;
  514. }
  515. char* p;
  516. if (!(p = strtok_r(nullptr, delim, &save_ptr))) {
  517. LERROR << "Error parsing mount_point";
  518. break;
  519. }
  520. mount_info entry = {p, false};
  521. if (!strtok_r(nullptr, delim, &save_ptr)) {
  522. LERROR << "Error parsing mount_flags";
  523. break;
  524. }
  525. while ((p = strtok_r(nullptr, delim, &save_ptr))) {
  526. if ((p[0] == '-') && (p[1] == '\0')) break;
  527. if (android::base::StartsWith(p, "shared:")) entry.shared_flag = true;
  528. }
  529. if (!p) {
  530. LERROR << "Error parsing fields";
  531. break;
  532. }
  533. info.emplace_back(std::move(entry));
  534. }
  535. free(line);
  536. if (info.empty()) {
  537. LERROR << __FUNCTION__ << "(): failed to load mountinfo from : '" << path << "'";
  538. }
  539. return info;
  540. }
  541. bool fs_mgr_overlayfs_mount(const std::string& mount_point) {
  542. auto options = fs_mgr_get_overlayfs_options(mount_point);
  543. if (options.empty()) return false;
  544. auto retval = true;
  545. auto save_errno = errno;
  546. struct move_entry {
  547. std::string mount_point;
  548. std::string dir;
  549. bool shared_flag;
  550. };
  551. std::vector<move_entry> move;
  552. auto parent_private = false;
  553. auto parent_made_private = false;
  554. auto dev_private = false;
  555. auto dev_made_private = false;
  556. for (auto& entry : ReadMountinfoFromFile("/proc/self/mountinfo")) {
  557. if ((entry.mount_point == mount_point) && !entry.shared_flag) {
  558. parent_private = true;
  559. }
  560. if ((entry.mount_point == "/dev") && !entry.shared_flag) {
  561. dev_private = true;
  562. }
  563. if (!android::base::StartsWith(entry.mount_point, mount_point + "/")) {
  564. continue;
  565. }
  566. if (std::find_if(move.begin(), move.end(), [&entry](const auto& it) {
  567. return android::base::StartsWith(entry.mount_point, it.mount_point + "/");
  568. }) != move.end()) {
  569. continue;
  570. }
  571. // use as the bound directory in /dev.
  572. auto new_context = fs_mgr_get_context(entry.mount_point);
  573. if (!new_context.empty() && setfscreatecon(new_context.c_str())) {
  574. PERROR << "setfscreatecon " << new_context;
  575. }
  576. move_entry new_entry = {std::move(entry.mount_point), "/dev/TemporaryDir-XXXXXX",
  577. entry.shared_flag};
  578. const auto target = mkdtemp(new_entry.dir.data());
  579. if (!target) {
  580. retval = false;
  581. save_errno = errno;
  582. PERROR << "temporary directory for MS_BIND";
  583. setfscreatecon(nullptr);
  584. continue;
  585. }
  586. setfscreatecon(nullptr);
  587. if (!parent_private && !parent_made_private) {
  588. parent_made_private = fs_mgr_overlayfs_set_shared_mount(mount_point, false);
  589. }
  590. if (new_entry.shared_flag) {
  591. new_entry.shared_flag = fs_mgr_overlayfs_set_shared_mount(new_entry.mount_point, false);
  592. }
  593. if (!fs_mgr_overlayfs_move_mount(new_entry.mount_point, new_entry.dir)) {
  594. retval = false;
  595. save_errno = errno;
  596. if (new_entry.shared_flag) {
  597. fs_mgr_overlayfs_set_shared_mount(new_entry.mount_point, true);
  598. }
  599. continue;
  600. }
  601. move.emplace_back(std::move(new_entry));
  602. }
  603. // hijack __mount() report format to help triage
  604. auto report = "__mount(source=overlay,target="s + mount_point + ",type=overlay";
  605. const auto opt_list = android::base::Split(options, ",");
  606. for (const auto& opt : opt_list) {
  607. if (android::base::StartsWith(opt, kUpperdirOption)) {
  608. report = report + "," + opt;
  609. break;
  610. }
  611. }
  612. report = report + ")=";
  613. auto ret = mount("overlay", mount_point.c_str(), "overlay", MS_RDONLY | MS_RELATIME,
  614. options.c_str());
  615. if (ret) {
  616. retval = false;
  617. save_errno = errno;
  618. PERROR << report << ret;
  619. } else {
  620. LINFO << report << ret;
  621. }
  622. // Move submounts back.
  623. for (const auto& entry : move) {
  624. if (!dev_private && !dev_made_private) {
  625. dev_made_private = fs_mgr_overlayfs_set_shared_mount("/dev", false);
  626. }
  627. if (!fs_mgr_overlayfs_move_mount(entry.dir, entry.mount_point)) {
  628. retval = false;
  629. save_errno = errno;
  630. } else if (entry.shared_flag &&
  631. !fs_mgr_overlayfs_set_shared_mount(entry.mount_point, true)) {
  632. retval = false;
  633. save_errno = errno;
  634. }
  635. rmdir(entry.dir.c_str());
  636. }
  637. if (dev_made_private) {
  638. fs_mgr_overlayfs_set_shared_mount("/dev", true);
  639. }
  640. if (parent_made_private) {
  641. fs_mgr_overlayfs_set_shared_mount(mount_point, true);
  642. }
  643. errno = save_errno;
  644. return retval;
  645. }
  646. // Mount kScratchMountPoint
  647. bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::string mnt_type,
  648. bool readonly = false) {
  649. if (readonly) {
  650. if (!fs_mgr_access(device_path)) return false;
  651. } else {
  652. if (!fs_mgr_rw_access(device_path)) return false;
  653. }
  654. auto f2fs = fs_mgr_is_f2fs(device_path);
  655. auto ext4 = fs_mgr_is_ext4(device_path);
  656. if (!f2fs && !ext4) return false;
  657. if (setfscreatecon(kOverlayfsFileContext)) {
  658. PERROR << "setfscreatecon " << kOverlayfsFileContext;
  659. }
  660. if (mkdir(kScratchMountPoint.c_str(), 0755) && (errno != EEXIST)) {
  661. PERROR << "create " << kScratchMountPoint;
  662. }
  663. FstabEntry entry;
  664. entry.blk_device = device_path;
  665. entry.mount_point = kScratchMountPoint;
  666. entry.fs_type = mnt_type;
  667. if ((mnt_type == "f2fs") && !f2fs) entry.fs_type = "ext4";
  668. if ((mnt_type == "ext4") && !ext4) entry.fs_type = "f2fs";
  669. entry.flags = MS_RELATIME;
  670. if (readonly) {
  671. entry.flags |= MS_RDONLY;
  672. } else {
  673. fs_mgr_set_blk_ro(device_path, false);
  674. }
  675. auto save_errno = errno;
  676. auto mounted = fs_mgr_do_mount_one(entry) == 0;
  677. if (!mounted) {
  678. if ((entry.fs_type == "f2fs") && ext4) {
  679. entry.fs_type = "ext4";
  680. mounted = fs_mgr_do_mount_one(entry) == 0;
  681. } else if ((entry.fs_type == "ext4") && f2fs) {
  682. entry.fs_type = "f2fs";
  683. mounted = fs_mgr_do_mount_one(entry) == 0;
  684. }
  685. if (!mounted) save_errno = errno;
  686. }
  687. setfscreatecon(nullptr);
  688. if (!mounted) rmdir(kScratchMountPoint.c_str());
  689. errno = save_errno;
  690. return mounted;
  691. }
  692. const std::string kMkF2fs("/system/bin/make_f2fs");
  693. const std::string kMkExt4("/system/bin/mke2fs");
  694. // Only a suggestion for _first_ try during mounting
  695. std::string fs_mgr_overlayfs_scratch_mount_type() {
  696. if (!access(kMkF2fs.c_str(), X_OK) && fs_mgr_overlayfs_filesystem_available("f2fs")) {
  697. return "f2fs";
  698. }
  699. if (!access(kMkExt4.c_str(), X_OK) && fs_mgr_overlayfs_filesystem_available("ext4")) {
  700. return "ext4";
  701. }
  702. return "auto";
  703. }
  704. std::string fs_mgr_overlayfs_scratch_device() {
  705. if (!scratch_device_cache.empty()) return scratch_device_cache;
  706. // Is this a multiple super device (retrofit)?
  707. auto slot_number = fs_mgr_overlayfs_slot_number();
  708. auto super_device = fs_mgr_overlayfs_super_device(slot_number);
  709. auto path = fs_mgr_overlayfs_super_device(slot_number == 0);
  710. if (super_device == path) {
  711. // Create from within single super device;
  712. auto& dm = DeviceMapper::Instance();
  713. const auto partition_name = android::base::Basename(kScratchMountPoint);
  714. if (!dm.GetDmDevicePathByName(partition_name, &path)) {
  715. // non-DAP A/B device?
  716. if (fs_mgr_access(super_device)) return "";
  717. auto other_slot = fs_mgr_get_other_slot_suffix();
  718. if (other_slot.empty()) return "";
  719. path = kPhysicalDevice + "system" + other_slot;
  720. }
  721. }
  722. return scratch_device_cache = path;
  723. }
  724. bool fs_mgr_overlayfs_make_scratch(const std::string& scratch_device, const std::string& mnt_type) {
  725. // Force mkfs by design for overlay support of adb remount, simplify and
  726. // thus do not rely on fsck to correct problems that could creep in.
  727. auto command = ""s;
  728. if (mnt_type == "f2fs") {
  729. command = kMkF2fs + " -w 4096 -f -d1 -l" + android::base::Basename(kScratchMountPoint);
  730. } else if (mnt_type == "ext4") {
  731. command = kMkExt4 + " -F -b 4096 -t ext4 -m 0 -O has_journal -M " + kScratchMountPoint;
  732. } else {
  733. errno = ESRCH;
  734. LERROR << mnt_type << " has no mkfs cookbook";
  735. return false;
  736. }
  737. command += " " + scratch_device + " >/dev/null 2>/dev/null </dev/null";
  738. fs_mgr_set_blk_ro(scratch_device, false);
  739. auto ret = system(command.c_str());
  740. if (ret) {
  741. LERROR << "make " << mnt_type << " filesystem on " << scratch_device << " return=" << ret;
  742. return false;
  743. }
  744. return true;
  745. }
  746. bool fs_mgr_overlayfs_create_scratch(const Fstab& fstab, std::string* scratch_device,
  747. bool* partition_exists, bool* change) {
  748. *scratch_device = fs_mgr_overlayfs_scratch_device();
  749. *partition_exists = fs_mgr_rw_access(*scratch_device);
  750. auto partition_create = !*partition_exists;
  751. // Do we need to create a logical "scratch" partition?
  752. if (!partition_create && android::base::StartsWith(*scratch_device, kPhysicalDevice)) {
  753. return true;
  754. }
  755. auto slot_number = fs_mgr_overlayfs_slot_number();
  756. auto super_device = fs_mgr_overlayfs_super_device(slot_number);
  757. if (!fs_mgr_rw_access(super_device)) return false;
  758. if (!fs_mgr_overlayfs_has_logical(fstab)) return false;
  759. auto builder = MetadataBuilder::New(super_device, slot_number);
  760. if (!builder) {
  761. LERROR << "open " << super_device << " metadata";
  762. return false;
  763. }
  764. const auto partition_name = android::base::Basename(kScratchMountPoint);
  765. auto partition = builder->FindPartition(partition_name);
  766. *partition_exists = partition != nullptr;
  767. auto changed = false;
  768. if (!*partition_exists) {
  769. partition = builder->AddPartition(partition_name, LP_PARTITION_ATTR_NONE);
  770. if (!partition) {
  771. LERROR << "create " << partition_name;
  772. return false;
  773. }
  774. changed = true;
  775. }
  776. // Take half of free space, minimum 512MB or maximum free - margin.
  777. static constexpr auto kMinimumSize = uint64_t(512 * 1024 * 1024);
  778. if (partition->size() < kMinimumSize) {
  779. auto partition_size =
  780. builder->AllocatableSpace() - builder->UsedSpace() + partition->size();
  781. if ((partition_size > kMinimumSize) || !partition->size()) {
  782. // Leave some space for free space jitter of a few erase
  783. // blocks, in case they are needed for any individual updates
  784. // to any other partition that needs to be flashed while
  785. // overlayfs is in force. Of course if margin_size is not
  786. // enough could normally get a flash failure, so
  787. // ResizePartition() will delete the scratch partition in
  788. // order to fulfill. Deleting scratch will destroy all of
  789. // the adb remount overrides :-( .
  790. auto margin_size = uint64_t(3 * 256 * 1024);
  791. BlockDeviceInfo info;
  792. if (builder->GetBlockDeviceInfo(partition_name, &info)) {
  793. margin_size = 3 * info.logical_block_size;
  794. }
  795. partition_size = std::max(std::min(kMinimumSize, partition_size - margin_size),
  796. partition_size / 2);
  797. if (partition_size > partition->size()) {
  798. if (!builder->ResizePartition(partition, partition_size)) {
  799. LERROR << "resize " << partition_name;
  800. return false;
  801. }
  802. if (!partition_create) DestroyLogicalPartition(partition_name, 10s);
  803. changed = true;
  804. *partition_exists = false;
  805. }
  806. }
  807. }
  808. // land the update back on to the partition
  809. if (changed) {
  810. auto metadata = builder->Export();
  811. if (!metadata || !UpdatePartitionTable(super_device, *metadata.get(), slot_number)) {
  812. LERROR << "add partition " << partition_name;
  813. return false;
  814. }
  815. if (change) *change = true;
  816. }
  817. if (changed || partition_create) {
  818. if (!CreateLogicalPartition(super_device, slot_number, partition_name, true, 10s,
  819. scratch_device))
  820. return false;
  821. if (change) *change = true;
  822. }
  823. return true;
  824. }
  825. // Create and mount kScratchMountPoint storage if we have logical partitions
  826. bool fs_mgr_overlayfs_setup_scratch(const Fstab& fstab, bool* change) {
  827. if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return true;
  828. std::string scratch_device;
  829. bool partition_exists;
  830. if (!fs_mgr_overlayfs_create_scratch(fstab, &scratch_device, &partition_exists, change)) {
  831. return false;
  832. }
  833. // If the partition exists, assume first that it can be mounted.
  834. auto mnt_type = fs_mgr_overlayfs_scratch_mount_type();
  835. if (partition_exists) {
  836. if (fs_mgr_overlayfs_mount_scratch(scratch_device, mnt_type)) {
  837. if (!fs_mgr_access(kScratchMountPoint + kOverlayTopDir) &&
  838. !fs_mgr_filesystem_has_space(kScratchMountPoint)) {
  839. // declare it useless, no overrides and no free space
  840. fs_mgr_overlayfs_umount_scratch();
  841. } else {
  842. if (change) *change = true;
  843. return true;
  844. }
  845. }
  846. // partition existed, but was not initialized; fall through to make it.
  847. errno = 0;
  848. }
  849. if (!fs_mgr_overlayfs_make_scratch(scratch_device, mnt_type)) return false;
  850. if (change) *change = true;
  851. return fs_mgr_overlayfs_mount_scratch(scratch_device, mnt_type);
  852. }
  853. bool fs_mgr_overlayfs_scratch_can_be_mounted(const std::string& scratch_device) {
  854. if (scratch_device.empty()) return false;
  855. if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return false;
  856. if (android::base::StartsWith(scratch_device, kPhysicalDevice)) return true;
  857. if (fs_mgr_rw_access(scratch_device)) return true;
  858. auto slot_number = fs_mgr_overlayfs_slot_number();
  859. auto super_device = fs_mgr_overlayfs_super_device(slot_number);
  860. if (!fs_mgr_rw_access(super_device)) return false;
  861. auto builder = MetadataBuilder::New(super_device, slot_number);
  862. if (!builder) return false;
  863. return builder->FindPartition(android::base::Basename(kScratchMountPoint)) != nullptr;
  864. }
  865. bool fs_mgr_overlayfs_invalid() {
  866. if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) return true;
  867. // in recovery, fastbootd, or gsi mode, not allowed!
  868. if (fs_mgr_access("/system/bin/recovery")) return true;
  869. auto save_errno = errno;
  870. auto ret = android::gsi::IsGsiRunning();
  871. errno = save_errno;
  872. return ret;
  873. }
  874. } // namespace
  875. Fstab fs_mgr_overlayfs_candidate_list(const Fstab& fstab) {
  876. Fstab candidates;
  877. for (const auto& entry : fstab) {
  878. FstabEntry new_entry = entry;
  879. if (!fs_mgr_overlayfs_already_mounted(entry.mount_point) &&
  880. !fs_mgr_wants_overlayfs(&new_entry)) {
  881. continue;
  882. }
  883. auto new_mount_point = fs_mgr_mount_point(entry.mount_point);
  884. auto duplicate_or_more_specific = false;
  885. for (auto it = candidates.begin(); it != candidates.end();) {
  886. auto it_mount_point = fs_mgr_mount_point(it->mount_point);
  887. if ((it_mount_point == new_mount_point) ||
  888. (android::base::StartsWith(new_mount_point, it_mount_point + "/"))) {
  889. duplicate_or_more_specific = true;
  890. break;
  891. }
  892. if (android::base::StartsWith(it_mount_point, new_mount_point + "/")) {
  893. it = candidates.erase(it);
  894. } else {
  895. ++it;
  896. }
  897. }
  898. if (!duplicate_or_more_specific) candidates.emplace_back(std::move(new_entry));
  899. }
  900. return candidates;
  901. }
  902. bool fs_mgr_overlayfs_mount_all(Fstab* fstab) {
  903. auto ret = false;
  904. if (fs_mgr_overlayfs_invalid()) return ret;
  905. auto scratch_can_be_mounted = true;
  906. for (const auto& entry : fs_mgr_overlayfs_candidate_list(*fstab)) {
  907. if (fs_mgr_is_verity_enabled(entry)) continue;
  908. auto mount_point = fs_mgr_mount_point(entry.mount_point);
  909. if (fs_mgr_overlayfs_already_mounted(mount_point)) {
  910. ret = true;
  911. continue;
  912. }
  913. if (scratch_can_be_mounted) {
  914. scratch_can_be_mounted = false;
  915. auto scratch_device = fs_mgr_overlayfs_scratch_device();
  916. if (fs_mgr_overlayfs_scratch_can_be_mounted(scratch_device) &&
  917. fs_mgr_wait_for_file(scratch_device, 10s)) {
  918. const auto mount_type = fs_mgr_overlayfs_scratch_mount_type();
  919. if (fs_mgr_overlayfs_mount_scratch(scratch_device, mount_type,
  920. true /* readonly */)) {
  921. auto has_overlayfs_dir = fs_mgr_access(kScratchMountPoint + kOverlayTopDir);
  922. fs_mgr_overlayfs_umount_scratch();
  923. if (has_overlayfs_dir) {
  924. fs_mgr_overlayfs_mount_scratch(scratch_device, mount_type);
  925. }
  926. }
  927. }
  928. }
  929. if (fs_mgr_overlayfs_mount(mount_point)) ret = true;
  930. }
  931. return ret;
  932. }
  933. std::vector<std::string> fs_mgr_overlayfs_required_devices(Fstab* fstab) {
  934. if (fs_mgr_overlayfs_invalid()) return {};
  935. if (GetEntryForMountPoint(fstab, kScratchMountPoint) != nullptr) {
  936. return {};
  937. }
  938. for (const auto& entry : fs_mgr_overlayfs_candidate_list(*fstab)) {
  939. if (fs_mgr_is_verity_enabled(entry)) continue;
  940. if (fs_mgr_overlayfs_already_mounted(fs_mgr_mount_point(entry.mount_point))) continue;
  941. auto device = fs_mgr_overlayfs_scratch_device();
  942. if (!fs_mgr_overlayfs_scratch_can_be_mounted(device)) break;
  943. return {device};
  944. }
  945. return {};
  946. }
  947. // Returns false if setup not permitted, errno set to last error.
  948. // If something is altered, set *change.
  949. bool fs_mgr_overlayfs_setup(const char* backing, const char* mount_point, bool* change,
  950. bool force) {
  951. if (change) *change = false;
  952. auto ret = false;
  953. if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) return ret;
  954. if (!fs_mgr_boot_completed()) {
  955. errno = EBUSY;
  956. PERROR << "setup";
  957. return ret;
  958. }
  959. auto save_errno = errno;
  960. Fstab fstab;
  961. if (!ReadDefaultFstab(&fstab)) {
  962. return false;
  963. }
  964. errno = save_errno;
  965. auto candidates = fs_mgr_overlayfs_candidate_list(fstab);
  966. for (auto it = candidates.begin(); it != candidates.end();) {
  967. if (mount_point &&
  968. (fs_mgr_mount_point(it->mount_point) != fs_mgr_mount_point(mount_point))) {
  969. it = candidates.erase(it);
  970. continue;
  971. }
  972. save_errno = errno;
  973. auto verity_enabled = !force && fs_mgr_is_verity_enabled(*it);
  974. if (errno == ENOENT || errno == ENXIO) errno = save_errno;
  975. if (verity_enabled) {
  976. it = candidates.erase(it);
  977. continue;
  978. }
  979. ++it;
  980. }
  981. if (candidates.empty()) return ret;
  982. std::string dir;
  983. for (const auto& overlay_mount_point : kOverlayMountPoints) {
  984. if (backing && backing[0] && (overlay_mount_point != backing)) continue;
  985. if (overlay_mount_point == kScratchMountPoint) {
  986. if (!fs_mgr_overlayfs_setup_scratch(fstab, change)) continue;
  987. } else {
  988. if (GetEntryForMountPoint(&fstab, overlay_mount_point) == nullptr) {
  989. continue;
  990. }
  991. }
  992. dir = overlay_mount_point;
  993. break;
  994. }
  995. if (dir.empty()) {
  996. if (change && *change) errno = ESRCH;
  997. if (errno == EPERM) errno = save_errno;
  998. return ret;
  999. }
  1000. std::string overlay;
  1001. ret |= fs_mgr_overlayfs_setup_dir(dir, &overlay, change);
  1002. for (const auto& entry : candidates) {
  1003. ret |= fs_mgr_overlayfs_setup_one(overlay, fs_mgr_mount_point(entry.mount_point), change);
  1004. }
  1005. return ret;
  1006. }
  1007. // Returns false if teardown not permitted, errno set to last error.
  1008. // If something is altered, set *change.
  1009. bool fs_mgr_overlayfs_teardown(const char* mount_point, bool* change) {
  1010. if (change) *change = false;
  1011. auto ret = true;
  1012. // If scratch exists, but is not mounted, lets gain access to clean
  1013. // specific override entries.
  1014. auto mount_scratch = false;
  1015. if ((mount_point != nullptr) && !fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) {
  1016. auto scratch_device = fs_mgr_overlayfs_scratch_device();
  1017. if (scratch_device.empty()) {
  1018. auto slot_number = fs_mgr_overlayfs_slot_number();
  1019. auto super_device = fs_mgr_overlayfs_super_device(slot_number);
  1020. const auto partition_name = android::base::Basename(kScratchMountPoint);
  1021. CreateLogicalPartition(super_device, slot_number, partition_name, true, 10s,
  1022. &scratch_device);
  1023. }
  1024. mount_scratch = fs_mgr_overlayfs_mount_scratch(scratch_device,
  1025. fs_mgr_overlayfs_scratch_mount_type());
  1026. }
  1027. for (const auto& overlay_mount_point : kOverlayMountPoints) {
  1028. ret &= fs_mgr_overlayfs_teardown_one(
  1029. overlay_mount_point, mount_point ? fs_mgr_mount_point(mount_point) : "", change);
  1030. }
  1031. if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) {
  1032. // After obligatory teardown to make sure everything is clean, but if
  1033. // we didn't want overlayfs in the the first place, we do not want to
  1034. // waste time on a reboot (or reboot request message).
  1035. if (change) *change = false;
  1036. }
  1037. // And now that we did what we could, lets inform
  1038. // caller that there may still be more to do.
  1039. if (!fs_mgr_boot_completed()) {
  1040. errno = EBUSY;
  1041. PERROR << "teardown";
  1042. ret = false;
  1043. }
  1044. if (mount_scratch) fs_mgr_overlayfs_umount_scratch();
  1045. return ret;
  1046. }
  1047. bool fs_mgr_overlayfs_is_setup() {
  1048. if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return true;
  1049. Fstab fstab;
  1050. if (!ReadDefaultFstab(&fstab)) {
  1051. return false;
  1052. }
  1053. if (fs_mgr_overlayfs_invalid()) return false;
  1054. for (const auto& entry : fs_mgr_overlayfs_candidate_list(fstab)) {
  1055. if (fs_mgr_is_verity_enabled(entry)) continue;
  1056. if (fs_mgr_overlayfs_already_mounted(fs_mgr_mount_point(entry.mount_point))) return true;
  1057. }
  1058. return false;
  1059. }
  1060. #endif // ALLOW_ADBD_DISABLE_VERITY != 0
  1061. bool fs_mgr_has_shared_blocks(const std::string& mount_point, const std::string& dev) {
  1062. struct statfs fs;
  1063. if ((statfs((mount_point + "/lost+found").c_str(), &fs) == -1) ||
  1064. (fs.f_type != EXT4_SUPER_MAGIC)) {
  1065. return false;
  1066. }
  1067. android::base::unique_fd fd(open(dev.c_str(), O_RDONLY | O_CLOEXEC));
  1068. if (fd < 0) return false;
  1069. struct ext4_super_block sb;
  1070. if ((TEMP_FAILURE_RETRY(lseek64(fd, 1024, SEEK_SET)) < 0) ||
  1071. (TEMP_FAILURE_RETRY(read(fd, &sb, sizeof(sb))) < 0)) {
  1072. return false;
  1073. }
  1074. struct fs_info info;
  1075. if (ext4_parse_sb(&sb, &info) < 0) return false;
  1076. return (info.feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS) != 0;
  1077. }
  1078. std::string fs_mgr_get_context(const std::string& mount_point) {
  1079. char* ctx = nullptr;
  1080. if (getfilecon(mount_point.c_str(), &ctx) == -1) {
  1081. return "";
  1082. }
  1083. std::string context(ctx);
  1084. free(ctx);
  1085. return context;
  1086. }
  1087. OverlayfsValidResult fs_mgr_overlayfs_valid() {
  1088. // Overlayfs available in the kernel, and patched for override_creds?
  1089. if (fs_mgr_access("/sys/module/overlay/parameters/override_creds")) {
  1090. return OverlayfsValidResult::kOverrideCredsRequired;
  1091. }
  1092. if (!fs_mgr_overlayfs_filesystem_available("overlay")) {
  1093. return OverlayfsValidResult::kNotSupported;
  1094. }
  1095. struct utsname uts;
  1096. if (uname(&uts) == -1) {
  1097. return OverlayfsValidResult::kNotSupported;
  1098. }
  1099. int major, minor;
  1100. if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
  1101. return OverlayfsValidResult::kNotSupported;
  1102. }
  1103. if (major < 4) {
  1104. return OverlayfsValidResult::kOk;
  1105. }
  1106. if (major > 4) {
  1107. return OverlayfsValidResult::kNotSupported;
  1108. }
  1109. if (minor > 3) {
  1110. return OverlayfsValidResult::kNotSupported;
  1111. }
  1112. return OverlayfsValidResult::kOk;
  1113. }