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