blob: d06563453f350bb71c55b7695c5a189bc87fcce3 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// 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
22static 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
30static 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
83out:
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 */
92int 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 */
125int 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 }
160out:
161 brelse(is.iloc.bh);
162 return error;
163}
164
165static 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
201out:
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 */
211static 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
250static 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
309out:
310 brelse(is.iloc.bh);
311 return error;
312}
313
314static 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
374out:
375 kfree(value);
376 brelse(is.iloc.bh);
377 return error;
378}
379
380static 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
404static 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);
458out:
459 brelse(is.iloc.bh);
460 if (error == -ENODATA)
461 error = 0;
462 return error;
463}
464
465static 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
495out:
496 return ret;
497}
498
499int 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
539static 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
565retry:
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);
640out:
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 */
659int 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;
731out_up_read:
732 up_read(&EXT4_I(inode)->xattr_sem);
733out:
734 if (handle && (ret != 1))
735 ext4_journal_stop(handle);
736 brelse(iloc.bh);
737 return ret;
738convert:
739 return ext4_convert_inline_data_to_extent(mapping,
740 inode, flags);
741}
742
743int 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);
777out:
778 return copied;
779}
780
781struct buffer_head *
782ext4_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 */
814static 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
855out:
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 */
872int 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
889retry_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;
948out_release_page:
949 up_read(&EXT4_I(inode)->xattr_sem);
950 unlock_page(page);
951 put_page(page);
952out_journal:
953 ext4_journal_stop(handle);
954out:
955 brelse(iloc.bh);
956 return ret;
957}
958
959int 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
997void 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 */
1029static 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
1069static 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. */
1085static 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
1110static 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
1132static 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
1141static 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 struct ext4_dir_entry_tail *t;
1150 void *target = dir_block->b_data;
1151
1152 /*
1153 * First create "." and ".." and then copy the dir information
1154 * back to the block.
1155 */
1156 de = (struct ext4_dir_entry_2 *)target;
1157 de = ext4_init_dot_dotdot(inode, de,
1158 inode->i_sb->s_blocksize, csum_size,
1159 le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
1160 header_size = (void *)de - target;
1161
1162 memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
1163 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1164
1165 if (ext4_has_metadata_csum(inode->i_sb))
1166 csum_size = sizeof(struct ext4_dir_entry_tail);
1167
1168 inode->i_size = inode->i_sb->s_blocksize;
1169 i_size_write(inode, inode->i_sb->s_blocksize);
1170 EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
1171 ext4_update_final_de(dir_block->b_data,
1172 inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
1173 inode->i_sb->s_blocksize - csum_size);
1174
1175 if (csum_size) {
1176 t = EXT4_DIRENT_TAIL(dir_block->b_data,
1177 inode->i_sb->s_blocksize);
1178 initialize_dirent_tail(t, inode->i_sb->s_blocksize);
1179 }
1180 set_buffer_uptodate(dir_block);
1181 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
1182 if (err)
1183 return err;
1184 set_buffer_verified(dir_block);
1185 return ext4_mark_inode_dirty(handle, inode);
1186}
1187
1188static int ext4_convert_inline_data_nolock(handle_t *handle,
1189 struct inode *inode,
1190 struct ext4_iloc *iloc)
1191{
1192 int error;
1193 void *buf = NULL;
1194 struct buffer_head *data_bh = NULL;
1195 struct ext4_map_blocks map;
1196 int inline_size;
1197
1198 inline_size = ext4_get_inline_size(inode);
1199 buf = kmalloc(inline_size, GFP_NOFS);
1200 if (!buf) {
1201 error = -ENOMEM;
1202 goto out;
1203 }
1204
1205 error = ext4_read_inline_data(inode, buf, inline_size, iloc);
1206 if (error < 0)
1207 goto out;
1208
1209 /*
1210 * Make sure the inline directory entries pass checks before we try to
1211 * convert them, so that we avoid touching stuff that needs fsck.
1212 */
1213 if (S_ISDIR(inode->i_mode)) {
1214 error = ext4_check_all_de(inode, iloc->bh,
1215 buf + EXT4_INLINE_DOTDOT_SIZE,
1216 inline_size - EXT4_INLINE_DOTDOT_SIZE);
1217 if (error)
1218 goto out;
1219 }
1220
1221 error = ext4_destroy_inline_data_nolock(handle, inode);
1222 if (error)
1223 goto out;
1224
1225 map.m_lblk = 0;
1226 map.m_len = 1;
1227 map.m_flags = 0;
1228 error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
1229 if (error < 0)
1230 goto out_restore;
1231 if (!(map.m_flags & EXT4_MAP_MAPPED)) {
1232 error = -EIO;
1233 goto out_restore;
1234 }
1235
1236 data_bh = sb_getblk(inode->i_sb, map.m_pblk);
1237 if (!data_bh) {
1238 error = -ENOMEM;
1239 goto out_restore;
1240 }
1241
1242 lock_buffer(data_bh);
1243 error = ext4_journal_get_create_access(handle, data_bh);
1244 if (error) {
1245 unlock_buffer(data_bh);
1246 error = -EIO;
1247 goto out_restore;
1248 }
1249 memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
1250
1251 if (!S_ISDIR(inode->i_mode)) {
1252 memcpy(data_bh->b_data, buf, inline_size);
1253 set_buffer_uptodate(data_bh);
1254 error = ext4_handle_dirty_metadata(handle,
1255 inode, data_bh);
1256 } else {
1257 error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
1258 buf, inline_size);
1259 }
1260
1261 unlock_buffer(data_bh);
1262out_restore:
1263 if (error)
1264 ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
1265
1266out:
1267 brelse(data_bh);
1268 kfree(buf);
1269 return error;
1270}
1271
1272/*
1273 * Try to add the new entry to the inline data.
1274 * If succeeds, return 0. If not, extended the inline dir and copied data to
1275 * the new created block.
1276 */
1277int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
1278 struct inode *dir, struct inode *inode)
1279{
1280 int ret, inline_size, no_expand;
1281 void *inline_start;
1282 struct ext4_iloc iloc;
1283
1284 ret = ext4_get_inode_loc(dir, &iloc);
1285 if (ret)
1286 return ret;
1287
1288 ext4_write_lock_xattr(dir, &no_expand);
1289 if (!ext4_has_inline_data(dir))
1290 goto out;
1291
1292 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1293 EXT4_INLINE_DOTDOT_SIZE;
1294 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1295
1296 ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
1297 inline_start, inline_size);
1298 if (ret != -ENOSPC)
1299 goto out;
1300
1301 /* check whether it can be inserted to inline xattr space. */
1302 inline_size = EXT4_I(dir)->i_inline_size -
1303 EXT4_MIN_INLINE_DATA_SIZE;
1304 if (!inline_size) {
1305 /* Try to use the xattr space.*/
1306 ret = ext4_update_inline_dir(handle, dir, &iloc);
1307 if (ret && ret != -ENOSPC)
1308 goto out;
1309
1310 inline_size = EXT4_I(dir)->i_inline_size -
1311 EXT4_MIN_INLINE_DATA_SIZE;
1312 }
1313
1314 if (inline_size) {
1315 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1316
1317 ret = ext4_add_dirent_to_inline(handle, fname, dir,
1318 inode, &iloc, inline_start,
1319 inline_size);
1320
1321 if (ret != -ENOSPC)
1322 goto out;
1323 }
1324
1325 /*
1326 * The inline space is filled up, so create a new block for it.
1327 * As the extent tree will be created, we have to save the inline
1328 * dir first.
1329 */
1330 ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
1331
1332out:
1333 ext4_write_unlock_xattr(dir, &no_expand);
1334 ext4_mark_inode_dirty(handle, dir);
1335 brelse(iloc.bh);
1336 return ret;
1337}
1338
1339/*
1340 * This function fills a red-black tree with information from an
1341 * inlined dir. It returns the number directory entries loaded
1342 * into the tree. If there is an error it is returned in err.
1343 */
1344int htree_inlinedir_to_tree(struct file *dir_file,
1345 struct inode *dir, ext4_lblk_t block,
1346 struct dx_hash_info *hinfo,
1347 __u32 start_hash, __u32 start_minor_hash,
1348 int *has_inline_data)
1349{
1350 int err = 0, count = 0;
1351 unsigned int parent_ino;
1352 int pos;
1353 struct ext4_dir_entry_2 *de;
1354 struct inode *inode = file_inode(dir_file);
1355 int ret, inline_size = 0;
1356 struct ext4_iloc iloc;
1357 void *dir_buf = NULL;
1358 struct ext4_dir_entry_2 fake;
1359 struct fscrypt_str tmp_str;
1360
1361 ret = ext4_get_inode_loc(inode, &iloc);
1362 if (ret)
1363 return ret;
1364
1365 down_read(&EXT4_I(inode)->xattr_sem);
1366 if (!ext4_has_inline_data(inode)) {
1367 up_read(&EXT4_I(inode)->xattr_sem);
1368 *has_inline_data = 0;
1369 goto out;
1370 }
1371
1372 inline_size = ext4_get_inline_size(inode);
1373 dir_buf = kmalloc(inline_size, GFP_NOFS);
1374 if (!dir_buf) {
1375 ret = -ENOMEM;
1376 up_read(&EXT4_I(inode)->xattr_sem);
1377 goto out;
1378 }
1379
1380 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1381 up_read(&EXT4_I(inode)->xattr_sem);
1382 if (ret < 0)
1383 goto out;
1384
1385 pos = 0;
1386 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1387 while (pos < inline_size) {
1388 /*
1389 * As inlined dir doesn't store any information about '.' and
1390 * only the inode number of '..' is stored, we have to handle
1391 * them differently.
1392 */
1393 if (pos == 0) {
1394 fake.inode = cpu_to_le32(inode->i_ino);
1395 fake.name_len = 1;
1396 strcpy(fake.name, ".");
1397 fake.rec_len = ext4_rec_len_to_disk(
1398 EXT4_DIR_REC_LEN(fake.name_len),
1399 inline_size);
1400 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1401 de = &fake;
1402 pos = EXT4_INLINE_DOTDOT_OFFSET;
1403 } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
1404 fake.inode = cpu_to_le32(parent_ino);
1405 fake.name_len = 2;
1406 strcpy(fake.name, "..");
1407 fake.rec_len = ext4_rec_len_to_disk(
1408 EXT4_DIR_REC_LEN(fake.name_len),
1409 inline_size);
1410 ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
1411 de = &fake;
1412 pos = EXT4_INLINE_DOTDOT_SIZE;
1413 } else {
1414 de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
1415 pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
1416 if (ext4_check_dir_entry(inode, dir_file, de,
1417 iloc.bh, dir_buf,
1418 inline_size, pos)) {
1419 ret = count;
1420 goto out;
1421 }
1422 }
1423
1424 ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
1425 if ((hinfo->hash < start_hash) ||
1426 ((hinfo->hash == start_hash) &&
1427 (hinfo->minor_hash < start_minor_hash)))
1428 continue;
1429 if (de->inode == 0)
1430 continue;
1431 tmp_str.name = de->name;
1432 tmp_str.len = de->name_len;
1433 err = ext4_htree_store_dirent(dir_file, hinfo->hash,
1434 hinfo->minor_hash, de, &tmp_str);
1435 if (err) {
1436 count = err;
1437 goto out;
1438 }
1439 count++;
1440 }
1441 ret = count;
1442out:
1443 kfree(dir_buf);
1444 brelse(iloc.bh);
1445 return ret;
1446}
1447
1448/*
1449 * So this function is called when the volume is mkfsed with
1450 * dir_index disabled. In order to keep f_pos persistent
1451 * after we convert from an inlined dir to a blocked based,
1452 * we just pretend that we are a normal dir and return the
1453 * offset as if '.' and '..' really take place.
1454 *
1455 */
1456int ext4_read_inline_dir(struct file *file,
1457 struct dir_context *ctx,
1458 int *has_inline_data)
1459{
1460 unsigned int offset, parent_ino;
1461 int i;
1462 struct ext4_dir_entry_2 *de;
1463 struct super_block *sb;
1464 struct inode *inode = file_inode(file);
1465 int ret, inline_size = 0;
1466 struct ext4_iloc iloc;
1467 void *dir_buf = NULL;
1468 int dotdot_offset, dotdot_size, extra_offset, extra_size;
1469
1470 ret = ext4_get_inode_loc(inode, &iloc);
1471 if (ret)
1472 return ret;
1473
1474 down_read(&EXT4_I(inode)->xattr_sem);
1475 if (!ext4_has_inline_data(inode)) {
1476 up_read(&EXT4_I(inode)->xattr_sem);
1477 *has_inline_data = 0;
1478 goto out;
1479 }
1480
1481 inline_size = ext4_get_inline_size(inode);
1482 dir_buf = kmalloc(inline_size, GFP_NOFS);
1483 if (!dir_buf) {
1484 ret = -ENOMEM;
1485 up_read(&EXT4_I(inode)->xattr_sem);
1486 goto out;
1487 }
1488
1489 ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
1490 up_read(&EXT4_I(inode)->xattr_sem);
1491 if (ret < 0)
1492 goto out;
1493
1494 ret = 0;
1495 sb = inode->i_sb;
1496 parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
1497 offset = ctx->pos;
1498
1499 /*
1500 * dotdot_offset and dotdot_size is the real offset and
1501 * size for ".." and "." if the dir is block based while
1502 * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
1503 * So we will use extra_offset and extra_size to indicate them
1504 * during the inline dir iteration.
1505 */
1506 dotdot_offset = EXT4_DIR_REC_LEN(1);
1507 dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
1508 extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
1509 extra_size = extra_offset + inline_size;
1510
1511 /*
1512 * If the version has changed since the last call to
1513 * readdir(2), then we might be pointing to an invalid
1514 * dirent right now. Scan from the start of the inline
1515 * dir to make sure.
1516 */
1517 if (!inode_eq_iversion(inode, file->f_version)) {
1518 for (i = 0; i < extra_size && i < offset;) {
1519 /*
1520 * "." is with offset 0 and
1521 * ".." is dotdot_offset.
1522 */
1523 if (!i) {
1524 i = dotdot_offset;
1525 continue;
1526 } else if (i == dotdot_offset) {
1527 i = dotdot_size;
1528 continue;
1529 }
1530 /* for other entry, the real offset in
1531 * the buf has to be tuned accordingly.
1532 */
1533 de = (struct ext4_dir_entry_2 *)
1534 (dir_buf + i - extra_offset);
1535 /* It's too expensive to do a full
1536 * dirent test each time round this
1537 * loop, but we do have to test at
1538 * least that it is non-zero. A
1539 * failure will be detected in the
1540 * dirent test below. */
1541 if (ext4_rec_len_from_disk(de->rec_len, extra_size)
1542 < EXT4_DIR_REC_LEN(1))
1543 break;
1544 i += ext4_rec_len_from_disk(de->rec_len,
1545 extra_size);
1546 }
1547 offset = i;
1548 ctx->pos = offset;
1549 file->f_version = inode_query_iversion(inode);
1550 }
1551
1552 while (ctx->pos < extra_size) {
1553 if (ctx->pos == 0) {
1554 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
1555 goto out;
1556 ctx->pos = dotdot_offset;
1557 continue;
1558 }
1559
1560 if (ctx->pos == dotdot_offset) {
1561 if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
1562 goto out;
1563 ctx->pos = dotdot_size;
1564 continue;
1565 }
1566
1567 de = (struct ext4_dir_entry_2 *)
1568 (dir_buf + ctx->pos - extra_offset);
1569 if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
1570 extra_size, ctx->pos))
1571 goto out;
1572 if (le32_to_cpu(de->inode)) {
1573 if (!dir_emit(ctx, de->name, de->name_len,
1574 le32_to_cpu(de->inode),
1575 get_dtype(sb, de->file_type)))
1576 goto out;
1577 }
1578 ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
1579 }
1580out:
1581 kfree(dir_buf);
1582 brelse(iloc.bh);
1583 return ret;
1584}
1585
1586struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1587 struct ext4_dir_entry_2 **parent_de,
1588 int *retval)
1589{
1590 struct ext4_iloc iloc;
1591
1592 *retval = ext4_get_inode_loc(inode, &iloc);
1593 if (*retval)
1594 return NULL;
1595
1596 *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1597
1598 return iloc.bh;
1599}
1600
1601/*
1602 * Try to create the inline data for the new dir.
1603 * If it succeeds, return 0, otherwise return the error.
1604 * In case of ENOSPC, the caller should create the normal disk layout dir.
1605 */
1606int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
1607 struct inode *inode)
1608{
1609 int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1610 struct ext4_iloc iloc;
1611 struct ext4_dir_entry_2 *de;
1612
1613 ret = ext4_get_inode_loc(inode, &iloc);
1614 if (ret)
1615 return ret;
1616
1617 ret = ext4_prepare_inline_data(handle, inode, inline_size);
1618 if (ret)
1619 goto out;
1620
1621 /*
1622 * For inline dir, we only save the inode information for the ".."
1623 * and create a fake dentry to cover the left space.
1624 */
1625 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1626 de->inode = cpu_to_le32(parent->i_ino);
1627 de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
1628 de->inode = 0;
1629 de->rec_len = ext4_rec_len_to_disk(
1630 inline_size - EXT4_INLINE_DOTDOT_SIZE,
1631 inline_size);
1632 set_nlink(inode, 2);
1633 inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
1634out:
1635 brelse(iloc.bh);
1636 return ret;
1637}
1638
1639struct buffer_head *ext4_find_inline_entry(struct inode *dir,
1640 struct ext4_filename *fname,
1641 struct ext4_dir_entry_2 **res_dir,
1642 int *has_inline_data)
1643{
1644 int ret;
1645 struct ext4_iloc iloc;
1646 void *inline_start;
1647 int inline_size;
1648
1649 if (ext4_get_inode_loc(dir, &iloc))
1650 return NULL;
1651
1652 down_read(&EXT4_I(dir)->xattr_sem);
1653 if (!ext4_has_inline_data(dir)) {
1654 *has_inline_data = 0;
1655 goto out;
1656 }
1657
1658 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1659 EXT4_INLINE_DOTDOT_SIZE;
1660 inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
1661 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1662 dir, fname, 0, res_dir);
1663 if (ret == 1)
1664 goto out_find;
1665 if (ret < 0)
1666 goto out;
1667
1668 if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
1669 goto out;
1670
1671 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1672 inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
1673
1674 ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
1675 dir, fname, 0, res_dir);
1676 if (ret == 1)
1677 goto out_find;
1678
1679out:
1680 brelse(iloc.bh);
1681 iloc.bh = NULL;
1682out_find:
1683 up_read(&EXT4_I(dir)->xattr_sem);
1684 return iloc.bh;
1685}
1686
1687int ext4_delete_inline_entry(handle_t *handle,
1688 struct inode *dir,
1689 struct ext4_dir_entry_2 *de_del,
1690 struct buffer_head *bh,
1691 int *has_inline_data)
1692{
1693 int err, inline_size, no_expand;
1694 struct ext4_iloc iloc;
1695 void *inline_start;
1696
1697 err = ext4_get_inode_loc(dir, &iloc);
1698 if (err)
1699 return err;
1700
1701 ext4_write_lock_xattr(dir, &no_expand);
1702 if (!ext4_has_inline_data(dir)) {
1703 *has_inline_data = 0;
1704 goto out;
1705 }
1706
1707 if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
1708 EXT4_MIN_INLINE_DATA_SIZE) {
1709 inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
1710 EXT4_INLINE_DOTDOT_SIZE;
1711 inline_size = EXT4_MIN_INLINE_DATA_SIZE -
1712 EXT4_INLINE_DOTDOT_SIZE;
1713 } else {
1714 inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
1715 inline_size = ext4_get_inline_size(dir) -
1716 EXT4_MIN_INLINE_DATA_SIZE;
1717 }
1718
1719 BUFFER_TRACE(bh, "get_write_access");
1720 err = ext4_journal_get_write_access(handle, bh);
1721 if (err)
1722 goto out;
1723
1724 err = ext4_generic_delete_entry(handle, dir, de_del, bh,
1725 inline_start, inline_size, 0);
1726 if (err)
1727 goto out;
1728
1729 ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
1730out:
1731 ext4_write_unlock_xattr(dir, &no_expand);
1732 if (likely(err == 0))
1733 err = ext4_mark_inode_dirty(handle, dir);
1734 brelse(iloc.bh);
1735 if (err != -ENOENT)
1736 ext4_std_error(dir->i_sb, err);
1737 return err;
1738}
1739
1740/*
1741 * Get the inline dentry at offset.
1742 */
1743static inline struct ext4_dir_entry_2 *
1744ext4_get_inline_entry(struct inode *inode,
1745 struct ext4_iloc *iloc,
1746 unsigned int offset,
1747 void **inline_start,
1748 int *inline_size)
1749{
1750 void *inline_pos;
1751
1752 BUG_ON(offset > ext4_get_inline_size(inode));
1753
1754 if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
1755 inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
1756 *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
1757 } else {
1758 inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
1759 offset -= EXT4_MIN_INLINE_DATA_SIZE;
1760 *inline_size = ext4_get_inline_size(inode) -
1761 EXT4_MIN_INLINE_DATA_SIZE;
1762 }
1763
1764 if (inline_start)
1765 *inline_start = inline_pos;
1766 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
1767}
1768
1769bool empty_inline_dir(struct inode *dir, int *has_inline_data)
1770{
1771 int err, inline_size;
1772 struct ext4_iloc iloc;
1773 size_t inline_len;
1774 void *inline_pos;
1775 unsigned int offset;
1776 struct ext4_dir_entry_2 *de;
1777 bool ret = true;
1778
1779 err = ext4_get_inode_loc(dir, &iloc);
1780 if (err) {
1781 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
1782 err, dir->i_ino);
1783 return true;
1784 }
1785
1786 down_read(&EXT4_I(dir)->xattr_sem);
1787 if (!ext4_has_inline_data(dir)) {
1788 *has_inline_data = 0;
1789 goto out;
1790 }
1791
1792 de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
1793 if (!le32_to_cpu(de->inode)) {
1794 ext4_warning(dir->i_sb,
1795 "bad inline directory (dir #%lu) - no `..'",
1796 dir->i_ino);
1797 ret = true;
1798 goto out;
1799 }
1800
1801 inline_len = ext4_get_inline_size(dir);
1802 offset = EXT4_INLINE_DOTDOT_SIZE;
1803 while (offset < inline_len) {
1804 de = ext4_get_inline_entry(dir, &iloc, offset,
1805 &inline_pos, &inline_size);
1806 if (ext4_check_dir_entry(dir, NULL, de,
1807 iloc.bh, inline_pos,
1808 inline_size, offset)) {
1809 ext4_warning(dir->i_sb,
1810 "bad inline directory (dir #%lu) - "
1811 "inode %u, rec_len %u, name_len %d"
1812 "inline size %d",
1813 dir->i_ino, le32_to_cpu(de->inode),
1814 le16_to_cpu(de->rec_len), de->name_len,
1815 inline_size);
1816 ret = true;
1817 goto out;
1818 }
1819 if (le32_to_cpu(de->inode)) {
1820 ret = false;
1821 goto out;
1822 }
1823 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
1824 }
1825
1826out:
1827 up_read(&EXT4_I(dir)->xattr_sem);
1828 brelse(iloc.bh);
1829 return ret;
1830}
1831
1832int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
1833{
1834 int ret, no_expand;
1835
1836 ext4_write_lock_xattr(inode, &no_expand);
1837 ret = ext4_destroy_inline_data_nolock(handle, inode);
1838 ext4_write_unlock_xattr(inode, &no_expand);
1839
1840 return ret;
1841}
1842
1843int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
1844{
1845 __u64 addr;
1846 int error = -EAGAIN;
1847 struct ext4_iloc iloc;
1848
1849 down_read(&EXT4_I(inode)->xattr_sem);
1850 if (!ext4_has_inline_data(inode))
1851 goto out;
1852
1853 error = ext4_get_inode_loc(inode, &iloc);
1854 if (error)
1855 goto out;
1856
1857 addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1858 addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1859 addr += offsetof(struct ext4_inode, i_block);
1860
1861 brelse(iloc.bh);
1862
1863 iomap->addr = addr;
1864 iomap->offset = 0;
1865 iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
1866 i_size_read(inode));
1867 iomap->type = IOMAP_INLINE;
1868 iomap->flags = 0;
1869
1870out:
1871 up_read(&EXT4_I(inode)->xattr_sem);
1872 return error;
1873}
1874
1875int ext4_inline_data_fiemap(struct inode *inode,
1876 struct fiemap_extent_info *fieinfo,
1877 int *has_inline, __u64 start, __u64 len)
1878{
1879 __u64 physical = 0;
1880 __u64 inline_len;
1881 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
1882 FIEMAP_EXTENT_LAST;
1883 int error = 0;
1884 struct ext4_iloc iloc;
1885
1886 down_read(&EXT4_I(inode)->xattr_sem);
1887 if (!ext4_has_inline_data(inode)) {
1888 *has_inline = 0;
1889 goto out;
1890 }
1891 inline_len = min_t(size_t, ext4_get_inline_size(inode),
1892 i_size_read(inode));
1893 if (start >= inline_len)
1894 goto out;
1895 if (start + len < inline_len)
1896 inline_len = start + len;
1897 inline_len -= start;
1898
1899 error = ext4_get_inode_loc(inode, &iloc);
1900 if (error)
1901 goto out;
1902
1903 physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
1904 physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
1905 physical += offsetof(struct ext4_inode, i_block);
1906
1907 brelse(iloc.bh);
1908out:
1909 up_read(&EXT4_I(inode)->xattr_sem);
1910 if (physical)
1911 error = fiemap_fill_next_extent(fieinfo, start, physical,
1912 inline_len, flags);
1913 return (error < 0 ? error : 0);
1914}
1915
1916int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
1917{
1918 handle_t *handle;
1919 int inline_size, value_len, needed_blocks, no_expand, err = 0;
1920 size_t i_size;
1921 void *value = NULL;
1922 struct ext4_xattr_ibody_find is = {
1923 .s = { .not_found = -ENODATA, },
1924 };
1925 struct ext4_xattr_info i = {
1926 .name_index = EXT4_XATTR_INDEX_SYSTEM,
1927 .name = EXT4_XATTR_SYSTEM_DATA,
1928 };
1929
1930
1931 needed_blocks = ext4_writepage_trans_blocks(inode);
1932 handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
1933 if (IS_ERR(handle))
1934 return PTR_ERR(handle);
1935
1936 ext4_write_lock_xattr(inode, &no_expand);
1937 if (!ext4_has_inline_data(inode)) {
1938 *has_inline = 0;
1939 ext4_journal_stop(handle);
1940 return 0;
1941 }
1942
1943 if ((err = ext4_orphan_add(handle, inode)) != 0)
1944 goto out;
1945
1946 if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
1947 goto out;
1948
1949 down_write(&EXT4_I(inode)->i_data_sem);
1950 i_size = inode->i_size;
1951 inline_size = ext4_get_inline_size(inode);
1952 EXT4_I(inode)->i_disksize = i_size;
1953
1954 if (i_size < inline_size) {
1955 /* Clear the content in the xattr space. */
1956 if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
1957 if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
1958 goto out_error;
1959
1960 BUG_ON(is.s.not_found);
1961
1962 value_len = le32_to_cpu(is.s.here->e_value_size);
1963 value = kmalloc(value_len, GFP_NOFS);
1964 if (!value) {
1965 err = -ENOMEM;
1966 goto out_error;
1967 }
1968
1969 err = ext4_xattr_ibody_get(inode, i.name_index,
1970 i.name, value, value_len);
1971 if (err <= 0)
1972 goto out_error;
1973
1974 i.value = value;
1975 i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
1976 i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
1977 err = ext4_xattr_ibody_inline_set(handle, inode,
1978 &i, &is);
1979 if (err)
1980 goto out_error;
1981 }
1982
1983 /* Clear the content within i_blocks. */
1984 if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
1985 void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
1986 memset(p + i_size, 0,
1987 EXT4_MIN_INLINE_DATA_SIZE - i_size);
1988 }
1989
1990 EXT4_I(inode)->i_inline_size = i_size <
1991 EXT4_MIN_INLINE_DATA_SIZE ?
1992 EXT4_MIN_INLINE_DATA_SIZE : i_size;
1993 }
1994
1995out_error:
1996 up_write(&EXT4_I(inode)->i_data_sem);
1997out:
1998 brelse(is.iloc.bh);
1999 ext4_write_unlock_xattr(inode, &no_expand);
2000 kfree(value);
2001 if (inode->i_nlink)
2002 ext4_orphan_del(handle, inode);
2003
2004 if (err == 0) {
2005 inode->i_mtime = inode->i_ctime = current_time(inode);
2006 err = ext4_mark_inode_dirty(handle, inode);
2007 if (IS_SYNC(inode))
2008 ext4_handle_sync(handle);
2009 }
2010 ext4_journal_stop(handle);
2011 return err;
2012}
2013
2014int ext4_convert_inline_data(struct inode *inode)
2015{
2016 int error, needed_blocks, no_expand;
2017 handle_t *handle;
2018 struct ext4_iloc iloc;
2019
2020 if (!ext4_has_inline_data(inode)) {
2021 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
2022 return 0;
2023 }
2024
2025 needed_blocks = ext4_writepage_trans_blocks(inode);
2026
2027 iloc.bh = NULL;
2028 error = ext4_get_inode_loc(inode, &iloc);
2029 if (error)
2030 return error;
2031
2032 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
2033 if (IS_ERR(handle)) {
2034 error = PTR_ERR(handle);
2035 goto out_free;
2036 }
2037
2038 ext4_write_lock_xattr(inode, &no_expand);
2039 if (ext4_has_inline_data(inode))
2040 error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
2041 ext4_write_unlock_xattr(inode, &no_expand);
2042 ext4_journal_stop(handle);
2043out_free:
2044 brelse(iloc.bh);
2045 return error;
2046}