linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Theodore Ts'o <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: Theodore Ts'o <tytso@mit.edu>, stable@kernel.org
Subject: [PATCH] ext4: atomically set inode->i_flags in ext4_set_inode_flags()
Date: Mon, 24 Mar 2014 14:43:59 -0400	[thread overview]
Message-ID: <1395686639-1853-1-git-send-email-tytso@mit.edu> (raw)

Use cmpxchg() to atomically set i_flags instead of clearing out the
S_IMMUTABLE, S_APPEND, etc. flags and then setting them from the
EXT4_IMMUTABLE_FL, EXT4_APPEND_FL flags, since this opens up a race
where an immutable file has the immutable flag cleared for a brief
window of time.

Reported-by: Petr Matousek <pmatouse@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Cc: stable@kernel.org
---
 fs/ext4/inode.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index b5e182a..169fff3 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3937,19 +3937,26 @@ int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
 
 void ext4_set_inode_flags(struct inode *inode)
 {
-	unsigned int flags = EXT4_I(inode)->i_flags;
-
-	inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
-	if (flags & EXT4_SYNC_FL)
-		inode->i_flags |= S_SYNC;
-	if (flags & EXT4_APPEND_FL)
-		inode->i_flags |= S_APPEND;
-	if (flags & EXT4_IMMUTABLE_FL)
-		inode->i_flags |= S_IMMUTABLE;
-	if (flags & EXT4_NOATIME_FL)
-		inode->i_flags |= S_NOATIME;
-	if (flags & EXT4_DIRSYNC_FL)
-		inode->i_flags |= S_DIRSYNC;
+	unsigned int flags;
+	unsigned int old_fl, new_fl;
+
+	do {
+		flags = EXT4_I(inode)->i_flags;
+		old_fl = inode->i_flags;
+		new_fl = old_fl &
+			~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
+
+		if (flags & EXT4_SYNC_FL)
+			new_fl |= S_SYNC;
+		if (flags & EXT4_APPEND_FL)
+			new_fl |= S_APPEND;
+		if (flags & EXT4_IMMUTABLE_FL)
+			new_fl |= S_IMMUTABLE;
+		if (flags & EXT4_NOATIME_FL)
+			new_fl |= S_NOATIME;
+		if (flags & EXT4_DIRSYNC_FL)
+			new_fl |= S_DIRSYNC;
+	} while (cmpxchg(&inode->i_flags, old_fl, new_fl) != old_fl);
 }
 
 /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
-- 
1.9.0


                 reply	other threads:[~2014-03-24 18:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1395686639-1853-1-git-send-email-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    --cc=stable@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;
as well as URLs for NNTP newsgroup(s).