IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android系统userdata自适应方案 -> 正文阅读

[移动开发]Android系统userdata自适应方案

  • Android 11
diff --git a/system/core/fs_mgr/fs_mgr.cpp b/system/core/fs_mgr/fs_mgr.cpp
index d70d5a7..6b5d438 100755
--- a/system/core/fs_mgr/fs_mgr.cpp
+++ b/system/core/fs_mgr/fs_mgr.cpp
@@ -679,6 +679,63 @@
     }
     return sb == cpu_to_le32(F2FS_SUPER_MAGIC);
 }
+/*add for auto userdata resize*/
+static int get_dev_sz(const char *fs_blkdev, uint64_t *dev_sz)
+{
+  int fd;
+
+  if ((fd = open(fs_blkdev, O_RDONLY)) < 0) {
+    LERROR << "Cannot open block device";
+    return -1;
+  }
+
+  if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
+    LERROR << "Cannot get block device size";
+    close(fd);
+    return -1;
+  }
+
+  close(fd);
+  return 0;
+}
+
+static bool read_f2fs_size(const std::string& blk_device, uint64_t* size) {
+  android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
+  __le64 block_count1, block_count2;
+  __le32 log_blocksize1, log_blocksize2;
+
+  if (fd < 0) {
+    PERROR << "Failed to open '" << blk_device << "'";
+    return false;
+  }
+
+  if (TEMP_FAILURE_RETRY(pread(fd, &log_blocksize1, sizeof(log_blocksize1),
+                               F2FS_SUPER_OFFSET + sizeof(__le32)*4)) != sizeof(log_blocksize1)) {
+    PERROR << "Can't read '" << blk_device << "' superblock1";
+  }
+  if (TEMP_FAILURE_RETRY(pread(fd, &block_count1, sizeof(block_count1),
+                               F2FS_SUPER_OFFSET + sizeof(__le32)*9)) != sizeof(block_count1)) {
+    PERROR << "Can't read '" << blk_device << "' superblock1";
+  }
+  if (TEMP_FAILURE_RETRY(pread(fd, &log_blocksize2, sizeof(log_blocksize2),
+                               F2FS_BLKSIZE + F2FS_SUPER_OFFSET + sizeof(__le32)*4))
+      != sizeof(log_blocksize2)) {
+    PERROR << "Can't read '" << blk_device << "' superblock2";
+  }
+  if (TEMP_FAILURE_RETRY(pread(fd, &block_count2, sizeof(block_count2),
+                               F2FS_BLKSIZE + F2FS_SUPER_OFFSET + sizeof(__le32)*9))
+      != sizeof(block_count2)) {
+    PERROR << "Can't read '" << blk_device << "' superblock2";
+  }
+  LINFO << __func__ << "get " << blk_device << " log_blocksize1:" << log_blocksize1
+        << " block_count1:" << block_count1 << " log_blocksize2:" << log_blocksize2
+        << " block_count2:" << block_count2;
+  if (log_blocksize1 != log_blocksize2 || block_count1 != block_count2)
+    return false;
+  *size = (1 << log_blocksize1) * block_count1;
+  return true;
+}
+/*add for auto userdata resize*/
 
 //
 // Prepare the filesystem on the given block device to be mounted.
@@ -714,7 +771,50 @@
             return fs_stat;
         }
     }
+/*add for auto userdata resize*/
+    LINFO<< __func__<<" : mount "<<entry.mount_point;
+    if(!strcmp(entry.mount_point.c_str(),"/data")){
+        uint64_t dev_sz = 0;
 
+        int rc = get_dev_sz(blk_device.c_str(), &dev_sz);
+
+        uint64_t partition_size = 0;
+        if (is_f2fs(entry.fs_type)) {
+          read_f2fs_size(blk_device, &partition_size);
+        } else {
+          partition_size = entry.length;
+        }
+
+        if (!rc && partition_size) {
+            LINFO << __func__<< blk_device<<","<<" real size: "<<dev_sz ;
+            LINFO << __func__<< blk_device<<","<<" partition_size: "<<partition_size ;
+
+            if(dev_sz > partition_size+1073741824 || dev_sz < partition_size){
+                if (access("/system/bin/make_f2fs", X_OK)) {
+                    LINFO << "Not running resize on " << blk_device << " (executable not in system image)";
+                } else {
+                    #define CRYPT_FOOTER_OFFSET 0x4000
+                    dev_sz -= CRYPT_FOOTER_OFFSET;
+                    std::string size_str = std::to_string(dev_sz / 4096);
+                    // clang-format off
+                    const char* const args[] = {
+                        "/system/bin/make_f2fs",
+                        "-g", "android",
+                        blk_device.c_str(),
+                        size_str.c_str(),
+                        nullptr
+                    };
+                    // clang-format on
+                    rc = logwrap_fork_execvp(ARRAY_SIZE(args), args, nullptr, false,
+                          LOG_KLOG, false, nullptr);
+                    if(rc){
+                        LERROR << "make_f2fs returned " << rc;
+                    }
+                }
+            }
+        }
+    }
+/*add for auto userdata resize*/
     if (entry.fs_mgr_flags.check ||
         (fs_stat & (FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED))) {
         check_fs(blk_device, entry.fs_type, entry.mount_point, &fs_stat);

  • Android10
diff --git a/system/core/fs_mgr/fs_mgr.cpp b/system/core/fs_mgr/fs_mgr.cpp
index d70d5a7..6b5d438 100755
--- a/system/core/fs_mgr/fs_mgr.cpp
+++ b/system/core/fs_mgr/fs_mgr.cpp
@@ -679,6 +679,63 @@
     }
     return sb == cpu_to_le32(F2FS_SUPER_MAGIC);
 }
 
