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