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