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