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