ext4: add ioctl EXT4_IOC_CHECKPOINT
authorLeah Rumancik <leah.rumancik@gmail.com>
Tue, 18 May 2021 15:13:26 +0000 (15:13 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 23 Jun 2021 01:34:08 +0000 (21:34 -0400)
ioctl EXT4_IOC_CHECKPOINT checkpoints and flushes the journal. This
includes forcing all the transactions to the log, checkpointing the
transactions, and flushing the log to disk. This ioctl takes u32 "flags"
as an argument. Three flags are supported. EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN
can be used to verify input to the ioctl. It returns error if there is any
invalid input, otherwise it returns success without performing
any checkpointing. The other two flags, EXT4_IOC_CHECKPOINT_FLAG_DISCARD
and EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT, can be used to issue requests to
discard or zeroout the journal logs blocks, respectively. At this
point, EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT is primarily added to enable
testing of this codepath on devices that don't support discard.
EXT4_IOC_CHECKPOINT_FLAG_DISCARD and EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT
cannot both be set.

Systems that wish to achieve content deletion SLO can set up a daemon
that calls this ioctl at a regular interval such that it matches with the
SLO requirement. Thus, with this patch, the ext4_dir_entry2 wipeout
patch[1], and the Ext4 "-o discard" mount option set, Ext4 can now
guarantee that all file contents, file metatdata, and filenames will not
be accessible through the filesystem and will have had discard or
zeroout requests issued for corresponding device blocks.

The __jbd2_journal_erase function could also be used to discard or
zero-fill the journal during journal load after recovery. This would
provide a potential solution to a journal replay bug reported earlier this
year[2]. After a successful journal recovery, e2fsck can call this ioctl to
discard the journal as well.

[1] https://lore.kernel.org/linux-ext4/YIHknqxngB1sUdie@mit.edu/
[2] https://lore.kernel.org/linux-ext4/YDZoaacIYStFQT8g@mit.edu/

Link: https://lore.kernel.org/r/20210518151327.130198-2-leah.rumancik@gmail.com
Signed-off-by: Leah Rumancik <leah.rumancik@gmail.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/ext4.h
fs/ext4/ioctl.c

index 2b51cbe..a646bfc 100644 (file)
@@ -720,6 +720,7 @@ enum {
 #define EXT4_IOC_CLEAR_ES_CACHE                _IO('f', 40)
 #define EXT4_IOC_GETSTATE              _IOW('f', 41, __u32)
 #define EXT4_IOC_GET_ES_CACHE          _IOWR('f', 42, struct fiemap)
+#define EXT4_IOC_CHECKPOINT            _IOW('f', 43, __u32)
 
 #define EXT4_IOC_SHUTDOWN _IOR ('X', 125, __u32)
 
@@ -741,6 +742,14 @@ enum {
 #define EXT4_STATE_FLAG_NEWENTRY       0x00000004
 #define EXT4_STATE_FLAG_DA_ALLOC_CLOSE 0x00000008
 
+/* flags for ioctl EXT4_IOC_CHECKPOINT */
+#define EXT4_IOC_CHECKPOINT_FLAG_DISCARD       0x1
+#define EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT       0x2
+#define EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN       0x4
+#define EXT4_IOC_CHECKPOINT_FLAG_VALID         (EXT4_IOC_CHECKPOINT_FLAG_DISCARD | \
+                                               EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT | \
+                                               EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN)
+
 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
 /*
  * ioctl commands in 32 bit emulation
index 93e9419..5730aec 100644 (file)
@@ -799,6 +799,57 @@ static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
        return error;
 }
 
+static int ext4_ioctl_checkpoint(struct file *filp, unsigned long arg)
+{
+       int err = 0;
+       __u32 flags = 0;
+       unsigned int flush_flags = 0;
+       struct super_block *sb = file_inode(filp)->i_sb;
+       struct request_queue *q;
+
+       if (copy_from_user(&flags, (__u32 __user *)arg,
+                               sizeof(__u32)))
+               return -EFAULT;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       /* check for invalid bits set */
+       if ((flags & ~EXT4_IOC_CHECKPOINT_FLAG_VALID) ||
+                               ((flags & JBD2_JOURNAL_FLUSH_DISCARD) &&
+                               (flags & JBD2_JOURNAL_FLUSH_ZEROOUT)))
+               return -EINVAL;
+
+       if (!EXT4_SB(sb)->s_journal)
+               return -ENODEV;
+
+       if (flags & ~JBD2_JOURNAL_FLUSH_VALID)
+               return -EINVAL;
+
+       q = bdev_get_queue(EXT4_SB(sb)->s_journal->j_dev);
+       if (!q)
+               return -ENXIO;
+       if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && !blk_queue_discard(q))
+               return -EOPNOTSUPP;
+
+       if (flags & EXT4_IOC_CHECKPOINT_FLAG_DRY_RUN)
+               return 0;
+
+       if (flags & EXT4_IOC_CHECKPOINT_FLAG_DISCARD)
+               flush_flags |= JBD2_JOURNAL_FLUSH_DISCARD;
+
+       if (flags & EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT) {
+               flush_flags |= JBD2_JOURNAL_FLUSH_ZEROOUT;
+               pr_info_ratelimited("warning: checkpointing journal with EXT4_IOC_CHECKPOINT_FLAG_ZEROOUT can be slow");
+       }
+
+       jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
+       err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, flush_flags);
+       jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
+
+       return err;
+}
+
 static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct inode *inode = file_inode(filp);
@@ -1210,6 +1261,9 @@ resizefs_out:
                return fsverity_ioctl_read_metadata(filp,
                                                    (const void __user *)arg);
 
+       case EXT4_IOC_CHECKPOINT:
+               return ext4_ioctl_checkpoint(filp, arg);
+
        default:
                return -ENOTTY;
        }
@@ -1290,6 +1344,7 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        case EXT4_IOC_CLEAR_ES_CACHE:
        case EXT4_IOC_GETSTATE:
        case EXT4_IOC_GET_ES_CACHE:
+       case EXT4_IOC_CHECKPOINT:
                break;
        default:
                return -ENOIOCTLCMD;