1 /* 2 * Copyright (c) 2012 Taobao. 3 * Written by Tao Ma <boyu.mt@taobao.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2.1 of the GNU Lesser General Public License 7 * as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/iomap.h> 16 #include <linux/fiemap.h> 17 #include <linux/iversion.h> 18 19 #include "ext4_jbd2.h" 20 #include "ext4.h" 21 #include "xattr.h" 22 #include "truncate.h" 23 24 #define EXT4_XATTR_SYSTEM_DATA "data" 25 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS)) 26 #define EXT4_INLINE_DOTDOT_OFFSET 2 27 #define EXT4_INLINE_DOTDOT_SIZE 4 28 29 static int ext4_get_inline_size(struct inode *inode) 30 { 31 if (EXT4_I(inode)->i_inline_off) 32 return EXT4_I(inode)->i_inline_size; 33 34 return 0; 35 } 36 37 static int get_max_inline_xattr_value_size(struct inode *inode, 38 struct ext4_iloc *iloc) 39 { 40 struct ext4_xattr_ibody_header *header; 41 struct ext4_xattr_entry *entry; 42 struct ext4_inode *raw_inode; 43 int free, min_offs; 44 45 min_offs = EXT4_SB(inode->i_sb)->s_inode_size - 46 EXT4_GOOD_OLD_INODE_SIZE - 47 EXT4_I(inode)->i_extra_isize - 48 sizeof(struct ext4_xattr_ibody_header); 49 50 /* 51 * We need to subtract another sizeof(__u32) since an in-inode xattr 52 * needs an empty 4 bytes to indicate the gap between the xattr entry 53 * and the name/value pair. 54 */ 55 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) 56 return EXT4_XATTR_SIZE(min_offs - 57 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) - 58 EXT4_XATTR_ROUND - sizeof(__u32)); 59 60 raw_inode = ext4_raw_inode(iloc); 61 header = IHDR(inode, raw_inode); 62 entry = IFIRST(header); 63 64 /* Compute min_offs. */ 65 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { 66 if (!entry->e_value_inum && entry->e_value_size) { 67 size_t offs = le16_to_cpu(entry->e_value_offs); 68 if (offs < min_offs) 69 min_offs = offs; 70 } 71 } 72 free = min_offs - 73 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32); 74 75 if (EXT4_I(inode)->i_inline_off) { 76 entry = (struct ext4_xattr_entry *) 77 ((void *)raw_inode + EXT4_I(inode)->i_inline_off); 78 79 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)); 80 goto out; 81 } 82 83 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)); 84 85 if (free > EXT4_XATTR_ROUND) 86 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND); 87 else 88 free = 0; 89 90 out: 91 return free; 92 } 93 94 /* 95 * Get the maximum size we now can store in an inode. 96 * If we can't find the space for a xattr entry, don't use the space 97 * of the extents since we have no space to indicate the inline data. 98 */ 99 int ext4_get_max_inline_size(struct inode *inode) 100 { 101 int error, max_inline_size; 102 struct ext4_iloc iloc; 103 104 if (EXT4_I(inode)->i_extra_isize == 0) 105 return 0; 106 107 error = ext4_get_inode_loc(inode, &iloc); 108 if (error) { 109 ext4_error_inode(inode, __func__, __LINE__, 0, 110 "can't get inode location %lu", 111 inode->i_ino); 112 return 0; 113 } 114 115 down_read(&EXT4_I(inode)->xattr_sem); 116 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc); 117 up_read(&EXT4_I(inode)->xattr_sem); 118 119 brelse(iloc.bh); 120 121 if (!max_inline_size) 122 return 0; 123 124 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE; 125 } 126 127 /* 128 * this function does not take xattr_sem, which is OK because it is 129 * currently only used in a code path coming form ext4_iget, before 130 * the new inode has been unlocked 131 */ 132 int ext4_find_inline_data_nolock(struct inode *inode) 133 { 134 struct ext4_xattr_ibody_find is = { 135 .s = { .not_found = -ENODATA, }, 136 }; 137 struct ext4_xattr_info i = { 138 .name_index = EXT4_XATTR_INDEX_SYSTEM, 139 .name = EXT4_XATTR_SYSTEM_DATA, 140 }; 141 int error; 142 143 if (EXT4_I(inode)->i_extra_isize == 0) 144 return 0; 145 146 error = ext4_get_inode_loc(inode, &is.iloc); 147 if (error) 148 return error; 149 150 error = ext4_xattr_ibody_find(inode, &i, &is); 151 if (error) 152 goto out; 153 154 if (!is.s.not_found) { 155 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - 156 (void *)ext4_raw_inode(&is.iloc)); 157 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + 158 le32_to_cpu(is.s.here->e_value_size); 159 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 160 } 161 out: 162 brelse(is.iloc.bh); 163 return error; 164 } 165 166 static int ext4_read_inline_data(struct inode *inode, void *buffer, 167 unsigned int len, 168 struct ext4_iloc *iloc) 169 { 170 struct ext4_xattr_entry *entry; 171 struct ext4_xattr_ibody_header *header; 172 int cp_len = 0; 173 struct ext4_inode *raw_inode; 174 175 if (!len) 176 return 0; 177 178 BUG_ON(len > EXT4_I(inode)->i_inline_size); 179 180 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ? 181 len : EXT4_MIN_INLINE_DATA_SIZE; 182 183 raw_inode = ext4_raw_inode(iloc); 184 memcpy(buffer, (void *)(raw_inode->i_block), cp_len); 185 186 len -= cp_len; 187 buffer += cp_len; 188 189 if (!len) 190 goto out; 191 192 header = IHDR(inode, raw_inode); 193 entry = (struct ext4_xattr_entry *)((void *)raw_inode + 194 EXT4_I(inode)->i_inline_off); 195 len = min_t(unsigned int, len, 196 (unsigned int)le32_to_cpu(entry->e_value_size)); 197 198 memcpy(buffer, 199 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len); 200 cp_len += len; 201 202 out: 203 return cp_len; 204 } 205 206 /* 207 * write the buffer to the inline inode. 208 * If 'create' is set, we don't need to do the extra copy in the xattr 209 * value since it is already handled by ext4_xattr_ibody_inline_set. 210 * That saves us one memcpy. 211 */ 212 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc, 213 void *buffer, loff_t pos, unsigned int len) 214 { 215 struct ext4_xattr_entry *entry; 216 struct ext4_xattr_ibody_header *header; 217 struct ext4_inode *raw_inode; 218 int cp_len = 0; 219 220 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 221 return; 222 223 BUG_ON(!EXT4_I(inode)->i_inline_off); 224 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size); 225 226 raw_inode = ext4_raw_inode(iloc); 227 buffer += pos; 228 229 if (pos < EXT4_MIN_INLINE_DATA_SIZE) { 230 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ? 231 EXT4_MIN_INLINE_DATA_SIZE - pos : len; 232 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len); 233 234 len -= cp_len; 235 buffer += cp_len; 236 pos += cp_len; 237 } 238 239 if (!len) 240 return; 241 242 pos -= EXT4_MIN_INLINE_DATA_SIZE; 243 header = IHDR(inode, raw_inode); 244 entry = (struct ext4_xattr_entry *)((void *)raw_inode + 245 EXT4_I(inode)->i_inline_off); 246 247 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos, 248 buffer, len); 249 } 250 251 static int ext4_create_inline_data(handle_t *handle, 252 struct inode *inode, unsigned len) 253 { 254 int error; 255 void *value = NULL; 256 struct ext4_xattr_ibody_find is = { 257 .s = { .not_found = -ENODATA, }, 258 }; 259 struct ext4_xattr_info i = { 260 .name_index = EXT4_XATTR_INDEX_SYSTEM, 261 .name = EXT4_XATTR_SYSTEM_DATA, 262 }; 263 264 error = ext4_get_inode_loc(inode, &is.iloc); 265 if (error) 266 return error; 267 268 BUFFER_TRACE(is.iloc.bh, "get_write_access"); 269 error = ext4_journal_get_write_access(handle, is.iloc.bh); 270 if (error) 271 goto out; 272 273 if (len > EXT4_MIN_INLINE_DATA_SIZE) { 274 value = EXT4_ZERO_XATTR_VALUE; 275 len -= EXT4_MIN_INLINE_DATA_SIZE; 276 } else { 277 value = ""; 278 len = 0; 279 } 280 281 /* Insert the the xttr entry. */ 282 i.value = value; 283 i.value_len = len; 284 285 error = ext4_xattr_ibody_find(inode, &i, &is); 286 if (error) 287 goto out; 288 289 BUG_ON(!is.s.not_found); 290 291 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is); 292 if (error) { 293 if (error == -ENOSPC) 294 ext4_clear_inode_state(inode, 295 EXT4_STATE_MAY_INLINE_DATA); 296 goto out; 297 } 298 299 memset((void *)ext4_raw_inode(&is.iloc)->i_block, 300 0, EXT4_MIN_INLINE_DATA_SIZE); 301 302 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - 303 (void *)ext4_raw_inode(&is.iloc)); 304 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE; 305 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); 306 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA); 307 get_bh(is.iloc.bh); 308 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); 309 310 out: 311 brelse(is.iloc.bh); 312 return error; 313 } 314 315 static int ext4_update_inline_data(handle_t *handle, struct inode *inode, 316 unsigned int len) 317 { 318 int error; 319 void *value = NULL; 320 struct ext4_xattr_ibody_find is = { 321 .s = { .not_found = -ENODATA, }, 322 }; 323 struct ext4_xattr_info i = { 324 .name_index = EXT4_XATTR_INDEX_SYSTEM, 325 .name = EXT4_XATTR_SYSTEM_DATA, 326 }; 327 328 /* If the old space is ok, write the data directly. */ 329 if (len <= EXT4_I(inode)->i_inline_size) 330 return 0; 331 332 error = ext4_get_inode_loc(inode, &is.iloc); 333 if (error) 334 return error; 335 336 error = ext4_xattr_ibody_find(inode, &i, &is); 337 if (error) 338 goto out; 339 340 BUG_ON(is.s.not_found); 341 342 len -= EXT4_MIN_INLINE_DATA_SIZE; 343 value = kzalloc(len, GFP_NOFS); 344 if (!value) { 345 error = -ENOMEM; 346 goto out; 347 } 348 349 error = ext4_xattr_ibody_get(inode, i.name_index, i.name, 350 value, len); 351 if (error == -ENODATA) 352 goto out; 353 354 BUFFER_TRACE(is.iloc.bh, "get_write_access"); 355 error = ext4_journal_get_write_access(handle, is.iloc.bh); 356 if (error) 357 goto out; 358 359 /* Update the xttr entry. */ 360 i.value = value; 361 i.value_len = len; 362 363 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is); 364 if (error) 365 goto out; 366 367 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - 368 (void *)ext4_raw_inode(&is.iloc)); 369 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + 370 le32_to_cpu(is.s.here->e_value_size); 371 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 372 get_bh(is.iloc.bh); 373 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); 374 375 out: 376 kfree(value); 377 brelse(is.iloc.bh); 378 return error; 379 } 380 381 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode, 382 unsigned int len) 383 { 384 int ret, size, no_expand; 385 struct ext4_inode_info *ei = EXT4_I(inode); 386 387 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) 388 return -ENOSPC; 389 390 size = ext4_get_max_inline_size(inode); 391 if (size < len) 392 return -ENOSPC; 393 394 ext4_write_lock_xattr(inode, &no_expand); 395 396 if (ei->i_inline_off) 397 ret = ext4_update_inline_data(handle, inode, len); 398 else 399 ret = ext4_create_inline_data(handle, inode, len); 400 401 ext4_write_unlock_xattr(inode, &no_expand); 402 return ret; 403 } 404 405 static int ext4_destroy_inline_data_nolock(handle_t *handle, 406 struct inode *inode) 407 { 408 struct ext4_inode_info *ei = EXT4_I(inode); 409 struct ext4_xattr_ibody_find is = { 410 .s = { .not_found = 0, }, 411 }; 412 struct ext4_xattr_info i = { 413 .name_index = EXT4_XATTR_INDEX_SYSTEM, 414 .name = EXT4_XATTR_SYSTEM_DATA, 415 .value = NULL, 416 .value_len = 0, 417 }; 418 int error; 419 420 if (!ei->i_inline_off) 421 return 0; 422 423 error = ext4_get_inode_loc(inode, &is.iloc); 424 if (error) 425 return error; 426 427 error = ext4_xattr_ibody_find(inode, &i, &is); 428 if (error) 429 goto out; 430 431 BUFFER_TRACE(is.iloc.bh, "get_write_access"); 432 error = ext4_journal_get_write_access(handle, is.iloc.bh); 433 if (error) 434 goto out; 435 436 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is); 437 if (error) 438 goto out; 439 440 memset((void *)ext4_raw_inode(&is.iloc)->i_block, 441 0, EXT4_MIN_INLINE_DATA_SIZE); 442 443 if (ext4_has_feature_extents(inode->i_sb)) { 444 if (S_ISDIR(inode->i_mode) || 445 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) { 446 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 447 ext4_ext_tree_init(handle, inode); 448 } 449 } 450 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA); 451 452 get_bh(is.iloc.bh); 453 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); 454 455 EXT4_I(inode)->i_inline_off = 0; 456 EXT4_I(inode)->i_inline_size = 0; 457 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 458 out: 459 brelse(is.iloc.bh); 460 if (error == -ENODATA) 461 error = 0; 462 return error; 463 } 464 465 static int ext4_read_inline_page(struct inode *inode, struct page *page) 466 { 467 void *kaddr; 468 int ret = 0; 469 size_t len; 470 struct ext4_iloc iloc; 471 472 BUG_ON(!PageLocked(page)); 473 BUG_ON(!ext4_has_inline_data(inode)); 474 BUG_ON(page->index); 475 476 if (!EXT4_I(inode)->i_inline_off) { 477 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.", 478 inode->i_ino); 479 goto out; 480 } 481 482 ret = ext4_get_inode_loc(inode, &iloc); 483 if (ret) 484 goto out; 485 486 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode)); 487 kaddr = kmap_atomic(page); 488 ret = ext4_read_inline_data(inode, kaddr, len, &iloc); 489 flush_dcache_page(page); 490 kunmap_atomic(kaddr); 491 zero_user_segment(page, len, PAGE_SIZE); 492 SetPageUptodate(page); 493 brelse(iloc.bh); 494 495 out: 496 return ret; 497 } 498 499 int ext4_readpage_inline(struct inode *inode, struct page *page) 500 { 501 int ret = 0; 502 503 down_read(&EXT4_I(inode)->xattr_sem); 504 if (!ext4_has_inline_data(inode)) { 505 up_read(&EXT4_I(inode)->xattr_sem); 506 return -EAGAIN; 507 } 508 509 /* 510 * Current inline data can only exist in the 1st page, 511 * So for all the other pages, just set them uptodate. 512 */ 513 if (!page->index) 514 ret = ext4_read_inline_page(inode, page); 515 else if (!PageUptodate(page)) { 516 zero_user_segment(page, 0, PAGE_SIZE); 517 SetPageUptodate(page); 518 } 519 520 up_read(&EXT4_I(inode)->xattr_sem); 521 522 unlock_page(page); 523 return ret >= 0 ? 0 : ret; 524 } 525 526 static int ext4_convert_inline_data_to_extent(struct address_space *mapping, 527 struct inode *inode, 528 unsigned flags) 529 { 530 int ret, needed_blocks, no_expand; 531 handle_t *handle = NULL; 532 int retries = 0, sem_held = 0; 533 struct page *page = NULL; 534 unsigned from, to; 535 struct ext4_iloc iloc; 536 537 if (!ext4_has_inline_data(inode)) { 538 /* 539 * clear the flag so that no new write 540 * will trap here again. 541 */ 542 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 543 return 0; 544 } 545 546 needed_blocks = ext4_writepage_trans_blocks(inode); 547 548 ret = ext4_get_inode_loc(inode, &iloc); 549 if (ret) 550 return ret; 551 552 retry: 553 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 554 if (IS_ERR(handle)) { 555 ret = PTR_ERR(handle); 556 handle = NULL; 557 goto out; 558 } 559 560 /* We cannot recurse into the filesystem as the transaction is already 561 * started */ 562 flags |= AOP_FLAG_NOFS; 563 564 page = grab_cache_page_write_begin(mapping, 0, flags); 565 if (!page) { 566 ret = -ENOMEM; 567 goto out; 568 } 569 570 ext4_write_lock_xattr(inode, &no_expand); 571 sem_held = 1; 572 /* If some one has already done this for us, just exit. */ 573 if (!ext4_has_inline_data(inode)) { 574 ret = 0; 575 goto out; 576 } 577 578 from = 0; 579 to = ext4_get_inline_size(inode); 580 if (!PageUptodate(page)) { 581 ret = ext4_read_inline_page(inode, page); 582 if (ret < 0) 583 goto out; 584 } 585 586 ret = ext4_destroy_inline_data_nolock(handle, inode); 587 if (ret) 588 goto out; 589 590 if (ext4_should_dioread_nolock(inode)) { 591 ret = __block_write_begin(page, from, to, 592 ext4_get_block_unwritten); 593 } else 594 ret = __block_write_begin(page, from, to, ext4_get_block); 595 596 if (!ret && ext4_should_journal_data(inode)) { 597 ret = ext4_walk_page_buffers(handle, page_buffers(page), 598 from, to, NULL, 599 do_journal_get_write_access); 600 } 601 602 if (ret) { 603 unlock_page(page); 604 put_page(page); 605 page = NULL; 606 ext4_orphan_add(handle, inode); 607 ext4_write_unlock_xattr(inode, &no_expand); 608 sem_held = 0; 609 ext4_journal_stop(handle); 610 handle = NULL; 611 ext4_truncate_failed_write(inode); 612 /* 613 * If truncate failed early the inode might 614 * still be on the orphan list; we need to 615 * make sure the inode is removed from the 616 * orphan list in that case. 617 */ 618 if (inode->i_nlink) 619 ext4_orphan_del(NULL, inode); 620 } 621 622 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 623 goto retry; 624 625 if (page) 626 block_commit_write(page, from, to); 627 out: 628 if (page) { 629 unlock_page(page); 630 put_page(page); 631 } 632 if (sem_held) 633 ext4_write_unlock_xattr(inode, &no_expand); 634 if (handle) 635 ext4_journal_stop(handle); 636 brelse(iloc.bh); 637 return ret; 638 } 639 640 /* 641 * Try to write data in the inode. 642 * If the inode has inline data, check whether the new write can be 643 * in the inode also. If not, create the page the handle, move the data 644 * to the page make it update and let the later codes create extent for it. 645 */ 646 int ext4_try_to_write_inline_data(struct address_space *mapping, 647 struct inode *inode, 648 loff_t pos, unsigned len, 649 unsigned flags, 650 struct page **pagep) 651 { 652 int ret; 653 handle_t *handle; 654 struct page *page; 655 struct ext4_iloc iloc; 656 657 if (pos + len > ext4_get_max_inline_size(inode)) 658 goto convert; 659 660 ret = ext4_get_inode_loc(inode, &iloc); 661 if (ret) 662 return ret; 663 664 /* 665 * The possible write could happen in the inode, 666 * so try to reserve the space in inode first. 667 */ 668 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 669 if (IS_ERR(handle)) { 670 ret = PTR_ERR(handle); 671 handle = NULL; 672 goto out; 673 } 674 675 ret = ext4_prepare_inline_data(handle, inode, pos + len); 676 if (ret && ret != -ENOSPC) 677 goto out; 678 679 /* We don't have space in inline inode, so convert it to extent. */ 680 if (ret == -ENOSPC) { 681 ext4_journal_stop(handle); 682 brelse(iloc.bh); 683 goto convert; 684 } 685 686 flags |= AOP_FLAG_NOFS; 687 688 page = grab_cache_page_write_begin(mapping, 0, flags); 689 if (!page) { 690 ret = -ENOMEM; 691 goto out; 692 } 693 694 *pagep = page; 695 down_read(&EXT4_I(inode)->xattr_sem); 696 if (!ext4_has_inline_data(inode)) { 697 ret = 0; 698 unlock_page(page); 699 put_page(page); 700 goto out_up_read; 701 } 702 703 if (!PageUptodate(page)) { 704 ret = ext4_read_inline_page(inode, page); 705 if (ret < 0) 706 goto out_up_read; 707 } 708 709 ret = 1; 710 handle = NULL; 711 out_up_read: 712 up_read(&EXT4_I(inode)->xattr_sem); 713 out: 714 if (handle) 715 ext4_journal_stop(handle); 716 brelse(iloc.bh); 717 return ret; 718 convert: 719 return ext4_convert_inline_data_to_extent(mapping, 720 inode, flags); 721 } 722 723 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len, 724 unsigned copied, struct page *page) 725 { 726 int ret, no_expand; 727 void *kaddr; 728 struct ext4_iloc iloc; 729 730 if (unlikely(copied < len)) { 731 if (!PageUptodate(page)) { 732 copied = 0; 733 goto out; 734 } 735 } 736 737 ret = ext4_get_inode_loc(inode, &iloc); 738 if (ret) { 739 ext4_std_error(inode->i_sb, ret); 740 copied = 0; 741 goto out; 742 } 743 744 ext4_write_lock_xattr(inode, &no_expand); 745 BUG_ON(!ext4_has_inline_data(inode)); 746 747 kaddr = kmap_atomic(page); 748 ext4_write_inline_data(inode, &iloc, kaddr, pos, len); 749 kunmap_atomic(kaddr); 750 SetPageUptodate(page); 751 /* clear page dirty so that writepages wouldn't work for us. */ 752 ClearPageDirty(page); 753 754 ext4_write_unlock_xattr(inode, &no_expand); 755 brelse(iloc.bh); 756 out: 757 return copied; 758 } 759 760 struct buffer_head * 761 ext4_journalled_write_inline_data(struct inode *inode, 762 unsigned len, 763 struct page *page) 764 { 765 int ret, no_expand; 766 void *kaddr; 767 struct ext4_iloc iloc; 768 769 ret = ext4_get_inode_loc(inode, &iloc); 770 if (ret) { 771 ext4_std_error(inode->i_sb, ret); 772 return NULL; 773 } 774 775 ext4_write_lock_xattr(inode, &no_expand); 776 kaddr = kmap_atomic(page); 777 ext4_write_inline_data(inode, &iloc, kaddr, 0, len); 778 kunmap_atomic(kaddr); 779 ext4_write_unlock_xattr(inode, &no_expand); 780 781 return iloc.bh; 782 } 783 784 /* 785 * Try to make the page cache and handle ready for the inline data case. 786 * We can call this function in 2 cases: 787 * 1. The inode is created and the first write exceeds inline size. We can 788 * clear the inode state safely. 789 * 2. The inode has inline data, then we need to read the data, make it 790 * update and dirty so that ext4_da_writepages can handle it. We don't 791 * need to start the journal since the file's metatdata isn't changed now. 792 */ 793 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, 794 struct inode *inode, 795 unsigned flags, 796 void **fsdata) 797 { 798 int ret = 0, inline_size; 799 struct page *page; 800 801 page = grab_cache_page_write_begin(mapping, 0, flags); 802 if (!page) 803 return -ENOMEM; 804 805 down_read(&EXT4_I(inode)->xattr_sem); 806 if (!ext4_has_inline_data(inode)) { 807 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 808 goto out; 809 } 810 811 inline_size = ext4_get_inline_size(inode); 812 813 if (!PageUptodate(page)) { 814 ret = ext4_read_inline_page(inode, page); 815 if (ret < 0) 816 goto out; 817 } 818 819 ret = __block_write_begin(page, 0, inline_size, 820 ext4_da_get_block_prep); 821 if (ret) { 822 up_read(&EXT4_I(inode)->xattr_sem); 823 unlock_page(page); 824 put_page(page); 825 ext4_truncate_failed_write(inode); 826 return ret; 827 } 828 829 SetPageDirty(page); 830 SetPageUptodate(page); 831 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 832 *fsdata = (void *)CONVERT_INLINE_DATA; 833 834 out: 835 up_read(&EXT4_I(inode)->xattr_sem); 836 if (page) { 837 unlock_page(page); 838 put_page(page); 839 } 840 return ret; 841 } 842 843 /* 844 * Prepare the write for the inline data. 845 * If the the data can be written into the inode, we just read 846 * the page and make it uptodate, and start the journal. 847 * Otherwise read the page, makes it dirty so that it can be 848 * handle in writepages(the i_disksize update is left to the 849 * normal ext4_da_write_end). 850 */ 851 int ext4_da_write_inline_data_begin(struct address_space *mapping, 852 struct inode *inode, 853 loff_t pos, unsigned len, 854 unsigned flags, 855 struct page **pagep, 856 void **fsdata) 857 { 858 int ret, inline_size; 859 handle_t *handle; 860 struct page *page; 861 struct ext4_iloc iloc; 862 int retries; 863 864 ret = ext4_get_inode_loc(inode, &iloc); 865 if (ret) 866 return ret; 867 868 retry_journal: 869 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 870 if (IS_ERR(handle)) { 871 ret = PTR_ERR(handle); 872 goto out; 873 } 874 875 inline_size = ext4_get_max_inline_size(inode); 876 877 ret = -ENOSPC; 878 if (inline_size >= pos + len) { 879 ret = ext4_prepare_inline_data(handle, inode, pos + len); 880 if (ret && ret != -ENOSPC) 881 goto out_journal; 882 } 883 884 /* 885 * We cannot recurse into the filesystem as the transaction 886 * is already started. 887 */ 888 flags |= AOP_FLAG_NOFS; 889 890 if (ret == -ENOSPC) { 891 ret = ext4_da_convert_inline_data_to_extent(mapping, 892 inode, 893 flags, 894 fsdata); 895 ext4_journal_stop(handle); 896 if (ret == -ENOSPC && 897 ext4_should_retry_alloc(inode->i_sb, &retries)) 898 goto retry_journal; 899 goto out; 900 } 901 902 903 page = grab_cache_page_write_begin(mapping, 0, flags); 904 if (!page) { 905 ret = -ENOMEM; 906 goto out_journal; 907 } 908 909 down_read(&EXT4_I(inode)->xattr_sem); 910 if (!ext4_has_inline_data(inode)) { 911 ret = 0; 912 goto out_release_page; 913 } 914 915 if (!PageUptodate(page)) { 916 ret = ext4_read_inline_page(inode, page); 917 if (ret < 0) 918 goto out_release_page; 919 } 920 921 up_read(&EXT4_I(inode)->xattr_sem); 922 *pagep = page; 923 brelse(iloc.bh); 924 return 1; 925 out_release_page: 926 up_read(&EXT4_I(inode)->xattr_sem); 927 unlock_page(page); 928 put_page(page); 929 out_journal: 930 ext4_journal_stop(handle); 931 out: 932 brelse(iloc.bh); 933 return ret; 934 } 935 936 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos, 937 unsigned len, unsigned copied, 938 struct page *page) 939 { 940 int i_size_changed = 0; 941 int ret; 942 943 ret = ext4_write_inline_data_end(inode, pos, len, copied, page); 944 if (ret < 0) { 945 unlock_page(page); 946 put_page(page); 947 return ret; 948 } 949 copied = ret; 950 951 /* 952 * No need to use i_size_read() here, the i_size 953 * cannot change under us because we hold i_mutex. 954 * 955 * But it's important to update i_size while still holding page lock: 956 * page writeout could otherwise come in and zero beyond i_size. 957 */ 958 if (pos+copied > inode->i_size) { 959 i_size_write(inode, pos+copied); 960 i_size_changed = 1; 961 } 962 unlock_page(page); 963 put_page(page); 964 965 /* 966 * Don't mark the inode dirty under page lock. First, it unnecessarily 967 * makes the holding time of page lock longer. Second, it forces lock 968 * ordering of page lock and transaction start for journaling 969 * filesystems. 970 */ 971 if (i_size_changed) 972 mark_inode_dirty(inode); 973 974 return copied; 975 } 976 977 #ifdef INLINE_DIR_DEBUG 978 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh, 979 void *inline_start, int inline_size) 980 { 981 int offset; 982 unsigned short de_len; 983 struct ext4_dir_entry_2 *de = inline_start; 984 void *dlimit = inline_start + inline_size; 985 986 trace_printk("inode %lu\n", dir->i_ino); 987 offset = 0; 988 while ((void *)de < dlimit) { 989 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size); 990 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n", 991 offset, de_len, de->name_len, de->name, 992 de->name_len, le32_to_cpu(de->inode)); 993 if (ext4_check_dir_entry(dir, NULL, de, bh, 994 inline_start, inline_size, offset)) 995 BUG(); 996 997 offset += de_len; 998 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); 999 } 1000 } 1001 #else 1002 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size) 1003 #endif 1004 1005 /* 1006 * Add a new entry into a inline dir. 1007 * It will return -ENOSPC if no space is available, and -EIO 1008 * and -EEXIST if directory entry already exists. 1009 */ 1010 static int ext4_add_dirent_to_inline(handle_t *handle, 1011 struct ext4_filename *fname, 1012 struct inode *dir, 1013 struct inode *inode, 1014 struct ext4_iloc *iloc, 1015 void *inline_start, int inline_size) 1016 { 1017 int err; 1018 struct ext4_dir_entry_2 *de; 1019 1020 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start, 1021 inline_size, fname, &de); 1022 if (err) 1023 return err; 1024 1025 BUFFER_TRACE(iloc->bh, "get_write_access"); 1026 err = ext4_journal_get_write_access(handle, iloc->bh); 1027 if (err) 1028 return err; 1029 ext4_insert_dentry(inode, de, inline_size, fname); 1030 1031 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size); 1032 1033 /* 1034 * XXX shouldn't update any times until successful 1035 * completion of syscall, but too many callers depend 1036 * on this. 1037 * 1038 * XXX similarly, too many callers depend on 1039 * ext4_new_inode() setting the times, but error 1040 * recovery deletes the inode, so the worst that can 1041 * happen is that the times are slightly out of date 1042 * and/or different from the directory change time. 1043 */ 1044 dir->i_mtime = dir->i_ctime = current_time(dir); 1045 ext4_update_dx_flag(dir); 1046 inode_inc_iversion(dir); 1047 return 1; 1048 } 1049 1050 static void *ext4_get_inline_xattr_pos(struct inode *inode, 1051 struct ext4_iloc *iloc) 1052 { 1053 struct ext4_xattr_entry *entry; 1054 struct ext4_xattr_ibody_header *header; 1055 1056 BUG_ON(!EXT4_I(inode)->i_inline_off); 1057 1058 header = IHDR(inode, ext4_raw_inode(iloc)); 1059 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) + 1060 EXT4_I(inode)->i_inline_off); 1061 1062 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs); 1063 } 1064 1065 /* Set the final de to cover the whole block. */ 1066 static void ext4_update_final_de(void *de_buf, int old_size, int new_size) 1067 { 1068 struct ext4_dir_entry_2 *de, *prev_de; 1069 void *limit; 1070 int de_len; 1071 1072 de = (struct ext4_dir_entry_2 *)de_buf; 1073 if (old_size) { 1074 limit = de_buf + old_size; 1075 do { 1076 prev_de = de; 1077 de_len = ext4_rec_len_from_disk(de->rec_len, old_size); 1078 de_buf += de_len; 1079 de = (struct ext4_dir_entry_2 *)de_buf; 1080 } while (de_buf < limit); 1081 1082 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size - 1083 old_size, new_size); 1084 } else { 1085 /* this is just created, so create an empty entry. */ 1086 de->inode = 0; 1087 de->rec_len = ext4_rec_len_to_disk(new_size, new_size); 1088 } 1089 } 1090 1091 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir, 1092 struct ext4_iloc *iloc) 1093 { 1094 int ret; 1095 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE; 1096 int new_size = get_max_inline_xattr_value_size(dir, iloc); 1097 1098 if (new_size - old_size <= EXT4_DIR_REC_LEN(1)) 1099 return -ENOSPC; 1100 1101 ret = ext4_update_inline_data(handle, dir, 1102 new_size + EXT4_MIN_INLINE_DATA_SIZE); 1103 if (ret) 1104 return ret; 1105 1106 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size, 1107 EXT4_I(dir)->i_inline_size - 1108 EXT4_MIN_INLINE_DATA_SIZE); 1109 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size; 1110 return 0; 1111 } 1112 1113 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode, 1114 struct ext4_iloc *iloc, 1115 void *buf, int inline_size) 1116 { 1117 ext4_create_inline_data(handle, inode, inline_size); 1118 ext4_write_inline_data(inode, iloc, buf, 0, inline_size); 1119 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 1120 } 1121 1122 static int ext4_finish_convert_inline_dir(handle_t *handle, 1123 struct inode *inode, 1124 struct buffer_head *dir_block, 1125 void *buf, 1126 int inline_size) 1127 { 1128 int err, csum_size = 0, header_size = 0; 1129 struct ext4_dir_entry_2 *de; 1130 struct ext4_dir_entry_tail *t; 1131 void *target = dir_block->b_data; 1132 1133 /* 1134 * First create "." and ".." and then copy the dir information 1135 * back to the block. 1136 */ 1137 de = (struct ext4_dir_entry_2 *)target; 1138 de = ext4_init_dot_dotdot(inode, de, 1139 inode->i_sb->s_blocksize, csum_size, 1140 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1); 1141 header_size = (void *)de - target; 1142 1143 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE, 1144 inline_size - EXT4_INLINE_DOTDOT_SIZE); 1145 1146 if (ext4_has_metadata_csum(inode->i_sb)) 1147 csum_size = sizeof(struct ext4_dir_entry_tail); 1148 1149 inode->i_size = inode->i_sb->s_blocksize; 1150 i_size_write(inode, inode->i_sb->s_blocksize); 1151 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize; 1152 ext4_update_final_de(dir_block->b_data, 1153 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size, 1154 inode->i_sb->s_blocksize - csum_size); 1155 1156 if (csum_size) { 1157 t = EXT4_DIRENT_TAIL(dir_block->b_data, 1158 inode->i_sb->s_blocksize); 1159 initialize_dirent_tail(t, inode->i_sb->s_blocksize); 1160 } 1161 set_buffer_uptodate(dir_block); 1162 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block); 1163 if (err) 1164 return err; 1165 set_buffer_verified(dir_block); 1166 return ext4_mark_inode_dirty(handle, inode); 1167 } 1168 1169 static int ext4_convert_inline_data_nolock(handle_t *handle, 1170 struct inode *inode, 1171 struct ext4_iloc *iloc) 1172 { 1173 int error; 1174 void *buf = NULL; 1175 struct buffer_head *data_bh = NULL; 1176 struct ext4_map_blocks map; 1177 int inline_size; 1178 1179 inline_size = ext4_get_inline_size(inode); 1180 buf = kmalloc(inline_size, GFP_NOFS); 1181 if (!buf) { 1182 error = -ENOMEM; 1183 goto out; 1184 } 1185 1186 error = ext4_read_inline_data(inode, buf, inline_size, iloc); 1187 if (error < 0) 1188 goto out; 1189 1190 /* 1191 * Make sure the inline directory entries pass checks before we try to 1192 * convert them, so that we avoid touching stuff that needs fsck. 1193 */ 1194 if (S_ISDIR(inode->i_mode)) { 1195 error = ext4_check_all_de(inode, iloc->bh, 1196 buf + EXT4_INLINE_DOTDOT_SIZE, 1197 inline_size - EXT4_INLINE_DOTDOT_SIZE); 1198 if (error) 1199 goto out; 1200 } 1201 1202 error = ext4_destroy_inline_data_nolock(handle, inode); 1203 if (error) 1204 goto out; 1205 1206 map.m_lblk = 0; 1207 map.m_len = 1; 1208 map.m_flags = 0; 1209 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE); 1210 if (error < 0) 1211 goto out_restore; 1212 if (!(map.m_flags & EXT4_MAP_MAPPED)) { 1213 error = -EIO; 1214 goto out_restore; 1215 } 1216 1217 data_bh = sb_getblk(inode->i_sb, map.m_pblk); 1218 if (!data_bh) { 1219 error = -ENOMEM; 1220 goto out_restore; 1221 } 1222 1223 lock_buffer(data_bh); 1224 error = ext4_journal_get_create_access(handle, data_bh); 1225 if (error) { 1226 unlock_buffer(data_bh); 1227 error = -EIO; 1228 goto out_restore; 1229 } 1230 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize); 1231 1232 if (!S_ISDIR(inode->i_mode)) { 1233 memcpy(data_bh->b_data, buf, inline_size); 1234 set_buffer_uptodate(data_bh); 1235 error = ext4_handle_dirty_metadata(handle, 1236 inode, data_bh); 1237 } else { 1238 error = ext4_finish_convert_inline_dir(handle, inode, data_bh, 1239 buf, inline_size); 1240 } 1241 1242 unlock_buffer(data_bh); 1243 out_restore: 1244 if (error) 1245 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size); 1246 1247 out: 1248 brelse(data_bh); 1249 kfree(buf); 1250 return error; 1251 } 1252 1253 /* 1254 * Try to add the new entry to the inline data. 1255 * If succeeds, return 0. If not, extended the inline dir and copied data to 1256 * the new created block. 1257 */ 1258 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname, 1259 struct inode *dir, struct inode *inode) 1260 { 1261 int ret, inline_size, no_expand; 1262 void *inline_start; 1263 struct ext4_iloc iloc; 1264 1265 ret = ext4_get_inode_loc(dir, &iloc); 1266 if (ret) 1267 return ret; 1268 1269 ext4_write_lock_xattr(dir, &no_expand); 1270 if (!ext4_has_inline_data(dir)) 1271 goto out; 1272 1273 inline_start = (void *)ext4_raw_inode(&iloc)->i_block + 1274 EXT4_INLINE_DOTDOT_SIZE; 1275 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; 1276 1277 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc, 1278 inline_start, inline_size); 1279 if (ret != -ENOSPC) 1280 goto out; 1281 1282 /* check whether it can be inserted to inline xattr space. */ 1283 inline_size = EXT4_I(dir)->i_inline_size - 1284 EXT4_MIN_INLINE_DATA_SIZE; 1285 if (!inline_size) { 1286 /* Try to use the xattr space.*/ 1287 ret = ext4_update_inline_dir(handle, dir, &iloc); 1288 if (ret && ret != -ENOSPC) 1289 goto out; 1290 1291 inline_size = EXT4_I(dir)->i_inline_size - 1292 EXT4_MIN_INLINE_DATA_SIZE; 1293 } 1294 1295 if (inline_size) { 1296 inline_start = ext4_get_inline_xattr_pos(dir, &iloc); 1297 1298 ret = ext4_add_dirent_to_inline(handle, fname, dir, 1299 inode, &iloc, inline_start, 1300 inline_size); 1301 1302 if (ret != -ENOSPC) 1303 goto out; 1304 } 1305 1306 /* 1307 * The inline space is filled up, so create a new block for it. 1308 * As the extent tree will be created, we have to save the inline 1309 * dir first. 1310 */ 1311 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc); 1312 1313 out: 1314 ext4_write_unlock_xattr(dir, &no_expand); 1315 ext4_mark_inode_dirty(handle, dir); 1316 brelse(iloc.bh); 1317 return ret; 1318 } 1319 1320 /* 1321 * This function fills a red-black tree with information from an 1322 * inlined dir. It returns the number directory entries loaded 1323 * into the tree. If there is an error it is returned in err. 1324 */ 1325 int htree_inlinedir_to_tree(struct file *dir_file, 1326 struct inode *dir, ext4_lblk_t block, 1327 struct dx_hash_info *hinfo, 1328 __u32 start_hash, __u32 start_minor_hash, 1329 int *has_inline_data) 1330 { 1331 int err = 0, count = 0; 1332 unsigned int parent_ino; 1333 int pos; 1334 struct ext4_dir_entry_2 *de; 1335 struct inode *inode = file_inode(dir_file); 1336 int ret, inline_size = 0; 1337 struct ext4_iloc iloc; 1338 void *dir_buf = NULL; 1339 struct ext4_dir_entry_2 fake; 1340 struct fscrypt_str tmp_str; 1341 1342 ret = ext4_get_inode_loc(inode, &iloc); 1343 if (ret) 1344 return ret; 1345 1346 down_read(&EXT4_I(inode)->xattr_sem); 1347 if (!ext4_has_inline_data(inode)) { 1348 up_read(&EXT4_I(inode)->xattr_sem); 1349 *has_inline_data = 0; 1350 goto out; 1351 } 1352 1353 inline_size = ext4_get_inline_size(inode); 1354 dir_buf = kmalloc(inline_size, GFP_NOFS); 1355 if (!dir_buf) { 1356 ret = -ENOMEM; 1357 up_read(&EXT4_I(inode)->xattr_sem); 1358 goto out; 1359 } 1360 1361 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc); 1362 up_read(&EXT4_I(inode)->xattr_sem); 1363 if (ret < 0) 1364 goto out; 1365 1366 pos = 0; 1367 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode); 1368 while (pos < inline_size) { 1369 /* 1370 * As inlined dir doesn't store any information about '.' and 1371 * only the inode number of '..' is stored, we have to handle 1372 * them differently. 1373 */ 1374 if (pos == 0) { 1375 fake.inode = cpu_to_le32(inode->i_ino); 1376 fake.name_len = 1; 1377 strcpy(fake.name, "."); 1378 fake.rec_len = ext4_rec_len_to_disk( 1379 EXT4_DIR_REC_LEN(fake.name_len), 1380 inline_size); 1381 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR); 1382 de = &fake; 1383 pos = EXT4_INLINE_DOTDOT_OFFSET; 1384 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) { 1385 fake.inode = cpu_to_le32(parent_ino); 1386 fake.name_len = 2; 1387 strcpy(fake.name, ".."); 1388 fake.rec_len = ext4_rec_len_to_disk( 1389 EXT4_DIR_REC_LEN(fake.name_len), 1390 inline_size); 1391 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR); 1392 de = &fake; 1393 pos = EXT4_INLINE_DOTDOT_SIZE; 1394 } else { 1395 de = (struct ext4_dir_entry_2 *)(dir_buf + pos); 1396 pos += ext4_rec_len_from_disk(de->rec_len, inline_size); 1397 if (ext4_check_dir_entry(inode, dir_file, de, 1398 iloc.bh, dir_buf, 1399 inline_size, pos)) { 1400 ret = count; 1401 goto out; 1402 } 1403 } 1404 1405 ext4fs_dirhash(de->name, de->name_len, hinfo); 1406 if ((hinfo->hash < start_hash) || 1407 ((hinfo->hash == start_hash) && 1408 (hinfo->minor_hash < start_minor_hash))) 1409 continue; 1410 if (de->inode == 0) 1411 continue; 1412 tmp_str.name = de->name; 1413 tmp_str.len = de->name_len; 1414 err = ext4_htree_store_dirent(dir_file, hinfo->hash, 1415 hinfo->minor_hash, de, &tmp_str); 1416 if (err) { 1417 count = err; 1418 goto out; 1419 } 1420 count++; 1421 } 1422 ret = count; 1423 out: 1424 kfree(dir_buf); 1425 brelse(iloc.bh); 1426 return ret; 1427 } 1428 1429 /* 1430 * So this function is called when the volume is mkfsed with 1431 * dir_index disabled. In order to keep f_pos persistent 1432 * after we convert from an inlined dir to a blocked based, 1433 * we just pretend that we are a normal dir and return the 1434 * offset as if '.' and '..' really take place. 1435 * 1436 */ 1437 int ext4_read_inline_dir(struct file *file, 1438 struct dir_context *ctx, 1439 int *has_inline_data) 1440 { 1441 unsigned int offset, parent_ino; 1442 int i; 1443 struct ext4_dir_entry_2 *de; 1444 struct super_block *sb; 1445 struct inode *inode = file_inode(file); 1446 int ret, inline_size = 0; 1447 struct ext4_iloc iloc; 1448 void *dir_buf = NULL; 1449 int dotdot_offset, dotdot_size, extra_offset, extra_size; 1450 1451 ret = ext4_get_inode_loc(inode, &iloc); 1452 if (ret) 1453 return ret; 1454 1455 down_read(&EXT4_I(inode)->xattr_sem); 1456 if (!ext4_has_inline_data(inode)) { 1457 up_read(&EXT4_I(inode)->xattr_sem); 1458 *has_inline_data = 0; 1459 goto out; 1460 } 1461 1462 inline_size = ext4_get_inline_size(inode); 1463 dir_buf = kmalloc(inline_size, GFP_NOFS); 1464 if (!dir_buf) { 1465 ret = -ENOMEM; 1466 up_read(&EXT4_I(inode)->xattr_sem); 1467 goto out; 1468 } 1469 1470 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc); 1471 up_read(&EXT4_I(inode)->xattr_sem); 1472 if (ret < 0) 1473 goto out; 1474 1475 ret = 0; 1476 sb = inode->i_sb; 1477 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode); 1478 offset = ctx->pos; 1479 1480 /* 1481 * dotdot_offset and dotdot_size is the real offset and 1482 * size for ".." and "." if the dir is block based while 1483 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE. 1484 * So we will use extra_offset and extra_size to indicate them 1485 * during the inline dir iteration. 1486 */ 1487 dotdot_offset = EXT4_DIR_REC_LEN(1); 1488 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2); 1489 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE; 1490 extra_size = extra_offset + inline_size; 1491 1492 /* 1493 * If the version has changed since the last call to 1494 * readdir(2), then we might be pointing to an invalid 1495 * dirent right now. Scan from the start of the inline 1496 * dir to make sure. 1497 */ 1498 if (inode_cmp_iversion(inode, file->f_version)) { 1499 for (i = 0; i < extra_size && i < offset;) { 1500 /* 1501 * "." is with offset 0 and 1502 * ".." is dotdot_offset. 1503 */ 1504 if (!i) { 1505 i = dotdot_offset; 1506 continue; 1507 } else if (i == dotdot_offset) { 1508 i = dotdot_size; 1509 continue; 1510 } 1511 /* for other entry, the real offset in 1512 * the buf has to be tuned accordingly. 1513 */ 1514 de = (struct ext4_dir_entry_2 *) 1515 (dir_buf + i - extra_offset); 1516 /* It's too expensive to do a full 1517 * dirent test each time round this 1518 * loop, but we do have to test at 1519 * least that it is non-zero. A 1520 * failure will be detected in the 1521 * dirent test below. */ 1522 if (ext4_rec_len_from_disk(de->rec_len, extra_size) 1523 < EXT4_DIR_REC_LEN(1)) 1524 break; 1525 i += ext4_rec_len_from_disk(de->rec_len, 1526 extra_size); 1527 } 1528 offset = i; 1529 ctx->pos = offset; 1530 file->f_version = inode_query_iversion(inode); 1531 } 1532 1533 while (ctx->pos < extra_size) { 1534 if (ctx->pos == 0) { 1535 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR)) 1536 goto out; 1537 ctx->pos = dotdot_offset; 1538 continue; 1539 } 1540 1541 if (ctx->pos == dotdot_offset) { 1542 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR)) 1543 goto out; 1544 ctx->pos = dotdot_size; 1545 continue; 1546 } 1547 1548 de = (struct ext4_dir_entry_2 *) 1549 (dir_buf + ctx->pos - extra_offset); 1550 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf, 1551 extra_size, ctx->pos)) 1552 goto out; 1553 if (le32_to_cpu(de->inode)) { 1554 if (!dir_emit(ctx, de->name, de->name_len, 1555 le32_to_cpu(de->inode), 1556 get_dtype(sb, de->file_type))) 1557 goto out; 1558 } 1559 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size); 1560 } 1561 out: 1562 kfree(dir_buf); 1563 brelse(iloc.bh); 1564 return ret; 1565 } 1566 1567 struct buffer_head *ext4_get_first_inline_block(struct inode *inode, 1568 struct ext4_dir_entry_2 **parent_de, 1569 int *retval) 1570 { 1571 struct ext4_iloc iloc; 1572 1573 *retval = ext4_get_inode_loc(inode, &iloc); 1574 if (*retval) 1575 return NULL; 1576 1577 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; 1578 1579 return iloc.bh; 1580 } 1581 1582 /* 1583 * Try to create the inline data for the new dir. 1584 * If it succeeds, return 0, otherwise return the error. 1585 * In case of ENOSPC, the caller should create the normal disk layout dir. 1586 */ 1587 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent, 1588 struct inode *inode) 1589 { 1590 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE; 1591 struct ext4_iloc iloc; 1592 struct ext4_dir_entry_2 *de; 1593 1594 ret = ext4_get_inode_loc(inode, &iloc); 1595 if (ret) 1596 return ret; 1597 1598 ret = ext4_prepare_inline_data(handle, inode, inline_size); 1599 if (ret) 1600 goto out; 1601 1602 /* 1603 * For inline dir, we only save the inode information for the ".." 1604 * and create a fake dentry to cover the left space. 1605 */ 1606 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; 1607 de->inode = cpu_to_le32(parent->i_ino); 1608 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE); 1609 de->inode = 0; 1610 de->rec_len = ext4_rec_len_to_disk( 1611 inline_size - EXT4_INLINE_DOTDOT_SIZE, 1612 inline_size); 1613 set_nlink(inode, 2); 1614 inode->i_size = EXT4_I(inode)->i_disksize = inline_size; 1615 out: 1616 brelse(iloc.bh); 1617 return ret; 1618 } 1619 1620 struct buffer_head *ext4_find_inline_entry(struct inode *dir, 1621 struct ext4_filename *fname, 1622 struct ext4_dir_entry_2 **res_dir, 1623 int *has_inline_data) 1624 { 1625 int ret; 1626 struct ext4_iloc iloc; 1627 void *inline_start; 1628 int inline_size; 1629 1630 if (ext4_get_inode_loc(dir, &iloc)) 1631 return NULL; 1632 1633 down_read(&EXT4_I(dir)->xattr_sem); 1634 if (!ext4_has_inline_data(dir)) { 1635 *has_inline_data = 0; 1636 goto out; 1637 } 1638 1639 inline_start = (void *)ext4_raw_inode(&iloc)->i_block + 1640 EXT4_INLINE_DOTDOT_SIZE; 1641 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; 1642 ret = ext4_search_dir(iloc.bh, inline_start, inline_size, 1643 dir, fname, 0, res_dir); 1644 if (ret == 1) 1645 goto out_find; 1646 if (ret < 0) 1647 goto out; 1648 1649 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE) 1650 goto out; 1651 1652 inline_start = ext4_get_inline_xattr_pos(dir, &iloc); 1653 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE; 1654 1655 ret = ext4_search_dir(iloc.bh, inline_start, inline_size, 1656 dir, fname, 0, res_dir); 1657 if (ret == 1) 1658 goto out_find; 1659 1660 out: 1661 brelse(iloc.bh); 1662 iloc.bh = NULL; 1663 out_find: 1664 up_read(&EXT4_I(dir)->xattr_sem); 1665 return iloc.bh; 1666 } 1667 1668 int ext4_delete_inline_entry(handle_t *handle, 1669 struct inode *dir, 1670 struct ext4_dir_entry_2 *de_del, 1671 struct buffer_head *bh, 1672 int *has_inline_data) 1673 { 1674 int err, inline_size, no_expand; 1675 struct ext4_iloc iloc; 1676 void *inline_start; 1677 1678 err = ext4_get_inode_loc(dir, &iloc); 1679 if (err) 1680 return err; 1681 1682 ext4_write_lock_xattr(dir, &no_expand); 1683 if (!ext4_has_inline_data(dir)) { 1684 *has_inline_data = 0; 1685 goto out; 1686 } 1687 1688 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) < 1689 EXT4_MIN_INLINE_DATA_SIZE) { 1690 inline_start = (void *)ext4_raw_inode(&iloc)->i_block + 1691 EXT4_INLINE_DOTDOT_SIZE; 1692 inline_size = EXT4_MIN_INLINE_DATA_SIZE - 1693 EXT4_INLINE_DOTDOT_SIZE; 1694 } else { 1695 inline_start = ext4_get_inline_xattr_pos(dir, &iloc); 1696 inline_size = ext4_get_inline_size(dir) - 1697 EXT4_MIN_INLINE_DATA_SIZE; 1698 } 1699 1700 BUFFER_TRACE(bh, "get_write_access"); 1701 err = ext4_journal_get_write_access(handle, bh); 1702 if (err) 1703 goto out; 1704 1705 err = ext4_generic_delete_entry(handle, dir, de_del, bh, 1706 inline_start, inline_size, 0); 1707 if (err) 1708 goto out; 1709 1710 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size); 1711 out: 1712 ext4_write_unlock_xattr(dir, &no_expand); 1713 if (likely(err == 0)) 1714 err = ext4_mark_inode_dirty(handle, dir); 1715 brelse(iloc.bh); 1716 if (err != -ENOENT) 1717 ext4_std_error(dir->i_sb, err); 1718 return err; 1719 } 1720 1721 /* 1722 * Get the inline dentry at offset. 1723 */ 1724 static inline struct ext4_dir_entry_2 * 1725 ext4_get_inline_entry(struct inode *inode, 1726 struct ext4_iloc *iloc, 1727 unsigned int offset, 1728 void **inline_start, 1729 int *inline_size) 1730 { 1731 void *inline_pos; 1732 1733 BUG_ON(offset > ext4_get_inline_size(inode)); 1734 1735 if (offset < EXT4_MIN_INLINE_DATA_SIZE) { 1736 inline_pos = (void *)ext4_raw_inode(iloc)->i_block; 1737 *inline_size = EXT4_MIN_INLINE_DATA_SIZE; 1738 } else { 1739 inline_pos = ext4_get_inline_xattr_pos(inode, iloc); 1740 offset -= EXT4_MIN_INLINE_DATA_SIZE; 1741 *inline_size = ext4_get_inline_size(inode) - 1742 EXT4_MIN_INLINE_DATA_SIZE; 1743 } 1744 1745 if (inline_start) 1746 *inline_start = inline_pos; 1747 return (struct ext4_dir_entry_2 *)(inline_pos + offset); 1748 } 1749 1750 bool empty_inline_dir(struct inode *dir, int *has_inline_data) 1751 { 1752 int err, inline_size; 1753 struct ext4_iloc iloc; 1754 void *inline_pos; 1755 unsigned int offset; 1756 struct ext4_dir_entry_2 *de; 1757 bool ret = true; 1758 1759 err = ext4_get_inode_loc(dir, &iloc); 1760 if (err) { 1761 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block", 1762 err, dir->i_ino); 1763 return true; 1764 } 1765 1766 down_read(&EXT4_I(dir)->xattr_sem); 1767 if (!ext4_has_inline_data(dir)) { 1768 *has_inline_data = 0; 1769 goto out; 1770 } 1771 1772 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; 1773 if (!le32_to_cpu(de->inode)) { 1774 ext4_warning(dir->i_sb, 1775 "bad inline directory (dir #%lu) - no `..'", 1776 dir->i_ino); 1777 ret = true; 1778 goto out; 1779 } 1780 1781 offset = EXT4_INLINE_DOTDOT_SIZE; 1782 while (offset < dir->i_size) { 1783 de = ext4_get_inline_entry(dir, &iloc, offset, 1784 &inline_pos, &inline_size); 1785 if (ext4_check_dir_entry(dir, NULL, de, 1786 iloc.bh, inline_pos, 1787 inline_size, offset)) { 1788 ext4_warning(dir->i_sb, 1789 "bad inline directory (dir #%lu) - " 1790 "inode %u, rec_len %u, name_len %d" 1791 "inline size %d", 1792 dir->i_ino, le32_to_cpu(de->inode), 1793 le16_to_cpu(de->rec_len), de->name_len, 1794 inline_size); 1795 ret = true; 1796 goto out; 1797 } 1798 if (le32_to_cpu(de->inode)) { 1799 ret = false; 1800 goto out; 1801 } 1802 offset += ext4_rec_len_from_disk(de->rec_len, inline_size); 1803 } 1804 1805 out: 1806 up_read(&EXT4_I(dir)->xattr_sem); 1807 brelse(iloc.bh); 1808 return ret; 1809 } 1810 1811 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode) 1812 { 1813 int ret, no_expand; 1814 1815 ext4_write_lock_xattr(inode, &no_expand); 1816 ret = ext4_destroy_inline_data_nolock(handle, inode); 1817 ext4_write_unlock_xattr(inode, &no_expand); 1818 1819 return ret; 1820 } 1821 1822 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap) 1823 { 1824 __u64 addr; 1825 int error = -EAGAIN; 1826 struct ext4_iloc iloc; 1827 1828 down_read(&EXT4_I(inode)->xattr_sem); 1829 if (!ext4_has_inline_data(inode)) 1830 goto out; 1831 1832 error = ext4_get_inode_loc(inode, &iloc); 1833 if (error) 1834 goto out; 1835 1836 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits; 1837 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data; 1838 addr += offsetof(struct ext4_inode, i_block); 1839 1840 brelse(iloc.bh); 1841 1842 iomap->addr = addr; 1843 iomap->offset = 0; 1844 iomap->length = min_t(loff_t, ext4_get_inline_size(inode), 1845 i_size_read(inode)); 1846 iomap->type = 0; 1847 iomap->flags = IOMAP_F_DATA_INLINE; 1848 1849 out: 1850 up_read(&EXT4_I(inode)->xattr_sem); 1851 return error; 1852 } 1853 1854 int ext4_inline_data_fiemap(struct inode *inode, 1855 struct fiemap_extent_info *fieinfo, 1856 int *has_inline, __u64 start, __u64 len) 1857 { 1858 __u64 physical = 0; 1859 __u64 inline_len; 1860 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | 1861 FIEMAP_EXTENT_LAST; 1862 int error = 0; 1863 struct ext4_iloc iloc; 1864 1865 down_read(&EXT4_I(inode)->xattr_sem); 1866 if (!ext4_has_inline_data(inode)) { 1867 *has_inline = 0; 1868 goto out; 1869 } 1870 inline_len = min_t(size_t, ext4_get_inline_size(inode), 1871 i_size_read(inode)); 1872 if (start >= inline_len) 1873 goto out; 1874 if (start + len < inline_len) 1875 inline_len = start + len; 1876 inline_len -= start; 1877 1878 error = ext4_get_inode_loc(inode, &iloc); 1879 if (error) 1880 goto out; 1881 1882 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits; 1883 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data; 1884 physical += offsetof(struct ext4_inode, i_block); 1885 1886 if (physical) 1887 error = fiemap_fill_next_extent(fieinfo, start, physical, 1888 inline_len, flags); 1889 brelse(iloc.bh); 1890 out: 1891 up_read(&EXT4_I(inode)->xattr_sem); 1892 return (error < 0 ? error : 0); 1893 } 1894 1895 /* 1896 * Called during xattr set, and if we can sparse space 'needed', 1897 * just create the extent tree evict the data to the outer block. 1898 * 1899 * We use jbd2 instead of page cache to move data to the 1st block 1900 * so that the whole transaction can be committed as a whole and 1901 * the data isn't lost because of the delayed page cache write. 1902 */ 1903 int ext4_try_to_evict_inline_data(handle_t *handle, 1904 struct inode *inode, 1905 int needed) 1906 { 1907 int error; 1908 struct ext4_xattr_entry *entry; 1909 struct ext4_inode *raw_inode; 1910 struct ext4_iloc iloc; 1911 1912 error = ext4_get_inode_loc(inode, &iloc); 1913 if (error) 1914 return error; 1915 1916 raw_inode = ext4_raw_inode(&iloc); 1917 entry = (struct ext4_xattr_entry *)((void *)raw_inode + 1918 EXT4_I(inode)->i_inline_off); 1919 if (EXT4_XATTR_LEN(entry->e_name_len) + 1920 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) { 1921 error = -ENOSPC; 1922 goto out; 1923 } 1924 1925 error = ext4_convert_inline_data_nolock(handle, inode, &iloc); 1926 out: 1927 brelse(iloc.bh); 1928 return error; 1929 } 1930 1931 int ext4_inline_data_truncate(struct inode *inode, int *has_inline) 1932 { 1933 handle_t *handle; 1934 int inline_size, value_len, needed_blocks, no_expand, err = 0; 1935 size_t i_size; 1936 void *value = NULL; 1937 struct ext4_xattr_ibody_find is = { 1938 .s = { .not_found = -ENODATA, }, 1939 }; 1940 struct ext4_xattr_info i = { 1941 .name_index = EXT4_XATTR_INDEX_SYSTEM, 1942 .name = EXT4_XATTR_SYSTEM_DATA, 1943 }; 1944 1945 1946 needed_blocks = ext4_writepage_trans_blocks(inode); 1947 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks); 1948 if (IS_ERR(handle)) 1949 return PTR_ERR(handle); 1950 1951 ext4_write_lock_xattr(inode, &no_expand); 1952 if (!ext4_has_inline_data(inode)) { 1953 *has_inline = 0; 1954 ext4_journal_stop(handle); 1955 return 0; 1956 } 1957 1958 if ((err = ext4_orphan_add(handle, inode)) != 0) 1959 goto out; 1960 1961 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0) 1962 goto out; 1963 1964 down_write(&EXT4_I(inode)->i_data_sem); 1965 i_size = inode->i_size; 1966 inline_size = ext4_get_inline_size(inode); 1967 EXT4_I(inode)->i_disksize = i_size; 1968 1969 if (i_size < inline_size) { 1970 /* Clear the content in the xattr space. */ 1971 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) { 1972 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0) 1973 goto out_error; 1974 1975 BUG_ON(is.s.not_found); 1976 1977 value_len = le32_to_cpu(is.s.here->e_value_size); 1978 value = kmalloc(value_len, GFP_NOFS); 1979 if (!value) { 1980 err = -ENOMEM; 1981 goto out_error; 1982 } 1983 1984 err = ext4_xattr_ibody_get(inode, i.name_index, 1985 i.name, value, value_len); 1986 if (err <= 0) 1987 goto out_error; 1988 1989 i.value = value; 1990 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ? 1991 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0; 1992 err = ext4_xattr_ibody_inline_set(handle, inode, 1993 &i, &is); 1994 if (err) 1995 goto out_error; 1996 } 1997 1998 /* Clear the content within i_blocks. */ 1999 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) { 2000 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block; 2001 memset(p + i_size, 0, 2002 EXT4_MIN_INLINE_DATA_SIZE - i_size); 2003 } 2004 2005 EXT4_I(inode)->i_inline_size = i_size < 2006 EXT4_MIN_INLINE_DATA_SIZE ? 2007 EXT4_MIN_INLINE_DATA_SIZE : i_size; 2008 } 2009 2010 out_error: 2011 up_write(&EXT4_I(inode)->i_data_sem); 2012 out: 2013 brelse(is.iloc.bh); 2014 ext4_write_unlock_xattr(inode, &no_expand); 2015 kfree(value); 2016 if (inode->i_nlink) 2017 ext4_orphan_del(handle, inode); 2018 2019 if (err == 0) { 2020 inode->i_mtime = inode->i_ctime = current_time(inode); 2021 err = ext4_mark_inode_dirty(handle, inode); 2022 if (IS_SYNC(inode)) 2023 ext4_handle_sync(handle); 2024 } 2025 ext4_journal_stop(handle); 2026 return err; 2027 } 2028 2029 int ext4_convert_inline_data(struct inode *inode) 2030 { 2031 int error, needed_blocks, no_expand; 2032 handle_t *handle; 2033 struct ext4_iloc iloc; 2034 2035 if (!ext4_has_inline_data(inode)) { 2036 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 2037 return 0; 2038 } 2039 2040 needed_blocks = ext4_writepage_trans_blocks(inode); 2041 2042 iloc.bh = NULL; 2043 error = ext4_get_inode_loc(inode, &iloc); 2044 if (error) 2045 return error; 2046 2047 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 2048 if (IS_ERR(handle)) { 2049 error = PTR_ERR(handle); 2050 goto out_free; 2051 } 2052 2053 ext4_write_lock_xattr(inode, &no_expand); 2054 if (ext4_has_inline_data(inode)) 2055 error = ext4_convert_inline_data_nolock(handle, inode, &iloc); 2056 ext4_write_unlock_xattr(inode, &no_expand); 2057 ext4_journal_stop(handle); 2058 out_free: 2059 brelse(iloc.bh); 2060 return error; 2061 } 2062