1f5166768STheodore Ts'o // SPDX-License-Identifier: LGPL-2.1
267cf5b09STao Ma /*
367cf5b09STao Ma * Copyright (c) 2012 Taobao.
467cf5b09STao Ma * Written by Tao Ma <boyu.mt@taobao.com>
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
ext4_get_inline_size(struct inode * inode)23c197855eSStephen Hemminger static int ext4_get_inline_size(struct inode *inode)
2467cf5b09STao Ma {
2567cf5b09STao Ma if (EXT4_I(inode)->i_inline_off)
2667cf5b09STao Ma return EXT4_I(inode)->i_inline_size;
2767cf5b09STao Ma
2867cf5b09STao Ma return 0;
2967cf5b09STao Ma }
3067cf5b09STao Ma
get_max_inline_xattr_value_size(struct inode * inode,struct ext4_iloc * iloc)3167cf5b09STao Ma static int get_max_inline_xattr_value_size(struct inode *inode,
3267cf5b09STao Ma struct ext4_iloc *iloc)
3367cf5b09STao Ma {
3467cf5b09STao Ma struct ext4_xattr_ibody_header *header;
3567cf5b09STao Ma struct ext4_xattr_entry *entry;
3667cf5b09STao Ma struct ext4_inode *raw_inode;
372220eaf9STheodore Ts'o void *end;
3867cf5b09STao Ma int free, min_offs;
3967cf5b09STao Ma
40c9fd167dSBaokun Li if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
41c9fd167dSBaokun Li return 0;
42c9fd167dSBaokun Li
4367cf5b09STao Ma min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
4467cf5b09STao Ma EXT4_GOOD_OLD_INODE_SIZE -
4567cf5b09STao Ma EXT4_I(inode)->i_extra_isize -
4667cf5b09STao Ma sizeof(struct ext4_xattr_ibody_header);
4767cf5b09STao Ma
4867cf5b09STao Ma /*
4967cf5b09STao Ma * We need to subtract another sizeof(__u32) since an in-inode xattr
5067cf5b09STao Ma * needs an empty 4 bytes to indicate the gap between the xattr entry
5167cf5b09STao Ma * and the name/value pair.
5267cf5b09STao Ma */
5367cf5b09STao Ma if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
5467cf5b09STao Ma return EXT4_XATTR_SIZE(min_offs -
5567cf5b09STao Ma EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
5667cf5b09STao Ma EXT4_XATTR_ROUND - sizeof(__u32));
5767cf5b09STao Ma
5867cf5b09STao Ma raw_inode = ext4_raw_inode(iloc);
5967cf5b09STao Ma header = IHDR(inode, raw_inode);
6067cf5b09STao Ma entry = IFIRST(header);
612220eaf9STheodore Ts'o end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
6267cf5b09STao Ma
6367cf5b09STao Ma /* Compute min_offs. */
642220eaf9STheodore Ts'o while (!IS_LAST_ENTRY(entry)) {
652220eaf9STheodore Ts'o void *next = EXT4_XATTR_NEXT(entry);
662220eaf9STheodore Ts'o
672220eaf9STheodore Ts'o if (next >= end) {
682220eaf9STheodore Ts'o EXT4_ERROR_INODE(inode,
692220eaf9STheodore Ts'o "corrupt xattr in inline inode");
702220eaf9STheodore Ts'o return 0;
712220eaf9STheodore Ts'o }
72e50e5129SAndreas Dilger if (!entry->e_value_inum && entry->e_value_size) {
7367cf5b09STao Ma size_t offs = le16_to_cpu(entry->e_value_offs);
7467cf5b09STao Ma if (offs < min_offs)
7567cf5b09STao Ma min_offs = offs;
7667cf5b09STao Ma }
772220eaf9STheodore Ts'o entry = next;
7867cf5b09STao Ma }
7967cf5b09STao Ma free = min_offs -
8067cf5b09STao Ma ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
8167cf5b09STao Ma
8267cf5b09STao Ma if (EXT4_I(inode)->i_inline_off) {
8367cf5b09STao Ma entry = (struct ext4_xattr_entry *)
8467cf5b09STao Ma ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
8567cf5b09STao Ma
86c4932dbeSboxi liu free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
8767cf5b09STao Ma goto out;
8867cf5b09STao Ma }
8967cf5b09STao Ma
9067cf5b09STao Ma free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
9167cf5b09STao Ma
9267cf5b09STao Ma if (free > EXT4_XATTR_ROUND)
9367cf5b09STao Ma free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
9467cf5b09STao Ma else
9567cf5b09STao Ma free = 0;
9667cf5b09STao Ma
9767cf5b09STao Ma out:
9867cf5b09STao Ma return free;
9967cf5b09STao Ma }
10067cf5b09STao Ma
10167cf5b09STao Ma /*
10267cf5b09STao Ma * Get the maximum size we now can store in an inode.
10367cf5b09STao Ma * If we can't find the space for a xattr entry, don't use the space
10467cf5b09STao Ma * of the extents since we have no space to indicate the inline data.
10567cf5b09STao Ma */
ext4_get_max_inline_size(struct inode * inode)10667cf5b09STao Ma int ext4_get_max_inline_size(struct inode *inode)
10767cf5b09STao Ma {
10867cf5b09STao Ma int error, max_inline_size;
10967cf5b09STao Ma struct ext4_iloc iloc;
11067cf5b09STao Ma
11167cf5b09STao Ma if (EXT4_I(inode)->i_extra_isize == 0)
11267cf5b09STao Ma return 0;
11367cf5b09STao Ma
11467cf5b09STao Ma error = ext4_get_inode_loc(inode, &iloc);
11567cf5b09STao Ma if (error) {
11654d3adbcSTheodore Ts'o ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
11767cf5b09STao Ma "can't get inode location %lu",
11867cf5b09STao Ma inode->i_ino);
11967cf5b09STao Ma return 0;
12067cf5b09STao Ma }
12167cf5b09STao Ma
12267cf5b09STao Ma down_read(&EXT4_I(inode)->xattr_sem);
12367cf5b09STao Ma max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
12467cf5b09STao Ma up_read(&EXT4_I(inode)->xattr_sem);
12567cf5b09STao Ma
12667cf5b09STao Ma brelse(iloc.bh);
12767cf5b09STao Ma
12867cf5b09STao Ma if (!max_inline_size)
12967cf5b09STao Ma return 0;
13067cf5b09STao Ma
13167cf5b09STao Ma return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
13267cf5b09STao Ma }
13367cf5b09STao Ma
13467cf5b09STao Ma /*
13567cf5b09STao Ma * this function does not take xattr_sem, which is OK because it is
13667cf5b09STao Ma * currently only used in a code path coming form ext4_iget, before
13767cf5b09STao Ma * the new inode has been unlocked
13867cf5b09STao Ma */
ext4_find_inline_data_nolock(struct inode * inode)13967cf5b09STao Ma int ext4_find_inline_data_nolock(struct inode *inode)
14067cf5b09STao Ma {
14167cf5b09STao Ma struct ext4_xattr_ibody_find is = {
14267cf5b09STao Ma .s = { .not_found = -ENODATA, },
14367cf5b09STao Ma };
14467cf5b09STao Ma struct ext4_xattr_info i = {
14567cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
14667cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
14767cf5b09STao Ma };
14867cf5b09STao Ma int error;
14967cf5b09STao Ma
15067cf5b09STao Ma if (EXT4_I(inode)->i_extra_isize == 0)
15167cf5b09STao Ma return 0;
15267cf5b09STao Ma
15367cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
15467cf5b09STao Ma if (error)
15567cf5b09STao Ma return error;
15667cf5b09STao Ma
15767cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
15867cf5b09STao Ma if (error)
15967cf5b09STao Ma goto out;
16067cf5b09STao Ma
16167cf5b09STao Ma if (!is.s.not_found) {
162117166efSTheodore Ts'o if (is.s.here->e_value_inum) {
163117166efSTheodore Ts'o EXT4_ERROR_INODE(inode, "inline data xattr refers "
164117166efSTheodore Ts'o "to an external xattr inode");
165117166efSTheodore Ts'o error = -EFSCORRUPTED;
166117166efSTheodore Ts'o goto out;
167117166efSTheodore Ts'o }
16867cf5b09STao Ma EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
16967cf5b09STao Ma (void *)ext4_raw_inode(&is.iloc));
17067cf5b09STao Ma EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
17167cf5b09STao Ma le32_to_cpu(is.s.here->e_value_size);
17267cf5b09STao Ma }
17367cf5b09STao Ma out:
17467cf5b09STao Ma brelse(is.iloc.bh);
17567cf5b09STao Ma return error;
17667cf5b09STao Ma }
17767cf5b09STao Ma
ext4_read_inline_data(struct inode * inode,void * buffer,unsigned int len,struct ext4_iloc * iloc)17867cf5b09STao Ma static int ext4_read_inline_data(struct inode *inode, void *buffer,
17967cf5b09STao Ma unsigned int len,
18067cf5b09STao Ma struct ext4_iloc *iloc)
18167cf5b09STao Ma {
18267cf5b09STao Ma struct ext4_xattr_entry *entry;
18367cf5b09STao Ma struct ext4_xattr_ibody_header *header;
18467cf5b09STao Ma int cp_len = 0;
18567cf5b09STao Ma struct ext4_inode *raw_inode;
18667cf5b09STao Ma
18767cf5b09STao Ma if (!len)
18867cf5b09STao Ma return 0;
18967cf5b09STao Ma
19067cf5b09STao Ma BUG_ON(len > EXT4_I(inode)->i_inline_size);
19167cf5b09STao Ma
19266267814SJiangshan Yi cp_len = min_t(unsigned int, len, EXT4_MIN_INLINE_DATA_SIZE);
19367cf5b09STao Ma
19467cf5b09STao Ma raw_inode = ext4_raw_inode(iloc);
19567cf5b09STao Ma memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
19667cf5b09STao Ma
19767cf5b09STao Ma len -= cp_len;
19867cf5b09STao Ma buffer += cp_len;
19967cf5b09STao Ma
20067cf5b09STao Ma if (!len)
20167cf5b09STao Ma goto out;
20267cf5b09STao Ma
20367cf5b09STao Ma header = IHDR(inode, raw_inode);
20467cf5b09STao Ma entry = (struct ext4_xattr_entry *)((void *)raw_inode +
20567cf5b09STao Ma EXT4_I(inode)->i_inline_off);
20667cf5b09STao Ma len = min_t(unsigned int, len,
20767cf5b09STao Ma (unsigned int)le32_to_cpu(entry->e_value_size));
20867cf5b09STao Ma
20967cf5b09STao Ma memcpy(buffer,
21067cf5b09STao Ma (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
21167cf5b09STao Ma cp_len += len;
21267cf5b09STao Ma
21367cf5b09STao Ma out:
21467cf5b09STao Ma return cp_len;
21567cf5b09STao Ma }
21667cf5b09STao Ma
21767cf5b09STao Ma /*
21867cf5b09STao Ma * write the buffer to the inline inode.
21967cf5b09STao Ma * If 'create' is set, we don't need to do the extra copy in the xattr
220310c097cSRitesh Harjani * value since it is already handled by ext4_xattr_ibody_set.
2210d812f77STao Ma * That saves us one memcpy.
22267cf5b09STao Ma */
ext4_write_inline_data(struct inode * inode,struct ext4_iloc * iloc,void * buffer,loff_t pos,unsigned int len)223c197855eSStephen Hemminger static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
22467cf5b09STao Ma void *buffer, loff_t pos, unsigned int len)
22567cf5b09STao Ma {
22667cf5b09STao Ma struct ext4_xattr_entry *entry;
22767cf5b09STao Ma struct ext4_xattr_ibody_header *header;
22867cf5b09STao Ma struct ext4_inode *raw_inode;
22967cf5b09STao Ma int cp_len = 0;
23067cf5b09STao Ma
231eb8ab444SJan Kara if (unlikely(ext4_forced_shutdown(inode->i_sb)))
2320db1ff22STheodore Ts'o return;
2330db1ff22STheodore Ts'o
23467cf5b09STao Ma BUG_ON(!EXT4_I(inode)->i_inline_off);
23567cf5b09STao Ma BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
23667cf5b09STao Ma
23767cf5b09STao Ma raw_inode = ext4_raw_inode(iloc);
23867cf5b09STao Ma buffer += pos;
23967cf5b09STao Ma
24067cf5b09STao Ma if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
24167cf5b09STao Ma cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
24267cf5b09STao Ma EXT4_MIN_INLINE_DATA_SIZE - pos : len;
24367cf5b09STao Ma memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
24467cf5b09STao Ma
24567cf5b09STao Ma len -= cp_len;
24667cf5b09STao Ma buffer += cp_len;
24767cf5b09STao Ma pos += cp_len;
24867cf5b09STao Ma }
24967cf5b09STao Ma
25067cf5b09STao Ma if (!len)
25167cf5b09STao Ma return;
25267cf5b09STao Ma
25367cf5b09STao Ma pos -= EXT4_MIN_INLINE_DATA_SIZE;
25467cf5b09STao Ma header = IHDR(inode, raw_inode);
25567cf5b09STao Ma entry = (struct ext4_xattr_entry *)((void *)raw_inode +
25667cf5b09STao Ma EXT4_I(inode)->i_inline_off);
25767cf5b09STao Ma
25867cf5b09STao Ma memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
25967cf5b09STao Ma buffer, len);
26067cf5b09STao Ma }
26167cf5b09STao Ma
ext4_create_inline_data(handle_t * handle,struct inode * inode,unsigned len)26267cf5b09STao Ma static int ext4_create_inline_data(handle_t *handle,
26367cf5b09STao Ma struct inode *inode, unsigned len)
26467cf5b09STao Ma {
26567cf5b09STao Ma int error;
26667cf5b09STao Ma void *value = NULL;
26767cf5b09STao Ma struct ext4_xattr_ibody_find is = {
26867cf5b09STao Ma .s = { .not_found = -ENODATA, },
26967cf5b09STao Ma };
27067cf5b09STao Ma struct ext4_xattr_info i = {
27167cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
27267cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
27367cf5b09STao Ma };
27467cf5b09STao Ma
27567cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
27667cf5b09STao Ma if (error)
27767cf5b09STao Ma return error;
27867cf5b09STao Ma
2795d601255Sliang xie BUFFER_TRACE(is.iloc.bh, "get_write_access");
280188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
281188c299eSJan Kara EXT4_JTR_NONE);
28267cf5b09STao Ma if (error)
28367cf5b09STao Ma goto out;
28467cf5b09STao Ma
28567cf5b09STao Ma if (len > EXT4_MIN_INLINE_DATA_SIZE) {
286bd9926e8STheodore Ts'o value = EXT4_ZERO_XATTR_VALUE;
28767cf5b09STao Ma len -= EXT4_MIN_INLINE_DATA_SIZE;
28867cf5b09STao Ma } else {
28967cf5b09STao Ma value = "";
29067cf5b09STao Ma len = 0;
29167cf5b09STao Ma }
29267cf5b09STao Ma
2937ca4fcbaSkyoungho koo /* Insert the xttr entry. */
29467cf5b09STao Ma i.value = value;
29567cf5b09STao Ma i.value_len = len;
29667cf5b09STao Ma
29767cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
29867cf5b09STao Ma if (error)
29967cf5b09STao Ma goto out;
30067cf5b09STao Ma
30167cf5b09STao Ma BUG_ON(!is.s.not_found);
30267cf5b09STao Ma
303310c097cSRitesh Harjani error = ext4_xattr_ibody_set(handle, inode, &i, &is);
30467cf5b09STao Ma if (error) {
30567cf5b09STao Ma if (error == -ENOSPC)
30667cf5b09STao Ma ext4_clear_inode_state(inode,
30767cf5b09STao Ma EXT4_STATE_MAY_INLINE_DATA);
30867cf5b09STao Ma goto out;
30967cf5b09STao Ma }
31067cf5b09STao Ma
31167cf5b09STao Ma memset((void *)ext4_raw_inode(&is.iloc)->i_block,
31267cf5b09STao Ma 0, EXT4_MIN_INLINE_DATA_SIZE);
31367cf5b09STao Ma
31467cf5b09STao Ma EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
31567cf5b09STao Ma (void *)ext4_raw_inode(&is.iloc));
31667cf5b09STao Ma EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
31767cf5b09STao Ma ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
31867cf5b09STao Ma ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
31967cf5b09STao Ma get_bh(is.iloc.bh);
32067cf5b09STao Ma error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
32167cf5b09STao Ma
32267cf5b09STao Ma out:
32367cf5b09STao Ma brelse(is.iloc.bh);
32467cf5b09STao Ma return error;
32567cf5b09STao Ma }
32667cf5b09STao Ma
ext4_update_inline_data(handle_t * handle,struct inode * inode,unsigned int len)32767cf5b09STao Ma static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
32867cf5b09STao Ma unsigned int len)
32967cf5b09STao Ma {
33067cf5b09STao Ma int error;
33167cf5b09STao Ma void *value = NULL;
33267cf5b09STao Ma struct ext4_xattr_ibody_find is = {
33367cf5b09STao Ma .s = { .not_found = -ENODATA, },
33467cf5b09STao Ma };
33567cf5b09STao Ma struct ext4_xattr_info i = {
33667cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
33767cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
33867cf5b09STao Ma };
33967cf5b09STao Ma
34067cf5b09STao Ma /* If the old space is ok, write the data directly. */
34167cf5b09STao Ma if (len <= EXT4_I(inode)->i_inline_size)
34267cf5b09STao Ma return 0;
34367cf5b09STao Ma
34467cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
34567cf5b09STao Ma if (error)
34667cf5b09STao Ma return error;
34767cf5b09STao Ma
34867cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
34967cf5b09STao Ma if (error)
35067cf5b09STao Ma goto out;
35167cf5b09STao Ma
35267cf5b09STao Ma BUG_ON(is.s.not_found);
35367cf5b09STao Ma
35467cf5b09STao Ma len -= EXT4_MIN_INLINE_DATA_SIZE;
35567cf5b09STao Ma value = kzalloc(len, GFP_NOFS);
356578620f4SDan Carpenter if (!value) {
357578620f4SDan Carpenter error = -ENOMEM;
35867cf5b09STao Ma goto out;
359578620f4SDan Carpenter }
36067cf5b09STao Ma
36167cf5b09STao Ma error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
36267cf5b09STao Ma value, len);
3632a534e1dSTheodore Ts'o if (error < 0)
36467cf5b09STao Ma goto out;
36567cf5b09STao Ma
3665d601255Sliang xie BUFFER_TRACE(is.iloc.bh, "get_write_access");
367188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
368188c299eSJan Kara EXT4_JTR_NONE);
36967cf5b09STao Ma if (error)
37067cf5b09STao Ma goto out;
37167cf5b09STao Ma
372b483bb77SRandy Dunlap /* Update the xattr entry. */
37367cf5b09STao Ma i.value = value;
37467cf5b09STao Ma i.value_len = len;
37567cf5b09STao Ma
376310c097cSRitesh Harjani error = ext4_xattr_ibody_set(handle, inode, &i, &is);
37767cf5b09STao Ma if (error)
37867cf5b09STao Ma goto out;
37967cf5b09STao Ma
38067cf5b09STao Ma EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
38167cf5b09STao Ma (void *)ext4_raw_inode(&is.iloc));
38267cf5b09STao Ma EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
38367cf5b09STao Ma le32_to_cpu(is.s.here->e_value_size);
38467cf5b09STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
38567cf5b09STao Ma get_bh(is.iloc.bh);
38667cf5b09STao Ma error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
38767cf5b09STao Ma
38867cf5b09STao Ma out:
38967cf5b09STao Ma kfree(value);
39067cf5b09STao Ma brelse(is.iloc.bh);
39167cf5b09STao Ma return error;
39267cf5b09STao Ma }
39367cf5b09STao Ma
ext4_prepare_inline_data(handle_t * handle,struct inode * inode,unsigned int len)394c197855eSStephen Hemminger static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
39567cf5b09STao Ma unsigned int len)
39667cf5b09STao Ma {
397c755e251STheodore Ts'o int ret, size, no_expand;
39867cf5b09STao Ma struct ext4_inode_info *ei = EXT4_I(inode);
39967cf5b09STao Ma
40067cf5b09STao Ma if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
40167cf5b09STao Ma return -ENOSPC;
40267cf5b09STao Ma
40367cf5b09STao Ma size = ext4_get_max_inline_size(inode);
40467cf5b09STao Ma if (size < len)
40567cf5b09STao Ma return -ENOSPC;
40667cf5b09STao Ma
407c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
40867cf5b09STao Ma
40967cf5b09STao Ma if (ei->i_inline_off)
41067cf5b09STao Ma ret = ext4_update_inline_data(handle, inode, len);
41167cf5b09STao Ma else
41267cf5b09STao Ma ret = ext4_create_inline_data(handle, inode, len);
41367cf5b09STao Ma
414c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
41567cf5b09STao Ma return ret;
41667cf5b09STao Ma }
41767cf5b09STao Ma
ext4_destroy_inline_data_nolock(handle_t * handle,struct inode * inode)41867cf5b09STao Ma static int ext4_destroy_inline_data_nolock(handle_t *handle,
41967cf5b09STao Ma struct inode *inode)
42067cf5b09STao Ma {
42167cf5b09STao Ma struct ext4_inode_info *ei = EXT4_I(inode);
42267cf5b09STao Ma struct ext4_xattr_ibody_find is = {
42367cf5b09STao Ma .s = { .not_found = 0, },
42467cf5b09STao Ma };
42567cf5b09STao Ma struct ext4_xattr_info i = {
42667cf5b09STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
42767cf5b09STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
42867cf5b09STao Ma .value = NULL,
42967cf5b09STao Ma .value_len = 0,
43067cf5b09STao Ma };
43167cf5b09STao Ma int error;
43267cf5b09STao Ma
43367cf5b09STao Ma if (!ei->i_inline_off)
43467cf5b09STao Ma return 0;
43567cf5b09STao Ma
43667cf5b09STao Ma error = ext4_get_inode_loc(inode, &is.iloc);
43767cf5b09STao Ma if (error)
43867cf5b09STao Ma return error;
43967cf5b09STao Ma
44067cf5b09STao Ma error = ext4_xattr_ibody_find(inode, &i, &is);
44167cf5b09STao Ma if (error)
44267cf5b09STao Ma goto out;
44367cf5b09STao Ma
4445d601255Sliang xie BUFFER_TRACE(is.iloc.bh, "get_write_access");
445188c299eSJan Kara error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
446188c299eSJan Kara EXT4_JTR_NONE);
44767cf5b09STao Ma if (error)
44867cf5b09STao Ma goto out;
44967cf5b09STao Ma
450310c097cSRitesh Harjani error = ext4_xattr_ibody_set(handle, inode, &i, &is);
45167cf5b09STao Ma if (error)
45267cf5b09STao Ma goto out;
45367cf5b09STao Ma
45467cf5b09STao Ma memset((void *)ext4_raw_inode(&is.iloc)->i_block,
45567cf5b09STao Ma 0, EXT4_MIN_INLINE_DATA_SIZE);
4566e8ab72aSTheodore Ts'o memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
45767cf5b09STao Ma
458e2b911c5SDarrick J. Wong if (ext4_has_feature_extents(inode->i_sb)) {
45967cf5b09STao Ma if (S_ISDIR(inode->i_mode) ||
46067cf5b09STao Ma S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
46167cf5b09STao Ma ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
46267cf5b09STao Ma ext4_ext_tree_init(handle, inode);
46367cf5b09STao Ma }
46467cf5b09STao Ma }
46567cf5b09STao Ma ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
46667cf5b09STao Ma
46767cf5b09STao Ma get_bh(is.iloc.bh);
46867cf5b09STao Ma error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
46967cf5b09STao Ma
47067cf5b09STao Ma EXT4_I(inode)->i_inline_off = 0;
47167cf5b09STao Ma EXT4_I(inode)->i_inline_size = 0;
47267cf5b09STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
47367cf5b09STao Ma out:
47467cf5b09STao Ma brelse(is.iloc.bh);
47567cf5b09STao Ma if (error == -ENODATA)
47667cf5b09STao Ma error = 0;
47767cf5b09STao Ma return error;
47867cf5b09STao Ma }
47967cf5b09STao Ma
ext4_read_inline_folio(struct inode * inode,struct folio * folio)4806b87fbe4SMatthew Wilcox static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
48146c7f254STao Ma {
48246c7f254STao Ma void *kaddr;
48346c7f254STao Ma int ret = 0;
48446c7f254STao Ma size_t len;
48546c7f254STao Ma struct ext4_iloc iloc;
48646c7f254STao Ma
4876b87fbe4SMatthew Wilcox BUG_ON(!folio_test_locked(folio));
48846c7f254STao Ma BUG_ON(!ext4_has_inline_data(inode));
4896b87fbe4SMatthew Wilcox BUG_ON(folio->index);
49046c7f254STao Ma
49146c7f254STao Ma if (!EXT4_I(inode)->i_inline_off) {
49246c7f254STao Ma ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
49346c7f254STao Ma inode->i_ino);
49446c7f254STao Ma goto out;
49546c7f254STao Ma }
49646c7f254STao Ma
49746c7f254STao Ma ret = ext4_get_inode_loc(inode, &iloc);
49846c7f254STao Ma if (ret)
49946c7f254STao Ma goto out;
50046c7f254STao Ma
50146c7f254STao Ma len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
5026b87fbe4SMatthew Wilcox BUG_ON(len > PAGE_SIZE);
5036b87fbe4SMatthew Wilcox kaddr = kmap_local_folio(folio, 0);
50446c7f254STao Ma ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
5056b87fbe4SMatthew Wilcox flush_dcache_folio(folio);
5066b87fbe4SMatthew Wilcox kunmap_local(kaddr);
5076b87fbe4SMatthew Wilcox folio_zero_segment(folio, len, folio_size(folio));
5086b87fbe4SMatthew Wilcox folio_mark_uptodate(folio);
50946c7f254STao Ma brelse(iloc.bh);
51046c7f254STao Ma
51146c7f254STao Ma out:
51246c7f254STao Ma return ret;
51346c7f254STao Ma }
51446c7f254STao Ma
ext4_readpage_inline(struct inode * inode,struct folio * folio)5153edde93eSMatthew Wilcox int ext4_readpage_inline(struct inode *inode, struct folio *folio)
51646c7f254STao Ma {
51746c7f254STao Ma int ret = 0;
51846c7f254STao Ma
51946c7f254STao Ma down_read(&EXT4_I(inode)->xattr_sem);
52046c7f254STao Ma if (!ext4_has_inline_data(inode)) {
52146c7f254STao Ma up_read(&EXT4_I(inode)->xattr_sem);
52246c7f254STao Ma return -EAGAIN;
52346c7f254STao Ma }
52446c7f254STao Ma
52546c7f254STao Ma /*
52646c7f254STao Ma * Current inline data can only exist in the 1st page,
52746c7f254STao Ma * So for all the other pages, just set them uptodate.
52846c7f254STao Ma */
5293edde93eSMatthew Wilcox if (!folio->index)
5306b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
5313edde93eSMatthew Wilcox else if (!folio_test_uptodate(folio)) {
5323edde93eSMatthew Wilcox folio_zero_segment(folio, 0, folio_size(folio));
5333edde93eSMatthew Wilcox folio_mark_uptodate(folio);
53446c7f254STao Ma }
53546c7f254STao Ma
53646c7f254STao Ma up_read(&EXT4_I(inode)->xattr_sem);
53746c7f254STao Ma
5383edde93eSMatthew Wilcox folio_unlock(folio);
53946c7f254STao Ma return ret >= 0 ? 0 : ret;
54046c7f254STao Ma }
54146c7f254STao Ma
ext4_convert_inline_data_to_extent(struct address_space * mapping,struct inode * inode)542f19d5870STao Ma static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
543832ee62dSMatthew Wilcox (Oracle) struct inode *inode)
544f19d5870STao Ma {
545c755e251STheodore Ts'o int ret, needed_blocks, no_expand;
546f19d5870STao Ma handle_t *handle = NULL;
547f19d5870STao Ma int retries = 0, sem_held = 0;
54883eba701SMatthew Wilcox struct folio *folio = NULL;
549f19d5870STao Ma unsigned from, to;
550f19d5870STao Ma struct ext4_iloc iloc;
551f19d5870STao Ma
552f19d5870STao Ma if (!ext4_has_inline_data(inode)) {
553f19d5870STao Ma /*
554f19d5870STao Ma * clear the flag so that no new write
555f19d5870STao Ma * will trap here again.
556f19d5870STao Ma */
557f19d5870STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
558f19d5870STao Ma return 0;
559f19d5870STao Ma }
560f19d5870STao Ma
561f19d5870STao Ma needed_blocks = ext4_writepage_trans_blocks(inode);
562f19d5870STao Ma
563f19d5870STao Ma ret = ext4_get_inode_loc(inode, &iloc);
564f19d5870STao Ma if (ret)
565f19d5870STao Ma return ret;
566f19d5870STao Ma
567f19d5870STao Ma retry:
5689924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
569f19d5870STao Ma if (IS_ERR(handle)) {
570f19d5870STao Ma ret = PTR_ERR(handle);
571f19d5870STao Ma handle = NULL;
572f19d5870STao Ma goto out;
573f19d5870STao Ma }
574f19d5870STao Ma
575f19d5870STao Ma /* We cannot recurse into the filesystem as the transaction is already
576f19d5870STao Ma * started */
57783eba701SMatthew Wilcox folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
57883eba701SMatthew Wilcox mapping_gfp_mask(mapping));
5797fa8a8eeSLinus Torvalds if (IS_ERR(folio)) {
5807fa8a8eeSLinus Torvalds ret = PTR_ERR(folio);
5817fa8a8eeSLinus Torvalds goto out_nofolio;
582f19d5870STao Ma }
583f19d5870STao Ma
584c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
585f19d5870STao Ma sem_held = 1;
586f19d5870STao Ma /* If some one has already done this for us, just exit. */
587f19d5870STao Ma if (!ext4_has_inline_data(inode)) {
588f19d5870STao Ma ret = 0;
589f19d5870STao Ma goto out;
590f19d5870STao Ma }
591f19d5870STao Ma
592f19d5870STao Ma from = 0;
593f19d5870STao Ma to = ext4_get_inline_size(inode);
59483eba701SMatthew Wilcox if (!folio_test_uptodate(folio)) {
5956b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
596f19d5870STao Ma if (ret < 0)
597f19d5870STao Ma goto out;
598f19d5870STao Ma }
599f19d5870STao Ma
600f19d5870STao Ma ret = ext4_destroy_inline_data_nolock(handle, inode);
601f19d5870STao Ma if (ret)
602f19d5870STao Ma goto out;
603f19d5870STao Ma
604705965bdSJan Kara if (ext4_should_dioread_nolock(inode)) {
60583eba701SMatthew Wilcox ret = __block_write_begin(&folio->page, from, to,
606705965bdSJan Kara ext4_get_block_unwritten);
607705965bdSJan Kara } else
60883eba701SMatthew Wilcox ret = __block_write_begin(&folio->page, from, to, ext4_get_block);
609f19d5870STao Ma
610f19d5870STao Ma if (!ret && ext4_should_journal_data(inode)) {
61183eba701SMatthew Wilcox ret = ext4_walk_page_buffers(handle, inode,
61283eba701SMatthew Wilcox folio_buffers(folio), from, to,
61383eba701SMatthew Wilcox NULL, do_journal_get_write_access);
614f19d5870STao Ma }
615f19d5870STao Ma
616f19d5870STao Ma if (ret) {
61783eba701SMatthew Wilcox folio_unlock(folio);
61883eba701SMatthew Wilcox folio_put(folio);
61983eba701SMatthew Wilcox folio = NULL;
620f19d5870STao Ma ext4_orphan_add(handle, inode);
621c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
622f19d5870STao Ma sem_held = 0;
623f19d5870STao Ma ext4_journal_stop(handle);
624f19d5870STao Ma handle = NULL;
625f19d5870STao Ma ext4_truncate_failed_write(inode);
626f19d5870STao Ma /*
627f19d5870STao Ma * If truncate failed early the inode might
628f19d5870STao Ma * still be on the orphan list; we need to
629f19d5870STao Ma * make sure the inode is removed from the
630f19d5870STao Ma * orphan list in that case.
631f19d5870STao Ma */
632f19d5870STao Ma if (inode->i_nlink)
633f19d5870STao Ma ext4_orphan_del(NULL, inode);
634f19d5870STao Ma }
635f19d5870STao Ma
636f19d5870STao Ma if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
637f19d5870STao Ma goto retry;
638f19d5870STao Ma
63983eba701SMatthew Wilcox if (folio)
64083eba701SMatthew Wilcox block_commit_write(&folio->page, from, to);
641f19d5870STao Ma out:
64283eba701SMatthew Wilcox if (folio) {
64383eba701SMatthew Wilcox folio_unlock(folio);
64483eba701SMatthew Wilcox folio_put(folio);
645f19d5870STao Ma }
6467fa8a8eeSLinus Torvalds out_nofolio:
647f19d5870STao Ma if (sem_held)
648c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
649f19d5870STao Ma if (handle)
650f19d5870STao Ma ext4_journal_stop(handle);
651f19d5870STao Ma brelse(iloc.bh);
652f19d5870STao Ma return ret;
653f19d5870STao Ma }
654f19d5870STao Ma
655f19d5870STao Ma /*
656f19d5870STao Ma * Try to write data in the inode.
657f19d5870STao Ma * If the inode has inline data, check whether the new write can be
658f19d5870STao Ma * in the inode also. If not, create the page the handle, move the data
659f19d5870STao Ma * to the page make it update and let the later codes create extent for it.
660f19d5870STao Ma */
ext4_try_to_write_inline_data(struct address_space * mapping,struct inode * inode,loff_t pos,unsigned len,struct page ** pagep)661f19d5870STao Ma int ext4_try_to_write_inline_data(struct address_space *mapping,
662f19d5870STao Ma struct inode *inode,
663f19d5870STao Ma loff_t pos, unsigned len,
664f19d5870STao Ma struct page **pagep)
665f19d5870STao Ma {
666f19d5870STao Ma int ret;
667f19d5870STao Ma handle_t *handle;
668f8f8c89fSMatthew Wilcox struct folio *folio;
669f19d5870STao Ma struct ext4_iloc iloc;
670f19d5870STao Ma
671f19d5870STao Ma if (pos + len > ext4_get_max_inline_size(inode))
672f19d5870STao Ma goto convert;
673f19d5870STao Ma
674f19d5870STao Ma ret = ext4_get_inode_loc(inode, &iloc);
675f19d5870STao Ma if (ret)
676f19d5870STao Ma return ret;
677f19d5870STao Ma
678f19d5870STao Ma /*
679f19d5870STao Ma * The possible write could happen in the inode,
680f19d5870STao Ma * so try to reserve the space in inode first.
681f19d5870STao Ma */
6829924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
683f19d5870STao Ma if (IS_ERR(handle)) {
684f19d5870STao Ma ret = PTR_ERR(handle);
685f19d5870STao Ma handle = NULL;
686f19d5870STao Ma goto out;
687f19d5870STao Ma }
688f19d5870STao Ma
689f19d5870STao Ma ret = ext4_prepare_inline_data(handle, inode, pos + len);
690f19d5870STao Ma if (ret && ret != -ENOSPC)
691f19d5870STao Ma goto out;
692f19d5870STao Ma
693f19d5870STao Ma /* We don't have space in inline inode, so convert it to extent. */
694f19d5870STao Ma if (ret == -ENOSPC) {
695f19d5870STao Ma ext4_journal_stop(handle);
696f19d5870STao Ma brelse(iloc.bh);
697f19d5870STao Ma goto convert;
698f19d5870STao Ma }
699f19d5870STao Ma
700188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
701188c299eSJan Kara EXT4_JTR_NONE);
702362eca70STheodore Ts'o if (ret)
703362eca70STheodore Ts'o goto out;
704362eca70STheodore Ts'o
705f8f8c89fSMatthew Wilcox folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
706f8f8c89fSMatthew Wilcox mapping_gfp_mask(mapping));
7077fa8a8eeSLinus Torvalds if (IS_ERR(folio)) {
7087fa8a8eeSLinus Torvalds ret = PTR_ERR(folio);
709f19d5870STao Ma goto out;
710f19d5870STao Ma }
711f19d5870STao Ma
712f8f8c89fSMatthew Wilcox *pagep = &folio->page;
713f19d5870STao Ma down_read(&EXT4_I(inode)->xattr_sem);
714f19d5870STao Ma if (!ext4_has_inline_data(inode)) {
715f19d5870STao Ma ret = 0;
716f8f8c89fSMatthew Wilcox folio_unlock(folio);
717f8f8c89fSMatthew Wilcox folio_put(folio);
718f19d5870STao Ma goto out_up_read;
719f19d5870STao Ma }
720f19d5870STao Ma
721f8f8c89fSMatthew Wilcox if (!folio_test_uptodate(folio)) {
7226b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
723132d00beSMaurizio Lombardi if (ret < 0) {
724f8f8c89fSMatthew Wilcox folio_unlock(folio);
725f8f8c89fSMatthew Wilcox folio_put(folio);
726f19d5870STao Ma goto out_up_read;
727f19d5870STao Ma }
728132d00beSMaurizio Lombardi }
729f19d5870STao Ma
730f19d5870STao Ma ret = 1;
731f19d5870STao Ma handle = NULL;
732f19d5870STao Ma out_up_read:
733f19d5870STao Ma up_read(&EXT4_I(inode)->xattr_sem);
734f19d5870STao Ma out:
735362eca70STheodore Ts'o if (handle && (ret != 1))
736f19d5870STao Ma ext4_journal_stop(handle);
737f19d5870STao Ma brelse(iloc.bh);
738f19d5870STao Ma return ret;
739f19d5870STao Ma convert:
740832ee62dSMatthew Wilcox (Oracle) return ext4_convert_inline_data_to_extent(mapping, inode);
741f19d5870STao Ma }
742f19d5870STao Ma
ext4_write_inline_data_end(struct inode * inode,loff_t pos,unsigned len,unsigned copied,struct folio * folio)743f19d5870STao Ma int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
744d19500daSRitesh Harjani unsigned copied, struct folio *folio)
745f19d5870STao Ma {
7466984aef5SZhang Yi handle_t *handle = ext4_journal_current_handle();
7476984aef5SZhang Yi int no_expand;
748f19d5870STao Ma void *kaddr;
749f19d5870STao Ma struct ext4_iloc iloc;
7506984aef5SZhang Yi int ret = 0, ret2;
751f19d5870STao Ma
7526b90d413SMatthew Wilcox if (unlikely(copied < len) && !folio_test_uptodate(folio))
753f19d5870STao Ma copied = 0;
754f19d5870STao Ma
7556984aef5SZhang Yi if (likely(copied)) {
756f19d5870STao Ma ret = ext4_get_inode_loc(inode, &iloc);
757f19d5870STao Ma if (ret) {
7586b90d413SMatthew Wilcox folio_unlock(folio);
7596b90d413SMatthew Wilcox folio_put(folio);
760f19d5870STao Ma ext4_std_error(inode->i_sb, ret);
761f19d5870STao Ma goto out;
762f19d5870STao Ma }
763c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
764f19d5870STao Ma BUG_ON(!ext4_has_inline_data(inode));
765f19d5870STao Ma
766a54c4613STheodore Ts'o /*
76711ef08c9STheodore Ts'o * ei->i_inline_off may have changed since
76811ef08c9STheodore Ts'o * ext4_write_begin() called
76911ef08c9STheodore Ts'o * ext4_try_to_write_inline_data()
770a54c4613STheodore Ts'o */
771a54c4613STheodore Ts'o (void) ext4_find_inline_data_nolock(inode);
772a54c4613STheodore Ts'o
7736b90d413SMatthew Wilcox kaddr = kmap_local_folio(folio, 0);
77455ce2f64SZhang Yi ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
7756b90d413SMatthew Wilcox kunmap_local(kaddr);
7766b90d413SMatthew Wilcox folio_mark_uptodate(folio);
7776b90d413SMatthew Wilcox /* clear dirty flag so that writepages wouldn't work for us. */
7786b90d413SMatthew Wilcox folio_clear_dirty(folio);
779f19d5870STao Ma
780c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
781f19d5870STao Ma brelse(iloc.bh);
78255ce2f64SZhang Yi
7836984aef5SZhang Yi /*
7846b90d413SMatthew Wilcox * It's important to update i_size while still holding folio
7856984aef5SZhang Yi * lock: page writeout could otherwise come in and zero
7866984aef5SZhang Yi * beyond i_size.
7876984aef5SZhang Yi */
7886984aef5SZhang Yi ext4_update_inode_size(inode, pos + copied);
7896984aef5SZhang Yi }
7906b90d413SMatthew Wilcox folio_unlock(folio);
7916b90d413SMatthew Wilcox folio_put(folio);
7926984aef5SZhang Yi
7936984aef5SZhang Yi /*
7946b90d413SMatthew Wilcox * Don't mark the inode dirty under folio lock. First, it unnecessarily
7956b90d413SMatthew Wilcox * makes the holding time of folio lock longer. Second, it forces lock
7966b90d413SMatthew Wilcox * ordering of folio lock and transaction start for journaling
7976984aef5SZhang Yi * filesystems.
7986984aef5SZhang Yi */
7996984aef5SZhang Yi if (likely(copied))
800362eca70STheodore Ts'o mark_inode_dirty(inode);
801f19d5870STao Ma out:
8026984aef5SZhang Yi /*
8036984aef5SZhang Yi * If we didn't copy as much data as expected, we need to trim back
8046984aef5SZhang Yi * size of xattr containing inline data.
8056984aef5SZhang Yi */
8066984aef5SZhang Yi if (pos + len > inode->i_size && ext4_can_truncate(inode))
8076984aef5SZhang Yi ext4_orphan_add(handle, inode);
8086984aef5SZhang Yi
8096984aef5SZhang Yi ret2 = ext4_journal_stop(handle);
8106984aef5SZhang Yi if (!ret)
8116984aef5SZhang Yi ret = ret2;
8126984aef5SZhang Yi if (pos + len > inode->i_size) {
8136984aef5SZhang Yi ext4_truncate_failed_write(inode);
8146984aef5SZhang Yi /*
8156984aef5SZhang Yi * If truncate failed early the inode might still be
8166984aef5SZhang Yi * on the orphan list; we need to make sure the inode
8176984aef5SZhang Yi * is removed from the orphan list in that case.
8186984aef5SZhang Yi */
8196984aef5SZhang Yi if (inode->i_nlink)
8206984aef5SZhang Yi ext4_orphan_del(NULL, inode);
8216984aef5SZhang Yi }
8226984aef5SZhang Yi return ret ? ret : copied;
823f19d5870STao Ma }
824f19d5870STao Ma
8259c3569b5STao Ma /*
8269c3569b5STao Ma * Try to make the page cache and handle ready for the inline data case.
8279c3569b5STao Ma * We can call this function in 2 cases:
8289c3569b5STao Ma * 1. The inode is created and the first write exceeds inline size. We can
8299c3569b5STao Ma * clear the inode state safely.
8309c3569b5STao Ma * 2. The inode has inline data, then we need to read the data, make it
8319c3569b5STao Ma * update and dirty so that ext4_da_writepages can handle it. We don't
8323088e5a5SBhaskar Chowdhury * need to start the journal since the file's metadata isn't changed now.
8339c3569b5STao Ma */
ext4_da_convert_inline_data_to_extent(struct address_space * mapping,struct inode * inode,void ** fsdata)8349c3569b5STao Ma static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
8359c3569b5STao Ma struct inode *inode,
8369c3569b5STao Ma void **fsdata)
8379c3569b5STao Ma {
8389c3569b5STao Ma int ret = 0, inline_size;
8394ed9b598SMatthew Wilcox struct folio *folio;
8409c3569b5STao Ma
8414ed9b598SMatthew Wilcox folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN,
8424ed9b598SMatthew Wilcox mapping_gfp_mask(mapping));
8437fa8a8eeSLinus Torvalds if (IS_ERR(folio))
8447fa8a8eeSLinus Torvalds return PTR_ERR(folio);
8459c3569b5STao Ma
8469c3569b5STao Ma down_read(&EXT4_I(inode)->xattr_sem);
8479c3569b5STao Ma if (!ext4_has_inline_data(inode)) {
8489c3569b5STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
8499c3569b5STao Ma goto out;
8509c3569b5STao Ma }
8519c3569b5STao Ma
8529c3569b5STao Ma inline_size = ext4_get_inline_size(inode);
8539c3569b5STao Ma
8544ed9b598SMatthew Wilcox if (!folio_test_uptodate(folio)) {
8556b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
8569c3569b5STao Ma if (ret < 0)
8579c3569b5STao Ma goto out;
8589c3569b5STao Ma }
8599c3569b5STao Ma
8604ed9b598SMatthew Wilcox ret = __block_write_begin(&folio->page, 0, inline_size,
8619c3569b5STao Ma ext4_da_get_block_prep);
8629c3569b5STao Ma if (ret) {
86350db71abSDmitry Monakhov up_read(&EXT4_I(inode)->xattr_sem);
8644ed9b598SMatthew Wilcox folio_unlock(folio);
8654ed9b598SMatthew Wilcox folio_put(folio);
8669c3569b5STao Ma ext4_truncate_failed_write(inode);
86750db71abSDmitry Monakhov return ret;
8689c3569b5STao Ma }
8699c3569b5STao Ma
8704ed9b598SMatthew Wilcox folio_mark_dirty(folio);
8714ed9b598SMatthew Wilcox folio_mark_uptodate(folio);
8729c3569b5STao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
8739c3569b5STao Ma *fsdata = (void *)CONVERT_INLINE_DATA;
8749c3569b5STao Ma
8759c3569b5STao Ma out:
8769c3569b5STao Ma up_read(&EXT4_I(inode)->xattr_sem);
8774ed9b598SMatthew Wilcox if (folio) {
8784ed9b598SMatthew Wilcox folio_unlock(folio);
8794ed9b598SMatthew Wilcox folio_put(folio);
8809c3569b5STao Ma }
8819c3569b5STao Ma return ret;
8829c3569b5STao Ma }
8839c3569b5STao Ma
8849c3569b5STao Ma /*
8859c3569b5STao Ma * Prepare the write for the inline data.
8868d6ce136SShijie Luo * If the data can be written into the inode, we just read
8879c3569b5STao Ma * the page and make it uptodate, and start the journal.
8889c3569b5STao Ma * Otherwise read the page, makes it dirty so that it can be
8899c3569b5STao Ma * handle in writepages(the i_disksize update is left to the
8909c3569b5STao Ma * normal ext4_da_write_end).
8919c3569b5STao Ma */
ext4_da_write_inline_data_begin(struct address_space * mapping,struct inode * inode,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)8929c3569b5STao Ma int ext4_da_write_inline_data_begin(struct address_space *mapping,
8939c3569b5STao Ma struct inode *inode,
8949c3569b5STao Ma loff_t pos, unsigned len,
8959c3569b5STao Ma struct page **pagep,
8969c3569b5STao Ma void **fsdata)
8979c3569b5STao Ma {
89809355d9dSRitesh Harjani int ret;
8999c3569b5STao Ma handle_t *handle;
9009a9d01f0SMatthew Wilcox struct folio *folio;
9019c3569b5STao Ma struct ext4_iloc iloc;
902625ef8a3SLukas Czerner int retries = 0;
9039c3569b5STao Ma
9049c3569b5STao Ma ret = ext4_get_inode_loc(inode, &iloc);
9059c3569b5STao Ma if (ret)
9069c3569b5STao Ma return ret;
9079c3569b5STao Ma
908bc0ca9dfSJan Kara retry_journal:
9099924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
9109c3569b5STao Ma if (IS_ERR(handle)) {
9119c3569b5STao Ma ret = PTR_ERR(handle);
9129c3569b5STao Ma goto out;
9139c3569b5STao Ma }
9149c3569b5STao Ma
9159c3569b5STao Ma ret = ext4_prepare_inline_data(handle, inode, pos + len);
9169c3569b5STao Ma if (ret && ret != -ENOSPC)
91752e44777SJan Kara goto out_journal;
9189c3569b5STao Ma
9199c3569b5STao Ma if (ret == -ENOSPC) {
9208bc1379bSTheodore Ts'o ext4_journal_stop(handle);
9219c3569b5STao Ma ret = ext4_da_convert_inline_data_to_extent(mapping,
9229c3569b5STao Ma inode,
9239c3569b5STao Ma fsdata);
924bc0ca9dfSJan Kara if (ret == -ENOSPC &&
925bc0ca9dfSJan Kara ext4_should_retry_alloc(inode->i_sb, &retries))
926bc0ca9dfSJan Kara goto retry_journal;
9279c3569b5STao Ma goto out;
9289c3569b5STao Ma }
9299c3569b5STao Ma
93036d116e9SMatthew Wilcox (Oracle) /*
93136d116e9SMatthew Wilcox (Oracle) * We cannot recurse into the filesystem as the transaction
93236d116e9SMatthew Wilcox (Oracle) * is already started.
93336d116e9SMatthew Wilcox (Oracle) */
9349a9d01f0SMatthew Wilcox folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
9359a9d01f0SMatthew Wilcox mapping_gfp_mask(mapping));
9367fa8a8eeSLinus Torvalds if (IS_ERR(folio)) {
9377fa8a8eeSLinus Torvalds ret = PTR_ERR(folio);
93852e44777SJan Kara goto out_journal;
9399c3569b5STao Ma }
9409c3569b5STao Ma
9419c3569b5STao Ma down_read(&EXT4_I(inode)->xattr_sem);
9429c3569b5STao Ma if (!ext4_has_inline_data(inode)) {
9439c3569b5STao Ma ret = 0;
9449c3569b5STao Ma goto out_release_page;
9459c3569b5STao Ma }
9469c3569b5STao Ma
9479a9d01f0SMatthew Wilcox if (!folio_test_uptodate(folio)) {
9486b87fbe4SMatthew Wilcox ret = ext4_read_inline_folio(inode, folio);
9499c3569b5STao Ma if (ret < 0)
9509c3569b5STao Ma goto out_release_page;
9519c3569b5STao Ma }
952188c299eSJan Kara ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh,
953188c299eSJan Kara EXT4_JTR_NONE);
954362eca70STheodore Ts'o if (ret)
955362eca70STheodore Ts'o goto out_release_page;
9569c3569b5STao Ma
9579c3569b5STao Ma up_read(&EXT4_I(inode)->xattr_sem);
9589a9d01f0SMatthew Wilcox *pagep = &folio->page;
9599c3569b5STao Ma brelse(iloc.bh);
9609c3569b5STao Ma return 1;
9619c3569b5STao Ma out_release_page:
9629c3569b5STao Ma up_read(&EXT4_I(inode)->xattr_sem);
9639a9d01f0SMatthew Wilcox folio_unlock(folio);
9649a9d01f0SMatthew Wilcox folio_put(folio);
96552e44777SJan Kara out_journal:
9669c3569b5STao Ma ext4_journal_stop(handle);
96752e44777SJan Kara out:
9689c3569b5STao Ma brelse(iloc.bh);
9699c3569b5STao Ma return ret;
9709c3569b5STao Ma }
9719c3569b5STao Ma
9723c47d541STao Ma #ifdef INLINE_DIR_DEBUG
ext4_show_inline_dir(struct inode * dir,struct buffer_head * bh,void * inline_start,int inline_size)9733c47d541STao Ma void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
9743c47d541STao Ma void *inline_start, int inline_size)
9753c47d541STao Ma {
9763c47d541STao Ma int offset;
9773c47d541STao Ma unsigned short de_len;
9783c47d541STao Ma struct ext4_dir_entry_2 *de = inline_start;
9793c47d541STao Ma void *dlimit = inline_start + inline_size;
9803c47d541STao Ma
9813c47d541STao Ma trace_printk("inode %lu\n", dir->i_ino);
9823c47d541STao Ma offset = 0;
9833c47d541STao Ma while ((void *)de < dlimit) {
9843c47d541STao Ma de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
98580cfb71eSRasmus Villemoes trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
9863c47d541STao Ma offset, de_len, de->name_len, de->name,
9873c47d541STao Ma de->name_len, le32_to_cpu(de->inode));
9883c47d541STao Ma if (ext4_check_dir_entry(dir, NULL, de, bh,
9893c47d541STao Ma inline_start, inline_size, offset))
9903c47d541STao Ma BUG();
9913c47d541STao Ma
9923c47d541STao Ma offset += de_len;
9933c47d541STao Ma de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
9943c47d541STao Ma }
9953c47d541STao Ma }
9963c47d541STao Ma #else
9973c47d541STao Ma #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
9983c47d541STao Ma #endif
9993c47d541STao Ma
10003c47d541STao Ma /*
10013c47d541STao Ma * Add a new entry into a inline dir.
10023c47d541STao Ma * It will return -ENOSPC if no space is available, and -EIO
10033c47d541STao Ma * and -EEXIST if directory entry already exists.
10043c47d541STao 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)10053c47d541STao Ma static int ext4_add_dirent_to_inline(handle_t *handle,
10065b643f9cSTheodore Ts'o struct ext4_filename *fname,
100756a04915STheodore Ts'o struct inode *dir,
10083c47d541STao Ma struct inode *inode,
10093c47d541STao Ma struct ext4_iloc *iloc,
10103c47d541STao Ma void *inline_start, int inline_size)
10113c47d541STao Ma {
10123c47d541STao Ma int err;
10133c47d541STao Ma struct ext4_dir_entry_2 *de;
10143c47d541STao Ma
10155b643f9cSTheodore Ts'o err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
10165b643f9cSTheodore Ts'o inline_size, fname, &de);
10173c47d541STao Ma if (err)
10183c47d541STao Ma return err;
10193c47d541STao Ma
10205d601255Sliang xie BUFFER_TRACE(iloc->bh, "get_write_access");
1021188c299eSJan Kara err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
1022188c299eSJan Kara EXT4_JTR_NONE);
10233c47d541STao Ma if (err)
10243c47d541STao Ma return err;
1025471fbbeaSDaniel Rosenberg ext4_insert_dentry(dir, inode, de, inline_size, fname);
10263c47d541STao Ma
10273c47d541STao Ma ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
10283c47d541STao Ma
10293c47d541STao Ma /*
10303c47d541STao Ma * XXX shouldn't update any times until successful
10313c47d541STao Ma * completion of syscall, but too many callers depend
10323c47d541STao Ma * on this.
10333c47d541STao Ma *
10343c47d541STao Ma * XXX similarly, too many callers depend on
10353c47d541STao Ma * ext4_new_inode() setting the times, but error
10363c47d541STao Ma * recovery deletes the inode, so the worst that can
10373c47d541STao Ma * happen is that the times are slightly out of date
10383c47d541STao Ma * and/or different from the directory change time.
10393c47d541STao Ma */
1040*fa42d5f1SJeff Layton inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
10413c47d541STao Ma ext4_update_dx_flag(dir);
1042ee73f9a5SJeff Layton inode_inc_iversion(dir);
10433c47d541STao Ma return 1;
10443c47d541STao Ma }
10453c47d541STao Ma
ext4_get_inline_xattr_pos(struct inode * inode,struct ext4_iloc * iloc)10463c47d541STao Ma static void *ext4_get_inline_xattr_pos(struct inode *inode,
10473c47d541STao Ma struct ext4_iloc *iloc)
10483c47d541STao Ma {
10493c47d541STao Ma struct ext4_xattr_entry *entry;
10503c47d541STao Ma struct ext4_xattr_ibody_header *header;
10513c47d541STao Ma
10523c47d541STao Ma BUG_ON(!EXT4_I(inode)->i_inline_off);
10533c47d541STao Ma
10543c47d541STao Ma header = IHDR(inode, ext4_raw_inode(iloc));
10553c47d541STao Ma entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
10563c47d541STao Ma EXT4_I(inode)->i_inline_off);
10573c47d541STao Ma
10583c47d541STao Ma return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
10593c47d541STao Ma }
10603c47d541STao Ma
10613c47d541STao Ma /* Set the final de to cover the whole block. */
ext4_update_final_de(void * de_buf,int old_size,int new_size)10623c47d541STao Ma static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
10633c47d541STao Ma {
10643c47d541STao Ma struct ext4_dir_entry_2 *de, *prev_de;
10653c47d541STao Ma void *limit;
10663c47d541STao Ma int de_len;
10673c47d541STao Ma
1068c30365b9SYu Zhe de = de_buf;
10693c47d541STao Ma if (old_size) {
10703c47d541STao Ma limit = de_buf + old_size;
10713c47d541STao Ma do {
10723c47d541STao Ma prev_de = de;
10733c47d541STao Ma de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
10743c47d541STao Ma de_buf += de_len;
1075c30365b9SYu Zhe de = de_buf;
10763c47d541STao Ma } while (de_buf < limit);
10773c47d541STao Ma
10783c47d541STao Ma prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
10793c47d541STao Ma old_size, new_size);
10803c47d541STao Ma } else {
10813c47d541STao Ma /* this is just created, so create an empty entry. */
10823c47d541STao Ma de->inode = 0;
10833c47d541STao Ma de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
10843c47d541STao Ma }
10853c47d541STao Ma }
10863c47d541STao Ma
ext4_update_inline_dir(handle_t * handle,struct inode * dir,struct ext4_iloc * iloc)10873c47d541STao Ma static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
10883c47d541STao Ma struct ext4_iloc *iloc)
10893c47d541STao Ma {
10903c47d541STao Ma int ret;
10913c47d541STao Ma int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
10923c47d541STao Ma int new_size = get_max_inline_xattr_value_size(dir, iloc);
10933c47d541STao Ma
1094471fbbeaSDaniel Rosenberg if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
10953c47d541STao Ma return -ENOSPC;
10963c47d541STao Ma
10973c47d541STao Ma ret = ext4_update_inline_data(handle, dir,
10983c47d541STao Ma new_size + EXT4_MIN_INLINE_DATA_SIZE);
10993c47d541STao Ma if (ret)
11003c47d541STao Ma return ret;
11013c47d541STao Ma
11023c47d541STao Ma ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
11033c47d541STao Ma EXT4_I(dir)->i_inline_size -
11043c47d541STao Ma EXT4_MIN_INLINE_DATA_SIZE);
11053c47d541STao Ma dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
11063c47d541STao Ma return 0;
11073c47d541STao Ma }
11083c47d541STao Ma
ext4_restore_inline_data(handle_t * handle,struct inode * inode,struct ext4_iloc * iloc,void * buf,int inline_size)11093c47d541STao Ma static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
11103c47d541STao Ma struct ext4_iloc *iloc,
11113c47d541STao Ma void *buf, int inline_size)
11123c47d541STao Ma {
1113897026aaSRitesh Harjani int ret;
1114897026aaSRitesh Harjani
1115897026aaSRitesh Harjani ret = ext4_create_inline_data(handle, inode, inline_size);
1116897026aaSRitesh Harjani if (ret) {
1117897026aaSRitesh Harjani ext4_msg(inode->i_sb, KERN_EMERG,
1118897026aaSRitesh Harjani "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
1119897026aaSRitesh Harjani inode->i_ino, ret);
1120897026aaSRitesh Harjani return;
1121897026aaSRitesh Harjani }
11223c47d541STao Ma ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
11233c47d541STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
11243c47d541STao Ma }
11253c47d541STao Ma
ext4_finish_convert_inline_dir(handle_t * handle,struct inode * inode,struct buffer_head * dir_block,void * buf,int inline_size)11263c47d541STao Ma static int ext4_finish_convert_inline_dir(handle_t *handle,
11273c47d541STao Ma struct inode *inode,
11283c47d541STao Ma struct buffer_head *dir_block,
11293c47d541STao Ma void *buf,
11303c47d541STao Ma int inline_size)
11313c47d541STao Ma {
11323c47d541STao Ma int err, csum_size = 0, header_size = 0;
11333c47d541STao Ma struct ext4_dir_entry_2 *de;
11343c47d541STao Ma void *target = dir_block->b_data;
11353c47d541STao Ma
11363c47d541STao Ma /*
11373c47d541STao Ma * First create "." and ".." and then copy the dir information
11383c47d541STao Ma * back to the block.
11393c47d541STao Ma */
1140c30365b9SYu Zhe de = target;
11413c47d541STao Ma de = ext4_init_dot_dotdot(inode, de,
11423c47d541STao Ma inode->i_sb->s_blocksize, csum_size,
11433c47d541STao Ma le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
11443c47d541STao Ma header_size = (void *)de - target;
11453c47d541STao Ma
11463c47d541STao Ma memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
11473c47d541STao Ma inline_size - EXT4_INLINE_DOTDOT_SIZE);
11483c47d541STao Ma
11499aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(inode->i_sb))
11503c47d541STao Ma csum_size = sizeof(struct ext4_dir_entry_tail);
11513c47d541STao Ma
11523c47d541STao Ma inode->i_size = inode->i_sb->s_blocksize;
11533c47d541STao Ma i_size_write(inode, inode->i_sb->s_blocksize);
11543c47d541STao Ma EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
11553c47d541STao Ma ext4_update_final_de(dir_block->b_data,
11563c47d541STao Ma inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
11573c47d541STao Ma inode->i_sb->s_blocksize - csum_size);
11583c47d541STao Ma
1159ddce3b94STheodore Ts'o if (csum_size)
1160ddce3b94STheodore Ts'o ext4_initialize_dirent_tail(dir_block,
11613c47d541STao Ma inode->i_sb->s_blocksize);
11623c47d541STao Ma set_buffer_uptodate(dir_block);
1163f4ce24f5STheodore Ts'o unlock_buffer(dir_block);
1164f036adb3STheodore Ts'o err = ext4_handle_dirty_dirblock(handle, inode, dir_block);
11653c47d541STao Ma if (err)
11663c47d541STao Ma return err;
1167b9cf625dSEric Biggers set_buffer_verified(dir_block);
1168b9cf625dSEric Biggers return ext4_mark_inode_dirty(handle, inode);
11693c47d541STao Ma }
11703c47d541STao Ma
ext4_convert_inline_data_nolock(handle_t * handle,struct inode * inode,struct ext4_iloc * iloc)11713c47d541STao Ma static int ext4_convert_inline_data_nolock(handle_t *handle,
11723c47d541STao Ma struct inode *inode,
11733c47d541STao Ma struct ext4_iloc *iloc)
11743c47d541STao Ma {
11753c47d541STao Ma int error;
11763c47d541STao Ma void *buf = NULL;
11773c47d541STao Ma struct buffer_head *data_bh = NULL;
11783c47d541STao Ma struct ext4_map_blocks map;
11793c47d541STao Ma int inline_size;
11803c47d541STao Ma
11813c47d541STao Ma inline_size = ext4_get_inline_size(inode);
11823c47d541STao Ma buf = kmalloc(inline_size, GFP_NOFS);
11833c47d541STao Ma if (!buf) {
11843c47d541STao Ma error = -ENOMEM;
11853c47d541STao Ma goto out;
11863c47d541STao Ma }
11873c47d541STao Ma
11883c47d541STao Ma error = ext4_read_inline_data(inode, buf, inline_size, iloc);
11893c47d541STao Ma if (error < 0)
11903c47d541STao Ma goto out;
11913c47d541STao Ma
119240b163f1SDarrick J. Wong /*
119340b163f1SDarrick J. Wong * Make sure the inline directory entries pass checks before we try to
119440b163f1SDarrick J. Wong * convert them, so that we avoid touching stuff that needs fsck.
119540b163f1SDarrick J. Wong */
119640b163f1SDarrick J. Wong if (S_ISDIR(inode->i_mode)) {
119740b163f1SDarrick J. Wong error = ext4_check_all_de(inode, iloc->bh,
119840b163f1SDarrick J. Wong buf + EXT4_INLINE_DOTDOT_SIZE,
119940b163f1SDarrick J. Wong inline_size - EXT4_INLINE_DOTDOT_SIZE);
120040b163f1SDarrick J. Wong if (error)
120140b163f1SDarrick J. Wong goto out;
120240b163f1SDarrick J. Wong }
120340b163f1SDarrick J. Wong
12043c47d541STao Ma error = ext4_destroy_inline_data_nolock(handle, inode);
12053c47d541STao Ma if (error)
12063c47d541STao Ma goto out;
12073c47d541STao Ma
12083c47d541STao Ma map.m_lblk = 0;
12093c47d541STao Ma map.m_len = 1;
12103c47d541STao Ma map.m_flags = 0;
12113c47d541STao Ma error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
12123c47d541STao Ma if (error < 0)
12133c47d541STao Ma goto out_restore;
12143c47d541STao Ma if (!(map.m_flags & EXT4_MAP_MAPPED)) {
12153c47d541STao Ma error = -EIO;
12163c47d541STao Ma goto out_restore;
12173c47d541STao Ma }
12183c47d541STao Ma
12193c47d541STao Ma data_bh = sb_getblk(inode->i_sb, map.m_pblk);
12203c47d541STao Ma if (!data_bh) {
1221860d21e2STheodore Ts'o error = -ENOMEM;
12223c47d541STao Ma goto out_restore;
12233c47d541STao Ma }
12243c47d541STao Ma
12253c47d541STao Ma lock_buffer(data_bh);
1226188c299eSJan Kara error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1227188c299eSJan Kara EXT4_JTR_NONE);
12283c47d541STao Ma if (error) {
12293c47d541STao Ma unlock_buffer(data_bh);
12303c47d541STao Ma error = -EIO;
12313c47d541STao Ma goto out_restore;
12323c47d541STao Ma }
12333c47d541STao Ma memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
12343c47d541STao Ma
12353c47d541STao Ma if (!S_ISDIR(inode->i_mode)) {
12363c47d541STao Ma memcpy(data_bh->b_data, buf, inline_size);
12373c47d541STao Ma set_buffer_uptodate(data_bh);
1238f4ce24f5STheodore Ts'o unlock_buffer(data_bh);
12393c47d541STao Ma error = ext4_handle_dirty_metadata(handle,
12403c47d541STao Ma inode, data_bh);
12413c47d541STao Ma } else {
12423c47d541STao Ma error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
12433c47d541STao Ma buf, inline_size);
12443c47d541STao Ma }
12453c47d541STao Ma
12463c47d541STao Ma out_restore:
12473c47d541STao Ma if (error)
12483c47d541STao Ma ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
12493c47d541STao Ma
12503c47d541STao Ma out:
12513c47d541STao Ma brelse(data_bh);
12523c47d541STao Ma kfree(buf);
12533c47d541STao Ma return error;
12543c47d541STao Ma }
12553c47d541STao Ma
12563c47d541STao Ma /*
12573c47d541STao Ma * Try to add the new entry to the inline data.
12583c47d541STao Ma * If succeeds, return 0. If not, extended the inline dir and copied data to
12593c47d541STao Ma * the new created block.
12603c47d541STao Ma */
ext4_try_add_inline_entry(handle_t * handle,struct ext4_filename * fname,struct inode * dir,struct inode * inode)12615b643f9cSTheodore Ts'o int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
126256a04915STheodore Ts'o struct inode *dir, struct inode *inode)
12633c47d541STao Ma {
12644209ae12SHarshad Shirwadkar int ret, ret2, inline_size, no_expand;
12653c47d541STao Ma void *inline_start;
12663c47d541STao Ma struct ext4_iloc iloc;
12673c47d541STao Ma
12683c47d541STao Ma ret = ext4_get_inode_loc(dir, &iloc);
12693c47d541STao Ma if (ret)
12703c47d541STao Ma return ret;
12713c47d541STao Ma
1272c755e251STheodore Ts'o ext4_write_lock_xattr(dir, &no_expand);
12733c47d541STao Ma if (!ext4_has_inline_data(dir))
12743c47d541STao Ma goto out;
12753c47d541STao Ma
12763c47d541STao Ma inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
12773c47d541STao Ma EXT4_INLINE_DOTDOT_SIZE;
12783c47d541STao Ma inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
12793c47d541STao Ma
128056a04915STheodore Ts'o ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
12813c47d541STao Ma inline_start, inline_size);
12823c47d541STao Ma if (ret != -ENOSPC)
12833c47d541STao Ma goto out;
12843c47d541STao Ma
12853c47d541STao Ma /* check whether it can be inserted to inline xattr space. */
12863c47d541STao Ma inline_size = EXT4_I(dir)->i_inline_size -
12873c47d541STao Ma EXT4_MIN_INLINE_DATA_SIZE;
12883c47d541STao Ma if (!inline_size) {
12893c47d541STao Ma /* Try to use the xattr space.*/
12903c47d541STao Ma ret = ext4_update_inline_dir(handle, dir, &iloc);
12913c47d541STao Ma if (ret && ret != -ENOSPC)
12923c47d541STao Ma goto out;
12933c47d541STao Ma
12943c47d541STao Ma inline_size = EXT4_I(dir)->i_inline_size -
12953c47d541STao Ma EXT4_MIN_INLINE_DATA_SIZE;
12963c47d541STao Ma }
12973c47d541STao Ma
12983c47d541STao Ma if (inline_size) {
12993c47d541STao Ma inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
13003c47d541STao Ma
130156a04915STheodore Ts'o ret = ext4_add_dirent_to_inline(handle, fname, dir,
13025b643f9cSTheodore Ts'o inode, &iloc, inline_start,
13035b643f9cSTheodore Ts'o inline_size);
13043c47d541STao Ma
13053c47d541STao Ma if (ret != -ENOSPC)
13063c47d541STao Ma goto out;
13073c47d541STao Ma }
13083c47d541STao Ma
13093c47d541STao Ma /*
13103c47d541STao Ma * The inline space is filled up, so create a new block for it.
13113c47d541STao Ma * As the extent tree will be created, we have to save the inline
13123c47d541STao Ma * dir first.
13133c47d541STao Ma */
13143c47d541STao Ma ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
13153c47d541STao Ma
13163c47d541STao Ma out:
1317c755e251STheodore Ts'o ext4_write_unlock_xattr(dir, &no_expand);
13184209ae12SHarshad Shirwadkar ret2 = ext4_mark_inode_dirty(handle, dir);
13194209ae12SHarshad Shirwadkar if (unlikely(ret2 && !ret))
13204209ae12SHarshad Shirwadkar ret = ret2;
13213c47d541STao Ma brelse(iloc.bh);
13223c47d541STao Ma return ret;
13233c47d541STao Ma }
13243c47d541STao Ma
13258af0f082STao Ma /*
13268af0f082STao Ma * This function fills a red-black tree with information from an
13278af0f082STao Ma * inlined dir. It returns the number directory entries loaded
13288af0f082STao Ma * into the tree. If there is an error it is returned in err.
13298af0f082STao 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)13307633b08bSTheodore Ts'o int ext4_inlinedir_to_tree(struct file *dir_file,
13318af0f082STao Ma struct inode *dir, ext4_lblk_t block,
13328af0f082STao Ma struct dx_hash_info *hinfo,
13338af0f082STao Ma __u32 start_hash, __u32 start_minor_hash,
13348af0f082STao Ma int *has_inline_data)
13358af0f082STao Ma {
13368af0f082STao Ma int err = 0, count = 0;
13378af0f082STao Ma unsigned int parent_ino;
13388af0f082STao Ma int pos;
13398af0f082STao Ma struct ext4_dir_entry_2 *de;
13408af0f082STao Ma struct inode *inode = file_inode(dir_file);
13418af0f082STao Ma int ret, inline_size = 0;
13428af0f082STao Ma struct ext4_iloc iloc;
13438af0f082STao Ma void *dir_buf = NULL;
13448af0f082STao Ma struct ext4_dir_entry_2 fake;
1345a7550b30SJaegeuk Kim struct fscrypt_str tmp_str;
13468af0f082STao Ma
13478af0f082STao Ma ret = ext4_get_inode_loc(inode, &iloc);
13488af0f082STao Ma if (ret)
13498af0f082STao Ma return ret;
13508af0f082STao Ma
13518af0f082STao Ma down_read(&EXT4_I(inode)->xattr_sem);
13528af0f082STao Ma if (!ext4_has_inline_data(inode)) {
13538af0f082STao Ma up_read(&EXT4_I(inode)->xattr_sem);
13548af0f082STao Ma *has_inline_data = 0;
13558af0f082STao Ma goto out;
13568af0f082STao Ma }
13578af0f082STao Ma
13588af0f082STao Ma inline_size = ext4_get_inline_size(inode);
13598af0f082STao Ma dir_buf = kmalloc(inline_size, GFP_NOFS);
13608af0f082STao Ma if (!dir_buf) {
13618af0f082STao Ma ret = -ENOMEM;
13628af0f082STao Ma up_read(&EXT4_I(inode)->xattr_sem);
13638af0f082STao Ma goto out;
13648af0f082STao Ma }
13658af0f082STao Ma
13668af0f082STao Ma ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
13678af0f082STao Ma up_read(&EXT4_I(inode)->xattr_sem);
13688af0f082STao Ma if (ret < 0)
13698af0f082STao Ma goto out;
13708af0f082STao Ma
13718af0f082STao Ma pos = 0;
13728af0f082STao Ma parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
13738af0f082STao Ma while (pos < inline_size) {
13748af0f082STao Ma /*
13758af0f082STao Ma * As inlined dir doesn't store any information about '.' and
13768af0f082STao Ma * only the inode number of '..' is stored, we have to handle
13778af0f082STao Ma * them differently.
13788af0f082STao Ma */
13798af0f082STao Ma if (pos == 0) {
13808af0f082STao Ma fake.inode = cpu_to_le32(inode->i_ino);
13818af0f082STao Ma fake.name_len = 1;
13828af0f082STao Ma strcpy(fake.name, ".");
13838af0f082STao Ma fake.rec_len = ext4_rec_len_to_disk(
1384471fbbeaSDaniel Rosenberg ext4_dir_rec_len(fake.name_len, NULL),
13858af0f082STao Ma inline_size);
13868af0f082STao Ma ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
13878af0f082STao Ma de = &fake;
13888af0f082STao Ma pos = EXT4_INLINE_DOTDOT_OFFSET;
13898af0f082STao Ma } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
13908af0f082STao Ma fake.inode = cpu_to_le32(parent_ino);
13918af0f082STao Ma fake.name_len = 2;
13928af0f082STao Ma strcpy(fake.name, "..");
13938af0f082STao Ma fake.rec_len = ext4_rec_len_to_disk(
1394471fbbeaSDaniel Rosenberg ext4_dir_rec_len(fake.name_len, NULL),
13958af0f082STao Ma inline_size);
13968af0f082STao Ma ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
13978af0f082STao Ma de = &fake;
13988af0f082STao Ma pos = EXT4_INLINE_DOTDOT_SIZE;
13998af0f082STao Ma } else {
14008af0f082STao Ma de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
14018af0f082STao Ma pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
14028af0f082STao Ma if (ext4_check_dir_entry(inode, dir_file, de,
14038af0f082STao Ma iloc.bh, dir_buf,
14048af0f082STao Ma inline_size, pos)) {
14058af0f082STao Ma ret = count;
14068af0f082STao Ma goto out;
14078af0f082STao Ma }
14088af0f082STao Ma }
14098af0f082STao Ma
1410471fbbeaSDaniel Rosenberg if (ext4_hash_in_dirent(dir)) {
1411471fbbeaSDaniel Rosenberg hinfo->hash = EXT4_DIRENT_HASH(de);
1412471fbbeaSDaniel Rosenberg hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1413471fbbeaSDaniel Rosenberg } else {
1414e7abdad6SXiaxi Shen err = ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1415e7abdad6SXiaxi Shen if (err) {
1416e7abdad6SXiaxi Shen ret = err;
1417e7abdad6SXiaxi Shen goto out;
1418e7abdad6SXiaxi Shen }
1419471fbbeaSDaniel Rosenberg }
14208af0f082STao Ma if ((hinfo->hash < start_hash) ||
14218af0f082STao Ma ((hinfo->hash == start_hash) &&
14228af0f082STao Ma (hinfo->minor_hash < start_minor_hash)))
14238af0f082STao Ma continue;
14248af0f082STao Ma if (de->inode == 0)
14258af0f082STao Ma continue;
14262f61830aSTheodore Ts'o tmp_str.name = de->name;
14272f61830aSTheodore Ts'o tmp_str.len = de->name_len;
14282f61830aSTheodore Ts'o err = ext4_htree_store_dirent(dir_file, hinfo->hash,
14292f61830aSTheodore Ts'o hinfo->minor_hash, de, &tmp_str);
14308af0f082STao Ma if (err) {
14317a14826eSColin Ian King ret = err;
14328af0f082STao Ma goto out;
14338af0f082STao Ma }
14348af0f082STao Ma count++;
14358af0f082STao Ma }
14368af0f082STao Ma ret = count;
14378af0f082STao Ma out:
14388af0f082STao Ma kfree(dir_buf);
14398af0f082STao Ma brelse(iloc.bh);
14408af0f082STao Ma return ret;
14418af0f082STao Ma }
14428af0f082STao Ma
1443c4d8b023STao Ma /*
1444c4d8b023STao Ma * So this function is called when the volume is mkfsed with
1445c4d8b023STao Ma * dir_index disabled. In order to keep f_pos persistent
1446c4d8b023STao Ma * after we convert from an inlined dir to a blocked based,
1447c4d8b023STao Ma * we just pretend that we are a normal dir and return the
1448c4d8b023STao Ma * offset as if '.' and '..' really take place.
1449c4d8b023STao Ma *
1450c4d8b023STao Ma */
ext4_read_inline_dir(struct file * file,struct dir_context * ctx,int * has_inline_data)1451725bebb2SAl Viro int ext4_read_inline_dir(struct file *file,
1452725bebb2SAl Viro struct dir_context *ctx,
145365d165d9STao Ma int *has_inline_data)
145465d165d9STao Ma {
145565d165d9STao Ma unsigned int offset, parent_ino;
1456725bebb2SAl Viro int i;
145765d165d9STao Ma struct ext4_dir_entry_2 *de;
145865d165d9STao Ma struct super_block *sb;
1459725bebb2SAl Viro struct inode *inode = file_inode(file);
146065d165d9STao Ma int ret, inline_size = 0;
146165d165d9STao Ma struct ext4_iloc iloc;
146265d165d9STao Ma void *dir_buf = NULL;
1463c4d8b023STao Ma int dotdot_offset, dotdot_size, extra_offset, extra_size;
146465d165d9STao Ma
146565d165d9STao Ma ret = ext4_get_inode_loc(inode, &iloc);
146665d165d9STao Ma if (ret)
146765d165d9STao Ma return ret;
146865d165d9STao Ma
146965d165d9STao Ma down_read(&EXT4_I(inode)->xattr_sem);
147065d165d9STao Ma if (!ext4_has_inline_data(inode)) {
147165d165d9STao Ma up_read(&EXT4_I(inode)->xattr_sem);
147265d165d9STao Ma *has_inline_data = 0;
147365d165d9STao Ma goto out;
147465d165d9STao Ma }
147565d165d9STao Ma
147665d165d9STao Ma inline_size = ext4_get_inline_size(inode);
147765d165d9STao Ma dir_buf = kmalloc(inline_size, GFP_NOFS);
147865d165d9STao Ma if (!dir_buf) {
147965d165d9STao Ma ret = -ENOMEM;
148065d165d9STao Ma up_read(&EXT4_I(inode)->xattr_sem);
148165d165d9STao Ma goto out;
148265d165d9STao Ma }
148365d165d9STao Ma
148465d165d9STao Ma ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
148565d165d9STao Ma up_read(&EXT4_I(inode)->xattr_sem);
148665d165d9STao Ma if (ret < 0)
148765d165d9STao Ma goto out;
148865d165d9STao Ma
148948ffdab1SBoxiLiu ret = 0;
149065d165d9STao Ma sb = inode->i_sb;
149165d165d9STao Ma parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1492725bebb2SAl Viro offset = ctx->pos;
149365d165d9STao Ma
1494c4d8b023STao Ma /*
1495c4d8b023STao Ma * dotdot_offset and dotdot_size is the real offset and
1496c4d8b023STao Ma * size for ".." and "." if the dir is block based while
1497c4d8b023STao Ma * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1498c4d8b023STao Ma * So we will use extra_offset and extra_size to indicate them
1499c4d8b023STao Ma * during the inline dir iteration.
1500c4d8b023STao Ma */
1501471fbbeaSDaniel Rosenberg dotdot_offset = ext4_dir_rec_len(1, NULL);
1502471fbbeaSDaniel Rosenberg dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1503c4d8b023STao Ma extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1504c4d8b023STao Ma extra_size = extra_offset + inline_size;
1505c4d8b023STao Ma
150665d165d9STao Ma /*
150765d165d9STao Ma * If the version has changed since the last call to
150865d165d9STao Ma * readdir(2), then we might be pointing to an invalid
150965d165d9STao Ma * dirent right now. Scan from the start of the inline
151065d165d9STao Ma * dir to make sure.
151165d165d9STao Ma */
1512c472c07bSGoffredo Baroncelli if (!inode_eq_iversion(inode, file->f_version)) {
1513c4d8b023STao Ma for (i = 0; i < extra_size && i < offset;) {
1514c4d8b023STao Ma /*
1515c4d8b023STao Ma * "." is with offset 0 and
1516c4d8b023STao Ma * ".." is dotdot_offset.
1517c4d8b023STao Ma */
151865d165d9STao Ma if (!i) {
1519c4d8b023STao Ma i = dotdot_offset;
1520c4d8b023STao Ma continue;
1521c4d8b023STao Ma } else if (i == dotdot_offset) {
1522c4d8b023STao Ma i = dotdot_size;
152365d165d9STao Ma continue;
152465d165d9STao Ma }
1525c4d8b023STao Ma /* for other entry, the real offset in
1526c4d8b023STao Ma * the buf has to be tuned accordingly.
1527c4d8b023STao Ma */
152865d165d9STao Ma de = (struct ext4_dir_entry_2 *)
1529c4d8b023STao Ma (dir_buf + i - extra_offset);
153065d165d9STao Ma /* It's too expensive to do a full
153165d165d9STao Ma * dirent test each time round this
153265d165d9STao Ma * loop, but we do have to test at
153365d165d9STao Ma * least that it is non-zero. A
153465d165d9STao Ma * failure will be detected in the
153565d165d9STao Ma * dirent test below. */
1536725bebb2SAl Viro if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1537471fbbeaSDaniel Rosenberg < ext4_dir_rec_len(1, NULL))
153865d165d9STao Ma break;
153965d165d9STao Ma i += ext4_rec_len_from_disk(de->rec_len,
1540c4d8b023STao Ma extra_size);
154165d165d9STao Ma }
154265d165d9STao Ma offset = i;
1543725bebb2SAl Viro ctx->pos = offset;
1544ee73f9a5SJeff Layton file->f_version = inode_query_iversion(inode);
154565d165d9STao Ma }
154665d165d9STao Ma
1547725bebb2SAl Viro while (ctx->pos < extra_size) {
1548725bebb2SAl Viro if (ctx->pos == 0) {
1549725bebb2SAl Viro if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1550725bebb2SAl Viro goto out;
1551725bebb2SAl Viro ctx->pos = dotdot_offset;
1552c4d8b023STao Ma continue;
1553c4d8b023STao Ma }
155465d165d9STao Ma
1555725bebb2SAl Viro if (ctx->pos == dotdot_offset) {
1556725bebb2SAl Viro if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1557725bebb2SAl Viro goto out;
1558725bebb2SAl Viro ctx->pos = dotdot_size;
155965d165d9STao Ma continue;
156065d165d9STao Ma }
156165d165d9STao Ma
1562c4d8b023STao Ma de = (struct ext4_dir_entry_2 *)
1563725bebb2SAl Viro (dir_buf + ctx->pos - extra_offset);
1564725bebb2SAl Viro if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1565725bebb2SAl Viro extra_size, ctx->pos))
1566725bebb2SAl Viro goto out;
1567725bebb2SAl Viro if (le32_to_cpu(de->inode)) {
1568725bebb2SAl Viro if (!dir_emit(ctx, de->name, de->name_len,
1569725bebb2SAl Viro le32_to_cpu(de->inode),
1570725bebb2SAl Viro get_dtype(sb, de->file_type)))
157165d165d9STao Ma goto out;
157265d165d9STao Ma }
1573725bebb2SAl Viro ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
157465d165d9STao Ma }
157565d165d9STao Ma out:
157665d165d9STao Ma kfree(dir_buf);
157765d165d9STao Ma brelse(iloc.bh);
157865d165d9STao Ma return ret;
157965d165d9STao Ma }
158065d165d9STao Ma
ext4_read_inline_link(struct inode * inode)15815a57bca9SZhang Yi void *ext4_read_inline_link(struct inode *inode)
15825a57bca9SZhang Yi {
15835a57bca9SZhang Yi struct ext4_iloc iloc;
15845a57bca9SZhang Yi int ret, inline_size;
15855a57bca9SZhang Yi void *link;
15865a57bca9SZhang Yi
15875a57bca9SZhang Yi ret = ext4_get_inode_loc(inode, &iloc);
15885a57bca9SZhang Yi if (ret)
15895a57bca9SZhang Yi return ERR_PTR(ret);
15905a57bca9SZhang Yi
15915a57bca9SZhang Yi ret = -ENOMEM;
15925a57bca9SZhang Yi inline_size = ext4_get_inline_size(inode);
15935a57bca9SZhang Yi link = kmalloc(inline_size + 1, GFP_NOFS);
15945a57bca9SZhang Yi if (!link)
15955a57bca9SZhang Yi goto out;
15965a57bca9SZhang Yi
15975a57bca9SZhang Yi ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
15985a57bca9SZhang Yi if (ret < 0) {
15995a57bca9SZhang Yi kfree(link);
16005a57bca9SZhang Yi goto out;
16015a57bca9SZhang Yi }
16025a57bca9SZhang Yi nd_terminate_link(link, inode->i_size, ret);
16035a57bca9SZhang Yi out:
16045a57bca9SZhang Yi if (ret < 0)
16055a57bca9SZhang Yi link = ERR_PTR(ret);
16065a57bca9SZhang Yi brelse(iloc.bh);
16075a57bca9SZhang Yi return link;
16085a57bca9SZhang Yi }
16095a57bca9SZhang Yi
ext4_get_first_inline_block(struct inode * inode,struct ext4_dir_entry_2 ** parent_de,int * retval)161032f7f22cSTao Ma struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
161132f7f22cSTao Ma struct ext4_dir_entry_2 **parent_de,
161232f7f22cSTao Ma int *retval)
161332f7f22cSTao Ma {
161432f7f22cSTao Ma struct ext4_iloc iloc;
161532f7f22cSTao Ma
161632f7f22cSTao Ma *retval = ext4_get_inode_loc(inode, &iloc);
161732f7f22cSTao Ma if (*retval)
161832f7f22cSTao Ma return NULL;
161932f7f22cSTao Ma
162032f7f22cSTao Ma *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
162132f7f22cSTao Ma
162232f7f22cSTao Ma return iloc.bh;
162332f7f22cSTao Ma }
162432f7f22cSTao Ma
16253c47d541STao Ma /*
16263c47d541STao Ma * Try to create the inline data for the new dir.
16273c47d541STao Ma * If it succeeds, return 0, otherwise return the error.
16283c47d541STao Ma * In case of ENOSPC, the caller should create the normal disk layout dir.
16293c47d541STao Ma */
ext4_try_create_inline_dir(handle_t * handle,struct inode * parent,struct inode * inode)16303c47d541STao Ma int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
16313c47d541STao Ma struct inode *inode)
16323c47d541STao Ma {
16333c47d541STao Ma int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
16343c47d541STao Ma struct ext4_iloc iloc;
16353c47d541STao Ma struct ext4_dir_entry_2 *de;
16363c47d541STao Ma
16373c47d541STao Ma ret = ext4_get_inode_loc(inode, &iloc);
16383c47d541STao Ma if (ret)
16393c47d541STao Ma return ret;
16403c47d541STao Ma
16413c47d541STao Ma ret = ext4_prepare_inline_data(handle, inode, inline_size);
16423c47d541STao Ma if (ret)
16433c47d541STao Ma goto out;
16443c47d541STao Ma
16453c47d541STao Ma /*
16463c47d541STao Ma * For inline dir, we only save the inode information for the ".."
16473c47d541STao Ma * and create a fake dentry to cover the left space.
16483c47d541STao Ma */
16493c47d541STao Ma de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
16503c47d541STao Ma de->inode = cpu_to_le32(parent->i_ino);
16513c47d541STao Ma de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
16523c47d541STao Ma de->inode = 0;
16533c47d541STao Ma de->rec_len = ext4_rec_len_to_disk(
16543c47d541STao Ma inline_size - EXT4_INLINE_DOTDOT_SIZE,
16553c47d541STao Ma inline_size);
16563c47d541STao Ma set_nlink(inode, 2);
16573c47d541STao Ma inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
16583c47d541STao Ma out:
16593c47d541STao Ma brelse(iloc.bh);
16603c47d541STao Ma return ret;
16613c47d541STao Ma }
16623c47d541STao Ma
ext4_find_inline_entry(struct inode * dir,struct ext4_filename * fname,struct ext4_dir_entry_2 ** res_dir,int * has_inline_data)1663e8e948e7STao Ma struct buffer_head *ext4_find_inline_entry(struct inode *dir,
16645b643f9cSTheodore Ts'o struct ext4_filename *fname,
1665e8e948e7STao Ma struct ext4_dir_entry_2 **res_dir,
1666e8e948e7STao Ma int *has_inline_data)
1667e8e948e7STao Ma {
16682a6579efSThadeu Lima de Souza Cascardo struct ext4_xattr_ibody_find is = {
16692a6579efSThadeu Lima de Souza Cascardo .s = { .not_found = -ENODATA, },
16702a6579efSThadeu Lima de Souza Cascardo };
16712a6579efSThadeu Lima de Souza Cascardo struct ext4_xattr_info i = {
16722a6579efSThadeu Lima de Souza Cascardo .name_index = EXT4_XATTR_INDEX_SYSTEM,
16732a6579efSThadeu Lima de Souza Cascardo .name = EXT4_XATTR_SYSTEM_DATA,
16742a6579efSThadeu Lima de Souza Cascardo };
1675e8e948e7STao Ma int ret;
1676e8e948e7STao Ma void *inline_start;
1677e8e948e7STao Ma int inline_size;
1678e8e948e7STao Ma
16792a6579efSThadeu Lima de Souza Cascardo ret = ext4_get_inode_loc(dir, &is.iloc);
1680dd3f90e8SThadeu Lima de Souza Cascardo if (ret)
1681dd3f90e8SThadeu Lima de Souza Cascardo return ERR_PTR(ret);
1682e8e948e7STao Ma
1683e8e948e7STao Ma down_read(&EXT4_I(dir)->xattr_sem);
16842a6579efSThadeu Lima de Souza Cascardo
16852a6579efSThadeu Lima de Souza Cascardo ret = ext4_xattr_ibody_find(dir, &i, &is);
16862a6579efSThadeu Lima de Souza Cascardo if (ret)
16872a6579efSThadeu Lima de Souza Cascardo goto out;
16882a6579efSThadeu Lima de Souza Cascardo
1689e8e948e7STao Ma if (!ext4_has_inline_data(dir)) {
1690e8e948e7STao Ma *has_inline_data = 0;
1691e8e948e7STao Ma goto out;
1692e8e948e7STao Ma }
1693e8e948e7STao Ma
16942a6579efSThadeu Lima de Souza Cascardo inline_start = (void *)ext4_raw_inode(&is.iloc)->i_block +
1695e8e948e7STao Ma EXT4_INLINE_DOTDOT_SIZE;
1696e8e948e7STao Ma inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
16972a6579efSThadeu Lima de Souza Cascardo ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
1698d6b97550SEric Biggers dir, fname, 0, res_dir);
1699e8e948e7STao Ma if (ret == 1)
1700e8e948e7STao Ma goto out_find;
1701e8e948e7STao Ma if (ret < 0)
1702e8e948e7STao Ma goto out;
1703e8e948e7STao Ma
1704e8e948e7STao Ma if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1705e8e948e7STao Ma goto out;
1706e8e948e7STao Ma
17072a6579efSThadeu Lima de Souza Cascardo inline_start = ext4_get_inline_xattr_pos(dir, &is.iloc);
1708e8e948e7STao Ma inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1709e8e948e7STao Ma
17102a6579efSThadeu Lima de Souza Cascardo ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
1711d6b97550SEric Biggers dir, fname, 0, res_dir);
1712e8e948e7STao Ma if (ret == 1)
1713e8e948e7STao Ma goto out_find;
1714e8e948e7STao Ma
1715e8e948e7STao Ma out:
17162a6579efSThadeu Lima de Souza Cascardo brelse(is.iloc.bh);
1717dd3f90e8SThadeu Lima de Souza Cascardo if (ret < 0)
17182a6579efSThadeu Lima de Souza Cascardo is.iloc.bh = ERR_PTR(ret);
1719dd3f90e8SThadeu Lima de Souza Cascardo else
17202a6579efSThadeu Lima de Souza Cascardo is.iloc.bh = NULL;
1721e8e948e7STao Ma out_find:
1722e8e948e7STao Ma up_read(&EXT4_I(dir)->xattr_sem);
17232a6579efSThadeu Lima de Souza Cascardo return is.iloc.bh;
1724e8e948e7STao Ma }
1725e8e948e7STao 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)17269f40fe54STao Ma int ext4_delete_inline_entry(handle_t *handle,
17279f40fe54STao Ma struct inode *dir,
17289f40fe54STao Ma struct ext4_dir_entry_2 *de_del,
17299f40fe54STao Ma struct buffer_head *bh,
17309f40fe54STao Ma int *has_inline_data)
17319f40fe54STao Ma {
1732c755e251STheodore Ts'o int err, inline_size, no_expand;
17339f40fe54STao Ma struct ext4_iloc iloc;
17349f40fe54STao Ma void *inline_start;
17359f40fe54STao Ma
17369f40fe54STao Ma err = ext4_get_inode_loc(dir, &iloc);
17379f40fe54STao Ma if (err)
17389f40fe54STao Ma return err;
17399f40fe54STao Ma
1740c755e251STheodore Ts'o ext4_write_lock_xattr(dir, &no_expand);
17419f40fe54STao Ma if (!ext4_has_inline_data(dir)) {
17429f40fe54STao Ma *has_inline_data = 0;
17439f40fe54STao Ma goto out;
17449f40fe54STao Ma }
17459f40fe54STao Ma
17469f40fe54STao Ma if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
17479f40fe54STao Ma EXT4_MIN_INLINE_DATA_SIZE) {
17489f40fe54STao Ma inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
17499f40fe54STao Ma EXT4_INLINE_DOTDOT_SIZE;
17509f40fe54STao Ma inline_size = EXT4_MIN_INLINE_DATA_SIZE -
17519f40fe54STao Ma EXT4_INLINE_DOTDOT_SIZE;
17529f40fe54STao Ma } else {
17539f40fe54STao Ma inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
17549f40fe54STao Ma inline_size = ext4_get_inline_size(dir) -
17559f40fe54STao Ma EXT4_MIN_INLINE_DATA_SIZE;
17569f40fe54STao Ma }
17579f40fe54STao Ma
17585d601255Sliang xie BUFFER_TRACE(bh, "get_write_access");
1759188c299eSJan Kara err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1760188c299eSJan Kara EXT4_JTR_NONE);
17619f40fe54STao Ma if (err)
17629f40fe54STao Ma goto out;
17639f40fe54STao Ma
17642fe34d29SKyoungho Koo err = ext4_generic_delete_entry(dir, de_del, bh,
17659f40fe54STao Ma inline_start, inline_size, 0);
17669f40fe54STao Ma if (err)
17679f40fe54STao Ma goto out;
17689f40fe54STao Ma
17699f40fe54STao Ma ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
17709f40fe54STao Ma out:
1771c755e251STheodore Ts'o ext4_write_unlock_xattr(dir, &no_expand);
1772b907f2d5STheodore Ts'o if (likely(err == 0))
1773b907f2d5STheodore Ts'o err = ext4_mark_inode_dirty(handle, dir);
17749f40fe54STao Ma brelse(iloc.bh);
17759f40fe54STao Ma if (err != -ENOENT)
17769f40fe54STao Ma ext4_std_error(dir->i_sb, err);
17779f40fe54STao Ma return err;
17789f40fe54STao Ma }
17799f40fe54STao Ma
178061f86638STao Ma /*
178161f86638STao Ma * Get the inline dentry at offset.
178261f86638STao Ma */
178361f86638STao 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)178461f86638STao Ma ext4_get_inline_entry(struct inode *inode,
178561f86638STao Ma struct ext4_iloc *iloc,
178661f86638STao Ma unsigned int offset,
178761f86638STao Ma void **inline_start,
178861f86638STao Ma int *inline_size)
178961f86638STao Ma {
179061f86638STao Ma void *inline_pos;
179161f86638STao Ma
179261f86638STao Ma BUG_ON(offset > ext4_get_inline_size(inode));
179361f86638STao Ma
179461f86638STao Ma if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
179561f86638STao Ma inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
179661f86638STao Ma *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
179761f86638STao Ma } else {
179861f86638STao Ma inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
179961f86638STao Ma offset -= EXT4_MIN_INLINE_DATA_SIZE;
180061f86638STao Ma *inline_size = ext4_get_inline_size(inode) -
180161f86638STao Ma EXT4_MIN_INLINE_DATA_SIZE;
180261f86638STao Ma }
180361f86638STao Ma
180461f86638STao Ma if (inline_start)
180561f86638STao Ma *inline_start = inline_pos;
180661f86638STao Ma return (struct ext4_dir_entry_2 *)(inline_pos + offset);
180761f86638STao Ma }
180861f86638STao Ma
empty_inline_dir(struct inode * dir,int * has_inline_data)1809a7550b30SJaegeuk Kim bool empty_inline_dir(struct inode *dir, int *has_inline_data)
181061f86638STao Ma {
181161f86638STao Ma int err, inline_size;
181261f86638STao Ma struct ext4_iloc iloc;
18134d982e25STheodore Ts'o size_t inline_len;
181461f86638STao Ma void *inline_pos;
181561f86638STao Ma unsigned int offset;
181661f86638STao Ma struct ext4_dir_entry_2 *de;
18177aab5c84SYe Bin bool ret = false;
181861f86638STao Ma
181961f86638STao Ma err = ext4_get_inode_loc(dir, &iloc);
182061f86638STao Ma if (err) {
182154d3adbcSTheodore Ts'o EXT4_ERROR_INODE_ERR(dir, -err,
182254d3adbcSTheodore Ts'o "error %d getting inode %lu block",
182361f86638STao Ma err, dir->i_ino);
18247aab5c84SYe Bin return false;
182561f86638STao Ma }
182661f86638STao Ma
182761f86638STao Ma down_read(&EXT4_I(dir)->xattr_sem);
182861f86638STao Ma if (!ext4_has_inline_data(dir)) {
182961f86638STao Ma *has_inline_data = 0;
18307aab5c84SYe Bin ret = true;
183161f86638STao Ma goto out;
183261f86638STao Ma }
183361f86638STao Ma
183461f86638STao Ma de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
183561f86638STao Ma if (!le32_to_cpu(de->inode)) {
183661f86638STao Ma ext4_warning(dir->i_sb,
183761f86638STao Ma "bad inline directory (dir #%lu) - no `..'",
183861f86638STao Ma dir->i_ino);
183961f86638STao Ma goto out;
184061f86638STao Ma }
184161f86638STao Ma
18424d982e25STheodore Ts'o inline_len = ext4_get_inline_size(dir);
184361f86638STao Ma offset = EXT4_INLINE_DOTDOT_SIZE;
18444d982e25STheodore Ts'o while (offset < inline_len) {
184561f86638STao Ma de = ext4_get_inline_entry(dir, &iloc, offset,
184661f86638STao Ma &inline_pos, &inline_size);
184761f86638STao Ma if (ext4_check_dir_entry(dir, NULL, de,
184861f86638STao Ma iloc.bh, inline_pos,
184961f86638STao Ma inline_size, offset)) {
185061f86638STao Ma ext4_warning(dir->i_sb,
185161f86638STao Ma "bad inline directory (dir #%lu) - "
185261f86638STao Ma "inode %u, rec_len %u, name_len %d"
18538d2ae1cbSJakub Wilk "inline size %d",
185461f86638STao Ma dir->i_ino, le32_to_cpu(de->inode),
185561f86638STao Ma le16_to_cpu(de->rec_len), de->name_len,
185661f86638STao Ma inline_size);
185761f86638STao Ma goto out;
185861f86638STao Ma }
185961f86638STao Ma if (le32_to_cpu(de->inode)) {
186061f86638STao Ma goto out;
186161f86638STao Ma }
186261f86638STao Ma offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
186361f86638STao Ma }
186461f86638STao Ma
18657aab5c84SYe Bin ret = true;
186661f86638STao Ma out:
186761f86638STao Ma up_read(&EXT4_I(dir)->xattr_sem);
186861f86638STao Ma brelse(iloc.bh);
186961f86638STao Ma return ret;
187061f86638STao Ma }
187161f86638STao Ma
ext4_destroy_inline_data(handle_t * handle,struct inode * inode)187267cf5b09STao Ma int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
187367cf5b09STao Ma {
1874c755e251STheodore Ts'o int ret, no_expand;
187567cf5b09STao Ma
1876c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
187767cf5b09STao Ma ret = ext4_destroy_inline_data_nolock(handle, inode);
1878c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
187967cf5b09STao Ma
188067cf5b09STao Ma return ret;
188167cf5b09STao Ma }
188294191985STao Ma
ext4_inline_data_iomap(struct inode * inode,struct iomap * iomap)18837046ae35SAndreas Gruenbacher int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
18847046ae35SAndreas Gruenbacher {
18857046ae35SAndreas Gruenbacher __u64 addr;
18867046ae35SAndreas Gruenbacher int error = -EAGAIN;
18877046ae35SAndreas Gruenbacher struct ext4_iloc iloc;
18887046ae35SAndreas Gruenbacher
18897046ae35SAndreas Gruenbacher down_read(&EXT4_I(inode)->xattr_sem);
18907046ae35SAndreas Gruenbacher if (!ext4_has_inline_data(inode))
18917046ae35SAndreas Gruenbacher goto out;
18927046ae35SAndreas Gruenbacher
18937046ae35SAndreas Gruenbacher error = ext4_get_inode_loc(inode, &iloc);
18947046ae35SAndreas Gruenbacher if (error)
18957046ae35SAndreas Gruenbacher goto out;
18967046ae35SAndreas Gruenbacher
18977046ae35SAndreas Gruenbacher addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
18987046ae35SAndreas Gruenbacher addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
18997046ae35SAndreas Gruenbacher addr += offsetof(struct ext4_inode, i_block);
19007046ae35SAndreas Gruenbacher
19017046ae35SAndreas Gruenbacher brelse(iloc.bh);
19027046ae35SAndreas Gruenbacher
19037046ae35SAndreas Gruenbacher iomap->addr = addr;
19047046ae35SAndreas Gruenbacher iomap->offset = 0;
19057046ae35SAndreas Gruenbacher iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
19067046ae35SAndreas Gruenbacher i_size_read(inode));
190719319b53SChristoph Hellwig iomap->type = IOMAP_INLINE;
190819319b53SChristoph Hellwig iomap->flags = 0;
19097046ae35SAndreas Gruenbacher
19107046ae35SAndreas Gruenbacher out:
19117046ae35SAndreas Gruenbacher up_read(&EXT4_I(inode)->xattr_sem);
19127046ae35SAndreas Gruenbacher return error;
19137046ae35SAndreas Gruenbacher }
19147046ae35SAndreas Gruenbacher
ext4_inline_data_truncate(struct inode * inode,int * has_inline)191501daf945STheodore Ts'o int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1916aef1c851STao Ma {
1917aef1c851STao Ma handle_t *handle;
191801daf945STheodore Ts'o int inline_size, value_len, needed_blocks, no_expand, err = 0;
1919aef1c851STao Ma size_t i_size;
1920aef1c851STao Ma void *value = NULL;
1921aef1c851STao Ma struct ext4_xattr_ibody_find is = {
1922aef1c851STao Ma .s = { .not_found = -ENODATA, },
1923aef1c851STao Ma };
1924aef1c851STao Ma struct ext4_xattr_info i = {
1925aef1c851STao Ma .name_index = EXT4_XATTR_INDEX_SYSTEM,
1926aef1c851STao Ma .name = EXT4_XATTR_SYSTEM_DATA,
1927aef1c851STao Ma };
1928aef1c851STao Ma
1929aef1c851STao Ma
1930aef1c851STao Ma needed_blocks = ext4_writepage_trans_blocks(inode);
19319924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1932aef1c851STao Ma if (IS_ERR(handle))
193301daf945STheodore Ts'o return PTR_ERR(handle);
1934aef1c851STao Ma
1935c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
1936aef1c851STao Ma if (!ext4_has_inline_data(inode)) {
19377067b261SJoseph Qi ext4_write_unlock_xattr(inode, &no_expand);
1938aef1c851STao Ma *has_inline = 0;
1939aef1c851STao Ma ext4_journal_stop(handle);
194001daf945STheodore Ts'o return 0;
1941aef1c851STao Ma }
1942aef1c851STao Ma
194301daf945STheodore Ts'o if ((err = ext4_orphan_add(handle, inode)) != 0)
1944aef1c851STao Ma goto out;
1945aef1c851STao Ma
194601daf945STheodore Ts'o if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1947aef1c851STao Ma goto out;
1948aef1c851STao Ma
1949aef1c851STao Ma down_write(&EXT4_I(inode)->i_data_sem);
1950aef1c851STao Ma i_size = inode->i_size;
1951aef1c851STao Ma inline_size = ext4_get_inline_size(inode);
1952aef1c851STao Ma EXT4_I(inode)->i_disksize = i_size;
1953aef1c851STao Ma
1954aef1c851STao Ma if (i_size < inline_size) {
19550add491dSEric Whitney /*
19560add491dSEric Whitney * if there's inline data to truncate and this file was
19570add491dSEric Whitney * converted to extents after that inline data was written,
19580add491dSEric Whitney * the extent status cache must be cleared to avoid leaving
19590add491dSEric Whitney * behind stale delayed allocated extent entries
19600add491dSEric Whitney */
1961ed5d285bSBaokun Li if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
1962ed5d285bSBaokun Li ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
19630add491dSEric Whitney
1964aef1c851STao Ma /* Clear the content in the xattr space. */
1965aef1c851STao Ma if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
196601daf945STheodore Ts'o if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1967aef1c851STao Ma goto out_error;
1968aef1c851STao Ma
1969aef1c851STao Ma BUG_ON(is.s.not_found);
1970aef1c851STao Ma
1971aef1c851STao Ma value_len = le32_to_cpu(is.s.here->e_value_size);
1972aef1c851STao Ma value = kmalloc(value_len, GFP_NOFS);
197301daf945STheodore Ts'o if (!value) {
197401daf945STheodore Ts'o err = -ENOMEM;
1975aef1c851STao Ma goto out_error;
197601daf945STheodore Ts'o }
1977aef1c851STao Ma
197801daf945STheodore Ts'o err = ext4_xattr_ibody_get(inode, i.name_index,
197901daf945STheodore Ts'o i.name, value, value_len);
198001daf945STheodore Ts'o if (err <= 0)
1981aef1c851STao Ma goto out_error;
1982aef1c851STao Ma
1983aef1c851STao Ma i.value = value;
1984aef1c851STao Ma i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1985aef1c851STao Ma i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1986310c097cSRitesh Harjani err = ext4_xattr_ibody_set(handle, inode, &i, &is);
198701daf945STheodore Ts'o if (err)
1988aef1c851STao Ma goto out_error;
1989aef1c851STao Ma }
1990aef1c851STao Ma
1991aef1c851STao Ma /* Clear the content within i_blocks. */
199209c455aaSTheodore Ts'o if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
199309c455aaSTheodore Ts'o void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
199409c455aaSTheodore Ts'o memset(p + i_size, 0,
1995aef1c851STao Ma EXT4_MIN_INLINE_DATA_SIZE - i_size);
199609c455aaSTheodore Ts'o }
1997aef1c851STao Ma
1998aef1c851STao Ma EXT4_I(inode)->i_inline_size = i_size <
1999aef1c851STao Ma EXT4_MIN_INLINE_DATA_SIZE ?
2000aef1c851STao Ma EXT4_MIN_INLINE_DATA_SIZE : i_size;
2001aef1c851STao Ma }
2002aef1c851STao Ma
2003aef1c851STao Ma out_error:
2004aef1c851STao Ma up_write(&EXT4_I(inode)->i_data_sem);
2005aef1c851STao Ma out:
2006aef1c851STao Ma brelse(is.iloc.bh);
2007c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
2008aef1c851STao Ma kfree(value);
2009aef1c851STao Ma if (inode->i_nlink)
2010aef1c851STao Ma ext4_orphan_del(handle, inode);
2011aef1c851STao Ma
201201daf945STheodore Ts'o if (err == 0) {
2013*fa42d5f1SJeff Layton inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
201401daf945STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode);
2015aef1c851STao Ma if (IS_SYNC(inode))
2016aef1c851STao Ma ext4_handle_sync(handle);
201701daf945STheodore Ts'o }
2018aef1c851STao Ma ext4_journal_stop(handle);
201901daf945STheodore Ts'o return err;
2020aef1c851STao Ma }
20210c8d414fSTao Ma
ext4_convert_inline_data(struct inode * inode)20220c8d414fSTao Ma int ext4_convert_inline_data(struct inode *inode)
20230c8d414fSTao Ma {
2024c755e251STheodore Ts'o int error, needed_blocks, no_expand;
20250c8d414fSTao Ma handle_t *handle;
20260c8d414fSTao Ma struct ext4_iloc iloc;
20270c8d414fSTao Ma
20280c8d414fSTao Ma if (!ext4_has_inline_data(inode)) {
20290c8d414fSTao Ma ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
20300c8d414fSTao Ma return 0;
2031ef09ed5dSYe Bin } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2032ef09ed5dSYe Bin /*
2033ef09ed5dSYe Bin * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2034ef09ed5dSYe Bin * cleared. This means we are in the middle of moving of
2035ef09ed5dSYe Bin * inline data to delay allocated block. Just force writeout
2036ef09ed5dSYe Bin * here to finish conversion.
2037ef09ed5dSYe Bin */
2038ef09ed5dSYe Bin error = filemap_flush(inode->i_mapping);
2039ef09ed5dSYe Bin if (error)
2040ef09ed5dSYe Bin return error;
2041ef09ed5dSYe Bin if (!ext4_has_inline_data(inode))
2042ef09ed5dSYe Bin return 0;
20430c8d414fSTao Ma }
20440c8d414fSTao Ma
20450c8d414fSTao Ma needed_blocks = ext4_writepage_trans_blocks(inode);
20460c8d414fSTao Ma
20470c8d414fSTao Ma iloc.bh = NULL;
20480c8d414fSTao Ma error = ext4_get_inode_loc(inode, &iloc);
20490c8d414fSTao Ma if (error)
20500c8d414fSTao Ma return error;
20510c8d414fSTao Ma
20529924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
20530c8d414fSTao Ma if (IS_ERR(handle)) {
20540c8d414fSTao Ma error = PTR_ERR(handle);
20550c8d414fSTao Ma goto out_free;
20560c8d414fSTao Ma }
20570c8d414fSTao Ma
2058c755e251STheodore Ts'o ext4_write_lock_xattr(inode, &no_expand);
2059c755e251STheodore Ts'o if (ext4_has_inline_data(inode))
20600c8d414fSTao Ma error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2061c755e251STheodore Ts'o ext4_write_unlock_xattr(inode, &no_expand);
20620c8d414fSTao Ma ext4_journal_stop(handle);
20630c8d414fSTao Ma out_free:
20640c8d414fSTao Ma brelse(iloc.bh);
20650c8d414fSTao Ma return error;
20660c8d414fSTao Ma }
2067