| 1 | /* |
| 2 | * Copyright (C) 2021 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 | #define LOG_TAG "F2fsUtils" |
| 18 | |
| 19 | #include "core_jni_helpers.h" |
| 20 | |
| 21 | #include <nativehelper/ScopedUtfChars.h> |
| 22 | #include <nativehelper/jni_macros.h> |
| 23 | |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <linux/f2fs.h> |
| 28 | #include <linux/fs.h> |
| 29 | |
| 30 | #include <android-base/unique_fd.h> |
| 31 | |
| 32 | #include <utils/Log.h> |
| 33 | |
| 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | |
| 37 | #include <array> |
| 38 | |
| 39 | using namespace std::literals; |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | static jlong com_android_internal_content_F2fsUtils_nativeReleaseCompressedBlocks(JNIEnv *env, |
| 44 | jclass clazz, |
| 45 | jstring path) { |
| 46 | unsigned long long blkcnt; |
| 47 | int ret; |
| 48 | ScopedUtfChars filePath(env, path); |
| 49 | |
| 50 | android::base::unique_fd fd(open(filePath.c_str(), O_RDONLY | O_CLOEXEC, 0)); |
| 51 | if (fd < 0) { |
| 52 | ALOGW("Failed to open file: %s (%d)\n", filePath.c_str(), errno); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | long flags = 0; |
| 57 | ret = ioctl(fd, FS_IOC_GETFLAGS, &flags); |
| 58 | if (ret < 0) { |
| 59 | ALOGW("Failed to get flags for file: %s (%d)\n", filePath.c_str(), errno); |
| 60 | return 0; |
| 61 | } |
| 62 | if ((flags & FS_COMPR_FL) == 0) { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | ret = ioctl(fd, F2FS_IOC_RELEASE_COMPRESS_BLOCKS, &blkcnt); |
| 67 | if (ret < 0) { |
| 68 | return -errno; |
| 69 | } |
| 70 | return blkcnt; |
| 71 | } |
| 72 | |
| 73 | static const std::array gMethods = { |
| 74 | MAKE_JNI_NATIVE_METHOD( |
| 75 | "nativeReleaseCompressedBlocks", "(Ljava/lang/String;)J", |
| 76 | com_android_internal_content_F2fsUtils_nativeReleaseCompressedBlocks), |
| 77 | }; |
| 78 | |
| 79 | int register_com_android_internal_content_F2fsUtils(JNIEnv *env) { |
| 80 | return RegisterMethodsOrDie(env, "com/android/internal/content/F2fsUtils", gMethods.data(), |
| 81 | gMethods.size()); |
| 82 | } |
| 83 | |
| 84 | }; // namespace android |