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