Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2 | * fs/f2fs/data.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/mpage.h> |
| 15 | #include <linux/writeback.h> |
| 16 | #include <linux/backing-dev.h> |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 17 | #include <linux/pagevec.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/bio.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 20 | #include <linux/prefetch.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 21 | #include <linux/uio.h> |
Jaegeuk Kim | fe76b79 | 2016-06-30 18:40:10 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
| 23 | #include <linux/memcontrol.h> |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 24 | #include <linux/cleancache.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 25 | #include <linux/sched/signal.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 26 | |
| 27 | #include "f2fs.h" |
| 28 | #include "node.h" |
| 29 | #include "segment.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 30 | #include "trace.h" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 31 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 32 | |
Chao Yu | 36951b3 | 2016-11-16 10:41:20 +0800 | [diff] [blame] | 33 | static bool __is_cp_guaranteed(struct page *page) |
| 34 | { |
| 35 | struct address_space *mapping = page->mapping; |
| 36 | struct inode *inode; |
| 37 | struct f2fs_sb_info *sbi; |
| 38 | |
| 39 | if (!mapping) |
| 40 | return false; |
| 41 | |
| 42 | inode = mapping->host; |
| 43 | sbi = F2FS_I_SB(inode); |
| 44 | |
| 45 | if (inode->i_ino == F2FS_META_INO(sbi) || |
| 46 | inode->i_ino == F2FS_NODE_INO(sbi) || |
| 47 | S_ISDIR(inode->i_mode) || |
| 48 | is_cold_data(page)) |
| 49 | return true; |
| 50 | return false; |
| 51 | } |
| 52 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 53 | static void f2fs_read_end_io(struct bio *bio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 54 | { |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 55 | struct bio_vec *bvec; |
| 56 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 57 | |
Chao Yu | 8b038c7 | 2016-09-18 23:30:07 +0800 | [diff] [blame] | 58 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 59 | if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) { |
| 60 | f2fs_show_injection_info(FAULT_IO); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 61 | bio->bi_status = BLK_STS_IOERR; |
Chao Yu | 5552351 | 2017-02-25 11:08:28 +0800 | [diff] [blame] | 62 | } |
Chao Yu | 8b038c7 | 2016-09-18 23:30:07 +0800 | [diff] [blame] | 63 | #endif |
| 64 | |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 65 | if (f2fs_bio_encrypted(bio)) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 66 | if (bio->bi_status) { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 67 | fscrypt_release_ctx(bio->bi_private); |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 68 | } else { |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 69 | fscrypt_decrypt_bio_pages(bio->bi_private, bio); |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 74 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 75 | struct page *page = bvec->bv_page; |
| 76 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 77 | if (!bio->bi_status) { |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 78 | if (!PageUptodate(page)) |
| 79 | SetPageUptodate(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 80 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 81 | ClearPageUptodate(page); |
| 82 | SetPageError(page); |
| 83 | } |
| 84 | unlock_page(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 85 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 86 | bio_put(bio); |
| 87 | } |
| 88 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 89 | static void f2fs_write_end_io(struct bio *bio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 90 | { |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 91 | struct f2fs_sb_info *sbi = bio->bi_private; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 92 | struct bio_vec *bvec; |
| 93 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 94 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 95 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 96 | struct page *page = bvec->bv_page; |
Chao Yu | 36951b3 | 2016-11-16 10:41:20 +0800 | [diff] [blame] | 97 | enum count_type type = WB_DATA_TYPE(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 98 | |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 99 | if (IS_DUMMY_WRITTEN_PAGE(page)) { |
| 100 | set_page_private(page, (unsigned long)NULL); |
| 101 | ClearPagePrivate(page); |
| 102 | unlock_page(page); |
| 103 | mempool_free(page, sbi->write_io_dummy); |
| 104 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 105 | if (unlikely(bio->bi_status)) |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 106 | f2fs_stop_checkpoint(sbi, true); |
| 107 | continue; |
| 108 | } |
| 109 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 110 | fscrypt_pullback_bio_page(&page, true); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 111 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 112 | if (unlikely(bio->bi_status)) { |
Michal Hocko | 5114a97 | 2016-10-11 13:56:01 -0700 | [diff] [blame] | 113 | mapping_set_error(page->mapping, -EIO); |
Jaegeuk Kim | 38f91ca | 2016-05-18 14:07:56 -0700 | [diff] [blame] | 114 | f2fs_stop_checkpoint(sbi, true); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 115 | } |
Chao Yu | 36951b3 | 2016-11-16 10:41:20 +0800 | [diff] [blame] | 116 | dec_page_count(sbi, type); |
| 117 | clear_cold_data(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 118 | end_page_writeback(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 119 | } |
Chao Yu | 36951b3 | 2016-11-16 10:41:20 +0800 | [diff] [blame] | 120 | if (!get_pages(sbi, F2FS_WB_CP_DATA) && |
Jaegeuk Kim | f573018 | 2016-05-17 16:23:36 -0700 | [diff] [blame] | 121 | wq_has_sleeper(&sbi->cp_wait)) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 122 | wake_up(&sbi->cp_wait); |
| 123 | |
| 124 | bio_put(bio); |
| 125 | } |
| 126 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 127 | /* |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 128 | * Return true, if pre_bio's bdev is same as its target device. |
| 129 | */ |
| 130 | struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, |
| 131 | block_t blk_addr, struct bio *bio) |
| 132 | { |
| 133 | struct block_device *bdev = sbi->sb->s_bdev; |
| 134 | int i; |
| 135 | |
| 136 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 137 | if (FDEV(i).start_blk <= blk_addr && |
| 138 | FDEV(i).end_blk >= blk_addr) { |
| 139 | blk_addr -= FDEV(i).start_blk; |
| 140 | bdev = FDEV(i).bdev; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | if (bio) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 145 | bio_set_dev(bio, bdev); |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 146 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
| 147 | } |
| 148 | return bdev; |
| 149 | } |
| 150 | |
| 151 | int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | for (i = 0; i < sbi->s_ndevs; i++) |
| 156 | if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) |
| 157 | return i; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static bool __same_bdev(struct f2fs_sb_info *sbi, |
| 162 | block_t blk_addr, struct bio *bio) |
| 163 | { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 164 | struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL); |
| 165 | return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno; |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 169 | * Low-level block read/write IO operations. |
| 170 | */ |
| 171 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 172 | int npages, bool is_read) |
| 173 | { |
| 174 | struct bio *bio; |
| 175 | |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 176 | bio = f2fs_bio_alloc(npages); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 177 | |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 178 | f2fs_target_device(sbi, blk_addr, bio); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 179 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 180 | bio->bi_private = is_read ? NULL : sbi; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 181 | |
| 182 | return bio; |
| 183 | } |
| 184 | |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 185 | static inline void __submit_bio(struct f2fs_sb_info *sbi, |
| 186 | struct bio *bio, enum page_type type) |
Jaegeuk Kim | f573018 | 2016-05-17 16:23:36 -0700 | [diff] [blame] | 187 | { |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 188 | if (!is_read_io(bio_op(bio))) { |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 189 | unsigned int start; |
| 190 | |
Damien Le Moal | 0bfd7a0 | 2016-10-28 17:45:00 +0900 | [diff] [blame] | 191 | if (f2fs_sb_mounted_blkzoned(sbi->sb) && |
Jaegeuk Kim | 52763a4 | 2016-06-13 09:47:48 -0700 | [diff] [blame] | 192 | current->plug && (type == DATA || type == NODE)) |
Jaegeuk Kim | 19a5f5e | 2016-06-04 14:25:24 -0700 | [diff] [blame] | 193 | blk_finish_plug(current->plug); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 194 | |
| 195 | if (type != DATA && type != NODE) |
| 196 | goto submit_io; |
| 197 | |
| 198 | start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS; |
| 199 | start %= F2FS_IO_SIZE(sbi); |
| 200 | |
| 201 | if (start == 0) |
| 202 | goto submit_io; |
| 203 | |
| 204 | /* fill dummy pages */ |
| 205 | for (; start < F2FS_IO_SIZE(sbi); start++) { |
| 206 | struct page *page = |
| 207 | mempool_alloc(sbi->write_io_dummy, |
| 208 | GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL); |
| 209 | f2fs_bug_on(sbi, !page); |
| 210 | |
| 211 | SetPagePrivate(page); |
| 212 | set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE); |
| 213 | lock_page(page); |
| 214 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) |
| 215 | f2fs_bug_on(sbi, 1); |
| 216 | } |
| 217 | /* |
| 218 | * In the NODE case, we lose next block address chain. So, we |
| 219 | * need to do checkpoint in f2fs_sync_file. |
| 220 | */ |
| 221 | if (type == NODE) |
| 222 | set_sbi_flag(sbi, SBI_NEED_CP); |
Jaegeuk Kim | 19a5f5e | 2016-06-04 14:25:24 -0700 | [diff] [blame] | 223 | } |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 224 | submit_io: |
Jaegeuk Kim | 554b512 | 2016-12-21 12:13:03 -0800 | [diff] [blame] | 225 | if (is_read_io(bio_op(bio))) |
| 226 | trace_f2fs_submit_read_bio(sbi->sb, type, bio); |
| 227 | else |
| 228 | trace_f2fs_submit_write_bio(sbi->sb, type, bio); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 229 | submit_bio(bio); |
Jaegeuk Kim | f573018 | 2016-05-17 16:23:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 232 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 233 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 234 | struct f2fs_io_info *fio = &io->fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 235 | |
| 236 | if (!io->bio) |
| 237 | return; |
| 238 | |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 239 | bio_set_op_attrs(io->bio, fio->op, fio->op_flags); |
| 240 | |
Jaegeuk Kim | 554b512 | 2016-12-21 12:13:03 -0800 | [diff] [blame] | 241 | if (is_read_io(fio->op)) |
| 242 | trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); |
| 243 | else |
| 244 | trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); |
| 245 | |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 246 | __submit_bio(io->sbi, io->bio, fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 247 | io->bio = NULL; |
| 248 | } |
| 249 | |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 250 | static bool __has_merged_page(struct f2fs_bio_info *io, |
| 251 | struct inode *inode, nid_t ino, pgoff_t idx) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 252 | { |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 253 | struct bio_vec *bvec; |
| 254 | struct page *target; |
| 255 | int i; |
| 256 | |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 257 | if (!io->bio) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 258 | return false; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 259 | |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 260 | if (!inode && !ino) |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 261 | return true; |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 262 | |
| 263 | bio_for_each_segment_all(bvec, io->bio, i) { |
| 264 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 265 | if (bvec->bv_page->mapping) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 266 | target = bvec->bv_page; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 267 | else |
| 268 | target = fscrypt_control_page(bvec->bv_page); |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 269 | |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 270 | if (idx != target->index) |
| 271 | continue; |
| 272 | |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 273 | if (inode && inode == target->mapping->host) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 274 | return true; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 275 | if (ino && ino == ino_of_node(target)) |
| 276 | return true; |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 277 | } |
| 278 | |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 282 | static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode, |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 283 | nid_t ino, pgoff_t idx, enum page_type type) |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 284 | { |
| 285 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 286 | enum temp_type temp; |
| 287 | struct f2fs_bio_info *io; |
| 288 | bool ret = false; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 289 | |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 290 | for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { |
| 291 | io = sbi->write_io[btype] + temp; |
| 292 | |
| 293 | down_read(&io->io_rwsem); |
| 294 | ret = __has_merged_page(io, inode, ino, idx); |
| 295 | up_read(&io->io_rwsem); |
| 296 | |
| 297 | /* TODO: use HOT temp only for meta pages now. */ |
| 298 | if (ret || btype == META) |
| 299 | break; |
| 300 | } |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 301 | return ret; |
| 302 | } |
| 303 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 304 | static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 305 | enum page_type type, enum temp_type temp) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 306 | { |
| 307 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 308 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 309 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 310 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 311 | |
| 312 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 313 | if (type >= META_FLUSH) { |
| 314 | io->fio.type = META_FLUSH; |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 315 | io->fio.op = REQ_OP_WRITE; |
Jan Kara | 3adc5fcb | 2017-05-02 17:03:47 +0200 | [diff] [blame] | 316 | io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 317 | if (!test_opt(sbi, NOBARRIER)) |
Jaegeuk Kim | 7f54f51 | 2017-02-06 13:57:58 -0800 | [diff] [blame] | 318 | io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 319 | } |
| 320 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 321 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 324 | static void __submit_merged_write_cond(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 325 | struct inode *inode, nid_t ino, pgoff_t idx, |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 326 | enum page_type type, bool force) |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 327 | { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 328 | enum temp_type temp; |
| 329 | |
| 330 | if (!force && !has_merged_page(sbi, inode, ino, idx, type)) |
| 331 | return; |
| 332 | |
| 333 | for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { |
| 334 | |
| 335 | __f2fs_submit_merged_write(sbi, type, temp); |
| 336 | |
| 337 | /* TODO: use HOT temp only for meta pages now. */ |
| 338 | if (type >= META) |
| 339 | break; |
| 340 | } |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 343 | void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) |
Chao Yu | 406657d | 2016-02-24 17:17:55 +0800 | [diff] [blame] | 344 | { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 345 | __submit_merged_write_cond(sbi, NULL, 0, 0, type, true); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 346 | } |
| 347 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 348 | void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 349 | struct inode *inode, nid_t ino, pgoff_t idx, |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 350 | enum page_type type) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 351 | { |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 352 | __submit_merged_write_cond(sbi, inode, ino, idx, type, false); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 353 | } |
| 354 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 355 | void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) |
Chao Yu | 406657d | 2016-02-24 17:17:55 +0800 | [diff] [blame] | 356 | { |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 357 | f2fs_submit_merged_write(sbi, DATA); |
| 358 | f2fs_submit_merged_write(sbi, NODE); |
| 359 | f2fs_submit_merged_write(sbi, META); |
Chao Yu | 406657d | 2016-02-24 17:17:55 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 362 | /* |
| 363 | * Fill the locked page with data located in the block address. |
Tomohiro Kusumi | 771a9a7 | 2017-04-05 22:49:44 +0300 | [diff] [blame] | 364 | * A caller needs to unlock the page on failure. |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 365 | */ |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 366 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 367 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 368 | struct bio *bio; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 369 | struct page *page = fio->encrypted_page ? |
| 370 | fio->encrypted_page : fio->page; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 371 | |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 372 | trace_f2fs_submit_page_bio(page, fio); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 373 | f2fs_trace_ios(fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 374 | |
| 375 | /* Allocate a new bio */ |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 376 | bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 377 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 378 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 379 | bio_put(bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 380 | return -EFAULT; |
| 381 | } |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 382 | bio_set_op_attrs(bio, fio->op, fio->op_flags); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 383 | |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 384 | __submit_bio(fio->sbi, bio, fio->type); |
Jaegeuk Kim | d1b3e72 | 2017-03-30 21:02:46 -0700 | [diff] [blame] | 385 | |
| 386 | if (!is_read_io(fio->op)) |
| 387 | inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 391 | int f2fs_submit_page_write(struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 392 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 393 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 394 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 395 | struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 396 | struct page *bio_page; |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 397 | int err = 0; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 398 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 399 | f2fs_bug_on(sbi, is_read_io(fio->op)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 400 | |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 401 | down_write(&io->io_rwsem); |
| 402 | next: |
| 403 | if (fio->in_list) { |
| 404 | spin_lock(&io->io_lock); |
| 405 | if (list_empty(&io->io_list)) { |
| 406 | spin_unlock(&io->io_lock); |
| 407 | goto out_fail; |
| 408 | } |
| 409 | fio = list_first_entry(&io->io_list, |
| 410 | struct f2fs_io_info, list); |
| 411 | list_del(&fio->list); |
| 412 | spin_unlock(&io->io_lock); |
| 413 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 414 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 415 | if (fio->old_blkaddr != NEW_ADDR) |
| 416 | verify_block_addr(sbi, fio->old_blkaddr); |
| 417 | verify_block_addr(sbi, fio->new_blkaddr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 418 | |
Chao Yu | 36951b3 | 2016-11-16 10:41:20 +0800 | [diff] [blame] | 419 | bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
| 420 | |
Thomas Meyer | ebf7c52 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 421 | /* set submitted = true as a return value */ |
| 422 | fio->submitted = true; |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 423 | |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 424 | inc_page_count(sbi, WB_DATA_TYPE(bio_page)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 425 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 426 | if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 || |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 427 | (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) || |
| 428 | !__same_bdev(sbi, fio->new_blkaddr, io->bio))) |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 429 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 430 | alloc_new: |
| 431 | if (io->bio == NULL) { |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 432 | if ((fio->type == DATA || fio->type == NODE) && |
| 433 | fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) { |
| 434 | err = -EAGAIN; |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 435 | dec_page_count(sbi, WB_DATA_TYPE(bio_page)); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 436 | goto out_fail; |
| 437 | } |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 438 | io->bio = __bio_alloc(sbi, fio->new_blkaddr, |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 439 | BIO_MAX_PAGES, false); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 440 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 441 | } |
| 442 | |
Jaegeuk Kim | a912b54 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 443 | if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 444 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 445 | goto alloc_new; |
| 446 | } |
| 447 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 448 | io->last_block_in_bio = fio->new_blkaddr; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 449 | f2fs_trace_ios(fio, 0); |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 450 | |
| 451 | trace_f2fs_submit_page_write(fio->page, fio); |
| 452 | |
| 453 | if (fio->in_list) |
| 454 | goto next; |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 455 | out_fail: |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 456 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 0a595eb | 2016-12-14 10:12:56 -0800 | [diff] [blame] | 457 | return err; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 458 | } |
| 459 | |
Jaegeuk Kim | 13ba41e | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 460 | static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, |
| 461 | unsigned nr_pages) |
| 462 | { |
| 463 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 464 | struct fscrypt_ctx *ctx = NULL; |
| 465 | struct bio *bio; |
| 466 | |
| 467 | if (f2fs_encrypted_file(inode)) { |
| 468 | ctx = fscrypt_get_ctx(inode, GFP_NOFS); |
| 469 | if (IS_ERR(ctx)) |
| 470 | return ERR_CAST(ctx); |
| 471 | |
| 472 | /* wait the page to be moved by cleaning */ |
| 473 | f2fs_wait_on_block_writeback(sbi, blkaddr); |
| 474 | } |
| 475 | |
| 476 | bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES)); |
| 477 | if (!bio) { |
| 478 | if (ctx) |
| 479 | fscrypt_release_ctx(ctx); |
| 480 | return ERR_PTR(-ENOMEM); |
| 481 | } |
| 482 | f2fs_target_device(sbi, blkaddr, bio); |
| 483 | bio->bi_end_io = f2fs_read_end_io; |
| 484 | bio->bi_private = ctx; |
| 485 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 486 | |
| 487 | return bio; |
| 488 | } |
| 489 | |
| 490 | /* This can handle encryption stuffs */ |
| 491 | static int f2fs_submit_page_read(struct inode *inode, struct page *page, |
| 492 | block_t blkaddr) |
| 493 | { |
| 494 | struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1); |
| 495 | |
| 496 | if (IS_ERR(bio)) |
| 497 | return PTR_ERR(bio); |
| 498 | |
| 499 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
| 500 | bio_put(bio); |
| 501 | return -EFAULT; |
| 502 | } |
| 503 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 507 | static void __set_data_blkaddr(struct dnode_of_data *dn) |
| 508 | { |
| 509 | struct f2fs_node *rn = F2FS_NODE(dn->node_page); |
| 510 | __le32 *addr_array; |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 511 | int base = 0; |
| 512 | |
| 513 | if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) |
| 514 | base = get_extra_isize(dn->inode); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 515 | |
| 516 | /* Get physical address of data block */ |
| 517 | addr_array = blkaddr_in_node(rn); |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 518 | addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 519 | } |
| 520 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 521 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 522 | * Lock ordering for the change of data block address: |
| 523 | * ->data_page |
| 524 | * ->node_page |
| 525 | * update block addresses in the node page |
| 526 | */ |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 527 | void set_data_blkaddr(struct dnode_of_data *dn) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 528 | { |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 529 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true); |
| 530 | __set_data_blkaddr(dn); |
| 531 | if (set_page_dirty(dn->node_page)) |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 532 | dn->node_changed = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 533 | } |
| 534 | |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 535 | void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
| 536 | { |
| 537 | dn->data_blkaddr = blkaddr; |
| 538 | set_data_blkaddr(dn); |
| 539 | f2fs_update_extent_cache(dn); |
| 540 | } |
| 541 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 542 | /* dn->ofs_in_node will be returned with up-to-date last block pointer */ |
| 543 | int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 544 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 545 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 546 | int err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 547 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 548 | if (!count) |
| 549 | return 0; |
| 550 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 551 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 552 | return -EPERM; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 553 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
| 554 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 555 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 556 | trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, |
| 557 | dn->ofs_in_node, count); |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 558 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 559 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true); |
| 560 | |
| 561 | for (; count > 0; dn->ofs_in_node++) { |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 562 | block_t blkaddr = datablock_addr(dn->inode, |
| 563 | dn->node_page, dn->ofs_in_node); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 564 | if (blkaddr == NULL_ADDR) { |
| 565 | dn->data_blkaddr = NEW_ADDR; |
| 566 | __set_data_blkaddr(dn); |
| 567 | count--; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (set_page_dirty(dn->node_page)) |
| 572 | dn->node_changed = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 576 | /* Should keep dn->ofs_in_node unchanged */ |
| 577 | int reserve_new_block(struct dnode_of_data *dn) |
| 578 | { |
| 579 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 580 | int ret; |
| 581 | |
| 582 | ret = reserve_new_blocks(dn, 1); |
| 583 | dn->ofs_in_node = ofs_in_node; |
| 584 | return ret; |
| 585 | } |
| 586 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 587 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 588 | { |
| 589 | bool need_put = dn->inode_page ? false : true; |
| 590 | int err; |
| 591 | |
| 592 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 593 | if (err) |
| 594 | return err; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 595 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 596 | if (dn->data_blkaddr == NULL_ADDR) |
| 597 | err = reserve_new_block(dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 598 | if (err || need_put) |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 599 | f2fs_put_dnode(dn); |
| 600 | return err; |
| 601 | } |
| 602 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 603 | int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 604 | { |
Hou Pengyang | e15882b | 2017-02-23 09:18:05 +0000 | [diff] [blame] | 605 | struct extent_info ei = {0,0,0}; |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 606 | struct inode *inode = dn->inode; |
Chao Yu | 028a41e | 2015-03-19 19:26:02 +0800 | [diff] [blame] | 607 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 608 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 609 | dn->data_blkaddr = ei.blk + index - ei.fofs; |
| 610 | return 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 611 | } |
| 612 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 613 | return f2fs_reserve_block(dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 614 | } |
| 615 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 616 | struct page *get_read_data_page(struct inode *inode, pgoff_t index, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 617 | int op_flags, bool for_write) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 618 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 619 | struct address_space *mapping = inode->i_mapping; |
| 620 | struct dnode_of_data dn; |
| 621 | struct page *page; |
Hou Pengyang | e15882b | 2017-02-23 09:18:05 +0000 | [diff] [blame] | 622 | struct extent_info ei = {0,0,0}; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 623 | int err; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 624 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 625 | page = f2fs_grab_cache_page(mapping, index, for_write); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 626 | if (!page) |
| 627 | return ERR_PTR(-ENOMEM); |
| 628 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 629 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 630 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 631 | goto got_it; |
| 632 | } |
| 633 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 634 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 635 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 636 | if (err) |
| 637 | goto put_err; |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 638 | f2fs_put_dnode(&dn); |
| 639 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 640 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 641 | err = -ENOENT; |
| 642 | goto put_err; |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 643 | } |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 644 | got_it: |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 645 | if (PageUptodate(page)) { |
| 646 | unlock_page(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 647 | return page; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 648 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 649 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 650 | /* |
| 651 | * A new dentry page is allocated but not able to be written, since its |
| 652 | * new inode page couldn't be allocated due to -ENOSPC. |
| 653 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 654 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 655 | */ |
| 656 | if (dn.data_blkaddr == NEW_ADDR) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 657 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 658 | if (!PageUptodate(page)) |
| 659 | SetPageUptodate(page); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 660 | unlock_page(page); |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 661 | return page; |
| 662 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 663 | |
Jaegeuk Kim | 13ba41e | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 664 | err = f2fs_submit_page_read(inode, page, dn.data_blkaddr); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 665 | if (err) |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 666 | goto put_err; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 667 | return page; |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 668 | |
| 669 | put_err: |
| 670 | f2fs_put_page(page, 1); |
| 671 | return ERR_PTR(err); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 672 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 673 | |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 674 | struct page *find_data_page(struct inode *inode, pgoff_t index) |
| 675 | { |
| 676 | struct address_space *mapping = inode->i_mapping; |
| 677 | struct page *page; |
| 678 | |
| 679 | page = find_get_page(mapping, index); |
| 680 | if (page && PageUptodate(page)) |
| 681 | return page; |
| 682 | f2fs_put_page(page, 0); |
| 683 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 684 | page = get_read_data_page(inode, index, 0, false); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 685 | if (IS_ERR(page)) |
| 686 | return page; |
| 687 | |
| 688 | if (PageUptodate(page)) |
| 689 | return page; |
| 690 | |
| 691 | wait_on_page_locked(page); |
| 692 | if (unlikely(!PageUptodate(page))) { |
| 693 | f2fs_put_page(page, 0); |
| 694 | return ERR_PTR(-EIO); |
| 695 | } |
| 696 | return page; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * If it tries to access a hole, return an error. |
| 701 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 702 | * whether this page exists or not. |
| 703 | */ |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 704 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index, |
| 705 | bool for_write) |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 706 | { |
| 707 | struct address_space *mapping = inode->i_mapping; |
| 708 | struct page *page; |
| 709 | repeat: |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 710 | page = get_read_data_page(inode, index, 0, for_write); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 711 | if (IS_ERR(page)) |
| 712 | return page; |
| 713 | |
| 714 | /* wait for read completion */ |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 715 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 716 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 717 | f2fs_put_page(page, 1); |
| 718 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 719 | } |
Chao Yu | 1563ac7 | 2016-07-03 22:05:12 +0800 | [diff] [blame] | 720 | if (unlikely(!PageUptodate(page))) { |
| 721 | f2fs_put_page(page, 1); |
| 722 | return ERR_PTR(-EIO); |
| 723 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 724 | return page; |
| 725 | } |
| 726 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 727 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 728 | * Caller ensures that this data page is never allocated. |
| 729 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 730 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 731 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 732 | * f2fs_unlock_op(). |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 733 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
| 734 | * ipage should be released by this function. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 735 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 736 | struct page *get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 737 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 738 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 739 | struct address_space *mapping = inode->i_mapping; |
| 740 | struct page *page; |
| 741 | struct dnode_of_data dn; |
| 742 | int err; |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 743 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 744 | page = f2fs_grab_cache_page(mapping, index, true); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 745 | if (!page) { |
| 746 | /* |
| 747 | * before exiting, we should make sure ipage will be released |
| 748 | * if any error occur. |
| 749 | */ |
| 750 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 751 | return ERR_PTR(-ENOMEM); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 752 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 753 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 754 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 755 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 756 | if (err) { |
| 757 | f2fs_put_page(page, 1); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 758 | return ERR_PTR(err); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 759 | } |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 760 | if (!ipage) |
| 761 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 762 | |
| 763 | if (PageUptodate(page)) |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 764 | goto got_it; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 765 | |
| 766 | if (dn.data_blkaddr == NEW_ADDR) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 767 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 768 | if (!PageUptodate(page)) |
| 769 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 770 | } else { |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 771 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 772 | |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 773 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
| 774 | f2fs_bug_on(F2FS_I_SB(inode), ipage); |
| 775 | page = get_lock_data_page(inode, index, true); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 776 | if (IS_ERR(page)) |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 777 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 778 | } |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 779 | got_it: |
Chao Yu | 9edcdab | 2015-09-11 14:43:52 +0800 | [diff] [blame] | 780 | if (new_i_size && i_size_read(inode) < |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 781 | ((loff_t)(index + 1) << PAGE_SHIFT)) |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 782 | f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 783 | return page; |
| 784 | } |
| 785 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 786 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 787 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 788 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 789 | struct f2fs_summary sum; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 790 | struct node_info ni; |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 791 | pgoff_t fofs; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 792 | blkcnt_t count = 1; |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 793 | int err; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 794 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 795 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 796 | return -EPERM; |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 797 | |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 798 | dn->data_blkaddr = datablock_addr(dn->inode, |
| 799 | dn->node_page, dn->ofs_in_node); |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 800 | if (dn->data_blkaddr == NEW_ADDR) |
| 801 | goto alloc; |
| 802 | |
Chao Yu | 0abd675 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 803 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
| 804 | return err; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 805 | |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 806 | alloc: |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 807 | get_node_info(sbi, dn->nid, &ni); |
| 808 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 809 | |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 810 | allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr, |
Chao Yu | fb830fc | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 811 | &sum, CURSEG_WARM_DATA, NULL, false); |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 812 | set_data_blkaddr(dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 813 | |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 814 | /* update i_size */ |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 815 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) + |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 816 | dn->ofs_in_node; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 817 | if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT)) |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 818 | f2fs_i_size_write(dn->inode, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 819 | ((loff_t)(fofs + 1) << PAGE_SHIFT)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
Jaegeuk Kim | c040ff9 | 2016-11-11 16:46:40 -0800 | [diff] [blame] | 823 | static inline bool __force_buffered_io(struct inode *inode, int rw) |
| 824 | { |
Jaegeuk Kim | 1958593 | 2017-09-05 16:54:24 -0700 | [diff] [blame] | 825 | return (f2fs_encrypted_file(inode) || |
Jaegeuk Kim | c040ff9 | 2016-11-11 16:46:40 -0800 | [diff] [blame] | 826 | (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) || |
| 827 | F2FS_I_SB(inode)->s_ndevs); |
| 828 | } |
| 829 | |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 830 | int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 831 | { |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 832 | struct inode *inode = file_inode(iocb->ki_filp); |
Chao Yu | 5b8db7f | 2016-01-26 15:38:29 +0800 | [diff] [blame] | 833 | struct f2fs_map_blocks map; |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 834 | int err = 0; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 835 | |
Weichao Guo | 71ad682 | 2017-09-29 22:43:23 +0800 | [diff] [blame] | 836 | /* convert inline data for Direct I/O*/ |
| 837 | if (iocb->ki_flags & IOCB_DIRECT) { |
| 838 | err = f2fs_convert_inline_inode(inode); |
| 839 | if (err) |
| 840 | return err; |
| 841 | } |
| 842 | |
Jaegeuk Kim | dc91de7 | 2017-01-13 13:12:29 -0800 | [diff] [blame] | 843 | if (is_inode_flag_set(inode, FI_NO_PREALLOC)) |
| 844 | return 0; |
| 845 | |
Jaegeuk Kim | 0080c50 | 2016-05-07 08:52:57 -0700 | [diff] [blame] | 846 | map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos); |
Chao Yu | dfd02e4 | 2016-08-20 15:12:01 +0800 | [diff] [blame] | 847 | map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from)); |
| 848 | if (map.m_len > map.m_lblk) |
| 849 | map.m_len -= map.m_lblk; |
| 850 | else |
| 851 | map.m_len = 0; |
| 852 | |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 853 | map.m_next_pgofs = NULL; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 854 | |
Weichao Guo | 71ad682 | 2017-09-29 22:43:23 +0800 | [diff] [blame] | 855 | if (iocb->ki_flags & IOCB_DIRECT) |
Jaegeuk Kim | c040ff9 | 2016-11-11 16:46:40 -0800 | [diff] [blame] | 856 | return f2fs_map_blocks(inode, &map, 1, |
| 857 | __force_buffered_io(inode, WRITE) ? |
| 858 | F2FS_GET_BLOCK_PRE_AIO : |
| 859 | F2FS_GET_BLOCK_PRE_DIO); |
Chao Yu | f247037 | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 860 | if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) { |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 861 | err = f2fs_convert_inline_inode(inode); |
| 862 | if (err) |
| 863 | return err; |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 864 | } |
| 865 | if (!f2fs_has_inline_data(inode)) |
| 866 | return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO); |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 867 | return err; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 870 | static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock) |
| 871 | { |
| 872 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
| 873 | if (lock) |
| 874 | down_read(&sbi->node_change); |
| 875 | else |
| 876 | up_read(&sbi->node_change); |
| 877 | } else { |
| 878 | if (lock) |
| 879 | f2fs_lock_op(sbi); |
| 880 | else |
| 881 | f2fs_unlock_op(sbi); |
| 882 | } |
| 883 | } |
| 884 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 885 | /* |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 886 | * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with |
| 887 | * f2fs_map_blocks structure. |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 888 | * If original data blocks are allocated, then give them to blockdev. |
| 889 | * Otherwise, |
| 890 | * a. preallocate requested block addresses |
| 891 | * b. do not use extent cache for better performance |
| 892 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 893 | */ |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 894 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 895 | int create, int flag) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 896 | { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 897 | unsigned int maxblocks = map->m_len; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 898 | struct dnode_of_data dn; |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 899 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | ac6f199 | 2016-06-16 17:03:23 -0700 | [diff] [blame] | 900 | int mode = create ? ALLOC_NODE : LOOKUP_NODE; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 901 | pgoff_t pgofs, end_offset, end; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 902 | int err = 0, ofs = 1; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 903 | unsigned int ofs_in_node, last_ofs_in_node; |
| 904 | blkcnt_t prealloc; |
Hou Pengyang | e15882b | 2017-02-23 09:18:05 +0000 | [diff] [blame] | 905 | struct extent_info ei = {0,0,0}; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 906 | block_t blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 907 | |
Chao Yu | dfd02e4 | 2016-08-20 15:12:01 +0800 | [diff] [blame] | 908 | if (!maxblocks) |
| 909 | return 0; |
| 910 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 911 | map->m_len = 0; |
| 912 | map->m_flags = 0; |
| 913 | |
| 914 | /* it only supports block size == page size */ |
| 915 | pgofs = (pgoff_t)map->m_lblk; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 916 | end = pgofs + maxblocks; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 917 | |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 918 | if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 919 | map->m_pblk = ei.blk + pgofs - ei.fofs; |
| 920 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); |
| 921 | map->m_flags = F2FS_MAP_MAPPED; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 922 | goto out; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 923 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 924 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 925 | next_dnode: |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 926 | if (create) |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 927 | __do_map_lock(sbi, flag, true); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 928 | |
| 929 | /* When reading holes, we need its node page */ |
| 930 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 931 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 932 | if (err) { |
Chao Yu | 43473f9 | 2016-05-05 19:13:02 +0800 | [diff] [blame] | 933 | if (flag == F2FS_GET_BLOCK_BMAP) |
| 934 | map->m_pblk = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 935 | if (err == -ENOENT) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 936 | err = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 937 | if (map->m_next_pgofs) |
| 938 | *map->m_next_pgofs = |
| 939 | get_next_page_offset(&dn, pgofs); |
| 940 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 941 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 942 | } |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 943 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 944 | prealloc = 0; |
Arnd Bergmann | 230436b | 2016-11-02 14:52:15 +0100 | [diff] [blame] | 945 | last_ofs_in_node = ofs_in_node = dn.ofs_in_node; |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 946 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 947 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 948 | next_block: |
Chao Yu | 7a2af76 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 949 | blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node); |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 950 | |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 951 | if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) { |
| 952 | if (create) { |
| 953 | if (unlikely(f2fs_cp_error(sbi))) { |
| 954 | err = -EIO; |
| 955 | goto sync_out; |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 956 | } |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 957 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 958 | if (blkaddr == NULL_ADDR) { |
| 959 | prealloc++; |
| 960 | last_ofs_in_node = dn.ofs_in_node; |
| 961 | } |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 962 | } else { |
| 963 | err = __allocate_data_block(&dn); |
Chao Yu | 6f2d8ed | 2016-10-11 22:57:03 +0800 | [diff] [blame] | 964 | if (!err) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 965 | set_inode_flag(inode, FI_APPEND_WRITE); |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 966 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 967 | if (err) |
| 968 | goto sync_out; |
Kinglong Mee | 3f2be04 | 2017-02-23 19:55:05 +0800 | [diff] [blame] | 969 | map->m_flags |= F2FS_MAP_NEW; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 970 | blkaddr = dn.data_blkaddr; |
| 971 | } else { |
Chao Yu | 43473f9 | 2016-05-05 19:13:02 +0800 | [diff] [blame] | 972 | if (flag == F2FS_GET_BLOCK_BMAP) { |
| 973 | map->m_pblk = 0; |
| 974 | goto sync_out; |
| 975 | } |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 976 | if (flag == F2FS_GET_BLOCK_FIEMAP && |
| 977 | blkaddr == NULL_ADDR) { |
| 978 | if (map->m_next_pgofs) |
| 979 | *map->m_next_pgofs = pgofs + 1; |
| 980 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 981 | if (flag != F2FS_GET_BLOCK_FIEMAP || |
Chao Yu | 43473f9 | 2016-05-05 19:13:02 +0800 | [diff] [blame] | 982 | blkaddr != NEW_ADDR) |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 983 | goto sync_out; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 984 | } |
| 985 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 986 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 987 | if (flag == F2FS_GET_BLOCK_PRE_AIO) |
| 988 | goto skip; |
| 989 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 990 | if (map->m_len == 0) { |
| 991 | /* preallocated unwritten block should be mapped for fiemap. */ |
| 992 | if (blkaddr == NEW_ADDR) |
| 993 | map->m_flags |= F2FS_MAP_UNWRITTEN; |
| 994 | map->m_flags |= F2FS_MAP_MAPPED; |
| 995 | |
| 996 | map->m_pblk = blkaddr; |
| 997 | map->m_len = 1; |
| 998 | } else if ((map->m_pblk != NEW_ADDR && |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 999 | blkaddr == (map->m_pblk + ofs)) || |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 1000 | (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) || |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1001 | flag == F2FS_GET_BLOCK_PRE_DIO) { |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1002 | ofs++; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1003 | map->m_len++; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1004 | } else { |
| 1005 | goto sync_out; |
| 1006 | } |
| 1007 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1008 | skip: |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1009 | dn.ofs_in_node++; |
| 1010 | pgofs++; |
| 1011 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1012 | /* preallocate blocks in batch for one dnode page */ |
| 1013 | if (flag == F2FS_GET_BLOCK_PRE_AIO && |
| 1014 | (pgofs == end || dn.ofs_in_node == end_offset)) { |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1015 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1016 | dn.ofs_in_node = ofs_in_node; |
| 1017 | err = reserve_new_blocks(&dn, prealloc); |
| 1018 | if (err) |
| 1019 | goto sync_out; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1020 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1021 | map->m_len += dn.ofs_in_node - ofs_in_node; |
| 1022 | if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { |
| 1023 | err = -ENOSPC; |
| 1024 | goto sync_out; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1025 | } |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1026 | dn.ofs_in_node = end_offset; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1029 | if (pgofs >= end) |
| 1030 | goto sync_out; |
| 1031 | else if (dn.ofs_in_node < end_offset) |
| 1032 | goto next_block; |
| 1033 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1034 | f2fs_put_dnode(&dn); |
| 1035 | |
| 1036 | if (create) { |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 1037 | __do_map_lock(sbi, flag, false); |
Chao Yu | 6f2d8ed | 2016-10-11 22:57:03 +0800 | [diff] [blame] | 1038 | f2fs_balance_fs(sbi, dn.node_changed); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1039 | } |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1040 | goto next_dnode; |
| 1041 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1042 | sync_out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1043 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1044 | unlock_out: |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1045 | if (create) { |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 1046 | __do_map_lock(sbi, flag, false); |
Chao Yu | 6f2d8ed | 2016-10-11 22:57:03 +0800 | [diff] [blame] | 1047 | f2fs_balance_fs(sbi, dn.node_changed); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1048 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1049 | out: |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1050 | trace_f2fs_map_blocks(inode, map, err); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1051 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1052 | } |
| 1053 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1054 | static int __get_data_block(struct inode *inode, sector_t iblock, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1055 | struct buffer_head *bh, int create, int flag, |
| 1056 | pgoff_t *next_pgofs) |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1057 | { |
| 1058 | struct f2fs_map_blocks map; |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 1059 | int err; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1060 | |
| 1061 | map.m_lblk = iblock; |
| 1062 | map.m_len = bh->b_size >> inode->i_blkbits; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1063 | map.m_next_pgofs = next_pgofs; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1064 | |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 1065 | err = f2fs_map_blocks(inode, &map, create, flag); |
| 1066 | if (!err) { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1067 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 1068 | bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; |
Wei Fang | b86e330 | 2017-01-22 12:21:02 +0800 | [diff] [blame] | 1069 | bh->b_size = (u64)map.m_len << inode->i_blkbits; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1070 | } |
Jaegeuk Kim | a7de608 | 2016-11-11 16:31:56 -0800 | [diff] [blame] | 1071 | return err; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1074 | static int get_data_block(struct inode *inode, sector_t iblock, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1075 | struct buffer_head *bh_result, int create, int flag, |
| 1076 | pgoff_t *next_pgofs) |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1077 | { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1078 | return __get_data_block(inode, iblock, bh_result, create, |
| 1079 | flag, next_pgofs); |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1080 | } |
| 1081 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1082 | static int get_data_block_dio(struct inode *inode, sector_t iblock, |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1083 | struct buffer_head *bh_result, int create) |
| 1084 | { |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1085 | return __get_data_block(inode, iblock, bh_result, create, |
Qiuyang Sun | f2220c7 | 2017-08-09 17:27:30 +0800 | [diff] [blame] | 1086 | F2FS_GET_BLOCK_DEFAULT, NULL); |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static int get_data_block_bmap(struct inode *inode, sector_t iblock, |
| 1090 | struct buffer_head *bh_result, int create) |
| 1091 | { |
Yunlei He | 179448b | 2015-12-28 21:48:32 +0800 | [diff] [blame] | 1092 | /* Block number less than F2FS MAX BLOCKS */ |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 1093 | if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) |
Yunlei He | 179448b | 2015-12-28 21:48:32 +0800 | [diff] [blame] | 1094 | return -EFBIG; |
| 1095 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1096 | return __get_data_block(inode, iblock, bh_result, create, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1097 | F2FS_GET_BLOCK_BMAP, NULL); |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1098 | } |
| 1099 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1100 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
| 1101 | { |
| 1102 | return (offset >> inode->i_blkbits); |
| 1103 | } |
| 1104 | |
| 1105 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) |
| 1106 | { |
| 1107 | return (blk << inode->i_blkbits); |
| 1108 | } |
| 1109 | |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1110 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1111 | u64 start, u64 len) |
| 1112 | { |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1113 | struct buffer_head map_bh; |
| 1114 | sector_t start_blk, last_blk; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1115 | pgoff_t next_pgofs; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1116 | u64 logical = 0, phys = 0, size = 0; |
| 1117 | u32 flags = 0; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1118 | int ret = 0; |
| 1119 | |
| 1120 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
| 1121 | if (ret) |
| 1122 | return ret; |
| 1123 | |
Jaegeuk Kim | 67f8cf3 | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 1124 | if (f2fs_has_inline_data(inode)) { |
| 1125 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
| 1126 | if (ret != -EAGAIN) |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1130 | inode_lock(inode); |
Fan Li | de1475c | 2016-01-04 15:56:50 +0800 | [diff] [blame] | 1131 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1132 | if (logical_to_blk(inode, len) == 0) |
| 1133 | len = blk_to_logical(inode, 1); |
| 1134 | |
| 1135 | start_blk = logical_to_blk(inode, start); |
| 1136 | last_blk = logical_to_blk(inode, start + len - 1); |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1137 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1138 | next: |
| 1139 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
| 1140 | map_bh.b_size = len; |
| 1141 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1142 | ret = get_data_block(inode, start_blk, &map_bh, 0, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1143 | F2FS_GET_BLOCK_FIEMAP, &next_pgofs); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1144 | if (ret) |
| 1145 | goto out; |
| 1146 | |
| 1147 | /* HOLE */ |
| 1148 | if (!buffer_mapped(&map_bh)) { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1149 | start_blk = next_pgofs; |
Chao Yu | 58736fa | 2016-10-11 22:57:04 +0800 | [diff] [blame] | 1150 | |
| 1151 | if (blk_to_logical(inode, start_blk) < blk_to_logical(inode, |
| 1152 | F2FS_I_SB(inode)->max_file_blocks)) |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1153 | goto prep_next; |
Chao Yu | 58736fa | 2016-10-11 22:57:04 +0800 | [diff] [blame] | 1154 | |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1155 | flags |= FIEMAP_EXTENT_LAST; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1156 | } |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1157 | |
Chao Yu | da5af12 | 2016-01-08 20:19:27 +0800 | [diff] [blame] | 1158 | if (size) { |
| 1159 | if (f2fs_encrypted_inode(inode)) |
| 1160 | flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; |
| 1161 | |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1162 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 1163 | phys, size, flags); |
Chao Yu | da5af12 | 2016-01-08 20:19:27 +0800 | [diff] [blame] | 1164 | } |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1165 | |
| 1166 | if (start_blk > last_blk || ret) |
| 1167 | goto out; |
| 1168 | |
| 1169 | logical = blk_to_logical(inode, start_blk); |
| 1170 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
| 1171 | size = map_bh.b_size; |
| 1172 | flags = 0; |
| 1173 | if (buffer_unwritten(&map_bh)) |
| 1174 | flags = FIEMAP_EXTENT_UNWRITTEN; |
| 1175 | |
| 1176 | start_blk += logical_to_blk(inode, size); |
| 1177 | |
| 1178 | prep_next: |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1179 | cond_resched(); |
| 1180 | if (fatal_signal_pending(current)) |
| 1181 | ret = -EINTR; |
| 1182 | else |
| 1183 | goto next; |
| 1184 | out: |
| 1185 | if (ret == 1) |
| 1186 | ret = 0; |
| 1187 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1188 | inode_unlock(inode); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1189 | return ret; |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1190 | } |
| 1191 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1192 | /* |
| 1193 | * This function was originally taken from fs/mpage.c, and customized for f2fs. |
| 1194 | * Major change was from block_size == page_size in f2fs by default. |
| 1195 | */ |
| 1196 | static int f2fs_mpage_readpages(struct address_space *mapping, |
| 1197 | struct list_head *pages, struct page *page, |
| 1198 | unsigned nr_pages) |
| 1199 | { |
| 1200 | struct bio *bio = NULL; |
| 1201 | unsigned page_idx; |
| 1202 | sector_t last_block_in_bio = 0; |
| 1203 | struct inode *inode = mapping->host; |
| 1204 | const unsigned blkbits = inode->i_blkbits; |
| 1205 | const unsigned blocksize = 1 << blkbits; |
| 1206 | sector_t block_in_file; |
| 1207 | sector_t last_block; |
| 1208 | sector_t last_block_in_file; |
| 1209 | sector_t block_nr; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1210 | struct f2fs_map_blocks map; |
| 1211 | |
| 1212 | map.m_pblk = 0; |
| 1213 | map.m_lblk = 0; |
| 1214 | map.m_len = 0; |
| 1215 | map.m_flags = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1216 | map.m_next_pgofs = NULL; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1217 | |
| 1218 | for (page_idx = 0; nr_pages; page_idx++, nr_pages--) { |
| 1219 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1220 | if (pages) { |
Chao Yu | 939afa9 | 2017-01-07 18:49:42 +0800 | [diff] [blame] | 1221 | page = list_last_entry(pages, struct page, lru); |
Kinglong Mee | a83d50b | 2017-03-13 16:35:13 +0800 | [diff] [blame] | 1222 | |
| 1223 | prefetchw(&page->flags); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1224 | list_del(&page->lru); |
| 1225 | if (add_to_page_cache_lru(page, mapping, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 1226 | page->index, |
| 1227 | readahead_gfp_mask(mapping))) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1228 | goto next_page; |
| 1229 | } |
| 1230 | |
| 1231 | block_in_file = (sector_t)page->index; |
| 1232 | last_block = block_in_file + nr_pages; |
| 1233 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> |
| 1234 | blkbits; |
| 1235 | if (last_block > last_block_in_file) |
| 1236 | last_block = last_block_in_file; |
| 1237 | |
| 1238 | /* |
| 1239 | * Map blocks using the previous result first. |
| 1240 | */ |
| 1241 | if ((map.m_flags & F2FS_MAP_MAPPED) && |
| 1242 | block_in_file > map.m_lblk && |
| 1243 | block_in_file < (map.m_lblk + map.m_len)) |
| 1244 | goto got_it; |
| 1245 | |
| 1246 | /* |
| 1247 | * Then do more f2fs_map_blocks() calls until we are |
| 1248 | * done with this page. |
| 1249 | */ |
| 1250 | map.m_flags = 0; |
| 1251 | |
| 1252 | if (block_in_file < last_block) { |
| 1253 | map.m_lblk = block_in_file; |
| 1254 | map.m_len = last_block - block_in_file; |
| 1255 | |
Chao Yu | 46c9e14 | 2015-09-18 16:54:16 +0800 | [diff] [blame] | 1256 | if (f2fs_map_blocks(inode, &map, 0, |
Qiuyang Sun | f2220c7 | 2017-08-09 17:27:30 +0800 | [diff] [blame] | 1257 | F2FS_GET_BLOCK_DEFAULT)) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1258 | goto set_error_page; |
| 1259 | } |
| 1260 | got_it: |
| 1261 | if ((map.m_flags & F2FS_MAP_MAPPED)) { |
| 1262 | block_nr = map.m_pblk + block_in_file - map.m_lblk; |
| 1263 | SetPageMappedToDisk(page); |
| 1264 | |
| 1265 | if (!PageUptodate(page) && !cleancache_get_page(page)) { |
| 1266 | SetPageUptodate(page); |
| 1267 | goto confused; |
| 1268 | } |
| 1269 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1270 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 1271 | if (!PageUptodate(page)) |
| 1272 | SetPageUptodate(page); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1273 | unlock_page(page); |
| 1274 | goto next_page; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * This page will go to BIO. Do we need to send this |
| 1279 | * BIO off first? |
| 1280 | */ |
Jaegeuk Kim | 3c62be1 | 2016-10-06 19:02:05 -0700 | [diff] [blame] | 1281 | if (bio && (last_block_in_bio != block_nr - 1 || |
| 1282 | !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) { |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1283 | submit_and_realloc: |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 1284 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1285 | bio = NULL; |
| 1286 | } |
| 1287 | if (bio == NULL) { |
Jaegeuk Kim | 13ba41e | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 1288 | bio = f2fs_grab_read_bio(inode, block_nr, nr_pages); |
Jaegeuk Kim | 1d353eb | 2016-07-12 09:38:48 -0700 | [diff] [blame] | 1289 | if (IS_ERR(bio)) { |
| 1290 | bio = NULL; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1291 | goto set_error_page; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1292 | } |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) |
| 1296 | goto submit_and_realloc; |
| 1297 | |
| 1298 | last_block_in_bio = block_nr; |
| 1299 | goto next_page; |
| 1300 | set_error_page: |
| 1301 | SetPageError(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1302 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1303 | unlock_page(page); |
| 1304 | goto next_page; |
| 1305 | confused: |
| 1306 | if (bio) { |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 1307 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1308 | bio = NULL; |
| 1309 | } |
| 1310 | unlock_page(page); |
| 1311 | next_page: |
| 1312 | if (pages) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1313 | put_page(page); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1314 | } |
| 1315 | BUG_ON(pages && !list_empty(pages)); |
| 1316 | if (bio) |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 1317 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1318 | return 0; |
| 1319 | } |
| 1320 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1321 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 1322 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1323 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1324 | int ret = -EAGAIN; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1325 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 1326 | trace_f2fs_readpage(page, DATA); |
| 1327 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1328 | /* If the file has inline data, try to read it directly */ |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1329 | if (f2fs_has_inline_data(inode)) |
| 1330 | ret = f2fs_read_inline_data(inode, page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1331 | if (ret == -EAGAIN) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1332 | ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1333 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static int f2fs_read_data_pages(struct file *file, |
| 1337 | struct address_space *mapping, |
| 1338 | struct list_head *pages, unsigned nr_pages) |
| 1339 | { |
Hsiang Kao | 71cb4af | 2017-09-24 02:45:42 +0800 | [diff] [blame] | 1340 | struct inode *inode = mapping->host; |
Chao Yu | 939afa9 | 2017-01-07 18:49:42 +0800 | [diff] [blame] | 1341 | struct page *page = list_last_entry(pages, struct page, lru); |
Chao Yu | b8c2940 | 2015-10-12 17:02:26 +0800 | [diff] [blame] | 1342 | |
| 1343 | trace_f2fs_readpages(inode, page, nr_pages); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1344 | |
| 1345 | /* If the file has inline data, skip readpages */ |
| 1346 | if (f2fs_has_inline_data(inode)) |
| 1347 | return 0; |
| 1348 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1349 | return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1350 | } |
| 1351 | |
Hou Pengyang | 7eab0c0 | 2017-04-25 12:45:12 +0000 | [diff] [blame] | 1352 | static int encrypt_one_page(struct f2fs_io_info *fio) |
| 1353 | { |
| 1354 | struct inode *inode = fio->page->mapping->host; |
| 1355 | gfp_t gfp_flags = GFP_NOFS; |
| 1356 | |
Jaegeuk Kim | 1958593 | 2017-09-05 16:54:24 -0700 | [diff] [blame] | 1357 | if (!f2fs_encrypted_file(inode)) |
Hou Pengyang | 7eab0c0 | 2017-04-25 12:45:12 +0000 | [diff] [blame] | 1358 | return 0; |
| 1359 | |
| 1360 | /* wait for GCed encrypted page writeback */ |
Jaegeuk Kim | d4c759e | 2017-09-05 17:04:35 -0700 | [diff] [blame] | 1361 | f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr); |
Hou Pengyang | 7eab0c0 | 2017-04-25 12:45:12 +0000 | [diff] [blame] | 1362 | |
| 1363 | retry_encrypt: |
| 1364 | fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, |
| 1365 | PAGE_SIZE, 0, fio->page->index, gfp_flags); |
| 1366 | if (!IS_ERR(fio->encrypted_page)) |
| 1367 | return 0; |
| 1368 | |
| 1369 | /* flush pending IOs and wait for a while in the ENOMEM case */ |
| 1370 | if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 1371 | f2fs_flush_merged_writes(fio->sbi); |
Hou Pengyang | 7eab0c0 | 2017-04-25 12:45:12 +0000 | [diff] [blame] | 1372 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 1373 | gfp_flags |= __GFP_NOFAIL; |
| 1374 | goto retry_encrypt; |
| 1375 | } |
| 1376 | return PTR_ERR(fio->encrypted_page); |
| 1377 | } |
| 1378 | |
| 1379 | static inline bool need_inplace_update(struct f2fs_io_info *fio) |
| 1380 | { |
| 1381 | struct inode *inode = fio->page->mapping->host; |
| 1382 | |
Hou Pengyang | 7eab0c0 | 2017-04-25 12:45:12 +0000 | [diff] [blame] | 1383 | if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode)) |
| 1384 | return false; |
| 1385 | if (is_cold_data(fio->page)) |
| 1386 | return false; |
| 1387 | if (IS_ATOMIC_WRITTEN_PAGE(fio->page)) |
| 1388 | return false; |
| 1389 | |
| 1390 | return need_inplace_update_policy(inode, fio); |
| 1391 | } |
| 1392 | |
Jaegeuk Kim | a817737 | 2017-04-24 15:20:16 -0700 | [diff] [blame] | 1393 | static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio) |
| 1394 | { |
| 1395 | if (fio->old_blkaddr == NEW_ADDR) |
| 1396 | return false; |
| 1397 | if (fio->old_blkaddr == NULL_ADDR) |
| 1398 | return false; |
| 1399 | return true; |
| 1400 | } |
| 1401 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1402 | int do_write_data_page(struct f2fs_io_info *fio) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1403 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1404 | struct page *page = fio->page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1405 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1406 | struct dnode_of_data dn; |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 1407 | struct extent_info ei = {0,0,0}; |
| 1408 | bool ipu_force = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1409 | int err = 0; |
| 1410 | |
| 1411 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 1412 | if (need_inplace_update(fio) && |
| 1413 | f2fs_lookup_extent_cache(inode, page->index, &ei)) { |
| 1414 | fio->old_blkaddr = ei.blk + page->index - ei.fofs; |
Jaegeuk Kim | a817737 | 2017-04-24 15:20:16 -0700 | [diff] [blame] | 1415 | |
| 1416 | if (valid_ipu_blkaddr(fio)) { |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 1417 | ipu_force = true; |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1418 | fio->need_lock = LOCK_DONE; |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 1419 | goto got_it; |
| 1420 | } |
| 1421 | } |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1422 | |
Jaegeuk Kim | d29460e | 2017-06-21 17:52:39 -0700 | [diff] [blame] | 1423 | /* Deadlock due to between page->lock and f2fs_lock_op */ |
| 1424 | if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi)) |
| 1425 | return -EAGAIN; |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1426 | |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 1427 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1428 | if (err) |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1429 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1430 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 1431 | fio->old_blkaddr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1432 | |
| 1433 | /* This page is already truncated */ |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 1434 | if (fio->old_blkaddr == NULL_ADDR) { |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1435 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1436 | goto out_writepage; |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1437 | } |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 1438 | got_it: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1439 | /* |
| 1440 | * If current allocation needs SSR, |
| 1441 | * it had better in-place writes for updated data. |
| 1442 | */ |
Jaegeuk Kim | a817737 | 2017-04-24 15:20:16 -0700 | [diff] [blame] | 1443 | if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) { |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1444 | err = encrypt_one_page(fio); |
| 1445 | if (err) |
| 1446 | goto out_writepage; |
| 1447 | |
| 1448 | set_page_writeback(page); |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1449 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1450 | if (fio->need_lock == LOCK_REQ) |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1451 | f2fs_unlock_op(fio->sbi); |
Jaegeuk Kim | d1b3e72 | 2017-03-30 21:02:46 -0700 | [diff] [blame] | 1452 | err = rewrite_data_page(fio); |
Hou Pengyang | 7eab0c0 | 2017-04-25 12:45:12 +0000 | [diff] [blame] | 1453 | trace_f2fs_do_write_data_page(fio->page, IPU); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 1454 | set_inode_flag(inode, FI_UPDATE_WRITE); |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1455 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1456 | } |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1457 | |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1458 | if (fio->need_lock == LOCK_RETRY) { |
| 1459 | if (!f2fs_trylock_op(fio->sbi)) { |
| 1460 | err = -EAGAIN; |
| 1461 | goto out_writepage; |
| 1462 | } |
| 1463 | fio->need_lock = LOCK_REQ; |
| 1464 | } |
| 1465 | |
| 1466 | err = encrypt_one_page(fio); |
| 1467 | if (err) |
| 1468 | goto out_writepage; |
| 1469 | |
| 1470 | set_page_writeback(page); |
| 1471 | |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1472 | /* LFS mode write path */ |
| 1473 | write_data_page(&dn, fio); |
| 1474 | trace_f2fs_do_write_data_page(page, OPU); |
| 1475 | set_inode_flag(inode, FI_APPEND_WRITE); |
| 1476 | if (page->index == 0) |
| 1477 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1478 | out_writepage: |
| 1479 | f2fs_put_dnode(&dn); |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1480 | out: |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1481 | if (fio->need_lock == LOCK_REQ) |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1482 | f2fs_unlock_op(fio->sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1483 | return err; |
| 1484 | } |
| 1485 | |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1486 | static int __write_data_page(struct page *page, bool *submitted, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1487 | struct writeback_control *wbc, |
| 1488 | enum iostat_type io_type) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1489 | { |
| 1490 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1491 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1492 | loff_t i_size = i_size_read(inode); |
| 1493 | const pgoff_t end_index = ((unsigned long long) i_size) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1494 | >> PAGE_SHIFT; |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 1495 | loff_t psize = (page->index + 1) << PAGE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1496 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1497 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1498 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1499 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1500 | .sbi = sbi, |
Chao Yu | 39d787b | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 1501 | .ino = inode->i_ino, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1502 | .type = DATA, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 1503 | .op = REQ_OP_WRITE, |
Jens Axboe | 7637241 | 2016-11-01 10:00:38 -0600 | [diff] [blame] | 1504 | .op_flags = wbc_to_write_flags(wbc), |
Hou Pengyang | e959c8f | 2017-04-25 12:45:13 +0000 | [diff] [blame] | 1505 | .old_blkaddr = NULL_ADDR, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1506 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1507 | .encrypted_page = NULL, |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1508 | .submitted = false, |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1509 | .need_lock = LOCK_RETRY, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1510 | .io_type = io_type, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1511 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1512 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 1513 | trace_f2fs_writepage(page, DATA); |
| 1514 | |
Chao Yu | 0771fcc | 2017-06-29 23:20:45 +0800 | [diff] [blame] | 1515 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1516 | goto redirty_out; |
| 1517 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1518 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1519 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1520 | |
| 1521 | /* |
| 1522 | * If the offset is out-of-range of file size, |
| 1523 | * this page does not have to be written to disk. |
| 1524 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1525 | offset = i_size & (PAGE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1526 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1527 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1528 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1529 | zero_user_segment(page, offset, PAGE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1530 | write: |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1531 | if (f2fs_is_drop_cache(inode)) |
| 1532 | goto out; |
Jaegeuk Kim | e6e5f56 | 2016-04-14 16:48:52 -0700 | [diff] [blame] | 1533 | /* we should not write 0'th page having journal header */ |
| 1534 | if (f2fs_is_volatile_file(inode) && (!page->index || |
| 1535 | (!wbc->for_reclaim && |
| 1536 | available_free_memory(sbi, BASE_CHECK)))) |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1537 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1538 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1539 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 1540 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | 7f31997 | 2016-06-03 12:28:26 -0700 | [diff] [blame] | 1541 | mapping_set_error(page->mapping, -EIO); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1542 | goto out; |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 1545 | /* Dentry blocks are controlled by checkpoint */ |
| 1546 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1547 | fio.need_lock = LOCK_DONE; |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 1548 | err = do_write_data_page(&fio); |
| 1549 | goto done; |
| 1550 | } |
| 1551 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1552 | if (!wbc->for_reclaim) |
| 1553 | need_balance_fs = true; |
Jaegeuk Kim | 7f3037a | 2016-09-01 12:02:51 -0700 | [diff] [blame] | 1554 | else if (has_not_enough_free_secs(sbi, 0, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1555 | goto redirty_out; |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 1556 | else |
| 1557 | set_inode_flag(inode, FI_HOT_DATA); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1558 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1559 | err = -EAGAIN; |
Yunlei He | dd7b233 | 2017-02-23 20:31:20 +0800 | [diff] [blame] | 1560 | if (f2fs_has_inline_data(inode)) { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1561 | err = f2fs_write_inline_data(inode, page); |
Yunlei He | dd7b233 | 2017-02-23 20:31:20 +0800 | [diff] [blame] | 1562 | if (!err) |
| 1563 | goto out; |
| 1564 | } |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1565 | |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1566 | if (err == -EAGAIN) { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1567 | err = do_write_data_page(&fio); |
Jaegeuk Kim | cc15620 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1568 | if (err == -EAGAIN) { |
| 1569 | fio.need_lock = LOCK_REQ; |
| 1570 | err = do_write_data_page(&fio); |
| 1571 | } |
| 1572 | } |
Chao Yu | a0d00fa | 2017-10-09 17:55:19 +0800 | [diff] [blame] | 1573 | |
| 1574 | down_write(&F2FS_I(inode)->i_sem); |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 1575 | if (F2FS_I(inode)->last_disk_size < psize) |
| 1576 | F2FS_I(inode)->last_disk_size = psize; |
Chao Yu | a0d00fa | 2017-10-09 17:55:19 +0800 | [diff] [blame] | 1577 | up_write(&F2FS_I(inode)->i_sem); |
Hou Pengyang | 279d6df | 2017-04-27 00:17:21 +0800 | [diff] [blame] | 1578 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1579 | done: |
| 1580 | if (err && err != -ENOENT) |
| 1581 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1582 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1583 | out: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1584 | inode_dec_dirty_pages(inode); |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1585 | if (err) |
| 1586 | ClearPageUptodate(page); |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 1587 | |
| 1588 | if (wbc->for_reclaim) { |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 1589 | f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA); |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 1590 | clear_inode_flag(inode, FI_HOT_DATA); |
Chao Yu | c227f91 | 2015-12-16 13:09:20 +0800 | [diff] [blame] | 1591 | remove_dirty_inode(inode); |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1592 | submitted = NULL; |
Chao Yu | eb7e813 | 2015-11-10 18:45:07 +0800 | [diff] [blame] | 1593 | } |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 1594 | |
| 1595 | unlock_page(page); |
Jaegeuk Kim | a788189 | 2017-04-20 13:51:57 -0700 | [diff] [blame] | 1596 | if (!S_ISDIR(inode->i_mode)) |
| 1597 | f2fs_balance_fs(sbi, need_balance_fs); |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 1598 | |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1599 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 1600 | f2fs_submit_merged_write(sbi, DATA); |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1601 | submitted = NULL; |
| 1602 | } |
| 1603 | |
| 1604 | if (submitted) |
| 1605 | *submitted = fio.submitted; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 1606 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1607 | return 0; |
| 1608 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1609 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1610 | redirty_page_for_writepage(wbc, page); |
Chao Yu | 0002b61 | 2016-11-28 19:13:43 -0800 | [diff] [blame] | 1611 | if (!err) |
| 1612 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 1613 | unlock_page(page); |
| 1614 | return err; |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 1615 | } |
| 1616 | |
Jaegeuk Kim | f566bae | 2017-02-03 17:18:00 -0800 | [diff] [blame] | 1617 | static int f2fs_write_data_page(struct page *page, |
| 1618 | struct writeback_control *wbc) |
| 1619 | { |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1620 | return __write_data_page(page, NULL, wbc, FS_DATA_IO); |
Jaegeuk Kim | f566bae | 2017-02-03 17:18:00 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1623 | /* |
| 1624 | * This function was copied from write_cche_pages from mm/page-writeback.c. |
| 1625 | * The major change is making write step of cold data page separately from |
| 1626 | * warm/hot data page. |
| 1627 | */ |
| 1628 | static int f2fs_write_cache_pages(struct address_space *mapping, |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1629 | struct writeback_control *wbc, |
| 1630 | enum iostat_type io_type) |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1631 | { |
| 1632 | int ret = 0; |
| 1633 | int done = 0; |
| 1634 | struct pagevec pvec; |
| 1635 | int nr_pages; |
| 1636 | pgoff_t uninitialized_var(writeback_index); |
| 1637 | pgoff_t index; |
| 1638 | pgoff_t end; /* Inclusive */ |
| 1639 | pgoff_t done_index; |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 1640 | pgoff_t last_idx = ULONG_MAX; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1641 | int cycled; |
| 1642 | int range_whole = 0; |
| 1643 | int tag; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1644 | |
| 1645 | pagevec_init(&pvec, 0); |
Jaegeuk Kim | 46ae957 | 2016-05-25 20:57:16 -0700 | [diff] [blame] | 1646 | |
Jaegeuk Kim | ef095d1 | 2017-03-24 20:05:13 -0400 | [diff] [blame] | 1647 | if (get_dirty_pages(mapping->host) <= |
| 1648 | SM_I(F2FS_M_SB(mapping))->min_hot_blocks) |
| 1649 | set_inode_flag(mapping->host, FI_HOT_DATA); |
| 1650 | else |
| 1651 | clear_inode_flag(mapping->host, FI_HOT_DATA); |
| 1652 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1653 | if (wbc->range_cyclic) { |
| 1654 | writeback_index = mapping->writeback_index; /* prev offset */ |
| 1655 | index = writeback_index; |
| 1656 | if (index == 0) |
| 1657 | cycled = 1; |
| 1658 | else |
| 1659 | cycled = 0; |
| 1660 | end = -1; |
| 1661 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1662 | index = wbc->range_start >> PAGE_SHIFT; |
| 1663 | end = wbc->range_end >> PAGE_SHIFT; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1664 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 1665 | range_whole = 1; |
| 1666 | cycled = 1; /* ignore range_cyclic tests */ |
| 1667 | } |
| 1668 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 1669 | tag = PAGECACHE_TAG_TOWRITE; |
| 1670 | else |
| 1671 | tag = PAGECACHE_TAG_DIRTY; |
| 1672 | retry: |
| 1673 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 1674 | tag_pages_for_writeback(mapping, index, end); |
| 1675 | done_index = index; |
| 1676 | while (!done && (index <= end)) { |
| 1677 | int i; |
| 1678 | |
| 1679 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 1680 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1); |
| 1681 | if (nr_pages == 0) |
| 1682 | break; |
| 1683 | |
| 1684 | for (i = 0; i < nr_pages; i++) { |
| 1685 | struct page *page = pvec.pages[i]; |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1686 | bool submitted = false; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1687 | |
| 1688 | if (page->index > end) { |
| 1689 | done = 1; |
| 1690 | break; |
| 1691 | } |
| 1692 | |
| 1693 | done_index = page->index; |
Jaegeuk Kim | d29460e | 2017-06-21 17:52:39 -0700 | [diff] [blame] | 1694 | retry_write: |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1695 | lock_page(page); |
| 1696 | |
| 1697 | if (unlikely(page->mapping != mapping)) { |
| 1698 | continue_unlock: |
| 1699 | unlock_page(page); |
| 1700 | continue; |
| 1701 | } |
| 1702 | |
| 1703 | if (!PageDirty(page)) { |
| 1704 | /* someone wrote it for us */ |
| 1705 | goto continue_unlock; |
| 1706 | } |
| 1707 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1708 | if (PageWriteback(page)) { |
| 1709 | if (wbc->sync_mode != WB_SYNC_NONE) |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 1710 | f2fs_wait_on_page_writeback(page, |
| 1711 | DATA, true); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1712 | else |
| 1713 | goto continue_unlock; |
| 1714 | } |
| 1715 | |
| 1716 | BUG_ON(PageWriteback(page)); |
| 1717 | if (!clear_page_dirty_for_io(page)) |
| 1718 | goto continue_unlock; |
| 1719 | |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1720 | ret = __write_data_page(page, &submitted, wbc, io_type); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1721 | if (unlikely(ret)) { |
Chao Yu | 0002b61 | 2016-11-28 19:13:43 -0800 | [diff] [blame] | 1722 | /* |
| 1723 | * keep nr_to_write, since vfs uses this to |
| 1724 | * get # of written pages. |
| 1725 | */ |
| 1726 | if (ret == AOP_WRITEPAGE_ACTIVATE) { |
| 1727 | unlock_page(page); |
| 1728 | ret = 0; |
| 1729 | continue; |
Jaegeuk Kim | d29460e | 2017-06-21 17:52:39 -0700 | [diff] [blame] | 1730 | } else if (ret == -EAGAIN) { |
| 1731 | ret = 0; |
| 1732 | if (wbc->sync_mode == WB_SYNC_ALL) { |
| 1733 | cond_resched(); |
| 1734 | congestion_wait(BLK_RW_ASYNC, |
| 1735 | HZ/50); |
| 1736 | goto retry_write; |
| 1737 | } |
| 1738 | continue; |
Chao Yu | 0002b61 | 2016-11-28 19:13:43 -0800 | [diff] [blame] | 1739 | } |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 1740 | done_index = page->index + 1; |
| 1741 | done = 1; |
| 1742 | break; |
Jaegeuk Kim | d68f735 | 2017-02-03 17:44:04 -0800 | [diff] [blame] | 1743 | } else if (submitted) { |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 1744 | last_idx = page->index; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1745 | } |
| 1746 | |
Jaegeuk Kim | 687de7f | 2017-03-28 18:07:38 -0700 | [diff] [blame] | 1747 | /* give a priority to WB_SYNC threads */ |
| 1748 | if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) || |
| 1749 | --wbc->nr_to_write <= 0) && |
| 1750 | wbc->sync_mode == WB_SYNC_NONE) { |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1751 | done = 1; |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | pagevec_release(&pvec); |
| 1756 | cond_resched(); |
| 1757 | } |
| 1758 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1759 | if (!cycled && !done) { |
| 1760 | cycled = 1; |
| 1761 | index = 0; |
| 1762 | end = writeback_index - 1; |
| 1763 | goto retry; |
| 1764 | } |
| 1765 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 1766 | mapping->writeback_index = done_index; |
| 1767 | |
Jaegeuk Kim | 942fd31 | 2017-02-01 16:51:22 -0800 | [diff] [blame] | 1768 | if (last_idx != ULONG_MAX) |
Jaegeuk Kim | b9109b0 | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 1769 | f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, |
| 1770 | 0, last_idx, DATA); |
Chao Yu | 6ca56ca | 2016-09-29 18:50:11 +0800 | [diff] [blame] | 1771 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1772 | return ret; |
| 1773 | } |
| 1774 | |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1775 | int __f2fs_write_data_pages(struct address_space *mapping, |
| 1776 | struct writeback_control *wbc, |
| 1777 | enum iostat_type io_type) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1778 | { |
| 1779 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1780 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 1781 | struct blk_plug plug; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1782 | int ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1783 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 1784 | /* deal with chardevs and other special file */ |
| 1785 | if (!mapping->a_ops->writepage) |
| 1786 | return 0; |
| 1787 | |
Chao Yu | 6a29054 | 2015-07-17 18:02:39 +0800 | [diff] [blame] | 1788 | /* skip writing if there is no dirty page in this inode */ |
| 1789 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) |
| 1790 | return 0; |
| 1791 | |
Chao Yu | 0771fcc | 2017-06-29 23:20:45 +0800 | [diff] [blame] | 1792 | /* during POR, we don't need to trigger writepage at all. */ |
| 1793 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1794 | goto skip_write; |
| 1795 | |
Jaegeuk Kim | a125702 | 2015-10-08 10:40:07 -0700 | [diff] [blame] | 1796 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
| 1797 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
| 1798 | available_free_memory(sbi, DIRTY_DENTS)) |
| 1799 | goto skip_write; |
| 1800 | |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 1801 | /* skip writing during file defragment */ |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 1802 | if (is_inode_flag_set(inode, FI_DO_DEFRAG)) |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 1803 | goto skip_write; |
| 1804 | |
Yunlei He | d31c7c3 | 2016-02-04 16:14:00 +0800 | [diff] [blame] | 1805 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 1806 | |
Jaegeuk Kim | 687de7f | 2017-03-28 18:07:38 -0700 | [diff] [blame] | 1807 | /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ |
| 1808 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 1809 | atomic_inc(&sbi->wb_sync_req); |
| 1810 | else if (atomic_read(&sbi->wb_sync_req)) |
| 1811 | goto skip_write; |
| 1812 | |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 1813 | blk_start_plug(&plug); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1814 | ret = f2fs_write_cache_pages(mapping, wbc, io_type); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 1815 | blk_finish_plug(&plug); |
Jaegeuk Kim | 687de7f | 2017-03-28 18:07:38 -0700 | [diff] [blame] | 1816 | |
| 1817 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 1818 | atomic_dec(&sbi->wb_sync_req); |
Jaegeuk Kim | 28ea616 | 2016-05-25 17:17:56 -0700 | [diff] [blame] | 1819 | /* |
| 1820 | * if some pages were truncated, we cannot guarantee its mapping->host |
| 1821 | * to detect pending bios. |
| 1822 | */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1823 | |
Chao Yu | c227f91 | 2015-12-16 13:09:20 +0800 | [diff] [blame] | 1824 | remove_dirty_inode(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1825 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1826 | |
| 1827 | skip_write: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1828 | wbc->pages_skipped += get_dirty_pages(inode); |
Yunlei He | d31c7c3 | 2016-02-04 16:14:00 +0800 | [diff] [blame] | 1829 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1830 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1831 | } |
| 1832 | |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1833 | static int f2fs_write_data_pages(struct address_space *mapping, |
| 1834 | struct writeback_control *wbc) |
| 1835 | { |
| 1836 | struct inode *inode = mapping->host; |
| 1837 | |
| 1838 | return __f2fs_write_data_pages(mapping, wbc, |
| 1839 | F2FS_I(inode)->cp_task == current ? |
| 1840 | FS_CP_DATA_IO : FS_DATA_IO); |
| 1841 | } |
| 1842 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1843 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 1844 | { |
| 1845 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 1846 | loff_t i_size = i_size_read(inode); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1847 | |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 1848 | if (to > i_size) { |
Qiuyang Sun | 5a3a2d8 | 2017-05-18 11:06:45 +0800 | [diff] [blame] | 1849 | down_write(&F2FS_I(inode)->i_mmap_sem); |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 1850 | truncate_pagecache(inode, i_size); |
| 1851 | truncate_blocks(inode, i_size, true); |
Qiuyang Sun | 5a3a2d8 | 2017-05-18 11:06:45 +0800 | [diff] [blame] | 1852 | up_write(&F2FS_I(inode)->i_mmap_sem); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1856 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
| 1857 | struct page *page, loff_t pos, unsigned len, |
| 1858 | block_t *blk_addr, bool *node_changed) |
| 1859 | { |
| 1860 | struct inode *inode = page->mapping->host; |
| 1861 | pgoff_t index = page->index; |
| 1862 | struct dnode_of_data dn; |
| 1863 | struct page *ipage; |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1864 | bool locked = false; |
Hou Pengyang | e15882b | 2017-02-23 09:18:05 +0000 | [diff] [blame] | 1865 | struct extent_info ei = {0,0,0}; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1866 | int err = 0; |
| 1867 | |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 1868 | /* |
| 1869 | * we already allocated all the blocks, so we don't need to get |
| 1870 | * the block addresses when there is no need to fill the page. |
| 1871 | */ |
Jaegeuk Kim | dc91de7 | 2017-01-13 13:12:29 -0800 | [diff] [blame] | 1872 | if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE && |
| 1873 | !is_inode_flag_set(inode, FI_NO_PREALLOC)) |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 1874 | return 0; |
| 1875 | |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1876 | if (f2fs_has_inline_data(inode) || |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1877 | (pos & PAGE_MASK) >= i_size_read(inode)) { |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 1878 | __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1879 | locked = true; |
| 1880 | } |
| 1881 | restart: |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1882 | /* check inline_data */ |
| 1883 | ipage = get_node_page(sbi, inode->i_ino); |
| 1884 | if (IS_ERR(ipage)) { |
| 1885 | err = PTR_ERR(ipage); |
| 1886 | goto unlock_out; |
| 1887 | } |
| 1888 | |
| 1889 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 1890 | |
| 1891 | if (f2fs_has_inline_data(inode)) { |
Chao Yu | f247037 | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 1892 | if (pos + len <= MAX_INLINE_DATA(inode)) { |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1893 | read_inline_data(page, ipage); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 1894 | set_inode_flag(inode, FI_DATA_EXIST); |
Chao Yu | ab47036 | 2016-05-11 19:48:44 +0800 | [diff] [blame] | 1895 | if (inode->i_nlink) |
| 1896 | set_inline_node(ipage); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1897 | } else { |
| 1898 | err = f2fs_convert_inline_page(&dn, page); |
| 1899 | if (err) |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1900 | goto out; |
| 1901 | if (dn.data_blkaddr == NULL_ADDR) |
| 1902 | err = f2fs_get_block(&dn, index); |
| 1903 | } |
| 1904 | } else if (locked) { |
| 1905 | err = f2fs_get_block(&dn, index); |
| 1906 | } else { |
| 1907 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 1908 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 1909 | } else { |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1910 | /* hole case */ |
| 1911 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | 4da7bf5 | 2016-04-06 11:27:03 -0700 | [diff] [blame] | 1912 | if (err || dn.data_blkaddr == NULL_ADDR) { |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1913 | f2fs_put_dnode(&dn); |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 1914 | __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, |
| 1915 | true); |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1916 | locked = true; |
| 1917 | goto restart; |
| 1918 | } |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1919 | } |
| 1920 | } |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1921 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1922 | /* convert_inline_page can make node_changed */ |
| 1923 | *blk_addr = dn.data_blkaddr; |
| 1924 | *node_changed = dn.node_changed; |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1925 | out: |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1926 | f2fs_put_dnode(&dn); |
| 1927 | unlock_out: |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1928 | if (locked) |
Yunlei He | 59c9081 | 2017-03-13 20:22:18 +0800 | [diff] [blame] | 1929 | __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1930 | return err; |
| 1931 | } |
| 1932 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1933 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 1934 | loff_t pos, unsigned len, unsigned flags, |
| 1935 | struct page **pagep, void **fsdata) |
| 1936 | { |
| 1937 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1938 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1939 | struct page *page = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1940 | pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1941 | bool need_balance = false; |
| 1942 | block_t blkaddr = NULL_ADDR; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1943 | int err = 0; |
| 1944 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 1945 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 1946 | |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame^] | 1947 | if (f2fs_is_atomic_file(inode) && |
| 1948 | !available_free_memory(sbi, INMEM_PAGES)) { |
| 1949 | err = -ENOMEM; |
| 1950 | goto fail; |
| 1951 | } |
| 1952 | |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1953 | /* |
| 1954 | * We should check this at this moment to avoid deadlock on inode page |
| 1955 | * and #0 page. The locking rule for inline_data conversion should be: |
| 1956 | * lock_page(page #0) -> lock_page(inode_page) |
| 1957 | */ |
| 1958 | if (index != 0) { |
| 1959 | err = f2fs_convert_inline_inode(inode); |
| 1960 | if (err) |
| 1961 | goto fail; |
| 1962 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1963 | repeat: |
Jaegeuk Kim | 86d5479 | 2017-02-17 09:55:55 -0800 | [diff] [blame] | 1964 | /* |
| 1965 | * Do not use grab_cache_page_write_begin() to avoid deadlock due to |
| 1966 | * wait_for_stable_page. Will wait that below with our IO control. |
| 1967 | */ |
| 1968 | page = pagecache_get_page(mapping, index, |
| 1969 | FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1970 | if (!page) { |
| 1971 | err = -ENOMEM; |
| 1972 | goto fail; |
| 1973 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 1974 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1975 | *pagep = page; |
| 1976 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1977 | err = prepare_write_begin(sbi, page, pos, len, |
| 1978 | &blkaddr, &need_balance); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1979 | if (err) |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1980 | goto fail; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1981 | |
Jaegeuk Kim | 7f3037a | 2016-09-01 12:02:51 -0700 | [diff] [blame] | 1982 | if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) { |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1983 | unlock_page(page); |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 1984 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1985 | lock_page(page); |
| 1986 | if (page->mapping != mapping) { |
| 1987 | /* The page got truncated from under us */ |
| 1988 | f2fs_put_page(page, 1); |
| 1989 | goto repeat; |
| 1990 | } |
| 1991 | } |
| 1992 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 1993 | f2fs_wait_on_page_writeback(page, DATA, false); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1994 | |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 1995 | /* wait for GCed encrypted page writeback */ |
Jaegeuk Kim | 1958593 | 2017-09-05 16:54:24 -0700 | [diff] [blame] | 1996 | if (f2fs_encrypted_file(inode)) |
Jaegeuk Kim | d4c759e | 2017-09-05 17:04:35 -0700 | [diff] [blame] | 1997 | f2fs_wait_on_block_writeback(sbi, blkaddr); |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 1998 | |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 1999 | if (len == PAGE_SIZE || PageUptodate(page)) |
| 2000 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2001 | |
Yunlei He | 746e240 | 2016-12-20 11:11:35 +0800 | [diff] [blame] | 2002 | if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) { |
| 2003 | zero_user_segment(page, len, PAGE_SIZE); |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2007 | if (blkaddr == NEW_ADDR) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2008 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2009 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2010 | } else { |
Jaegeuk Kim | 13ba41e | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 2011 | err = f2fs_submit_page_read(inode, page, blkaddr); |
| 2012 | if (err) |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2013 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 2014 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 2015 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 2016 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 2017 | f2fs_put_page(page, 1); |
| 2018 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2019 | } |
Chao Yu | 1563ac7 | 2016-07-03 22:05:12 +0800 | [diff] [blame] | 2020 | if (unlikely(!PageUptodate(page))) { |
| 2021 | err = -EIO; |
| 2022 | goto fail; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 2023 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2024 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2025 | return 0; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 2026 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2027 | fail: |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 2028 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2029 | f2fs_write_failed(mapping, pos + len); |
Jaegeuk Kim | 57864ae | 2017-10-18 19:05:57 -0700 | [diff] [blame^] | 2030 | if (f2fs_is_atomic_file(inode)) |
| 2031 | drop_inmem_pages_all(sbi); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2032 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2033 | } |
| 2034 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2035 | static int f2fs_write_end(struct file *file, |
| 2036 | struct address_space *mapping, |
| 2037 | loff_t pos, unsigned len, unsigned copied, |
| 2038 | struct page *page, void *fsdata) |
| 2039 | { |
| 2040 | struct inode *inode = page->mapping->host; |
| 2041 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 2042 | trace_f2fs_write_end(inode, pos, len, copied); |
| 2043 | |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2044 | /* |
| 2045 | * This should be come from len == PAGE_SIZE, and we expect copied |
| 2046 | * should be PAGE_SIZE. Otherwise, we treat it with zero copied and |
| 2047 | * let generic_perform_write() try to copy data again through copied=0. |
| 2048 | */ |
| 2049 | if (!PageUptodate(page)) { |
Yunlei He | 746e240 | 2016-12-20 11:11:35 +0800 | [diff] [blame] | 2050 | if (unlikely(copied != len)) |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2051 | copied = 0; |
| 2052 | else |
| 2053 | SetPageUptodate(page); |
| 2054 | } |
| 2055 | if (!copied) |
| 2056 | goto unlock_out; |
| 2057 | |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 2058 | set_page_dirty(page); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2059 | |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 2060 | if (pos + copied > i_size_read(inode)) |
| 2061 | f2fs_i_size_write(inode, pos + copied); |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2062 | unlock_out: |
Chao Yu | 3024c9a | 2016-08-06 21:09:41 +0800 | [diff] [blame] | 2063 | f2fs_put_page(page, 1); |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 2064 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2065 | return copied; |
| 2066 | } |
| 2067 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 2068 | static int check_direct_IO(struct inode *inode, struct iov_iter *iter, |
| 2069 | loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2070 | { |
| 2071 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2072 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2073 | if (offset & blocksize_mask) |
| 2074 | return -EINVAL; |
| 2075 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 2076 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 2077 | return -EINVAL; |
| 2078 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2079 | return 0; |
| 2080 | } |
| 2081 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 2082 | static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2083 | { |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 2084 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2085 | struct inode *inode = mapping->host; |
| 2086 | size_t count = iov_iter_count(iter); |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 2087 | loff_t offset = iocb->ki_pos; |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 2088 | int rw = iov_iter_rw(iter); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2089 | int err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2090 | |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 2091 | err = check_direct_IO(inode, iter, offset); |
Jaegeuk Kim | b9d777b | 2015-12-22 11:09:35 -0800 | [diff] [blame] | 2092 | if (err) |
| 2093 | return err; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 2094 | |
Jaegeuk Kim | c040ff9 | 2016-11-11 16:46:40 -0800 | [diff] [blame] | 2095 | if (__force_buffered_io(inode, rw)) |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2096 | return 0; |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 2097 | |
Jaegeuk Kim | 5302fb0 | 2016-07-22 15:25:47 -0700 | [diff] [blame] | 2098 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 2099 | |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 2100 | down_read(&F2FS_I(inode)->dio_rwsem[rw]); |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 2101 | err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio); |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 2102 | up_read(&F2FS_I(inode)->dio_rwsem[rw]); |
| 2103 | |
| 2104 | if (rw == WRITE) { |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2105 | if (err > 0) { |
| 2106 | f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO, |
| 2107 | err); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 2108 | set_inode_flag(inode, FI_UPDATE_WRITE); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2109 | } else if (err < 0) { |
Jaegeuk Kim | 6bfc491 | 2016-04-18 17:07:44 -0400 | [diff] [blame] | 2110 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | b0af6d4 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2111 | } |
Jaegeuk Kim | 6bfc491 | 2016-04-18 17:07:44 -0400 | [diff] [blame] | 2112 | } |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 2113 | |
Jaegeuk Kim | 5302fb0 | 2016-07-22 15:25:47 -0700 | [diff] [blame] | 2114 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 2115 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2116 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2117 | } |
| 2118 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2119 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 2120 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2121 | { |
| 2122 | struct inode *inode = page->mapping->host; |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2123 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2124 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2125 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2126 | (offset % PAGE_SIZE || length != PAGE_SIZE)) |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2127 | return; |
| 2128 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2129 | if (PageDirty(page)) { |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 2130 | if (inode->i_ino == F2FS_META_INO(sbi)) { |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2131 | dec_page_count(sbi, F2FS_DIRTY_META); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 2132 | } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2133 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 2134 | } else { |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2135 | inode_dec_dirty_pages(inode); |
Chao Yu | 933439c | 2016-10-11 22:57:01 +0800 | [diff] [blame] | 2136 | remove_dirty_inode(inode); |
| 2137 | } |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2138 | } |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2139 | |
| 2140 | /* This is atomic written page, keep Private */ |
| 2141 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
Jaegeuk Kim | 8c242db | 2017-03-17 09:55:52 +0800 | [diff] [blame] | 2142 | return drop_inmem_page(inode, page); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2143 | |
Chao Yu | 23dc974 | 2016-04-29 20:09:15 +0800 | [diff] [blame] | 2144 | set_page_private(page, 0); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2145 | ClearPagePrivate(page); |
| 2146 | } |
| 2147 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2148 | int f2fs_release_page(struct page *page, gfp_t wait) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2149 | { |
Jaegeuk Kim | f68daee | 2015-01-30 11:39:08 -0800 | [diff] [blame] | 2150 | /* If this is dirty page, keep PagePrivate */ |
| 2151 | if (PageDirty(page)) |
| 2152 | return 0; |
| 2153 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2154 | /* This is atomic written page, keep Private */ |
| 2155 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 2156 | return 0; |
| 2157 | |
Chao Yu | 23dc974 | 2016-04-29 20:09:15 +0800 | [diff] [blame] | 2158 | set_page_private(page, 0); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2159 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 2160 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2161 | } |
| 2162 | |
Jaegeuk Kim | fe76b79 | 2016-06-30 18:40:10 -0700 | [diff] [blame] | 2163 | /* |
| 2164 | * This was copied from __set_page_dirty_buffers which gives higher performance |
| 2165 | * in very high speed storages. (e.g., pmem) |
| 2166 | */ |
| 2167 | void f2fs_set_page_dirty_nobuffers(struct page *page) |
| 2168 | { |
| 2169 | struct address_space *mapping = page->mapping; |
| 2170 | unsigned long flags; |
| 2171 | |
| 2172 | if (unlikely(!mapping)) |
| 2173 | return; |
| 2174 | |
| 2175 | spin_lock(&mapping->private_lock); |
| 2176 | lock_page_memcg(page); |
| 2177 | SetPageDirty(page); |
| 2178 | spin_unlock(&mapping->private_lock); |
| 2179 | |
| 2180 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 2181 | WARN_ON_ONCE(!PageUptodate(page)); |
| 2182 | account_page_dirtied(page, mapping); |
| 2183 | radix_tree_tag_set(&mapping->page_tree, |
| 2184 | page_index(page), PAGECACHE_TAG_DIRTY); |
| 2185 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
| 2186 | unlock_page_memcg(page); |
| 2187 | |
| 2188 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
| 2189 | return; |
| 2190 | } |
| 2191 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2192 | static int f2fs_set_data_page_dirty(struct page *page) |
| 2193 | { |
| 2194 | struct address_space *mapping = page->mapping; |
| 2195 | struct inode *inode = mapping->host; |
| 2196 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 2197 | trace_f2fs_set_page_dirty(page, DATA); |
| 2198 | |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 2199 | if (!PageUptodate(page)) |
| 2200 | SetPageUptodate(page); |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 2201 | |
Chao Yu | 5fe4574 | 2017-01-07 18:50:26 +0800 | [diff] [blame] | 2202 | if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) { |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2203 | if (!IS_ATOMIC_WRITTEN_PAGE(page)) { |
| 2204 | register_inmem_page(inode, page); |
| 2205 | return 1; |
| 2206 | } |
| 2207 | /* |
| 2208 | * Previously, this page has been registered, we just |
| 2209 | * return here. |
| 2210 | */ |
| 2211 | return 0; |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2214 | if (!PageDirty(page)) { |
Jaegeuk Kim | fe76b79 | 2016-06-30 18:40:10 -0700 | [diff] [blame] | 2215 | f2fs_set_page_dirty_nobuffers(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2216 | update_dirty_page(inode, page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2217 | return 1; |
| 2218 | } |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 2222 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 2223 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 2224 | struct inode *inode = mapping->host; |
| 2225 | |
Jaegeuk Kim | 1d373a0 | 2015-10-19 10:29:51 -0700 | [diff] [blame] | 2226 | if (f2fs_has_inline_data(inode)) |
| 2227 | return 0; |
| 2228 | |
| 2229 | /* make sure allocating whole blocks */ |
| 2230 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 2231 | filemap_write_and_wait(mapping); |
| 2232 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 2233 | return generic_block_bmap(mapping, block, get_data_block_bmap); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 2234 | } |
| 2235 | |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2236 | #ifdef CONFIG_MIGRATION |
| 2237 | #include <linux/migrate.h> |
| 2238 | |
| 2239 | int f2fs_migrate_page(struct address_space *mapping, |
| 2240 | struct page *newpage, struct page *page, enum migrate_mode mode) |
| 2241 | { |
| 2242 | int rc, extra_count; |
| 2243 | struct f2fs_inode_info *fi = F2FS_I(mapping->host); |
| 2244 | bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page); |
| 2245 | |
| 2246 | BUG_ON(PageWriteback(page)); |
| 2247 | |
| 2248 | /* migrating an atomic written page is safe with the inmem_lock hold */ |
Jaegeuk Kim | ff1048e | 2017-07-06 14:46:01 -0700 | [diff] [blame] | 2249 | if (atomic_written) { |
| 2250 | if (mode != MIGRATE_SYNC) |
| 2251 | return -EBUSY; |
| 2252 | if (!mutex_trylock(&fi->inmem_lock)) |
| 2253 | return -EAGAIN; |
| 2254 | } |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2255 | |
| 2256 | /* |
| 2257 | * A reference is expected if PagePrivate set when move mapping, |
| 2258 | * however F2FS breaks this for maintaining dirty page counts when |
| 2259 | * truncating pages. So here adjusting the 'extra_count' make it work. |
| 2260 | */ |
| 2261 | extra_count = (atomic_written ? 1 : 0) - page_has_private(page); |
| 2262 | rc = migrate_page_move_mapping(mapping, newpage, |
| 2263 | page, NULL, mode, extra_count); |
| 2264 | if (rc != MIGRATEPAGE_SUCCESS) { |
| 2265 | if (atomic_written) |
| 2266 | mutex_unlock(&fi->inmem_lock); |
| 2267 | return rc; |
| 2268 | } |
| 2269 | |
| 2270 | if (atomic_written) { |
| 2271 | struct inmem_pages *cur; |
| 2272 | list_for_each_entry(cur, &fi->inmem_pages, list) |
| 2273 | if (cur->page == page) { |
| 2274 | cur->page = newpage; |
| 2275 | break; |
| 2276 | } |
| 2277 | mutex_unlock(&fi->inmem_lock); |
| 2278 | put_page(page); |
| 2279 | get_page(newpage); |
| 2280 | } |
| 2281 | |
| 2282 | if (PagePrivate(page)) |
| 2283 | SetPagePrivate(newpage); |
| 2284 | set_page_private(newpage, page_private(page)); |
| 2285 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 2286 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 2287 | migrate_page_copy(newpage, page); |
| 2288 | else |
| 2289 | migrate_page_states(newpage, page); |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2290 | |
| 2291 | return MIGRATEPAGE_SUCCESS; |
| 2292 | } |
| 2293 | #endif |
| 2294 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2295 | const struct address_space_operations f2fs_dblock_aops = { |
| 2296 | .readpage = f2fs_read_data_page, |
| 2297 | .readpages = f2fs_read_data_pages, |
| 2298 | .writepage = f2fs_write_data_page, |
| 2299 | .writepages = f2fs_write_data_pages, |
| 2300 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2301 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2302 | .set_page_dirty = f2fs_set_data_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2303 | .invalidatepage = f2fs_invalidate_page, |
| 2304 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2305 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 2306 | .bmap = f2fs_bmap, |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2307 | #ifdef CONFIG_MIGRATION |
| 2308 | .migratepage = f2fs_migrate_page, |
| 2309 | #endif |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2310 | }; |