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 X-Spam-Level: X-Spam-Status: No, score=-20.6 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B48F6C433E7 for ; Mon, 20 Jul 2020 23:37:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 957C322B4D for ; Mon, 20 Jul 2020 23:37:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lkpZaPPO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728052AbgGTXhp (ORCPT ); Mon, 20 Jul 2020 19:37:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727073AbgGTXho (ORCPT ); Mon, 20 Jul 2020 19:37:44 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87383C0619D4 for ; Mon, 20 Jul 2020 16:37:44 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id j9so14849248pgm.8 for ; Mon, 20 Jul 2020 16:37:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=78GCUtC2EJb5xFkhhGe6IF9iMHYw65a1BFMDAATd+tM=; b=lkpZaPPO0o9p4yIi3O6zslsFFa6VTPWtl9+t0qQhOOHEfjykofCGv4XWyGbTVGdZBG HzWDaEtN8698pdn2aTFpG8HuDPZfDac0Y/XH9WALkV4xMk4RcIO9bcz2ZXOpaY0Sfehx lP71p+Rta787ELUsWjbLhMHWDGiFThC9sMyjANfpSd26fX1QXddv9rX0t5wBFzIZSeR2 s9T5O03polQpLellXjs1QFq90w68S3SdWsUozTPGniK/YZTIqHTdGkIloqhPUHAEMAV0 2SPVD+fqQJyOQbl2r4Mid3U2Lt99zM2U41kXVxc4sPtt2HLgvlIFc0EXEWIxHiVJaQjD Mrgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=78GCUtC2EJb5xFkhhGe6IF9iMHYw65a1BFMDAATd+tM=; b=uga+PFk6EJpP9Q6DaA2BvTd4eV2hY2QeWZ6MlhrRuUg11KO+R+zPw4hcbo06lwXh4w cPCbUTTTeawhJwizGFJDKQhoGngq5xs2ZBg3dcp4/T5AUYUoaD9nF4nVgk1pgtS2M7Hx IIBA4L9Nd6yko9zTUHDwGLtiyA2pKUmjL5T1nnCKI3XdEjtRfUEmHyaS1zDVWGAm3S1J fFnC1KC10OvVcArTkf3Ge7/Lzyv2ibL3jneHl5SL94pdAt/jBk+BZADJ2HYVhBiSv4i1 AxARlvWJCGbuJtkr+TyTViCPRA1G+F+Flhq+OFl5s5UL/d3dSrecNEaS3CIKiz2aaEr5 ZZsQ== X-Gm-Message-State: AOAM5335Um6c/i2Yz/mc7KKUZhP3zroemzS2rBOTFUxQcr7U3AKAMKk4 SmHSZZcbCbPF6+JA8kUQTqkkl+bVoqaCjZR+Qc1UC8DBqBOoePPwUUn/wqChPEXRzA98suLoVDl fq49BfYvt5ZsIB8iugFMg9m4+uf7oPbcuD6nnuxlzjW4SdkUPL2VyQ2VvnIwH6bsVfZlxEn4= X-Google-Smtp-Source: ABdhPJy7HvbL+o/wyQ1O2/q8SGQvr4PUhB7PDyMWpWlUU6ltb62Bb253HTRsPOHxSxZ+knnHL7Gb31iKEic= X-Received: by 2002:a62:3741:: with SMTP id e62mr20982650pfa.127.1595288263980; Mon, 20 Jul 2020 16:37:43 -0700 (PDT) Date: Mon, 20 Jul 2020 23:37:33 +0000 In-Reply-To: <20200720233739.824943-1-satyat@google.com> Message-Id: <20200720233739.824943-2-satyat@google.com> Mime-Version: 1.0 References: <20200720233739.824943-1-satyat@google.com> X-Mailer: git-send-email 2.28.0.rc0.105.gf9edc3c819-goog Subject: [PATCH v4 1/7] fscrypt: Add functions for direct I/O support From: Satya Tangirala To: linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-ext4@vger.kernel.org Cc: linux-xfs@vger.kernel.org, Eric Biggers , Satya Tangirala Content-Type: text/plain; charset="UTF-8" Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Eric Biggers Introduce fscrypt_dio_supported() to check whether a direct I/O request is unsupported due to encryption constraints. Also introduce fscrypt_limit_io_pages() to limit how many pages can be added to a bio being prepared for direct I/O. This is needed for the iomap direct I/O implementation to avoid DUN wraparound in the middle of a bio (which is possible with the IV_INO_LBLK_32 IV generation method). Elsewhere fscrypt_mergeable_bio() is used for this, but iomap operates on logical ranges directly and doesn't have a chance to call fscrypt_mergeable_bio() on every block or page. So we need this function which limits a logical range in one go. Signed-off-by: Eric Biggers Co-developed-by: Satya Tangirala Signed-off-by: Satya Tangirala --- fs/crypto/crypto.c | 8 ++++ fs/crypto/inline_crypt.c | 82 ++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt.h | 19 ++++++++++ 3 files changed, 109 insertions(+) diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index a52cf32733ab..fb34364360b3 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c @@ -69,6 +69,14 @@ void fscrypt_free_bounce_page(struct page *bounce_page) } EXPORT_SYMBOL(fscrypt_free_bounce_page); +/* + * Generate the IV for the given logical block number within the given file. + * For filenames encryption, lblk_num == 0. + * + * Keep this in sync with fscrypt_limit_io_pages(). fscrypt_limit_io_pages() + * needs to know about any IV generation methods where the low bits of IV don't + * simply contain the lblk_num (e.g., IV_INO_LBLK_32). + */ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, const struct fscrypt_info *ci) { diff --git a/fs/crypto/inline_crypt.c b/fs/crypto/inline_crypt.c index d7aecadf33c1..578739712e00 100644 --- a/fs/crypto/inline_crypt.c +++ b/fs/crypto/inline_crypt.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "fscrypt_private.h" @@ -362,3 +363,84 @@ bool fscrypt_mergeable_bio_bh(struct bio *bio, return fscrypt_mergeable_bio(bio, inode, next_lblk); } EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh); + +/** + * fscrypt_dio_supported() - check whether a direct I/O request is unsupported + * due to encryption constraints + * @iocb: the file and position the I/O is targeting + * @iter: the I/O data segment(s) + * + * Return: true if direct I/O is supported + */ +bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter) +{ + const struct inode *inode = file_inode(iocb->ki_filp); + const unsigned int blocksize = i_blocksize(inode); + + /* If the file is unencrypted, no veto from us. */ + if (!fscrypt_needs_contents_encryption(inode)) + return true; + + /* We only support direct I/O with inline crypto, not fs-layer crypto */ + if (!fscrypt_inode_uses_inline_crypto(inode)) + return false; + + /* + * Since the granularity of encryption is filesystem blocks, the I/O + * must be block aligned -- not just disk sector aligned. + */ + if (!IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), blocksize)) + return false; + + return true; +} +EXPORT_SYMBOL_GPL(fscrypt_dio_supported); + +/** + * fscrypt_limit_io_pages() - limit I/O pages to avoid discontiguous DUNs + * @inode: the file on which I/O is being done + * @pos: the file position (in bytes) at which the I/O is being done + * @nr_pages: the number of pages we want to submit starting at @pos + * + * Determine the limit to the number of pages that can be submitted in the bio + * targeting @pos without causing a data unit number (DUN) discontinuity. + * + * This is normally just @nr_pages, as normally the DUNs just increment along + * with the logical blocks. (Or the file is not encrypted.) + * + * In rare cases, fscrypt can be using an IV generation method that allows the + * DUN to wrap around within logically continuous blocks, and that wraparound + * will occur. If this happens, a value less than @nr_pages will be returned so + * that the wraparound doesn't occur in the middle of the bio. Note that we + * only support block_size == PAGE_SIZE (and page-aligned DIO) in such cases. + * + * Return: the actual number of pages that can be submitted + */ +int fscrypt_limit_io_pages(const struct inode *inode, loff_t pos, int nr_pages) +{ + const struct fscrypt_info *ci = inode->i_crypt_info; + u32 dun; + + if (!fscrypt_inode_uses_inline_crypto(inode)) + return nr_pages; + + if (nr_pages <= 1) + return nr_pages; + + if (!(fscrypt_policy_flags(&ci->ci_policy) & + FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) + return nr_pages; + + /* + * fscrypt_select_encryption_impl() ensures that block_size == PAGE_SIZE + * when using FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32. + */ + if (WARN_ON_ONCE(i_blocksize(inode) != PAGE_SIZE)) + return 1; + + /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */ + + dun = ci->ci_hashed_ino + (pos >> inode->i_blkbits); + + return min_t(u64, nr_pages, (u64)U32_MAX + 1 - dun); +} diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index bb257411365f..c205c214b35e 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -559,6 +559,11 @@ bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, bool fscrypt_mergeable_bio_bh(struct bio *bio, const struct buffer_head *next_bh); +bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter); + +int fscrypt_limit_io_pages(const struct inode *inode, loff_t pos, + int nr_pages); + #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) @@ -587,6 +592,20 @@ static inline bool fscrypt_mergeable_bio_bh(struct bio *bio, { return true; } + +static inline bool fscrypt_dio_supported(struct kiocb *iocb, + struct iov_iter *iter) +{ + const struct inode *inode = file_inode(iocb->ki_filp); + + return !fscrypt_needs_contents_encryption(inode); +} + +static inline int fscrypt_limit_io_pages(const struct inode *inode, loff_t pos, + int nr_pages) +{ + return nr_pages; +} #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ /** -- 2.28.0.rc0.105.gf9edc3c819-goog 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 X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2185C433FB for ; Mon, 20 Jul 2020 23:37:55 +0000 (UTC) Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7F45922B4D; Mon, 20 Jul 2020 23:37:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=lists.sourceforge.net header.i=@lists.sourceforge.net header.b="KmUuU2XB"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sourceforge.net header.i=@sourceforge.net header.b="KLa3pWlf"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sf.net header.i=@sf.net header.b="Pf5BsCwo"; dkim=neutral (0-bit key) header.d=google.com header.i=@google.com header.b="lkpZaPPO" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7F45922B4D Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=lists.sourceforge.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linux-f2fs-devel-bounces@lists.sourceforge.net DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type:Cc: Reply-To:From:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Subject:To:References:Mime-Version:Message-Id: In-Reply-To:Date:Sender:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=itpfZhR2Cw/THBYGF+GUDC1SqcWoLV2MPvB/hKt3CmM=; b=KmUuU2XB7LaLD0yU57tQMkuk+ Rx7l++pHnjV9l45/OilJPvVLCHY9kqOaxJiw9LK5eJOPLZRz/nfuTJajZIXOT3TiQa+KjpNBnekiX Cn3ckc/Nwg6yS1HogRF+s4oJG4WF61G0ZX5ywJXw28AsP0U7Tqt14FkCj/s8XylfIkMiw=; Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1jxfM2-0006kw-Bg; Mon, 20 Jul 2020 23:37:54 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from <3xyoWXwYKAHQkSlqSlYggYdW.Uge@flex--satyat.bounces.google.com>) id 1jxfLy-0006kb-PW for linux-f2fs-devel@lists.sourceforge.net; Mon, 20 Jul 2020 23:37:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=78GCUtC2EJb5xFkhhGe6IF9iMHYw65a1BFMDAATd+tM=; b=KLa3pWlfJSIVssz13WXFi1Wrv 1ZVddBuf9voebhKnqJ50463gncF4fud5+TdjCAL5IOHcoX6urFpZW3s6S0AtQ/fHBUdYsJLYWFfzd 279UhIWfKJQi3oqbUJiRIxhNHmNp66CBBCdlrIOX7j5yjcsKlPPjo6uXN0WVRIABMcqO0=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Cc:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=78GCUtC2EJb5xFkhhGe6IF9iMHYw65a1BFMDAATd+tM=; b=Pf5BsCwotm9gBK6/htwRpcaSuK Qdh28UpuBjFDEJRAtPhyXJP40YqE4Eti/yj3eLunSEBep1PJ5SqWfBDG+S0K/EhiZUXU3hOd9gCvk TcsnPXhxcARUjOBorC6TiT+RbB2TuGagPo3QXmFb0lGxJ/9U+/86ufp9OcdjczcKr8bI=; Received: from mail-pf1-f201.google.com ([209.85.210.201]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.2) id 1jxfLx-00BOmd-JM for linux-f2fs-devel@lists.sourceforge.net; Mon, 20 Jul 2020 23:37:50 +0000 Received: by mail-pf1-f201.google.com with SMTP id p127so13744079pfb.18 for ; Mon, 20 Jul 2020 16:37:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=78GCUtC2EJb5xFkhhGe6IF9iMHYw65a1BFMDAATd+tM=; b=lkpZaPPO0o9p4yIi3O6zslsFFa6VTPWtl9+t0qQhOOHEfjykofCGv4XWyGbTVGdZBG HzWDaEtN8698pdn2aTFpG8HuDPZfDac0Y/XH9WALkV4xMk4RcIO9bcz2ZXOpaY0Sfehx lP71p+Rta787ELUsWjbLhMHWDGiFThC9sMyjANfpSd26fX1QXddv9rX0t5wBFzIZSeR2 s9T5O03polQpLellXjs1QFq90w68S3SdWsUozTPGniK/YZTIqHTdGkIloqhPUHAEMAV0 2SPVD+fqQJyOQbl2r4Mid3U2Lt99zM2U41kXVxc4sPtt2HLgvlIFc0EXEWIxHiVJaQjD Mrgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=78GCUtC2EJb5xFkhhGe6IF9iMHYw65a1BFMDAATd+tM=; b=sxfLgZIWZjiPUYUPuFtaBiXbWT25swSGp4szDESb7IvCDkoIX+1GaNA9dzzHLYMhQQ 5RKkN06WBEAoXbV3YCSPMwbGxvrC2rKFOrX9Ai6X52XZUhSmBs+jlFZkNQoTpd+1hUEs 5ZQz5Ku1mo/huLAB6Tz7Dxbujo8Q3EI6vHK+izAP+vhugQ4Cf+WE0JIhM5+8/qpcszJG v6KJPbhuoKgK/hqQ015b1PJ4L+rcM7WmK9SM0xK48wI80+gVi/YZR8kMtujHgPPn3txo +6vlk7OZdPC6It09ukdSo23RgZAzGtyvm3gEHTP9xCj/U89HPp6l4A8g2DsNBdrZ4bhn PXsA== X-Gm-Message-State: AOAM531yVG6WRXqGeewZzYJknuMkJwYjZ14eQrJKWMUnrUakJ5/sROkB Im2rPwvQunVAHcCdCwPGDZbcWHZnc5E= X-Google-Smtp-Source: ABdhPJy7HvbL+o/wyQ1O2/q8SGQvr4PUhB7PDyMWpWlUU6ltb62Bb253HTRsPOHxSxZ+knnHL7Gb31iKEic= X-Received: by 2002:a62:3741:: with SMTP id e62mr20982650pfa.127.1595288263980; Mon, 20 Jul 2020 16:37:43 -0700 (PDT) Date: Mon, 20 Jul 2020 23:37:33 +0000 In-Reply-To: <20200720233739.824943-1-satyat@google.com> Message-Id: <20200720233739.824943-2-satyat@google.com> Mime-Version: 1.0 References: <20200720233739.824943-1-satyat@google.com> X-Mailer: git-send-email 2.28.0.rc0.105.gf9edc3c819-goog To: linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-ext4@vger.kernel.org X-Headers-End: 1jxfLx-00BOmd-JM Subject: [f2fs-dev] [PATCH v4 1/7] fscrypt: Add functions for direct I/O support X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Satya Tangirala via Linux-f2fs-devel Reply-To: Satya Tangirala Cc: linux-xfs@vger.kernel.org, Satya Tangirala , Eric Biggers Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net From: Eric Biggers Introduce fscrypt_dio_supported() to check whether a direct I/O request is unsupported due to encryption constraints. Also introduce fscrypt_limit_io_pages() to limit how many pages can be added to a bio being prepared for direct I/O. This is needed for the iomap direct I/O implementation to avoid DUN wraparound in the middle of a bio (which is possible with the IV_INO_LBLK_32 IV generation method). Elsewhere fscrypt_mergeable_bio() is used for this, but iomap operates on logical ranges directly and doesn't have a chance to call fscrypt_mergeable_bio() on every block or page. So we need this function which limits a logical range in one go. Signed-off-by: Eric Biggers Co-developed-by: Satya Tangirala Signed-off-by: Satya Tangirala --- fs/crypto/crypto.c | 8 ++++ fs/crypto/inline_crypt.c | 82 ++++++++++++++++++++++++++++++++++++++++ include/linux/fscrypt.h | 19 ++++++++++ 3 files changed, 109 insertions(+) diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index a52cf32733ab..fb34364360b3 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c @@ -69,6 +69,14 @@ void fscrypt_free_bounce_page(struct page *bounce_page) } EXPORT_SYMBOL(fscrypt_free_bounce_page); +/* + * Generate the IV for the given logical block number within the given file. + * For filenames encryption, lblk_num == 0. + * + * Keep this in sync with fscrypt_limit_io_pages(). fscrypt_limit_io_pages() + * needs to know about any IV generation methods where the low bits of IV don't + * simply contain the lblk_num (e.g., IV_INO_LBLK_32). + */ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, const struct fscrypt_info *ci) { diff --git a/fs/crypto/inline_crypt.c b/fs/crypto/inline_crypt.c index d7aecadf33c1..578739712e00 100644 --- a/fs/crypto/inline_crypt.c +++ b/fs/crypto/inline_crypt.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "fscrypt_private.h" @@ -362,3 +363,84 @@ bool fscrypt_mergeable_bio_bh(struct bio *bio, return fscrypt_mergeable_bio(bio, inode, next_lblk); } EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh); + +/** + * fscrypt_dio_supported() - check whether a direct I/O request is unsupported + * due to encryption constraints + * @iocb: the file and position the I/O is targeting + * @iter: the I/O data segment(s) + * + * Return: true if direct I/O is supported + */ +bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter) +{ + const struct inode *inode = file_inode(iocb->ki_filp); + const unsigned int blocksize = i_blocksize(inode); + + /* If the file is unencrypted, no veto from us. */ + if (!fscrypt_needs_contents_encryption(inode)) + return true; + + /* We only support direct I/O with inline crypto, not fs-layer crypto */ + if (!fscrypt_inode_uses_inline_crypto(inode)) + return false; + + /* + * Since the granularity of encryption is filesystem blocks, the I/O + * must be block aligned -- not just disk sector aligned. + */ + if (!IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), blocksize)) + return false; + + return true; +} +EXPORT_SYMBOL_GPL(fscrypt_dio_supported); + +/** + * fscrypt_limit_io_pages() - limit I/O pages to avoid discontiguous DUNs + * @inode: the file on which I/O is being done + * @pos: the file position (in bytes) at which the I/O is being done + * @nr_pages: the number of pages we want to submit starting at @pos + * + * Determine the limit to the number of pages that can be submitted in the bio + * targeting @pos without causing a data unit number (DUN) discontinuity. + * + * This is normally just @nr_pages, as normally the DUNs just increment along + * with the logical blocks. (Or the file is not encrypted.) + * + * In rare cases, fscrypt can be using an IV generation method that allows the + * DUN to wrap around within logically continuous blocks, and that wraparound + * will occur. If this happens, a value less than @nr_pages will be returned so + * that the wraparound doesn't occur in the middle of the bio. Note that we + * only support block_size == PAGE_SIZE (and page-aligned DIO) in such cases. + * + * Return: the actual number of pages that can be submitted + */ +int fscrypt_limit_io_pages(const struct inode *inode, loff_t pos, int nr_pages) +{ + const struct fscrypt_info *ci = inode->i_crypt_info; + u32 dun; + + if (!fscrypt_inode_uses_inline_crypto(inode)) + return nr_pages; + + if (nr_pages <= 1) + return nr_pages; + + if (!(fscrypt_policy_flags(&ci->ci_policy) & + FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) + return nr_pages; + + /* + * fscrypt_select_encryption_impl() ensures that block_size == PAGE_SIZE + * when using FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32. + */ + if (WARN_ON_ONCE(i_blocksize(inode) != PAGE_SIZE)) + return 1; + + /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */ + + dun = ci->ci_hashed_ino + (pos >> inode->i_blkbits); + + return min_t(u64, nr_pages, (u64)U32_MAX + 1 - dun); +} diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index bb257411365f..c205c214b35e 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -559,6 +559,11 @@ bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, bool fscrypt_mergeable_bio_bh(struct bio *bio, const struct buffer_head *next_bh); +bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter); + +int fscrypt_limit_io_pages(const struct inode *inode, loff_t pos, + int nr_pages); + #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode) @@ -587,6 +592,20 @@ static inline bool fscrypt_mergeable_bio_bh(struct bio *bio, { return true; } + +static inline bool fscrypt_dio_supported(struct kiocb *iocb, + struct iov_iter *iter) +{ + const struct inode *inode = file_inode(iocb->ki_filp); + + return !fscrypt_needs_contents_encryption(inode); +} + +static inline int fscrypt_limit_io_pages(const struct inode *inode, loff_t pos, + int nr_pages) +{ + return nr_pages; +} #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ /** -- 2.28.0.rc0.105.gf9edc3c819-goog _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel