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> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 15 | #include <linux/aio.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 16 | #include <linux/writeback.h> |
| 17 | #include <linux/backing-dev.h> |
| 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> |
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" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 25 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 26 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 27 | static void f2fs_read_end_io(struct bio *bio, int err) |
| 28 | { |
| 29 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 30 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 31 | |
| 32 | do { |
| 33 | struct page *page = bvec->bv_page; |
| 34 | |
| 35 | if (--bvec >= bio->bi_io_vec) |
| 36 | prefetchw(&bvec->bv_page->flags); |
| 37 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 38 | if (unlikely(!uptodate)) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 39 | ClearPageUptodate(page); |
| 40 | SetPageError(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 41 | } else { |
| 42 | SetPageUptodate(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 43 | } |
| 44 | unlock_page(page); |
| 45 | } while (bvec >= bio->bi_io_vec); |
| 46 | |
| 47 | bio_put(bio); |
| 48 | } |
| 49 | |
| 50 | static void f2fs_write_end_io(struct bio *bio, int err) |
| 51 | { |
| 52 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 53 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 54 | struct f2fs_sb_info *sbi = F2FS_SB(bvec->bv_page->mapping->host->i_sb); |
| 55 | |
| 56 | do { |
| 57 | struct page *page = bvec->bv_page; |
| 58 | |
| 59 | if (--bvec >= bio->bi_io_vec) |
| 60 | prefetchw(&bvec->bv_page->flags); |
| 61 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 62 | if (unlikely(!uptodate)) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 63 | SetPageError(page); |
| 64 | set_bit(AS_EIO, &page->mapping->flags); |
| 65 | set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG); |
| 66 | sbi->sb->s_flags |= MS_RDONLY; |
| 67 | } |
| 68 | end_page_writeback(page); |
| 69 | dec_page_count(sbi, F2FS_WRITEBACK); |
| 70 | } while (bvec >= bio->bi_io_vec); |
| 71 | |
| 72 | if (bio->bi_private) |
| 73 | complete(bio->bi_private); |
| 74 | |
| 75 | if (!get_pages(sbi, F2FS_WRITEBACK) && |
| 76 | !list_empty(&sbi->cp_wait.task_list)) |
| 77 | wake_up(&sbi->cp_wait); |
| 78 | |
| 79 | bio_put(bio); |
| 80 | } |
| 81 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 82 | /* |
| 83 | * Low-level block read/write IO operations. |
| 84 | */ |
| 85 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 86 | int npages, bool is_read) |
| 87 | { |
| 88 | struct bio *bio; |
| 89 | |
| 90 | /* No failure on bio allocation */ |
| 91 | bio = bio_alloc(GFP_NOIO, npages); |
| 92 | |
| 93 | bio->bi_bdev = sbi->sb->s_bdev; |
| 94 | bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr); |
| 95 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
| 96 | |
| 97 | return bio; |
| 98 | } |
| 99 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 100 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 101 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 102 | struct f2fs_io_info *fio = &io->fio; |
| 103 | int rw; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 104 | |
| 105 | if (!io->bio) |
| 106 | return; |
| 107 | |
Gu Zheng | 7e8f230 | 2013-12-20 18:17:49 +0800 | [diff] [blame^] | 108 | rw = fio->rw; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 109 | |
| 110 | if (is_read_io(rw)) { |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 111 | trace_f2fs_submit_read_bio(io->sbi->sb, rw, |
| 112 | fio->type, io->bio); |
Jaegeuk Kim | 76130cc | 2013-12-11 14:29:39 +0900 | [diff] [blame] | 113 | submit_bio(rw, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 114 | } else { |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 115 | trace_f2fs_submit_write_bio(io->sbi->sb, rw, |
| 116 | fio->type, io->bio); |
| 117 | /* |
| 118 | * META_FLUSH is only from the checkpoint procedure, and we |
| 119 | * should wait this metadata bio for FS consistency. |
| 120 | */ |
| 121 | if (fio->type == META_FLUSH) { |
| 122 | DECLARE_COMPLETION_ONSTACK(wait); |
| 123 | io->bio->bi_private = &wait; |
| 124 | submit_bio(rw, io->bio); |
| 125 | wait_for_completion(&wait); |
| 126 | } else { |
| 127 | submit_bio(rw, io->bio); |
| 128 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 129 | } |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 130 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 131 | io->bio = NULL; |
| 132 | } |
| 133 | |
| 134 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 135 | enum page_type type, int rw) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 136 | { |
| 137 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 138 | struct f2fs_bio_info *io; |
| 139 | |
| 140 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; |
| 141 | |
| 142 | mutex_lock(&io->io_mutex); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 143 | |
| 144 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 145 | if (type >= META_FLUSH) { |
| 146 | io->fio.type = META_FLUSH; |
| 147 | io->fio.rw = WRITE_FLUSH_FUA; |
| 148 | } |
| 149 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 150 | mutex_unlock(&io->io_mutex); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Fill the locked page with data located in the block address. |
| 155 | * Return unlocked page. |
| 156 | */ |
| 157 | int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page, |
| 158 | block_t blk_addr, int rw) |
| 159 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 160 | struct bio *bio; |
| 161 | |
| 162 | trace_f2fs_submit_page_bio(page, blk_addr, rw); |
| 163 | |
| 164 | /* Allocate a new bio */ |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 165 | bio = __bio_alloc(sbi, blk_addr, 1, is_read_io(rw)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 166 | |
| 167 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
| 168 | bio_put(bio); |
| 169 | f2fs_put_page(page, 1); |
| 170 | return -EFAULT; |
| 171 | } |
| 172 | |
| 173 | submit_bio(rw, bio); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 178 | block_t blk_addr, struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 179 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 180 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 181 | struct f2fs_bio_info *io; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 182 | bool is_read = is_read_io(fio->rw); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 183 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 184 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 185 | |
| 186 | verify_block_addr(sbi, blk_addr); |
| 187 | |
| 188 | mutex_lock(&io->io_mutex); |
| 189 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 190 | if (!is_read) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 191 | inc_page_count(sbi, F2FS_WRITEBACK); |
| 192 | |
Fan Li | 63a0b7cb | 2013-12-09 16:09:00 +0800 | [diff] [blame] | 193 | if (io->bio && (io->last_block_in_bio != blk_addr - 1 || |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 194 | io->fio.rw != fio->rw)) |
| 195 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 196 | alloc_new: |
| 197 | if (io->bio == NULL) { |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 198 | int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi)); |
| 199 | |
| 200 | io->bio = __bio_alloc(sbi, blk_addr, bio_blocks, is_read); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 201 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) < |
| 205 | PAGE_CACHE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 206 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 207 | goto alloc_new; |
| 208 | } |
| 209 | |
| 210 | io->last_block_in_bio = blk_addr; |
| 211 | |
| 212 | mutex_unlock(&io->io_mutex); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 213 | trace_f2fs_submit_page_mbio(page, fio->rw, fio->type, blk_addr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 217 | * Lock ordering for the change of data block address: |
| 218 | * ->data_page |
| 219 | * ->node_page |
| 220 | * update block addresses in the node page |
| 221 | */ |
| 222 | static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr) |
| 223 | { |
| 224 | struct f2fs_node *rn; |
| 225 | __le32 *addr_array; |
| 226 | struct page *node_page = dn->node_page; |
| 227 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 228 | |
Jin Xu | a569469 | 2013-08-05 20:02:04 +0800 | [diff] [blame] | 229 | f2fs_wait_on_page_writeback(node_page, NODE, false); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 230 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 231 | rn = F2FS_NODE(node_page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 232 | |
| 233 | /* Get physical address of data block */ |
| 234 | addr_array = blkaddr_in_node(rn); |
| 235 | addr_array[ofs_in_node] = cpu_to_le32(new_addr); |
| 236 | set_page_dirty(node_page); |
| 237 | } |
| 238 | |
| 239 | int reserve_new_block(struct dnode_of_data *dn) |
| 240 | { |
| 241 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 242 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 243 | 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] | 244 | return -EPERM; |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 245 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 246 | return -ENOSPC; |
| 247 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 248 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 249 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 250 | __set_data_blkaddr(dn, NEW_ADDR); |
| 251 | dn->data_blkaddr = NEW_ADDR; |
| 252 | sync_inode_page(dn); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 256 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 257 | { |
| 258 | bool need_put = dn->inode_page ? false : true; |
| 259 | int err; |
| 260 | |
| 261 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 262 | if (err) |
| 263 | return err; |
| 264 | if (dn->data_blkaddr == NULL_ADDR) |
| 265 | err = reserve_new_block(dn); |
| 266 | |
| 267 | if (need_put) |
| 268 | f2fs_put_dnode(dn); |
| 269 | return err; |
| 270 | } |
| 271 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 272 | static int check_extent_cache(struct inode *inode, pgoff_t pgofs, |
| 273 | struct buffer_head *bh_result) |
| 274 | { |
| 275 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 276 | pgoff_t start_fofs, end_fofs; |
| 277 | block_t start_blkaddr; |
| 278 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 279 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 280 | return 0; |
| 281 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 282 | read_lock(&fi->ext.ext_lock); |
| 283 | if (fi->ext.len == 0) { |
| 284 | read_unlock(&fi->ext.ext_lock); |
| 285 | return 0; |
| 286 | } |
| 287 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 288 | stat_inc_total_hit(inode->i_sb); |
| 289 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 290 | start_fofs = fi->ext.fofs; |
| 291 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 292 | start_blkaddr = fi->ext.blk_addr; |
| 293 | |
| 294 | if (pgofs >= start_fofs && pgofs <= end_fofs) { |
| 295 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 296 | size_t count; |
| 297 | |
| 298 | clear_buffer_new(bh_result); |
| 299 | map_bh(bh_result, inode->i_sb, |
| 300 | start_blkaddr + pgofs - start_fofs); |
| 301 | count = end_fofs - pgofs + 1; |
| 302 | if (count < (UINT_MAX >> blkbits)) |
| 303 | bh_result->b_size = (count << blkbits); |
| 304 | else |
| 305 | bh_result->b_size = UINT_MAX; |
| 306 | |
Jaegeuk Kim | dcdfff6 | 2013-10-22 20:56:10 +0900 | [diff] [blame] | 307 | stat_inc_read_hit(inode->i_sb); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 308 | read_unlock(&fi->ext.ext_lock); |
| 309 | return 1; |
| 310 | } |
| 311 | read_unlock(&fi->ext.ext_lock); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn) |
| 316 | { |
| 317 | struct f2fs_inode_info *fi = F2FS_I(dn->inode); |
| 318 | pgoff_t fofs, start_fofs, end_fofs; |
| 319 | block_t start_blkaddr, end_blkaddr; |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 320 | int need_update = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 321 | |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 322 | f2fs_bug_on(blk_addr == NEW_ADDR); |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 323 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) + |
| 324 | dn->ofs_in_node; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 325 | |
| 326 | /* Update the page address in the parent node */ |
| 327 | __set_data_blkaddr(dn, blk_addr); |
| 328 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 329 | if (is_inode_flag_set(fi, FI_NO_EXTENT)) |
| 330 | return; |
| 331 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 332 | write_lock(&fi->ext.ext_lock); |
| 333 | |
| 334 | start_fofs = fi->ext.fofs; |
| 335 | end_fofs = fi->ext.fofs + fi->ext.len - 1; |
| 336 | start_blkaddr = fi->ext.blk_addr; |
| 337 | end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1; |
| 338 | |
| 339 | /* Drop and initialize the matched extent */ |
| 340 | if (fi->ext.len == 1 && fofs == start_fofs) |
| 341 | fi->ext.len = 0; |
| 342 | |
| 343 | /* Initial extent */ |
| 344 | if (fi->ext.len == 0) { |
| 345 | if (blk_addr != NULL_ADDR) { |
| 346 | fi->ext.fofs = fofs; |
| 347 | fi->ext.blk_addr = blk_addr; |
| 348 | fi->ext.len = 1; |
| 349 | } |
| 350 | goto end_update; |
| 351 | } |
| 352 | |
Namjae Jeon | 6224da8 | 2013-04-06 14:44:32 +0900 | [diff] [blame] | 353 | /* Front merge */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 354 | if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) { |
| 355 | fi->ext.fofs--; |
| 356 | fi->ext.blk_addr--; |
| 357 | fi->ext.len++; |
| 358 | goto end_update; |
| 359 | } |
| 360 | |
| 361 | /* Back merge */ |
| 362 | if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) { |
| 363 | fi->ext.len++; |
| 364 | goto end_update; |
| 365 | } |
| 366 | |
| 367 | /* Split the existing extent */ |
| 368 | if (fi->ext.len > 1 && |
| 369 | fofs >= start_fofs && fofs <= end_fofs) { |
| 370 | if ((end_fofs - fofs) < (fi->ext.len >> 1)) { |
| 371 | fi->ext.len = fofs - start_fofs; |
| 372 | } else { |
| 373 | fi->ext.fofs = fofs + 1; |
| 374 | fi->ext.blk_addr = start_blkaddr + |
| 375 | fofs - start_fofs + 1; |
| 376 | fi->ext.len -= fofs - start_fofs + 1; |
| 377 | } |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 378 | } else { |
| 379 | need_update = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 380 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 381 | |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 382 | /* Finally, if the extent is very fragmented, let's drop the cache. */ |
| 383 | if (fi->ext.len < F2FS_MIN_EXTENT_LEN) { |
| 384 | fi->ext.len = 0; |
| 385 | set_inode_flag(fi, FI_NO_EXTENT); |
| 386 | need_update = true; |
| 387 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 388 | end_update: |
| 389 | write_unlock(&fi->ext.ext_lock); |
Jaegeuk Kim | c11abd1 | 2013-11-19 10:41:54 +0900 | [diff] [blame] | 390 | if (need_update) |
| 391 | sync_inode_page(dn); |
| 392 | return; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 395 | 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] | 396 | { |
| 397 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 398 | struct address_space *mapping = inode->i_mapping; |
| 399 | struct dnode_of_data dn; |
| 400 | struct page *page; |
| 401 | int err; |
| 402 | |
| 403 | page = find_get_page(mapping, index); |
| 404 | if (page && PageUptodate(page)) |
| 405 | return page; |
| 406 | f2fs_put_page(page, 0); |
| 407 | |
| 408 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 409 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 410 | if (err) |
| 411 | return ERR_PTR(err); |
| 412 | f2fs_put_dnode(&dn); |
| 413 | |
| 414 | if (dn.data_blkaddr == NULL_ADDR) |
| 415 | return ERR_PTR(-ENOENT); |
| 416 | |
| 417 | /* By fallocate(), there is no cached page, but with NEW_ADDR */ |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 418 | if (unlikely(dn.data_blkaddr == NEW_ADDR)) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 419 | return ERR_PTR(-EINVAL); |
| 420 | |
Jaegeuk Kim | 6f85b35 | 2013-05-20 16:15:22 +0900 | [diff] [blame] | 421 | page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 422 | if (!page) |
| 423 | return ERR_PTR(-ENOMEM); |
| 424 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 425 | if (PageUptodate(page)) { |
| 426 | unlock_page(page); |
| 427 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 428 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 429 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 430 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 431 | sync ? READ_SYNC : READA); |
Chao Yu | 1069bbf | 2013-11-28 15:43:43 +0800 | [diff] [blame] | 432 | if (err) |
| 433 | return ERR_PTR(err); |
| 434 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 435 | if (sync) { |
| 436 | wait_on_page_locked(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 437 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 438 | f2fs_put_page(page, 0); |
| 439 | return ERR_PTR(-EIO); |
| 440 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 441 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 442 | return page; |
| 443 | } |
| 444 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 445 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 446 | * If it tries to access a hole, return an error. |
| 447 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 448 | * whether this page exists or not. |
| 449 | */ |
| 450 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index) |
| 451 | { |
| 452 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 453 | struct address_space *mapping = inode->i_mapping; |
| 454 | struct dnode_of_data dn; |
| 455 | struct page *page; |
| 456 | int err; |
| 457 | |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 458 | repeat: |
Jaegeuk Kim | 6f85b35 | 2013-05-20 16:15:22 +0900 | [diff] [blame] | 459 | page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 460 | if (!page) |
| 461 | return ERR_PTR(-ENOMEM); |
| 462 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 463 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 464 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 465 | if (err) { |
| 466 | f2fs_put_page(page, 1); |
| 467 | return ERR_PTR(err); |
| 468 | } |
| 469 | f2fs_put_dnode(&dn); |
| 470 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 471 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 472 | f2fs_put_page(page, 1); |
| 473 | return ERR_PTR(-ENOENT); |
| 474 | } |
| 475 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 476 | if (PageUptodate(page)) |
| 477 | return page; |
| 478 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 479 | /* |
| 480 | * A new dentry page is allocated but not able to be written, since its |
| 481 | * new inode page couldn't be allocated due to -ENOSPC. |
| 482 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 483 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 484 | */ |
| 485 | if (dn.data_blkaddr == NEW_ADDR) { |
| 486 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 487 | SetPageUptodate(page); |
| 488 | return page; |
| 489 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 490 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 491 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 492 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 493 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 494 | |
| 495 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 496 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 497 | f2fs_put_page(page, 1); |
| 498 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 499 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 500 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 501 | f2fs_put_page(page, 1); |
| 502 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 503 | } |
| 504 | return page; |
| 505 | } |
| 506 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 507 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 508 | * Caller ensures that this data page is never allocated. |
| 509 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 510 | * |
| 511 | * Also, caller should grab and release a mutex by calling mutex_lock_op() and |
| 512 | * mutex_unlock_op(). |
Jaegeuk Kim | 44a83ff | 2013-05-20 10:10:29 +0900 | [diff] [blame] | 513 | * Note that, npage is set only by make_empty_dir. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 514 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 515 | struct page *get_new_data_page(struct inode *inode, |
| 516 | struct page *npage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 517 | { |
| 518 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 519 | struct address_space *mapping = inode->i_mapping; |
| 520 | struct page *page; |
| 521 | struct dnode_of_data dn; |
| 522 | int err; |
| 523 | |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 524 | set_new_dnode(&dn, inode, npage, npage, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 525 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 526 | if (err) |
| 527 | return ERR_PTR(err); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 528 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 529 | page = grab_cache_page(mapping, index); |
| 530 | if (!page) |
| 531 | return ERR_PTR(-ENOMEM); |
| 532 | |
| 533 | if (PageUptodate(page)) |
| 534 | return page; |
| 535 | |
| 536 | if (dn.data_blkaddr == NEW_ADDR) { |
| 537 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 538 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 539 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 540 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, |
| 541 | READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 542 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 543 | return ERR_PTR(err); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 544 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 545 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 546 | f2fs_put_page(page, 1); |
| 547 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 548 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 549 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 550 | f2fs_put_page(page, 1); |
| 551 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 552 | } |
| 553 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 554 | |
| 555 | if (new_i_size && |
| 556 | i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) { |
| 557 | i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 558 | /* Only the directory inode sets new_i_size */ |
| 559 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 560 | mark_inode_dirty_sync(inode); |
| 561 | } |
| 562 | return page; |
| 563 | } |
| 564 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 565 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 566 | { |
| 567 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 568 | struct f2fs_summary sum; |
| 569 | block_t new_blkaddr; |
| 570 | struct node_info ni; |
| 571 | int type; |
| 572 | |
| 573 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 574 | return -EPERM; |
| 575 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 576 | return -ENOSPC; |
| 577 | |
| 578 | __set_data_blkaddr(dn, NEW_ADDR); |
| 579 | dn->data_blkaddr = NEW_ADDR; |
| 580 | |
| 581 | get_node_info(sbi, dn->nid, &ni); |
| 582 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 583 | |
| 584 | type = CURSEG_WARM_DATA; |
| 585 | |
| 586 | allocate_data_block(sbi, NULL, NULL_ADDR, &new_blkaddr, &sum, type); |
| 587 | |
| 588 | /* direct IO doesn't use extent cache to maximize the performance */ |
| 589 | set_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT); |
| 590 | update_extent_cache(new_blkaddr, dn); |
| 591 | clear_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT); |
| 592 | |
| 593 | dn->data_blkaddr = new_blkaddr; |
| 594 | return 0; |
| 595 | } |
| 596 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 597 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 598 | * This function should be used by the data read flow only where it |
| 599 | * does not check the "create" flag that indicates block allocation. |
| 600 | * The reason for this special functionality is to exploit VFS readahead |
| 601 | * mechanism. |
| 602 | */ |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 603 | static int get_data_block(struct inode *inode, sector_t iblock, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 604 | struct buffer_head *bh_result, int create) |
| 605 | { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 606 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 607 | unsigned int blkbits = inode->i_sb->s_blocksize_bits; |
| 608 | unsigned maxblocks = bh_result->b_size >> blkbits; |
| 609 | struct dnode_of_data dn; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 610 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 611 | pgoff_t pgofs, end_offset; |
| 612 | int err = 0, ofs = 1; |
| 613 | bool allocated = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 614 | |
| 615 | /* Get the page offset from the block offset(iblock) */ |
| 616 | pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits)); |
| 617 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 618 | if (check_extent_cache(inode, pgofs, bh_result)) |
| 619 | goto out; |
| 620 | |
| 621 | if (create) |
| 622 | f2fs_lock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 623 | |
| 624 | /* When reading holes, we need its node page */ |
| 625 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 626 | err = get_dnode_of_data(&dn, pgofs, mode); |
| 627 | if (err || dn.data_blkaddr == NEW_ADDR) { |
| 628 | if (err == -ENOENT) |
| 629 | err = 0; |
| 630 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 631 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 632 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 633 | if (dn.data_blkaddr != NULL_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 634 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 635 | } else if (create) { |
| 636 | err = __allocate_data_block(&dn); |
| 637 | if (err) |
| 638 | goto put_out; |
| 639 | allocated = true; |
| 640 | map_bh(bh_result, inode->i_sb, dn.data_blkaddr); |
| 641 | } else { |
| 642 | goto put_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 643 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 644 | |
| 645 | end_offset = IS_INODE(dn.node_page) ? |
| 646 | ADDRS_PER_INODE(F2FS_I(inode)) : ADDRS_PER_BLOCK; |
| 647 | bh_result->b_size = (((size_t)1) << blkbits); |
| 648 | dn.ofs_in_node++; |
| 649 | pgofs++; |
| 650 | |
| 651 | get_next: |
| 652 | if (dn.ofs_in_node >= end_offset) { |
| 653 | if (allocated) |
| 654 | sync_inode_page(&dn); |
| 655 | allocated = false; |
| 656 | f2fs_put_dnode(&dn); |
| 657 | |
| 658 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 659 | err = get_dnode_of_data(&dn, pgofs, mode); |
| 660 | if (err || dn.data_blkaddr == NEW_ADDR) { |
| 661 | if (err == -ENOENT) |
| 662 | err = 0; |
| 663 | goto unlock_out; |
| 664 | } |
| 665 | end_offset = IS_INODE(dn.node_page) ? |
| 666 | ADDRS_PER_INODE(F2FS_I(inode)) : ADDRS_PER_BLOCK; |
| 667 | } |
| 668 | |
| 669 | if (maxblocks > (bh_result->b_size >> blkbits)) { |
| 670 | block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 671 | if (blkaddr == NULL_ADDR && create) { |
| 672 | err = __allocate_data_block(&dn); |
| 673 | if (err) |
| 674 | goto sync_out; |
| 675 | allocated = true; |
| 676 | blkaddr = dn.data_blkaddr; |
| 677 | } |
| 678 | /* Give more consecutive addresses for the read ahead */ |
| 679 | if (blkaddr == (bh_result->b_blocknr + ofs)) { |
| 680 | ofs++; |
| 681 | dn.ofs_in_node++; |
| 682 | pgofs++; |
| 683 | bh_result->b_size += (((size_t)1) << blkbits); |
| 684 | goto get_next; |
| 685 | } |
| 686 | } |
| 687 | sync_out: |
| 688 | if (allocated) |
| 689 | sync_inode_page(&dn); |
| 690 | put_out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 691 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 692 | unlock_out: |
| 693 | if (create) |
| 694 | f2fs_unlock_op(sbi); |
| 695 | out: |
| 696 | trace_f2fs_get_data_block(inode, iblock, bh_result, err); |
| 697 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 701 | { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 702 | return mpage_readpage(page, get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static int f2fs_read_data_pages(struct file *file, |
| 706 | struct address_space *mapping, |
| 707 | struct list_head *pages, unsigned nr_pages) |
| 708 | { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 709 | return mpage_readpages(mapping, pages, nr_pages, get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 710 | } |
| 711 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 712 | 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] | 713 | { |
| 714 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 715 | block_t old_blkaddr, new_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 716 | struct dnode_of_data dn; |
| 717 | int err = 0; |
| 718 | |
| 719 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 720 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 721 | if (err) |
| 722 | return err; |
| 723 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 724 | old_blkaddr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 725 | |
| 726 | /* This page is already truncated */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 727 | if (old_blkaddr == NULL_ADDR) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 728 | goto out_writepage; |
| 729 | |
| 730 | set_page_writeback(page); |
| 731 | |
| 732 | /* |
| 733 | * If current allocation needs SSR, |
| 734 | * it had better in-place writes for updated data. |
| 735 | */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 736 | if (unlikely(old_blkaddr != NEW_ADDR && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 737 | !is_cold_data(page) && |
| 738 | need_inplace_update(inode))) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 739 | rewrite_data_page(page, old_blkaddr, fio); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 740 | } else { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 741 | write_data_page(page, &dn, &new_blkaddr, fio); |
| 742 | update_extent_cache(new_blkaddr, &dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 743 | } |
| 744 | out_writepage: |
| 745 | f2fs_put_dnode(&dn); |
| 746 | return err; |
| 747 | } |
| 748 | |
| 749 | static int f2fs_write_data_page(struct page *page, |
| 750 | struct writeback_control *wbc) |
| 751 | { |
| 752 | struct inode *inode = page->mapping->host; |
| 753 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 754 | loff_t i_size = i_size_read(inode); |
| 755 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 756 | >> PAGE_CACHE_SHIFT; |
| 757 | unsigned offset; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 758 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 759 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 760 | struct f2fs_io_info fio = { |
| 761 | .type = DATA, |
| 762 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC: WRITE, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 763 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 764 | |
| 765 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 766 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * If the offset is out-of-range of file size, |
| 770 | * this page does not have to be written to disk. |
| 771 | */ |
| 772 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
| 773 | if ((page->index >= end_index + 1) || !offset) { |
| 774 | if (S_ISDIR(inode->i_mode)) { |
| 775 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 776 | inode_dec_dirty_dents(inode); |
| 777 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 778 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 782 | write: |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 783 | if (unlikely(sbi->por_doing)) { |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 784 | err = AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 785 | goto redirty_out; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 786 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 787 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 788 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 789 | if (S_ISDIR(inode->i_mode)) { |
| 790 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 791 | inode_dec_dirty_dents(inode); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 792 | err = do_write_data_page(page, &fio); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 793 | } else { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 794 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 795 | err = do_write_data_page(page, &fio); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 796 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 797 | need_balance_fs = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 798 | } |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 799 | if (err == -ENOENT) |
| 800 | goto out; |
| 801 | else if (err) |
| 802 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 803 | |
| 804 | if (wbc->for_reclaim) |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 805 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 806 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 807 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 808 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 809 | unlock_page(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 810 | if (need_balance_fs) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 811 | f2fs_balance_fs(sbi); |
| 812 | return 0; |
| 813 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 814 | redirty_out: |
| 815 | wbc->pages_skipped++; |
| 816 | set_page_dirty(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 817 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | #define MAX_DESIRED_PAGES_WP 4096 |
| 821 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 822 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 823 | void *data) |
| 824 | { |
| 825 | struct address_space *mapping = data; |
| 826 | int ret = mapping->a_ops->writepage(page, wbc); |
| 827 | mapping_set_error(mapping, ret); |
| 828 | return ret; |
| 829 | } |
| 830 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 831 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 832 | struct writeback_control *wbc) |
| 833 | { |
| 834 | struct inode *inode = mapping->host; |
| 835 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 836 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 837 | int ret; |
| 838 | long excess_nrtw = 0, desired_nrtw; |
| 839 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 840 | /* deal with chardevs and other special file */ |
| 841 | if (!mapping->a_ops->writepage) |
| 842 | return 0; |
| 843 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 844 | if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) { |
| 845 | desired_nrtw = MAX_DESIRED_PAGES_WP; |
| 846 | excess_nrtw = desired_nrtw - wbc->nr_to_write; |
| 847 | wbc->nr_to_write = desired_nrtw; |
| 848 | } |
| 849 | |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 850 | if (!S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 851 | mutex_lock(&sbi->writepages); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 852 | locked = true; |
| 853 | } |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 854 | ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Jaegeuk Kim | 531ad7d | 2013-04-30 11:33:27 +0900 | [diff] [blame] | 855 | if (locked) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 856 | mutex_unlock(&sbi->writepages); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 857 | |
| 858 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 859 | |
| 860 | remove_dirty_dir_inode(inode); |
| 861 | |
| 862 | wbc->nr_to_write -= excess_nrtw; |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 867 | loff_t pos, unsigned len, unsigned flags, |
| 868 | struct page **pagep, void **fsdata) |
| 869 | { |
| 870 | struct inode *inode = mapping->host; |
| 871 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 872 | struct page *page; |
| 873 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
| 874 | struct dnode_of_data dn; |
| 875 | int err = 0; |
| 876 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 877 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 878 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 879 | page = grab_cache_page_write_begin(mapping, index, flags); |
| 880 | if (!page) |
| 881 | return -ENOMEM; |
| 882 | *pagep = page; |
| 883 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 884 | f2fs_lock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 885 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 886 | err = f2fs_reserve_block(&dn, index); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 887 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 888 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 889 | if (err) { |
| 890 | f2fs_put_page(page, 1); |
| 891 | return err; |
| 892 | } |
| 893 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 894 | if ((len == PAGE_CACHE_SIZE) || PageUptodate(page)) |
| 895 | return 0; |
| 896 | |
| 897 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 898 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 899 | unsigned end = start + len; |
| 900 | |
| 901 | /* Reading beyond i_size is simple: memset to zero */ |
| 902 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 903 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | if (dn.data_blkaddr == NEW_ADDR) { |
| 907 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 908 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 909 | err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, |
| 910 | READ_SYNC); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 911 | if (err) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 912 | return err; |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 913 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 914 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 915 | f2fs_put_page(page, 1); |
| 916 | return -EIO; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 917 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 918 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 919 | f2fs_put_page(page, 1); |
| 920 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 921 | } |
| 922 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 923 | out: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 924 | SetPageUptodate(page); |
| 925 | clear_cold_data(page); |
| 926 | return 0; |
| 927 | } |
| 928 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 929 | static int f2fs_write_end(struct file *file, |
| 930 | struct address_space *mapping, |
| 931 | loff_t pos, unsigned len, unsigned copied, |
| 932 | struct page *page, void *fsdata) |
| 933 | { |
| 934 | struct inode *inode = page->mapping->host; |
| 935 | |
| 936 | SetPageUptodate(page); |
| 937 | set_page_dirty(page); |
| 938 | |
| 939 | if (pos + copied > i_size_read(inode)) { |
| 940 | i_size_write(inode, pos + copied); |
| 941 | mark_inode_dirty(inode); |
| 942 | update_inode_page(inode); |
| 943 | } |
| 944 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 945 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 946 | return copied; |
| 947 | } |
| 948 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 949 | static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb, |
| 950 | const struct iovec *iov, loff_t offset, unsigned long nr_segs) |
| 951 | { |
| 952 | struct file *file = iocb->ki_filp; |
| 953 | struct inode *inode = file->f_mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 954 | return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs, |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 955 | get_data_block); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 956 | } |
| 957 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 958 | static void f2fs_invalidate_data_page(struct page *page, unsigned int offset, |
| 959 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 960 | { |
| 961 | struct inode *inode = page->mapping->host; |
| 962 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 963 | if (S_ISDIR(inode->i_mode) && PageDirty(page)) { |
| 964 | dec_page_count(sbi, F2FS_DIRTY_DENTS); |
| 965 | inode_dec_dirty_dents(inode); |
| 966 | } |
| 967 | ClearPagePrivate(page); |
| 968 | } |
| 969 | |
| 970 | static int f2fs_release_data_page(struct page *page, gfp_t wait) |
| 971 | { |
| 972 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 973 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static int f2fs_set_data_page_dirty(struct page *page) |
| 977 | { |
| 978 | struct address_space *mapping = page->mapping; |
| 979 | struct inode *inode = mapping->host; |
| 980 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 981 | trace_f2fs_set_page_dirty(page, DATA); |
| 982 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 983 | SetPageUptodate(page); |
| 984 | if (!PageDirty(page)) { |
| 985 | __set_page_dirty_nobuffers(page); |
| 986 | set_dirty_dir_page(inode, page); |
| 987 | return 1; |
| 988 | } |
| 989 | return 0; |
| 990 | } |
| 991 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 992 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 993 | { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 994 | return generic_block_bmap(mapping, block, get_data_block); |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 995 | } |
| 996 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 997 | const struct address_space_operations f2fs_dblock_aops = { |
| 998 | .readpage = f2fs_read_data_page, |
| 999 | .readpages = f2fs_read_data_pages, |
| 1000 | .writepage = f2fs_write_data_page, |
| 1001 | .writepages = f2fs_write_data_pages, |
| 1002 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1003 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1004 | .set_page_dirty = f2fs_set_data_page_dirty, |
| 1005 | .invalidatepage = f2fs_invalidate_data_page, |
| 1006 | .releasepage = f2fs_release_data_page, |
| 1007 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1008 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1009 | }; |