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