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 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
38 EXT4_GOOD_OLD_INODE_SIZE -
39 EXT4_I(inode)->i_extra_isize -
40 sizeof(struct ext4_xattr_ibody_header);
43 * We need to subtract another sizeof(__u32) since an in-inode xattr
44 * needs an empty 4 bytes to indicate the gap between the xattr entry
45 * and the name/value pair.
47 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
48 return EXT4_XATTR_SIZE(min_offs -
49 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
50 EXT4_XATTR_ROUND - sizeof(__u32));
52 raw_inode = ext4_raw_inode(iloc);
53 header = IHDR(inode, raw_inode);
54 entry = IFIRST(header);
56 /* Compute min_offs. */
57 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
58 if (!entry->e_value_inum && entry->e_value_size) {
59 size_t offs = le16_to_cpu(entry->e_value_offs);
65 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
67 if (EXT4_I(inode)->i_inline_off) {
68 entry = (struct ext4_xattr_entry *)
69 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
71 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
75 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
77 if (free > EXT4_XATTR_ROUND)
78 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
87 * Get the maximum size we now can store in an inode.
88 * If we can't find the space for a xattr entry, don't use the space
89 * of the extents since we have no space to indicate the inline data.
91 int ext4_get_max_inline_size(struct inode *inode)
93 int error, max_inline_size;
94 struct ext4_iloc iloc;
96 if (EXT4_I(inode)->i_extra_isize == 0)
99 error = ext4_get_inode_loc(inode, &iloc);
101 ext4_set_errno(inode->i_sb, -error);
102 ext4_error_inode(inode, __func__, __LINE__, 0,
103 "can't get inode location %lu",
108 down_read(&EXT4_I(inode)->xattr_sem);
109 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
110 up_read(&EXT4_I(inode)->xattr_sem);
114 if (!max_inline_size)
117 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
121 * this function does not take xattr_sem, which is OK because it is
122 * currently only used in a code path coming form ext4_iget, before
123 * the new inode has been unlocked
125 int ext4_find_inline_data_nolock(struct inode *inode)
127 struct ext4_xattr_ibody_find is = {
128 .s = { .not_found = -ENODATA, },
130 struct ext4_xattr_info i = {
131 .name_index = EXT4_XATTR_INDEX_SYSTEM,
132 .name = EXT4_XATTR_SYSTEM_DATA,
136 if (EXT4_I(inode)->i_extra_isize == 0)
139 error = ext4_get_inode_loc(inode, &is.iloc);
143 error = ext4_xattr_ibody_find(inode, &i, &is);
147 if (!is.s.not_found) {
148 if (is.s.here->e_value_inum) {
149 EXT4_ERROR_INODE(inode, "inline data xattr refers "
150 "to an external xattr inode");
151 error = -EFSCORRUPTED;
154 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
155 (void *)ext4_raw_inode(&is.iloc));
156 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
157 le32_to_cpu(is.s.here->e_value_size);
158 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
165 static int ext4_read_inline_data(struct inode *inode, void *buffer,
167 struct ext4_iloc *iloc)
169 struct ext4_xattr_entry *entry;
170 struct ext4_xattr_ibody_header *header;
172 struct ext4_inode *raw_inode;
177 BUG_ON(len > EXT4_I(inode)->i_inline_size);
179 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
180 len : EXT4_MIN_INLINE_DATA_SIZE;
182 raw_inode = ext4_raw_inode(iloc);
183 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
191 header = IHDR(inode, raw_inode);
192 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
193 EXT4_I(inode)->i_inline_off);
194 len = min_t(unsigned int, len,
195 (unsigned int)le32_to_cpu(entry->e_value_size));
198 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
206 * write the buffer to the inline inode.
207 * If 'create' is set, we don't need to do the extra copy in the xattr
208 * value since it is already handled by ext4_xattr_ibody_inline_set.
209 * That saves us one memcpy.
211 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
212 void *buffer, loff_t pos, unsigned int len)
214 struct ext4_xattr_entry *entry;
215 struct ext4_xattr_ibody_header *header;
216 struct ext4_inode *raw_inode;
219 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
222 BUG_ON(!EXT4_I(inode)->i_inline_off);
223 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
225 raw_inode = ext4_raw_inode(iloc);
228 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
229 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
230 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
231 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
241 pos -= EXT4_MIN_INLINE_DATA_SIZE;
242 header = IHDR(inode, raw_inode);
243 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
244 EXT4_I(inode)->i_inline_off);
246 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
250 static int ext4_create_inline_data(handle_t *handle,
251 struct inode *inode, unsigned len)
255 struct ext4_xattr_ibody_find is = {
256 .s = { .not_found = -ENODATA, },
258 struct ext4_xattr_info i = {
259 .name_index = EXT4_XATTR_INDEX_SYSTEM,
260 .name = EXT4_XATTR_SYSTEM_DATA,
263 error = ext4_get_inode_loc(inode, &is.iloc);
267 BUFFER_TRACE(is.iloc.bh, "get_write_access");
268 error = ext4_journal_get_write_access(handle, is.iloc.bh);
272 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
273 value = EXT4_ZERO_XATTR_VALUE;
274 len -= EXT4_MIN_INLINE_DATA_SIZE;
280 /* Insert the the xttr entry. */
284 error = ext4_xattr_ibody_find(inode, &i, &is);
288 BUG_ON(!is.s.not_found);
290 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
292 if (error == -ENOSPC)
293 ext4_clear_inode_state(inode,
294 EXT4_STATE_MAY_INLINE_DATA);
298 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
299 0, EXT4_MIN_INLINE_DATA_SIZE);
301 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
302 (void *)ext4_raw_inode(&is.iloc));
303 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
304 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
305 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
307 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
314 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
319 struct ext4_xattr_ibody_find is = {
320 .s = { .not_found = -ENODATA, },
322 struct ext4_xattr_info i = {
323 .name_index = EXT4_XATTR_INDEX_SYSTEM,
324 .name = EXT4_XATTR_SYSTEM_DATA,
327 /* If the old space is ok, write the data directly. */
328 if (len <= EXT4_I(inode)->i_inline_size)
331 error = ext4_get_inode_loc(inode, &is.iloc);
335 error = ext4_xattr_ibody_find(inode, &i, &is);
339 BUG_ON(is.s.not_found);
341 len -= EXT4_MIN_INLINE_DATA_SIZE;
342 value = kzalloc(len, GFP_NOFS);
348 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
350 if (error == -ENODATA)
353 BUFFER_TRACE(is.iloc.bh, "get_write_access");
354 error = ext4_journal_get_write_access(handle, is.iloc.bh);
358 /* Update the xttr entry. */
362 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
366 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
367 (void *)ext4_raw_inode(&is.iloc));
368 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
369 le32_to_cpu(is.s.here->e_value_size);
370 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
372 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
380 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
383 int ret, size, no_expand;
384 struct ext4_inode_info *ei = EXT4_I(inode);
386 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
389 size = ext4_get_max_inline_size(inode);
393 ext4_write_lock_xattr(inode, &no_expand);
395 if (ei->i_inline_off)
396 ret = ext4_update_inline_data(handle, inode, len);
398 ret = ext4_create_inline_data(handle, inode, len);
400 ext4_write_unlock_xattr(inode, &no_expand);
404 static int ext4_destroy_inline_data_nolock(handle_t *handle,
407 struct ext4_inode_info *ei = EXT4_I(inode);
408 struct ext4_xattr_ibody_find is = {
409 .s = { .not_found = 0, },
411 struct ext4_xattr_info i = {
412 .name_index = EXT4_XATTR_INDEX_SYSTEM,
413 .name = EXT4_XATTR_SYSTEM_DATA,
419 if (!ei->i_inline_off)
422 error = ext4_get_inode_loc(inode, &is.iloc);
426 error = ext4_xattr_ibody_find(inode, &i, &is);
430 BUFFER_TRACE(is.iloc.bh, "get_write_access");
431 error = ext4_journal_get_write_access(handle, is.iloc.bh);
435 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
439 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
440 0, EXT4_MIN_INLINE_DATA_SIZE);
441 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
443 if (ext4_has_feature_extents(inode->i_sb)) {
444 if (S_ISDIR(inode->i_mode) ||
445 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
446 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
447 ext4_ext_tree_init(handle, inode);
450 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
453 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
455 EXT4_I(inode)->i_inline_off = 0;
456 EXT4_I(inode)->i_inline_size = 0;
457 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
460 if (error == -ENODATA)
465 static int ext4_read_inline_page(struct inode *inode, struct page *page)
470 struct ext4_iloc iloc;
472 BUG_ON(!PageLocked(page));
473 BUG_ON(!ext4_has_inline_data(inode));
476 if (!EXT4_I(inode)->i_inline_off) {
477 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
482 ret = ext4_get_inode_loc(inode, &iloc);
486 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
487 kaddr = kmap_atomic(page);
488 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
489 flush_dcache_page(page);
490 kunmap_atomic(kaddr);
491 zero_user_segment(page, len, PAGE_SIZE);
492 SetPageUptodate(page);
499 int ext4_readpage_inline(struct inode *inode, struct page *page)
503 down_read(&EXT4_I(inode)->xattr_sem);
504 if (!ext4_has_inline_data(inode)) {
505 up_read(&EXT4_I(inode)->xattr_sem);
510 * Current inline data can only exist in the 1st page,
511 * So for all the other pages, just set them uptodate.
514 ret = ext4_read_inline_page(inode, page);
515 else if (!PageUptodate(page)) {
516 zero_user_segment(page, 0, PAGE_SIZE);
517 SetPageUptodate(page);
520 up_read(&EXT4_I(inode)->xattr_sem);
523 return ret >= 0 ? 0 : ret;
526 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
530 int ret, needed_blocks, no_expand;
531 handle_t *handle = NULL;
532 int retries = 0, sem_held = 0;
533 struct page *page = NULL;
535 struct ext4_iloc iloc;
537 if (!ext4_has_inline_data(inode)) {
539 * clear the flag so that no new write
540 * will trap here again.
542 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
546 needed_blocks = ext4_writepage_trans_blocks(inode);
548 ret = ext4_get_inode_loc(inode, &iloc);
553 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
554 if (IS_ERR(handle)) {
555 ret = PTR_ERR(handle);
560 /* We cannot recurse into the filesystem as the transaction is already
562 flags |= AOP_FLAG_NOFS;
564 page = grab_cache_page_write_begin(mapping, 0, flags);
570 ext4_write_lock_xattr(inode, &no_expand);
572 /* If some one has already done this for us, just exit. */
573 if (!ext4_has_inline_data(inode)) {
579 to = ext4_get_inline_size(inode);
580 if (!PageUptodate(page)) {
581 ret = ext4_read_inline_page(inode, page);
586 ret = ext4_destroy_inline_data_nolock(handle, inode);
590 if (ext4_should_dioread_nolock(inode)) {
591 ret = __block_write_begin(page, from, to,
592 ext4_get_block_unwritten);
594 ret = __block_write_begin(page, from, to, ext4_get_block);
596 if (!ret && ext4_should_journal_data(inode)) {
597 ret = ext4_walk_page_buffers(handle, page_buffers(page),
599 do_journal_get_write_access);
606 ext4_orphan_add(handle, inode);
607 ext4_write_unlock_xattr(inode, &no_expand);
609 ext4_journal_stop(handle);
611 ext4_truncate_failed_write(inode);
613 * If truncate failed early the inode might
614 * still be on the orphan list; we need to
615 * make sure the inode is removed from the
616 * orphan list in that case.
619 ext4_orphan_del(NULL, inode);
622 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
626 block_commit_write(page, from, to);
633 ext4_write_unlock_xattr(inode, &no_expand);
635 ext4_journal_stop(handle);
641 * Try to write data in the inode.
642 * If the inode has inline data, check whether the new write can be
643 * in the inode also. If not, create the page the handle, move the data
644 * to the page make it update and let the later codes create extent for it.
646 int ext4_try_to_write_inline_data(struct address_space *mapping,
648 loff_t pos, unsigned len,
655 struct ext4_iloc iloc;
657 if (pos + len > ext4_get_max_inline_size(inode))
660 ret = ext4_get_inode_loc(inode, &iloc);
665 * The possible write could happen in the inode,
666 * so try to reserve the space in inode first.
668 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
669 if (IS_ERR(handle)) {
670 ret = PTR_ERR(handle);
675 ret = ext4_prepare_inline_data(handle, inode, pos + len);
676 if (ret && ret != -ENOSPC)
679 /* We don't have space in inline inode, so convert it to extent. */
680 if (ret == -ENOSPC) {
681 ext4_journal_stop(handle);
686 ret = ext4_journal_get_write_access(handle, iloc.bh);
690 flags |= AOP_FLAG_NOFS;
692 page = grab_cache_page_write_begin(mapping, 0, flags);
699 down_read(&EXT4_I(inode)->xattr_sem);
700 if (!ext4_has_inline_data(inode)) {
707 if (!PageUptodate(page)) {
708 ret = ext4_read_inline_page(inode, page);
719 up_read(&EXT4_I(inode)->xattr_sem);
721 if (handle && (ret != 1))
722 ext4_journal_stop(handle);
726 return ext4_convert_inline_data_to_extent(mapping,
730 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
731 unsigned copied, struct page *page)
735 struct ext4_iloc iloc;
737 if (unlikely(copied < len)) {
738 if (!PageUptodate(page)) {
744 ret = ext4_get_inode_loc(inode, &iloc);
746 ext4_std_error(inode->i_sb, ret);
751 ext4_write_lock_xattr(inode, &no_expand);
752 BUG_ON(!ext4_has_inline_data(inode));
754 kaddr = kmap_atomic(page);
755 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
756 kunmap_atomic(kaddr);
757 SetPageUptodate(page);
758 /* clear page dirty so that writepages wouldn't work for us. */
759 ClearPageDirty(page);
761 ext4_write_unlock_xattr(inode, &no_expand);
763 mark_inode_dirty(inode);
769 ext4_journalled_write_inline_data(struct inode *inode,
775 struct ext4_iloc iloc;
777 ret = ext4_get_inode_loc(inode, &iloc);
779 ext4_std_error(inode->i_sb, ret);
783 ext4_write_lock_xattr(inode, &no_expand);
784 kaddr = kmap_atomic(page);
785 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
786 kunmap_atomic(kaddr);
787 ext4_write_unlock_xattr(inode, &no_expand);
793 * Try to make the page cache and handle ready for the inline data case.
794 * We can call this function in 2 cases:
795 * 1. The inode is created and the first write exceeds inline size. We can
796 * clear the inode state safely.
797 * 2. The inode has inline data, then we need to read the data, make it
798 * update and dirty so that ext4_da_writepages can handle it. We don't
799 * need to start the journal since the file's metatdata isn't changed now.
801 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
806 int ret = 0, inline_size;
809 page = grab_cache_page_write_begin(mapping, 0, flags);
813 down_read(&EXT4_I(inode)->xattr_sem);
814 if (!ext4_has_inline_data(inode)) {
815 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
819 inline_size = ext4_get_inline_size(inode);
821 if (!PageUptodate(page)) {
822 ret = ext4_read_inline_page(inode, page);
827 ret = __block_write_begin(page, 0, inline_size,
828 ext4_da_get_block_prep);
830 up_read(&EXT4_I(inode)->xattr_sem);
833 ext4_truncate_failed_write(inode);
838 SetPageUptodate(page);
839 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
840 *fsdata = (void *)CONVERT_INLINE_DATA;
843 up_read(&EXT4_I(inode)->xattr_sem);
852 * Prepare the write for the inline data.
853 * If the data can be written into the inode, we just read
854 * the page and make it uptodate, and start the journal.
855 * Otherwise read the page, makes it dirty so that it can be
856 * handle in writepages(the i_disksize update is left to the
857 * normal ext4_da_write_end).
859 int ext4_da_write_inline_data_begin(struct address_space *mapping,
861 loff_t pos, unsigned len,
866 int ret, inline_size;
869 struct ext4_iloc iloc;
872 ret = ext4_get_inode_loc(inode, &iloc);
877 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
878 if (IS_ERR(handle)) {
879 ret = PTR_ERR(handle);
883 inline_size = ext4_get_max_inline_size(inode);
886 if (inline_size >= pos + len) {
887 ret = ext4_prepare_inline_data(handle, inode, pos + len);
888 if (ret && ret != -ENOSPC)
893 * We cannot recurse into the filesystem as the transaction
894 * is already started.
896 flags |= AOP_FLAG_NOFS;
898 if (ret == -ENOSPC) {
899 ext4_journal_stop(handle);
900 ret = ext4_da_convert_inline_data_to_extent(mapping,
904 if (ret == -ENOSPC &&
905 ext4_should_retry_alloc(inode->i_sb, &retries))
910 page = grab_cache_page_write_begin(mapping, 0, flags);
916 down_read(&EXT4_I(inode)->xattr_sem);
917 if (!ext4_has_inline_data(inode)) {
919 goto out_release_page;
922 if (!PageUptodate(page)) {
923 ret = ext4_read_inline_page(inode, page);
925 goto out_release_page;
927 ret = ext4_journal_get_write_access(handle, iloc.bh);
929 goto out_release_page;
931 up_read(&EXT4_I(inode)->xattr_sem);
936 up_read(&EXT4_I(inode)->xattr_sem);
940 ext4_journal_stop(handle);
946 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
947 unsigned len, unsigned copied,
952 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
961 * No need to use i_size_read() here, the i_size
962 * cannot change under us because we hold i_mutex.
964 * But it's important to update i_size while still holding page lock:
965 * page writeout could otherwise come in and zero beyond i_size.
967 if (pos+copied > inode->i_size)
968 i_size_write(inode, pos+copied);
973 * Don't mark the inode dirty under page lock. First, it unnecessarily
974 * makes the holding time of page lock longer. Second, it forces lock
975 * ordering of page lock and transaction start for journaling
978 mark_inode_dirty(inode);
983 #ifdef INLINE_DIR_DEBUG
984 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
985 void *inline_start, int inline_size)
988 unsigned short de_len;
989 struct ext4_dir_entry_2 *de = inline_start;
990 void *dlimit = inline_start + inline_size;
992 trace_printk("inode %lu\n", dir->i_ino);
994 while ((void *)de < dlimit) {
995 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
996 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
997 offset, de_len, de->name_len, de->name,
998 de->name_len, le32_to_cpu(de->inode));
999 if (ext4_check_dir_entry(dir, NULL, de, bh,
1000 inline_start, inline_size, offset))
1004 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1008 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1012 * Add a new entry into a inline dir.
1013 * It will return -ENOSPC if no space is available, and -EIO
1014 * and -EEXIST if directory entry already exists.
1016 static int ext4_add_dirent_to_inline(handle_t *handle,
1017 struct ext4_filename *fname,
1019 struct inode *inode,
1020 struct ext4_iloc *iloc,
1021 void *inline_start, int inline_size)
1024 struct ext4_dir_entry_2 *de;
1026 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1027 inline_size, fname, &de);
1031 BUFFER_TRACE(iloc->bh, "get_write_access");
1032 err = ext4_journal_get_write_access(handle, iloc->bh);
1035 ext4_insert_dentry(inode, de, inline_size, fname);
1037 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1040 * XXX shouldn't update any times until successful
1041 * completion of syscall, but too many callers depend
1044 * XXX similarly, too many callers depend on
1045 * ext4_new_inode() setting the times, but error
1046 * recovery deletes the inode, so the worst that can
1047 * happen is that the times are slightly out of date
1048 * and/or different from the directory change time.
1050 dir->i_mtime = dir->i_ctime = current_time(dir);
1051 ext4_update_dx_flag(dir);
1052 inode_inc_iversion(dir);
1056 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1057 struct ext4_iloc *iloc)
1059 struct ext4_xattr_entry *entry;
1060 struct ext4_xattr_ibody_header *header;
1062 BUG_ON(!EXT4_I(inode)->i_inline_off);
1064 header = IHDR(inode, ext4_raw_inode(iloc));
1065 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1066 EXT4_I(inode)->i_inline_off);
1068 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1071 /* Set the final de to cover the whole block. */
1072 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1074 struct ext4_dir_entry_2 *de, *prev_de;
1078 de = (struct ext4_dir_entry_2 *)de_buf;
1080 limit = de_buf + old_size;
1083 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1085 de = (struct ext4_dir_entry_2 *)de_buf;
1086 } while (de_buf < limit);
1088 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1089 old_size, new_size);
1091 /* this is just created, so create an empty entry. */
1093 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1097 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1098 struct ext4_iloc *iloc)
1101 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1102 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1104 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1107 ret = ext4_update_inline_data(handle, dir,
1108 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1112 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1113 EXT4_I(dir)->i_inline_size -
1114 EXT4_MIN_INLINE_DATA_SIZE);
1115 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1119 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1120 struct ext4_iloc *iloc,
1121 void *buf, int inline_size)
1123 ext4_create_inline_data(handle, inode, inline_size);
1124 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1125 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1128 static int ext4_finish_convert_inline_dir(handle_t *handle,
1129 struct inode *inode,
1130 struct buffer_head *dir_block,
1134 int err, csum_size = 0, header_size = 0;
1135 struct ext4_dir_entry_2 *de;
1136 void *target = dir_block->b_data;
1139 * First create "." and ".." and then copy the dir information
1140 * back to the block.
1142 de = (struct ext4_dir_entry_2 *)target;
1143 de = ext4_init_dot_dotdot(inode, de,
1144 inode->i_sb->s_blocksize, csum_size,
1145 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1146 header_size = (void *)de - target;
1148 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1149 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1151 if (ext4_has_metadata_csum(inode->i_sb))
1152 csum_size = sizeof(struct ext4_dir_entry_tail);
1154 inode->i_size = inode->i_sb->s_blocksize;
1155 i_size_write(inode, inode->i_sb->s_blocksize);
1156 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1157 ext4_update_final_de(dir_block->b_data,
1158 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1159 inode->i_sb->s_blocksize - csum_size);
1162 ext4_initialize_dirent_tail(dir_block,
1163 inode->i_sb->s_blocksize);
1164 set_buffer_uptodate(dir_block);
1165 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
1168 set_buffer_verified(dir_block);
1169 return ext4_mark_inode_dirty(handle, inode);
1172 static int ext4_convert_inline_data_nolock(handle_t *handle,
1173 struct inode *inode,
1174 struct ext4_iloc *iloc)
1178 struct buffer_head *data_bh = NULL;
1179 struct ext4_map_blocks map;
1182 inline_size = ext4_get_inline_size(inode);
1183 buf = kmalloc(inline_size, GFP_NOFS);
1189 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1194 * Make sure the inline directory entries pass checks before we try to
1195 * convert them, so that we avoid touching stuff that needs fsck.
1197 if (S_ISDIR(inode->i_mode)) {
1198 error = ext4_check_all_de(inode, iloc->bh,
1199 buf + EXT4_INLINE_DOTDOT_SIZE,
1200 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1205 error = ext4_destroy_inline_data_nolock(handle, inode);
1212 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1215 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1220 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1226 lock_buffer(data_bh);
1227 error = ext4_journal_get_create_access(handle, data_bh);
1229 unlock_buffer(data_bh);
1233 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1235 if (!S_ISDIR(inode->i_mode)) {
1236 memcpy(data_bh->b_data, buf, inline_size);
1237 set_buffer_uptodate(data_bh);
1238 error = ext4_handle_dirty_metadata(handle,
1241 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1245 unlock_buffer(data_bh);
1248 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1257 * Try to add the new entry to the inline data.
1258 * If succeeds, return 0. If not, extended the inline dir and copied data to
1259 * the new created block.
1261 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1262 struct inode *dir, struct inode *inode)
1264 int ret, inline_size, no_expand;
1266 struct ext4_iloc iloc;
1268 ret = ext4_get_inode_loc(dir, &iloc);
1272 ext4_write_lock_xattr(dir, &no_expand);
1273 if (!ext4_has_inline_data(dir))
1276 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1277 EXT4_INLINE_DOTDOT_SIZE;
1278 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1280 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1281 inline_start, inline_size);
1285 /* check whether it can be inserted to inline xattr space. */
1286 inline_size = EXT4_I(dir)->i_inline_size -
1287 EXT4_MIN_INLINE_DATA_SIZE;
1289 /* Try to use the xattr space.*/
1290 ret = ext4_update_inline_dir(handle, dir, &iloc);
1291 if (ret && ret != -ENOSPC)
1294 inline_size = EXT4_I(dir)->i_inline_size -
1295 EXT4_MIN_INLINE_DATA_SIZE;
1299 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1301 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1302 inode, &iloc, inline_start,
1310 * The inline space is filled up, so create a new block for it.
1311 * As the extent tree will be created, we have to save the inline
1314 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1317 ext4_write_unlock_xattr(dir, &no_expand);
1318 ext4_mark_inode_dirty(handle, dir);
1324 * This function fills a red-black tree with information from an
1325 * inlined dir. It returns the number directory entries loaded
1326 * into the tree. If there is an error it is returned in err.
1328 int ext4_inlinedir_to_tree(struct file *dir_file,
1329 struct inode *dir, ext4_lblk_t block,
1330 struct dx_hash_info *hinfo,
1331 __u32 start_hash, __u32 start_minor_hash,
1332 int *has_inline_data)
1334 int err = 0, count = 0;
1335 unsigned int parent_ino;
1337 struct ext4_dir_entry_2 *de;
1338 struct inode *inode = file_inode(dir_file);
1339 int ret, inline_size = 0;
1340 struct ext4_iloc iloc;
1341 void *dir_buf = NULL;
1342 struct ext4_dir_entry_2 fake;
1343 struct fscrypt_str tmp_str;
1345 ret = ext4_get_inode_loc(inode, &iloc);
1349 down_read(&EXT4_I(inode)->xattr_sem);
1350 if (!ext4_has_inline_data(inode)) {
1351 up_read(&EXT4_I(inode)->xattr_sem);
1352 *has_inline_data = 0;
1356 inline_size = ext4_get_inline_size(inode);
1357 dir_buf = kmalloc(inline_size, GFP_NOFS);
1360 up_read(&EXT4_I(inode)->xattr_sem);
1364 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1365 up_read(&EXT4_I(inode)->xattr_sem);
1370 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1371 while (pos < inline_size) {
1373 * As inlined dir doesn't store any information about '.' and
1374 * only the inode number of '..' is stored, we have to handle
1378 fake.inode = cpu_to_le32(inode->i_ino);
1380 strcpy(fake.name, ".");
1381 fake.rec_len = ext4_rec_len_to_disk(
1382 EXT4_DIR_REC_LEN(fake.name_len),
1384 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1386 pos = EXT4_INLINE_DOTDOT_OFFSET;
1387 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1388 fake.inode = cpu_to_le32(parent_ino);
1390 strcpy(fake.name, "..");
1391 fake.rec_len = ext4_rec_len_to_disk(
1392 EXT4_DIR_REC_LEN(fake.name_len),
1394 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1396 pos = EXT4_INLINE_DOTDOT_SIZE;
1398 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1399 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1400 if (ext4_check_dir_entry(inode, dir_file, de,
1402 inline_size, pos)) {
1408 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1409 if ((hinfo->hash < start_hash) ||
1410 ((hinfo->hash == start_hash) &&
1411 (hinfo->minor_hash < start_minor_hash)))
1415 tmp_str.name = de->name;
1416 tmp_str.len = de->name_len;
1417 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1418 hinfo->minor_hash, de, &tmp_str);
1433 * So this function is called when the volume is mkfsed with
1434 * dir_index disabled. In order to keep f_pos persistent
1435 * after we convert from an inlined dir to a blocked based,
1436 * we just pretend that we are a normal dir and return the
1437 * offset as if '.' and '..' really take place.
1440 int ext4_read_inline_dir(struct file *file,
1441 struct dir_context *ctx,
1442 int *has_inline_data)
1444 unsigned int offset, parent_ino;
1446 struct ext4_dir_entry_2 *de;
1447 struct super_block *sb;
1448 struct inode *inode = file_inode(file);
1449 int ret, inline_size = 0;
1450 struct ext4_iloc iloc;
1451 void *dir_buf = NULL;
1452 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1454 ret = ext4_get_inode_loc(inode, &iloc);
1458 down_read(&EXT4_I(inode)->xattr_sem);
1459 if (!ext4_has_inline_data(inode)) {
1460 up_read(&EXT4_I(inode)->xattr_sem);
1461 *has_inline_data = 0;
1465 inline_size = ext4_get_inline_size(inode);
1466 dir_buf = kmalloc(inline_size, GFP_NOFS);
1469 up_read(&EXT4_I(inode)->xattr_sem);
1473 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1474 up_read(&EXT4_I(inode)->xattr_sem);
1480 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1484 * dotdot_offset and dotdot_size is the real offset and
1485 * size for ".." and "." if the dir is block based while
1486 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1487 * So we will use extra_offset and extra_size to indicate them
1488 * during the inline dir iteration.
1490 dotdot_offset = EXT4_DIR_REC_LEN(1);
1491 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1492 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1493 extra_size = extra_offset + inline_size;
1496 * If the version has changed since the last call to
1497 * readdir(2), then we might be pointing to an invalid
1498 * dirent right now. Scan from the start of the inline
1501 if (!inode_eq_iversion(inode, file->f_version)) {
1502 for (i = 0; i < extra_size && i < offset;) {
1504 * "." is with offset 0 and
1505 * ".." is dotdot_offset.
1510 } else if (i == dotdot_offset) {
1514 /* for other entry, the real offset in
1515 * the buf has to be tuned accordingly.
1517 de = (struct ext4_dir_entry_2 *)
1518 (dir_buf + i - extra_offset);
1519 /* It's too expensive to do a full
1520 * dirent test each time round this
1521 * loop, but we do have to test at
1522 * least that it is non-zero. A
1523 * failure will be detected in the
1524 * dirent test below. */
1525 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1526 < EXT4_DIR_REC_LEN(1))
1528 i += ext4_rec_len_from_disk(de->rec_len,
1533 file->f_version = inode_query_iversion(inode);
1536 while (ctx->pos < extra_size) {
1537 if (ctx->pos == 0) {
1538 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1540 ctx->pos = dotdot_offset;
1544 if (ctx->pos == dotdot_offset) {
1545 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1547 ctx->pos = dotdot_size;
1551 de = (struct ext4_dir_entry_2 *)
1552 (dir_buf + ctx->pos - extra_offset);
1553 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1554 extra_size, ctx->pos))
1556 if (le32_to_cpu(de->inode)) {
1557 if (!dir_emit(ctx, de->name, de->name_len,
1558 le32_to_cpu(de->inode),
1559 get_dtype(sb, de->file_type)))
1562 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1570 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1571 struct ext4_dir_entry_2 **parent_de,
1574 struct ext4_iloc iloc;
1576 *retval = ext4_get_inode_loc(inode, &iloc);
1580 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1586 * Try to create the inline data for the new dir.
1587 * If it succeeds, return 0, otherwise return the error.
1588 * In case of ENOSPC, the caller should create the normal disk layout dir.
1590 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1591 struct inode *inode)
1593 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1594 struct ext4_iloc iloc;
1595 struct ext4_dir_entry_2 *de;
1597 ret = ext4_get_inode_loc(inode, &iloc);
1601 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1606 * For inline dir, we only save the inode information for the ".."
1607 * and create a fake dentry to cover the left space.
1609 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1610 de->inode = cpu_to_le32(parent->i_ino);
1611 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1613 de->rec_len = ext4_rec_len_to_disk(
1614 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1616 set_nlink(inode, 2);
1617 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1623 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1624 struct ext4_filename *fname,
1625 struct ext4_dir_entry_2 **res_dir,
1626 int *has_inline_data)
1629 struct ext4_iloc iloc;
1633 if (ext4_get_inode_loc(dir, &iloc))
1636 down_read(&EXT4_I(dir)->xattr_sem);
1637 if (!ext4_has_inline_data(dir)) {
1638 *has_inline_data = 0;
1642 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1643 EXT4_INLINE_DOTDOT_SIZE;
1644 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1645 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1646 dir, fname, 0, res_dir);
1652 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1655 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1656 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1658 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1659 dir, fname, 0, res_dir);
1667 up_read(&EXT4_I(dir)->xattr_sem);
1671 int ext4_delete_inline_entry(handle_t *handle,
1673 struct ext4_dir_entry_2 *de_del,
1674 struct buffer_head *bh,
1675 int *has_inline_data)
1677 int err, inline_size, no_expand;
1678 struct ext4_iloc iloc;
1681 err = ext4_get_inode_loc(dir, &iloc);
1685 ext4_write_lock_xattr(dir, &no_expand);
1686 if (!ext4_has_inline_data(dir)) {
1687 *has_inline_data = 0;
1691 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1692 EXT4_MIN_INLINE_DATA_SIZE) {
1693 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1694 EXT4_INLINE_DOTDOT_SIZE;
1695 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1696 EXT4_INLINE_DOTDOT_SIZE;
1698 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1699 inline_size = ext4_get_inline_size(dir) -
1700 EXT4_MIN_INLINE_DATA_SIZE;
1703 BUFFER_TRACE(bh, "get_write_access");
1704 err = ext4_journal_get_write_access(handle, bh);
1708 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1709 inline_start, inline_size, 0);
1713 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1715 ext4_write_unlock_xattr(dir, &no_expand);
1716 if (likely(err == 0))
1717 err = ext4_mark_inode_dirty(handle, dir);
1720 ext4_std_error(dir->i_sb, err);
1725 * Get the inline dentry at offset.
1727 static inline struct ext4_dir_entry_2 *
1728 ext4_get_inline_entry(struct inode *inode,
1729 struct ext4_iloc *iloc,
1730 unsigned int offset,
1731 void **inline_start,
1736 BUG_ON(offset > ext4_get_inline_size(inode));
1738 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1739 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1740 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1742 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1743 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1744 *inline_size = ext4_get_inline_size(inode) -
1745 EXT4_MIN_INLINE_DATA_SIZE;
1749 *inline_start = inline_pos;
1750 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1753 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1755 int err, inline_size;
1756 struct ext4_iloc iloc;
1759 unsigned int offset;
1760 struct ext4_dir_entry_2 *de;
1763 err = ext4_get_inode_loc(dir, &iloc);
1765 ext4_set_errno(dir->i_sb, -err);
1766 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1771 down_read(&EXT4_I(dir)->xattr_sem);
1772 if (!ext4_has_inline_data(dir)) {
1773 *has_inline_data = 0;
1777 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1778 if (!le32_to_cpu(de->inode)) {
1779 ext4_warning(dir->i_sb,
1780 "bad inline directory (dir #%lu) - no `..'",
1786 inline_len = ext4_get_inline_size(dir);
1787 offset = EXT4_INLINE_DOTDOT_SIZE;
1788 while (offset < inline_len) {
1789 de = ext4_get_inline_entry(dir, &iloc, offset,
1790 &inline_pos, &inline_size);
1791 if (ext4_check_dir_entry(dir, NULL, de,
1792 iloc.bh, inline_pos,
1793 inline_size, offset)) {
1794 ext4_warning(dir->i_sb,
1795 "bad inline directory (dir #%lu) - "
1796 "inode %u, rec_len %u, name_len %d"
1798 dir->i_ino, le32_to_cpu(de->inode),
1799 le16_to_cpu(de->rec_len), de->name_len,
1804 if (le32_to_cpu(de->inode)) {
1808 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1812 up_read(&EXT4_I(dir)->xattr_sem);
1817 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1821 ext4_write_lock_xattr(inode, &no_expand);
1822 ret = ext4_destroy_inline_data_nolock(handle, inode);
1823 ext4_write_unlock_xattr(inode, &no_expand);
1828 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1831 int error = -EAGAIN;
1832 struct ext4_iloc iloc;
1834 down_read(&EXT4_I(inode)->xattr_sem);
1835 if (!ext4_has_inline_data(inode))
1838 error = ext4_get_inode_loc(inode, &iloc);
1842 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1843 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1844 addr += offsetof(struct ext4_inode, i_block);
1850 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1851 i_size_read(inode));
1852 iomap->type = IOMAP_INLINE;
1856 up_read(&EXT4_I(inode)->xattr_sem);
1860 int ext4_inline_data_fiemap(struct inode *inode,
1861 struct fiemap_extent_info *fieinfo,
1862 int *has_inline, __u64 start, __u64 len)
1866 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1869 struct ext4_iloc iloc;
1871 down_read(&EXT4_I(inode)->xattr_sem);
1872 if (!ext4_has_inline_data(inode)) {
1876 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1877 i_size_read(inode));
1878 if (start >= inline_len)
1880 if (start + len < inline_len)
1881 inline_len = start + len;
1882 inline_len -= start;
1884 error = ext4_get_inode_loc(inode, &iloc);
1888 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1889 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1890 physical += offsetof(struct ext4_inode, i_block);
1894 up_read(&EXT4_I(inode)->xattr_sem);
1896 error = fiemap_fill_next_extent(fieinfo, start, physical,
1898 return (error < 0 ? error : 0);
1901 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1904 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1907 struct ext4_xattr_ibody_find is = {
1908 .s = { .not_found = -ENODATA, },
1910 struct ext4_xattr_info i = {
1911 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1912 .name = EXT4_XATTR_SYSTEM_DATA,
1916 needed_blocks = ext4_writepage_trans_blocks(inode);
1917 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1919 return PTR_ERR(handle);
1921 ext4_write_lock_xattr(inode, &no_expand);
1922 if (!ext4_has_inline_data(inode)) {
1924 ext4_journal_stop(handle);
1928 if ((err = ext4_orphan_add(handle, inode)) != 0)
1931 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1934 down_write(&EXT4_I(inode)->i_data_sem);
1935 i_size = inode->i_size;
1936 inline_size = ext4_get_inline_size(inode);
1937 EXT4_I(inode)->i_disksize = i_size;
1939 if (i_size < inline_size) {
1940 /* Clear the content in the xattr space. */
1941 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1942 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1945 BUG_ON(is.s.not_found);
1947 value_len = le32_to_cpu(is.s.here->e_value_size);
1948 value = kmalloc(value_len, GFP_NOFS);
1954 err = ext4_xattr_ibody_get(inode, i.name_index,
1955 i.name, value, value_len);
1960 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1961 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1962 err = ext4_xattr_ibody_inline_set(handle, inode,
1968 /* Clear the content within i_blocks. */
1969 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1970 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1971 memset(p + i_size, 0,
1972 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1975 EXT4_I(inode)->i_inline_size = i_size <
1976 EXT4_MIN_INLINE_DATA_SIZE ?
1977 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1981 up_write(&EXT4_I(inode)->i_data_sem);
1984 ext4_write_unlock_xattr(inode, &no_expand);
1987 ext4_orphan_del(handle, inode);
1990 inode->i_mtime = inode->i_ctime = current_time(inode);
1991 err = ext4_mark_inode_dirty(handle, inode);
1993 ext4_handle_sync(handle);
1995 ext4_journal_stop(handle);
1999 int ext4_convert_inline_data(struct inode *inode)
2001 int error, needed_blocks, no_expand;
2003 struct ext4_iloc iloc;
2005 if (!ext4_has_inline_data(inode)) {
2006 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2010 needed_blocks = ext4_writepage_trans_blocks(inode);
2013 error = ext4_get_inode_loc(inode, &iloc);
2017 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2018 if (IS_ERR(handle)) {
2019 error = PTR_ERR(handle);
2023 ext4_write_lock_xattr(inode, &no_expand);
2024 if (ext4_has_inline_data(inode))
2025 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2026 ext4_write_unlock_xattr(inode, &no_expand);
2027 ext4_journal_stop(handle);