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