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> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/bio.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 19 | #include <linux/prefetch.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 20 | #include <linux/uio.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 21 | |
| 22 | #include "f2fs.h" |
| 23 | #include "node.h" |
| 24 | #include "segment.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 25 | #include "trace.h" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 26 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 27 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 28 | static void f2fs_read_end_io(struct bio *bio, int err) |
| 29 | { |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 30 | struct bio_vec *bvec; |
| 31 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 32 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 33 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 34 | struct page *page = bvec->bv_page; |
| 35 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 36 | if (!err) { |
| 37 | SetPageUptodate(page); |
| 38 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 39 | ClearPageUptodate(page); |
| 40 | SetPageError(page); |
| 41 | } |
| 42 | unlock_page(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 43 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 44 | bio_put(bio); |
| 45 | } |
| 46 | |
| 47 | static void f2fs_write_end_io(struct bio *bio, int err) |
| 48 | { |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 49 | struct f2fs_sb_info *sbi = bio->bi_private; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 50 | struct bio_vec *bvec; |
| 51 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 52 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 53 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 54 | struct page *page = bvec->bv_page; |
| 55 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 56 | if (unlikely(err)) { |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 57 | set_page_dirty(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 58 | set_bit(AS_EIO, &page->mapping->flags); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 59 | f2fs_stop_checkpoint(sbi); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 60 | } |
| 61 | end_page_writeback(page); |
| 62 | dec_page_count(sbi, F2FS_WRITEBACK); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 63 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 64 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 65 | if (!get_pages(sbi, F2FS_WRITEBACK) && |
| 66 | !list_empty(&sbi->cp_wait.task_list)) |
| 67 | wake_up(&sbi->cp_wait); |
| 68 | |
| 69 | bio_put(bio); |
| 70 | } |
| 71 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 72 | /* |
| 73 | * Low-level block read/write IO operations. |
| 74 | */ |
| 75 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 76 | int npages, bool is_read) |
| 77 | { |
| 78 | struct bio *bio; |
| 79 | |
| 80 | /* No failure on bio allocation */ |
| 81 | bio = bio_alloc(GFP_NOIO, npages); |
| 82 | |
| 83 | bio->bi_bdev = sbi->sb->s_bdev; |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 84 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 85 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 86 | bio->bi_private = sbi; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 87 | |
| 88 | return bio; |
| 89 | } |
| 90 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 91 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 92 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 93 | struct f2fs_io_info *fio = &io->fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 94 | |
| 95 | if (!io->bio) |
| 96 | return; |
| 97 | |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 98 | if (is_read_io(fio->rw)) |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 99 | trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio); |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 100 | else |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 101 | trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 102 | |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 103 | submit_bio(fio->rw, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 104 | io->bio = NULL; |
| 105 | } |
| 106 | |
| 107 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 108 | enum page_type type, int rw) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 109 | { |
| 110 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 111 | struct f2fs_bio_info *io; |
| 112 | |
| 113 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; |
| 114 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 115 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 116 | |
| 117 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 118 | if (type >= META_FLUSH) { |
| 119 | io->fio.type = META_FLUSH; |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 120 | if (test_opt(sbi, NOBARRIER)) |
| 121 | io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO; |
| 122 | else |
| 123 | io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 124 | } |
| 125 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 126 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Fill the locked page with data located in the block address. |
| 131 | * Return unlocked page. |
| 132 | */ |
| 133 | int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 134 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 135 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 136 | struct bio *bio; |
| 137 | |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 138 | trace_f2fs_submit_page_bio(page, fio); |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 139 | f2fs_trace_ios(page, fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 140 | |
| 141 | /* Allocate a new bio */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 142 | bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 143 | |
| 144 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
| 145 | bio_put(bio); |
| 146 | f2fs_put_page(page, 1); |
| 147 | return -EFAULT; |
| 148 | } |
| 149 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 150 | submit_bio(fio->rw, bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 155 | struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 156 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 157 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 158 | struct f2fs_bio_info *io; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 159 | bool is_read = is_read_io(fio->rw); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 160 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 161 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 162 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 163 | verify_block_addr(sbi, fio->blk_addr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 164 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 165 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 166 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 167 | if (!is_read) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 168 | inc_page_count(sbi, F2FS_WRITEBACK); |
| 169 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 170 | if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 || |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 171 | io->fio.rw != fio->rw)) |
| 172 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 173 | alloc_new: |
| 174 | if (io->bio == NULL) { |
Jaegeuk Kim | 90a893c | 2014-09-22 16:21:07 -0700 | [diff] [blame] | 175 | int bio_blocks = MAX_BIO_BLOCKS(sbi); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 176 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 177 | io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 178 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) < |
| 182 | PAGE_CACHE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 183 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 184 | goto alloc_new; |
| 185 | } |
| 186 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 187 | io->last_block_in_bio = fio->blk_addr; |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 188 | f2fs_trace_ios(page, fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 189 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 190 | up_write(&io->io_rwsem); |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 191 | trace_f2fs_submit_page_mbio(page, fio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 195 | * Lock ordering for the change of data block address: |
| 196 | * ->data_page |
| 197 | * ->node_page |
| 198 | * update block addresses in the node page |
| 199 | */ |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 200 | static void __set_data_blkaddr(struct dnode_of_data *dn) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 201 | { |
| 202 | struct f2fs_node *rn; |
| 203 | __le32 *addr_array; |
| 204 | struct page *node_page = dn->node_page; |
| 205 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 206 | |
Yuan Zhong | 5514f0a | 2014-01-10 07:26:14 +0000 | [diff] [blame] | 207 | f2fs_wait_on_page_writeback(node_page, NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 208 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 209 | rn = F2FS_NODE(node_page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 210 | |
| 211 | /* Get physical address of data block */ |
| 212 | addr_array = blkaddr_in_node(rn); |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 213 | addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 214 | set_page_dirty(node_page); |
| 215 | } |
| 216 | |
| 217 | int reserve_new_block(struct dnode_of_data *dn) |
| 218 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 219 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 220 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 221 | 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] | 222 | return -EPERM; |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 223 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 224 | return -ENOSPC; |
| 225 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 226 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 227 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 228 | dn->data_blkaddr = NEW_ADDR; |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 229 | __set_data_blkaddr(dn); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 230 | mark_inode_dirty(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 231 | sync_inode_page(dn); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 235 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 236 | { |
| 237 | bool need_put = dn->inode_page ? false : true; |
| 238 | int err; |
| 239 | |
| 240 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 241 | if (err) |
| 242 | return err; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 243 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 244 | if (dn->data_blkaddr == NULL_ADDR) |
| 245 | err = reserve_new_block(dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 246 | if (err || need_put) |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 247 | f2fs_put_dnode(dn); |
| 248 | return err; |
| 249 | } |
| 250 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 251 | static int check_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 252 | struct buffer_head *bh_result) |
| 253 | { |
| 254 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 255 | pgoff_t start_fofs, end_fofs; |
| 256 | block_t start_blkaddr; |
| 257 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 258 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 259 | return 0; |
| 260 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 261 | read_lock(&fi->ext.ext_lock); |
| 262 | if (fi->ext.len == 0) { |
| 263 | read_unlock(&fi->ext.ext_lock); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 267 | stat_inc_total_hit(inode->i_sb); |
| 268 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 269 | start_fofs = fi->ext.fofs; |
| 270 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 271 | start_blkaddr = fi->ext.blk_addr; |
| 272 | |
| 273 | if (pgofs >= start_fofs && pgofs <= end_fofs) { |
| 274 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 275 | size_t count; |
| 276 | |
Jaegeuk Kim | da17eec | 2015-02-09 10:34:38 -0800 | [diff] [blame] | 277 | set_buffer_new(bh_result); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 278 | map_bh(bh_result, inode->i_sb, |
| 279 | start_blkaddr + pgofs - start_fofs); |
| 280 | count = end_fofs - pgofs + 1; |
| 281 | if (count < (UINT_MAX >> blkbits)) |
| 282 | bh_result->b_size = (count << blkbits); |
| 283 | else |
| 284 | bh_result->b_size = UINT_MAX; |
| 285 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 286 | stat_inc_read_hit(inode->i_sb); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 287 | read_unlock(&fi->ext.ext_lock); |
| 288 | return 1; |
| 289 | } |
| 290 | read_unlock(&fi->ext.ext_lock); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 294 | void update_extent_cache(struct dnode_of_data *dn) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 295 | { |
| 296 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 297 | pgoff_t fofs, start_fofs, end_fofs; |
| 298 | block_t start_blkaddr, end_blkaddr; |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 299 | int need_update = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 300 | |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 301 | f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 302 | |
| 303 | /* Update the page address in the parent node */ |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 304 | __set_data_blkaddr(dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 305 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 306 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 307 | return; |
| 308 | |
Jaegeuk Kim | 3547ea9 | 2014-12-30 23:08:26 -0800 | [diff] [blame] | 309 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 310 | dn->ofs_in_node; |
| 311 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 312 | write_lock(&fi->ext.ext_lock); |
| 313 | |
| 314 | start_fofs = fi->ext.fofs; |
| 315 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 316 | start_blkaddr = fi->ext.blk_addr; |
| 317 | end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1; |
| 318 | |
| 319 | /* Drop and initialize the matched extent */ |
| 320 | if (fi->ext.len == 1 && fofs == start_fofs) |
| 321 | fi->ext.len = 0; |
| 322 | |
| 323 | /* Initial extent */ |
| 324 | if (fi->ext.len == 0) { |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 325 | if (dn->data_blkaddr != NULL_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 326 | fi->ext.fofs = fofs; |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 327 | fi->ext.blk_addr = dn->data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 328 | fi->ext.len = 1; |
| 329 | } |
| 330 | goto end_update; |
| 331 | } |
| 332 | |
Namjae Jeon | 6224da8 | 2013-04-06 14:44:32 +0900 | [diff] [blame] | 333 | /* Front merge */ |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 334 | if (fofs == start_fofs - 1 && dn->data_blkaddr == start_blkaddr - 1) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 335 | fi->ext.fofs--; |
| 336 | fi->ext.blk_addr--; |
| 337 | fi->ext.len++; |
| 338 | goto end_update; |
| 339 | } |
| 340 | |
| 341 | /* Back merge */ |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 342 | if (fofs == end_fofs + 1 && dn->data_blkaddr == end_blkaddr + 1) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 343 | fi->ext.len++; |
| 344 | goto end_update; |
| 345 | } |
| 346 | |
| 347 | /* Split the existing extent */ |
| 348 | if (fi->ext.len > 1 && |
| 349 | fofs >= start_fofs && fofs <= end_fofs) { |
| 350 | if ((end_fofs - fofs) < (fi->ext.len >> 1)) { |
| 351 | fi->ext.len = fofs - start_fofs; |
| 352 | } else { |
| 353 | fi->ext.fofs = fofs + 1; |
| 354 | fi->ext.blk_addr = start_blkaddr + |
| 355 | fofs - start_fofs + 1; |
| 356 | fi->ext.len -= fofs - start_fofs + 1; |
| 357 | } |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 358 | } else { |
| 359 | need_update = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 360 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 361 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 362 | /* Finally, if the extent is very fragmented, let's drop the cache. */ |
| 363 | if (fi->ext.len < F2FS_MIN_EXTENT_LEN) { |
| 364 | fi->ext.len = 0; |
| 365 | set_inode_flag(fi, FI_NO_EXTENT); |
| 366 | need_update = true; |
| 367 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 368 | end_update: |
| 369 | write_unlock(&fi->ext.ext_lock); |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 370 | if (need_update) |
| 371 | sync_inode_page(dn); |
| 372 | return; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 373 | } |
| 374 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 375 | struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 376 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 377 | struct address_space *mapping = inode->i_mapping; |
| 378 | struct dnode_of_data dn; |
| 379 | struct page *page; |
| 380 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 381 | struct f2fs_io_info fio = { |
| 382 | .type = DATA, |
| 383 | .rw = sync ? READ_SYNC : READA, |
| 384 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 385 | |
| 386 | page = find_get_page(mapping, index); |
| 387 | if (page && PageUptodate(page)) |
| 388 | return page; |
| 389 | f2fs_put_page(page, 0); |
| 390 | |
| 391 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 392 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 393 | if (err) |
| 394 | return ERR_PTR(err); |
| 395 | f2fs_put_dnode(&dn); |
| 396 | |
| 397 | if (dn.data_blkaddr == NULL_ADDR) |
| 398 | return ERR_PTR(-ENOENT); |
| 399 | |
| 400 | /* By fallocate(), there is no cached page, but with NEW_ADDR */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 401 | if (unlikely(dn.data_blkaddr == NEW_ADDR)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 402 | return ERR_PTR(-EINVAL); |
| 403 | |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 404 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 405 | if (!page) |
| 406 | return ERR_PTR(-ENOMEM); |
| 407 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 408 | if (PageUptodate(page)) { |
| 409 | unlock_page(page); |
| 410 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 411 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 412 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 413 | fio.blk_addr = dn.data_blkaddr; |
| 414 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Chao Yu | 1069bbf | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 415 | if (err) |
| 416 | return ERR_PTR(err); |
| 417 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 418 | if (sync) { |
| 419 | wait_on_page_locked(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 420 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 421 | f2fs_put_page(page, 0); |
| 422 | return ERR_PTR(-EIO); |
| 423 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 424 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 425 | return page; |
| 426 | } |
| 427 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 428 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 429 | * If it tries to access a hole, return an error. |
| 430 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 431 | * whether this page exists or not. |
| 432 | */ |
| 433 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index) |
| 434 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 435 | struct address_space *mapping = inode->i_mapping; |
| 436 | struct dnode_of_data dn; |
| 437 | struct page *page; |
| 438 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 439 | struct f2fs_io_info fio = { |
| 440 | .type = DATA, |
| 441 | .rw = READ_SYNC, |
| 442 | }; |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 443 | repeat: |
Jaegeuk Kim | 9ac1349 | 2014-04-29 17:35:10 +0900 | [diff] [blame] | 444 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 445 | if (!page) |
| 446 | return ERR_PTR(-ENOMEM); |
| 447 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 448 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 449 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 450 | if (err) { |
| 451 | f2fs_put_page(page, 1); |
| 452 | return ERR_PTR(err); |
| 453 | } |
| 454 | f2fs_put_dnode(&dn); |
| 455 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 456 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 457 | f2fs_put_page(page, 1); |
| 458 | return ERR_PTR(-ENOENT); |
| 459 | } |
| 460 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 461 | if (PageUptodate(page)) |
| 462 | return page; |
| 463 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 464 | /* |
| 465 | * A new dentry page is allocated but not able to be written, since its |
| 466 | * new inode page couldn't be allocated due to -ENOSPC. |
| 467 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 468 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 469 | */ |
| 470 | if (dn.data_blkaddr == NEW_ADDR) { |
| 471 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 472 | SetPageUptodate(page); |
| 473 | return page; |
| 474 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 475 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 476 | fio.blk_addr = dn.data_blkaddr; |
| 477 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 478 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 479 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 480 | |
| 481 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 482 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 483 | f2fs_put_page(page, 1); |
| 484 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 485 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 486 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 487 | f2fs_put_page(page, 1); |
| 488 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 489 | } |
| 490 | return page; |
| 491 | } |
| 492 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 493 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 494 | * Caller ensures that this data page is never allocated. |
| 495 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 496 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 497 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 498 | * f2fs_unlock_op(). |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 499 | * Note that, ipage is set only by make_empty_dir. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 500 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 501 | struct page *get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 502 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 503 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 504 | struct address_space *mapping = inode->i_mapping; |
| 505 | struct page *page; |
| 506 | struct dnode_of_data dn; |
| 507 | int err; |
| 508 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 509 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 510 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 511 | if (err) |
| 512 | return ERR_PTR(err); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 513 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 514 | page = grab_cache_page(mapping, index); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 515 | if (!page) { |
| 516 | err = -ENOMEM; |
| 517 | goto put_err; |
| 518 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 519 | |
| 520 | if (PageUptodate(page)) |
| 521 | return page; |
| 522 | |
| 523 | if (dn.data_blkaddr == NEW_ADDR) { |
| 524 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 525 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 526 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 527 | struct f2fs_io_info fio = { |
| 528 | .type = DATA, |
| 529 | .rw = READ_SYNC, |
| 530 | .blk_addr = dn.data_blkaddr, |
| 531 | }; |
| 532 | err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 533 | if (err) |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 534 | goto put_err; |
| 535 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 536 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 537 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 538 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 539 | err = -EIO; |
| 540 | goto put_err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 541 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 542 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 543 | f2fs_put_page(page, 1); |
| 544 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 545 | } |
| 546 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 547 | |
| 548 | if (new_i_size && |
| 549 | i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) { |
| 550 | i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 551 | /* Only the directory inode sets new_i_size */ |
| 552 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 553 | } |
| 554 | return page; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 555 | |
| 556 | put_err: |
| 557 | f2fs_put_dnode(&dn); |
| 558 | return ERR_PTR(err); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 559 | } |
| 560 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 561 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 562 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 563 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 564 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 565 | struct f2fs_summary sum; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 566 | struct node_info ni; |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 567 | int seg = CURSEG_WARM_DATA; |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 568 | pgoff_t fofs; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 569 | |
| 570 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 571 | return -EPERM; |
| 572 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 573 | return -ENOSPC; |
| 574 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 575 | get_node_info(sbi, dn->nid, &ni); |
| 576 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 577 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 578 | if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page) |
| 579 | seg = CURSEG_DIRECT_IO; |
| 580 | |
| 581 | allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 582 | |
| 583 | /* direct IO doesn't use extent cache to maximize the performance */ |
Jaegeuk Kim | 41ef94b | 2014-12-30 23:12:11 -0800 | [diff] [blame] | 584 | __set_data_blkaddr(dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 585 | |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 586 | /* update i_size */ |
| 587 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 588 | dn->ofs_in_node; |
| 589 | if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT)) |
| 590 | i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT)); |
| 591 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 595 | static void __allocate_data_blocks(struct inode *inode, loff_t offset, |
| 596 | size_t count) |
| 597 | { |
| 598 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 599 | struct dnode_of_data dn; |
| 600 | u64 start = F2FS_BYTES_TO_BLK(offset); |
| 601 | u64 len = F2FS_BYTES_TO_BLK(count); |
| 602 | bool allocated; |
| 603 | u64 end_offset; |
| 604 | |
| 605 | while (len) { |
| 606 | f2fs_balance_fs(sbi); |
| 607 | f2fs_lock_op(sbi); |
| 608 | |
| 609 | /* When reading holes, we need its node page */ |
| 610 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 611 | if (get_dnode_of_data(&dn, start, ALLOC_NODE)) |
| 612 | goto out; |
| 613 | |
| 614 | allocated = false; |
| 615 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
| 616 | |
| 617 | while (dn.ofs_in_node < end_offset && len) { |
| 618 | if (dn.data_blkaddr == NULL_ADDR) { |
| 619 | if (__allocate_data_block(&dn)) |
| 620 | goto sync_out; |
| 621 | allocated = true; |
| 622 | } |
| 623 | len--; |
| 624 | start++; |
| 625 | dn.ofs_in_node++; |
| 626 | } |
| 627 | |
| 628 | if (allocated) |
| 629 | sync_inode_page(&dn); |
| 630 | |
| 631 | f2fs_put_dnode(&dn); |
| 632 | f2fs_unlock_op(sbi); |
| 633 | } |
| 634 | return; |
| 635 | |
| 636 | sync_out: |
| 637 | if (allocated) |
| 638 | sync_inode_page(&dn); |
| 639 | f2fs_put_dnode(&dn); |
| 640 | out: |
| 641 | f2fs_unlock_op(sbi); |
| 642 | return; |
| 643 | } |
| 644 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 645 | /* |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 646 | * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh. |
| 647 | * If original data blocks are allocated, then give them to blockdev. |
| 648 | * Otherwise, |
| 649 | * a. preallocate requested block addresses |
| 650 | * b. do not use extent cache for better performance |
| 651 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 652 | */ |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 653 | static int __get_data_block(struct inode *inode, sector_t iblock, |
| 654 | struct buffer_head *bh_result, int create, bool fiemap) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 655 | { |
| 656 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 657 | unsigned maxblocks = bh_result->b_size >> blkbits; |
| 658 | struct dnode_of_data dn; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 659 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 660 | pgoff_t pgofs, end_offset; |
| 661 | int err = 0, ofs = 1; |
| 662 | bool allocated = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 663 | |
| 664 | /* Get the page offset from the block offset(iblock) */ |
| 665 | pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits)); |
| 666 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 667 | if (check_extent_cache(inode, pgofs, bh_result)) |
| 668 | goto out; |
| 669 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 670 | if (create) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 671 | f2fs_lock_op(F2FS_I_SB(inode)); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 672 | |
| 673 | /* When reading holes, we need its node page */ |
| 674 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 675 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 676 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 677 | if (err == -ENOENT) |
| 678 | err = 0; |
| 679 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 680 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 681 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 682 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 683 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 684 | if (dn.data_blkaddr != NULL_ADDR) { |
Jaegeuk Kim | da17eec | 2015-02-09 10:34:38 -0800 | [diff] [blame] | 685 | set_buffer_new(bh_result); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 686 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 687 | } else if (create) { |
| 688 | err = __allocate_data_block(&dn); |
| 689 | if (err) |
| 690 | goto put_out; |
| 691 | allocated = true; |
Jaegeuk Kim | da17eec | 2015-02-09 10:34:38 -0800 | [diff] [blame] | 692 | set_buffer_new(bh_result); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 693 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
| 694 | } else { |
| 695 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 696 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 697 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 698 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 699 | bh_result->b_size = (((size_t)1) << blkbits); |
| 700 | dn.ofs_in_node++; |
| 701 | pgofs++; |
| 702 | |
| 703 | get_next: |
| 704 | if (dn.ofs_in_node >= end_offset) { |
| 705 | if (allocated) |
| 706 | sync_inode_page(&dn); |
| 707 | allocated = false; |
| 708 | f2fs_put_dnode(&dn); |
| 709 | |
| 710 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 711 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 712 | if (err) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 713 | if (err == -ENOENT) |
| 714 | err = 0; |
| 715 | goto unlock_out; |
| 716 | } |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 717 | if (dn.data_blkaddr == NEW_ADDR && !fiemap) |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 718 | goto put_out; |
| 719 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 720 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if (maxblocks > (bh_result->b_size >> blkbits)) { |
| 724 | block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 725 | if (blkaddr == NULL_ADDR && create) { |
| 726 | err = __allocate_data_block(&dn); |
| 727 | if (err) |
| 728 | goto sync_out; |
| 729 | allocated = true; |
| 730 | blkaddr = dn.data_blkaddr; |
| 731 | } |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 732 | /* Give more consecutive addresses for the readahead */ |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 733 | if (blkaddr == (bh_result->b_blocknr + ofs)) { |
| 734 | ofs++; |
| 735 | dn.ofs_in_node++; |
| 736 | pgofs++; |
| 737 | bh_result->b_size += (((size_t)1) << blkbits); |
| 738 | goto get_next; |
| 739 | } |
| 740 | } |
| 741 | sync_out: |
| 742 | if (allocated) |
| 743 | sync_inode_page(&dn); |
| 744 | put_out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 745 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 746 | unlock_out: |
| 747 | if (create) |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 748 | f2fs_unlock_op(F2FS_I_SB(inode)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 749 | out: |
| 750 | trace_f2fs_get_data_block(inode, iblock, bh_result, err); |
| 751 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 752 | } |
| 753 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 754 | static int get_data_block(struct inode *inode, sector_t iblock, |
| 755 | struct buffer_head *bh_result, int create) |
| 756 | { |
| 757 | return __get_data_block(inode, iblock, bh_result, create, false); |
| 758 | } |
| 759 | |
| 760 | static int get_data_block_fiemap(struct inode *inode, sector_t iblock, |
| 761 | struct buffer_head *bh_result, int create) |
| 762 | { |
| 763 | return __get_data_block(inode, iblock, bh_result, create, true); |
| 764 | } |
| 765 | |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 766 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 767 | u64 start, u64 len) |
| 768 | { |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 769 | return generic_block_fiemap(inode, fieinfo, |
| 770 | start, len, get_data_block_fiemap); |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 771 | } |
| 772 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 773 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 774 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 775 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 776 | int ret = -EAGAIN; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 777 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 778 | trace_f2fs_readpage(page, DATA); |
| 779 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 780 | /* If the file has inline data, try to read it directly */ |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 781 | if (f2fs_has_inline_data(inode)) |
| 782 | ret = f2fs_read_inline_data(inode, page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 783 | if (ret == -EAGAIN) |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 784 | ret = mpage_readpage(page, get_data_block); |
| 785 | |
| 786 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static int f2fs_read_data_pages(struct file *file, |
| 790 | struct address_space *mapping, |
| 791 | struct list_head *pages, unsigned nr_pages) |
| 792 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 793 | struct inode *inode = file->f_mapping->host; |
| 794 | |
| 795 | /* If the file has inline data, skip readpages */ |
| 796 | if (f2fs_has_inline_data(inode)) |
| 797 | return 0; |
| 798 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 799 | return mpage_readpages(mapping, pages, nr_pages, get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 800 | } |
| 801 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 802 | int do_write_data_page(struct page *page, struct f2fs_io_info *fio) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 803 | { |
| 804 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 805 | struct dnode_of_data dn; |
| 806 | int err = 0; |
| 807 | |
| 808 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 809 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 810 | if (err) |
| 811 | return err; |
| 812 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 813 | fio->blk_addr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 814 | |
| 815 | /* This page is already truncated */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 816 | if (fio->blk_addr == NULL_ADDR) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 817 | goto out_writepage; |
| 818 | |
| 819 | set_page_writeback(page); |
| 820 | |
| 821 | /* |
| 822 | * If current allocation needs SSR, |
| 823 | * it had better in-place writes for updated data. |
| 824 | */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 825 | if (unlikely(fio->blk_addr != NEW_ADDR && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 826 | !is_cold_data(page) && |
| 827 | need_inplace_update(inode))) { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 828 | rewrite_data_page(page, fio); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 829 | set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 830 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 831 | write_data_page(page, &dn, fio); |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 832 | update_extent_cache(&dn); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 833 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 834 | } |
| 835 | out_writepage: |
| 836 | f2fs_put_dnode(&dn); |
| 837 | return err; |
| 838 | } |
| 839 | |
| 840 | static int f2fs_write_data_page(struct page *page, |
| 841 | struct writeback_control *wbc) |
| 842 | { |
| 843 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 844 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 845 | loff_t i_size = i_size_read(inode); |
| 846 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 847 | >> PAGE_CACHE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 848 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 849 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 850 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 851 | struct f2fs_io_info fio = { |
| 852 | .type = DATA, |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 853 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 854 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 855 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 856 | trace_f2fs_writepage(page, DATA); |
| 857 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 858 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 859 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 860 | |
| 861 | /* |
| 862 | * If the offset is out-of-range of file size, |
| 863 | * this page does not have to be written to disk. |
| 864 | */ |
| 865 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 866 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 867 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 868 | |
| 869 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 870 | write: |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 871 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 872 | goto redirty_out; |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 873 | if (f2fs_is_drop_cache(inode)) |
| 874 | goto out; |
| 875 | if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim && |
| 876 | available_free_memory(sbi, BASE_CHECK)) |
| 877 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 878 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 879 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 880 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 881 | if (unlikely(f2fs_cp_error(sbi))) |
| 882 | goto redirty_out; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 883 | err = do_write_data_page(page, &fio); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 884 | goto done; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 885 | } |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 886 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 887 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 888 | if (unlikely(f2fs_cp_error(sbi))) { |
| 889 | SetPageError(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 890 | goto out; |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 893 | if (!wbc->for_reclaim) |
| 894 | need_balance_fs = true; |
| 895 | else if (has_not_enough_free_secs(sbi, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 896 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 897 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 898 | err = -EAGAIN; |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 899 | f2fs_lock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 900 | if (f2fs_has_inline_data(inode)) |
| 901 | err = f2fs_write_inline_data(inode, page); |
| 902 | if (err == -EAGAIN) |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 903 | err = do_write_data_page(page, &fio); |
| 904 | f2fs_unlock_op(sbi); |
| 905 | done: |
| 906 | if (err && err != -ENOENT) |
| 907 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 908 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 909 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 910 | out: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 911 | inode_dec_dirty_pages(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 912 | unlock_page(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 913 | if (need_balance_fs) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 914 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 2aea39e | 2014-04-24 09:49:52 +0900 | [diff] [blame] | 915 | if (wbc->for_reclaim) |
| 916 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 917 | return 0; |
| 918 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 919 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 920 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 921 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 922 | } |
| 923 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 924 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 925 | void *data) |
| 926 | { |
| 927 | struct address_space *mapping = data; |
| 928 | int ret = mapping->a_ops->writepage(page, wbc); |
| 929 | mapping_set_error(mapping, ret); |
| 930 | return ret; |
| 931 | } |
| 932 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 933 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 934 | struct writeback_control *wbc) |
| 935 | { |
| 936 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 937 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 938 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 939 | int ret; |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 940 | long diff; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 941 | |
Chao Yu | e574843 | 2014-05-06 16:51:24 +0800 | [diff] [blame] | 942 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 943 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 944 | /* deal with chardevs and other special file */ |
| 945 | if (!mapping->a_ops->writepage) |
| 946 | return 0; |
| 947 | |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 948 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 949 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
Jaegeuk Kim | 6fb03f3 | 2014-04-16 10:47:06 +0900 | [diff] [blame] | 950 | available_free_memory(sbi, DIRTY_DENTS)) |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 951 | goto skip_write; |
Jaegeuk Kim | 87d6f89 | 2014-03-18 12:40:49 +0900 | [diff] [blame] | 952 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 953 | diff = nr_pages_to_write(sbi, DATA, wbc); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 954 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 955 | if (!S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 956 | mutex_lock(&sbi->writepages); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 957 | locked = true; |
| 958 | } |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 959 | ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 960 | if (locked) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 961 | mutex_unlock(&sbi->writepages); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 962 | |
| 963 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 964 | |
| 965 | remove_dirty_dir_inode(inode); |
| 966 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 967 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 968 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 969 | |
| 970 | skip_write: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 971 | wbc->pages_skipped += get_dirty_pages(inode); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 972 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 973 | } |
| 974 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 975 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 976 | { |
| 977 | struct inode *inode = mapping->host; |
| 978 | |
| 979 | if (to > inode->i_size) { |
| 980 | truncate_pagecache(inode, inode->i_size); |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 981 | truncate_blocks(inode, inode->i_size, true); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 985 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 986 | loff_t pos, unsigned len, unsigned flags, |
| 987 | struct page **pagep, void **fsdata) |
| 988 | { |
| 989 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 990 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 991 | struct page *page, *ipage; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 992 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 993 | struct dnode_of_data dn; |
| 994 | int err = 0; |
| 995 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 996 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 997 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 998 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * We should check this at this moment to avoid deadlock on inode page |
| 1002 | * and #0 page. The locking rule for inline_data conversion should be: |
| 1003 | * lock_page(page #0) -> lock_page(inode_page) |
| 1004 | */ |
| 1005 | if (index != 0) { |
| 1006 | err = f2fs_convert_inline_inode(inode); |
| 1007 | if (err) |
| 1008 | goto fail; |
| 1009 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1010 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1011 | page = grab_cache_page_write_begin(mapping, index, flags); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1012 | if (!page) { |
| 1013 | err = -ENOMEM; |
| 1014 | goto fail; |
| 1015 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 1016 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1017 | *pagep = page; |
| 1018 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 1019 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1020 | |
| 1021 | /* check inline_data */ |
| 1022 | ipage = get_node_page(sbi, inode->i_ino); |
Chao Yu | cd34e29 | 2014-12-01 11:30:20 +0800 | [diff] [blame] | 1023 | if (IS_ERR(ipage)) { |
| 1024 | err = PTR_ERR(ipage); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1025 | goto unlock_fail; |
Chao Yu | cd34e29 | 2014-12-01 11:30:20 +0800 | [diff] [blame] | 1026 | } |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1027 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1028 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1029 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1030 | if (f2fs_has_inline_data(inode)) { |
| 1031 | if (pos + len <= MAX_INLINE_DATA) { |
| 1032 | read_inline_data(page, ipage); |
| 1033 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
| 1034 | sync_inode_page(&dn); |
| 1035 | goto put_next; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1036 | } |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1037 | err = f2fs_convert_inline_page(&dn, page); |
| 1038 | if (err) |
| 1039 | goto put_fail; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1040 | } |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1041 | err = f2fs_reserve_block(&dn, index); |
| 1042 | if (err) |
Jaegeuk Kim | 8cdcb71 | 2014-11-17 16:14:11 -0800 | [diff] [blame] | 1043 | goto put_fail; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1044 | put_next: |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1045 | f2fs_put_dnode(&dn); |
| 1046 | f2fs_unlock_op(sbi); |
| 1047 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1048 | if ((len == PAGE_CACHE_SIZE) || PageUptodate(page)) |
| 1049 | return 0; |
| 1050 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1051 | f2fs_wait_on_page_writeback(page, DATA); |
| 1052 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1053 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1054 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 1055 | unsigned end = start + len; |
| 1056 | |
| 1057 | /* Reading beyond i_size is simple: memset to zero */ |
| 1058 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1059 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1060 | } |
| 1061 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1062 | if (dn.data_blkaddr == NEW_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1063 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 1064 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1065 | struct f2fs_io_info fio = { |
| 1066 | .type = DATA, |
| 1067 | .rw = READ_SYNC, |
| 1068 | .blk_addr = dn.data_blkaddr, |
| 1069 | }; |
| 1070 | err = f2fs_submit_page_bio(sbi, page, &fio); |
Jan Kara | 9234f31 | 2014-10-22 15:21:47 +0200 | [diff] [blame] | 1071 | if (err) |
| 1072 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1073 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1074 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1075 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1076 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1077 | err = -EIO; |
| 1078 | goto fail; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1079 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1080 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1081 | f2fs_put_page(page, 1); |
| 1082 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1083 | } |
| 1084 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1085 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1086 | SetPageUptodate(page); |
| 1087 | clear_cold_data(page); |
| 1088 | return 0; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1089 | |
Jaegeuk Kim | 8cdcb71 | 2014-11-17 16:14:11 -0800 | [diff] [blame] | 1090 | put_fail: |
| 1091 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1092 | unlock_fail: |
| 1093 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1094 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1095 | fail: |
| 1096 | f2fs_write_failed(mapping, pos + len); |
| 1097 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1098 | } |
| 1099 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1100 | static int f2fs_write_end(struct file *file, |
| 1101 | struct address_space *mapping, |
| 1102 | loff_t pos, unsigned len, unsigned copied, |
| 1103 | struct page *page, void *fsdata) |
| 1104 | { |
| 1105 | struct inode *inode = page->mapping->host; |
| 1106 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 1107 | trace_f2fs_write_end(inode, pos, len, copied); |
| 1108 | |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1109 | set_page_dirty(page); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1110 | |
| 1111 | if (pos + copied > i_size_read(inode)) { |
| 1112 | i_size_write(inode, pos + copied); |
| 1113 | mark_inode_dirty(inode); |
| 1114 | update_inode_page(inode); |
| 1115 | } |
| 1116 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 1117 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1118 | return copied; |
| 1119 | } |
| 1120 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1121 | static int check_direct_IO(struct inode *inode, int rw, |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1122 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1123 | { |
| 1124 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1125 | |
| 1126 | if (rw == READ) |
| 1127 | return 0; |
| 1128 | |
| 1129 | if (offset & blocksize_mask) |
| 1130 | return -EINVAL; |
| 1131 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1132 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 1133 | return -EINVAL; |
| 1134 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1135 | return 0; |
| 1136 | } |
| 1137 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1138 | static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 1139 | struct iov_iter *iter, loff_t offset) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1140 | { |
| 1141 | struct file *file = iocb->ki_filp; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1142 | struct address_space *mapping = file->f_mapping; |
| 1143 | struct inode *inode = mapping->host; |
| 1144 | size_t count = iov_iter_count(iter); |
| 1145 | int err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1146 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1147 | /* we don't need to use inline_data strictly */ |
| 1148 | if (f2fs_has_inline_data(inode)) { |
| 1149 | err = f2fs_convert_inline_inode(inode); |
| 1150 | if (err) |
| 1151 | return err; |
| 1152 | } |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1153 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1154 | if (check_direct_IO(inode, rw, iter, offset)) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1155 | return 0; |
| 1156 | |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1157 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
| 1158 | |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1159 | if (rw & WRITE) |
| 1160 | __allocate_data_blocks(inode, offset, count); |
| 1161 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1162 | err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block); |
| 1163 | if (err < 0 && (rw & WRITE)) |
| 1164 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1165 | |
| 1166 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
| 1167 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1168 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1169 | } |
| 1170 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1171 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 1172 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1173 | { |
| 1174 | struct inode *inode = page->mapping->host; |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1175 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1176 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1177 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 1178 | (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE)) |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1179 | return; |
| 1180 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1181 | if (PageDirty(page)) { |
| 1182 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 1183 | dec_page_count(sbi, F2FS_DIRTY_META); |
| 1184 | else if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 1185 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1186 | else |
| 1187 | inode_dec_dirty_pages(inode); |
| 1188 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1189 | ClearPagePrivate(page); |
| 1190 | } |
| 1191 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1192 | int f2fs_release_page(struct page *page, gfp_t wait) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1193 | { |
Jaegeuk Kim | f68daee | 2015-01-30 11:39:08 -0800 | [diff] [blame] | 1194 | /* If this is dirty page, keep PagePrivate */ |
| 1195 | if (PageDirty(page)) |
| 1196 | return 0; |
| 1197 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1198 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 1199 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static int f2fs_set_data_page_dirty(struct page *page) |
| 1203 | { |
| 1204 | struct address_space *mapping = page->mapping; |
| 1205 | struct inode *inode = mapping->host; |
| 1206 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 1207 | trace_f2fs_set_page_dirty(page, DATA); |
| 1208 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1209 | SetPageUptodate(page); |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1210 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1211 | if (f2fs_is_atomic_file(inode)) { |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1212 | register_inmem_page(inode, page); |
| 1213 | return 1; |
| 1214 | } |
| 1215 | |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 1216 | mark_inode_dirty(inode); |
| 1217 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1218 | if (!PageDirty(page)) { |
| 1219 | __set_page_dirty_nobuffers(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1220 | update_dirty_page(inode, page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1221 | return 1; |
| 1222 | } |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1226 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 1227 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 1228 | struct inode *inode = mapping->host; |
| 1229 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1230 | /* we don't need to use inline_data strictly */ |
| 1231 | if (f2fs_has_inline_data(inode)) { |
| 1232 | int err = f2fs_convert_inline_inode(inode); |
| 1233 | if (err) |
| 1234 | return err; |
| 1235 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1236 | return generic_block_bmap(mapping, block, get_data_block); |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1237 | } |
| 1238 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1239 | const struct address_space_operations f2fs_dblock_aops = { |
| 1240 | .readpage = f2fs_read_data_page, |
| 1241 | .readpages = f2fs_read_data_pages, |
| 1242 | .writepage = f2fs_write_data_page, |
| 1243 | .writepages = f2fs_write_data_pages, |
| 1244 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1245 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1246 | .set_page_dirty = f2fs_set_data_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1247 | .invalidatepage = f2fs_invalidate_page, |
| 1248 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1249 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1250 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1251 | }; |