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/iomap.h>
16 #include <linux/fiemap.h>
18 #include "ext4_jbd2.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_inum
&& 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 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
222 BUG_ON(!EXT4_I(inode
)->i_inline_off
);
223 BUG_ON(pos
+ len
> EXT4_I(inode
)->i_inline_size
);
225 raw_inode
= ext4_raw_inode(iloc
);
228 if (pos
< EXT4_MIN_INLINE_DATA_SIZE
) {
229 cp_len
= pos
+ len
> EXT4_MIN_INLINE_DATA_SIZE
?
230 EXT4_MIN_INLINE_DATA_SIZE
- pos
: len
;
231 memcpy((void *)raw_inode
->i_block
+ pos
, buffer
, cp_len
);
241 pos
-= EXT4_MIN_INLINE_DATA_SIZE
;
242 header
= IHDR(inode
, raw_inode
);
243 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
244 EXT4_I(inode
)->i_inline_off
);
246 memcpy((void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
) + pos
,
250 static int ext4_create_inline_data(handle_t
*handle
,
251 struct inode
*inode
, unsigned len
)
255 struct ext4_xattr_ibody_find is
= {
256 .s
= { .not_found
= -ENODATA
, },
258 struct ext4_xattr_info i
= {
259 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
260 .name
= EXT4_XATTR_SYSTEM_DATA
,
263 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
267 BUFFER_TRACE(is
.iloc
.bh
, "get_write_access");
268 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
272 if (len
> EXT4_MIN_INLINE_DATA_SIZE
) {
273 value
= EXT4_ZERO_XATTR_VALUE
;
274 len
-= EXT4_MIN_INLINE_DATA_SIZE
;
280 /* Insert the the xttr entry. */
284 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
288 BUG_ON(!is
.s
.not_found
);
290 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
292 if (error
== -ENOSPC
)
293 ext4_clear_inode_state(inode
,
294 EXT4_STATE_MAY_INLINE_DATA
);
298 memset((void *)ext4_raw_inode(&is
.iloc
)->i_block
,
299 0, EXT4_MIN_INLINE_DATA_SIZE
);
301 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
302 (void *)ext4_raw_inode(&is
.iloc
));
303 EXT4_I(inode
)->i_inline_size
= len
+ EXT4_MIN_INLINE_DATA_SIZE
;
304 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
305 ext4_set_inode_flag(inode
, EXT4_INODE_INLINE_DATA
);
307 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
314 static int ext4_update_inline_data(handle_t
*handle
, struct inode
*inode
,
319 struct ext4_xattr_ibody_find is
= {
320 .s
= { .not_found
= -ENODATA
, },
322 struct ext4_xattr_info i
= {
323 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
324 .name
= EXT4_XATTR_SYSTEM_DATA
,
327 /* If the old space is ok, write the data directly. */
328 if (len
<= EXT4_I(inode
)->i_inline_size
)
331 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
335 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
339 BUG_ON(is
.s
.not_found
);
341 len
-= EXT4_MIN_INLINE_DATA_SIZE
;
342 value
= kzalloc(len
, GFP_NOFS
);
348 error
= ext4_xattr_ibody_get(inode
, i
.name_index
, i
.name
,
350 if (error
== -ENODATA
)
353 BUFFER_TRACE(is
.iloc
.bh
, "get_write_access");
354 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
358 /* Update the xttr entry. */
362 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
366 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
367 (void *)ext4_raw_inode(&is
.iloc
));
368 EXT4_I(inode
)->i_inline_size
= EXT4_MIN_INLINE_DATA_SIZE
+
369 le32_to_cpu(is
.s
.here
->e_value_size
);
370 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
372 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
380 static int ext4_prepare_inline_data(handle_t
*handle
, struct inode
*inode
,
383 int ret
, size
, no_expand
;
384 struct ext4_inode_info
*ei
= EXT4_I(inode
);
386 if (!ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
))
389 size
= ext4_get_max_inline_size(inode
);
393 ext4_write_lock_xattr(inode
, &no_expand
);
395 if (ei
->i_inline_off
)
396 ret
= ext4_update_inline_data(handle
, inode
, len
);
398 ret
= ext4_create_inline_data(handle
, inode
, len
);
400 ext4_write_unlock_xattr(inode
, &no_expand
);
404 static int ext4_destroy_inline_data_nolock(handle_t
*handle
,
407 struct ext4_inode_info
*ei
= EXT4_I(inode
);
408 struct ext4_xattr_ibody_find is
= {
409 .s
= { .not_found
= 0, },
411 struct ext4_xattr_info i
= {
412 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
413 .name
= EXT4_XATTR_SYSTEM_DATA
,
419 if (!ei
->i_inline_off
)
422 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
426 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
430 BUFFER_TRACE(is
.iloc
.bh
, "get_write_access");
431 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
435 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
439 memset((void *)ext4_raw_inode(&is
.iloc
)->i_block
,
440 0, EXT4_MIN_INLINE_DATA_SIZE
);
442 if (ext4_has_feature_extents(inode
->i_sb
)) {
443 if (S_ISDIR(inode
->i_mode
) ||
444 S_ISREG(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) {
445 ext4_set_inode_flag(inode
, EXT4_INODE_EXTENTS
);
446 ext4_ext_tree_init(handle
, inode
);
449 ext4_clear_inode_flag(inode
, EXT4_INODE_INLINE_DATA
);
452 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
454 EXT4_I(inode
)->i_inline_off
= 0;
455 EXT4_I(inode
)->i_inline_size
= 0;
456 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
459 if (error
== -ENODATA
)
464 static int ext4_read_inline_page(struct inode
*inode
, struct page
*page
)
469 struct ext4_iloc iloc
;
471 BUG_ON(!PageLocked(page
));
472 BUG_ON(!ext4_has_inline_data(inode
));
475 if (!EXT4_I(inode
)->i_inline_off
) {
476 ext4_warning(inode
->i_sb
, "inode %lu doesn't have inline data.",
481 ret
= ext4_get_inode_loc(inode
, &iloc
);
485 len
= min_t(size_t, ext4_get_inline_size(inode
), i_size_read(inode
));
486 kaddr
= kmap_atomic(page
);
487 ret
= ext4_read_inline_data(inode
, kaddr
, len
, &iloc
);
488 flush_dcache_page(page
);
489 kunmap_atomic(kaddr
);
490 zero_user_segment(page
, len
, PAGE_SIZE
);
491 SetPageUptodate(page
);
498 int ext4_readpage_inline(struct inode
*inode
, struct page
*page
)
502 down_read(&EXT4_I(inode
)->xattr_sem
);
503 if (!ext4_has_inline_data(inode
)) {
504 up_read(&EXT4_I(inode
)->xattr_sem
);
509 * Current inline data can only exist in the 1st page,
510 * So for all the other pages, just set them uptodate.
513 ret
= ext4_read_inline_page(inode
, page
);
514 else if (!PageUptodate(page
)) {
515 zero_user_segment(page
, 0, PAGE_SIZE
);
516 SetPageUptodate(page
);
519 up_read(&EXT4_I(inode
)->xattr_sem
);
522 return ret
>= 0 ? 0 : ret
;
525 static int ext4_convert_inline_data_to_extent(struct address_space
*mapping
,
529 int ret
, needed_blocks
, no_expand
;
530 handle_t
*handle
= NULL
;
531 int retries
= 0, sem_held
= 0;
532 struct page
*page
= NULL
;
534 struct ext4_iloc iloc
;
536 if (!ext4_has_inline_data(inode
)) {
538 * clear the flag so that no new write
539 * will trap here again.
541 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
545 needed_blocks
= ext4_writepage_trans_blocks(inode
);
547 ret
= ext4_get_inode_loc(inode
, &iloc
);
552 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
553 if (IS_ERR(handle
)) {
554 ret
= PTR_ERR(handle
);
559 /* We cannot recurse into the filesystem as the transaction is already
561 flags
|= AOP_FLAG_NOFS
;
563 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
569 ext4_write_lock_xattr(inode
, &no_expand
);
571 /* If some one has already done this for us, just exit. */
572 if (!ext4_has_inline_data(inode
)) {
578 to
= ext4_get_inline_size(inode
);
579 if (!PageUptodate(page
)) {
580 ret
= ext4_read_inline_page(inode
, page
);
585 ret
= ext4_destroy_inline_data_nolock(handle
, inode
);
589 if (ext4_should_dioread_nolock(inode
)) {
590 ret
= __block_write_begin(page
, from
, to
,
591 ext4_get_block_unwritten
);
593 ret
= __block_write_begin(page
, from
, to
, ext4_get_block
);
595 if (!ret
&& ext4_should_journal_data(inode
)) {
596 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
598 do_journal_get_write_access
);
605 ext4_orphan_add(handle
, inode
);
606 ext4_write_unlock_xattr(inode
, &no_expand
);
608 ext4_journal_stop(handle
);
610 ext4_truncate_failed_write(inode
);
612 * If truncate failed early the inode might
613 * still be on the orphan list; we need to
614 * make sure the inode is removed from the
615 * orphan list in that case.
618 ext4_orphan_del(NULL
, inode
);
621 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
625 block_commit_write(page
, from
, to
);
632 ext4_write_unlock_xattr(inode
, &no_expand
);
634 ext4_journal_stop(handle
);
640 * Try to write data in the inode.
641 * If the inode has inline data, check whether the new write can be
642 * in the inode also. If not, create the page the handle, move the data
643 * to the page make it update and let the later codes create extent for it.
645 int ext4_try_to_write_inline_data(struct address_space
*mapping
,
647 loff_t pos
, unsigned len
,
654 struct ext4_iloc iloc
;
656 if (pos
+ len
> ext4_get_max_inline_size(inode
))
659 ret
= ext4_get_inode_loc(inode
, &iloc
);
664 * The possible write could happen in the inode,
665 * so try to reserve the space in inode first.
667 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
668 if (IS_ERR(handle
)) {
669 ret
= PTR_ERR(handle
);
674 ret
= ext4_prepare_inline_data(handle
, inode
, pos
+ len
);
675 if (ret
&& ret
!= -ENOSPC
)
678 /* We don't have space in inline inode, so convert it to extent. */
679 if (ret
== -ENOSPC
) {
680 ext4_journal_stop(handle
);
685 flags
|= AOP_FLAG_NOFS
;
687 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
694 down_read(&EXT4_I(inode
)->xattr_sem
);
695 if (!ext4_has_inline_data(inode
)) {
702 if (!PageUptodate(page
)) {
703 ret
= ext4_read_inline_page(inode
, page
);
711 up_read(&EXT4_I(inode
)->xattr_sem
);
714 ext4_journal_stop(handle
);
718 return ext4_convert_inline_data_to_extent(mapping
,
722 int ext4_write_inline_data_end(struct inode
*inode
, loff_t pos
, unsigned len
,
723 unsigned copied
, struct page
*page
)
727 struct ext4_iloc iloc
;
729 if (unlikely(copied
< len
)) {
730 if (!PageUptodate(page
)) {
736 ret
= ext4_get_inode_loc(inode
, &iloc
);
738 ext4_std_error(inode
->i_sb
, ret
);
743 ext4_write_lock_xattr(inode
, &no_expand
);
744 BUG_ON(!ext4_has_inline_data(inode
));
746 kaddr
= kmap_atomic(page
);
747 ext4_write_inline_data(inode
, &iloc
, kaddr
, pos
, len
);
748 kunmap_atomic(kaddr
);
749 SetPageUptodate(page
);
750 /* clear page dirty so that writepages wouldn't work for us. */
751 ClearPageDirty(page
);
753 ext4_write_unlock_xattr(inode
, &no_expand
);
760 ext4_journalled_write_inline_data(struct inode
*inode
,
766 struct ext4_iloc iloc
;
768 ret
= ext4_get_inode_loc(inode
, &iloc
);
770 ext4_std_error(inode
->i_sb
, ret
);
774 ext4_write_lock_xattr(inode
, &no_expand
);
775 kaddr
= kmap_atomic(page
);
776 ext4_write_inline_data(inode
, &iloc
, kaddr
, 0, len
);
777 kunmap_atomic(kaddr
);
778 ext4_write_unlock_xattr(inode
, &no_expand
);
784 * Try to make the page cache and handle ready for the inline data case.
785 * We can call this function in 2 cases:
786 * 1. The inode is created and the first write exceeds inline size. We can
787 * clear the inode state safely.
788 * 2. The inode has inline data, then we need to read the data, make it
789 * update and dirty so that ext4_da_writepages can handle it. We don't
790 * need to start the journal since the file's metatdata isn't changed now.
792 static int ext4_da_convert_inline_data_to_extent(struct address_space
*mapping
,
797 int ret
= 0, inline_size
;
800 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
804 down_read(&EXT4_I(inode
)->xattr_sem
);
805 if (!ext4_has_inline_data(inode
)) {
806 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
810 inline_size
= ext4_get_inline_size(inode
);
812 if (!PageUptodate(page
)) {
813 ret
= ext4_read_inline_page(inode
, page
);
818 ret
= __block_write_begin(page
, 0, inline_size
,
819 ext4_da_get_block_prep
);
821 up_read(&EXT4_I(inode
)->xattr_sem
);
824 ext4_truncate_failed_write(inode
);
829 SetPageUptodate(page
);
830 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
831 *fsdata
= (void *)CONVERT_INLINE_DATA
;
834 up_read(&EXT4_I(inode
)->xattr_sem
);
843 * Prepare the write for the inline data.
844 * If the the data can be written into the inode, we just read
845 * the page and make it uptodate, and start the journal.
846 * Otherwise read the page, makes it dirty so that it can be
847 * handle in writepages(the i_disksize update is left to the
848 * normal ext4_da_write_end).
850 int ext4_da_write_inline_data_begin(struct address_space
*mapping
,
852 loff_t pos
, unsigned len
,
857 int ret
, inline_size
;
860 struct ext4_iloc iloc
;
863 ret
= ext4_get_inode_loc(inode
, &iloc
);
868 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
869 if (IS_ERR(handle
)) {
870 ret
= PTR_ERR(handle
);
874 inline_size
= ext4_get_max_inline_size(inode
);
877 if (inline_size
>= pos
+ len
) {
878 ret
= ext4_prepare_inline_data(handle
, inode
, pos
+ len
);
879 if (ret
&& ret
!= -ENOSPC
)
884 * We cannot recurse into the filesystem as the transaction
885 * is already started.
887 flags
|= AOP_FLAG_NOFS
;
889 if (ret
== -ENOSPC
) {
890 ret
= ext4_da_convert_inline_data_to_extent(mapping
,
894 ext4_journal_stop(handle
);
895 if (ret
== -ENOSPC
&&
896 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
902 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
908 down_read(&EXT4_I(inode
)->xattr_sem
);
909 if (!ext4_has_inline_data(inode
)) {
911 goto out_release_page
;
914 if (!PageUptodate(page
)) {
915 ret
= ext4_read_inline_page(inode
, page
);
917 goto out_release_page
;
920 up_read(&EXT4_I(inode
)->xattr_sem
);
925 up_read(&EXT4_I(inode
)->xattr_sem
);
929 ext4_journal_stop(handle
);
935 int ext4_da_write_inline_data_end(struct inode
*inode
, loff_t pos
,
936 unsigned len
, unsigned copied
,
939 int i_size_changed
= 0;
942 ret
= ext4_write_inline_data_end(inode
, pos
, len
, copied
, page
);
951 * No need to use i_size_read() here, the i_size
952 * cannot change under us because we hold i_mutex.
954 * But it's important to update i_size while still holding page lock:
955 * page writeout could otherwise come in and zero beyond i_size.
957 if (pos
+copied
> inode
->i_size
) {
958 i_size_write(inode
, pos
+copied
);
965 * Don't mark the inode dirty under page lock. First, it unnecessarily
966 * makes the holding time of page lock longer. Second, it forces lock
967 * ordering of page lock and transaction start for journaling
971 mark_inode_dirty(inode
);
976 #ifdef INLINE_DIR_DEBUG
977 void ext4_show_inline_dir(struct inode
*dir
, struct buffer_head
*bh
,
978 void *inline_start
, int inline_size
)
981 unsigned short de_len
;
982 struct ext4_dir_entry_2
*de
= inline_start
;
983 void *dlimit
= inline_start
+ inline_size
;
985 trace_printk("inode %lu\n", dir
->i_ino
);
987 while ((void *)de
< dlimit
) {
988 de_len
= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
989 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
990 offset
, de_len
, de
->name_len
, de
->name
,
991 de
->name_len
, le32_to_cpu(de
->inode
));
992 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
993 inline_start
, inline_size
, offset
))
997 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1001 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1005 * Add a new entry into a inline dir.
1006 * It will return -ENOSPC if no space is available, and -EIO
1007 * and -EEXIST if directory entry already exists.
1009 static int ext4_add_dirent_to_inline(handle_t
*handle
,
1010 struct ext4_filename
*fname
,
1012 struct inode
*inode
,
1013 struct ext4_iloc
*iloc
,
1014 void *inline_start
, int inline_size
)
1017 struct ext4_dir_entry_2
*de
;
1019 err
= ext4_find_dest_de(dir
, inode
, iloc
->bh
, inline_start
,
1020 inline_size
, fname
, &de
);
1024 BUFFER_TRACE(iloc
->bh
, "get_write_access");
1025 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
1028 ext4_insert_dentry(inode
, de
, inline_size
, fname
);
1030 ext4_show_inline_dir(dir
, iloc
->bh
, inline_start
, inline_size
);
1033 * XXX shouldn't update any times until successful
1034 * completion of syscall, but too many callers depend
1037 * XXX similarly, too many callers depend on
1038 * ext4_new_inode() setting the times, but error
1039 * recovery deletes the inode, so the worst that can
1040 * happen is that the times are slightly out of date
1041 * and/or different from the directory change time.
1043 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
1044 ext4_update_dx_flag(dir
);
1049 static void *ext4_get_inline_xattr_pos(struct inode
*inode
,
1050 struct ext4_iloc
*iloc
)
1052 struct ext4_xattr_entry
*entry
;
1053 struct ext4_xattr_ibody_header
*header
;
1055 BUG_ON(!EXT4_I(inode
)->i_inline_off
);
1057 header
= IHDR(inode
, ext4_raw_inode(iloc
));
1058 entry
= (struct ext4_xattr_entry
*)((void *)ext4_raw_inode(iloc
) +
1059 EXT4_I(inode
)->i_inline_off
);
1061 return (void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
);
1064 /* Set the final de to cover the whole block. */
1065 static void ext4_update_final_de(void *de_buf
, int old_size
, int new_size
)
1067 struct ext4_dir_entry_2
*de
, *prev_de
;
1071 de
= (struct ext4_dir_entry_2
*)de_buf
;
1073 limit
= de_buf
+ old_size
;
1076 de_len
= ext4_rec_len_from_disk(de
->rec_len
, old_size
);
1078 de
= (struct ext4_dir_entry_2
*)de_buf
;
1079 } while (de_buf
< limit
);
1081 prev_de
->rec_len
= ext4_rec_len_to_disk(de_len
+ new_size
-
1082 old_size
, new_size
);
1084 /* this is just created, so create an empty entry. */
1086 de
->rec_len
= ext4_rec_len_to_disk(new_size
, new_size
);
1090 static int ext4_update_inline_dir(handle_t
*handle
, struct inode
*dir
,
1091 struct ext4_iloc
*iloc
)
1094 int old_size
= EXT4_I(dir
)->i_inline_size
- EXT4_MIN_INLINE_DATA_SIZE
;
1095 int new_size
= get_max_inline_xattr_value_size(dir
, iloc
);
1097 if (new_size
- old_size
<= EXT4_DIR_REC_LEN(1))
1100 ret
= ext4_update_inline_data(handle
, dir
,
1101 new_size
+ EXT4_MIN_INLINE_DATA_SIZE
);
1105 ext4_update_final_de(ext4_get_inline_xattr_pos(dir
, iloc
), old_size
,
1106 EXT4_I(dir
)->i_inline_size
-
1107 EXT4_MIN_INLINE_DATA_SIZE
);
1108 dir
->i_size
= EXT4_I(dir
)->i_disksize
= EXT4_I(dir
)->i_inline_size
;
1112 static void ext4_restore_inline_data(handle_t
*handle
, struct inode
*inode
,
1113 struct ext4_iloc
*iloc
,
1114 void *buf
, int inline_size
)
1116 ext4_create_inline_data(handle
, inode
, inline_size
);
1117 ext4_write_inline_data(inode
, iloc
, buf
, 0, inline_size
);
1118 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
1121 static int ext4_finish_convert_inline_dir(handle_t
*handle
,
1122 struct inode
*inode
,
1123 struct buffer_head
*dir_block
,
1127 int err
, csum_size
= 0, header_size
= 0;
1128 struct ext4_dir_entry_2
*de
;
1129 struct ext4_dir_entry_tail
*t
;
1130 void *target
= dir_block
->b_data
;
1133 * First create "." and ".." and then copy the dir information
1134 * back to the block.
1136 de
= (struct ext4_dir_entry_2
*)target
;
1137 de
= ext4_init_dot_dotdot(inode
, de
,
1138 inode
->i_sb
->s_blocksize
, csum_size
,
1139 le32_to_cpu(((struct ext4_dir_entry_2
*)buf
)->inode
), 1);
1140 header_size
= (void *)de
- target
;
1142 memcpy((void *)de
, buf
+ EXT4_INLINE_DOTDOT_SIZE
,
1143 inline_size
- EXT4_INLINE_DOTDOT_SIZE
);
1145 if (ext4_has_metadata_csum(inode
->i_sb
))
1146 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1148 inode
->i_size
= inode
->i_sb
->s_blocksize
;
1149 i_size_write(inode
, inode
->i_sb
->s_blocksize
);
1150 EXT4_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1151 ext4_update_final_de(dir_block
->b_data
,
1152 inline_size
- EXT4_INLINE_DOTDOT_SIZE
+ header_size
,
1153 inode
->i_sb
->s_blocksize
- csum_size
);
1156 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
,
1157 inode
->i_sb
->s_blocksize
);
1158 initialize_dirent_tail(t
, inode
->i_sb
->s_blocksize
);
1160 set_buffer_uptodate(dir_block
);
1161 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
1164 set_buffer_verified(dir_block
);
1165 return ext4_mark_inode_dirty(handle
, inode
);
1168 static int ext4_convert_inline_data_nolock(handle_t
*handle
,
1169 struct inode
*inode
,
1170 struct ext4_iloc
*iloc
)
1174 struct buffer_head
*data_bh
= NULL
;
1175 struct ext4_map_blocks map
;
1178 inline_size
= ext4_get_inline_size(inode
);
1179 buf
= kmalloc(inline_size
, GFP_NOFS
);
1185 error
= ext4_read_inline_data(inode
, buf
, inline_size
, iloc
);
1190 * Make sure the inline directory entries pass checks before we try to
1191 * convert them, so that we avoid touching stuff that needs fsck.
1193 if (S_ISDIR(inode
->i_mode
)) {
1194 error
= ext4_check_all_de(inode
, iloc
->bh
,
1195 buf
+ EXT4_INLINE_DOTDOT_SIZE
,
1196 inline_size
- EXT4_INLINE_DOTDOT_SIZE
);
1201 error
= ext4_destroy_inline_data_nolock(handle
, inode
);
1208 error
= ext4_map_blocks(handle
, inode
, &map
, EXT4_GET_BLOCKS_CREATE
);
1211 if (!(map
.m_flags
& EXT4_MAP_MAPPED
)) {
1216 data_bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
1222 lock_buffer(data_bh
);
1223 error
= ext4_journal_get_create_access(handle
, data_bh
);
1225 unlock_buffer(data_bh
);
1229 memset(data_bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
1231 if (!S_ISDIR(inode
->i_mode
)) {
1232 memcpy(data_bh
->b_data
, buf
, inline_size
);
1233 set_buffer_uptodate(data_bh
);
1234 error
= ext4_handle_dirty_metadata(handle
,
1237 error
= ext4_finish_convert_inline_dir(handle
, inode
, data_bh
,
1241 unlock_buffer(data_bh
);
1244 ext4_restore_inline_data(handle
, inode
, iloc
, buf
, inline_size
);
1253 * Try to add the new entry to the inline data.
1254 * If succeeds, return 0. If not, extended the inline dir and copied data to
1255 * the new created block.
1257 int ext4_try_add_inline_entry(handle_t
*handle
, struct ext4_filename
*fname
,
1258 struct inode
*dir
, struct inode
*inode
)
1260 int ret
, inline_size
, no_expand
;
1262 struct ext4_iloc iloc
;
1264 ret
= ext4_get_inode_loc(dir
, &iloc
);
1268 ext4_write_lock_xattr(dir
, &no_expand
);
1269 if (!ext4_has_inline_data(dir
))
1272 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1273 EXT4_INLINE_DOTDOT_SIZE
;
1274 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DOTDOT_SIZE
;
1276 ret
= ext4_add_dirent_to_inline(handle
, fname
, dir
, inode
, &iloc
,
1277 inline_start
, inline_size
);
1281 /* check whether it can be inserted to inline xattr space. */
1282 inline_size
= EXT4_I(dir
)->i_inline_size
-
1283 EXT4_MIN_INLINE_DATA_SIZE
;
1285 /* Try to use the xattr space.*/
1286 ret
= ext4_update_inline_dir(handle
, dir
, &iloc
);
1287 if (ret
&& ret
!= -ENOSPC
)
1290 inline_size
= EXT4_I(dir
)->i_inline_size
-
1291 EXT4_MIN_INLINE_DATA_SIZE
;
1295 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1297 ret
= ext4_add_dirent_to_inline(handle
, fname
, dir
,
1298 inode
, &iloc
, inline_start
,
1306 * The inline space is filled up, so create a new block for it.
1307 * As the extent tree will be created, we have to save the inline
1310 ret
= ext4_convert_inline_data_nolock(handle
, dir
, &iloc
);
1313 ext4_write_unlock_xattr(dir
, &no_expand
);
1314 ext4_mark_inode_dirty(handle
, dir
);
1320 * This function fills a red-black tree with information from an
1321 * inlined dir. It returns the number directory entries loaded
1322 * into the tree. If there is an error it is returned in err.
1324 int htree_inlinedir_to_tree(struct file
*dir_file
,
1325 struct inode
*dir
, ext4_lblk_t block
,
1326 struct dx_hash_info
*hinfo
,
1327 __u32 start_hash
, __u32 start_minor_hash
,
1328 int *has_inline_data
)
1330 int err
= 0, count
= 0;
1331 unsigned int parent_ino
;
1333 struct ext4_dir_entry_2
*de
;
1334 struct inode
*inode
= file_inode(dir_file
);
1335 int ret
, inline_size
= 0;
1336 struct ext4_iloc iloc
;
1337 void *dir_buf
= NULL
;
1338 struct ext4_dir_entry_2 fake
;
1339 struct fscrypt_str tmp_str
;
1341 ret
= ext4_get_inode_loc(inode
, &iloc
);
1345 down_read(&EXT4_I(inode
)->xattr_sem
);
1346 if (!ext4_has_inline_data(inode
)) {
1347 up_read(&EXT4_I(inode
)->xattr_sem
);
1348 *has_inline_data
= 0;
1352 inline_size
= ext4_get_inline_size(inode
);
1353 dir_buf
= kmalloc(inline_size
, GFP_NOFS
);
1356 up_read(&EXT4_I(inode
)->xattr_sem
);
1360 ret
= ext4_read_inline_data(inode
, dir_buf
, inline_size
, &iloc
);
1361 up_read(&EXT4_I(inode
)->xattr_sem
);
1366 parent_ino
= le32_to_cpu(((struct ext4_dir_entry_2
*)dir_buf
)->inode
);
1367 while (pos
< inline_size
) {
1369 * As inlined dir doesn't store any information about '.' and
1370 * only the inode number of '..' is stored, we have to handle
1374 fake
.inode
= cpu_to_le32(inode
->i_ino
);
1376 strcpy(fake
.name
, ".");
1377 fake
.rec_len
= ext4_rec_len_to_disk(
1378 EXT4_DIR_REC_LEN(fake
.name_len
),
1380 ext4_set_de_type(inode
->i_sb
, &fake
, S_IFDIR
);
1382 pos
= EXT4_INLINE_DOTDOT_OFFSET
;
1383 } else if (pos
== EXT4_INLINE_DOTDOT_OFFSET
) {
1384 fake
.inode
= cpu_to_le32(parent_ino
);
1386 strcpy(fake
.name
, "..");
1387 fake
.rec_len
= ext4_rec_len_to_disk(
1388 EXT4_DIR_REC_LEN(fake
.name_len
),
1390 ext4_set_de_type(inode
->i_sb
, &fake
, S_IFDIR
);
1392 pos
= EXT4_INLINE_DOTDOT_SIZE
;
1394 de
= (struct ext4_dir_entry_2
*)(dir_buf
+ pos
);
1395 pos
+= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
1396 if (ext4_check_dir_entry(inode
, dir_file
, de
,
1398 inline_size
, pos
)) {
1404 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
1405 if ((hinfo
->hash
< start_hash
) ||
1406 ((hinfo
->hash
== start_hash
) &&
1407 (hinfo
->minor_hash
< start_minor_hash
)))
1411 tmp_str
.name
= de
->name
;
1412 tmp_str
.len
= de
->name_len
;
1413 err
= ext4_htree_store_dirent(dir_file
, hinfo
->hash
,
1414 hinfo
->minor_hash
, de
, &tmp_str
);
1429 * So this function is called when the volume is mkfsed with
1430 * dir_index disabled. In order to keep f_pos persistent
1431 * after we convert from an inlined dir to a blocked based,
1432 * we just pretend that we are a normal dir and return the
1433 * offset as if '.' and '..' really take place.
1436 int ext4_read_inline_dir(struct file
*file
,
1437 struct dir_context
*ctx
,
1438 int *has_inline_data
)
1440 unsigned int offset
, parent_ino
;
1442 struct ext4_dir_entry_2
*de
;
1443 struct super_block
*sb
;
1444 struct inode
*inode
= file_inode(file
);
1445 int ret
, inline_size
= 0;
1446 struct ext4_iloc iloc
;
1447 void *dir_buf
= NULL
;
1448 int dotdot_offset
, dotdot_size
, extra_offset
, extra_size
;
1450 ret
= ext4_get_inode_loc(inode
, &iloc
);
1454 down_read(&EXT4_I(inode
)->xattr_sem
);
1455 if (!ext4_has_inline_data(inode
)) {
1456 up_read(&EXT4_I(inode
)->xattr_sem
);
1457 *has_inline_data
= 0;
1461 inline_size
= ext4_get_inline_size(inode
);
1462 dir_buf
= kmalloc(inline_size
, GFP_NOFS
);
1465 up_read(&EXT4_I(inode
)->xattr_sem
);
1469 ret
= ext4_read_inline_data(inode
, dir_buf
, inline_size
, &iloc
);
1470 up_read(&EXT4_I(inode
)->xattr_sem
);
1476 parent_ino
= le32_to_cpu(((struct ext4_dir_entry_2
*)dir_buf
)->inode
);
1480 * dotdot_offset and dotdot_size is the real offset and
1481 * size for ".." and "." if the dir is block based while
1482 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1483 * So we will use extra_offset and extra_size to indicate them
1484 * during the inline dir iteration.
1486 dotdot_offset
= EXT4_DIR_REC_LEN(1);
1487 dotdot_size
= dotdot_offset
+ EXT4_DIR_REC_LEN(2);
1488 extra_offset
= dotdot_size
- EXT4_INLINE_DOTDOT_SIZE
;
1489 extra_size
= extra_offset
+ inline_size
;
1492 * If the version has changed since the last call to
1493 * readdir(2), then we might be pointing to an invalid
1494 * dirent right now. Scan from the start of the inline
1497 if (file
->f_version
!= inode
->i_version
) {
1498 for (i
= 0; i
< extra_size
&& i
< offset
;) {
1500 * "." is with offset 0 and
1501 * ".." is dotdot_offset.
1506 } else if (i
== dotdot_offset
) {
1510 /* for other entry, the real offset in
1511 * the buf has to be tuned accordingly.
1513 de
= (struct ext4_dir_entry_2
*)
1514 (dir_buf
+ i
- extra_offset
);
1515 /* It's too expensive to do a full
1516 * dirent test each time round this
1517 * loop, but we do have to test at
1518 * least that it is non-zero. A
1519 * failure will be detected in the
1520 * dirent test below. */
1521 if (ext4_rec_len_from_disk(de
->rec_len
, extra_size
)
1522 < EXT4_DIR_REC_LEN(1))
1524 i
+= ext4_rec_len_from_disk(de
->rec_len
,
1529 file
->f_version
= inode
->i_version
;
1532 while (ctx
->pos
< extra_size
) {
1533 if (ctx
->pos
== 0) {
1534 if (!dir_emit(ctx
, ".", 1, inode
->i_ino
, DT_DIR
))
1536 ctx
->pos
= dotdot_offset
;
1540 if (ctx
->pos
== dotdot_offset
) {
1541 if (!dir_emit(ctx
, "..", 2, parent_ino
, DT_DIR
))
1543 ctx
->pos
= dotdot_size
;
1547 de
= (struct ext4_dir_entry_2
*)
1548 (dir_buf
+ ctx
->pos
- extra_offset
);
1549 if (ext4_check_dir_entry(inode
, file
, de
, iloc
.bh
, dir_buf
,
1550 extra_size
, ctx
->pos
))
1552 if (le32_to_cpu(de
->inode
)) {
1553 if (!dir_emit(ctx
, de
->name
, de
->name_len
,
1554 le32_to_cpu(de
->inode
),
1555 get_dtype(sb
, de
->file_type
)))
1558 ctx
->pos
+= ext4_rec_len_from_disk(de
->rec_len
, extra_size
);
1566 struct buffer_head
*ext4_get_first_inline_block(struct inode
*inode
,
1567 struct ext4_dir_entry_2
**parent_de
,
1570 struct ext4_iloc iloc
;
1572 *retval
= ext4_get_inode_loc(inode
, &iloc
);
1576 *parent_de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1582 * Try to create the inline data for the new dir.
1583 * If it succeeds, return 0, otherwise return the error.
1584 * In case of ENOSPC, the caller should create the normal disk layout dir.
1586 int ext4_try_create_inline_dir(handle_t
*handle
, struct inode
*parent
,
1587 struct inode
*inode
)
1589 int ret
, inline_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1590 struct ext4_iloc iloc
;
1591 struct ext4_dir_entry_2
*de
;
1593 ret
= ext4_get_inode_loc(inode
, &iloc
);
1597 ret
= ext4_prepare_inline_data(handle
, inode
, inline_size
);
1602 * For inline dir, we only save the inode information for the ".."
1603 * and create a fake dentry to cover the left space.
1605 de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1606 de
->inode
= cpu_to_le32(parent
->i_ino
);
1607 de
= (struct ext4_dir_entry_2
*)((void *)de
+ EXT4_INLINE_DOTDOT_SIZE
);
1609 de
->rec_len
= ext4_rec_len_to_disk(
1610 inline_size
- EXT4_INLINE_DOTDOT_SIZE
,
1612 set_nlink(inode
, 2);
1613 inode
->i_size
= EXT4_I(inode
)->i_disksize
= inline_size
;
1619 struct buffer_head
*ext4_find_inline_entry(struct inode
*dir
,
1620 struct ext4_filename
*fname
,
1621 struct ext4_dir_entry_2
**res_dir
,
1622 int *has_inline_data
)
1625 struct ext4_iloc iloc
;
1629 if (ext4_get_inode_loc(dir
, &iloc
))
1632 down_read(&EXT4_I(dir
)->xattr_sem
);
1633 if (!ext4_has_inline_data(dir
)) {
1634 *has_inline_data
= 0;
1638 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1639 EXT4_INLINE_DOTDOT_SIZE
;
1640 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DOTDOT_SIZE
;
1641 ret
= ext4_search_dir(iloc
.bh
, inline_start
, inline_size
,
1642 dir
, fname
, 0, res_dir
);
1648 if (ext4_get_inline_size(dir
) == EXT4_MIN_INLINE_DATA_SIZE
)
1651 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1652 inline_size
= ext4_get_inline_size(dir
) - EXT4_MIN_INLINE_DATA_SIZE
;
1654 ret
= ext4_search_dir(iloc
.bh
, inline_start
, inline_size
,
1655 dir
, fname
, 0, res_dir
);
1663 up_read(&EXT4_I(dir
)->xattr_sem
);
1667 int ext4_delete_inline_entry(handle_t
*handle
,
1669 struct ext4_dir_entry_2
*de_del
,
1670 struct buffer_head
*bh
,
1671 int *has_inline_data
)
1673 int err
, inline_size
, no_expand
;
1674 struct ext4_iloc iloc
;
1677 err
= ext4_get_inode_loc(dir
, &iloc
);
1681 ext4_write_lock_xattr(dir
, &no_expand
);
1682 if (!ext4_has_inline_data(dir
)) {
1683 *has_inline_data
= 0;
1687 if ((void *)de_del
- ((void *)ext4_raw_inode(&iloc
)->i_block
) <
1688 EXT4_MIN_INLINE_DATA_SIZE
) {
1689 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1690 EXT4_INLINE_DOTDOT_SIZE
;
1691 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
-
1692 EXT4_INLINE_DOTDOT_SIZE
;
1694 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1695 inline_size
= ext4_get_inline_size(dir
) -
1696 EXT4_MIN_INLINE_DATA_SIZE
;
1699 BUFFER_TRACE(bh
, "get_write_access");
1700 err
= ext4_journal_get_write_access(handle
, bh
);
1704 err
= ext4_generic_delete_entry(handle
, dir
, de_del
, bh
,
1705 inline_start
, inline_size
, 0);
1709 ext4_show_inline_dir(dir
, iloc
.bh
, inline_start
, inline_size
);
1711 ext4_write_unlock_xattr(dir
, &no_expand
);
1712 if (likely(err
== 0))
1713 err
= ext4_mark_inode_dirty(handle
, dir
);
1716 ext4_std_error(dir
->i_sb
, err
);
1721 * Get the inline dentry at offset.
1723 static inline struct ext4_dir_entry_2
*
1724 ext4_get_inline_entry(struct inode
*inode
,
1725 struct ext4_iloc
*iloc
,
1726 unsigned int offset
,
1727 void **inline_start
,
1732 BUG_ON(offset
> ext4_get_inline_size(inode
));
1734 if (offset
< EXT4_MIN_INLINE_DATA_SIZE
) {
1735 inline_pos
= (void *)ext4_raw_inode(iloc
)->i_block
;
1736 *inline_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1738 inline_pos
= ext4_get_inline_xattr_pos(inode
, iloc
);
1739 offset
-= EXT4_MIN_INLINE_DATA_SIZE
;
1740 *inline_size
= ext4_get_inline_size(inode
) -
1741 EXT4_MIN_INLINE_DATA_SIZE
;
1745 *inline_start
= inline_pos
;
1746 return (struct ext4_dir_entry_2
*)(inline_pos
+ offset
);
1749 bool empty_inline_dir(struct inode
*dir
, int *has_inline_data
)
1751 int err
, inline_size
;
1752 struct ext4_iloc iloc
;
1754 unsigned int offset
;
1755 struct ext4_dir_entry_2
*de
;
1758 err
= ext4_get_inode_loc(dir
, &iloc
);
1760 EXT4_ERROR_INODE(dir
, "error %d getting inode %lu block",
1765 down_read(&EXT4_I(dir
)->xattr_sem
);
1766 if (!ext4_has_inline_data(dir
)) {
1767 *has_inline_data
= 0;
1771 de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1772 if (!le32_to_cpu(de
->inode
)) {
1773 ext4_warning(dir
->i_sb
,
1774 "bad inline directory (dir #%lu) - no `..'",
1780 offset
= EXT4_INLINE_DOTDOT_SIZE
;
1781 while (offset
< dir
->i_size
) {
1782 de
= ext4_get_inline_entry(dir
, &iloc
, offset
,
1783 &inline_pos
, &inline_size
);
1784 if (ext4_check_dir_entry(dir
, NULL
, de
,
1785 iloc
.bh
, inline_pos
,
1786 inline_size
, offset
)) {
1787 ext4_warning(dir
->i_sb
,
1788 "bad inline directory (dir #%lu) - "
1789 "inode %u, rec_len %u, name_len %d"
1791 dir
->i_ino
, le32_to_cpu(de
->inode
),
1792 le16_to_cpu(de
->rec_len
), de
->name_len
,
1797 if (le32_to_cpu(de
->inode
)) {
1801 offset
+= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
1805 up_read(&EXT4_I(dir
)->xattr_sem
);
1810 int ext4_destroy_inline_data(handle_t
*handle
, struct inode
*inode
)
1814 ext4_write_lock_xattr(inode
, &no_expand
);
1815 ret
= ext4_destroy_inline_data_nolock(handle
, inode
);
1816 ext4_write_unlock_xattr(inode
, &no_expand
);
1821 int ext4_inline_data_iomap(struct inode
*inode
, struct iomap
*iomap
)
1824 int error
= -EAGAIN
;
1825 struct ext4_iloc iloc
;
1827 down_read(&EXT4_I(inode
)->xattr_sem
);
1828 if (!ext4_has_inline_data(inode
))
1831 error
= ext4_get_inode_loc(inode
, &iloc
);
1835 addr
= (__u64
)iloc
.bh
->b_blocknr
<< inode
->i_sb
->s_blocksize_bits
;
1836 addr
+= (char *)ext4_raw_inode(&iloc
) - iloc
.bh
->b_data
;
1837 addr
+= offsetof(struct ext4_inode
, i_block
);
1843 iomap
->length
= min_t(loff_t
, ext4_get_inline_size(inode
),
1844 i_size_read(inode
));
1846 iomap
->flags
= IOMAP_F_DATA_INLINE
;
1849 up_read(&EXT4_I(inode
)->xattr_sem
);
1853 int ext4_inline_data_fiemap(struct inode
*inode
,
1854 struct fiemap_extent_info
*fieinfo
,
1855 int *has_inline
, __u64 start
, __u64 len
)
1859 __u32 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
|
1862 struct ext4_iloc iloc
;
1864 down_read(&EXT4_I(inode
)->xattr_sem
);
1865 if (!ext4_has_inline_data(inode
)) {
1869 inline_len
= min_t(size_t, ext4_get_inline_size(inode
),
1870 i_size_read(inode
));
1871 if (start
>= inline_len
)
1873 if (start
+ len
< inline_len
)
1874 inline_len
= start
+ len
;
1875 inline_len
-= start
;
1877 error
= ext4_get_inode_loc(inode
, &iloc
);
1881 physical
= (__u64
)iloc
.bh
->b_blocknr
<< inode
->i_sb
->s_blocksize_bits
;
1882 physical
+= (char *)ext4_raw_inode(&iloc
) - iloc
.bh
->b_data
;
1883 physical
+= offsetof(struct ext4_inode
, i_block
);
1886 error
= fiemap_fill_next_extent(fieinfo
, start
, physical
,
1890 up_read(&EXT4_I(inode
)->xattr_sem
);
1891 return (error
< 0 ? error
: 0);
1895 * Called during xattr set, and if we can sparse space 'needed',
1896 * just create the extent tree evict the data to the outer block.
1898 * We use jbd2 instead of page cache to move data to the 1st block
1899 * so that the whole transaction can be committed as a whole and
1900 * the data isn't lost because of the delayed page cache write.
1902 int ext4_try_to_evict_inline_data(handle_t
*handle
,
1903 struct inode
*inode
,
1907 struct ext4_xattr_entry
*entry
;
1908 struct ext4_inode
*raw_inode
;
1909 struct ext4_iloc iloc
;
1911 error
= ext4_get_inode_loc(inode
, &iloc
);
1915 raw_inode
= ext4_raw_inode(&iloc
);
1916 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
1917 EXT4_I(inode
)->i_inline_off
);
1918 if (EXT4_XATTR_LEN(entry
->e_name_len
) +
1919 EXT4_XATTR_SIZE(le32_to_cpu(entry
->e_value_size
)) < needed
) {
1924 error
= ext4_convert_inline_data_nolock(handle
, inode
, &iloc
);
1930 int ext4_inline_data_truncate(struct inode
*inode
, int *has_inline
)
1933 int inline_size
, value_len
, needed_blocks
, no_expand
, err
= 0;
1936 struct ext4_xattr_ibody_find is
= {
1937 .s
= { .not_found
= -ENODATA
, },
1939 struct ext4_xattr_info i
= {
1940 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
1941 .name
= EXT4_XATTR_SYSTEM_DATA
,
1945 needed_blocks
= ext4_writepage_trans_blocks(inode
);
1946 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, needed_blocks
);
1948 return PTR_ERR(handle
);
1950 ext4_write_lock_xattr(inode
, &no_expand
);
1951 if (!ext4_has_inline_data(inode
)) {
1953 ext4_journal_stop(handle
);
1957 if ((err
= ext4_orphan_add(handle
, inode
)) != 0)
1960 if ((err
= ext4_get_inode_loc(inode
, &is
.iloc
)) != 0)
1963 down_write(&EXT4_I(inode
)->i_data_sem
);
1964 i_size
= inode
->i_size
;
1965 inline_size
= ext4_get_inline_size(inode
);
1966 EXT4_I(inode
)->i_disksize
= i_size
;
1968 if (i_size
< inline_size
) {
1969 /* Clear the content in the xattr space. */
1970 if (inline_size
> EXT4_MIN_INLINE_DATA_SIZE
) {
1971 if ((err
= ext4_xattr_ibody_find(inode
, &i
, &is
)) != 0)
1974 BUG_ON(is
.s
.not_found
);
1976 value_len
= le32_to_cpu(is
.s
.here
->e_value_size
);
1977 value
= kmalloc(value_len
, GFP_NOFS
);
1983 err
= ext4_xattr_ibody_get(inode
, i
.name_index
,
1984 i
.name
, value
, value_len
);
1989 i
.value_len
= i_size
> EXT4_MIN_INLINE_DATA_SIZE
?
1990 i_size
- EXT4_MIN_INLINE_DATA_SIZE
: 0;
1991 err
= ext4_xattr_ibody_inline_set(handle
, inode
,
1997 /* Clear the content within i_blocks. */
1998 if (i_size
< EXT4_MIN_INLINE_DATA_SIZE
) {
1999 void *p
= (void *) ext4_raw_inode(&is
.iloc
)->i_block
;
2000 memset(p
+ i_size
, 0,
2001 EXT4_MIN_INLINE_DATA_SIZE
- i_size
);
2004 EXT4_I(inode
)->i_inline_size
= i_size
<
2005 EXT4_MIN_INLINE_DATA_SIZE
?
2006 EXT4_MIN_INLINE_DATA_SIZE
: i_size
;
2010 up_write(&EXT4_I(inode
)->i_data_sem
);
2013 ext4_write_unlock_xattr(inode
, &no_expand
);
2016 ext4_orphan_del(handle
, inode
);
2019 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
2020 err
= ext4_mark_inode_dirty(handle
, inode
);
2022 ext4_handle_sync(handle
);
2024 ext4_journal_stop(handle
);
2028 int ext4_convert_inline_data(struct inode
*inode
)
2030 int error
, needed_blocks
, no_expand
;
2032 struct ext4_iloc iloc
;
2034 if (!ext4_has_inline_data(inode
)) {
2035 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
2039 needed_blocks
= ext4_writepage_trans_blocks(inode
);
2042 error
= ext4_get_inode_loc(inode
, &iloc
);
2046 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
2047 if (IS_ERR(handle
)) {
2048 error
= PTR_ERR(handle
);
2052 ext4_write_lock_xattr(inode
, &no_expand
);
2053 if (ext4_has_inline_data(inode
))
2054 error
= ext4_convert_inline_data_nolock(handle
, inode
, &iloc
);
2055 ext4_write_unlock_xattr(inode
, &no_expand
);
2056 ext4_journal_stop(handle
);