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