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/namei.h>
10 #include <linux/iversion.h>
11 #include <linux/sched/mm.h>
13 #include "ext4_jbd2.h"
18 #define EXT4_XATTR_SYSTEM_DATA "data"
19 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
20 #define EXT4_INLINE_DOTDOT_OFFSET 2
21 #define EXT4_INLINE_DOTDOT_SIZE 4
23 static int ext4_get_inline_size(struct inode *inode)
25 if (EXT4_I(inode)->i_inline_off)
26 return EXT4_I(inode)->i_inline_size;
31 static int get_max_inline_xattr_value_size(struct inode *inode,
32 struct ext4_iloc *iloc)
34 struct ext4_xattr_ibody_header *header;
35 struct ext4_xattr_entry *entry;
36 struct ext4_inode *raw_inode;
40 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
43 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
44 EXT4_GOOD_OLD_INODE_SIZE -
45 EXT4_I(inode)->i_extra_isize -
46 sizeof(struct ext4_xattr_ibody_header);
49 * We need to subtract another sizeof(__u32) since an in-inode xattr
50 * needs an empty 4 bytes to indicate the gap between the xattr entry
51 * and the name/value pair.
53 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
54 return EXT4_XATTR_SIZE(min_offs -
55 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
56 EXT4_XATTR_ROUND - sizeof(__u32));
58 raw_inode = ext4_raw_inode(iloc);
59 header = IHDR(inode, raw_inode);
60 entry = IFIRST(header);
61 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
63 /* Compute min_offs. */
64 while (!IS_LAST_ENTRY(entry)) {
65 void *next = EXT4_XATTR_NEXT(entry);
68 EXT4_ERROR_INODE(inode,
69 "corrupt xattr in inline inode");
72 if (!entry->e_value_inum && entry->e_value_size) {
73 size_t offs = le16_to_cpu(entry->e_value_offs);
80 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
82 if (EXT4_I(inode)->i_inline_off) {
83 entry = (struct ext4_xattr_entry *)
84 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
86 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
90 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
92 if (free > EXT4_XATTR_ROUND)
93 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
102 * Get the maximum size we now can store in an inode.
103 * If we can't find the space for a xattr entry, don't use the space
104 * of the extents since we have no space to indicate the inline data.
106 int ext4_get_max_inline_size(struct inode *inode)
108 int error, max_inline_size;
109 struct ext4_iloc iloc;
111 if (EXT4_I(inode)->i_extra_isize == 0)
114 error = ext4_get_inode_loc(inode, &iloc);
116 ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
117 "can't get inode location %lu",
122 down_read(&EXT4_I(inode)->xattr_sem);
123 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
124 up_read(&EXT4_I(inode)->xattr_sem);
128 if (!max_inline_size)
131 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
135 * this function does not take xattr_sem, which is OK because it is
136 * currently only used in a code path coming form ext4_iget, before
137 * the new inode has been unlocked
139 int ext4_find_inline_data_nolock(struct inode *inode)
141 struct ext4_xattr_ibody_find is = {
142 .s = { .not_found = -ENODATA, },
144 struct ext4_xattr_info i = {
145 .name_index = EXT4_XATTR_INDEX_SYSTEM,
146 .name = EXT4_XATTR_SYSTEM_DATA,
150 if (EXT4_I(inode)->i_extra_isize == 0)
153 error = ext4_get_inode_loc(inode, &is.iloc);
157 error = ext4_xattr_ibody_find(inode, &i, &is);
161 if (!is.s.not_found) {
162 if (is.s.here->e_value_inum) {
163 EXT4_ERROR_INODE(inode, "inline data xattr refers "
164 "to an external xattr inode");
165 error = -EFSCORRUPTED;
168 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
169 (void *)ext4_raw_inode(&is.iloc));
170 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
171 le32_to_cpu(is.s.here->e_value_size);
178 static int ext4_read_inline_data(struct inode *inode, void *buffer,
180 struct ext4_iloc *iloc)
182 struct ext4_xattr_entry *entry;
183 struct ext4_xattr_ibody_header *header;
185 struct ext4_inode *raw_inode;
190 BUG_ON(len > EXT4_I(inode)->i_inline_size);
192 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
193 len : EXT4_MIN_INLINE_DATA_SIZE;
195 raw_inode = ext4_raw_inode(iloc);
196 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
204 header = IHDR(inode, raw_inode);
205 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
206 EXT4_I(inode)->i_inline_off);
207 len = min_t(unsigned int, len,
208 (unsigned int)le32_to_cpu(entry->e_value_size));
211 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
219 * write the buffer to the inline inode.
220 * If 'create' is set, we don't need to do the extra copy in the xattr
221 * value since it is already handled by ext4_xattr_ibody_set.
222 * That saves us one memcpy.
224 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
225 void *buffer, loff_t pos, unsigned int len)
227 struct ext4_xattr_entry *entry;
228 struct ext4_xattr_ibody_header *header;
229 struct ext4_inode *raw_inode;
232 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
235 BUG_ON(!EXT4_I(inode)->i_inline_off);
236 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
238 raw_inode = ext4_raw_inode(iloc);
241 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
242 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
243 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
244 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
254 pos -= EXT4_MIN_INLINE_DATA_SIZE;
255 header = IHDR(inode, raw_inode);
256 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
257 EXT4_I(inode)->i_inline_off);
259 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
263 static int ext4_create_inline_data(handle_t *handle,
264 struct inode *inode, unsigned len)
268 struct ext4_xattr_ibody_find is = {
269 .s = { .not_found = -ENODATA, },
271 struct ext4_xattr_info i = {
272 .name_index = EXT4_XATTR_INDEX_SYSTEM,
273 .name = EXT4_XATTR_SYSTEM_DATA,
276 error = ext4_get_inode_loc(inode, &is.iloc);
280 BUFFER_TRACE(is.iloc.bh, "get_write_access");
281 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
286 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
287 value = EXT4_ZERO_XATTR_VALUE;
288 len -= EXT4_MIN_INLINE_DATA_SIZE;
294 /* Insert the xttr entry. */
298 error = ext4_xattr_ibody_find(inode, &i, &is);
302 BUG_ON(!is.s.not_found);
304 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
306 if (error == -ENOSPC)
307 ext4_clear_inode_state(inode,
308 EXT4_STATE_MAY_INLINE_DATA);
312 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
313 0, EXT4_MIN_INLINE_DATA_SIZE);
315 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
316 (void *)ext4_raw_inode(&is.iloc));
317 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
318 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
319 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
321 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
328 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
333 struct ext4_xattr_ibody_find is = {
334 .s = { .not_found = -ENODATA, },
336 struct ext4_xattr_info i = {
337 .name_index = EXT4_XATTR_INDEX_SYSTEM,
338 .name = EXT4_XATTR_SYSTEM_DATA,
341 /* If the old space is ok, write the data directly. */
342 if (len <= EXT4_I(inode)->i_inline_size)
345 error = ext4_get_inode_loc(inode, &is.iloc);
349 error = ext4_xattr_ibody_find(inode, &i, &is);
353 BUG_ON(is.s.not_found);
355 len -= EXT4_MIN_INLINE_DATA_SIZE;
356 value = kzalloc(len, GFP_NOFS);
362 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
367 BUFFER_TRACE(is.iloc.bh, "get_write_access");
368 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
373 /* Update the xattr entry. */
377 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
381 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
382 (void *)ext4_raw_inode(&is.iloc));
383 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
384 le32_to_cpu(is.s.here->e_value_size);
385 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
387 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
395 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
398 int ret, size, no_expand;
399 struct ext4_inode_info *ei = EXT4_I(inode);
401 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
404 size = ext4_get_max_inline_size(inode);
408 ext4_write_lock_xattr(inode, &no_expand);
410 if (ei->i_inline_off)
411 ret = ext4_update_inline_data(handle, inode, len);
413 ret = ext4_create_inline_data(handle, inode, len);
415 ext4_write_unlock_xattr(inode, &no_expand);
419 static int ext4_destroy_inline_data_nolock(handle_t *handle,
422 struct ext4_inode_info *ei = EXT4_I(inode);
423 struct ext4_xattr_ibody_find is = {
424 .s = { .not_found = 0, },
426 struct ext4_xattr_info i = {
427 .name_index = EXT4_XATTR_INDEX_SYSTEM,
428 .name = EXT4_XATTR_SYSTEM_DATA,
434 if (!ei->i_inline_off)
437 error = ext4_get_inode_loc(inode, &is.iloc);
441 error = ext4_xattr_ibody_find(inode, &i, &is);
445 BUFFER_TRACE(is.iloc.bh, "get_write_access");
446 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
451 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
455 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
456 0, EXT4_MIN_INLINE_DATA_SIZE);
457 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
459 if (ext4_has_feature_extents(inode->i_sb)) {
460 if (S_ISDIR(inode->i_mode) ||
461 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
462 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
463 ext4_ext_tree_init(handle, inode);
466 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
469 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
471 EXT4_I(inode)->i_inline_off = 0;
472 EXT4_I(inode)->i_inline_size = 0;
473 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
476 if (error == -ENODATA)
481 static int ext4_read_inline_page(struct inode *inode, struct page *page)
486 struct ext4_iloc iloc;
488 BUG_ON(!PageLocked(page));
489 BUG_ON(!ext4_has_inline_data(inode));
492 if (!EXT4_I(inode)->i_inline_off) {
493 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
498 ret = ext4_get_inode_loc(inode, &iloc);
502 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
503 kaddr = kmap_atomic(page);
504 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
505 flush_dcache_page(page);
506 kunmap_atomic(kaddr);
507 zero_user_segment(page, len, PAGE_SIZE);
508 SetPageUptodate(page);
515 int ext4_readpage_inline(struct inode *inode, struct page *page)
519 down_read(&EXT4_I(inode)->xattr_sem);
520 if (!ext4_has_inline_data(inode)) {
521 up_read(&EXT4_I(inode)->xattr_sem);
526 * Current inline data can only exist in the 1st page,
527 * So for all the other pages, just set them uptodate.
530 ret = ext4_read_inline_page(inode, page);
531 else if (!PageUptodate(page)) {
532 zero_user_segment(page, 0, PAGE_SIZE);
533 SetPageUptodate(page);
536 up_read(&EXT4_I(inode)->xattr_sem);
539 return ret >= 0 ? 0 : ret;
542 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;
551 struct ext4_iloc iloc;
553 if (!ext4_has_inline_data(inode)) {
555 * clear the flag so that no new write
556 * will trap here again.
558 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
562 needed_blocks = ext4_writepage_trans_blocks(inode);
564 ret = ext4_get_inode_loc(inode, &iloc);
569 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
570 if (IS_ERR(handle)) {
571 ret = PTR_ERR(handle);
576 /* We cannot recurse into the filesystem as the transaction is already
578 flags = memalloc_nofs_save();
579 page = grab_cache_page_write_begin(mapping, 0);
580 memalloc_nofs_restore(flags);
586 ext4_write_lock_xattr(inode, &no_expand);
588 /* If some one has already done this for us, just exit. */
589 if (!ext4_has_inline_data(inode)) {
595 to = ext4_get_inline_size(inode);
596 if (!PageUptodate(page)) {
597 ret = ext4_read_inline_page(inode, page);
602 ret = ext4_destroy_inline_data_nolock(handle, inode);
606 if (ext4_should_dioread_nolock(inode)) {
607 ret = __block_write_begin(page, from, to,
608 ext4_get_block_unwritten);
610 ret = __block_write_begin(page, from, to, ext4_get_block);
612 if (!ret && ext4_should_journal_data(inode)) {
613 ret = ext4_walk_page_buffers(handle, inode, page_buffers(page),
615 do_journal_get_write_access);
622 ext4_orphan_add(handle, inode);
623 ext4_write_unlock_xattr(inode, &no_expand);
625 ext4_journal_stop(handle);
627 ext4_truncate_failed_write(inode);
629 * If truncate failed early the inode might
630 * still be on the orphan list; we need to
631 * make sure the inode is removed from the
632 * orphan list in that case.
635 ext4_orphan_del(NULL, inode);
638 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
642 block_commit_write(page, from, to);
649 ext4_write_unlock_xattr(inode, &no_expand);
651 ext4_journal_stop(handle);
657 * Try to write data in the inode.
658 * If the inode has inline data, check whether the new write can be
659 * in the inode also. If not, create the page the handle, move the data
660 * to the page make it update and let the later codes create extent for it.
662 int ext4_try_to_write_inline_data(struct address_space *mapping,
664 loff_t pos, unsigned len,
671 struct ext4_iloc iloc;
673 if (pos + len > ext4_get_max_inline_size(inode))
676 ret = ext4_get_inode_loc(inode, &iloc);
681 * The possible write could happen in the inode,
682 * so try to reserve the space in inode first.
684 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
685 if (IS_ERR(handle)) {
686 ret = PTR_ERR(handle);
691 ret = ext4_prepare_inline_data(handle, inode, pos + len);
692 if (ret && ret != -ENOSPC)
695 /* We don't have space in inline inode, so convert it to extent. */
696 if (ret == -ENOSPC) {
697 ext4_journal_stop(handle);
702 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
707 flags = memalloc_nofs_save();
708 page = grab_cache_page_write_begin(mapping, 0);
709 memalloc_nofs_restore(flags);
716 down_read(&EXT4_I(inode)->xattr_sem);
717 if (!ext4_has_inline_data(inode)) {
724 if (!PageUptodate(page)) {
725 ret = ext4_read_inline_page(inode, page);
736 up_read(&EXT4_I(inode)->xattr_sem);
738 if (handle && (ret != 1))
739 ext4_journal_stop(handle);
743 return ext4_convert_inline_data_to_extent(mapping, inode);
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,
865 int ret = 0, inline_size;
868 page = grab_cache_page_write_begin(mapping, 0);
872 down_read(&EXT4_I(inode)->xattr_sem);
873 if (!ext4_has_inline_data(inode)) {
874 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
878 inline_size = ext4_get_inline_size(inode);
880 if (!PageUptodate(page)) {
881 ret = ext4_read_inline_page(inode, page);
886 ret = __block_write_begin(page, 0, inline_size,
887 ext4_da_get_block_prep);
889 up_read(&EXT4_I(inode)->xattr_sem);
892 ext4_truncate_failed_write(inode);
897 SetPageUptodate(page);
898 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
899 *fsdata = (void *)CONVERT_INLINE_DATA;
902 up_read(&EXT4_I(inode)->xattr_sem);
911 * Prepare the write for the inline data.
912 * If the data can be written into the inode, we just read
913 * the page and make it uptodate, and start the journal.
914 * Otherwise read the page, makes it dirty so that it can be
915 * handle in writepages(the i_disksize update is left to the
916 * normal ext4_da_write_end).
918 int ext4_da_write_inline_data_begin(struct address_space *mapping,
920 loff_t pos, unsigned len,
927 struct ext4_iloc iloc;
931 ret = ext4_get_inode_loc(inode, &iloc);
936 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
937 if (IS_ERR(handle)) {
938 ret = PTR_ERR(handle);
942 ret = ext4_prepare_inline_data(handle, inode, pos + len);
943 if (ret && ret != -ENOSPC)
946 if (ret == -ENOSPC) {
947 ext4_journal_stop(handle);
948 ret = ext4_da_convert_inline_data_to_extent(mapping,
951 if (ret == -ENOSPC &&
952 ext4_should_retry_alloc(inode->i_sb, &retries))
958 * We cannot recurse into the filesystem as the transaction
959 * is already started.
961 flags = memalloc_nofs_save();
962 page = grab_cache_page_write_begin(mapping, 0);
963 memalloc_nofs_restore(flags);
969 down_read(&EXT4_I(inode)->xattr_sem);
970 if (!ext4_has_inline_data(inode)) {
972 goto out_release_page;
975 if (!PageUptodate(page)) {
976 ret = ext4_read_inline_page(inode, page);
978 goto out_release_page;
980 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
983 goto out_release_page;
985 up_read(&EXT4_I(inode)->xattr_sem);
990 up_read(&EXT4_I(inode)->xattr_sem);
994 ext4_journal_stop(handle);
1000 #ifdef INLINE_DIR_DEBUG
1001 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
1002 void *inline_start, int inline_size)
1005 unsigned short de_len;
1006 struct ext4_dir_entry_2 *de = inline_start;
1007 void *dlimit = inline_start + inline_size;
1009 trace_printk("inode %lu\n", dir->i_ino);
1011 while ((void *)de < dlimit) {
1012 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1013 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1014 offset, de_len, de->name_len, de->name,
1015 de->name_len, le32_to_cpu(de->inode));
1016 if (ext4_check_dir_entry(dir, NULL, de, bh,
1017 inline_start, inline_size, offset))
1021 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1025 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1029 * Add a new entry into a inline dir.
1030 * It will return -ENOSPC if no space is available, and -EIO
1031 * and -EEXIST if directory entry already exists.
1033 static int ext4_add_dirent_to_inline(handle_t *handle,
1034 struct ext4_filename *fname,
1036 struct inode *inode,
1037 struct ext4_iloc *iloc,
1038 void *inline_start, int inline_size)
1041 struct ext4_dir_entry_2 *de;
1043 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1044 inline_size, fname, &de);
1048 BUFFER_TRACE(iloc->bh, "get_write_access");
1049 err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
1053 ext4_insert_dentry(dir, inode, de, inline_size, fname);
1055 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1058 * XXX shouldn't update any times until successful
1059 * completion of syscall, but too many callers depend
1062 * XXX similarly, too many callers depend on
1063 * ext4_new_inode() setting the times, but error
1064 * recovery deletes the inode, so the worst that can
1065 * happen is that the times are slightly out of date
1066 * and/or different from the directory change time.
1068 dir->i_mtime = dir->i_ctime = current_time(dir);
1069 ext4_update_dx_flag(dir);
1070 inode_inc_iversion(dir);
1074 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1075 struct ext4_iloc *iloc)
1077 struct ext4_xattr_entry *entry;
1078 struct ext4_xattr_ibody_header *header;
1080 BUG_ON(!EXT4_I(inode)->i_inline_off);
1082 header = IHDR(inode, ext4_raw_inode(iloc));
1083 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1084 EXT4_I(inode)->i_inline_off);
1086 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1089 /* Set the final de to cover the whole block. */
1090 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1092 struct ext4_dir_entry_2 *de, *prev_de;
1098 limit = de_buf + old_size;
1101 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1104 } while (de_buf < limit);
1106 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1107 old_size, new_size);
1109 /* this is just created, so create an empty entry. */
1111 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1115 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1116 struct ext4_iloc *iloc)
1119 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1120 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1122 if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
1125 ret = ext4_update_inline_data(handle, dir,
1126 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1130 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1131 EXT4_I(dir)->i_inline_size -
1132 EXT4_MIN_INLINE_DATA_SIZE);
1133 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1137 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1138 struct ext4_iloc *iloc,
1139 void *buf, int inline_size)
1143 ret = ext4_create_inline_data(handle, inode, inline_size);
1145 ext4_msg(inode->i_sb, KERN_EMERG,
1146 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1150 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1151 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1154 static int ext4_finish_convert_inline_dir(handle_t *handle,
1155 struct inode *inode,
1156 struct buffer_head *dir_block,
1160 int err, csum_size = 0, header_size = 0;
1161 struct ext4_dir_entry_2 *de;
1162 void *target = dir_block->b_data;
1165 * First create "." and ".." and then copy the dir information
1166 * back to the block.
1169 de = ext4_init_dot_dotdot(inode, de,
1170 inode->i_sb->s_blocksize, csum_size,
1171 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1172 header_size = (void *)de - target;
1174 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1175 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1177 if (ext4_has_metadata_csum(inode->i_sb))
1178 csum_size = sizeof(struct ext4_dir_entry_tail);
1180 inode->i_size = inode->i_sb->s_blocksize;
1181 i_size_write(inode, inode->i_sb->s_blocksize);
1182 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1183 ext4_update_final_de(dir_block->b_data,
1184 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1185 inode->i_sb->s_blocksize - csum_size);
1188 ext4_initialize_dirent_tail(dir_block,
1189 inode->i_sb->s_blocksize);
1190 set_buffer_uptodate(dir_block);
1191 unlock_buffer(dir_block);
1192 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
1195 set_buffer_verified(dir_block);
1196 return ext4_mark_inode_dirty(handle, inode);
1199 static int ext4_convert_inline_data_nolock(handle_t *handle,
1200 struct inode *inode,
1201 struct ext4_iloc *iloc)
1205 struct buffer_head *data_bh = NULL;
1206 struct ext4_map_blocks map;
1209 inline_size = ext4_get_inline_size(inode);
1210 buf = kmalloc(inline_size, GFP_NOFS);
1216 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1221 * Make sure the inline directory entries pass checks before we try to
1222 * convert them, so that we avoid touching stuff that needs fsck.
1224 if (S_ISDIR(inode->i_mode)) {
1225 error = ext4_check_all_de(inode, iloc->bh,
1226 buf + EXT4_INLINE_DOTDOT_SIZE,
1227 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1232 error = ext4_destroy_inline_data_nolock(handle, inode);
1239 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1242 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1247 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1253 lock_buffer(data_bh);
1254 error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1257 unlock_buffer(data_bh);
1261 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1263 if (!S_ISDIR(inode->i_mode)) {
1264 memcpy(data_bh->b_data, buf, inline_size);
1265 set_buffer_uptodate(data_bh);
1266 unlock_buffer(data_bh);
1267 error = ext4_handle_dirty_metadata(handle,
1270 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1276 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1285 * Try to add the new entry to the inline data.
1286 * If succeeds, return 0. If not, extended the inline dir and copied data to
1287 * the new created block.
1289 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1290 struct inode *dir, struct inode *inode)
1292 int ret, ret2, inline_size, no_expand;
1294 struct ext4_iloc iloc;
1296 ret = ext4_get_inode_loc(dir, &iloc);
1300 ext4_write_lock_xattr(dir, &no_expand);
1301 if (!ext4_has_inline_data(dir))
1304 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1305 EXT4_INLINE_DOTDOT_SIZE;
1306 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1308 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1309 inline_start, inline_size);
1313 /* check whether it can be inserted to inline xattr space. */
1314 inline_size = EXT4_I(dir)->i_inline_size -
1315 EXT4_MIN_INLINE_DATA_SIZE;
1317 /* Try to use the xattr space.*/
1318 ret = ext4_update_inline_dir(handle, dir, &iloc);
1319 if (ret && ret != -ENOSPC)
1322 inline_size = EXT4_I(dir)->i_inline_size -
1323 EXT4_MIN_INLINE_DATA_SIZE;
1327 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1329 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1330 inode, &iloc, inline_start,
1338 * The inline space is filled up, so create a new block for it.
1339 * As the extent tree will be created, we have to save the inline
1342 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1345 ext4_write_unlock_xattr(dir, &no_expand);
1346 ret2 = ext4_mark_inode_dirty(handle, dir);
1347 if (unlikely(ret2 && !ret))
1354 * This function fills a red-black tree with information from an
1355 * inlined dir. It returns the number directory entries loaded
1356 * into the tree. If there is an error it is returned in err.
1358 int ext4_inlinedir_to_tree(struct file *dir_file,
1359 struct inode *dir, ext4_lblk_t block,
1360 struct dx_hash_info *hinfo,
1361 __u32 start_hash, __u32 start_minor_hash,
1362 int *has_inline_data)
1364 int err = 0, count = 0;
1365 unsigned int parent_ino;
1367 struct ext4_dir_entry_2 *de;
1368 struct inode *inode = file_inode(dir_file);
1369 int ret, inline_size = 0;
1370 struct ext4_iloc iloc;
1371 void *dir_buf = NULL;
1372 struct ext4_dir_entry_2 fake;
1373 struct fscrypt_str tmp_str;
1375 ret = ext4_get_inode_loc(inode, &iloc);
1379 down_read(&EXT4_I(inode)->xattr_sem);
1380 if (!ext4_has_inline_data(inode)) {
1381 up_read(&EXT4_I(inode)->xattr_sem);
1382 *has_inline_data = 0;
1386 inline_size = ext4_get_inline_size(inode);
1387 dir_buf = kmalloc(inline_size, GFP_NOFS);
1390 up_read(&EXT4_I(inode)->xattr_sem);
1394 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1395 up_read(&EXT4_I(inode)->xattr_sem);
1400 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1401 while (pos < inline_size) {
1403 * As inlined dir doesn't store any information about '.' and
1404 * only the inode number of '..' is stored, we have to handle
1408 fake.inode = cpu_to_le32(inode->i_ino);
1410 strcpy(fake.name, ".");
1411 fake.rec_len = ext4_rec_len_to_disk(
1412 ext4_dir_rec_len(fake.name_len, NULL),
1414 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1416 pos = EXT4_INLINE_DOTDOT_OFFSET;
1417 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1418 fake.inode = cpu_to_le32(parent_ino);
1420 strcpy(fake.name, "..");
1421 fake.rec_len = ext4_rec_len_to_disk(
1422 ext4_dir_rec_len(fake.name_len, NULL),
1424 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1426 pos = EXT4_INLINE_DOTDOT_SIZE;
1428 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1429 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1430 if (ext4_check_dir_entry(inode, dir_file, de,
1432 inline_size, pos)) {
1438 if (ext4_hash_in_dirent(dir)) {
1439 hinfo->hash = EXT4_DIRENT_HASH(de);
1440 hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1442 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1444 if ((hinfo->hash < start_hash) ||
1445 ((hinfo->hash == start_hash) &&
1446 (hinfo->minor_hash < start_minor_hash)))
1450 tmp_str.name = de->name;
1451 tmp_str.len = de->name_len;
1452 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1453 hinfo->minor_hash, de, &tmp_str);
1468 * So this function is called when the volume is mkfsed with
1469 * dir_index disabled. In order to keep f_pos persistent
1470 * after we convert from an inlined dir to a blocked based,
1471 * we just pretend that we are a normal dir and return the
1472 * offset as if '.' and '..' really take place.
1475 int ext4_read_inline_dir(struct file *file,
1476 struct dir_context *ctx,
1477 int *has_inline_data)
1479 unsigned int offset, parent_ino;
1481 struct ext4_dir_entry_2 *de;
1482 struct super_block *sb;
1483 struct inode *inode = file_inode(file);
1484 int ret, inline_size = 0;
1485 struct ext4_iloc iloc;
1486 void *dir_buf = NULL;
1487 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1489 ret = ext4_get_inode_loc(inode, &iloc);
1493 down_read(&EXT4_I(inode)->xattr_sem);
1494 if (!ext4_has_inline_data(inode)) {
1495 up_read(&EXT4_I(inode)->xattr_sem);
1496 *has_inline_data = 0;
1500 inline_size = ext4_get_inline_size(inode);
1501 dir_buf = kmalloc(inline_size, GFP_NOFS);
1504 up_read(&EXT4_I(inode)->xattr_sem);
1508 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1509 up_read(&EXT4_I(inode)->xattr_sem);
1515 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1519 * dotdot_offset and dotdot_size is the real offset and
1520 * size for ".." and "." if the dir is block based while
1521 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1522 * So we will use extra_offset and extra_size to indicate them
1523 * during the inline dir iteration.
1525 dotdot_offset = ext4_dir_rec_len(1, NULL);
1526 dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1527 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1528 extra_size = extra_offset + inline_size;
1531 * If the version has changed since the last call to
1532 * readdir(2), then we might be pointing to an invalid
1533 * dirent right now. Scan from the start of the inline
1536 if (!inode_eq_iversion(inode, file->f_version)) {
1537 for (i = 0; i < extra_size && i < offset;) {
1539 * "." is with offset 0 and
1540 * ".." is dotdot_offset.
1545 } else if (i == dotdot_offset) {
1549 /* for other entry, the real offset in
1550 * the buf has to be tuned accordingly.
1552 de = (struct ext4_dir_entry_2 *)
1553 (dir_buf + i - extra_offset);
1554 /* It's too expensive to do a full
1555 * dirent test each time round this
1556 * loop, but we do have to test at
1557 * least that it is non-zero. A
1558 * failure will be detected in the
1559 * dirent test below. */
1560 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1561 < ext4_dir_rec_len(1, NULL))
1563 i += ext4_rec_len_from_disk(de->rec_len,
1568 file->f_version = inode_query_iversion(inode);
1571 while (ctx->pos < extra_size) {
1572 if (ctx->pos == 0) {
1573 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1575 ctx->pos = dotdot_offset;
1579 if (ctx->pos == dotdot_offset) {
1580 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1582 ctx->pos = dotdot_size;
1586 de = (struct ext4_dir_entry_2 *)
1587 (dir_buf + ctx->pos - extra_offset);
1588 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1589 extra_size, ctx->pos))
1591 if (le32_to_cpu(de->inode)) {
1592 if (!dir_emit(ctx, de->name, de->name_len,
1593 le32_to_cpu(de->inode),
1594 get_dtype(sb, de->file_type)))
1597 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1605 void *ext4_read_inline_link(struct inode *inode)
1607 struct ext4_iloc iloc;
1608 int ret, inline_size;
1611 ret = ext4_get_inode_loc(inode, &iloc);
1613 return ERR_PTR(ret);
1616 inline_size = ext4_get_inline_size(inode);
1617 link = kmalloc(inline_size + 1, GFP_NOFS);
1621 ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
1626 nd_terminate_link(link, inode->i_size, ret);
1629 link = ERR_PTR(ret);
1634 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1635 struct ext4_dir_entry_2 **parent_de,
1638 struct ext4_iloc iloc;
1640 *retval = ext4_get_inode_loc(inode, &iloc);
1644 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1650 * Try to create the inline data for the new dir.
1651 * If it succeeds, return 0, otherwise return the error.
1652 * In case of ENOSPC, the caller should create the normal disk layout dir.
1654 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1655 struct inode *inode)
1657 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1658 struct ext4_iloc iloc;
1659 struct ext4_dir_entry_2 *de;
1661 ret = ext4_get_inode_loc(inode, &iloc);
1665 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1670 * For inline dir, we only save the inode information for the ".."
1671 * and create a fake dentry to cover the left space.
1673 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1674 de->inode = cpu_to_le32(parent->i_ino);
1675 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1677 de->rec_len = ext4_rec_len_to_disk(
1678 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1680 set_nlink(inode, 2);
1681 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1687 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1688 struct ext4_filename *fname,
1689 struct ext4_dir_entry_2 **res_dir,
1690 int *has_inline_data)
1693 struct ext4_iloc iloc;
1697 if (ext4_get_inode_loc(dir, &iloc))
1700 down_read(&EXT4_I(dir)->xattr_sem);
1701 if (!ext4_has_inline_data(dir)) {
1702 *has_inline_data = 0;
1706 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1707 EXT4_INLINE_DOTDOT_SIZE;
1708 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1709 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1710 dir, fname, 0, res_dir);
1716 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1719 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1720 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1722 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1723 dir, fname, 0, res_dir);
1731 up_read(&EXT4_I(dir)->xattr_sem);
1735 int ext4_delete_inline_entry(handle_t *handle,
1737 struct ext4_dir_entry_2 *de_del,
1738 struct buffer_head *bh,
1739 int *has_inline_data)
1741 int err, inline_size, no_expand;
1742 struct ext4_iloc iloc;
1745 err = ext4_get_inode_loc(dir, &iloc);
1749 ext4_write_lock_xattr(dir, &no_expand);
1750 if (!ext4_has_inline_data(dir)) {
1751 *has_inline_data = 0;
1755 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1756 EXT4_MIN_INLINE_DATA_SIZE) {
1757 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1758 EXT4_INLINE_DOTDOT_SIZE;
1759 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1760 EXT4_INLINE_DOTDOT_SIZE;
1762 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1763 inline_size = ext4_get_inline_size(dir) -
1764 EXT4_MIN_INLINE_DATA_SIZE;
1767 BUFFER_TRACE(bh, "get_write_access");
1768 err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1773 err = ext4_generic_delete_entry(dir, de_del, bh,
1774 inline_start, inline_size, 0);
1778 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1780 ext4_write_unlock_xattr(dir, &no_expand);
1781 if (likely(err == 0))
1782 err = ext4_mark_inode_dirty(handle, dir);
1785 ext4_std_error(dir->i_sb, err);
1790 * Get the inline dentry at offset.
1792 static inline struct ext4_dir_entry_2 *
1793 ext4_get_inline_entry(struct inode *inode,
1794 struct ext4_iloc *iloc,
1795 unsigned int offset,
1796 void **inline_start,
1801 BUG_ON(offset > ext4_get_inline_size(inode));
1803 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1804 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1805 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1807 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1808 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1809 *inline_size = ext4_get_inline_size(inode) -
1810 EXT4_MIN_INLINE_DATA_SIZE;
1814 *inline_start = inline_pos;
1815 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1818 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1820 int err, inline_size;
1821 struct ext4_iloc iloc;
1824 unsigned int offset;
1825 struct ext4_dir_entry_2 *de;
1828 err = ext4_get_inode_loc(dir, &iloc);
1830 EXT4_ERROR_INODE_ERR(dir, -err,
1831 "error %d getting inode %lu block",
1836 down_read(&EXT4_I(dir)->xattr_sem);
1837 if (!ext4_has_inline_data(dir)) {
1838 *has_inline_data = 0;
1843 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1844 if (!le32_to_cpu(de->inode)) {
1845 ext4_warning(dir->i_sb,
1846 "bad inline directory (dir #%lu) - no `..'",
1851 inline_len = ext4_get_inline_size(dir);
1852 offset = EXT4_INLINE_DOTDOT_SIZE;
1853 while (offset < inline_len) {
1854 de = ext4_get_inline_entry(dir, &iloc, offset,
1855 &inline_pos, &inline_size);
1856 if (ext4_check_dir_entry(dir, NULL, de,
1857 iloc.bh, inline_pos,
1858 inline_size, offset)) {
1859 ext4_warning(dir->i_sb,
1860 "bad inline directory (dir #%lu) - "
1861 "inode %u, rec_len %u, name_len %d"
1863 dir->i_ino, le32_to_cpu(de->inode),
1864 le16_to_cpu(de->rec_len), de->name_len,
1868 if (le32_to_cpu(de->inode)) {
1871 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1876 up_read(&EXT4_I(dir)->xattr_sem);
1881 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1885 ext4_write_lock_xattr(inode, &no_expand);
1886 ret = ext4_destroy_inline_data_nolock(handle, inode);
1887 ext4_write_unlock_xattr(inode, &no_expand);
1892 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1895 int error = -EAGAIN;
1896 struct ext4_iloc iloc;
1898 down_read(&EXT4_I(inode)->xattr_sem);
1899 if (!ext4_has_inline_data(inode))
1902 error = ext4_get_inode_loc(inode, &iloc);
1906 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1907 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1908 addr += offsetof(struct ext4_inode, i_block);
1914 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1915 i_size_read(inode));
1916 iomap->type = IOMAP_INLINE;
1920 up_read(&EXT4_I(inode)->xattr_sem);
1924 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1927 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1930 struct ext4_xattr_ibody_find is = {
1931 .s = { .not_found = -ENODATA, },
1933 struct ext4_xattr_info i = {
1934 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1935 .name = EXT4_XATTR_SYSTEM_DATA,
1939 needed_blocks = ext4_writepage_trans_blocks(inode);
1940 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1942 return PTR_ERR(handle);
1944 ext4_write_lock_xattr(inode, &no_expand);
1945 if (!ext4_has_inline_data(inode)) {
1946 ext4_write_unlock_xattr(inode, &no_expand);
1948 ext4_journal_stop(handle);
1952 if ((err = ext4_orphan_add(handle, inode)) != 0)
1955 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1958 down_write(&EXT4_I(inode)->i_data_sem);
1959 i_size = inode->i_size;
1960 inline_size = ext4_get_inline_size(inode);
1961 EXT4_I(inode)->i_disksize = i_size;
1963 if (i_size < inline_size) {
1965 * if there's inline data to truncate and this file was
1966 * converted to extents after that inline data was written,
1967 * the extent status cache must be cleared to avoid leaving
1968 * behind stale delayed allocated extent entries
1970 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1972 err = ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1973 if (err == -ENOMEM) {
1974 memalloc_retry_wait(GFP_ATOMIC);
1981 /* Clear the content in the xattr space. */
1982 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1983 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1986 BUG_ON(is.s.not_found);
1988 value_len = le32_to_cpu(is.s.here->e_value_size);
1989 value = kmalloc(value_len, GFP_NOFS);
1995 err = ext4_xattr_ibody_get(inode, i.name_index,
1996 i.name, value, value_len);
2001 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
2002 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
2003 err = ext4_xattr_ibody_set(handle, inode, &i, &is);
2008 /* Clear the content within i_blocks. */
2009 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
2010 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
2011 memset(p + i_size, 0,
2012 EXT4_MIN_INLINE_DATA_SIZE - i_size);
2015 EXT4_I(inode)->i_inline_size = i_size <
2016 EXT4_MIN_INLINE_DATA_SIZE ?
2017 EXT4_MIN_INLINE_DATA_SIZE : i_size;
2021 up_write(&EXT4_I(inode)->i_data_sem);
2024 ext4_write_unlock_xattr(inode, &no_expand);
2027 ext4_orphan_del(handle, inode);
2030 inode->i_mtime = inode->i_ctime = current_time(inode);
2031 err = ext4_mark_inode_dirty(handle, inode);
2033 ext4_handle_sync(handle);
2035 ext4_journal_stop(handle);
2039 int ext4_convert_inline_data(struct inode *inode)
2041 int error, needed_blocks, no_expand;
2043 struct ext4_iloc iloc;
2045 if (!ext4_has_inline_data(inode)) {
2046 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2048 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2050 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2051 * cleared. This means we are in the middle of moving of
2052 * inline data to delay allocated block. Just force writeout
2053 * here to finish conversion.
2055 error = filemap_flush(inode->i_mapping);
2058 if (!ext4_has_inline_data(inode))
2062 needed_blocks = ext4_writepage_trans_blocks(inode);
2065 error = ext4_get_inode_loc(inode, &iloc);
2069 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2070 if (IS_ERR(handle)) {
2071 error = PTR_ERR(handle);
2075 ext4_write_lock_xattr(inode, &no_expand);
2076 if (ext4_has_inline_data(inode))
2077 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2078 ext4_write_unlock_xattr(inode, &no_expand);
2079 ext4_journal_stop(handle);