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