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