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