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