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"
21 #include <trace/events/android_fs.h>
23 #define EXT4_XATTR_SYSTEM_DATA "data"
24 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
25 #define EXT4_INLINE_DOTDOT_OFFSET 2
26 #define EXT4_INLINE_DOTDOT_SIZE 4
28 static int ext4_get_inline_size(struct inode *inode)
30 if (EXT4_I(inode)->i_inline_off)
31 return EXT4_I(inode)->i_inline_size;
36 static int get_max_inline_xattr_value_size(struct inode *inode,
37 struct ext4_iloc *iloc)
39 struct ext4_xattr_ibody_header *header;
40 struct ext4_xattr_entry *entry;
41 struct ext4_inode *raw_inode;
44 min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
45 EXT4_GOOD_OLD_INODE_SIZE -
46 EXT4_I(inode)->i_extra_isize -
47 sizeof(struct ext4_xattr_ibody_header);
50 * We need to subtract another sizeof(__u32) since an in-inode xattr
51 * needs an empty 4 bytes to indicate the gap between the xattr entry
52 * and the name/value pair.
54 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
55 return EXT4_XATTR_SIZE(min_offs -
56 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
57 EXT4_XATTR_ROUND - sizeof(__u32));
59 raw_inode = ext4_raw_inode(iloc);
60 header = IHDR(inode, raw_inode);
61 entry = IFIRST(header);
63 /* Compute min_offs. */
64 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
65 if (!entry->e_value_block && entry->e_value_size) {
66 size_t offs = le16_to_cpu(entry->e_value_offs);
72 ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
74 if (EXT4_I(inode)->i_inline_off) {
75 entry = (struct ext4_xattr_entry *)
76 ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
78 free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
82 free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
84 if (free > EXT4_XATTR_ROUND)
85 free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
94 * Get the maximum size we now can store in an inode.
95 * If we can't find the space for a xattr entry, don't use the space
96 * of the extents since we have no space to indicate the inline data.
98 int ext4_get_max_inline_size(struct inode *inode)
100 int error, max_inline_size;
101 struct ext4_iloc iloc;
103 if (EXT4_I(inode)->i_extra_isize == 0)
106 error = ext4_get_inode_loc(inode, &iloc);
108 ext4_error_inode(inode, __func__, __LINE__, 0,
109 "can't get inode location %lu",
114 down_read(&EXT4_I(inode)->xattr_sem);
115 max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
116 up_read(&EXT4_I(inode)->xattr_sem);
120 if (!max_inline_size)
123 return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
127 * this function does not take xattr_sem, which is OK because it is
128 * currently only used in a code path coming form ext4_iget, before
129 * the new inode has been unlocked
131 int ext4_find_inline_data_nolock(struct inode *inode)
133 struct ext4_xattr_ibody_find is = {
134 .s = { .not_found = -ENODATA, },
136 struct ext4_xattr_info i = {
137 .name_index = EXT4_XATTR_INDEX_SYSTEM,
138 .name = EXT4_XATTR_SYSTEM_DATA,
142 if (EXT4_I(inode)->i_extra_isize == 0)
145 error = ext4_get_inode_loc(inode, &is.iloc);
149 error = ext4_xattr_ibody_find(inode, &i, &is);
153 if (!is.s.not_found) {
154 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
155 (void *)ext4_raw_inode(&is.iloc));
156 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
157 le32_to_cpu(is.s.here->e_value_size);
158 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
165 static int ext4_read_inline_data(struct inode *inode, void *buffer,
167 struct ext4_iloc *iloc)
169 struct ext4_xattr_entry *entry;
170 struct ext4_xattr_ibody_header *header;
172 struct ext4_inode *raw_inode;
177 BUG_ON(len > EXT4_I(inode)->i_inline_size);
179 cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
180 len : EXT4_MIN_INLINE_DATA_SIZE;
182 raw_inode = ext4_raw_inode(iloc);
183 memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
191 header = IHDR(inode, raw_inode);
192 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
193 EXT4_I(inode)->i_inline_off);
194 len = min_t(unsigned int, len,
195 (unsigned int)le32_to_cpu(entry->e_value_size));
198 (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
206 * write the buffer to the inline inode.
207 * If 'create' is set, we don't need to do the extra copy in the xattr
208 * value since it is already handled by ext4_xattr_ibody_inline_set.
209 * That saves us one memcpy.
211 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
212 void *buffer, loff_t pos, unsigned int len)
214 struct ext4_xattr_entry *entry;
215 struct ext4_xattr_ibody_header *header;
216 struct ext4_inode *raw_inode;
219 BUG_ON(!EXT4_I(inode)->i_inline_off);
220 BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
222 raw_inode = ext4_raw_inode(iloc);
225 if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
226 cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
227 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
228 memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
238 pos -= EXT4_MIN_INLINE_DATA_SIZE;
239 header = IHDR(inode, raw_inode);
240 entry = (struct ext4_xattr_entry *)((void *)raw_inode +
241 EXT4_I(inode)->i_inline_off);
243 memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
247 static int ext4_create_inline_data(handle_t *handle,
248 struct inode *inode, unsigned len)
252 struct ext4_xattr_ibody_find is = {
253 .s = { .not_found = -ENODATA, },
255 struct ext4_xattr_info i = {
256 .name_index = EXT4_XATTR_INDEX_SYSTEM,
257 .name = EXT4_XATTR_SYSTEM_DATA,
260 error = ext4_get_inode_loc(inode, &is.iloc);
264 BUFFER_TRACE(is.iloc.bh, "get_write_access");
265 error = ext4_journal_get_write_access(handle, is.iloc.bh);
269 if (len > EXT4_MIN_INLINE_DATA_SIZE) {
270 value = EXT4_ZERO_XATTR_VALUE;
271 len -= EXT4_MIN_INLINE_DATA_SIZE;
277 /* Insert the the xttr entry. */
281 error = ext4_xattr_ibody_find(inode, &i, &is);
285 BUG_ON(!is.s.not_found);
287 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
289 if (error == -ENOSPC)
290 ext4_clear_inode_state(inode,
291 EXT4_STATE_MAY_INLINE_DATA);
295 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
296 0, EXT4_MIN_INLINE_DATA_SIZE);
298 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
299 (void *)ext4_raw_inode(&is.iloc));
300 EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
301 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
302 ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
304 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
311 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
316 struct ext4_xattr_ibody_find is = {
317 .s = { .not_found = -ENODATA, },
319 struct ext4_xattr_info i = {
320 .name_index = EXT4_XATTR_INDEX_SYSTEM,
321 .name = EXT4_XATTR_SYSTEM_DATA,
324 /* If the old space is ok, write the data directly. */
325 if (len <= EXT4_I(inode)->i_inline_size)
328 error = ext4_get_inode_loc(inode, &is.iloc);
332 error = ext4_xattr_ibody_find(inode, &i, &is);
336 BUG_ON(is.s.not_found);
338 len -= EXT4_MIN_INLINE_DATA_SIZE;
339 value = kzalloc(len, GFP_NOFS);
345 error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
347 if (error == -ENODATA)
350 BUFFER_TRACE(is.iloc.bh, "get_write_access");
351 error = ext4_journal_get_write_access(handle, is.iloc.bh);
355 /* Update the xttr entry. */
359 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
363 EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
364 (void *)ext4_raw_inode(&is.iloc));
365 EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
366 le32_to_cpu(is.s.here->e_value_size);
367 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
369 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
377 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
380 int ret, size, no_expand;
381 struct ext4_inode_info *ei = EXT4_I(inode);
383 if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
386 size = ext4_get_max_inline_size(inode);
390 ext4_write_lock_xattr(inode, &no_expand);
392 if (ei->i_inline_off)
393 ret = ext4_update_inline_data(handle, inode, len);
395 ret = ext4_create_inline_data(handle, inode, len);
397 ext4_write_unlock_xattr(inode, &no_expand);
401 static int ext4_destroy_inline_data_nolock(handle_t *handle,
404 struct ext4_inode_info *ei = EXT4_I(inode);
405 struct ext4_xattr_ibody_find is = {
406 .s = { .not_found = 0, },
408 struct ext4_xattr_info i = {
409 .name_index = EXT4_XATTR_INDEX_SYSTEM,
410 .name = EXT4_XATTR_SYSTEM_DATA,
416 if (!ei->i_inline_off)
419 error = ext4_get_inode_loc(inode, &is.iloc);
423 error = ext4_xattr_ibody_find(inode, &i, &is);
427 BUFFER_TRACE(is.iloc.bh, "get_write_access");
428 error = ext4_journal_get_write_access(handle, is.iloc.bh);
432 error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
436 memset((void *)ext4_raw_inode(&is.iloc)->i_block,
437 0, EXT4_MIN_INLINE_DATA_SIZE);
438 memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
440 if (ext4_has_feature_extents(inode->i_sb)) {
441 if (S_ISDIR(inode->i_mode) ||
442 S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
443 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
444 ext4_ext_tree_init(handle, inode);
447 ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
450 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
452 EXT4_I(inode)->i_inline_off = 0;
453 EXT4_I(inode)->i_inline_size = 0;
454 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
457 if (error == -ENODATA)
462 static int ext4_read_inline_page(struct inode *inode, struct page *page)
467 struct ext4_iloc iloc;
469 BUG_ON(!PageLocked(page));
470 BUG_ON(!ext4_has_inline_data(inode));
473 if (!EXT4_I(inode)->i_inline_off) {
474 ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
479 ret = ext4_get_inode_loc(inode, &iloc);
483 len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
484 kaddr = kmap_atomic(page);
485 ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
486 flush_dcache_page(page);
487 kunmap_atomic(kaddr);
488 zero_user_segment(page, len, PAGE_CACHE_SIZE);
489 SetPageUptodate(page);
496 int ext4_readpage_inline(struct inode *inode, struct page *page)
500 down_read(&EXT4_I(inode)->xattr_sem);
501 if (!ext4_has_inline_data(inode)) {
502 up_read(&EXT4_I(inode)->xattr_sem);
506 if (trace_android_fs_dataread_start_enabled()) {
507 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
509 path = android_fstrace_get_pathname(pathbuf,
510 MAX_TRACE_PATHBUF_LEN,
512 trace_android_fs_dataread_start(inode, page_offset(page),
513 PAGE_SIZE, current->pid,
514 path, current->comm);
518 * Current inline data can only exist in the 1st page,
519 * So for all the other pages, just set them uptodate.
522 ret = ext4_read_inline_page(inode, page);
523 else if (!PageUptodate(page)) {
524 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
525 SetPageUptodate(page);
528 trace_android_fs_dataread_end(inode, page_offset(page), PAGE_SIZE);
530 up_read(&EXT4_I(inode)->xattr_sem);
533 return ret >= 0 ? 0 : ret;
536 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
540 int ret, needed_blocks, no_expand;
541 handle_t *handle = NULL;
542 int retries = 0, sem_held = 0;
543 struct page *page = NULL;
545 struct ext4_iloc iloc;
547 if (!ext4_has_inline_data(inode)) {
549 * clear the flag so that no new write
550 * will trap here again.
552 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
556 needed_blocks = ext4_writepage_trans_blocks(inode);
558 ret = ext4_get_inode_loc(inode, &iloc);
563 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
564 if (IS_ERR(handle)) {
565 ret = PTR_ERR(handle);
570 /* We cannot recurse into the filesystem as the transaction is already
572 flags |= AOP_FLAG_NOFS;
574 page = grab_cache_page_write_begin(mapping, 0, flags);
580 ext4_write_lock_xattr(inode, &no_expand);
582 /* If some one has already done this for us, just exit. */
583 if (!ext4_has_inline_data(inode)) {
589 to = ext4_get_inline_size(inode);
590 if (!PageUptodate(page)) {
591 ret = ext4_read_inline_page(inode, page);
596 ret = ext4_destroy_inline_data_nolock(handle, inode);
600 if (ext4_should_dioread_nolock(inode))
601 ret = __block_write_begin(page, from, to, ext4_get_block_write);
603 ret = __block_write_begin(page, from, to, ext4_get_block);
605 if (!ret && ext4_should_journal_data(inode)) {
606 ret = ext4_walk_page_buffers(handle, page_buffers(page),
608 do_journal_get_write_access);
613 page_cache_release(page);
615 ext4_orphan_add(handle, inode);
616 ext4_write_unlock_xattr(inode, &no_expand);
618 ext4_journal_stop(handle);
620 ext4_truncate_failed_write(inode);
622 * If truncate failed early the inode might
623 * still be on the orphan list; we need to
624 * make sure the inode is removed from the
625 * orphan list in that case.
628 ext4_orphan_del(NULL, inode);
631 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
635 block_commit_write(page, from, to);
639 page_cache_release(page);
642 ext4_write_unlock_xattr(inode, &no_expand);
644 ext4_journal_stop(handle);
650 * Try to write data in the inode.
651 * If the inode has inline data, check whether the new write can be
652 * in the inode also. If not, create the page the handle, move the data
653 * to the page make it update and let the later codes create extent for it.
655 int ext4_try_to_write_inline_data(struct address_space *mapping,
657 loff_t pos, unsigned len,
664 struct ext4_iloc iloc;
666 if (pos + len > ext4_get_max_inline_size(inode))
669 ret = ext4_get_inode_loc(inode, &iloc);
674 * The possible write could happen in the inode,
675 * so try to reserve the space in inode first.
677 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
678 if (IS_ERR(handle)) {
679 ret = PTR_ERR(handle);
684 ret = ext4_prepare_inline_data(handle, inode, pos + len);
685 if (ret && ret != -ENOSPC)
688 /* We don't have space in inline inode, so convert it to extent. */
689 if (ret == -ENOSPC) {
690 ext4_journal_stop(handle);
695 ret = ext4_journal_get_write_access(handle, iloc.bh);
699 flags |= AOP_FLAG_NOFS;
701 page = grab_cache_page_write_begin(mapping, 0, flags);
708 down_read(&EXT4_I(inode)->xattr_sem);
709 if (!ext4_has_inline_data(inode)) {
712 page_cache_release(page);
716 if (!PageUptodate(page)) {
717 ret = ext4_read_inline_page(inode, page);
728 up_read(&EXT4_I(inode)->xattr_sem);
730 if (handle && (ret != 1))
731 ext4_journal_stop(handle);
735 return ext4_convert_inline_data_to_extent(mapping,
739 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
740 unsigned copied, struct page *page)
744 struct ext4_iloc iloc;
746 if (unlikely(copied < len)) {
747 if (!PageUptodate(page)) {
753 ret = ext4_get_inode_loc(inode, &iloc);
755 ext4_std_error(inode->i_sb, ret);
760 ext4_write_lock_xattr(inode, &no_expand);
761 BUG_ON(!ext4_has_inline_data(inode));
763 kaddr = kmap_atomic(page);
764 ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
765 kunmap_atomic(kaddr);
766 SetPageUptodate(page);
767 /* clear page dirty so that writepages wouldn't work for us. */
768 ClearPageDirty(page);
770 ext4_write_unlock_xattr(inode, &no_expand);
772 mark_inode_dirty(inode);
778 ext4_journalled_write_inline_data(struct inode *inode,
784 struct ext4_iloc iloc;
786 ret = ext4_get_inode_loc(inode, &iloc);
788 ext4_std_error(inode->i_sb, ret);
792 ext4_write_lock_xattr(inode, &no_expand);
793 kaddr = kmap_atomic(page);
794 ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
795 kunmap_atomic(kaddr);
796 ext4_write_unlock_xattr(inode, &no_expand);
802 * Try to make the page cache and handle ready for the inline data case.
803 * We can call this function in 2 cases:
804 * 1. The inode is created and the first write exceeds inline size. We can
805 * clear the inode state safely.
806 * 2. The inode has inline data, then we need to read the data, make it
807 * update and dirty so that ext4_da_writepages can handle it. We don't
808 * need to start the journal since the file's metatdata isn't changed now.
810 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
815 int ret = 0, inline_size;
818 page = grab_cache_page_write_begin(mapping, 0, flags);
822 down_read(&EXT4_I(inode)->xattr_sem);
823 if (!ext4_has_inline_data(inode)) {
824 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
828 inline_size = ext4_get_inline_size(inode);
830 if (!PageUptodate(page)) {
831 ret = ext4_read_inline_page(inode, page);
836 ret = __block_write_begin(page, 0, inline_size,
837 ext4_da_get_block_prep);
839 up_read(&EXT4_I(inode)->xattr_sem);
841 page_cache_release(page);
842 ext4_truncate_failed_write(inode);
847 SetPageUptodate(page);
848 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
849 *fsdata = (void *)CONVERT_INLINE_DATA;
852 up_read(&EXT4_I(inode)->xattr_sem);
855 page_cache_release(page);
861 * Prepare the write for the inline data.
862 * If the the data can be written into the inode, we just read
863 * the page and make it uptodate, and start the journal.
864 * Otherwise read the page, makes it dirty so that it can be
865 * handle in writepages(the i_disksize update is left to the
866 * normal ext4_da_write_end).
868 int ext4_da_write_inline_data_begin(struct address_space *mapping,
870 loff_t pos, unsigned len,
875 int ret, inline_size;
878 struct ext4_iloc iloc;
881 ret = ext4_get_inode_loc(inode, &iloc);
886 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
887 if (IS_ERR(handle)) {
888 ret = PTR_ERR(handle);
892 inline_size = ext4_get_max_inline_size(inode);
895 if (inline_size >= pos + len) {
896 ret = ext4_prepare_inline_data(handle, inode, pos + len);
897 if (ret && ret != -ENOSPC)
902 * We cannot recurse into the filesystem as the transaction
903 * is already started.
905 flags |= AOP_FLAG_NOFS;
907 if (ret == -ENOSPC) {
908 ext4_journal_stop(handle);
909 ret = ext4_da_convert_inline_data_to_extent(mapping,
913 if (ret == -ENOSPC &&
914 ext4_should_retry_alloc(inode->i_sb, &retries))
919 page = grab_cache_page_write_begin(mapping, 0, flags);
925 down_read(&EXT4_I(inode)->xattr_sem);
926 if (!ext4_has_inline_data(inode)) {
928 goto out_release_page;
931 if (!PageUptodate(page)) {
932 ret = ext4_read_inline_page(inode, page);
934 goto out_release_page;
936 ret = ext4_journal_get_write_access(handle, iloc.bh);
938 goto out_release_page;
940 up_read(&EXT4_I(inode)->xattr_sem);
945 up_read(&EXT4_I(inode)->xattr_sem);
947 page_cache_release(page);
949 ext4_journal_stop(handle);
955 int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
956 unsigned len, unsigned copied,
961 ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
970 * No need to use i_size_read() here, the i_size
971 * cannot change under us because we hold i_mutex.
973 * But it's important to update i_size while still holding page lock:
974 * page writeout could otherwise come in and zero beyond i_size.
976 if (pos+copied > inode->i_size)
977 i_size_write(inode, pos+copied);
979 page_cache_release(page);
982 * Don't mark the inode dirty under page lock. First, it unnecessarily
983 * makes the holding time of page lock longer. Second, it forces lock
984 * ordering of page lock and transaction start for journaling
987 mark_inode_dirty(inode);
992 #ifdef INLINE_DIR_DEBUG
993 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
994 void *inline_start, int inline_size)
997 unsigned short de_len;
998 struct ext4_dir_entry_2 *de = inline_start;
999 void *dlimit = inline_start + inline_size;
1001 trace_printk("inode %lu\n", dir->i_ino);
1003 while ((void *)de < dlimit) {
1004 de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
1005 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
1006 offset, de_len, de->name_len, de->name,
1007 de->name_len, le32_to_cpu(de->inode));
1008 if (ext4_check_dir_entry(dir, NULL, de, bh,
1009 inline_start, inline_size, offset))
1013 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1017 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1021 * Add a new entry into a inline dir.
1022 * It will return -ENOSPC if no space is available, and -EIO
1023 * and -EEXIST if directory entry already exists.
1025 static int ext4_add_dirent_to_inline(handle_t *handle,
1026 struct ext4_filename *fname,
1027 struct dentry *dentry,
1028 struct inode *inode,
1029 struct ext4_iloc *iloc,
1030 void *inline_start, int inline_size)
1032 struct inode *dir = d_inode(dentry->d_parent);
1034 struct ext4_dir_entry_2 *de;
1036 err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
1037 inline_size, fname, &de);
1041 BUFFER_TRACE(iloc->bh, "get_write_access");
1042 err = ext4_journal_get_write_access(handle, iloc->bh);
1045 ext4_insert_dentry(dir, inode, de, inline_size, fname);
1047 ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1050 * XXX shouldn't update any times until successful
1051 * completion of syscall, but too many callers depend
1054 * XXX similarly, too many callers depend on
1055 * ext4_new_inode() setting the times, but error
1056 * recovery deletes the inode, so the worst that can
1057 * happen is that the times are slightly out of date
1058 * and/or different from the directory change time.
1060 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1061 ext4_update_dx_flag(dir);
1063 ext4_mark_inode_dirty(handle, dir);
1067 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1068 struct ext4_iloc *iloc)
1070 struct ext4_xattr_entry *entry;
1071 struct ext4_xattr_ibody_header *header;
1073 BUG_ON(!EXT4_I(inode)->i_inline_off);
1075 header = IHDR(inode, ext4_raw_inode(iloc));
1076 entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1077 EXT4_I(inode)->i_inline_off);
1079 return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1082 /* Set the final de to cover the whole block. */
1083 static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1085 struct ext4_dir_entry_2 *de, *prev_de;
1089 de = (struct ext4_dir_entry_2 *)de_buf;
1091 limit = de_buf + old_size;
1094 de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1096 de = (struct ext4_dir_entry_2 *)de_buf;
1097 } while (de_buf < limit);
1099 prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1100 old_size, new_size);
1102 /* this is just created, so create an empty entry. */
1104 de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1108 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1109 struct ext4_iloc *iloc)
1112 int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1113 int new_size = get_max_inline_xattr_value_size(dir, iloc);
1115 if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
1118 ret = ext4_update_inline_data(handle, dir,
1119 new_size + EXT4_MIN_INLINE_DATA_SIZE);
1123 ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1124 EXT4_I(dir)->i_inline_size -
1125 EXT4_MIN_INLINE_DATA_SIZE);
1126 dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1130 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1131 struct ext4_iloc *iloc,
1132 void *buf, int inline_size)
1134 ext4_create_inline_data(handle, inode, inline_size);
1135 ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1136 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1139 static int ext4_finish_convert_inline_dir(handle_t *handle,
1140 struct inode *inode,
1141 struct buffer_head *dir_block,
1145 int err, csum_size = 0, header_size = 0;
1146 struct ext4_dir_entry_2 *de;
1147 struct ext4_dir_entry_tail *t;
1148 void *target = dir_block->b_data;
1151 * First create "." and ".." and then copy the dir information
1152 * back to the block.
1154 de = (struct ext4_dir_entry_2 *)target;
1155 de = ext4_init_dot_dotdot(inode, de,
1156 inode->i_sb->s_blocksize, csum_size,
1157 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1158 header_size = (void *)de - target;
1160 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1161 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1163 if (ext4_has_metadata_csum(inode->i_sb))
1164 csum_size = sizeof(struct ext4_dir_entry_tail);
1166 inode->i_size = inode->i_sb->s_blocksize;
1167 i_size_write(inode, inode->i_sb->s_blocksize);
1168 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1169 ext4_update_final_de(dir_block->b_data,
1170 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1171 inode->i_sb->s_blocksize - csum_size);
1174 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1175 inode->i_sb->s_blocksize);
1176 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1178 set_buffer_uptodate(dir_block);
1179 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1182 set_buffer_verified(dir_block);
1183 return ext4_mark_inode_dirty(handle, inode);
1186 static int ext4_convert_inline_data_nolock(handle_t *handle,
1187 struct inode *inode,
1188 struct ext4_iloc *iloc)
1192 struct buffer_head *data_bh = NULL;
1193 struct ext4_map_blocks map;
1196 inline_size = ext4_get_inline_size(inode);
1197 buf = kmalloc(inline_size, GFP_NOFS);
1203 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1208 * Make sure the inline directory entries pass checks before we try to
1209 * convert them, so that we avoid touching stuff that needs fsck.
1211 if (S_ISDIR(inode->i_mode)) {
1212 error = ext4_check_all_de(inode, iloc->bh,
1213 buf + EXT4_INLINE_DOTDOT_SIZE,
1214 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1219 error = ext4_destroy_inline_data_nolock(handle, inode);
1226 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1229 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1234 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1240 lock_buffer(data_bh);
1241 error = ext4_journal_get_create_access(handle, data_bh);
1243 unlock_buffer(data_bh);
1247 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1249 if (!S_ISDIR(inode->i_mode)) {
1250 memcpy(data_bh->b_data, buf, inline_size);
1251 set_buffer_uptodate(data_bh);
1252 error = ext4_handle_dirty_metadata(handle,
1255 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1259 unlock_buffer(data_bh);
1262 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1271 * Try to add the new entry to the inline data.
1272 * If succeeds, return 0. If not, extended the inline dir and copied data to
1273 * the new created block.
1275 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1276 struct dentry *dentry, struct inode *inode)
1278 int ret, inline_size, no_expand;
1280 struct ext4_iloc iloc;
1281 struct inode *dir = d_inode(dentry->d_parent);
1283 ret = ext4_get_inode_loc(dir, &iloc);
1287 ext4_write_lock_xattr(dir, &no_expand);
1288 if (!ext4_has_inline_data(dir))
1291 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1292 EXT4_INLINE_DOTDOT_SIZE;
1293 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1295 ret = ext4_add_dirent_to_inline(handle, fname, dentry, inode, &iloc,
1296 inline_start, inline_size);
1300 /* check whether it can be inserted to inline xattr space. */
1301 inline_size = EXT4_I(dir)->i_inline_size -
1302 EXT4_MIN_INLINE_DATA_SIZE;
1304 /* Try to use the xattr space.*/
1305 ret = ext4_update_inline_dir(handle, dir, &iloc);
1306 if (ret && ret != -ENOSPC)
1309 inline_size = EXT4_I(dir)->i_inline_size -
1310 EXT4_MIN_INLINE_DATA_SIZE;
1314 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1316 ret = ext4_add_dirent_to_inline(handle, fname, dentry,
1317 inode, &iloc, inline_start,
1325 * The inline space is filled up, so create a new block for it.
1326 * As the extent tree will be created, we have to save the inline
1329 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1332 ext4_mark_inode_dirty(handle, dir);
1333 ext4_write_unlock_xattr(dir, &no_expand);
1339 * This function fills a red-black tree with information from an
1340 * inlined dir. It returns the number directory entries loaded
1341 * into the tree. If there is an error it is returned in err.
1343 int htree_inlinedir_to_tree(struct file *dir_file,
1344 struct inode *dir, ext4_lblk_t block,
1345 struct dx_hash_info *hinfo,
1346 __u32 start_hash, __u32 start_minor_hash,
1347 int *has_inline_data)
1349 int err = 0, count = 0;
1350 unsigned int parent_ino;
1352 struct ext4_dir_entry_2 *de;
1353 struct inode *inode = file_inode(dir_file);
1354 int ret, inline_size = 0;
1355 struct ext4_iloc iloc;
1356 void *dir_buf = NULL;
1357 struct ext4_dir_entry_2 fake;
1358 struct ext4_str tmp_str;
1360 ret = ext4_get_inode_loc(inode, &iloc);
1364 down_read(&EXT4_I(inode)->xattr_sem);
1365 if (!ext4_has_inline_data(inode)) {
1366 up_read(&EXT4_I(inode)->xattr_sem);
1367 *has_inline_data = 0;
1371 inline_size = ext4_get_inline_size(inode);
1372 dir_buf = kmalloc(inline_size, GFP_NOFS);
1375 up_read(&EXT4_I(inode)->xattr_sem);
1379 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1380 up_read(&EXT4_I(inode)->xattr_sem);
1385 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1386 while (pos < inline_size) {
1388 * As inlined dir doesn't store any information about '.' and
1389 * only the inode number of '..' is stored, we have to handle
1393 fake.inode = cpu_to_le32(inode->i_ino);
1395 strcpy(fake.name, ".");
1396 fake.rec_len = ext4_rec_len_to_disk(
1397 EXT4_DIR_REC_LEN(fake.name_len),
1399 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1401 pos = EXT4_INLINE_DOTDOT_OFFSET;
1402 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1403 fake.inode = cpu_to_le32(parent_ino);
1405 strcpy(fake.name, "..");
1406 fake.rec_len = ext4_rec_len_to_disk(
1407 EXT4_DIR_REC_LEN(fake.name_len),
1409 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1411 pos = EXT4_INLINE_DOTDOT_SIZE;
1413 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1414 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1415 if (ext4_check_dir_entry(inode, dir_file, de,
1417 inline_size, pos)) {
1423 ext4fs_dirhash(de->name, de->name_len, hinfo);
1424 if ((hinfo->hash < start_hash) ||
1425 ((hinfo->hash == start_hash) &&
1426 (hinfo->minor_hash < start_minor_hash)))
1430 tmp_str.name = de->name;
1431 tmp_str.len = de->name_len;
1432 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1433 hinfo->minor_hash, de, &tmp_str);
1448 * So this function is called when the volume is mkfsed with
1449 * dir_index disabled. In order to keep f_pos persistent
1450 * after we convert from an inlined dir to a blocked based,
1451 * we just pretend that we are a normal dir and return the
1452 * offset as if '.' and '..' really take place.
1455 int ext4_read_inline_dir(struct file *file,
1456 struct dir_context *ctx,
1457 int *has_inline_data)
1459 unsigned int offset, parent_ino;
1461 struct ext4_dir_entry_2 *de;
1462 struct super_block *sb;
1463 struct inode *inode = file_inode(file);
1464 int ret, inline_size = 0;
1465 struct ext4_iloc iloc;
1466 void *dir_buf = NULL;
1467 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1469 ret = ext4_get_inode_loc(inode, &iloc);
1473 down_read(&EXT4_I(inode)->xattr_sem);
1474 if (!ext4_has_inline_data(inode)) {
1475 up_read(&EXT4_I(inode)->xattr_sem);
1476 *has_inline_data = 0;
1480 inline_size = ext4_get_inline_size(inode);
1481 dir_buf = kmalloc(inline_size, GFP_NOFS);
1484 up_read(&EXT4_I(inode)->xattr_sem);
1488 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1489 up_read(&EXT4_I(inode)->xattr_sem);
1495 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1499 * dotdot_offset and dotdot_size is the real offset and
1500 * size for ".." and "." if the dir is block based while
1501 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1502 * So we will use extra_offset and extra_size to indicate them
1503 * during the inline dir iteration.
1505 dotdot_offset = EXT4_DIR_REC_LEN(1);
1506 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1507 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1508 extra_size = extra_offset + inline_size;
1511 * If the version has changed since the last call to
1512 * readdir(2), then we might be pointing to an invalid
1513 * dirent right now. Scan from the start of the inline
1516 if (file->f_version != inode->i_version) {
1517 for (i = 0; i < extra_size && i < offset;) {
1519 * "." is with offset 0 and
1520 * ".." is dotdot_offset.
1525 } else if (i == dotdot_offset) {
1529 /* for other entry, the real offset in
1530 * the buf has to be tuned accordingly.
1532 de = (struct ext4_dir_entry_2 *)
1533 (dir_buf + i - extra_offset);
1534 /* It's too expensive to do a full
1535 * dirent test each time round this
1536 * loop, but we do have to test at
1537 * least that it is non-zero. A
1538 * failure will be detected in the
1539 * dirent test below. */
1540 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1541 < EXT4_DIR_REC_LEN(1))
1543 i += ext4_rec_len_from_disk(de->rec_len,
1548 file->f_version = inode->i_version;
1551 while (ctx->pos < extra_size) {
1552 if (ctx->pos == 0) {
1553 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1555 ctx->pos = dotdot_offset;
1559 if (ctx->pos == dotdot_offset) {
1560 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1562 ctx->pos = dotdot_size;
1566 de = (struct ext4_dir_entry_2 *)
1567 (dir_buf + ctx->pos - extra_offset);
1568 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1569 extra_size, ctx->pos))
1571 if (le32_to_cpu(de->inode)) {
1572 if (!dir_emit(ctx, de->name, de->name_len,
1573 le32_to_cpu(de->inode),
1574 get_dtype(sb, de->file_type)))
1577 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1585 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1586 struct ext4_dir_entry_2 **parent_de,
1589 struct ext4_iloc iloc;
1591 *retval = ext4_get_inode_loc(inode, &iloc);
1595 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1601 * Try to create the inline data for the new dir.
1602 * If it succeeds, return 0, otherwise return the error.
1603 * In case of ENOSPC, the caller should create the normal disk layout dir.
1605 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1606 struct inode *inode)
1608 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1609 struct ext4_iloc iloc;
1610 struct ext4_dir_entry_2 *de;
1612 ret = ext4_get_inode_loc(inode, &iloc);
1616 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1621 * For inline dir, we only save the inode information for the ".."
1622 * and create a fake dentry to cover the left space.
1624 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1625 de->inode = cpu_to_le32(parent->i_ino);
1626 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1628 de->rec_len = ext4_rec_len_to_disk(
1629 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1631 set_nlink(inode, 2);
1632 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1638 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1639 struct ext4_filename *fname,
1640 const struct qstr *d_name,
1641 struct ext4_dir_entry_2 **res_dir,
1642 int *has_inline_data)
1645 struct ext4_iloc iloc;
1649 if (ext4_get_inode_loc(dir, &iloc))
1652 down_read(&EXT4_I(dir)->xattr_sem);
1653 if (!ext4_has_inline_data(dir)) {
1654 *has_inline_data = 0;
1658 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1659 EXT4_INLINE_DOTDOT_SIZE;
1660 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1661 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1662 dir, fname, d_name, 0, res_dir);
1668 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1671 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1672 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1674 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1675 dir, fname, d_name, 0, res_dir);
1683 up_read(&EXT4_I(dir)->xattr_sem);
1687 int ext4_delete_inline_entry(handle_t *handle,
1689 struct ext4_dir_entry_2 *de_del,
1690 struct buffer_head *bh,
1691 int *has_inline_data)
1693 int err, inline_size, no_expand;
1694 struct ext4_iloc iloc;
1697 err = ext4_get_inode_loc(dir, &iloc);
1701 ext4_write_lock_xattr(dir, &no_expand);
1702 if (!ext4_has_inline_data(dir)) {
1703 *has_inline_data = 0;
1707 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1708 EXT4_MIN_INLINE_DATA_SIZE) {
1709 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1710 EXT4_INLINE_DOTDOT_SIZE;
1711 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1712 EXT4_INLINE_DOTDOT_SIZE;
1714 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1715 inline_size = ext4_get_inline_size(dir) -
1716 EXT4_MIN_INLINE_DATA_SIZE;
1719 BUFFER_TRACE(bh, "get_write_access");
1720 err = ext4_journal_get_write_access(handle, bh);
1724 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1725 inline_start, inline_size, 0);
1729 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1730 err = ext4_mark_inode_dirty(handle, dir);
1734 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1736 ext4_write_unlock_xattr(dir, &no_expand);
1739 ext4_std_error(dir->i_sb, err);
1744 * Get the inline dentry at offset.
1746 static inline struct ext4_dir_entry_2 *
1747 ext4_get_inline_entry(struct inode *inode,
1748 struct ext4_iloc *iloc,
1749 unsigned int offset,
1750 void **inline_start,
1755 BUG_ON(offset > ext4_get_inline_size(inode));
1757 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1758 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1759 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1761 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1762 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1763 *inline_size = ext4_get_inline_size(inode) -
1764 EXT4_MIN_INLINE_DATA_SIZE;
1768 *inline_start = inline_pos;
1769 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1772 int empty_inline_dir(struct inode *dir, int *has_inline_data)
1774 int err, inline_size;
1775 struct ext4_iloc iloc;
1778 unsigned int offset;
1779 struct ext4_dir_entry_2 *de;
1782 err = ext4_get_inode_loc(dir, &iloc);
1784 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1789 down_read(&EXT4_I(dir)->xattr_sem);
1790 if (!ext4_has_inline_data(dir)) {
1791 *has_inline_data = 0;
1795 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1796 if (!le32_to_cpu(de->inode)) {
1797 ext4_warning(dir->i_sb,
1798 "bad inline directory (dir #%lu) - no `..'",
1804 inline_len = ext4_get_inline_size(dir);
1805 offset = EXT4_INLINE_DOTDOT_SIZE;
1806 while (offset < inline_len) {
1807 de = ext4_get_inline_entry(dir, &iloc, offset,
1808 &inline_pos, &inline_size);
1809 if (ext4_check_dir_entry(dir, NULL, de,
1810 iloc.bh, inline_pos,
1811 inline_size, offset)) {
1812 ext4_warning(dir->i_sb,
1813 "bad inline directory (dir #%lu) - "
1814 "inode %u, rec_len %u, name_len %d"
1816 dir->i_ino, le32_to_cpu(de->inode),
1817 le16_to_cpu(de->rec_len), de->name_len,
1822 if (le32_to_cpu(de->inode)) {
1826 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1830 up_read(&EXT4_I(dir)->xattr_sem);
1835 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1839 ext4_write_lock_xattr(inode, &no_expand);
1840 ret = ext4_destroy_inline_data_nolock(handle, inode);
1841 ext4_write_unlock_xattr(inode, &no_expand);
1846 int ext4_inline_data_fiemap(struct inode *inode,
1847 struct fiemap_extent_info *fieinfo,
1848 int *has_inline, __u64 start, __u64 len)
1852 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1855 struct ext4_iloc iloc;
1857 down_read(&EXT4_I(inode)->xattr_sem);
1858 if (!ext4_has_inline_data(inode)) {
1862 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1863 i_size_read(inode));
1864 if (start >= inline_len)
1866 if (start + len < inline_len)
1867 inline_len = start + len;
1868 inline_len -= start;
1870 error = ext4_get_inode_loc(inode, &iloc);
1874 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1875 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1876 physical += offsetof(struct ext4_inode, i_block);
1880 up_read(&EXT4_I(inode)->xattr_sem);
1882 error = fiemap_fill_next_extent(fieinfo, start, physical,
1884 return (error < 0 ? error : 0);
1887 void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1890 int inline_size, value_len, needed_blocks, no_expand;
1893 struct ext4_xattr_ibody_find is = {
1894 .s = { .not_found = -ENODATA, },
1896 struct ext4_xattr_info i = {
1897 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1898 .name = EXT4_XATTR_SYSTEM_DATA,
1902 needed_blocks = ext4_writepage_trans_blocks(inode);
1903 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1907 ext4_write_lock_xattr(inode, &no_expand);
1908 if (!ext4_has_inline_data(inode)) {
1910 ext4_journal_stop(handle);
1914 if (ext4_orphan_add(handle, inode))
1917 if (ext4_get_inode_loc(inode, &is.iloc))
1920 down_write(&EXT4_I(inode)->i_data_sem);
1921 i_size = inode->i_size;
1922 inline_size = ext4_get_inline_size(inode);
1923 EXT4_I(inode)->i_disksize = i_size;
1925 if (i_size < inline_size) {
1926 /* Clear the content in the xattr space. */
1927 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1928 if (ext4_xattr_ibody_find(inode, &i, &is))
1931 BUG_ON(is.s.not_found);
1933 value_len = le32_to_cpu(is.s.here->e_value_size);
1934 value = kmalloc(value_len, GFP_NOFS);
1938 if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
1943 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1944 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1945 if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
1949 /* Clear the content within i_blocks. */
1950 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1951 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1952 memset(p + i_size, 0,
1953 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1956 EXT4_I(inode)->i_inline_size = i_size <
1957 EXT4_MIN_INLINE_DATA_SIZE ?
1958 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1962 up_write(&EXT4_I(inode)->i_data_sem);
1965 ext4_write_unlock_xattr(inode, &no_expand);
1968 ext4_orphan_del(handle, inode);
1970 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
1971 ext4_mark_inode_dirty(handle, inode);
1973 ext4_handle_sync(handle);
1975 ext4_journal_stop(handle);
1979 int ext4_convert_inline_data(struct inode *inode)
1981 int error, needed_blocks, no_expand;
1983 struct ext4_iloc iloc;
1985 if (!ext4_has_inline_data(inode)) {
1986 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1990 needed_blocks = ext4_writepage_trans_blocks(inode);
1993 error = ext4_get_inode_loc(inode, &iloc);
1997 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
1998 if (IS_ERR(handle)) {
1999 error = PTR_ERR(handle);
2003 ext4_write_lock_xattr(inode, &no_expand);
2004 if (ext4_has_inline_data(inode))
2005 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2006 ext4_write_unlock_xattr(inode, &no_expand);
2007 ext4_journal_stop(handle);