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