1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (c) 2012 Taobao.
4 * Written by Tao Ma <boyu.mt@taobao.com>
7 #include <linux/iomap.h>
8 #include <linux/fiemap.h>
9 #include <linux/iversion.h>
10 #include <linux/backing-dev.h>
12 #include "ext4_jbd2.h"
17 #define EXT4_XATTR_SYSTEM_DATA "data"
18 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
19 #define EXT4_INLINE_DOTDOT_OFFSET 2
20 #define EXT4_INLINE_DOTDOT_SIZE 4
22 static int ext4_get_inline_size(struct inode *inode)
24 if (EXT4_I(inode)->i_inline_off)
25 return EXT4_I(inode)->i_inline_size;
30 static int get_max_inline_xattr_value_size(struct inode *inode,
31 struct ext4_iloc *iloc)
33 struct ext4_xattr_ibody_header *header;
34 struct ext4_xattr_entry *entry;
35 struct ext4_inode *raw_inode;
39 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
42 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
43 EXT4_GOOD_OLD_INODE_SIZE -
44 EXT4_I(inode)->i_extra_isize -
45 sizeof(struct ext4_xattr_ibody_header);
48 * We need to subtract another sizeof(__u32) since an in-inode xattr
49 * needs an empty 4 bytes to indicate the gap between the xattr entry
50 * and the name/value pair.
52 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
53 return EXT4_XATTR_SIZE(min_offs -
54 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
55 EXT4_XATTR_ROUND - sizeof(__u32));
57 raw_inode = ext4_raw_inode(iloc);
58 header = IHDR(inode, raw_inode);
59 entry = IFIRST(header);
60 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
62 /* Compute min_offs. */
63 while (!IS_LAST_ENTRY(entry)) {
64 void *next = EXT4_XATTR_NEXT(entry);
67 EXT4_ERROR_INODE(inode,
68 "corrupt xattr in inline inode");
71 if (!entry->e_value_inum && entry->e_value_size) {
72 size_t offs = le16_to_cpu(entry->e_value_offs);
79 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
81 if (EXT4_I(inode)->i_inline_off) {
82 entry = (struct ext4_xattr_entry *)
83 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
85 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
89 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
91 if (free > EXT4_XATTR_ROUND)
92 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
101 * Get the maximum size we now can store in an inode.
102 * If we can't find the space for a xattr entry, don't use the space
103 * of the extents since we have no space to indicate the inline data.
105 int ext4_get_max_inline_size(struct inode *inode)
107 int error, max_inline_size;
108 struct ext4_iloc iloc;
110 if (EXT4_I(inode)->i_extra_isize == 0)
113 error = ext4_get_inode_loc(inode, &iloc);
115 ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
116 "can't get inode location %lu",
121 down_read(&EXT4_I(inode)->xattr_sem);
122 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
123 up_read(&EXT4_I(inode)->xattr_sem);
127 if (!max_inline_size)
130 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
134 * this function does not take xattr_sem, which is OK because it is
135 * currently only used in a code path coming form ext4_iget, before
136 * the new inode has been unlocked
138 int ext4_find_inline_data_nolock(struct inode *inode)
140 struct ext4_xattr_ibody_find is = {
141 .s = { .not_found = -ENODATA, },
143 struct ext4_xattr_info i = {
144 .name_index = EXT4_XATTR_INDEX_SYSTEM,
145 .name = EXT4_XATTR_SYSTEM_DATA,
149 if (EXT4_I(inode)->i_extra_isize == 0)
152 error = ext4_get_inode_loc(inode, &is.iloc);
156 error = ext4_xattr_ibody_find(inode, &i, &is);
160 if (!is.s.not_found) {
161 if (is.s.here->e_value_inum) {
162 EXT4_ERROR_INODE(inode, "inline data xattr refers "
163 "to an external xattr inode");
164 error = -EFSCORRUPTED;
167 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
168 (void *)ext4_raw_inode(&is.iloc));
169 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
170 le32_to_cpu(is.s.here->e_value_size);
177 static int ext4_read_inline_data(struct inode *inode, void *buffer,
179 struct ext4_iloc *iloc)
181 struct ext4_xattr_entry *entry;
182 struct ext4_xattr_ibody_header *header;
184 struct ext4_inode *raw_inode;
189 BUG_ON(len > EXT4_I(inode)->i_inline_size);
191 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
192 len : EXT4_MIN_INLINE_DATA_SIZE;
194 raw_inode = ext4_raw_inode(iloc);
195 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
203 header = IHDR(inode, raw_inode);
204 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
205 EXT4_I(inode)->i_inline_off);
206 len = min_t(unsigned int, len,
207 (unsigned int)le32_to_cpu(entry->e_value_size));
210 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
218 * write the buffer to the inline inode.
219 * If 'create' is set, we don't need to do the extra copy in the xattr
220 * value since it is already handled by ext4_xattr_ibody_set.
221 * That saves us one memcpy.
223 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
224 void *buffer, loff_t pos, unsigned int len)
226 struct ext4_xattr_entry *entry;
227 struct ext4_xattr_ibody_header *header;
228 struct ext4_inode *raw_inode;
231 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
234 BUG_ON(!EXT4_I(inode)->i_inline_off);
235 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
237 raw_inode = ext4_raw_inode(iloc);
240 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
241 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
242 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
243 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
253 pos -= EXT4_MIN_INLINE_DATA_SIZE;
254 header = IHDR(inode, raw_inode);
255 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
256 EXT4_I(inode)->i_inline_off);
258 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
262 static int ext4_create_inline_data(handle_t *handle,
263 struct inode *inode, unsigned len)
267 struct ext4_xattr_ibody_find is = {
268 .s = { .not_found = -ENODATA, },
270 struct ext4_xattr_info i = {
271 .name_index = EXT4_XATTR_INDEX_SYSTEM,
272 .name = EXT4_XATTR_SYSTEM_DATA,
275 error = ext4_get_inode_loc(inode, &is.iloc);
279 BUFFER_TRACE(is.iloc.bh, "get_write_access");
280 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
285 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
286 value = EXT4_ZERO_XATTR_VALUE;
287 len -= EXT4_MIN_INLINE_DATA_SIZE;
293 /* Insert the xttr entry. */
297 error = ext4_xattr_ibody_find(inode, &i, &is);
301 BUG_ON(!is.s.not_found);
303 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
305 if (error == -ENOSPC)
306 ext4_clear_inode_state(inode,
307 EXT4_STATE_MAY_INLINE_DATA);
311 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
312 0, EXT4_MIN_INLINE_DATA_SIZE);
314 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
315 (void *)ext4_raw_inode(&is.iloc));
316 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
317 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
318 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
320 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
327 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
332 struct ext4_xattr_ibody_find is = {
333 .s = { .not_found = -ENODATA, },
335 struct ext4_xattr_info i = {
336 .name_index = EXT4_XATTR_INDEX_SYSTEM,
337 .name = EXT4_XATTR_SYSTEM_DATA,
340 /* If the old space is ok, write the data directly. */
341 if (len <= EXT4_I(inode)->i_inline_size)
344 error = ext4_get_inode_loc(inode, &is.iloc);
348 error = ext4_xattr_ibody_find(inode, &i, &is);
352 BUG_ON(is.s.not_found);
354 len -= EXT4_MIN_INLINE_DATA_SIZE;
355 value = kzalloc(len, GFP_NOFS);
361 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
366 BUFFER_TRACE(is.iloc.bh, "get_write_access");
367 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
372 /* Update the xattr entry. */
376 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
380 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
381 (void *)ext4_raw_inode(&is.iloc));
382 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
383 le32_to_cpu(is.s.here->e_value_size);
384 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
386 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
394 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
397 int ret, size, no_expand;
398 struct ext4_inode_info *ei = EXT4_I(inode);
400 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
403 size = ext4_get_max_inline_size(inode);
407 ext4_write_lock_xattr(inode, &no_expand);
409 if (ei->i_inline_off)
410 ret = ext4_update_inline_data(handle, inode, len);
412 ret = ext4_create_inline_data(handle, inode, len);
414 ext4_write_unlock_xattr(inode, &no_expand);
418 static int ext4_destroy_inline_data_nolock(handle_t *handle,
421 struct ext4_inode_info *ei = EXT4_I(inode);
422 struct ext4_xattr_ibody_find is = {
423 .s = { .not_found = 0, },
425 struct ext4_xattr_info i = {
426 .name_index = EXT4_XATTR_INDEX_SYSTEM,
427 .name = EXT4_XATTR_SYSTEM_DATA,
433 if (!ei->i_inline_off)
436 error = ext4_get_inode_loc(inode, &is.iloc);
440 error = ext4_xattr_ibody_find(inode, &i, &is);
444 BUFFER_TRACE(is.iloc.bh, "get_write_access");
445 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
450 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
454 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
455 0, EXT4_MIN_INLINE_DATA_SIZE);
456 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
458 if (ext4_has_feature_extents(inode->i_sb)) {
459 if (S_ISDIR(inode->i_mode) ||
460 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
461 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
462 ext4_ext_tree_init(handle, inode);
465 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
468 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
470 EXT4_I(inode)->i_inline_off = 0;
471 EXT4_I(inode)->i_inline_size = 0;
472 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
475 if (error == -ENODATA)
480 static int ext4_read_inline_page(struct inode *inode, struct page *page)
485 struct ext4_iloc iloc;
487 BUG_ON(!PageLocked(page));
488 BUG_ON(!ext4_has_inline_data(inode));
491 if (!EXT4_I(inode)->i_inline_off) {
492 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
497 ret = ext4_get_inode_loc(inode, &iloc);
501 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
502 kaddr = kmap_atomic(page);
503 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
504 flush_dcache_page(page);
505 kunmap_atomic(kaddr);
506 zero_user_segment(page, len, PAGE_SIZE);
507 SetPageUptodate(page);
514 int ext4_readpage_inline(struct inode *inode, struct page *page)
518 down_read(&EXT4_I(inode)->xattr_sem);
519 if (!ext4_has_inline_data(inode)) {
520 up_read(&EXT4_I(inode)->xattr_sem);
525 * Current inline data can only exist in the 1st page,
526 * So for all the other pages, just set them uptodate.
529 ret = ext4_read_inline_page(inode, page);
530 else if (!PageUptodate(page)) {
531 zero_user_segment(page, 0, PAGE_SIZE);
532 SetPageUptodate(page);
535 up_read(&EXT4_I(inode)->xattr_sem);
538 return ret >= 0 ? 0 : ret;
541 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
545 int ret, needed_blocks, no_expand;
546 handle_t *handle = NULL;
547 int retries = 0, sem_held = 0;
548 struct page *page = NULL;
550 struct ext4_iloc iloc;
552 if (!ext4_has_inline_data(inode)) {
554 * clear the flag so that no new write
555 * will trap here again.
557 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
561 needed_blocks = ext4_writepage_trans_blocks(inode);
563 ret = ext4_get_inode_loc(inode, &iloc);
568 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
569 if (IS_ERR(handle)) {
570 ret = PTR_ERR(handle);
575 /* We cannot recurse into the filesystem as the transaction is already
577 flags |= AOP_FLAG_NOFS;
579 page = grab_cache_page_write_begin(mapping, 0, flags);
585 ext4_write_lock_xattr(inode, &no_expand);
587 /* If some one has already done this for us, just exit. */
588 if (!ext4_has_inline_data(inode)) {
594 to = ext4_get_inline_size(inode);
595 if (!PageUptodate(page)) {
596 ret = ext4_read_inline_page(inode, page);
601 ret = ext4_destroy_inline_data_nolock(handle, inode);
605 if (ext4_should_dioread_nolock(inode)) {
606 ret = __block_write_begin(page, from, to,
607 ext4_get_block_unwritten);
609 ret = __block_write_begin(page, from, to, ext4_get_block);
611 if (!ret && ext4_should_journal_data(inode)) {
612 ret = ext4_walk_page_buffers(handle, inode, page_buffers(page),
614 do_journal_get_write_access);
621 ext4_orphan_add(handle, inode);
622 ext4_write_unlock_xattr(inode, &no_expand);
624 ext4_journal_stop(handle);
626 ext4_truncate_failed_write(inode);
628 * If truncate failed early the inode might
629 * still be on the orphan list; we need to
630 * make sure the inode is removed from the
631 * orphan list in that case.
634 ext4_orphan_del(NULL, inode);
637 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
641 block_commit_write(page, from, to);
648 ext4_write_unlock_xattr(inode, &no_expand);
650 ext4_journal_stop(handle);
656 * Try to write data in the inode.
657 * If the inode has inline data, check whether the new write can be
658 * in the inode also. If not, create the page the handle, move the data
659 * to the page make it update and let the later codes create extent for it.
661 int ext4_try_to_write_inline_data(struct address_space *mapping,
663 loff_t pos, unsigned len,
670 struct ext4_iloc iloc;
672 if (pos + len > ext4_get_max_inline_size(inode))
675 ret = ext4_get_inode_loc(inode, &iloc);
680 * The possible write could happen in the inode,
681 * so try to reserve the space in inode first.
683 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
684 if (IS_ERR(handle)) {
685 ret = PTR_ERR(handle);
690 ret = ext4_prepare_inline_data(handle, inode, pos + len);
691 if (ret && ret != -ENOSPC)
694 /* We don't have space in inline inode, so convert it to extent. */
695 if (ret == -ENOSPC) {
696 ext4_journal_stop(handle);
701 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
706 flags |= AOP_FLAG_NOFS;
708 page = grab_cache_page_write_begin(mapping, 0, flags);
715 down_read(&EXT4_I(inode)->xattr_sem);
716 if (!ext4_has_inline_data(inode)) {
723 if (!PageUptodate(page)) {
724 ret = ext4_read_inline_page(inode, page);
735 up_read(&EXT4_I(inode)->xattr_sem);
737 if (handle && (ret != 1))
738 ext4_journal_stop(handle);
742 return ext4_convert_inline_data_to_extent(mapping,
746 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
747 unsigned copied, struct page *page)
749 handle_t *handle = ext4_journal_current_handle();
752 struct ext4_iloc iloc;
755 if (unlikely(copied < len) && !PageUptodate(page))
758 if (likely(copied)) {
759 ret = ext4_get_inode_loc(inode, &iloc);
763 ext4_std_error(inode->i_sb, ret);
766 ext4_write_lock_xattr(inode, &no_expand);
767 BUG_ON(!ext4_has_inline_data(inode));
770 * ei->i_inline_off may have changed since
771 * ext4_write_begin() called
772 * ext4_try_to_write_inline_data()
774 (void) ext4_find_inline_data_nolock(inode);
776 kaddr = kmap_atomic(page);
777 ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
778 kunmap_atomic(kaddr);
779 SetPageUptodate(page);
780 /* clear page dirty so that writepages wouldn't work for us. */
781 ClearPageDirty(page);
783 ext4_write_unlock_xattr(inode, &no_expand);
787 * It's important to update i_size while still holding page
788 * lock: page writeout could otherwise come in and zero
791 ext4_update_inode_size(inode, pos + copied);
797 * Don't mark the inode dirty under page lock. First, it unnecessarily
798 * makes the holding time of page lock longer. Second, it forces lock
799 * ordering of page lock and transaction start for journaling
803 mark_inode_dirty(inode);
806 * If we didn't copy as much data as expected, we need to trim back
807 * size of xattr containing inline data.
809 if (pos + len > inode->i_size && ext4_can_truncate(inode))
810 ext4_orphan_add(handle, inode);
812 ret2 = ext4_journal_stop(handle);
815 if (pos + len > inode->i_size) {
816 ext4_truncate_failed_write(inode);
818 * If truncate failed early the inode might still be
819 * on the orphan list; we need to make sure the inode
820 * is removed from the orphan list in that case.
823 ext4_orphan_del(NULL, inode);
825 return ret ? ret : copied;
829 ext4_journalled_write_inline_data(struct inode *inode,
835 struct ext4_iloc iloc;
837 ret = ext4_get_inode_loc(inode, &iloc);
839 ext4_std_error(inode->i_sb, ret);
843 ext4_write_lock_xattr(inode, &no_expand);
844 kaddr = kmap_atomic(page);
845 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
846 kunmap_atomic(kaddr);
847 ext4_write_unlock_xattr(inode, &no_expand);
853 * Try to make the page cache and handle ready for the inline data case.
854 * We can call this function in 2 cases:
855 * 1. The inode is created and the first write exceeds inline size. We can
856 * clear the inode state safely.
857 * 2. The inode has inline data, then we need to read the data, make it
858 * update and dirty so that ext4_da_writepages can handle it. We don't
859 * need to start the journal since the file's metadata isn't changed now.
861 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
866 int ret = 0, inline_size;
869 page = grab_cache_page_write_begin(mapping, 0, flags);
873 down_read(&EXT4_I(inode)->xattr_sem);
874 if (!ext4_has_inline_data(inode)) {
875 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
879 inline_size = ext4_get_inline_size(inode);
881 if (!PageUptodate(page)) {
882 ret = ext4_read_inline_page(inode, page);
887 ret = __block_write_begin(page, 0, inline_size,
888 ext4_da_get_block_prep);
890 up_read(&EXT4_I(inode)->xattr_sem);
893 ext4_truncate_failed_write(inode);
898 SetPageUptodate(page);
899 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
900 *fsdata = (void *)CONVERT_INLINE_DATA;
903 up_read(&EXT4_I(inode)->xattr_sem);
912 * Prepare the write for the inline data.
913 * If the data can be written into the inode, we just read
914 * the page and make it uptodate, and start the journal.
915 * Otherwise read the page, makes it dirty so that it can be
916 * handle in writepages(the i_disksize update is left to the
917 * normal ext4_da_write_end).
919 int ext4_da_write_inline_data_begin(struct address_space *mapping,
921 loff_t pos, unsigned len,
926 int ret, inline_size;
929 struct ext4_iloc iloc;
932 ret = ext4_get_inode_loc(inode, &iloc);
937 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
938 if (IS_ERR(handle)) {
939 ret = PTR_ERR(handle);
943 inline_size = ext4_get_max_inline_size(inode);
946 if (inline_size >= pos + len) {
947 ret = ext4_prepare_inline_data(handle, inode, pos + len);
948 if (ret && ret != -ENOSPC)
953 * We cannot recurse into the filesystem as the transaction
954 * is already started.
956 flags |= AOP_FLAG_NOFS;
958 if (ret == -ENOSPC) {
959 ext4_journal_stop(handle);
960 ret = ext4_da_convert_inline_data_to_extent(mapping,
964 if (ret == -ENOSPC &&
965 ext4_should_retry_alloc(inode->i_sb, &retries))
970 page = grab_cache_page_write_begin(mapping, 0, flags);
976 down_read(&EXT4_I(inode)->xattr_sem);
977 if (!ext4_has_inline_data(inode)) {
979 goto out_release_page;
982 if (!PageUptodate(page)) {
983 ret = ext4_read_inline_page(inode, page);
985 goto out_release_page;
987 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
990 goto out_release_page;
992 up_read(&EXT4_I(inode)->xattr_sem);
997 up_read(&EXT4_I(inode)->xattr_sem);
1001 ext4_journal_stop(handle);
1007 #ifdef INLINE_DIR_DEBUG
1008 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
1009 void *inline_start, int inline_size)
1012 unsigned short de_len;
1013 struct ext4_dir_entry_2 *de = inline_start;
1014 void *dlimit = inline_start + inline_size;
1016 trace_printk("inode %lu\n", dir->i_ino);
1018 while ((void *)de < dlimit) {
1019 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1020 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1021 offset, de_len, de->name_len, de->name,
1022 de->name_len, le32_to_cpu(de->inode));
1023 if (ext4_check_dir_entry(dir, NULL, de, bh,
1024 inline_start, inline_size, offset))
1028 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1032 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1036 * Add a new entry into a inline dir.
1037 * It will return -ENOSPC if no space is available, and -EIO
1038 * and -EEXIST if directory entry already exists.
1040 static int ext4_add_dirent_to_inline(handle_t *handle,
1041 struct ext4_filename *fname,
1043 struct inode *inode,
1044 struct ext4_iloc *iloc,
1045 void *inline_start, int inline_size)
1048 struct ext4_dir_entry_2 *de;
1050 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1051 inline_size, fname, &de);
1055 BUFFER_TRACE(iloc->bh, "get_write_access");
1056 err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
1060 ext4_insert_dentry(dir, inode, de, inline_size, fname);
1062 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1065 * XXX shouldn't update any times until successful
1066 * completion of syscall, but too many callers depend
1069 * XXX similarly, too many callers depend on
1070 * ext4_new_inode() setting the times, but error
1071 * recovery deletes the inode, so the worst that can
1072 * happen is that the times are slightly out of date
1073 * and/or different from the directory change time.
1075 dir->i_mtime = dir->i_ctime = current_time(dir);
1076 ext4_update_dx_flag(dir);
1077 inode_inc_iversion(dir);
1081 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1082 struct ext4_iloc *iloc)
1084 struct ext4_xattr_entry *entry;
1085 struct ext4_xattr_ibody_header *header;
1087 BUG_ON(!EXT4_I(inode)->i_inline_off);
1089 header = IHDR(inode, ext4_raw_inode(iloc));
1090 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1091 EXT4_I(inode)->i_inline_off);
1093 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1096 /* Set the final de to cover the whole block. */
1097 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1099 struct ext4_dir_entry_2 *de, *prev_de;
1103 de = (struct ext4_dir_entry_2 *)de_buf;
1105 limit = de_buf + old_size;
1108 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1110 de = (struct ext4_dir_entry_2 *)de_buf;
1111 } while (de_buf < limit);
1113 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1114 old_size, new_size);
1116 /* this is just created, so create an empty entry. */
1118 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1122 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1123 struct ext4_iloc *iloc)
1126 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1127 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1129 if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
1132 ret = ext4_update_inline_data(handle, dir,
1133 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1137 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1138 EXT4_I(dir)->i_inline_size -
1139 EXT4_MIN_INLINE_DATA_SIZE);
1140 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1144 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1145 struct ext4_iloc *iloc,
1146 void *buf, int inline_size)
1150 ret = ext4_create_inline_data(handle, inode, inline_size);
1152 ext4_msg(inode->i_sb, KERN_EMERG,
1153 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1157 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1158 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1161 static int ext4_finish_convert_inline_dir(handle_t *handle,
1162 struct inode *inode,
1163 struct buffer_head *dir_block,
1167 int err, csum_size = 0, header_size = 0;
1168 struct ext4_dir_entry_2 *de;
1169 void *target = dir_block->b_data;
1172 * First create "." and ".." and then copy the dir information
1173 * back to the block.
1175 de = (struct ext4_dir_entry_2 *)target;
1176 de = ext4_init_dot_dotdot(inode, de,
1177 inode->i_sb->s_blocksize, csum_size,
1178 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1179 header_size = (void *)de - target;
1181 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1182 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1184 if (ext4_has_metadata_csum(inode->i_sb))
1185 csum_size = sizeof(struct ext4_dir_entry_tail);
1187 inode->i_size = inode->i_sb->s_blocksize;
1188 i_size_write(inode, inode->i_sb->s_blocksize);
1189 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1190 ext4_update_final_de(dir_block->b_data,
1191 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1192 inode->i_sb->s_blocksize - csum_size);
1195 ext4_initialize_dirent_tail(dir_block,
1196 inode->i_sb->s_blocksize);
1197 set_buffer_uptodate(dir_block);
1198 unlock_buffer(dir_block);
1199 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
1202 set_buffer_verified(dir_block);
1203 return ext4_mark_inode_dirty(handle, inode);
1206 static int ext4_convert_inline_data_nolock(handle_t *handle,
1207 struct inode *inode,
1208 struct ext4_iloc *iloc)
1212 struct buffer_head *data_bh = NULL;
1213 struct ext4_map_blocks map;
1216 inline_size = ext4_get_inline_size(inode);
1217 buf = kmalloc(inline_size, GFP_NOFS);
1223 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1228 * Make sure the inline directory entries pass checks before we try to
1229 * convert them, so that we avoid touching stuff that needs fsck.
1231 if (S_ISDIR(inode->i_mode)) {
1232 error = ext4_check_all_de(inode, iloc->bh,
1233 buf + EXT4_INLINE_DOTDOT_SIZE,
1234 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1239 error = ext4_destroy_inline_data_nolock(handle, inode);
1246 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1249 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1254 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1260 lock_buffer(data_bh);
1261 error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1264 unlock_buffer(data_bh);
1268 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1270 if (!S_ISDIR(inode->i_mode)) {
1271 memcpy(data_bh->b_data, buf, inline_size);
1272 set_buffer_uptodate(data_bh);
1273 unlock_buffer(data_bh);
1274 error = ext4_handle_dirty_metadata(handle,
1277 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1283 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1292 * Try to add the new entry to the inline data.
1293 * If succeeds, return 0. If not, extended the inline dir and copied data to
1294 * the new created block.
1296 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1297 struct inode *dir, struct inode *inode)
1299 int ret, ret2, inline_size, no_expand;
1301 struct ext4_iloc iloc;
1303 ret = ext4_get_inode_loc(dir, &iloc);
1307 ext4_write_lock_xattr(dir, &no_expand);
1308 if (!ext4_has_inline_data(dir))
1311 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1312 EXT4_INLINE_DOTDOT_SIZE;
1313 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1315 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1316 inline_start, inline_size);
1320 /* check whether it can be inserted to inline xattr space. */
1321 inline_size = EXT4_I(dir)->i_inline_size -
1322 EXT4_MIN_INLINE_DATA_SIZE;
1324 /* Try to use the xattr space.*/
1325 ret = ext4_update_inline_dir(handle, dir, &iloc);
1326 if (ret && ret != -ENOSPC)
1329 inline_size = EXT4_I(dir)->i_inline_size -
1330 EXT4_MIN_INLINE_DATA_SIZE;
1334 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1336 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1337 inode, &iloc, inline_start,
1345 * The inline space is filled up, so create a new block for it.
1346 * As the extent tree will be created, we have to save the inline
1349 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1352 ext4_write_unlock_xattr(dir, &no_expand);
1353 ret2 = ext4_mark_inode_dirty(handle, dir);
1354 if (unlikely(ret2 && !ret))
1361 * This function fills a red-black tree with information from an
1362 * inlined dir. It returns the number directory entries loaded
1363 * into the tree. If there is an error it is returned in err.
1365 int ext4_inlinedir_to_tree(struct file *dir_file,
1366 struct inode *dir, ext4_lblk_t block,
1367 struct dx_hash_info *hinfo,
1368 __u32 start_hash, __u32 start_minor_hash,
1369 int *has_inline_data)
1371 int err = 0, count = 0;
1372 unsigned int parent_ino;
1374 struct ext4_dir_entry_2 *de;
1375 struct inode *inode = file_inode(dir_file);
1376 int ret, inline_size = 0;
1377 struct ext4_iloc iloc;
1378 void *dir_buf = NULL;
1379 struct ext4_dir_entry_2 fake;
1380 struct fscrypt_str tmp_str;
1382 ret = ext4_get_inode_loc(inode, &iloc);
1386 down_read(&EXT4_I(inode)->xattr_sem);
1387 if (!ext4_has_inline_data(inode)) {
1388 up_read(&EXT4_I(inode)->xattr_sem);
1389 *has_inline_data = 0;
1393 inline_size = ext4_get_inline_size(inode);
1394 dir_buf = kmalloc(inline_size, GFP_NOFS);
1397 up_read(&EXT4_I(inode)->xattr_sem);
1401 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1402 up_read(&EXT4_I(inode)->xattr_sem);
1407 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1408 while (pos < inline_size) {
1410 * As inlined dir doesn't store any information about '.' and
1411 * only the inode number of '..' is stored, we have to handle
1415 fake.inode = cpu_to_le32(inode->i_ino);
1417 strcpy(fake.name, ".");
1418 fake.rec_len = ext4_rec_len_to_disk(
1419 ext4_dir_rec_len(fake.name_len, NULL),
1421 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1423 pos = EXT4_INLINE_DOTDOT_OFFSET;
1424 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1425 fake.inode = cpu_to_le32(parent_ino);
1427 strcpy(fake.name, "..");
1428 fake.rec_len = ext4_rec_len_to_disk(
1429 ext4_dir_rec_len(fake.name_len, NULL),
1431 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1433 pos = EXT4_INLINE_DOTDOT_SIZE;
1435 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1436 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1437 if (ext4_check_dir_entry(inode, dir_file, de,
1439 inline_size, pos)) {
1445 if (ext4_hash_in_dirent(dir)) {
1446 hinfo->hash = EXT4_DIRENT_HASH(de);
1447 hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1449 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1451 if ((hinfo->hash < start_hash) ||
1452 ((hinfo->hash == start_hash) &&
1453 (hinfo->minor_hash < start_minor_hash)))
1457 tmp_str.name = de->name;
1458 tmp_str.len = de->name_len;
1459 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1460 hinfo->minor_hash, de, &tmp_str);
1475 * So this function is called when the volume is mkfsed with
1476 * dir_index disabled. In order to keep f_pos persistent
1477 * after we convert from an inlined dir to a blocked based,
1478 * we just pretend that we are a normal dir and return the
1479 * offset as if '.' and '..' really take place.
1482 int ext4_read_inline_dir(struct file *file,
1483 struct dir_context *ctx,
1484 int *has_inline_data)
1486 unsigned int offset, parent_ino;
1488 struct ext4_dir_entry_2 *de;
1489 struct super_block *sb;
1490 struct inode *inode = file_inode(file);
1491 int ret, inline_size = 0;
1492 struct ext4_iloc iloc;
1493 void *dir_buf = NULL;
1494 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1496 ret = ext4_get_inode_loc(inode, &iloc);
1500 down_read(&EXT4_I(inode)->xattr_sem);
1501 if (!ext4_has_inline_data(inode)) {
1502 up_read(&EXT4_I(inode)->xattr_sem);
1503 *has_inline_data = 0;
1507 inline_size = ext4_get_inline_size(inode);
1508 dir_buf = kmalloc(inline_size, GFP_NOFS);
1511 up_read(&EXT4_I(inode)->xattr_sem);
1515 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1516 up_read(&EXT4_I(inode)->xattr_sem);
1522 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1526 * dotdot_offset and dotdot_size is the real offset and
1527 * size for ".." and "." if the dir is block based while
1528 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1529 * So we will use extra_offset and extra_size to indicate them
1530 * during the inline dir iteration.
1532 dotdot_offset = ext4_dir_rec_len(1, NULL);
1533 dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1534 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1535 extra_size = extra_offset + inline_size;
1538 * If the version has changed since the last call to
1539 * readdir(2), then we might be pointing to an invalid
1540 * dirent right now. Scan from the start of the inline
1543 if (!inode_eq_iversion(inode, file->f_version)) {
1544 for (i = 0; i < extra_size && i < offset;) {
1546 * "." is with offset 0 and
1547 * ".." is dotdot_offset.
1552 } else if (i == dotdot_offset) {
1556 /* for other entry, the real offset in
1557 * the buf has to be tuned accordingly.
1559 de = (struct ext4_dir_entry_2 *)
1560 (dir_buf + i - extra_offset);
1561 /* It's too expensive to do a full
1562 * dirent test each time round this
1563 * loop, but we do have to test at
1564 * least that it is non-zero. A
1565 * failure will be detected in the
1566 * dirent test below. */
1567 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1568 < ext4_dir_rec_len(1, NULL))
1570 i += ext4_rec_len_from_disk(de->rec_len,
1575 file->f_version = inode_query_iversion(inode);
1578 while (ctx->pos < extra_size) {
1579 if (ctx->pos == 0) {
1580 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1582 ctx->pos = dotdot_offset;
1586 if (ctx->pos == dotdot_offset) {
1587 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1589 ctx->pos = dotdot_size;
1593 de = (struct ext4_dir_entry_2 *)
1594 (dir_buf + ctx->pos - extra_offset);
1595 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1596 extra_size, ctx->pos))
1598 if (le32_to_cpu(de->inode)) {
1599 if (!dir_emit(ctx, de->name, de->name_len,
1600 le32_to_cpu(de->inode),
1601 get_dtype(sb, de->file_type)))
1604 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1612 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1613 struct ext4_dir_entry_2 **parent_de,
1616 struct ext4_iloc iloc;
1618 *retval = ext4_get_inode_loc(inode, &iloc);
1622 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1628 * Try to create the inline data for the new dir.
1629 * If it succeeds, return 0, otherwise return the error.
1630 * In case of ENOSPC, the caller should create the normal disk layout dir.
1632 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1633 struct inode *inode)
1635 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1636 struct ext4_iloc iloc;
1637 struct ext4_dir_entry_2 *de;
1639 ret = ext4_get_inode_loc(inode, &iloc);
1643 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1648 * For inline dir, we only save the inode information for the ".."
1649 * and create a fake dentry to cover the left space.
1651 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1652 de->inode = cpu_to_le32(parent->i_ino);
1653 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1655 de->rec_len = ext4_rec_len_to_disk(
1656 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1658 set_nlink(inode, 2);
1659 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1665 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1666 struct ext4_filename *fname,
1667 struct ext4_dir_entry_2 **res_dir,
1668 int *has_inline_data)
1671 struct ext4_iloc iloc;
1675 if (ext4_get_inode_loc(dir, &iloc))
1678 down_read(&EXT4_I(dir)->xattr_sem);
1679 if (!ext4_has_inline_data(dir)) {
1680 *has_inline_data = 0;
1684 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1685 EXT4_INLINE_DOTDOT_SIZE;
1686 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1687 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1688 dir, fname, 0, res_dir);
1694 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1697 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1698 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1700 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1701 dir, fname, 0, res_dir);
1709 up_read(&EXT4_I(dir)->xattr_sem);
1713 int ext4_delete_inline_entry(handle_t *handle,
1715 struct ext4_dir_entry_2 *de_del,
1716 struct buffer_head *bh,
1717 int *has_inline_data)
1719 int err, inline_size, no_expand;
1720 struct ext4_iloc iloc;
1723 err = ext4_get_inode_loc(dir, &iloc);
1727 ext4_write_lock_xattr(dir, &no_expand);
1728 if (!ext4_has_inline_data(dir)) {
1729 *has_inline_data = 0;
1733 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1734 EXT4_MIN_INLINE_DATA_SIZE) {
1735 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1736 EXT4_INLINE_DOTDOT_SIZE;
1737 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1738 EXT4_INLINE_DOTDOT_SIZE;
1740 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1741 inline_size = ext4_get_inline_size(dir) -
1742 EXT4_MIN_INLINE_DATA_SIZE;
1745 BUFFER_TRACE(bh, "get_write_access");
1746 err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1751 err = ext4_generic_delete_entry(dir, de_del, bh,
1752 inline_start, inline_size, 0);
1756 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1758 ext4_write_unlock_xattr(dir, &no_expand);
1759 if (likely(err == 0))
1760 err = ext4_mark_inode_dirty(handle, dir);
1763 ext4_std_error(dir->i_sb, err);
1768 * Get the inline dentry at offset.
1770 static inline struct ext4_dir_entry_2 *
1771 ext4_get_inline_entry(struct inode *inode,
1772 struct ext4_iloc *iloc,
1773 unsigned int offset,
1774 void **inline_start,
1779 BUG_ON(offset > ext4_get_inline_size(inode));
1781 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1782 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1783 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1785 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1786 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1787 *inline_size = ext4_get_inline_size(inode) -
1788 EXT4_MIN_INLINE_DATA_SIZE;
1792 *inline_start = inline_pos;
1793 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1796 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1798 int err, inline_size;
1799 struct ext4_iloc iloc;
1802 unsigned int offset;
1803 struct ext4_dir_entry_2 *de;
1806 err = ext4_get_inode_loc(dir, &iloc);
1808 EXT4_ERROR_INODE_ERR(dir, -err,
1809 "error %d getting inode %lu block",
1814 down_read(&EXT4_I(dir)->xattr_sem);
1815 if (!ext4_has_inline_data(dir)) {
1816 *has_inline_data = 0;
1821 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1822 if (!le32_to_cpu(de->inode)) {
1823 ext4_warning(dir->i_sb,
1824 "bad inline directory (dir #%lu) - no `..'",
1829 inline_len = ext4_get_inline_size(dir);
1830 offset = EXT4_INLINE_DOTDOT_SIZE;
1831 while (offset < inline_len) {
1832 de = ext4_get_inline_entry(dir, &iloc, offset,
1833 &inline_pos, &inline_size);
1834 if (ext4_check_dir_entry(dir, NULL, de,
1835 iloc.bh, inline_pos,
1836 inline_size, offset)) {
1837 ext4_warning(dir->i_sb,
1838 "bad inline directory (dir #%lu) - "
1839 "inode %u, rec_len %u, name_len %d"
1841 dir->i_ino, le32_to_cpu(de->inode),
1842 le16_to_cpu(de->rec_len), de->name_len,
1846 if (le32_to_cpu(de->inode)) {
1849 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1854 up_read(&EXT4_I(dir)->xattr_sem);
1859 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1863 ext4_write_lock_xattr(inode, &no_expand);
1864 ret = ext4_destroy_inline_data_nolock(handle, inode);
1865 ext4_write_unlock_xattr(inode, &no_expand);
1870 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1873 int error = -EAGAIN;
1874 struct ext4_iloc iloc;
1876 down_read(&EXT4_I(inode)->xattr_sem);
1877 if (!ext4_has_inline_data(inode))
1880 error = ext4_get_inode_loc(inode, &iloc);
1884 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1885 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1886 addr += offsetof(struct ext4_inode, i_block);
1892 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1893 i_size_read(inode));
1894 iomap->type = IOMAP_INLINE;
1898 up_read(&EXT4_I(inode)->xattr_sem);
1902 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1905 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1908 struct ext4_xattr_ibody_find is = {
1909 .s = { .not_found = -ENODATA, },
1911 struct ext4_xattr_info i = {
1912 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1913 .name = EXT4_XATTR_SYSTEM_DATA,
1917 needed_blocks = ext4_writepage_trans_blocks(inode);
1918 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1920 return PTR_ERR(handle);
1922 ext4_write_lock_xattr(inode, &no_expand);
1923 if (!ext4_has_inline_data(inode)) {
1924 ext4_write_unlock_xattr(inode, &no_expand);
1926 ext4_journal_stop(handle);
1930 if ((err = ext4_orphan_add(handle, inode)) != 0)
1933 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1936 down_write(&EXT4_I(inode)->i_data_sem);
1937 i_size = inode->i_size;
1938 inline_size = ext4_get_inline_size(inode);
1939 EXT4_I(inode)->i_disksize = i_size;
1941 if (i_size < inline_size) {
1943 * if there's inline data to truncate and this file was
1944 * converted to extents after that inline data was written,
1945 * the extent status cache must be cleared to avoid leaving
1946 * behind stale delayed allocated extent entries
1948 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1950 err = ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1951 if (err == -ENOMEM) {
1953 congestion_wait(BLK_RW_ASYNC, HZ/50);
1960 /* Clear the content in the xattr space. */
1961 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1962 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1965 BUG_ON(is.s.not_found);
1967 value_len = le32_to_cpu(is.s.here->e_value_size);
1968 value = kmalloc(value_len, GFP_NOFS);
1974 err = ext4_xattr_ibody_get(inode, i.name_index,
1975 i.name, value, value_len);
1980 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1981 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1982 err = ext4_xattr_ibody_set(handle, inode, &i, &is);
1987 /* Clear the content within i_blocks. */
1988 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1989 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1990 memset(p + i_size, 0,
1991 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1994 EXT4_I(inode)->i_inline_size = i_size <
1995 EXT4_MIN_INLINE_DATA_SIZE ?
1996 EXT4_MIN_INLINE_DATA_SIZE : i_size;
2000 up_write(&EXT4_I(inode)->i_data_sem);
2003 ext4_write_unlock_xattr(inode, &no_expand);
2006 ext4_orphan_del(handle, inode);
2009 inode->i_mtime = inode->i_ctime = current_time(inode);
2010 err = ext4_mark_inode_dirty(handle, inode);
2012 ext4_handle_sync(handle);
2014 ext4_journal_stop(handle);
2018 int ext4_convert_inline_data(struct inode *inode)
2020 int error, needed_blocks, no_expand;
2022 struct ext4_iloc iloc;
2024 if (!ext4_has_inline_data(inode)) {
2025 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2027 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2029 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2030 * cleared. This means we are in the middle of moving of
2031 * inline data to delay allocated block. Just force writeout
2032 * here to finish conversion.
2034 error = filemap_flush(inode->i_mapping);
2037 if (!ext4_has_inline_data(inode))
2041 needed_blocks = ext4_writepage_trans_blocks(inode);
2044 error = ext4_get_inode_loc(inode, &iloc);
2048 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2049 if (IS_ERR(handle)) {
2050 error = PTR_ERR(handle);
2054 ext4_write_lock_xattr(inode, &no_expand);
2055 if (ext4_has_inline_data(inode))
2056 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2057 ext4_write_unlock_xattr(inode, &no_expand);
2058 ext4_journal_stop(handle);