From patchwork Mon Oct 9 19:15:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9994087 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4746E60223 for ; Mon, 9 Oct 2017 19:19:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3B2B0284AA for ; Mon, 9 Oct 2017 19:19:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2EDD8286BF; Mon, 9 Oct 2017 19:19:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D0BC6284AA for ; Mon, 9 Oct 2017 19:19:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755059AbdJITTM (ORCPT ); Mon, 9 Oct 2017 15:19:12 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:34861 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754772AbdJITTF (ORCPT ); Mon, 9 Oct 2017 15:19:05 -0400 Received: by mail-pf0-f195.google.com with SMTP id i23so29876571pfi.2; Mon, 09 Oct 2017 12:19:05 -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:in-reply-to:references; bh=lnCFLmTHIE3cYMhlwH5Y7fgroBczDPBRSzqOVWvsk6U=; b=F2JdxmXe8hPEnJVbrLVPaiOwPnaKwyezm/IsROAtvGPPSc+2e/vgvKve53bi3i8l2+ XXD3J4ycMJSzG8nZar+JK8JN/62lMTJ5qK7tMINfelAFddZcaHtTt8vNkUUvVRXemXtJ +NYyYoSjl/SWVe5q8NEReng3o3Dnu3TFgct6xlavZDh5xptlahj8iKbUHIFREHJtK00V FqdZ/Mvy01Q1jaPzd1xYaf7WtEXqx23BV8Bf40cMqgudKHdYcW3vHGjrKGUeA2TpadvD LsEI4cHlhPZLZ2/5yzv38dWKqQybrCbRlXIW44gc9e640cjjRvDPsBJpfvKCBwrFLH1S 2pbg== 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:in-reply-to :references; bh=lnCFLmTHIE3cYMhlwH5Y7fgroBczDPBRSzqOVWvsk6U=; b=ogjqoURUN+sKiocLLpmV7KvTZKuiIgFEknjGdKwOt0minUSSVW5J7iBSpRyvZvaDmh mMRKMRiFagCTeQrP4YnUs5UwtrxOhmJNrnTEl7NeLqmH3rKj77RruXcLl/t40cxswS4p lyHaWPgcJTBTLmevGfrTj/uIH4jBpxC5wreiIIawWuV9+QGEAmAlKPyCiMnRcEcjxEZ9 0kjUGv5n68i/GshYe2uU7BpXHEHtmef1BlmSlp7aXp09yhgf/9yPZy4vcWcuLmL1qCr5 W15kD08Oaj7TgLK0EgvTiFn1z+RCK92A8h4s7A+R8GY+S4OCsfLq3iPpp7DKBVOfqr3T LA9w== X-Gm-Message-State: AMCzsaWek5uld6ggh6bZS7N9gcr+3m3BXaj4HjwoaxrSaDz/4ewbj8Pb oMo0QOWPU4W2pfJtxazZfW0xNMh7 X-Google-Smtp-Source: AOwi7QAEn31UmfIj3gn7fF0+xCR8deYcdbJUvLD6u2/A0J7fDlej09UGpCS9hzOQ15anbzXCR3CzhA== X-Received: by 10.84.235.198 with SMTP id m6mr9749629plt.57.1507576745021; Mon, 09 Oct 2017 12:19:05 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id n29sm17039819pgf.44.2017.10.09.12.19.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 09 Oct 2017 12:19:04 -0700 (PDT) From: Eric Biggers To: linux-fscrypt@vger.kernel.org, "Theodore Y . Ts'o" Cc: Jaegeuk Kim , linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, Eric Biggers Subject: [PATCH v2 06/11] fscrypt: new helper function - fscrypt_require_key() Date: Mon, 9 Oct 2017 12:15:39 -0700 Message-Id: <20171009191544.43656-7-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.2.920.gcf0c67979c-goog In-Reply-To: <20171009191544.43656-1-ebiggers3@gmail.com> References: <20171009191544.43656-1-ebiggers3@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Add a helper function which checks if an inode is encrypted, and if so, tries to set up its encryption key. This is a pattern which is duplicated in multiple places in each of ext4, f2fs, and ubifs --- for example, when a regular file is asked to be opened or truncated. Acked-by: Dave Chinner Signed-off-by: Eric Biggers --- include/linux/fscrypt.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index d58e6a90cfe4..b3e2a5f93415 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h @@ -151,5 +151,30 @@ static inline bool fscrypt_has_encryption_key(const struct inode *inode) #include #endif /* __FS_HAS_ENCRYPTION */ +/** + * fscrypt_require_key - require an inode's encryption key + * @inode: the inode we need the key for + * + * If the inode is encrypted, set up its encryption key if not already done. + * Then require that the key be present and return -ENOKEY otherwise. + * + * No locks are needed, and the key will live as long as the struct inode --- so + * it won't go away from under you. + * + * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code + * if a problem occurred while setting up the encryption key. + */ +static inline int fscrypt_require_key(struct inode *inode) +{ + if (IS_ENCRYPTED(inode)) { + int err = fscrypt_get_encryption_info(inode); + + if (err) + return err; + if (!fscrypt_has_encryption_key(inode)) + return -ENOKEY; + } + return 0; +} #endif /* _LINUX_FSCRYPT_H */