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