1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/fs.h>
23#include <linux/time.h>
24#include <linux/highuid.h>
25#include <linux/pagemap.h>
26#include <linux/dax.h>
27#include <linux/quotaops.h>
28#include <linux/string.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
31#include <linux/pagevec.h>
32#include <linux/mpage.h>
33#include <linux/namei.h>
34#include <linux/uio.h>
35#include <linux/bio.h>
36#include <linux/workqueue.h>
37#include <linux/kernel.h>
38#include <linux/printk.h>
39#include <linux/slab.h>
40#include <linux/bitops.h>
41#include <linux/iomap.h>
42#include <linux/iversion.h>
43
44#include "ext4_jbd2.h"
45#include "xattr.h"
46#include "acl.h"
47#include "truncate.h"
48
49#include <trace/events/ext4.h>
50
51static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
52 struct ext4_inode_info *ei)
53{
54 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
55 __u32 csum;
56 __u16 dummy_csum = 0;
57 int offset = offsetof(struct ext4_inode, i_checksum_lo);
58 unsigned int csum_size = sizeof(dummy_csum);
59
60 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
61 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
62 offset += csum_size;
63 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
64 EXT4_GOOD_OLD_INODE_SIZE - offset);
65
66 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
67 offset = offsetof(struct ext4_inode, i_checksum_hi);
68 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
69 EXT4_GOOD_OLD_INODE_SIZE,
70 offset - EXT4_GOOD_OLD_INODE_SIZE);
71 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
72 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
73 csum_size);
74 offset += csum_size;
75 }
76 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
77 EXT4_INODE_SIZE(inode->i_sb) - offset);
78 }
79
80 return csum;
81}
82
83static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
84 struct ext4_inode_info *ei)
85{
86 __u32 provided, calculated;
87
88 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
89 cpu_to_le32(EXT4_OS_LINUX) ||
90 !ext4_has_metadata_csum(inode->i_sb))
91 return 1;
92
93 provided = le16_to_cpu(raw->i_checksum_lo);
94 calculated = ext4_inode_csum(inode, raw, ei);
95 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
96 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
97 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
98 else
99 calculated &= 0xFFFF;
100
101 return provided == calculated;
102}
103
104static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
105 struct ext4_inode_info *ei)
106{
107 __u32 csum;
108
109 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
110 cpu_to_le32(EXT4_OS_LINUX) ||
111 !ext4_has_metadata_csum(inode->i_sb))
112 return;
113
114 csum = ext4_inode_csum(inode, raw, ei);
115 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
116 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
117 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
118 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
119}
120
121static inline int ext4_begin_ordered_truncate(struct inode *inode,
122 loff_t new_size)
123{
124 trace_ext4_begin_ordered_truncate(inode, new_size);
125
126
127
128
129
130
131 if (!EXT4_I(inode)->jinode)
132 return 0;
133 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
134 EXT4_I(inode)->jinode,
135 new_size);
136}
137
138static void ext4_invalidatepage(struct page *page, unsigned int offset,
139 unsigned int length);
140static int __ext4_journalled_writepage(struct page *page, unsigned int len);
141static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
142static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
143 int pextents);
144
145
146
147
148
149int ext4_inode_is_fast_symlink(struct inode *inode)
150{
151 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
152 int ea_blocks = EXT4_I(inode)->i_file_acl ?
153 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
154
155 if (ext4_has_inline_data(inode))
156 return 0;
157
158 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
159 }
160 return S_ISLNK(inode->i_mode) && inode->i_size &&
161 (inode->i_size < EXT4_N_BLOCKS * 4);
162}
163
164
165
166
167void ext4_evict_inode(struct inode *inode)
168{
169 handle_t *handle;
170 int err;
171
172
173
174
175
176 int extra_credits = 6;
177 struct ext4_xattr_inode_array *ea_inode_array = NULL;
178
179 trace_ext4_evict_inode(inode);
180
181 if (inode->i_nlink) {
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 if (inode->i_ino != EXT4_JOURNAL_INO &&
201 ext4_should_journal_data(inode) &&
202 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
203 inode->i_data.nrpages) {
204 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
205 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
206
207 jbd2_complete_transaction(journal, commit_tid);
208 filemap_write_and_wait(&inode->i_data);
209 }
210 truncate_inode_pages_final(&inode->i_data);
211
212 goto no_delete;
213 }
214
215 if (is_bad_inode(inode))
216 goto no_delete;
217 dquot_initialize(inode);
218
219 if (ext4_should_order_data(inode))
220 ext4_begin_ordered_truncate(inode, 0);
221 truncate_inode_pages_final(&inode->i_data);
222
223
224
225
226
227 sb_start_intwrite(inode->i_sb);
228
229 if (!IS_NOQUOTA(inode))
230 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
231
232
233
234
235
236 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
237 ext4_blocks_for_truncate(inode) + extra_credits - 3);
238 if (IS_ERR(handle)) {
239 ext4_std_error(inode->i_sb, PTR_ERR(handle));
240
241
242
243
244
245 ext4_orphan_del(NULL, inode);
246 sb_end_intwrite(inode->i_sb);
247 goto no_delete;
248 }
249
250 if (IS_SYNC(inode))
251 ext4_handle_sync(handle);
252
253
254
255
256
257
258
259
260 if (ext4_inode_is_fast_symlink(inode))
261 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
262 inode->i_size = 0;
263 err = ext4_mark_inode_dirty(handle, inode);
264 if (err) {
265 ext4_warning(inode->i_sb,
266 "couldn't mark inode dirty (err %d)", err);
267 goto stop_handle;
268 }
269 if (inode->i_blocks) {
270 err = ext4_truncate(inode);
271 if (err) {
272 ext4_error_err(inode->i_sb, -err,
273 "couldn't truncate inode %lu (err %d)",
274 inode->i_ino, err);
275 goto stop_handle;
276 }
277 }
278
279
280 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
281 extra_credits);
282 if (err) {
283 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
284stop_handle:
285 ext4_journal_stop(handle);
286 ext4_orphan_del(NULL, inode);
287 sb_end_intwrite(inode->i_sb);
288 ext4_xattr_inode_array_free(ea_inode_array);
289 goto no_delete;
290 }
291
292
293
294
295
296
297
298
299
300 ext4_orphan_del(handle, inode);
301 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
302
303
304
305
306
307
308
309
310 if (ext4_mark_inode_dirty(handle, inode))
311
312 ext4_clear_inode(inode);
313 else
314 ext4_free_inode(handle, inode);
315 ext4_journal_stop(handle);
316 sb_end_intwrite(inode->i_sb);
317 ext4_xattr_inode_array_free(ea_inode_array);
318 return;
319no_delete:
320 ext4_clear_inode(inode);
321}
322
323#ifdef CONFIG_QUOTA
324qsize_t *ext4_get_reserved_space(struct inode *inode)
325{
326 return &EXT4_I(inode)->i_reserved_quota;
327}
328#endif
329
330
331
332
333
334void ext4_da_update_reserve_space(struct inode *inode,
335 int used, int quota_claim)
336{
337 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
338 struct ext4_inode_info *ei = EXT4_I(inode);
339
340 spin_lock(&ei->i_block_reservation_lock);
341 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
342 if (unlikely(used > ei->i_reserved_data_blocks)) {
343 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
344 "with only %d reserved data blocks",
345 __func__, inode->i_ino, used,
346 ei->i_reserved_data_blocks);
347 WARN_ON(1);
348 used = ei->i_reserved_data_blocks;
349 }
350
351
352 ei->i_reserved_data_blocks -= used;
353 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
354
355 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
356
357
358 if (quota_claim)
359 dquot_claim_block(inode, EXT4_C2B(sbi, used));
360 else {
361
362
363
364
365
366 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
367 }
368
369
370
371
372
373
374 if ((ei->i_reserved_data_blocks == 0) &&
375 !inode_is_open_for_write(inode))
376 ext4_discard_preallocations(inode);
377}
378
379static int __check_block_validity(struct inode *inode, const char *func,
380 unsigned int line,
381 struct ext4_map_blocks *map)
382{
383 if (ext4_has_feature_journal(inode->i_sb) &&
384 (inode->i_ino ==
385 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
386 return 0;
387 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
388 map->m_len)) {
389 ext4_error_inode(inode, func, line, map->m_pblk,
390 "lblock %lu mapped to illegal pblock %llu "
391 "(length %d)", (unsigned long) map->m_lblk,
392 map->m_pblk, map->m_len);
393 return -EFSCORRUPTED;
394 }
395 return 0;
396}
397
398int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
399 ext4_lblk_t len)
400{
401 int ret;
402
403 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
404 return fscrypt_zeroout_range(inode, lblk, pblk, len);
405
406 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
407 if (ret > 0)
408 ret = 0;
409
410 return ret;
411}
412
413#define check_block_validity(inode, map) \
414 __check_block_validity((inode), __func__, __LINE__, (map))
415
416#ifdef ES_AGGRESSIVE_TEST
417static void ext4_map_blocks_es_recheck(handle_t *handle,
418 struct inode *inode,
419 struct ext4_map_blocks *es_map,
420 struct ext4_map_blocks *map,
421 int flags)
422{
423 int retval;
424
425 map->m_flags = 0;
426
427
428
429
430
431
432
433 down_read(&EXT4_I(inode)->i_data_sem);
434 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
435 retval = ext4_ext_map_blocks(handle, inode, map, flags &
436 EXT4_GET_BLOCKS_KEEP_SIZE);
437 } else {
438 retval = ext4_ind_map_blocks(handle, inode, map, flags &
439 EXT4_GET_BLOCKS_KEEP_SIZE);
440 }
441 up_read((&EXT4_I(inode)->i_data_sem));
442
443
444
445
446
447 if (es_map->m_lblk != map->m_lblk ||
448 es_map->m_flags != map->m_flags ||
449 es_map->m_pblk != map->m_pblk) {
450 printk("ES cache assertion failed for inode: %lu "
451 "es_cached ex [%d/%d/%llu/%x] != "
452 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
453 inode->i_ino, es_map->m_lblk, es_map->m_len,
454 es_map->m_pblk, es_map->m_flags, map->m_lblk,
455 map->m_len, map->m_pblk, map->m_flags,
456 retval, flags);
457 }
458}
459#endif
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483int ext4_map_blocks(handle_t *handle, struct inode *inode,
484 struct ext4_map_blocks *map, int flags)
485{
486 struct extent_status es;
487 int retval;
488 int ret = 0;
489#ifdef ES_AGGRESSIVE_TEST
490 struct ext4_map_blocks orig_map;
491
492 memcpy(&orig_map, map, sizeof(*map));
493#endif
494
495 map->m_flags = 0;
496 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
497 "logical block %lu\n", inode->i_ino, flags, map->m_len,
498 (unsigned long) map->m_lblk);
499
500
501
502
503 if (unlikely(map->m_len > INT_MAX))
504 map->m_len = INT_MAX;
505
506
507 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
508 return -EFSCORRUPTED;
509
510
511 if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
512 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
513 map->m_pblk = ext4_es_pblock(&es) +
514 map->m_lblk - es.es_lblk;
515 map->m_flags |= ext4_es_is_written(&es) ?
516 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
517 retval = es.es_len - (map->m_lblk - es.es_lblk);
518 if (retval > map->m_len)
519 retval = map->m_len;
520 map->m_len = retval;
521 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
522 map->m_pblk = 0;
523 retval = es.es_len - (map->m_lblk - es.es_lblk);
524 if (retval > map->m_len)
525 retval = map->m_len;
526 map->m_len = retval;
527 retval = 0;
528 } else {
529 BUG();
530 }
531#ifdef ES_AGGRESSIVE_TEST
532 ext4_map_blocks_es_recheck(handle, inode, map,
533 &orig_map, flags);
534#endif
535 goto found;
536 }
537
538
539
540
541
542 down_read(&EXT4_I(inode)->i_data_sem);
543 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
544 retval = ext4_ext_map_blocks(handle, inode, map, flags &
545 EXT4_GET_BLOCKS_KEEP_SIZE);
546 } else {
547 retval = ext4_ind_map_blocks(handle, inode, map, flags &
548 EXT4_GET_BLOCKS_KEEP_SIZE);
549 }
550 if (retval > 0) {
551 unsigned int status;
552
553 if (unlikely(retval != map->m_len)) {
554 ext4_warning(inode->i_sb,
555 "ES len assertion failed for inode "
556 "%lu: retval %d != map->m_len %d",
557 inode->i_ino, retval, map->m_len);
558 WARN_ON(1);
559 }
560
561 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
562 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
563 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
564 !(status & EXTENT_STATUS_WRITTEN) &&
565 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
566 map->m_lblk + map->m_len - 1))
567 status |= EXTENT_STATUS_DELAYED;
568 ret = ext4_es_insert_extent(inode, map->m_lblk,
569 map->m_len, map->m_pblk, status);
570 if (ret < 0)
571 retval = ret;
572 }
573 up_read((&EXT4_I(inode)->i_data_sem));
574
575found:
576 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
577 ret = check_block_validity(inode, map);
578 if (ret != 0)
579 return ret;
580 }
581
582
583 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
584 return retval;
585
586
587
588
589
590
591
592
593 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
594
595
596
597
598
599 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
600 return retval;
601
602
603
604
605
606 map->m_flags &= ~EXT4_MAP_FLAGS;
607
608
609
610
611
612
613
614 down_write(&EXT4_I(inode)->i_data_sem);
615
616
617
618
619
620 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
621 retval = ext4_ext_map_blocks(handle, inode, map, flags);
622 } else {
623 retval = ext4_ind_map_blocks(handle, inode, map, flags);
624
625 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
626
627
628
629
630
631 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
632 }
633
634
635
636
637
638
639
640 if ((retval > 0) &&
641 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
642 ext4_da_update_reserve_space(inode, retval, 1);
643 }
644
645 if (retval > 0) {
646 unsigned int status;
647
648 if (unlikely(retval != map->m_len)) {
649 ext4_warning(inode->i_sb,
650 "ES len assertion failed for inode "
651 "%lu: retval %d != map->m_len %d",
652 inode->i_ino, retval, map->m_len);
653 WARN_ON(1);
654 }
655
656
657
658
659
660
661
662
663 if (flags & EXT4_GET_BLOCKS_ZERO &&
664 map->m_flags & EXT4_MAP_MAPPED &&
665 map->m_flags & EXT4_MAP_NEW) {
666 ret = ext4_issue_zeroout(inode, map->m_lblk,
667 map->m_pblk, map->m_len);
668 if (ret) {
669 retval = ret;
670 goto out_sem;
671 }
672 }
673
674
675
676
677
678 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
679 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
680 if (ext4_es_is_written(&es))
681 goto out_sem;
682 }
683 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
684 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
685 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
686 !(status & EXTENT_STATUS_WRITTEN) &&
687 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
688 map->m_lblk + map->m_len - 1))
689 status |= EXTENT_STATUS_DELAYED;
690 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
691 map->m_pblk, status);
692 if (ret < 0) {
693 retval = ret;
694 goto out_sem;
695 }
696 }
697
698out_sem:
699 up_write((&EXT4_I(inode)->i_data_sem));
700 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
701 ret = check_block_validity(inode, map);
702 if (ret != 0)
703 return ret;
704
705
706
707
708
709
710 if (map->m_flags & EXT4_MAP_NEW &&
711 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
712 !(flags & EXT4_GET_BLOCKS_ZERO) &&
713 !ext4_is_quota_file(inode) &&
714 ext4_should_order_data(inode)) {
715 loff_t start_byte =
716 (loff_t)map->m_lblk << inode->i_blkbits;
717 loff_t length = (loff_t)map->m_len << inode->i_blkbits;
718
719 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
720 ret = ext4_jbd2_inode_add_wait(handle, inode,
721 start_byte, length);
722 else
723 ret = ext4_jbd2_inode_add_write(handle, inode,
724 start_byte, length);
725 if (ret)
726 return ret;
727 }
728 }
729 return retval;
730}
731
732
733
734
735
736static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
737{
738 unsigned long old_state;
739 unsigned long new_state;
740
741 flags &= EXT4_MAP_FLAGS;
742
743
744 if (!bh->b_page) {
745 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
746 return;
747 }
748
749
750
751
752
753 do {
754 old_state = READ_ONCE(bh->b_state);
755 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
756 } while (unlikely(
757 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
758}
759
760static int _ext4_get_block(struct inode *inode, sector_t iblock,
761 struct buffer_head *bh, int flags)
762{
763 struct ext4_map_blocks map;
764 int ret = 0;
765
766 if (ext4_has_inline_data(inode))
767 return -ERANGE;
768
769 map.m_lblk = iblock;
770 map.m_len = bh->b_size >> inode->i_blkbits;
771
772 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
773 flags);
774 if (ret > 0) {
775 map_bh(bh, inode->i_sb, map.m_pblk);
776 ext4_update_bh_state(bh, map.m_flags);
777 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
778 ret = 0;
779 } else if (ret == 0) {
780
781 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
782 }
783 return ret;
784}
785
786int ext4_get_block(struct inode *inode, sector_t iblock,
787 struct buffer_head *bh, int create)
788{
789 return _ext4_get_block(inode, iblock, bh,
790 create ? EXT4_GET_BLOCKS_CREATE : 0);
791}
792
793
794
795
796
797
798int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
799 struct buffer_head *bh_result, int create)
800{
801 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
802 inode->i_ino, create);
803 return _ext4_get_block(inode, iblock, bh_result,
804 EXT4_GET_BLOCKS_IO_CREATE_EXT);
805}
806
807
808#define DIO_MAX_BLOCKS 4096
809
810
811
812
813struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
814 ext4_lblk_t block, int map_flags)
815{
816 struct ext4_map_blocks map;
817 struct buffer_head *bh;
818 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
819 int err;
820
821 J_ASSERT(handle != NULL || create == 0);
822
823 map.m_lblk = block;
824 map.m_len = 1;
825 err = ext4_map_blocks(handle, inode, &map, map_flags);
826
827 if (err == 0)
828 return create ? ERR_PTR(-ENOSPC) : NULL;
829 if (err < 0)
830 return ERR_PTR(err);
831
832 bh = sb_getblk(inode->i_sb, map.m_pblk);
833 if (unlikely(!bh))
834 return ERR_PTR(-ENOMEM);
835 if (map.m_flags & EXT4_MAP_NEW) {
836 J_ASSERT(create != 0);
837 J_ASSERT(handle != NULL);
838
839
840
841
842
843
844
845
846 lock_buffer(bh);
847 BUFFER_TRACE(bh, "call get_create_access");
848 err = ext4_journal_get_create_access(handle, bh);
849 if (unlikely(err)) {
850 unlock_buffer(bh);
851 goto errout;
852 }
853 if (!buffer_uptodate(bh)) {
854 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
855 set_buffer_uptodate(bh);
856 }
857 unlock_buffer(bh);
858 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
859 err = ext4_handle_dirty_metadata(handle, inode, bh);
860 if (unlikely(err))
861 goto errout;
862 } else
863 BUFFER_TRACE(bh, "not a new buffer");
864 return bh;
865errout:
866 brelse(bh);
867 return ERR_PTR(err);
868}
869
870struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
871 ext4_lblk_t block, int map_flags)
872{
873 struct buffer_head *bh;
874
875 bh = ext4_getblk(handle, inode, block, map_flags);
876 if (IS_ERR(bh))
877 return bh;
878 if (!bh || ext4_buffer_uptodate(bh))
879 return bh;
880 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
881 wait_on_buffer(bh);
882 if (buffer_uptodate(bh))
883 return bh;
884 put_bh(bh);
885 return ERR_PTR(-EIO);
886}
887
888
889int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
890 bool wait, struct buffer_head **bhs)
891{
892 int i, err;
893
894 for (i = 0; i < bh_count; i++) {
895 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 );
896 if (IS_ERR(bhs[i])) {
897 err = PTR_ERR(bhs[i]);
898 bh_count = i;
899 goto out_brelse;
900 }
901 }
902
903 for (i = 0; i < bh_count; i++)
904
905 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
906 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1,
907 &bhs[i]);
908
909 if (!wait)
910 return 0;
911
912 for (i = 0; i < bh_count; i++)
913 if (bhs[i])
914 wait_on_buffer(bhs[i]);
915
916 for (i = 0; i < bh_count; i++) {
917 if (bhs[i] && !buffer_uptodate(bhs[i])) {
918 err = -EIO;
919 goto out_brelse;
920 }
921 }
922 return 0;
923
924out_brelse:
925 for (i = 0; i < bh_count; i++) {
926 brelse(bhs[i]);
927 bhs[i] = NULL;
928 }
929 return err;
930}
931
932int ext4_walk_page_buffers(handle_t *handle,
933 struct buffer_head *head,
934 unsigned from,
935 unsigned to,
936 int *partial,
937 int (*fn)(handle_t *handle,
938 struct buffer_head *bh))
939{
940 struct buffer_head *bh;
941 unsigned block_start, block_end;
942 unsigned blocksize = head->b_size;
943 int err, ret = 0;
944 struct buffer_head *next;
945
946 for (bh = head, block_start = 0;
947 ret == 0 && (bh != head || !block_start);
948 block_start = block_end, bh = next) {
949 next = bh->b_this_page;
950 block_end = block_start + blocksize;
951 if (block_end <= from || block_start >= to) {
952 if (partial && !buffer_uptodate(bh))
953 *partial = 1;
954 continue;
955 }
956 err = (*fn)(handle, bh);
957 if (!ret)
958 ret = err;
959 }
960 return ret;
961}
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987int do_journal_get_write_access(handle_t *handle,
988 struct buffer_head *bh)
989{
990 int dirty = buffer_dirty(bh);
991 int ret;
992
993 if (!buffer_mapped(bh) || buffer_freed(bh))
994 return 0;
995
996
997
998
999
1000
1001
1002
1003 if (dirty)
1004 clear_buffer_dirty(bh);
1005 BUFFER_TRACE(bh, "get write access");
1006 ret = ext4_journal_get_write_access(handle, bh);
1007 if (!ret && dirty)
1008 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1009 return ret;
1010}
1011
1012#ifdef CONFIG_FS_ENCRYPTION
1013static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1014 get_block_t *get_block)
1015{
1016 unsigned from = pos & (PAGE_SIZE - 1);
1017 unsigned to = from + len;
1018 struct inode *inode = page->mapping->host;
1019 unsigned block_start, block_end;
1020 sector_t block;
1021 int err = 0;
1022 unsigned blocksize = inode->i_sb->s_blocksize;
1023 unsigned bbits;
1024 struct buffer_head *bh, *head, *wait[2];
1025 int nr_wait = 0;
1026 int i;
1027
1028 BUG_ON(!PageLocked(page));
1029 BUG_ON(from > PAGE_SIZE);
1030 BUG_ON(to > PAGE_SIZE);
1031 BUG_ON(from > to);
1032
1033 if (!page_has_buffers(page))
1034 create_empty_buffers(page, blocksize, 0);
1035 head = page_buffers(page);
1036 bbits = ilog2(blocksize);
1037 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
1038
1039 for (bh = head, block_start = 0; bh != head || !block_start;
1040 block++, block_start = block_end, bh = bh->b_this_page) {
1041 block_end = block_start + blocksize;
1042 if (block_end <= from || block_start >= to) {
1043 if (PageUptodate(page)) {
1044 if (!buffer_uptodate(bh))
1045 set_buffer_uptodate(bh);
1046 }
1047 continue;
1048 }
1049 if (buffer_new(bh))
1050 clear_buffer_new(bh);
1051 if (!buffer_mapped(bh)) {
1052 WARN_ON(bh->b_size != blocksize);
1053 err = get_block(inode, block, bh, 1);
1054 if (err)
1055 break;
1056 if (buffer_new(bh)) {
1057 if (PageUptodate(page)) {
1058 clear_buffer_new(bh);
1059 set_buffer_uptodate(bh);
1060 mark_buffer_dirty(bh);
1061 continue;
1062 }
1063 if (block_end > to || block_start < from)
1064 zero_user_segments(page, to, block_end,
1065 block_start, from);
1066 continue;
1067 }
1068 }
1069 if (PageUptodate(page)) {
1070 if (!buffer_uptodate(bh))
1071 set_buffer_uptodate(bh);
1072 continue;
1073 }
1074 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1075 !buffer_unwritten(bh) &&
1076 (block_start < from || block_end > to)) {
1077 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1078 wait[nr_wait++] = bh;
1079 }
1080 }
1081
1082
1083
1084 for (i = 0; i < nr_wait; i++) {
1085 wait_on_buffer(wait[i]);
1086 if (!buffer_uptodate(wait[i]))
1087 err = -EIO;
1088 }
1089 if (unlikely(err)) {
1090 page_zero_new_buffers(page, from, to);
1091 } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
1092 for (i = 0; i < nr_wait; i++) {
1093 int err2;
1094
1095 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
1096 bh_offset(wait[i]));
1097 if (err2) {
1098 clear_buffer_uptodate(wait[i]);
1099 err = err2;
1100 }
1101 }
1102 }
1103
1104 return err;
1105}
1106#endif
1107
1108static int ext4_write_begin(struct file *file, struct address_space *mapping,
1109 loff_t pos, unsigned len, unsigned flags,
1110 struct page **pagep, void **fsdata)
1111{
1112 struct inode *inode = mapping->host;
1113 int ret, needed_blocks;
1114 handle_t *handle;
1115 int retries = 0;
1116 struct page *page;
1117 pgoff_t index;
1118 unsigned from, to;
1119
1120 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
1121 return -EIO;
1122
1123 trace_ext4_write_begin(inode, pos, len, flags);
1124
1125
1126
1127
1128 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
1129 index = pos >> PAGE_SHIFT;
1130 from = pos & (PAGE_SIZE - 1);
1131 to = from + len;
1132
1133 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1134 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1135 flags, pagep);
1136 if (ret < 0)
1137 return ret;
1138 if (ret == 1)
1139 return 0;
1140 }
1141
1142
1143
1144
1145
1146
1147
1148
1149retry_grab:
1150 page = grab_cache_page_write_begin(mapping, index, flags);
1151 if (!page)
1152 return -ENOMEM;
1153 unlock_page(page);
1154
1155retry_journal:
1156 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
1157 if (IS_ERR(handle)) {
1158 put_page(page);
1159 return PTR_ERR(handle);
1160 }
1161
1162 lock_page(page);
1163 if (page->mapping != mapping) {
1164
1165 unlock_page(page);
1166 put_page(page);
1167 ext4_journal_stop(handle);
1168 goto retry_grab;
1169 }
1170
1171 wait_for_stable_page(page);
1172
1173#ifdef CONFIG_FS_ENCRYPTION
1174 if (ext4_should_dioread_nolock(inode))
1175 ret = ext4_block_write_begin(page, pos, len,
1176 ext4_get_block_unwritten);
1177 else
1178 ret = ext4_block_write_begin(page, pos, len,
1179 ext4_get_block);
1180#else
1181 if (ext4_should_dioread_nolock(inode))
1182 ret = __block_write_begin(page, pos, len,
1183 ext4_get_block_unwritten);
1184 else
1185 ret = __block_write_begin(page, pos, len, ext4_get_block);
1186#endif
1187 if (!ret && ext4_should_journal_data(inode)) {
1188 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1189 from, to, NULL,
1190 do_journal_get_write_access);
1191 }
1192
1193 if (ret) {
1194 bool extended = (pos + len > inode->i_size) &&
1195 !ext4_verity_in_progress(inode);
1196
1197 unlock_page(page);
1198
1199
1200
1201
1202
1203
1204
1205
1206 if (extended && ext4_can_truncate(inode))
1207 ext4_orphan_add(handle, inode);
1208
1209 ext4_journal_stop(handle);
1210 if (extended) {
1211 ext4_truncate_failed_write(inode);
1212
1213
1214
1215
1216
1217
1218 if (inode->i_nlink)
1219 ext4_orphan_del(NULL, inode);
1220 }
1221
1222 if (ret == -ENOSPC &&
1223 ext4_should_retry_alloc(inode->i_sb, &retries))
1224 goto retry_journal;
1225 put_page(page);
1226 return ret;
1227 }
1228 *pagep = page;
1229 return ret;
1230}
1231
1232
1233static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1234{
1235 int ret;
1236 if (!buffer_mapped(bh) || buffer_freed(bh))
1237 return 0;
1238 set_buffer_uptodate(bh);
1239 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1240 clear_buffer_meta(bh);
1241 clear_buffer_prio(bh);
1242 return ret;
1243}
1244
1245
1246
1247
1248
1249
1250
1251
1252static int ext4_write_end(struct file *file,
1253 struct address_space *mapping,
1254 loff_t pos, unsigned len, unsigned copied,
1255 struct page *page, void *fsdata)
1256{
1257 handle_t *handle = ext4_journal_current_handle();
1258 struct inode *inode = mapping->host;
1259 loff_t old_size = inode->i_size;
1260 int ret = 0, ret2;
1261 int i_size_changed = 0;
1262 int inline_data = ext4_has_inline_data(inode);
1263 bool verity = ext4_verity_in_progress(inode);
1264
1265 trace_ext4_write_end(inode, pos, len, copied);
1266 if (inline_data) {
1267 ret = ext4_write_inline_data_end(inode, pos, len,
1268 copied, page);
1269 if (ret < 0) {
1270 unlock_page(page);
1271 put_page(page);
1272 goto errout;
1273 }
1274 copied = ret;
1275 } else
1276 copied = block_write_end(file, mapping, pos,
1277 len, copied, page, fsdata);
1278
1279
1280
1281
1282
1283
1284
1285 if (!verity)
1286 i_size_changed = ext4_update_inode_size(inode, pos + copied);
1287 unlock_page(page);
1288 put_page(page);
1289
1290 if (old_size < pos && !verity)
1291 pagecache_isize_extended(inode, old_size, pos);
1292
1293
1294
1295
1296
1297
1298 if (i_size_changed || inline_data)
1299 ext4_mark_inode_dirty(handle, inode);
1300
1301 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
1302
1303
1304
1305
1306 ext4_orphan_add(handle, inode);
1307errout:
1308 ret2 = ext4_journal_stop(handle);
1309 if (!ret)
1310 ret = ret2;
1311
1312 if (pos + len > inode->i_size && !verity) {
1313 ext4_truncate_failed_write(inode);
1314
1315
1316
1317
1318
1319 if (inode->i_nlink)
1320 ext4_orphan_del(NULL, inode);
1321 }
1322
1323 return ret ? ret : copied;
1324}
1325
1326
1327
1328
1329
1330
1331static void ext4_journalled_zero_new_buffers(handle_t *handle,
1332 struct page *page,
1333 unsigned from, unsigned to)
1334{
1335 unsigned int block_start = 0, block_end;
1336 struct buffer_head *head, *bh;
1337
1338 bh = head = page_buffers(page);
1339 do {
1340 block_end = block_start + bh->b_size;
1341 if (buffer_new(bh)) {
1342 if (block_end > from && block_start < to) {
1343 if (!PageUptodate(page)) {
1344 unsigned start, size;
1345
1346 start = max(from, block_start);
1347 size = min(to, block_end) - start;
1348
1349 zero_user(page, start, size);
1350 write_end_fn(handle, bh);
1351 }
1352 clear_buffer_new(bh);
1353 }
1354 }
1355 block_start = block_end;
1356 bh = bh->b_this_page;
1357 } while (bh != head);
1358}
1359
1360static int ext4_journalled_write_end(struct file *file,
1361 struct address_space *mapping,
1362 loff_t pos, unsigned len, unsigned copied,
1363 struct page *page, void *fsdata)
1364{
1365 handle_t *handle = ext4_journal_current_handle();
1366 struct inode *inode = mapping->host;
1367 loff_t old_size = inode->i_size;
1368 int ret = 0, ret2;
1369 int partial = 0;
1370 unsigned from, to;
1371 int size_changed = 0;
1372 int inline_data = ext4_has_inline_data(inode);
1373 bool verity = ext4_verity_in_progress(inode);
1374
1375 trace_ext4_journalled_write_end(inode, pos, len, copied);
1376 from = pos & (PAGE_SIZE - 1);
1377 to = from + len;
1378
1379 BUG_ON(!ext4_handle_valid(handle));
1380
1381 if (inline_data) {
1382 ret = ext4_write_inline_data_end(inode, pos, len,
1383 copied, page);
1384 if (ret < 0) {
1385 unlock_page(page);
1386 put_page(page);
1387 goto errout;
1388 }
1389 copied = ret;
1390 } else if (unlikely(copied < len) && !PageUptodate(page)) {
1391 copied = 0;
1392 ext4_journalled_zero_new_buffers(handle, page, from, to);
1393 } else {
1394 if (unlikely(copied < len))
1395 ext4_journalled_zero_new_buffers(handle, page,
1396 from + copied, to);
1397 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1398 from + copied, &partial,
1399 write_end_fn);
1400 if (!partial)
1401 SetPageUptodate(page);
1402 }
1403 if (!verity)
1404 size_changed = ext4_update_inode_size(inode, pos + copied);
1405 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
1406 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
1407 unlock_page(page);
1408 put_page(page);
1409
1410 if (old_size < pos && !verity)
1411 pagecache_isize_extended(inode, old_size, pos);
1412
1413 if (size_changed || inline_data) {
1414 ret2 = ext4_mark_inode_dirty(handle, inode);
1415 if (!ret)
1416 ret = ret2;
1417 }
1418
1419 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
1420
1421
1422
1423
1424 ext4_orphan_add(handle, inode);
1425
1426errout:
1427 ret2 = ext4_journal_stop(handle);
1428 if (!ret)
1429 ret = ret2;
1430 if (pos + len > inode->i_size && !verity) {
1431 ext4_truncate_failed_write(inode);
1432
1433
1434
1435
1436
1437 if (inode->i_nlink)
1438 ext4_orphan_del(NULL, inode);
1439 }
1440
1441 return ret ? ret : copied;
1442}
1443
1444
1445
1446
1447static int ext4_da_reserve_space(struct inode *inode)
1448{
1449 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1450 struct ext4_inode_info *ei = EXT4_I(inode);
1451 int ret;
1452
1453
1454
1455
1456
1457
1458 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
1459 if (ret)
1460 return ret;
1461
1462 spin_lock(&ei->i_block_reservation_lock);
1463 if (ext4_claim_free_clusters(sbi, 1, 0)) {
1464 spin_unlock(&ei->i_block_reservation_lock);
1465 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
1466 return -ENOSPC;
1467 }
1468 ei->i_reserved_data_blocks++;
1469 trace_ext4_da_reserve_space(inode);
1470 spin_unlock(&ei->i_block_reservation_lock);
1471
1472 return 0;
1473}
1474
1475void ext4_da_release_space(struct inode *inode, int to_free)
1476{
1477 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1478 struct ext4_inode_info *ei = EXT4_I(inode);
1479
1480 if (!to_free)
1481 return;
1482
1483 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1484
1485 trace_ext4_da_release_space(inode, to_free);
1486 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
1487
1488
1489
1490
1491
1492
1493 ext4_warning(inode->i_sb, "ext4_da_release_space: "
1494 "ino %lu, to_free %d with only %d reserved "
1495 "data blocks", inode->i_ino, to_free,
1496 ei->i_reserved_data_blocks);
1497 WARN_ON(1);
1498 to_free = ei->i_reserved_data_blocks;
1499 }
1500 ei->i_reserved_data_blocks -= to_free;
1501
1502
1503 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
1504
1505 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1506
1507 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
1508}
1509
1510
1511
1512
1513
1514struct mpage_da_data {
1515 struct inode *inode;
1516 struct writeback_control *wbc;
1517
1518 pgoff_t first_page;
1519 pgoff_t next_page;
1520 pgoff_t last_page;
1521
1522
1523
1524
1525
1526 struct ext4_map_blocks map;
1527 struct ext4_io_submit io_submit;
1528 unsigned int do_map:1;
1529};
1530
1531static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1532 bool invalidate)
1533{
1534 int nr_pages, i;
1535 pgoff_t index, end;
1536 struct pagevec pvec;
1537 struct inode *inode = mpd->inode;
1538 struct address_space *mapping = inode->i_mapping;
1539
1540
1541 if (mpd->first_page >= mpd->next_page)
1542 return;
1543
1544 index = mpd->first_page;
1545 end = mpd->next_page - 1;
1546 if (invalidate) {
1547 ext4_lblk_t start, last;
1548 start = index << (PAGE_SHIFT - inode->i_blkbits);
1549 last = end << (PAGE_SHIFT - inode->i_blkbits);
1550 ext4_es_remove_extent(inode, start, last - start + 1);
1551 }
1552
1553 pagevec_init(&pvec);
1554 while (index <= end) {
1555 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
1556 if (nr_pages == 0)
1557 break;
1558 for (i = 0; i < nr_pages; i++) {
1559 struct page *page = pvec.pages[i];
1560
1561 BUG_ON(!PageLocked(page));
1562 BUG_ON(PageWriteback(page));
1563 if (invalidate) {
1564 if (page_mapped(page))
1565 clear_page_dirty_for_io(page);
1566 block_invalidatepage(page, 0, PAGE_SIZE);
1567 ClearPageUptodate(page);
1568 }
1569 unlock_page(page);
1570 }
1571 pagevec_release(&pvec);
1572 }
1573}
1574
1575static void ext4_print_free_blocks(struct inode *inode)
1576{
1577 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1578 struct super_block *sb = inode->i_sb;
1579 struct ext4_inode_info *ei = EXT4_I(inode);
1580
1581 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
1582 EXT4_C2B(EXT4_SB(inode->i_sb),
1583 ext4_count_free_clusters(sb)));
1584 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1585 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
1586 (long long) EXT4_C2B(EXT4_SB(sb),
1587 percpu_counter_sum(&sbi->s_freeclusters_counter)));
1588 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
1589 (long long) EXT4_C2B(EXT4_SB(sb),
1590 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
1591 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1592 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
1593 ei->i_reserved_data_blocks);
1594 return;
1595}
1596
1597static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
1598{
1599 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
1600}
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
1614{
1615 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1616 int ret;
1617 bool allocated = false;
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630 if (sbi->s_cluster_ratio == 1) {
1631 ret = ext4_da_reserve_space(inode);
1632 if (ret != 0)
1633 goto errout;
1634 } else {
1635 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
1636 if (!ext4_es_scan_clu(inode,
1637 &ext4_es_is_mapped, lblk)) {
1638 ret = ext4_clu_mapped(inode,
1639 EXT4_B2C(sbi, lblk));
1640 if (ret < 0)
1641 goto errout;
1642 if (ret == 0) {
1643 ret = ext4_da_reserve_space(inode);
1644 if (ret != 0)
1645 goto errout;
1646 } else {
1647 allocated = true;
1648 }
1649 } else {
1650 allocated = true;
1651 }
1652 }
1653 }
1654
1655 ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
1656
1657errout:
1658 return ret;
1659}
1660
1661
1662
1663
1664
1665
1666
1667static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1668 struct ext4_map_blocks *map,
1669 struct buffer_head *bh)
1670{
1671 struct extent_status es;
1672 int retval;
1673 sector_t invalid_block = ~((sector_t) 0xffff);
1674#ifdef ES_AGGRESSIVE_TEST
1675 struct ext4_map_blocks orig_map;
1676
1677 memcpy(&orig_map, map, sizeof(*map));
1678#endif
1679
1680 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1681 invalid_block = ~0;
1682
1683 map->m_flags = 0;
1684 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1685 "logical block %lu\n", inode->i_ino, map->m_len,
1686 (unsigned long) map->m_lblk);
1687
1688
1689 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
1690 if (ext4_es_is_hole(&es)) {
1691 retval = 0;
1692 down_read(&EXT4_I(inode)->i_data_sem);
1693 goto add_delayed;
1694 }
1695
1696
1697
1698
1699
1700 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1701 map_bh(bh, inode->i_sb, invalid_block);
1702 set_buffer_new(bh);
1703 set_buffer_delay(bh);
1704 return 0;
1705 }
1706
1707 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1708 retval = es.es_len - (iblock - es.es_lblk);
1709 if (retval > map->m_len)
1710 retval = map->m_len;
1711 map->m_len = retval;
1712 if (ext4_es_is_written(&es))
1713 map->m_flags |= EXT4_MAP_MAPPED;
1714 else if (ext4_es_is_unwritten(&es))
1715 map->m_flags |= EXT4_MAP_UNWRITTEN;
1716 else
1717 BUG();
1718
1719#ifdef ES_AGGRESSIVE_TEST
1720 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1721#endif
1722 return retval;
1723 }
1724
1725
1726
1727
1728
1729 down_read(&EXT4_I(inode)->i_data_sem);
1730 if (ext4_has_inline_data(inode))
1731 retval = 0;
1732 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
1733 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
1734 else
1735 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
1736
1737add_delayed:
1738 if (retval == 0) {
1739 int ret;
1740
1741
1742
1743
1744
1745
1746 ret = ext4_insert_delayed_block(inode, map->m_lblk);
1747 if (ret != 0) {
1748 retval = ret;
1749 goto out_unlock;
1750 }
1751
1752 map_bh(bh, inode->i_sb, invalid_block);
1753 set_buffer_new(bh);
1754 set_buffer_delay(bh);
1755 } else if (retval > 0) {
1756 int ret;
1757 unsigned int status;
1758
1759 if (unlikely(retval != map->m_len)) {
1760 ext4_warning(inode->i_sb,
1761 "ES len assertion failed for inode "
1762 "%lu: retval %d != map->m_len %d",
1763 inode->i_ino, retval, map->m_len);
1764 WARN_ON(1);
1765 }
1766
1767 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1768 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1769 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1770 map->m_pblk, status);
1771 if (ret != 0)
1772 retval = ret;
1773 }
1774
1775out_unlock:
1776 up_read((&EXT4_I(inode)->i_data_sem));
1777
1778 return retval;
1779}
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1794 struct buffer_head *bh, int create)
1795{
1796 struct ext4_map_blocks map;
1797 int ret = 0;
1798
1799 BUG_ON(create == 0);
1800 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1801
1802 map.m_lblk = iblock;
1803 map.m_len = 1;
1804
1805
1806
1807
1808
1809
1810 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1811 if (ret <= 0)
1812 return ret;
1813
1814 map_bh(bh, inode->i_sb, map.m_pblk);
1815 ext4_update_bh_state(bh, map.m_flags);
1816
1817 if (buffer_unwritten(bh)) {
1818
1819
1820
1821
1822
1823
1824 set_buffer_new(bh);
1825 set_buffer_mapped(bh);
1826 }
1827 return 0;
1828}
1829
1830static int bget_one(handle_t *handle, struct buffer_head *bh)
1831{
1832 get_bh(bh);
1833 return 0;
1834}
1835
1836static int bput_one(handle_t *handle, struct buffer_head *bh)
1837{
1838 put_bh(bh);
1839 return 0;
1840}
1841
1842static int __ext4_journalled_writepage(struct page *page,
1843 unsigned int len)
1844{
1845 struct address_space *mapping = page->mapping;
1846 struct inode *inode = mapping->host;
1847 struct buffer_head *page_bufs = NULL;
1848 handle_t *handle = NULL;
1849 int ret = 0, err = 0;
1850 int inline_data = ext4_has_inline_data(inode);
1851 struct buffer_head *inode_bh = NULL;
1852
1853 ClearPageChecked(page);
1854
1855 if (inline_data) {
1856 BUG_ON(page->index != 0);
1857 BUG_ON(len > ext4_get_max_inline_size(inode));
1858 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1859 if (inode_bh == NULL)
1860 goto out;
1861 } else {
1862 page_bufs = page_buffers(page);
1863 if (!page_bufs) {
1864 BUG();
1865 goto out;
1866 }
1867 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1868 NULL, bget_one);
1869 }
1870
1871
1872
1873
1874
1875 get_page(page);
1876 unlock_page(page);
1877
1878 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1879 ext4_writepage_trans_blocks(inode));
1880 if (IS_ERR(handle)) {
1881 ret = PTR_ERR(handle);
1882 put_page(page);
1883 goto out_no_pagelock;
1884 }
1885 BUG_ON(!ext4_handle_valid(handle));
1886
1887 lock_page(page);
1888 put_page(page);
1889 if (page->mapping != mapping) {
1890
1891 ext4_journal_stop(handle);
1892 ret = 0;
1893 goto out;
1894 }
1895
1896 if (inline_data) {
1897 ret = ext4_mark_inode_dirty(handle, inode);
1898 } else {
1899 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1900 do_journal_get_write_access);
1901
1902 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1903 write_end_fn);
1904 }
1905 if (ret == 0)
1906 ret = err;
1907 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
1908 err = ext4_journal_stop(handle);
1909 if (!ret)
1910 ret = err;
1911
1912 if (!ext4_has_inline_data(inode))
1913 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
1914 NULL, bput_one);
1915 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
1916out:
1917 unlock_page(page);
1918out_no_pagelock:
1919 brelse(inode_bh);
1920 return ret;
1921}
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964static int ext4_writepage(struct page *page,
1965 struct writeback_control *wbc)
1966{
1967 int ret = 0;
1968 loff_t size;
1969 unsigned int len;
1970 struct buffer_head *page_bufs = NULL;
1971 struct inode *inode = page->mapping->host;
1972 struct ext4_io_submit io_submit;
1973 bool keep_towrite = false;
1974
1975 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
1976 inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
1977 unlock_page(page);
1978 return -EIO;
1979 }
1980
1981 trace_ext4_writepage(page);
1982 size = i_size_read(inode);
1983 if (page->index == size >> PAGE_SHIFT &&
1984 !ext4_verity_in_progress(inode))
1985 len = size & ~PAGE_MASK;
1986 else
1987 len = PAGE_SIZE;
1988
1989 page_bufs = page_buffers(page);
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2008 ext4_bh_delay_or_unwritten)) {
2009 redirty_page_for_writepage(wbc, page);
2010 if ((current->flags & PF_MEMALLOC) ||
2011 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
2012
2013
2014
2015
2016
2017 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2018 == PF_MEMALLOC);
2019 unlock_page(page);
2020 return 0;
2021 }
2022 keep_towrite = true;
2023 }
2024
2025 if (PageChecked(page) && ext4_should_journal_data(inode))
2026
2027
2028
2029
2030 return __ext4_journalled_writepage(page, len);
2031
2032 ext4_io_submit_init(&io_submit, wbc);
2033 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2034 if (!io_submit.io_end) {
2035 redirty_page_for_writepage(wbc, page);
2036 unlock_page(page);
2037 return -ENOMEM;
2038 }
2039 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
2040 ext4_io_submit(&io_submit);
2041
2042 ext4_put_io_end_defer(io_submit.io_end);
2043 return ret;
2044}
2045
2046static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2047{
2048 int len;
2049 loff_t size;
2050 int err;
2051
2052 BUG_ON(page->index != mpd->first_page);
2053 clear_page_dirty_for_io(page);
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067 size = i_size_read(mpd->inode);
2068 if (page->index == size >> PAGE_SHIFT &&
2069 !ext4_verity_in_progress(mpd->inode))
2070 len = size & ~PAGE_MASK;
2071 else
2072 len = PAGE_SIZE;
2073 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
2074 if (!err)
2075 mpd->wbc->nr_to_write--;
2076 mpd->first_page++;
2077
2078 return err;
2079}
2080
2081#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2082
2083
2084
2085
2086
2087
2088#define MAX_WRITEPAGES_EXTENT_LEN 2048
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2105 struct buffer_head *bh)
2106{
2107 struct ext4_map_blocks *map = &mpd->map;
2108
2109
2110 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2111 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2112
2113 if (map->m_len == 0)
2114 return true;
2115 return false;
2116 }
2117
2118
2119 if (map->m_len == 0) {
2120
2121 if (!mpd->do_map)
2122 return false;
2123 map->m_lblk = lblk;
2124 map->m_len = 1;
2125 map->m_flags = bh->b_state & BH_FLAGS;
2126 return true;
2127 }
2128
2129
2130 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2131 return false;
2132
2133
2134 if (lblk == map->m_lblk + map->m_len &&
2135 (bh->b_state & BH_FLAGS) == map->m_flags) {
2136 map->m_len++;
2137 return true;
2138 }
2139 return false;
2140}
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2159 struct buffer_head *head,
2160 struct buffer_head *bh,
2161 ext4_lblk_t lblk)
2162{
2163 struct inode *inode = mpd->inode;
2164 int err;
2165 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
2166 >> inode->i_blkbits;
2167
2168 if (ext4_verity_in_progress(inode))
2169 blocks = EXT_MAX_BLOCKS;
2170
2171 do {
2172 BUG_ON(buffer_locked(bh));
2173
2174 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
2175
2176 if (mpd->map.m_len)
2177 return 0;
2178
2179 if (!mpd->do_map)
2180 return 0;
2181
2182 break;
2183 }
2184 } while (lblk++, (bh = bh->b_this_page) != head);
2185
2186 if (mpd->map.m_len == 0) {
2187 err = mpage_submit_page(mpd, head->b_page);
2188 if (err < 0)
2189 return err;
2190 }
2191 return lblk < blocks;
2192}
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
2210 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
2211 bool *map_bh)
2212{
2213 struct buffer_head *head, *bh;
2214 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2215 ext4_lblk_t lblk = *m_lblk;
2216 ext4_fsblk_t pblock = *m_pblk;
2217 int err = 0;
2218 int blkbits = mpd->inode->i_blkbits;
2219 ssize_t io_end_size = 0;
2220 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
2221
2222 bh = head = page_buffers(page);
2223 do {
2224 if (lblk < mpd->map.m_lblk)
2225 continue;
2226 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2227
2228
2229
2230
2231 mpd->map.m_len = 0;
2232 mpd->map.m_flags = 0;
2233 io_end_vec->size += io_end_size;
2234 io_end_size = 0;
2235
2236 err = mpage_process_page_bufs(mpd, head, bh, lblk);
2237 if (err > 0)
2238 err = 0;
2239 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
2240 io_end_vec = ext4_alloc_io_end_vec(io_end);
2241 if (IS_ERR(io_end_vec)) {
2242 err = PTR_ERR(io_end_vec);
2243 goto out;
2244 }
2245 io_end_vec->offset = mpd->map.m_lblk << blkbits;
2246 }
2247 *map_bh = true;
2248 goto out;
2249 }
2250 if (buffer_delay(bh)) {
2251 clear_buffer_delay(bh);
2252 bh->b_blocknr = pblock++;
2253 }
2254 clear_buffer_unwritten(bh);
2255 io_end_size += (1 << blkbits);
2256 } while (lblk++, (bh = bh->b_this_page) != head);
2257
2258 io_end_vec->size += io_end_size;
2259 io_end_size = 0;
2260 *map_bh = false;
2261out:
2262 *m_lblk = lblk;
2263 *m_pblk = pblock;
2264 return err;
2265}
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2282{
2283 struct pagevec pvec;
2284 int nr_pages, i;
2285 struct inode *inode = mpd->inode;
2286 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
2287 pgoff_t start, end;
2288 ext4_lblk_t lblk;
2289 ext4_fsblk_t pblock;
2290 int err;
2291 bool map_bh = false;
2292
2293 start = mpd->map.m_lblk >> bpp_bits;
2294 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2295 lblk = start << bpp_bits;
2296 pblock = mpd->map.m_pblk;
2297
2298 pagevec_init(&pvec);
2299 while (start <= end) {
2300 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
2301 &start, end);
2302 if (nr_pages == 0)
2303 break;
2304 for (i = 0; i < nr_pages; i++) {
2305 struct page *page = pvec.pages[i];
2306
2307 err = mpage_process_page(mpd, page, &lblk, &pblock,
2308 &map_bh);
2309
2310
2311
2312
2313
2314 if (err < 0 || map_bh == true)
2315 goto out;
2316
2317 err = mpage_submit_page(mpd, page);
2318 if (err < 0)
2319 goto out;
2320 }
2321 pagevec_release(&pvec);
2322 }
2323
2324 mpd->map.m_len = 0;
2325 mpd->map.m_flags = 0;
2326 return 0;
2327out:
2328 pagevec_release(&pvec);
2329 return err;
2330}
2331
2332static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2333{
2334 struct inode *inode = mpd->inode;
2335 struct ext4_map_blocks *map = &mpd->map;
2336 int get_blocks_flags;
2337 int err, dioread_nolock;
2338
2339 trace_ext4_da_write_pages_extent(inode, map);
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
2356 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2357 EXT4_GET_BLOCKS_IO_SUBMIT;
2358 dioread_nolock = ext4_should_dioread_nolock(inode);
2359 if (dioread_nolock)
2360 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2361 if (map->m_flags & (1 << BH_Delay))
2362 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2363
2364 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2365 if (err < 0)
2366 return err;
2367 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
2368 if (!mpd->io_submit.io_end->handle &&
2369 ext4_handle_valid(handle)) {
2370 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2371 handle->h_rsv_handle = NULL;
2372 }
2373 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
2374 }
2375
2376 BUG_ON(map->m_len == 0);
2377 return 0;
2378}
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400static int mpage_map_and_submit_extent(handle_t *handle,
2401 struct mpage_da_data *mpd,
2402 bool *give_up_on_write)
2403{
2404 struct inode *inode = mpd->inode;
2405 struct ext4_map_blocks *map = &mpd->map;
2406 int err;
2407 loff_t disksize;
2408 int progress = 0;
2409 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2410 struct ext4_io_end_vec *io_end_vec;
2411
2412 io_end_vec = ext4_alloc_io_end_vec(io_end);
2413 if (IS_ERR(io_end_vec))
2414 return PTR_ERR(io_end_vec);
2415 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
2416 do {
2417 err = mpage_map_one_extent(handle, mpd);
2418 if (err < 0) {
2419 struct super_block *sb = inode->i_sb;
2420
2421 if (ext4_forced_shutdown(EXT4_SB(sb)) ||
2422 EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
2423 goto invalidate_dirty_pages;
2424
2425
2426
2427
2428
2429 if ((err == -ENOMEM) ||
2430 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2431 if (progress)
2432 goto update_disksize;
2433 return err;
2434 }
2435 ext4_msg(sb, KERN_CRIT,
2436 "Delayed block allocation failed for "
2437 "inode %lu at logical offset %llu with"
2438 " max blocks %u with error %d",
2439 inode->i_ino,
2440 (unsigned long long)map->m_lblk,
2441 (unsigned)map->m_len, -err);
2442 ext4_msg(sb, KERN_CRIT,
2443 "This should not happen!! Data will "
2444 "be lost\n");
2445 if (err == -ENOSPC)
2446 ext4_print_free_blocks(inode);
2447 invalidate_dirty_pages:
2448 *give_up_on_write = true;
2449 return err;
2450 }
2451 progress = 1;
2452
2453
2454
2455
2456 err = mpage_map_and_submit_buffers(mpd);
2457 if (err < 0)
2458 goto update_disksize;
2459 } while (map->m_len);
2460
2461update_disksize:
2462
2463
2464
2465
2466 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
2467 if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
2468 int err2;
2469 loff_t i_size;
2470
2471 down_write(&EXT4_I(inode)->i_data_sem);
2472 i_size = i_size_read(inode);
2473 if (disksize > i_size)
2474 disksize = i_size;
2475 if (disksize > EXT4_I(inode)->i_disksize)
2476 EXT4_I(inode)->i_disksize = disksize;
2477 up_write(&EXT4_I(inode)->i_data_sem);
2478 err2 = ext4_mark_inode_dirty(handle, inode);
2479 if (err2) {
2480 ext4_error_err(inode->i_sb, -err2,
2481 "Failed to mark inode %lu dirty",
2482 inode->i_ino);
2483 }
2484 if (!err)
2485 err = err2;
2486 }
2487 return err;
2488}
2489
2490
2491
2492
2493
2494
2495
2496
2497static int ext4_da_writepages_trans_blocks(struct inode *inode)
2498{
2499 int bpp = ext4_journal_blocks_per_page(inode);
2500
2501 return ext4_meta_trans_blocks(inode,
2502 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
2503}
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
2524{
2525 struct address_space *mapping = mpd->inode->i_mapping;
2526 struct pagevec pvec;
2527 unsigned int nr_pages;
2528 long left = mpd->wbc->nr_to_write;
2529 pgoff_t index = mpd->first_page;
2530 pgoff_t end = mpd->last_page;
2531 xa_mark_t tag;
2532 int i, err = 0;
2533 int blkbits = mpd->inode->i_blkbits;
2534 ext4_lblk_t lblk;
2535 struct buffer_head *head;
2536
2537 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
2538 tag = PAGECACHE_TAG_TOWRITE;
2539 else
2540 tag = PAGECACHE_TAG_DIRTY;
2541
2542 pagevec_init(&pvec);
2543 mpd->map.m_len = 0;
2544 mpd->next_page = index;
2545 while (index <= end) {
2546 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2547 tag);
2548 if (nr_pages == 0)
2549 goto out;
2550
2551 for (i = 0; i < nr_pages; i++) {
2552 struct page *page = pvec.pages[i];
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2563 goto out;
2564
2565
2566 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2567 goto out;
2568
2569 lock_page(page);
2570
2571
2572
2573
2574
2575
2576
2577 if (!PageDirty(page) ||
2578 (PageWriteback(page) &&
2579 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
2580 unlikely(page->mapping != mapping)) {
2581 unlock_page(page);
2582 continue;
2583 }
2584
2585 wait_on_page_writeback(page);
2586 BUG_ON(PageWriteback(page));
2587
2588 if (mpd->map.m_len == 0)
2589 mpd->first_page = page->index;
2590 mpd->next_page = page->index + 1;
2591
2592 lblk = ((ext4_lblk_t)page->index) <<
2593 (PAGE_SHIFT - blkbits);
2594 head = page_buffers(page);
2595 err = mpage_process_page_bufs(mpd, head, head, lblk);
2596 if (err <= 0)
2597 goto out;
2598 err = 0;
2599 left--;
2600 }
2601 pagevec_release(&pvec);
2602 cond_resched();
2603 }
2604 return 0;
2605out:
2606 pagevec_release(&pvec);
2607 return err;
2608}
2609
2610static int ext4_writepages(struct address_space *mapping,
2611 struct writeback_control *wbc)
2612{
2613 pgoff_t writeback_index = 0;
2614 long nr_to_write = wbc->nr_to_write;
2615 int range_whole = 0;
2616 int cycled = 1;
2617 handle_t *handle = NULL;
2618 struct mpage_da_data mpd;
2619 struct inode *inode = mapping->host;
2620 int needed_blocks, rsv_blocks = 0, ret = 0;
2621 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2622 bool done;
2623 struct blk_plug plug;
2624 bool give_up_on_write = false;
2625
2626 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2627 return -EIO;
2628
2629 percpu_down_read(&sbi->s_writepages_rwsem);
2630 trace_ext4_writepages(inode, wbc);
2631
2632
2633
2634
2635
2636
2637 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2638 goto out_writepages;
2639
2640 if (ext4_should_journal_data(inode)) {
2641 ret = generic_writepages(mapping, wbc);
2642 goto out_writepages;
2643 }
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
2656 sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2657 ret = -EROFS;
2658 goto out_writepages;
2659 }
2660
2661
2662
2663
2664
2665
2666 if (ext4_has_inline_data(inode)) {
2667
2668 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2669 if (IS_ERR(handle)) {
2670 ret = PTR_ERR(handle);
2671 goto out_writepages;
2672 }
2673 BUG_ON(ext4_test_inode_state(inode,
2674 EXT4_STATE_MAY_INLINE_DATA));
2675 ext4_destroy_inline_data(handle, inode);
2676 ext4_journal_stop(handle);
2677 }
2678
2679 if (ext4_should_dioread_nolock(inode)) {
2680
2681
2682
2683
2684 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
2685 PAGE_SIZE >> inode->i_blkbits);
2686 }
2687
2688 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2689 range_whole = 1;
2690
2691 if (wbc->range_cyclic) {
2692 writeback_index = mapping->writeback_index;
2693 if (writeback_index)
2694 cycled = 0;
2695 mpd.first_page = writeback_index;
2696 mpd.last_page = -1;
2697 } else {
2698 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2699 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
2700 }
2701
2702 mpd.inode = inode;
2703 mpd.wbc = wbc;
2704 ext4_io_submit_init(&mpd.io_submit, wbc);
2705retry:
2706 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2707 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2708 done = false;
2709 blk_start_plug(&plug);
2710
2711
2712
2713
2714
2715
2716
2717 mpd.do_map = 0;
2718 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2719 if (!mpd.io_submit.io_end) {
2720 ret = -ENOMEM;
2721 goto unplug;
2722 }
2723 ret = mpage_prepare_extent_to_map(&mpd);
2724
2725 mpage_release_unused_pages(&mpd, false);
2726
2727 ext4_io_submit(&mpd.io_submit);
2728 ext4_put_io_end_defer(mpd.io_submit.io_end);
2729 mpd.io_submit.io_end = NULL;
2730 if (ret < 0)
2731 goto unplug;
2732
2733 while (!done && mpd.first_page <= mpd.last_page) {
2734
2735 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2736 if (!mpd.io_submit.io_end) {
2737 ret = -ENOMEM;
2738 break;
2739 }
2740
2741
2742
2743
2744
2745
2746
2747
2748 BUG_ON(ext4_should_journal_data(inode));
2749 needed_blocks = ext4_da_writepages_trans_blocks(inode);
2750
2751
2752 handle = ext4_journal_start_with_reserve(inode,
2753 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
2754 if (IS_ERR(handle)) {
2755 ret = PTR_ERR(handle);
2756 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
2757 "%ld pages, ino %lu; err %d", __func__,
2758 wbc->nr_to_write, inode->i_ino, ret);
2759
2760 ext4_put_io_end(mpd.io_submit.io_end);
2761 mpd.io_submit.io_end = NULL;
2762 break;
2763 }
2764 mpd.do_map = 1;
2765
2766 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2767 ret = mpage_prepare_extent_to_map(&mpd);
2768 if (!ret) {
2769 if (mpd.map.m_len)
2770 ret = mpage_map_and_submit_extent(handle, &mpd,
2771 &give_up_on_write);
2772 else {
2773
2774
2775
2776
2777
2778
2779 done = true;
2780 }
2781 }
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2793 ext4_journal_stop(handle);
2794 handle = NULL;
2795 mpd.do_map = 0;
2796 }
2797
2798 mpage_release_unused_pages(&mpd, give_up_on_write);
2799
2800 ext4_io_submit(&mpd.io_submit);
2801
2802
2803
2804
2805
2806
2807
2808
2809 if (handle) {
2810 ext4_put_io_end_defer(mpd.io_submit.io_end);
2811 ext4_journal_stop(handle);
2812 } else
2813 ext4_put_io_end(mpd.io_submit.io_end);
2814 mpd.io_submit.io_end = NULL;
2815
2816 if (ret == -ENOSPC && sbi->s_journal) {
2817
2818
2819
2820
2821
2822 jbd2_journal_force_commit_nested(sbi->s_journal);
2823 ret = 0;
2824 continue;
2825 }
2826
2827 if (ret)
2828 break;
2829 }
2830unplug:
2831 blk_finish_plug(&plug);
2832 if (!ret && !cycled && wbc->nr_to_write > 0) {
2833 cycled = 1;
2834 mpd.last_page = writeback_index - 1;
2835 mpd.first_page = 0;
2836 goto retry;
2837 }
2838
2839
2840 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2841
2842
2843
2844
2845 mapping->writeback_index = mpd.first_page;
2846
2847out_writepages:
2848 trace_ext4_writepages_result(inode, wbc, ret,
2849 nr_to_write - wbc->nr_to_write);
2850 percpu_up_read(&sbi->s_writepages_rwsem);
2851 return ret;
2852}
2853
2854static int ext4_dax_writepages(struct address_space *mapping,
2855 struct writeback_control *wbc)
2856{
2857 int ret;
2858 long nr_to_write = wbc->nr_to_write;
2859 struct inode *inode = mapping->host;
2860 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2861
2862 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2863 return -EIO;
2864
2865 percpu_down_read(&sbi->s_writepages_rwsem);
2866 trace_ext4_writepages(inode, wbc);
2867
2868 ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc);
2869 trace_ext4_writepages_result(inode, wbc, ret,
2870 nr_to_write - wbc->nr_to_write);
2871 percpu_up_read(&sbi->s_writepages_rwsem);
2872 return ret;
2873}
2874
2875static int ext4_nonda_switch(struct super_block *sb)
2876{
2877 s64 free_clusters, dirty_clusters;
2878 struct ext4_sb_info *sbi = EXT4_SB(sb);
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888 free_clusters =
2889 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2890 dirty_clusters =
2891 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
2892
2893
2894
2895 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
2896 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
2897
2898 if (2 * free_clusters < 3 * dirty_clusters ||
2899 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
2900
2901
2902
2903
2904 return 1;
2905 }
2906 return 0;
2907}
2908
2909
2910static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2911{
2912 if (likely(ext4_has_feature_large_file(inode->i_sb)))
2913 return 1;
2914
2915 if (pos + len <= 0x7fffffffULL)
2916 return 1;
2917
2918
2919 return 2;
2920}
2921
2922static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
2923 loff_t pos, unsigned len, unsigned flags,
2924 struct page **pagep, void **fsdata)
2925{
2926 int ret, retries = 0;
2927 struct page *page;
2928 pgoff_t index;
2929 struct inode *inode = mapping->host;
2930 handle_t *handle;
2931
2932 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2933 return -EIO;
2934
2935 index = pos >> PAGE_SHIFT;
2936
2937 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
2938 ext4_verity_in_progress(inode)) {
2939 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2940 return ext4_write_begin(file, mapping, pos,
2941 len, flags, pagep, fsdata);
2942 }
2943 *fsdata = (void *)0;
2944 trace_ext4_da_write_begin(inode, pos, len, flags);
2945
2946 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2947 ret = ext4_da_write_inline_data_begin(mapping, inode,
2948 pos, len, flags,
2949 pagep, fsdata);
2950 if (ret < 0)
2951 return ret;
2952 if (ret == 1)
2953 return 0;
2954 }
2955
2956
2957
2958
2959
2960
2961
2962
2963retry_grab:
2964 page = grab_cache_page_write_begin(mapping, index, flags);
2965 if (!page)
2966 return -ENOMEM;
2967 unlock_page(page);
2968
2969
2970
2971
2972
2973
2974
2975retry_journal:
2976 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2977 ext4_da_write_credits(inode, pos, len));
2978 if (IS_ERR(handle)) {
2979 put_page(page);
2980 return PTR_ERR(handle);
2981 }
2982
2983 lock_page(page);
2984 if (page->mapping != mapping) {
2985
2986 unlock_page(page);
2987 put_page(page);
2988 ext4_journal_stop(handle);
2989 goto retry_grab;
2990 }
2991
2992 wait_for_stable_page(page);
2993
2994#ifdef CONFIG_FS_ENCRYPTION
2995 ret = ext4_block_write_begin(page, pos, len,
2996 ext4_da_get_block_prep);
2997#else
2998 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
2999#endif
3000 if (ret < 0) {
3001 unlock_page(page);
3002 ext4_journal_stop(handle);
3003
3004
3005
3006
3007
3008 if (pos + len > inode->i_size)
3009 ext4_truncate_failed_write(inode);
3010
3011 if (ret == -ENOSPC &&
3012 ext4_should_retry_alloc(inode->i_sb, &retries))
3013 goto retry_journal;
3014
3015 put_page(page);
3016 return ret;
3017 }
3018
3019 *pagep = page;
3020 return ret;
3021}
3022
3023
3024
3025
3026
3027static int ext4_da_should_update_i_disksize(struct page *page,
3028 unsigned long offset)
3029{
3030 struct buffer_head *bh;
3031 struct inode *inode = page->mapping->host;
3032 unsigned int idx;
3033 int i;
3034
3035 bh = page_buffers(page);
3036 idx = offset >> inode->i_blkbits;
3037
3038 for (i = 0; i < idx; i++)
3039 bh = bh->b_this_page;
3040
3041 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
3042 return 0;
3043 return 1;
3044}
3045
3046static int ext4_da_write_end(struct file *file,
3047 struct address_space *mapping,
3048 loff_t pos, unsigned len, unsigned copied,
3049 struct page *page, void *fsdata)
3050{
3051 struct inode *inode = mapping->host;
3052 int ret = 0, ret2;
3053 handle_t *handle = ext4_journal_current_handle();
3054 loff_t new_i_size;
3055 unsigned long start, end;
3056 int write_mode = (int)(unsigned long)fsdata;
3057
3058 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3059 return ext4_write_end(file, mapping, pos,
3060 len, copied, page, fsdata);
3061
3062 trace_ext4_da_write_end(inode, pos, len, copied);
3063 start = pos & (PAGE_SIZE - 1);
3064 end = start + copied - 1;
3065
3066
3067
3068
3069
3070
3071 new_i_size = pos + copied;
3072 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
3073 if (ext4_has_inline_data(inode) ||
3074 ext4_da_should_update_i_disksize(page, end)) {
3075 ext4_update_i_disksize(inode, new_i_size);
3076
3077
3078
3079
3080 ext4_mark_inode_dirty(handle, inode);
3081 }
3082 }
3083
3084 if (write_mode != CONVERT_INLINE_DATA &&
3085 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3086 ext4_has_inline_data(inode))
3087 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3088 page);
3089 else
3090 ret2 = generic_write_end(file, mapping, pos, len, copied,
3091 page, fsdata);
3092
3093 copied = ret2;
3094 if (ret2 < 0)
3095 ret = ret2;
3096 ret2 = ext4_journal_stop(handle);
3097 if (!ret)
3098 ret = ret2;
3099
3100 return ret ? ret : copied;
3101}
3102
3103
3104
3105
3106int ext4_alloc_da_blocks(struct inode *inode)
3107{
3108 trace_ext4_alloc_da_blocks(inode);
3109
3110 if (!EXT4_I(inode)->i_reserved_data_blocks)
3111 return 0;
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144 return filemap_flush(inode->i_mapping);
3145}
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
3162{
3163 struct inode *inode = mapping->host;
3164 journal_t *journal;
3165 int err;
3166
3167
3168
3169
3170 if (ext4_has_inline_data(inode))
3171 return 0;
3172
3173 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3174 test_opt(inode->i_sb, DELALLOC)) {
3175
3176
3177
3178
3179
3180 filemap_write_and_wait(mapping);
3181 }
3182
3183 if (EXT4_JOURNAL(inode) &&
3184 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
3204 journal = EXT4_JOURNAL(inode);
3205 jbd2_journal_lock_updates(journal);
3206 err = jbd2_journal_flush(journal);
3207 jbd2_journal_unlock_updates(journal);
3208
3209 if (err)
3210 return 0;
3211 }
3212
3213 return iomap_bmap(mapping, block, &ext4_iomap_ops);
3214}
3215
3216static int ext4_readpage(struct file *file, struct page *page)
3217{
3218 int ret = -EAGAIN;
3219 struct inode *inode = page->mapping->host;
3220
3221 trace_ext4_readpage(page);
3222
3223 if (ext4_has_inline_data(inode))
3224 ret = ext4_readpage_inline(inode, page);
3225
3226 if (ret == -EAGAIN)
3227 return ext4_mpage_readpages(page->mapping, NULL, page, 1,
3228 false);
3229
3230 return ret;
3231}
3232
3233static int
3234ext4_readpages(struct file *file, struct address_space *mapping,
3235 struct list_head *pages, unsigned nr_pages)
3236{
3237 struct inode *inode = mapping->host;
3238
3239
3240 if (ext4_has_inline_data(inode))
3241 return 0;
3242
3243 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true);
3244}
3245
3246static void ext4_invalidatepage(struct page *page, unsigned int offset,
3247 unsigned int length)
3248{
3249 trace_ext4_invalidatepage(page, offset, length);
3250
3251
3252 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3253
3254 block_invalidatepage(page, offset, length);
3255}
3256
3257static int __ext4_journalled_invalidatepage(struct page *page,
3258 unsigned int offset,
3259 unsigned int length)
3260{
3261 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3262
3263 trace_ext4_journalled_invalidatepage(page, offset, length);
3264
3265
3266
3267
3268 if (offset == 0 && length == PAGE_SIZE)
3269 ClearPageChecked(page);
3270
3271 return jbd2_journal_invalidatepage(journal, page, offset, length);
3272}
3273
3274
3275static void ext4_journalled_invalidatepage(struct page *page,
3276 unsigned int offset,
3277 unsigned int length)
3278{
3279 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
3280}
3281
3282static int ext4_releasepage(struct page *page, gfp_t wait)
3283{
3284 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3285
3286 trace_ext4_releasepage(page);
3287
3288
3289 if (PageChecked(page))
3290 return 0;
3291 if (journal)
3292 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3293 else
3294 return try_to_free_buffers(page);
3295}
3296
3297static bool ext4_inode_datasync_dirty(struct inode *inode)
3298{
3299 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
3300
3301 if (journal)
3302 return !jbd2_transaction_committed(journal,
3303 EXT4_I(inode)->i_datasync_tid);
3304
3305 if (!list_empty(&inode->i_mapping->private_list))
3306 return true;
3307 return inode->i_state & I_DIRTY_DATASYNC;
3308}
3309
3310static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
3311 struct ext4_map_blocks *map, loff_t offset,
3312 loff_t length)
3313{
3314 u8 blkbits = inode->i_blkbits;
3315
3316
3317
3318
3319
3320
3321 iomap->flags = 0;
3322 if (ext4_inode_datasync_dirty(inode) ||
3323 offset + length > i_size_read(inode))
3324 iomap->flags |= IOMAP_F_DIRTY;
3325
3326 if (map->m_flags & EXT4_MAP_NEW)
3327 iomap->flags |= IOMAP_F_NEW;
3328
3329 iomap->bdev = inode->i_sb->s_bdev;
3330 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
3331 iomap->offset = (u64) map->m_lblk << blkbits;
3332 iomap->length = (u64) map->m_len << blkbits;
3333
3334 if ((map->m_flags & EXT4_MAP_MAPPED) &&
3335 !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3336 iomap->flags |= IOMAP_F_MERGED;
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347 if (map->m_flags & EXT4_MAP_UNWRITTEN) {
3348 iomap->type = IOMAP_UNWRITTEN;
3349 iomap->addr = (u64) map->m_pblk << blkbits;
3350 } else if (map->m_flags & EXT4_MAP_MAPPED) {
3351 iomap->type = IOMAP_MAPPED;
3352 iomap->addr = (u64) map->m_pblk << blkbits;
3353 } else {
3354 iomap->type = IOMAP_HOLE;
3355 iomap->addr = IOMAP_NULL_ADDR;
3356 }
3357}
3358
3359static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
3360 unsigned int flags)
3361{
3362 handle_t *handle;
3363 u8 blkbits = inode->i_blkbits;
3364 int ret, dio_credits, m_flags = 0, retries = 0;
3365
3366
3367
3368
3369
3370 if (map->m_len > DIO_MAX_BLOCKS)
3371 map->m_len = DIO_MAX_BLOCKS;
3372 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
3373
3374retry:
3375
3376
3377
3378
3379
3380
3381 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
3382 if (IS_ERR(handle))
3383 return PTR_ERR(handle);
3384
3385
3386
3387
3388
3389 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
3390 if (IS_DAX(inode))
3391 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
3392
3393
3394
3395
3396
3397
3398 else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode))
3399 m_flags = EXT4_GET_BLOCKS_CREATE;
3400 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3401 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
3402
3403 ret = ext4_map_blocks(handle, inode, map, m_flags);
3404
3405
3406
3407
3408
3409
3410 if (!m_flags && !ret)
3411 ret = -ENOTBLK;
3412
3413 ext4_journal_stop(handle);
3414 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3415 goto retry;
3416
3417 return ret;
3418}
3419
3420
3421static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
3422 unsigned flags, struct iomap *iomap, struct iomap *srcmap)
3423{
3424 int ret;
3425 struct ext4_map_blocks map;
3426 u8 blkbits = inode->i_blkbits;
3427
3428 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3429 return -EINVAL;
3430
3431 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3432 return -ERANGE;
3433
3434
3435
3436
3437 map.m_lblk = offset >> blkbits;
3438 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3439 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3440
3441 if (flags & IOMAP_WRITE)
3442 ret = ext4_iomap_alloc(inode, &map, flags);
3443 else
3444 ret = ext4_map_blocks(NULL, inode, &map, 0);
3445
3446 if (ret < 0)
3447 return ret;
3448
3449 ext4_set_iomap(inode, iomap, &map, offset, length);
3450
3451 return 0;
3452}
3453
3454static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
3455 loff_t length, unsigned flags, struct iomap *iomap,
3456 struct iomap *srcmap)
3457{
3458 int ret;
3459
3460
3461
3462
3463
3464 flags &= ~IOMAP_WRITE;
3465 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
3466 WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
3467 return ret;
3468}
3469
3470static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
3471 ssize_t written, unsigned flags, struct iomap *iomap)
3472{
3473
3474
3475
3476
3477
3478
3479
3480 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3481 return -ENOTBLK;
3482
3483 return 0;
3484}
3485
3486const struct iomap_ops ext4_iomap_ops = {
3487 .iomap_begin = ext4_iomap_begin,
3488 .iomap_end = ext4_iomap_end,
3489};
3490
3491const struct iomap_ops ext4_iomap_overwrite_ops = {
3492 .iomap_begin = ext4_iomap_overwrite_begin,
3493 .iomap_end = ext4_iomap_end,
3494};
3495
3496static bool ext4_iomap_is_delalloc(struct inode *inode,
3497 struct ext4_map_blocks *map)
3498{
3499 struct extent_status es;
3500 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
3501
3502 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3503 map->m_lblk, end, &es);
3504
3505 if (!es.es_len || es.es_lblk > end)
3506 return false;
3507
3508 if (es.es_lblk > map->m_lblk) {
3509 map->m_len = es.es_lblk - map->m_lblk;
3510 return false;
3511 }
3512
3513 offset = map->m_lblk - es.es_lblk;
3514 map->m_len = es.es_len - offset;
3515
3516 return true;
3517}
3518
3519static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3520 loff_t length, unsigned int flags,
3521 struct iomap *iomap, struct iomap *srcmap)
3522{
3523 int ret;
3524 bool delalloc = false;
3525 struct ext4_map_blocks map;
3526 u8 blkbits = inode->i_blkbits;
3527
3528 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3529 return -EINVAL;
3530
3531 if (ext4_has_inline_data(inode)) {
3532 ret = ext4_inline_data_iomap(inode, iomap);
3533 if (ret != -EAGAIN) {
3534 if (ret == 0 && offset >= iomap->length)
3535 ret = -ENOENT;
3536 return ret;
3537 }
3538 }
3539
3540
3541
3542
3543 map.m_lblk = offset >> blkbits;
3544 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3545 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3546
3547
3548
3549
3550
3551
3552
3553 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
3554 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3555
3556 if (offset >= sbi->s_bitmap_maxbytes) {
3557 map.m_flags = 0;
3558 goto set_iomap;
3559 }
3560 }
3561
3562 ret = ext4_map_blocks(NULL, inode, &map, 0);
3563 if (ret < 0)
3564 return ret;
3565 if (ret == 0)
3566 delalloc = ext4_iomap_is_delalloc(inode, &map);
3567
3568set_iomap:
3569 ext4_set_iomap(inode, iomap, &map, offset, length);
3570 if (delalloc && iomap->type == IOMAP_HOLE)
3571 iomap->type = IOMAP_DELALLOC;
3572
3573 return 0;
3574}
3575
3576const struct iomap_ops ext4_iomap_report_ops = {
3577 .iomap_begin = ext4_iomap_begin_report,
3578};
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593static int ext4_journalled_set_page_dirty(struct page *page)
3594{
3595 SetPageChecked(page);
3596 return __set_page_dirty_nobuffers(page);
3597}
3598
3599static int ext4_set_page_dirty(struct page *page)
3600{
3601 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
3602 WARN_ON_ONCE(!page_has_buffers(page));
3603 return __set_page_dirty_buffers(page);
3604}
3605
3606static const struct address_space_operations ext4_aops = {
3607 .readpage = ext4_readpage,
3608 .readpages = ext4_readpages,
3609 .writepage = ext4_writepage,
3610 .writepages = ext4_writepages,
3611 .write_begin = ext4_write_begin,
3612 .write_end = ext4_write_end,
3613 .set_page_dirty = ext4_set_page_dirty,
3614 .bmap = ext4_bmap,
3615 .invalidatepage = ext4_invalidatepage,
3616 .releasepage = ext4_releasepage,
3617 .direct_IO = noop_direct_IO,
3618 .migratepage = buffer_migrate_page,
3619 .is_partially_uptodate = block_is_partially_uptodate,
3620 .error_remove_page = generic_error_remove_page,
3621};
3622
3623static const struct address_space_operations ext4_journalled_aops = {
3624 .readpage = ext4_readpage,
3625 .readpages = ext4_readpages,
3626 .writepage = ext4_writepage,
3627 .writepages = ext4_writepages,
3628 .write_begin = ext4_write_begin,
3629 .write_end = ext4_journalled_write_end,
3630 .set_page_dirty = ext4_journalled_set_page_dirty,
3631 .bmap = ext4_bmap,
3632 .invalidatepage = ext4_journalled_invalidatepage,
3633 .releasepage = ext4_releasepage,
3634 .direct_IO = noop_direct_IO,
3635 .is_partially_uptodate = block_is_partially_uptodate,
3636 .error_remove_page = generic_error_remove_page,
3637};
3638
3639static const struct address_space_operations ext4_da_aops = {
3640 .readpage = ext4_readpage,
3641 .readpages = ext4_readpages,
3642 .writepage = ext4_writepage,
3643 .writepages = ext4_writepages,
3644 .write_begin = ext4_da_write_begin,
3645 .write_end = ext4_da_write_end,
3646 .set_page_dirty = ext4_set_page_dirty,
3647 .bmap = ext4_bmap,
3648 .invalidatepage = ext4_invalidatepage,
3649 .releasepage = ext4_releasepage,
3650 .direct_IO = noop_direct_IO,
3651 .migratepage = buffer_migrate_page,
3652 .is_partially_uptodate = block_is_partially_uptodate,
3653 .error_remove_page = generic_error_remove_page,
3654};
3655
3656static const struct address_space_operations ext4_dax_aops = {
3657 .writepages = ext4_dax_writepages,
3658 .direct_IO = noop_direct_IO,
3659 .set_page_dirty = noop_set_page_dirty,
3660 .bmap = ext4_bmap,
3661 .invalidatepage = noop_invalidatepage,
3662};
3663
3664void ext4_set_aops(struct inode *inode)
3665{
3666 switch (ext4_inode_journal_mode(inode)) {
3667 case EXT4_INODE_ORDERED_DATA_MODE:
3668 case EXT4_INODE_WRITEBACK_DATA_MODE:
3669 break;
3670 case EXT4_INODE_JOURNAL_DATA_MODE:
3671 inode->i_mapping->a_ops = &ext4_journalled_aops;
3672 return;
3673 default:
3674 BUG();
3675 }
3676 if (IS_DAX(inode))
3677 inode->i_mapping->a_ops = &ext4_dax_aops;
3678 else if (test_opt(inode->i_sb, DELALLOC))
3679 inode->i_mapping->a_ops = &ext4_da_aops;
3680 else
3681 inode->i_mapping->a_ops = &ext4_aops;
3682}
3683
3684static int __ext4_block_zero_page_range(handle_t *handle,
3685 struct address_space *mapping, loff_t from, loff_t length)
3686{
3687 ext4_fsblk_t index = from >> PAGE_SHIFT;
3688 unsigned offset = from & (PAGE_SIZE-1);
3689 unsigned blocksize, pos;
3690 ext4_lblk_t iblock;
3691 struct inode *inode = mapping->host;
3692 struct buffer_head *bh;
3693 struct page *page;
3694 int err = 0;
3695
3696 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
3697 mapping_gfp_constraint(mapping, ~__GFP_FS));
3698 if (!page)
3699 return -ENOMEM;
3700
3701 blocksize = inode->i_sb->s_blocksize;
3702
3703 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
3704
3705 if (!page_has_buffers(page))
3706 create_empty_buffers(page, blocksize, 0);
3707
3708
3709 bh = page_buffers(page);
3710 pos = blocksize;
3711 while (offset >= pos) {
3712 bh = bh->b_this_page;
3713 iblock++;
3714 pos += blocksize;
3715 }
3716 if (buffer_freed(bh)) {
3717 BUFFER_TRACE(bh, "freed: skip");
3718 goto unlock;
3719 }
3720 if (!buffer_mapped(bh)) {
3721 BUFFER_TRACE(bh, "unmapped");
3722 ext4_get_block(inode, iblock, bh, 0);
3723
3724 if (!buffer_mapped(bh)) {
3725 BUFFER_TRACE(bh, "still unmapped");
3726 goto unlock;
3727 }
3728 }
3729
3730
3731 if (PageUptodate(page))
3732 set_buffer_uptodate(bh);
3733
3734 if (!buffer_uptodate(bh)) {
3735 err = -EIO;
3736 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
3737 wait_on_buffer(bh);
3738
3739 if (!buffer_uptodate(bh))
3740 goto unlock;
3741 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
3742
3743 BUG_ON(!fscrypt_has_encryption_key(inode));
3744 err = fscrypt_decrypt_pagecache_blocks(page, blocksize,
3745 bh_offset(bh));
3746 if (err) {
3747 clear_buffer_uptodate(bh);
3748 goto unlock;
3749 }
3750 }
3751 }
3752 if (ext4_should_journal_data(inode)) {
3753 BUFFER_TRACE(bh, "get write access");
3754 err = ext4_journal_get_write_access(handle, bh);
3755 if (err)
3756 goto unlock;
3757 }
3758 zero_user(page, offset, length);
3759 BUFFER_TRACE(bh, "zeroed end of block");
3760
3761 if (ext4_should_journal_data(inode)) {
3762 err = ext4_handle_dirty_metadata(handle, inode, bh);
3763 } else {
3764 err = 0;
3765 mark_buffer_dirty(bh);
3766 if (ext4_should_order_data(inode))
3767 err = ext4_jbd2_inode_add_write(handle, inode, from,
3768 length);
3769 }
3770
3771unlock:
3772 unlock_page(page);
3773 put_page(page);
3774 return err;
3775}
3776
3777
3778
3779
3780
3781
3782
3783
3784static int ext4_block_zero_page_range(handle_t *handle,
3785 struct address_space *mapping, loff_t from, loff_t length)
3786{
3787 struct inode *inode = mapping->host;
3788 unsigned offset = from & (PAGE_SIZE-1);
3789 unsigned blocksize = inode->i_sb->s_blocksize;
3790 unsigned max = blocksize - (offset & (blocksize - 1));
3791
3792
3793
3794
3795
3796 if (length > max || length < 0)
3797 length = max;
3798
3799 if (IS_DAX(inode)) {
3800 return iomap_zero_range(inode, from, length, NULL,
3801 &ext4_iomap_ops);
3802 }
3803 return __ext4_block_zero_page_range(handle, mapping, from, length);
3804}
3805
3806
3807
3808
3809
3810
3811
3812static int ext4_block_truncate_page(handle_t *handle,
3813 struct address_space *mapping, loff_t from)
3814{
3815 unsigned offset = from & (PAGE_SIZE-1);
3816 unsigned length;
3817 unsigned blocksize;
3818 struct inode *inode = mapping->host;
3819
3820
3821 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
3822 return 0;
3823
3824 blocksize = inode->i_sb->s_blocksize;
3825 length = blocksize - (offset & (blocksize - 1));
3826
3827 return ext4_block_zero_page_range(handle, mapping, from, length);
3828}
3829
3830int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3831 loff_t lstart, loff_t length)
3832{
3833 struct super_block *sb = inode->i_sb;
3834 struct address_space *mapping = inode->i_mapping;
3835 unsigned partial_start, partial_end;
3836 ext4_fsblk_t start, end;
3837 loff_t byte_end = (lstart + length - 1);
3838 int err = 0;
3839
3840 partial_start = lstart & (sb->s_blocksize - 1);
3841 partial_end = byte_end & (sb->s_blocksize - 1);
3842
3843 start = lstart >> sb->s_blocksize_bits;
3844 end = byte_end >> sb->s_blocksize_bits;
3845
3846
3847 if (start == end &&
3848 (partial_start || (partial_end != sb->s_blocksize - 1))) {
3849 err = ext4_block_zero_page_range(handle, mapping,
3850 lstart, length);
3851 return err;
3852 }
3853
3854 if (partial_start) {
3855 err = ext4_block_zero_page_range(handle, mapping,
3856 lstart, sb->s_blocksize);
3857 if (err)
3858 return err;
3859 }
3860
3861 if (partial_end != sb->s_blocksize - 1)
3862 err = ext4_block_zero_page_range(handle, mapping,
3863 byte_end - partial_end,
3864 partial_end + 1);
3865 return err;
3866}
3867
3868int ext4_can_truncate(struct inode *inode)
3869{
3870 if (S_ISREG(inode->i_mode))
3871 return 1;
3872 if (S_ISDIR(inode->i_mode))
3873 return 1;
3874 if (S_ISLNK(inode->i_mode))
3875 return !ext4_inode_is_fast_symlink(inode);
3876 return 0;
3877}
3878
3879
3880
3881
3882
3883
3884
3885int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3886 loff_t len)
3887{
3888 handle_t *handle;
3889 loff_t size = i_size_read(inode);
3890
3891 WARN_ON(!inode_is_locked(inode));
3892 if (offset > size || offset + len < size)
3893 return 0;
3894
3895 if (EXT4_I(inode)->i_disksize >= size)
3896 return 0;
3897
3898 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3899 if (IS_ERR(handle))
3900 return PTR_ERR(handle);
3901 ext4_update_i_disksize(inode, size);
3902 ext4_mark_inode_dirty(handle, inode);
3903 ext4_journal_stop(handle);
3904
3905 return 0;
3906}
3907
3908static void ext4_wait_dax_page(struct ext4_inode_info *ei)
3909{
3910 up_write(&ei->i_mmap_sem);
3911 schedule();
3912 down_write(&ei->i_mmap_sem);
3913}
3914
3915int ext4_break_layouts(struct inode *inode)
3916{
3917 struct ext4_inode_info *ei = EXT4_I(inode);
3918 struct page *page;
3919 int error;
3920
3921 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
3922 return -EINVAL;
3923
3924 do {
3925 page = dax_layout_busy_page(inode->i_mapping);
3926 if (!page)
3927 return 0;
3928
3929 error = ___wait_var_event(&page->_refcount,
3930 atomic_read(&page->_refcount) == 1,
3931 TASK_INTERRUPTIBLE, 0, 0,
3932 ext4_wait_dax_page(ei));
3933 } while (error == 0);
3934
3935 return error;
3936}
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
3950{
3951 struct super_block *sb = inode->i_sb;
3952 ext4_lblk_t first_block, stop_block;
3953 struct address_space *mapping = inode->i_mapping;
3954 loff_t first_block_offset, last_block_offset;
3955 handle_t *handle;
3956 unsigned int credits;
3957 int ret = 0;
3958
3959 trace_ext4_punch_hole(inode, offset, length, 0);
3960
3961 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
3962 if (ext4_has_inline_data(inode)) {
3963 down_write(&EXT4_I(inode)->i_mmap_sem);
3964 ret = ext4_convert_inline_data(inode);
3965 up_write(&EXT4_I(inode)->i_mmap_sem);
3966 if (ret)
3967 return ret;
3968 }
3969
3970
3971
3972
3973
3974 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3975 ret = filemap_write_and_wait_range(mapping, offset,
3976 offset + length - 1);
3977 if (ret)
3978 return ret;
3979 }
3980
3981 inode_lock(inode);
3982
3983
3984 if (offset >= inode->i_size)
3985 goto out_mutex;
3986
3987
3988
3989
3990
3991 if (offset + length > inode->i_size) {
3992 length = inode->i_size +
3993 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
3994 offset;
3995 }
3996
3997 if (offset & (sb->s_blocksize - 1) ||
3998 (offset + length) & (sb->s_blocksize - 1)) {
3999
4000
4001
4002
4003 ret = ext4_inode_attach_jinode(inode);
4004 if (ret < 0)
4005 goto out_mutex;
4006
4007 }
4008
4009
4010 inode_dio_wait(inode);
4011
4012
4013
4014
4015
4016 down_write(&EXT4_I(inode)->i_mmap_sem);
4017
4018 ret = ext4_break_layouts(inode);
4019 if (ret)
4020 goto out_dio;
4021
4022 first_block_offset = round_up(offset, sb->s_blocksize);
4023 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
4024
4025
4026 if (last_block_offset > first_block_offset) {
4027 ret = ext4_update_disksize_before_punch(inode, offset, length);
4028 if (ret)
4029 goto out_dio;
4030 truncate_pagecache_range(inode, first_block_offset,
4031 last_block_offset);
4032 }
4033
4034 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4035 credits = ext4_writepage_trans_blocks(inode);
4036 else
4037 credits = ext4_blocks_for_truncate(inode);
4038 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4039 if (IS_ERR(handle)) {
4040 ret = PTR_ERR(handle);
4041 ext4_std_error(sb, ret);
4042 goto out_dio;
4043 }
4044
4045 ret = ext4_zero_partial_blocks(handle, inode, offset,
4046 length);
4047 if (ret)
4048 goto out_stop;
4049
4050 first_block = (offset + sb->s_blocksize - 1) >>
4051 EXT4_BLOCK_SIZE_BITS(sb);
4052 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4053
4054
4055 if (stop_block > first_block) {
4056
4057 down_write(&EXT4_I(inode)->i_data_sem);
4058 ext4_discard_preallocations(inode);
4059
4060 ret = ext4_es_remove_extent(inode, first_block,
4061 stop_block - first_block);
4062 if (ret) {
4063 up_write(&EXT4_I(inode)->i_data_sem);
4064 goto out_stop;
4065 }
4066
4067 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4068 ret = ext4_ext_remove_space(inode, first_block,
4069 stop_block - 1);
4070 else
4071 ret = ext4_ind_remove_space(handle, inode, first_block,
4072 stop_block);
4073
4074 up_write(&EXT4_I(inode)->i_data_sem);
4075 }
4076 if (IS_SYNC(inode))
4077 ext4_handle_sync(handle);
4078
4079 inode->i_mtime = inode->i_ctime = current_time(inode);
4080 ext4_mark_inode_dirty(handle, inode);
4081 if (ret >= 0)
4082 ext4_update_inode_fsync_trans(handle, inode, 1);
4083out_stop:
4084 ext4_journal_stop(handle);
4085out_dio:
4086 up_write(&EXT4_I(inode)->i_mmap_sem);
4087out_mutex:
4088 inode_unlock(inode);
4089 return ret;
4090}
4091
4092int ext4_inode_attach_jinode(struct inode *inode)
4093{
4094 struct ext4_inode_info *ei = EXT4_I(inode);
4095 struct jbd2_inode *jinode;
4096
4097 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4098 return 0;
4099
4100 jinode = jbd2_alloc_inode(GFP_KERNEL);
4101 spin_lock(&inode->i_lock);
4102 if (!ei->jinode) {
4103 if (!jinode) {
4104 spin_unlock(&inode->i_lock);
4105 return -ENOMEM;
4106 }
4107 ei->jinode = jinode;
4108 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4109 jinode = NULL;
4110 }
4111 spin_unlock(&inode->i_lock);
4112 if (unlikely(jinode != NULL))
4113 jbd2_free_inode(jinode);
4114 return 0;
4115}
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145int ext4_truncate(struct inode *inode)
4146{
4147 struct ext4_inode_info *ei = EXT4_I(inode);
4148 unsigned int credits;
4149 int err = 0;
4150 handle_t *handle;
4151 struct address_space *mapping = inode->i_mapping;
4152
4153
4154
4155
4156
4157
4158 if (!(inode->i_state & (I_NEW|I_FREEING)))
4159 WARN_ON(!inode_is_locked(inode));
4160 trace_ext4_truncate_enter(inode);
4161
4162 if (!ext4_can_truncate(inode))
4163 return 0;
4164
4165 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
4166 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
4167
4168 if (ext4_has_inline_data(inode)) {
4169 int has_inline = 1;
4170
4171 err = ext4_inline_data_truncate(inode, &has_inline);
4172 if (err)
4173 return err;
4174 if (has_inline)
4175 return 0;
4176 }
4177
4178
4179 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4180 if (ext4_inode_attach_jinode(inode) < 0)
4181 return 0;
4182 }
4183
4184 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4185 credits = ext4_writepage_trans_blocks(inode);
4186 else
4187 credits = ext4_blocks_for_truncate(inode);
4188
4189 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4190 if (IS_ERR(handle))
4191 return PTR_ERR(handle);
4192
4193 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4194 ext4_block_truncate_page(handle, mapping, inode->i_size);
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205 err = ext4_orphan_add(handle, inode);
4206 if (err)
4207 goto out_stop;
4208
4209 down_write(&EXT4_I(inode)->i_data_sem);
4210
4211 ext4_discard_preallocations(inode);
4212
4213 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4214 err = ext4_ext_truncate(handle, inode);
4215 else
4216 ext4_ind_truncate(handle, inode);
4217
4218 up_write(&ei->i_data_sem);
4219 if (err)
4220 goto out_stop;
4221
4222 if (IS_SYNC(inode))
4223 ext4_handle_sync(handle);
4224
4225out_stop:
4226
4227
4228
4229
4230
4231
4232
4233 if (inode->i_nlink)
4234 ext4_orphan_del(handle, inode);
4235
4236 inode->i_mtime = inode->i_ctime = current_time(inode);
4237 ext4_mark_inode_dirty(handle, inode);
4238 ext4_journal_stop(handle);
4239
4240 trace_ext4_truncate_exit(inode);
4241 return err;
4242}
4243
4244
4245
4246
4247
4248
4249
4250static int __ext4_get_inode_loc(struct inode *inode,
4251 struct ext4_iloc *iloc, int in_mem)
4252{
4253 struct ext4_group_desc *gdp;
4254 struct buffer_head *bh;
4255 struct super_block *sb = inode->i_sb;
4256 ext4_fsblk_t block;
4257 struct blk_plug plug;
4258 int inodes_per_block, inode_offset;
4259
4260 iloc->bh = NULL;
4261 if (inode->i_ino < EXT4_ROOT_INO ||
4262 inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
4263 return -EFSCORRUPTED;
4264
4265 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4266 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4267 if (!gdp)
4268 return -EIO;
4269
4270
4271
4272
4273 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4274 inode_offset = ((inode->i_ino - 1) %
4275 EXT4_INODES_PER_GROUP(sb));
4276 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4277 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4278
4279 bh = sb_getblk(sb, block);
4280 if (unlikely(!bh))
4281 return -ENOMEM;
4282 if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
4283 goto simulate_eio;
4284 if (!buffer_uptodate(bh)) {
4285 lock_buffer(bh);
4286
4287
4288
4289
4290
4291
4292
4293 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4294 set_buffer_uptodate(bh);
4295
4296 if (buffer_uptodate(bh)) {
4297
4298 unlock_buffer(bh);
4299 goto has_buffer;
4300 }
4301
4302
4303
4304
4305
4306
4307 if (in_mem) {
4308 struct buffer_head *bitmap_bh;
4309 int i, start;
4310
4311 start = inode_offset & ~(inodes_per_block - 1);
4312
4313
4314 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
4315 if (unlikely(!bitmap_bh))
4316 goto make_io;
4317
4318
4319
4320
4321
4322
4323 if (!buffer_uptodate(bitmap_bh)) {
4324 brelse(bitmap_bh);
4325 goto make_io;
4326 }
4327 for (i = start; i < start + inodes_per_block; i++) {
4328 if (i == inode_offset)
4329 continue;
4330 if (ext4_test_bit(i, bitmap_bh->b_data))
4331 break;
4332 }
4333 brelse(bitmap_bh);
4334 if (i == start + inodes_per_block) {
4335
4336 memset(bh->b_data, 0, bh->b_size);
4337 set_buffer_uptodate(bh);
4338 unlock_buffer(bh);
4339 goto has_buffer;
4340 }
4341 }
4342
4343make_io:
4344
4345
4346
4347
4348 blk_start_plug(&plug);
4349 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4350 ext4_fsblk_t b, end, table;
4351 unsigned num;
4352 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
4353
4354 table = ext4_inode_table(sb, gdp);
4355
4356 b = block & ~((ext4_fsblk_t) ra_blks - 1);
4357 if (table > b)
4358 b = table;
4359 end = b + ra_blks;
4360 num = EXT4_INODES_PER_GROUP(sb);
4361 if (ext4_has_group_desc_csum(sb))
4362 num -= ext4_itable_unused_count(sb, gdp);
4363 table += num / inodes_per_block;
4364 if (end > table)
4365 end = table;
4366 while (b <= end)
4367 sb_breadahead_unmovable(sb, b++);
4368 }
4369
4370
4371
4372
4373
4374
4375 trace_ext4_load_inode(inode);
4376 get_bh(bh);
4377 bh->b_end_io = end_buffer_read_sync;
4378 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
4379 blk_finish_plug(&plug);
4380 wait_on_buffer(bh);
4381 if (!buffer_uptodate(bh)) {
4382 simulate_eio:
4383 ext4_error_inode_block(inode, block, EIO,
4384 "unable to read itable block");
4385 brelse(bh);
4386 return -EIO;
4387 }
4388 }
4389has_buffer:
4390 iloc->bh = bh;
4391 return 0;
4392}
4393
4394int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
4395{
4396
4397 return __ext4_get_inode_loc(inode, iloc,
4398 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
4399}
4400
4401static bool ext4_should_use_dax(struct inode *inode)
4402{
4403 if (!test_opt(inode->i_sb, DAX))
4404 return false;
4405 if (!S_ISREG(inode->i_mode))
4406 return false;
4407 if (ext4_should_journal_data(inode))
4408 return false;
4409 if (ext4_has_inline_data(inode))
4410 return false;
4411 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
4412 return false;
4413 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4414 return false;
4415 return true;
4416}
4417
4418void ext4_set_inode_flags(struct inode *inode)
4419{
4420 unsigned int flags = EXT4_I(inode)->i_flags;
4421 unsigned int new_fl = 0;
4422
4423 if (flags & EXT4_SYNC_FL)
4424 new_fl |= S_SYNC;
4425 if (flags & EXT4_APPEND_FL)
4426 new_fl |= S_APPEND;
4427 if (flags & EXT4_IMMUTABLE_FL)
4428 new_fl |= S_IMMUTABLE;
4429 if (flags & EXT4_NOATIME_FL)
4430 new_fl |= S_NOATIME;
4431 if (flags & EXT4_DIRSYNC_FL)
4432 new_fl |= S_DIRSYNC;
4433 if (ext4_should_use_dax(inode))
4434 new_fl |= S_DAX;
4435 if (flags & EXT4_ENCRYPT_FL)
4436 new_fl |= S_ENCRYPTED;
4437 if (flags & EXT4_CASEFOLD_FL)
4438 new_fl |= S_CASEFOLD;
4439 if (flags & EXT4_VERITY_FL)
4440 new_fl |= S_VERITY;
4441 inode_set_flags(inode, new_fl,
4442 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
4443 S_ENCRYPTED|S_CASEFOLD|S_VERITY);
4444}
4445
4446static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
4447 struct ext4_inode_info *ei)
4448{
4449 blkcnt_t i_blocks ;
4450 struct inode *inode = &(ei->vfs_inode);
4451 struct super_block *sb = inode->i_sb;
4452
4453 if (ext4_has_feature_huge_file(sb)) {
4454
4455 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4456 le32_to_cpu(raw_inode->i_blocks_lo);
4457 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
4458
4459 return i_blocks << (inode->i_blkbits - 9);
4460 } else {
4461 return i_blocks;
4462 }
4463 } else {
4464 return le32_to_cpu(raw_inode->i_blocks_lo);
4465 }
4466}
4467
4468static inline int ext4_iget_extra_inode(struct inode *inode,
4469 struct ext4_inode *raw_inode,
4470 struct ext4_inode_info *ei)
4471{
4472 __le32 *magic = (void *)raw_inode +
4473 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
4474
4475 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
4476 EXT4_INODE_SIZE(inode->i_sb) &&
4477 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
4478 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
4479 return ext4_find_inline_data_nolock(inode);
4480 } else
4481 EXT4_I(inode)->i_inline_off = 0;
4482 return 0;
4483}
4484
4485int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4486{
4487 if (!ext4_has_feature_project(inode->i_sb))
4488 return -EOPNOTSUPP;
4489 *projid = EXT4_I(inode)->i_projid;
4490 return 0;
4491}
4492
4493
4494
4495
4496
4497
4498static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
4499{
4500 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4501 inode_set_iversion_raw(inode, val);
4502 else
4503 inode_set_iversion_queried(inode, val);
4504}
4505static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
4506{
4507 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4508 return inode_peek_iversion_raw(inode);
4509 else
4510 return inode_peek_iversion(inode);
4511}
4512
4513struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
4514 ext4_iget_flags flags, const char *function,
4515 unsigned int line)
4516{
4517 struct ext4_iloc iloc;
4518 struct ext4_inode *raw_inode;
4519 struct ext4_inode_info *ei;
4520 struct inode *inode;
4521 journal_t *journal = EXT4_SB(sb)->s_journal;
4522 long ret;
4523 loff_t size;
4524 int block;
4525 uid_t i_uid;
4526 gid_t i_gid;
4527 projid_t i_projid;
4528
4529 if ((!(flags & EXT4_IGET_SPECIAL) &&
4530 (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
4531 (ino < EXT4_ROOT_INO) ||
4532 (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
4533 if (flags & EXT4_IGET_HANDLE)
4534 return ERR_PTR(-ESTALE);
4535 __ext4_error(sb, function, line, EFSCORRUPTED, 0,
4536 "inode #%lu: comm %s: iget: illegal inode #",
4537 ino, current->comm);
4538 return ERR_PTR(-EFSCORRUPTED);
4539 }
4540
4541 inode = iget_locked(sb, ino);
4542 if (!inode)
4543 return ERR_PTR(-ENOMEM);
4544 if (!(inode->i_state & I_NEW))
4545 return inode;
4546
4547 ei = EXT4_I(inode);
4548 iloc.bh = NULL;
4549
4550 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4551 if (ret < 0)
4552 goto bad_inode;
4553 raw_inode = ext4_raw_inode(&iloc);
4554
4555 if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
4556 ext4_error_inode(inode, function, line, 0,
4557 "iget: root inode unallocated");
4558 ret = -EFSCORRUPTED;
4559 goto bad_inode;
4560 }
4561
4562 if ((flags & EXT4_IGET_HANDLE) &&
4563 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
4564 ret = -ESTALE;
4565 goto bad_inode;
4566 }
4567
4568 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4569 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4570 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4571 EXT4_INODE_SIZE(inode->i_sb) ||
4572 (ei->i_extra_isize & 3)) {
4573 ext4_error_inode(inode, function, line, 0,
4574 "iget: bad extra_isize %u "
4575 "(inode size %u)",
4576 ei->i_extra_isize,
4577 EXT4_INODE_SIZE(inode->i_sb));
4578 ret = -EFSCORRUPTED;
4579 goto bad_inode;
4580 }
4581 } else
4582 ei->i_extra_isize = 0;
4583
4584
4585 if (ext4_has_metadata_csum(sb)) {
4586 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4587 __u32 csum;
4588 __le32 inum = cpu_to_le32(inode->i_ino);
4589 __le32 gen = raw_inode->i_generation;
4590 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4591 sizeof(inum));
4592 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4593 sizeof(gen));
4594 }
4595
4596 if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4597 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
4598 ext4_error_inode_err(inode, function, line, 0, EFSBADCRC,
4599 "iget: checksum invalid");
4600 ret = -EFSBADCRC;
4601 goto bad_inode;
4602 }
4603
4604 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4605 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4606 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
4607 if (ext4_has_feature_project(sb) &&
4608 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4609 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4610 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4611 else
4612 i_projid = EXT4_DEF_PROJID;
4613
4614 if (!(test_opt(inode->i_sb, NO_UID32))) {
4615 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4616 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4617 }
4618 i_uid_write(inode, i_uid);
4619 i_gid_write(inode, i_gid);
4620 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
4621 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
4622
4623 ext4_clear_state_flags(ei);
4624 ei->i_inline_off = 0;
4625 ei->i_dir_start_lookup = 0;
4626 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4627
4628
4629
4630
4631
4632 if (inode->i_nlink == 0) {
4633 if ((inode->i_mode == 0 ||
4634 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4635 ino != EXT4_BOOT_LOADER_INO) {
4636
4637 ret = -ESTALE;
4638 goto bad_inode;
4639 }
4640
4641
4642
4643
4644
4645
4646 }
4647 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
4648 ext4_set_inode_flags(inode);
4649 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
4650 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
4651 if (ext4_has_feature_64bit(sb))
4652 ei->i_file_acl |=
4653 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
4654 inode->i_size = ext4_isize(sb, raw_inode);
4655 if ((size = i_size_read(inode)) < 0) {
4656 ext4_error_inode(inode, function, line, 0,
4657 "iget: bad i_size value: %lld", size);
4658 ret = -EFSCORRUPTED;
4659 goto bad_inode;
4660 }
4661
4662
4663
4664
4665
4666 if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) &&
4667 ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
4668 ext4_error_inode(inode, function, line, 0,
4669 "iget: Dir with htree data on filesystem without dir_index feature.");
4670 ret = -EFSCORRUPTED;
4671 goto bad_inode;
4672 }
4673 ei->i_disksize = inode->i_size;
4674#ifdef CONFIG_QUOTA
4675 ei->i_reserved_quota = 0;
4676#endif
4677 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4678 ei->i_block_group = iloc.block_group;
4679 ei->i_last_alloc_group = ~0;
4680
4681
4682
4683
4684 for (block = 0; block < EXT4_N_BLOCKS; block++)
4685 ei->i_data[block] = raw_inode->i_block[block];
4686 INIT_LIST_HEAD(&ei->i_orphan);
4687
4688
4689
4690
4691
4692
4693
4694
4695 if (journal) {
4696 transaction_t *transaction;
4697 tid_t tid;
4698
4699 read_lock(&journal->j_state_lock);
4700 if (journal->j_running_transaction)
4701 transaction = journal->j_running_transaction;
4702 else
4703 transaction = journal->j_committing_transaction;
4704 if (transaction)
4705 tid = transaction->t_tid;
4706 else
4707 tid = journal->j_commit_sequence;
4708 read_unlock(&journal->j_state_lock);
4709 ei->i_sync_tid = tid;
4710 ei->i_datasync_tid = tid;
4711 }
4712
4713 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4714 if (ei->i_extra_isize == 0) {
4715
4716 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
4717 ei->i_extra_isize = sizeof(struct ext4_inode) -
4718 EXT4_GOOD_OLD_INODE_SIZE;
4719 } else {
4720 ret = ext4_iget_extra_inode(inode, raw_inode, ei);
4721 if (ret)
4722 goto bad_inode;
4723 }
4724 }
4725
4726 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4727 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4728 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4729 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4730
4731 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
4732 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
4733
4734 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4735 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4736 ivers |=
4737 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4738 }
4739 ext4_inode_set_iversion_queried(inode, ivers);
4740 }
4741
4742 ret = 0;
4743 if (ei->i_file_acl &&
4744 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
4745 ext4_error_inode(inode, function, line, 0,
4746 "iget: bad extended attribute block %llu",
4747 ei->i_file_acl);
4748 ret = -EFSCORRUPTED;
4749 goto bad_inode;
4750 } else if (!ext4_has_inline_data(inode)) {
4751
4752 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4753 (S_ISLNK(inode->i_mode) &&
4754 !ext4_inode_is_fast_symlink(inode))) {
4755 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4756 ret = ext4_ext_check_inode(inode);
4757 else
4758 ret = ext4_ind_check_inode(inode);
4759 }
4760 }
4761 if (ret)
4762 goto bad_inode;
4763
4764 if (S_ISREG(inode->i_mode)) {
4765 inode->i_op = &ext4_file_inode_operations;
4766 inode->i_fop = &ext4_file_operations;
4767 ext4_set_aops(inode);
4768 } else if (S_ISDIR(inode->i_mode)) {
4769 inode->i_op = &ext4_dir_inode_operations;
4770 inode->i_fop = &ext4_dir_operations;
4771 } else if (S_ISLNK(inode->i_mode)) {
4772
4773 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
4774 ext4_error_inode(inode, function, line, 0,
4775 "iget: immutable or append flags "
4776 "not allowed on symlinks");
4777 ret = -EFSCORRUPTED;
4778 goto bad_inode;
4779 }
4780 if (IS_ENCRYPTED(inode)) {
4781 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4782 ext4_set_aops(inode);
4783 } else if (ext4_inode_is_fast_symlink(inode)) {
4784 inode->i_link = (char *)ei->i_data;
4785 inode->i_op = &ext4_fast_symlink_inode_operations;
4786 nd_terminate_link(ei->i_data, inode->i_size,
4787 sizeof(ei->i_data) - 1);
4788 } else {
4789 inode->i_op = &ext4_symlink_inode_operations;
4790 ext4_set_aops(inode);
4791 }
4792 inode_nohighmem(inode);
4793 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4794 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
4795 inode->i_op = &ext4_special_inode_operations;
4796 if (raw_inode->i_block[0])
4797 init_special_inode(inode, inode->i_mode,
4798 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4799 else
4800 init_special_inode(inode, inode->i_mode,
4801 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
4802 } else if (ino == EXT4_BOOT_LOADER_INO) {
4803 make_bad_inode(inode);
4804 } else {
4805 ret = -EFSCORRUPTED;
4806 ext4_error_inode(inode, function, line, 0,
4807 "iget: bogus i_mode (%o)", inode->i_mode);
4808 goto bad_inode;
4809 }
4810 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
4811 ext4_error_inode(inode, function, line, 0,
4812 "casefold flag without casefold feature");
4813 brelse(iloc.bh);
4814
4815 unlock_new_inode(inode);
4816 return inode;
4817
4818bad_inode:
4819 brelse(iloc.bh);
4820 iget_failed(inode);
4821 return ERR_PTR(ret);
4822}
4823
4824static int ext4_inode_blocks_set(handle_t *handle,
4825 struct ext4_inode *raw_inode,
4826 struct ext4_inode_info *ei)
4827{
4828 struct inode *inode = &(ei->vfs_inode);
4829 u64 i_blocks = READ_ONCE(inode->i_blocks);
4830 struct super_block *sb = inode->i_sb;
4831
4832 if (i_blocks <= ~0U) {
4833
4834
4835
4836
4837 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4838 raw_inode->i_blocks_high = 0;
4839 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4840 return 0;
4841 }
4842 if (!ext4_has_feature_huge_file(sb))
4843 return -EFBIG;
4844
4845 if (i_blocks <= 0xffffffffffffULL) {
4846
4847
4848
4849
4850 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4851 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
4852 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4853 } else {
4854 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
4855
4856 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4857 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4858 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
4859 }
4860 return 0;
4861}
4862
4863struct other_inode {
4864 unsigned long orig_ino;
4865 struct ext4_inode *raw_inode;
4866};
4867
4868static int other_inode_match(struct inode * inode, unsigned long ino,
4869 void *data)
4870{
4871 struct other_inode *oi = (struct other_inode *) data;
4872
4873 if ((inode->i_ino != ino) ||
4874 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4875 I_DIRTY_INODE)) ||
4876 ((inode->i_state & I_DIRTY_TIME) == 0))
4877 return 0;
4878 spin_lock(&inode->i_lock);
4879 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4880 I_DIRTY_INODE)) == 0) &&
4881 (inode->i_state & I_DIRTY_TIME)) {
4882 struct ext4_inode_info *ei = EXT4_I(inode);
4883
4884 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4885 spin_unlock(&inode->i_lock);
4886
4887 spin_lock(&ei->i_raw_lock);
4888 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4889 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4890 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4891 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4892 spin_unlock(&ei->i_raw_lock);
4893 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4894 return -1;
4895 }
4896 spin_unlock(&inode->i_lock);
4897 return -1;
4898}
4899
4900
4901
4902
4903
4904static void ext4_update_other_inodes_time(struct super_block *sb,
4905 unsigned long orig_ino, char *buf)
4906{
4907 struct other_inode oi;
4908 unsigned long ino;
4909 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4910 int inode_size = EXT4_INODE_SIZE(sb);
4911
4912 oi.orig_ino = orig_ino;
4913
4914
4915
4916
4917
4918 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
4919 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4920 if (ino == orig_ino)
4921 continue;
4922 oi.raw_inode = (struct ext4_inode *) buf;
4923 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4924 }
4925}
4926
4927
4928
4929
4930
4931
4932
4933
4934static int ext4_do_update_inode(handle_t *handle,
4935 struct inode *inode,
4936 struct ext4_iloc *iloc)
4937{
4938 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4939 struct ext4_inode_info *ei = EXT4_I(inode);
4940 struct buffer_head *bh = iloc->bh;
4941 struct super_block *sb = inode->i_sb;
4942 int err = 0, rc, block;
4943 int need_datasync = 0, set_large_file = 0;
4944 uid_t i_uid;
4945 gid_t i_gid;
4946 projid_t i_projid;
4947
4948 spin_lock(&ei->i_raw_lock);
4949
4950
4951
4952 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
4953 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
4954
4955 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
4956 i_uid = i_uid_read(inode);
4957 i_gid = i_gid_read(inode);
4958 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
4959 if (!(test_opt(inode->i_sb, NO_UID32))) {
4960 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4961 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
4962
4963
4964
4965
4966 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4967 raw_inode->i_uid_high = 0;
4968 raw_inode->i_gid_high = 0;
4969 } else {
4970 raw_inode->i_uid_high =
4971 cpu_to_le16(high_16_bits(i_uid));
4972 raw_inode->i_gid_high =
4973 cpu_to_le16(high_16_bits(i_gid));
4974 }
4975 } else {
4976 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4977 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
4978 raw_inode->i_uid_high = 0;
4979 raw_inode->i_gid_high = 0;
4980 }
4981 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
4982
4983 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4984 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4985 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4986 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4987
4988 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4989 if (err) {
4990 spin_unlock(&ei->i_raw_lock);
4991 goto out_brelse;
4992 }
4993 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
4994 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
4995 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
4996 raw_inode->i_file_acl_high =
4997 cpu_to_le16(ei->i_file_acl >> 32);
4998 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
4999 if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) {
5000 ext4_isize_set(raw_inode, ei->i_disksize);
5001 need_datasync = 1;
5002 }
5003 if (ei->i_disksize > 0x7fffffffULL) {
5004 if (!ext4_has_feature_large_file(sb) ||
5005 EXT4_SB(sb)->s_es->s_rev_level ==
5006 cpu_to_le32(EXT4_GOOD_OLD_REV))
5007 set_large_file = 1;
5008 }
5009 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5010 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5011 if (old_valid_dev(inode->i_rdev)) {
5012 raw_inode->i_block[0] =
5013 cpu_to_le32(old_encode_dev(inode->i_rdev));
5014 raw_inode->i_block[1] = 0;
5015 } else {
5016 raw_inode->i_block[0] = 0;
5017 raw_inode->i_block[1] =
5018 cpu_to_le32(new_encode_dev(inode->i_rdev));
5019 raw_inode->i_block[2] = 0;
5020 }
5021 } else if (!ext4_has_inline_data(inode)) {
5022 for (block = 0; block < EXT4_N_BLOCKS; block++)
5023 raw_inode->i_block[block] = ei->i_data[block];
5024 }
5025
5026 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
5027 u64 ivers = ext4_inode_peek_iversion(inode);
5028
5029 raw_inode->i_disk_version = cpu_to_le32(ivers);
5030 if (ei->i_extra_isize) {
5031 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5032 raw_inode->i_version_hi =
5033 cpu_to_le32(ivers >> 32);
5034 raw_inode->i_extra_isize =
5035 cpu_to_le16(ei->i_extra_isize);
5036 }
5037 }
5038
5039 BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
5040 i_projid != EXT4_DEF_PROJID);
5041
5042 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
5043 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
5044 raw_inode->i_projid = cpu_to_le32(i_projid);
5045
5046 ext4_inode_csum_set(inode, raw_inode, ei);
5047 spin_unlock(&ei->i_raw_lock);
5048 if (inode->i_sb->s_flags & SB_LAZYTIME)
5049 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
5050 bh->b_data);
5051
5052 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
5053 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
5054 if (!err)
5055 err = rc;
5056 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
5057 if (set_large_file) {
5058 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
5059 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
5060 if (err)
5061 goto out_brelse;
5062 ext4_set_feature_large_file(sb);
5063 ext4_handle_sync(handle);
5064 err = ext4_handle_dirty_super(handle, sb);
5065 }
5066 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
5067out_brelse:
5068 brelse(bh);
5069 ext4_std_error(inode->i_sb, err);
5070 return err;
5071}
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
5108{
5109 int err;
5110
5111 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
5112 sb_rdonly(inode->i_sb))
5113 return 0;
5114
5115 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5116 return -EIO;
5117
5118 if (EXT4_SB(inode->i_sb)->s_journal) {
5119 if (ext4_journal_current_handle()) {
5120 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5121 dump_stack();
5122 return -EIO;
5123 }
5124
5125
5126
5127
5128
5129
5130 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
5131 return 0;
5132
5133 err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5134 EXT4_I(inode)->i_sync_tid);
5135 } else {
5136 struct ext4_iloc iloc;
5137
5138 err = __ext4_get_inode_loc(inode, &iloc, 0);
5139 if (err)
5140 return err;
5141
5142
5143
5144
5145 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
5146 sync_dirty_buffer(iloc.bh);
5147 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
5148 ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO,
5149 "IO error syncing inode");
5150 err = -EIO;
5151 }
5152 brelse(iloc.bh);
5153 }
5154 return err;
5155}
5156
5157
5158
5159
5160
5161
5162static void ext4_wait_for_tail_page_commit(struct inode *inode)
5163{
5164 struct page *page;
5165 unsigned offset;
5166 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5167 tid_t commit_tid = 0;
5168 int ret;
5169
5170 offset = inode->i_size & (PAGE_SIZE - 1);
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
5181 return;
5182 while (1) {
5183 page = find_lock_page(inode->i_mapping,
5184 inode->i_size >> PAGE_SHIFT);
5185 if (!page)
5186 return;
5187 ret = __ext4_journalled_invalidatepage(page, offset,
5188 PAGE_SIZE - offset);
5189 unlock_page(page);
5190 put_page(page);
5191 if (ret != -EBUSY)
5192 return;
5193 commit_tid = 0;
5194 read_lock(&journal->j_state_lock);
5195 if (journal->j_committing_transaction)
5196 commit_tid = journal->j_committing_transaction->t_tid;
5197 read_unlock(&journal->j_state_lock);
5198 if (commit_tid)
5199 jbd2_log_wait_commit(journal, commit_tid);
5200 }
5201}
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227int ext4_setattr(struct dentry *dentry, struct iattr *attr)
5228{
5229 struct inode *inode = d_inode(dentry);
5230 int error, rc = 0;
5231 int orphan = 0;
5232 const unsigned int ia_valid = attr->ia_valid;
5233
5234 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5235 return -EIO;
5236
5237 if (unlikely(IS_IMMUTABLE(inode)))
5238 return -EPERM;
5239
5240 if (unlikely(IS_APPEND(inode) &&
5241 (ia_valid & (ATTR_MODE | ATTR_UID |
5242 ATTR_GID | ATTR_TIMES_SET))))
5243 return -EPERM;
5244
5245 error = setattr_prepare(dentry, attr);
5246 if (error)
5247 return error;
5248
5249 error = fscrypt_prepare_setattr(dentry, attr);
5250 if (error)
5251 return error;
5252
5253 error = fsverity_prepare_setattr(dentry, attr);
5254 if (error)
5255 return error;
5256
5257 if (is_quota_modification(inode, attr)) {
5258 error = dquot_initialize(inode);
5259 if (error)
5260 return error;
5261 }
5262 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5263 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
5264 handle_t *handle;
5265
5266
5267
5268 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5269 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5270 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
5271 if (IS_ERR(handle)) {
5272 error = PTR_ERR(handle);
5273 goto err_out;
5274 }
5275
5276
5277
5278
5279 down_read(&EXT4_I(inode)->xattr_sem);
5280 error = dquot_transfer(inode, attr);
5281 up_read(&EXT4_I(inode)->xattr_sem);
5282
5283 if (error) {
5284 ext4_journal_stop(handle);
5285 return error;
5286 }
5287
5288
5289 if (attr->ia_valid & ATTR_UID)
5290 inode->i_uid = attr->ia_uid;
5291 if (attr->ia_valid & ATTR_GID)
5292 inode->i_gid = attr->ia_gid;
5293 error = ext4_mark_inode_dirty(handle, inode);
5294 ext4_journal_stop(handle);
5295 }
5296
5297 if (attr->ia_valid & ATTR_SIZE) {
5298 handle_t *handle;
5299 loff_t oldsize = inode->i_size;
5300 int shrink = (attr->ia_size < inode->i_size);
5301
5302 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
5303 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5304
5305 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5306 return -EFBIG;
5307 }
5308 if (!S_ISREG(inode->i_mode))
5309 return -EINVAL;
5310
5311 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5312 inode_inc_iversion(inode);
5313
5314 if (shrink) {
5315 if (ext4_should_order_data(inode)) {
5316 error = ext4_begin_ordered_truncate(inode,
5317 attr->ia_size);
5318 if (error)
5319 goto err_out;
5320 }
5321
5322
5323
5324
5325 inode_dio_wait(inode);
5326 }
5327
5328 down_write(&EXT4_I(inode)->i_mmap_sem);
5329
5330 rc = ext4_break_layouts(inode);
5331 if (rc) {
5332 up_write(&EXT4_I(inode)->i_mmap_sem);
5333 return rc;
5334 }
5335
5336 if (attr->ia_size != inode->i_size) {
5337 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5338 if (IS_ERR(handle)) {
5339 error = PTR_ERR(handle);
5340 goto out_mmap_sem;
5341 }
5342 if (ext4_handle_valid(handle) && shrink) {
5343 error = ext4_orphan_add(handle, inode);
5344 orphan = 1;
5345 }
5346
5347
5348
5349
5350 if (!shrink) {
5351 inode->i_mtime = current_time(inode);
5352 inode->i_ctime = inode->i_mtime;
5353 }
5354 down_write(&EXT4_I(inode)->i_data_sem);
5355 EXT4_I(inode)->i_disksize = attr->ia_size;
5356 rc = ext4_mark_inode_dirty(handle, inode);
5357 if (!error)
5358 error = rc;
5359
5360
5361
5362
5363
5364 if (!error)
5365 i_size_write(inode, attr->ia_size);
5366 up_write(&EXT4_I(inode)->i_data_sem);
5367 ext4_journal_stop(handle);
5368 if (error)
5369 goto out_mmap_sem;
5370 if (!shrink) {
5371 pagecache_isize_extended(inode, oldsize,
5372 inode->i_size);
5373 } else if (ext4_should_journal_data(inode)) {
5374 ext4_wait_for_tail_page_commit(inode);
5375 }
5376 }
5377
5378
5379
5380
5381
5382 truncate_pagecache(inode, inode->i_size);
5383
5384
5385
5386
5387 if (attr->ia_size <= oldsize) {
5388 rc = ext4_truncate(inode);
5389 if (rc)
5390 error = rc;
5391 }
5392out_mmap_sem:
5393 up_write(&EXT4_I(inode)->i_mmap_sem);
5394 }
5395
5396 if (!error) {
5397 setattr_copy(inode, attr);
5398 mark_inode_dirty(inode);
5399 }
5400
5401
5402
5403
5404
5405 if (orphan && inode->i_nlink)
5406 ext4_orphan_del(NULL, inode);
5407
5408 if (!error && (ia_valid & ATTR_MODE))
5409 rc = posix_acl_chmod(inode, inode->i_mode);
5410
5411err_out:
5412 ext4_std_error(inode->i_sb, error);
5413 if (!error)
5414 error = rc;
5415 return error;
5416}
5417
5418int ext4_getattr(const struct path *path, struct kstat *stat,
5419 u32 request_mask, unsigned int query_flags)
5420{
5421 struct inode *inode = d_inode(path->dentry);
5422 struct ext4_inode *raw_inode;
5423 struct ext4_inode_info *ei = EXT4_I(inode);
5424 unsigned int flags;
5425
5426 if ((request_mask & STATX_BTIME) &&
5427 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
5428 stat->result_mask |= STATX_BTIME;
5429 stat->btime.tv_sec = ei->i_crtime.tv_sec;
5430 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
5431 }
5432
5433 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
5434 if (flags & EXT4_APPEND_FL)
5435 stat->attributes |= STATX_ATTR_APPEND;
5436 if (flags & EXT4_COMPR_FL)
5437 stat->attributes |= STATX_ATTR_COMPRESSED;
5438 if (flags & EXT4_ENCRYPT_FL)
5439 stat->attributes |= STATX_ATTR_ENCRYPTED;
5440 if (flags & EXT4_IMMUTABLE_FL)
5441 stat->attributes |= STATX_ATTR_IMMUTABLE;
5442 if (flags & EXT4_NODUMP_FL)
5443 stat->attributes |= STATX_ATTR_NODUMP;
5444 if (flags & EXT4_VERITY_FL)
5445 stat->attributes |= STATX_ATTR_VERITY;
5446
5447 stat->attributes_mask |= (STATX_ATTR_APPEND |
5448 STATX_ATTR_COMPRESSED |
5449 STATX_ATTR_ENCRYPTED |
5450 STATX_ATTR_IMMUTABLE |
5451 STATX_ATTR_NODUMP |
5452 STATX_ATTR_VERITY);
5453
5454 generic_fillattr(inode, stat);
5455 return 0;
5456}
5457
5458int ext4_file_getattr(const struct path *path, struct kstat *stat,
5459 u32 request_mask, unsigned int query_flags)
5460{
5461 struct inode *inode = d_inode(path->dentry);
5462 u64 delalloc_blocks;
5463
5464 ext4_getattr(path, stat, request_mask, query_flags);
5465
5466
5467
5468
5469
5470
5471
5472 if (unlikely(ext4_has_inline_data(inode)))
5473 stat->blocks += (stat->size + 511) >> 9;
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
5486 EXT4_I(inode)->i_reserved_data_blocks);
5487 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
5488 return 0;
5489}
5490
5491static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5492 int pextents)
5493{
5494 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
5495 return ext4_ind_trans_blocks(inode, lblocks);
5496 return ext4_ext_index_trans_blocks(inode, pextents);
5497}
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
5511 int pextents)
5512{
5513 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5514 int gdpblocks;
5515 int idxblocks;
5516 int ret = 0;
5517
5518
5519
5520
5521
5522 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
5523
5524 ret = idxblocks;
5525
5526
5527
5528
5529
5530 groups = idxblocks + pextents;
5531 gdpblocks = groups;
5532 if (groups > ngroups)
5533 groups = ngroups;
5534 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5535 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5536
5537
5538 ret += groups + gdpblocks;
5539
5540
5541 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
5542
5543 return ret;
5544}
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556int ext4_writepage_trans_blocks(struct inode *inode)
5557{
5558 int bpp = ext4_journal_blocks_per_page(inode);
5559 int ret;
5560
5561 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
5562
5563
5564 if (ext4_should_journal_data(inode))
5565 ret += bpp;
5566 return ret;
5567}
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5579{
5580 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5581}
5582
5583
5584
5585
5586
5587int ext4_mark_iloc_dirty(handle_t *handle,
5588 struct inode *inode, struct ext4_iloc *iloc)
5589{
5590 int err = 0;
5591
5592 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
5593 put_bh(iloc->bh);
5594 return -EIO;
5595 }
5596 if (IS_I_VERSION(inode))
5597 inode_inc_iversion(inode);
5598
5599
5600 get_bh(iloc->bh);
5601
5602
5603 err = ext4_do_update_inode(handle, inode, iloc);
5604 put_bh(iloc->bh);
5605 return err;
5606}
5607
5608
5609
5610
5611
5612
5613int
5614ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5615 struct ext4_iloc *iloc)
5616{
5617 int err;
5618
5619 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5620 return -EIO;
5621
5622 err = ext4_get_inode_loc(inode, iloc);
5623 if (!err) {
5624 BUFFER_TRACE(iloc->bh, "get_write_access");
5625 err = ext4_journal_get_write_access(handle, iloc->bh);
5626 if (err) {
5627 brelse(iloc->bh);
5628 iloc->bh = NULL;
5629 }
5630 }
5631 ext4_std_error(inode->i_sb, err);
5632 return err;
5633}
5634
5635static int __ext4_expand_extra_isize(struct inode *inode,
5636 unsigned int new_extra_isize,
5637 struct ext4_iloc *iloc,
5638 handle_t *handle, int *no_expand)
5639{
5640 struct ext4_inode *raw_inode;
5641 struct ext4_xattr_ibody_header *header;
5642 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
5643 struct ext4_inode_info *ei = EXT4_I(inode);
5644 int error;
5645
5646
5647 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
5648 (ei->i_extra_isize & 3)) {
5649 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
5650 ei->i_extra_isize,
5651 EXT4_INODE_SIZE(inode->i_sb));
5652 return -EFSCORRUPTED;
5653 }
5654 if ((new_extra_isize < ei->i_extra_isize) ||
5655 (new_extra_isize < 4) ||
5656 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
5657 return -EINVAL;
5658
5659 raw_inode = ext4_raw_inode(iloc);
5660
5661 header = IHDR(inode, raw_inode);
5662
5663
5664 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5665 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5666 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
5667 EXT4_I(inode)->i_extra_isize, 0,
5668 new_extra_isize - EXT4_I(inode)->i_extra_isize);
5669 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5670 return 0;
5671 }
5672
5673
5674 error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
5675 raw_inode, handle);
5676 if (error) {
5677
5678
5679
5680 *no_expand = 1;
5681 }
5682
5683 return error;
5684}
5685
5686
5687
5688
5689
5690static int ext4_try_to_expand_extra_isize(struct inode *inode,
5691 unsigned int new_extra_isize,
5692 struct ext4_iloc iloc,
5693 handle_t *handle)
5694{
5695 int no_expand;
5696 int error;
5697
5698 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
5699 return -EOVERFLOW;
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710 if (ext4_journal_extend(handle,
5711 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
5712 return -ENOSPC;
5713
5714 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
5715 return -EBUSY;
5716
5717 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
5718 handle, &no_expand);
5719 ext4_write_unlock_xattr(inode, &no_expand);
5720
5721 return error;
5722}
5723
5724int ext4_expand_extra_isize(struct inode *inode,
5725 unsigned int new_extra_isize,
5726 struct ext4_iloc *iloc)
5727{
5728 handle_t *handle;
5729 int no_expand;
5730 int error, rc;
5731
5732 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5733 brelse(iloc->bh);
5734 return -EOVERFLOW;
5735 }
5736
5737 handle = ext4_journal_start(inode, EXT4_HT_INODE,
5738 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
5739 if (IS_ERR(handle)) {
5740 error = PTR_ERR(handle);
5741 brelse(iloc->bh);
5742 return error;
5743 }
5744
5745 ext4_write_lock_xattr(inode, &no_expand);
5746
5747 BUFFER_TRACE(iloc->bh, "get_write_access");
5748 error = ext4_journal_get_write_access(handle, iloc->bh);
5749 if (error) {
5750 brelse(iloc->bh);
5751 goto out_unlock;
5752 }
5753
5754 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
5755 handle, &no_expand);
5756
5757 rc = ext4_mark_iloc_dirty(handle, inode, iloc);
5758 if (!error)
5759 error = rc;
5760
5761out_unlock:
5762 ext4_write_unlock_xattr(inode, &no_expand);
5763 ext4_journal_stop(handle);
5764 return error;
5765}
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
5781{
5782 struct ext4_iloc iloc;
5783 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5784 int err;
5785
5786 might_sleep();
5787 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
5788 err = ext4_reserve_inode_write(handle, inode, &iloc);
5789 if (err)
5790 return err;
5791
5792 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
5793 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
5794 iloc, handle);
5795
5796 return ext4_mark_iloc_dirty(handle, inode, &iloc);
5797}
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817void ext4_dirty_inode(struct inode *inode, int flags)
5818{
5819 handle_t *handle;
5820
5821 if (flags == I_DIRTY_TIME)
5822 return;
5823 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
5824 if (IS_ERR(handle))
5825 goto out;
5826
5827 ext4_mark_inode_dirty(handle, inode);
5828
5829 ext4_journal_stop(handle);
5830out:
5831 return;
5832}
5833
5834int ext4_change_inode_journal_flag(struct inode *inode, int val)
5835{
5836 journal_t *journal;
5837 handle_t *handle;
5838 int err;
5839 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851 journal = EXT4_JOURNAL(inode);
5852 if (!journal)
5853 return 0;
5854 if (is_journal_aborted(journal))
5855 return -EROFS;
5856
5857
5858 inode_dio_wait(inode);
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868 if (val) {
5869 down_write(&EXT4_I(inode)->i_mmap_sem);
5870 err = filemap_write_and_wait(inode->i_mapping);
5871 if (err < 0) {
5872 up_write(&EXT4_I(inode)->i_mmap_sem);
5873 return err;
5874 }
5875 }
5876
5877 percpu_down_write(&sbi->s_writepages_rwsem);
5878 jbd2_journal_lock_updates(journal);
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888 if (val)
5889 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
5890 else {
5891 err = jbd2_journal_flush(journal);
5892 if (err < 0) {
5893 jbd2_journal_unlock_updates(journal);
5894 percpu_up_write(&sbi->s_writepages_rwsem);
5895 return err;
5896 }
5897 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
5898 }
5899 ext4_set_aops(inode);
5900
5901 jbd2_journal_unlock_updates(journal);
5902 percpu_up_write(&sbi->s_writepages_rwsem);
5903
5904 if (val)
5905 up_write(&EXT4_I(inode)->i_mmap_sem);
5906
5907
5908
5909 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
5910 if (IS_ERR(handle))
5911 return PTR_ERR(handle);
5912
5913 err = ext4_mark_inode_dirty(handle, inode);
5914 ext4_handle_sync(handle);
5915 ext4_journal_stop(handle);
5916 ext4_std_error(inode->i_sb, err);
5917
5918 return err;
5919}
5920
5921static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5922{
5923 return !buffer_mapped(bh);
5924}
5925
5926vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
5927{
5928 struct vm_area_struct *vma = vmf->vma;
5929 struct page *page = vmf->page;
5930 loff_t size;
5931 unsigned long len;
5932 int err;
5933 vm_fault_t ret;
5934 struct file *file = vma->vm_file;
5935 struct inode *inode = file_inode(file);
5936 struct address_space *mapping = inode->i_mapping;
5937 handle_t *handle;
5938 get_block_t *get_block;
5939 int retries = 0;
5940
5941 if (unlikely(IS_IMMUTABLE(inode)))
5942 return VM_FAULT_SIGBUS;
5943
5944 sb_start_pagefault(inode->i_sb);
5945 file_update_time(vma->vm_file);
5946
5947 down_read(&EXT4_I(inode)->i_mmap_sem);
5948
5949 err = ext4_convert_inline_data(inode);
5950 if (err)
5951 goto out_ret;
5952
5953
5954 if (test_opt(inode->i_sb, DELALLOC) &&
5955 !ext4_should_journal_data(inode) &&
5956 !ext4_nonda_switch(inode->i_sb)) {
5957 do {
5958 err = block_page_mkwrite(vma, vmf,
5959 ext4_da_get_block_prep);
5960 } while (err == -ENOSPC &&
5961 ext4_should_retry_alloc(inode->i_sb, &retries));
5962 goto out_ret;
5963 }
5964
5965 lock_page(page);
5966 size = i_size_read(inode);
5967
5968 if (page->mapping != mapping || page_offset(page) > size) {
5969 unlock_page(page);
5970 ret = VM_FAULT_NOPAGE;
5971 goto out;
5972 }
5973
5974 if (page->index == size >> PAGE_SHIFT)
5975 len = size & ~PAGE_MASK;
5976 else
5977 len = PAGE_SIZE;
5978
5979
5980
5981
5982 if (page_has_buffers(page)) {
5983 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5984 0, len, NULL,
5985 ext4_bh_unmapped)) {
5986
5987 wait_for_stable_page(page);
5988 ret = VM_FAULT_LOCKED;
5989 goto out;
5990 }
5991 }
5992 unlock_page(page);
5993
5994 if (ext4_should_dioread_nolock(inode))
5995 get_block = ext4_get_block_unwritten;
5996 else
5997 get_block = ext4_get_block;
5998retry_alloc:
5999 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
6000 ext4_writepage_trans_blocks(inode));
6001 if (IS_ERR(handle)) {
6002 ret = VM_FAULT_SIGBUS;
6003 goto out;
6004 }
6005 err = block_page_mkwrite(vma, vmf, get_block);
6006 if (!err && ext4_should_journal_data(inode)) {
6007 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
6008 PAGE_SIZE, NULL, do_journal_get_write_access)) {
6009 unlock_page(page);
6010 ret = VM_FAULT_SIGBUS;
6011 ext4_journal_stop(handle);
6012 goto out;
6013 }
6014 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
6015 }
6016 ext4_journal_stop(handle);
6017 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
6018 goto retry_alloc;
6019out_ret:
6020 ret = block_page_mkwrite_return(err);
6021out:
6022 up_read(&EXT4_I(inode)->i_mmap_sem);
6023 sb_end_pagefault(inode->i_sb);
6024 return ret;
6025}
6026
6027vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
6028{
6029 struct inode *inode = file_inode(vmf->vma->vm_file);
6030 vm_fault_t ret;
6031
6032 down_read(&EXT4_I(inode)->i_mmap_sem);
6033 ret = filemap_fault(vmf);
6034 up_read(&EXT4_I(inode)->i_mmap_sem);
6035
6036 return ret;
6037}
6038