Skip to content

Commit dbe0e78

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux
Pull fscrypt updates from Eric Biggers: "A few cleanups for fs/crypto/, and another patch to prepare for the upcoming CephFS encryption support" * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux: fscrypt: optimize fscrypt_initialize() fscrypt: use WARN_ON_ONCE instead of WARN_ON fscrypt: new helper function - fscrypt_prepare_lookup_partial() fs/buffer.c: use b_folio for fscrypt work
2 parents 733f7e9 + 83e57e4 commit dbe0e78

File tree

11 files changed

+78
-36
lines changed

11 files changed

+78
-36
lines changed

fs/buffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ static void decrypt_bh(struct work_struct *work)
331331
struct buffer_head *bh = ctx->bh;
332332
int err;
333333

334-
err = fscrypt_decrypt_pagecache_blocks(page_folio(bh->b_page),
335-
bh->b_size, bh_offset(bh));
334+
err = fscrypt_decrypt_pagecache_blocks(bh->b_folio, bh->b_size,
335+
bh_offset(bh));
336336
if (err == 0 && need_fsverity(bh)) {
337337
/*
338338
* We use different work queues for decryption and for verity

fs/crypto/bio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
6969
pblk << (blockbits - SECTOR_SHIFT);
7070
}
7171
ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
72-
if (WARN_ON(ret != bytes_this_page)) {
72+
if (WARN_ON_ONCE(ret != bytes_this_page)) {
7373
err = -EIO;
7474
goto out;
7575
}
@@ -147,7 +147,7 @@ int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
147147
break;
148148
}
149149
nr_pages = i;
150-
if (WARN_ON(nr_pages <= 0))
150+
if (WARN_ON_ONCE(nr_pages <= 0))
151151
return -EINVAL;
152152

153153
/* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
@@ -170,7 +170,7 @@ int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
170170
offset += blocksize;
171171
if (offset == PAGE_SIZE || len == 0) {
172172
ret = bio_add_page(bio, pages[i++], offset, 0);
173-
if (WARN_ON(ret != offset)) {
173+
if (WARN_ON_ONCE(ret != offset)) {
174174
err = -EIO;
175175
goto out;
176176
}

fs/crypto/crypto.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,31 +308,36 @@ EXPORT_SYMBOL(fscrypt_decrypt_block_inplace);
308308

309309
/**
310310
* fscrypt_initialize() - allocate major buffers for fs encryption.
311-
* @cop_flags: fscrypt operations flags
311+
* @sb: the filesystem superblock
312312
*
313313
* We only call this when we start accessing encrypted files, since it
314314
* results in memory getting allocated that wouldn't otherwise be used.
315315
*
316316
* Return: 0 on success; -errno on failure
317317
*/
318-
int fscrypt_initialize(unsigned int cop_flags)
318+
int fscrypt_initialize(struct super_block *sb)
319319
{
320320
int err = 0;
321+
mempool_t *pool;
322+
323+
/* pairs with smp_store_release() below */
324+
if (likely(smp_load_acquire(&fscrypt_bounce_page_pool)))
325+
return 0;
321326

322327
/* No need to allocate a bounce page pool if this FS won't use it. */
323-
if (cop_flags & FS_CFLG_OWN_PAGES)
328+
if (sb->s_cop->flags & FS_CFLG_OWN_PAGES)
324329
return 0;
325330

326331
mutex_lock(&fscrypt_init_mutex);
327332
if (fscrypt_bounce_page_pool)
328333
goto out_unlock;
329334

330335
err = -ENOMEM;
331-
fscrypt_bounce_page_pool =
332-
mempool_create_page_pool(num_prealloc_crypto_pages, 0);
333-
if (!fscrypt_bounce_page_pool)
336+
pool = mempool_create_page_pool(num_prealloc_crypto_pages, 0);
337+
if (!pool)
334338
goto out_unlock;
335-
339+
/* pairs with smp_load_acquire() above */
340+
smp_store_release(&fscrypt_bounce_page_pool, pool);
336341
err = 0;
337342
out_unlock:
338343
mutex_unlock(&fscrypt_init_mutex);

fs/crypto/fname.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
110110
* Copy the filename to the output buffer for encrypting in-place and
111111
* pad it with the needed number of NUL bytes.
112112
*/
113-
if (WARN_ON(olen < iname->len))
113+
if (WARN_ON_ONCE(olen < iname->len))
114114
return -ENOBUFS;
115115
memcpy(out, iname->name, iname->len);
116116
memset(out + iname->len, 0, olen - iname->len);
@@ -570,7 +570,7 @@ u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name)
570570
{
571571
const struct fscrypt_info *ci = dir->i_crypt_info;
572572

573-
WARN_ON(!ci->ci_dirhash_key_initialized);
573+
WARN_ON_ONCE(!ci->ci_dirhash_key_initialized);
574574

575575
return siphash(name->name, name->len, &ci->ci_dirhash_key);
576576
}

fs/crypto/fscrypt_private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
101101
case FSCRYPT_CONTEXT_V2:
102102
return ctx->v2.nonce;
103103
}
104-
WARN_ON(1);
104+
WARN_ON_ONCE(1);
105105
return NULL;
106106
}
107107

