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