From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA711C433F5 for ; Tue, 16 Nov 2021 03:58:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 908E961A58 for ; Tue, 16 Nov 2021 03:58:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237364AbhKPEBW (ORCPT ); Mon, 15 Nov 2021 23:01:22 -0500 Received: from szxga03-in.huawei.com ([45.249.212.189]:27212 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237195AbhKPEBM (ORCPT ); Mon, 15 Nov 2021 23:01:12 -0500 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4HtXL76Jrcz8tvq; Tue, 16 Nov 2021 11:56:31 +0800 (CST) Received: from dggpemm500007.china.huawei.com (7.185.36.183) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.20; Tue, 16 Nov 2021 11:58:14 +0800 Received: from use12-sp2.huawei.com (10.67.189.20) by dggpemm500007.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.20; Tue, 16 Nov 2021 11:58:14 +0800 From: Jubin Zhong To: CC: , , , , , Subject: Re: [PATCH] fs: Fix truncate never updates m/ctime Date: Tue, 16 Nov 2021 11:58:10 +0800 Message-ID: <1637035090-52547-1-git-send-email-zhongjubin@huawei.com> X-Mailer: git-send-email 1.8.5.6 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.189.20] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500007.china.huawei.com (7.185.36.183) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org > On Mon, Nov 15, 2021 at 07:00:18PM +0800, Jubin Zhong wrote: >> From: zhongjubin >> >> Syscall truncate() never updates m/ctime even if the file size is >> changed. However, this is incorrect according to man file: >> >> truncate (2): >> If the size changed, then the st_ctime and st_mtime fields >> (respectively, time of last status change and time of last modification; >> see stat(2)) for the file are updated, and the set-user-ID and >> set-group-ID mode bits may be cleared. >> >> Check file size before do_truncate() to fix this. > > Please try to actually reproduce your alleged "bug". And maybe also > look at the actual setattr implementations. Hint: The XFS one even > has extensive comments. Thanks for your advice. I found this problem on yaffs2 in the beginning, ftruncate() always works fine but truncate() does not. Now I have done a few more tests and the following are the results: Test Environmont: kernel: Linux Kernel v5.16 hardware: QEMU emulator version 3.1.0 arch: vexpress-v2p-ca9 Teset Results: filesystems m/ctime updated by truncate? jffs2 fail yaffs2 fail ubifs success ext2 success ext4 success tmpfs success xfs success Test Steps: 1. cd /path/to/mnt/point 2. dd if=/dev/zero of=test bs=1M count=1 3. stat test 4. /bin/my_truncate -s 1024 test 5. stat test 6. compare m/ctime of step 5 with step 3 Program source: #include #include #include #include #include int main(int argc, char **argv) { int ret; char file_name[128] = {0}; if (argc < 4 || argv == NULL || argv[1] == NULL || argv[2] == NULL || argv[3] == NULL) { return -1; } if (strcmp(argv[1], "-s")) { return -1; } if (realpath(argv[3], file_name) == NULL) { printf("truncate: input file name %s err.\n", argv[3]); return -1; } off_t size = (off_t)strtol(argv[2], 0, 0); ret = truncate(file_name, size); if (ret) { printf("truncate return err %d\n", ret); } return ret; } I work on embedded devices so concern about jffs2/yaffs2/ubifs the most. If there are any errors in my test program please let me know. Thanks.