blob: 82ecaa30fd772411e226de3780c984b678faed9c [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * 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 Yu8f46dca2015-07-14 18:56:10 +080017#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090018#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080021#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070022#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020030static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031{
Linus Torvaldsf5688492014-01-30 11:19:05 -080032 struct bio_vec *bvec;
33 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090034
Chao Yu12377022015-05-25 18:03:38 +080035 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020036 if (bio->bi_error) {
Chao Yu12377022015-05-25 18:03:38 +080037 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 Torvaldsf5688492014-01-30 11:19:05 -080044 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090045 struct page *page = bvec->bv_page;
46
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020047 if (!bio->bi_error) {
Linus Torvaldsf5688492014-01-30 11:19:05 -080048 SetPageUptodate(page);
49 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090050 ClearPageUptodate(page);
51 SetPageError(page);
52 }
53 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080054 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090055 bio_put(bio);
56}
57
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020058static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090059{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090060 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080061 struct bio_vec *bvec;
62 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063
Linus Torvaldsf5688492014-01-30 11:19:05 -080064 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090065 struct page *page = bvec->bv_page;
66
Jaegeuk Kim4375a332015-04-23 12:04:33 -070067 f2fs_restore_and_release_control_page(&page);
68
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020069 if (unlikely(bio->bi_error)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070070 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090071 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090072 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090073 }
74 end_page_writeback(page);
75 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080076 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090077
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 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 Zheng940a6d32013-12-20 17:39:59 +080085/*
86 * Low-level block read/write IO operations.
87 */
88static 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 Kim740432f2015-08-14 11:43:56 -070093 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080094
95 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080096 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080097 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080098 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080099
100 return bio;
101}
102
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900103static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900105 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900106
107 if (!io->bio)
108 return;
109
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700110 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800111 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700112 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800113 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700115 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900116 io->bio = NULL;
117}
118
119void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900120 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900121{
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 Yudf0f8dc2014-03-22 14:57:23 +0800127 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900128
129 /* change META to META_FLUSH in the checkpoint procedure */
130 if (type >= META_FLUSH) {
131 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700132 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 Kim458e6192013-12-11 13:54:01 +0900136 }
137 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800138 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900139}
140
141/*
142 * Fill the locked page with data located in the block address.
143 * Return unlocked page.
144 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700145int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900146{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900147 struct bio *bio;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700148 struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900149
Chao Yu2ace38e2014-12-24 16:08:14 +0800150 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700151 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900152
153 /* Allocate a new bio */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700154 bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900155
156 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
157 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900158 return -EFAULT;
159 }
160
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800161 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900162 return 0;
163}
164
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700165void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900166{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700167 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900168 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900169 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800170 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700171 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900172
Gu Zheng940a6d32013-12-20 17:39:59 +0800173 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900174
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800175 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900176
Chao Yudf0f8dc2014-03-22 14:57:23 +0800177 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900178
Gu Zheng940a6d32013-12-20 17:39:59 +0800179 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900180 inc_page_count(sbi, F2FS_WRITEBACK);
181
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800182 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183 io->fio.rw != fio->rw))
184 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185alloc_new:
186 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700187 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800188
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800189 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900190 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900191 }
192
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700193 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 Kim93dfe2a2013-11-30 12:51:14 +0900196 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900197 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900198 goto alloc_new;
199 }
200
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800201 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700202 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900203
Chao Yudf0f8dc2014-03-22 14:57:23 +0800204 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700205 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900206}
207
208/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900209 * 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 Yu216a6202015-03-19 19:23:32 +0800214void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900215{
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 Zhong5514f0a2014-01-10 07:26:14 +0000221 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900222
Gu Zheng45590712013-07-15 17:57:38 +0800223 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900224
225 /* Get physical address of data block */
226 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800227 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900228 set_page_dirty(node_page);
Jaegeuk Kim93bae092015-12-22 12:59:54 -0800229 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900230}
231
232int reserve_new_block(struct dnode_of_data *dn)
233{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700234 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900235
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900236 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900237 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800238 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900239 return -ENOSPC;
240
Namjae Jeonc01e2852013-04-23 17:00:52 +0900241 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
242
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900243 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800244 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900245 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900246 sync_inode_page(dn);
247 return 0;
248}
249
Huajun Lib6009652013-11-10 23:13:18 +0800250int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
251{
252 bool need_put = dn->inode_page ? false : true;
253 int err;
254
255 err = get_dnode_of_data(dn, index, ALLOC_NODE);
256 if (err)
257 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900258
Huajun Lib6009652013-11-10 23:13:18 +0800259 if (dn->data_blkaddr == NULL_ADDR)
260 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900261 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800262 f2fs_put_dnode(dn);
263 return err;
264}
265
Fan Li759af1c2015-08-05 15:52:16 +0800266int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900267{
Chao Yu028a41e2015-03-19 19:26:02 +0800268 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800269 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800270
Fan Li759af1c2015-08-05 15:52:16 +0800271 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
272 dn->data_blkaddr = ei.blk + index - ei.fofs;
273 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800274 }
275
Fan Li759af1c2015-08-05 15:52:16 +0800276 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900277}
278
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700279struct page *get_read_data_page(struct inode *inode, pgoff_t index,
280 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900281{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900282 struct address_space *mapping = inode->i_mapping;
283 struct dnode_of_data dn;
284 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800285 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900286 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800287 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700288 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800289 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700290 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700291 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800292 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900293
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700294 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
295 return read_mapping_page(mapping, index, NULL);
296
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700297 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900298 if (!page)
299 return ERR_PTR(-ENOMEM);
300
Chao Yucb3bc9e2015-02-05 18:03:40 +0800301 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
302 dn.data_blkaddr = ei.blk + index - ei.fofs;
303 goto got_it;
304 }
305
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900306 set_new_dnode(&dn, inode, NULL, NULL, 0);
307 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700308 if (err)
309 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900310 f2fs_put_dnode(&dn);
311
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900312 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700313 err = -ENOENT;
314 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900315 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800316got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700317 if (PageUptodate(page)) {
318 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900319 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700320 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900321
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900322 /*
323 * A new dentry page is allocated but not able to be written, since its
324 * new inode page couldn't be allocated due to -ENOSPC.
325 * In such the case, its blkaddr can be remained as NEW_ADDR.
326 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
327 */
328 if (dn.data_blkaddr == NEW_ADDR) {
329 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
330 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700331 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900332 return page;
333 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900334
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800335 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700336 fio.page = page;
337 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900338 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700339 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700340 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700341
342put_err:
343 f2fs_put_page(page, 1);
344 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700345}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900346
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700347struct page *find_data_page(struct inode *inode, pgoff_t index)
348{
349 struct address_space *mapping = inode->i_mapping;
350 struct page *page;
351
352 page = find_get_page(mapping, index);
353 if (page && PageUptodate(page))
354 return page;
355 f2fs_put_page(page, 0);
356
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700357 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700358 if (IS_ERR(page))
359 return page;
360
361 if (PageUptodate(page))
362 return page;
363
364 wait_on_page_locked(page);
365 if (unlikely(!PageUptodate(page))) {
366 f2fs_put_page(page, 0);
367 return ERR_PTR(-EIO);
368 }
369 return page;
370}
371
372/*
373 * If it tries to access a hole, return an error.
374 * Because, the callers, functions in dir.c and GC, should be able to know
375 * whether this page exists or not.
376 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700377struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
378 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700379{
380 struct address_space *mapping = inode->i_mapping;
381 struct page *page;
382repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700383 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700384 if (IS_ERR(page))
385 return page;
386
387 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900388 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900389 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900390 f2fs_put_page(page, 1);
391 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900393 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900394 f2fs_put_page(page, 1);
395 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 }
397 return page;
398}
399
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900400/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900401 * Caller ensures that this data page is never allocated.
402 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900403 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800404 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
405 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800406 * Note that, ipage is set only by make_empty_dir, and if any error occur,
407 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900408 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900409struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900410 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900411{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900412 struct address_space *mapping = inode->i_mapping;
413 struct page *page;
414 struct dnode_of_data dn;
415 int err;
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700416repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700417 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800418 if (!page) {
419 /*
420 * before exiting, we should make sure ipage will be released
421 * if any error occur.
422 */
423 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700424 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800425 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900426
Jaegeuk Kima8865372013-12-27 17:04:17 +0900427 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800428 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700429 if (err) {
430 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900431 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900432 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700433 if (!ipage)
434 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900435
436 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700437 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900438
439 if (dn.data_blkaddr == NEW_ADDR) {
440 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900441 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900442 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700443 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900444
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700445 page = get_read_data_page(inode, index, READ_SYNC, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700446 if (IS_ERR(page))
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900447 goto repeat;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700448
449 /* wait for read completion */
450 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900451 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700452got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800453 if (new_i_size && i_size_read(inode) <
454 ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) {
455 i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900456 /* Only the directory inode sets new_i_size */
457 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900458 }
459 return page;
460}
461
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900462static int __allocate_data_block(struct dnode_of_data *dn)
463{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700464 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700465 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900466 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900467 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800468 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700469 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900470
471 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
472 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800473
474 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
475 if (dn->data_blkaddr == NEW_ADDR)
476 goto alloc;
477
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900478 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
479 return -ENOSPC;
480
Chao Yudf6136e2015-03-23 10:33:37 +0800481alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900482 get_node_info(sbi, dn->nid, &ni);
483 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
484
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800485 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
486 seg = CURSEG_DIRECT_IO;
487
Chao Yudf6136e2015-03-23 10:33:37 +0800488 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
489 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800490 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900491
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700492 /* update i_size */
493 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
494 dn->ofs_in_node;
Chao Yu9edcdab2015-09-11 14:43:52 +0800495 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT))
496 i_size_write(dn->inode,
497 ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900498 return 0;
499}
500
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800501static void __allocate_data_blocks(struct inode *inode, loff_t offset,
502 size_t count)
503{
504 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
505 struct dnode_of_data dn;
506 u64 start = F2FS_BYTES_TO_BLK(offset);
507 u64 len = F2FS_BYTES_TO_BLK(count);
508 bool allocated;
509 u64 end_offset;
510
511 while (len) {
512 f2fs_balance_fs(sbi);
513 f2fs_lock_op(sbi);
514
515 /* When reading holes, we need its node page */
516 set_new_dnode(&dn, inode, NULL, NULL, 0);
517 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
518 goto out;
519
520 allocated = false;
521 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
522
523 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +0800524 block_t blkaddr;
525
Chao Yuf9811702015-09-21 20:17:52 +0800526 if (unlikely(f2fs_cp_error(sbi)))
527 goto sync_out;
528
Chao Yud6d4f1c2015-03-12 17:04:24 +0800529 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800530 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800531 if (__allocate_data_block(&dn))
532 goto sync_out;
533 allocated = true;
534 }
535 len--;
536 start++;
537 dn.ofs_in_node++;
538 }
539
540 if (allocated)
541 sync_inode_page(&dn);
542
543 f2fs_put_dnode(&dn);
544 f2fs_unlock_op(sbi);
545 }
546 return;
547
548sync_out:
549 if (allocated)
550 sync_inode_page(&dn);
551 f2fs_put_dnode(&dn);
552out:
553 f2fs_unlock_op(sbi);
554 return;
555}
556
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900557/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700558 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
559 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800560 * If original data blocks are allocated, then give them to blockdev.
561 * Otherwise,
562 * a. preallocate requested block addresses
563 * b. do not use extent cache for better performance
564 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565 */
Chao Yud323d002015-10-27 09:53:45 +0800566int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800567 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700569 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900570 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800571 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900572 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
573 pgoff_t pgofs, end_offset;
574 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800575 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800577 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900578
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700579 map->m_len = 0;
580 map->m_flags = 0;
581
582 /* it only supports block size == page size */
583 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900584
Chao Yu7e4dde72015-02-05 17:51:34 +0800585 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700586 map->m_pblk = ei.blk + pgofs - ei.fofs;
587 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
588 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900589 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800590 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900591
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800592 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800593 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900594
595 /* When reading holes, we need its node page */
596 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900597 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900598 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900599 if (err == -ENOENT)
600 err = 0;
601 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900602 }
Chao Yu973163f2015-09-18 16:51:51 +0800603
604 if (dn.data_blkaddr == NEW_ADDR || dn.data_blkaddr == NULL_ADDR) {
605 if (create) {
Chao Yuf9811702015-09-21 20:17:52 +0800606 if (unlikely(f2fs_cp_error(sbi))) {
607 err = -EIO;
608 goto put_out;
609 }
Chao Yu973163f2015-09-18 16:51:51 +0800610 err = __allocate_data_block(&dn);
611 if (err)
612 goto put_out;
613 allocated = true;
614 map->m_flags = F2FS_MAP_NEW;
615 } else {
616 if (flag != F2FS_GET_BLOCK_FIEMAP ||
617 dn.data_blkaddr != NEW_ADDR) {
618 if (flag == F2FS_GET_BLOCK_BMAP)
619 err = -ENOENT;
620 goto put_out;
621 }
622
623 /*
624 * preallocated unwritten block should be mapped
625 * for fiemap.
626 */
627 if (dn.data_blkaddr == NEW_ADDR)
628 map->m_flags = F2FS_MAP_UNWRITTEN;
Chao Yue2b4e2b2015-08-19 19:11:19 +0800629 }
Chao Yue2b4e2b2015-08-19 19:11:19 +0800630 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900631
Chao Yu973163f2015-09-18 16:51:51 +0800632 map->m_flags |= F2FS_MAP_MAPPED;
633 map->m_pblk = dn.data_blkaddr;
634 map->m_len = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900635
Chao Yu6403eb12014-04-26 19:59:52 +0800636 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900637 dn.ofs_in_node++;
638 pgofs++;
639
640get_next:
Fan Li7df3a432015-12-17 13:20:59 +0800641 if (map->m_len >= maxblocks)
642 goto sync_out;
643
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900644 if (dn.ofs_in_node >= end_offset) {
645 if (allocated)
646 sync_inode_page(&dn);
647 allocated = false;
648 f2fs_put_dnode(&dn);
649
Chao Yu3104af352015-12-23 17:11:43 +0800650 if (create) {
651 f2fs_unlock_op(sbi);
652 f2fs_lock_op(sbi);
653 }
654
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900655 set_new_dnode(&dn, inode, NULL, NULL, 0);
656 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900657 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900658 if (err == -ENOENT)
659 err = 0;
660 goto unlock_out;
661 }
Chao Yue2b4e2b2015-08-19 19:11:19 +0800662
Chao Yu6403eb12014-04-26 19:59:52 +0800663 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900664 }
665
Fan Li7df3a432015-12-17 13:20:59 +0800666 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800667
Fan Li7df3a432015-12-17 13:20:59 +0800668 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
669 if (create) {
670 if (unlikely(f2fs_cp_error(sbi))) {
671 err = -EIO;
672 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800673 }
Fan Li7df3a432015-12-17 13:20:59 +0800674 err = __allocate_data_block(&dn);
675 if (err)
676 goto sync_out;
677 allocated = true;
678 map->m_flags |= F2FS_MAP_NEW;
679 blkaddr = dn.data_blkaddr;
680 } else {
681 /*
682 * we only merge preallocated unwritten blocks
683 * for fiemap.
684 */
685 if (flag != F2FS_GET_BLOCK_FIEMAP ||
686 blkaddr != NEW_ADDR)
687 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900688 }
689 }
Fan Li7df3a432015-12-17 13:20:59 +0800690
691 /* Give more consecutive addresses for the readahead */
692 if ((map->m_pblk != NEW_ADDR &&
693 blkaddr == (map->m_pblk + ofs)) ||
694 (map->m_pblk == NEW_ADDR &&
695 blkaddr == NEW_ADDR)) {
696 ofs++;
697 dn.ofs_in_node++;
698 pgofs++;
699 map->m_len++;
700 goto get_next;
701 }
702
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900703sync_out:
704 if (allocated)
705 sync_inode_page(&dn);
706put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900707 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900708unlock_out:
709 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800710 f2fs_unlock_op(sbi);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900711out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700712 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900713 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900714}
715
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700716static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800717 struct buffer_head *bh, int create, int flag)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700718{
719 struct f2fs_map_blocks map;
720 int ret;
721
722 map.m_lblk = iblock;
723 map.m_len = bh->b_size >> inode->i_blkbits;
724
Chao Yue2b4e2b2015-08-19 19:11:19 +0800725 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700726 if (!ret) {
727 map_bh(bh, inode->i_sb, map.m_pblk);
728 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
729 bh->b_size = map.m_len << inode->i_blkbits;
730 }
731 return ret;
732}
733
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900734static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800735 struct buffer_head *bh_result, int create, int flag)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900736{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800737 return __get_data_block(inode, iblock, bh_result, create, flag);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900738}
739
Chao Yue2b4e2b2015-08-19 19:11:19 +0800740static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900741 struct buffer_head *bh_result, int create)
742{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800743 return __get_data_block(inode, iblock, bh_result, create,
744 F2FS_GET_BLOCK_DIO);
745}
746
747static int get_data_block_bmap(struct inode *inode, sector_t iblock,
748 struct buffer_head *bh_result, int create)
749{
750 return __get_data_block(inode, iblock, bh_result, create,
751 F2FS_GET_BLOCK_BMAP);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900752}
753
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700754static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
755{
756 return (offset >> inode->i_blkbits);
757}
758
759static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
760{
761 return (blk << inode->i_blkbits);
762}
763
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900764int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
765 u64 start, u64 len)
766{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700767 struct buffer_head map_bh;
768 sector_t start_blk, last_blk;
769 loff_t isize = i_size_read(inode);
770 u64 logical = 0, phys = 0, size = 0;
771 u32 flags = 0;
772 bool past_eof = false, whole_file = false;
773 int ret = 0;
774
775 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
776 if (ret)
777 return ret;
778
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700779 if (f2fs_has_inline_data(inode)) {
780 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
781 if (ret != -EAGAIN)
782 return ret;
783 }
784
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700785 mutex_lock(&inode->i_mutex);
786
787 if (len >= isize) {
788 whole_file = true;
789 len = isize;
790 }
791
792 if (logical_to_blk(inode, len) == 0)
793 len = blk_to_logical(inode, 1);
794
795 start_blk = logical_to_blk(inode, start);
796 last_blk = logical_to_blk(inode, start + len - 1);
797next:
798 memset(&map_bh, 0, sizeof(struct buffer_head));
799 map_bh.b_size = len;
800
Chao Yue2b4e2b2015-08-19 19:11:19 +0800801 ret = get_data_block(inode, start_blk, &map_bh, 0,
802 F2FS_GET_BLOCK_FIEMAP);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700803 if (ret)
804 goto out;
805
806 /* HOLE */
807 if (!buffer_mapped(&map_bh)) {
808 start_blk++;
809
810 if (!past_eof && blk_to_logical(inode, start_blk) >= isize)
811 past_eof = 1;
812
813 if (past_eof && size) {
814 flags |= FIEMAP_EXTENT_LAST;
815 ret = fiemap_fill_next_extent(fieinfo, logical,
816 phys, size, flags);
817 } else if (size) {
818 ret = fiemap_fill_next_extent(fieinfo, logical,
819 phys, size, flags);
820 size = 0;
821 }
822
823 /* if we have holes up to/past EOF then we're done */
824 if (start_blk > last_blk || past_eof || ret)
825 goto out;
826 } else {
827 if (start_blk > last_blk && !whole_file) {
828 ret = fiemap_fill_next_extent(fieinfo, logical,
829 phys, size, flags);
830 goto out;
831 }
832
833 /*
834 * if size != 0 then we know we already have an extent
835 * to add, so add it.
836 */
837 if (size) {
838 ret = fiemap_fill_next_extent(fieinfo, logical,
839 phys, size, flags);
840 if (ret)
841 goto out;
842 }
843
844 logical = blk_to_logical(inode, start_blk);
845 phys = blk_to_logical(inode, map_bh.b_blocknr);
846 size = map_bh.b_size;
847 flags = 0;
848 if (buffer_unwritten(&map_bh))
849 flags = FIEMAP_EXTENT_UNWRITTEN;
850
851 start_blk += logical_to_blk(inode, size);
852
853 /*
854 * If we are past the EOF, then we need to make sure as
855 * soon as we find a hole that the last extent we found
856 * is marked with FIEMAP_EXTENT_LAST
857 */
858 if (!past_eof && logical + size >= isize)
859 past_eof = true;
860 }
861 cond_resched();
862 if (fatal_signal_pending(current))
863 ret = -EINTR;
864 else
865 goto next;
866out:
867 if (ret == 1)
868 ret = 0;
869
870 mutex_unlock(&inode->i_mutex);
871 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900872}
873
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700874/*
875 * This function was originally taken from fs/mpage.c, and customized for f2fs.
876 * Major change was from block_size == page_size in f2fs by default.
877 */
878static int f2fs_mpage_readpages(struct address_space *mapping,
879 struct list_head *pages, struct page *page,
880 unsigned nr_pages)
881{
882 struct bio *bio = NULL;
883 unsigned page_idx;
884 sector_t last_block_in_bio = 0;
885 struct inode *inode = mapping->host;
886 const unsigned blkbits = inode->i_blkbits;
887 const unsigned blocksize = 1 << blkbits;
888 sector_t block_in_file;
889 sector_t last_block;
890 sector_t last_block_in_file;
891 sector_t block_nr;
892 struct block_device *bdev = inode->i_sb->s_bdev;
893 struct f2fs_map_blocks map;
894
895 map.m_pblk = 0;
896 map.m_lblk = 0;
897 map.m_len = 0;
898 map.m_flags = 0;
899
900 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
901
902 prefetchw(&page->flags);
903 if (pages) {
904 page = list_entry(pages->prev, struct page, lru);
905 list_del(&page->lru);
906 if (add_to_page_cache_lru(page, mapping,
907 page->index, GFP_KERNEL))
908 goto next_page;
909 }
910
911 block_in_file = (sector_t)page->index;
912 last_block = block_in_file + nr_pages;
913 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
914 blkbits;
915 if (last_block > last_block_in_file)
916 last_block = last_block_in_file;
917
918 /*
919 * Map blocks using the previous result first.
920 */
921 if ((map.m_flags & F2FS_MAP_MAPPED) &&
922 block_in_file > map.m_lblk &&
923 block_in_file < (map.m_lblk + map.m_len))
924 goto got_it;
925
926 /*
927 * Then do more f2fs_map_blocks() calls until we are
928 * done with this page.
929 */
930 map.m_flags = 0;
931
932 if (block_in_file < last_block) {
933 map.m_lblk = block_in_file;
934 map.m_len = last_block - block_in_file;
935
Chao Yu46c9e142015-09-18 16:54:16 +0800936 if (f2fs_map_blocks(inode, &map, 0,
937 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700938 goto set_error_page;
939 }
940got_it:
941 if ((map.m_flags & F2FS_MAP_MAPPED)) {
942 block_nr = map.m_pblk + block_in_file - map.m_lblk;
943 SetPageMappedToDisk(page);
944
945 if (!PageUptodate(page) && !cleancache_get_page(page)) {
946 SetPageUptodate(page);
947 goto confused;
948 }
949 } else {
950 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
951 SetPageUptodate(page);
952 unlock_page(page);
953 goto next_page;
954 }
955
956 /*
957 * This page will go to BIO. Do we need to send this
958 * BIO off first?
959 */
960 if (bio && (last_block_in_bio != block_nr - 1)) {
961submit_and_realloc:
962 submit_bio(READ, bio);
963 bio = NULL;
964 }
965 if (bio == NULL) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700966 struct f2fs_crypto_ctx *ctx = NULL;
967
968 if (f2fs_encrypted_inode(inode) &&
969 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700970
971 ctx = f2fs_get_crypto_ctx(inode);
972 if (IS_ERR(ctx))
973 goto set_error_page;
974
975 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +0800976 f2fs_wait_on_encrypted_page_writeback(
977 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700978 }
979
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700980 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200981 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700982 if (!bio) {
983 if (ctx)
984 f2fs_release_crypto_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700985 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700986 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700987 bio->bi_bdev = bdev;
988 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +0800989 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700990 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700991 }
992
993 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
994 goto submit_and_realloc;
995
996 last_block_in_bio = block_nr;
997 goto next_page;
998set_error_page:
999 SetPageError(page);
1000 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1001 unlock_page(page);
1002 goto next_page;
1003confused:
1004 if (bio) {
1005 submit_bio(READ, bio);
1006 bio = NULL;
1007 }
1008 unlock_page(page);
1009next_page:
1010 if (pages)
1011 page_cache_release(page);
1012 }
1013 BUG_ON(pages && !list_empty(pages));
1014 if (bio)
1015 submit_bio(READ, bio);
1016 return 0;
1017}
1018
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001019static int f2fs_read_data_page(struct file *file, struct page *page)
1020{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001021 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001022 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001023
Chao Yuc20e89c2014-05-06 16:53:08 +08001024 trace_f2fs_readpage(page, DATA);
1025
arter97e1c42042014-08-06 23:22:50 +09001026 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001027 if (f2fs_has_inline_data(inode))
1028 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001029 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001030 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001031 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001032}
1033
1034static int f2fs_read_data_pages(struct file *file,
1035 struct address_space *mapping,
1036 struct list_head *pages, unsigned nr_pages)
1037{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001038 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001039 struct page *page = list_entry(pages->prev, struct page, lru);
1040
1041 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001042
1043 /* If the file has inline data, skip readpages */
1044 if (f2fs_has_inline_data(inode))
1045 return 0;
1046
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001047 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001048}
1049
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001050int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001051{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001052 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001053 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001054 struct dnode_of_data dn;
1055 int err = 0;
1056
1057 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001058 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001059 if (err)
1060 return err;
1061
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001062 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001063
1064 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001065 if (fio->blk_addr == NULL_ADDR) {
1066 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001067 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001068 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001069
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001070 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Chao Yu08b39fb2015-10-08 13:27:34 +08001071
1072 /* wait for GCed encrypted page writeback */
1073 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
1074 fio->blk_addr);
1075
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001076 fio->encrypted_page = f2fs_encrypt(inode, fio->page);
1077 if (IS_ERR(fio->encrypted_page)) {
1078 err = PTR_ERR(fio->encrypted_page);
1079 goto out_writepage;
1080 }
1081 }
1082
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001083 set_page_writeback(page);
1084
1085 /*
1086 * If current allocation needs SSR,
1087 * it had better in-place writes for updated data.
1088 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001089 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001090 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001091 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001092 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001093 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001094 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001095 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001096 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001097 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001098 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001099 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001100 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001101 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001102 if (page->index == 0)
1103 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001104 }
1105out_writepage:
1106 f2fs_put_dnode(&dn);
1107 return err;
1108}
1109
1110static int f2fs_write_data_page(struct page *page,
1111 struct writeback_control *wbc)
1112{
1113 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001114 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001115 loff_t i_size = i_size_read(inode);
1116 const pgoff_t end_index = ((unsigned long long) i_size)
1117 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001118 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001119 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001120 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001121 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001122 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001123 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001124 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001125 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001126 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001127 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001128
Chao Yuecda0de2014-05-06 16:48:26 +08001129 trace_f2fs_writepage(page, DATA);
1130
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001131 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001132 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001133
1134 /*
1135 * If the offset is out-of-range of file size,
1136 * this page does not have to be written to disk.
1137 */
1138 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001139 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001140 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001141
1142 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001143write:
Chao Yucaf00472015-01-28 17:48:42 +08001144 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001145 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001146 if (f2fs_is_drop_cache(inode))
1147 goto out;
1148 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1149 available_free_memory(sbi, BASE_CHECK))
1150 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001151
Jaegeuk Kim39936832012-11-22 16:21:29 +09001152 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001153 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001154 if (unlikely(f2fs_cp_error(sbi)))
1155 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001156 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001157 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001158 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001159
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001160 /* we should bypass data pages to proceed the kworkder jobs */
1161 if (unlikely(f2fs_cp_error(sbi))) {
1162 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001163 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001164 }
1165
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001166 if (!wbc->for_reclaim)
1167 need_balance_fs = true;
1168 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001169 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001170
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001171 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001172 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001173 if (f2fs_has_inline_data(inode))
1174 err = f2fs_write_inline_data(inode, page);
1175 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001176 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001177 f2fs_unlock_op(sbi);
1178done:
1179 if (err && err != -ENOENT)
1180 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001181
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001182 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001183out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001184 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001185 if (err)
1186 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001188 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001189 f2fs_balance_fs(sbi);
Chao Yueb7e8132015-11-10 18:45:07 +08001190 if (wbc->for_reclaim) {
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001191 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001192 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001193 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001194 return 0;
1195
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001196redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001197 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001198 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001199}
1200
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001201static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1202 void *data)
1203{
1204 struct address_space *mapping = data;
1205 int ret = mapping->a_ops->writepage(page, wbc);
1206 mapping_set_error(mapping, ret);
1207 return ret;
1208}
1209
Chao Yu8f46dca2015-07-14 18:56:10 +08001210/*
1211 * This function was copied from write_cche_pages from mm/page-writeback.c.
1212 * The major change is making write step of cold data page separately from
1213 * warm/hot data page.
1214 */
1215static int f2fs_write_cache_pages(struct address_space *mapping,
1216 struct writeback_control *wbc, writepage_t writepage,
1217 void *data)
1218{
1219 int ret = 0;
1220 int done = 0;
1221 struct pagevec pvec;
1222 int nr_pages;
1223 pgoff_t uninitialized_var(writeback_index);
1224 pgoff_t index;
1225 pgoff_t end; /* Inclusive */
1226 pgoff_t done_index;
1227 int cycled;
1228 int range_whole = 0;
1229 int tag;
1230 int step = 0;
1231
1232 pagevec_init(&pvec, 0);
1233next:
1234 if (wbc->range_cyclic) {
1235 writeback_index = mapping->writeback_index; /* prev offset */
1236 index = writeback_index;
1237 if (index == 0)
1238 cycled = 1;
1239 else
1240 cycled = 0;
1241 end = -1;
1242 } else {
1243 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1244 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1245 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1246 range_whole = 1;
1247 cycled = 1; /* ignore range_cyclic tests */
1248 }
1249 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1250 tag = PAGECACHE_TAG_TOWRITE;
1251 else
1252 tag = PAGECACHE_TAG_DIRTY;
1253retry:
1254 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1255 tag_pages_for_writeback(mapping, index, end);
1256 done_index = index;
1257 while (!done && (index <= end)) {
1258 int i;
1259
1260 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1261 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1262 if (nr_pages == 0)
1263 break;
1264
1265 for (i = 0; i < nr_pages; i++) {
1266 struct page *page = pvec.pages[i];
1267
1268 if (page->index > end) {
1269 done = 1;
1270 break;
1271 }
1272
1273 done_index = page->index;
1274
1275 lock_page(page);
1276
1277 if (unlikely(page->mapping != mapping)) {
1278continue_unlock:
1279 unlock_page(page);
1280 continue;
1281 }
1282
1283 if (!PageDirty(page)) {
1284 /* someone wrote it for us */
1285 goto continue_unlock;
1286 }
1287
Tiezhu Yang737f1892015-07-17 12:56:00 +08001288 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001289 goto continue_unlock;
1290
1291 if (PageWriteback(page)) {
1292 if (wbc->sync_mode != WB_SYNC_NONE)
1293 f2fs_wait_on_page_writeback(page, DATA);
1294 else
1295 goto continue_unlock;
1296 }
1297
1298 BUG_ON(PageWriteback(page));
1299 if (!clear_page_dirty_for_io(page))
1300 goto continue_unlock;
1301
1302 ret = (*writepage)(page, wbc, data);
1303 if (unlikely(ret)) {
1304 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1305 unlock_page(page);
1306 ret = 0;
1307 } else {
1308 done_index = page->index + 1;
1309 done = 1;
1310 break;
1311 }
1312 }
1313
1314 if (--wbc->nr_to_write <= 0 &&
1315 wbc->sync_mode == WB_SYNC_NONE) {
1316 done = 1;
1317 break;
1318 }
1319 }
1320 pagevec_release(&pvec);
1321 cond_resched();
1322 }
1323
1324 if (step < 1) {
1325 step++;
1326 goto next;
1327 }
1328
1329 if (!cycled && !done) {
1330 cycled = 1;
1331 index = 0;
1332 end = writeback_index - 1;
1333 goto retry;
1334 }
1335 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1336 mapping->writeback_index = done_index;
1337
1338 return ret;
1339}
1340
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001341static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001342 struct writeback_control *wbc)
1343{
1344 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001345 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001346 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001347 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001348 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001349
Chao Yue5748432014-05-06 16:51:24 +08001350 trace_f2fs_writepages(mapping->host, wbc, DATA);
1351
P J Pcfb185a2013-04-03 11:38:00 +09001352 /* deal with chardevs and other special file */
1353 if (!mapping->a_ops->writepage)
1354 return 0;
1355
Chao Yu6a290542015-07-17 18:02:39 +08001356 /* skip writing if there is no dirty page in this inode */
1357 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1358 return 0;
1359
Jaegeuk Kima1257022015-10-08 10:40:07 -07001360 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1361 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1362 available_free_memory(sbi, DIRTY_DENTS))
1363 goto skip_write;
1364
Chao Yud323d002015-10-27 09:53:45 +08001365 /* skip writing during file defragment */
1366 if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG))
1367 goto skip_write;
1368
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001369 /* during POR, we don't need to trigger writepage at all. */
1370 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1371 goto skip_write;
1372
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001373 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001374
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001375 if (!S_ISDIR(inode->i_mode)) {
1376 mutex_lock(&sbi->writepages);
1377 locked = true;
1378 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001379 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yubb96a8d2015-07-06 20:31:49 +08001380 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001381 if (locked)
1382 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001383
Chao Yuc227f912015-12-16 13:09:20 +08001384 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001385
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001386 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001387 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001388
1389skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001390 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001391 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001392}
1393
Chao Yu3aab8f82014-07-02 13:25:04 +08001394static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1395{
1396 struct inode *inode = mapping->host;
1397
1398 if (to > inode->i_size) {
1399 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001400 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001401 }
1402}
1403
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001404static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1405 loff_t pos, unsigned len, unsigned flags,
1406 struct page **pagep, void **fsdata)
1407{
1408 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001409 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001410 struct page *page = NULL;
1411 struct page *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001412 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1413 struct dnode_of_data dn;
1414 int err = 0;
1415
Chao Yu62aed042014-05-06 16:46:04 +08001416 trace_f2fs_write_begin(inode, pos, len, flags);
1417
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001418 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001419
1420 /*
1421 * We should check this at this moment to avoid deadlock on inode page
1422 * and #0 page. The locking rule for inline_data conversion should be:
1423 * lock_page(page #0) -> lock_page(inode_page)
1424 */
1425 if (index != 0) {
1426 err = f2fs_convert_inline_inode(inode);
1427 if (err)
1428 goto fail;
1429 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001430repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001431 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001432 if (!page) {
1433 err = -ENOMEM;
1434 goto fail;
1435 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001436
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001437 *pagep = page;
1438
Gu Zhenge4795562013-09-27 18:08:30 +08001439 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001440
1441 /* check inline_data */
1442 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001443 if (IS_ERR(ipage)) {
1444 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001445 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001446 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001447
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001448 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001449
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001450 if (f2fs_has_inline_data(inode)) {
1451 if (pos + len <= MAX_INLINE_DATA) {
1452 read_inline_data(page, ipage);
1453 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1454 sync_inode_page(&dn);
1455 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001456 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001457 err = f2fs_convert_inline_page(&dn, page);
1458 if (err)
1459 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001460 }
Fan Li759af1c2015-08-05 15:52:16 +08001461
1462 err = f2fs_get_block(&dn, index);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001463 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001464 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001465put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001466 f2fs_put_dnode(&dn);
1467 f2fs_unlock_op(sbi);
1468
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001469 f2fs_wait_on_page_writeback(page, DATA);
1470
Chao Yu08b39fb2015-10-08 13:27:34 +08001471 /* wait for GCed encrypted page writeback */
1472 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1473 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
1474
Chao Yu90d43882015-07-08 18:24:38 +08001475 if (len == PAGE_CACHE_SIZE)
1476 goto out_update;
1477 if (PageUptodate(page))
1478 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001479
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001480 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1481 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1482 unsigned end = start + len;
1483
1484 /* Reading beyond i_size is simple: memset to zero */
1485 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001486 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001487 }
1488
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001489 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001490 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1491 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001492 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001493 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001494 .type = DATA,
1495 .rw = READ_SYNC,
1496 .blk_addr = dn.data_blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001497 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001498 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001499 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001500 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001501 if (err)
1502 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001503
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001504 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001505 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001506 err = -EIO;
1507 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001508 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001509 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001510 f2fs_put_page(page, 1);
1511 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001512 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001513
1514 /* avoid symlink page */
1515 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1516 err = f2fs_decrypt_one(inode, page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001517 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001518 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001519 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001520 }
Chao Yu90d43882015-07-08 18:24:38 +08001521out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001522 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001523out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001524 clear_cold_data(page);
1525 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001526
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001527put_fail:
1528 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001529unlock_fail:
1530 f2fs_unlock_op(sbi);
Chao Yu3aab8f82014-07-02 13:25:04 +08001531fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001532 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001533 f2fs_write_failed(mapping, pos + len);
1534 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001535}
1536
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001537static int f2fs_write_end(struct file *file,
1538 struct address_space *mapping,
1539 loff_t pos, unsigned len, unsigned copied,
1540 struct page *page, void *fsdata)
1541{
1542 struct inode *inode = page->mapping->host;
1543
Chao Yudfb2bf32014-05-06 16:47:23 +08001544 trace_f2fs_write_end(inode, pos, len, copied);
1545
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001546 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001547
1548 if (pos + copied > i_size_read(inode)) {
1549 i_size_write(inode, pos + copied);
1550 mark_inode_dirty(inode);
1551 update_inode_page(inode);
1552 }
1553
Chao Yu75c3c8b2013-11-16 14:15:59 +08001554 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001555 return copied;
1556}
1557
Omar Sandoval6f673762015-03-16 04:33:52 -07001558static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1559 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001560{
1561 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001562
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001563 if (offset & blocksize_mask)
1564 return -EINVAL;
1565
Al Viro5b46f252014-03-16 18:07:34 -04001566 if (iov_iter_alignment(iter) & blocksize_mask)
1567 return -EINVAL;
1568
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001569 return 0;
1570}
1571
Omar Sandoval22c61862015-03-16 04:33:53 -07001572static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1573 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001574{
1575 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001576 struct address_space *mapping = file->f_mapping;
1577 struct inode *inode = mapping->host;
1578 size_t count = iov_iter_count(iter);
1579 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001580
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001581 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001582 err = f2fs_convert_inline_inode(inode);
1583 if (err)
1584 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001585
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001586 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1587 return 0;
1588
Chao Yuc15e8592015-08-07 18:39:32 +08001589 err = check_direct_IO(inode, iter, offset);
1590 if (err)
1591 return err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001592
Omar Sandoval6f673762015-03-16 04:33:52 -07001593 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001594
Chao Yuf9811702015-09-21 20:17:52 +08001595 if (iov_iter_rw(iter) == WRITE) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001596 __allocate_data_blocks(inode, offset, count);
Chao Yuf9811702015-09-21 20:17:52 +08001597 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1598 err = -EIO;
1599 goto out;
1600 }
1601 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001602
Chao Yue2b4e2b2015-08-19 19:11:19 +08001603 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
Chao Yuf9811702015-09-21 20:17:52 +08001604out:
Omar Sandoval6f673762015-03-16 04:33:52 -07001605 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001606 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001607
Omar Sandoval6f673762015-03-16 04:33:52 -07001608 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001609
Chao Yu3aab8f82014-07-02 13:25:04 +08001610 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001611}
1612
Chao Yu487261f2015-02-05 17:44:29 +08001613void f2fs_invalidate_page(struct page *page, unsigned int offset,
1614 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001615{
1616 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001617 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001618
Chao Yu487261f2015-02-05 17:44:29 +08001619 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1620 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001621 return;
1622
Chao Yu487261f2015-02-05 17:44:29 +08001623 if (PageDirty(page)) {
1624 if (inode->i_ino == F2FS_META_INO(sbi))
1625 dec_page_count(sbi, F2FS_DIRTY_META);
1626 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1627 dec_page_count(sbi, F2FS_DIRTY_NODES);
1628 else
1629 inode_dec_dirty_pages(inode);
1630 }
Chao Yudecd36b2015-08-07 18:42:09 +08001631
1632 /* This is atomic written page, keep Private */
1633 if (IS_ATOMIC_WRITTEN_PAGE(page))
1634 return;
1635
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001636 ClearPagePrivate(page);
1637}
1638
Chao Yu487261f2015-02-05 17:44:29 +08001639int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001640{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001641 /* If this is dirty page, keep PagePrivate */
1642 if (PageDirty(page))
1643 return 0;
1644
Chao Yudecd36b2015-08-07 18:42:09 +08001645 /* This is atomic written page, keep Private */
1646 if (IS_ATOMIC_WRITTEN_PAGE(page))
1647 return 0;
1648
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001649 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001650 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001651}
1652
1653static int f2fs_set_data_page_dirty(struct page *page)
1654{
1655 struct address_space *mapping = page->mapping;
1656 struct inode *inode = mapping->host;
1657
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001658 trace_f2fs_set_page_dirty(page, DATA);
1659
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001660 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001661
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001662 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001663 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1664 register_inmem_page(inode, page);
1665 return 1;
1666 }
1667 /*
1668 * Previously, this page has been registered, we just
1669 * return here.
1670 */
1671 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001672 }
1673
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001674 if (!PageDirty(page)) {
1675 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001676 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001677 return 1;
1678 }
1679 return 0;
1680}
1681
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001682static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1683{
Chao Yu454ae7e2014-04-22 13:34:01 +08001684 struct inode *inode = mapping->host;
1685
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001686 if (f2fs_has_inline_data(inode))
1687 return 0;
1688
1689 /* make sure allocating whole blocks */
1690 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1691 filemap_write_and_wait(mapping);
1692
Chao Yue2b4e2b2015-08-19 19:11:19 +08001693 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001694}
1695
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001696const struct address_space_operations f2fs_dblock_aops = {
1697 .readpage = f2fs_read_data_page,
1698 .readpages = f2fs_read_data_pages,
1699 .writepage = f2fs_write_data_page,
1700 .writepages = f2fs_write_data_pages,
1701 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001702 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001703 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001704 .invalidatepage = f2fs_invalidate_page,
1705 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001706 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001707 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001708};