@@ -264,7 +264,7 @@ typedef enum {
264264

265265
/* crypto.c */
266266
extern struct kmem_cache *fscrypt_info_cachep;
267-
int fscrypt_initialize(unsigned int cop_flags);
267+
int fscrypt_initialize(struct super_block *sb);
268268
int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
269269
u64 lblk_num, struct page *src_page,
270270
struct page *dest_page, unsigned int len,
@@ -386,7 +386,7 @@ fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
386386
const u8 *raw_key,
387387
const struct fscrypt_info *ci)
388388
{
389-
WARN_ON(1);
389+
WARN_ON_ONCE(1);
390390
return -EOPNOTSUPP;
391391
}
392392

fs/crypto/hkdf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
7979
return PTR_ERR(hmac_tfm);
8080
}
8181

82-
if (WARN_ON(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
82+
if (WARN_ON_ONCE(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
8383
err = -EINVAL;
8484
goto err_free_tfm;
8585
}
@@ -125,7 +125,7 @@ int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
125125
u8 counter = 1;
126126
u8 tmp[HKDF_HASHLEN];
127127

128-
if (WARN_ON(okmlen > 255 * HKDF_HASHLEN))
128+
if (WARN_ON_ONCE(okmlen > 255 * HKDF_HASHLEN))
129129
return -EINVAL;
130130

131131
desc->tfm = hkdf->hmac_tfm;

fs/crypto/hooks.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
111111
}
112112
EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup);
113113

114+
/**
115+
* fscrypt_prepare_lookup_partial() - prepare lookup without filename setup
116+
* @dir: the encrypted directory being searched
117+
* @dentry: the dentry being looked up in @dir
118+
*
119+
* This function should be used by the ->lookup and ->atomic_open methods of
120+
* filesystems that handle filename encryption and no-key name encoding
121+
* themselves and thus can't use fscrypt_prepare_lookup(). Like
122+
* fscrypt_prepare_lookup(), this will try to set up the directory's encryption
123+
* key and will set DCACHE_NOKEY_NAME on the dentry if the key is unavailable.
124+
* However, this function doesn't set up a struct fscrypt_name for the filename.
125+
*
126+
* Return: 0 on success; -errno on error. Note that the encryption key being
127+
* unavailable is not considered an error. It is also not an error if
128+
* the encryption policy is unsupported by this kernel; that is treated
129+
* like the key being unavailable, so that files can still be deleted.
130+
*/
131+
int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
132+
{
133+
int err = fscrypt_get_encryption_info(dir, true);
134+
135+
if (!err && !fscrypt_has_encryption_key(dir)) {
136+
spin_lock(&dentry->d_lock);
137+
dentry->d_flags |= DCACHE_NOKEY_NAME;
138+
spin_unlock(&dentry->d_lock);
139+
}
140+
return err;
141+
}
142+
EXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
143+
114144
int __fscrypt_prepare_readdir(struct inode *dir)
115145
{
116146
return fscrypt_get_encryption_info(dir, true);
@@ -315,7 +345,7 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
315345
int err;
316346

317347
/* This is for encrypted symlinks only */
318-
if (WARN_ON(!IS_ENCRYPTED(inode)))
348+
if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
319349
return ERR_PTR(-EINVAL);
320350

321351
/* If the decrypted target is already cached, just return it. */

fs/crypto/keyring.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void fscrypt_put_master_key(struct fscrypt_master_key *mk)
7373
* fscrypt_master_key struct itself after an RCU grace period ensures
7474
* that concurrent keyring lookups can no longer find it.
7575
*/
76-
WARN_ON(refcount_read(&mk->mk_active_refs) != 0);
76+
WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 0);
7777
key_put(mk->mk_users);
7878
mk->mk_users = NULL;
7979
call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key);
@@ -92,7 +92,7 @@ void fscrypt_put_master_key_activeref(struct super_block *sb,
9292
* destroying any subkeys embedded in it.
9393
*/
9494

95-
if (WARN_ON(!sb->s_master_keys))
95+
if (WARN_ON_ONCE(!sb->s_master_keys))
9696
return;
9797
spin_lock(&sb->s_master_keys->lock);
9898
hlist_del_rcu(&mk->mk_node);
@@ -102,8 +102,8 @@ void fscrypt_put_master_key_activeref(struct super_block *sb,
102102
* ->mk_active_refs == 0 implies that ->mk_secret is not present and
103103
* that ->mk_decrypted_inodes is empty.
104104
*/
105-
WARN_ON(is_master_key_secret_present(&mk->mk_secret));
106-
WARN_ON(!list_empty(&mk->mk_decrypted_inodes));
105+
WARN_ON_ONCE(is_master_key_secret_present(&mk->mk_secret));
106+
WARN_ON_ONCE(!list_empty(&mk->mk_decrypted_inodes));
107107

108108
for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
109109
fscrypt_destroy_prepared_key(
@@ -237,9 +237,9 @@ void fscrypt_destroy_keyring(struct super_block *sb)
237237
* with ->mk_secret. There should be no structural refs
238238
* beyond the one associated with the active ref.
239239
*/
240-
WARN_ON(refcount_read(&mk->mk_active_refs) != 1);
241-
WARN_ON(refcount_read(&mk->mk_struct_refs) != 1);
242-
WARN_ON(!is_master_key_secret_present(&mk->mk_secret));
240+
WARN_ON_ONCE(refcount_read(&mk->mk_active_refs) != 1);
241+
WARN_ON_ONCE(refcount_read(&mk->mk_struct_refs) != 1);
242+
WARN_ON_ONCE(!is_master_key_secret_present(&mk->mk_secret));
243243
wipe_master_key_secret(&mk->mk_secret);
244244
fscrypt_put_master_key_activeref(sb, mk);
245245
}

fs/crypto/keysetup.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
125125
pr_info("fscrypt: %s using implementation \"%s\"\n",
126126
mode->friendly_name, crypto_skcipher_driver_name(tfm));
127127
}
128-
if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
128+
if (WARN_ON_ONCE(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
129129
err = -EINVAL;
130130
goto err_free_tfm;
131131
}
@@ -199,7 +199,7 @@ static int setup_per_mode_enc_key(struct fscrypt_info *ci,
199199
unsigned int hkdf_infolen = 0;
200200
int err;
201201

202-
if (WARN_ON(mode_num > FSCRYPT_MODE_MAX))
202+
if (WARN_ON_ONCE(mode_num > FSCRYPT_MODE_MAX))
203203
return -EINVAL;
204204

205205
prep_key = &keys[mode_num];
@@ -282,8 +282,8 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
282282
void fscrypt_hash_inode_number(struct fscrypt_info *ci,
283283
const struct fscrypt_master_key *mk)
284284
{
285-
WARN_ON(ci->ci_inode->i_ino == 0);
286-
WARN_ON(!mk->mk_ino_hash_key_initialized);
285+
WARN_ON_ONCE(ci->ci_inode->i_ino == 0);
286+
WARN_ON_ONCE(!mk->mk_ino_hash_key_initialized);
287287

288288
ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
289289
&mk->mk_ino_hash_key);
@@ -503,7 +503,7 @@ static int setup_file_encryption_key(struct fscrypt_info *ci,
503503
err = fscrypt_setup_v2_file_key(ci, mk, need_dirhash_key);
504504
break;
505505
default:
506-
WARN_ON(1);
506+
WARN_ON_ONCE(1);
507507
err = -EINVAL;
508508
break;
509509
}
@@ -560,7 +560,7 @@ fscrypt_setup_encryption_info(struct inode *inode,
560560
struct fscrypt_master_key *mk = NULL;
561561
int res;
562562

563-
res = fscrypt_initialize(inode->i_sb->s_cop->flags);
563+
res = fscrypt_initialize(inode->i_sb);
564564
if (res)
565565
return res;
566566

@@ -577,7 +577,7 @@ fscrypt_setup_encryption_info(struct inode *inode,
577577
res = PTR_ERR(mode);
578578
goto out;
579579
}
580-
WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
580+
WARN_ON_ONCE(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
581581
crypt_info->ci_mode = mode;
582582

583583
res = setup_file_encryption_key(crypt_info, need_dirhash_key, &mk);

fs/crypto/policy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
4848
FSCRYPT_KEY_IDENTIFIER_SIZE);
4949
return 0;
5050
default:
51-
WARN_ON(1);
51+
WARN_ON_ONCE(1);
5252
return -EINVAL;
5353
}
5454
}
@@ -463,7 +463,7 @@ static int set_encryption_policy(struct inode *inode,
463463
current->comm, current->pid);
464464
break;
465465
default:
466-
WARN_ON(1);
466+
WARN_ON_ONCE(1);
467467
return -EINVAL;
468468
}
469469

0 commit comments

Comments
 (0)