1f5166768STheodore Ts'o // SPDX-License-Identifier: LGPL-2.1
267cf5b09STao Ma /*
367cf5b09STao Ma * Copyright (c) 2012 Taobao.
467cf5b09STao Ma * Written by Tao Ma <[email protected]>
567cf5b09STao Ma */
64bdfc873SMichael Halcrow
77046ae35SAndreas Gruenbacher #include <linux/iomap.h>
84bdfc873SMichael Halcrow #include <linux/fiemap.h>
95a57bca9SZhang Yi #include <linux/namei.h>
10ee73f9a5SJeff Layton #include <linux/iversion.h>
114034247aSNeilBrown #include <linux/sched/mm.h>
124bdfc873SMichael Halcrow
1367cf5b09STao Ma #include "ext4_jbd2.h"
1467cf5b09STao Ma #include "ext4.h"
1567cf5b09STao Ma #include "xattr.h"
16f19d5870STao Ma #include "truncate.h"
1767cf5b09STao Ma
1867cf5b09STao Ma #define EXT4_XATTR_SYSTEM_DATA "data"
1967cf5b09STao Ma #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
208af0f082STao Ma #define EXT4_INLINE_DOTDOT_OFFSET 2
213c47d541STao Ma #define EXT4_INLINE_DOTDOT_SIZE 4
2267cf5b09STao Ma
233db572f7SJulian Sun
243db572f7SJulian Sun static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
253db572f7SJulian Sun struct inode *inode,
263db572f7SJulian Sun void **fsdata);
273db572f7SJulian Sun
ext4_get_inline_size(struct inode * inode)28c197855eSStephen Hemminger static int ext4_get_inline_size(struct inode *inode)
2967cf5b09STao Ma {
3067cf5b09STao Ma if (EXT4_I(inode)->i_inline_off)
3167cf5b09STao Ma return EXT4_I(inode)->i_inline_size;
3267cf5b09STao Ma
3367cf5b09STao Ma return 0;
3467cf5b09STao Ma }
3567cf5b09STao Ma
get_max_inline_xattr_value_size(struct inode * inode,struct ext4_iloc * iloc)3667cf5b09STao Ma static int get_max_inline_xattr_value_size(struct inode *inode,
3767cf5b09STao Ma struct ext4_iloc *iloc)
3867cf5b09STao Ma {
3967cf5b09STao Ma struct ext4_xattr_ibody_header *header;
4067cf5b09STao Ma struct ext4_xattr_entry *entry;
4167cf5b09STao Ma struct ext4_inode *raw_inode;
422220eaf9STheodore Ts'o void *end;
4367cf5b09STao Ma int free, min_offs;
4467cf5b09STao Ma
45c9fd167dSBaokun Li if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
46c9fd167dSBaokun Li return 0;
47c9fd167dSBaokun Li
4867cf5b09STao Ma min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
4967cf5b09STao Ma EXT4_GOOD_OLD_INODE_SIZE -
5067cf5b09STao Ma EXT4_I(inode)->i_extra_isize -
5167cf5b09STao Ma sizeof(struct ext4_xattr_ibody_header);
5267cf5b09STao Ma
5367cf5b09STao Ma /*
5467cf5b09STao Ma * We need to subtract another sizeof(__u32) since an in-inode xattr
5567cf5b09STao Ma * needs an empty 4 bytes to indicate the gap between the xattr entry
5667cf5b09STao Ma * and the name/value pair.
5767cf5b09STao Ma */
5867cf5b09STao Ma if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
5967cf5b09STao Ma return EXT4_XATTR_SIZE(min_offs -
6067cf5b09STao Ma EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
6167cf5b09STao Ma EXT4_XATTR_ROUND - sizeof(__u32));
6267cf5b09STao Ma
6367cf5b09STao Ma raw_inode = ext4_raw_inode(iloc);
6467cf5b09STao Ma header = IHDR(inode, raw_inode);
6567cf5b09STao Ma entry = IFIRST(header);
662220eaf9STheodore Ts'o end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
6767cf5b09STao Ma
6867cf5b09STao Ma /* Compute min_offs. */
692220eaf9STheodore Ts'o while (!IS_LAST_ENTRY(entry)) {
702220eaf9STheodore Ts'o void *next = EXT4_XATTR_NEXT(entry);
712220eaf9STheodore Ts'o
722220eaf9STheodore Ts'o if (next >= end) {
732220eaf9STheodore Ts'o EXT4_ERROR_INODE(inode,
742220eaf9STheodore Ts'o "corrupt xattr in inline inode");
752220eaf9STheodore Ts'o return 0;
762220eaf9STheodore Ts'o }
77e50e5129SAndreas Dilger if (!entry->e_value_inum && entry->e_value_size) {
7867cf5b09STao Ma size_t offs = le16_to_cpu(entry->e_value_offs);
7967cf5b09STao Ma if (offs < min_offs)
8067cf5b09STao Ma min_offs = offs;
8167cf5b09STao Ma }
822220eaf9STheodore Ts'o entry = next;
8367cf5b09STao Ma }
8467cf5b09STao Ma free = min_offs -
8567cf5b09STao Ma ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
8667cf5b09STao Ma
8767cf5b09STao Ma if (EXT4_I(inode)->i_inline_off) {
8867cf5b09STao Ma entry = (struct ext4_xattr_entry *)
8967cf5b09STao Ma ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
9067cf5b09STao Ma
91c4932dbeSboxi liu free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
9267cf5b09STao Ma goto out;
9367cf5b09STao Ma }
9467cf5b09STao Ma
9567cf5b09STao Ma free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
9667cf5b09STao Ma
9767cf5b09STao Ma if (free > EXT4_XATTR_ROUND)
9867cf5b09STao Ma free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
9967cf5b09STao Ma else
10067cf5b09STao Ma free = 0;
10167cf5b09STao Ma
10267cf5b09STao Ma out:
10367cf5b09STao Ma return free;
10467cf5b09STao Ma }
10567cf5b09STao Ma
10667cf5b09STao Ma /*
10767cf5b09STao Ma * Get the maximum size we now can store in an inode.
10867cf5b09STao Ma * If we can't find the space for a xattr entry, don't use the space
10967cf5b09STao Ma * of the extents since we have no space to indicate the inline data.
11067cf5b09STao Ma */
ext4_get_max_inline_size(struct inode * inode)11167cf5b09STao Ma int ext4_get_max_inline_size(struct inode *inode)
11267cf5b09STao Ma {
11367cf5b09STao Ma int error, max_inline_size;
11467cf5b09STao Ma struct ext4_iloc iloc;
11567cf5b09STao Ma
11667cf5b09STao Ma if (EXT4_I(inode)->i_extra_isize == 0)
11767cf5b09STao Ma return 0;
11867cf5b09STao Ma
11967cf5b09STao Ma error = ext4_get_inode_loc(inode, &iloc);
12067cf5b09STao Ma if (error) {
12154d3adbcSTheodore Ts'o ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
12267cf5b09STao Ma "can't get inode location %lu",
12367cf5b09STao Ma inode->i_ino);
12467cf5b09STao Ma return 0;
12567cf5b09STao Ma }
12667cf5b09STao Ma
12767cf5b09STao Ma down_read(&EXT4_I(inode)->xattr_sem);
12867cf5b09STao Ma max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
12967cf5b09STao Ma up_read(&EXT4_I(inode)->xattr_sem);
13067cf5b09STao Ma
13167cf5b09STao Ma brelse(iloc.bh);
13267cf5b09STao Ma
13367cf5b09STao Ma if (!max_inline_size)
13467cf5b09STao Ma return 0;
13567cf5b09STao Ma
13667cf5b09STao Ma return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
13767cf5b09STao Ma }
13867cf5b09STao Ma
13967cf5b09STao Ma /*
14067cf5b09STao Ma * this function does not take xattr_sem, which is OK because it is
14167cf5b09STao Ma * currently only used in a code path coming form ext4_iget, before
14267cf5b09STao Ma * the new inode has been unlocked
14367cf5b09STao Ma */
ext4_find_inline_data_nolock(struct inode * inode)14467cf5b09STao Ma int ext4_find_inline_data_nolock(struct inode *inode)
14567cf5b09STao Ma {
14667cf5b09STao Ma struct ext4_xattr_ibody_find is = {
14767cf5b09STao Ma .s = { .not_found = -ENODATA, },
14867cf5b09STao Ma };
14967cf5b09STao Ma struct ext4_xattr_info i = {
15067cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
15167cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
15267cf5b09STao Ma };
15367cf5b09STao Ma int error;
15467cf5b09STao Ma
15567cf5b09STao Ma if (EXT4_I(inode)->i_extra_isize == 0)
15667cf5b09STao Ma return 0;
15767cf5b09STao Ma
15867cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
15967cf5b09STao Ma if (error)
16067cf5b09STao Ma return error;
16167cf5b09STao Ma
16267cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
16367cf5b09STao Ma if (error)
16467cf5b09STao Ma goto out;
16567cf5b09STao Ma
16667cf5b09STao Ma if (!is.s.not_found) {
167117166efSTheodore Ts'o if (is.s.here->e_value_inum) {
168117166efSTheodore Ts'o EXT4_ERROR_INODE(inode, "inline data xattr refers "
169117166efSTheodore Ts'o "to an external xattr inode");
170117166efSTheodore Ts'o error = -EFSCORRUPTED;
171117166efSTheodore Ts'o goto out;
172117166efSTheodore Ts'o }
17367cf5b09STao Ma EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
17467cf5b09STao Ma (void *)ext4_raw_inode(&is.iloc));
17567cf5b09STao Ma EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
17667cf5b09STao Ma le32_to_cpu(is.s.here->e_value_size);
17767cf5b09STao Ma }
17867cf5b09STao Ma out:
17967cf5b09STao Ma brelse(is.iloc.bh);
18067cf5b09STao Ma return error;
18167cf5b09STao Ma }
18267cf5b09STao Ma
ext4_read_inline_data(struct inode * inode,void * buffer,unsigned int len,struct ext4_iloc * iloc)18367cf5b09STao Ma static int ext4_read_inline_data(struct inode *inode, void *buffer,
18467cf5b09STao Ma unsigned int len,
18567cf5b09STao Ma struct ext4_iloc *iloc)
18667cf5b09STao Ma {
18767cf5b09STao Ma struct ext4_xattr_entry *entry;
18867cf5b09STao Ma struct ext4_xattr_ibody_header *header;
18967cf5b09STao Ma int cp_len = 0;
19067cf5b09STao Ma struct ext4_inode *raw_inode;
19167cf5b09STao Ma
19267cf5b09STao Ma if (!len)
19367cf5b09STao Ma return 0;
19467cf5b09STao Ma
19567cf5b09STao Ma BUG_ON(len > EXT4_I(inode)->i_inline_size);
19667cf5b09STao Ma
19766267814SJiangshan Yi cp_len = min_t(unsigned int, len, EXT4_MIN_INLINE_DATA_SIZE);
19867cf5b09STao Ma
19967cf5b09STao Ma raw_inode = ext4_raw_inode(iloc);
20067cf5b09STao Ma memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
20167cf5b09STao Ma
20267cf5b09STao Ma len -= cp_len;
20367cf5b09STao Ma buffer += cp_len;
20467cf5b09STao Ma
20567cf5b09STao Ma if (!len)
20667cf5b09STao Ma goto out;
20767cf5b09STao Ma
20867cf5b09STao Ma header = IHDR(inode, raw_inode);
20967cf5b09STao Ma entry = (struct ext4_xattr_entry *)((void *)raw_inode +
21067cf5b09STao Ma EXT4_I(inode)->i_inline_off);
21167cf5b09STao Ma len = min_t(unsigned int, len,
21267cf5b09STao Ma (unsigned int)le32_to_cpu(entry->e_value_size));
21367cf5b09STao Ma
21467cf5b09STao Ma memcpy(buffer,
21567cf5b09STao Ma (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
21667cf5b09STao Ma cp_len += len;
21767cf5b09STao Ma
21867cf5b09STao Ma out:
21967cf5b09STao Ma return cp_len;
22067cf5b09STao Ma }
22167cf5b09STao Ma
22267cf5b09STao Ma /*
22367cf5b09STao Ma * write the buffer to the inline inode.
22467cf5b09STao Ma * If 'create' is set, we don't need to do the extra copy in the xattr
225310c097cSRitesh Harjani * value since it is already handled by ext4_xattr_ibody_set.
2260d812f77STao Ma * That saves us one memcpy.
22767cf5b09STao Ma */
ext4_write_inline_data(struct inode * inode,struct ext4_iloc * iloc,void * buffer,loff_t pos,unsigned int len)228c197855eSStephen Hemminger static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
22967cf5b09STao Ma void *buffer, loff_t pos, unsigned int len)
23067cf5b09STao Ma {
23167cf5b09STao Ma struct ext4_xattr_entry *entry;
23267cf5b09STao Ma struct ext4_xattr_ibody_header *header;
23367cf5b09STao Ma struct ext4_inode *raw_inode;
23467cf5b09STao Ma int cp_len = 0;
23567cf5b09STao Ma
2360a1b2f5eSBaokun Li if (unlikely(ext4_emergency_state(inode->i_sb)))
2370db1ff22STheodore Ts'o return;
2380db1ff22STheodore Ts'o
23967cf5b09STao Ma BUG_ON(!EXT4_I(inode)->i_inline_off);
24067cf5b09STao Ma BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
24167cf5b09STao Ma
24267cf5b09STao Ma raw_inode = ext4_raw_inode(iloc);
24367cf5b09STao Ma buffer += pos;
24467cf5b09STao Ma
24567cf5b09STao Ma if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
24667cf5b09STao Ma cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
24767cf5b09STao Ma EXT4_MIN_INLINE_DATA_SIZE - pos : len;
24867cf5b09STao Ma memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
24967cf5b09STao Ma
25067cf5b09STao Ma len -= cp_len;
25167cf5b09STao Ma buffer += cp_len;
25267cf5b09STao Ma pos += cp_len;
25367cf5b09STao Ma }
25467cf5b09STao Ma
25567cf5b09STao Ma if (!len)
25667cf5b09STao Ma return;
25767cf5b09STao Ma
25867cf5b09STao Ma pos -= EXT4_MIN_INLINE_DATA_SIZE;
25967cf5b09STao Ma header = IHDR(inode, raw_inode);
26067cf5b09STao Ma entry = (struct ext4_xattr_entry *)((void *)raw_inode +
26167cf5b09STao Ma EXT4_I(inode)->i_inline_off);
26267cf5b09STao Ma
26367cf5b09STao Ma memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
26467cf5b09STao Ma buffer, len);
26567cf5b09STao Ma }
26667cf5b09STao Ma
ext4_create_inline_data(handle_t * handle,struct inode * inode,unsigned len)26767cf5b09STao Ma static int ext4_create_inline_data(handle_t *handle,
26867cf5b09STao Ma struct inode *inode, unsigned len)
26967cf5b09STao Ma {
27067cf5b09STao Ma int error;
27167cf5b09STao Ma void *value = NULL;
27267cf5b09STao Ma struct ext4_xattr_ibody_find is = {
27367cf5b09STao Ma .s = { .not_found = -ENODATA, },
27467cf5b09STao Ma };
27567cf5b09STao Ma struct ext4_xattr_info i = {
27667cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
27767cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
27867cf5b09STao Ma };
27967cf5b09STao Ma
28067cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
28167cf5b09STao Ma if (error)
28267cf5b09STao Ma return error;
28367cf5b09STao Ma
2845d601255Sliang xie BUFFER_TRACE(is.iloc.bh, "get_write_access");
285188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
286188c299eSJan Kara EXT4_JTR_NONE);
28767cf5b09STao Ma if (error)
28867cf5b09STao Ma goto out;
28967cf5b09STao Ma
29067cf5b09STao Ma if (len > EXT4_MIN_INLINE_DATA_SIZE) {
291bd9926e8STheodore Ts'o value = EXT4_ZERO_XATTR_VALUE;
29267cf5b09STao Ma len -= EXT4_MIN_INLINE_DATA_SIZE;
29367cf5b09STao Ma } else {
29467cf5b09STao Ma value = "";
29567cf5b09STao Ma len = 0;
29667cf5b09STao Ma }
29767cf5b09STao Ma
2987ca4fcbaSkyoungho koo /* Insert the xttr entry. */
29967cf5b09STao Ma i.value = value;
30067cf5b09STao Ma i.value_len = len;
30167cf5b09STao Ma
30267cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
30367cf5b09STao Ma if (error)
30467cf5b09STao Ma goto out;
30567cf5b09STao Ma
30667cf5b09STao Ma BUG_ON(!is.s.not_found);
30767cf5b09STao Ma
308310c097cSRitesh Harjani error = ext4_xattr_ibody_set(handle, inode, &i, &is);
30967cf5b09STao Ma if (error) {
31067cf5b09STao Ma if (error == -ENOSPC)
31167cf5b09STao Ma ext4_clear_inode_state(inode,
31267cf5b09STao Ma EXT4_STATE_MAY_INLINE_DATA);
31367cf5b09STao Ma goto out;
31467cf5b09STao Ma }
31567cf5b09STao Ma
31667cf5b09STao Ma memset((void *)ext4_raw_inode(&is.iloc)->i_block,
31767cf5b09STao Ma 0, EXT4_MIN_INLINE_DATA_SIZE);
31867cf5b09STao Ma
31967cf5b09STao Ma EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
32067cf5b09STao Ma (void *)ext4_raw_inode(&is.iloc));
32167cf5b09STao Ma EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
32267cf5b09STao Ma ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
32367cf5b09STao Ma ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
32467cf5b09STao Ma get_bh(is.iloc.bh);
32567cf5b09STao Ma error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
32667cf5b09STao Ma
32767cf5b09STao Ma out:
32867cf5b09STao Ma brelse(is.iloc.bh);
32967cf5b09STao Ma return error;
33067cf5b09STao Ma }
33167cf5b09STao Ma
ext4_update_inline_data(handle_t * handle,struct inode * inode,unsigned int len)33267cf5b09STao Ma static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
33367cf5b09STao Ma unsigned int len)
33467cf5b09STao Ma {
33567cf5b09STao Ma int error;
33667cf5b09STao Ma void *value = NULL;
33767cf5b09STao Ma struct ext4_xattr_ibody_find is = {
33867cf5b09STao Ma .s = { .not_found = -ENODATA, },
33967cf5b09STao Ma };
34067cf5b09STao Ma struct ext4_xattr_info i = {
34167cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
34267cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
34367cf5b09STao Ma };
34467cf5b09STao Ma
34567cf5b09STao Ma /* If the old space is ok, write the data directly. */
34667cf5b09STao Ma if (len <= EXT4_I(inode)->i_inline_size)
34767cf5b09STao Ma return 0;
34867cf5b09STao Ma
34967cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
35067cf5b09STao Ma if (error)
35167cf5b09STao Ma return error;
35267cf5b09STao Ma
35367cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
35467cf5b09STao Ma if (error)
35567cf5b09STao Ma goto out;
35667cf5b09STao Ma
35767cf5b09STao Ma BUG_ON(is.s.not_found);
35867cf5b09STao Ma
35967cf5b09STao Ma len -= EXT4_MIN_INLINE_DATA_SIZE;
36067cf5b09STao Ma value = kzalloc(len, GFP_NOFS);
361578620f4SDan Carpenter if (!value) {
362578620f4SDan Carpenter error = -ENOMEM;
36367cf5b09STao Ma goto out;
364578620f4SDan Carpenter }
36567cf5b09STao Ma
36667cf5b09STao Ma error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
36767cf5b09STao Ma value, len);
3682a534e1dSTheodore Ts'o if (error < 0)
36967cf5b09STao Ma goto out;
37067cf5b09STao Ma
3715d601255Sliang xie BUFFER_TRACE(is.iloc.bh, "get_write_access");
372188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
373188c299eSJan Kara EXT4_JTR_NONE);
37467cf5b09STao Ma if (error)
37567cf5b09STao Ma goto out;
37667cf5b09STao Ma
377b483bb77SRandy Dunlap /* Update the xattr entry. */
37867cf5b09STao Ma i.value = value;
37967cf5b09STao Ma i.value_len = len;
38067cf5b09STao Ma
381310c097cSRitesh Harjani error = ext4_xattr_ibody_set(handle, inode, &i, &is);
38267cf5b09STao Ma if (error)
38367cf5b09STao Ma goto out;
38467cf5b09STao Ma
38567cf5b09STao Ma EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
38667cf5b09STao Ma (void *)ext4_raw_inode(&is.iloc));
38767cf5b09STao Ma EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
38867cf5b09STao Ma le32_to_cpu(is.s.here->e_value_size);
38967cf5b09STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
39067cf5b09STao Ma get_bh(is.iloc.bh);
39167cf5b09STao Ma error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
39267cf5b09STao Ma
39367cf5b09STao Ma out:
39467cf5b09STao Ma kfree(value);
39567cf5b09STao Ma brelse(is.iloc.bh);
39667cf5b09STao Ma return error;
39767cf5b09STao Ma }
39867cf5b09STao Ma
ext4_prepare_inline_data(handle_t * handle,struct inode * inode,unsigned int len)399c197855eSStephen Hemminger static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
40067cf5b09STao Ma unsigned int len)
40167cf5b09STao Ma {
402c755e251STheodore Ts'o int ret, size, no_expand;
40367cf5b09STao Ma struct ext4_inode_info *ei = EXT4_I(inode);
40467cf5b09STao Ma
40567cf5b09STao Ma if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
40667cf5b09STao Ma return -ENOSPC;
40767cf5b09STao Ma
40867cf5b09STao Ma size = ext4_get_max_inline_size(inode);
40967cf5b09STao Ma if (size < len)
41067cf5b09STao Ma return -ENOSPC;
41167cf5b09STao Ma
412c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
41367cf5b09STao Ma
41467cf5b09STao Ma if (ei->i_inline_off)
41567cf5b09STao Ma ret = ext4_update_inline_data(handle, inode, len);
41667cf5b09STao Ma else
41767cf5b09STao Ma ret = ext4_create_inline_data(handle, inode, len);
41867cf5b09STao Ma
419c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
42067cf5b09STao Ma return ret;
42167cf5b09STao Ma }
42267cf5b09STao Ma
ext4_destroy_inline_data_nolock(handle_t * handle,struct inode * inode)42367cf5b09STao Ma static int ext4_destroy_inline_data_nolock(handle_t *handle,
42467cf5b09STao Ma struct inode *inode)
42567cf5b09STao Ma {
42667cf5b09STao Ma struct ext4_inode_info *ei = EXT4_I(inode);
42767cf5b09STao Ma struct ext4_xattr_ibody_find is = {
42867cf5b09STao Ma .s = { .not_found = 0, },
42967cf5b09STao Ma };
43067cf5b09STao Ma struct ext4_xattr_info i = {
43167cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
43267cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
43367cf5b09STao Ma .value = NULL,
43467cf5b09STao Ma .value_len = 0,
43567cf5b09STao Ma };
43667cf5b09STao Ma int error;
43767cf5b09STao Ma
43867cf5b09STao Ma if (!ei->i_inline_off)
43967cf5b09STao Ma return 0;
44067cf5b09STao Ma
44167cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
44267cf5b09STao Ma if (error)
44367cf5b09STao Ma return error;
44467cf5b09STao Ma
44567cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
44667cf5b09STao Ma if (error)
44767cf5b09STao Ma goto out;
44867cf5b09STao Ma
4495d601255Sliang xie BUFFER_TRACE(is.iloc.bh, "get_write_access");
450188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
451188c299eSJan Kara EXT4_JTR_NONE);
45267cf5b09STao Ma if (error)
45367cf5b09STao Ma goto out;
45467cf5b09STao Ma
455310c097cSRitesh Harjani error = ext4_xattr_ibody_set(handle, inode, &i, &is);
45667cf5b09STao Ma if (error)
45767cf5b09STao Ma goto out;
45867cf5b09STao Ma
45967cf5b09STao Ma memset((void *)ext4_raw_inode(&is.iloc)->i_block,
46067cf5b09STao Ma 0, EXT4_MIN_INLINE_DATA_SIZE);
4616e8ab72aSTheodore Ts'o memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
46267cf5b09STao Ma
463e2b911c5SDarrick J. Wong if (ext4_has_feature_extents(inode->i_sb)) {
46467cf5b09STao Ma if (S_ISDIR(inode->i_mode) ||
46567cf5b09STao Ma S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
46667cf5b09STao Ma ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
46767cf5b09STao Ma ext4_ext_tree_init(handle, inode);
46867cf5b09STao Ma }
46967cf5b09STao Ma }
47067cf5b09STao Ma ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
47167cf5b09STao Ma
47267cf5b09STao Ma get_bh(is.iloc.bh);
47367cf5b09STao Ma error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
47467cf5b09STao Ma
47567cf5b09STao Ma EXT4_I(inode)->i_inline_off = 0;
47667cf5b09STao Ma EXT4_I(inode)->i_inline_size = 0;
47767cf5b09STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
47867cf5b09STao Ma out:
47967cf5b09STao Ma brelse(is.iloc.bh);
48067cf5b09STao Ma if (error == -ENODATA)
48167cf5b09STao Ma error = 0;
48267cf5b09STao Ma return error;
48367cf5b09STao Ma }
48467cf5b09STao Ma
ext4_read_inline_folio(struct inode * inode,struct folio * folio)4856b87fbe4SMatthew Wilcox static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
48646c7f254STao Ma {
48746c7f254STao Ma void *kaddr;
48846c7f254STao Ma int ret = 0;
48946c7f254STao Ma size_t len;
49046c7f254STao Ma struct ext4_iloc iloc;
49146c7f254STao Ma
4926b87fbe4SMatthew Wilcox BUG_ON(!folio_test_locked(folio));
49346c7f254STao Ma BUG_ON(!ext4_has_inline_data(inode));
4946b87fbe4SMatthew Wilcox BUG_ON(folio->index);
49546c7f254STao Ma
49646c7f254STao Ma if (!EXT4_I(inode)->i_inline_off) {
49746c7f254STao Ma ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
49846c7f254STao Ma inode->i_ino);
49946c7f254STao Ma goto out;
50046c7f254STao Ma }
50146c7f254STao Ma
50246c7f254STao Ma ret = ext4_get_inode_loc(inode, &iloc);
50346c7f254STao Ma if (ret)
50446c7f254STao Ma goto out;
50546c7f254STao Ma
50646c7f254STao Ma len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
5076b87fbe4SMatthew Wilcox BUG_ON(len > PAGE_SIZE);
5086b87fbe4SMatthew Wilcox kaddr = kmap_local_folio(folio, 0);
50946c7f254STao Ma ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
510a4fc4a0cSMatthew Wilcox (Oracle) kaddr = folio_zero_tail(folio, len, kaddr + len);
5116b87fbe4SMatthew Wilcox kunmap_local(kaddr);
5126b87fbe4SMatthew Wilcox folio_mark_uptodate(folio);
51346c7f254STao Ma brelse(iloc.bh);
51446c7f254STao Ma
51546c7f254STao Ma out:
51646c7f254STao Ma return ret;
51746c7f254STao Ma }
51846c7f254STao Ma
ext4_readpage_inline(struct inode * inode,struct folio * folio)5193edde93eSMatthew Wilcox int ext4_readpage_inline(struct inode *inode, struct folio *folio)
52046c7f254STao Ma {
52146c7f254STao Ma int ret = 0;
52246c7f254STao Ma
52346c7f254STao Ma down_read(&EXT4_I(inode)->xattr_sem);
52446c7f254STao Ma if (!ext4_has_inline_data(inode)) {
52546c7f254STao Ma up_read(&EXT4_I(inode)->xattr_sem);
52646c7f254STao Ma return -EAGAIN;
52746c7f254STao Ma }
52846c7f254STao Ma
52946c7f254STao Ma /*
53046c7f254STao Ma * Current inline data can only exist in the 1st page,
53146c7f254STao Ma * So for all the other pages, just set them uptodate.
53246c7f254STao Ma */
5333edde93eSMatthew Wilcox if (!folio->index)
5346b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
5353edde93eSMatthew Wilcox else if (!folio_test_uptodate(folio)) {
5363edde93eSMatthew Wilcox folio_zero_segment(folio, 0, folio_size(folio));
5373edde93eSMatthew Wilcox folio_mark_uptodate(folio);
53846c7f254STao Ma }
53946c7f254STao Ma
54046c7f254STao Ma up_read(&EXT4_I(inode)->xattr_sem);
54146c7f254STao Ma
5423edde93eSMatthew Wilcox folio_unlock(folio);
54346c7f254STao Ma return ret >= 0 ? 0 : ret;
54446c7f254STao Ma }
54546c7f254STao Ma
ext4_convert_inline_data_to_extent(struct address_space * mapping,struct inode * inode)546f19d5870STao Ma static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
547832ee62dSMatthew Wilcox (Oracle) struct inode *inode)
548f19d5870STao Ma {
549c755e251STheodore Ts'o int ret, needed_blocks, no_expand;
550f19d5870STao Ma handle_t *handle = NULL;
551f19d5870STao Ma int retries = 0, sem_held = 0;
55283eba701SMatthew Wilcox struct folio *folio = NULL;
553f19d5870STao Ma unsigned from, to;
554f19d5870STao Ma struct ext4_iloc iloc;
555f19d5870STao Ma
556f19d5870STao Ma if (!ext4_has_inline_data(inode)) {
557f19d5870STao Ma /*
558f19d5870STao Ma * clear the flag so that no new write
559f19d5870STao Ma * will trap here again.
560f19d5870STao Ma */
561f19d5870STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
562f19d5870STao Ma return 0;
563f19d5870STao Ma }
564f19d5870STao Ma
565f19d5870STao Ma needed_blocks = ext4_writepage_trans_blocks(inode);
566f19d5870STao Ma
567f19d5870STao Ma ret = ext4_get_inode_loc(inode, &iloc);
568f19d5870STao Ma if (ret)
569f19d5870STao Ma return ret;
570f19d5870STao Ma
571f19d5870STao Ma retry:
5729924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
573f19d5870STao Ma if (IS_ERR(handle)) {
574f19d5870STao Ma ret = PTR_ERR(handle);
575f19d5870STao Ma handle = NULL;
576f19d5870STao Ma goto out;
577f19d5870STao Ma }
578f19d5870STao Ma
579f19d5870STao Ma /* We cannot recurse into the filesystem as the transaction is already
580f19d5870STao Ma * started */
58183eba701SMatthew Wilcox folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
58283eba701SMatthew Wilcox mapping_gfp_mask(mapping));
5837fa8a8eeSLinus Torvalds if (IS_ERR(folio)) {
5847fa8a8eeSLinus Torvalds ret = PTR_ERR(folio);
5857fa8a8eeSLinus Torvalds goto out_nofolio;
586f19d5870STao Ma }
587f19d5870STao Ma
588c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
589f19d5870STao Ma sem_held = 1;
590f19d5870STao Ma /* If some one has already done this for us, just exit. */
591f19d5870STao Ma if (!ext4_has_inline_data(inode)) {
592f19d5870STao Ma ret = 0;
593f19d5870STao Ma goto out;
594f19d5870STao Ma }
595f19d5870STao Ma
596f19d5870STao Ma from = 0;
597f19d5870STao Ma to = ext4_get_inline_size(inode);
59883eba701SMatthew Wilcox if (!folio_test_uptodate(folio)) {
5996b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
600f19d5870STao Ma if (ret < 0)
601f19d5870STao Ma goto out;
602f19d5870STao Ma }
603f19d5870STao Ma
604f19d5870STao Ma ret = ext4_destroy_inline_data_nolock(handle, inode);
605f19d5870STao Ma if (ret)
606f19d5870STao Ma goto out;
607f19d5870STao Ma
608705965bdSJan Kara if (ext4_should_dioread_nolock(inode)) {
609cb3de5fcSShida Zhang ret = ext4_block_write_begin(handle, folio, from, to,
610705965bdSJan Kara ext4_get_block_unwritten);
611705965bdSJan Kara } else
612cb3de5fcSShida Zhang ret = ext4_block_write_begin(handle, folio, from, to,
613cb3de5fcSShida Zhang ext4_get_block);
614f19d5870STao Ma
615f19d5870STao Ma if (!ret && ext4_should_journal_data(inode)) {
61683eba701SMatthew Wilcox ret = ext4_walk_page_buffers(handle, inode,
61783eba701SMatthew Wilcox folio_buffers(folio), from, to,
61883eba701SMatthew Wilcox NULL, do_journal_get_write_access);
619f19d5870STao Ma }
620f19d5870STao Ma
621f19d5870STao Ma if (ret) {
62283eba701SMatthew Wilcox folio_unlock(folio);
62383eba701SMatthew Wilcox folio_put(folio);
62483eba701SMatthew Wilcox folio = NULL;
625f19d5870STao Ma ext4_orphan_add(handle, inode);
626c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
627f19d5870STao Ma sem_held = 0;
628f19d5870STao Ma ext4_journal_stop(handle);
629f19d5870STao Ma handle = NULL;
630f19d5870STao Ma ext4_truncate_failed_write(inode);
631f19d5870STao Ma /*
632f19d5870STao Ma * If truncate failed early the inode might
633f19d5870STao Ma * still be on the orphan list; we need to
634f19d5870STao Ma * make sure the inode is removed from the
635f19d5870STao Ma * orphan list in that case.
636f19d5870STao Ma */
637f19d5870STao Ma if (inode->i_nlink)
638f19d5870STao Ma ext4_orphan_del(NULL, inode);
639f19d5870STao Ma }
640f19d5870STao Ma
641f19d5870STao Ma if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
642f19d5870STao Ma goto retry;
643f19d5870STao Ma
64483eba701SMatthew Wilcox if (folio)
64583eba701SMatthew Wilcox block_commit_write(folio, from, to);
646f19d5870STao Ma out:
64783eba701SMatthew Wilcox if (folio) {
64883eba701SMatthew Wilcox folio_unlock(folio);
64983eba701SMatthew Wilcox folio_put(folio);
650f19d5870STao Ma }
6517fa8a8eeSLinus Torvalds out_nofolio:
652f19d5870STao Ma if (sem_held)
653c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
654f19d5870STao Ma if (handle)
655f19d5870STao Ma ext4_journal_stop(handle);
656f19d5870STao Ma brelse(iloc.bh);
657f19d5870STao Ma return ret;
658f19d5870STao Ma }
659f19d5870STao Ma
660f9bdb042SJulian Sun /*
661f9bdb042SJulian Sun * Prepare the write for the inline data.
662f9bdb042SJulian Sun * If the data can be written into the inode, we just read
663f9bdb042SJulian Sun * the page and make it uptodate, and start the journal.
664f9bdb042SJulian Sun * Otherwise read the page, makes it dirty so that it can be
665f9bdb042SJulian Sun * handle in writepages(the i_disksize update is left to the
666f9bdb042SJulian Sun * normal ext4_da_write_end).
667f9bdb042SJulian Sun */
ext4_generic_write_inline_data(struct address_space * mapping,struct inode * inode,loff_t pos,unsigned len,struct folio ** foliop,void ** fsdata,bool da)668f9bdb042SJulian Sun int ext4_generic_write_inline_data(struct address_space *mapping,
6693db572f7SJulian Sun struct inode *inode,
6703db572f7SJulian Sun loff_t pos, unsigned len,
6713db572f7SJulian Sun struct folio **foliop,
6723db572f7SJulian Sun void **fsdata, bool da)
6733db572f7SJulian Sun {
6743db572f7SJulian Sun int ret;
6753db572f7SJulian Sun handle_t *handle;
6763db572f7SJulian Sun struct folio *folio;
6773db572f7SJulian Sun struct ext4_iloc iloc;
6783db572f7SJulian Sun int retries = 0;
6793db572f7SJulian Sun
6803db572f7SJulian Sun ret = ext4_get_inode_loc(inode, &iloc);
6813db572f7SJulian Sun if (ret)
6823db572f7SJulian Sun return ret;
6833db572f7SJulian Sun
6843db572f7SJulian Sun retry_journal:
6853db572f7SJulian Sun handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
6863db572f7SJulian Sun if (IS_ERR(handle)) {
6873db572f7SJulian Sun ret = PTR_ERR(handle);
6883db572f7SJulian Sun goto out_release_bh;
6893db572f7SJulian Sun }
6903db572f7SJulian Sun
6913db572f7SJulian Sun ret = ext4_prepare_inline_data(handle, inode, pos + len);
6923db572f7SJulian Sun if (ret && ret != -ENOSPC)
6933db572f7SJulian Sun goto out_stop_journal;
6943db572f7SJulian Sun
6953db572f7SJulian Sun if (ret == -ENOSPC) {
6963db572f7SJulian Sun ext4_journal_stop(handle);
6973db572f7SJulian Sun if (!da) {
6983db572f7SJulian Sun brelse(iloc.bh);
6993db572f7SJulian Sun /* Retry inside */
7003db572f7SJulian Sun return ext4_convert_inline_data_to_extent(mapping, inode);
7013db572f7SJulian Sun }
7023db572f7SJulian Sun
7033db572f7SJulian Sun ret = ext4_da_convert_inline_data_to_extent(mapping, inode, fsdata);
7043db572f7SJulian Sun if (ret == -ENOSPC &&
7053db572f7SJulian Sun ext4_should_retry_alloc(inode->i_sb, &retries))
7063db572f7SJulian Sun goto retry_journal;
7073db572f7SJulian Sun goto out_release_bh;
7083db572f7SJulian Sun }
7093db572f7SJulian Sun
7103db572f7SJulian Sun folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
7113db572f7SJulian Sun mapping_gfp_mask(mapping));
7123db572f7SJulian Sun if (IS_ERR(folio)) {
7133db572f7SJulian Sun ret = PTR_ERR(folio);
7143db572f7SJulian Sun goto out_stop_journal;
7153db572f7SJulian Sun }
7163db572f7SJulian Sun
7173db572f7SJulian Sun down_read(&EXT4_I(inode)->xattr_sem);
7183db572f7SJulian Sun /* Someone else had converted it to extent */
7193db572f7SJulian Sun if (!ext4_has_inline_data(inode)) {
7203db572f7SJulian Sun ret = 0;
7213db572f7SJulian Sun goto out_release_folio;
7223db572f7SJulian Sun }
7233db572f7SJulian Sun
7243db572f7SJulian Sun if (!folio_test_uptodate(folio)) {
7253db572f7SJulian Sun ret = ext4_read_inline_folio(inode, folio);
7263db572f7SJulian Sun if (ret < 0)
7273db572f7SJulian Sun goto out_release_folio;
7283db572f7SJulian Sun }
7293db572f7SJulian Sun
7303db572f7SJulian Sun ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh, EXT4_JTR_NONE);
7313db572f7SJulian Sun if (ret)
7323db572f7SJulian Sun goto out_release_folio;
7333db572f7SJulian Sun *foliop = folio;
7343db572f7SJulian Sun up_read(&EXT4_I(inode)->xattr_sem);
7353db572f7SJulian Sun brelse(iloc.bh);
7363db572f7SJulian Sun return 1;
7373db572f7SJulian Sun
7383db572f7SJulian Sun out_release_folio:
7393db572f7SJulian Sun up_read(&EXT4_I(inode)->xattr_sem);
7403db572f7SJulian Sun folio_unlock(folio);
7413db572f7SJulian Sun folio_put(folio);
7423db572f7SJulian Sun out_stop_journal:
7433db572f7SJulian Sun ext4_journal_stop(handle);
7443db572f7SJulian Sun out_release_bh:
7453db572f7SJulian Sun brelse(iloc.bh);
7463db572f7SJulian Sun return ret;
7473db572f7SJulian Sun }
7483db572f7SJulian Sun
749f19d5870STao Ma /*
750f19d5870STao Ma * Try to write data in the inode.
751f19d5870STao Ma * If the inode has inline data, check whether the new write can be
752f19d5870STao Ma * in the inode also. If not, create the page the handle, move the data
753f19d5870STao Ma * to the page make it update and let the later codes create extent for it.
754f19d5870STao Ma */
ext4_try_to_write_inline_data(struct address_space * mapping,struct inode * inode,loff_t pos,unsigned len,struct folio ** foliop)755f19d5870STao Ma int ext4_try_to_write_inline_data(struct address_space *mapping,
756f19d5870STao Ma struct inode *inode,
757f19d5870STao Ma loff_t pos, unsigned len,
7581da86618SMatthew Wilcox (Oracle) struct folio **foliop)
759f19d5870STao Ma {
760f19d5870STao Ma if (pos + len > ext4_get_max_inline_size(inode))
761832ee62dSMatthew Wilcox (Oracle) return ext4_convert_inline_data_to_extent(mapping, inode);
76230cbe84dSJulian Sun return ext4_generic_write_inline_data(mapping, inode, pos, len,
76330cbe84dSJulian Sun foliop, NULL, false);
764f19d5870STao Ma }
765f19d5870STao Ma
ext4_write_inline_data_end(struct inode * inode,loff_t pos,unsigned len,unsigned copied,struct folio * folio)766f19d5870STao Ma int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
767d19500daSRitesh Harjani unsigned copied, struct folio *folio)
768f19d5870STao Ma {
7696984aef5SZhang Yi handle_t *handle = ext4_journal_current_handle();
7706984aef5SZhang Yi int no_expand;
771f19d5870STao Ma void *kaddr;
772f19d5870STao Ma struct ext4_iloc iloc;
7736984aef5SZhang Yi int ret = 0, ret2;
774f19d5870STao Ma
7756b90d413SMatthew Wilcox if (unlikely(copied < len) && !folio_test_uptodate(folio))
776f19d5870STao Ma copied = 0;
777f19d5870STao Ma
7786984aef5SZhang Yi if (likely(copied)) {
779f19d5870STao Ma ret = ext4_get_inode_loc(inode, &iloc);
780f19d5870STao Ma if (ret) {
7816b90d413SMatthew Wilcox folio_unlock(folio);
7826b90d413SMatthew Wilcox folio_put(folio);
783f19d5870STao Ma ext4_std_error(inode->i_sb, ret);
784f19d5870STao Ma goto out;
785f19d5870STao Ma }
786c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
787f19d5870STao Ma BUG_ON(!ext4_has_inline_data(inode));
788f19d5870STao Ma
789a54c4613STheodore Ts'o /*
79011ef08c9STheodore Ts'o * ei->i_inline_off may have changed since
79111ef08c9STheodore Ts'o * ext4_write_begin() called
79211ef08c9STheodore Ts'o * ext4_try_to_write_inline_data()
793a54c4613STheodore Ts'o */
794a54c4613STheodore Ts'o (void) ext4_find_inline_data_nolock(inode);
795a54c4613STheodore Ts'o
7966b90d413SMatthew Wilcox kaddr = kmap_local_folio(folio, 0);
79755ce2f64SZhang Yi ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
7986b90d413SMatthew Wilcox kunmap_local(kaddr);
7996b90d413SMatthew Wilcox folio_mark_uptodate(folio);
8006b90d413SMatthew Wilcox /* clear dirty flag so that writepages wouldn't work for us. */
8016b90d413SMatthew Wilcox folio_clear_dirty(folio);
802f19d5870STao Ma
803c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
804f19d5870STao Ma brelse(iloc.bh);
80555ce2f64SZhang Yi
8066984aef5SZhang Yi /*
8076b90d413SMatthew Wilcox * It's important to update i_size while still holding folio
8086984aef5SZhang Yi * lock: page writeout could otherwise come in and zero
8096984aef5SZhang Yi * beyond i_size.
8106984aef5SZhang Yi */
8116984aef5SZhang Yi ext4_update_inode_size(inode, pos + copied);
8126984aef5SZhang Yi }
8136b90d413SMatthew Wilcox folio_unlock(folio);
8146b90d413SMatthew Wilcox folio_put(folio);
8156984aef5SZhang Yi
8166984aef5SZhang Yi /*
8176b90d413SMatthew Wilcox * Don't mark the inode dirty under folio lock. First, it unnecessarily
8186b90d413SMatthew Wilcox * makes the holding time of folio lock longer. Second, it forces lock
8196b90d413SMatthew Wilcox * ordering of folio lock and transaction start for journaling
8206984aef5SZhang Yi * filesystems.
8216984aef5SZhang Yi */
8226984aef5SZhang Yi if (likely(copied))
823362eca70STheodore Ts'o mark_inode_dirty(inode);
824f19d5870STao Ma out:
8256984aef5SZhang Yi /*
8266984aef5SZhang Yi * If we didn't copy as much data as expected, we need to trim back
8276984aef5SZhang Yi * size of xattr containing inline data.
8286984aef5SZhang Yi */
8296984aef5SZhang Yi if (pos + len > inode->i_size && ext4_can_truncate(inode))
8306984aef5SZhang Yi ext4_orphan_add(handle, inode);
8316984aef5SZhang Yi
8326984aef5SZhang Yi ret2 = ext4_journal_stop(handle);
8336984aef5SZhang Yi if (!ret)
8346984aef5SZhang Yi ret = ret2;
8356984aef5SZhang Yi if (pos + len > inode->i_size) {
8366984aef5SZhang Yi ext4_truncate_failed_write(inode);
8376984aef5SZhang Yi /*
8386984aef5SZhang Yi * If truncate failed early the inode might still be
8396984aef5SZhang Yi * on the orphan list; we need to make sure the inode
8406984aef5SZhang Yi * is removed from the orphan list in that case.
8416984aef5SZhang Yi */
8426984aef5SZhang Yi if (inode->i_nlink)
8436984aef5SZhang Yi ext4_orphan_del(NULL, inode);
8446984aef5SZhang Yi }
8456984aef5SZhang Yi return ret ? ret : copied;
846f19d5870STao Ma }
847f19d5870STao Ma
8489c3569b5STao Ma /*
8499c3569b5STao Ma * Try to make the page cache and handle ready for the inline data case.
8509c3569b5STao Ma * We can call this function in 2 cases:
8519c3569b5STao Ma * 1. The inode is created and the first write exceeds inline size. We can
8529c3569b5STao Ma * clear the inode state safely.
8539c3569b5STao Ma * 2. The inode has inline data, then we need to read the data, make it
8549c3569b5STao Ma * update and dirty so that ext4_da_writepages can handle it. We don't
8553088e5a5SBhaskar Chowdhury * need to start the journal since the file's metadata isn't changed now.
8569c3569b5STao Ma */
ext4_da_convert_inline_data_to_extent(struct address_space * mapping,struct inode * inode,void ** fsdata)8579c3569b5STao Ma static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
8589c3569b5STao Ma struct inode *inode,
8599c3569b5STao Ma void **fsdata)
8609c3569b5STao Ma {
8619c3569b5STao Ma int ret = 0, inline_size;
8624ed9b598SMatthew Wilcox struct folio *folio;
8639c3569b5STao Ma
8644ed9b598SMatthew Wilcox folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN,
8654ed9b598SMatthew Wilcox mapping_gfp_mask(mapping));
8667fa8a8eeSLinus Torvalds if (IS_ERR(folio))
8677fa8a8eeSLinus Torvalds return PTR_ERR(folio);
8689c3569b5STao Ma
8699c3569b5STao Ma down_read(&EXT4_I(inode)->xattr_sem);
8709c3569b5STao Ma if (!ext4_has_inline_data(inode)) {
8719c3569b5STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
8729c3569b5STao Ma goto out;
8739c3569b5STao Ma }
8749c3569b5STao Ma
8759c3569b5STao Ma inline_size = ext4_get_inline_size(inode);
8769c3569b5STao Ma
8774ed9b598SMatthew Wilcox if (!folio_test_uptodate(folio)) {
8786b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
8799c3569b5STao Ma if (ret < 0)
8809c3569b5STao Ma goto out;
8819c3569b5STao Ma }
8829c3569b5STao Ma
883cb3de5fcSShida Zhang ret = ext4_block_write_begin(NULL, folio, 0, inline_size,
8849c3569b5STao Ma ext4_da_get_block_prep);
8859c3569b5STao Ma if (ret) {
88650db71abSDmitry Monakhov up_read(&EXT4_I(inode)->xattr_sem);
8874ed9b598SMatthew Wilcox folio_unlock(folio);
8884ed9b598SMatthew Wilcox folio_put(folio);
8899c3569b5STao Ma ext4_truncate_failed_write(inode);
89050db71abSDmitry Monakhov return ret;
8919c3569b5STao Ma }
8929c3569b5STao Ma
8934ed9b598SMatthew Wilcox folio_mark_dirty(folio);
8944ed9b598SMatthew Wilcox folio_mark_uptodate(folio);
8959c3569b5STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
8969c3569b5STao Ma *fsdata = (void *)CONVERT_INLINE_DATA;
8979c3569b5STao Ma
8989c3569b5STao Ma out:
8999c3569b5STao Ma up_read(&EXT4_I(inode)->xattr_sem);
9004ed9b598SMatthew Wilcox if (folio) {
9014ed9b598SMatthew Wilcox folio_unlock(folio);
9024ed9b598SMatthew Wilcox folio_put(folio);
9039c3569b5STao Ma }
9049c3569b5STao Ma return ret;
9059c3569b5STao Ma }
9069c3569b5STao Ma
9073c47d541STao Ma #ifdef INLINE_DIR_DEBUG
ext4_show_inline_dir(struct inode * dir,struct buffer_head * bh,void * inline_start,int inline_size)9083c47d541STao Ma void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
9093c47d541STao Ma void *inline_start, int inline_size)
9103c47d541STao Ma {
9113c47d541STao Ma int offset;
9123c47d541STao Ma unsigned short de_len;
9133c47d541STao Ma struct ext4_dir_entry_2 *de = inline_start;
9143c47d541STao Ma void *dlimit = inline_start + inline_size;
9153c47d541STao Ma
9163c47d541STao Ma trace_printk("inode %lu\n", dir->i_ino);
9173c47d541STao Ma offset = 0;
9183c47d541STao Ma while ((void *)de < dlimit) {
9193c47d541STao Ma de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
92080cfb71eSRasmus Villemoes trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
9213c47d541STao Ma offset, de_len, de->name_len, de->name,
9223c47d541STao Ma de->name_len, le32_to_cpu(de->inode));
9233c47d541STao Ma if (ext4_check_dir_entry(dir, NULL, de, bh,
9243c47d541STao Ma inline_start, inline_size, offset))
9253c47d541STao Ma BUG();
9263c47d541STao Ma
9273c47d541STao Ma offset += de_len;
9283c47d541STao Ma de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
9293c47d541STao Ma }
9303c47d541STao Ma }
9313c47d541STao Ma #else
9323c47d541STao Ma #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
9333c47d541STao Ma #endif
9343c47d541STao Ma
9353c47d541STao Ma /*
9363c47d541STao Ma * Add a new entry into a inline dir.
9373c47d541STao Ma * It will return -ENOSPC if no space is available, and -EIO
9383c47d541STao Ma * and -EEXIST if directory entry already exists.
9393c47d541STao Ma */
ext4_add_dirent_to_inline(handle_t * handle,struct ext4_filename * fname,struct inode * dir,struct inode * inode,struct ext4_iloc * iloc,void * inline_start,int inline_size)9403c47d541STao Ma static int ext4_add_dirent_to_inline(handle_t *handle,
9415b643f9cSTheodore Ts'o struct ext4_filename *fname,
94256a04915STheodore Ts'o struct inode *dir,
9433c47d541STao Ma struct inode *inode,
9443c47d541STao Ma struct ext4_iloc *iloc,
9453c47d541STao Ma void *inline_start, int inline_size)
9463c47d541STao Ma {
9473c47d541STao Ma int err;
9483c47d541STao Ma struct ext4_dir_entry_2 *de;
9493c47d541STao Ma
950477aa77cSKemeng Shi err = ext4_find_dest_de(dir, iloc->bh, inline_start,
9515b643f9cSTheodore Ts'o inline_size, fname, &de);
9523c47d541STao Ma if (err)
9533c47d541STao Ma return err;
9543c47d541STao Ma
9555d601255Sliang xie BUFFER_TRACE(iloc->bh, "get_write_access");
956188c299eSJan Kara err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
957188c299eSJan Kara EXT4_JTR_NONE);
9583c47d541STao Ma if (err)
9593c47d541STao Ma return err;
960471fbbeaSDaniel Rosenberg ext4_insert_dentry(dir, inode, de, inline_size, fname);
9613c47d541STao Ma
9623c47d541STao Ma ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
9633c47d541STao Ma
9643c47d541STao Ma /*
9653c47d541STao Ma * XXX shouldn't update any times until successful
9663c47d541STao Ma * completion of syscall, but too many callers depend
9673c47d541STao Ma * on this.
9683c47d541STao Ma *
9693c47d541STao Ma * XXX similarly, too many callers depend on
9703c47d541STao Ma * ext4_new_inode() setting the times, but error
9713c47d541STao Ma * recovery deletes the inode, so the worst that can
9723c47d541STao Ma * happen is that the times are slightly out of date
9733c47d541STao Ma * and/or different from the directory change time.
9743c47d541STao Ma */
975b898ab23SJeff Layton inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
9763c47d541STao Ma ext4_update_dx_flag(dir);
977ee73f9a5SJeff Layton inode_inc_iversion(dir);
9783c47d541STao Ma return 1;
9793c47d541STao Ma }
9803c47d541STao Ma
ext4_get_inline_xattr_pos(struct inode * inode,struct ext4_iloc * iloc)9813c47d541STao Ma static void *ext4_get_inline_xattr_pos(struct inode *inode,
9823c47d541STao Ma struct ext4_iloc *iloc)
9833c47d541STao Ma {
9843c47d541STao Ma struct ext4_xattr_entry *entry;
9853c47d541STao Ma struct ext4_xattr_ibody_header *header;
9863c47d541STao Ma
9873c47d541STao Ma BUG_ON(!EXT4_I(inode)->i_inline_off);
9883c47d541STao Ma
9893c47d541STao Ma header = IHDR(inode, ext4_raw_inode(iloc));
9903c47d541STao Ma entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
9913c47d541STao Ma EXT4_I(inode)->i_inline_off);
9923c47d541STao Ma
9933c47d541STao Ma return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
9943c47d541STao Ma }
9953c47d541STao Ma
9963c47d541STao Ma /* Set the final de to cover the whole block. */
ext4_update_final_de(void * de_buf,int old_size,int new_size)9973c47d541STao Ma static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
9983c47d541STao Ma {
9993c47d541STao Ma struct ext4_dir_entry_2 *de, *prev_de;
10003c47d541STao Ma void *limit;
10013c47d541STao Ma int de_len;
10023c47d541STao Ma
1003c30365b9SYu Zhe de = de_buf;
10043c47d541STao Ma if (old_size) {
10053c47d541STao Ma limit = de_buf + old_size;
10063c47d541STao Ma do {
10073c47d541STao Ma prev_de = de;
10083c47d541STao Ma de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
10093c47d541STao Ma de_buf += de_len;
1010c30365b9SYu Zhe de = de_buf;
10113c47d541STao Ma } while (de_buf < limit);
10123c47d541STao Ma
10133c47d541STao Ma prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
10143c47d541STao Ma old_size, new_size);
10153c47d541STao Ma } else {
10163c47d541STao Ma /* this is just created, so create an empty entry. */
10173c47d541STao Ma de->inode = 0;
10183c47d541STao Ma de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
10193c47d541STao Ma }
10203c47d541STao Ma }
10213c47d541STao Ma
ext4_update_inline_dir(handle_t * handle,struct inode * dir,struct ext4_iloc * iloc)10223c47d541STao Ma static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
10233c47d541STao Ma struct ext4_iloc *iloc)
10243c47d541STao Ma {
10253c47d541STao Ma int ret;
10263c47d541STao Ma int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
10273c47d541STao Ma int new_size = get_max_inline_xattr_value_size(dir, iloc);
10283c47d541STao Ma
1029471fbbeaSDaniel Rosenberg if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
10303c47d541STao Ma return -ENOSPC;
10313c47d541STao Ma
10323c47d541STao Ma ret = ext4_update_inline_data(handle, dir,
10333c47d541STao Ma new_size + EXT4_MIN_INLINE_DATA_SIZE);
10343c47d541STao Ma if (ret)
10353c47d541STao Ma return ret;
10363c47d541STao Ma
10373c47d541STao Ma ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
10383c47d541STao Ma EXT4_I(dir)->i_inline_size -
10393c47d541STao Ma EXT4_MIN_INLINE_DATA_SIZE);
10403c47d541STao Ma dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
10413c47d541STao Ma return 0;
10423c47d541STao Ma }
10433c47d541STao Ma
ext4_restore_inline_data(handle_t * handle,struct inode * inode,struct ext4_iloc * iloc,void * buf,int inline_size)10443c47d541STao Ma static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
10453c47d541STao Ma struct ext4_iloc *iloc,
10463c47d541STao Ma void *buf, int inline_size)
10473c47d541STao Ma {
1048897026aaSRitesh Harjani int ret;
1049897026aaSRitesh Harjani
1050897026aaSRitesh Harjani ret = ext4_create_inline_data(handle, inode, inline_size);
1051897026aaSRitesh Harjani if (ret) {
1052897026aaSRitesh Harjani ext4_msg(inode->i_sb, KERN_EMERG,
1053897026aaSRitesh Harjani "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1054897026aaSRitesh Harjani inode->i_ino, ret);
1055897026aaSRitesh Harjani return;
1056897026aaSRitesh Harjani }
10573c47d541STao Ma ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
10583c47d541STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
10593c47d541STao Ma }
10603c47d541STao Ma
ext4_finish_convert_inline_dir(handle_t * handle,struct inode * inode,struct buffer_head * dir_block,void * buf,int inline_size)10613c47d541STao Ma static int ext4_finish_convert_inline_dir(handle_t *handle,
10623c47d541STao Ma struct inode *inode,
10633c47d541STao Ma struct buffer_head *dir_block,
10643c47d541STao Ma void *buf,
10653c47d541STao Ma int inline_size)
10663c47d541STao Ma {
10673c47d541STao Ma int err, csum_size = 0, header_size = 0;
10683c47d541STao Ma struct ext4_dir_entry_2 *de;
10693c47d541STao Ma void *target = dir_block->b_data;
10703c47d541STao Ma
10713c47d541STao Ma /*
10723c47d541STao Ma * First create "." and ".." and then copy the dir information
10733c47d541STao Ma * back to the block.
10743c47d541STao Ma */
1075c30365b9SYu Zhe de = target;
10763c47d541STao Ma de = ext4_init_dot_dotdot(inode, de,
10773c47d541STao Ma inode->i_sb->s_blocksize, csum_size,
10783c47d541STao Ma le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
10793c47d541STao Ma header_size = (void *)de - target;
10803c47d541STao Ma
10813c47d541STao Ma memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
10823c47d541STao Ma inline_size - EXT4_INLINE_DOTDOT_SIZE);
10833c47d541STao Ma
1084*e224fa3bSEric Biggers if (ext4_has_feature_metadata_csum(inode->i_sb))
10853c47d541STao Ma csum_size = sizeof(struct ext4_dir_entry_tail);
10863c47d541STao Ma
10873c47d541STao Ma inode->i_size = inode->i_sb->s_blocksize;
10883c47d541STao Ma i_size_write(inode, inode->i_sb->s_blocksize);
10893c47d541STao Ma EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
10903c47d541STao Ma ext4_update_final_de(dir_block->b_data,
10913c47d541STao Ma inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
10923c47d541STao Ma inode->i_sb->s_blocksize - csum_size);
10933c47d541STao Ma
1094ddce3b94STheodore Ts'o if (csum_size)
1095ddce3b94STheodore Ts'o ext4_initialize_dirent_tail(dir_block,
10963c47d541STao Ma inode->i_sb->s_blocksize);
10973c47d541STao Ma set_buffer_uptodate(dir_block);
1098f4ce24f5STheodore Ts'o unlock_buffer(dir_block);
1099f036adb3STheodore Ts'o err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
11003c47d541STao Ma if (err)
11013c47d541STao Ma return err;
1102b9cf625dSEric Biggers set_buffer_verified(dir_block);
1103b9cf625dSEric Biggers return ext4_mark_inode_dirty(handle, inode);
11043c47d541STao Ma }
11053c47d541STao Ma
ext4_convert_inline_data_nolock(handle_t * handle,struct inode * inode,struct ext4_iloc * iloc)11063c47d541STao Ma static int ext4_convert_inline_data_nolock(handle_t *handle,
11073c47d541STao Ma struct inode *inode,
11083c47d541STao Ma struct ext4_iloc *iloc)
11093c47d541STao Ma {
11103c47d541STao Ma int error;
11113c47d541STao Ma void *buf = NULL;
11123c47d541STao Ma struct buffer_head *data_bh = NULL;
11133c47d541STao Ma struct ext4_map_blocks map;
11143c47d541STao Ma int inline_size;
11153c47d541STao Ma
11163c47d541STao Ma inline_size = ext4_get_inline_size(inode);
11173c47d541STao Ma buf = kmalloc(inline_size, GFP_NOFS);
11183c47d541STao Ma if (!buf) {
11193c47d541STao Ma error = -ENOMEM;
11203c47d541STao Ma goto out;
11213c47d541STao Ma }
11223c47d541STao Ma
11233c47d541STao Ma error = ext4_read_inline_data(inode, buf, inline_size, iloc);
11243c47d541STao Ma if (error < 0)
11253c47d541STao Ma goto out;
11263c47d541STao Ma
112740b163f1SDarrick J. Wong /*
112840b163f1SDarrick J. Wong * Make sure the inline directory entries pass checks before we try to
112940b163f1SDarrick J. Wong * convert them, so that we avoid touching stuff that needs fsck.
113040b163f1SDarrick J. Wong */
113140b163f1SDarrick J. Wong if (S_ISDIR(inode->i_mode)) {
113240b163f1SDarrick J. Wong error = ext4_check_all_de(inode, iloc->bh,
113340b163f1SDarrick J. Wong buf + EXT4_INLINE_DOTDOT_SIZE,
113440b163f1SDarrick J. Wong inline_size - EXT4_INLINE_DOTDOT_SIZE);
113540b163f1SDarrick J. Wong if (error)
113640b163f1SDarrick J. Wong goto out;
113740b163f1SDarrick J. Wong }
113840b163f1SDarrick J. Wong
11393c47d541STao Ma error = ext4_destroy_inline_data_nolock(handle, inode);
11403c47d541STao Ma if (error)
11413c47d541STao Ma goto out;
11423c47d541STao Ma
11433c47d541STao Ma map.m_lblk = 0;
11443c47d541STao Ma map.m_len = 1;
11453c47d541STao Ma map.m_flags = 0;
11463c47d541STao Ma error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
11473c47d541STao Ma if (error < 0)
11483c47d541STao Ma goto out_restore;
11493c47d541STao Ma if (!(map.m_flags & EXT4_MAP_MAPPED)) {
11503c47d541STao Ma error = -EIO;
11513c47d541STao Ma goto out_restore;
11523c47d541STao Ma }
11533c47d541STao Ma
11543c47d541STao Ma data_bh = sb_getblk(inode->i_sb, map.m_pblk);
11553c47d541STao Ma if (!data_bh) {
1156860d21e2STheodore Ts'o error = -ENOMEM;
11573c47d541STao Ma goto out_restore;
11583c47d541STao Ma }
11593c47d541STao Ma
11603c47d541STao Ma lock_buffer(data_bh);
1161188c299eSJan Kara error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1162188c299eSJan Kara EXT4_JTR_NONE);
11633c47d541STao Ma if (error) {
11643c47d541STao Ma unlock_buffer(data_bh);
11653c47d541STao Ma error = -EIO;
11663c47d541STao Ma goto out_restore;
11673c47d541STao Ma }
11683c47d541STao Ma memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
11693c47d541STao Ma
11703c47d541STao Ma if (!S_ISDIR(inode->i_mode)) {
11713c47d541STao Ma memcpy(data_bh->b_data, buf, inline_size);
11723c47d541STao Ma set_buffer_uptodate(data_bh);
1173f4ce24f5STheodore Ts'o unlock_buffer(data_bh);
11743c47d541STao Ma error = ext4_handle_dirty_metadata(handle,
11753c47d541STao Ma inode, data_bh);
11763c47d541STao Ma } else {
11773c47d541STao Ma error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
11783c47d541STao Ma buf, inline_size);
11793c47d541STao Ma }
11803c47d541STao Ma
11813c47d541STao Ma out_restore:
11823c47d541STao Ma if (error)
11833c47d541STao Ma ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
11843c47d541STao Ma
11853c47d541STao Ma out:
11863c47d541STao Ma brelse(data_bh);
11873c47d541STao Ma kfree(buf);
11883c47d541STao Ma return error;
11893c47d541STao Ma }
11903c47d541STao Ma
11913c47d541STao Ma /*
11923c47d541STao Ma * Try to add the new entry to the inline data.
11933c47d541STao Ma * If succeeds, return 0. If not, extended the inline dir and copied data to
11943c47d541STao Ma * the new created block.
11953c47d541STao Ma */
ext4_try_add_inline_entry(handle_t * handle,struct ext4_filename * fname,struct inode * dir,struct inode * inode)11965b643f9cSTheodore Ts'o int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
119756a04915STheodore Ts'o struct inode *dir, struct inode *inode)
11983c47d541STao Ma {
11994209ae12SHarshad Shirwadkar int ret, ret2, inline_size, no_expand;
12003c47d541STao Ma void *inline_start;
12013c47d541STao Ma struct ext4_iloc iloc;
12023c47d541STao Ma
12033c47d541STao Ma ret = ext4_get_inode_loc(dir, &iloc);
12043c47d541STao Ma if (ret)
12053c47d541STao Ma return ret;
12063c47d541STao Ma
1207c755e251STheodore Ts'o ext4_write_lock_xattr(dir, &no_expand);
12083c47d541STao Ma if (!ext4_has_inline_data(dir))
12093c47d541STao Ma goto out;
12103c47d541STao Ma
12113c47d541STao Ma inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
12123c47d541STao Ma EXT4_INLINE_DOTDOT_SIZE;
12133c47d541STao Ma inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
12143c47d541STao Ma
121556a04915STheodore Ts'o ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
12163c47d541STao Ma inline_start, inline_size);
12173c47d541STao Ma if (ret != -ENOSPC)
12183c47d541STao Ma goto out;
12193c47d541STao Ma
12203c47d541STao Ma /* check whether it can be inserted to inline xattr space. */
12213c47d541STao Ma inline_size = EXT4_I(dir)->i_inline_size -
12223c47d541STao Ma EXT4_MIN_INLINE_DATA_SIZE;
12233c47d541STao Ma if (!inline_size) {
12243c47d541STao Ma /* Try to use the xattr space.*/
12253c47d541STao Ma ret = ext4_update_inline_dir(handle, dir, &iloc);
12263c47d541STao Ma if (ret && ret != -ENOSPC)
12273c47d541STao Ma goto out;
12283c47d541STao Ma
12293c47d541STao Ma inline_size = EXT4_I(dir)->i_inline_size -
12303c47d541STao Ma EXT4_MIN_INLINE_DATA_SIZE;
12313c47d541STao Ma }
12323c47d541STao Ma
12333c47d541STao Ma if (inline_size) {
12343c47d541STao Ma inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
12353c47d541STao Ma
123656a04915STheodore Ts'o ret = ext4_add_dirent_to_inline(handle, fname, dir,
12375b643f9cSTheodore Ts'o inode, &iloc, inline_start,
12385b643f9cSTheodore Ts'o inline_size);
12393c47d541STao Ma
12403c47d541STao Ma if (ret != -ENOSPC)
12413c47d541STao Ma goto out;
12423c47d541STao Ma }
12433c47d541STao Ma
12443c47d541STao Ma /*
12453c47d541STao Ma * The inline space is filled up, so create a new block for it.
12463c47d541STao Ma * As the extent tree will be created, we have to save the inline
12473c47d541STao Ma * dir first.
12483c47d541STao Ma */
12493c47d541STao Ma ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
12503c47d541STao Ma
12513c47d541STao Ma out:
1252c755e251STheodore Ts'o ext4_write_unlock_xattr(dir, &no_expand);
12534209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, dir);
12544209ae12SHarshad Shirwadkar if (unlikely(ret2 && !ret))
12554209ae12SHarshad Shirwadkar ret = ret2;
12563c47d541STao Ma brelse(iloc.bh);
12573c47d541STao Ma return ret;
12583c47d541STao Ma }
12593c47d541STao Ma
12608af0f082STao Ma /*
12618af0f082STao Ma * This function fills a red-black tree with information from an
12628af0f082STao Ma * inlined dir. It returns the number directory entries loaded
12638af0f082STao Ma * into the tree. If there is an error it is returned in err.
12648af0f082STao Ma */
ext4_inlinedir_to_tree(struct file * dir_file,struct inode * dir,ext4_lblk_t block,struct dx_hash_info * hinfo,__u32 start_hash,__u32 start_minor_hash,int * has_inline_data)12657633b08bSTheodore Ts'o int ext4_inlinedir_to_tree(struct file *dir_file,
12668af0f082STao Ma struct inode *dir, ext4_lblk_t block,
12678af0f082STao Ma struct dx_hash_info *hinfo,
12688af0f082STao Ma __u32 start_hash, __u32 start_minor_hash,
12698af0f082STao Ma int *has_inline_data)
12708af0f082STao Ma {
12718af0f082STao Ma int err = 0, count = 0;
12728af0f082STao Ma unsigned int parent_ino;
12738af0f082STao Ma int pos;
12748af0f082STao Ma struct ext4_dir_entry_2 *de;
12758af0f082STao Ma struct inode *inode = file_inode(dir_file);
12768af0f082STao Ma int ret, inline_size = 0;
12778af0f082STao Ma struct ext4_iloc iloc;
12788af0f082STao Ma void *dir_buf = NULL;
12798af0f082STao Ma struct ext4_dir_entry_2 fake;
1280a7550b30SJaegeuk Kim struct fscrypt_str tmp_str;
12818af0f082STao Ma
12828af0f082STao Ma ret = ext4_get_inode_loc(inode, &iloc);
12838af0f082STao Ma if (ret)
12848af0f082STao Ma return ret;
12858af0f082STao Ma
12868af0f082STao Ma down_read(&EXT4_I(inode)->xattr_sem);
12878af0f082STao Ma if (!ext4_has_inline_data(inode)) {
12888af0f082STao Ma up_read(&EXT4_I(inode)->xattr_sem);
12898af0f082STao Ma *has_inline_data = 0;
12908af0f082STao Ma goto out;
12918af0f082STao Ma }
12928af0f082STao Ma
12938af0f082STao Ma inline_size = ext4_get_inline_size(inode);
12948af0f082STao Ma dir_buf = kmalloc(inline_size, GFP_NOFS);
12958af0f082STao Ma if (!dir_buf) {
12968af0f082STao Ma ret = -ENOMEM;
12978af0f082STao Ma up_read(&EXT4_I(inode)->xattr_sem);
12988af0f082STao Ma goto out;
12998af0f082STao Ma }
13008af0f082STao Ma
13018af0f082STao Ma ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
13028af0f082STao Ma up_read(&EXT4_I(inode)->xattr_sem);
13038af0f082STao Ma if (ret < 0)
13048af0f082STao Ma goto out;
13058af0f082STao Ma
13068af0f082STao Ma pos = 0;
13078af0f082STao Ma parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
13088af0f082STao Ma while (pos < inline_size) {
13098af0f082STao Ma /*
13108af0f082STao Ma * As inlined dir doesn't store any information about '.' and
13118af0f082STao Ma * only the inode number of '..' is stored, we have to handle
13128af0f082STao Ma * them differently.
13138af0f082STao Ma */
13148af0f082STao Ma if (pos == 0) {
13158af0f082STao Ma fake.inode = cpu_to_le32(inode->i_ino);
13168af0f082STao Ma fake.name_len = 1;
13178af0f082STao Ma strcpy(fake.name, ".");
13188af0f082STao Ma fake.rec_len = ext4_rec_len_to_disk(
1319471fbbeaSDaniel Rosenberg ext4_dir_rec_len(fake.name_len, NULL),
13208af0f082STao Ma inline_size);
13218af0f082STao Ma ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
13228af0f082STao Ma de = &fake;
13238af0f082STao Ma pos = EXT4_INLINE_DOTDOT_OFFSET;
13248af0f082STao Ma } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
13258af0f082STao Ma fake.inode = cpu_to_le32(parent_ino);
13268af0f082STao Ma fake.name_len = 2;
13278af0f082STao Ma strcpy(fake.name, "..");
13288af0f082STao Ma fake.rec_len = ext4_rec_len_to_disk(
1329471fbbeaSDaniel Rosenberg ext4_dir_rec_len(fake.name_len, NULL),
13308af0f082STao Ma inline_size);
13318af0f082STao Ma ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
13328af0f082STao Ma de = &fake;
13338af0f082STao Ma pos = EXT4_INLINE_DOTDOT_SIZE;
13348af0f082STao Ma } else {
13358af0f082STao Ma de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
13368af0f082STao Ma pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
13378af0f082STao Ma if (ext4_check_dir_entry(inode, dir_file, de,
13388af0f082STao Ma iloc.bh, dir_buf,
13398af0f082STao Ma inline_size, pos)) {
13408af0f082STao Ma ret = count;
13418af0f082STao Ma goto out;
13428af0f082STao Ma }
13438af0f082STao Ma }
13448af0f082STao Ma
1345471fbbeaSDaniel Rosenberg if (ext4_hash_in_dirent(dir)) {
1346471fbbeaSDaniel Rosenberg hinfo->hash = EXT4_DIRENT_HASH(de);
1347471fbbeaSDaniel Rosenberg hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1348471fbbeaSDaniel Rosenberg } else {
13498dc9c3daSXiaxi Shen err = ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
13508dc9c3daSXiaxi Shen if (err) {
13518dc9c3daSXiaxi Shen ret = err;
13528dc9c3daSXiaxi Shen goto out;
13538dc9c3daSXiaxi Shen }
1354471fbbeaSDaniel Rosenberg }
13558af0f082STao Ma if ((hinfo->hash < start_hash) ||
13568af0f082STao Ma ((hinfo->hash == start_hash) &&
13578af0f082STao Ma (hinfo->minor_hash < start_minor_hash)))
13588af0f082STao Ma continue;
13598af0f082STao Ma if (de->inode == 0)
13608af0f082STao Ma continue;
13612f61830aSTheodore Ts'o tmp_str.name = de->name;
13622f61830aSTheodore Ts'o tmp_str.len = de->name_len;
13632f61830aSTheodore Ts'o err = ext4_htree_store_dirent(dir_file, hinfo->hash,
13642f61830aSTheodore Ts'o hinfo->minor_hash, de, &tmp_str);
13658af0f082STao Ma if (err) {
13667a14826eSColin Ian King ret = err;
13678af0f082STao Ma goto out;
13688af0f082STao Ma }
13698af0f082STao Ma count++;
13708af0f082STao Ma }
13718af0f082STao Ma ret = count;
13728af0f082STao Ma out:
13738af0f082STao Ma kfree(dir_buf);
13748af0f082STao Ma brelse(iloc.bh);
13758af0f082STao Ma return ret;
13768af0f082STao Ma }
13778af0f082STao Ma
1378c4d8b023STao Ma /*
1379c4d8b023STao Ma * So this function is called when the volume is mkfsed with
1380c4d8b023STao Ma * dir_index disabled. In order to keep f_pos persistent
1381c4d8b023STao Ma * after we convert from an inlined dir to a blocked based,
1382c4d8b023STao Ma * we just pretend that we are a normal dir and return the
1383c4d8b023STao Ma * offset as if '.' and '..' really take place.
1384c4d8b023STao Ma *
1385c4d8b023STao Ma */
ext4_read_inline_dir(struct file * file,struct dir_context * ctx,int * has_inline_data)1386725bebb2SAl Viro int ext4_read_inline_dir(struct file *file,
1387725bebb2SAl Viro struct dir_context *ctx,
138865d165d9STao Ma int *has_inline_data)
138965d165d9STao Ma {
139065d165d9STao Ma unsigned int offset, parent_ino;
1391725bebb2SAl Viro int i;
139265d165d9STao Ma struct ext4_dir_entry_2 *de;
139365d165d9STao Ma struct super_block *sb;
1394725bebb2SAl Viro struct inode *inode = file_inode(file);
139565d165d9STao Ma int ret, inline_size = 0;
139665d165d9STao Ma struct ext4_iloc iloc;
139765d165d9STao Ma void *dir_buf = NULL;
1398c4d8b023STao Ma int dotdot_offset, dotdot_size, extra_offset, extra_size;
13994f05ee2fSChristian Brauner struct dir_private_info *info = file->private_data;
140065d165d9STao Ma
140165d165d9STao Ma ret = ext4_get_inode_loc(inode, &iloc);
140265d165d9STao Ma if (ret)
140365d165d9STao Ma return ret;
140465d165d9STao Ma
140565d165d9STao Ma down_read(&EXT4_I(inode)->xattr_sem);
140665d165d9STao Ma if (!ext4_has_inline_data(inode)) {
140765d165d9STao Ma up_read(&EXT4_I(inode)->xattr_sem);
140865d165d9STao Ma *has_inline_data = 0;
140965d165d9STao Ma goto out;
141065d165d9STao Ma }
141165d165d9STao Ma
141265d165d9STao Ma inline_size = ext4_get_inline_size(inode);
141365d165d9STao Ma dir_buf = kmalloc(inline_size, GFP_NOFS);
141465d165d9STao Ma if (!dir_buf) {
141565d165d9STao Ma ret = -ENOMEM;
141665d165d9STao Ma up_read(&EXT4_I(inode)->xattr_sem);
141765d165d9STao Ma goto out;
141865d165d9STao Ma }
141965d165d9STao Ma
142065d165d9STao Ma ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
142165d165d9STao Ma up_read(&EXT4_I(inode)->xattr_sem);
142265d165d9STao Ma if (ret < 0)
142365d165d9STao Ma goto out;
142465d165d9STao Ma
142548ffdab1SBoxiLiu ret = 0;
142665d165d9STao Ma sb = inode->i_sb;
142765d165d9STao Ma parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1428725bebb2SAl Viro offset = ctx->pos;
142965d165d9STao Ma
1430c4d8b023STao Ma /*
1431c4d8b023STao Ma * dotdot_offset and dotdot_size is the real offset and
1432c4d8b023STao Ma * size for ".." and "." if the dir is block based while
1433c4d8b023STao Ma * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1434c4d8b023STao Ma * So we will use extra_offset and extra_size to indicate them
1435c4d8b023STao Ma * during the inline dir iteration.
1436c4d8b023STao Ma */
1437471fbbeaSDaniel Rosenberg dotdot_offset = ext4_dir_rec_len(1, NULL);
1438471fbbeaSDaniel Rosenberg dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1439c4d8b023STao Ma extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1440c4d8b023STao Ma extra_size = extra_offset + inline_size;
1441c4d8b023STao Ma
144265d165d9STao Ma /*
14434f05ee2fSChristian Brauner * If the cookie has changed since the last call to
144465d165d9STao Ma * readdir(2), then we might be pointing to an invalid
144565d165d9STao Ma * dirent right now. Scan from the start of the inline
144665d165d9STao Ma * dir to make sure.
144765d165d9STao Ma */
14484f05ee2fSChristian Brauner if (!inode_eq_iversion(inode, info->cookie)) {
1449c4d8b023STao Ma for (i = 0; i < extra_size && i < offset;) {
1450c4d8b023STao Ma /*
1451c4d8b023STao Ma * "." is with offset 0 and
1452c4d8b023STao Ma * ".." is dotdot_offset.
1453c4d8b023STao Ma */
145465d165d9STao Ma if (!i) {
1455c4d8b023STao Ma i = dotdot_offset;
1456c4d8b023STao Ma continue;
1457c4d8b023STao Ma } else if (i == dotdot_offset) {
1458c4d8b023STao Ma i = dotdot_size;
145965d165d9STao Ma continue;
146065d165d9STao Ma }
1461c4d8b023STao Ma /* for other entry, the real offset in
1462c4d8b023STao Ma * the buf has to be tuned accordingly.
1463c4d8b023STao Ma */
146465d165d9STao Ma de = (struct ext4_dir_entry_2 *)
1465c4d8b023STao Ma (dir_buf + i - extra_offset);
146665d165d9STao Ma /* It's too expensive to do a full
146765d165d9STao Ma * dirent test each time round this
146865d165d9STao Ma * loop, but we do have to test at
146965d165d9STao Ma * least that it is non-zero. A
147065d165d9STao Ma * failure will be detected in the
147165d165d9STao Ma * dirent test below. */
1472725bebb2SAl Viro if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1473471fbbeaSDaniel Rosenberg < ext4_dir_rec_len(1, NULL))
147465d165d9STao Ma break;
147565d165d9STao Ma i += ext4_rec_len_from_disk(de->rec_len,
1476c4d8b023STao Ma extra_size);
147765d165d9STao Ma }
147865d165d9STao Ma offset = i;
1479725bebb2SAl Viro ctx->pos = offset;
14804f05ee2fSChristian Brauner info->cookie = inode_query_iversion(inode);
148165d165d9STao Ma }
148265d165d9STao Ma
1483725bebb2SAl Viro while (ctx->pos < extra_size) {
1484725bebb2SAl Viro if (ctx->pos == 0) {
1485725bebb2SAl Viro if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1486725bebb2SAl Viro goto out;
1487725bebb2SAl Viro ctx->pos = dotdot_offset;
1488c4d8b023STao Ma continue;
1489c4d8b023STao Ma }
149065d165d9STao Ma
1491725bebb2SAl Viro if (ctx->pos == dotdot_offset) {
1492725bebb2SAl Viro if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1493725bebb2SAl Viro goto out;
1494725bebb2SAl Viro ctx->pos = dotdot_size;
149565d165d9STao Ma continue;
149665d165d9STao Ma }
149765d165d9STao Ma
1498c4d8b023STao Ma de = (struct ext4_dir_entry_2 *)
1499725bebb2SAl Viro (dir_buf + ctx->pos - extra_offset);
1500725bebb2SAl Viro if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1501725bebb2SAl Viro extra_size, ctx->pos))
1502725bebb2SAl Viro goto out;
1503725bebb2SAl Viro if (le32_to_cpu(de->inode)) {
1504725bebb2SAl Viro if (!dir_emit(ctx, de->name, de->name_len,
1505725bebb2SAl Viro le32_to_cpu(de->inode),
1506725bebb2SAl Viro get_dtype(sb, de->file_type)))
150765d165d9STao Ma goto out;
150865d165d9STao Ma }
1509725bebb2SAl Viro ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
151065d165d9STao Ma }
151165d165d9STao Ma out:
151265d165d9STao Ma kfree(dir_buf);
151365d165d9STao Ma brelse(iloc.bh);
151465d165d9STao Ma return ret;
151565d165d9STao Ma }
151665d165d9STao Ma
ext4_read_inline_link(struct inode * inode)15175a57bca9SZhang Yi void *ext4_read_inline_link(struct inode *inode)
15185a57bca9SZhang Yi {
15195a57bca9SZhang Yi struct ext4_iloc iloc;
15205a57bca9SZhang Yi int ret, inline_size;
15215a57bca9SZhang Yi void *link;
15225a57bca9SZhang Yi
15235a57bca9SZhang Yi ret = ext4_get_inode_loc(inode, &iloc);
15245a57bca9SZhang Yi if (ret)
15255a57bca9SZhang Yi return ERR_PTR(ret);
15265a57bca9SZhang Yi
15275a57bca9SZhang Yi ret = -ENOMEM;
15285a57bca9SZhang Yi inline_size = ext4_get_inline_size(inode);
15295a57bca9SZhang Yi link = kmalloc(inline_size + 1, GFP_NOFS);
15305a57bca9SZhang Yi if (!link)
15315a57bca9SZhang Yi goto out;
15325a57bca9SZhang Yi
15335a57bca9SZhang Yi ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
15345a57bca9SZhang Yi if (ret < 0) {
15355a57bca9SZhang Yi kfree(link);
15365a57bca9SZhang Yi goto out;
15375a57bca9SZhang Yi }
15385a57bca9SZhang Yi nd_terminate_link(link, inode->i_size, ret);
15395a57bca9SZhang Yi out:
15405a57bca9SZhang Yi if (ret < 0)
15415a57bca9SZhang Yi link = ERR_PTR(ret);
15425a57bca9SZhang Yi brelse(iloc.bh);
15435a57bca9SZhang Yi return link;
15445a57bca9SZhang Yi }
15455a57bca9SZhang Yi
ext4_get_first_inline_block(struct inode * inode,struct ext4_dir_entry_2 ** parent_de,int * retval)154632f7f22cSTao Ma struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
154732f7f22cSTao Ma struct ext4_dir_entry_2 **parent_de,
154832f7f22cSTao Ma int *retval)
154932f7f22cSTao Ma {
155032f7f22cSTao Ma struct ext4_iloc iloc;
155132f7f22cSTao Ma
155232f7f22cSTao Ma *retval = ext4_get_inode_loc(inode, &iloc);
155332f7f22cSTao Ma if (*retval)
155432f7f22cSTao Ma return NULL;
155532f7f22cSTao Ma
155632f7f22cSTao Ma *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
155732f7f22cSTao Ma
155832f7f22cSTao Ma return iloc.bh;
155932f7f22cSTao Ma }
156032f7f22cSTao Ma
15613c47d541STao Ma /*
15623c47d541STao Ma * Try to create the inline data for the new dir.
15633c47d541STao Ma * If it succeeds, return 0, otherwise return the error.
15643c47d541STao Ma * In case of ENOSPC, the caller should create the normal disk layout dir.
15653c47d541STao Ma */
ext4_try_create_inline_dir(handle_t * handle,struct inode * parent,struct inode * inode)15663c47d541STao Ma int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
15673c47d541STao Ma struct inode *inode)
15683c47d541STao Ma {
15693c47d541STao Ma int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
15703c47d541STao Ma struct ext4_iloc iloc;
15713c47d541STao Ma struct ext4_dir_entry_2 *de;
15723c47d541STao Ma
15733c47d541STao Ma ret = ext4_get_inode_loc(inode, &iloc);
15743c47d541STao Ma if (ret)
15753c47d541STao Ma return ret;
15763c47d541STao Ma
15773c47d541STao Ma ret = ext4_prepare_inline_data(handle, inode, inline_size);
15783c47d541STao Ma if (ret)
15793c47d541STao Ma goto out;
15803c47d541STao Ma
15813c47d541STao Ma /*
15823c47d541STao Ma * For inline dir, we only save the inode information for the ".."
15833c47d541STao Ma * and create a fake dentry to cover the left space.
15843c47d541STao Ma */
15853c47d541STao Ma de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
15863c47d541STao Ma de->inode = cpu_to_le32(parent->i_ino);
15873c47d541STao Ma de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
15883c47d541STao Ma de->inode = 0;
15893c47d541STao Ma de->rec_len = ext4_rec_len_to_disk(
15903c47d541STao Ma inline_size - EXT4_INLINE_DOTDOT_SIZE,
15913c47d541STao Ma inline_size);
15923c47d541STao Ma set_nlink(inode, 2);
15933c47d541STao Ma inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
15943c47d541STao Ma out:
15953c47d541STao Ma brelse(iloc.bh);
15963c47d541STao Ma return ret;
15973c47d541STao Ma }
15983c47d541STao Ma
ext4_find_inline_entry(struct inode * dir,struct ext4_filename * fname,struct ext4_dir_entry_2 ** res_dir,int * has_inline_data)1599e8e948e7STao Ma struct buffer_head *ext4_find_inline_entry(struct inode *dir,
16005b643f9cSTheodore Ts'o struct ext4_filename *fname,
1601e8e948e7STao Ma struct ext4_dir_entry_2 **res_dir,
1602e8e948e7STao Ma int *has_inline_data)
1603e8e948e7STao Ma {
1604c6b72f5dSThadeu Lima de Souza Cascardo struct ext4_xattr_ibody_find is = {
1605c6b72f5dSThadeu Lima de Souza Cascardo .s = { .not_found = -ENODATA, },
1606c6b72f5dSThadeu Lima de Souza Cascardo };
1607c6b72f5dSThadeu Lima de Souza Cascardo struct ext4_xattr_info i = {
1608c6b72f5dSThadeu Lima de Souza Cascardo .name_index = EXT4_XATTR_INDEX_SYSTEM,
1609c6b72f5dSThadeu Lima de Souza Cascardo .name = EXT4_XATTR_SYSTEM_DATA,
1610c6b72f5dSThadeu Lima de Souza Cascardo };
1611e8e948e7STao Ma int ret;
1612e8e948e7STao Ma void *inline_start;
1613e8e948e7STao Ma int inline_size;
1614e8e948e7STao Ma
1615c6b72f5dSThadeu Lima de Souza Cascardo ret = ext4_get_inode_loc(dir, &is.iloc);
16164d231b91SThadeu Lima de Souza Cascardo if (ret)
16174d231b91SThadeu Lima de Souza Cascardo return ERR_PTR(ret);
1618e8e948e7STao Ma
1619e8e948e7STao Ma down_read(&EXT4_I(dir)->xattr_sem);
1620c6b72f5dSThadeu Lima de Souza Cascardo
1621c6b72f5dSThadeu Lima de Souza Cascardo ret = ext4_xattr_ibody_find(dir, &i, &is);
1622c6b72f5dSThadeu Lima de Souza Cascardo if (ret)
1623c6b72f5dSThadeu Lima de Souza Cascardo goto out;
1624c6b72f5dSThadeu Lima de Souza Cascardo
1625e8e948e7STao Ma if (!ext4_has_inline_data(dir)) {
1626e8e948e7STao Ma *has_inline_data = 0;
1627e8e948e7STao Ma goto out;
1628e8e948e7STao Ma }
1629e8e948e7STao Ma
1630c6b72f5dSThadeu Lima de Souza Cascardo inline_start = (void *)ext4_raw_inode(&is.iloc)->i_block +
1631e8e948e7STao Ma EXT4_INLINE_DOTDOT_SIZE;
1632e8e948e7STao Ma inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1633c6b72f5dSThadeu Lima de Souza Cascardo ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
1634d6b97550SEric Biggers dir, fname, 0, res_dir);
1635e8e948e7STao Ma if (ret == 1)
1636e8e948e7STao Ma goto out_find;
1637e8e948e7STao Ma if (ret < 0)
1638e8e948e7STao Ma goto out;
1639e8e948e7STao Ma
1640e8e948e7STao Ma if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1641e8e948e7STao Ma goto out;
1642e8e948e7STao Ma
1643c6b72f5dSThadeu Lima de Souza Cascardo inline_start = ext4_get_inline_xattr_pos(dir, &is.iloc);
1644e8e948e7STao Ma inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1645e8e948e7STao Ma
1646c6b72f5dSThadeu Lima de Souza Cascardo ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
1647d6b97550SEric Biggers dir, fname, 0, res_dir);
1648e8e948e7STao Ma if (ret == 1)
1649e8e948e7STao Ma goto out_find;
1650e8e948e7STao Ma
1651e8e948e7STao Ma out:
1652c6b72f5dSThadeu Lima de Souza Cascardo brelse(is.iloc.bh);
16534d231b91SThadeu Lima de Souza Cascardo if (ret < 0)
1654c6b72f5dSThadeu Lima de Souza Cascardo is.iloc.bh = ERR_PTR(ret);
16554d231b91SThadeu Lima de Souza Cascardo else
1656c6b72f5dSThadeu Lima de Souza Cascardo is.iloc.bh = NULL;
1657e8e948e7STao Ma out_find:
1658e8e948e7STao Ma up_read(&EXT4_I(dir)->xattr_sem);
1659c6b72f5dSThadeu Lima de Souza Cascardo return is.iloc.bh;
1660e8e948e7STao Ma }
1661e8e948e7STao Ma
ext4_delete_inline_entry(handle_t * handle,struct inode * dir,struct ext4_dir_entry_2 * de_del,struct buffer_head * bh,int * has_inline_data)16629f40fe54STao Ma int ext4_delete_inline_entry(handle_t *handle,
16639f40fe54STao Ma struct inode *dir,
16649f40fe54STao Ma struct ext4_dir_entry_2 *de_del,
16659f40fe54STao Ma struct buffer_head *bh,
16669f40fe54STao Ma int *has_inline_data)
16679f40fe54STao Ma {
1668c755e251STheodore Ts'o int err, inline_size, no_expand;
16699f40fe54STao Ma struct ext4_iloc iloc;
16709f40fe54STao Ma void *inline_start;
16719f40fe54STao Ma
16729f40fe54STao Ma err = ext4_get_inode_loc(dir, &iloc);
16739f40fe54STao Ma if (err)
16749f40fe54STao Ma return err;
16759f40fe54STao Ma
1676c755e251STheodore Ts'o ext4_write_lock_xattr(dir, &no_expand);
16779f40fe54STao Ma if (!ext4_has_inline_data(dir)) {
16789f40fe54STao Ma *has_inline_data = 0;
16799f40fe54STao Ma goto out;
16809f40fe54STao Ma }
16819f40fe54STao Ma
16829f40fe54STao Ma if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
16839f40fe54STao Ma EXT4_MIN_INLINE_DATA_SIZE) {
16849f40fe54STao Ma inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
16859f40fe54STao Ma EXT4_INLINE_DOTDOT_SIZE;
16869f40fe54STao Ma inline_size = EXT4_MIN_INLINE_DATA_SIZE -
16879f40fe54STao Ma EXT4_INLINE_DOTDOT_SIZE;
16889f40fe54STao Ma } else {
16899f40fe54STao Ma inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
16909f40fe54STao Ma inline_size = ext4_get_inline_size(dir) -
16919f40fe54STao Ma EXT4_MIN_INLINE_DATA_SIZE;
16929f40fe54STao Ma }
16939f40fe54STao Ma
16945d601255Sliang xie BUFFER_TRACE(bh, "get_write_access");
1695188c299eSJan Kara err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1696188c299eSJan Kara EXT4_JTR_NONE);
16979f40fe54STao Ma if (err)
16989f40fe54STao Ma goto out;
16999f40fe54STao Ma
17002fe34d29SKyoungho Koo err = ext4_generic_delete_entry(dir, de_del, bh,
17019f40fe54STao Ma inline_start, inline_size, 0);
17029f40fe54STao Ma if (err)
17039f40fe54STao Ma goto out;
17049f40fe54STao Ma
17059f40fe54STao Ma ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
17069f40fe54STao Ma out:
1707c755e251STheodore Ts'o ext4_write_unlock_xattr(dir, &no_expand);
1708b907f2d5STheodore Ts'o if (likely(err == 0))
1709b907f2d5STheodore Ts'o err = ext4_mark_inode_dirty(handle, dir);
17109f40fe54STao Ma brelse(iloc.bh);
17119f40fe54STao Ma if (err != -ENOENT)
17129f40fe54STao Ma ext4_std_error(dir->i_sb, err);
17139f40fe54STao Ma return err;
17149f40fe54STao Ma }
17159f40fe54STao Ma
171661f86638STao Ma /*
171761f86638STao Ma * Get the inline dentry at offset.
171861f86638STao Ma */
171961f86638STao Ma static inline struct ext4_dir_entry_2 *
ext4_get_inline_entry(struct inode * inode,struct ext4_iloc * iloc,unsigned int offset,void ** inline_start,int * inline_size)172061f86638STao Ma ext4_get_inline_entry(struct inode *inode,
172161f86638STao Ma struct ext4_iloc *iloc,
172261f86638STao Ma unsigned int offset,
172361f86638STao Ma void **inline_start,
172461f86638STao Ma int *inline_size)
172561f86638STao Ma {
172661f86638STao Ma void *inline_pos;
172761f86638STao Ma
172861f86638STao Ma BUG_ON(offset > ext4_get_inline_size(inode));
172961f86638STao Ma
173061f86638STao Ma if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
173161f86638STao Ma inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
173261f86638STao Ma *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
173361f86638STao Ma } else {
173461f86638STao Ma inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
173561f86638STao Ma offset -= EXT4_MIN_INLINE_DATA_SIZE;
173661f86638STao Ma *inline_size = ext4_get_inline_size(inode) -
173761f86638STao Ma EXT4_MIN_INLINE_DATA_SIZE;
173861f86638STao Ma }
173961f86638STao Ma
174061f86638STao Ma if (inline_start)
174161f86638STao Ma *inline_start = inline_pos;
174261f86638STao Ma return (struct ext4_dir_entry_2 *)(inline_pos + offset);
174361f86638STao Ma }
174461f86638STao Ma
empty_inline_dir(struct inode * dir,int * has_inline_data)1745a7550b30SJaegeuk Kim bool empty_inline_dir(struct inode *dir, int *has_inline_data)
174661f86638STao Ma {
174761f86638STao Ma int err, inline_size;
174861f86638STao Ma struct ext4_iloc iloc;
17494d982e25STheodore Ts'o size_t inline_len;
175061f86638STao Ma void *inline_pos;
175161f86638STao Ma unsigned int offset;
175261f86638STao Ma struct ext4_dir_entry_2 *de;
17537aab5c84SYe Bin bool ret = false;
175461f86638STao Ma
175561f86638STao Ma err = ext4_get_inode_loc(dir, &iloc);
175661f86638STao Ma if (err) {
175754d3adbcSTheodore Ts'o EXT4_ERROR_INODE_ERR(dir, -err,
175854d3adbcSTheodore Ts'o "error %d getting inode %lu block",
175961f86638STao Ma err, dir->i_ino);
17607aab5c84SYe Bin return false;
176161f86638STao Ma }
176261f86638STao Ma
176361f86638STao Ma down_read(&EXT4_I(dir)->xattr_sem);
176461f86638STao Ma if (!ext4_has_inline_data(dir)) {
176561f86638STao Ma *has_inline_data = 0;
17667aab5c84SYe Bin ret = true;
176761f86638STao Ma goto out;
176861f86638STao Ma }
176961f86638STao Ma
177061f86638STao Ma de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
177161f86638STao Ma if (!le32_to_cpu(de->inode)) {
177261f86638STao Ma ext4_warning(dir->i_sb,
177361f86638STao Ma "bad inline directory (dir #%lu) - no `..'",
177461f86638STao Ma dir->i_ino);
177561f86638STao Ma goto out;
177661f86638STao Ma }
177761f86638STao Ma
17784d982e25STheodore Ts'o inline_len = ext4_get_inline_size(dir);
177961f86638STao Ma offset = EXT4_INLINE_DOTDOT_SIZE;
17804d982e25STheodore Ts'o while (offset < inline_len) {
178161f86638STao Ma de = ext4_get_inline_entry(dir, &iloc, offset,
178261f86638STao Ma &inline_pos, &inline_size);
178361f86638STao Ma if (ext4_check_dir_entry(dir, NULL, de,
178461f86638STao Ma iloc.bh, inline_pos,
178561f86638STao Ma inline_size, offset)) {
178661f86638STao Ma ext4_warning(dir->i_sb,
178761f86638STao Ma "bad inline directory (dir #%lu) - "
178861f86638STao Ma "inode %u, rec_len %u, name_len %d"
17898d2ae1cbSJakub Wilk "inline size %d",
179061f86638STao Ma dir->i_ino, le32_to_cpu(de->inode),
179161f86638STao Ma le16_to_cpu(de->rec_len), de->name_len,
179261f86638STao Ma inline_size);
179361f86638STao Ma goto out;
179461f86638STao Ma }
179561f86638STao Ma if (le32_to_cpu(de->inode)) {
179661f86638STao Ma goto out;
179761f86638STao Ma }
179861f86638STao Ma offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
179961f86638STao Ma }
180061f86638STao Ma
18017aab5c84SYe Bin ret = true;
180261f86638STao Ma out:
180361f86638STao Ma up_read(&EXT4_I(dir)->xattr_sem);
180461f86638STao Ma brelse(iloc.bh);
180561f86638STao Ma return ret;
180661f86638STao Ma }
180761f86638STao Ma
ext4_destroy_inline_data(handle_t * handle,struct inode * inode)180867cf5b09STao Ma int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
180967cf5b09STao Ma {
1810c755e251STheodore Ts'o int ret, no_expand;
181167cf5b09STao Ma
1812c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
181367cf5b09STao Ma ret = ext4_destroy_inline_data_nolock(handle, inode);
1814c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
181567cf5b09STao Ma
181667cf5b09STao Ma return ret;
181767cf5b09STao Ma }
181894191985STao Ma
ext4_inline_data_iomap(struct inode * inode,struct iomap * iomap)18197046ae35SAndreas Gruenbacher int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
18207046ae35SAndreas Gruenbacher {
18217046ae35SAndreas Gruenbacher __u64 addr;
18227046ae35SAndreas Gruenbacher int error = -EAGAIN;
18237046ae35SAndreas Gruenbacher struct ext4_iloc iloc;
18247046ae35SAndreas Gruenbacher
18257046ae35SAndreas Gruenbacher down_read(&EXT4_I(inode)->xattr_sem);
18267046ae35SAndreas Gruenbacher if (!ext4_has_inline_data(inode))
18277046ae35SAndreas Gruenbacher goto out;
18287046ae35SAndreas Gruenbacher
18297046ae35SAndreas Gruenbacher error = ext4_get_inode_loc(inode, &iloc);
18307046ae35SAndreas Gruenbacher if (error)
18317046ae35SAndreas Gruenbacher goto out;
18327046ae35SAndreas Gruenbacher
18337046ae35SAndreas Gruenbacher addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
18347046ae35SAndreas Gruenbacher addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
18357046ae35SAndreas Gruenbacher addr += offsetof(struct ext4_inode, i_block);
18367046ae35SAndreas Gruenbacher
18377046ae35SAndreas Gruenbacher brelse(iloc.bh);
18387046ae35SAndreas Gruenbacher
18397046ae35SAndreas Gruenbacher iomap->addr = addr;
18407046ae35SAndreas Gruenbacher iomap->offset = 0;
18417046ae35SAndreas Gruenbacher iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
18427046ae35SAndreas Gruenbacher i_size_read(inode));
184319319b53SChristoph Hellwig iomap->type = IOMAP_INLINE;
184419319b53SChristoph Hellwig iomap->flags = 0;
18457046ae35SAndreas Gruenbacher
18467046ae35SAndreas Gruenbacher out:
18477046ae35SAndreas Gruenbacher up_read(&EXT4_I(inode)->xattr_sem);
18487046ae35SAndreas Gruenbacher return error;
18497046ae35SAndreas Gruenbacher }
18507046ae35SAndreas Gruenbacher
ext4_inline_data_truncate(struct inode * inode,int * has_inline)185101daf945STheodore Ts'o int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1852aef1c851STao Ma {
1853aef1c851STao Ma handle_t *handle;
185401daf945STheodore Ts'o int inline_size, value_len, needed_blocks, no_expand, err = 0;
1855aef1c851STao Ma size_t i_size;
1856aef1c851STao Ma void *value = NULL;
1857aef1c851STao Ma struct ext4_xattr_ibody_find is = {
1858aef1c851STao Ma .s = { .not_found = -ENODATA, },
1859aef1c851STao Ma };
1860aef1c851STao Ma struct ext4_xattr_info i = {
1861aef1c851STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
1862aef1c851STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
1863aef1c851STao Ma };
1864aef1c851STao Ma
1865aef1c851STao Ma
1866aef1c851STao Ma needed_blocks = ext4_writepage_trans_blocks(inode);
18679924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1868aef1c851STao Ma if (IS_ERR(handle))
186901daf945STheodore Ts'o return PTR_ERR(handle);
1870aef1c851STao Ma
1871c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
1872aef1c851STao Ma if (!ext4_has_inline_data(inode)) {
18737067b261SJoseph Qi ext4_write_unlock_xattr(inode, &no_expand);
1874aef1c851STao Ma *has_inline = 0;
1875aef1c851STao Ma ext4_journal_stop(handle);
187601daf945STheodore Ts'o return 0;
1877aef1c851STao Ma }
1878aef1c851STao Ma
187901daf945STheodore Ts'o if ((err = ext4_orphan_add(handle, inode)) != 0)
1880aef1c851STao Ma goto out;
1881aef1c851STao Ma
188201daf945STheodore Ts'o if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1883aef1c851STao Ma goto out;
1884aef1c851STao Ma
1885aef1c851STao Ma down_write(&EXT4_I(inode)->i_data_sem);
1886aef1c851STao Ma i_size = inode->i_size;
1887aef1c851STao Ma inline_size = ext4_get_inline_size(inode);
1888aef1c851STao Ma EXT4_I(inode)->i_disksize = i_size;
1889aef1c851STao Ma
1890aef1c851STao Ma if (i_size < inline_size) {
18910add491dSEric Whitney /*
18920add491dSEric Whitney * if there's inline data to truncate and this file was
18930add491dSEric Whitney * converted to extents after that inline data was written,
18940add491dSEric Whitney * the extent status cache must be cleared to avoid leaving
18950add491dSEric Whitney * behind stale delayed allocated extent entries
18960add491dSEric Whitney */
1897ed5d285bSBaokun Li if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
1898ed5d285bSBaokun Li ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
18990add491dSEric Whitney
1900aef1c851STao Ma /* Clear the content in the xattr space. */
1901aef1c851STao Ma if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
190201daf945STheodore Ts'o if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1903aef1c851STao Ma goto out_error;
1904aef1c851STao Ma
1905aef1c851STao Ma BUG_ON(is.s.not_found);
1906aef1c851STao Ma
1907aef1c851STao Ma value_len = le32_to_cpu(is.s.here->e_value_size);
1908aef1c851STao Ma value = kmalloc(value_len, GFP_NOFS);
190901daf945STheodore Ts'o if (!value) {
191001daf945STheodore Ts'o err = -ENOMEM;
1911aef1c851STao Ma goto out_error;
191201daf945STheodore Ts'o }
1913aef1c851STao Ma
191401daf945STheodore Ts'o err = ext4_xattr_ibody_get(inode, i.name_index,
191501daf945STheodore Ts'o i.name, value, value_len);
191601daf945STheodore Ts'o if (err <= 0)
1917aef1c851STao Ma goto out_error;
1918aef1c851STao Ma
1919aef1c851STao Ma i.value = value;
1920aef1c851STao Ma i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1921aef1c851STao Ma i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1922310c097cSRitesh Harjani err = ext4_xattr_ibody_set(handle, inode, &i, &is);
192301daf945STheodore Ts'o if (err)
1924aef1c851STao Ma goto out_error;
1925aef1c851STao Ma }
1926aef1c851STao Ma
1927aef1c851STao Ma /* Clear the content within i_blocks. */
192809c455aaSTheodore Ts'o if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
192909c455aaSTheodore Ts'o void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
193009c455aaSTheodore Ts'o memset(p + i_size, 0,
1931aef1c851STao Ma EXT4_MIN_INLINE_DATA_SIZE - i_size);
193209c455aaSTheodore Ts'o }
1933aef1c851STao Ma
1934aef1c851STao Ma EXT4_I(inode)->i_inline_size = i_size <
1935aef1c851STao Ma EXT4_MIN_INLINE_DATA_SIZE ?
1936aef1c851STao Ma EXT4_MIN_INLINE_DATA_SIZE : i_size;
1937aef1c851STao Ma }
1938aef1c851STao Ma
1939aef1c851STao Ma out_error:
1940aef1c851STao Ma up_write(&EXT4_I(inode)->i_data_sem);
1941aef1c851STao Ma out:
1942aef1c851STao Ma brelse(is.iloc.bh);
1943c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
1944aef1c851STao Ma kfree(value);
1945aef1c851STao Ma if (inode->i_nlink)
1946aef1c851STao Ma ext4_orphan_del(handle, inode);
1947aef1c851STao Ma
194801daf945STheodore Ts'o if (err == 0) {
1949b898ab23SJeff Layton inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
195001daf945STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode);
1951aef1c851STao Ma if (IS_SYNC(inode))
1952aef1c851STao Ma ext4_handle_sync(handle);
195301daf945STheodore Ts'o }
1954aef1c851STao Ma ext4_journal_stop(handle);
195501daf945STheodore Ts'o return err;
1956aef1c851STao Ma }
19570c8d414fSTao Ma
ext4_convert_inline_data(struct inode * inode)19580c8d414fSTao Ma int ext4_convert_inline_data(struct inode *inode)
19590c8d414fSTao Ma {
1960c755e251STheodore Ts'o int error, needed_blocks, no_expand;
19610c8d414fSTao Ma handle_t *handle;
19620c8d414fSTao Ma struct ext4_iloc iloc;
19630c8d414fSTao Ma
19640c8d414fSTao Ma if (!ext4_has_inline_data(inode)) {
19650c8d414fSTao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
19660c8d414fSTao Ma return 0;
1967ef09ed5dSYe Bin } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1968ef09ed5dSYe Bin /*
1969ef09ed5dSYe Bin * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
1970ef09ed5dSYe Bin * cleared. This means we are in the middle of moving of
1971ef09ed5dSYe Bin * inline data to delay allocated block. Just force writeout
1972ef09ed5dSYe Bin * here to finish conversion.
1973ef09ed5dSYe Bin */
1974ef09ed5dSYe Bin error = filemap_flush(inode->i_mapping);
1975ef09ed5dSYe Bin if (error)
1976ef09ed5dSYe Bin return error;
1977ef09ed5dSYe Bin if (!ext4_has_inline_data(inode))
1978ef09ed5dSYe Bin return 0;
19790c8d414fSTao Ma }
19800c8d414fSTao Ma
19810c8d414fSTao Ma needed_blocks = ext4_writepage_trans_blocks(inode);
19820c8d414fSTao Ma
19830c8d414fSTao Ma iloc.bh = NULL;
19840c8d414fSTao Ma error = ext4_get_inode_loc(inode, &iloc);
19850c8d414fSTao Ma if (error)
19860c8d414fSTao Ma return error;
19870c8d414fSTao Ma
19889924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
19890c8d414fSTao Ma if (IS_ERR(handle)) {
19900c8d414fSTao Ma error = PTR_ERR(handle);
19910c8d414fSTao Ma goto out_free;
19920c8d414fSTao Ma }
19930c8d414fSTao Ma
1994c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
1995c755e251STheodore Ts'o if (ext4_has_inline_data(inode))
19960c8d414fSTao Ma error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
1997c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
19980c8d414fSTao Ma ext4_journal_stop(handle);
19990c8d414fSTao Ma out_free:
20000c8d414fSTao Ma brelse(iloc.bh);
20010c8d414fSTao Ma return error;
20020c8d414fSTao Ma }
2003