1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (c) 2012 Taobao.
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;
39 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
42 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
43 EXT4_GOOD_OLD_INODE_SIZE -
44 EXT4_I(inode)->i_extra_isize -
45 sizeof(struct ext4_xattr_ibody_header);
48 * We need to subtract another sizeof(__u32) since an in-inode xattr
49 * needs an empty 4 bytes to indicate the gap between the xattr entry
50 * and the name/value pair.
52 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
53 return EXT4_XATTR_SIZE(min_offs -
54 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
55 EXT4_XATTR_ROUND - sizeof(__u32));
57 raw_inode = ext4_raw_inode(iloc);
58 header = IHDR(inode, raw_inode);
59 entry = IFIRST(header);
61 /* Compute min_offs. */
62 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
63 if (!entry->e_value_inum && entry->e_value_size) {
64 size_t offs = le16_to_cpu(entry->e_value_offs);
70 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
72 if (EXT4_I(inode)->i_inline_off) {
73 entry = (struct ext4_xattr_entry *)
74 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
76 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
80 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
82 if (free > EXT4_XATTR_ROUND)
83 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
92 * Get the maximum size we now can store in an inode.
93 * If we can't find the space for a xattr entry, don't use the space
94 * of the extents since we have no space to indicate the inline data.
96 int ext4_get_max_inline_size(struct inode *inode)
98 int error, max_inline_size;
99 struct ext4_iloc iloc;
101 if (EXT4_I(inode)->i_extra_isize == 0)
104 error = ext4_get_inode_loc(inode, &iloc);
106 ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
107 "can't get inode location %lu",
112 down_read(&EXT4_I(inode)->xattr_sem);
113 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
114 up_read(&EXT4_I(inode)->xattr_sem);
118 if (!max_inline_size)
121 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
125 * this function does not take xattr_sem, which is OK because it is
126 * currently only used in a code path coming form ext4_iget, before
127 * the new inode has been unlocked
129 int ext4_find_inline_data_nolock(struct inode *inode)
131 struct ext4_xattr_ibody_find is = {
132 .s = { .not_found = -ENODATA, },
134 struct ext4_xattr_info i = {
135 .name_index = EXT4_XATTR_INDEX_SYSTEM,
136 .name = EXT4_XATTR_SYSTEM_DATA,
140 if (EXT4_I(inode)->i_extra_isize == 0)
143 error = ext4_get_inode_loc(inode, &is.iloc);
147 error = ext4_xattr_ibody_find(inode, &i, &is);
151 if (!is.s.not_found) {
152 if (is.s.here->e_value_inum) {
153 EXT4_ERROR_INODE(inode, "inline data xattr refers "
154 "to an external xattr inode");
155 error = -EFSCORRUPTED;
158 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
159 (void *)ext4_raw_inode(&is.iloc));
160 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
161 le32_to_cpu(is.s.here->e_value_size);
162 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
169 static int ext4_read_inline_data(struct inode *inode, void *buffer,
171 struct ext4_iloc *iloc)
173 struct ext4_xattr_entry *entry;
174 struct ext4_xattr_ibody_header *header;
176 struct ext4_inode *raw_inode;
181 BUG_ON(len > EXT4_I(inode)->i_inline_size);
183 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
184 len : EXT4_MIN_INLINE_DATA_SIZE;
186 raw_inode = ext4_raw_inode(iloc);
187 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
195 header = IHDR(inode, raw_inode);
196 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
197 EXT4_I(inode)->i_inline_off);
198 len = min_t(unsigned int, len,
199 (unsigned int)le32_to_cpu(entry->e_value_size));
202 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
210 * write the buffer to the inline inode.
211 * If 'create' is set, we don't need to do the extra copy in the xattr
212 * value since it is already handled by ext4_xattr_ibody_set.
213 * That saves us one memcpy.
215 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
216 void *buffer, loff_t pos, unsigned int len)
218 struct ext4_xattr_entry *entry;
219 struct ext4_xattr_ibody_header *header;
220 struct ext4_inode *raw_inode;
223 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
226 BUG_ON(!EXT4_I(inode)->i_inline_off);
227 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
229 raw_inode = ext4_raw_inode(iloc);
232 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
233 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
234 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
235 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
245 pos -= EXT4_MIN_INLINE_DATA_SIZE;
246 header = IHDR(inode, raw_inode);
247 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
248 EXT4_I(inode)->i_inline_off);
250 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
254 static int ext4_create_inline_data(handle_t *handle,
255 struct inode *inode, unsigned len)
259 struct ext4_xattr_ibody_find is = {
260 .s = { .not_found = -ENODATA, },
262 struct ext4_xattr_info i = {
263 .name_index = EXT4_XATTR_INDEX_SYSTEM,
264 .name = EXT4_XATTR_SYSTEM_DATA,
267 error = ext4_get_inode_loc(inode, &is.iloc);
271 BUFFER_TRACE(is.iloc.bh, "get_write_access");
272 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
277 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
278 value = EXT4_ZERO_XATTR_VALUE;
279 len -= EXT4_MIN_INLINE_DATA_SIZE;
285 /* Insert the xttr entry. */
289 error = ext4_xattr_ibody_find(inode, &i, &is);
293 BUG_ON(!is.s.not_found);
295 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
297 if (error == -ENOSPC)
298 ext4_clear_inode_state(inode,
299 EXT4_STATE_MAY_INLINE_DATA);
303 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
304 0, EXT4_MIN_INLINE_DATA_SIZE);
306 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
307 (void *)ext4_raw_inode(&is.iloc));
308 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
309 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
310 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
312 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
319 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
324 struct ext4_xattr_ibody_find is = {
325 .s = { .not_found = -ENODATA, },
327 struct ext4_xattr_info i = {
328 .name_index = EXT4_XATTR_INDEX_SYSTEM,
329 .name = EXT4_XATTR_SYSTEM_DATA,
332 /* If the old space is ok, write the data directly. */
333 if (len <= EXT4_I(inode)->i_inline_size)
336 error = ext4_get_inode_loc(inode, &is.iloc);
340 error = ext4_xattr_ibody_find(inode, &i, &is);
344 BUG_ON(is.s.not_found);
346 len -= EXT4_MIN_INLINE_DATA_SIZE;
347 value = kzalloc(len, GFP_NOFS);
353 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
355 if (error == -ENODATA)
358 BUFFER_TRACE(is.iloc.bh, "get_write_access");
359 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
364 /* Update the xattr entry. */
368 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
372 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
373 (void *)ext4_raw_inode(&is.iloc));
374 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
375 le32_to_cpu(is.s.here->e_value_size);
376 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
378 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
386 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
389 int ret, size, no_expand;
390 struct ext4_inode_info *ei = EXT4_I(inode);
392 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
395 size = ext4_get_max_inline_size(inode);
399 ext4_write_lock_xattr(inode, &no_expand);
401 if (ei->i_inline_off)
402 ret = ext4_update_inline_data(handle, inode, len);
404 ret = ext4_create_inline_data(handle, inode, len);
406 ext4_write_unlock_xattr(inode, &no_expand);
410 static int ext4_destroy_inline_data_nolock(handle_t *handle,
413 struct ext4_inode_info *ei = EXT4_I(inode);
414 struct ext4_xattr_ibody_find is = {
415 .s = { .not_found = 0, },
417 struct ext4_xattr_info i = {
418 .name_index = EXT4_XATTR_INDEX_SYSTEM,
419 .name = EXT4_XATTR_SYSTEM_DATA,
425 if (!ei->i_inline_off)
428 error = ext4_get_inode_loc(inode, &is.iloc);
432 error = ext4_xattr_ibody_find(inode, &i, &is);
436 BUFFER_TRACE(is.iloc.bh, "get_write_access");
437 error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
442 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
446 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
447 0, EXT4_MIN_INLINE_DATA_SIZE);
448 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
450 if (ext4_has_feature_extents(inode->i_sb)) {
451 if (S_ISDIR(inode->i_mode) ||
452 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
453 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
454 ext4_ext_tree_init(handle, inode);
457 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
460 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
462 EXT4_I(inode)->i_inline_off = 0;
463 EXT4_I(inode)->i_inline_size = 0;
464 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
467 if (error == -ENODATA)
472 static int ext4_read_inline_page(struct inode *inode, struct page *page)
477 struct ext4_iloc iloc;
479 BUG_ON(!PageLocked(page));
480 BUG_ON(!ext4_has_inline_data(inode));
483 if (!EXT4_I(inode)->i_inline_off) {
484 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
489 ret = ext4_get_inode_loc(inode, &iloc);
493 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
494 kaddr = kmap_atomic(page);
495 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
496 flush_dcache_page(page);
497 kunmap_atomic(kaddr);
498 zero_user_segment(page, len, PAGE_SIZE);
499 SetPageUptodate(page);
506 int ext4_readpage_inline(struct inode *inode, struct page *page)
510 down_read(&EXT4_I(inode)->xattr_sem);
511 if (!ext4_has_inline_data(inode)) {
512 up_read(&EXT4_I(inode)->xattr_sem);
517 * Current inline data can only exist in the 1st page,
518 * So for all the other pages, just set them uptodate.
521 ret = ext4_read_inline_page(inode, page);
522 else if (!PageUptodate(page)) {
523 zero_user_segment(page, 0, PAGE_SIZE);
524 SetPageUptodate(page);
527 up_read(&EXT4_I(inode)->xattr_sem);
530 return ret >= 0 ? 0 : ret;
533 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
536 int ret, needed_blocks, no_expand;
537 handle_t *handle = NULL;
538 int retries = 0, sem_held = 0;
539 struct page *page = NULL;
542 struct ext4_iloc iloc;
544 if (!ext4_has_inline_data(inode)) {
546 * clear the flag so that no new write
547 * will trap here again.
549 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
553 needed_blocks = ext4_writepage_trans_blocks(inode);
555 ret = ext4_get_inode_loc(inode, &iloc);
560 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
561 if (IS_ERR(handle)) {
562 ret = PTR_ERR(handle);
567 /* We cannot recurse into the filesystem as the transaction is already
569 flags = memalloc_nofs_save();
570 page = grab_cache_page_write_begin(mapping, 0);
571 memalloc_nofs_restore(flags);
577 ext4_write_lock_xattr(inode, &no_expand);
579 /* If some one has already done this for us, just exit. */
580 if (!ext4_has_inline_data(inode)) {
586 to = ext4_get_inline_size(inode);
587 if (!PageUptodate(page)) {
588 ret = ext4_read_inline_page(inode, page);
593 ret = ext4_destroy_inline_data_nolock(handle, inode);
597 if (ext4_should_dioread_nolock(inode)) {
598 ret = __block_write_begin(page, from, to,
599 ext4_get_block_unwritten);
601 ret = __block_write_begin(page, from, to, ext4_get_block);
603 if (!ret && ext4_should_journal_data(inode)) {
604 ret = ext4_walk_page_buffers(handle, inode, page_buffers(page),
606 do_journal_get_write_access);
613 ext4_orphan_add(handle, inode);
614 ext4_write_unlock_xattr(inode, &no_expand);
616 ext4_journal_stop(handle);
618 ext4_truncate_failed_write(inode);
620 * If truncate failed early the inode might
621 * still be on the orphan list; we need to
622 * make sure the inode is removed from the
623 * orphan list in that case.
626 ext4_orphan_del(NULL, inode);
629 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
633 block_commit_write(page, from, to);
640 ext4_write_unlock_xattr(inode, &no_expand);
642 ext4_journal_stop(handle);
648 * Try to write data in the inode.
649 * If the inode has inline data, check whether the new write can be
650 * in the inode also. If not, create the page the handle, move the data
651 * to the page make it update and let the later codes create extent for it.
653 int ext4_try_to_write_inline_data(struct address_space *mapping,
655 loff_t pos, unsigned len,
662 struct ext4_iloc iloc;
664 if (pos + len > ext4_get_max_inline_size(inode))
667 ret = ext4_get_inode_loc(inode, &iloc);
672 * The possible write could happen in the inode,
673 * so try to reserve the space in inode first.
675 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
676 if (IS_ERR(handle)) {
677 ret = PTR_ERR(handle);
682 ret = ext4_prepare_inline_data(handle, inode, pos + len);
683 if (ret && ret != -ENOSPC)
686 /* We don't have space in inline inode, so convert it to extent. */
687 if (ret == -ENOSPC) {
688 ext4_journal_stop(handle);
693 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
698 flags = memalloc_nofs_save();
699 page = grab_cache_page_write_begin(mapping, 0);
700 memalloc_nofs_restore(flags);
707 down_read(&EXT4_I(inode)->xattr_sem);
708 if (!ext4_has_inline_data(inode)) {
715 if (!PageUptodate(page)) {
716 ret = ext4_read_inline_page(inode, page);
727 up_read(&EXT4_I(inode)->xattr_sem);
729 if (handle && (ret != 1))
730 ext4_journal_stop(handle);
734 return ext4_convert_inline_data_to_extent(mapping, inode);
737 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
738 unsigned copied, struct page *page)
740 handle_t *handle = ext4_journal_current_handle();
743 struct ext4_iloc iloc;
746 if (unlikely(copied < len) && !PageUptodate(page))
749 if (likely(copied)) {
750 ret = ext4_get_inode_loc(inode, &iloc);
754 ext4_std_error(inode->i_sb, ret);
757 ext4_write_lock_xattr(inode, &no_expand);
758 BUG_ON(!ext4_has_inline_data(inode));
761 * ei->i_inline_off may have changed since
762 * ext4_write_begin() called
763 * ext4_try_to_write_inline_data()
765 (void) ext4_find_inline_data_nolock(inode);
767 kaddr = kmap_atomic(page);
768 ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
769 kunmap_atomic(kaddr);
770 SetPageUptodate(page);
771 /* clear page dirty so that writepages wouldn't work for us. */
772 ClearPageDirty(page);
774 ext4_write_unlock_xattr(inode, &no_expand);
778 * It's important to update i_size while still holding page
779 * lock: page writeout could otherwise come in and zero
782 ext4_update_inode_size(inode, pos + copied);
788 * Don't mark the inode dirty under page lock. First, it unnecessarily
789 * makes the holding time of page lock longer. Second, it forces lock
790 * ordering of page lock and transaction start for journaling
794 mark_inode_dirty(inode);
797 * If we didn't copy as much data as expected, we need to trim back
798 * size of xattr containing inline data.
800 if (pos + len > inode->i_size && ext4_can_truncate(inode))
801 ext4_orphan_add(handle, inode);
803 ret2 = ext4_journal_stop(handle);
806 if (pos + len > inode->i_size) {
807 ext4_truncate_failed_write(inode);
809 * If truncate failed early the inode might still be
810 * on the orphan list; we need to make sure the inode
811 * is removed from the orphan list in that case.
814 ext4_orphan_del(NULL, inode);
816 return ret ? ret : copied;
820 ext4_journalled_write_inline_data(struct inode *inode,
826 struct ext4_iloc iloc;
828 ret = ext4_get_inode_loc(inode, &iloc);
830 ext4_std_error(inode->i_sb, ret);
834 ext4_write_lock_xattr(inode, &no_expand);
835 kaddr = kmap_atomic(page);
836 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
837 kunmap_atomic(kaddr);
838 ext4_write_unlock_xattr(inode, &no_expand);
844 * Try to make the page cache and handle ready for the inline data case.
845 * We can call this function in 2 cases:
846 * 1. The inode is created and the first write exceeds inline size. We can
847 * clear the inode state safely.
848 * 2. The inode has inline data, then we need to read the data, make it
849 * update and dirty so that ext4_da_writepages can handle it. We don't
850 * need to start the journal since the file's metadata isn't changed now.
852 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
856 int ret = 0, inline_size;
859 page = grab_cache_page_write_begin(mapping, 0);
863 down_read(&EXT4_I(inode)->xattr_sem);
864 if (!ext4_has_inline_data(inode)) {
865 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
869 inline_size = ext4_get_inline_size(inode);
871 if (!PageUptodate(page)) {
872 ret = ext4_read_inline_page(inode, page);
877 ret = __block_write_begin(page, 0, inline_size,
878 ext4_da_get_block_prep);
880 up_read(&EXT4_I(inode)->xattr_sem);
883 ext4_truncate_failed_write(inode);
888 SetPageUptodate(page);
889 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
890 *fsdata = (void *)CONVERT_INLINE_DATA;
893 up_read(&EXT4_I(inode)->xattr_sem);
902 * Prepare the write for the inline data.
903 * If the data can be written into the inode, we just read
904 * the page and make it uptodate, and start the journal.
905 * Otherwise read the page, makes it dirty so that it can be
906 * handle in writepages(the i_disksize update is left to the
907 * normal ext4_da_write_end).
909 int ext4_da_write_inline_data_begin(struct address_space *mapping,
911 loff_t pos, unsigned len,
918 struct ext4_iloc iloc;
922 ret = ext4_get_inode_loc(inode, &iloc);
927 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
928 if (IS_ERR(handle)) {
929 ret = PTR_ERR(handle);
933 ret = ext4_prepare_inline_data(handle, inode, pos + len);
934 if (ret && ret != -ENOSPC)
937 if (ret == -ENOSPC) {
938 ext4_journal_stop(handle);
939 ret = ext4_da_convert_inline_data_to_extent(mapping,
942 if (ret == -ENOSPC &&
943 ext4_should_retry_alloc(inode->i_sb, &retries))
949 * We cannot recurse into the filesystem as the transaction
950 * is already started.
952 flags = memalloc_nofs_save();
953 page = grab_cache_page_write_begin(mapping, 0);
954 memalloc_nofs_restore(flags);
960 down_read(&EXT4_I(inode)->xattr_sem);
961 if (!ext4_has_inline_data(inode)) {
963 goto out_release_page;
966 if (!PageUptodate(page)) {
967 ret = ext4_read_inline_page(inode, page);
969 goto out_release_page;
971 ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
974 goto out_release_page;
976 up_read(&EXT4_I(inode)->xattr_sem);
981 up_read(&EXT4_I(inode)->xattr_sem);
985 ext4_journal_stop(handle);
991 #ifdef INLINE_DIR_DEBUG
992 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
993 void *inline_start, int inline_size)
996 unsigned short de_len;
997 struct ext4_dir_entry_2 *de = inline_start;
998 void *dlimit = inline_start + inline_size;
1000 trace_printk("inode %lu\n", dir->i_ino);
1002 while ((void *)de < dlimit) {
1003 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1004 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1005 offset, de_len, de->name_len, de->name,
1006 de->name_len, le32_to_cpu(de->inode));
1007 if (ext4_check_dir_entry(dir, NULL, de, bh,
1008 inline_start, inline_size, offset))
1012 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1016 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1020 * Add a new entry into a inline dir.
1021 * It will return -ENOSPC if no space is available, and -EIO
1022 * and -EEXIST if directory entry already exists.
1024 static int ext4_add_dirent_to_inline(handle_t *handle,
1025 struct ext4_filename *fname,
1027 struct inode *inode,
1028 struct ext4_iloc *iloc,
1029 void *inline_start, int inline_size)
1032 struct ext4_dir_entry_2 *de;
1034 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1035 inline_size, fname, &de);
1039 BUFFER_TRACE(iloc->bh, "get_write_access");
1040 err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
1044 ext4_insert_dentry(dir, inode, de, inline_size, fname);
1046 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1049 * XXX shouldn't update any times until successful
1050 * completion of syscall, but too many callers depend
1053 * XXX similarly, too many callers depend on
1054 * ext4_new_inode() setting the times, but error
1055 * recovery deletes the inode, so the worst that can
1056 * happen is that the times are slightly out of date
1057 * and/or different from the directory change time.
1059 dir->i_mtime = dir->i_ctime = current_time(dir);
1060 ext4_update_dx_flag(dir);
1061 inode_inc_iversion(dir);
1065 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1066 struct ext4_iloc *iloc)
1068 struct ext4_xattr_entry *entry;
1069 struct ext4_xattr_ibody_header *header;
1071 BUG_ON(!EXT4_I(inode)->i_inline_off);
1073 header = IHDR(inode, ext4_raw_inode(iloc));
1074 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1075 EXT4_I(inode)->i_inline_off);
1077 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1080 /* Set the final de to cover the whole block. */
1081 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1083 struct ext4_dir_entry_2 *de, *prev_de;
1089 limit = de_buf + old_size;
1092 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1095 } while (de_buf < limit);
1097 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1098 old_size, new_size);
1100 /* this is just created, so create an empty entry. */
1102 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1106 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1107 struct ext4_iloc *iloc)
1110 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1111 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1113 if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
1116 ret = ext4_update_inline_data(handle, dir,
1117 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1121 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1122 EXT4_I(dir)->i_inline_size -
1123 EXT4_MIN_INLINE_DATA_SIZE);
1124 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1128 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1129 struct ext4_iloc *iloc,
1130 void *buf, int inline_size)
1134 ret = ext4_create_inline_data(handle, inode, inline_size);
1136 ext4_msg(inode->i_sb, KERN_EMERG,
1137 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1141 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1142 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1145 static int ext4_finish_convert_inline_dir(handle_t *handle,
1146 struct inode *inode,
1147 struct buffer_head *dir_block,
1151 int err, csum_size = 0, header_size = 0;
1152 struct ext4_dir_entry_2 *de;
1153 void *target = dir_block->b_data;
1156 * First create "." and ".." and then copy the dir information
1157 * back to the block.
1160 de = ext4_init_dot_dotdot(inode, de,
1161 inode->i_sb->s_blocksize, csum_size,
1162 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1163 header_size = (void *)de - target;
1165 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1166 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1168 if (ext4_has_metadata_csum(inode->i_sb))
1169 csum_size = sizeof(struct ext4_dir_entry_tail);
1171 inode->i_size = inode->i_sb->s_blocksize;
1172 i_size_write(inode, inode->i_sb->s_blocksize);
1173 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1174 ext4_update_final_de(dir_block->b_data,
1175 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1176 inode->i_sb->s_blocksize - csum_size);
1179 ext4_initialize_dirent_tail(dir_block,
1180 inode->i_sb->s_blocksize);
1181 set_buffer_uptodate(dir_block);
1182 err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
1185 set_buffer_verified(dir_block);
1186 return ext4_mark_inode_dirty(handle, inode);
1189 static int ext4_convert_inline_data_nolock(handle_t *handle,
1190 struct inode *inode,
1191 struct ext4_iloc *iloc)
1195 struct buffer_head *data_bh = NULL;
1196 struct ext4_map_blocks map;
1199 inline_size = ext4_get_inline_size(inode);
1200 buf = kmalloc(inline_size, GFP_NOFS);
1206 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1211 * Make sure the inline directory entries pass checks before we try to
1212 * convert them, so that we avoid touching stuff that needs fsck.
1214 if (S_ISDIR(inode->i_mode)) {
1215 error = ext4_check_all_de(inode, iloc->bh,
1216 buf + EXT4_INLINE_DOTDOT_SIZE,
1217 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1222 error = ext4_destroy_inline_data_nolock(handle, inode);
1229 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1232 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1237 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1243 lock_buffer(data_bh);
1244 error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1247 unlock_buffer(data_bh);
1251 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1253 if (!S_ISDIR(inode->i_mode)) {
1254 memcpy(data_bh->b_data, buf, inline_size);
1255 set_buffer_uptodate(data_bh);
1256 error = ext4_handle_dirty_metadata(handle,
1259 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1263 unlock_buffer(data_bh);
1266 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1275 * Try to add the new entry to the inline data.
1276 * If succeeds, return 0. If not, extended the inline dir and copied data to
1277 * the new created block.
1279 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1280 struct inode *dir, struct inode *inode)
1282 int ret, ret2, inline_size, no_expand;
1284 struct ext4_iloc iloc;
1286 ret = ext4_get_inode_loc(dir, &iloc);
1290 ext4_write_lock_xattr(dir, &no_expand);
1291 if (!ext4_has_inline_data(dir))
1294 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1295 EXT4_INLINE_DOTDOT_SIZE;
1296 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1298 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1299 inline_start, inline_size);
1303 /* check whether it can be inserted to inline xattr space. */
1304 inline_size = EXT4_I(dir)->i_inline_size -
1305 EXT4_MIN_INLINE_DATA_SIZE;
1307 /* Try to use the xattr space.*/
1308 ret = ext4_update_inline_dir(handle, dir, &iloc);
1309 if (ret && ret != -ENOSPC)
1312 inline_size = EXT4_I(dir)->i_inline_size -
1313 EXT4_MIN_INLINE_DATA_SIZE;
1317 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1319 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1320 inode, &iloc, inline_start,
1328 * The inline space is filled up, so create a new block for it.
1329 * As the extent tree will be created, we have to save the inline
1332 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1335 ext4_write_unlock_xattr(dir, &no_expand);
1336 ret2 = ext4_mark_inode_dirty(handle, dir);
1337 if (unlikely(ret2 && !ret))
1344 * This function fills a red-black tree with information from an
1345 * inlined dir. It returns the number directory entries loaded
1346 * into the tree. If there is an error it is returned in err.
1348 int ext4_inlinedir_to_tree(struct file *dir_file,
1349 struct inode *dir, ext4_lblk_t block,
1350 struct dx_hash_info *hinfo,
1351 __u32 start_hash, __u32 start_minor_hash,
1352 int *has_inline_data)
1354 int err = 0, count = 0;
1355 unsigned int parent_ino;
1357 struct ext4_dir_entry_2 *de;
1358 struct inode *inode = file_inode(dir_file);
1359 int ret, inline_size = 0;
1360 struct ext4_iloc iloc;
1361 void *dir_buf = NULL;
1362 struct ext4_dir_entry_2 fake;
1363 struct fscrypt_str tmp_str;
1365 ret = ext4_get_inode_loc(inode, &iloc);
1369 down_read(&EXT4_I(inode)->xattr_sem);
1370 if (!ext4_has_inline_data(inode)) {
1371 up_read(&EXT4_I(inode)->xattr_sem);
1372 *has_inline_data = 0;
1376 inline_size = ext4_get_inline_size(inode);
1377 dir_buf = kmalloc(inline_size, GFP_NOFS);
1380 up_read(&EXT4_I(inode)->xattr_sem);
1384 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1385 up_read(&EXT4_I(inode)->xattr_sem);
1390 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1391 while (pos < inline_size) {
1393 * As inlined dir doesn't store any information about '.' and
1394 * only the inode number of '..' is stored, we have to handle
1398 fake.inode = cpu_to_le32(inode->i_ino);
1400 strcpy(fake.name, ".");
1401 fake.rec_len = ext4_rec_len_to_disk(
1402 ext4_dir_rec_len(fake.name_len, NULL),
1404 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1406 pos = EXT4_INLINE_DOTDOT_OFFSET;
1407 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1408 fake.inode = cpu_to_le32(parent_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_SIZE;
1418 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1419 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1420 if (ext4_check_dir_entry(inode, dir_file, de,
1422 inline_size, pos)) {
1428 if (ext4_hash_in_dirent(dir)) {
1429 hinfo->hash = EXT4_DIRENT_HASH(de);
1430 hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1432 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1434 if ((hinfo->hash < start_hash) ||
1435 ((hinfo->hash == start_hash) &&
1436 (hinfo->minor_hash < start_minor_hash)))
1440 tmp_str.name = de->name;
1441 tmp_str.len = de->name_len;
1442 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1443 hinfo->minor_hash, de, &tmp_str);
1458 * So this function is called when the volume is mkfsed with
1459 * dir_index disabled. In order to keep f_pos persistent
1460 * after we convert from an inlined dir to a blocked based,
1461 * we just pretend that we are a normal dir and return the
1462 * offset as if '.' and '..' really take place.
1465 int ext4_read_inline_dir(struct file *file,
1466 struct dir_context *ctx,
1467 int *has_inline_data)
1469 unsigned int offset, parent_ino;
1471 struct ext4_dir_entry_2 *de;
1472 struct super_block *sb;
1473 struct inode *inode = file_inode(file);
1474 int ret, inline_size = 0;
1475 struct ext4_iloc iloc;
1476 void *dir_buf = NULL;
1477 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1479 ret = ext4_get_inode_loc(inode, &iloc);
1483 down_read(&EXT4_I(inode)->xattr_sem);
1484 if (!ext4_has_inline_data(inode)) {
1485 up_read(&EXT4_I(inode)->xattr_sem);
1486 *has_inline_data = 0;
1490 inline_size = ext4_get_inline_size(inode);
1491 dir_buf = kmalloc(inline_size, GFP_NOFS);
1494 up_read(&EXT4_I(inode)->xattr_sem);
1498 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1499 up_read(&EXT4_I(inode)->xattr_sem);
1505 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1509 * dotdot_offset and dotdot_size is the real offset and
1510 * size for ".." and "." if the dir is block based while
1511 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1512 * So we will use extra_offset and extra_size to indicate them
1513 * during the inline dir iteration.
1515 dotdot_offset = ext4_dir_rec_len(1, NULL);
1516 dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1517 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1518 extra_size = extra_offset + inline_size;
1521 * If the version has changed since the last call to
1522 * readdir(2), then we might be pointing to an invalid
1523 * dirent right now. Scan from the start of the inline
1526 if (!inode_eq_iversion(inode, file->f_version)) {
1527 for (i = 0; i < extra_size && i < offset;) {
1529 * "." is with offset 0 and
1530 * ".." is dotdot_offset.
1535 } else if (i == dotdot_offset) {
1539 /* for other entry, the real offset in
1540 * the buf has to be tuned accordingly.
1542 de = (struct ext4_dir_entry_2 *)
1543 (dir_buf + i - extra_offset);
1544 /* It's too expensive to do a full
1545 * dirent test each time round this
1546 * loop, but we do have to test at
1547 * least that it is non-zero. A
1548 * failure will be detected in the
1549 * dirent test below. */
1550 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1551 < ext4_dir_rec_len(1, NULL))
1553 i += ext4_rec_len_from_disk(de->rec_len,
1558 file->f_version = inode_query_iversion(inode);
1561 while (ctx->pos < extra_size) {
1562 if (ctx->pos == 0) {
1563 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1565 ctx->pos = dotdot_offset;
1569 if (ctx->pos == dotdot_offset) {
1570 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1572 ctx->pos = dotdot_size;
1576 de = (struct ext4_dir_entry_2 *)
1577 (dir_buf + ctx->pos - extra_offset);
1578 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1579 extra_size, ctx->pos))
1581 if (le32_to_cpu(de->inode)) {
1582 if (!dir_emit(ctx, de->name, de->name_len,
1583 le32_to_cpu(de->inode),
1584 get_dtype(sb, de->file_type)))
1587 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1595 void *ext4_read_inline_link(struct inode *inode)
1597 struct ext4_iloc iloc;
1598 int ret, inline_size;
1601 ret = ext4_get_inode_loc(inode, &iloc);
1603 return ERR_PTR(ret);
1606 inline_size = ext4_get_inline_size(inode);
1607 link = kmalloc(inline_size + 1, GFP_NOFS);
1611 ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
1616 nd_terminate_link(link, inode->i_size, ret);
1619 link = ERR_PTR(ret);
1624 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1625 struct ext4_dir_entry_2 **parent_de,
1628 struct ext4_iloc iloc;
1630 *retval = ext4_get_inode_loc(inode, &iloc);
1634 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1640 * Try to create the inline data for the new dir.
1641 * If it succeeds, return 0, otherwise return the error.
1642 * In case of ENOSPC, the caller should create the normal disk layout dir.
1644 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1645 struct inode *inode)
1647 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1648 struct ext4_iloc iloc;
1649 struct ext4_dir_entry_2 *de;
1651 ret = ext4_get_inode_loc(inode, &iloc);
1655 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1660 * For inline dir, we only save the inode information for the ".."
1661 * and create a fake dentry to cover the left space.
1663 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1664 de->inode = cpu_to_le32(parent->i_ino);
1665 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1667 de->rec_len = ext4_rec_len_to_disk(
1668 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1670 set_nlink(inode, 2);
1671 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1677 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1678 struct ext4_filename *fname,
1679 struct ext4_dir_entry_2 **res_dir,
1680 int *has_inline_data)
1683 struct ext4_iloc iloc;
1687 if (ext4_get_inode_loc(dir, &iloc))
1690 down_read(&EXT4_I(dir)->xattr_sem);
1691 if (!ext4_has_inline_data(dir)) {
1692 *has_inline_data = 0;
1696 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1697 EXT4_INLINE_DOTDOT_SIZE;
1698 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1699 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1700 dir, fname, 0, res_dir);
1706 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1709 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1710 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1712 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1713 dir, fname, 0, res_dir);
1721 up_read(&EXT4_I(dir)->xattr_sem);
1725 int ext4_delete_inline_entry(handle_t *handle,
1727 struct ext4_dir_entry_2 *de_del,
1728 struct buffer_head *bh,
1729 int *has_inline_data)
1731 int err, inline_size, no_expand;
1732 struct ext4_iloc iloc;
1735 err = ext4_get_inode_loc(dir, &iloc);
1739 ext4_write_lock_xattr(dir, &no_expand);
1740 if (!ext4_has_inline_data(dir)) {
1741 *has_inline_data = 0;
1745 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1746 EXT4_MIN_INLINE_DATA_SIZE) {
1747 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1748 EXT4_INLINE_DOTDOT_SIZE;
1749 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1750 EXT4_INLINE_DOTDOT_SIZE;
1752 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1753 inline_size = ext4_get_inline_size(dir) -
1754 EXT4_MIN_INLINE_DATA_SIZE;
1757 BUFFER_TRACE(bh, "get_write_access");
1758 err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1763 err = ext4_generic_delete_entry(dir, de_del, bh,
1764 inline_start, inline_size, 0);
1768 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1770 ext4_write_unlock_xattr(dir, &no_expand);
1771 if (likely(err == 0))
1772 err = ext4_mark_inode_dirty(handle, dir);
1775 ext4_std_error(dir->i_sb, err);
1780 * Get the inline dentry at offset.
1782 static inline struct ext4_dir_entry_2 *
1783 ext4_get_inline_entry(struct inode *inode,
1784 struct ext4_iloc *iloc,
1785 unsigned int offset,
1786 void **inline_start,
1791 BUG_ON(offset > ext4_get_inline_size(inode));
1793 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1794 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1795 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1797 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1798 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1799 *inline_size = ext4_get_inline_size(inode) -
1800 EXT4_MIN_INLINE_DATA_SIZE;
1804 *inline_start = inline_pos;
1805 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1808 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1810 int err, inline_size;
1811 struct ext4_iloc iloc;
1814 unsigned int offset;
1815 struct ext4_dir_entry_2 *de;
1818 err = ext4_get_inode_loc(dir, &iloc);
1820 EXT4_ERROR_INODE_ERR(dir, -err,
1821 "error %d getting inode %lu block",
1826 down_read(&EXT4_I(dir)->xattr_sem);
1827 if (!ext4_has_inline_data(dir)) {
1828 *has_inline_data = 0;
1833 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1834 if (!le32_to_cpu(de->inode)) {
1835 ext4_warning(dir->i_sb,
1836 "bad inline directory (dir #%lu) - no `..'",
1841 inline_len = ext4_get_inline_size(dir);
1842 offset = EXT4_INLINE_DOTDOT_SIZE;
1843 while (offset < inline_len) {
1844 de = ext4_get_inline_entry(dir, &iloc, offset,
1845 &inline_pos, &inline_size);
1846 if (ext4_check_dir_entry(dir, NULL, de,
1847 iloc.bh, inline_pos,
1848 inline_size, offset)) {
1849 ext4_warning(dir->i_sb,
1850 "bad inline directory (dir #%lu) - "
1851 "inode %u, rec_len %u, name_len %d"
1853 dir->i_ino, le32_to_cpu(de->inode),
1854 le16_to_cpu(de->rec_len), de->name_len,
1858 if (le32_to_cpu(de->inode)) {
1861 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1866 up_read(&EXT4_I(dir)->xattr_sem);
1871 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1875 ext4_write_lock_xattr(inode, &no_expand);
1876 ret = ext4_destroy_inline_data_nolock(handle, inode);
1877 ext4_write_unlock_xattr(inode, &no_expand);
1882 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1885 int error = -EAGAIN;
1886 struct ext4_iloc iloc;
1888 down_read(&EXT4_I(inode)->xattr_sem);
1889 if (!ext4_has_inline_data(inode))
1892 error = ext4_get_inode_loc(inode, &iloc);
1896 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1897 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1898 addr += offsetof(struct ext4_inode, i_block);
1904 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1905 i_size_read(inode));
1906 iomap->type = IOMAP_INLINE;
1910 up_read(&EXT4_I(inode)->xattr_sem);
1914 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1917 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1920 struct ext4_xattr_ibody_find is = {
1921 .s = { .not_found = -ENODATA, },
1923 struct ext4_xattr_info i = {
1924 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1925 .name = EXT4_XATTR_SYSTEM_DATA,
1929 needed_blocks = ext4_writepage_trans_blocks(inode);
1930 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1932 return PTR_ERR(handle);
1934 ext4_write_lock_xattr(inode, &no_expand);
1935 if (!ext4_has_inline_data(inode)) {
1936 ext4_write_unlock_xattr(inode, &no_expand);
1938 ext4_journal_stop(handle);
1942 if ((err = ext4_orphan_add(handle, inode)) != 0)
1945 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1948 down_write(&EXT4_I(inode)->i_data_sem);
1949 i_size = inode->i_size;
1950 inline_size = ext4_get_inline_size(inode);
1951 EXT4_I(inode)->i_disksize = i_size;
1953 if (i_size < inline_size) {
1955 * if there's inline data to truncate and this file was
1956 * converted to extents after that inline data was written,
1957 * the extent status cache must be cleared to avoid leaving
1958 * behind stale delayed allocated extent entries
1960 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1962 err = ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1963 if (err == -ENOMEM) {
1964 memalloc_retry_wait(GFP_ATOMIC);
1971 /* Clear the content in the xattr space. */
1972 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1973 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1976 BUG_ON(is.s.not_found);
1978 value_len = le32_to_cpu(is.s.here->e_value_size);
1979 value = kmalloc(value_len, GFP_NOFS);
1985 err = ext4_xattr_ibody_get(inode, i.name_index,
1986 i.name, value, value_len);
1991 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1992 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1993 err = ext4_xattr_ibody_set(handle, inode, &i, &is);
1998 /* Clear the content within i_blocks. */
1999 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
2000 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
2001 memset(p + i_size, 0,
2002 EXT4_MIN_INLINE_DATA_SIZE - i_size);
2005 EXT4_I(inode)->i_inline_size = i_size <
2006 EXT4_MIN_INLINE_DATA_SIZE ?
2007 EXT4_MIN_INLINE_DATA_SIZE : i_size;
2011 up_write(&EXT4_I(inode)->i_data_sem);
2014 ext4_write_unlock_xattr(inode, &no_expand);
2017 ext4_orphan_del(handle, inode);
2020 inode->i_mtime = inode->i_ctime = current_time(inode);
2021 err = ext4_mark_inode_dirty(handle, inode);
2023 ext4_handle_sync(handle);
2025 ext4_journal_stop(handle);
2029 int ext4_convert_inline_data(struct inode *inode)
2031 int error, needed_blocks, no_expand;
2033 struct ext4_iloc iloc;
2035 if (!ext4_has_inline_data(inode)) {
2036 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2038 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2040 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2041 * cleared. This means we are in the middle of moving of
2042 * inline data to delay allocated block. Just force writeout
2043 * here to finish conversion.
2045 error = filemap_flush(inode->i_mapping);
2048 if (!ext4_has_inline_data(inode))
2052 needed_blocks = ext4_writepage_trans_blocks(inode);
2055 error = ext4_get_inode_loc(inode, &iloc);
2059 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2060 if (IS_ERR(handle)) {
2061 error = PTR_ERR(handle);
2065 ext4_write_lock_xattr(inode, &no_expand);
2066 if (ext4_has_inline_data(inode))
2067 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2068 ext4_write_unlock_xattr(inode, &no_expand);
2069 ext4_journal_stop(handle);