+  //<!-- add for bug 25338,userdata resize
+  static int get_dev_sz(const char *fs_blkdev, uint64_t *dev_sz)
+  {
+    int fd;
+  
+    if ((fd = open(fs_blkdev, O_RDONLY)) < 0) {
+      LERROR << "Cannot open block device";
+      return -1;
+    }
+  
+    if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
+      LERROR << "Cannot get block device size";
+      close(fd);
+      return -1;
+    }
+  
+    close(fd);
+    return 0;
+  }
+  
+  static bool read_f2fs_size(const std::string& blk_device, uint64_t* size) {
+    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blk_device.c_str(), O_RDONLY | O_CLOEXEC)));
+    __le64 block_count1, block_count2;
+    __le32 log_blocksize1, log_blocksize2;
+  
+    if (fd < 0) {
+      PERROR << "Failed to open '" << blk_device << "'";
+      return false;
+    }
+  
+    if (TEMP_FAILURE_RETRY(pread(fd, &log_blocksize1, sizeof(log_blocksize1),
+                                 F2FS_SUPER_OFFSET + sizeof(__le32)*4)) != sizeof(log_blocksize1)) {
+      PERROR << "Can't read '" << blk_device << "' superblock1";
+    }
+    if (TEMP_FAILURE_RETRY(pread(fd, &block_count1, sizeof(block_count1),
+                                 F2FS_SUPER_OFFSET + sizeof(__le32)*9)) != sizeof(block_count1)) {
+      PERROR << "Can't read '" << blk_device << "' superblock1";
+    }
+    if (TEMP_FAILURE_RETRY(pread(fd, &log_blocksize2, sizeof(log_blocksize2),
+                                 F2FS_BLKSIZE + F2FS_SUPER_OFFSET + sizeof(__le32)*4))
+        != sizeof(log_blocksize2)) {
+      PERROR << "Can't read '" << blk_device << "' superblock2";
+    }
+    if (TEMP_FAILURE_RETRY(pread(fd, &block_count2, sizeof(block_count2),
+                                 F2FS_BLKSIZE + F2FS_SUPER_OFFSET + sizeof(__le32)*9))
+        != sizeof(block_count2)) {
+      PERROR << "Can't read '" << blk_device << "' superblock2";
+    }
+    LINFO << __func__ << "get " << blk_device << " log_blocksize1:" << log_blocksize1
+          << " block_count1:" << block_count1 << " log_blocksize2:" << log_blocksize2
+          << " block_count2:" << block_count2;
+    if (log_blocksize1 != log_blocksize2 || block_count1 != block_count2)
+      return false;
+    *size = (1 << log_blocksize1) * block_count1;
+    return true;
+  }
+  //add for userdata resize -->
 
 //
 // Prepare the filesystem on the given block device to be mounted.
@@ -714,7 +771,50 @@
             return fs_stat;
         }
     }
+    //<!-- add for userdata resize
+    LINFO<< __func__<<" : mount "<<entry.mount_point;
+    if(!strcmp(entry.mount_point.c_str(),"/data")){
+        uint64_t dev_sz = 0;
+
+        int rc = get_dev_sz(blk_device.c_str(), &dev_sz);
+        
+        uint64_t partition_size = 0;
+        if (is_f2fs(entry.fs_type)) {
+          read_f2fs_size(blk_device, &partition_size);
+        } else {
+          partition_size = entry.length;
+        }        
+
+        if (!rc && partition_size) {
+            LINFO << __func__<< blk_device<<","<<" real size: "<<dev_sz ;
+            LINFO << __func__<< blk_device<<","<<" partition_size: "<<partition_size ;
+
+            if(dev_sz > partition_size+1073741824 || dev_sz < partition_size){
+                if (access("/system/bin/make_f2fs", X_OK)) {
+                    LINFO << "Not running resize on " << blk_device << " (executable not in system image)";
+                } else {
+                    #define CRYPT_FOOTER_OFFSET 0x4000
+                    dev_sz -= CRYPT_FOOTER_OFFSET;
+                    std::string size_str = std::to_string(dev_sz / 4096);
+                    // clang-format off
+                    const char* const args[] = {
+                        "/system/bin/make_f2fs",
+                        "-g", "android",
+                        blk_device.c_str(),
+                        size_str.c_str(),
+                        nullptr
+                    };
+                    // clang-format on
+                    rc = android_fork_execvp_ext(ARRAY_SIZE(args), const_cast<char**>(args), nullptr, false,
+                          LOG_KLOG, false, nullptr, nullptr, 0);
+                    if(rc){
+                        LERROR << "make_f2fs returned " << rc;
+                    }
+                }
+            }
+        }
+    }
+    //add for userdata resize -->
     if (entry.fs_mgr_flags.check ||
         (fs_stat & (FS_STAT_UNCLEAN_SHUTDOWN | FS_STAT_QUOTA_ENABLED))) {
         check_fs(blk_device, entry.fs_type, entry.mount_point, &fs_stat);

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-31 15:33:43  更:2021-08-31 15:34:51 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/31 23:55:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码