cfg80211: Fix array-bounds warning in fragment copy
[linux/fpc-iii.git] / fs / crypto / bio.c
bloba409a84f1bcab289d676bfd1a2c9e0c39e14c1ad
1 /*
2 * This contains encryption functions for per-file encryption.
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility
7 * Written by Michael Halcrow, 2014.
9 * Filename encryption additions
10 * Uday Savagaonkar, 2014
11 * Encryption policy handling additions
12 * Ildar Muslukhov, 2014
13 * Add fscrypt_pullback_bio_page()
14 * Jaegeuk Kim, 2015.
16 * This has not yet undergone a rigorous security audit.
18 * The usage of AES-XTS should conform to recommendations in NIST
19 * Special Publication 800-38E and IEEE P1619/D16.
22 #include <linux/pagemap.h>
23 #include <linux/module.h>
24 #include <linux/bio.h>
25 #include <linux/namei.h>
26 #include "fscrypt_private.h"
29 * Call fscrypt_decrypt_page on every single page, reusing the encryption
30 * context.
32 static void completion_pages(struct work_struct *work)
34 struct fscrypt_ctx *ctx =
35 container_of(work, struct fscrypt_ctx, r.work);
36 struct bio *bio = ctx->r.bio;
37 struct bio_vec *bv;
38 int i;
40 bio_for_each_segment_all(bv, bio, i) {
41 struct page *page = bv->bv_page;
42 int ret = fscrypt_decrypt_page(page->mapping->host, page,
43 PAGE_SIZE, 0, page->index);
45 if (ret) {
46 WARN_ON_ONCE(1);
47 SetPageError(page);
48 } else {
49 SetPageUptodate(page);
51 unlock_page(page);
53 fscrypt_release_ctx(ctx);
54 bio_put(bio);
57 void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *ctx, struct bio *bio)
59 INIT_WORK(&ctx->r.work, completion_pages);
60 ctx->r.bio = bio;
61 queue_work(fscrypt_read_workqueue, &ctx->r.work);
63 EXPORT_SYMBOL(fscrypt_decrypt_bio_pages);
65 void fscrypt_pullback_bio_page(struct page **page, bool restore)
67 struct fscrypt_ctx *ctx;
68 struct page *bounce_page;
70 /* The bounce data pages are unmapped. */
71 if ((*page)->mapping)
72 return;
74 /* The bounce data page is unmapped. */
75 bounce_page = *page;
76 ctx = (struct fscrypt_ctx *)page_private(bounce_page);
78 /* restore control page */
79 *page = ctx->w.control_page;
81 if (restore)
82 fscrypt_restore_control_page(bounce_page);
84 EXPORT_SYMBOL(fscrypt_pullback_bio_page);
86 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
87 sector_t pblk, unsigned int len)
89 struct fscrypt_ctx *ctx;
90 struct page *ciphertext_page = NULL;
91 struct bio *bio;
92 int ret, err = 0;
94 BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
96 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
97 if (IS_ERR(ctx))
98 return PTR_ERR(ctx);
100 ciphertext_page = fscrypt_alloc_bounce_page(ctx, GFP_NOWAIT);
101 if (IS_ERR(ciphertext_page)) {
102 err = PTR_ERR(ciphertext_page);
103 goto errout;
106 while (len--) {
107 err = fscrypt_do_page_crypto(inode, FS_ENCRYPT, lblk,
108 ZERO_PAGE(0), ciphertext_page,
109 PAGE_SIZE, 0, GFP_NOFS);
110 if (err)
111 goto errout;
113 bio = bio_alloc(GFP_NOWAIT, 1);
114 if (!bio) {
115 err = -ENOMEM;
116 goto errout;
118 bio->bi_bdev = inode->i_sb->s_bdev;
119 bio->bi_iter.bi_sector =
120 pblk << (inode->i_sb->s_blocksize_bits - 9);
121 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
122 ret = bio_add_page(bio, ciphertext_page,
123 inode->i_sb->s_blocksize, 0);
124 if (ret != inode->i_sb->s_blocksize) {
125 /* should never happen! */
126 WARN_ON(1);
127 bio_put(bio);
128 err = -EIO;
129 goto errout;
131 err = submit_bio_wait(bio);
132 if ((err == 0) && bio->bi_error)
133 err = -EIO;
134 bio_put(bio);
135 if (err)
136 goto errout;
137 lblk++;
138 pblk++;
140 err = 0;
141 errout:
142 fscrypt_release_ctx(ctx);
143 return err;
145 EXPORT_SYMBOL(fscrypt_zeroout_range);