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