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