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