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