xref: /linux/fs/ext4/inline.c (revision 0a72862ce7c5a7dfe565cf348ccc76bed8005d54)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright (c) 2012 Taobao.
4  * Written by Tao Ma <boyu.mt@taobao.com>
5  */
6 
7 #include <linux/iomap.h>
8 #include <linux/fiemap.h>
9 #include <linux/namei.h>
10 #include <linux/iversion.h>
11 #include <linux/sched/mm.h>
12 #include <kunit/static_stub.h>
13 
14 #include "ext4_jbd2.h"
15 #include "ext4.h"
16 #include "xattr.h"
17 #include "truncate.h"
18 
19 #define EXT4_XATTR_SYSTEM_DATA	"data"
20 #define EXT4_MIN_INLINE_DATA_SIZE	((sizeof(__le32) * EXT4_N_BLOCKS))
21 #define EXT4_INLINE_DOTDOT_OFFSET	2
22 #define EXT4_INLINE_DOTDOT_SIZE		4
23 
24 
25 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
26 						 struct inode *inode);
27 
28 static int ext4_get_inline_size(struct inode *inode)
29 {
30 	if (EXT4_I(inode)->i_inline_off)
31 		return EXT4_I(inode)->i_inline_size;
32 
33 	return 0;
34 }
35 
36 static int get_max_inline_xattr_value_size(struct inode *inode,
37 					   struct ext4_iloc *iloc)
38 {
39 	struct ext4_xattr_ibody_header *header;
40 	struct ext4_xattr_entry *entry;
41 	struct ext4_inode *raw_inode;
42 	void *end;
43 	int free, min_offs;
44 
45 	if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
46 		return 0;
47 
48 	min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
49 			EXT4_GOOD_OLD_INODE_SIZE -
50 			EXT4_I(inode)->i_extra_isize -
51 			sizeof(struct ext4_xattr_ibody_header);
52 
53 	/*
54 	 * We need to subtract another sizeof(__u32) since an in-inode xattr
55 	 * needs an empty 4 bytes to indicate the gap between the xattr entry
56 	 * and the name/value pair.
57 	 */
58 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
59 		return EXT4_XATTR_SIZE(min_offs -
60 			EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
61 			EXT4_XATTR_ROUND - sizeof(__u32));
62 
63 	raw_inode = ext4_raw_inode(iloc);
64 	header = IHDR(inode, raw_inode);
65 	entry = IFIRST(header);
66 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
67 
68 	/* Compute min_offs. */
69 	while (!IS_LAST_ENTRY(entry)) {
70 		void *next = EXT4_XATTR_NEXT(entry);
71 
72 		if (next >= end) {
73 			EXT4_ERROR_INODE(inode,
74 					 "corrupt xattr in inline inode");
75 			return 0;
76 		}
77 		if (!entry->e_value_inum && entry->e_value_size) {
78 			size_t offs = le16_to_cpu(entry->e_value_offs);
79 			if (offs < min_offs)
80 				min_offs = offs;
81 		}
82 		entry = next;
83 	}
84 	free = min_offs -
85 		((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
86 
87 	if (EXT4_I(inode)->i_inline_off) {
88 		entry = (struct ext4_xattr_entry *)
89 			((void *)raw_inode + EXT4_I(inode)->i_inline_off);
90 
91 		free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
92 		goto out;
93 	}
94 
95 	free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
96 
97 	if (free > EXT4_XATTR_ROUND)
98 		free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
99 	else
100 		free = 0;
101 
102 out:
103 	return free;
104 }
105 
106 /*
107  * Get the maximum size we now can store in an inode.
108  * If we can't find the space for a xattr entry, don't use the space
109  * of the extents since we have no space to indicate the inline data.
110  */
111 int ext4_get_max_inline_size(struct inode *inode)
112 {
113 	int error, max_inline_size;
114 	struct ext4_iloc iloc;
115 
116 	if (EXT4_I(inode)->i_extra_isize == 0)
117 		return 0;
118 
119 	error = ext4_get_inode_loc(inode, &iloc);
120 	if (error) {
121 		ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
122 				     "can't get inode location %llu",
123 				     inode->i_ino);
124 		return 0;
125 	}
126 
127 	down_read(&EXT4_I(inode)->xattr_sem);
128 	max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
129 	up_read(&EXT4_I(inode)->xattr_sem);
130 
131 	brelse(iloc.bh);
132 
133 	if (!max_inline_size)
134 		return 0;
135 
136 	return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
137 }
138 
139 /*
140  * this function does not take xattr_sem, which is OK because it is
141  * currently only used in a code path coming form ext4_iget, before
142  * the new inode has been unlocked
143  */
144 int ext4_find_inline_data_nolock(struct inode *inode)
145 {
146 	struct ext4_xattr_ibody_find is = {
147 		.s = { .not_found = -ENODATA, },
148 	};
149 	struct ext4_xattr_info i = {
150 		.name_index = EXT4_XATTR_INDEX_SYSTEM,
151 		.name = EXT4_XATTR_SYSTEM_DATA,
152 	};
153 	int error;
154 
155 	if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
156 		return 0;
157 
158 	error = ext4_get_inode_loc(inode, &is.iloc);
159 	if (error)
160 		return error;
161 
162 	error = ext4_xattr_ibody_find(inode, &i, &is);
163 	if (error)
164 		goto out;
165 
166 	if (!is.s.not_found) {
167 		if (is.s.here->e_value_inum) {
168 			EXT4_ERROR_INODE(inode, "inline data xattr refers "
169 					 "to an external xattr inode");
170 			error = -EFSCORRUPTED;
171 			goto out;
172 		}
173 		EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
174 					(void *)ext4_raw_inode(&is.iloc));
175 		EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
176 				le32_to_cpu(is.s.here->e_value_size);
177 	}
178 out:
179 	brelse(is.iloc.bh);
180 	return error;
181 }
182 
183 static int ext4_read_inline_data(struct inode *inode, void *buffer,
184 				 unsigned int len,
185 				 struct ext4_iloc *iloc)
186 {
187 	struct ext4_xattr_entry *entry;
188 	struct ext4_xattr_ibody_header *header;
189 	int cp_len = 0;
190 	struct ext4_inode *raw_inode;
191 	void *end, *p;
192 	u16 offset;
193 
194 	if (!len)
195 		return 0;
196 
197 	BUG_ON(len > EXT4_I(inode)->i_inline_size);
198 
199 	cp_len = min_t(unsigned int, len, EXT4_MIN_INLINE_DATA_SIZE);
200 
201 	raw_inode = ext4_raw_inode(iloc);
202 	memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
203 
204 	len -= cp_len;
205 	buffer += cp_len;
206 
207 	if (!len)
208 		goto out;
209 
210 	header = IHDR(inode, raw_inode);
211 	end = ITAIL(inode, raw_inode);
212 	entry = (struct ext4_xattr_entry *)((void *)raw_inode +
213 					    EXT4_I(inode)->i_inline_off);
214 	len = min_t(unsigned int, len,
215 		    (unsigned int)le32_to_cpu(entry->e_value_size));
216 
217 	offset = le16_to_cpu(entry->e_value_offs);
218 	p = (void *)IFIRST(header) + offset;
219 
220 	if (unlikely(p + len > end)) {
221 		EXT4_ERROR_INODE(inode, "corrupt inline xattr entry");
222 		return -EFSCORRUPTED;
223 	}
224 
225 	memcpy(buffer, p, len);
226 	cp_len += len;
227 
228 out:
229 	return cp_len;
230 }
231 
232 /*
233  * write the buffer to the inline inode.
234  * If 'create' is set, we don't need to do the extra copy in the xattr
235  * value since it is already handled by ext4_xattr_ibody_set.
236  * That saves us one memcpy.
237  */
238 static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
239 				   void *buffer, loff_t pos, unsigned int len)
240 {
241 	struct ext4_xattr_entry *entry;
242 	struct ext4_xattr_ibody_header *header;
243 	struct ext4_inode *raw_inode;
244 	int cp_len = 0;
245 
246 	if (unlikely(ext4_emergency_state(inode->i_sb)))
247 		return;
248 
249 	BUG_ON(!EXT4_I(inode)->i_inline_off);
250 	BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
251 
252 	raw_inode = ext4_raw_inode(iloc);
253 	buffer += pos;
254 
255 	if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
256 		cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
257 			 EXT4_MIN_INLINE_DATA_SIZE - pos : len;
258 		memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
259 
260 		len -= cp_len;
261 		buffer += cp_len;
262 		pos += cp_len;
263 	}
264 
265 	if (!len)
266 		return;
267 
268 	pos -= EXT4_MIN_INLINE_DATA_SIZE;
269 	header = IHDR(inode, raw_inode);
270 	entry = (struct ext4_xattr_entry *)((void *)raw_inode +
271 					    EXT4_I(inode)->i_inline_off);
272 
273 	memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
274 	       buffer, len);
275 }
276 
277 static int ext4_create_inline_data(handle_t *handle,
278 				   struct inode *inode, unsigned len)
279 {
280 	int error;
281 	void *value = NULL;
282 	struct ext4_xattr_ibody_find is = {
283 		.s = { .not_found = -ENODATA, },
284 	};
285 	struct ext4_xattr_info i = {
286 		.name_index = EXT4_XATTR_INDEX_SYSTEM,
287 		.name = EXT4_XATTR_SYSTEM_DATA,
288 	};
289 
290 	error = ext4_get_inode_loc(inode, &is.iloc);
291 	if (error)
292 		return error;
293 
294 	BUFFER_TRACE(is.iloc.bh, "get_write_access");
295 	error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
296 					      EXT4_JTR_NONE);
297 	if (error)
298 		goto out;
299 
300 	if (len > EXT4_MIN_INLINE_DATA_SIZE) {
301 		value = EXT4_ZERO_XATTR_VALUE;
302 		len -= EXT4_MIN_INLINE_DATA_SIZE;
303 	} else {
304 		value = "";
305 		len = 0;
306 	}
307 
308 	/* Insert the xttr entry. */
309 	i.value = value;
310 	i.value_len = len;
311 
312 	error = ext4_xattr_ibody_find(inode, &i, &is);
313 	if (error)
314 		goto out;
315 
316 	if (!is.s.not_found) {
317 		EXT4_ERROR_INODE(inode, "unexpected inline data xattr");
318 		error = -EFSCORRUPTED;
319 		goto out;
320 	}
321 
322 	error = ext4_xattr_ibody_set(handle, inode, &i, &is);
323 	if (error) {
324 		if (error == -ENOSPC)
325 			ext4_clear_inode_state(inode,
326 					       EXT4_STATE_MAY_INLINE_DATA);
327 		goto out;
328 	}
329 
330 	memset((void *)ext4_raw_inode(&is.iloc)->i_block,
331 		0, EXT4_MIN_INLINE_DATA_SIZE);
332 
333 	EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
334 				      (void *)ext4_raw_inode(&is.iloc));
335 	EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
336 	ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
337 	ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
338 	get_bh(is.iloc.bh);
339 	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
340 
341 out:
342 	brelse(is.iloc.bh);
343 	return error;
344 }
345 
346 static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
347 				   unsigned int len)
348 {
349 	int error;
350 	void *value = NULL;
351 	struct ext4_xattr_ibody_find is = {
352 		.s = { .not_found = -ENODATA, },
353 	};
354 	struct ext4_xattr_info i = {
355 		.name_index = EXT4_XATTR_INDEX_SYSTEM,
356 		.name = EXT4_XATTR_SYSTEM_DATA,
357 	};
358 
359 	/* If the old space is ok, write the data directly. */
360 	if (len <= EXT4_I(inode)->i_inline_size)
361 		return 0;
362 
363 	error = ext4_get_inode_loc(inode, &is.iloc);
364 	if (error)
365 		return error;
366 
367 	error = ext4_xattr_ibody_find(inode, &i, &is);
368 	if (error)
369 		goto out;
370 
371 	if (is.s.not_found) {
372 		EXT4_ERROR_INODE(inode, "missing inline data xattr");
373 		error = -EFSCORRUPTED;
374 		goto out;
375 	}
376 
377 	len -= EXT4_MIN_INLINE_DATA_SIZE;
378 	value = kzalloc(len, GFP_NOFS);
379 	if (!value) {
380 		error = -ENOMEM;
381 		goto out;
382 	}
383 
384 	error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
385 				     value, len);
386 	if (error < 0)
387 		goto out;
388 
389 	BUFFER_TRACE(is.iloc.bh, "get_write_access");
390 	error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
391 					      EXT4_JTR_NONE);
392 	if (error)
393 		goto out;
394 
395 	/* Update the xattr entry. */
396 	i.value = value;
397 	i.value_len = len;
398 
399 	error = ext4_xattr_ibody_set(handle, inode, &i, &is);
400 	if (error)
401 		goto out;
402 
403 	EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
404 				      (void *)ext4_raw_inode(&is.iloc));
405 	EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
406 				le32_to_cpu(is.s.here->e_value_size);
407 	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
408 	get_bh(is.iloc.bh);
409 	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
410 
411 out:
412 	kfree(value);
413 	brelse(is.iloc.bh);
414 	return error;
415 }
416 
417 static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
418 				    loff_t len)
419 {
420 	int ret, size, no_expand;
421 	struct ext4_inode_info *ei = EXT4_I(inode);
422 
423 	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
424 		return -ENOSPC;
425 
426 	size = ext4_get_max_inline_size(inode);
427 	if (size < len)
428 		return -ENOSPC;
429 
430 	ext4_write_lock_xattr(inode, &no_expand);
431 	/*
432 	 * ei->i_inline_size may have changed since the initial check
433 	 * if other xattrs were added. Recalculate to ensure
434 	 * ext4_update_inline_data() validates against current capacity.
435 	 */
436 	(void) ext4_find_inline_data_nolock(inode);
437 	if (ei->i_inline_off)
438 		ret = ext4_update_inline_data(handle, inode, len);
439 	else
440 		ret = ext4_create_inline_data(handle, inode, len);
441 
442 	ext4_write_unlock_xattr(inode, &no_expand);
443 	return ret;
444 }
445 
446 static int ext4_destroy_inline_data_nolock(handle_t *handle,
447 					   struct inode *inode)
448 {
449 	struct ext4_inode_info *ei = EXT4_I(inode);
450 	struct ext4_xattr_ibody_find is = {
451 		.s = { .not_found = 0, },
452 	};
453 	struct ext4_xattr_info i = {
454 		.name_index = EXT4_XATTR_INDEX_SYSTEM,
455 		.name = EXT4_XATTR_SYSTEM_DATA,
456 		.value = NULL,
457 		.value_len = 0,
458 	};
459 	int error;
460 
461 	if (!ei->i_inline_off)
462 		return 0;
463 
464 	down_write(&ei->i_data_sem);
465 
466 	error = ext4_get_inode_loc(inode, &is.iloc);
467 	if (error) {
468 		up_write(&ei->i_data_sem);
469 		return error;
470 	}
471 
472 	error = ext4_xattr_ibody_find(inode, &i, &is);
473 	if (error)
474 		goto out;
475 	if (is.s.not_found) {
476 		EXT4_ERROR_INODE(inode,
477 				 "corrupted inline data without system.data xattr");
478 		error = -EFSCORRUPTED;
479 		goto out;
480 	}
481 
482 	BUFFER_TRACE(is.iloc.bh, "get_write_access");
483 	error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
484 					      EXT4_JTR_NONE);
485 	if (error)
486 		goto out;
487 
488 	error = ext4_xattr_ibody_set(handle, inode, &i, &is);
489 	if (error)
490 		goto out;
491 
492 	memset((void *)ext4_raw_inode(&is.iloc)->i_block,
493 		0, EXT4_MIN_INLINE_DATA_SIZE);
494 	memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
495 
496 	if (ext4_has_feature_extents(inode->i_sb)) {
497 		if (S_ISDIR(inode->i_mode) ||
498 		    S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
499 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
500 			ext4_ext_tree_init(handle, inode);
501 		}
502 	}
503 	ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
504 
505 	get_bh(is.iloc.bh);
506 	error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
507 
508 	EXT4_I(inode)->i_inline_off = 0;
509 	EXT4_I(inode)->i_inline_size = 0;
510 	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
511 out:
512 	brelse(is.iloc.bh);
513 	if (error == -ENODATA)
514 		error = 0;
515 	up_write(&ei->i_data_sem);
516 	return error;
517 }
518 
519 static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
520 {
521 	void *kaddr;
522 	int ret = 0;
523 	size_t len;
524 	struct ext4_iloc iloc;
525 
526 	BUG_ON(!folio_test_locked(folio));
527 	BUG_ON(!ext4_has_inline_data(inode));
528 	BUG_ON(folio->index);
529 
530 	if (!EXT4_I(inode)->i_inline_off) {
531 		ext4_warning(inode->i_sb, "inode %llu doesn't have inline data.",
532 			     inode->i_ino);
533 		goto out;
534 	}
535 
536 	ret = ext4_get_inode_loc(inode, &iloc);
537 	if (ret)
538 		goto out;
539 
540 	len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
541 
542 	if (len > PAGE_SIZE) {
543 		ext4_error_inode(inode, __func__, __LINE__, 0,
544 				 "inline size %zu exceeds PAGE_SIZE", len);
545 		ret = -EFSCORRUPTED;
546 		brelse(iloc.bh);
547 		goto out;
548 	}
549 
550 	kaddr = kmap_local_folio(folio, 0);
551 	ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
552 	kaddr = folio_zero_tail(folio, len, kaddr + len);
553 	kunmap_local(kaddr);
554 	folio_mark_uptodate(folio);
555 	brelse(iloc.bh);
556 
557 out:
558 	return ret;
559 }
560 
561 int ext4_readpage_inline(struct inode *inode, struct folio *folio)
562 {
563 	int ret = 0;
564 
565 	down_read(&EXT4_I(inode)->xattr_sem);
566 	if (!ext4_has_inline_data(inode)) {
567 		up_read(&EXT4_I(inode)->xattr_sem);
568 		return -EAGAIN;
569 	}
570 
571 	/*
572 	 * Current inline data can only exist in the 1st page,
573 	 * So for all the other pages, just set them uptodate.
574 	 */
575 	if (!folio->index)
576 		ret = ext4_read_inline_folio(inode, folio);
577 	else if (!folio_test_uptodate(folio)) {
578 		folio_zero_segment(folio, 0, folio_size(folio));
579 		folio_mark_uptodate(folio);
580 	}
581 
582 	up_read(&EXT4_I(inode)->xattr_sem);
583 
584 	folio_unlock(folio);
585 	return ret >= 0 ? 0 : ret;
586 }
587 
588 static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
589 					      struct inode *inode)
590 {
591 	int ret, needed_blocks, no_expand;
592 	handle_t *handle = NULL;
593 	int retries = 0, sem_held = 0;
594 	struct folio *folio = NULL;
595 	unsigned from, to;
596 	struct ext4_iloc iloc;
597 
598 	if (!ext4_has_inline_data(inode)) {
599 		/*
600 		 * clear the flag so that no new write
601 		 * will trap here again.
602 		 */
603 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
604 		return 0;
605 	}
606 
607 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
608 
609 	ret = ext4_get_inode_loc(inode, &iloc);
610 	if (ret)
611 		return ret;
612 
613 retry:
614 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
615 	if (IS_ERR(handle)) {
616 		ret = PTR_ERR(handle);
617 		handle = NULL;
618 		goto out;
619 	}
620 
621 	/* We cannot recurse into the filesystem as the transaction is already
622 	 * started */
623 	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
624 			mapping_gfp_mask(mapping));
625 	if (IS_ERR(folio)) {
626 		ret = PTR_ERR(folio);
627 		goto out_nofolio;
628 	}
629 
630 	ext4_write_lock_xattr(inode, &no_expand);
631 	sem_held = 1;
632 	/* If some one has already done this for us, just exit. */
633 	if (!ext4_has_inline_data(inode)) {
634 		ret = 0;
635 		goto out;
636 	}
637 
638 	from = 0;
639 	to = ext4_get_inline_size(inode);
640 	if (!folio_test_uptodate(folio)) {
641 		ret = ext4_read_inline_folio(inode, folio);
642 		if (ret < 0)
643 			goto out;
644 	}
645 
646 	ext4_fc_track_inode(handle, inode);
647 	ret = ext4_destroy_inline_data_nolock(handle, inode);
648 	if (ret)
649 		goto out;
650 
651 	if (ext4_should_dioread_nolock(inode)) {
652 		ret = ext4_block_write_begin(handle, folio, from, to,
653 					     ext4_get_block_unwritten);
654 	} else
655 		ret = ext4_block_write_begin(handle, folio, from, to,
656 					     ext4_get_block);
657 	clear_buffer_new(folio_buffers(folio));
658 
659 	if (!ret && ext4_should_journal_data(inode)) {
660 		ret = ext4_walk_page_buffers(handle, inode,
661 					     folio_buffers(folio), from, to,
662 					     NULL, do_journal_get_write_access);
663 	}
664 
665 	if (ret) {
666 		folio_unlock(folio);
667 		folio_put(folio);
668 		folio = NULL;
669 		ext4_orphan_add(handle, inode);
670 		ext4_write_unlock_xattr(inode, &no_expand);
671 		sem_held = 0;
672 		ext4_journal_stop(handle);
673 		handle = NULL;
674 		ext4_truncate_failed_write(inode);
675 		/*
676 		 * If truncate failed early the inode might
677 		 * still be on the orphan list; we need to
678 		 * make sure the inode is removed from the
679 		 * orphan list in that case.
680 		 */
681 		if (inode->i_nlink)
682 			ext4_orphan_del(NULL, inode);
683 	}
684 
685 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
686 		goto retry;
687 
688 	if (folio)
689 		block_commit_write(folio, from, to);
690 out:
691 	if (folio) {
692 		folio_unlock(folio);
693 		folio_put(folio);
694 	}
695 out_nofolio:
696 	if (sem_held)
697 		ext4_write_unlock_xattr(inode, &no_expand);
698 	if (handle)
699 		ext4_journal_stop(handle);
700 	brelse(iloc.bh);
701 	return ret;
702 }
703 
704 /*
705  * Prepare the write for the inline data.
706  * If the data can be written into the inode, we just read
707  * the page and make it uptodate, and start the journal.
708  * Otherwise read the page, makes it dirty so that it can be
709  * handle in writepages(the i_disksize update is left to the
710  * normal ext4_da_write_end).
711  */
712 int ext4_generic_write_inline_data(struct address_space *mapping,
713 					  struct inode *inode,
714 					  loff_t pos, unsigned len,
715 					  struct folio **foliop,
716 					  bool da)
717 {
718 	int ret;
719 	handle_t *handle;
720 	struct folio *folio;
721 	struct ext4_iloc iloc;
722 	int retries = 0;
723 
724 	ret = ext4_get_inode_loc(inode, &iloc);
725 	if (ret)
726 		return ret;
727 
728 retry_journal:
729 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
730 	if (IS_ERR(handle)) {
731 		ret = PTR_ERR(handle);
732 		goto out_release_bh;
733 	}
734 
735 	ret = ext4_prepare_inline_data(handle, inode, pos + len);
736 	if (ret && ret != -ENOSPC)
737 		goto out_stop_journal;
738 
739 	if (ret == -ENOSPC) {
740 		ext4_journal_stop(handle);
741 		if (!da) {
742 			brelse(iloc.bh);
743 			/* Retry inside */
744 			return ext4_convert_inline_data_to_extent(mapping, inode);
745 		}
746 
747 		ret = ext4_da_convert_inline_data_to_extent(mapping, inode);
748 		if (ret == -ENOSPC &&
749 		    ext4_should_retry_alloc(inode->i_sb, &retries))
750 			goto retry_journal;
751 		goto out_release_bh;
752 	}
753 
754 	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
755 					mapping_gfp_mask(mapping));
756 	if (IS_ERR(folio)) {
757 		ret = PTR_ERR(folio);
758 		goto out_stop_journal;
759 	}
760 
761 	down_read(&EXT4_I(inode)->xattr_sem);
762 	/* Someone else had converted it to extent */
763 	if (!ext4_has_inline_data(inode)) {
764 		ret = 0;
765 		goto out_release_folio;
766 	}
767 
768 	if (!folio_test_uptodate(folio)) {
769 		ret = ext4_read_inline_folio(inode, folio);
770 		if (ret < 0)
771 			goto out_release_folio;
772 	}
773 
774 	ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh, EXT4_JTR_NONE);
775 	if (ret)
776 		goto out_release_folio;
777 	*foliop = folio;
778 	up_read(&EXT4_I(inode)->xattr_sem);
779 	brelse(iloc.bh);
780 	return 1;
781 
782 out_release_folio:
783 	up_read(&EXT4_I(inode)->xattr_sem);
784 	folio_unlock(folio);
785 	folio_put(folio);
786 out_stop_journal:
787 	ext4_journal_stop(handle);
788 out_release_bh:
789 	brelse(iloc.bh);
790 	return ret;
791 }
792 
793 /*
794  * Try to write data in the inode.
795  * If the inode has inline data, check whether the new write can be
796  * in the inode also. If not, create the page the handle, move the data
797  * to the page make it update and let the later codes create extent for it.
798  */
799 int ext4_try_to_write_inline_data(struct address_space *mapping,
800 				  struct inode *inode,
801 				  loff_t pos, unsigned len,
802 				  struct folio **foliop)
803 {
804 	if (pos + len > ext4_get_max_inline_size(inode))
805 		return ext4_convert_inline_data_to_extent(mapping, inode);
806 	return ext4_generic_write_inline_data(mapping, inode, pos, len,
807 					      foliop, false);
808 }
809 
810 int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
811 			       unsigned copied, struct folio *folio)
812 {
813 	handle_t *handle = ext4_journal_current_handle();
814 	int no_expand;
815 	void *kaddr;
816 	struct ext4_iloc iloc;
817 	int ret = 0, ret2;
818 
819 	if (unlikely(copied < len) && !folio_test_uptodate(folio))
820 		copied = 0;
821 
822 	if (likely(copied)) {
823 		ret = ext4_get_inode_loc(inode, &iloc);
824 		if (ret) {
825 			folio_unlock(folio);
826 			folio_put(folio);
827 			ext4_std_error(inode->i_sb, ret);
828 			goto out;
829 		}
830 		ext4_write_lock_xattr(inode, &no_expand);
831 		/*
832 		 * We could have raced with ext4_page_mkwrite() converting
833 		 * the inode and clearing the inline data flag, so we just
834 		 * release resources and retry the whole write.
835 		 */
836 		if (unlikely(!ext4_has_inline_data(inode))) {
837 			ext4_write_unlock_xattr(inode, &no_expand);
838 			brelse(iloc.bh);
839 			folio_unlock(folio);
840 			folio_put(folio);
841 			ext4_journal_stop(handle);
842 			return 0;
843 		}
844 
845 		/*
846 		 * ei->i_inline_off may have changed since
847 		 * ext4_write_begin() called
848 		 * ext4_try_to_write_inline_data()
849 		 */
850 		(void) ext4_find_inline_data_nolock(inode);
851 
852 		kaddr = kmap_local_folio(folio, 0);
853 		ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
854 		kunmap_local(kaddr);
855 		folio_mark_uptodate(folio);
856 		/* clear dirty flag so that writepages wouldn't work for us. */
857 		folio_clear_dirty(folio);
858 
859 		ext4_write_unlock_xattr(inode, &no_expand);
860 		brelse(iloc.bh);
861 
862 		/*
863 		 * It's important to update i_size while still holding folio
864 		 * lock: page writeout could otherwise come in and zero
865 		 * beyond i_size.
866 		 */
867 		ext4_update_inode_size(inode, pos + copied);
868 	}
869 	folio_unlock(folio);
870 	folio_put(folio);
871 
872 	/*
873 	 * Don't mark the inode dirty under folio lock. First, it unnecessarily
874 	 * makes the holding time of folio lock longer. Second, it forces lock
875 	 * ordering of folio lock and transaction start for journaling
876 	 * filesystems.
877 	 */
878 	if (likely(copied))
879 		mark_inode_dirty(inode);
880 out:
881 	/*
882 	 * If we didn't copy as much data as expected, we need to trim back
883 	 * size of xattr containing inline data.
884 	 */
885 	if (pos + len > inode->i_size && ext4_can_truncate(inode))
886 		ext4_orphan_add(handle, inode);
887 
888 	ret2 = ext4_journal_stop(handle);
889 	if (!ret)
890 		ret = ret2;
891 	if (pos + len > inode->i_size) {
892 		ext4_truncate_failed_write(inode);
893 		/*
894 		 * If truncate failed early the inode might still be
895 		 * on the orphan list; we need to make sure the inode
896 		 * is removed from the orphan list in that case.
897 		 */
898 		if (inode->i_nlink)
899 			ext4_orphan_del(NULL, inode);
900 	}
901 	return ret ? ret : copied;
902 }
903 
904 /*
905  * Try to make the page cache and handle ready for the inline data case.
906  * We can call this function in 2 cases:
907  * 1. The inode is created and the first write exceeds inline size. We can
908  *    clear the inode state safely.
909  * 2. The inode has inline data, then we need to read the data, make it
910  *    update and dirty so that ext4_da_writepages can handle it. We don't
911  *    need to start the journal since the file's metadata isn't changed now.
912  */
913 static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
914 						 struct inode *inode)
915 {
916 	int ret = 0, inline_size;
917 	struct folio *folio;
918 
919 	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN,
920 					mapping_gfp_mask(mapping));
921 	if (IS_ERR(folio))
922 		return PTR_ERR(folio);
923 
924 	down_read(&EXT4_I(inode)->xattr_sem);
925 	if (!ext4_has_inline_data(inode)) {
926 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
927 		goto out;
928 	}
929 
930 	inline_size = ext4_get_inline_size(inode);
931 
932 	if (!folio_test_uptodate(folio)) {
933 		ret = ext4_read_inline_folio(inode, folio);
934 		if (ret < 0)
935 			goto out;
936 	}
937 
938 	ret = ext4_block_write_begin(NULL, folio, 0, inline_size,
939 				     ext4_da_get_block_prep);
940 	if (ret) {
941 		up_read(&EXT4_I(inode)->xattr_sem);
942 		folio_unlock(folio);
943 		folio_put(folio);
944 		ext4_truncate_failed_write(inode);
945 		return ret;
946 	}
947 
948 	clear_buffer_new(folio_buffers(folio));
949 	folio_mark_dirty(folio);
950 	folio_mark_uptodate(folio);
951 	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
952 
953 out:
954 	up_read(&EXT4_I(inode)->xattr_sem);
955 	if (folio) {
956 		folio_unlock(folio);
957 		folio_put(folio);
958 	}
959 	return ret;
960 }
961 
962 #ifdef INLINE_DIR_DEBUG
963 void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
964 			  void *inline_start, int inline_size)
965 {
966 	int offset;
967 	unsigned short de_len;
968 	struct ext4_dir_entry_2 *de = inline_start;
969 	void *dlimit = inline_start + inline_size;
970 
971 	trace_printk("inode %llu\n", dir->i_ino);
972 	offset = 0;
973 	while ((void *)de < dlimit) {
974 		de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
975 		trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
976 			     offset, de_len, de->name_len, de->name,
977 			     de->name_len, le32_to_cpu(de->inode));
978 		if (ext4_check_dir_entry(dir, NULL, de, bh,
979 					 inline_start, inline_size, offset))
980 			BUG();
981 
982 		offset += de_len;
983 		de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
984 	}
985 }
986 #else
987 #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
988 #endif
989 
990 /*
991  * Add a new entry into a inline dir.
992  * It will return -ENOSPC if no space is available, and -EIO
993  * and -EEXIST if directory entry already exists.
994  */
995 static int ext4_add_dirent_to_inline(handle_t *handle,
996 				     struct ext4_filename *fname,
997 				     struct inode *dir,
998 				     struct inode *inode,
999 				     struct ext4_iloc *iloc,
1000 				     void *inline_start, int inline_size)
1001 {
1002 	int		err;
1003 	struct ext4_dir_entry_2 *de;
1004 
1005 	err = ext4_find_dest_de(dir, iloc->bh, inline_start,
1006 				inline_size, fname, &de);
1007 	if (err)
1008 		return err;
1009 
1010 	BUFFER_TRACE(iloc->bh, "get_write_access");
1011 	err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
1012 					    EXT4_JTR_NONE);
1013 	if (err)
1014 		return err;
1015 	ext4_insert_dentry(dir, inode, de, inline_size, fname);
1016 
1017 	ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
1018 
1019 	/*
1020 	 * XXX shouldn't update any times until successful
1021 	 * completion of syscall, but too many callers depend
1022 	 * on this.
1023 	 *
1024 	 * XXX similarly, too many callers depend on
1025 	 * ext4_new_inode() setting the times, but error
1026 	 * recovery deletes the inode, so the worst that can
1027 	 * happen is that the times are slightly out of date
1028 	 * and/or different from the directory change time.
1029 	 */
1030 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1031 	ext4_update_dx_flag(dir);
1032 	inode_inc_iversion(dir);
1033 	return 1;
1034 }
1035 
1036 static void *ext4_get_inline_xattr_pos(struct inode *inode,
1037 				       struct ext4_iloc *iloc)
1038 {
1039 	struct ext4_xattr_entry *entry;
1040 	struct ext4_xattr_ibody_header *header;
1041 
1042 	BUG_ON(!EXT4_I(inode)->i_inline_off);
1043 
1044 	header = IHDR(inode, ext4_raw_inode(iloc));
1045 	entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
1046 					    EXT4_I(inode)->i_inline_off);
1047 
1048 	return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
1049 }
1050 
1051 /* Set the final de to cover the whole block. */
1052 void ext4_update_final_de(void *de_buf, int old_size, int new_size)
1053 {
1054 	struct ext4_dir_entry_2 *de, *prev_de;
1055 	void *limit;
1056 	int de_len;
1057 
1058 	de = de_buf;
1059 	if (old_size) {
1060 		limit = de_buf + old_size;
1061 		do {
1062 			prev_de = de;
1063 			de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
1064 			de_buf += de_len;
1065 			de = de_buf;
1066 		} while (de_buf < limit);
1067 
1068 		prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
1069 							old_size, new_size);
1070 	} else {
1071 		/* this is just created, so create an empty entry. */
1072 		de->inode = 0;
1073 		de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
1074 	}
1075 }
1076 
1077 static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
1078 				  struct ext4_iloc *iloc)
1079 {
1080 	int ret;
1081 	int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
1082 	int new_size = get_max_inline_xattr_value_size(dir, iloc);
1083 
1084 	if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
1085 		return -ENOSPC;
1086 
1087 	ret = ext4_update_inline_data(handle, dir,
1088 				      new_size + EXT4_MIN_INLINE_DATA_SIZE);
1089 	if (ret)
1090 		return ret;
1091 
1092 	ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
1093 			     EXT4_I(dir)->i_inline_size -
1094 						EXT4_MIN_INLINE_DATA_SIZE);
1095 	dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
1096 	return 0;
1097 }
1098 
1099 static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
1100 				     struct ext4_iloc *iloc,
1101 				     void *buf, int inline_size)
1102 {
1103 	int ret;
1104 
1105 	ret = ext4_create_inline_data(handle, inode, inline_size);
1106 	if (ret) {
1107 		ext4_msg(inode->i_sb, KERN_EMERG,
1108 			"error restoring inline_data for inode -- potential data loss! (inode %llu, error %d)",
1109 			inode->i_ino, ret);
1110 		return;
1111 	}
1112 	ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
1113 	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1114 }
1115 
1116 struct buffer_head *ext4_inline_getblk(struct super_block *sb,
1117 				       ext4_fsblk_t block)
1118 {
1119 	KUNIT_STATIC_STUB_REDIRECT(ext4_inline_getblk, sb, block);
1120 	return sb_getblk(sb, block);
1121 }
1122 
1123 static int ext4_convert_inline_data_nolock(handle_t *handle,
1124 					   struct inode *inode,
1125 					   struct ext4_iloc *iloc)
1126 {
1127 	int error;
1128 	void *buf = NULL;
1129 	struct buffer_head *data_bh = NULL;
1130 	struct ext4_map_blocks map;
1131 	int inline_size;
1132 
1133 	inline_size = ext4_get_inline_size(inode);
1134 	buf = kmalloc(inline_size, GFP_NOFS);
1135 	if (!buf) {
1136 		error = -ENOMEM;
1137 		goto out;
1138 	}
1139 
1140 	error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1141 	if (error < 0)
1142 		goto out;
1143 
1144 	/*
1145 	 * Make sure the inline directory entries pass checks before we try to
1146 	 * convert them, so that we avoid touching stuff that needs fsck.
1147 	 */
1148 	if (S_ISDIR(inode->i_mode)) {
1149 		error = ext4_check_all_de(inode, iloc->bh,
1150 					buf + EXT4_INLINE_DOTDOT_SIZE,
1151 					inline_size - EXT4_INLINE_DOTDOT_SIZE);
1152 		if (error)
1153 			goto out;
1154 	}
1155 
1156 	error = ext4_destroy_inline_data_nolock(handle, inode);
1157 	if (error)
1158 		goto out;
1159 
1160 	map.m_lblk = 0;
1161 	map.m_len = 1;
1162 	map.m_flags = 0;
1163 	error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1164 	if (error < 0)
1165 		goto out_restore;
1166 	if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1167 		error = -EIO;
1168 		goto out_restore;
1169 	}
1170 
1171 	data_bh = ext4_inline_getblk(inode->i_sb, map.m_pblk);
1172 	if (!data_bh) {
1173 		error = -ENOMEM;
1174 		goto out_restore;
1175 	}
1176 
1177 	lock_buffer(data_bh);
1178 	error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
1179 					       EXT4_JTR_NONE);
1180 	if (error) {
1181 		unlock_buffer(data_bh);
1182 		error = -EIO;
1183 		goto out_restore;
1184 	}
1185 	memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1186 
1187 	if (!S_ISDIR(inode->i_mode)) {
1188 		memcpy(data_bh->b_data, buf, inline_size);
1189 		set_buffer_uptodate(data_bh);
1190 		unlock_buffer(data_bh);
1191 		error = ext4_handle_dirty_metadata(handle,
1192 						   inode, data_bh);
1193 	} else {
1194 		unlock_buffer(data_bh);
1195 		inode->i_size = inode->i_sb->s_blocksize;
1196 		i_size_write(inode, inode->i_sb->s_blocksize);
1197 		EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1198 
1199 		error = ext4_init_dirblock(handle, inode, data_bh,
1200 			  le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode),
1201 			  buf + EXT4_INLINE_DOTDOT_SIZE,
1202 			  inline_size - EXT4_INLINE_DOTDOT_SIZE);
1203 		if (!error)
1204 			error = ext4_mark_inode_dirty(handle, inode);
1205 	}
1206 
1207 out_restore:
1208 	if (error)
1209 		ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1210 
1211 out:
1212 	brelse(data_bh);
1213 	kfree(buf);
1214 	return error;
1215 }
1216 
1217 /*
1218  * Try to add the new entry to the inline data.
1219  * If succeeds, return 0. If not, extended the inline dir and copied data to
1220  * the new created block.
1221  */
1222 int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1223 			      struct inode *dir, struct inode *inode)
1224 {
1225 	int ret, ret2, inline_size, no_expand;
1226 	void *inline_start;
1227 	struct ext4_iloc iloc;
1228 
1229 	ret = ext4_get_inode_loc(dir, &iloc);
1230 	if (ret)
1231 		return ret;
1232 
1233 	ext4_write_lock_xattr(dir, &no_expand);
1234 	if (!ext4_has_inline_data(dir))
1235 		goto out;
1236 
1237 	inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1238 						 EXT4_INLINE_DOTDOT_SIZE;
1239 	inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1240 
1241 	ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1242 					inline_start, inline_size);
1243 	if (ret != -ENOSPC)
1244 		goto out;
1245 
1246 	/* check whether it can be inserted to inline xattr space. */
1247 	inline_size = EXT4_I(dir)->i_inline_size -
1248 			EXT4_MIN_INLINE_DATA_SIZE;
1249 	if (!inline_size) {
1250 		/* Try to use the xattr space.*/
1251 		ret = ext4_update_inline_dir(handle, dir, &iloc);
1252 		if (ret && ret != -ENOSPC)
1253 			goto out;
1254 
1255 		inline_size = EXT4_I(dir)->i_inline_size -
1256 				EXT4_MIN_INLINE_DATA_SIZE;
1257 	}
1258 
1259 	if (inline_size) {
1260 		inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1261 
1262 		ret = ext4_add_dirent_to_inline(handle, fname, dir,
1263 						inode, &iloc, inline_start,
1264 						inline_size);
1265 
1266 		if (ret != -ENOSPC)
1267 			goto out;
1268 	}
1269 
1270 	/*
1271 	 * The inline space is filled up, so create a new block for it.
1272 	 * As the extent tree will be created, we have to save the inline
1273 	 * dir first.
1274 	 */
1275 	ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1276 
1277 out:
1278 	ext4_write_unlock_xattr(dir, &no_expand);
1279 	ret2 = ext4_mark_inode_dirty(handle, dir);
1280 	if (unlikely(ret2 && !ret))
1281 		ret = ret2;
1282 	brelse(iloc.bh);
1283 	return ret;
1284 }
1285 
1286 /*
1287  * This function fills a red-black tree with information from an
1288  * inlined dir.  It returns the number directory entries loaded
1289  * into the tree.  If there is an error it is returned in err.
1290  */
1291 int ext4_inlinedir_to_tree(struct file *dir_file,
1292 			   struct inode *dir, ext4_lblk_t block,
1293 			   struct dx_hash_info *hinfo,
1294 			   __u32 start_hash, __u32 start_minor_hash,
1295 			   int *has_inline_data)
1296 {
1297 	int err = 0, count = 0;
1298 	unsigned int parent_ino;
1299 	int pos;
1300 	struct ext4_dir_entry_2 *de;
1301 	struct inode *inode = file_inode(dir_file);
1302 	int ret, inline_size = 0;
1303 	struct ext4_iloc iloc;
1304 	void *dir_buf = NULL;
1305 	struct ext4_dir_entry_2 fake;
1306 	struct fscrypt_str tmp_str;
1307 
1308 	ret = ext4_get_inode_loc(inode, &iloc);
1309 	if (ret)
1310 		return ret;
1311 
1312 	down_read(&EXT4_I(inode)->xattr_sem);
1313 	if (!ext4_has_inline_data(inode)) {
1314 		up_read(&EXT4_I(inode)->xattr_sem);
1315 		*has_inline_data = 0;
1316 		goto out;
1317 	}
1318 
1319 	inline_size = ext4_get_inline_size(inode);
1320 	dir_buf = kmalloc(inline_size, GFP_NOFS);
1321 	if (!dir_buf) {
1322 		ret = -ENOMEM;
1323 		up_read(&EXT4_I(inode)->xattr_sem);
1324 		goto out;
1325 	}
1326 
1327 	ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1328 	up_read(&EXT4_I(inode)->xattr_sem);
1329 	if (ret < 0)
1330 		goto out;
1331 
1332 	pos = 0;
1333 	parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1334 	while (pos < inline_size) {
1335 		/*
1336 		 * As inlined dir doesn't store any information about '.' and
1337 		 * only the inode number of '..' is stored, we have to handle
1338 		 * them differently.
1339 		 */
1340 		if (pos == 0) {
1341 			fake.inode = cpu_to_le32(inode->i_ino);
1342 			fake.name_len = 1;
1343 			memcpy(fake.name, ".", 2);
1344 			fake.rec_len = ext4_rec_len_to_disk(
1345 					  ext4_dir_rec_len(fake.name_len, NULL),
1346 					  inline_size);
1347 			ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1348 			de = &fake;
1349 			pos = EXT4_INLINE_DOTDOT_OFFSET;
1350 		} else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1351 			fake.inode = cpu_to_le32(parent_ino);
1352 			fake.name_len = 2;
1353 			memcpy(fake.name, "..", 3);
1354 			fake.rec_len = ext4_rec_len_to_disk(
1355 					  ext4_dir_rec_len(fake.name_len, NULL),
1356 					  inline_size);
1357 			ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1358 			de = &fake;
1359 			pos = EXT4_INLINE_DOTDOT_SIZE;
1360 		} else {
1361 			de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1362 			pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1363 			if (ext4_check_dir_entry(inode, dir_file, de,
1364 					 iloc.bh, dir_buf,
1365 					 inline_size, pos)) {
1366 				ret = count;
1367 				goto out;
1368 			}
1369 		}
1370 
1371 		if (ext4_hash_in_dirent(dir)) {
1372 			hinfo->hash = EXT4_DIRENT_HASH(de);
1373 			hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
1374 		} else {
1375 			err = ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1376 			if (err) {
1377 				ret = err;
1378 				goto out;
1379 			}
1380 		}
1381 		if ((hinfo->hash < start_hash) ||
1382 		    ((hinfo->hash == start_hash) &&
1383 		     (hinfo->minor_hash < start_minor_hash)))
1384 			continue;
1385 		if (de->inode == 0)
1386 			continue;
1387 		tmp_str.name = de->name;
1388 		tmp_str.len = de->name_len;
1389 		err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1390 					      hinfo->minor_hash, de, &tmp_str);
1391 		if (err) {
1392 			ret = err;
1393 			goto out;
1394 		}
1395 		count++;
1396 	}
1397 	ret = count;
1398 out:
1399 	kfree(dir_buf);
1400 	brelse(iloc.bh);
1401 	return ret;
1402 }
1403 
1404 /*
1405  * So this function is called when the volume is mkfsed with
1406  * dir_index disabled. In order to keep f_pos persistent
1407  * after we convert from an inlined dir to a blocked based,
1408  * we just pretend that we are a normal dir and return the
1409  * offset as if '.' and '..' really take place.
1410  *
1411  */
1412 int ext4_read_inline_dir(struct file *file,
1413 			 struct dir_context *ctx,
1414 			 int *has_inline_data)
1415 {
1416 	unsigned int offset, parent_ino;
1417 	int i;
1418 	struct ext4_dir_entry_2 *de;
1419 	struct super_block *sb;
1420 	struct inode *inode = file_inode(file);
1421 	int ret, inline_size = 0;
1422 	struct ext4_iloc iloc;
1423 	void *dir_buf = NULL;
1424 	int dotdot_offset, dotdot_size, extra_offset, extra_size;
1425 	struct dir_private_info *info = file->private_data;
1426 
1427 	ret = ext4_get_inode_loc(inode, &iloc);
1428 	if (ret)
1429 		return ret;
1430 
1431 	down_read(&EXT4_I(inode)->xattr_sem);
1432 	if (!ext4_has_inline_data(inode)) {
1433 		up_read(&EXT4_I(inode)->xattr_sem);
1434 		*has_inline_data = 0;
1435 		goto out;
1436 	}
1437 
1438 	inline_size = ext4_get_inline_size(inode);
1439 	dir_buf = kmalloc(inline_size, GFP_NOFS);
1440 	if (!dir_buf) {
1441 		ret = -ENOMEM;
1442 		up_read(&EXT4_I(inode)->xattr_sem);
1443 		goto out;
1444 	}
1445 
1446 	ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1447 	up_read(&EXT4_I(inode)->xattr_sem);
1448 	if (ret < 0)
1449 		goto out;
1450 
1451 	ret = 0;
1452 	sb = inode->i_sb;
1453 	parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1454 	offset = ctx->pos;
1455 
1456 	/*
1457 	 * dotdot_offset and dotdot_size is the real offset and
1458 	 * size for ".." and "." if the dir is block based while
1459 	 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1460 	 * So we will use extra_offset and extra_size to indicate them
1461 	 * during the inline dir iteration.
1462 	 */
1463 	dotdot_offset = ext4_dir_rec_len(1, NULL);
1464 	dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
1465 	extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1466 	extra_size = extra_offset + inline_size;
1467 
1468 	/*
1469 	 * If the cookie has changed since the last call to
1470 	 * readdir(2), then we might be pointing to an invalid
1471 	 * dirent right now.  Scan from the start of the inline
1472 	 * dir to make sure.
1473 	 */
1474 	if (!inode_eq_iversion(inode, info->cookie)) {
1475 		for (i = 0; i < extra_size && i < offset;) {
1476 			/*
1477 			 * "." is with offset 0 and
1478 			 * ".." is dotdot_offset.
1479 			 */
1480 			if (!i) {
1481 				i = dotdot_offset;
1482 				continue;
1483 			} else if (i == dotdot_offset) {
1484 				i = dotdot_size;
1485 				continue;
1486 			}
1487 			/* for other entry, the real offset in
1488 			 * the buf has to be tuned accordingly.
1489 			 */
1490 			if (i + ext4_dir_rec_len(1, NULL) > extra_size)
1491 				break;
1492 			de = (struct ext4_dir_entry_2 *)
1493 				(dir_buf + i - extra_offset);
1494 			/* It's too expensive to do a full
1495 			 * dirent test each time round this
1496 			 * loop, but we do have to test at
1497 			 * least that it is non-zero.  A
1498 			 * failure will be detected in the
1499 			 * dirent test below. */
1500 			if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1501 				< ext4_dir_rec_len(1, NULL))
1502 				break;
1503 			i += ext4_rec_len_from_disk(de->rec_len,
1504 						    extra_size);
1505 		}
1506 		offset = i;
1507 		ctx->pos = offset;
1508 		info->cookie = inode_query_iversion(inode);
1509 	}
1510 
1511 	while (ctx->pos < extra_size) {
1512 		if (ctx->pos == 0) {
1513 			if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1514 				goto out;
1515 			ctx->pos = dotdot_offset;
1516 			continue;
1517 		}
1518 
1519 		if (ctx->pos == dotdot_offset) {
1520 			if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1521 				goto out;
1522 			ctx->pos = dotdot_size;
1523 			continue;
1524 		}
1525 
1526 		/*
1527 		 * de lives at dir_buf + ctx->pos - extra_offset, within the
1528 		 * kmalloc(inline_size) buffer.  Make sure its header fits before
1529 		 * ext4_check_dir_entry() dereferences de->rec_len.
1530 		 */
1531 		if (ctx->pos + ext4_dir_rec_len(1, NULL) > extra_size)
1532 			goto out;
1533 		de = (struct ext4_dir_entry_2 *)
1534 			(dir_buf + ctx->pos - extra_offset);
1535 		if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1536 					 inline_size, ctx->pos))
1537 			goto out;
1538 		if (le32_to_cpu(de->inode)) {
1539 			if (!dir_emit(ctx, de->name, de->name_len,
1540 				      le32_to_cpu(de->inode),
1541 				      get_dtype(sb, de->file_type)))
1542 				goto out;
1543 		}
1544 		ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1545 	}
1546 out:
1547 	kfree(dir_buf);
1548 	brelse(iloc.bh);
1549 	return ret;
1550 }
1551 
1552 void *ext4_read_inline_link(struct inode *inode)
1553 {
1554 	struct ext4_iloc iloc;
1555 	int ret, inline_size;
1556 	void *link;
1557 
1558 	ret = ext4_get_inode_loc(inode, &iloc);
1559 	if (ret)
1560 		return ERR_PTR(ret);
1561 
1562 	ret = -ENOMEM;
1563 	inline_size = ext4_get_inline_size(inode);
1564 	link = kmalloc(inline_size + 1, GFP_NOFS);
1565 	if (!link)
1566 		goto out;
1567 
1568 	ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
1569 	if (ret < 0) {
1570 		kfree(link);
1571 		goto out;
1572 	}
1573 	nd_terminate_link(link, inode->i_size, ret);
1574 out:
1575 	if (ret < 0)
1576 		link = ERR_PTR(ret);
1577 	brelse(iloc.bh);
1578 	return link;
1579 }
1580 
1581 struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1582 					struct ext4_dir_entry_2 **parent_de,
1583 					int *retval)
1584 {
1585 	struct ext4_iloc iloc;
1586 
1587 	*retval = ext4_get_inode_loc(inode, &iloc);
1588 	if (*retval)
1589 		return NULL;
1590 
1591 	*parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1592 
1593 	return iloc.bh;
1594 }
1595 
1596 /*
1597  * Try to create the inline data for the new dir.
1598  * If it succeeds, return 0, otherwise return the error.
1599  * In case of ENOSPC, the caller should create the normal disk layout dir.
1600  */
1601 int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1602 			       struct inode *inode)
1603 {
1604 	int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1605 	struct ext4_iloc iloc;
1606 	struct ext4_dir_entry_2 *de;
1607 
1608 	ret = ext4_get_inode_loc(inode, &iloc);
1609 	if (ret)
1610 		return ret;
1611 
1612 	ret = ext4_prepare_inline_data(handle, inode, inline_size);
1613 	if (ret)
1614 		goto out;
1615 
1616 	/*
1617 	 * For inline dir, we only save the inode information for the ".."
1618 	 * and create a fake dentry to cover the left space.
1619 	 */
1620 	de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1621 	de->inode = cpu_to_le32(parent->i_ino);
1622 	de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1623 	de->inode = 0;
1624 	de->rec_len = ext4_rec_len_to_disk(
1625 				inline_size - EXT4_INLINE_DOTDOT_SIZE,
1626 				inline_size);
1627 	set_nlink(inode, 2);
1628 	inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1629 out:
1630 	brelse(iloc.bh);
1631 	return ret;
1632 }
1633 
1634 struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1635 					struct ext4_filename *fname,
1636 					struct ext4_dir_entry_2 **res_dir,
1637 					int *has_inline_data)
1638 {
1639 	struct ext4_xattr_ibody_find is = {
1640 		.s = { .not_found = -ENODATA, },
1641 	};
1642 	struct ext4_xattr_info i = {
1643 		.name_index = EXT4_XATTR_INDEX_SYSTEM,
1644 		.name = EXT4_XATTR_SYSTEM_DATA,
1645 	};
1646 	int ret;
1647 	void *inline_start;
1648 	int inline_size;
1649 
1650 	ret = ext4_get_inode_loc(dir, &is.iloc);
1651 	if (ret)
1652 		return ERR_PTR(ret);
1653 
1654 	down_read(&EXT4_I(dir)->xattr_sem);
1655 
1656 	ret = ext4_xattr_ibody_find(dir, &i, &is);
1657 	if (ret)
1658 		goto out;
1659 
1660 	if (!ext4_has_inline_data(dir)) {
1661 		*has_inline_data = 0;
1662 		goto out;
1663 	}
1664 
1665 	inline_start = (void *)ext4_raw_inode(&is.iloc)->i_block +
1666 						EXT4_INLINE_DOTDOT_SIZE;
1667 	inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1668 	ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
1669 			      dir, fname, 0, res_dir);
1670 	if (ret == 1)
1671 		goto out_find;
1672 	if (ret < 0)
1673 		goto out;
1674 
1675 	if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1676 		goto out;
1677 
1678 	inline_start = ext4_get_inline_xattr_pos(dir, &is.iloc);
1679 	inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1680 
1681 	ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
1682 			      dir, fname, 0, res_dir);
1683 	if (ret == 1)
1684 		goto out_find;
1685 
1686 out:
1687 	brelse(is.iloc.bh);
1688 	if (ret < 0)
1689 		is.iloc.bh = ERR_PTR(ret);
1690 	else
1691 		is.iloc.bh = NULL;
1692 out_find:
1693 	up_read(&EXT4_I(dir)->xattr_sem);
1694 	return is.iloc.bh;
1695 }
1696 
1697 int ext4_delete_inline_entry(handle_t *handle,
1698 			     struct inode *dir,
1699 			     struct ext4_dir_entry_2 *de_del,
1700 			     struct buffer_head *bh,
1701 			     int *has_inline_data)
1702 {
1703 	int err, inline_size, no_expand;
1704 	struct ext4_iloc iloc;
1705 	void *inline_start;
1706 
1707 	err = ext4_get_inode_loc(dir, &iloc);
1708 	if (err)
1709 		return err;
1710 
1711 	ext4_write_lock_xattr(dir, &no_expand);
1712 	if (!ext4_has_inline_data(dir)) {
1713 		*has_inline_data = 0;
1714 		goto out;
1715 	}
1716 
1717 	if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1718 		EXT4_MIN_INLINE_DATA_SIZE) {
1719 		inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1720 					EXT4_INLINE_DOTDOT_SIZE;
1721 		inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1722 				EXT4_INLINE_DOTDOT_SIZE;
1723 	} else {
1724 		inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1725 		inline_size = ext4_get_inline_size(dir) -
1726 				EXT4_MIN_INLINE_DATA_SIZE;
1727 	}
1728 
1729 	BUFFER_TRACE(bh, "get_write_access");
1730 	err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
1731 					    EXT4_JTR_NONE);
1732 	if (err)
1733 		goto out;
1734 
1735 	err = ext4_generic_delete_entry(dir, de_del, bh,
1736 					inline_start, inline_size, 0);
1737 	if (err)
1738 		goto out;
1739 
1740 	ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1741 out:
1742 	ext4_write_unlock_xattr(dir, &no_expand);
1743 	if (likely(err == 0))
1744 		err = ext4_mark_inode_dirty(handle, dir);
1745 	brelse(iloc.bh);
1746 	if (err != -ENOENT)
1747 		ext4_std_error(dir->i_sb, err);
1748 	return err;
1749 }
1750 
1751 /*
1752  * Get the inline dentry at offset.
1753  */
1754 static inline struct ext4_dir_entry_2 *
1755 ext4_get_inline_entry(struct inode *inode,
1756 		      struct ext4_iloc *iloc,
1757 		      unsigned int offset,
1758 		      void **inline_start,
1759 		      int *inline_size)
1760 {
1761 	void *inline_pos;
1762 
1763 	BUG_ON(offset > ext4_get_inline_size(inode));
1764 
1765 	if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1766 		inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1767 		*inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1768 	} else {
1769 		inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1770 		offset -= EXT4_MIN_INLINE_DATA_SIZE;
1771 		*inline_size = ext4_get_inline_size(inode) -
1772 				EXT4_MIN_INLINE_DATA_SIZE;
1773 	}
1774 
1775 	if (inline_start)
1776 		*inline_start = inline_pos;
1777 	return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1778 }
1779 
1780 bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1781 {
1782 	int err, inline_size;
1783 	struct ext4_iloc iloc;
1784 	size_t inline_len;
1785 	void *inline_pos;
1786 	unsigned int offset;
1787 	struct ext4_dir_entry_2 *de;
1788 	bool ret = false;
1789 
1790 	err = ext4_get_inode_loc(dir, &iloc);
1791 	if (err) {
1792 		EXT4_ERROR_INODE_ERR(dir, -err,
1793 				     "error %d getting inode %llu block",
1794 				     err, dir->i_ino);
1795 		return false;
1796 	}
1797 
1798 	down_read(&EXT4_I(dir)->xattr_sem);
1799 	if (!ext4_has_inline_data(dir)) {
1800 		*has_inline_data = 0;
1801 		ret = true;
1802 		goto out;
1803 	}
1804 
1805 	de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1806 	if (!le32_to_cpu(de->inode)) {
1807 		ext4_warning(dir->i_sb,
1808 			     "bad inline directory (dir #%llu) - no `..'",
1809 			     dir->i_ino);
1810 		goto out;
1811 	}
1812 
1813 	inline_len = ext4_get_inline_size(dir);
1814 	offset = EXT4_INLINE_DOTDOT_SIZE;
1815 	while (offset < inline_len) {
1816 		de = ext4_get_inline_entry(dir, &iloc, offset,
1817 					   &inline_pos, &inline_size);
1818 		if (ext4_check_dir_entry(dir, NULL, de,
1819 					 iloc.bh, inline_pos,
1820 					 inline_size, offset)) {
1821 			ext4_warning(dir->i_sb,
1822 				     "bad inline directory (dir #%llu) - "
1823 				     "inode %u, rec_len %u, name_len %d"
1824 				     "inline size %d",
1825 				     dir->i_ino, le32_to_cpu(de->inode),
1826 				     le16_to_cpu(de->rec_len), de->name_len,
1827 				     inline_size);
1828 			goto out;
1829 		}
1830 		if (le32_to_cpu(de->inode)) {
1831 			goto out;
1832 		}
1833 		offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1834 	}
1835 
1836 	ret = true;
1837 out:
1838 	up_read(&EXT4_I(dir)->xattr_sem);
1839 	brelse(iloc.bh);
1840 	return ret;
1841 }
1842 
1843 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1844 {
1845 	int ret, no_expand;
1846 
1847 	ext4_write_lock_xattr(inode, &no_expand);
1848 	ret = ext4_destroy_inline_data_nolock(handle, inode);
1849 	ext4_write_unlock_xattr(inode, &no_expand);
1850 
1851 	return ret;
1852 }
1853 
1854 int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1855 {
1856 	__u64 addr;
1857 	int error = -EAGAIN;
1858 	struct ext4_iloc iloc;
1859 
1860 	down_read(&EXT4_I(inode)->xattr_sem);
1861 	if (!ext4_has_inline_data(inode))
1862 		goto out;
1863 
1864 	error = ext4_get_inode_loc(inode, &iloc);
1865 	if (error)
1866 		goto out;
1867 
1868 	addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1869 	addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1870 	addr += offsetof(struct ext4_inode, i_block);
1871 
1872 	brelse(iloc.bh);
1873 
1874 	iomap->addr = addr;
1875 	iomap->offset = 0;
1876 	iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1877 			      i_size_read(inode));
1878 	iomap->type = IOMAP_INLINE;
1879 	iomap->flags = 0;
1880 
1881 out:
1882 	up_read(&EXT4_I(inode)->xattr_sem);
1883 	return error;
1884 }
1885 
1886 int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1887 {
1888 	handle_t *handle;
1889 	int inline_size, value_len, needed_blocks, no_expand, err = 0;
1890 	size_t i_size;
1891 	void *value = NULL;
1892 	struct ext4_xattr_ibody_find is = {
1893 		.s = { .not_found = -ENODATA, },
1894 	};
1895 	struct ext4_xattr_info i = {
1896 		.name_index = EXT4_XATTR_INDEX_SYSTEM,
1897 		.name = EXT4_XATTR_SYSTEM_DATA,
1898 	};
1899 
1900 
1901 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
1902 	handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1903 	if (IS_ERR(handle))
1904 		return PTR_ERR(handle);
1905 
1906 	ext4_write_lock_xattr(inode, &no_expand);
1907 	if (!ext4_has_inline_data(inode)) {
1908 		ext4_write_unlock_xattr(inode, &no_expand);
1909 		*has_inline = 0;
1910 		ext4_journal_stop(handle);
1911 		return 0;
1912 	}
1913 
1914 	if ((err = ext4_orphan_add(handle, inode)) != 0)
1915 		goto out;
1916 
1917 	if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1918 		goto out;
1919 
1920 	down_write(&EXT4_I(inode)->i_data_sem);
1921 	i_size = inode->i_size;
1922 	inline_size = ext4_get_inline_size(inode);
1923 	EXT4_I(inode)->i_disksize = i_size;
1924 
1925 	if (i_size < inline_size) {
1926 		/*
1927 		 * if there's inline data to truncate and this file was
1928 		 * converted to extents after that inline data was written,
1929 		 * the extent status cache must be cleared to avoid leaving
1930 		 * behind stale delayed allocated extent entries
1931 		 */
1932 		if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1933 			ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, i_size);
1934 
1935 			ext4_es_remove_extent(inode, lblk,
1936 					      EXT_MAX_BLOCKS - lblk);
1937 		}
1938 
1939 		/* Clear the content in the xattr space. */
1940 		if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1941 			if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1942 				goto out_error;
1943 
1944 			if (is.s.not_found) {
1945 				EXT4_ERROR_INODE(inode,
1946 						 "missing inline data xattr");
1947 				err = -EFSCORRUPTED;
1948 				goto out_error;
1949 			}
1950 
1951 			value_len = le32_to_cpu(is.s.here->e_value_size);
1952 			value = kmalloc(value_len, GFP_NOFS);
1953 			if (!value) {
1954 				err = -ENOMEM;
1955 				goto out_error;
1956 			}
1957 
1958 			err = ext4_xattr_ibody_get(inode, i.name_index,
1959 						   i.name, value, value_len);
1960 			if (err <= 0)
1961 				goto out_error;
1962 
1963 			i.value = value;
1964 			i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1965 					i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1966 			err = ext4_xattr_ibody_set(handle, inode, &i, &is);
1967 			if (err)
1968 				goto out_error;
1969 		}
1970 
1971 		/* Clear the content within i_blocks. */
1972 		if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1973 			void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1974 			memset(p + i_size, 0,
1975 			       EXT4_MIN_INLINE_DATA_SIZE - i_size);
1976 		}
1977 
1978 		EXT4_I(inode)->i_inline_size = i_size <
1979 					EXT4_MIN_INLINE_DATA_SIZE ?
1980 					EXT4_MIN_INLINE_DATA_SIZE : i_size;
1981 	}
1982 
1983 out_error:
1984 	up_write(&EXT4_I(inode)->i_data_sem);
1985 out:
1986 	brelse(is.iloc.bh);
1987 	ext4_write_unlock_xattr(inode, &no_expand);
1988 	kfree(value);
1989 	if (inode->i_nlink)
1990 		ext4_orphan_del(handle, inode);
1991 
1992 	if (err == 0) {
1993 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1994 		err = ext4_mark_inode_dirty(handle, inode);
1995 		if (IS_SYNC(inode))
1996 			ext4_handle_sync(handle);
1997 	}
1998 	ext4_journal_stop(handle);
1999 	return err;
2000 }
2001 
2002 int ext4_convert_inline_data(struct inode *inode)
2003 {
2004 	int error, needed_blocks, no_expand;
2005 	handle_t *handle;
2006 	struct ext4_iloc iloc;
2007 
2008 	if (!ext4_has_inline_data(inode)) {
2009 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2010 		return 0;
2011 	} else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2012 		/*
2013 		 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
2014 		 * cleared. This means we are in the middle of moving of
2015 		 * inline data to delay allocated block. Just force writeout
2016 		 * here to finish conversion.
2017 		 */
2018 		error = filemap_flush(inode->i_mapping);
2019 		if (error)
2020 			return error;
2021 		if (!ext4_has_inline_data(inode))
2022 			return 0;
2023 	}
2024 
2025 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
2026 
2027 	iloc.bh = NULL;
2028 	error = ext4_get_inode_loc(inode, &iloc);
2029 	if (error)
2030 		return error;
2031 
2032 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2033 	if (IS_ERR(handle)) {
2034 		error = PTR_ERR(handle);
2035 		goto out_free;
2036 	}
2037 
2038 	ext4_write_lock_xattr(inode, &no_expand);
2039 	if (ext4_has_inline_data(inode))
2040 		error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2041 	ext4_write_unlock_xattr(inode, &no_expand);
2042 	ext4_journal_stop(handle);
2043 out_free:
2044 	brelse(iloc.bh);
2045 	return error;
2046 }
2047 
2048 #if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS)
2049 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_inline_getblk);
2050 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_get_max_inline_size);
2051 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_inline_data_nolock);
2052 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_readpage_inline);
2053 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_generic_write_inline_data);
2054 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_try_to_write_inline_data);
2055 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_write_inline_data_end);
2056 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_update_final_de);
2057 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_try_add_inline_entry);
2058 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_inlinedir_to_tree);
2059 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_read_inline_dir);
2060 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_read_inline_link);
2061 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_get_first_inline_block);
2062 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_try_create_inline_dir);
2063 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_inline_entry);
2064 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_delete_inline_entry);
2065 EXPORT_SYMBOL_FOR_EXT4_TEST(empty_inline_dir);
2066 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_destroy_inline_data);
2067 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_inline_data_iomap);
2068 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_inline_data_truncate);
2069 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_convert_inline_data);
2070 #endif
2071