1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * The base64 encode/decode code was copied from fscrypt:
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility
6 * Written by Uday Savagaonkar, 2014.
7 * Modified by Jaegeuk Kim, 2015.
8 */
9 #include <linux/ceph/ceph_debug.h>
10 #include <linux/xattr.h>
11 #include <linux/fscrypt.h>
12 #include <linux/ceph/striper.h>
13
14 #include "super.h"
15 #include "mds_client.h"
16 #include "crypto.h"
17
18 /*
19 * The base64url encoding used by fscrypt includes the '_' character, which may
20 * cause problems in snapshot names (which can not start with '_'). Thus, we
21 * used the base64 encoding defined for IMAP mailbox names (RFC 3501) instead,
22 * which replaces '-' and '_' by '+' and ','.
23 */
24 static const char base64_table[65] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
26
ceph_base64_encode(const u8 * src,int srclen,char * dst)27 int ceph_base64_encode(const u8 *src, int srclen, char *dst)
28 {
29 u32 ac = 0;
30 int bits = 0;
31 int i;
32 char *cp = dst;
33
34 for (i = 0; i < srclen; i++) {
35 ac = (ac << 8) | src[i];
36 bits += 8;
37 do {
38 bits -= 6;
39 *cp++ = base64_table[(ac >> bits) & 0x3f];
40 } while (bits >= 6);
41 }
42 if (bits)
43 *cp++ = base64_table[(ac << (6 - bits)) & 0x3f];
44 return cp - dst;
45 }
46
ceph_base64_decode(const char * src,int srclen,u8 * dst)47 int ceph_base64_decode(const char *src, int srclen, u8 *dst)
48 {
49 u32 ac = 0;
50 int bits = 0;
51 int i;
52 u8 *bp = dst;
53
54 for (i = 0; i < srclen; i++) {
55 const char *p = strchr(base64_table, src[i]);
56
57 if (p == NULL || src[i] == 0)
58 return -1;
59 ac = (ac << 6) | (p - base64_table);
60 bits += 6;
61 if (bits >= 8) {
62 bits -= 8;
63 *bp++ = (u8)(ac >> bits);
64 }
65 }
66 if (ac & ((1 << bits) - 1))
67 return -1;
68 return bp - dst;
69 }
70
ceph_crypt_get_context(struct inode * inode,void * ctx,size_t len)71 static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
72 {
73 struct ceph_inode_info *ci = ceph_inode(inode);
74 struct ceph_fscrypt_auth *cfa = (struct ceph_fscrypt_auth *)ci->fscrypt_auth;
75 u32 ctxlen;
76
77 /* Non existent or too short? */
78 if (!cfa || (ci->fscrypt_auth_len < (offsetof(struct ceph_fscrypt_auth, cfa_blob) + 1)))
79 return -ENOBUFS;
80
81 /* Some format we don't recognize? */
82 if (le32_to_cpu(cfa->cfa_version) != CEPH_FSCRYPT_AUTH_VERSION)
83 return -ENOBUFS;
84
85 ctxlen = le32_to_cpu(cfa->cfa_blob_len);
86 if (len < ctxlen)
87 return -ERANGE;
88
89 memcpy(ctx, cfa->cfa_blob, ctxlen);
90 return ctxlen;
91 }
92
ceph_crypt_set_context(struct inode * inode,const void * ctx,size_t len,void * fs_data)93 static int ceph_crypt_set_context(struct inode *inode, const void *ctx,
94 size_t len, void *fs_data)
95 {
96 int ret;
97 struct iattr attr = { };
98 struct ceph_iattr cia = { };
99 struct ceph_fscrypt_auth *cfa;
100
101 WARN_ON_ONCE(fs_data);
102
103 if (len > FSCRYPT_SET_CONTEXT_MAX_SIZE)
104 return -EINVAL;
105
106 cfa = kzalloc(sizeof(*cfa), GFP_KERNEL);
107 if (!cfa)
108 return -ENOMEM;
109
110 cfa->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
111 cfa->cfa_blob_len = cpu_to_le32(len);
112 memcpy(cfa->cfa_blob, ctx, len);
113
114 cia.fscrypt_auth = cfa;
115
116 ret = __ceph_setattr(&nop_mnt_idmap, inode, &attr, &cia);
117 if (ret == 0)
118 inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
119 kfree(cia.fscrypt_auth);
120 return ret;
121 }
122
ceph_crypt_empty_dir(struct inode * inode)123 static bool ceph_crypt_empty_dir(struct inode *inode)
124 {
125 struct ceph_inode_info *ci = ceph_inode(inode);
126
127 return ci->i_rsubdirs + ci->i_rfiles == 1;
128 }
129
ceph_get_dummy_policy(struct super_block * sb)130 static const union fscrypt_policy *ceph_get_dummy_policy(struct super_block *sb)
131 {
132 return ceph_sb_to_fs_client(sb)->fsc_dummy_enc_policy.policy;
133 }
134
135 static struct fscrypt_operations ceph_fscrypt_ops = {
136 .needs_bounce_pages = 1,
137 .get_context = ceph_crypt_get_context,
138 .set_context = ceph_crypt_set_context,
139 .get_dummy_policy = ceph_get_dummy_policy,
140 .empty_dir = ceph_crypt_empty_dir,
141 };
142
ceph_fscrypt_set_ops(struct super_block * sb)143 void ceph_fscrypt_set_ops(struct super_block *sb)
144 {
145 fscrypt_set_ops(sb, &ceph_fscrypt_ops);
146 }
147
ceph_fscrypt_free_dummy_policy(struct ceph_fs_client * fsc)148 void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc)
149 {
150 fscrypt_free_dummy_policy(&fsc->fsc_dummy_enc_policy);
151 }
152
ceph_fscrypt_prepare_context(struct inode * dir,struct inode * inode,struct ceph_acl_sec_ctx * as)153 int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
154 struct ceph_acl_sec_ctx *as)
155 {
156 int ret, ctxsize;
157 bool encrypted = false;
158 struct ceph_inode_info *ci = ceph_inode(inode);
159
160 ret = fscrypt_prepare_new_inode(dir, inode, &encrypted);
161 if (ret)
162 return ret;
163 if (!encrypted)
164 return 0;
165
166 as->fscrypt_auth = kzalloc(sizeof(*as->fscrypt_auth), GFP_KERNEL);
167 if (!as->fscrypt_auth)
168 return -ENOMEM;
169
170 ctxsize = fscrypt_context_for_new_inode(as->fscrypt_auth->cfa_blob,
171 inode);
172 if (ctxsize < 0)
173 return ctxsize;
174
175 as->fscrypt_auth->cfa_version = cpu_to_le32(CEPH_FSCRYPT_AUTH_VERSION);
176 as->fscrypt_auth->cfa_blob_len = cpu_to_le32(ctxsize);
177
178 WARN_ON_ONCE(ci->fscrypt_auth);
179 kfree(ci->fscrypt_auth);
180 ci->fscrypt_auth_len = ceph_fscrypt_auth_len(as->fscrypt_auth);
181 ci->fscrypt_auth = kmemdup(as->fscrypt_auth, ci->fscrypt_auth_len,
182 GFP_KERNEL);
183 if (!ci->fscrypt_auth)
184 return -ENOMEM;
185
186 inode->i_flags |= S_ENCRYPTED;
187
188 return 0;
189 }
190
ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request * req,struct ceph_acl_sec_ctx * as)191 void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
192 struct ceph_acl_sec_ctx *as)
193 {
194 swap(req->r_fscrypt_auth, as->fscrypt_auth);
195 }
196
197 /*
198 * User-created snapshots can't start with '_'. Snapshots that start with this
199 * character are special (hint: there aren't real snapshots) and use the
200 * following format:
201 *
202 * _<SNAPSHOT-NAME>_<INODE-NUMBER>
203 *
204 * where:
205 * - <SNAPSHOT-NAME> - the real snapshot name that may need to be decrypted,
206 * - <INODE-NUMBER> - the inode number (in decimal) for the actual snapshot
207 *
208 * This function parses these snapshot names and returns the inode
209 * <INODE-NUMBER>. 'name_len' will also bet set with the <SNAPSHOT-NAME>
210 * length.
211 */
parse_longname(const struct inode * parent,const char * name,int * name_len)212 static struct inode *parse_longname(const struct inode *parent,
213 const char *name, int *name_len)
214 {
215 struct ceph_client *cl = ceph_inode_to_client(parent);
216 struct inode *dir = NULL;
217 struct ceph_vino vino = { .snap = CEPH_NOSNAP };
218 char *name_end, *inode_number;
219 int ret = -EIO;
220 /* NUL-terminate */
221 char *str __free(kfree) = kmemdup_nul(name, *name_len, GFP_KERNEL);
222 if (!str)
223 return ERR_PTR(-ENOMEM);
224 /* Skip initial '_' */
225 str++;
226 name_end = strrchr(str, '_');
227 if (!name_end) {
228 doutc(cl, "failed to parse long snapshot name: %s\n", str);
229 return ERR_PTR(-EIO);
230 }
231 *name_len = (name_end - str);
232 if (*name_len <= 0) {
233 pr_err_client(cl, "failed to parse long snapshot name\n");
234 return ERR_PTR(-EIO);
235 }
236
237 /* Get the inode number */
238 inode_number = name_end + 1;
239 ret = kstrtou64(inode_number, 10, &vino.ino);
240 if (ret) {
241 doutc(cl, "failed to parse inode number: %s\n", str);
242 return ERR_PTR(ret);
243 }
244
245 /* And finally the inode */
246 dir = ceph_find_inode(parent->i_sb, vino);
247 if (!dir) {
248 /* This can happen if we're not mounting cephfs on the root */
249 dir = ceph_get_inode(parent->i_sb, vino, NULL);
250 if (IS_ERR(dir))
251 doutc(cl, "can't find inode %s (%s)\n", inode_number, name);
252 }
253 return dir;
254 }
255
ceph_encode_encrypted_dname(struct inode * parent,struct qstr * d_name,char * buf)256 int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
257 char *buf)
258 {
259 struct ceph_client *cl = ceph_inode_to_client(parent);
260 struct inode *dir = parent;
261 struct qstr iname;
262 u32 len;
263 int name_len;
264 int elen;
265 int ret;
266 u8 *cryptbuf = NULL;
267
268 iname.name = d_name->name;
269 name_len = d_name->len;
270
271 /* Handle the special case of snapshot names that start with '_' */
272 if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
273 (iname.name[0] == '_')) {
274 dir = parse_longname(parent, iname.name, &name_len);
275 if (IS_ERR(dir))
276 return PTR_ERR(dir);
277 iname.name++; /* skip initial '_' */
278 }
279 iname.len = name_len;
280
281 if (!fscrypt_has_encryption_key(dir)) {
282 memcpy(buf, d_name->name, d_name->len);
283 elen = d_name->len;
284 goto out;
285 }
286
287 /*
288 * Convert cleartext d_name to ciphertext. If result is longer than
289 * CEPH_NOHASH_NAME_MAX, sha256 the remaining bytes
290 *
291 * See: fscrypt_setup_filename
292 */
293 if (!fscrypt_fname_encrypted_size(dir, iname.len, NAME_MAX, &len)) {
294 elen = -ENAMETOOLONG;
295 goto out;
296 }
297
298 /* Allocate a buffer appropriate to hold the result */
299 cryptbuf = kmalloc(len > CEPH_NOHASH_NAME_MAX ? NAME_MAX : len,
300 GFP_KERNEL);
301 if (!cryptbuf) {
302 elen = -ENOMEM;
303 goto out;
304 }
305
306 ret = fscrypt_fname_encrypt(dir, &iname, cryptbuf, len);
307 if (ret) {
308 elen = ret;
309 goto out;
310 }
311
312 /* hash the end if the name is long enough */
313 if (len > CEPH_NOHASH_NAME_MAX) {
314 u8 hash[SHA256_DIGEST_SIZE];
315 u8 *extra = cryptbuf + CEPH_NOHASH_NAME_MAX;
316
317 /*
318 * hash the extra bytes and overwrite crypttext beyond that
319 * point with it
320 */
321 sha256(extra, len - CEPH_NOHASH_NAME_MAX, hash);
322 memcpy(extra, hash, SHA256_DIGEST_SIZE);
323 len = CEPH_NOHASH_NAME_MAX + SHA256_DIGEST_SIZE;
324 }
325
326 /* base64 encode the encrypted name */
327 elen = ceph_base64_encode(cryptbuf, len, buf);
328 doutc(cl, "base64-encoded ciphertext name = %.*s\n", elen, buf);
329
330 /* To understand the 240 limit, see CEPH_NOHASH_NAME_MAX comments */
331 WARN_ON(elen > 240);
332 if ((elen > 0) && (dir != parent)) {
333 char tmp_buf[NAME_MAX];
334
335 elen = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
336 elen, buf, dir->i_ino);
337 memcpy(buf, tmp_buf, elen);
338 }
339
340 out:
341 kfree(cryptbuf);
342 if (dir != parent) {
343 if ((dir->i_state & I_NEW))
344 discard_new_inode(dir);
345 else
346 iput(dir);
347 }
348 return elen;
349 }
350
ceph_encode_encrypted_fname(struct inode * parent,struct dentry * dentry,char * buf)351 int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
352 char *buf)
353 {
354 WARN_ON_ONCE(!fscrypt_has_encryption_key(parent));
355
356 return ceph_encode_encrypted_dname(parent, &dentry->d_name, buf);
357 }
358
359 /**
360 * ceph_fname_to_usr - convert a filename for userland presentation
361 * @fname: ceph_fname to be converted
362 * @tname: temporary name buffer to use for conversion (may be NULL)
363 * @oname: where converted name should be placed
364 * @is_nokey: set to true if key wasn't available during conversion (may be NULL)
365 *
366 * Given a filename (usually from the MDS), format it for presentation to
367 * userland. If @parent is not encrypted, just pass it back as-is.
368 *
369 * Otherwise, base64 decode the string, and then ask fscrypt to format it
370 * for userland presentation.
371 *
372 * Returns 0 on success or negative error code on error.
373 */
ceph_fname_to_usr(const struct ceph_fname * fname,struct fscrypt_str * tname,struct fscrypt_str * oname,bool * is_nokey)374 int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
375 struct fscrypt_str *oname, bool *is_nokey)
376 {
377 struct inode *dir = fname->dir;
378 struct fscrypt_str _tname = FSTR_INIT(NULL, 0);
379 struct fscrypt_str iname;
380 char *name = fname->name;
381 int name_len = fname->name_len;
382 int ret;
383
384 /* Sanity check that the resulting name will fit in the buffer */
385 if (fname->name_len > NAME_MAX || fname->ctext_len > NAME_MAX)
386 return -EIO;
387
388 /* Handle the special case of snapshot names that start with '_' */
389 if ((ceph_snap(dir) == CEPH_SNAPDIR) && (name_len > 0) &&
390 (name[0] == '_')) {
391 dir = parse_longname(dir, name, &name_len);
392 if (IS_ERR(dir))
393 return PTR_ERR(dir);
394 name++; /* skip initial '_' */
395 }
396
397 if (!IS_ENCRYPTED(dir)) {
398 oname->name = fname->name;
399 oname->len = fname->name_len;
400 ret = 0;
401 goto out_inode;
402 }
403
404 ret = ceph_fscrypt_prepare_readdir(dir);
405 if (ret)
406 goto out_inode;
407
408 /*
409 * Use the raw dentry name as sent by the MDS instead of
410 * generating a nokey name via fscrypt.
411 */
412 if (!fscrypt_has_encryption_key(dir)) {
413 if (fname->no_copy)
414 oname->name = fname->name;
415 else
416 memcpy(oname->name, fname->name, fname->name_len);
417 oname->len = fname->name_len;
418 if (is_nokey)
419 *is_nokey = true;
420 ret = 0;
421 goto out_inode;
422 }
423
424 if (fname->ctext_len == 0) {
425 int declen;
426
427 if (!tname) {
428 ret = fscrypt_fname_alloc_buffer(NAME_MAX, &_tname);
429 if (ret)
430 goto out_inode;
431 tname = &_tname;
432 }
433
434 declen = ceph_base64_decode(name, name_len, tname->name);
435 if (declen <= 0) {
436 ret = -EIO;
437 goto out;
438 }
439 iname.name = tname->name;
440 iname.len = declen;
441 } else {
442 iname.name = fname->ctext;
443 iname.len = fname->ctext_len;
444 }
445
446 ret = fscrypt_fname_disk_to_usr(dir, 0, 0, &iname, oname);
447 if (!ret && (dir != fname->dir)) {
448 char tmp_buf[CEPH_BASE64_CHARS(NAME_MAX)];
449
450 name_len = snprintf(tmp_buf, sizeof(tmp_buf), "_%.*s_%ld",
451 oname->len, oname->name, dir->i_ino);
452 memcpy(oname->name, tmp_buf, name_len);
453 oname->len = name_len;
454 }
455
456 out:
457 fscrypt_fname_free_buffer(&_tname);
458 out_inode:
459 if (dir != fname->dir) {
460 if ((dir->i_state & I_NEW))
461 discard_new_inode(dir);
462 else
463 iput(dir);
464 }
465 return ret;
466 }
467
468 /**
469 * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper
470 * @dir: directory inode for readdir prep
471 *
472 * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as
473 * non-complete if this call results in having the directory unlocked.
474 *
475 * Returns:
476 * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked)
477 * 0 - if directory is still locked
478 * < 0 - if __fscrypt_prepare_readdir() fails
479 */
ceph_fscrypt_prepare_readdir(struct inode * dir)480 int ceph_fscrypt_prepare_readdir(struct inode *dir)
481 {
482 bool had_key = fscrypt_has_encryption_key(dir);
483 int err;
484
485 if (!IS_ENCRYPTED(dir))
486 return 0;
487
488 err = __fscrypt_prepare_readdir(dir);
489 if (err)
490 return err;
491 if (!had_key && fscrypt_has_encryption_key(dir)) {
492 /* directory just got unlocked, mark it as not complete */
493 ceph_dir_clear_complete(dir);
494 return 1;
495 }
496 return 0;
497 }
498
ceph_fscrypt_decrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num)499 int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
500 struct page *page, unsigned int len,
501 unsigned int offs, u64 lblk_num)
502 {
503 struct ceph_client *cl = ceph_inode_to_client(inode);
504
505 doutc(cl, "%p %llx.%llx len %u offs %u blk %llu\n", inode,
506 ceph_vinop(inode), len, offs, lblk_num);
507 return fscrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num);
508 }
509
ceph_fscrypt_encrypt_block_inplace(const struct inode * inode,struct page * page,unsigned int len,unsigned int offs,u64 lblk_num,gfp_t gfp_flags)510 int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
511 struct page *page, unsigned int len,
512 unsigned int offs, u64 lblk_num,
513 gfp_t gfp_flags)
514 {
515 struct ceph_client *cl = ceph_inode_to_client(inode);
516
517 doutc(cl, "%p %llx.%llx len %u offs %u blk %llu\n", inode,
518 ceph_vinop(inode), len, offs, lblk_num);
519 return fscrypt_encrypt_block_inplace(inode, page, len, offs, lblk_num,
520 gfp_flags);
521 }
522
523 /**
524 * ceph_fscrypt_decrypt_pages - decrypt an array of pages
525 * @inode: pointer to inode associated with these pages
526 * @page: pointer to page array
527 * @off: offset into the file that the read data starts
528 * @len: max length to decrypt
529 *
530 * Decrypt an array of fscrypt'ed pages and return the amount of
531 * data decrypted. Any data in the page prior to the start of the
532 * first complete block in the read is ignored. Any incomplete
533 * crypto blocks at the end of the array are ignored (and should
534 * probably be zeroed by the caller).
535 *
536 * Returns the length of the decrypted data or a negative errno.
537 */
ceph_fscrypt_decrypt_pages(struct inode * inode,struct page ** page,u64 off,int len)538 int ceph_fscrypt_decrypt_pages(struct inode *inode, struct page **page,
539 u64 off, int len)
540 {
541 int i, num_blocks;
542 u64 baseblk = off >> CEPH_FSCRYPT_BLOCK_SHIFT;
543 int ret = 0;
544
545 /*
546 * We can't deal with partial blocks on an encrypted file, so mask off
547 * the last bit.
548 */
549 num_blocks = ceph_fscrypt_blocks(off, len & CEPH_FSCRYPT_BLOCK_MASK);
550
551 /* Decrypt each block */
552 for (i = 0; i < num_blocks; ++i) {
553 int blkoff = i << CEPH_FSCRYPT_BLOCK_SHIFT;
554 int pgidx = blkoff >> PAGE_SHIFT;
555 unsigned int pgoffs = offset_in_page(blkoff);
556 int fret;
557
558 fret = ceph_fscrypt_decrypt_block_inplace(inode, page[pgidx],
559 CEPH_FSCRYPT_BLOCK_SIZE, pgoffs,
560 baseblk + i);
561 if (fret < 0) {
562 if (ret == 0)
563 ret = fret;
564 break;
565 }
566 ret += CEPH_FSCRYPT_BLOCK_SIZE;
567 }
568 return ret;
569 }
570
571 /**
572 * ceph_fscrypt_decrypt_extents: decrypt received extents in given buffer
573 * @inode: inode associated with pages being decrypted
574 * @page: pointer to page array
575 * @off: offset into the file that the data in page[0] starts
576 * @map: pointer to extent array
577 * @ext_cnt: length of extent array
578 *
579 * Given an extent map and a page array, decrypt the received data in-place,
580 * skipping holes. Returns the offset into buffer of end of last decrypted
581 * block.
582 */
ceph_fscrypt_decrypt_extents(struct inode * inode,struct page ** page,u64 off,struct ceph_sparse_extent * map,u32 ext_cnt)583 int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page,
584 u64 off, struct ceph_sparse_extent *map,
585 u32 ext_cnt)
586 {
587 struct ceph_client *cl = ceph_inode_to_client(inode);
588 int i, ret = 0;
589 struct ceph_inode_info *ci = ceph_inode(inode);
590 u64 objno, objoff;
591 u32 xlen;
592
593 /* Nothing to do for empty array */
594 if (ext_cnt == 0) {
595 doutc(cl, "%p %llx.%llx empty array, ret 0\n", inode,
596 ceph_vinop(inode));
597 return 0;
598 }
599
600 ceph_calc_file_object_mapping(&ci->i_layout, off, map[0].len,
601 &objno, &objoff, &xlen);
602
603 for (i = 0; i < ext_cnt; ++i) {
604 struct ceph_sparse_extent *ext = &map[i];
605 int pgsoff = ext->off - objoff;
606 int pgidx = pgsoff >> PAGE_SHIFT;
607 int fret;
608
609 if ((ext->off | ext->len) & ~CEPH_FSCRYPT_BLOCK_MASK) {
610 pr_warn_client(cl,
611 "%p %llx.%llx bad encrypted sparse extent "
612 "idx %d off %llx len %llx\n",
613 inode, ceph_vinop(inode), i, ext->off,
614 ext->len);
615 return -EIO;
616 }
617 fret = ceph_fscrypt_decrypt_pages(inode, &page[pgidx],
618 off + pgsoff, ext->len);
619 doutc(cl, "%p %llx.%llx [%d] 0x%llx~0x%llx fret %d\n", inode,
620 ceph_vinop(inode), i, ext->off, ext->len, fret);
621 if (fret < 0) {
622 if (ret == 0)
623 ret = fret;
624 break;
625 }
626 ret = pgsoff + fret;
627 }
628 doutc(cl, "ret %d\n", ret);
629 return ret;
630 }
631
632 /**
633 * ceph_fscrypt_encrypt_pages - encrypt an array of pages
634 * @inode: pointer to inode associated with these pages
635 * @page: pointer to page array
636 * @off: offset into the file that the data starts
637 * @len: max length to encrypt
638 * @gfp: gfp flags to use for allocation
639 *
640 * Decrypt an array of cleartext pages and return the amount of
641 * data encrypted. Any data in the page prior to the start of the
642 * first complete block in the read is ignored. Any incomplete
643 * crypto blocks at the end of the array are ignored.
644 *
645 * Returns the length of the encrypted data or a negative errno.
646 */
ceph_fscrypt_encrypt_pages(struct inode * inode,struct page ** page,u64 off,int len,gfp_t gfp)647 int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
648 int len, gfp_t gfp)
649 {
650 int i, num_blocks;
651 u64 baseblk = off >> CEPH_FSCRYPT_BLOCK_SHIFT;
652 int ret = 0;
653
654 /*
655 * We can't deal with partial blocks on an encrypted file, so mask off
656 * the last bit.
657 */
658 num_blocks = ceph_fscrypt_blocks(off, len & CEPH_FSCRYPT_BLOCK_MASK);
659
660 /* Encrypt each block */
661 for (i = 0; i < num_blocks; ++i) {
662 int blkoff = i << CEPH_FSCRYPT_BLOCK_SHIFT;
663 int pgidx = blkoff >> PAGE_SHIFT;
664 unsigned int pgoffs = offset_in_page(blkoff);
665 int fret;
666
667 fret = ceph_fscrypt_encrypt_block_inplace(inode, page[pgidx],
668 CEPH_FSCRYPT_BLOCK_SIZE, pgoffs,
669 baseblk + i, gfp);
670 if (fret < 0) {
671 if (ret == 0)
672 ret = fret;
673 break;
674 }
675 ret += CEPH_FSCRYPT_BLOCK_SIZE;
676 }
677 return ret;
678 }
679