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