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