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