From 6a1e1d788b6d9104a42449bb0b8f50815bcc109d Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Thu, 23 Oct 2025 22:16:06 +0800 Subject: [PATCH] kernel: sulog: Simplify code using vfs_truncate --- kernel/kernel_compat.c | 36 ------------------------------------ kernel/kernel_compat.h | 4 ---- kernel/sulog.c | 37 ++++--------------------------------- kernel/sulog.h | 1 - 4 files changed, 4 insertions(+), 74 deletions(-) diff --git a/kernel/kernel_compat.c b/kernel/kernel_compat.c index 961c7c19..c49ac125 100644 --- a/kernel/kernel_compat.c +++ b/kernel/kernel_compat.c @@ -93,39 +93,3 @@ long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr, return strncpy_from_user_nofault(dst, unsafe_addr, count); } -int ksu_vfs_unlink(struct inode *dir, struct dentry *dentry) -{ -#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0) - struct inode *delegated_inode = NULL; - return vfs_unlink(&nop_mnt_idmap, dir, dentry, &delegated_inode); -#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0) - struct inode *delegated_inode = NULL; - return vfs_unlink(&init_user_ns, dir, dentry, &delegated_inode); -#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0) - struct inode *delegated_inode = NULL; - return vfs_unlink(dir, dentry, &delegated_inode); -#else - return vfs_unlink(dir, dentry); -#endif -} - -int ksu_vfs_rename(struct inode *old_dir, struct dentry *old_dentry, - struct inode *new_dir, struct dentry *new_dentry) -{ -#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0) - struct renamedata rd = { - .old_dir = old_dir, - .old_dentry = old_dentry, - .new_dir = new_dir, - .new_dentry = new_dentry, - .delegated_inode = NULL, - .flags = 0, - }; - return vfs_rename(&rd); -#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0) - struct inode *delegated_inode = NULL; - return vfs_rename(old_dir, old_dentry, new_dir, new_dentry, &delegated_inode, 0); -#else - return vfs_rename(old_dir, old_dentry, new_dir, new_dentry); -#endif -} diff --git a/kernel/kernel_compat.h b/kernel/kernel_compat.h index 9952e35a..11ee9023 100644 --- a/kernel/kernel_compat.h +++ b/kernel/kernel_compat.h @@ -94,8 +94,4 @@ static long ksu_copy_from_user_retry(void *to, return copy_from_user(to, from, count); } -extern int ksu_vfs_unlink(struct inode *dir, struct dentry *dentry); -extern int ksu_vfs_rename(struct inode *old_dir, struct dentry *old_dentry, - struct inode *new_dir, struct dentry *new_dentry); - #endif diff --git a/kernel/sulog.c b/kernel/sulog.c index e8b552eb..d54862ac 100644 --- a/kernel/sulog.c +++ b/kernel/sulog.c @@ -135,40 +135,11 @@ static void sulog_work_handler(struct work_struct *work) } if (fp->f_inode->i_size > SULOG_MAX_SIZE) { - pr_info("sulog: rotating log file, size: %lld\n", fp->f_inode->i_size); - filp_close(fp, 0); - - struct path old_path; - if (!kern_path(SULOG_OLD_PATH, 0, &old_path)) { - ksu_vfs_unlink(old_path.dentry->d_parent->d_inode, old_path.dentry); - path_put(&old_path); + pr_info("sulog: log file exceeds maximum size, clearing...\n"); + if (vfs_truncate(&fp->f_path, 0)) { + pr_err("sulog: failed to truncate log file\n"); } - - struct path current_path, parent_path; - if (!kern_path(SULOG_PATH, 0, ¤t_path)) { - parent_path = current_path; - path_get(&parent_path); - parent_path.dentry = current_path.dentry->d_parent; - - struct dentry *old_dentry = lookup_one_len("sulog.log.old", - parent_path.dentry, strlen("sulog.log.old")); - if (!IS_ERR(old_dentry)) { - ksu_vfs_rename(parent_path.dentry->d_inode, current_path.dentry, - parent_path.dentry->d_inode, old_dentry); - dput(old_dentry); - } - path_put(¤t_path); - path_put(&parent_path); - } - - fp = ksu_filp_open_compat(SULOG_PATH, O_WRONLY | O_CREAT | O_EXCL, 0640); - if (IS_ERR(fp)) { - pr_err("sulog: failed to create new log file: %ld\n", PTR_ERR(fp)); - goto cleanup; - } - - const char *rotate_msg = "=== Log file rotated, old log saved as sulog.log.old ===\n"; - ksu_kernel_write_compat(fp, rotate_msg, strlen(rotate_msg), &pos); + pos = 0; } else { pos = fp->f_inode->i_size; } diff --git a/kernel/sulog.h b/kernel/sulog.h index 5be99957..459b04af 100644 --- a/kernel/sulog.h +++ b/kernel/sulog.h @@ -6,7 +6,6 @@ extern struct timezone sys_tz; #define SULOG_PATH "/data/adb/ksu/log/sulog.log" -#define SULOG_OLD_PATH "/data/adb/ksu/log/sulog.log.old" #define SULOG_MAX_SIZE (128 * 1024 * 1024) // 128MB #define SULOG_ENTRY_MAX_LEN 512 #define SULOG_COMM_LEN 256