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