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.
14 #include "ext4_jbd2.h"
18 #include <linux/fiemap.h>
20 #define EXT4_XATTR_SYSTEM_DATA "data"
21 #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
22 #define EXT4_INLINE_DOTDOT_SIZE 4
24 int ext4_get_inline_size(struct inode
*inode
)
26 if (EXT4_I(inode
)->i_inline_off
)
27 return EXT4_I(inode
)->i_inline_size
;
32 static int get_max_inline_xattr_value_size(struct inode
*inode
,
33 struct ext4_iloc
*iloc
)
35 struct ext4_xattr_ibody_header
*header
;
36 struct ext4_xattr_entry
*entry
;
37 struct ext4_inode
*raw_inode
;
40 min_offs
= EXT4_SB(inode
->i_sb
)->s_inode_size
-
41 EXT4_GOOD_OLD_INODE_SIZE
-
42 EXT4_I(inode
)->i_extra_isize
-
43 sizeof(struct ext4_xattr_ibody_header
);
46 * We need to subtract another sizeof(__u32) since an in-inode xattr
47 * needs an empty 4 bytes to indicate the gap between the xattr entry
48 * and the name/value pair.
50 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
51 return EXT4_XATTR_SIZE(min_offs
-
52 EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA
)) -
53 EXT4_XATTR_ROUND
- sizeof(__u32
));
55 raw_inode
= ext4_raw_inode(iloc
);
56 header
= IHDR(inode
, raw_inode
);
57 entry
= IFIRST(header
);
59 /* Compute min_offs. */
60 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
61 if (!entry
->e_value_block
&& entry
->e_value_size
) {
62 size_t offs
= le16_to_cpu(entry
->e_value_offs
);
68 ((void *)entry
- (void *)IFIRST(header
)) - sizeof(__u32
);
70 if (EXT4_I(inode
)->i_inline_off
) {
71 entry
= (struct ext4_xattr_entry
*)
72 ((void *)raw_inode
+ EXT4_I(inode
)->i_inline_off
);
74 free
+= le32_to_cpu(entry
->e_value_size
);
78 free
-= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA
));
80 if (free
> EXT4_XATTR_ROUND
)
81 free
= EXT4_XATTR_SIZE(free
- EXT4_XATTR_ROUND
);
90 * Get the maximum size we now can store in an inode.
91 * If we can't find the space for a xattr entry, don't use the space
92 * of the extents since we have no space to indicate the inline data.
94 int ext4_get_max_inline_size(struct inode
*inode
)
96 int error
, max_inline_size
;
97 struct ext4_iloc iloc
;
99 if (EXT4_I(inode
)->i_extra_isize
== 0)
102 error
= ext4_get_inode_loc(inode
, &iloc
);
104 ext4_error_inode(inode
, __func__
, __LINE__
, 0,
105 "can't get inode location %lu",
110 down_read(&EXT4_I(inode
)->xattr_sem
);
111 max_inline_size
= get_max_inline_xattr_value_size(inode
, &iloc
);
112 up_read(&EXT4_I(inode
)->xattr_sem
);
116 if (!max_inline_size
)
119 return max_inline_size
+ EXT4_MIN_INLINE_DATA_SIZE
;
122 int ext4_has_inline_data(struct inode
*inode
)
124 return ext4_test_inode_flag(inode
, EXT4_INODE_INLINE_DATA
) &&
125 EXT4_I(inode
)->i_inline_off
;
129 * this function does not take xattr_sem, which is OK because it is
130 * currently only used in a code path coming form ext4_iget, before
131 * the new inode has been unlocked
133 int ext4_find_inline_data_nolock(struct inode
*inode
)
135 struct ext4_xattr_ibody_find is
= {
136 .s
= { .not_found
= -ENODATA
, },
138 struct ext4_xattr_info i
= {
139 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
140 .name
= EXT4_XATTR_SYSTEM_DATA
,
144 if (EXT4_I(inode
)->i_extra_isize
== 0)
147 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
151 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
155 if (!is
.s
.not_found
) {
156 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
157 (void *)ext4_raw_inode(&is
.iloc
));
158 EXT4_I(inode
)->i_inline_size
= EXT4_MIN_INLINE_DATA_SIZE
+
159 le32_to_cpu(is
.s
.here
->e_value_size
);
160 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
167 static int ext4_read_inline_data(struct inode
*inode
, void *buffer
,
169 struct ext4_iloc
*iloc
)
171 struct ext4_xattr_entry
*entry
;
172 struct ext4_xattr_ibody_header
*header
;
174 struct ext4_inode
*raw_inode
;
179 BUG_ON(len
> EXT4_I(inode
)->i_inline_size
);
181 cp_len
= len
< EXT4_MIN_INLINE_DATA_SIZE
?
182 len
: EXT4_MIN_INLINE_DATA_SIZE
;
184 raw_inode
= ext4_raw_inode(iloc
);
185 memcpy(buffer
, (void *)(raw_inode
->i_block
), cp_len
);
193 header
= IHDR(inode
, raw_inode
);
194 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
195 EXT4_I(inode
)->i_inline_off
);
196 len
= min_t(unsigned int, len
,
197 (unsigned int)le32_to_cpu(entry
->e_value_size
));
200 (void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
), len
);
208 * write the buffer to the inline inode.
209 * If 'create' is set, we don't need to do the extra copy in the xattr
210 * value since it is already handled by ext4_xattr_ibody_inline_set.
211 * That saves us one memcpy.
213 void ext4_write_inline_data(struct inode
*inode
, struct ext4_iloc
*iloc
,
214 void *buffer
, loff_t pos
, unsigned int len
)
216 struct ext4_xattr_entry
*entry
;
217 struct ext4_xattr_ibody_header
*header
;
218 struct ext4_inode
*raw_inode
;
221 BUG_ON(!EXT4_I(inode
)->i_inline_off
);
222 BUG_ON(pos
+ len
> EXT4_I(inode
)->i_inline_size
);
224 raw_inode
= ext4_raw_inode(iloc
);
227 if (pos
< EXT4_MIN_INLINE_DATA_SIZE
) {
228 cp_len
= pos
+ len
> EXT4_MIN_INLINE_DATA_SIZE
?
229 EXT4_MIN_INLINE_DATA_SIZE
- pos
: len
;
230 memcpy((void *)raw_inode
->i_block
+ pos
, buffer
, cp_len
);
240 pos
-= EXT4_MIN_INLINE_DATA_SIZE
;
241 header
= IHDR(inode
, raw_inode
);
242 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
243 EXT4_I(inode
)->i_inline_off
);
245 memcpy((void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
) + pos
,
249 static int ext4_create_inline_data(handle_t
*handle
,
250 struct inode
*inode
, unsigned len
)
254 struct ext4_xattr_ibody_find is
= {
255 .s
= { .not_found
= -ENODATA
, },
257 struct ext4_xattr_info i
= {
258 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
259 .name
= EXT4_XATTR_SYSTEM_DATA
,
262 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
266 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
270 if (len
> EXT4_MIN_INLINE_DATA_SIZE
) {
271 value
= EXT4_ZERO_XATTR_VALUE
;
272 len
-= EXT4_MIN_INLINE_DATA_SIZE
;
278 /* Insert the the xttr entry. */
282 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
286 BUG_ON(!is
.s
.not_found
);
288 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
290 if (error
== -ENOSPC
)
291 ext4_clear_inode_state(inode
,
292 EXT4_STATE_MAY_INLINE_DATA
);
296 memset((void *)ext4_raw_inode(&is
.iloc
)->i_block
,
297 0, EXT4_MIN_INLINE_DATA_SIZE
);
299 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
300 (void *)ext4_raw_inode(&is
.iloc
));
301 EXT4_I(inode
)->i_inline_size
= len
+ EXT4_MIN_INLINE_DATA_SIZE
;
302 ext4_clear_inode_flag(inode
, EXT4_INODE_EXTENTS
);
303 ext4_set_inode_flag(inode
, EXT4_INODE_INLINE_DATA
);
305 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
312 static int ext4_update_inline_data(handle_t
*handle
, struct inode
*inode
,
317 struct ext4_xattr_ibody_find is
= {
318 .s
= { .not_found
= -ENODATA
, },
320 struct ext4_xattr_info i
= {
321 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
322 .name
= EXT4_XATTR_SYSTEM_DATA
,
325 /* If the old space is ok, write the data directly. */
326 if (len
<= EXT4_I(inode
)->i_inline_size
)
329 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
333 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
337 BUG_ON(is
.s
.not_found
);
339 len
-= EXT4_MIN_INLINE_DATA_SIZE
;
340 value
= kzalloc(len
, GFP_NOFS
);
344 error
= ext4_xattr_ibody_get(inode
, i
.name_index
, i
.name
,
346 if (error
== -ENODATA
)
349 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
353 /* Update the xttr entry. */
357 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
361 EXT4_I(inode
)->i_inline_off
= (u16
)((void *)is
.s
.here
-
362 (void *)ext4_raw_inode(&is
.iloc
));
363 EXT4_I(inode
)->i_inline_size
= EXT4_MIN_INLINE_DATA_SIZE
+
364 le32_to_cpu(is
.s
.here
->e_value_size
);
365 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
367 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
375 int ext4_prepare_inline_data(handle_t
*handle
, struct inode
*inode
,
379 struct ext4_inode_info
*ei
= EXT4_I(inode
);
381 if (!ext4_test_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
))
384 size
= ext4_get_max_inline_size(inode
);
388 down_write(&EXT4_I(inode
)->xattr_sem
);
390 if (ei
->i_inline_off
)
391 ret
= ext4_update_inline_data(handle
, inode
, len
);
393 ret
= ext4_create_inline_data(handle
, inode
, len
);
395 up_write(&EXT4_I(inode
)->xattr_sem
);
400 static int ext4_destroy_inline_data_nolock(handle_t
*handle
,
403 struct ext4_inode_info
*ei
= EXT4_I(inode
);
404 struct ext4_xattr_ibody_find is
= {
405 .s
= { .not_found
= 0, },
407 struct ext4_xattr_info i
= {
408 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
409 .name
= EXT4_XATTR_SYSTEM_DATA
,
415 if (!ei
->i_inline_off
)
418 error
= ext4_get_inode_loc(inode
, &is
.iloc
);
422 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
426 error
= ext4_journal_get_write_access(handle
, is
.iloc
.bh
);
430 error
= ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
);
434 memset((void *)ext4_raw_inode(&is
.iloc
)->i_block
,
435 0, EXT4_MIN_INLINE_DATA_SIZE
);
437 if (EXT4_HAS_INCOMPAT_FEATURE(inode
->i_sb
,
438 EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
439 if (S_ISDIR(inode
->i_mode
) ||
440 S_ISREG(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) {
441 ext4_set_inode_flag(inode
, EXT4_INODE_EXTENTS
);
442 ext4_ext_tree_init(handle
, inode
);
445 ext4_clear_inode_flag(inode
, EXT4_INODE_INLINE_DATA
);
448 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
450 EXT4_I(inode
)->i_inline_off
= 0;
451 EXT4_I(inode
)->i_inline_size
= 0;
452 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
455 if (error
== -ENODATA
)
460 static int ext4_read_inline_page(struct inode
*inode
, struct page
*page
)
465 struct ext4_iloc iloc
;
467 BUG_ON(!PageLocked(page
));
468 BUG_ON(!ext4_has_inline_data(inode
));
471 if (!EXT4_I(inode
)->i_inline_off
) {
472 ext4_warning(inode
->i_sb
, "inode %lu doesn't have inline data.",
477 ret
= ext4_get_inode_loc(inode
, &iloc
);
481 len
= min_t(size_t, ext4_get_inline_size(inode
), i_size_read(inode
));
482 kaddr
= kmap_atomic(page
);
483 ret
= ext4_read_inline_data(inode
, kaddr
, len
, &iloc
);
484 flush_dcache_page(page
);
485 kunmap_atomic(kaddr
);
486 zero_user_segment(page
, len
, PAGE_CACHE_SIZE
);
487 SetPageUptodate(page
);
494 int ext4_readpage_inline(struct inode
*inode
, struct page
*page
)
498 down_read(&EXT4_I(inode
)->xattr_sem
);
499 if (!ext4_has_inline_data(inode
)) {
500 up_read(&EXT4_I(inode
)->xattr_sem
);
505 * Current inline data can only exist in the 1st page,
506 * So for all the other pages, just set them uptodate.
509 ret
= ext4_read_inline_page(inode
, page
);
510 else if (!PageUptodate(page
)) {
511 zero_user_segment(page
, 0, PAGE_CACHE_SIZE
);
512 SetPageUptodate(page
);
515 up_read(&EXT4_I(inode
)->xattr_sem
);
518 return ret
>= 0 ? 0 : ret
;
521 static int ext4_convert_inline_data_to_extent(struct address_space
*mapping
,
525 int ret
, needed_blocks
;
526 handle_t
*handle
= NULL
;
527 int retries
= 0, sem_held
= 0;
528 struct page
*page
= NULL
;
530 struct ext4_iloc iloc
;
532 if (!ext4_has_inline_data(inode
)) {
534 * clear the flag so that no new write
535 * will trap here again.
537 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
541 needed_blocks
= ext4_writepage_trans_blocks(inode
);
543 ret
= ext4_get_inode_loc(inode
, &iloc
);
548 handle
= ext4_journal_start(inode
, needed_blocks
);
549 if (IS_ERR(handle
)) {
550 ret
= PTR_ERR(handle
);
555 /* We cannot recurse into the filesystem as the transaction is already
557 flags
|= AOP_FLAG_NOFS
;
559 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
565 down_write(&EXT4_I(inode
)->xattr_sem
);
567 /* If some one has already done this for us, just exit. */
568 if (!ext4_has_inline_data(inode
)) {
574 to
= ext4_get_inline_size(inode
);
575 if (!PageUptodate(page
)) {
576 ret
= ext4_read_inline_page(inode
, page
);
581 ret
= ext4_destroy_inline_data_nolock(handle
, inode
);
585 if (ext4_should_dioread_nolock(inode
))
586 ret
= __block_write_begin(page
, from
, to
, ext4_get_block_write
);
588 ret
= __block_write_begin(page
, from
, to
, ext4_get_block
);
590 if (!ret
&& ext4_should_journal_data(inode
)) {
591 ret
= ext4_walk_page_buffers(handle
, page_buffers(page
),
593 do_journal_get_write_access
);
598 page_cache_release(page
);
599 ext4_orphan_add(handle
, inode
);
600 up_write(&EXT4_I(inode
)->xattr_sem
);
602 ext4_journal_stop(handle
);
604 ext4_truncate_failed_write(inode
);
606 * If truncate failed early the inode might
607 * still be on the orphan list; we need to
608 * make sure the inode is removed from the
609 * orphan list in that case.
612 ext4_orphan_del(NULL
, inode
);
615 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
618 block_commit_write(page
, from
, to
);
622 page_cache_release(page
);
625 up_write(&EXT4_I(inode
)->xattr_sem
);
627 ext4_journal_stop(handle
);
633 * Try to write data in the inode.
634 * If the inode has inline data, check whether the new write can be
635 * in the inode also. If not, create the page the handle, move the data
636 * to the page make it update and let the later codes create extent for it.
638 int ext4_try_to_write_inline_data(struct address_space
*mapping
,
640 loff_t pos
, unsigned len
,
647 struct ext4_iloc iloc
;
649 if (pos
+ len
> ext4_get_max_inline_size(inode
))
652 ret
= ext4_get_inode_loc(inode
, &iloc
);
657 * The possible write could happen in the inode,
658 * so try to reserve the space in inode first.
660 handle
= ext4_journal_start(inode
, 1);
661 if (IS_ERR(handle
)) {
662 ret
= PTR_ERR(handle
);
667 ret
= ext4_prepare_inline_data(handle
, inode
, pos
+ len
);
668 if (ret
&& ret
!= -ENOSPC
)
671 /* We don't have space in inline inode, so convert it to extent. */
672 if (ret
== -ENOSPC
) {
673 ext4_journal_stop(handle
);
678 flags
|= AOP_FLAG_NOFS
;
680 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
687 down_read(&EXT4_I(inode
)->xattr_sem
);
688 if (!ext4_has_inline_data(inode
)) {
691 page_cache_release(page
);
695 if (!PageUptodate(page
)) {
696 ret
= ext4_read_inline_page(inode
, page
);
704 up_read(&EXT4_I(inode
)->xattr_sem
);
707 ext4_journal_stop(handle
);
711 return ext4_convert_inline_data_to_extent(mapping
,
715 int ext4_write_inline_data_end(struct inode
*inode
, loff_t pos
, unsigned len
,
716 unsigned copied
, struct page
*page
)
720 struct ext4_iloc iloc
;
722 if (unlikely(copied
< len
)) {
723 if (!PageUptodate(page
)) {
729 ret
= ext4_get_inode_loc(inode
, &iloc
);
731 ext4_std_error(inode
->i_sb
, ret
);
736 down_write(&EXT4_I(inode
)->xattr_sem
);
737 BUG_ON(!ext4_has_inline_data(inode
));
739 kaddr
= kmap_atomic(page
);
740 ext4_write_inline_data(inode
, &iloc
, kaddr
, pos
, len
);
741 kunmap_atomic(kaddr
);
742 SetPageUptodate(page
);
743 /* clear page dirty so that writepages wouldn't work for us. */
744 ClearPageDirty(page
);
746 up_write(&EXT4_I(inode
)->xattr_sem
);
753 ext4_journalled_write_inline_data(struct inode
*inode
,
759 struct ext4_iloc iloc
;
761 ret
= ext4_get_inode_loc(inode
, &iloc
);
763 ext4_std_error(inode
->i_sb
, ret
);
767 down_write(&EXT4_I(inode
)->xattr_sem
);
768 kaddr
= kmap_atomic(page
);
769 ext4_write_inline_data(inode
, &iloc
, kaddr
, 0, len
);
770 kunmap_atomic(kaddr
);
771 up_write(&EXT4_I(inode
)->xattr_sem
);
777 * Try to make the page cache and handle ready for the inline data case.
778 * We can call this function in 2 cases:
779 * 1. The inode is created and the first write exceeds inline size. We can
780 * clear the inode state safely.
781 * 2. The inode has inline data, then we need to read the data, make it
782 * update and dirty so that ext4_da_writepages can handle it. We don't
783 * need to start the journal since the file's metatdata isn't changed now.
785 static int ext4_da_convert_inline_data_to_extent(struct address_space
*mapping
,
790 int ret
= 0, inline_size
;
793 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
797 down_read(&EXT4_I(inode
)->xattr_sem
);
798 if (!ext4_has_inline_data(inode
)) {
799 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
803 inline_size
= ext4_get_inline_size(inode
);
805 if (!PageUptodate(page
)) {
806 ret
= ext4_read_inline_page(inode
, page
);
811 ret
= __block_write_begin(page
, 0, inline_size
,
812 ext4_da_get_block_prep
);
814 ext4_truncate_failed_write(inode
);
819 SetPageUptodate(page
);
820 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
821 *fsdata
= (void *)CONVERT_INLINE_DATA
;
824 up_read(&EXT4_I(inode
)->xattr_sem
);
827 page_cache_release(page
);
833 * Prepare the write for the inline data.
834 * If the the data can be written into the inode, we just read
835 * the page and make it uptodate, and start the journal.
836 * Otherwise read the page, makes it dirty so that it can be
837 * handle in writepages(the i_disksize update is left to the
838 * normal ext4_da_write_end).
840 int ext4_da_write_inline_data_begin(struct address_space
*mapping
,
842 loff_t pos
, unsigned len
,
847 int ret
, inline_size
;
850 struct ext4_iloc iloc
;
852 ret
= ext4_get_inode_loc(inode
, &iloc
);
856 handle
= ext4_journal_start(inode
, 1);
857 if (IS_ERR(handle
)) {
858 ret
= PTR_ERR(handle
);
863 inline_size
= ext4_get_max_inline_size(inode
);
866 if (inline_size
>= pos
+ len
) {
867 ret
= ext4_prepare_inline_data(handle
, inode
, pos
+ len
);
868 if (ret
&& ret
!= -ENOSPC
)
872 if (ret
== -ENOSPC
) {
873 ret
= ext4_da_convert_inline_data_to_extent(mapping
,
881 * We cannot recurse into the filesystem as the transaction
882 * is already started.
884 flags
|= AOP_FLAG_NOFS
;
886 page
= grab_cache_page_write_begin(mapping
, 0, flags
);
892 down_read(&EXT4_I(inode
)->xattr_sem
);
893 if (!ext4_has_inline_data(inode
)) {
895 goto out_release_page
;
898 if (!PageUptodate(page
)) {
899 ret
= ext4_read_inline_page(inode
, page
);
901 goto out_release_page
;
904 up_read(&EXT4_I(inode
)->xattr_sem
);
910 up_read(&EXT4_I(inode
)->xattr_sem
);
912 page_cache_release(page
);
915 ext4_journal_stop(handle
);
920 int ext4_da_write_inline_data_end(struct inode
*inode
, loff_t pos
,
921 unsigned len
, unsigned copied
,
924 int i_size_changed
= 0;
926 copied
= ext4_write_inline_data_end(inode
, pos
, len
, copied
, page
);
929 * No need to use i_size_read() here, the i_size
930 * cannot change under us because we hold i_mutex.
932 * But it's important to update i_size while still holding page lock:
933 * page writeout could otherwise come in and zero beyond i_size.
935 if (pos
+copied
> inode
->i_size
) {
936 i_size_write(inode
, pos
+copied
);
940 page_cache_release(page
);
943 * Don't mark the inode dirty under page lock. First, it unnecessarily
944 * makes the holding time of page lock longer. Second, it forces lock
945 * ordering of page lock and transaction start for journaling
949 mark_inode_dirty(inode
);
954 #ifdef INLINE_DIR_DEBUG
955 void ext4_show_inline_dir(struct inode
*dir
, struct buffer_head
*bh
,
956 void *inline_start
, int inline_size
)
959 unsigned short de_len
;
960 struct ext4_dir_entry_2
*de
= inline_start
;
961 void *dlimit
= inline_start
+ inline_size
;
963 trace_printk("inode %lu\n", dir
->i_ino
);
965 while ((void *)de
< dlimit
) {
966 de_len
= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
967 trace_printk("de: off %u rlen %u name %*.s nlen %u ino %u\n",
968 offset
, de_len
, de
->name_len
, de
->name
,
969 de
->name_len
, le32_to_cpu(de
->inode
));
970 if (ext4_check_dir_entry(dir
, NULL
, de
, bh
,
971 inline_start
, inline_size
, offset
))
975 de
= (struct ext4_dir_entry_2
*) ((char *) de
+ de_len
);
979 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
983 * Add a new entry into a inline dir.
984 * It will return -ENOSPC if no space is available, and -EIO
985 * and -EEXIST if directory entry already exists.
987 static int ext4_add_dirent_to_inline(handle_t
*handle
,
988 struct dentry
*dentry
,
990 struct ext4_iloc
*iloc
,
991 void *inline_start
, int inline_size
)
993 struct inode
*dir
= dentry
->d_parent
->d_inode
;
994 const char *name
= dentry
->d_name
.name
;
995 int namelen
= dentry
->d_name
.len
;
996 unsigned short reclen
;
998 struct ext4_dir_entry_2
*de
;
1000 reclen
= EXT4_DIR_REC_LEN(namelen
);
1001 err
= ext4_find_dest_de(dir
, inode
, iloc
->bh
,
1002 inline_start
, inline_size
,
1003 name
, namelen
, &de
);
1007 err
= ext4_journal_get_write_access(handle
, iloc
->bh
);
1010 ext4_insert_dentry(inode
, de
, inline_size
, name
, namelen
);
1012 ext4_show_inline_dir(dir
, iloc
->bh
, inline_start
, inline_size
);
1015 * XXX shouldn't update any times until successful
1016 * completion of syscall, but too many callers depend
1019 * XXX similarly, too many callers depend on
1020 * ext4_new_inode() setting the times, but error
1021 * recovery deletes the inode, so the worst that can
1022 * happen is that the times are slightly out of date
1023 * and/or different from the directory change time.
1025 dir
->i_mtime
= dir
->i_ctime
= ext4_current_time(dir
);
1026 ext4_update_dx_flag(dir
);
1028 ext4_mark_inode_dirty(handle
, dir
);
1032 static void *ext4_get_inline_xattr_pos(struct inode
*inode
,
1033 struct ext4_iloc
*iloc
)
1035 struct ext4_xattr_entry
*entry
;
1036 struct ext4_xattr_ibody_header
*header
;
1038 BUG_ON(!EXT4_I(inode
)->i_inline_off
);
1040 header
= IHDR(inode
, ext4_raw_inode(iloc
));
1041 entry
= (struct ext4_xattr_entry
*)((void *)ext4_raw_inode(iloc
) +
1042 EXT4_I(inode
)->i_inline_off
);
1044 return (void *)IFIRST(header
) + le16_to_cpu(entry
->e_value_offs
);
1047 /* Set the final de to cover the whole block. */
1048 static void ext4_update_final_de(void *de_buf
, int old_size
, int new_size
)
1050 struct ext4_dir_entry_2
*de
, *prev_de
;
1054 de
= (struct ext4_dir_entry_2
*)de_buf
;
1056 limit
= de_buf
+ old_size
;
1059 de_len
= ext4_rec_len_from_disk(de
->rec_len
, old_size
);
1061 de
= (struct ext4_dir_entry_2
*)de_buf
;
1062 } while (de_buf
< limit
);
1064 prev_de
->rec_len
= ext4_rec_len_to_disk(de_len
+ new_size
-
1065 old_size
, new_size
);
1067 /* this is just created, so create an empty entry. */
1069 de
->rec_len
= ext4_rec_len_to_disk(new_size
, new_size
);
1073 static int ext4_update_inline_dir(handle_t
*handle
, struct inode
*dir
,
1074 struct ext4_iloc
*iloc
)
1077 int old_size
= EXT4_I(dir
)->i_inline_size
- EXT4_MIN_INLINE_DATA_SIZE
;
1078 int new_size
= get_max_inline_xattr_value_size(dir
, iloc
);
1080 if (new_size
- old_size
<= EXT4_DIR_REC_LEN(1))
1083 ret
= ext4_update_inline_data(handle
, dir
,
1084 new_size
+ EXT4_MIN_INLINE_DATA_SIZE
);
1088 ext4_update_final_de(ext4_get_inline_xattr_pos(dir
, iloc
), old_size
,
1089 EXT4_I(dir
)->i_inline_size
-
1090 EXT4_MIN_INLINE_DATA_SIZE
);
1091 dir
->i_size
= EXT4_I(dir
)->i_disksize
= EXT4_I(dir
)->i_inline_size
;
1095 static void ext4_restore_inline_data(handle_t
*handle
, struct inode
*inode
,
1096 struct ext4_iloc
*iloc
,
1097 void *buf
, int inline_size
)
1099 ext4_create_inline_data(handle
, inode
, inline_size
);
1100 ext4_write_inline_data(inode
, iloc
, buf
, 0, inline_size
);
1101 ext4_set_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
1104 static int ext4_finish_convert_inline_dir(handle_t
*handle
,
1105 struct inode
*inode
,
1106 struct buffer_head
*dir_block
,
1110 int err
, csum_size
= 0, header_size
= 0;
1111 struct ext4_dir_entry_2
*de
;
1112 struct ext4_dir_entry_tail
*t
;
1113 void *target
= dir_block
->b_data
;
1116 * First create "." and ".." and then copy the dir information
1117 * back to the block.
1119 de
= (struct ext4_dir_entry_2
*)target
;
1120 de
= ext4_init_dot_dotdot(inode
, de
,
1121 inode
->i_sb
->s_blocksize
, csum_size
,
1122 le32_to_cpu(((struct ext4_dir_entry_2
*)buf
)->inode
), 1);
1123 header_size
= (void *)de
- target
;
1125 memcpy((void *)de
, buf
+ EXT4_INLINE_DOTDOT_SIZE
,
1126 inline_size
- EXT4_INLINE_DOTDOT_SIZE
);
1128 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
1129 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
1130 csum_size
= sizeof(struct ext4_dir_entry_tail
);
1132 inode
->i_size
= inode
->i_sb
->s_blocksize
;
1133 i_size_write(inode
, inode
->i_sb
->s_blocksize
);
1134 EXT4_I(inode
)->i_disksize
= inode
->i_sb
->s_blocksize
;
1135 ext4_update_final_de(dir_block
->b_data
,
1136 inline_size
- EXT4_INLINE_DOTDOT_SIZE
+ header_size
,
1137 inode
->i_sb
->s_blocksize
- csum_size
);
1140 t
= EXT4_DIRENT_TAIL(dir_block
->b_data
,
1141 inode
->i_sb
->s_blocksize
);
1142 initialize_dirent_tail(t
, inode
->i_sb
->s_blocksize
);
1144 set_buffer_uptodate(dir_block
);
1145 err
= ext4_handle_dirty_dirent_node(handle
, inode
, dir_block
);
1148 set_buffer_verified(dir_block
);
1153 static int ext4_convert_inline_data_nolock(handle_t
*handle
,
1154 struct inode
*inode
,
1155 struct ext4_iloc
*iloc
)
1159 struct buffer_head
*data_bh
= NULL
;
1160 struct ext4_map_blocks map
;
1163 inline_size
= ext4_get_inline_size(inode
);
1164 buf
= kmalloc(inline_size
, GFP_NOFS
);
1170 error
= ext4_read_inline_data(inode
, buf
, inline_size
, iloc
);
1174 error
= ext4_destroy_inline_data_nolock(handle
, inode
);
1181 error
= ext4_map_blocks(handle
, inode
, &map
, EXT4_GET_BLOCKS_CREATE
);
1184 if (!(map
.m_flags
& EXT4_MAP_MAPPED
)) {
1189 data_bh
= sb_getblk(inode
->i_sb
, map
.m_pblk
);
1195 lock_buffer(data_bh
);
1196 error
= ext4_journal_get_create_access(handle
, data_bh
);
1198 unlock_buffer(data_bh
);
1202 memset(data_bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
1204 if (!S_ISDIR(inode
->i_mode
)) {
1205 memcpy(data_bh
->b_data
, buf
, inline_size
);
1206 set_buffer_uptodate(data_bh
);
1207 error
= ext4_handle_dirty_metadata(handle
,
1210 error
= ext4_finish_convert_inline_dir(handle
, inode
, data_bh
,
1214 unlock_buffer(data_bh
);
1217 ext4_restore_inline_data(handle
, inode
, iloc
, buf
, inline_size
);
1226 * Try to add the new entry to the inline data.
1227 * If succeeds, return 0. If not, extended the inline dir and copied data to
1228 * the new created block.
1230 int ext4_try_add_inline_entry(handle_t
*handle
, struct dentry
*dentry
,
1231 struct inode
*inode
)
1233 int ret
, inline_size
;
1235 struct ext4_iloc iloc
;
1236 struct inode
*dir
= dentry
->d_parent
->d_inode
;
1238 ret
= ext4_get_inode_loc(dir
, &iloc
);
1242 down_write(&EXT4_I(dir
)->xattr_sem
);
1243 if (!ext4_has_inline_data(dir
))
1246 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1247 EXT4_INLINE_DOTDOT_SIZE
;
1248 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DOTDOT_SIZE
;
1250 ret
= ext4_add_dirent_to_inline(handle
, dentry
, inode
, &iloc
,
1251 inline_start
, inline_size
);
1255 /* check whether it can be inserted to inline xattr space. */
1256 inline_size
= EXT4_I(dir
)->i_inline_size
-
1257 EXT4_MIN_INLINE_DATA_SIZE
;
1259 /* Try to use the xattr space.*/
1260 ret
= ext4_update_inline_dir(handle
, dir
, &iloc
);
1261 if (ret
&& ret
!= -ENOSPC
)
1264 inline_size
= EXT4_I(dir
)->i_inline_size
-
1265 EXT4_MIN_INLINE_DATA_SIZE
;
1269 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1271 ret
= ext4_add_dirent_to_inline(handle
, dentry
, inode
, &iloc
,
1272 inline_start
, inline_size
);
1279 * The inline space is filled up, so create a new block for it.
1280 * As the extent tree will be created, we have to save the inline
1283 ret
= ext4_convert_inline_data_nolock(handle
, dir
, &iloc
);
1286 ext4_mark_inode_dirty(handle
, dir
);
1287 up_write(&EXT4_I(dir
)->xattr_sem
);
1292 int ext4_read_inline_dir(struct file
*filp
,
1293 void *dirent
, filldir_t filldir
,
1294 int *has_inline_data
)
1297 unsigned int offset
, parent_ino
;
1299 struct ext4_dir_entry_2
*de
;
1300 struct super_block
*sb
;
1301 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1302 int ret
, inline_size
= 0;
1303 struct ext4_iloc iloc
;
1304 void *dir_buf
= NULL
;
1306 ret
= ext4_get_inode_loc(inode
, &iloc
);
1310 down_read(&EXT4_I(inode
)->xattr_sem
);
1311 if (!ext4_has_inline_data(inode
)) {
1312 up_read(&EXT4_I(inode
)->xattr_sem
);
1313 *has_inline_data
= 0;
1317 inline_size
= ext4_get_inline_size(inode
);
1318 dir_buf
= kmalloc(inline_size
, GFP_NOFS
);
1321 up_read(&EXT4_I(inode
)->xattr_sem
);
1325 ret
= ext4_read_inline_data(inode
, dir_buf
, inline_size
, &iloc
);
1326 up_read(&EXT4_I(inode
)->xattr_sem
);
1332 parent_ino
= le32_to_cpu(((struct ext4_dir_entry_2
*)dir_buf
)->inode
);
1334 while (!error
&& !stored
&& filp
->f_pos
< inode
->i_size
) {
1337 * If the version has changed since the last call to
1338 * readdir(2), then we might be pointing to an invalid
1339 * dirent right now. Scan from the start of the inline
1342 if (filp
->f_version
!= inode
->i_version
) {
1344 i
< inode
->i_size
&& i
< offset
;) {
1346 /* skip "." and ".." if needed. */
1347 i
+= EXT4_INLINE_DOTDOT_SIZE
;
1350 de
= (struct ext4_dir_entry_2
*)
1352 /* It's too expensive to do a full
1353 * dirent test each time round this
1354 * loop, but we do have to test at
1355 * least that it is non-zero. A
1356 * failure will be detected in the
1357 * dirent test below. */
1358 if (ext4_rec_len_from_disk(de
->rec_len
,
1359 inline_size
) < EXT4_DIR_REC_LEN(1))
1361 i
+= ext4_rec_len_from_disk(de
->rec_len
,
1365 filp
->f_pos
= offset
;
1366 filp
->f_version
= inode
->i_version
;
1369 while (!error
&& filp
->f_pos
< inode
->i_size
) {
1370 if (filp
->f_pos
== 0) {
1371 error
= filldir(dirent
, ".", 1, 0, inode
->i_ino
,
1377 error
= filldir(dirent
, "..", 2, 0, parent_ino
,
1383 filp
->f_pos
= offset
= EXT4_INLINE_DOTDOT_SIZE
;
1387 de
= (struct ext4_dir_entry_2
*)(dir_buf
+ offset
);
1388 if (ext4_check_dir_entry(inode
, filp
, de
,
1390 inline_size
, offset
)) {
1394 offset
+= ext4_rec_len_from_disk(de
->rec_len
,
1396 if (le32_to_cpu(de
->inode
)) {
1397 /* We might block in the next section
1398 * if the data destination is
1399 * currently swapped out. So, use a
1400 * version stamp to detect whether or
1401 * not the directory has been modified
1402 * during the copy operation.
1404 u64 version
= filp
->f_version
;
1406 error
= filldir(dirent
, de
->name
,
1409 le32_to_cpu(de
->inode
),
1410 get_dtype(sb
, de
->file_type
));
1413 if (version
!= filp
->f_version
)
1417 filp
->f_pos
+= ext4_rec_len_from_disk(de
->rec_len
,
1428 struct buffer_head
*ext4_get_first_inline_block(struct inode
*inode
,
1429 struct ext4_dir_entry_2
**parent_de
,
1432 struct ext4_iloc iloc
;
1434 *retval
= ext4_get_inode_loc(inode
, &iloc
);
1438 *parent_de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1444 * Try to create the inline data for the new dir.
1445 * If it succeeds, return 0, otherwise return the error.
1446 * In case of ENOSPC, the caller should create the normal disk layout dir.
1448 int ext4_try_create_inline_dir(handle_t
*handle
, struct inode
*parent
,
1449 struct inode
*inode
)
1451 int ret
, inline_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1452 struct ext4_iloc iloc
;
1453 struct ext4_dir_entry_2
*de
;
1455 ret
= ext4_get_inode_loc(inode
, &iloc
);
1459 ret
= ext4_prepare_inline_data(handle
, inode
, inline_size
);
1464 * For inline dir, we only save the inode information for the ".."
1465 * and create a fake dentry to cover the left space.
1467 de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1468 de
->inode
= cpu_to_le32(parent
->i_ino
);
1469 de
= (struct ext4_dir_entry_2
*)((void *)de
+ EXT4_INLINE_DOTDOT_SIZE
);
1471 de
->rec_len
= ext4_rec_len_to_disk(
1472 inline_size
- EXT4_INLINE_DOTDOT_SIZE
,
1474 set_nlink(inode
, 2);
1475 inode
->i_size
= EXT4_I(inode
)->i_disksize
= inline_size
;
1481 struct buffer_head
*ext4_find_inline_entry(struct inode
*dir
,
1482 const struct qstr
*d_name
,
1483 struct ext4_dir_entry_2
**res_dir
,
1484 int *has_inline_data
)
1487 struct ext4_iloc iloc
;
1491 if (ext4_get_inode_loc(dir
, &iloc
))
1494 down_read(&EXT4_I(dir
)->xattr_sem
);
1495 if (!ext4_has_inline_data(dir
)) {
1496 *has_inline_data
= 0;
1500 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1501 EXT4_INLINE_DOTDOT_SIZE
;
1502 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
- EXT4_INLINE_DOTDOT_SIZE
;
1503 ret
= search_dir(iloc
.bh
, inline_start
, inline_size
,
1504 dir
, d_name
, 0, res_dir
);
1510 if (ext4_get_inline_size(dir
) == EXT4_MIN_INLINE_DATA_SIZE
)
1513 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1514 inline_size
= ext4_get_inline_size(dir
) - EXT4_MIN_INLINE_DATA_SIZE
;
1516 ret
= search_dir(iloc
.bh
, inline_start
, inline_size
,
1517 dir
, d_name
, 0, res_dir
);
1525 up_read(&EXT4_I(dir
)->xattr_sem
);
1529 int ext4_delete_inline_entry(handle_t
*handle
,
1531 struct ext4_dir_entry_2
*de_del
,
1532 struct buffer_head
*bh
,
1533 int *has_inline_data
)
1535 int err
, inline_size
;
1536 struct ext4_iloc iloc
;
1539 err
= ext4_get_inode_loc(dir
, &iloc
);
1543 down_write(&EXT4_I(dir
)->xattr_sem
);
1544 if (!ext4_has_inline_data(dir
)) {
1545 *has_inline_data
= 0;
1549 if ((void *)de_del
- ((void *)ext4_raw_inode(&iloc
)->i_block
) <
1550 EXT4_MIN_INLINE_DATA_SIZE
) {
1551 inline_start
= (void *)ext4_raw_inode(&iloc
)->i_block
+
1552 EXT4_INLINE_DOTDOT_SIZE
;
1553 inline_size
= EXT4_MIN_INLINE_DATA_SIZE
-
1554 EXT4_INLINE_DOTDOT_SIZE
;
1556 inline_start
= ext4_get_inline_xattr_pos(dir
, &iloc
);
1557 inline_size
= ext4_get_inline_size(dir
) -
1558 EXT4_MIN_INLINE_DATA_SIZE
;
1561 err
= ext4_journal_get_write_access(handle
, bh
);
1565 err
= ext4_generic_delete_entry(handle
, dir
, de_del
, bh
,
1566 inline_start
, inline_size
, 0);
1570 BUFFER_TRACE(bh
, "call ext4_handle_dirty_metadata");
1571 err
= ext4_mark_inode_dirty(handle
, dir
);
1575 ext4_show_inline_dir(dir
, iloc
.bh
, inline_start
, inline_size
);
1577 up_write(&EXT4_I(dir
)->xattr_sem
);
1580 ext4_std_error(dir
->i_sb
, err
);
1585 * Get the inline dentry at offset.
1587 static inline struct ext4_dir_entry_2
*
1588 ext4_get_inline_entry(struct inode
*inode
,
1589 struct ext4_iloc
*iloc
,
1590 unsigned int offset
,
1591 void **inline_start
,
1596 BUG_ON(offset
> ext4_get_inline_size(inode
));
1598 if (offset
< EXT4_MIN_INLINE_DATA_SIZE
) {
1599 inline_pos
= (void *)ext4_raw_inode(iloc
)->i_block
;
1600 *inline_size
= EXT4_MIN_INLINE_DATA_SIZE
;
1602 inline_pos
= ext4_get_inline_xattr_pos(inode
, iloc
);
1603 offset
-= EXT4_MIN_INLINE_DATA_SIZE
;
1604 *inline_size
= ext4_get_inline_size(inode
) -
1605 EXT4_MIN_INLINE_DATA_SIZE
;
1609 *inline_start
= inline_pos
;
1610 return (struct ext4_dir_entry_2
*)(inline_pos
+ offset
);
1613 int empty_inline_dir(struct inode
*dir
, int *has_inline_data
)
1615 int err
, inline_size
;
1616 struct ext4_iloc iloc
;
1618 unsigned int offset
;
1619 struct ext4_dir_entry_2
*de
;
1622 err
= ext4_get_inode_loc(dir
, &iloc
);
1624 EXT4_ERROR_INODE(dir
, "error %d getting inode %lu block",
1629 down_read(&EXT4_I(dir
)->xattr_sem
);
1630 if (!ext4_has_inline_data(dir
)) {
1631 *has_inline_data
= 0;
1635 de
= (struct ext4_dir_entry_2
*)ext4_raw_inode(&iloc
)->i_block
;
1636 if (!le32_to_cpu(de
->inode
)) {
1637 ext4_warning(dir
->i_sb
,
1638 "bad inline directory (dir #%lu) - no `..'",
1644 offset
= EXT4_INLINE_DOTDOT_SIZE
;
1645 while (offset
< dir
->i_size
) {
1646 de
= ext4_get_inline_entry(dir
, &iloc
, offset
,
1647 &inline_pos
, &inline_size
);
1648 if (ext4_check_dir_entry(dir
, NULL
, de
,
1649 iloc
.bh
, inline_pos
,
1650 inline_size
, offset
)) {
1651 ext4_warning(dir
->i_sb
,
1652 "bad inline directory (dir #%lu) - "
1653 "inode %u, rec_len %u, name_len %d"
1655 dir
->i_ino
, le32_to_cpu(de
->inode
),
1656 le16_to_cpu(de
->rec_len
), de
->name_len
,
1661 if (le32_to_cpu(de
->inode
)) {
1665 offset
+= ext4_rec_len_from_disk(de
->rec_len
, inline_size
);
1669 up_read(&EXT4_I(dir
)->xattr_sem
);
1674 int ext4_destroy_inline_data(handle_t
*handle
, struct inode
*inode
)
1678 down_write(&EXT4_I(inode
)->xattr_sem
);
1679 ret
= ext4_destroy_inline_data_nolock(handle
, inode
);
1680 up_write(&EXT4_I(inode
)->xattr_sem
);
1685 int ext4_inline_data_fiemap(struct inode
*inode
,
1686 struct fiemap_extent_info
*fieinfo
,
1691 __u32 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_LAST
;
1693 struct ext4_iloc iloc
;
1695 down_read(&EXT4_I(inode
)->xattr_sem
);
1696 if (!ext4_has_inline_data(inode
)) {
1701 error
= ext4_get_inode_loc(inode
, &iloc
);
1705 physical
= iloc
.bh
->b_blocknr
<< inode
->i_sb
->s_blocksize_bits
;
1706 physical
+= (char *)ext4_raw_inode(&iloc
) - iloc
.bh
->b_data
;
1707 physical
+= offsetof(struct ext4_inode
, i_block
);
1708 length
= i_size_read(inode
);
1711 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
1715 up_read(&EXT4_I(inode
)->xattr_sem
);
1716 return (error
< 0 ? error
: 0);
1720 * Called during xattr set, and if we can sparse space 'needed',
1721 * just create the extent tree evict the data to the outer block.
1723 * We use jbd2 instead of page cache to move data to the 1st block
1724 * so that the whole transaction can be committed as a whole and
1725 * the data isn't lost because of the delayed page cache write.
1727 int ext4_try_to_evict_inline_data(handle_t
*handle
,
1728 struct inode
*inode
,
1732 struct ext4_xattr_entry
*entry
;
1733 struct ext4_xattr_ibody_header
*header
;
1734 struct ext4_inode
*raw_inode
;
1735 struct ext4_iloc iloc
;
1737 error
= ext4_get_inode_loc(inode
, &iloc
);
1741 raw_inode
= ext4_raw_inode(&iloc
);
1742 header
= IHDR(inode
, raw_inode
);
1743 entry
= (struct ext4_xattr_entry
*)((void *)raw_inode
+
1744 EXT4_I(inode
)->i_inline_off
);
1745 if (EXT4_XATTR_LEN(entry
->e_name_len
) +
1746 EXT4_XATTR_SIZE(le32_to_cpu(entry
->e_value_size
)) < needed
) {
1751 error
= ext4_convert_inline_data_nolock(handle
, inode
, &iloc
);
1757 void ext4_inline_data_truncate(struct inode
*inode
, int *has_inline
)
1760 int inline_size
, value_len
, needed_blocks
;
1763 struct ext4_xattr_ibody_find is
= {
1764 .s
= { .not_found
= -ENODATA
, },
1766 struct ext4_xattr_info i
= {
1767 .name_index
= EXT4_XATTR_INDEX_SYSTEM
,
1768 .name
= EXT4_XATTR_SYSTEM_DATA
,
1772 needed_blocks
= ext4_writepage_trans_blocks(inode
);
1773 handle
= ext4_journal_start(inode
, needed_blocks
);
1777 down_write(&EXT4_I(inode
)->xattr_sem
);
1778 if (!ext4_has_inline_data(inode
)) {
1780 ext4_journal_stop(handle
);
1784 if (ext4_orphan_add(handle
, inode
))
1787 if (ext4_get_inode_loc(inode
, &is
.iloc
))
1790 down_write(&EXT4_I(inode
)->i_data_sem
);
1791 i_size
= inode
->i_size
;
1792 inline_size
= ext4_get_inline_size(inode
);
1793 EXT4_I(inode
)->i_disksize
= i_size
;
1795 if (i_size
< inline_size
) {
1796 /* Clear the content in the xattr space. */
1797 if (inline_size
> EXT4_MIN_INLINE_DATA_SIZE
) {
1798 if (ext4_xattr_ibody_find(inode
, &i
, &is
))
1801 BUG_ON(is
.s
.not_found
);
1803 value_len
= le32_to_cpu(is
.s
.here
->e_value_size
);
1804 value
= kmalloc(value_len
, GFP_NOFS
);
1808 if (ext4_xattr_ibody_get(inode
, i
.name_index
, i
.name
,
1813 i
.value_len
= i_size
> EXT4_MIN_INLINE_DATA_SIZE
?
1814 i_size
- EXT4_MIN_INLINE_DATA_SIZE
: 0;
1815 if (ext4_xattr_ibody_inline_set(handle
, inode
, &i
, &is
))
1819 /* Clear the content within i_blocks. */
1820 if (i_size
< EXT4_MIN_INLINE_DATA_SIZE
)
1821 memset(ext4_raw_inode(&is
.iloc
)->i_block
+ i_size
, 0,
1822 EXT4_MIN_INLINE_DATA_SIZE
- i_size
);
1824 EXT4_I(inode
)->i_inline_size
= i_size
<
1825 EXT4_MIN_INLINE_DATA_SIZE
?
1826 EXT4_MIN_INLINE_DATA_SIZE
: i_size
;
1830 up_write(&EXT4_I(inode
)->i_data_sem
);
1833 up_write(&EXT4_I(inode
)->xattr_sem
);
1836 ext4_orphan_del(handle
, inode
);
1838 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
1839 ext4_mark_inode_dirty(handle
, inode
);
1841 ext4_handle_sync(handle
);
1843 ext4_journal_stop(handle
);
1847 int ext4_convert_inline_data(struct inode
*inode
)
1849 int error
, needed_blocks
;
1851 struct ext4_iloc iloc
;
1853 if (!ext4_has_inline_data(inode
)) {
1854 ext4_clear_inode_state(inode
, EXT4_STATE_MAY_INLINE_DATA
);
1858 needed_blocks
= ext4_writepage_trans_blocks(inode
);
1861 error
= ext4_get_inode_loc(inode
, &iloc
);
1865 handle
= ext4_journal_start(inode
, needed_blocks
);
1866 if (IS_ERR(handle
)) {
1867 error
= PTR_ERR(handle
);
1871 down_write(&EXT4_I(inode
)->xattr_sem
);
1872 if (!ext4_has_inline_data(inode
)) {
1873 up_write(&EXT4_I(inode
)->xattr_sem
);
1877 error
= ext4_convert_inline_data_nolock(handle
, inode
, &iloc
);
1878 up_write(&EXT4_I(inode
)->xattr_sem
);
1880 ext4_journal_stop(handle
);