2 * Copyright (c) 2012 Taobao.
3 * Written by Tao Ma <boyu.mt@taobao.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/fiemap.h>
17 #include "ext4_jbd2.h"
22 #define EXT4_XATTR_SYSTEM_DATA "data"
23 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
24 #define EXT4_INLINE_DOTDOT_OFFSET 2
25 #define EXT4_INLINE_DOTDOT_SIZE 4
27 static int ext4_get_inline_size(struct inode *inode)
29 if (EXT4_I(inode)->i_inline_off)
30 return EXT4_I(inode)->i_inline_size;
35 static int get_max_inline_xattr_value_size(struct inode *inode,
36 struct ext4_iloc *iloc)
38 struct ext4_xattr_ibody_header *header;
39 struct ext4_xattr_entry *entry;
40 struct ext4_inode *raw_inode;
44 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
47 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
48 EXT4_GOOD_OLD_INODE_SIZE -
49 EXT4_I(inode)->i_extra_isize -
50 sizeof(struct ext4_xattr_ibody_header);
53 * We need to subtract another sizeof(__u32) since an in-inode xattr
54 * needs an empty 4 bytes to indicate the gap between the xattr entry
55 * and the name/value pair.
57 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
58 return EXT4_XATTR_SIZE(min_offs -
59 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
60 EXT4_XATTR_ROUND - sizeof(__u32));
62 raw_inode = ext4_raw_inode(iloc);
63 header = IHDR(inode, raw_inode);
64 entry = IFIRST(header);
65 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
67 /* Compute min_offs. */
68 while (!IS_LAST_ENTRY(entry)) {
69 void *next = EXT4_XATTR_NEXT(entry);
72 EXT4_ERROR_INODE(inode,
73 "corrupt xattr in inline inode");
76 if (!entry->e_value_inum && entry->e_value_size) {
77 size_t offs = le16_to_cpu(entry->e_value_offs);
84 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
86 if (EXT4_I(inode)->i_inline_off) {
87 entry = (struct ext4_xattr_entry *)
88 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
90 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
94 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
96 if (free > EXT4_XATTR_ROUND)
97 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
106 * Get the maximum size we now can store in an inode.
107 * If we can't find the space for a xattr entry, don't use the space
108 * of the extents since we have no space to indicate the inline data.
110 int ext4_get_max_inline_size(struct inode *inode)
112 int error, max_inline_size;
113 struct ext4_iloc iloc;
115 if (EXT4_I(inode)->i_extra_isize == 0)
118 error = ext4_get_inode_loc(inode, &iloc);
120 ext4_error_inode(inode, __func__, __LINE__, 0,
121 "can't get inode location %lu",
126 down_read(&EXT4_I(inode)->xattr_sem);
127 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
128 up_read(&EXT4_I(inode)->xattr_sem);
132 if (!max_inline_size)
135 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
139 * this function does not take xattr_sem, which is OK because it is
140 * currently only used in a code path coming form ext4_iget, before
141 * the new inode has been unlocked
143 int ext4_find_inline_data_nolock(struct inode *inode)
145 struct ext4_xattr_ibody_find is = {
146 .s = { .not_found = -ENODATA, },
148 struct ext4_xattr_info i = {
149 .name_index = EXT4_XATTR_INDEX_SYSTEM,
150 .name = EXT4_XATTR_SYSTEM_DATA,
154 if (EXT4_I(inode)->i_extra_isize == 0)
157 error = ext4_get_inode_loc(inode, &is.iloc);
161 error = ext4_xattr_ibody_find(inode, &i, &is);
165 if (!is.s.not_found) {
166 if (is.s.here->e_value_inum) {
167 EXT4_ERROR_INODE(inode, "inline data xattr refers "
168 "to an external xattr inode");
169 error = -EFSCORRUPTED;
172 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
173 (void *)ext4_raw_inode(&is.iloc));
174 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
175 le32_to_cpu(is.s.here->e_value_size);
182 static int ext4_read_inline_data(struct inode *inode, void *buffer,
184 struct ext4_iloc *iloc)
186 struct ext4_xattr_entry *entry;
187 struct ext4_xattr_ibody_header *header;
189 struct ext4_inode *raw_inode;
194 BUG_ON(len > EXT4_I(inode)->i_inline_size);
196 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
197 len : EXT4_MIN_INLINE_DATA_SIZE;
199 raw_inode = ext4_raw_inode(iloc);
200 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
208 header = IHDR(inode, raw_inode);
209 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
210 EXT4_I(inode)->i_inline_off);
211 len = min_t(unsigned int, len,
212 (unsigned int)le32_to_cpu(entry->e_value_size));
215 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
223 * write the buffer to the inline inode.
224 * If 'create' is set, we don't need to do the extra copy in the xattr
225 * value since it is already handled by ext4_xattr_ibody_set.
226 * That saves us one memcpy.
228 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
229 void *buffer, loff_t pos, unsigned int len)
231 struct ext4_xattr_entry *entry;
232 struct ext4_xattr_ibody_header *header;
233 struct ext4_inode *raw_inode;
236 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
239 BUG_ON(!EXT4_I(inode)->i_inline_off);
240 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
242 raw_inode = ext4_raw_inode(iloc);
245 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
246 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
247 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
248 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
258 pos -= EXT4_MIN_INLINE_DATA_SIZE;
259 header = IHDR(inode, raw_inode);
260 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
261 EXT4_I(inode)->i_inline_off);
263 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
267 static int ext4_create_inline_data(handle_t *handle,
268 struct inode *inode, unsigned len)
272 struct ext4_xattr_ibody_find is = {
273 .s = { .not_found = -ENODATA, },
275 struct ext4_xattr_info i = {
276 .name_index = EXT4_XATTR_INDEX_SYSTEM,
277 .name = EXT4_XATTR_SYSTEM_DATA,
280 error = ext4_get_inode_loc(inode, &is.iloc);
284 BUFFER_TRACE(is.iloc.bh, "get_write_access");
285 error = ext4_journal_get_write_access(handle, is.iloc.bh);
289 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
290 value = EXT4_ZERO_XATTR_VALUE;
291 len -= EXT4_MIN_INLINE_DATA_SIZE;
297 /* Insert the the xttr entry. */
301 error = ext4_xattr_ibody_find(inode, &i, &is);
305 BUG_ON(!is.s.not_found);
307 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
309 if (error == -ENOSPC)
310 ext4_clear_inode_state(inode,
311 EXT4_STATE_MAY_INLINE_DATA);
315 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
316 0, EXT4_MIN_INLINE_DATA_SIZE);
318 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
319 (void *)ext4_raw_inode(&is.iloc));
320 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
321 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
322 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
324 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
331 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
336 struct ext4_xattr_ibody_find is = {
337 .s = { .not_found = -ENODATA, },
339 struct ext4_xattr_info i = {
340 .name_index = EXT4_XATTR_INDEX_SYSTEM,
341 .name = EXT4_XATTR_SYSTEM_DATA,
344 /* If the old space is ok, write the data directly. */
345 if (len <= EXT4_I(inode)->i_inline_size)
348 error = ext4_get_inode_loc(inode, &is.iloc);
352 error = ext4_xattr_ibody_find(inode, &i, &is);
356 BUG_ON(is.s.not_found);
358 len -= EXT4_MIN_INLINE_DATA_SIZE;
359 value = kzalloc(len, GFP_NOFS);
365 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
370 BUFFER_TRACE(is.iloc.bh, "get_write_access");
371 error = ext4_journal_get_write_access(handle, is.iloc.bh);
375 /* Update the xttr entry. */
379 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
383 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
384 (void *)ext4_raw_inode(&is.iloc));
385 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
386 le32_to_cpu(is.s.here->e_value_size);
387 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
389 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
397 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
400 int ret, size, no_expand;
401 struct ext4_inode_info *ei = EXT4_I(inode);
403 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
406 size = ext4_get_max_inline_size(inode);
410 ext4_write_lock_xattr(inode, &no_expand);
412 if (ei->i_inline_off)
413 ret = ext4_update_inline_data(handle, inode, len);
415 ret = ext4_create_inline_data(handle, inode, len);
417 ext4_write_unlock_xattr(inode, &no_expand);
421 static int ext4_destroy_inline_data_nolock(handle_t *handle,
424 struct ext4_inode_info *ei = EXT4_I(inode);
425 struct ext4_xattr_ibody_find is = {
426 .s = { .not_found = 0, },
428 struct ext4_xattr_info i = {
429 .name_index = EXT4_XATTR_INDEX_SYSTEM,
430 .name = EXT4_XATTR_SYSTEM_DATA,
436 if (!ei->i_inline_off)
439 error = ext4_get_inode_loc(inode, &is.iloc);
443 error = ext4_xattr_ibody_find(inode, &i, &is);
447 BUFFER_TRACE(is.iloc.bh, "get_write_access");
448 error = ext4_journal_get_write_access(handle, is.iloc.bh);
452 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
456 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
457 0, EXT4_MIN_INLINE_DATA_SIZE);
458 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
460 if (ext4_has_feature_extents(inode->i_sb)) {
461 if (S_ISDIR(inode->i_mode) ||
462 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
463 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
464 ext4_ext_tree_init(handle, inode);
467 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
470 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
472 EXT4_I(inode)->i_inline_off = 0;
473 EXT4_I(inode)->i_inline_size = 0;
474 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
477 if (error == -ENODATA)
482 static int ext4_read_inline_page(struct inode *inode, struct page *page)
487 struct ext4_iloc iloc;
489 BUG_ON(!PageLocked(page));
490 BUG_ON(!ext4_has_inline_data(inode));
493 if (!EXT4_I(inode)->i_inline_off) {
494 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
499 ret = ext4_get_inode_loc(inode, &iloc);
503 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
504 kaddr = kmap_atomic(page);
505 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
506 flush_dcache_page(page);
507 kunmap_atomic(kaddr);
508 zero_user_segment(page, len, PAGE_SIZE);
509 SetPageUptodate(page);
516 int ext4_readpage_inline(struct inode *inode, struct page *page)
520 down_read(&EXT4_I(inode)->xattr_sem);
521 if (!ext4_has_inline_data(inode)) {
522 up_read(&EXT4_I(inode)->xattr_sem);
527 * Current inline data can only exist in the 1st page,
528 * So for all the other pages, just set them uptodate.
531 ret = ext4_read_inline_page(inode, page);
532 else if (!PageUptodate(page)) {
533 zero_user_segment(page, 0, PAGE_SIZE);
534 SetPageUptodate(page);
537 up_read(&EXT4_I(inode)->xattr_sem);
540 return ret >= 0 ? 0 : ret;
543 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
547 int ret, needed_blocks, no_expand;
548 handle_t *handle = NULL;
549 int retries = 0, sem_held = 0;
550 struct page *page = NULL;
552 struct ext4_iloc iloc;
554 if (!ext4_has_inline_data(inode)) {
556 * clear the flag so that no new write
557 * will trap here again.
559 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
563 needed_blocks = ext4_writepage_trans_blocks(inode);
565 ret = ext4_get_inode_loc(inode, &iloc);
570 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
571 if (IS_ERR(handle)) {
572 ret = PTR_ERR(handle);
577 /* We cannot recurse into the filesystem as the transaction is already
579 flags |= AOP_FLAG_NOFS;
581 page = grab_cache_page_write_begin(mapping, 0, flags);
587 ext4_write_lock_xattr(inode, &no_expand);
589 /* If some one has already done this for us, just exit. */
590 if (!ext4_has_inline_data(inode)) {
596 to = ext4_get_inline_size(inode);
597 if (!PageUptodate(page)) {
598 ret = ext4_read_inline_page(inode, page);
603 ret = ext4_destroy_inline_data_nolock(handle, inode);
607 if (ext4_should_dioread_nolock(inode)) {
608 ret = __block_write_begin(page, from, to,
609 ext4_get_block_unwritten);
611 ret = __block_write_begin(page, from, to, ext4_get_block);
613 if (!ret && ext4_should_journal_data(inode)) {
614 ret = ext4_walk_page_buffers(handle, page_buffers(page),
616 do_journal_get_write_access);
623 ext4_orphan_add(handle, inode);
624 ext4_write_unlock_xattr(inode, &no_expand);
626 ext4_journal_stop(handle);
628 ext4_truncate_failed_write(inode);
630 * If truncate failed early the inode might
631 * still be on the orphan list; we need to
632 * make sure the inode is removed from the
633 * orphan list in that case.
636 ext4_orphan_del(NULL, inode);
639 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
643 block_commit_write(page, from, to);
650 ext4_write_unlock_xattr(inode, &no_expand);
652 ext4_journal_stop(handle);
658 * Try to write data in the inode.
659 * If the inode has inline data, check whether the new write can be
660 * in the inode also. If not, create the page the handle, move the data
661 * to the page make it update and let the later codes create extent for it.
663 int ext4_try_to_write_inline_data(struct address_space *mapping,
665 loff_t pos, unsigned len,
672 struct ext4_iloc iloc;
674 if (pos + len > ext4_get_max_inline_size(inode))
677 ret = ext4_get_inode_loc(inode, &iloc);
682 * The possible write could happen in the inode,
683 * so try to reserve the space in inode first.
685 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
686 if (IS_ERR(handle)) {
687 ret = PTR_ERR(handle);
692 ret = ext4_prepare_inline_data(handle, inode, pos + len);
693 if (ret && ret != -ENOSPC)
696 /* We don't have space in inline inode, so convert it to extent. */
697 if (ret == -ENOSPC) {
698 ext4_journal_stop(handle);
703 ret = ext4_journal_get_write_access(handle, iloc.bh);
707 flags |= AOP_FLAG_NOFS;
709 page = grab_cache_page_write_begin(mapping, 0, flags);
716 down_read(&EXT4_I(inode)->xattr_sem);
717 if (!ext4_has_inline_data(inode)) {
724 if (!PageUptodate(page)) {
725 ret = ext4_read_inline_page(inode, page);
736 up_read(&EXT4_I(inode)->xattr_sem);
738 if (handle && (ret != 1))
739 ext4_journal_stop(handle);
743 return ext4_convert_inline_data_to_extent(mapping,
747 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
748 unsigned copied, struct page *page)
752 struct ext4_iloc iloc;
754 if (unlikely(copied < len)) {
755 if (!PageUptodate(page)) {
761 ret = ext4_get_inode_loc(inode, &iloc);
763 ext4_std_error(inode->i_sb, ret);
768 ext4_write_lock_xattr(inode, &no_expand);
769 BUG_ON(!ext4_has_inline_data(inode));
772 * ei->i_inline_off may have changed since ext4_write_begin()
773 * called ext4_try_to_write_inline_data()
775 (void) ext4_find_inline_data_nolock(inode);
777 kaddr = kmap_atomic(page);
778 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
779 kunmap_atomic(kaddr);
780 SetPageUptodate(page);
781 /* clear page dirty so that writepages wouldn't work for us. */
782 ClearPageDirty(page);
784 ext4_write_unlock_xattr(inode, &no_expand);
786 mark_inode_dirty(inode);
792 ext4_journalled_write_inline_data(struct inode *inode,
798 struct ext4_iloc iloc;
800 ret = ext4_get_inode_loc(inode, &iloc);
802 ext4_std_error(inode->i_sb, ret);
806 ext4_write_lock_xattr(inode, &no_expand);
807 kaddr = kmap_atomic(page);
808 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
809 kunmap_atomic(kaddr);
810 ext4_write_unlock_xattr(inode, &no_expand);
816 * Try to make the page cache and handle ready for the inline data case.
817 * We can call this function in 2 cases:
818 * 1. The inode is created and the first write exceeds inline size. We can
819 * clear the inode state safely.
820 * 2. The inode has inline data, then we need to read the data, make it
821 * update and dirty so that ext4_da_writepages can handle it. We don't
822 * need to start the journal since the file's metatdata isn't changed now.
824 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
829 int ret = 0, inline_size;
832 page = grab_cache_page_write_begin(mapping, 0, flags);
836 down_read(&EXT4_I(inode)->xattr_sem);
837 if (!ext4_has_inline_data(inode)) {
838 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
842 inline_size = ext4_get_inline_size(inode);
844 if (!PageUptodate(page)) {
845 ret = ext4_read_inline_page(inode, page);
850 ret = __block_write_begin(page, 0, inline_size,
851 ext4_da_get_block_prep);
853 up_read(&EXT4_I(inode)->xattr_sem);
856 ext4_truncate_failed_write(inode);
861 SetPageUptodate(page);
862 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
863 *fsdata = (void *)CONVERT_INLINE_DATA;
866 up_read(&EXT4_I(inode)->xattr_sem);
875 * Prepare the write for the inline data.
876 * If the the data can be written into the inode, we just read
877 * the page and make it uptodate, and start the journal.
878 * Otherwise read the page, makes it dirty so that it can be
879 * handle in writepages(the i_disksize update is left to the
880 * normal ext4_da_write_end).
882 int ext4_da_write_inline_data_begin(struct address_space *mapping,
884 loff_t pos, unsigned len,
889 int ret, inline_size;
892 struct ext4_iloc iloc;
895 ret = ext4_get_inode_loc(inode, &iloc);
900 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
901 if (IS_ERR(handle)) {
902 ret = PTR_ERR(handle);
906 inline_size = ext4_get_max_inline_size(inode);
909 if (inline_size >= pos + len) {
910 ret = ext4_prepare_inline_data(handle, inode, pos + len);
911 if (ret && ret != -ENOSPC)
916 * We cannot recurse into the filesystem as the transaction
917 * is already started.
919 flags |= AOP_FLAG_NOFS;
921 if (ret == -ENOSPC) {
922 ext4_journal_stop(handle);
923 ret = ext4_da_convert_inline_data_to_extent(mapping,
927 if (ret == -ENOSPC &&
928 ext4_should_retry_alloc(inode->i_sb, &retries))
933 page = grab_cache_page_write_begin(mapping, 0, flags);
939 down_read(&EXT4_I(inode)->xattr_sem);
940 if (!ext4_has_inline_data(inode)) {
942 goto out_release_page;
945 if (!PageUptodate(page)) {
946 ret = ext4_read_inline_page(inode, page);
948 goto out_release_page;
950 ret = ext4_journal_get_write_access(handle, iloc.bh);
952 goto out_release_page;
954 up_read(&EXT4_I(inode)->xattr_sem);
959 up_read(&EXT4_I(inode)->xattr_sem);
963 ext4_journal_stop(handle);
969 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
970 unsigned len, unsigned copied,
975 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
984 * No need to use i_size_read() here, the i_size
985 * cannot change under us because we hold i_mutex.
987 * But it's important to update i_size while still holding page lock:
988 * page writeout could otherwise come in and zero beyond i_size.
990 if (pos+copied > inode->i_size)
991 i_size_write(inode, pos+copied);
996 * Don't mark the inode dirty under page lock. First, it unnecessarily
997 * makes the holding time of page lock longer. Second, it forces lock
998 * ordering of page lock and transaction start for journaling
1001 mark_inode_dirty(inode);
1006 #ifdef INLINE_DIR_DEBUG
1007 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
1008 void *inline_start, int inline_size)
1011 unsigned short de_len;
1012 struct ext4_dir_entry_2 *de = inline_start;
1013 void *dlimit = inline_start + inline_size;
1015 trace_printk("inode %lu\n", dir->i_ino);
1017 while ((void *)de < dlimit) {
1018 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1019 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1020 offset, de_len, de->name_len, de->name,
1021 de->name_len, le32_to_cpu(de->inode));
1022 if (ext4_check_dir_entry(dir, NULL, de, bh,
1023 inline_start, inline_size, offset))
1027 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1031 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1035 * Add a new entry into a inline dir.
1036 * It will return -ENOSPC if no space is available, and -EIO
1037 * and -EEXIST if directory entry already exists.
1039 static int ext4_add_dirent_to_inline(handle_t *handle,
1040 struct ext4_filename *fname,
1042 struct inode *inode,
1043 struct ext4_iloc *iloc,
1044 void *inline_start, int inline_size)
1047 struct ext4_dir_entry_2 *de;
1049 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1050 inline_size, fname, &de);
1054 BUFFER_TRACE(iloc->bh, "get_write_access");
1055 err = ext4_journal_get_write_access(handle, iloc->bh);
1058 ext4_insert_dentry(inode, de, inline_size, fname);
1060 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1063 * XXX shouldn't update any times until successful
1064 * completion of syscall, but too many callers depend
1067 * XXX similarly, too many callers depend on
1068 * ext4_new_inode() setting the times, but error
1069 * recovery deletes the inode, so the worst that can
1070 * happen is that the times are slightly out of date
1071 * and/or different from the directory change time.
1073 dir->i_mtime = dir->i_ctime = current_time(dir);
1074 ext4_update_dx_flag(dir);
1079 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1080 struct ext4_iloc *iloc)
1082 struct ext4_xattr_entry *entry;
1083 struct ext4_xattr_ibody_header *header;
1085 BUG_ON(!EXT4_I(inode)->i_inline_off);
1087 header = IHDR(inode, ext4_raw_inode(iloc));
1088 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1089 EXT4_I(inode)->i_inline_off);
1091 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1094 /* Set the final de to cover the whole block. */
1095 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1097 struct ext4_dir_entry_2 *de, *prev_de;
1101 de = (struct ext4_dir_entry_2 *)de_buf;
1103 limit = de_buf + old_size;
1106 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1108 de = (struct ext4_dir_entry_2 *)de_buf;
1109 } while (de_buf < limit);
1111 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1112 old_size, new_size);
1114 /* this is just created, so create an empty entry. */
1116 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1120 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1121 struct ext4_iloc *iloc)
1124 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1125 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1127 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1130 ret = ext4_update_inline_data(handle, dir,
1131 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1135 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1136 EXT4_I(dir)->i_inline_size -
1137 EXT4_MIN_INLINE_DATA_SIZE);
1138 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1142 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1143 struct ext4_iloc *iloc,
1144 void *buf, int inline_size)
1148 ret = ext4_create_inline_data(handle, inode, inline_size);
1150 ext4_msg(inode->i_sb, KERN_EMERG,
1151 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1155 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1156 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1159 static int ext4_finish_convert_inline_dir(handle_t *handle,
1160 struct inode *inode,
1161 struct buffer_head *dir_block,
1165 int err, csum_size = 0, header_size = 0;
1166 struct ext4_dir_entry_2 *de;
1167 struct ext4_dir_entry_tail *t;
1168 void *target = dir_block->b_data;
1171 * First create "." and ".." and then copy the dir information
1172 * back to the block.
1174 de = (struct ext4_dir_entry_2 *)target;
1175 de = ext4_init_dot_dotdot(inode, de,
1176 inode->i_sb->s_blocksize, csum_size,
1177 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1178 header_size = (void *)de - target;
1180 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1181 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1183 if (ext4_has_metadata_csum(inode->i_sb))
1184 csum_size = sizeof(struct ext4_dir_entry_tail);
1186 inode->i_size = inode->i_sb->s_blocksize;
1187 i_size_write(inode, inode->i_sb->s_blocksize);
1188 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1189 ext4_update_final_de(dir_block->b_data,
1190 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1191 inode->i_sb->s_blocksize - csum_size);
1194 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1195 inode->i_sb->s_blocksize);
1196 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1198 set_buffer_uptodate(dir_block);
1199 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1202 set_buffer_verified(dir_block);
1203 return ext4_mark_inode_dirty(handle, inode);
1206 static int ext4_convert_inline_data_nolock(handle_t *handle,
1207 struct inode *inode,
1208 struct ext4_iloc *iloc)
1212 struct buffer_head *data_bh = NULL;
1213 struct ext4_map_blocks map;
1216 inline_size = ext4_get_inline_size(inode);
1217 buf = kmalloc(inline_size, GFP_NOFS);
1223 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1228 * Make sure the inline directory entries pass checks before we try to
1229 * convert them, so that we avoid touching stuff that needs fsck.
1231 if (S_ISDIR(inode->i_mode)) {
1232 error = ext4_check_all_de(inode, iloc->bh,
1233 buf + EXT4_INLINE_DOTDOT_SIZE,
1234 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1239 error = ext4_destroy_inline_data_nolock(handle, inode);
1246 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1249 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1254 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1260 lock_buffer(data_bh);
1261 error = ext4_journal_get_create_access(handle, data_bh);
1263 unlock_buffer(data_bh);
1267 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1269 if (!S_ISDIR(inode->i_mode)) {
1270 memcpy(data_bh->b_data, buf, inline_size);
1271 set_buffer_uptodate(data_bh);
1272 error = ext4_handle_dirty_metadata(handle,
1275 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1279 unlock_buffer(data_bh);
1282 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1291 * Try to add the new entry to the inline data.
1292 * If succeeds, return 0. If not, extended the inline dir and copied data to
1293 * the new created block.
1295 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1296 struct inode *dir, struct inode *inode)
1298 int ret, inline_size, no_expand;
1300 struct ext4_iloc iloc;
1302 ret = ext4_get_inode_loc(dir, &iloc);
1306 ext4_write_lock_xattr(dir, &no_expand);
1307 if (!ext4_has_inline_data(dir))
1310 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1311 EXT4_INLINE_DOTDOT_SIZE;
1312 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1314 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1315 inline_start, inline_size);
1319 /* check whether it can be inserted to inline xattr space. */
1320 inline_size = EXT4_I(dir)->i_inline_size -
1321 EXT4_MIN_INLINE_DATA_SIZE;
1323 /* Try to use the xattr space.*/
1324 ret = ext4_update_inline_dir(handle, dir, &iloc);
1325 if (ret && ret != -ENOSPC)
1328 inline_size = EXT4_I(dir)->i_inline_size -
1329 EXT4_MIN_INLINE_DATA_SIZE;
1333 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1335 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1336 inode, &iloc, inline_start,
1344 * The inline space is filled up, so create a new block for it.
1345 * As the extent tree will be created, we have to save the inline
1348 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1351 ext4_write_unlock_xattr(dir, &no_expand);
1352 ext4_mark_inode_dirty(handle, dir);
1358 * This function fills a red-black tree with information from an
1359 * inlined dir. It returns the number directory entries loaded
1360 * into the tree. If there is an error it is returned in err.
1362 int htree_inlinedir_to_tree(struct file *dir_file,
1363 struct inode *dir, ext4_lblk_t block,
1364 struct dx_hash_info *hinfo,
1365 __u32 start_hash, __u32 start_minor_hash,
1366 int *has_inline_data)
1368 int err = 0, count = 0;
1369 unsigned int parent_ino;
1371 struct ext4_dir_entry_2 *de;
1372 struct inode *inode = file_inode(dir_file);
1373 int ret, inline_size = 0;
1374 struct ext4_iloc iloc;
1375 void *dir_buf = NULL;
1376 struct ext4_dir_entry_2 fake;
1377 struct fscrypt_str tmp_str;
1379 ret = ext4_get_inode_loc(inode, &iloc);
1383 down_read(&EXT4_I(inode)->xattr_sem);
1384 if (!ext4_has_inline_data(inode)) {
1385 up_read(&EXT4_I(inode)->xattr_sem);
1386 *has_inline_data = 0;
1390 inline_size = ext4_get_inline_size(inode);
1391 dir_buf = kmalloc(inline_size, GFP_NOFS);
1394 up_read(&EXT4_I(inode)->xattr_sem);
1398 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1399 up_read(&EXT4_I(inode)->xattr_sem);
1404 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1405 while (pos < inline_size) {
1407 * As inlined dir doesn't store any information about '.' and
1408 * only the inode number of '..' is stored, we have to handle
1412 fake.inode = cpu_to_le32(inode->i_ino);
1414 strcpy(fake.name, ".");
1415 fake.rec_len = ext4_rec_len_to_disk(
1416 EXT4_DIR_REC_LEN(fake.name_len),
1418 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1420 pos = EXT4_INLINE_DOTDOT_OFFSET;
1421 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1422 fake.inode = cpu_to_le32(parent_ino);
1424 strcpy(fake.name, "..");
1425 fake.rec_len = ext4_rec_len_to_disk(
1426 EXT4_DIR_REC_LEN(fake.name_len),
1428 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1430 pos = EXT4_INLINE_DOTDOT_SIZE;
1432 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1433 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1434 if (ext4_check_dir_entry(inode, dir_file, de,
1436 inline_size, pos)) {
1442 ext4fs_dirhash(de->name, de->name_len, hinfo);
1443 if ((hinfo->hash < start_hash) ||
1444 ((hinfo->hash == start_hash) &&
1445 (hinfo->minor_hash < start_minor_hash)))
1449 tmp_str.name = de->name;
1450 tmp_str.len = de->name_len;
1451 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1452 hinfo->minor_hash, de, &tmp_str);
1467 * So this function is called when the volume is mkfsed with
1468 * dir_index disabled. In order to keep f_pos persistent
1469 * after we convert from an inlined dir to a blocked based,
1470 * we just pretend that we are a normal dir and return the
1471 * offset as if '.' and '..' really take place.
1474 int ext4_read_inline_dir(struct file *file,
1475 struct dir_context *ctx,
1476 int *has_inline_data)
1478 unsigned int offset, parent_ino;
1480 struct ext4_dir_entry_2 *de;
1481 struct super_block *sb;
1482 struct inode *inode = file_inode(file);
1483 int ret, inline_size = 0;
1484 struct ext4_iloc iloc;
1485 void *dir_buf = NULL;
1486 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1488 ret = ext4_get_inode_loc(inode, &iloc);
1492 down_read(&EXT4_I(inode)->xattr_sem);
1493 if (!ext4_has_inline_data(inode)) {
1494 up_read(&EXT4_I(inode)->xattr_sem);
1495 *has_inline_data = 0;
1499 inline_size = ext4_get_inline_size(inode);
1500 dir_buf = kmalloc(inline_size, GFP_NOFS);
1503 up_read(&EXT4_I(inode)->xattr_sem);
1507 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1508 up_read(&EXT4_I(inode)->xattr_sem);
1514 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1518 * dotdot_offset and dotdot_size is the real offset and
1519 * size for ".." and "." if the dir is block based while
1520 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1521 * So we will use extra_offset and extra_size to indicate them
1522 * during the inline dir iteration.
1524 dotdot_offset = EXT4_DIR_REC_LEN(1);
1525 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1526 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1527 extra_size = extra_offset + inline_size;
1530 * If the version has changed since the last call to
1531 * readdir(2), then we might be pointing to an invalid
1532 * dirent right now. Scan from the start of the inline
1535 if (file->f_version != inode->i_version) {
1536 for (i = 0; i < extra_size && i < offset;) {
1538 * "." is with offset 0 and
1539 * ".." is dotdot_offset.
1544 } else if (i == dotdot_offset) {
1548 /* for other entry, the real offset in
1549 * the buf has to be tuned accordingly.
1551 de = (struct ext4_dir_entry_2 *)
1552 (dir_buf + i - extra_offset);
1553 /* It's too expensive to do a full
1554 * dirent test each time round this
1555 * loop, but we do have to test at
1556 * least that it is non-zero. A
1557 * failure will be detected in the
1558 * dirent test below. */
1559 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1560 < EXT4_DIR_REC_LEN(1))
1562 i += ext4_rec_len_from_disk(de->rec_len,
1567 file->f_version = inode->i_version;
1570 while (ctx->pos < extra_size) {
1571 if (ctx->pos == 0) {
1572 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1574 ctx->pos = dotdot_offset;
1578 if (ctx->pos == dotdot_offset) {
1579 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1581 ctx->pos = dotdot_size;
1585 de = (struct ext4_dir_entry_2 *)
1586 (dir_buf + ctx->pos - extra_offset);
1587 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1588 extra_size, ctx->pos))
1590 if (le32_to_cpu(de->inode)) {
1591 if (!dir_emit(ctx, de->name, de->name_len,
1592 le32_to_cpu(de->inode),
1593 get_dtype(sb, de->file_type)))
1596 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1604 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1605 struct ext4_dir_entry_2 **parent_de,
1608 struct ext4_iloc iloc;
1610 *retval = ext4_get_inode_loc(inode, &iloc);
1614 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1620 * Try to create the inline data for the new dir.
1621 * If it succeeds, return 0, otherwise return the error.
1622 * In case of ENOSPC, the caller should create the normal disk layout dir.
1624 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1625 struct inode *inode)
1627 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1628 struct ext4_iloc iloc;
1629 struct ext4_dir_entry_2 *de;
1631 ret = ext4_get_inode_loc(inode, &iloc);
1635 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1640 * For inline dir, we only save the inode information for the ".."
1641 * and create a fake dentry to cover the left space.
1643 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1644 de->inode = cpu_to_le32(parent->i_ino);
1645 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1647 de->rec_len = ext4_rec_len_to_disk(
1648 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1650 set_nlink(inode, 2);
1651 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1657 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1658 struct ext4_filename *fname,
1659 struct ext4_dir_entry_2 **res_dir,
1660 int *has_inline_data)
1663 struct ext4_iloc iloc;
1667 if (ext4_get_inode_loc(dir, &iloc))
1670 down_read(&EXT4_I(dir)->xattr_sem);
1671 if (!ext4_has_inline_data(dir)) {
1672 *has_inline_data = 0;
1676 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1677 EXT4_INLINE_DOTDOT_SIZE;
1678 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1679 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1680 dir, fname, 0, res_dir);
1686 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1689 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1690 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1692 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1693 dir, fname, 0, res_dir);
1701 up_read(&EXT4_I(dir)->xattr_sem);
1705 int ext4_delete_inline_entry(handle_t *handle,
1707 struct ext4_dir_entry_2 *de_del,
1708 struct buffer_head *bh,
1709 int *has_inline_data)
1711 int err, inline_size, no_expand;
1712 struct ext4_iloc iloc;
1715 err = ext4_get_inode_loc(dir, &iloc);
1719 ext4_write_lock_xattr(dir, &no_expand);
1720 if (!ext4_has_inline_data(dir)) {
1721 *has_inline_data = 0;
1725 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1726 EXT4_MIN_INLINE_DATA_SIZE) {
1727 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1728 EXT4_INLINE_DOTDOT_SIZE;
1729 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1730 EXT4_INLINE_DOTDOT_SIZE;
1732 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1733 inline_size = ext4_get_inline_size(dir) -
1734 EXT4_MIN_INLINE_DATA_SIZE;
1737 BUFFER_TRACE(bh, "get_write_access");
1738 err = ext4_journal_get_write_access(handle, bh);
1742 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1743 inline_start, inline_size, 0);
1747 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1749 ext4_write_unlock_xattr(dir, &no_expand);
1750 if (likely(err == 0))
1751 err = ext4_mark_inode_dirty(handle, dir);
1754 ext4_std_error(dir->i_sb, err);
1759 * Get the inline dentry at offset.
1761 static inline struct ext4_dir_entry_2 *
1762 ext4_get_inline_entry(struct inode *inode,
1763 struct ext4_iloc *iloc,
1764 unsigned int offset,
1765 void **inline_start,
1770 BUG_ON(offset > ext4_get_inline_size(inode));
1772 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1773 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1774 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1776 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1777 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1778 *inline_size = ext4_get_inline_size(inode) -
1779 EXT4_MIN_INLINE_DATA_SIZE;
1783 *inline_start = inline_pos;
1784 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1787 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1789 int err, inline_size;
1790 struct ext4_iloc iloc;
1793 unsigned int offset;
1794 struct ext4_dir_entry_2 *de;
1797 err = ext4_get_inode_loc(dir, &iloc);
1799 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1804 down_read(&EXT4_I(dir)->xattr_sem);
1805 if (!ext4_has_inline_data(dir)) {
1806 *has_inline_data = 0;
1810 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1811 if (!le32_to_cpu(de->inode)) {
1812 ext4_warning(dir->i_sb,
1813 "bad inline directory (dir #%lu) - no `..'",
1819 inline_len = ext4_get_inline_size(dir);
1820 offset = EXT4_INLINE_DOTDOT_SIZE;
1821 while (offset < inline_len) {
1822 de = ext4_get_inline_entry(dir, &iloc, offset,
1823 &inline_pos, &inline_size);
1824 if (ext4_check_dir_entry(dir, NULL, de,
1825 iloc.bh, inline_pos,
1826 inline_size, offset)) {
1827 ext4_warning(dir->i_sb,
1828 "bad inline directory (dir #%lu) - "
1829 "inode %u, rec_len %u, name_len %d"
1831 dir->i_ino, le32_to_cpu(de->inode),
1832 le16_to_cpu(de->rec_len), de->name_len,
1837 if (le32_to_cpu(de->inode)) {
1841 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1845 up_read(&EXT4_I(dir)->xattr_sem);
1850 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1854 ext4_write_lock_xattr(inode, &no_expand);
1855 ret = ext4_destroy_inline_data_nolock(handle, inode);
1856 ext4_write_unlock_xattr(inode, &no_expand);
1861 int ext4_inline_data_fiemap(struct inode *inode,
1862 struct fiemap_extent_info *fieinfo,
1863 int *has_inline, __u64 start, __u64 len)
1867 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1870 struct ext4_iloc iloc;
1872 down_read(&EXT4_I(inode)->xattr_sem);
1873 if (!ext4_has_inline_data(inode)) {
1877 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1878 i_size_read(inode));
1879 if (start >= inline_len)
1881 if (start + len < inline_len)
1882 inline_len = start + len;
1883 inline_len -= start;
1885 error = ext4_get_inode_loc(inode, &iloc);
1889 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1890 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1891 physical += offsetof(struct ext4_inode, i_block);
1895 up_read(&EXT4_I(inode)->xattr_sem);
1897 error = fiemap_fill_next_extent(fieinfo, start, physical,
1899 return (error < 0 ? error : 0);
1902 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1905 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1908 struct ext4_xattr_ibody_find is = {
1909 .s = { .not_found = -ENODATA, },
1911 struct ext4_xattr_info i = {
1912 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1913 .name = EXT4_XATTR_SYSTEM_DATA,
1917 needed_blocks = ext4_writepage_trans_blocks(inode);
1918 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1920 return PTR_ERR(handle);
1922 ext4_write_lock_xattr(inode, &no_expand);
1923 if (!ext4_has_inline_data(inode)) {
1924 ext4_write_unlock_xattr(inode, &no_expand);
1926 ext4_journal_stop(handle);
1930 if ((err = ext4_orphan_add(handle, inode)) != 0)
1933 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1936 down_write(&EXT4_I(inode)->i_data_sem);
1937 i_size = inode->i_size;
1938 inline_size = ext4_get_inline_size(inode);
1939 EXT4_I(inode)->i_disksize = i_size;
1941 if (i_size < inline_size) {
1942 /* Clear the content in the xattr space. */
1943 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1944 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1947 BUG_ON(is.s.not_found);
1949 value_len = le32_to_cpu(is.s.here->e_value_size);
1950 value = kmalloc(value_len, GFP_NOFS);
1956 err = ext4_xattr_ibody_get(inode, i.name_index,
1957 i.name, value, value_len);
1962 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1963 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1964 err = ext4_xattr_ibody_set(handle, inode, &i, &is);
1969 /* Clear the content within i_blocks. */
1970 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1971 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1972 memset(p + i_size, 0,
1973 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1976 EXT4_I(inode)->i_inline_size = i_size <
1977 EXT4_MIN_INLINE_DATA_SIZE ?
1978 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1982 up_write(&EXT4_I(inode)->i_data_sem);
1985 ext4_write_unlock_xattr(inode, &no_expand);
1988 ext4_orphan_del(handle, inode);
1991 inode->i_mtime = inode->i_ctime = current_time(inode);
1992 err = ext4_mark_inode_dirty(handle, inode);
1994 ext4_handle_sync(handle);
1996 ext4_journal_stop(handle);
2000 int ext4_convert_inline_data(struct inode *inode)
2002 int error, needed_blocks, no_expand;
2004 struct ext4_iloc iloc;
2006 if (!ext4_has_inline_data(inode)) {
2007 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2009 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2011 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2012 * cleared. This means we are in the middle of moving of
2013 * inline data to delay allocated block. Just force writeout
2014 * here to finish conversion.
2016 error = filemap_flush(inode->i_mapping);
2019 if (!ext4_has_inline_data(inode))
2023 needed_blocks = ext4_writepage_trans_blocks(inode);
2026 error = ext4_get_inode_loc(inode, &iloc);
2030 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2031 if (IS_ERR(handle)) {
2032 error = PTR_ERR(handle);
2036 ext4_write_lock_xattr(inode, &no_expand);
2037 if (ext4_has_inline_data(inode))
2038 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2039 ext4_write_unlock_xattr(inode, &no_expand);
2040 ext4_journal_stop(handle);