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