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