The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Max Kellermann <max.kellermann@ionos.com>
To: gregkh@linuxfoundation.org, tj@kernel.org, linux-kernel@vger.kernel.org
Cc: Max Kellermann <max.kellermann@ionos.com>
Subject: [PATCH] fs/kernfs: implement STATX_BTIME
Date: Tue,  6 May 2025 18:40:17 +0200	[thread overview]
Message-ID: <20250506164017.249149-1-max.kellermann@ionos.com> (raw)

This allows finding out when an inode was initially created, for
example:

- when was a device plugged in (and its node in sysfs was created)?
- when was a cgroup created?

kernfs currently only implements `atime`, `mtime` and `ctime`.  All of
these are volatile (`mtime` and `ctime` get updated automatically, and
`atime` can be mainpulated using utime()).  Therefore, I suggest
implementing STATX_BTIME to have a reliable birth time in userspace.

Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
 fs/kernfs/dir.c        | 2 ++
 fs/kernfs/inode.c      | 6 ++++++
 include/linux/kernfs.h | 7 +++++++
 3 files changed, 15 insertions(+)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index fc70d72c3fe8..9a6857f2f3d7 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -678,6 +678,8 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 			goto err_out3;
 	}
 
+	ktime_get_real_ts64(&kn->btime);
+
 	return kn;
 
  err_out3:
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index b83054da68b3..1ff2ee62bfe6 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -189,6 +189,12 @@ int kernfs_iop_getattr(struct mnt_idmap *idmap,
 	struct kernfs_root *root = kernfs_root(kn);
 
 	down_read(&root->kernfs_iattr_rwsem);
+
+	if (request_mask & STATX_BTIME) {
+		stat->result_mask |= STATX_BTIME;
+		stat->btime = kn->btime;
+	}
+
 	kernfs_refresh_inode(kn, inode);
 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
 	up_read(&root->kernfs_iattr_rwsem);
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index b5a5f32fdfd1..9332aadf4b48 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -229,6 +229,13 @@ struct kernfs_node {
 	void			*priv;
 	struct kernfs_iattrs	*iattr;
 
+	/*
+	 * The birth time (for STATX_BTIME).  It lives here and not in
+	 * struct kernfs_iattrs because the latter is only created on
+	 * demand, not at actual node birth time.
+	 */
+	struct timespec64	btime;
+
 	struct rcu_head		rcu;
 };
 
-- 
2.47.2


             reply	other threads:[~2025-05-06 16:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-06 16:40 Max Kellermann [this message]
2025-05-06 17:43 ` [PATCH] fs/kernfs: implement STATX_BTIME Greg KH
2025-05-06 18:30   ` Max Kellermann
2025-05-21 11:24     ` Greg KH
2025-05-21 16:18       ` Tejun Heo
2025-05-21 20:19         ` Max Kellermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250506164017.249149-1-max.kellermann@ionos.com \
    --to=max.kellermann@ionos.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox