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