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;
38 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
41 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
42 EXT4_GOOD_OLD_INODE_SIZE -
43 EXT4_I(inode)->i_extra_isize -
44 sizeof(struct ext4_xattr_ibody_header);
47 * We need to subtract another sizeof(__u32) since an in-inode xattr
48 * needs an empty 4 bytes to indicate the gap between the xattr entry
49 * and the name/value pair.
51 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
52 return EXT4_XATTR_SIZE(min_offs -
53 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
54 EXT4_XATTR_ROUND - sizeof(__u32));
56 raw_inode = ext4_raw_inode(iloc);
57 header = IHDR(inode, raw_inode);
58 entry = IFIRST(header);
59 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
61 /* Compute min_offs. */
62 while (!IS_LAST_ENTRY(entry)) {
63 void *next = EXT4_XATTR_NEXT(entry);
66 EXT4_ERROR_INODE(inode,
67 "corrupt xattr in inline inode");
70 if (!entry->e_value_inum && entry->e_value_size) {
71 size_t offs = le16_to_cpu(entry->e_value_offs);
78 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
80 if (EXT4_I(inode)->i_inline_off) {
81 entry = (struct ext4_xattr_entry *)
82 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
84 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
88 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
90 if (free > EXT4_XATTR_ROUND)
91 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
100 * Get the maximum size we now can store in an inode.
101 * If we can't find the space for a xattr entry, don't use the space
102 * of the extents since we have no space to indicate the inline data.
104 int ext4_get_max_inline_size(struct inode *inode)
106 int error, max_inline_size;
107 struct ext4_iloc iloc;
109 if (EXT4_I(inode)->i_extra_isize == 0)
112 error = ext4_get_inode_loc(inode, &iloc);
114 ext4_error_inode(inode, __func__, __LINE__, 0,
115 "can't get inode location %lu",
120 down_read(&EXT4_I(inode)->xattr_sem);
121 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
122 up_read(&EXT4_I(inode)->xattr_sem);
126 if (!max_inline_size)
129 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
133 * this function does not take xattr_sem, which is OK because it is
134 * currently only used in a code path coming form ext4_iget, before
135 * the new inode has been unlocked
137 int ext4_find_inline_data_nolock(struct inode *inode)
139 struct ext4_xattr_ibody_find is = {
140 .s = { .not_found = -ENODATA, },
142 struct ext4_xattr_info i = {
143 .name_index = EXT4_XATTR_INDEX_SYSTEM,
144 .name = EXT4_XATTR_SYSTEM_DATA,
148 if (EXT4_I(inode)->i_extra_isize == 0)
151 error = ext4_get_inode_loc(inode, &is.iloc);
155 error = ext4_xattr_ibody_find(inode, &i, &is);
159 if (!is.s.not_found) {
160 if (is.s.here->e_value_inum) {
161 EXT4_ERROR_INODE(inode, "inline data xattr refers "
162 "to an external xattr inode");
163 error = -EFSCORRUPTED;
166 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
167 (void *)ext4_raw_inode(&is.iloc));
168 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
169 le32_to_cpu(is.s.here->e_value_size);
176 static int ext4_read_inline_data(struct inode *inode, void *buffer,
178 struct ext4_iloc *iloc)
180 struct ext4_xattr_entry *entry;
181 struct ext4_xattr_ibody_header *header;
183 struct ext4_inode *raw_inode;
188 BUG_ON(len > EXT4_I(inode)->i_inline_size);
190 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
191 len : EXT4_MIN_INLINE_DATA_SIZE;
193 raw_inode = ext4_raw_inode(iloc);
194 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
202 header = IHDR(inode, raw_inode);
203 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
204 EXT4_I(inode)->i_inline_off);
205 len = min_t(unsigned int, len,
206 (unsigned int)le32_to_cpu(entry->e_value_size));
209 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
217 * write the buffer to the inline inode.
218 * If 'create' is set, we don't need to do the extra copy in the xattr
219 * value since it is already handled by ext4_xattr_ibody_set.
220 * That saves us one memcpy.
222 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
223 void *buffer, loff_t pos, unsigned int len)
225 struct ext4_xattr_entry *entry;
226 struct ext4_xattr_ibody_header *header;
227 struct ext4_inode *raw_inode;
230 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
233 BUG_ON(!EXT4_I(inode)->i_inline_off);
234 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
236 raw_inode = ext4_raw_inode(iloc);
239 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
240 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
241 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
242 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
252 pos -= EXT4_MIN_INLINE_DATA_SIZE;
253 header = IHDR(inode, raw_inode);
254 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
255 EXT4_I(inode)->i_inline_off);
257 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
261 static int ext4_create_inline_data(handle_t *handle,
262 struct inode *inode, unsigned len)
266 struct ext4_xattr_ibody_find is = {
267 .s = { .not_found = -ENODATA, },
269 struct ext4_xattr_info i = {
270 .name_index = EXT4_XATTR_INDEX_SYSTEM,
271 .name = EXT4_XATTR_SYSTEM_DATA,
274 error = ext4_get_inode_loc(inode, &is.iloc);
278 BUFFER_TRACE(is.iloc.bh, "get_write_access");
279 error = ext4_journal_get_write_access(handle, is.iloc.bh);
283 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
284 value = EXT4_ZERO_XATTR_VALUE;
285 len -= EXT4_MIN_INLINE_DATA_SIZE;
291 /* Insert the the xttr entry. */
295 error = ext4_xattr_ibody_find(inode, &i, &is);
299 BUG_ON(!is.s.not_found);
301 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
303 if (error == -ENOSPC)
304 ext4_clear_inode_state(inode,
305 EXT4_STATE_MAY_INLINE_DATA);
309 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
310 0, EXT4_MIN_INLINE_DATA_SIZE);
312 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
313 (void *)ext4_raw_inode(&is.iloc));
314 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
315 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
316 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
318 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
325 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
330 struct ext4_xattr_ibody_find is = {
331 .s = { .not_found = -ENODATA, },
333 struct ext4_xattr_info i = {
334 .name_index = EXT4_XATTR_INDEX_SYSTEM,
335 .name = EXT4_XATTR_SYSTEM_DATA,
338 /* If the old space is ok, write the data directly. */
339 if (len <= EXT4_I(inode)->i_inline_size)
342 error = ext4_get_inode_loc(inode, &is.iloc);
346 error = ext4_xattr_ibody_find(inode, &i, &is);
350 BUG_ON(is.s.not_found);
352 len -= EXT4_MIN_INLINE_DATA_SIZE;
353 value = kzalloc(len, GFP_NOFS);
359 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
364 BUFFER_TRACE(is.iloc.bh, "get_write_access");
365 error = ext4_journal_get_write_access(handle, is.iloc.bh);
369 /* Update the xttr entry. */
373 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
377 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
378 (void *)ext4_raw_inode(&is.iloc));
379 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
380 le32_to_cpu(is.s.here->e_value_size);
381 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
383 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
391 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
394 int ret, size, no_expand;
395 struct ext4_inode_info *ei = EXT4_I(inode);
397 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
400 size = ext4_get_max_inline_size(inode);
404 ext4_write_lock_xattr(inode, &no_expand);
406 if (ei->i_inline_off)
407 ret = ext4_update_inline_data(handle, inode, len);
409 ret = ext4_create_inline_data(handle, inode, len);
411 ext4_write_unlock_xattr(inode, &no_expand);
415 static int ext4_destroy_inline_data_nolock(handle_t *handle,
418 struct ext4_inode_info *ei = EXT4_I(inode);
419 struct ext4_xattr_ibody_find is = {
420 .s = { .not_found = 0, },
422 struct ext4_xattr_info i = {
423 .name_index = EXT4_XATTR_INDEX_SYSTEM,
424 .name = EXT4_XATTR_SYSTEM_DATA,
430 if (!ei->i_inline_off)
433 error = ext4_get_inode_loc(inode, &is.iloc);
437 error = ext4_xattr_ibody_find(inode, &i, &is);
441 BUFFER_TRACE(is.iloc.bh, "get_write_access");
442 error = ext4_journal_get_write_access(handle, is.iloc.bh);
446 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
450 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
451 0, EXT4_MIN_INLINE_DATA_SIZE);
452 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
454 if (ext4_has_feature_extents(inode->i_sb)) {
455 if (S_ISDIR(inode->i_mode) ||
456 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
457 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
458 ext4_ext_tree_init(handle, inode);
461 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
464 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
466 EXT4_I(inode)->i_inline_off = 0;
467 EXT4_I(inode)->i_inline_size = 0;
468 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
471 if (error == -ENODATA)
476 static int ext4_read_inline_page(struct inode *inode, struct page *page)
481 struct ext4_iloc iloc;
483 BUG_ON(!PageLocked(page));
484 BUG_ON(!ext4_has_inline_data(inode));
487 if (!EXT4_I(inode)->i_inline_off) {
488 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
493 ret = ext4_get_inode_loc(inode, &iloc);
497 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
498 kaddr = kmap_atomic(page);
499 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
500 flush_dcache_page(page);
501 kunmap_atomic(kaddr);
502 zero_user_segment(page, len, PAGE_SIZE);
503 SetPageUptodate(page);
510 int ext4_readpage_inline(struct inode *inode, struct page *page)
514 down_read(&EXT4_I(inode)->xattr_sem);
515 if (!ext4_has_inline_data(inode)) {
516 up_read(&EXT4_I(inode)->xattr_sem);
521 * Current inline data can only exist in the 1st page,
522 * So for all the other pages, just set them uptodate.
525 ret = ext4_read_inline_page(inode, page);
526 else if (!PageUptodate(page)) {
527 zero_user_segment(page, 0, PAGE_SIZE);
528 SetPageUptodate(page);
531 up_read(&EXT4_I(inode)->xattr_sem);
534 return ret >= 0 ? 0 : ret;
537 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
541 int ret, needed_blocks, no_expand;
542 handle_t *handle = NULL;
543 int retries = 0, sem_held = 0;
544 struct page *page = NULL;
546 struct ext4_iloc iloc;
548 if (!ext4_has_inline_data(inode)) {
550 * clear the flag so that no new write
551 * will trap here again.
553 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
557 needed_blocks = ext4_writepage_trans_blocks(inode);
559 ret = ext4_get_inode_loc(inode, &iloc);
564 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
565 if (IS_ERR(handle)) {
566 ret = PTR_ERR(handle);
571 /* We cannot recurse into the filesystem as the transaction is already
573 flags |= AOP_FLAG_NOFS;
575 page = grab_cache_page_write_begin(mapping, 0, flags);
581 ext4_write_lock_xattr(inode, &no_expand);
583 /* If some one has already done this for us, just exit. */
584 if (!ext4_has_inline_data(inode)) {
590 to = ext4_get_inline_size(inode);
591 if (!PageUptodate(page)) {
592 ret = ext4_read_inline_page(inode, page);
597 ret = ext4_destroy_inline_data_nolock(handle, inode);
601 if (ext4_should_dioread_nolock(inode)) {
602 ret = __block_write_begin(page, from, to,
603 ext4_get_block_unwritten);
605 ret = __block_write_begin(page, from, to, ext4_get_block);
607 if (!ret && ext4_should_journal_data(inode)) {
608 ret = ext4_walk_page_buffers(handle, page_buffers(page),
610 do_journal_get_write_access);
617 ext4_orphan_add(handle, inode);
618 ext4_write_unlock_xattr(inode, &no_expand);
620 ext4_journal_stop(handle);
622 ext4_truncate_failed_write(inode);
624 * If truncate failed early the inode might
625 * still be on the orphan list; we need to
626 * make sure the inode is removed from the
627 * orphan list in that case.
630 ext4_orphan_del(NULL, inode);
633 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
637 block_commit_write(page, from, to);
644 ext4_write_unlock_xattr(inode, &no_expand);
646 ext4_journal_stop(handle);
652 * Try to write data in the inode.
653 * If the inode has inline data, check whether the new write can be
654 * in the inode also. If not, create the page the handle, move the data
655 * to the page make it update and let the later codes create extent for it.
657 int ext4_try_to_write_inline_data(struct address_space *mapping,
659 loff_t pos, unsigned len,
666 struct ext4_iloc iloc;
668 if (pos + len > ext4_get_max_inline_size(inode))
671 ret = ext4_get_inode_loc(inode, &iloc);
676 * The possible write could happen in the inode,
677 * so try to reserve the space in inode first.
679 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
680 if (IS_ERR(handle)) {
681 ret = PTR_ERR(handle);
686 ret = ext4_prepare_inline_data(handle, inode, pos + len);
687 if (ret && ret != -ENOSPC)
690 /* We don't have space in inline inode, so convert it to extent. */
691 if (ret == -ENOSPC) {
692 ext4_journal_stop(handle);
697 ret = ext4_journal_get_write_access(handle, iloc.bh);
701 flags |= AOP_FLAG_NOFS;
703 page = grab_cache_page_write_begin(mapping, 0, flags);
710 down_read(&EXT4_I(inode)->xattr_sem);
711 if (!ext4_has_inline_data(inode)) {
718 if (!PageUptodate(page)) {
719 ret = ext4_read_inline_page(inode, page);
730 up_read(&EXT4_I(inode)->xattr_sem);
732 if (handle && (ret != 1))
733 ext4_journal_stop(handle);
737 return ext4_convert_inline_data_to_extent(mapping,
741 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
742 unsigned copied, struct page *page)
746 struct ext4_iloc iloc;
748 if (unlikely(copied < len)) {
749 if (!PageUptodate(page)) {
755 ret = ext4_get_inode_loc(inode, &iloc);
757 ext4_std_error(inode->i_sb, ret);
762 ext4_write_lock_xattr(inode, &no_expand);
763 BUG_ON(!ext4_has_inline_data(inode));
766 * ei->i_inline_off may have changed since ext4_write_begin()
767 * called ext4_try_to_write_inline_data()
769 (void) ext4_find_inline_data_nolock(inode);
771 kaddr = kmap_atomic(page);
772 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
773 kunmap_atomic(kaddr);
774 SetPageUptodate(page);
775 /* clear page dirty so that writepages wouldn't work for us. */
776 ClearPageDirty(page);
778 ext4_write_unlock_xattr(inode, &no_expand);
780 mark_inode_dirty(inode);
786 ext4_journalled_write_inline_data(struct inode *inode,
792 struct ext4_iloc iloc;
794 ret = ext4_get_inode_loc(inode, &iloc);
796 ext4_std_error(inode->i_sb, ret);
800 ext4_write_lock_xattr(inode, &no_expand);
801 kaddr = kmap_atomic(page);
802 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
803 kunmap_atomic(kaddr);
804 ext4_write_unlock_xattr(inode, &no_expand);
810 * Try to make the page cache and handle ready for the inline data case.
811 * We can call this function in 2 cases:
812 * 1. The inode is created and the first write exceeds inline size. We can
813 * clear the inode state safely.
814 * 2. The inode has inline data, then we need to read the data, make it
815 * update and dirty so that ext4_da_writepages can handle it. We don't
816 * need to start the journal since the file's metatdata isn't changed now.
818 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
823 int ret = 0, inline_size;
826 page = grab_cache_page_write_begin(mapping, 0, flags);
830 down_read(&EXT4_I(inode)->xattr_sem);
831 if (!ext4_has_inline_data(inode)) {
832 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
836 inline_size = ext4_get_inline_size(inode);
838 if (!PageUptodate(page)) {
839 ret = ext4_read_inline_page(inode, page);
844 ret = __block_write_begin(page, 0, inline_size,
845 ext4_da_get_block_prep);
847 up_read(&EXT4_I(inode)->xattr_sem);
850 ext4_truncate_failed_write(inode);
855 SetPageUptodate(page);
856 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
857 *fsdata = (void *)CONVERT_INLINE_DATA;
860 up_read(&EXT4_I(inode)->xattr_sem);
869 * Prepare the write for the inline data.
870 * If the the data can be written into the inode, we just read
871 * the page and make it uptodate, and start the journal.
872 * Otherwise read the page, makes it dirty so that it can be
873 * handle in writepages(the i_disksize update is left to the
874 * normal ext4_da_write_end).
876 int ext4_da_write_inline_data_begin(struct address_space *mapping,
878 loff_t pos, unsigned len,
883 int ret, inline_size;
886 struct ext4_iloc iloc;
889 ret = ext4_get_inode_loc(inode, &iloc);
894 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
895 if (IS_ERR(handle)) {
896 ret = PTR_ERR(handle);
900 inline_size = ext4_get_max_inline_size(inode);
903 if (inline_size >= pos + len) {
904 ret = ext4_prepare_inline_data(handle, inode, pos + len);
905 if (ret && ret != -ENOSPC)
910 * We cannot recurse into the filesystem as the transaction
911 * is already started.
913 flags |= AOP_FLAG_NOFS;
915 if (ret == -ENOSPC) {
916 ext4_journal_stop(handle);
917 ret = ext4_da_convert_inline_data_to_extent(mapping,
921 if (ret == -ENOSPC &&
922 ext4_should_retry_alloc(inode->i_sb, &retries))
927 page = grab_cache_page_write_begin(mapping, 0, flags);
933 down_read(&EXT4_I(inode)->xattr_sem);
934 if (!ext4_has_inline_data(inode)) {
936 goto out_release_page;
939 if (!PageUptodate(page)) {
940 ret = ext4_read_inline_page(inode, page);
942 goto out_release_page;
944 ret = ext4_journal_get_write_access(handle, iloc.bh);
946 goto out_release_page;
948 up_read(&EXT4_I(inode)->xattr_sem);
953 up_read(&EXT4_I(inode)->xattr_sem);
957 ext4_journal_stop(handle);
963 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
964 unsigned len, unsigned copied,
969 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
978 * No need to use i_size_read() here, the i_size
979 * cannot change under us because we hold i_mutex.
981 * But it's important to update i_size while still holding page lock:
982 * page writeout could otherwise come in and zero beyond i_size.
984 if (pos+copied > inode->i_size)
985 i_size_write(inode, pos+copied);
990 * Don't mark the inode dirty under page lock. First, it unnecessarily
991 * makes the holding time of page lock longer. Second, it forces lock
992 * ordering of page lock and transaction start for journaling
995 mark_inode_dirty(inode);
1000 #ifdef INLINE_DIR_DEBUG
1001 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
1002 void *inline_start, int inline_size)
1005 unsigned short de_len;
1006 struct ext4_dir_entry_2 *de = inline_start;
1007 void *dlimit = inline_start + inline_size;
1009 trace_printk("inode %lu\n", dir->i_ino);
1011 while ((void *)de < dlimit) {
1012 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1013 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1014 offset, de_len, de->name_len, de->name,
1015 de->name_len, le32_to_cpu(de->inode));
1016 if (ext4_check_dir_entry(dir, NULL, de, bh,
1017 inline_start, inline_size, offset))
1021 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1025 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1029 * Add a new entry into a inline dir.
1030 * It will return -ENOSPC if no space is available, and -EIO
1031 * and -EEXIST if directory entry already exists.
1033 static int ext4_add_dirent_to_inline(handle_t *handle,
1034 struct ext4_filename *fname,
1036 struct inode *inode,
1037 struct ext4_iloc *iloc,
1038 void *inline_start, int inline_size)
1041 struct ext4_dir_entry_2 *de;
1043 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1044 inline_size, fname, &de);
1048 BUFFER_TRACE(iloc->bh, "get_write_access");
1049 err = ext4_journal_get_write_access(handle, iloc->bh);
1052 ext4_insert_dentry(inode, de, inline_size, fname);
1054 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1057 * XXX shouldn't update any times until successful
1058 * completion of syscall, but too many callers depend
1061 * XXX similarly, too many callers depend on
1062 * ext4_new_inode() setting the times, but error
1063 * recovery deletes the inode, so the worst that can
1064 * happen is that the times are slightly out of date
1065 * and/or different from the directory change time.
1067 dir->i_mtime = dir->i_ctime = current_time(dir);
1068 ext4_update_dx_flag(dir);
1069 inode_inc_iversion(dir);
1073 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1074 struct ext4_iloc *iloc)
1076 struct ext4_xattr_entry *entry;
1077 struct ext4_xattr_ibody_header *header;
1079 BUG_ON(!EXT4_I(inode)->i_inline_off);
1081 header = IHDR(inode, ext4_raw_inode(iloc));
1082 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1083 EXT4_I(inode)->i_inline_off);
1085 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1088 /* Set the final de to cover the whole block. */
1089 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1091 struct ext4_dir_entry_2 *de, *prev_de;
1095 de = (struct ext4_dir_entry_2 *)de_buf;
1097 limit = de_buf + old_size;
1100 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1102 de = (struct ext4_dir_entry_2 *)de_buf;
1103 } while (de_buf < limit);
1105 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1106 old_size, new_size);
1108 /* this is just created, so create an empty entry. */
1110 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1114 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1115 struct ext4_iloc *iloc)
1118 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1119 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1121 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1124 ret = ext4_update_inline_data(handle, dir,
1125 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1129 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1130 EXT4_I(dir)->i_inline_size -
1131 EXT4_MIN_INLINE_DATA_SIZE);
1132 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1136 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1137 struct ext4_iloc *iloc,
1138 void *buf, int inline_size)
1142 ret = ext4_create_inline_data(handle, inode, inline_size);
1144 ext4_msg(inode->i_sb, KERN_EMERG,
1145 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1149 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1150 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1153 static int ext4_finish_convert_inline_dir(handle_t *handle,
1154 struct inode *inode,
1155 struct buffer_head *dir_block,
1159 int err, csum_size = 0, header_size = 0;
1160 struct ext4_dir_entry_2 *de;
1161 struct ext4_dir_entry_tail *t;
1162 void *target = dir_block->b_data;
1165 * First create "." and ".." and then copy the dir information
1166 * back to the block.
1168 de = (struct ext4_dir_entry_2 *)target;
1169 de = ext4_init_dot_dotdot(inode, de,
1170 inode->i_sb->s_blocksize, csum_size,
1171 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1172 header_size = (void *)de - target;
1174 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1175 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1177 if (ext4_has_metadata_csum(inode->i_sb))
1178 csum_size = sizeof(struct ext4_dir_entry_tail);
1180 inode->i_size = inode->i_sb->s_blocksize;
1181 i_size_write(inode, inode->i_sb->s_blocksize);
1182 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1183 ext4_update_final_de(dir_block->b_data,
1184 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1185 inode->i_sb->s_blocksize - csum_size);
1188 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1189 inode->i_sb->s_blocksize);
1190 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1192 set_buffer_uptodate(dir_block);
1193 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1196 set_buffer_verified(dir_block);
1197 return ext4_mark_inode_dirty(handle, inode);
1200 static int ext4_convert_inline_data_nolock(handle_t *handle,
1201 struct inode *inode,
1202 struct ext4_iloc *iloc)
1206 struct buffer_head *data_bh = NULL;
1207 struct ext4_map_blocks map;
1210 inline_size = ext4_get_inline_size(inode);
1211 buf = kmalloc(inline_size, GFP_NOFS);
1217 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1222 * Make sure the inline directory entries pass checks before we try to
1223 * convert them, so that we avoid touching stuff that needs fsck.
1225 if (S_ISDIR(inode->i_mode)) {
1226 error = ext4_check_all_de(inode, iloc->bh,
1227 buf + EXT4_INLINE_DOTDOT_SIZE,
1228 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1233 error = ext4_destroy_inline_data_nolock(handle, inode);
1240 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1243 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1248 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1254 lock_buffer(data_bh);
1255 error = ext4_journal_get_create_access(handle, data_bh);
1257 unlock_buffer(data_bh);
1261 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1263 if (!S_ISDIR(inode->i_mode)) {
1264 memcpy(data_bh->b_data, buf, inline_size);
1265 set_buffer_uptodate(data_bh);
1266 error = ext4_handle_dirty_metadata(handle,
1269 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1273 unlock_buffer(data_bh);
1276 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1285 * Try to add the new entry to the inline data.
1286 * If succeeds, return 0. If not, extended the inline dir and copied data to
1287 * the new created block.
1289 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1290 struct inode *dir, struct inode *inode)
1292 int ret, inline_size, no_expand;
1294 struct ext4_iloc iloc;
1296 ret = ext4_get_inode_loc(dir, &iloc);
1300 ext4_write_lock_xattr(dir, &no_expand);
1301 if (!ext4_has_inline_data(dir))
1304 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1305 EXT4_INLINE_DOTDOT_SIZE;
1306 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1308 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1309 inline_start, inline_size);
1313 /* check whether it can be inserted to inline xattr space. */
1314 inline_size = EXT4_I(dir)->i_inline_size -
1315 EXT4_MIN_INLINE_DATA_SIZE;
1317 /* Try to use the xattr space.*/
1318 ret = ext4_update_inline_dir(handle, dir, &iloc);
1319 if (ret && ret != -ENOSPC)
1322 inline_size = EXT4_I(dir)->i_inline_size -
1323 EXT4_MIN_INLINE_DATA_SIZE;
1327 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1329 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1330 inode, &iloc, inline_start,
1338 * The inline space is filled up, so create a new block for it.
1339 * As the extent tree will be created, we have to save the inline
1342 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1345 ext4_write_unlock_xattr(dir, &no_expand);
1346 ext4_mark_inode_dirty(handle, dir);
1352 * This function fills a red-black tree with information from an
1353 * inlined dir. It returns the number directory entries loaded
1354 * into the tree. If there is an error it is returned in err.
1356 int htree_inlinedir_to_tree(struct file *dir_file,
1357 struct inode *dir, ext4_lblk_t block,
1358 struct dx_hash_info *hinfo,
1359 __u32 start_hash, __u32 start_minor_hash,
1360 int *has_inline_data)
1362 int err = 0, count = 0;
1363 unsigned int parent_ino;
1365 struct ext4_dir_entry_2 *de;
1366 struct inode *inode = file_inode(dir_file);
1367 int ret, inline_size = 0;
1368 struct ext4_iloc iloc;
1369 void *dir_buf = NULL;
1370 struct ext4_dir_entry_2 fake;
1371 struct fscrypt_str tmp_str;
1373 ret = ext4_get_inode_loc(inode, &iloc);
1377 down_read(&EXT4_I(inode)->xattr_sem);
1378 if (!ext4_has_inline_data(inode)) {
1379 up_read(&EXT4_I(inode)->xattr_sem);
1380 *has_inline_data = 0;
1384 inline_size = ext4_get_inline_size(inode);
1385 dir_buf = kmalloc(inline_size, GFP_NOFS);
1388 up_read(&EXT4_I(inode)->xattr_sem);
1392 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1393 up_read(&EXT4_I(inode)->xattr_sem);
1398 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1399 while (pos < inline_size) {
1401 * As inlined dir doesn't store any information about '.' and
1402 * only the inode number of '..' is stored, we have to handle
1406 fake.inode = cpu_to_le32(inode->i_ino);
1408 strcpy(fake.name, ".");
1409 fake.rec_len = ext4_rec_len_to_disk(
1410 EXT4_DIR_REC_LEN(fake.name_len),
1412 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1414 pos = EXT4_INLINE_DOTDOT_OFFSET;
1415 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1416 fake.inode = cpu_to_le32(parent_ino);
1418 strcpy(fake.name, "..");
1419 fake.rec_len = ext4_rec_len_to_disk(
1420 EXT4_DIR_REC_LEN(fake.name_len),
1422 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1424 pos = EXT4_INLINE_DOTDOT_SIZE;
1426 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1427 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1428 if (ext4_check_dir_entry(inode, dir_file, de,
1430 inline_size, pos)) {
1436 ext4fs_dirhash(de->name, de->name_len, hinfo);
1437 if ((hinfo->hash < start_hash) ||
1438 ((hinfo->hash == start_hash) &&
1439 (hinfo->minor_hash < start_minor_hash)))
1443 tmp_str.name = de->name;
1444 tmp_str.len = de->name_len;
1445 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1446 hinfo->minor_hash, de, &tmp_str);
1461 * So this function is called when the volume is mkfsed with
1462 * dir_index disabled. In order to keep f_pos persistent
1463 * after we convert from an inlined dir to a blocked based,
1464 * we just pretend that we are a normal dir and return the
1465 * offset as if '.' and '..' really take place.
1468 int ext4_read_inline_dir(struct file *file,
1469 struct dir_context *ctx,
1470 int *has_inline_data)
1472 unsigned int offset, parent_ino;
1474 struct ext4_dir_entry_2 *de;
1475 struct super_block *sb;
1476 struct inode *inode = file_inode(file);
1477 int ret, inline_size = 0;
1478 struct ext4_iloc iloc;
1479 void *dir_buf = NULL;
1480 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1482 ret = ext4_get_inode_loc(inode, &iloc);
1486 down_read(&EXT4_I(inode)->xattr_sem);
1487 if (!ext4_has_inline_data(inode)) {
1488 up_read(&EXT4_I(inode)->xattr_sem);
1489 *has_inline_data = 0;
1493 inline_size = ext4_get_inline_size(inode);
1494 dir_buf = kmalloc(inline_size, GFP_NOFS);
1497 up_read(&EXT4_I(inode)->xattr_sem);
1501 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1502 up_read(&EXT4_I(inode)->xattr_sem);
1508 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1512 * dotdot_offset and dotdot_size is the real offset and
1513 * size for ".." and "." if the dir is block based while
1514 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1515 * So we will use extra_offset and extra_size to indicate them
1516 * during the inline dir iteration.
1518 dotdot_offset = EXT4_DIR_REC_LEN(1);
1519 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1520 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1521 extra_size = extra_offset + inline_size;
1524 * If the version has changed since the last call to
1525 * readdir(2), then we might be pointing to an invalid
1526 * dirent right now. Scan from the start of the inline
1529 if (!inode_eq_iversion(inode, file->f_version)) {
1530 for (i = 0; i < extra_size && i < offset;) {
1532 * "." is with offset 0 and
1533 * ".." is dotdot_offset.
1538 } else if (i == dotdot_offset) {
1542 /* for other entry, the real offset in
1543 * the buf has to be tuned accordingly.
1545 de = (struct ext4_dir_entry_2 *)
1546 (dir_buf + i - extra_offset);
1547 /* It's too expensive to do a full
1548 * dirent test each time round this
1549 * loop, but we do have to test at
1550 * least that it is non-zero. A
1551 * failure will be detected in the
1552 * dirent test below. */
1553 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1554 < EXT4_DIR_REC_LEN(1))
1556 i += ext4_rec_len_from_disk(de->rec_len,
1561 file->f_version = inode_query_iversion(inode);
1564 while (ctx->pos < extra_size) {
1565 if (ctx->pos == 0) {
1566 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1568 ctx->pos = dotdot_offset;
1572 if (ctx->pos == dotdot_offset) {
1573 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1575 ctx->pos = dotdot_size;
1579 de = (struct ext4_dir_entry_2 *)
1580 (dir_buf + ctx->pos - extra_offset);
1581 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1582 extra_size, ctx->pos))
1584 if (le32_to_cpu(de->inode)) {
1585 if (!dir_emit(ctx, de->name, de->name_len,
1586 le32_to_cpu(de->inode),
1587 get_dtype(sb, de->file_type)))
1590 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1598 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1599 struct ext4_dir_entry_2 **parent_de,
1602 struct ext4_iloc iloc;
1604 *retval = ext4_get_inode_loc(inode, &iloc);
1608 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1614 * Try to create the inline data for the new dir.
1615 * If it succeeds, return 0, otherwise return the error.
1616 * In case of ENOSPC, the caller should create the normal disk layout dir.
1618 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1619 struct inode *inode)
1621 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1622 struct ext4_iloc iloc;
1623 struct ext4_dir_entry_2 *de;
1625 ret = ext4_get_inode_loc(inode, &iloc);
1629 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1634 * For inline dir, we only save the inode information for the ".."
1635 * and create a fake dentry to cover the left space.
1637 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1638 de->inode = cpu_to_le32(parent->i_ino);
1639 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1641 de->rec_len = ext4_rec_len_to_disk(
1642 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1644 set_nlink(inode, 2);
1645 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1651 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1652 struct ext4_filename *fname,
1653 struct ext4_dir_entry_2 **res_dir,
1654 int *has_inline_data)
1657 struct ext4_iloc iloc;
1661 if (ext4_get_inode_loc(dir, &iloc))
1664 down_read(&EXT4_I(dir)->xattr_sem);
1665 if (!ext4_has_inline_data(dir)) {
1666 *has_inline_data = 0;
1670 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1671 EXT4_INLINE_DOTDOT_SIZE;
1672 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1673 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1674 dir, fname, 0, res_dir);
1680 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1683 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1684 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1686 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1687 dir, fname, 0, res_dir);
1695 up_read(&EXT4_I(dir)->xattr_sem);
1699 int ext4_delete_inline_entry(handle_t *handle,
1701 struct ext4_dir_entry_2 *de_del,
1702 struct buffer_head *bh,
1703 int *has_inline_data)
1705 int err, inline_size, no_expand;
1706 struct ext4_iloc iloc;
1709 err = ext4_get_inode_loc(dir, &iloc);
1713 ext4_write_lock_xattr(dir, &no_expand);
1714 if (!ext4_has_inline_data(dir)) {
1715 *has_inline_data = 0;
1719 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1720 EXT4_MIN_INLINE_DATA_SIZE) {
1721 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1722 EXT4_INLINE_DOTDOT_SIZE;
1723 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1724 EXT4_INLINE_DOTDOT_SIZE;
1726 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1727 inline_size = ext4_get_inline_size(dir) -
1728 EXT4_MIN_INLINE_DATA_SIZE;
1731 BUFFER_TRACE(bh, "get_write_access");
1732 err = ext4_journal_get_write_access(handle, bh);
1736 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1737 inline_start, inline_size, 0);
1741 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1743 ext4_write_unlock_xattr(dir, &no_expand);
1744 if (likely(err == 0))
1745 err = ext4_mark_inode_dirty(handle, dir);
1748 ext4_std_error(dir->i_sb, err);
1753 * Get the inline dentry at offset.
1755 static inline struct ext4_dir_entry_2 *
1756 ext4_get_inline_entry(struct inode *inode,
1757 struct ext4_iloc *iloc,
1758 unsigned int offset,
1759 void **inline_start,
1764 BUG_ON(offset > ext4_get_inline_size(inode));
1766 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1767 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1768 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1770 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1771 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1772 *inline_size = ext4_get_inline_size(inode) -
1773 EXT4_MIN_INLINE_DATA_SIZE;
1777 *inline_start = inline_pos;
1778 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1781 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1783 int err, inline_size;
1784 struct ext4_iloc iloc;
1787 unsigned int offset;
1788 struct ext4_dir_entry_2 *de;
1791 err = ext4_get_inode_loc(dir, &iloc);
1793 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1798 down_read(&EXT4_I(dir)->xattr_sem);
1799 if (!ext4_has_inline_data(dir)) {
1800 *has_inline_data = 0;
1804 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1805 if (!le32_to_cpu(de->inode)) {
1806 ext4_warning(dir->i_sb,
1807 "bad inline directory (dir #%lu) - no `..'",
1813 inline_len = ext4_get_inline_size(dir);
1814 offset = EXT4_INLINE_DOTDOT_SIZE;
1815 while (offset < inline_len) {
1816 de = ext4_get_inline_entry(dir, &iloc, offset,
1817 &inline_pos, &inline_size);
1818 if (ext4_check_dir_entry(dir, NULL, de,
1819 iloc.bh, inline_pos,
1820 inline_size, offset)) {
1821 ext4_warning(dir->i_sb,
1822 "bad inline directory (dir #%lu) - "
1823 "inode %u, rec_len %u, name_len %d"
1825 dir->i_ino, le32_to_cpu(de->inode),
1826 le16_to_cpu(de->rec_len), de->name_len,
1831 if (le32_to_cpu(de->inode)) {
1835 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1839 up_read(&EXT4_I(dir)->xattr_sem);
1844 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1848 ext4_write_lock_xattr(inode, &no_expand);
1849 ret = ext4_destroy_inline_data_nolock(handle, inode);
1850 ext4_write_unlock_xattr(inode, &no_expand);
1855 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1858 int error = -EAGAIN;
1859 struct ext4_iloc iloc;
1861 down_read(&EXT4_I(inode)->xattr_sem);
1862 if (!ext4_has_inline_data(inode))
1865 error = ext4_get_inode_loc(inode, &iloc);
1869 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1870 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1871 addr += offsetof(struct ext4_inode, i_block);
1877 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1878 i_size_read(inode));
1879 iomap->type = IOMAP_INLINE;
1883 up_read(&EXT4_I(inode)->xattr_sem);
1887 int ext4_inline_data_fiemap(struct inode *inode,
1888 struct fiemap_extent_info *fieinfo,
1889 int *has_inline, __u64 start, __u64 len)
1893 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1896 struct ext4_iloc iloc;
1898 down_read(&EXT4_I(inode)->xattr_sem);
1899 if (!ext4_has_inline_data(inode)) {
1903 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1904 i_size_read(inode));
1905 if (start >= inline_len)
1907 if (start + len < inline_len)
1908 inline_len = start + len;
1909 inline_len -= start;
1911 error = ext4_get_inode_loc(inode, &iloc);
1915 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1916 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1917 physical += offsetof(struct ext4_inode, i_block);
1921 up_read(&EXT4_I(inode)->xattr_sem);
1923 error = fiemap_fill_next_extent(fieinfo, start, physical,
1925 return (error < 0 ? error : 0);
1928 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1931 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1934 struct ext4_xattr_ibody_find is = {
1935 .s = { .not_found = -ENODATA, },
1937 struct ext4_xattr_info i = {
1938 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1939 .name = EXT4_XATTR_SYSTEM_DATA,
1943 needed_blocks = ext4_writepage_trans_blocks(inode);
1944 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1946 return PTR_ERR(handle);
1948 ext4_write_lock_xattr(inode, &no_expand);
1949 if (!ext4_has_inline_data(inode)) {
1950 ext4_write_unlock_xattr(inode, &no_expand);
1952 ext4_journal_stop(handle);
1956 if ((err = ext4_orphan_add(handle, inode)) != 0)
1959 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1962 down_write(&EXT4_I(inode)->i_data_sem);
1963 i_size = inode->i_size;
1964 inline_size = ext4_get_inline_size(inode);
1965 EXT4_I(inode)->i_disksize = i_size;
1967 if (i_size < inline_size) {
1968 /* Clear the content in the xattr space. */
1969 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1970 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1973 BUG_ON(is.s.not_found);
1975 value_len = le32_to_cpu(is.s.here->e_value_size);
1976 value = kmalloc(value_len, GFP_NOFS);
1982 err = ext4_xattr_ibody_get(inode, i.name_index,
1983 i.name, value, value_len);
1988 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1989 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1990 err = ext4_xattr_ibody_set(handle, inode, &i, &is);
1995 /* Clear the content within i_blocks. */
1996 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1997 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1998 memset(p + i_size, 0,
1999 EXT4_MIN_INLINE_DATA_SIZE - i_size);
2002 EXT4_I(inode)->i_inline_size = i_size <
2003 EXT4_MIN_INLINE_DATA_SIZE ?
2004 EXT4_MIN_INLINE_DATA_SIZE : i_size;
2008 up_write(&EXT4_I(inode)->i_data_sem);
2011 ext4_write_unlock_xattr(inode, &no_expand);
2014 ext4_orphan_del(handle, inode);
2017 inode->i_mtime = inode->i_ctime = current_time(inode);
2018 err = ext4_mark_inode_dirty(handle, inode);
2020 ext4_handle_sync(handle);
2022 ext4_journal_stop(handle);
2026 int ext4_convert_inline_data(struct inode *inode)
2028 int error, needed_blocks, no_expand;
2030 struct ext4_iloc iloc;
2032 if (!ext4_has_inline_data(inode)) {
2033 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2035 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2037 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2038 * cleared. This means we are in the middle of moving of
2039 * inline data to delay allocated block. Just force writeout
2040 * here to finish conversion.
2042 error = filemap_flush(inode->i_mapping);
2045 if (!ext4_has_inline_data(inode))
2049 needed_blocks = ext4_writepage_trans_blocks(inode);
2052 error = ext4_get_inode_loc(inode, &iloc);
2056 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2057 if (IS_ERR(handle)) {
2058 error = PTR_ERR(handle);
2062 ext4_write_lock_xattr(inode, &no_expand);
2063 if (ext4_has_inline_data(inode))
2064 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2065 ext4_write_unlock_xattr(inode, &no_expand);
2066 ext4_journal_stop(handle);