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>
17 #include <linux/iversion.h>
19 #include "ext4_jbd2.h"
24 #define EXT4_XATTR_SYSTEM_DATA "data"
25 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
26 #define EXT4_INLINE_DOTDOT_OFFSET 2
27 #define EXT4_INLINE_DOTDOT_SIZE 4
29 static int ext4_get_inline_size(struct inode
*inode
)
31 if (EXT4_I(inode
)->i_inline_off
)
32 return EXT4_I(inode
)->i_inline_size
;
37 static int get_max_inline_xattr_value_size(struct inode
*inode
,
38 struct ext4_iloc
*iloc
)
40 struct ext4_xattr_ibody_header
*header
;
41 struct ext4_xattr_entry
*entry
;
42 struct ext4_inode
*raw_inode
;
45 min_offs
= EXT4_SB(inode
->i_sb
)->s_inode_size
-
46 EXT4_GOOD_OLD_INODE_SIZE
-
47 EXT4_I(inode
)->i_extra_isize
-
48 sizeof(struct ext4_xattr_ibody_header
);
51 * We need to subtract another sizeof(__u32) since an in-inode xattr
52 * needs an empty 4 bytes to indicate the gap between the xattr entry
53 * and the name/value pair.
55 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
56 return EXT4_XATTR_SIZE(min_offs
-
57 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA
)) -
58 EXT4_XATTR_ROUND
- sizeof(__u32
));
60 raw_inode
= ext4_raw_inode(iloc
);
61 header
= IHDR(inode
, raw_inode
);
62 entry
= IFIRST(header
);
64 /* Compute min_offs. */
65 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
66 if (!entry
->e_value_inum
&& entry
->e_value_size
) {
67 size_t offs
= le16_to_cpu(entry
->e_value_offs
);
73 ((void *)entry
- (void *)IFIRST(header
)) - sizeof(__u32
);
75 if (EXT4_I(inode
)->i_inline_off
) {
76 entry
= (struct ext4_xattr_entry
*)
77 ((void *)raw_inode
+ EXT4_I(inode
)->i_inline_off
);
79 free
+= EXT4_XATTR_SIZE(le32_to_cpu(entry
->e_value_size
));
83 free
-= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA
));
85 if (free
> EXT4_XATTR_ROUND
)
86 free
= EXT4_XATTR_SIZE(free
- EXT4_XATTR_ROUND
);
95 * Get the maximum size we now can store in an inode.
96 * If we can't find the space for a xattr entry, don't use the space
97 * of the extents since we have no space to indicate the inline data.
99 int ext4_get_max_inline_size(struct inode
*inode
)
101 int error
, max_inline_size
;
102 struct ext4_iloc iloc
;
104 if (EXT4_I(inode
)->i_extra_isize
== 0)
107 error
= ext4_get_inode_loc(inode
, &iloc
);
109 ext4_error_inode(inode
, __func__
, __LINE__
, 0,
110 "can't get inode location %lu",
115 down_read(&EXT4_I(inode
)->xattr_sem
);
116 max_inline_size
= get_max_inline_xattr_value_size(inode
, &iloc
);
117 up_read(&EXT4_I(inode
)->xattr_sem
);
121 if (!max_inline_size
)
124 return max_inline_size
+ EXT4_MIN_INLINE_DATA_SIZE
;
128 * this function does not take xattr_sem, which is OK because it is
129 * currently only used in a code path coming form ext4_iget, before
130 * the new inode has been unlocked
132 int ext4_find_inline_data_nolock(struct inode
*inode
)
134 struct ext4_xattr_ibody_find is
= {
135 .s
= { .not_found
= -ENODATA
, },
137 struct ext4_xattr_info i
= {
138 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
139 .name
= EXT4_XATTR_SYSTEM_DATA
,
143 if (EXT4_I(inode
)->i_extra_isize
== 0)
146 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
150 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
154 if (!is
.s
.not_found
) {
155 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
156 (void *)ext4_raw_inode(&is
.iloc
));
157 EXT4_I(inode
)->i_inline_size
= EXT4_MIN_INLINE_DATA_SIZE
+
158 le32_to_cpu(is
.s
.here
->e_value_size
);
159 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
166 static int ext4_read_inline_data(struct inode
*inode
, void *buffer
,
168 struct ext4_iloc
*iloc
)
170 struct ext4_xattr_entry
*entry
;
171 struct ext4_xattr_ibody_header
*header
;
173 struct ext4_inode
*raw_inode
;
178 BUG_ON(len
> EXT4_I(inode
)->i_inline_size
);
180 cp_len
= len
< EXT4_MIN_INLINE_DATA_SIZE
?
181 len
: EXT4_MIN_INLINE_DATA_SIZE
;
183 raw_inode
= ext4_raw_inode(iloc
);
184 memcpy(buffer
, (void *)(raw_inode
->i_block
), cp_len
);
192 header
= IHDR(inode
, raw_inode
);
193 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
194 EXT4_I(inode
)->i_inline_off
);
195 len
= min_t(unsigned int, len
,
196 (unsigned int)le32_to_cpu(entry
->e_value_size
));
199 (void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
), len
);
207 * write the buffer to the inline inode.
208 * If 'create' is set, we don't need to do the extra copy in the xattr
209 * value since it is already handled by ext4_xattr_ibody_inline_set.
210 * That saves us one memcpy.
212 static void ext4_write_inline_data(struct inode
*inode
, struct ext4_iloc
*iloc
,
213 void *buffer
, loff_t pos
, unsigned int len
)
215 struct ext4_xattr_entry
*entry
;
216 struct ext4_xattr_ibody_header
*header
;
217 struct ext4_inode
*raw_inode
;
220 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
223 BUG_ON(!EXT4_I(inode
)->i_inline_off
);
224 BUG_ON(pos
+ len
> EXT4_I(inode
)->i_inline_size
);
226 raw_inode
= ext4_raw_inode(iloc
);
229 if (pos
< EXT4_MIN_INLINE_DATA_SIZE
) {
230 cp_len
= pos
+ len
> EXT4_MIN_INLINE_DATA_SIZE
?
231 EXT4_MIN_INLINE_DATA_SIZE
- pos
: len
;
232 memcpy((void *)raw_inode
->i_block
+ pos
, buffer
, cp_len
);
242 pos
-= EXT4_MIN_INLINE_DATA_SIZE
;
243 header
= IHDR(inode
, raw_inode
);
244 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
245 EXT4_I(inode
)->i_inline_off
);
247 memcpy((void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
) + pos
,
251 static int ext4_create_inline_data(handle_t
*handle
,
252 struct inode
*inode
, unsigned len
)
256 struct ext4_xattr_ibody_find is
= {
257 .s
= { .not_found
= -ENODATA
, },
259 struct ext4_xattr_info i
= {
260 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
261 .name
= EXT4_XATTR_SYSTEM_DATA
,
264 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
268 BUFFER_TRACE(is
.iloc
.bh
, "get_write_access");
269 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
273 if (len
> EXT4_MIN_INLINE_DATA_SIZE
) {
274 value
= EXT4_ZERO_XATTR_VALUE
;
275 len
-= EXT4_MIN_INLINE_DATA_SIZE
;
281 /* Insert the the xttr entry. */
285 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
289 BUG_ON(!is
.s
.not_found
);
291 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
293 if (error
== -ENOSPC
)
294 ext4_clear_inode_state(inode
,
295 EXT4_STATE_MAY_INLINE_DATA
);
299 memset((void *)ext4_raw_inode(&is
.iloc
)->i_block
,
300 0, EXT4_MIN_INLINE_DATA_SIZE
);
302 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
303 (void *)ext4_raw_inode(&is
.iloc
));
304 EXT4_I(inode
)->i_inline_size
= len
+ EXT4_MIN_INLINE_DATA_SIZE
;
305 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
306 ext4_set_inode_flag(inode
, EXT4_INODE_INLINE_DATA
);
308 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
315 static int ext4_update_inline_data(handle_t
*handle
, struct inode
*inode
,
320 struct ext4_xattr_ibody_find is
= {
321 .s
= { .not_found
= -ENODATA
, },
323 struct ext4_xattr_info i
= {
324 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
325 .name
= EXT4_XATTR_SYSTEM_DATA
,
328 /* If the old space is ok, write the data directly. */
329 if (len
<= EXT4_I(inode
)->i_inline_size
)
332 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
336 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
340 BUG_ON(is
.s
.not_found
);
342 len
-= EXT4_MIN_INLINE_DATA_SIZE
;
343 value
= kzalloc(len
, GFP_NOFS
);
349 error
= ext4_xattr_ibody_get(inode
, i
.name_index
, i
.name
,
351 if (error
== -ENODATA
)
354 BUFFER_TRACE(is
.iloc
.bh
, "get_write_access");
355 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
359 /* Update the xttr entry. */
363 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
367 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
368 (void *)ext4_raw_inode(&is
.iloc
));
369 EXT4_I(inode
)->i_inline_size
= EXT4_MIN_INLINE_DATA_SIZE
+
370 le32_to_cpu(is
.s
.here
->e_value_size
);
371 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
373 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
381 static int ext4_prepare_inline_data(handle_t
*handle
, struct inode
*inode
,
384 int ret
, size
, no_expand
;
385 struct ext4_inode_info
*ei
= EXT4_I(inode
);
387 if (!ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
))
390 size
= ext4_get_max_inline_size(inode
);
394 ext4_write_lock_xattr(inode
, &no_expand
);
396 if (ei
->i_inline_off
)
397 ret
= ext4_update_inline_data(handle
, inode
, len
);
399 ret
= ext4_create_inline_data(handle
, inode
, len
);
401 ext4_write_unlock_xattr(inode
, &no_expand
);
405 static int ext4_destroy_inline_data_nolock(handle_t
*handle
,
408 struct ext4_inode_info
*ei
= EXT4_I(inode
);
409 struct ext4_xattr_ibody_find is
= {
410 .s
= { .not_found
= 0, },
412 struct ext4_xattr_info i
= {
413 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
414 .name
= EXT4_XATTR_SYSTEM_DATA
,
420 if (!ei
->i_inline_off
)
423 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
427 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
431 BUFFER_TRACE(is
.iloc
.bh
, "get_write_access");
432 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
436 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
440 memset((void *)ext4_raw_inode(&is
.iloc
)->i_block
,
441 0, EXT4_MIN_INLINE_DATA_SIZE
);
443 if (ext4_has_feature_extents(inode
->i_sb
)) {
444 if (S_ISDIR(inode
->i_mode
) ||
445 S_ISREG(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) {
446 ext4_set_inode_flag(inode
, EXT4_INODE_EXTENTS
);
447 ext4_ext_tree_init(handle
, inode
);
450 ext4_clear_inode_flag(inode
, EXT4_INODE_INLINE_DATA
);
453 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
455 EXT4_I(inode
)->i_inline_off
= 0;
456 EXT4_I(inode
)->i_inline_size
= 0;
457 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
460 if (error
== -ENODATA
)
465 static int ext4_read_inline_page(struct inode
*inode
, struct page
*page
)
470 struct ext4_iloc iloc
;
472 BUG_ON(!PageLocked(page
));
473 BUG_ON(!ext4_has_inline_data(inode
));
476 if (!EXT4_I(inode
)->i_inline_off
) {
477 ext4_warning(inode
->i_sb
, "inode %lu doesn't have inline data.",
482 ret
= ext4_get_inode_loc(inode
, &iloc
);
486 len
= min_t(size_t, ext4_get_inline_size(inode
), i_size_read(inode
));
487 kaddr
= kmap_atomic(page
);
488 ret
= ext4_read_inline_data(inode
, kaddr
, len
, &iloc
);
489 flush_dcache_page(page
);
490 kunmap_atomic(kaddr
);
491 zero_user_segment(page
, len
, PAGE_SIZE
);
492 SetPageUptodate(page
);
499 int ext4_readpage_inline(struct inode
*inode
, struct page
*page
)
503 down_read(&EXT4_I(inode
)->xattr_sem
);
504 if (!ext4_has_inline_data(inode
)) {
505 up_read(&EXT4_I(inode
)->xattr_sem
);
510 * Current inline data can only exist in the 1st page,
511 * So for all the other pages, just set them uptodate.
514 ret
= ext4_read_inline_page(inode
, page
);
515 else if (!PageUptodate(page
)) {
516 zero_user_segment(page
, 0, PAGE_SIZE
);
517 SetPageUptodate(page
);
520 up_read(&EXT4_I(inode
)->xattr_sem
);
523 return ret
>= 0 ? 0 : ret
;
526 static int ext4_convert_inline_data_to_extent(struct address_space
*mapping
,
530 int ret
, needed_blocks
, no_expand
;
531 handle_t
*handle
= NULL
;
532 int retries
= 0, sem_held
= 0;
533 struct page
*page
= NULL
;
535 struct ext4_iloc iloc
;
537 if (!ext4_has_inline_data(inode
)) {
539 * clear the flag so that no new write
540 * will trap here again.
542 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
546 needed_blocks
= ext4_writepage_trans_blocks(inode
);
548 ret
= ext4_get_inode_loc(inode
, &iloc
);
553 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
554 if (IS_ERR(handle
)) {
555 ret
= PTR_ERR(handle
);
560 /* We cannot recurse into the filesystem as the transaction is already
562 flags
|= AOP_FLAG_NOFS
;
564 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
570 ext4_write_lock_xattr(inode
, &no_expand
);
572 /* If some one has already done this for us, just exit. */
573 if (!ext4_has_inline_data(inode
)) {
579 to
= ext4_get_inline_size(inode
);
580 if (!PageUptodate(page
)) {
581 ret
= ext4_read_inline_page(inode
, page
);
586 ret
= ext4_destroy_inline_data_nolock(handle
, inode
);
590 if (ext4_should_dioread_nolock(inode
)) {
591 ret
= __block_write_begin(page
, from
, to
,
592 ext4_get_block_unwritten
);
594 ret
= __block_write_begin(page
, from
, to
, ext4_get_block
);
596 if (!ret
&& ext4_should_journal_data(inode
)) {
597 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
599 do_journal_get_write_access
);
606 ext4_orphan_add(handle
, inode
);
607 ext4_write_unlock_xattr(inode
, &no_expand
);
609 ext4_journal_stop(handle
);
611 ext4_truncate_failed_write(inode
);
613 * If truncate failed early the inode might
614 * still be on the orphan list; we need to
615 * make sure the inode is removed from the
616 * orphan list in that case.
619 ext4_orphan_del(NULL
, inode
);
622 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
626 block_commit_write(page
, from
, to
);
633 ext4_write_unlock_xattr(inode
, &no_expand
);
635 ext4_journal_stop(handle
);
641 * Try to write data in the inode.
642 * If the inode has inline data, check whether the new write can be
643 * in the inode also. If not, create the page the handle, move the data
644 * to the page make it update and let the later codes create extent for it.
646 int ext4_try_to_write_inline_data(struct address_space
*mapping
,
648 loff_t pos
, unsigned len
,
655 struct ext4_iloc iloc
;
657 if (pos
+ len
> ext4_get_max_inline_size(inode
))
660 ret
= ext4_get_inode_loc(inode
, &iloc
);
665 * The possible write could happen in the inode,
666 * so try to reserve the space in inode first.
668 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
669 if (IS_ERR(handle
)) {
670 ret
= PTR_ERR(handle
);
675 ret
= ext4_prepare_inline_data(handle
, inode
, pos
+ len
);
676 if (ret
&& ret
!= -ENOSPC
)
679 /* We don't have space in inline inode, so convert it to extent. */
680 if (ret
== -ENOSPC
) {
681 ext4_journal_stop(handle
);
686 flags
|= AOP_FLAG_NOFS
;
688 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
695 down_read(&EXT4_I(inode
)->xattr_sem
);
696 if (!ext4_has_inline_data(inode
)) {
703 if (!PageUptodate(page
)) {
704 ret
= ext4_read_inline_page(inode
, page
);
712 up_read(&EXT4_I(inode
)->xattr_sem
);
715 ext4_journal_stop(handle
);
719 return ext4_convert_inline_data_to_extent(mapping
,
723 int ext4_write_inline_data_end(struct inode
*inode
, loff_t pos
, unsigned len
,
724 unsigned copied
, struct page
*page
)
728 struct ext4_iloc iloc
;
730 if (unlikely(copied
< len
)) {
731 if (!PageUptodate(page
)) {
737 ret
= ext4_get_inode_loc(inode
, &iloc
);
739 ext4_std_error(inode
->i_sb
, ret
);
744 ext4_write_lock_xattr(inode
, &no_expand
);
745 BUG_ON(!ext4_has_inline_data(inode
));
747 kaddr
= kmap_atomic(page
);
748 ext4_write_inline_data(inode
, &iloc
, kaddr
, pos
, len
);
749 kunmap_atomic(kaddr
);
750 SetPageUptodate(page
);
751 /* clear page dirty so that writepages wouldn't work for us. */
752 ClearPageDirty(page
);
754 ext4_write_unlock_xattr(inode
, &no_expand
);
761 ext4_journalled_write_inline_data(struct inode
*inode
,
767 struct ext4_iloc iloc
;
769 ret
= ext4_get_inode_loc(inode
, &iloc
);
771 ext4_std_error(inode
->i_sb
, ret
);
775 ext4_write_lock_xattr(inode
, &no_expand
);
776 kaddr
= kmap_atomic(page
);
777 ext4_write_inline_data(inode
, &iloc
, kaddr
, 0, len
);
778 kunmap_atomic(kaddr
);
779 ext4_write_unlock_xattr(inode
, &no_expand
);
785 * Try to make the page cache and handle ready for the inline data case.
786 * We can call this function in 2 cases:
787 * 1. The inode is created and the first write exceeds inline size. We can
788 * clear the inode state safely.
789 * 2. The inode has inline data, then we need to read the data, make it
790 * update and dirty so that ext4_da_writepages can handle it. We don't
791 * need to start the journal since the file's metatdata isn't changed now.
793 static int ext4_da_convert_inline_data_to_extent(struct address_space
*mapping
,
798 int ret
= 0, inline_size
;
801 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
805 down_read(&EXT4_I(inode
)->xattr_sem
);
806 if (!ext4_has_inline_data(inode
)) {
807 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
811 inline_size
= ext4_get_inline_size(inode
);
813 if (!PageUptodate(page
)) {
814 ret
= ext4_read_inline_page(inode
, page
);
819 ret
= __block_write_begin(page
, 0, inline_size
,
820 ext4_da_get_block_prep
);
822 up_read(&EXT4_I(inode
)->xattr_sem
);
825 ext4_truncate_failed_write(inode
);
830 SetPageUptodate(page
);
831 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
832 *fsdata
= (void *)CONVERT_INLINE_DATA
;
835 up_read(&EXT4_I(inode
)->xattr_sem
);
844 * Prepare the write for the inline data.
845 * If the the data can be written into the inode, we just read
846 * the page and make it uptodate, and start the journal.
847 * Otherwise read the page, makes it dirty so that it can be
848 * handle in writepages(the i_disksize update is left to the
849 * normal ext4_da_write_end).
851 int ext4_da_write_inline_data_begin(struct address_space
*mapping
,
853 loff_t pos
, unsigned len
,
858 int ret
, inline_size
;
861 struct ext4_iloc iloc
;
864 ret
= ext4_get_inode_loc(inode
, &iloc
);
869 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, 1);
870 if (IS_ERR(handle
)) {
871 ret
= PTR_ERR(handle
);
875 inline_size
= ext4_get_max_inline_size(inode
);
878 if (inline_size
>= pos
+ len
) {
879 ret
= ext4_prepare_inline_data(handle
, inode
, pos
+ len
);
880 if (ret
&& ret
!= -ENOSPC
)
885 * We cannot recurse into the filesystem as the transaction
886 * is already started.
888 flags
|= AOP_FLAG_NOFS
;
890 if (ret
== -ENOSPC
) {
891 ret
= ext4_da_convert_inline_data_to_extent(mapping
,
895 ext4_journal_stop(handle
);
896 if (ret
== -ENOSPC
&&
897 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
903 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
909 down_read(&EXT4_I(inode
)->xattr_sem
);
910 if (!ext4_has_inline_data(inode
)) {
912 goto out_release_page
;
915 if (!PageUptodate(page
)) {
916 ret
= ext4_read_inline_page(inode
, page
);
918 goto out_release_page
;
921 up_read(&EXT4_I(inode
)->xattr_sem
);
926 up_read(&EXT4_I(inode
)->xattr_sem
);
930 ext4_journal_stop(handle
);
936 int ext4_da_write_inline_data_end(struct inode
*inode
, loff_t pos
,
937 unsigned len
, unsigned copied
,
940 int i_size_changed
= 0;
943 ret
= ext4_write_inline_data_end(inode
, pos
, len
, copied
, page
);
952 * No need to use i_size_read() here, the i_size
953 * cannot change under us because we hold i_mutex.
955 * But it's important to update i_size while still holding page lock:
956 * page writeout could otherwise come in and zero beyond i_size.
958 if (pos
+copied
> inode
->i_size
) {
959 i_size_write(inode
, pos
+copied
);
966 * Don't mark the inode dirty under page lock. First, it unnecessarily
967 * makes the holding time of page lock longer. Second, it forces lock
968 * ordering of page lock and transaction start for journaling
972 mark_inode_dirty(inode
);
977 #ifdef INLINE_DIR_DEBUG
978 void ext4_show_inline_dir(struct inode
*dir
, struct buffer_head
*bh
,
979 void *inline_start
, int inline_size
)
982 unsigned short de_len
;
983 struct ext4_dir_entry_2
*de
= inline_start
;
984 void *dlimit
= inline_start
+ inline_size
;
986 trace_printk("inode %lu\n", dir
->i_ino
);
988 while ((void *)de
< dlimit
) {
989 de_len
= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
990 trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
991 offset
, de_len
, de
->name_len
, de
->name
,
992 de
->name_len
, le32_to_cpu(de
->inode
));
993 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
994 inline_start
, inline_size
, offset
))
998 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
1002 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
1006 * Add a new entry into a inline dir.
1007 * It will return -ENOSPC if no space is available, and -EIO
1008 * and -EEXIST if directory entry already exists.
1010 static int ext4_add_dirent_to_inline(handle_t
*handle
,
1011 struct ext4_filename
*fname
,
1013 struct inode
*inode
,
1014 struct ext4_iloc
*iloc
,
1015 void *inline_start
, int inline_size
)
1018 struct ext4_dir_entry_2
*de
;
1020 err
= ext4_find_dest_de(dir
, inode
, iloc
->bh
, inline_start
,
1021 inline_size
, fname
, &de
);
1025 BUFFER_TRACE(iloc
->bh
, "get_write_access");
1026 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
1029 ext4_insert_dentry(inode
, de
, inline_size
, fname
);
1031 ext4_show_inline_dir(dir
, iloc
->bh
, inline_start
, inline_size
);
1034 * XXX shouldn't update any times until successful
1035 * completion of syscall, but too many callers depend
1038 * XXX similarly, too many callers depend on
1039 * ext4_new_inode() setting the times, but error
1040 * recovery deletes the inode, so the worst that can
1041 * happen is that the times are slightly out of date
1042 * and/or different from the directory change time.
1044 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
1045 ext4_update_dx_flag(dir
);
1046 inode_inc_iversion(dir
);
1050 static void *ext4_get_inline_xattr_pos(struct inode
*inode
,
1051 struct ext4_iloc
*iloc
)
1053 struct ext4_xattr_entry
*entry
;
1054 struct ext4_xattr_ibody_header
*header
;
1056 BUG_ON(!EXT4_I(inode
)->i_inline_off
);
1058 header
= IHDR(inode
, ext4_raw_inode(iloc
));
1059 entry
= (struct ext4_xattr_entry
*)((void *)ext4_raw_inode(iloc
) +
1060 EXT4_I(inode
)->i_inline_off
);
1062 return (void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
);
1065 /* Set the final de to cover the whole block. */
1066 static void ext4_update_final_de(void *de_buf
, int old_size
, int new_size
)
1068 struct ext4_dir_entry_2
*de
, *prev_de
;
1072 de
= (struct ext4_dir_entry_2
*)de_buf
;
1074 limit
= de_buf
+ old_size
;
1077 de_len
= ext4_rec_len_from_disk(de
->rec_len
, old_size
);
1079 de
= (struct ext4_dir_entry_2
*)de_buf
;
1080 } while (de_buf
< limit
);
1082 prev_de
->rec_len
= ext4_rec_len_to_disk(de_len
+ new_size
-
1083 old_size
, new_size
);
1085 /* this is just created, so create an empty entry. */
1087 de
->rec_len
= ext4_rec_len_to_disk(new_size
, new_size
);
1091 static int ext4_update_inline_dir(handle_t
*handle
, struct inode
*dir
,
1092 struct ext4_iloc
*iloc
)
1095 int old_size
= EXT4_I(dir
)->i_inline_size
- EXT4_MIN_INLINE_DATA_SIZE
;
1096 int new_size
= get_max_inline_xattr_value_size(dir
, iloc
);
1098 if (new_size
- old_size
<= EXT4_DIR_REC_LEN(1))
1101 ret
= ext4_update_inline_data(handle
, dir
,
1102 new_size
+ EXT4_MIN_INLINE_DATA_SIZE
);
1106 ext4_update_final_de(ext4_get_inline_xattr_pos(dir
, iloc
), old_size
,
1107 EXT4_I(dir
)->i_inline_size
-
1108 EXT4_MIN_INLINE_DATA_SIZE
);
1109 dir
->i_size
= EXT4_I(dir
)->i_disksize
= EXT4_I(dir
)->i_inline_size
;
1113 static void ext4_restore_inline_data(handle_t
*handle
, struct inode
*inode
,
1114 struct ext4_iloc
*iloc
,
1115 void *buf
, int inline_size
)
1117 ext4_create_inline_data(handle
, inode
, inline_size
);
1118 ext4_write_inline_data(inode
, iloc
, buf
, 0, inline_size
);
1119 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
1122 static int ext4_finish_convert_inline_dir(handle_t
*handle
,
1123 struct inode
*inode
,
1124 struct buffer_head
*dir_block
,
1128 int err
, csum_size
= 0, header_size
= 0;
1129 struct ext4_dir_entry_2
*de
;
1130 struct ext4_dir_entry_tail
*t
;
1131 void *target
= dir_block
->b_data
;
1134 * First create "." and ".." and then copy the dir information
1135 * back to the block.
1137 de
= (struct ext4_dir_entry_2
*)target
;
1138 de
= ext4_init_dot_dotdot(inode
, de
,
1139 inode
->i_sb
->s_blocksize
, csum_size
,
1140 le32_to_cpu(((struct ext4_dir_entry_2
*)buf
)->inode
), 1);
1141 header_size
= (void *)de
- target
;
1143 memcpy((void *)de
, buf
+ EXT4_INLINE_DOTDOT_SIZE
,
1144 inline_size
- EXT4_INLINE_DOTDOT_SIZE
);
1146 if (ext4_has_metadata_csum(inode
->i_sb
))
1147 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1149 inode
->i_size
= inode
->i_sb
->s_blocksize
;
1150 i_size_write(inode
, inode
->i_sb
->s_blocksize
);
1151 EXT4_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1152 ext4_update_final_de(dir_block
->b_data
,
1153 inline_size
- EXT4_INLINE_DOTDOT_SIZE
+ header_size
,
1154 inode
->i_sb
->s_blocksize
- csum_size
);
1157 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
,
1158 inode
->i_sb
->s_blocksize
);
1159 initialize_dirent_tail(t
, inode
->i_sb
->s_blocksize
);
1161 set_buffer_uptodate(dir_block
);
1162 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
1165 set_buffer_verified(dir_block
);
1166 return ext4_mark_inode_dirty(handle
, inode
);
1169 static int ext4_convert_inline_data_nolock(handle_t
*handle
,
1170 struct inode
*inode
,
1171 struct ext4_iloc
*iloc
)
1175 struct buffer_head
*data_bh
= NULL
;
1176 struct ext4_map_blocks map
;
1179 inline_size
= ext4_get_inline_size(inode
);
1180 buf
= kmalloc(inline_size
, GFP_NOFS
);
1186 error
= ext4_read_inline_data(inode
, buf
, inline_size
, iloc
);
1191 * Make sure the inline directory entries pass checks before we try to
1192 * convert them, so that we avoid touching stuff that needs fsck.
1194 if (S_ISDIR(inode
->i_mode
)) {
1195 error
= ext4_check_all_de(inode
, iloc
->bh
,
1196 buf
+ EXT4_INLINE_DOTDOT_SIZE
,
1197 inline_size
- EXT4_INLINE_DOTDOT_SIZE
);
1202 error
= ext4_destroy_inline_data_nolock(handle
, inode
);
1209 error
= ext4_map_blocks(handle
, inode
, &map
, EXT4_GET_BLOCKS_CREATE
);
1212 if (!(map
.m_flags
& EXT4_MAP_MAPPED
)) {
1217 data_bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
1223 lock_buffer(data_bh
);
1224 error
= ext4_journal_get_create_access(handle
, data_bh
);
1226 unlock_buffer(data_bh
);
1230 memset(data_bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
1232 if (!S_ISDIR(inode
->i_mode
)) {
1233 memcpy(data_bh
->b_data
, buf
, inline_size
);
1234 set_buffer_uptodate(data_bh
);
1235 error
= ext4_handle_dirty_metadata(handle
,
1238 error
= ext4_finish_convert_inline_dir(handle
, inode
, data_bh
,
1242 unlock_buffer(data_bh
);
1245 ext4_restore_inline_data(handle
, inode
, iloc
, buf
, inline_size
);
1254 * Try to add the new entry to the inline data.
1255 * If succeeds, return 0. If not, extended the inline dir and copied data to
1256 * the new created block.
1258 int ext4_try_add_inline_entry(handle_t
*handle
, struct ext4_filename
*fname
,
1259 struct inode
*dir
, struct inode
*inode
)
1261 int ret
, inline_size
, no_expand
;
1263 struct ext4_iloc iloc
;
1265 ret
= ext4_get_inode_loc(dir
, &iloc
);
1269 ext4_write_lock_xattr(dir
, &no_expand
);
1270 if (!ext4_has_inline_data(dir
))
1273 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1274 EXT4_INLINE_DOTDOT_SIZE
;
1275 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DOTDOT_SIZE
;
1277 ret
= ext4_add_dirent_to_inline(handle
, fname
, dir
, inode
, &iloc
,
1278 inline_start
, inline_size
);
1282 /* check whether it can be inserted to inline xattr space. */
1283 inline_size
= EXT4_I(dir
)->i_inline_size
-
1284 EXT4_MIN_INLINE_DATA_SIZE
;
1286 /* Try to use the xattr space.*/
1287 ret
= ext4_update_inline_dir(handle
, dir
, &iloc
);
1288 if (ret
&& ret
!= -ENOSPC
)
1291 inline_size
= EXT4_I(dir
)->i_inline_size
-
1292 EXT4_MIN_INLINE_DATA_SIZE
;
1296 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1298 ret
= ext4_add_dirent_to_inline(handle
, fname
, dir
,
1299 inode
, &iloc
, inline_start
,
1307 * The inline space is filled up, so create a new block for it.
1308 * As the extent tree will be created, we have to save the inline
1311 ret
= ext4_convert_inline_data_nolock(handle
, dir
, &iloc
);
1314 ext4_write_unlock_xattr(dir
, &no_expand
);
1315 ext4_mark_inode_dirty(handle
, dir
);
1321 * This function fills a red-black tree with information from an
1322 * inlined dir. It returns the number directory entries loaded
1323 * into the tree. If there is an error it is returned in err.
1325 int htree_inlinedir_to_tree(struct file
*dir_file
,
1326 struct inode
*dir
, ext4_lblk_t block
,
1327 struct dx_hash_info
*hinfo
,
1328 __u32 start_hash
, __u32 start_minor_hash
,
1329 int *has_inline_data
)
1331 int err
= 0, count
= 0;
1332 unsigned int parent_ino
;
1334 struct ext4_dir_entry_2
*de
;
1335 struct inode
*inode
= file_inode(dir_file
);
1336 int ret
, inline_size
= 0;
1337 struct ext4_iloc iloc
;
1338 void *dir_buf
= NULL
;
1339 struct ext4_dir_entry_2 fake
;
1340 struct fscrypt_str tmp_str
;
1342 ret
= ext4_get_inode_loc(inode
, &iloc
);
1346 down_read(&EXT4_I(inode
)->xattr_sem
);
1347 if (!ext4_has_inline_data(inode
)) {
1348 up_read(&EXT4_I(inode
)->xattr_sem
);
1349 *has_inline_data
= 0;
1353 inline_size
= ext4_get_inline_size(inode
);
1354 dir_buf
= kmalloc(inline_size
, GFP_NOFS
);
1357 up_read(&EXT4_I(inode
)->xattr_sem
);
1361 ret
= ext4_read_inline_data(inode
, dir_buf
, inline_size
, &iloc
);
1362 up_read(&EXT4_I(inode
)->xattr_sem
);
1367 parent_ino
= le32_to_cpu(((struct ext4_dir_entry_2
*)dir_buf
)->inode
);
1368 while (pos
< inline_size
) {
1370 * As inlined dir doesn't store any information about '.' and
1371 * only the inode number of '..' is stored, we have to handle
1375 fake
.inode
= cpu_to_le32(inode
->i_ino
);
1377 strcpy(fake
.name
, ".");
1378 fake
.rec_len
= ext4_rec_len_to_disk(
1379 EXT4_DIR_REC_LEN(fake
.name_len
),
1381 ext4_set_de_type(inode
->i_sb
, &fake
, S_IFDIR
);
1383 pos
= EXT4_INLINE_DOTDOT_OFFSET
;
1384 } else if (pos
== EXT4_INLINE_DOTDOT_OFFSET
) {
1385 fake
.inode
= cpu_to_le32(parent_ino
);
1387 strcpy(fake
.name
, "..");
1388 fake
.rec_len
= ext4_rec_len_to_disk(
1389 EXT4_DIR_REC_LEN(fake
.name_len
),
1391 ext4_set_de_type(inode
->i_sb
, &fake
, S_IFDIR
);
1393 pos
= EXT4_INLINE_DOTDOT_SIZE
;
1395 de
= (struct ext4_dir_entry_2
*)(dir_buf
+ pos
);
1396 pos
+= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
1397 if (ext4_check_dir_entry(inode
, dir_file
, de
,
1399 inline_size
, pos
)) {
1405 ext4fs_dirhash(de
->name
, de
->name_len
, hinfo
);
1406 if ((hinfo
->hash
< start_hash
) ||
1407 ((hinfo
->hash
== start_hash
) &&
1408 (hinfo
->minor_hash
< start_minor_hash
)))
1412 tmp_str
.name
= de
->name
;
1413 tmp_str
.len
= de
->name_len
;
1414 err
= ext4_htree_store_dirent(dir_file
, hinfo
->hash
,
1415 hinfo
->minor_hash
, de
, &tmp_str
);
1430 * So this function is called when the volume is mkfsed with
1431 * dir_index disabled. In order to keep f_pos persistent
1432 * after we convert from an inlined dir to a blocked based,
1433 * we just pretend that we are a normal dir and return the
1434 * offset as if '.' and '..' really take place.
1437 int ext4_read_inline_dir(struct file
*file
,
1438 struct dir_context
*ctx
,
1439 int *has_inline_data
)
1441 unsigned int offset
, parent_ino
;
1443 struct ext4_dir_entry_2
*de
;
1444 struct super_block
*sb
;
1445 struct inode
*inode
= file_inode(file
);
1446 int ret
, inline_size
= 0;
1447 struct ext4_iloc iloc
;
1448 void *dir_buf
= NULL
;
1449 int dotdot_offset
, dotdot_size
, extra_offset
, extra_size
;
1451 ret
= ext4_get_inode_loc(inode
, &iloc
);
1455 down_read(&EXT4_I(inode
)->xattr_sem
);
1456 if (!ext4_has_inline_data(inode
)) {
1457 up_read(&EXT4_I(inode
)->xattr_sem
);
1458 *has_inline_data
= 0;
1462 inline_size
= ext4_get_inline_size(inode
);
1463 dir_buf
= kmalloc(inline_size
, GFP_NOFS
);
1466 up_read(&EXT4_I(inode
)->xattr_sem
);
1470 ret
= ext4_read_inline_data(inode
, dir_buf
, inline_size
, &iloc
);
1471 up_read(&EXT4_I(inode
)->xattr_sem
);
1477 parent_ino
= le32_to_cpu(((struct ext4_dir_entry_2
*)dir_buf
)->inode
);
1481 * dotdot_offset and dotdot_size is the real offset and
1482 * size for ".." and "." if the dir is block based while
1483 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1484 * So we will use extra_offset and extra_size to indicate them
1485 * during the inline dir iteration.
1487 dotdot_offset
= EXT4_DIR_REC_LEN(1);
1488 dotdot_size
= dotdot_offset
+ EXT4_DIR_REC_LEN(2);
1489 extra_offset
= dotdot_size
- EXT4_INLINE_DOTDOT_SIZE
;
1490 extra_size
= extra_offset
+ inline_size
;
1493 * If the version has changed since the last call to
1494 * readdir(2), then we might be pointing to an invalid
1495 * dirent right now. Scan from the start of the inline
1498 if (inode_cmp_iversion(inode
, file
->f_version
)) {
1499 for (i
= 0; i
< extra_size
&& i
< offset
;) {
1501 * "." is with offset 0 and
1502 * ".." is dotdot_offset.
1507 } else if (i
== dotdot_offset
) {
1511 /* for other entry, the real offset in
1512 * the buf has to be tuned accordingly.
1514 de
= (struct ext4_dir_entry_2
*)
1515 (dir_buf
+ i
- extra_offset
);
1516 /* It's too expensive to do a full
1517 * dirent test each time round this
1518 * loop, but we do have to test at
1519 * least that it is non-zero. A
1520 * failure will be detected in the
1521 * dirent test below. */
1522 if (ext4_rec_len_from_disk(de
->rec_len
, extra_size
)
1523 < EXT4_DIR_REC_LEN(1))
1525 i
+= ext4_rec_len_from_disk(de
->rec_len
,
1530 file
->f_version
= inode_query_iversion(inode
);
1533 while (ctx
->pos
< extra_size
) {
1534 if (ctx
->pos
== 0) {
1535 if (!dir_emit(ctx
, ".", 1, inode
->i_ino
, DT_DIR
))
1537 ctx
->pos
= dotdot_offset
;
1541 if (ctx
->pos
== dotdot_offset
) {
1542 if (!dir_emit(ctx
, "..", 2, parent_ino
, DT_DIR
))
1544 ctx
->pos
= dotdot_size
;
1548 de
= (struct ext4_dir_entry_2
*)
1549 (dir_buf
+ ctx
->pos
- extra_offset
);
1550 if (ext4_check_dir_entry(inode
, file
, de
, iloc
.bh
, dir_buf
,
1551 extra_size
, ctx
->pos
))
1553 if (le32_to_cpu(de
->inode
)) {
1554 if (!dir_emit(ctx
, de
->name
, de
->name_len
,
1555 le32_to_cpu(de
->inode
),
1556 get_dtype(sb
, de
->file_type
)))
1559 ctx
->pos
+= ext4_rec_len_from_disk(de
->rec_len
, extra_size
);
1567 struct buffer_head
*ext4_get_first_inline_block(struct inode
*inode
,
1568 struct ext4_dir_entry_2
**parent_de
,
1571 struct ext4_iloc iloc
;
1573 *retval
= ext4_get_inode_loc(inode
, &iloc
);
1577 *parent_de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1583 * Try to create the inline data for the new dir.
1584 * If it succeeds, return 0, otherwise return the error.
1585 * In case of ENOSPC, the caller should create the normal disk layout dir.
1587 int ext4_try_create_inline_dir(handle_t
*handle
, struct inode
*parent
,
1588 struct inode
*inode
)
1590 int ret
, inline_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1591 struct ext4_iloc iloc
;
1592 struct ext4_dir_entry_2
*de
;
1594 ret
= ext4_get_inode_loc(inode
, &iloc
);
1598 ret
= ext4_prepare_inline_data(handle
, inode
, inline_size
);
1603 * For inline dir, we only save the inode information for the ".."
1604 * and create a fake dentry to cover the left space.
1606 de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1607 de
->inode
= cpu_to_le32(parent
->i_ino
);
1608 de
= (struct ext4_dir_entry_2
*)((void *)de
+ EXT4_INLINE_DOTDOT_SIZE
);
1610 de
->rec_len
= ext4_rec_len_to_disk(
1611 inline_size
- EXT4_INLINE_DOTDOT_SIZE
,
1613 set_nlink(inode
, 2);
1614 inode
->i_size
= EXT4_I(inode
)->i_disksize
= inline_size
;
1620 struct buffer_head
*ext4_find_inline_entry(struct inode
*dir
,
1621 struct ext4_filename
*fname
,
1622 struct ext4_dir_entry_2
**res_dir
,
1623 int *has_inline_data
)
1626 struct ext4_iloc iloc
;
1630 if (ext4_get_inode_loc(dir
, &iloc
))
1633 down_read(&EXT4_I(dir
)->xattr_sem
);
1634 if (!ext4_has_inline_data(dir
)) {
1635 *has_inline_data
= 0;
1639 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1640 EXT4_INLINE_DOTDOT_SIZE
;
1641 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DOTDOT_SIZE
;
1642 ret
= ext4_search_dir(iloc
.bh
, inline_start
, inline_size
,
1643 dir
, fname
, 0, res_dir
);
1649 if (ext4_get_inline_size(dir
) == EXT4_MIN_INLINE_DATA_SIZE
)
1652 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1653 inline_size
= ext4_get_inline_size(dir
) - EXT4_MIN_INLINE_DATA_SIZE
;
1655 ret
= ext4_search_dir(iloc
.bh
, inline_start
, inline_size
,
1656 dir
, fname
, 0, res_dir
);
1664 up_read(&EXT4_I(dir
)->xattr_sem
);
1668 int ext4_delete_inline_entry(handle_t
*handle
,
1670 struct ext4_dir_entry_2
*de_del
,
1671 struct buffer_head
*bh
,
1672 int *has_inline_data
)
1674 int err
, inline_size
, no_expand
;
1675 struct ext4_iloc iloc
;
1678 err
= ext4_get_inode_loc(dir
, &iloc
);
1682 ext4_write_lock_xattr(dir
, &no_expand
);
1683 if (!ext4_has_inline_data(dir
)) {
1684 *has_inline_data
= 0;
1688 if ((void *)de_del
- ((void *)ext4_raw_inode(&iloc
)->i_block
) <
1689 EXT4_MIN_INLINE_DATA_SIZE
) {
1690 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1691 EXT4_INLINE_DOTDOT_SIZE
;
1692 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
-
1693 EXT4_INLINE_DOTDOT_SIZE
;
1695 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1696 inline_size
= ext4_get_inline_size(dir
) -
1697 EXT4_MIN_INLINE_DATA_SIZE
;
1700 BUFFER_TRACE(bh
, "get_write_access");
1701 err
= ext4_journal_get_write_access(handle
, bh
);
1705 err
= ext4_generic_delete_entry(handle
, dir
, de_del
, bh
,
1706 inline_start
, inline_size
, 0);
1710 ext4_show_inline_dir(dir
, iloc
.bh
, inline_start
, inline_size
);
1712 ext4_write_unlock_xattr(dir
, &no_expand
);
1713 if (likely(err
== 0))
1714 err
= ext4_mark_inode_dirty(handle
, dir
);
1717 ext4_std_error(dir
->i_sb
, err
);
1722 * Get the inline dentry at offset.
1724 static inline struct ext4_dir_entry_2
*
1725 ext4_get_inline_entry(struct inode
*inode
,
1726 struct ext4_iloc
*iloc
,
1727 unsigned int offset
,
1728 void **inline_start
,
1733 BUG_ON(offset
> ext4_get_inline_size(inode
));
1735 if (offset
< EXT4_MIN_INLINE_DATA_SIZE
) {
1736 inline_pos
= (void *)ext4_raw_inode(iloc
)->i_block
;
1737 *inline_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1739 inline_pos
= ext4_get_inline_xattr_pos(inode
, iloc
);
1740 offset
-= EXT4_MIN_INLINE_DATA_SIZE
;
1741 *inline_size
= ext4_get_inline_size(inode
) -
1742 EXT4_MIN_INLINE_DATA_SIZE
;
1746 *inline_start
= inline_pos
;
1747 return (struct ext4_dir_entry_2
*)(inline_pos
+ offset
);
1750 bool empty_inline_dir(struct inode
*dir
, int *has_inline_data
)
1752 int err
, inline_size
;
1753 struct ext4_iloc iloc
;
1755 unsigned int offset
;
1756 struct ext4_dir_entry_2
*de
;
1759 err
= ext4_get_inode_loc(dir
, &iloc
);
1761 EXT4_ERROR_INODE(dir
, "error %d getting inode %lu block",
1766 down_read(&EXT4_I(dir
)->xattr_sem
);
1767 if (!ext4_has_inline_data(dir
)) {
1768 *has_inline_data
= 0;
1772 de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1773 if (!le32_to_cpu(de
->inode
)) {
1774 ext4_warning(dir
->i_sb
,
1775 "bad inline directory (dir #%lu) - no `..'",
1781 offset
= EXT4_INLINE_DOTDOT_SIZE
;
1782 while (offset
< dir
->i_size
) {
1783 de
= ext4_get_inline_entry(dir
, &iloc
, offset
,
1784 &inline_pos
, &inline_size
);
1785 if (ext4_check_dir_entry(dir
, NULL
, de
,
1786 iloc
.bh
, inline_pos
,
1787 inline_size
, offset
)) {
1788 ext4_warning(dir
->i_sb
,
1789 "bad inline directory (dir #%lu) - "
1790 "inode %u, rec_len %u, name_len %d"
1792 dir
->i_ino
, le32_to_cpu(de
->inode
),
1793 le16_to_cpu(de
->rec_len
), de
->name_len
,
1798 if (le32_to_cpu(de
->inode
)) {
1802 offset
+= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
1806 up_read(&EXT4_I(dir
)->xattr_sem
);
1811 int ext4_destroy_inline_data(handle_t
*handle
, struct inode
*inode
)
1815 ext4_write_lock_xattr(inode
, &no_expand
);
1816 ret
= ext4_destroy_inline_data_nolock(handle
, inode
);
1817 ext4_write_unlock_xattr(inode
, &no_expand
);
1822 int ext4_inline_data_iomap(struct inode
*inode
, struct iomap
*iomap
)
1825 int error
= -EAGAIN
;
1826 struct ext4_iloc iloc
;
1828 down_read(&EXT4_I(inode
)->xattr_sem
);
1829 if (!ext4_has_inline_data(inode
))
1832 error
= ext4_get_inode_loc(inode
, &iloc
);
1836 addr
= (__u64
)iloc
.bh
->b_blocknr
<< inode
->i_sb
->s_blocksize_bits
;
1837 addr
+= (char *)ext4_raw_inode(&iloc
) - iloc
.bh
->b_data
;
1838 addr
+= offsetof(struct ext4_inode
, i_block
);
1844 iomap
->length
= min_t(loff_t
, ext4_get_inline_size(inode
),
1845 i_size_read(inode
));
1847 iomap
->flags
= IOMAP_F_DATA_INLINE
;
1850 up_read(&EXT4_I(inode
)->xattr_sem
);
1854 int ext4_inline_data_fiemap(struct inode
*inode
,
1855 struct fiemap_extent_info
*fieinfo
,
1856 int *has_inline
, __u64 start
, __u64 len
)
1860 __u32 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
|
1863 struct ext4_iloc iloc
;
1865 down_read(&EXT4_I(inode
)->xattr_sem
);
1866 if (!ext4_has_inline_data(inode
)) {
1870 inline_len
= min_t(size_t, ext4_get_inline_size(inode
),
1871 i_size_read(inode
));
1872 if (start
>= inline_len
)
1874 if (start
+ len
< inline_len
)
1875 inline_len
= start
+ len
;
1876 inline_len
-= start
;
1878 error
= ext4_get_inode_loc(inode
, &iloc
);
1882 physical
= (__u64
)iloc
.bh
->b_blocknr
<< inode
->i_sb
->s_blocksize_bits
;
1883 physical
+= (char *)ext4_raw_inode(&iloc
) - iloc
.bh
->b_data
;
1884 physical
+= offsetof(struct ext4_inode
, i_block
);
1887 error
= fiemap_fill_next_extent(fieinfo
, start
, physical
,
1891 up_read(&EXT4_I(inode
)->xattr_sem
);
1892 return (error
< 0 ? error
: 0);
1896 * Called during xattr set, and if we can sparse space 'needed',
1897 * just create the extent tree evict the data to the outer block.
1899 * We use jbd2 instead of page cache to move data to the 1st block
1900 * so that the whole transaction can be committed as a whole and
1901 * the data isn't lost because of the delayed page cache write.
1903 int ext4_try_to_evict_inline_data(handle_t
*handle
,
1904 struct inode
*inode
,
1908 struct ext4_xattr_entry
*entry
;
1909 struct ext4_inode
*raw_inode
;
1910 struct ext4_iloc iloc
;
1912 error
= ext4_get_inode_loc(inode
, &iloc
);
1916 raw_inode
= ext4_raw_inode(&iloc
);
1917 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
1918 EXT4_I(inode
)->i_inline_off
);
1919 if (EXT4_XATTR_LEN(entry
->e_name_len
) +
1920 EXT4_XATTR_SIZE(le32_to_cpu(entry
->e_value_size
)) < needed
) {
1925 error
= ext4_convert_inline_data_nolock(handle
, inode
, &iloc
);
1931 int ext4_inline_data_truncate(struct inode
*inode
, int *has_inline
)
1934 int inline_size
, value_len
, needed_blocks
, no_expand
, err
= 0;
1937 struct ext4_xattr_ibody_find is
= {
1938 .s
= { .not_found
= -ENODATA
, },
1940 struct ext4_xattr_info i
= {
1941 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
1942 .name
= EXT4_XATTR_SYSTEM_DATA
,
1946 needed_blocks
= ext4_writepage_trans_blocks(inode
);
1947 handle
= ext4_journal_start(inode
, EXT4_HT_INODE
, needed_blocks
);
1949 return PTR_ERR(handle
);
1951 ext4_write_lock_xattr(inode
, &no_expand
);
1952 if (!ext4_has_inline_data(inode
)) {
1954 ext4_journal_stop(handle
);
1958 if ((err
= ext4_orphan_add(handle
, inode
)) != 0)
1961 if ((err
= ext4_get_inode_loc(inode
, &is
.iloc
)) != 0)
1964 down_write(&EXT4_I(inode
)->i_data_sem
);
1965 i_size
= inode
->i_size
;
1966 inline_size
= ext4_get_inline_size(inode
);
1967 EXT4_I(inode
)->i_disksize
= i_size
;
1969 if (i_size
< inline_size
) {
1970 /* Clear the content in the xattr space. */
1971 if (inline_size
> EXT4_MIN_INLINE_DATA_SIZE
) {
1972 if ((err
= ext4_xattr_ibody_find(inode
, &i
, &is
)) != 0)
1975 BUG_ON(is
.s
.not_found
);
1977 value_len
= le32_to_cpu(is
.s
.here
->e_value_size
);
1978 value
= kmalloc(value_len
, GFP_NOFS
);
1984 err
= ext4_xattr_ibody_get(inode
, i
.name_index
,
1985 i
.name
, value
, value_len
);
1990 i
.value_len
= i_size
> EXT4_MIN_INLINE_DATA_SIZE
?
1991 i_size
- EXT4_MIN_INLINE_DATA_SIZE
: 0;
1992 err
= ext4_xattr_ibody_inline_set(handle
, inode
,
1998 /* Clear the content within i_blocks. */
1999 if (i_size
< EXT4_MIN_INLINE_DATA_SIZE
) {
2000 void *p
= (void *) ext4_raw_inode(&is
.iloc
)->i_block
;
2001 memset(p
+ i_size
, 0,
2002 EXT4_MIN_INLINE_DATA_SIZE
- i_size
);
2005 EXT4_I(inode
)->i_inline_size
= i_size
<
2006 EXT4_MIN_INLINE_DATA_SIZE
?
2007 EXT4_MIN_INLINE_DATA_SIZE
: i_size
;
2011 up_write(&EXT4_I(inode
)->i_data_sem
);
2014 ext4_write_unlock_xattr(inode
, &no_expand
);
2017 ext4_orphan_del(handle
, inode
);
2020 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
2021 err
= ext4_mark_inode_dirty(handle
, inode
);
2023 ext4_handle_sync(handle
);
2025 ext4_journal_stop(handle
);
2029 int ext4_convert_inline_data(struct inode
*inode
)
2031 int error
, needed_blocks
, no_expand
;
2033 struct ext4_iloc iloc
;
2035 if (!ext4_has_inline_data(inode
)) {
2036 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
2040 needed_blocks
= ext4_writepage_trans_blocks(inode
);
2043 error
= ext4_get_inode_loc(inode
, &iloc
);
2047 handle
= ext4_journal_start(inode
, EXT4_HT_WRITE_PAGE
, needed_blocks
);
2048 if (IS_ERR(handle
)) {
2049 error
= PTR_ERR(handle
);
2053 ext4_write_lock_xattr(inode
, &no_expand
);
2054 if (ext4_has_inline_data(inode
))
2055 error
= ext4_convert_inline_data_nolock(handle
, inode
, &iloc
);
2056 ext4_write_unlock_xattr(inode
, &no_expand
);
2057 ext4_journal_stop(handle
);