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>
11 #include "ext4_jbd2.h"
16 #define EXT4_XATTR_SYSTEM_DATA "data"
17 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
18 #define EXT4_INLINE_DOTDOT_OFFSET 2
19 #define EXT4_INLINE_DOTDOT_SIZE 4
21 static int ext4_get_inline_size(struct inode *inode)
23 if (EXT4_I(inode)->i_inline_off)
24 return EXT4_I(inode)->i_inline_size;
29 static int get_max_inline_xattr_value_size(struct inode *inode,
30 struct ext4_iloc *iloc)
32 struct ext4_xattr_ibody_header *header;
33 struct ext4_xattr_entry *entry;
34 struct ext4_inode *raw_inode;
37 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
40 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
41 EXT4_GOOD_OLD_INODE_SIZE -
42 EXT4_I(inode)->i_extra_isize -
43 sizeof(struct ext4_xattr_ibody_header);
46 * We need to subtract another sizeof(__u32) since an in-inode xattr
47 * needs an empty 4 bytes to indicate the gap between the xattr entry
48 * and the name/value pair.
50 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
51 return EXT4_XATTR_SIZE(min_offs -
52 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
53 EXT4_XATTR_ROUND - sizeof(__u32));
55 raw_inode = ext4_raw_inode(iloc);
56 header = IHDR(inode, raw_inode);
57 entry = IFIRST(header);
59 /* Compute min_offs. */
60 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
61 if (!entry->e_value_inum && entry->e_value_size) {
62 size_t offs = le16_to_cpu(entry->e_value_offs);
68 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
70 if (EXT4_I(inode)->i_inline_off) {
71 entry = (struct ext4_xattr_entry *)
72 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
74 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
78 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
80 if (free > EXT4_XATTR_ROUND)
81 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
90 * Get the maximum size we now can store in an inode.
91 * If we can't find the space for a xattr entry, don't use the space
92 * of the extents since we have no space to indicate the inline data.
94 int ext4_get_max_inline_size(struct inode *inode)
96 int error, max_inline_size;
97 struct ext4_iloc iloc;
99 if (EXT4_I(inode)->i_extra_isize == 0)
102 error = ext4_get_inode_loc(inode, &iloc);
104 ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
105 "can't get inode location %lu",
110 down_read(&EXT4_I(inode)->xattr_sem);
111 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
112 up_read(&EXT4_I(inode)->xattr_sem);
116 if (!max_inline_size)
119 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
123 * this function does not take xattr_sem, which is OK because it is
124 * currently only used in a code path coming form ext4_iget, before
125 * the new inode has been unlocked
127 int ext4_find_inline_data_nolock(struct inode *inode)
129 struct ext4_xattr_ibody_find is = {
130 .s = { .not_found = -ENODATA, },
132 struct ext4_xattr_info i = {
133 .name_index = EXT4_XATTR_INDEX_SYSTEM,
134 .name = EXT4_XATTR_SYSTEM_DATA,
138 if (EXT4_I(inode)->i_extra_isize == 0)
141 error = ext4_get_inode_loc(inode, &is.iloc);
145 error = ext4_xattr_ibody_find(inode, &i, &is);
149 if (!is.s.not_found) {
150 if (is.s.here->e_value_inum) {
151 EXT4_ERROR_INODE(inode, "inline data xattr refers "
152 "to an external xattr inode");
153 error = -EFSCORRUPTED;
156 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
157 (void *)ext4_raw_inode(&is.iloc));
158 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
159 le32_to_cpu(is.s.here->e_value_size);
160 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
167 static int ext4_read_inline_data(struct inode *inode, void *buffer,
169 struct ext4_iloc *iloc)
171 struct ext4_xattr_entry *entry;
172 struct ext4_xattr_ibody_header *header;
174 struct ext4_inode *raw_inode;
179 BUG_ON(len > EXT4_I(inode)->i_inline_size);
181 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
182 len : EXT4_MIN_INLINE_DATA_SIZE;
184 raw_inode = ext4_raw_inode(iloc);
185 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
193 header = IHDR(inode, raw_inode);
194 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
195 EXT4_I(inode)->i_inline_off);
196 len = min_t(unsigned int, len,
197 (unsigned int)le32_to_cpu(entry->e_value_size));
200 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
208 * write the buffer to the inline inode.
209 * If 'create' is set, we don't need to do the extra copy in the xattr
210 * value since it is already handled by ext4_xattr_ibody_inline_set.
211 * That saves us one memcpy.
213 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
214 void *buffer, loff_t pos, unsigned int len)
216 struct ext4_xattr_entry *entry;
217 struct ext4_xattr_ibody_header *header;
218 struct ext4_inode *raw_inode;
221 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
224 BUG_ON(!EXT4_I(inode)->i_inline_off);
225 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
227 raw_inode = ext4_raw_inode(iloc);
230 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
231 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
232 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
233 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
243 pos -= EXT4_MIN_INLINE_DATA_SIZE;
244 header = IHDR(inode, raw_inode);
245 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
246 EXT4_I(inode)->i_inline_off);
248 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
252 static int ext4_create_inline_data(handle_t *handle,
253 struct inode *inode, unsigned len)
257 struct ext4_xattr_ibody_find is = {
258 .s = { .not_found = -ENODATA, },
260 struct ext4_xattr_info i = {
261 .name_index = EXT4_XATTR_INDEX_SYSTEM,
262 .name = EXT4_XATTR_SYSTEM_DATA,
265 error = ext4_get_inode_loc(inode, &is.iloc);
269 BUFFER_TRACE(is.iloc.bh, "get_write_access");
270 error = ext4_journal_get_write_access(handle, is.iloc.bh);
274 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
275 value = EXT4_ZERO_XATTR_VALUE;
276 len -= EXT4_MIN_INLINE_DATA_SIZE;
282 /* Insert the xttr entry. */
286 error = ext4_xattr_ibody_find(inode, &i, &is);
290 BUG_ON(!is.s.not_found);
292 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
294 if (error == -ENOSPC)
295 ext4_clear_inode_state(inode,
296 EXT4_STATE_MAY_INLINE_DATA);
300 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
301 0, EXT4_MIN_INLINE_DATA_SIZE);
303 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
304 (void *)ext4_raw_inode(&is.iloc));
305 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
306 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
307 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
309 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
316 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
321 struct ext4_xattr_ibody_find is = {
322 .s = { .not_found = -ENODATA, },
324 struct ext4_xattr_info i = {
325 .name_index = EXT4_XATTR_INDEX_SYSTEM,
326 .name = EXT4_XATTR_SYSTEM_DATA,
329 /* If the old space is ok, write the data directly. */
330 if (len <= EXT4_I(inode)->i_inline_size)
333 error = ext4_get_inode_loc(inode, &is.iloc);
337 error = ext4_xattr_ibody_find(inode, &i, &is);
341 BUG_ON(is.s.not_found);
343 len -= EXT4_MIN_INLINE_DATA_SIZE;
344 value = kzalloc(len, GFP_NOFS);
350 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
352 if (error == -ENODATA)
355 BUFFER_TRACE(is.iloc.bh, "get_write_access");
356 error = ext4_journal_get_write_access(handle, is.iloc.bh);
360 /* Update the xattr entry. */
364 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
368 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
369 (void *)ext4_raw_inode(&is.iloc));
370 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
371 le32_to_cpu(is.s.here->e_value_size);
372 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
374 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
382 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
385 int ret, size, no_expand;
386 struct ext4_inode_info *ei = EXT4_I(inode);
388 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
391 size = ext4_get_max_inline_size(inode);
395 ext4_write_lock_xattr(inode, &no_expand);
397 if (ei->i_inline_off)
398 ret = ext4_update_inline_data(handle, inode, len);
400 ret = ext4_create_inline_data(handle, inode, len);
402 ext4_write_unlock_xattr(inode, &no_expand);
406 static int ext4_destroy_inline_data_nolock(handle_t *handle,
409 struct ext4_inode_info *ei = EXT4_I(inode);
410 struct ext4_xattr_ibody_find is = {
411 .s = { .not_found = 0, },
413 struct ext4_xattr_info i = {
414 .name_index = EXT4_XATTR_INDEX_SYSTEM,
415 .name = EXT4_XATTR_SYSTEM_DATA,
421 if (!ei->i_inline_off)
424 error = ext4_get_inode_loc(inode, &is.iloc);
428 error = ext4_xattr_ibody_find(inode, &i, &is);
432 BUFFER_TRACE(is.iloc.bh, "get_write_access");
433 error = ext4_journal_get_write_access(handle, is.iloc.bh);
437 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
441 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
442 0, EXT4_MIN_INLINE_DATA_SIZE);
443 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
445 if (ext4_has_feature_extents(inode->i_sb)) {
446 if (S_ISDIR(inode->i_mode) ||
447 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
448 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
449 ext4_ext_tree_init(handle, inode);
452 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
455 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
457 EXT4_I(inode)->i_inline_off = 0;
458 EXT4_I(inode)->i_inline_size = 0;
459 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
462 if (error == -ENODATA)
467 static int ext4_read_inline_page(struct inode *inode, struct page *page)
472 struct ext4_iloc iloc;
474 BUG_ON(!PageLocked(page));
475 BUG_ON(!ext4_has_inline_data(inode));
478 if (!EXT4_I(inode)->i_inline_off) {
479 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
484 ret = ext4_get_inode_loc(inode, &iloc);
488 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
489 kaddr = kmap_atomic(page);
490 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
491 flush_dcache_page(page);
492 kunmap_atomic(kaddr);
493 zero_user_segment(page, len, PAGE_SIZE);
494 SetPageUptodate(page);
501 int ext4_readpage_inline(struct inode *inode, struct page *page)
505 down_read(&EXT4_I(inode)->xattr_sem);
506 if (!ext4_has_inline_data(inode)) {
507 up_read(&EXT4_I(inode)->xattr_sem);
512 * Current inline data can only exist in the 1st page,
513 * So for all the other pages, just set them uptodate.
516 ret = ext4_read_inline_page(inode, page);
517 else if (!PageUptodate(page)) {
518 zero_user_segment(page, 0, PAGE_SIZE);
519 SetPageUptodate(page);
522 up_read(&EXT4_I(inode)->xattr_sem);
525 return ret >= 0 ? 0 : ret;
528 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
532 int ret, needed_blocks, no_expand;
533 handle_t *handle = NULL;
534 int retries = 0, sem_held = 0;
535 struct page *page = NULL;
537 struct ext4_iloc iloc;
539 if (!ext4_has_inline_data(inode)) {
541 * clear the flag so that no new write
542 * will trap here again.
544 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
548 needed_blocks = ext4_writepage_trans_blocks(inode);
550 ret = ext4_get_inode_loc(inode, &iloc);
555 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
556 if (IS_ERR(handle)) {
557 ret = PTR_ERR(handle);
562 /* We cannot recurse into the filesystem as the transaction is already
564 flags |= AOP_FLAG_NOFS;
566 page = grab_cache_page_write_begin(mapping, 0, flags);
572 ext4_write_lock_xattr(inode, &no_expand);
574 /* If some one has already done this for us, just exit. */
575 if (!ext4_has_inline_data(inode)) {
581 to = ext4_get_inline_size(inode);
582 if (!PageUptodate(page)) {
583 ret = ext4_read_inline_page(inode, page);
588 ret = ext4_destroy_inline_data_nolock(handle, inode);
592 if (ext4_should_dioread_nolock(inode)) {
593 ret = __block_write_begin(page, from, to,
594 ext4_get_block_unwritten);
596 ret = __block_write_begin(page, from, to, ext4_get_block);
598 if (!ret && ext4_should_journal_data(inode)) {
599 ret = ext4_walk_page_buffers(handle, page_buffers(page),
601 do_journal_get_write_access);
608 ext4_orphan_add(handle, inode);
609 ext4_write_unlock_xattr(inode, &no_expand);
611 ext4_journal_stop(handle);
613 ext4_truncate_failed_write(inode);
615 * If truncate failed early the inode might
616 * still be on the orphan list; we need to
617 * make sure the inode is removed from the
618 * orphan list in that case.
621 ext4_orphan_del(NULL, inode);
624 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
628 block_commit_write(page, from, to);
635 ext4_write_unlock_xattr(inode, &no_expand);
637 ext4_journal_stop(handle);
643 * Try to write data in the inode.
644 * If the inode has inline data, check whether the new write can be
645 * in the inode also. If not, create the page the handle, move the data
646 * to the page make it update and let the later codes create extent for it.
648 int ext4_try_to_write_inline_data(struct address_space *mapping,
650 loff_t pos, unsigned len,
657 struct ext4_iloc iloc;
659 if (pos + len > ext4_get_max_inline_size(inode))
662 ret = ext4_get_inode_loc(inode, &iloc);
667 * The possible write could happen in the inode,
668 * so try to reserve the space in inode first.
670 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
671 if (IS_ERR(handle)) {
672 ret = PTR_ERR(handle);
677 ret = ext4_prepare_inline_data(handle, inode, pos + len);
678 if (ret && ret != -ENOSPC)
681 /* We don't have space in inline inode, so convert it to extent. */
682 if (ret == -ENOSPC) {
683 ext4_journal_stop(handle);
688 ret = ext4_journal_get_write_access(handle, iloc.bh);
692 flags |= AOP_FLAG_NOFS;
694 page = grab_cache_page_write_begin(mapping, 0, flags);
701 down_read(&EXT4_I(inode)->xattr_sem);
702 if (!ext4_has_inline_data(inode)) {
709 if (!PageUptodate(page)) {
710 ret = ext4_read_inline_page(inode, page);
721 up_read(&EXT4_I(inode)->xattr_sem);
723 if (handle && (ret != 1))
724 ext4_journal_stop(handle);
728 return ext4_convert_inline_data_to_extent(mapping,
732 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
733 unsigned copied, struct page *page)
737 struct ext4_iloc iloc;
739 if (unlikely(copied < len) && !PageUptodate(page))
742 ret = ext4_get_inode_loc(inode, &iloc);
744 ext4_std_error(inode->i_sb, ret);
748 ext4_write_lock_xattr(inode, &no_expand);
749 BUG_ON(!ext4_has_inline_data(inode));
752 * ei->i_inline_off may have changed since ext4_write_begin()
753 * called ext4_try_to_write_inline_data()
755 (void) ext4_find_inline_data_nolock(inode);
757 kaddr = kmap_atomic(page);
758 ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
759 kunmap_atomic(kaddr);
760 SetPageUptodate(page);
761 /* clear page dirty so that writepages wouldn't work for us. */
762 ClearPageDirty(page);
764 ext4_write_unlock_xattr(inode, &no_expand);
766 mark_inode_dirty(inode);
772 ext4_journalled_write_inline_data(struct inode *inode,
778 struct ext4_iloc iloc;
780 ret = ext4_get_inode_loc(inode, &iloc);
782 ext4_std_error(inode->i_sb, ret);
786 ext4_write_lock_xattr(inode, &no_expand);
787 kaddr = kmap_atomic(page);
788 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
789 kunmap_atomic(kaddr);
790 ext4_write_unlock_xattr(inode, &no_expand);
796 * Try to make the page cache and handle ready for the inline data case.
797 * We can call this function in 2 cases:
798 * 1. The inode is created and the first write exceeds inline size. We can
799 * clear the inode state safely.
800 * 2. The inode has inline data, then we need to read the data, make it
801 * update and dirty so that ext4_da_writepages can handle it. We don't
802 * need to start the journal since the file's metatdata isn't changed now.
804 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
809 int ret = 0, inline_size;
812 page = grab_cache_page_write_begin(mapping, 0, flags);
816 down_read(&EXT4_I(inode)->xattr_sem);
817 if (!ext4_has_inline_data(inode)) {
818 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
822 inline_size = ext4_get_inline_size(inode);
824 if (!PageUptodate(page)) {
825 ret = ext4_read_inline_page(inode, page);
830 ret = __block_write_begin(page, 0, inline_size,
831 ext4_da_get_block_prep);
833 up_read(&EXT4_I(inode)->xattr_sem);
836 ext4_truncate_failed_write(inode);
841 SetPageUptodate(page);
842 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
843 *fsdata = (void *)CONVERT_INLINE_DATA;
846 up_read(&EXT4_I(inode)->xattr_sem);
855 * Prepare the write for the inline data.
856 * If the data can be written into the inode, we just read
857 * the page and make it uptodate, and start the journal.
858 * Otherwise read the page, makes it dirty so that it can be
859 * handle in writepages(the i_disksize update is left to the
860 * normal ext4_da_write_end).
862 int ext4_da_write_inline_data_begin(struct address_space *mapping,
864 loff_t pos, unsigned len,
869 int ret, inline_size;
872 struct ext4_iloc iloc;
875 ret = ext4_get_inode_loc(inode, &iloc);
880 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
881 if (IS_ERR(handle)) {
882 ret = PTR_ERR(handle);
886 inline_size = ext4_get_max_inline_size(inode);
889 if (inline_size >= pos + len) {
890 ret = ext4_prepare_inline_data(handle, inode, pos + len);
891 if (ret && ret != -ENOSPC)
896 * We cannot recurse into the filesystem as the transaction
897 * is already started.
899 flags |= AOP_FLAG_NOFS;
901 if (ret == -ENOSPC) {
902 ext4_journal_stop(handle);
903 ret = ext4_da_convert_inline_data_to_extent(mapping,
907 if (ret == -ENOSPC &&
908 ext4_should_retry_alloc(inode->i_sb, &retries))
913 page = grab_cache_page_write_begin(mapping, 0, flags);
919 down_read(&EXT4_I(inode)->xattr_sem);
920 if (!ext4_has_inline_data(inode)) {
922 goto out_release_page;
925 if (!PageUptodate(page)) {
926 ret = ext4_read_inline_page(inode, page);
928 goto out_release_page;
930 ret = ext4_journal_get_write_access(handle, iloc.bh);
932 goto out_release_page;
934 up_read(&EXT4_I(inode)->xattr_sem);
939 up_read(&EXT4_I(inode)->xattr_sem);
943 ext4_journal_stop(handle);
949 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
950 unsigned len, unsigned copied,
955 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
964 * No need to use i_size_read() here, the i_size
965 * cannot change under us because we hold i_mutex.
967 * But it's important to update i_size while still holding page lock:
968 * page writeout could otherwise come in and zero beyond i_size.
970 if (pos+copied > inode->i_size)
971 i_size_write(inode, pos+copied);
976 * Don't mark the inode dirty under page lock. First, it unnecessarily
977 * makes the holding time of page lock longer. Second, it forces lock
978 * ordering of page lock and transaction start for journaling
981 mark_inode_dirty(inode);
986 #ifdef INLINE_DIR_DEBUG
987 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
988 void *inline_start, int inline_size)
991 unsigned short de_len;
992 struct ext4_dir_entry_2 *de = inline_start;
993 void *dlimit = inline_start + inline_size;
995 trace_printk("inode %lu\n", dir->i_ino);
997 while ((void *)de < dlimit) {
998 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
999 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1000 offset, de_len, de->name_len, de->name,
1001 de->name_len, le32_to_cpu(de->inode));
1002 if (ext4_check_dir_entry(dir, NULL, de, bh,
1003 inline_start, inline_size, offset))
1007 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1011 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1015 * Add a new entry into a inline dir.
1016 * It will return -ENOSPC if no space is available, and -EIO
1017 * and -EEXIST if directory entry already exists.
1019 static int ext4_add_dirent_to_inline(handle_t *handle,
1020 struct ext4_filename *fname,
1022 struct inode *inode,
1023 struct ext4_iloc *iloc,
1024 void *inline_start, int inline_size)
1027 struct ext4_dir_entry_2 *de;
1029 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1030 inline_size, fname, &de);
1034 BUFFER_TRACE(iloc->bh, "get_write_access");
1035 err = ext4_journal_get_write_access(handle, iloc->bh);
1038 ext4_insert_dentry(inode, de, inline_size, fname);
1040 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1043 * XXX shouldn't update any times until successful
1044 * completion of syscall, but too many callers depend
1047 * XXX similarly, too many callers depend on
1048 * ext4_new_inode() setting the times, but error
1049 * recovery deletes the inode, so the worst that can
1050 * happen is that the times are slightly out of date
1051 * and/or different from the directory change time.
1053 dir->i_mtime = dir->i_ctime = current_time(dir);
1054 ext4_update_dx_flag(dir);
1055 inode_inc_iversion(dir);
1059 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1060 struct ext4_iloc *iloc)
1062 struct ext4_xattr_entry *entry;
1063 struct ext4_xattr_ibody_header *header;
1065 BUG_ON(!EXT4_I(inode)->i_inline_off);
1067 header = IHDR(inode, ext4_raw_inode(iloc));
1068 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1069 EXT4_I(inode)->i_inline_off);
1071 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1074 /* Set the final de to cover the whole block. */
1075 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1077 struct ext4_dir_entry_2 *de, *prev_de;
1081 de = (struct ext4_dir_entry_2 *)de_buf;
1083 limit = de_buf + old_size;
1086 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1088 de = (struct ext4_dir_entry_2 *)de_buf;
1089 } while (de_buf < limit);
1091 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1092 old_size, new_size);
1094 /* this is just created, so create an empty entry. */
1096 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1100 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1101 struct ext4_iloc *iloc)
1104 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1105 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1107 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1110 ret = ext4_update_inline_data(handle, dir,
1111 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1115 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1116 EXT4_I(dir)->i_inline_size -
1117 EXT4_MIN_INLINE_DATA_SIZE);
1118 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1122 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1123 struct ext4_iloc *iloc,
1124 void *buf, int inline_size)
1128 ret = ext4_create_inline_data(handle, inode, inline_size);
1130 ext4_msg(inode->i_sb, KERN_EMERG,
1131 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1135 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1136 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1139 static int ext4_finish_convert_inline_dir(handle_t *handle,
1140 struct inode *inode,
1141 struct buffer_head *dir_block,
1145 int err, csum_size = 0, header_size = 0;
1146 struct ext4_dir_entry_2 *de;
1147 void *target = dir_block->b_data;
1150 * First create "." and ".." and then copy the dir information
1151 * back to the block.
1153 de = (struct ext4_dir_entry_2 *)target;
1154 de = ext4_init_dot_dotdot(inode, de,
1155 inode->i_sb->s_blocksize, csum_size,
1156 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1157 header_size = (void *)de - target;
1159 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1160 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1162 if (ext4_has_metadata_csum(inode->i_sb))
1163 csum_size = sizeof(struct ext4_dir_entry_tail);
1165 inode->i_size = inode->i_sb->s_blocksize;
1166 i_size_write(inode, inode->i_sb->s_blocksize);
1167 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1168 ext4_update_final_de(dir_block->b_data,
1169 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1170 inode->i_sb->s_blocksize - csum_size);
1173 ext4_initialize_dirent_tail(dir_block,
1174 inode->i_sb->s_blocksize);
1175 set_buffer_uptodate(dir_block);
1176 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
1179 set_buffer_verified(dir_block);
1180 return ext4_mark_inode_dirty(handle, inode);
1183 static int ext4_convert_inline_data_nolock(handle_t *handle,
1184 struct inode *inode,
1185 struct ext4_iloc *iloc)
1189 struct buffer_head *data_bh = NULL;
1190 struct ext4_map_blocks map;
1193 inline_size = ext4_get_inline_size(inode);
1194 buf = kmalloc(inline_size, GFP_NOFS);
1200 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1205 * Make sure the inline directory entries pass checks before we try to
1206 * convert them, so that we avoid touching stuff that needs fsck.
1208 if (S_ISDIR(inode->i_mode)) {
1209 error = ext4_check_all_de(inode, iloc->bh,
1210 buf + EXT4_INLINE_DOTDOT_SIZE,
1211 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1216 error = ext4_destroy_inline_data_nolock(handle, inode);
1223 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1226 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1231 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1237 lock_buffer(data_bh);
1238 error = ext4_journal_get_create_access(handle, data_bh);
1240 unlock_buffer(data_bh);
1244 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1246 if (!S_ISDIR(inode->i_mode)) {
1247 memcpy(data_bh->b_data, buf, inline_size);
1248 set_buffer_uptodate(data_bh);
1249 error = ext4_handle_dirty_metadata(handle,
1252 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1256 unlock_buffer(data_bh);
1259 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1268 * Try to add the new entry to the inline data.
1269 * If succeeds, return 0. If not, extended the inline dir and copied data to
1270 * the new created block.
1272 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1273 struct inode *dir, struct inode *inode)
1275 int ret, ret2, inline_size, no_expand;
1277 struct ext4_iloc iloc;
1279 ret = ext4_get_inode_loc(dir, &iloc);
1283 ext4_write_lock_xattr(dir, &no_expand);
1284 if (!ext4_has_inline_data(dir))
1287 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1288 EXT4_INLINE_DOTDOT_SIZE;
1289 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1291 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1292 inline_start, inline_size);
1296 /* check whether it can be inserted to inline xattr space. */
1297 inline_size = EXT4_I(dir)->i_inline_size -
1298 EXT4_MIN_INLINE_DATA_SIZE;
1300 /* Try to use the xattr space.*/
1301 ret = ext4_update_inline_dir(handle, dir, &iloc);
1302 if (ret && ret != -ENOSPC)
1305 inline_size = EXT4_I(dir)->i_inline_size -
1306 EXT4_MIN_INLINE_DATA_SIZE;
1310 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1312 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1313 inode, &iloc, inline_start,
1321 * The inline space is filled up, so create a new block for it.
1322 * As the extent tree will be created, we have to save the inline
1325 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1328 ext4_write_unlock_xattr(dir, &no_expand);
1329 ret2 = ext4_mark_inode_dirty(handle, dir);
1330 if (unlikely(ret2 && !ret))
1337 * This function fills a red-black tree with information from an
1338 * inlined dir. It returns the number directory entries loaded
1339 * into the tree. If there is an error it is returned in err.
1341 int ext4_inlinedir_to_tree(struct file *dir_file,
1342 struct inode *dir, ext4_lblk_t block,
1343 struct dx_hash_info *hinfo,
1344 __u32 start_hash, __u32 start_minor_hash,
1345 int *has_inline_data)
1347 int err = 0, count = 0;
1348 unsigned int parent_ino;
1350 struct ext4_dir_entry_2 *de;
1351 struct inode *inode = file_inode(dir_file);
1352 int ret, inline_size = 0;
1353 struct ext4_iloc iloc;
1354 void *dir_buf = NULL;
1355 struct ext4_dir_entry_2 fake;
1356 struct fscrypt_str tmp_str;
1358 ret = ext4_get_inode_loc(inode, &iloc);
1362 down_read(&EXT4_I(inode)->xattr_sem);
1363 if (!ext4_has_inline_data(inode)) {
1364 up_read(&EXT4_I(inode)->xattr_sem);
1365 *has_inline_data = 0;
1369 inline_size = ext4_get_inline_size(inode);
1370 dir_buf = kmalloc(inline_size, GFP_NOFS);
1373 up_read(&EXT4_I(inode)->xattr_sem);
1377 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1378 up_read(&EXT4_I(inode)->xattr_sem);
1383 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1384 while (pos < inline_size) {
1386 * As inlined dir doesn't store any information about '.' and
1387 * only the inode number of '..' is stored, we have to handle
1391 fake.inode = cpu_to_le32(inode->i_ino);
1393 strcpy(fake.name, ".");
1394 fake.rec_len = ext4_rec_len_to_disk(
1395 EXT4_DIR_REC_LEN(fake.name_len),
1397 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1399 pos = EXT4_INLINE_DOTDOT_OFFSET;
1400 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1401 fake.inode = cpu_to_le32(parent_ino);
1403 strcpy(fake.name, "..");
1404 fake.rec_len = ext4_rec_len_to_disk(
1405 EXT4_DIR_REC_LEN(fake.name_len),
1407 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1409 pos = EXT4_INLINE_DOTDOT_SIZE;
1411 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1412 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1413 if (ext4_check_dir_entry(inode, dir_file, de,
1415 inline_size, pos)) {
1421 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1422 if ((hinfo->hash < start_hash) ||
1423 ((hinfo->hash == start_hash) &&
1424 (hinfo->minor_hash < start_minor_hash)))
1428 tmp_str.name = de->name;
1429 tmp_str.len = de->name_len;
1430 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1431 hinfo->minor_hash, de, &tmp_str);
1446 * So this function is called when the volume is mkfsed with
1447 * dir_index disabled. In order to keep f_pos persistent
1448 * after we convert from an inlined dir to a blocked based,
1449 * we just pretend that we are a normal dir and return the
1450 * offset as if '.' and '..' really take place.
1453 int ext4_read_inline_dir(struct file *file,
1454 struct dir_context *ctx,
1455 int *has_inline_data)
1457 unsigned int offset, parent_ino;
1459 struct ext4_dir_entry_2 *de;
1460 struct super_block *sb;
1461 struct inode *inode = file_inode(file);
1462 int ret, inline_size = 0;
1463 struct ext4_iloc iloc;
1464 void *dir_buf = NULL;
1465 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1467 ret = ext4_get_inode_loc(inode, &iloc);
1471 down_read(&EXT4_I(inode)->xattr_sem);
1472 if (!ext4_has_inline_data(inode)) {
1473 up_read(&EXT4_I(inode)->xattr_sem);
1474 *has_inline_data = 0;
1478 inline_size = ext4_get_inline_size(inode);
1479 dir_buf = kmalloc(inline_size, GFP_NOFS);
1482 up_read(&EXT4_I(inode)->xattr_sem);
1486 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1487 up_read(&EXT4_I(inode)->xattr_sem);
1493 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1497 * dotdot_offset and dotdot_size is the real offset and
1498 * size for ".." and "." if the dir is block based while
1499 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1500 * So we will use extra_offset and extra_size to indicate them
1501 * during the inline dir iteration.
1503 dotdot_offset = EXT4_DIR_REC_LEN(1);
1504 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1505 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1506 extra_size = extra_offset + inline_size;
1509 * If the version has changed since the last call to
1510 * readdir(2), then we might be pointing to an invalid
1511 * dirent right now. Scan from the start of the inline
1514 if (!inode_eq_iversion(inode, file->f_version)) {
1515 for (i = 0; i < extra_size && i < offset;) {
1517 * "." is with offset 0 and
1518 * ".." is dotdot_offset.
1523 } else if (i == dotdot_offset) {
1527 /* for other entry, the real offset in
1528 * the buf has to be tuned accordingly.
1530 de = (struct ext4_dir_entry_2 *)
1531 (dir_buf + i - extra_offset);
1532 /* It's too expensive to do a full
1533 * dirent test each time round this
1534 * loop, but we do have to test at
1535 * least that it is non-zero. A
1536 * failure will be detected in the
1537 * dirent test below. */
1538 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1539 < EXT4_DIR_REC_LEN(1))
1541 i += ext4_rec_len_from_disk(de->rec_len,
1546 file->f_version = inode_query_iversion(inode);
1549 while (ctx->pos < extra_size) {
1550 if (ctx->pos == 0) {
1551 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1553 ctx->pos = dotdot_offset;
1557 if (ctx->pos == dotdot_offset) {
1558 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1560 ctx->pos = dotdot_size;
1564 de = (struct ext4_dir_entry_2 *)
1565 (dir_buf + ctx->pos - extra_offset);
1566 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1567 extra_size, ctx->pos))
1569 if (le32_to_cpu(de->inode)) {
1570 if (!dir_emit(ctx, de->name, de->name_len,
1571 le32_to_cpu(de->inode),
1572 get_dtype(sb, de->file_type)))
1575 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1583 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1584 struct ext4_dir_entry_2 **parent_de,
1587 struct ext4_iloc iloc;
1589 *retval = ext4_get_inode_loc(inode, &iloc);
1593 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1599 * Try to create the inline data for the new dir.
1600 * If it succeeds, return 0, otherwise return the error.
1601 * In case of ENOSPC, the caller should create the normal disk layout dir.
1603 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1604 struct inode *inode)
1606 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1607 struct ext4_iloc iloc;
1608 struct ext4_dir_entry_2 *de;
1610 ret = ext4_get_inode_loc(inode, &iloc);
1614 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1619 * For inline dir, we only save the inode information for the ".."
1620 * and create a fake dentry to cover the left space.
1622 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1623 de->inode = cpu_to_le32(parent->i_ino);
1624 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1626 de->rec_len = ext4_rec_len_to_disk(
1627 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1629 set_nlink(inode, 2);
1630 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1636 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1637 struct ext4_filename *fname,
1638 struct ext4_dir_entry_2 **res_dir,
1639 int *has_inline_data)
1642 struct ext4_iloc iloc;
1646 if (ext4_get_inode_loc(dir, &iloc))
1649 down_read(&EXT4_I(dir)->xattr_sem);
1650 if (!ext4_has_inline_data(dir)) {
1651 *has_inline_data = 0;
1655 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1656 EXT4_INLINE_DOTDOT_SIZE;
1657 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1658 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1659 dir, fname, 0, res_dir);
1665 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1668 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1669 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1671 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1672 dir, fname, 0, res_dir);
1680 up_read(&EXT4_I(dir)->xattr_sem);
1684 int ext4_delete_inline_entry(handle_t *handle,
1686 struct ext4_dir_entry_2 *de_del,
1687 struct buffer_head *bh,
1688 int *has_inline_data)
1690 int err, inline_size, no_expand;
1691 struct ext4_iloc iloc;
1694 err = ext4_get_inode_loc(dir, &iloc);
1698 ext4_write_lock_xattr(dir, &no_expand);
1699 if (!ext4_has_inline_data(dir)) {
1700 *has_inline_data = 0;
1704 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1705 EXT4_MIN_INLINE_DATA_SIZE) {
1706 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1707 EXT4_INLINE_DOTDOT_SIZE;
1708 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1709 EXT4_INLINE_DOTDOT_SIZE;
1711 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1712 inline_size = ext4_get_inline_size(dir) -
1713 EXT4_MIN_INLINE_DATA_SIZE;
1716 BUFFER_TRACE(bh, "get_write_access");
1717 err = ext4_journal_get_write_access(handle, bh);
1721 err = ext4_generic_delete_entry(dir, de_del, bh,
1722 inline_start, inline_size, 0);
1726 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1728 ext4_write_unlock_xattr(dir, &no_expand);
1729 if (likely(err == 0))
1730 err = ext4_mark_inode_dirty(handle, dir);
1733 ext4_std_error(dir->i_sb, err);
1738 * Get the inline dentry at offset.
1740 static inline struct ext4_dir_entry_2 *
1741 ext4_get_inline_entry(struct inode *inode,
1742 struct ext4_iloc *iloc,
1743 unsigned int offset,
1744 void **inline_start,
1749 BUG_ON(offset > ext4_get_inline_size(inode));
1751 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1752 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1753 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1755 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1756 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1757 *inline_size = ext4_get_inline_size(inode) -
1758 EXT4_MIN_INLINE_DATA_SIZE;
1762 *inline_start = inline_pos;
1763 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1766 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1768 int err, inline_size;
1769 struct ext4_iloc iloc;
1772 unsigned int offset;
1773 struct ext4_dir_entry_2 *de;
1776 err = ext4_get_inode_loc(dir, &iloc);
1778 EXT4_ERROR_INODE_ERR(dir, -err,
1779 "error %d getting inode %lu block",
1784 down_read(&EXT4_I(dir)->xattr_sem);
1785 if (!ext4_has_inline_data(dir)) {
1786 *has_inline_data = 0;
1791 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1792 if (!le32_to_cpu(de->inode)) {
1793 ext4_warning(dir->i_sb,
1794 "bad inline directory (dir #%lu) - no `..'",
1799 inline_len = ext4_get_inline_size(dir);
1800 offset = EXT4_INLINE_DOTDOT_SIZE;
1801 while (offset < inline_len) {
1802 de = ext4_get_inline_entry(dir, &iloc, offset,
1803 &inline_pos, &inline_size);
1804 if (ext4_check_dir_entry(dir, NULL, de,
1805 iloc.bh, inline_pos,
1806 inline_size, offset)) {
1807 ext4_warning(dir->i_sb,
1808 "bad inline directory (dir #%lu) - "
1809 "inode %u, rec_len %u, name_len %d"
1811 dir->i_ino, le32_to_cpu(de->inode),
1812 le16_to_cpu(de->rec_len), de->name_len,
1816 if (le32_to_cpu(de->inode)) {
1819 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1824 up_read(&EXT4_I(dir)->xattr_sem);
1829 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1833 ext4_write_lock_xattr(inode, &no_expand);
1834 ret = ext4_destroy_inline_data_nolock(handle, inode);
1835 ext4_write_unlock_xattr(inode, &no_expand);
1840 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1843 int error = -EAGAIN;
1844 struct ext4_iloc iloc;
1846 down_read(&EXT4_I(inode)->xattr_sem);
1847 if (!ext4_has_inline_data(inode))
1850 error = ext4_get_inode_loc(inode, &iloc);
1854 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1855 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1856 addr += offsetof(struct ext4_inode, i_block);
1862 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1863 i_size_read(inode));
1864 iomap->type = IOMAP_INLINE;
1868 up_read(&EXT4_I(inode)->xattr_sem);
1872 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1875 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1878 struct ext4_xattr_ibody_find is = {
1879 .s = { .not_found = -ENODATA, },
1881 struct ext4_xattr_info i = {
1882 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1883 .name = EXT4_XATTR_SYSTEM_DATA,
1887 needed_blocks = ext4_writepage_trans_blocks(inode);
1888 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1890 return PTR_ERR(handle);
1892 ext4_write_lock_xattr(inode, &no_expand);
1893 if (!ext4_has_inline_data(inode)) {
1894 ext4_write_unlock_xattr(inode, &no_expand);
1896 ext4_journal_stop(handle);
1900 if ((err = ext4_orphan_add(handle, inode)) != 0)
1903 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1906 down_write(&EXT4_I(inode)->i_data_sem);
1907 i_size = inode->i_size;
1908 inline_size = ext4_get_inline_size(inode);
1909 EXT4_I(inode)->i_disksize = i_size;
1911 if (i_size < inline_size) {
1912 /* Clear the content in the xattr space. */
1913 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1914 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1917 BUG_ON(is.s.not_found);
1919 value_len = le32_to_cpu(is.s.here->e_value_size);
1920 value = kmalloc(value_len, GFP_NOFS);
1926 err = ext4_xattr_ibody_get(inode, i.name_index,
1927 i.name, value, value_len);
1932 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1933 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1934 err = ext4_xattr_ibody_inline_set(handle, inode,
1940 /* Clear the content within i_blocks. */
1941 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1942 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1943 memset(p + i_size, 0,
1944 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1947 EXT4_I(inode)->i_inline_size = i_size <
1948 EXT4_MIN_INLINE_DATA_SIZE ?
1949 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1953 up_write(&EXT4_I(inode)->i_data_sem);
1956 ext4_write_unlock_xattr(inode, &no_expand);
1959 ext4_orphan_del(handle, inode);
1962 inode->i_mtime = inode->i_ctime = current_time(inode);
1963 err = ext4_mark_inode_dirty(handle, inode);
1965 ext4_handle_sync(handle);
1967 ext4_journal_stop(handle);
1971 int ext4_convert_inline_data(struct inode *inode)
1973 int error, needed_blocks, no_expand;
1975 struct ext4_iloc iloc;
1977 if (!ext4_has_inline_data(inode)) {
1978 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1980 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1982 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
1983 * cleared. This means we are in the middle of moving of
1984 * inline data to delay allocated block. Just force writeout
1985 * here to finish conversion.
1987 error = filemap_flush(inode->i_mapping);
1990 if (!ext4_has_inline_data(inode))
1994 needed_blocks = ext4_writepage_trans_blocks(inode);
1997 error = ext4_get_inode_loc(inode, &iloc);
2001 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2002 if (IS_ERR(handle)) {
2003 error = PTR_ERR(handle);
2007 ext4_write_lock_xattr(inode, &no_expand);
2008 if (ext4_has_inline_data(inode))
2009 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2010 ext4_write_unlock_xattr(inode, &no_expand);
2011 ext4_journal_stop(handle);