Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp7057ybt; Tue, 16 Jun 2020 14:58:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwB1QgrkYRp09cexRfsLriwTJi0ZPW31uXZc2VwXOXN4+GuKcK/pxbWQgs7jyZ9ePi4UB0 X-Received: by 2002:aa7:c403:: with SMTP id j3mr4524531edq.294.1592344732430; Tue, 16 Jun 2020 14:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592344732; cv=none; d=google.com; s=arc-20160816; b=BjIZMG2aSGb1kNsPmJa5nVzN1F9b2NB6uR3Kp1T+sfgATQdJINNulGEWHJzCZiZ0n1 74fbGzXbcK9L7lwMZgVmn2WbJPspIBczKgPeenIklFd/biV3CFxsRX8sUk+LiRsB/1ho qPaw6QLeyOOXg+mNU/n2Z01M9mQZE0I8b2OKPePZFAG+1Al4ux0ugiotrDhd1wiT1HGq rMeU6lCyWeoBvKmDbb6JgYBuNhrNskugYnI/Um/qB7YSlzO5bZdnG+Jv2TuRhjExb/Ji 2xZuwNEvOIij4BKIbcrrVHCIb1e7sJ2xSNI7yvM6xKtjKWEIkUmJ7UBfrWVy5xyvkQet eRYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=mmKD8LzC4+JXQ+yn9tQJXZKmx/YHughCwequ3LOYC3I=; b=hM3RCqF750c/ILJk4VTX/9TKoWcsd0xfFTUZOfxyAYxMMTLW6UvcqH4QgmqITNp2Lc zvkWSPvJbEdG9tDIkPTDjljnJp81fTleYwh1iWuasbmvyc2fqQvySMWGQhnJCMZ62jdU wVJwW0fk9WOalTIghYfHUiD6yLWRDwUPUXBpEvMXduT5f9wib8yoENwIkT533A2CXMDE V1DBdhpTnElwNK8LGMvv2EpN+v/Ej3DRlTfg8a0itiyfX8yFkRrm09D8vATR7ljkE7kk vZTEUvZIINnuD6mgZjHkKwVrvgg6Esdk490LQGVbkmJdVBFfVdqTYs1oO9EMVaszPrLc dY4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="UrY9m/GK"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id se20si12443736ejb.165.2020.06.16.14.58.29; Tue, 16 Jun 2020 14:58:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="UrY9m/GK"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726101AbgFPVy1 (ORCPT + 99 others); Tue, 16 Jun 2020 17:54:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725773AbgFPVy1 (ORCPT ); Tue, 16 Jun 2020 17:54:27 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EEB7C061573 for ; Tue, 16 Jun 2020 14:54:27 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id d6so2272350pjs.3 for ; Tue, 16 Jun 2020 14:54:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=mmKD8LzC4+JXQ+yn9tQJXZKmx/YHughCwequ3LOYC3I=; b=UrY9m/GKRbqG0bmjtbH+62nBxdmkRo/JWYDueGlNzbdj1Uc19qo1Iur3bPdLCPyqjP fpafAWyNQtVIH1lD4ypMnvOcPwUfxFKECobreim5eE65iYGOXEqc9IKKVrA9zZqmY6AZ u6RgxSW5liQUbvUZCfngnr4sTRoCJJpMdExujkTF++Wkly7pmx6mlqgvtNvn4kFkH8hy MMopJB1eLWAJam2/3/1iZUOLU8S2OesDD/92qL9SiQEElCLuMh93DFaxAP+IKk0oBO8l 4grleQ9woGd2Yu7Y+zREKGBtaTF065MAJneKpbaJPsWkbGIwzBVMhI3k4am13wBikKjB qtdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=mmKD8LzC4+JXQ+yn9tQJXZKmx/YHughCwequ3LOYC3I=; b=o8D45MI5G8esHjBCT0YvZ+QAk6tub5RkCl2G4mtmqXgGN6KIpVUWihVtBWJ1OGCeUX zE4qWtVOFDb+Gv3f0LOYnvI0uieYY2PydrXjh7Pxz45wzCeVo2jS9PBLcDwkATkPR75r DZZlD2JxTtYvU7eagKSYWJimYA8ihdpO1MNeiGVlJNxREMSyn/xa3pmqfEE1W+L5DpR4 VqbyU0bvPBIcavoAfsz1YgKYJvUOMHTLPtKSZ1BCSmlXN+oHHQmtKGwYgwOBur/JD0hr NrrauAelF83bT9bR6CpZzB2BJC7tI3YLQsVsv2UpHkCanjJeJ3lrdCqpVH7HDaOZcyrf dFHw== X-Gm-Message-State: AOAM531UnM+fcXFV4YkV39XDmrikcsDZrnjsQlSdFzcNL8eL+Oqr4cMr yLFrXGcMujwYFJ640+nNtk/wdxpl X-Received: by 2002:a17:90a:acf:: with SMTP id r15mr4864541pje.171.1592344465890; Tue, 16 Jun 2020 14:54:25 -0700 (PDT) Received: from daehojeong1.seo.corp.google.com ([2401:fa00:d:1:b8f4:bbde:37ba:20bd]) by smtp.gmail.com with ESMTPSA id a83sm17683616pfa.146.2020.06.16.14.54.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 14:54:25 -0700 (PDT) From: Daeho Jeong To: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com Cc: Daeho Jeong Subject: [PATCH v3] f2fs: add F2FS_IOC_SEC_TRIM_FILE ioctl Date: Wed, 17 Jun 2020 06:54:00 +0900 Message-Id: <20200616215400.207245-1-daeho43@gmail.com> X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daeho Jeong Added a new ioctl to send discard commands or/and zero out to selected data area of a regular file for security reason. Signed-off-by: Daeho Jeong --- fs/f2fs/f2fs.h | 15 +++++ fs/f2fs/file.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index c812fb8e2d9c..3763d9e03ca8 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -434,6 +434,8 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal, _IOR(F2FS_IOCTL_MAGIC, 18, __u64) #define F2FS_IOC_RESERVE_COMPRESS_BLOCKS \ _IOR(F2FS_IOCTL_MAGIC, 19, __u64) +#define F2FS_IOC_SEC_TRIM_FILE _IOW(F2FS_IOCTL_MAGIC, 20, \ + struct f2fs_sectrim_range) #define F2FS_IOC_GET_VOLUME_NAME FS_IOC_GETFSLABEL #define F2FS_IOC_SET_VOLUME_NAME FS_IOC_SETFSLABEL @@ -453,6 +455,13 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal, #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */ #define F2FS_GOING_DOWN_NEED_FSCK 0x4 /* going down to trigger fsck */ +/* + * Flags used by F2FS_IOC_SEC_TRIM_FILE + */ +#define F2FS_TRIM_FILE_DISCARD 0x1 /* send discard command */ +#define F2FS_TRIM_FILE_ZEROOUT 0x2 /* zero out */ +#define F2FS_TRIM_FILE_MASK 0x3 + #if defined(__KERNEL__) && defined(CONFIG_COMPAT) /* * ioctl commands in 32 bit emulation @@ -488,6 +497,12 @@ struct f2fs_flush_device { u32 segments; /* # of segments to flush */ }; +struct f2fs_sectrim_range { + u64 start; + u64 len; + u64 flags; +}; + /* for inline stuff */ #define DEF_INLINE_RESERVED_SIZE 1 static inline int get_extra_isize(struct inode *inode); diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index dfa1ac2d751a..28cebbb10cae 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "f2fs.h" #include "node.h" @@ -3749,6 +3750,181 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) return ret; } +static int f2fs_secure_erase(struct block_device *bdev, block_t block, + block_t len, u32 flags) +{ + struct request_queue *q = bdev_get_queue(bdev); + sector_t sector = SECTOR_FROM_BLOCK(block); + sector_t nr_sects = SECTOR_FROM_BLOCK(len); + int ret = 0; + + if (!q) + return -ENXIO; + + if (flags & F2FS_TRIM_FILE_DISCARD) + ret = blkdev_issue_discard(bdev, sector, nr_sects, GFP_NOFS, + blk_queue_secure_erase(q) ? + BLKDEV_DISCARD_SECURE : 0); + + if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) + ret = blkdev_issue_zeroout(bdev, sector, nr_sects, GFP_NOFS, 0); + + return ret; +} + +static int f2fs_sec_trim_file(struct file *filp, unsigned long arg) +{ + struct inode *inode = file_inode(filp); + struct f2fs_sb_info *sbi = F2FS_I_SB(inode); + struct address_space *mapping = inode->i_mapping; + struct block_device *prev_bdev = NULL; + struct f2fs_sectrim_range range; + pgoff_t index, pg_end; + block_t prev_block = 0, len = 0; + u64 end_addr; + bool to_end; + int ret = 0; + + if (!(filp->f_mode & FMODE_WRITE)) + return -EBADF; + + if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg, + sizeof(range))) + return -EFAULT; + + if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK)) + return -EINVAL; + + if ((range.flags & F2FS_TRIM_FILE_DISCARD) && + !f2fs_hw_support_discard(sbi)) + return -EOPNOTSUPP; + + file_start_write(filp); + inode_lock(inode); + + if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode) || + f2fs_compressed_file(inode)) { + ret = -EINVAL; + goto err; + } + + if (inode->i_size == 0) + goto err; + + end_addr = range.start + range.len; + if (end_addr > inode->i_size) { + ret = -EINVAL; + goto err; + } + + to_end = (end_addr == inode->i_size); + if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) || + (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) { + ret = -EINVAL; + goto err; + } + + index = F2FS_BYTES_TO_BLK(range.start); + pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE); + + ret = f2fs_convert_inline_inode(inode); + if (ret) + goto err; + + down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); + down_write(&F2FS_I(inode)->i_mmap_sem); + + ret = filemap_write_and_wait_range(mapping, range.start, end_addr - 1); + if (ret) + goto out; + + truncate_inode_pages_range(mapping, range.start, + to_end ? -1 : end_addr - 1); + + while (index < pg_end) { + struct dnode_of_data dn; + pgoff_t end_offset, count; + int i; + + set_new_dnode(&dn, inode, NULL, NULL, 0); + ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); + if (ret) { + if (ret == -ENOENT) { + index = f2fs_get_next_page_offset(&dn, index); + continue; + } + goto out; + } + + end_offset = ADDRS_PER_PAGE(dn.node_page, inode); + count = min(end_offset - dn.ofs_in_node, pg_end - index); + for (i = 0; i < count; i++, dn.ofs_in_node++) { + struct block_device *cur_bdev; + block_t blkaddr = f2fs_data_blkaddr(&dn); + + if (!__is_valid_data_blkaddr(blkaddr)) + continue; + + if (!f2fs_is_valid_blkaddr(sbi, blkaddr, + DATA_GENERIC_ENHANCE)) { + ret = -EFSCORRUPTED; + f2fs_put_dnode(&dn); + goto out; + } + + cur_bdev = f2fs_target_device(sbi, blkaddr, NULL); + if (f2fs_is_multi_device(sbi)) { + int di = f2fs_target_device_index(sbi, blkaddr); + + blkaddr -= FDEV(di).start_blk; + } + + if (len) { + if (prev_bdev == cur_bdev && + blkaddr == prev_block + len) { + len++; + } else { + ret = f2fs_secure_erase(prev_bdev, + prev_block, len, range.flags); + if (ret) { + f2fs_put_dnode(&dn); + goto out; + } + + len = 0; + } + } + + if (!len) { + prev_bdev = cur_bdev; + prev_block = blkaddr; + len = 1; + } + } + + f2fs_put_dnode(&dn); + index += count; + + if (fatal_signal_pending(current)) { + ret = -EINTR; + goto out; + } + cond_resched(); + } + + if (len) + ret = f2fs_secure_erase(prev_bdev, prev_block, len, + range.flags); +out: + up_write(&F2FS_I(inode)->i_mmap_sem); + up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); +err: + inode_unlock(inode); + file_end_write(filp); + + return ret; +} + long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp))))) @@ -3835,6 +4011,8 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return f2fs_release_compress_blocks(filp, arg); case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: return f2fs_reserve_compress_blocks(filp, arg); + case F2FS_IOC_SEC_TRIM_FILE: + return f2fs_sec_trim_file(filp, arg); default: return -ENOTTY; } @@ -4004,6 +4182,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) case F2FS_IOC_GET_COMPRESS_BLOCKS: case F2FS_IOC_RELEASE_COMPRESS_BLOCKS: case F2FS_IOC_RESERVE_COMPRESS_BLOCKS: + case F2FS_IOC_SEC_TRIM_FILE: break; default: return -ENOIOCTLCMD; -- 2.27.0.290.gba653c62da-goog