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;
43 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
46 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
47 EXT4_GOOD_OLD_INODE_SIZE -
48 EXT4_I(inode)->i_extra_isize -
49 sizeof(struct ext4_xattr_ibody_header);
52 * We need to subtract another sizeof(__u32) since an in-inode xattr
53 * needs an empty 4 bytes to indicate the gap between the xattr entry
54 * and the name/value pair.
56 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
57 return EXT4_XATTR_SIZE(min_offs -
58 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
59 EXT4_XATTR_ROUND - sizeof(__u32));
61 raw_inode = ext4_raw_inode(iloc);
62 header = IHDR(inode, raw_inode);
63 entry = IFIRST(header);
65 /* Compute min_offs. */
66 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
67 if (!entry->e_value_inum && entry->e_value_size) {
68 size_t offs = le16_to_cpu(entry->e_value_offs);
74 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
76 if (EXT4_I(inode)->i_inline_off) {
77 entry = (struct ext4_xattr_entry *)
78 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
80 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
84 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
86 if (free > EXT4_XATTR_ROUND)
87 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
96 * Get the maximum size we now can store in an inode.
97 * If we can't find the space for a xattr entry, don't use the space
98 * of the extents since we have no space to indicate the inline data.
100 int ext4_get_max_inline_size(struct inode *inode)
102 int error, max_inline_size;
103 struct ext4_iloc iloc;
105 if (EXT4_I(inode)->i_extra_isize == 0)
108 error = ext4_get_inode_loc(inode, &iloc);
110 ext4_error_inode(inode, __func__, __LINE__, 0,
111 "can't get inode location %lu",
116 down_read(&EXT4_I(inode)->xattr_sem);
117 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
118 up_read(&EXT4_I(inode)->xattr_sem);
122 if (!max_inline_size)
125 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
129 * this function does not take xattr_sem, which is OK because it is
130 * currently only used in a code path coming form ext4_iget, before
131 * the new inode has been unlocked
133 int ext4_find_inline_data_nolock(struct inode *inode)
135 struct ext4_xattr_ibody_find is = {
136 .s = { .not_found = -ENODATA, },
138 struct ext4_xattr_info i = {
139 .name_index = EXT4_XATTR_INDEX_SYSTEM,
140 .name = EXT4_XATTR_SYSTEM_DATA,
144 if (EXT4_I(inode)->i_extra_isize == 0)
147 error = ext4_get_inode_loc(inode, &is.iloc);
151 error = ext4_xattr_ibody_find(inode, &i, &is);
155 if (!is.s.not_found) {
156 if (is.s.here->e_value_inum) {
157 EXT4_ERROR_INODE(inode, "inline data xattr refers "
158 "to an external xattr inode");
159 error = -EFSCORRUPTED;
162 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
163 (void *)ext4_raw_inode(&is.iloc));
164 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
165 le32_to_cpu(is.s.here->e_value_size);
172 static int ext4_read_inline_data(struct inode *inode, void *buffer,
174 struct ext4_iloc *iloc)
176 struct ext4_xattr_entry *entry;
177 struct ext4_xattr_ibody_header *header;
179 struct ext4_inode *raw_inode;
184 BUG_ON(len > EXT4_I(inode)->i_inline_size);
186 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
187 len : EXT4_MIN_INLINE_DATA_SIZE;
189 raw_inode = ext4_raw_inode(iloc);
190 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
198 header = IHDR(inode, raw_inode);
199 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
200 EXT4_I(inode)->i_inline_off);
201 len = min_t(unsigned int, len,
202 (unsigned int)le32_to_cpu(entry->e_value_size));
205 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
213 * write the buffer to the inline inode.
214 * If 'create' is set, we don't need to do the extra copy in the xattr
215 * value since it is already handled by ext4_xattr_ibody_inline_set.
216 * That saves us one memcpy.
218 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
219 void *buffer, loff_t pos, unsigned int len)
221 struct ext4_xattr_entry *entry;
222 struct ext4_xattr_ibody_header *header;
223 struct ext4_inode *raw_inode;
226 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
229 BUG_ON(!EXT4_I(inode)->i_inline_off);
230 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
232 raw_inode = ext4_raw_inode(iloc);
235 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
236 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
237 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
238 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
248 pos -= EXT4_MIN_INLINE_DATA_SIZE;
249 header = IHDR(inode, raw_inode);
250 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
251 EXT4_I(inode)->i_inline_off);
253 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
257 static int ext4_create_inline_data(handle_t *handle,
258 struct inode *inode, unsigned len)
262 struct ext4_xattr_ibody_find is = {
263 .s = { .not_found = -ENODATA, },
265 struct ext4_xattr_info i = {
266 .name_index = EXT4_XATTR_INDEX_SYSTEM,
267 .name = EXT4_XATTR_SYSTEM_DATA,
270 error = ext4_get_inode_loc(inode, &is.iloc);
274 BUFFER_TRACE(is.iloc.bh, "get_write_access");
275 error = ext4_journal_get_write_access(handle, is.iloc.bh);
279 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
280 value = EXT4_ZERO_XATTR_VALUE;
281 len -= EXT4_MIN_INLINE_DATA_SIZE;
287 /* Insert the the xttr entry. */
291 error = ext4_xattr_ibody_find(inode, &i, &is);
295 BUG_ON(!is.s.not_found);
297 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
299 if (error == -ENOSPC)
300 ext4_clear_inode_state(inode,
301 EXT4_STATE_MAY_INLINE_DATA);
305 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
306 0, EXT4_MIN_INLINE_DATA_SIZE);
308 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
309 (void *)ext4_raw_inode(&is.iloc));
310 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
311 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
312 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
314 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
321 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
326 struct ext4_xattr_ibody_find is = {
327 .s = { .not_found = -ENODATA, },
329 struct ext4_xattr_info i = {
330 .name_index = EXT4_XATTR_INDEX_SYSTEM,
331 .name = EXT4_XATTR_SYSTEM_DATA,
334 /* If the old space is ok, write the data directly. */
335 if (len <= EXT4_I(inode)->i_inline_size)
338 error = ext4_get_inode_loc(inode, &is.iloc);
342 error = ext4_xattr_ibody_find(inode, &i, &is);
346 BUG_ON(is.s.not_found);
348 len -= EXT4_MIN_INLINE_DATA_SIZE;
349 value = kzalloc(len, GFP_NOFS);
355 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
357 if (error == -ENODATA)
360 BUFFER_TRACE(is.iloc.bh, "get_write_access");
361 error = ext4_journal_get_write_access(handle, is.iloc.bh);
365 /* Update the xttr entry. */
369 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
373 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
374 (void *)ext4_raw_inode(&is.iloc));
375 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
376 le32_to_cpu(is.s.here->e_value_size);
377 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
379 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
387 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
390 int ret, size, no_expand;
391 struct ext4_inode_info *ei = EXT4_I(inode);
393 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
396 size = ext4_get_max_inline_size(inode);
400 ext4_write_lock_xattr(inode, &no_expand);
402 if (ei->i_inline_off)
403 ret = ext4_update_inline_data(handle, inode, len);
405 ret = ext4_create_inline_data(handle, inode, len);
407 ext4_write_unlock_xattr(inode, &no_expand);
411 static int ext4_destroy_inline_data_nolock(handle_t *handle,
414 struct ext4_inode_info *ei = EXT4_I(inode);
415 struct ext4_xattr_ibody_find is = {
416 .s = { .not_found = 0, },
418 struct ext4_xattr_info i = {
419 .name_index = EXT4_XATTR_INDEX_SYSTEM,
420 .name = EXT4_XATTR_SYSTEM_DATA,
426 if (!ei->i_inline_off)
429 error = ext4_get_inode_loc(inode, &is.iloc);
433 error = ext4_xattr_ibody_find(inode, &i, &is);
437 BUFFER_TRACE(is.iloc.bh, "get_write_access");
438 error = ext4_journal_get_write_access(handle, is.iloc.bh);
442 error = ext4_xattr_ibody_inline_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,
537 int ret, needed_blocks, no_expand;
538 handle_t *handle = NULL;
539 int retries = 0, sem_held = 0;
540 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 |= AOP_FLAG_NOFS;
571 page = grab_cache_page_write_begin(mapping, 0, 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, 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, iloc.bh);
697 flags |= AOP_FLAG_NOFS;
699 page = grab_cache_page_write_begin(mapping, 0, flags);
706 down_read(&EXT4_I(inode)->xattr_sem);
707 if (!ext4_has_inline_data(inode)) {
714 if (!PageUptodate(page)) {
715 ret = ext4_read_inline_page(inode, page);
726 up_read(&EXT4_I(inode)->xattr_sem);
728 if (handle && (ret != 1))
729 ext4_journal_stop(handle);
733 return ext4_convert_inline_data_to_extent(mapping,
737 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
738 unsigned copied, struct page *page)
742 struct ext4_iloc iloc;
744 if (unlikely(copied < len)) {
745 if (!PageUptodate(page)) {
751 ret = ext4_get_inode_loc(inode, &iloc);
753 ext4_std_error(inode->i_sb, ret);
758 ext4_write_lock_xattr(inode, &no_expand);
759 BUG_ON(!ext4_has_inline_data(inode));
762 * ei->i_inline_off may have changed since ext4_write_begin()
763 * called 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, len);
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);
776 mark_inode_dirty(inode);
782 ext4_journalled_write_inline_data(struct inode *inode,
788 struct ext4_iloc iloc;
790 ret = ext4_get_inode_loc(inode, &iloc);
792 ext4_std_error(inode->i_sb, ret);
796 ext4_write_lock_xattr(inode, &no_expand);
797 kaddr = kmap_atomic(page);
798 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
799 kunmap_atomic(kaddr);
800 ext4_write_unlock_xattr(inode, &no_expand);
806 * Try to make the page cache and handle ready for the inline data case.
807 * We can call this function in 2 cases:
808 * 1. The inode is created and the first write exceeds inline size. We can
809 * clear the inode state safely.
810 * 2. The inode has inline data, then we need to read the data, make it
811 * update and dirty so that ext4_da_writepages can handle it. We don't
812 * need to start the journal since the file's metatdata isn't changed now.
814 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
819 int ret = 0, inline_size;
822 page = grab_cache_page_write_begin(mapping, 0, flags);
826 down_read(&EXT4_I(inode)->xattr_sem);
827 if (!ext4_has_inline_data(inode)) {
828 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
832 inline_size = ext4_get_inline_size(inode);
834 if (!PageUptodate(page)) {
835 ret = ext4_read_inline_page(inode, page);
840 ret = __block_write_begin(page, 0, inline_size,
841 ext4_da_get_block_prep);
843 up_read(&EXT4_I(inode)->xattr_sem);
846 ext4_truncate_failed_write(inode);
851 SetPageUptodate(page);
852 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
853 *fsdata = (void *)CONVERT_INLINE_DATA;
856 up_read(&EXT4_I(inode)->xattr_sem);
865 * Prepare the write for the inline data.
866 * If the the data can be written into the inode, we just read
867 * the page and make it uptodate, and start the journal.
868 * Otherwise read the page, makes it dirty so that it can be
869 * handle in writepages(the i_disksize update is left to the
870 * normal ext4_da_write_end).
872 int ext4_da_write_inline_data_begin(struct address_space *mapping,
874 loff_t pos, unsigned len,
879 int ret, inline_size;
882 struct ext4_iloc iloc;
885 ret = ext4_get_inode_loc(inode, &iloc);
890 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
891 if (IS_ERR(handle)) {
892 ret = PTR_ERR(handle);
896 inline_size = ext4_get_max_inline_size(inode);
899 if (inline_size >= pos + len) {
900 ret = ext4_prepare_inline_data(handle, inode, pos + len);
901 if (ret && ret != -ENOSPC)
906 * We cannot recurse into the filesystem as the transaction
907 * is already started.
909 flags |= AOP_FLAG_NOFS;
911 if (ret == -ENOSPC) {
912 ext4_journal_stop(handle);
913 ret = ext4_da_convert_inline_data_to_extent(mapping,
917 if (ret == -ENOSPC &&
918 ext4_should_retry_alloc(inode->i_sb, &retries))
923 page = grab_cache_page_write_begin(mapping, 0, flags);
929 down_read(&EXT4_I(inode)->xattr_sem);
930 if (!ext4_has_inline_data(inode)) {
932 goto out_release_page;
935 if (!PageUptodate(page)) {
936 ret = ext4_read_inline_page(inode, page);
938 goto out_release_page;
940 ret = ext4_journal_get_write_access(handle, iloc.bh);
942 goto out_release_page;
944 up_read(&EXT4_I(inode)->xattr_sem);
949 up_read(&EXT4_I(inode)->xattr_sem);
953 ext4_journal_stop(handle);
959 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
960 unsigned len, unsigned copied,
965 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
974 * No need to use i_size_read() here, the i_size
975 * cannot change under us because we hold i_mutex.
977 * But it's important to update i_size while still holding page lock:
978 * page writeout could otherwise come in and zero beyond i_size.
980 if (pos+copied > inode->i_size)
981 i_size_write(inode, pos+copied);
986 * Don't mark the inode dirty under page lock. First, it unnecessarily
987 * makes the holding time of page lock longer. Second, it forces lock
988 * ordering of page lock and transaction start for journaling
991 mark_inode_dirty(inode);
996 #ifdef INLINE_DIR_DEBUG
997 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
998 void *inline_start, int inline_size)
1001 unsigned short de_len;
1002 struct ext4_dir_entry_2 *de = inline_start;
1003 void *dlimit = inline_start + inline_size;
1005 trace_printk("inode %lu\n", dir->i_ino);
1007 while ((void *)de < dlimit) {
1008 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1009 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1010 offset, de_len, de->name_len, de->name,
1011 de->name_len, le32_to_cpu(de->inode));
1012 if (ext4_check_dir_entry(dir, NULL, de, bh,
1013 inline_start, inline_size, offset))
1017 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1021 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1025 * Add a new entry into a inline dir.
1026 * It will return -ENOSPC if no space is available, and -EIO
1027 * and -EEXIST if directory entry already exists.
1029 static int ext4_add_dirent_to_inline(handle_t *handle,
1030 struct ext4_filename *fname,
1032 struct inode *inode,
1033 struct ext4_iloc *iloc,
1034 void *inline_start, int inline_size)
1037 struct ext4_dir_entry_2 *de;
1039 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1040 inline_size, fname, &de);
1044 BUFFER_TRACE(iloc->bh, "get_write_access");
1045 err = ext4_journal_get_write_access(handle, iloc->bh);
1048 ext4_insert_dentry(inode, de, inline_size, fname);
1050 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1053 * XXX shouldn't update any times until successful
1054 * completion of syscall, but too many callers depend
1057 * XXX similarly, too many callers depend on
1058 * ext4_new_inode() setting the times, but error
1059 * recovery deletes the inode, so the worst that can
1060 * happen is that the times are slightly out of date
1061 * and/or different from the directory change time.
1063 dir->i_mtime = dir->i_ctime = current_time(dir);
1064 ext4_update_dx_flag(dir);
1069 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1070 struct ext4_iloc *iloc)
1072 struct ext4_xattr_entry *entry;
1073 struct ext4_xattr_ibody_header *header;
1075 BUG_ON(!EXT4_I(inode)->i_inline_off);
1077 header = IHDR(inode, ext4_raw_inode(iloc));
1078 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1079 EXT4_I(inode)->i_inline_off);
1081 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1084 /* Set the final de to cover the whole block. */
1085 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1087 struct ext4_dir_entry_2 *de, *prev_de;
1091 de = (struct ext4_dir_entry_2 *)de_buf;
1093 limit = de_buf + old_size;
1096 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1098 de = (struct ext4_dir_entry_2 *)de_buf;
1099 } while (de_buf < limit);
1101 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1102 old_size, new_size);
1104 /* this is just created, so create an empty entry. */
1106 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1110 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1111 struct ext4_iloc *iloc)
1114 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1115 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1117 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1120 ret = ext4_update_inline_data(handle, dir,
1121 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1125 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1126 EXT4_I(dir)->i_inline_size -
1127 EXT4_MIN_INLINE_DATA_SIZE);
1128 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1132 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1133 struct ext4_iloc *iloc,
1134 void *buf, int inline_size)
1138 ret = ext4_create_inline_data(handle, inode, inline_size);
1140 ext4_msg(inode->i_sb, KERN_EMERG,
1141 "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1145 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1146 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1149 static int ext4_finish_convert_inline_dir(handle_t *handle,
1150 struct inode *inode,
1151 struct buffer_head *dir_block,
1155 int err, csum_size = 0, header_size = 0;
1156 struct ext4_dir_entry_2 *de;
1157 struct ext4_dir_entry_tail *t;
1158 void *target = dir_block->b_data;
1161 * First create "." and ".." and then copy the dir information
1162 * back to the block.
1164 de = (struct ext4_dir_entry_2 *)target;
1165 de = ext4_init_dot_dotdot(inode, de,
1166 inode->i_sb->s_blocksize, csum_size,
1167 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1168 header_size = (void *)de - target;
1170 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1171 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1173 if (ext4_has_metadata_csum(inode->i_sb))
1174 csum_size = sizeof(struct ext4_dir_entry_tail);
1176 inode->i_size = inode->i_sb->s_blocksize;
1177 i_size_write(inode, inode->i_sb->s_blocksize);
1178 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1179 ext4_update_final_de(dir_block->b_data,
1180 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1181 inode->i_sb->s_blocksize - csum_size);
1184 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1185 inode->i_sb->s_blocksize);
1186 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1188 set_buffer_uptodate(dir_block);
1189 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1192 set_buffer_verified(dir_block);
1193 return ext4_mark_inode_dirty(handle, inode);
1196 static int ext4_convert_inline_data_nolock(handle_t *handle,
1197 struct inode *inode,
1198 struct ext4_iloc *iloc)
1202 struct buffer_head *data_bh = NULL;
1203 struct ext4_map_blocks map;
1206 inline_size = ext4_get_inline_size(inode);
1207 buf = kmalloc(inline_size, GFP_NOFS);
1213 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1218 * Make sure the inline directory entries pass checks before we try to
1219 * convert them, so that we avoid touching stuff that needs fsck.
1221 if (S_ISDIR(inode->i_mode)) {
1222 error = ext4_check_all_de(inode, iloc->bh,
1223 buf + EXT4_INLINE_DOTDOT_SIZE,
1224 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1229 error = ext4_destroy_inline_data_nolock(handle, inode);
1236 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1239 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1244 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1250 lock_buffer(data_bh);
1251 error = ext4_journal_get_create_access(handle, data_bh);
1253 unlock_buffer(data_bh);
1257 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1259 if (!S_ISDIR(inode->i_mode)) {
1260 memcpy(data_bh->b_data, buf, inline_size);
1261 set_buffer_uptodate(data_bh);
1262 error = ext4_handle_dirty_metadata(handle,
1265 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1269 unlock_buffer(data_bh);
1272 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1281 * Try to add the new entry to the inline data.
1282 * If succeeds, return 0. If not, extended the inline dir and copied data to
1283 * the new created block.
1285 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1286 struct inode *dir, struct inode *inode)
1288 int ret, inline_size, no_expand;
1290 struct ext4_iloc iloc;
1292 ret = ext4_get_inode_loc(dir, &iloc);
1296 ext4_write_lock_xattr(dir, &no_expand);
1297 if (!ext4_has_inline_data(dir))
1300 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1301 EXT4_INLINE_DOTDOT_SIZE;
1302 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1304 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1305 inline_start, inline_size);
1309 /* check whether it can be inserted to inline xattr space. */
1310 inline_size = EXT4_I(dir)->i_inline_size -
1311 EXT4_MIN_INLINE_DATA_SIZE;
1313 /* Try to use the xattr space.*/
1314 ret = ext4_update_inline_dir(handle, dir, &iloc);
1315 if (ret && ret != -ENOSPC)
1318 inline_size = EXT4_I(dir)->i_inline_size -
1319 EXT4_MIN_INLINE_DATA_SIZE;
1323 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1325 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1326 inode, &iloc, inline_start,
1334 * The inline space is filled up, so create a new block for it.
1335 * As the extent tree will be created, we have to save the inline
1338 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1341 ext4_write_unlock_xattr(dir, &no_expand);
1342 ext4_mark_inode_dirty(handle, dir);
1348 * This function fills a red-black tree with information from an
1349 * inlined dir. It returns the number directory entries loaded
1350 * into the tree. If there is an error it is returned in err.
1352 int htree_inlinedir_to_tree(struct file *dir_file,
1353 struct inode *dir, ext4_lblk_t block,
1354 struct dx_hash_info *hinfo,
1355 __u32 start_hash, __u32 start_minor_hash,
1356 int *has_inline_data)
1358 int err = 0, count = 0;
1359 unsigned int parent_ino;
1361 struct ext4_dir_entry_2 *de;
1362 struct inode *inode = file_inode(dir_file);
1363 int ret, inline_size = 0;
1364 struct ext4_iloc iloc;
1365 void *dir_buf = NULL;
1366 struct ext4_dir_entry_2 fake;
1367 struct fscrypt_str tmp_str;
1369 ret = ext4_get_inode_loc(inode, &iloc);
1373 down_read(&EXT4_I(inode)->xattr_sem);
1374 if (!ext4_has_inline_data(inode)) {
1375 up_read(&EXT4_I(inode)->xattr_sem);
1376 *has_inline_data = 0;
1380 inline_size = ext4_get_inline_size(inode);
1381 dir_buf = kmalloc(inline_size, GFP_NOFS);
1384 up_read(&EXT4_I(inode)->xattr_sem);
1388 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1389 up_read(&EXT4_I(inode)->xattr_sem);
1394 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1395 while (pos < inline_size) {
1397 * As inlined dir doesn't store any information about '.' and
1398 * only the inode number of '..' is stored, we have to handle
1402 fake.inode = cpu_to_le32(inode->i_ino);
1404 strcpy(fake.name, ".");
1405 fake.rec_len = ext4_rec_len_to_disk(
1406 EXT4_DIR_REC_LEN(fake.name_len),
1408 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1410 pos = EXT4_INLINE_DOTDOT_OFFSET;
1411 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1412 fake.inode = cpu_to_le32(parent_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_SIZE;
1422 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1423 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1424 if (ext4_check_dir_entry(inode, dir_file, de,
1426 inline_size, pos)) {
1432 ext4fs_dirhash(de->name, de->name_len, hinfo);
1433 if ((hinfo->hash < start_hash) ||
1434 ((hinfo->hash == start_hash) &&
1435 (hinfo->minor_hash < start_minor_hash)))
1439 tmp_str.name = de->name;
1440 tmp_str.len = de->name_len;
1441 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1442 hinfo->minor_hash, de, &tmp_str);
1457 * So this function is called when the volume is mkfsed with
1458 * dir_index disabled. In order to keep f_pos persistent
1459 * after we convert from an inlined dir to a blocked based,
1460 * we just pretend that we are a normal dir and return the
1461 * offset as if '.' and '..' really take place.
1464 int ext4_read_inline_dir(struct file *file,
1465 struct dir_context *ctx,
1466 int *has_inline_data)
1468 unsigned int offset, parent_ino;
1470 struct ext4_dir_entry_2 *de;
1471 struct super_block *sb;
1472 struct inode *inode = file_inode(file);
1473 int ret, inline_size = 0;
1474 struct ext4_iloc iloc;
1475 void *dir_buf = NULL;
1476 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1478 ret = ext4_get_inode_loc(inode, &iloc);
1482 down_read(&EXT4_I(inode)->xattr_sem);
1483 if (!ext4_has_inline_data(inode)) {
1484 up_read(&EXT4_I(inode)->xattr_sem);
1485 *has_inline_data = 0;
1489 inline_size = ext4_get_inline_size(inode);
1490 dir_buf = kmalloc(inline_size, GFP_NOFS);
1493 up_read(&EXT4_I(inode)->xattr_sem);
1497 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1498 up_read(&EXT4_I(inode)->xattr_sem);
1504 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1508 * dotdot_offset and dotdot_size is the real offset and
1509 * size for ".." and "." if the dir is block based while
1510 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1511 * So we will use extra_offset and extra_size to indicate them
1512 * during the inline dir iteration.
1514 dotdot_offset = EXT4_DIR_REC_LEN(1);
1515 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1516 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1517 extra_size = extra_offset + inline_size;
1520 * If the version has changed since the last call to
1521 * readdir(2), then we might be pointing to an invalid
1522 * dirent right now. Scan from the start of the inline
1525 if (file->f_version != inode->i_version) {
1526 for (i = 0; i < extra_size && i < offset;) {
1528 * "." is with offset 0 and
1529 * ".." is dotdot_offset.
1534 } else if (i == dotdot_offset) {
1538 /* for other entry, the real offset in
1539 * the buf has to be tuned accordingly.
1541 de = (struct ext4_dir_entry_2 *)
1542 (dir_buf + i - extra_offset);
1543 /* It's too expensive to do a full
1544 * dirent test each time round this
1545 * loop, but we do have to test at
1546 * least that it is non-zero. A
1547 * failure will be detected in the
1548 * dirent test below. */
1549 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1550 < EXT4_DIR_REC_LEN(1))
1552 i += ext4_rec_len_from_disk(de->rec_len,
1557 file->f_version = inode->i_version;
1560 while (ctx->pos < extra_size) {
1561 if (ctx->pos == 0) {
1562 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1564 ctx->pos = dotdot_offset;
1568 if (ctx->pos == dotdot_offset) {
1569 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1571 ctx->pos = dotdot_size;
1575 de = (struct ext4_dir_entry_2 *)
1576 (dir_buf + ctx->pos - extra_offset);
1577 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1578 extra_size, ctx->pos))
1580 if (le32_to_cpu(de->inode)) {
1581 if (!dir_emit(ctx, de->name, de->name_len,
1582 le32_to_cpu(de->inode),
1583 get_dtype(sb, de->file_type)))
1586 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1594 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1595 struct ext4_dir_entry_2 **parent_de,
1598 struct ext4_iloc iloc;
1600 *retval = ext4_get_inode_loc(inode, &iloc);
1604 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1610 * Try to create the inline data for the new dir.
1611 * If it succeeds, return 0, otherwise return the error.
1612 * In case of ENOSPC, the caller should create the normal disk layout dir.
1614 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1615 struct inode *inode)
1617 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1618 struct ext4_iloc iloc;
1619 struct ext4_dir_entry_2 *de;
1621 ret = ext4_get_inode_loc(inode, &iloc);
1625 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1630 * For inline dir, we only save the inode information for the ".."
1631 * and create a fake dentry to cover the left space.
1633 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1634 de->inode = cpu_to_le32(parent->i_ino);
1635 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1637 de->rec_len = ext4_rec_len_to_disk(
1638 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1640 set_nlink(inode, 2);
1641 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1647 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1648 struct ext4_filename *fname,
1649 struct ext4_dir_entry_2 **res_dir,
1650 int *has_inline_data)
1653 struct ext4_iloc iloc;
1657 if (ext4_get_inode_loc(dir, &iloc))
1660 down_read(&EXT4_I(dir)->xattr_sem);
1661 if (!ext4_has_inline_data(dir)) {
1662 *has_inline_data = 0;
1666 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1667 EXT4_INLINE_DOTDOT_SIZE;
1668 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1669 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1670 dir, fname, 0, res_dir);
1676 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1679 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1680 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1682 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1683 dir, fname, 0, res_dir);
1691 up_read(&EXT4_I(dir)->xattr_sem);
1695 int ext4_delete_inline_entry(handle_t *handle,
1697 struct ext4_dir_entry_2 *de_del,
1698 struct buffer_head *bh,
1699 int *has_inline_data)
1701 int err, inline_size, no_expand;
1702 struct ext4_iloc iloc;
1705 err = ext4_get_inode_loc(dir, &iloc);
1709 ext4_write_lock_xattr(dir, &no_expand);
1710 if (!ext4_has_inline_data(dir)) {
1711 *has_inline_data = 0;
1715 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1716 EXT4_MIN_INLINE_DATA_SIZE) {
1717 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1718 EXT4_INLINE_DOTDOT_SIZE;
1719 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1720 EXT4_INLINE_DOTDOT_SIZE;
1722 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1723 inline_size = ext4_get_inline_size(dir) -
1724 EXT4_MIN_INLINE_DATA_SIZE;
1727 BUFFER_TRACE(bh, "get_write_access");
1728 err = ext4_journal_get_write_access(handle, bh);
1732 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1733 inline_start, inline_size, 0);
1737 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1739 ext4_write_unlock_xattr(dir, &no_expand);
1740 if (likely(err == 0))
1741 err = ext4_mark_inode_dirty(handle, dir);
1744 ext4_std_error(dir->i_sb, err);
1749 * Get the inline dentry at offset.
1751 static inline struct ext4_dir_entry_2 *
1752 ext4_get_inline_entry(struct inode *inode,
1753 struct ext4_iloc *iloc,
1754 unsigned int offset,
1755 void **inline_start,
1760 BUG_ON(offset > ext4_get_inline_size(inode));
1762 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1763 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1764 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1766 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1767 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1768 *inline_size = ext4_get_inline_size(inode) -
1769 EXT4_MIN_INLINE_DATA_SIZE;
1773 *inline_start = inline_pos;
1774 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1777 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1779 int err, inline_size;
1780 struct ext4_iloc iloc;
1783 unsigned int offset;
1784 struct ext4_dir_entry_2 *de;
1787 err = ext4_get_inode_loc(dir, &iloc);
1789 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1794 down_read(&EXT4_I(dir)->xattr_sem);
1795 if (!ext4_has_inline_data(dir)) {
1796 *has_inline_data = 0;
1800 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1801 if (!le32_to_cpu(de->inode)) {
1802 ext4_warning(dir->i_sb,
1803 "bad inline directory (dir #%lu) - no `..'",
1809 inline_len = ext4_get_inline_size(dir);
1810 offset = EXT4_INLINE_DOTDOT_SIZE;
1811 while (offset < inline_len) {
1812 de = ext4_get_inline_entry(dir, &iloc, offset,
1813 &inline_pos, &inline_size);
1814 if (ext4_check_dir_entry(dir, NULL, de,
1815 iloc.bh, inline_pos,
1816 inline_size, offset)) {
1817 ext4_warning(dir->i_sb,
1818 "bad inline directory (dir #%lu) - "
1819 "inode %u, rec_len %u, name_len %d"
1821 dir->i_ino, le32_to_cpu(de->inode),
1822 le16_to_cpu(de->rec_len), de->name_len,
1827 if (le32_to_cpu(de->inode)) {
1831 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1835 up_read(&EXT4_I(dir)->xattr_sem);
1840 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1844 ext4_write_lock_xattr(inode, &no_expand);
1845 ret = ext4_destroy_inline_data_nolock(handle, inode);
1846 ext4_write_unlock_xattr(inode, &no_expand);
1851 int ext4_inline_data_fiemap(struct inode *inode,
1852 struct fiemap_extent_info *fieinfo,
1853 int *has_inline, __u64 start, __u64 len)
1857 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1860 struct ext4_iloc iloc;
1862 down_read(&EXT4_I(inode)->xattr_sem);
1863 if (!ext4_has_inline_data(inode)) {
1867 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1868 i_size_read(inode));
1869 if (start >= inline_len)
1871 if (start + len < inline_len)
1872 inline_len = start + len;
1873 inline_len -= start;
1875 error = ext4_get_inode_loc(inode, &iloc);
1879 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1880 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1881 physical += offsetof(struct ext4_inode, i_block);
1885 up_read(&EXT4_I(inode)->xattr_sem);
1887 error = fiemap_fill_next_extent(fieinfo, start, physical,
1889 return (error < 0 ? error : 0);
1892 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1895 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1898 struct ext4_xattr_ibody_find is = {
1899 .s = { .not_found = -ENODATA, },
1901 struct ext4_xattr_info i = {
1902 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1903 .name = EXT4_XATTR_SYSTEM_DATA,
1907 needed_blocks = ext4_writepage_trans_blocks(inode);
1908 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1910 return PTR_ERR(handle);
1912 ext4_write_lock_xattr(inode, &no_expand);
1913 if (!ext4_has_inline_data(inode)) {
1914 ext4_write_unlock_xattr(inode, &no_expand);
1916 ext4_journal_stop(handle);
1920 if ((err = ext4_orphan_add(handle, inode)) != 0)
1923 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1926 down_write(&EXT4_I(inode)->i_data_sem);
1927 i_size = inode->i_size;
1928 inline_size = ext4_get_inline_size(inode);
1929 EXT4_I(inode)->i_disksize = i_size;
1931 if (i_size < inline_size) {
1932 /* Clear the content in the xattr space. */
1933 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1934 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1937 BUG_ON(is.s.not_found);
1939 value_len = le32_to_cpu(is.s.here->e_value_size);
1940 value = kmalloc(value_len, GFP_NOFS);
1946 err = ext4_xattr_ibody_get(inode, i.name_index,
1947 i.name, value, value_len);
1952 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1953 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1954 err = ext4_xattr_ibody_inline_set(handle, inode,
1960 /* Clear the content within i_blocks. */
1961 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1962 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1963 memset(p + i_size, 0,
1964 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1967 EXT4_I(inode)->i_inline_size = i_size <
1968 EXT4_MIN_INLINE_DATA_SIZE ?
1969 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1973 up_write(&EXT4_I(inode)->i_data_sem);
1976 ext4_write_unlock_xattr(inode, &no_expand);
1979 ext4_orphan_del(handle, inode);
1982 inode->i_mtime = inode->i_ctime = current_time(inode);
1983 err = ext4_mark_inode_dirty(handle, inode);
1985 ext4_handle_sync(handle);
1987 ext4_journal_stop(handle);
1991 int ext4_convert_inline_data(struct inode *inode)
1993 int error, needed_blocks, no_expand;
1995 struct ext4_iloc iloc;
1997 if (!ext4_has_inline_data(inode)) {
1998 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2000 } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2002 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2003 * cleared. This means we are in the middle of moving of
2004 * inline data to delay allocated block. Just force writeout
2005 * here to finish conversion.
2007 error = filemap_flush(inode->i_mapping);
2010 if (!ext4_has_inline_data(inode))
2014 needed_blocks = ext4_writepage_trans_blocks(inode);
2017 error = ext4_get_inode_loc(inode, &iloc);
2021 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2022 if (IS_ERR(handle)) {
2023 error = PTR_ERR(handle);
2027 ext4_write_lock_xattr(inode, &no_expand);
2028 if (ext4_has_inline_data(inode))
2029 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2030 ext4_write_unlock_xattr(inode, &no_expand);
2031 ext4_journal_stop(handle);