Merge tag 'for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4

Pull ext4 fixes from Ted Ts'o:
"Some miscellaneous bug fixes and some final on-disk and ABI changes
for ext4 encryption which provide better security and performance"

* tag 'for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4:
ext4: fix growing of tiny filesystems
ext4: move check under lock scope to close a race.
ext4: fix data corruption caused by unwritten and delayed extents
ext4 crypto: remove duplicated encryption mode definitions
ext4 crypto: do not select from EXT4_FS_ENCRYPTION
ext4 crypto: add padding to filenames before encrypting
ext4 crypto: simplify and speed up filename encryption

+7 -2
fs/ext4/Kconfig
··· 64 64 If you are not using a security module that requires using 65 65 extended attributes for file security labels, say N. 66 66 67 - config EXT4_FS_ENCRYPTION 68 - bool "Ext4 Encryption" 67 + config EXT4_ENCRYPTION 68 + tristate "Ext4 Encryption" 69 69 depends on EXT4_FS 70 70 select CRYPTO_AES 71 71 select CRYPTO_CBC ··· 80 80 feature is similar to ecryptfs, but it is more memory 81 81 efficient since it avoids caching the encrypted and 82 82 decrypted pages in the page cache. 83 + 84 + config EXT4_FS_ENCRYPTION 85 + bool 86 + default y 87 + depends on EXT4_ENCRYPTION 83 88 84 89 config EXT4_DEBUG 85 90 bool "EXT4 debugging support"
+143 -133
fs/ext4/crypto_fname.c
··· 66 66 int res = 0; 67 67 char iv[EXT4_CRYPTO_BLOCK_SIZE]; 68 68 struct scatterlist sg[1]; 69 + int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK); 69 70 char *workbuf; 70 71 71 72 if (iname->len <= 0 || iname->len > ctx->lim) ··· 74 73 75 74 ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ? 76 75 EXT4_CRYPTO_BLOCK_SIZE : iname->len; 76 + ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding); 77 77 ciphertext_len = (ciphertext_len > ctx->lim) 78 78 ? ctx->lim : ciphertext_len; 79 79 ··· 103 101 /* Create encryption request */ 104 102 sg_init_table(sg, 1); 105 103 sg_set_page(sg, ctx->workpage, PAGE_SIZE, 0); 106 - ablkcipher_request_set_crypt(req, sg, sg, iname->len, iv); 104 + ablkcipher_request_set_crypt(req, sg, sg, ciphertext_len, iv); 107 105 res = crypto_ablkcipher_encrypt(req); 108 106 if (res == -EINPROGRESS || res == -EBUSY) { 109 107 BUG_ON(req->base.data != &ecr); ··· 200 198 return oname->len; 201 199 } 202 200 201 + static const char *lookup_table = 202 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; 203 + 203 204 /** 204 205 * ext4_fname_encode_digest() - 205 206 * 206 207 * Encodes the input digest using characters from the set [a-zA-Z0-9_+]. 207 208 * The encoded string is roughly 4/3 times the size of the input string. 208 209 */ 209 - int ext4_fname_encode_digest(char *dst, char *src, u32 len) 210 + static int digest_encode(const char *src, int len, char *dst) 210 211 { 211 - static const char *lookup_table = 212 - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+"; 213 - u32 current_chunk, num_chunks, i; 214 - char tmp_buf[3]; 215 - u32 c0, c1, c2, c3; 212 + int i = 0, bits = 0, ac = 0; 213 + char *cp = dst; 216 214 217 - current_chunk = 0; 218 - num_chunks = len/3; 219 - for (i = 0; i < num_chunks; i++) { 220 - c0 = src[3*i] & 0x3f; 221 - c1 = (((src[3*i]>>6)&0x3) | ((src[3*i+1] & 0xf)<<2)) & 0x3f; 222 - c2 = (((src[3*i+1]>>4)&0xf) | ((src[3*i+2] & 0x3)<<4)) & 0x3f; 223 - c3 = (src[3*i+2]>>2) & 0x3f; 224 - dst[4*i] = lookup_table[c0]; 225 - dst[4*i+1] = lookup_table[c1]; 226 - dst[4*i+2] = lookup_table[c2]; 227 - dst[4*i+3] = lookup_table[c3]; 228 - } 229 - if (i*3 < len) { 230 - memset(tmp_buf, 0, 3); 231 - memcpy(tmp_buf, &src[3*i], len-3*i); 232 - c0 = tmp_buf[0] & 0x3f; 233 - c1 = (((tmp_buf[0]>>6)&0x3) | ((tmp_buf[1] & 0xf)<<2)) & 0x3f; 234 - c2 = (((tmp_buf[1]>>4)&0xf) | ((tmp_buf[2] & 0x3)<<4)) & 0x3f; 235 - c3 = (tmp_buf[2]>>2) & 0x3f; 236 - dst[4*i] = lookup_table[c0]; 237 - dst[4*i+1] = lookup_table[c1]; 238 - dst[4*i+2] = lookup_table[c2]; 239 - dst[4*i+3] = lookup_table[c3]; 215 + while (i < len) { 216 + ac += (((unsigned char) src[i]) << bits); 217 + bits += 8; 218 + do { 219 + *cp++ = lookup_table[ac & 0x3f]; 220 + ac >>= 6; 221 + bits -= 6; 222 + } while (bits >= 6); 240 223 i++; 241 224 } 242 - return (i * 4); 225 + if (bits) 226 + *cp++ = lookup_table[ac & 0x3f]; 227 + return cp - dst; 243 228 } 244 229 245 - /** 246 - * ext4_fname_hash() - 247 - * 248 - * This function computes the hash of the input filename, and sets the output 249 - * buffer to the *encoded* digest. It returns the length of the digest as its 250 - * return value. Errors are returned as negative numbers. We trust the caller 251 - * to allocate sufficient memory to oname string. 252 - */ 253 - static int ext4_fname_hash(struct ext4_fname_crypto_ctx *ctx, 254 - const struct ext4_str *iname, 255 - struct ext4_str *oname) 230 + static int digest_decode(const char *src, int len, char *dst) 256 231 { 257 - struct scatterlist sg; 258 - struct hash_desc desc = { 259 - .tfm = (struct crypto_hash *)ctx->htfm, 260 - .flags = CRYPTO_TFM_REQ_MAY_SLEEP 261 - }; 262 - int res = 0; 232 + int i = 0, bits = 0, ac = 0; 233 + const char *p; 234 + char *cp = dst; 263 235 264 - if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) { 265 - res = ext4_fname_encode_digest(oname->name, iname->name, 266 - iname->len); 267 - oname->len = res; 268 - return res; 236 + while (i < len) { 237 + p = strchr(lookup_table, src[i]); 238 + if (p == NULL || src[i] == 0) 239 + return -2; 240 + ac += (p - lookup_table) << bits; 241 + bits += 6; 242 + if (bits >= 8) { 243 + *cp++ = ac & 0xff; 244 + ac >>= 8; 245 + bits -= 8; 246 + } 247 + i++; 269 248 } 270 - 271 - sg_init_one(&sg, iname->name, iname->len); 272 - res = crypto_hash_init(&desc); 273 - if (res) { 274 - printk(KERN_ERR 275 - "%s: Error initializing crypto hash; res = [%d]\n", 276 - __func__, res); 277 - goto out; 278 - } 279 - res = crypto_hash_update(&desc, &sg, iname->len); 280 - if (res) { 281 - printk(KERN_ERR 282 - "%s: Error updating crypto hash; res = [%d]\n", 283 - __func__, res); 284 - goto out; 285 - } 286 - res = crypto_hash_final(&desc, 287 - &oname->name[EXT4_FNAME_CRYPTO_DIGEST_SIZE]); 288 - if (res) { 289 - printk(KERN_ERR 290 - "%s: Error finalizing crypto hash; res = [%d]\n", 291 - __func__, res); 292 - goto out; 293 - } 294 - /* Encode the digest as a printable string--this will increase the 295 - * size of the digest */ 296 - oname->name[0] = 'I'; 297 - res = ext4_fname_encode_digest(oname->name+1, 298 - &oname->name[EXT4_FNAME_CRYPTO_DIGEST_SIZE], 299 - EXT4_FNAME_CRYPTO_DIGEST_SIZE) + 1; 300 - oname->len = res; 301 - out: 302 - return res; 249 + if (ac) 250 + return -1; 251 + return cp - dst; 303 252 } 304 253 305 254 /** ··· 358 405 if (IS_ERR(ctx)) 359 406 return ctx; 360 407 408 + ctx->flags = ei->i_crypt_policy_flags; 361 409 if (ctx->has_valid_key) { 362 410 if (ctx->key.mode != EXT4_ENCRYPTION_MODE_AES_256_CTS) { 363 411 printk_once(KERN_WARNING ··· 471 517 u32 namelen) 472 518 { 473 519 u32 ciphertext_len; 520 + int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK); 474 521 475 522 if (ctx == NULL) 476 523 return -EIO; ··· 479 524 return -EACCES; 480 525 ciphertext_len = (namelen < EXT4_CRYPTO_BLOCK_SIZE) ? 481 526 EXT4_CRYPTO_BLOCK_SIZE : namelen; 527 + ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding); 482 528 ciphertext_len = (ciphertext_len > ctx->lim) 483 529 ? ctx->lim : ciphertext_len; 484 530 return (int) ciphertext_len; ··· 495 539 u32 ilen, struct ext4_str *crypto_str) 496 540 { 497 541 unsigned int olen; 542 + int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK); 498 543 499 544 if (!ctx) 500 545 return -EIO; 501 - olen = ext4_fname_crypto_round_up(ilen, EXT4_CRYPTO_BLOCK_SIZE); 546 + if (padding < EXT4_CRYPTO_BLOCK_SIZE) 547 + padding = EXT4_CRYPTO_BLOCK_SIZE; 548 + olen = ext4_fname_crypto_round_up(ilen, padding); 502 549 crypto_str->len = olen; 503 550 if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) 504 551 olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2; ··· 530 571 * ext4_fname_disk_to_usr() - converts a filename from disk space to user space 531 572 */ 532 573 int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx, 533 - const struct ext4_str *iname, 534 - struct ext4_str *oname) 574 + struct dx_hash_info *hinfo, 575 + const struct ext4_str *iname, 576 + struct ext4_str *oname) 535 577 { 578 + char buf[24]; 579 + int ret; 580 + 536 581 if (ctx == NULL) 537 582 return -EIO; 538 583 if (iname->len < 3) { ··· 550 587 } 551 588 if (ctx->has_valid_key) 552 589 return ext4_fname_decrypt(ctx, iname, oname); 553 - else 554 - return ext4_fname_hash(ctx, iname, oname); 590 + 591 + if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) { 592 + ret = digest_encode(iname->name, iname->len, oname->name); 593 + oname->len = ret; 594 + return ret; 595 + } 596 + if (hinfo) { 597 + memcpy(buf, &hinfo->hash, 4); 598 + memcpy(buf+4, &hinfo->minor_hash, 4); 599 + } else 600 + memset(buf, 0, 8); 601 + memcpy(buf + 8, iname->name + iname->len - 16, 16); 602 + oname->name[0] = '_'; 603 + ret = digest_encode(buf, 24, oname->name+1); 604 + oname->len = ret + 1; 605 + return ret + 1; 555 606 } 556 607 557 608 int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx, 609 + struct dx_hash_info *hinfo, 558 610 const struct ext4_dir_entry_2 *de, 559 611 struct ext4_str *oname) 560 612 { 561 613 struct ext4_str iname = {.name = (unsigned char *) de->name, 562 614 .len = de->name_len }; 563 615 564 - return _ext4_fname_disk_to_usr(ctx, &iname, oname); 616 + return _ext4_fname_disk_to_usr(ctx, hinfo, &iname, oname); 565 617 } 566 618 567 619 ··· 618 640 const struct qstr *iname, 619 641 struct dx_hash_info *hinfo) 620 642 { 621 - struct ext4_str tmp, tmp2; 643 + struct ext4_str tmp; 622 644 int ret = 0; 645 + char buf[EXT4_FNAME_CRYPTO_DIGEST_SIZE+1]; 623 646 624 - if (!ctx || !ctx->has_valid_key || 647 + if (!ctx || 625 648 ((iname->name[0] == '.') && 626 649 ((iname->len == 1) || 627 650 ((iname->name[1] == '.') && (iname->len == 2))))) { 628 651 ext4fs_dirhash(iname->name, iname->len, hinfo); 652 + return 0; 653 + } 654 + 655 + if (!ctx->has_valid_key && iname->name[0] == '_') { 656 + if (iname->len != 33) 657 + return -ENOENT; 658 + ret = digest_decode(iname->name+1, iname->len, buf); 659 + if (ret != 24) 660 + return -ENOENT; 661 + memcpy(&hinfo->hash, buf, 4); 662 + memcpy(&hinfo->minor_hash, buf + 4, 4); 663 + return 0; 664 + } 665 + 666 + if (!ctx->has_valid_key && iname->name[0] != '_') { 667 + if (iname->len > 43) 668 + return -ENOENT; 669 + ret = digest_decode(iname->name, iname->len, buf); 670 + ext4fs_dirhash(buf, ret, hinfo); 629 671 return 0; 630 672 } 631 673 ··· 655 657 return ret; 656 658 657 659 ret = ext4_fname_encrypt(ctx, iname, &tmp); 658 - if (ret < 0) 659 - goto out; 660 - 661 - tmp2.len = (4 * ((EXT4_FNAME_CRYPTO_DIGEST_SIZE + 2) / 3)) + 1; 662 - tmp2.name = kmalloc(tmp2.len + 1, GFP_KERNEL); 663 - if (tmp2.name == NULL) { 664 - ret = -ENOMEM; 665 - goto out; 660 + if (ret >= 0) { 661 + ext4fs_dirhash(tmp.name, tmp.len, hinfo); 662 + ret = 0; 666 663 } 667 664 668 - ret = ext4_fname_hash(ctx, &tmp, &tmp2); 669 - if (ret > 0) 670 - ext4fs_dirhash(tmp2.name, tmp2.len, hinfo); 671 - ext4_fname_crypto_free_buffer(&tmp2); 672 - out: 673 665 ext4_fname_crypto_free_buffer(&tmp); 674 666 return ret; 675 667 } 676 668 677 - /** 678 - * ext4_fname_disk_to_htree() - converts a filename from disk space to htree-access string 679 - */ 680 - int ext4_fname_disk_to_hash(struct ext4_fname_crypto_ctx *ctx, 681 - const struct ext4_dir_entry_2 *de, 682 - struct dx_hash_info *hinfo) 669 + int ext4_fname_match(struct ext4_fname_crypto_ctx *ctx, struct ext4_str *cstr, 670 + int len, const char * const name, 671 + struct ext4_dir_entry_2 *de) 683 672 { 684 - struct ext4_str iname = {.name = (unsigned char *) de->name, 685 - .len = de->name_len}; 686 - struct ext4_str tmp; 687 - int ret; 673 + int ret = -ENOENT; 674 + int bigname = (*name == '_'); 688 675 689 - if (!ctx || 690 - ((iname.name[0] == '.') && 691 - ((iname.len == 1) || 692 - ((iname.name[1] == '.') && (iname.len == 2))))) { 693 - ext4fs_dirhash(iname.name, iname.len, hinfo); 694 - return 0; 676 + if (ctx->has_valid_key) { 677 + if (cstr->name == NULL) { 678 + struct qstr istr; 679 + 680 + ret = ext4_fname_crypto_alloc_buffer(ctx, len, cstr); 681 + if (ret < 0) 682 + goto errout; 683 + istr.name = name; 684 + istr.len = len; 685 + ret = ext4_fname_encrypt(ctx, &istr, cstr); 686 + if (ret < 0) 687 + goto errout; 688 + } 689 + } else { 690 + if (cstr->name == NULL) { 691 + cstr->name = kmalloc(32, GFP_KERNEL); 692 + if (cstr->name == NULL) 693 + return -ENOMEM; 694 + if ((bigname && (len != 33)) || 695 + (!bigname && (len > 43))) 696 + goto errout; 697 + ret = digest_decode(name+bigname, len-bigname, 698 + cstr->name); 699 + if (ret < 0) { 700 + ret = -ENOENT; 701 + goto errout; 702 + } 703 + cstr->len = ret; 704 + } 705 + if (bigname) { 706 + if (de->name_len < 16) 707 + return 0; 708 + ret = memcmp(de->name + de->name_len - 16, 709 + cstr->name + 8, 16); 710 + return (ret == 0) ? 1 : 0; 711 + } 695 712 } 696 - 697 - tmp.len = (4 * ((EXT4_FNAME_CRYPTO_DIGEST_SIZE + 2) / 3)) + 1; 698 - tmp.name = kmalloc(tmp.len + 1, GFP_KERNEL); 699 - if (tmp.name == NULL) 700 - return -ENOMEM; 701 - 702 - ret = ext4_fname_hash(ctx, &iname, &tmp); 703 - if (ret > 0) 704 - ext4fs_dirhash(tmp.name, tmp.len, hinfo); 705 - ext4_fname_crypto_free_buffer(&tmp); 713 + if (de->name_len != cstr->len) 714 + return 0; 715 + ret = memcmp(de->name, cstr->name, cstr->len); 716 + return (ret == 0) ? 1 : 0; 717 + errout: 718 + kfree(cstr->name); 719 + cstr->name = NULL; 706 720 return ret; 707 721 }
+1
fs/ext4/crypto_key.c
··· 110 110 } 111 111 res = 0; 112 112 113 + ei->i_crypt_policy_flags = ctx.flags; 113 114 if (S_ISREG(inode->i_mode)) 114 115 crypt_key->mode = ctx.contents_encryption_mode; 115 116 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
+9 -5
fs/ext4/crypto_policy.c
··· 37 37 return 0; 38 38 return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor, 39 39 EXT4_KEY_DESCRIPTOR_SIZE) == 0 && 40 + (ctx.flags == 41 + policy->flags) && 40 42 (ctx.contents_encryption_mode == 41 43 policy->contents_encryption_mode) && 42 44 (ctx.filenames_encryption_mode == ··· 58 56 printk(KERN_WARNING 59 57 "%s: Invalid contents encryption mode %d\n", __func__, 60 58 policy->contents_encryption_mode); 61 - res = -EINVAL; 62 - goto out; 59 + return -EINVAL; 63 60 } 64 61 if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) { 65 62 printk(KERN_WARNING 66 63 "%s: Invalid filenames encryption mode %d\n", __func__, 67 64 policy->filenames_encryption_mode); 68 - res = -EINVAL; 69 - goto out; 65 + return -EINVAL; 70 66 } 67 + if (policy->flags & ~EXT4_POLICY_FLAGS_VALID) 68 + return -EINVAL; 71 69 ctx.contents_encryption_mode = policy->contents_encryption_mode; 72 70 ctx.filenames_encryption_mode = policy->filenames_encryption_mode; 71 + ctx.flags = policy->flags; 73 72 BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE); 74 73 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE); 75 74 76 75 res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION, 77 76 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx, 78 77 sizeof(ctx), 0); 79 - out: 80 78 if (!res) 81 79 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); 82 80 return res; ··· 117 115 policy->version = 0; 118 116 policy->contents_encryption_mode = ctx.contents_encryption_mode; 119 117 policy->filenames_encryption_mode = ctx.filenames_encryption_mode; 118 + policy->flags = ctx.flags; 120 119 memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor, 121 120 EXT4_KEY_DESCRIPTOR_SIZE); 122 121 return 0; ··· 179 176 EXT4_ENCRYPTION_MODE_AES_256_XTS; 180 177 ctx.filenames_encryption_mode = 181 178 EXT4_ENCRYPTION_MODE_AES_256_CTS; 179 + ctx.flags = 0; 182 180 memset(ctx.master_key_descriptor, 0x42, 183 181 EXT4_KEY_DESCRIPTOR_SIZE); 184 182 res = 0;
+1 -1
fs/ext4/dir.c
··· 249 249 } else { 250 250 /* Directory is encrypted */ 251 251 err = ext4_fname_disk_to_usr(enc_ctx, 252 - de, &fname_crypto_str); 252 + NULL, de, &fname_crypto_str); 253 253 if (err < 0) 254 254 goto errout; 255 255 if (!dir_emit(ctx,
+7 -9
fs/ext4/ext4.h
··· 911 911 912 912 /* on-disk additional length */ 913 913 __u16 i_extra_isize; 914 + char i_crypt_policy_flags; 914 915 915 916 /* Indicate the inline data space. */ 916 917 u16 i_inline_off; ··· 1066 1065 1067 1066 /* Metadata checksum algorithm codes */ 1068 1067 #define EXT4_CRC32C_CHKSUM 1 1069 - 1070 - /* Encryption algorithms */ 1071 - #define EXT4_ENCRYPTION_MODE_INVALID 0 1072 - #define EXT4_ENCRYPTION_MODE_AES_256_XTS 1 1073 - #define EXT4_ENCRYPTION_MODE_AES_256_GCM 2 1074 - #define EXT4_ENCRYPTION_MODE_AES_256_CBC 3 1075 1068 1076 1069 /* 1077 1070 * Structure of the super block ··· 2088 2093 int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx *ctx, 2089 2094 u32 ilen, struct ext4_str *crypto_str); 2090 2095 int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx, 2096 + struct dx_hash_info *hinfo, 2091 2097 const struct ext4_str *iname, 2092 2098 struct ext4_str *oname); 2093 2099 int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx, 2100 + struct dx_hash_info *hinfo, 2094 2101 const struct ext4_dir_entry_2 *de, 2095 2102 struct ext4_str *oname); 2096 2103 int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx, ··· 2101 2104 int ext4_fname_usr_to_hash(struct ext4_fname_crypto_ctx *ctx, 2102 2105 const struct qstr *iname, 2103 2106 struct dx_hash_info *hinfo); 2104 - int ext4_fname_disk_to_hash(struct ext4_fname_crypto_ctx *ctx, 2105 - const struct ext4_dir_entry_2 *de, 2106 - struct dx_hash_info *hinfo); 2107 2107 int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx *ctx, 2108 2108 u32 namelen); 2109 + int ext4_fname_match(struct ext4_fname_crypto_ctx *ctx, struct ext4_str *cstr, 2110 + int len, const char * const name, 2111 + struct ext4_dir_entry_2 *de); 2112 + 2109 2113 2110 2114 #ifdef CONFIG_EXT4_FS_ENCRYPTION 2111 2115 void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx);
+10 -1
fs/ext4/ext4_crypto.h
··· 20 20 char version; 21 21 char contents_encryption_mode; 22 22 char filenames_encryption_mode; 23 + char flags; 23 24 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE]; 24 25 } __attribute__((__packed__)); 25 26 26 27 #define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1 27 28 #define EXT4_KEY_DERIVATION_NONCE_SIZE 16 29 + 30 + #define EXT4_POLICY_FLAGS_PAD_4 0x00 31 + #define EXT4_POLICY_FLAGS_PAD_8 0x01 32 + #define EXT4_POLICY_FLAGS_PAD_16 0x02 33 + #define EXT4_POLICY_FLAGS_PAD_32 0x03 34 + #define EXT4_POLICY_FLAGS_PAD_MASK 0x03 35 + #define EXT4_POLICY_FLAGS_VALID 0x03 28 36 29 37 /** 30 38 * Encryption context for inode ··· 49 41 char format; 50 42 char contents_encryption_mode; 51 43 char filenames_encryption_mode; 52 - char reserved; 44 + char flags; 53 45 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE]; 54 46 char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE]; 55 47 } __attribute__((__packed__)); ··· 128 120 struct crypto_hash *htfm; 129 121 struct page *workpage; 130 122 struct ext4_encryption_key key; 123 + unsigned flags : 8; 131 124 unsigned has_valid_key : 1; 132 125 unsigned ctfm_key_is_ready : 1; 133 126 };
+8 -7
fs/ext4/extents.c
··· 4927 4927 if (ret) 4928 4928 return ret; 4929 4929 4930 - /* 4931 - * currently supporting (pre)allocate mode for extent-based 4932 - * files _only_ 4933 - */ 4934 - if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 4935 - return -EOPNOTSUPP; 4936 - 4937 4930 if (mode & FALLOC_FL_COLLAPSE_RANGE) 4938 4931 return ext4_collapse_range(inode, offset, len); 4939 4932 ··· 4947 4954 flags |= EXT4_GET_BLOCKS_KEEP_SIZE; 4948 4955 4949 4956 mutex_lock(&inode->i_mutex); 4957 + 4958 + /* 4959 + * We only support preallocation for extent-based files only 4960 + */ 4961 + if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4962 + ret = -EOPNOTSUPP; 4963 + goto out; 4964 + } 4950 4965 4951 4966 if (!(mode & FALLOC_FL_KEEP_SIZE) && 4952 4967 offset + len > i_size_read(inode)) {
+8
fs/ext4/extents_status.c
··· 703 703 704 704 BUG_ON(end < lblk); 705 705 706 + if ((status & EXTENT_STATUS_DELAYED) && 707 + (status & EXTENT_STATUS_WRITTEN)) { 708 + ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as " 709 + " delayed and written which can potentially " 710 + " cause data loss.\n", lblk, len); 711 + WARN_ON(1); 712 + } 713 + 706 714 newes.es_lblk = lblk; 707 715 newes.es_len = len; 708 716 ext4_es_store_pblock_status(&newes, pblk, status);
+2
fs/ext4/inode.c
··· 531 531 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 532 532 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 533 533 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 534 + !(status & EXTENT_STATUS_WRITTEN) && 534 535 ext4_find_delalloc_range(inode, map->m_lblk, 535 536 map->m_lblk + map->m_len - 1)) 536 537 status |= EXTENT_STATUS_DELAYED; ··· 636 635 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 637 636 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 638 637 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 638 + !(status & EXTENT_STATUS_WRITTEN) && 639 639 ext4_find_delalloc_range(inode, map->m_lblk, 640 640 map->m_lblk + map->m_len - 1)) 641 641 status |= EXTENT_STATUS_DELAYED;
+6 -66
fs/ext4/namei.c
··· 640 640 ext4_put_fname_crypto_ctx(&ctx); 641 641 ctx = NULL; 642 642 } 643 - res = ext4_fname_disk_to_usr(ctx, de, 643 + res = ext4_fname_disk_to_usr(ctx, NULL, de, 644 644 &fname_crypto_str); 645 645 if (res < 0) { 646 646 printk(KERN_WARNING "Error " ··· 653 653 name = fname_crypto_str.name; 654 654 len = fname_crypto_str.len; 655 655 } 656 - res = ext4_fname_disk_to_hash(ctx, de, 657 - &h); 658 - if (res < 0) { 659 - printk(KERN_WARNING "Error " 660 - "converting filename " 661 - "from disk to htree" 662 - "\n"); 663 - h.hash = 0xDEADBEEF; 664 - } 656 + ext4fs_dirhash(de->name, de->name_len, 657 + &h); 665 658 printk("%*.s:(E)%x.%u ", len, name, 666 659 h.hash, (unsigned) ((char *) de 667 660 - base)); ··· 1001 1008 /* silently ignore the rest of the block */ 1002 1009 break; 1003 1010 } 1004 - #ifdef CONFIG_EXT4_FS_ENCRYPTION 1005 - err = ext4_fname_disk_to_hash(ctx, de, hinfo); 1006 - if (err < 0) { 1007 - count = err; 1008 - goto errout; 1009 - } 1010 - #else 1011 1011 ext4fs_dirhash(de->name, de->name_len, hinfo); 1012 - #endif 1013 1012 if ((hinfo->hash < start_hash) || 1014 1013 ((hinfo->hash == start_hash) && 1015 1014 (hinfo->minor_hash < start_minor_hash))) ··· 1017 1032 &tmp_str); 1018 1033 } else { 1019 1034 /* Directory is encrypted */ 1020 - err = ext4_fname_disk_to_usr(ctx, de, 1035 + err = ext4_fname_disk_to_usr(ctx, hinfo, de, 1021 1036 &fname_crypto_str); 1022 1037 if (err < 0) { 1023 1038 count = err; ··· 1178 1193 int count = 0; 1179 1194 char *base = (char *) de; 1180 1195 struct dx_hash_info h = *hinfo; 1181 - #ifdef CONFIG_EXT4_FS_ENCRYPTION 1182 - struct ext4_fname_crypto_ctx *ctx = NULL; 1183 - int err; 1184 - 1185 - ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN); 1186 - if (IS_ERR(ctx)) 1187 - return PTR_ERR(ctx); 1188 - #endif 1189 1196 1190 1197 while ((char *) de < base + blocksize) { 1191 1198 if (de->name_len && de->inode) { 1192 - #ifdef CONFIG_EXT4_FS_ENCRYPTION 1193 - err = ext4_fname_disk_to_hash(ctx, de, &h); 1194 - if (err < 0) { 1195 - ext4_put_fname_crypto_ctx(&ctx); 1196 - return err; 1197 - } 1198 - #else 1199 1199 ext4fs_dirhash(de->name, de->name_len, &h); 1200 - #endif 1201 1200 map_tail--; 1202 1201 map_tail->hash = h.hash; 1203 1202 map_tail->offs = ((char *) de - base)>>2; ··· 1192 1223 /* XXX: do we need to check rec_len == 0 case? -Chris */ 1193 1224 de = ext4_next_entry(de, blocksize); 1194 1225 } 1195 - #ifdef CONFIG_EXT4_FS_ENCRYPTION 1196 - ext4_put_fname_crypto_ctx(&ctx); 1197 - #endif 1198 1226 return count; 1199 1227 } 1200 1228 ··· 1253 1287 return 0; 1254 1288 1255 1289 #ifdef CONFIG_EXT4_FS_ENCRYPTION 1256 - if (ctx) { 1257 - /* Directory is encrypted */ 1258 - res = ext4_fname_disk_to_usr(ctx, de, fname_crypto_str); 1259 - if (res < 0) 1260 - return res; 1261 - if (len != res) 1262 - return 0; 1263 - res = memcmp(name, fname_crypto_str->name, len); 1264 - return (res == 0) ? 1 : 0; 1265 - } 1290 + if (ctx) 1291 + return ext4_fname_match(ctx, fname_crypto_str, len, name, de); 1266 1292 #endif 1267 1293 if (len != de->name_len) 1268 1294 return 0; ··· 1281 1323 ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN); 1282 1324 if (IS_ERR(ctx)) 1283 1325 return -1; 1284 - 1285 - if (ctx != NULL) { 1286 - /* Allocate buffer to hold maximum name length */ 1287 - res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN, 1288 - &fname_crypto_str); 1289 - if (res < 0) { 1290 - ext4_put_fname_crypto_ctx(&ctx); 1291 - return -1; 1292 - } 1293 - } 1294 1326 1295 1327 de = (struct ext4_dir_entry_2 *)search_buf; 1296 1328 dlimit = search_buf + buf_size; ··· 1820 1872 return res; 1821 1873 } 1822 1874 reclen = EXT4_DIR_REC_LEN(res); 1823 - 1824 - /* Allocate buffer to hold maximum name length */ 1825 - res = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN, 1826 - &fname_crypto_str); 1827 - if (res < 0) { 1828 - ext4_put_fname_crypto_ctx(&ctx); 1829 - return -1; 1830 - } 1831 1875 } 1832 1876 1833 1877 de = (struct ext4_dir_entry_2 *)buf;
+5 -2
fs/ext4/resize.c
··· 1432 1432 goto exit; 1433 1433 /* 1434 1434 * We will always be modifying at least the superblock and GDT 1435 - * block. If we are adding a group past the last current GDT block, 1435 + * blocks. If we are adding a group past the last current GDT block, 1436 1436 * we will also modify the inode and the dindirect block. If we 1437 1437 * are adding a group with superblock/GDT backups we will also 1438 1438 * modify each of the reserved GDT dindirect blocks. 1439 1439 */ 1440 - credit = flex_gd->count * 4 + reserved_gdb; 1440 + credit = 3; /* sb, resize inode, resize inode dindirect */ 1441 + /* GDT blocks */ 1442 + credit += 1 + DIV_ROUND_UP(flex_gd->count, EXT4_DESC_PER_BLOCK(sb)); 1443 + credit += reserved_gdb; /* Reserved GDT dindirect blocks */ 1441 1444 handle = ext4_journal_start_sb(sb, EXT4_HT_RESIZE, credit); 1442 1445 if (IS_ERR(handle)) { 1443 1446 err = PTR_ERR(handle);
+1 -1
fs/ext4/symlink.c
··· 74 74 goto errout; 75 75 } 76 76 pstr.name = paddr; 77 - res = _ext4_fname_disk_to_usr(ctx, &cstr, &pstr); 77 + res = _ext4_fname_disk_to_usr(ctx, NULL, &cstr, &pstr); 78 78 if (res < 0) 79 79 goto errout; 80 80 /* Null-terminate the name */