blob: 972eab7ac07193da485df3efc6b6c11c99dacd97 [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);
229}
230
231int reserve_new_block(struct dnode_of_data *dn)
232{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700233 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900234
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900235 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900236 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800237 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900238 return -ENOSPC;
239
Namjae Jeonc01e2852013-04-23 17:00:52 +0900240 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
241
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900242 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800243 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900244 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900245 sync_inode_page(dn);
246 return 0;
247}
248
Huajun Lib6009652013-11-10 23:13:18 +0800249int 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 Kima8865372013-12-27 17:04:17 +0900257
Huajun Lib6009652013-11-10 23:13:18 +0800258 if (dn->data_blkaddr == NULL_ADDR)
259 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900260 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800261 f2fs_put_dnode(dn);
262 return err;
263}
264
Fan Li759af1c2015-08-05 15:52:16 +0800265int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900266{
Chao Yu028a41e2015-03-19 19:26:02 +0800267 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800268 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800269
Fan Li759af1c2015-08-05 15:52:16 +0800270 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
271 dn->data_blkaddr = ei.blk + index - ei.fofs;
272 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800273 }
274
Fan Li759af1c2015-08-05 15:52:16 +0800275 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900276}
277
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700278struct page *get_read_data_page(struct inode *inode, pgoff_t index,
279 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900280{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900281 struct address_space *mapping = inode->i_mapping;
282 struct dnode_of_data dn;
283 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800284 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900285 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800286 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700287 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800288 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700289 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700290 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800291 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900292
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700293 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
294 return read_mapping_page(mapping, index, NULL);
295
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700296 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900297 if (!page)
298 return ERR_PTR(-ENOMEM);
299
Chao Yucb3bc9e2015-02-05 18:03:40 +0800300 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
301 dn.data_blkaddr = ei.blk + index - ei.fofs;
302 goto got_it;
303 }
304
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900305 set_new_dnode(&dn, inode, NULL, NULL, 0);
306 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700307 if (err)
308 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900309 f2fs_put_dnode(&dn);
310
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900311 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700312 err = -ENOENT;
313 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900314 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800315got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700316 if (PageUptodate(page)) {
317 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900318 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700319 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900321 /*
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 Kim43f3eae2015-04-30 17:00:33 -0700330 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900331 return page;
332 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900333
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800334 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700335 fio.page = page;
336 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900337 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700338 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700339 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700340
341put_err:
342 f2fs_put_page(page, 1);
343 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700344}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900345
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700346struct 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 Kima56c7c62015-10-09 15:11:38 -0700356 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700357 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 Kima56c7c62015-10-09 15:11:38 -0700376struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
377 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700378{
379 struct address_space *mapping = inode->i_mapping;
380 struct page *page;
381repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700382 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700383 if (IS_ERR(page))
384 return page;
385
386 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900387 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900388 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900389 f2fs_put_page(page, 1);
390 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900391 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900392 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900393 f2fs_put_page(page, 1);
394 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900395 }
396 return page;
397}
398
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900399/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900400 * Caller ensures that this data page is never allocated.
401 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900402 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800403 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
404 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800405 * Note that, ipage is set only by make_empty_dir, and if any error occur,
406 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900407 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900408struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900409 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900410{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900411 struct address_space *mapping = inode->i_mapping;
412 struct page *page;
413 struct dnode_of_data dn;
414 int err;
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700415repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700416 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800417 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 Kim01f28612015-04-29 11:18:42 -0700423 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800424 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900425
Jaegeuk Kima8865372013-12-27 17:04:17 +0900426 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800427 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700428 if (err) {
429 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900430 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900431 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700432 if (!ipage)
433 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900434
435 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700436 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900437
438 if (dn.data_blkaddr == NEW_ADDR) {
439 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900440 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900441 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700442 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900443
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700444 page = get_read_data_page(inode, index, READ_SYNC, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700445 if (IS_ERR(page))
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900446 goto repeat;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700447
448 /* wait for read completion */
449 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900450 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700451got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800452 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 Kim699489b2013-06-07 22:08:23 +0900455 /* Only the directory inode sets new_i_size */
456 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900457 }
458 return page;
459}
460
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900461static int __allocate_data_block(struct dnode_of_data *dn)
462{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700463 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700464 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900465 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900466 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800467 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700468 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900469
470 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
471 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800472
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 Kimbfad7c22013-12-16 19:04:05 +0900477 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
478 return -ENOSPC;
479
Chao Yudf6136e2015-03-23 10:33:37 +0800480alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900481 get_node_info(sbi, dn->nid, &ni);
482 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
483
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800484 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
485 seg = CURSEG_DIRECT_IO;
486
Chao Yudf6136e2015-03-23 10:33:37 +0800487 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
488 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800489 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900490
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700491 /* update i_size */
492 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
493 dn->ofs_in_node;
Chao Yu9edcdab2015-09-11 14:43:52 +0800494 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 Kim976e4c52014-09-15 19:32:16 -0700497
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700498 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yua28ef1f2015-07-08 17:59:36 +0800499 f2fs_drop_largest_extent(dn->inode, fofs);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700500
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900501 return 0;
502}
503
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800504static 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 Yud6d4f1c2015-03-12 17:04:24 +0800527 block_t blkaddr;
528
Chao Yuf9811702015-09-21 20:17:52 +0800529 if (unlikely(f2fs_cp_error(sbi)))
530 goto sync_out;
531
Chao Yud6d4f1c2015-03-12 17:04:24 +0800532 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800533 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800534 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
551sync_out:
552 if (allocated)
553 sync_inode_page(&dn);
554 f2fs_put_dnode(&dn);
555out:
556 f2fs_unlock_op(sbi);
557 return;
558}
559
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900560/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700561 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
562 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800563 * 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 Kimeb47b802012-11-02 17:10:12 +0900568 */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700569static int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800570 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900571{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700572 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900573 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800574 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900575 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
576 pgoff_t pgofs, end_offset;
577 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800578 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900579 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900580
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700581 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 Kimeb47b802012-11-02 17:10:12 +0900586
Chao Yu7e4dde72015-02-05 17:51:34 +0800587 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700588 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 Kimbfad7c22013-12-16 19:04:05 +0900591 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800592 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900593
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800594 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700595 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900596
597 /* When reading holes, we need its node page */
598 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900599 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900600 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900601 if (err == -ENOENT)
602 err = 0;
603 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900604 }
Chao Yu973163f2015-09-18 16:51:51 +0800605
606 if (dn.data_blkaddr == NEW_ADDR || dn.data_blkaddr == NULL_ADDR) {
607 if (create) {
Chao Yuf9811702015-09-21 20:17:52 +0800608 if (unlikely(f2fs_cp_error(sbi))) {
609 err = -EIO;
610 goto put_out;
611 }
Chao Yu973163f2015-09-18 16:51:51 +0800612 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 Yue2b4e2b2015-08-19 19:11:19 +0800631 }
Chao Yue2b4e2b2015-08-19 19:11:19 +0800632 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900633
Chao Yu973163f2015-09-18 16:51:51 +0800634 map->m_flags |= F2FS_MAP_MAPPED;
635 map->m_pblk = dn.data_blkaddr;
636 map->m_len = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900637
Chao Yu6403eb12014-04-26 19:59:52 +0800638 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900639 dn.ofs_in_node++;
640 pgofs++;
641
642get_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 Kim1ec79082013-12-26 16:55:22 +0900651 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900652 if (err == -ENOENT)
653 err = 0;
654 goto unlock_out;
655 }
Chao Yue2b4e2b2015-08-19 19:11:19 +0800656
Chao Yu6403eb12014-04-26 19:59:52 +0800657 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900658 }
659
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700660 if (maxblocks > map->m_len) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900661 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800662
663 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
664 if (create) {
Chao Yuf9811702015-09-21 20:17:52 +0800665 if (unlikely(f2fs_cp_error(sbi))) {
666 err = -EIO;
667 goto sync_out;
668 }
Chao Yu973163f2015-09-18 16:51:51 +0800669 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 Kimbfad7c22013-12-16 19:04:05 +0900684 }
Chao Yu973163f2015-09-18 16:51:51 +0800685
arter97e1c42042014-08-06 23:22:50 +0900686 /* Give more consecutive addresses for the readahead */
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700687 if ((map->m_pblk != NEW_ADDR &&
688 blkaddr == (map->m_pblk + ofs)) ||
689 (map->m_pblk == NEW_ADDR &&
690 blkaddr == NEW_ADDR)) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900691 ofs++;
692 dn.ofs_in_node++;
693 pgofs++;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700694 map->m_len++;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900695 goto get_next;
696 }
697 }
698sync_out:
699 if (allocated)
700 sync_inode_page(&dn);
701put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900702 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900703unlock_out:
704 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700705 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900706out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700707 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900708 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900709}
710
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700711static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800712 struct buffer_head *bh, int create, int flag)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700713{
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 Yue2b4e2b2015-08-19 19:11:19 +0800720 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700721 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 Kimccfb3002014-06-13 13:02:11 +0900729static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800730 struct buffer_head *bh_result, int create, int flag)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900731{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800732 return __get_data_block(inode, iblock, bh_result, create, flag);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900733}
734
Chao Yue2b4e2b2015-08-19 19:11:19 +0800735static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900736 struct buffer_head *bh_result, int create)
737{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800738 return __get_data_block(inode, iblock, bh_result, create,
739 F2FS_GET_BLOCK_DIO);
740}
741
742static 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 Kimccfb3002014-06-13 13:02:11 +0900747}
748
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700749static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
750{
751 return (offset >> inode->i_blkbits);
752}
753
754static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
755{
756 return (blk << inode->i_blkbits);
757}
758
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900759int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
760 u64 start, u64 len)
761{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700762 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
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700774 if (f2fs_has_inline_data(inode)) {
775 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
776 if (ret != -EAGAIN)
777 return ret;
778 }
779
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700780 mutex_lock(&inode->i_mutex);
781
782 if (len >= isize) {
783 whole_file = true;
784 len = isize;
785 }
786
787 if (logical_to_blk(inode, len) == 0)
788 len = blk_to_logical(inode, 1);
789
790 start_blk = logical_to_blk(inode, start);
791 last_blk = logical_to_blk(inode, start + len - 1);
792next:
793 memset(&map_bh, 0, sizeof(struct buffer_head));
794 map_bh.b_size = len;
795
Chao Yue2b4e2b2015-08-19 19:11:19 +0800796 ret = get_data_block(inode, start_blk, &map_bh, 0,
797 F2FS_GET_BLOCK_FIEMAP);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700798 if (ret)
799 goto out;
800
801 /* HOLE */
802 if (!buffer_mapped(&map_bh)) {
803 start_blk++;
804
805 if (!past_eof && blk_to_logical(inode, start_blk) >= isize)
806 past_eof = 1;
807
808 if (past_eof && size) {
809 flags |= FIEMAP_EXTENT_LAST;
810 ret = fiemap_fill_next_extent(fieinfo, logical,
811 phys, size, flags);
812 } else if (size) {
813 ret = fiemap_fill_next_extent(fieinfo, logical,
814 phys, size, flags);
815 size = 0;
816 }
817
818 /* if we have holes up to/past EOF then we're done */
819 if (start_blk > last_blk || past_eof || ret)
820 goto out;
821 } else {
822 if (start_blk > last_blk && !whole_file) {
823 ret = fiemap_fill_next_extent(fieinfo, logical,
824 phys, size, flags);
825 goto out;
826 }
827
828 /*
829 * if size != 0 then we know we already have an extent
830 * to add, so add it.
831 */
832 if (size) {
833 ret = fiemap_fill_next_extent(fieinfo, logical,
834 phys, size, flags);
835 if (ret)
836 goto out;
837 }
838
839 logical = blk_to_logical(inode, start_blk);
840 phys = blk_to_logical(inode, map_bh.b_blocknr);
841 size = map_bh.b_size;
842 flags = 0;
843 if (buffer_unwritten(&map_bh))
844 flags = FIEMAP_EXTENT_UNWRITTEN;
845
846 start_blk += logical_to_blk(inode, size);
847
848 /*
849 * If we are past the EOF, then we need to make sure as
850 * soon as we find a hole that the last extent we found
851 * is marked with FIEMAP_EXTENT_LAST
852 */
853 if (!past_eof && logical + size >= isize)
854 past_eof = true;
855 }
856 cond_resched();
857 if (fatal_signal_pending(current))
858 ret = -EINTR;
859 else
860 goto next;
861out:
862 if (ret == 1)
863 ret = 0;
864
865 mutex_unlock(&inode->i_mutex);
866 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900867}
868
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700869/*
870 * This function was originally taken from fs/mpage.c, and customized for f2fs.
871 * Major change was from block_size == page_size in f2fs by default.
872 */
873static int f2fs_mpage_readpages(struct address_space *mapping,
874 struct list_head *pages, struct page *page,
875 unsigned nr_pages)
876{
877 struct bio *bio = NULL;
878 unsigned page_idx;
879 sector_t last_block_in_bio = 0;
880 struct inode *inode = mapping->host;
881 const unsigned blkbits = inode->i_blkbits;
882 const unsigned blocksize = 1 << blkbits;
883 sector_t block_in_file;
884 sector_t last_block;
885 sector_t last_block_in_file;
886 sector_t block_nr;
887 struct block_device *bdev = inode->i_sb->s_bdev;
888 struct f2fs_map_blocks map;
889
890 map.m_pblk = 0;
891 map.m_lblk = 0;
892 map.m_len = 0;
893 map.m_flags = 0;
894
895 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
896
897 prefetchw(&page->flags);
898 if (pages) {
899 page = list_entry(pages->prev, struct page, lru);
900 list_del(&page->lru);
901 if (add_to_page_cache_lru(page, mapping,
902 page->index, GFP_KERNEL))
903 goto next_page;
904 }
905
906 block_in_file = (sector_t)page->index;
907 last_block = block_in_file + nr_pages;
908 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
909 blkbits;
910 if (last_block > last_block_in_file)
911 last_block = last_block_in_file;
912
913 /*
914 * Map blocks using the previous result first.
915 */
916 if ((map.m_flags & F2FS_MAP_MAPPED) &&
917 block_in_file > map.m_lblk &&
918 block_in_file < (map.m_lblk + map.m_len))
919 goto got_it;
920
921 /*
922 * Then do more f2fs_map_blocks() calls until we are
923 * done with this page.
924 */
925 map.m_flags = 0;
926
927 if (block_in_file < last_block) {
928 map.m_lblk = block_in_file;
929 map.m_len = last_block - block_in_file;
930
Chao Yu46c9e142015-09-18 16:54:16 +0800931 if (f2fs_map_blocks(inode, &map, 0,
932 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700933 goto set_error_page;
934 }
935got_it:
936 if ((map.m_flags & F2FS_MAP_MAPPED)) {
937 block_nr = map.m_pblk + block_in_file - map.m_lblk;
938 SetPageMappedToDisk(page);
939
940 if (!PageUptodate(page) && !cleancache_get_page(page)) {
941 SetPageUptodate(page);
942 goto confused;
943 }
944 } else {
945 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
946 SetPageUptodate(page);
947 unlock_page(page);
948 goto next_page;
949 }
950
951 /*
952 * This page will go to BIO. Do we need to send this
953 * BIO off first?
954 */
955 if (bio && (last_block_in_bio != block_nr - 1)) {
956submit_and_realloc:
957 submit_bio(READ, bio);
958 bio = NULL;
959 }
960 if (bio == NULL) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700961 struct f2fs_crypto_ctx *ctx = NULL;
962
963 if (f2fs_encrypted_inode(inode) &&
964 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700965
966 ctx = f2fs_get_crypto_ctx(inode);
967 if (IS_ERR(ctx))
968 goto set_error_page;
969
970 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +0800971 f2fs_wait_on_encrypted_page_writeback(
972 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700973 }
974
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700975 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200976 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700977 if (!bio) {
978 if (ctx)
979 f2fs_release_crypto_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700980 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700981 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700982 bio->bi_bdev = bdev;
983 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +0800984 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700985 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700986 }
987
988 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
989 goto submit_and_realloc;
990
991 last_block_in_bio = block_nr;
992 goto next_page;
993set_error_page:
994 SetPageError(page);
995 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
996 unlock_page(page);
997 goto next_page;
998confused:
999 if (bio) {
1000 submit_bio(READ, bio);
1001 bio = NULL;
1002 }
1003 unlock_page(page);
1004next_page:
1005 if (pages)
1006 page_cache_release(page);
1007 }
1008 BUG_ON(pages && !list_empty(pages));
1009 if (bio)
1010 submit_bio(READ, bio);
1011 return 0;
1012}
1013
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001014static int f2fs_read_data_page(struct file *file, struct page *page)
1015{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001016 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001017 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001018
Chao Yuc20e89c2014-05-06 16:53:08 +08001019 trace_f2fs_readpage(page, DATA);
1020
arter97e1c42042014-08-06 23:22:50 +09001021 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001022 if (f2fs_has_inline_data(inode))
1023 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001024 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001025 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001026 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001027}
1028
1029static int f2fs_read_data_pages(struct file *file,
1030 struct address_space *mapping,
1031 struct list_head *pages, unsigned nr_pages)
1032{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001033 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001034 struct page *page = list_entry(pages->prev, struct page, lru);
1035
1036 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001037
1038 /* If the file has inline data, skip readpages */
1039 if (f2fs_has_inline_data(inode))
1040 return 0;
1041
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001042 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001043}
1044
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001045int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001046{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001047 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001048 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001049 struct dnode_of_data dn;
1050 int err = 0;
1051
1052 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001053 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001054 if (err)
1055 return err;
1056
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001057 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001058
1059 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001060 if (fio->blk_addr == NULL_ADDR) {
1061 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001062 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001063 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001064
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001065 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Chao Yu08b39fb2015-10-08 13:27:34 +08001066
1067 /* wait for GCed encrypted page writeback */
1068 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
1069 fio->blk_addr);
1070
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001071 fio->encrypted_page = f2fs_encrypt(inode, fio->page);
1072 if (IS_ERR(fio->encrypted_page)) {
1073 err = PTR_ERR(fio->encrypted_page);
1074 goto out_writepage;
1075 }
1076 }
1077
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001078 set_page_writeback(page);
1079
1080 /*
1081 * If current allocation needs SSR,
1082 * it had better in-place writes for updated data.
1083 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001084 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001085 !is_cold_data(page) &&
1086 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001087 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001088 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001089 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001090 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001091 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001092 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001093 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001094 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001095 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001096 if (page->index == 0)
1097 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001098 }
1099out_writepage:
1100 f2fs_put_dnode(&dn);
1101 return err;
1102}
1103
1104static int f2fs_write_data_page(struct page *page,
1105 struct writeback_control *wbc)
1106{
1107 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001108 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001109 loff_t i_size = i_size_read(inode);
1110 const pgoff_t end_index = ((unsigned long long) i_size)
1111 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001112 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001113 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001114 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001115 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001116 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001117 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001118 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001119 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001120 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001121 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001122
Chao Yuecda0de2014-05-06 16:48:26 +08001123 trace_f2fs_writepage(page, DATA);
1124
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001125 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001126 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001127
1128 /*
1129 * If the offset is out-of-range of file size,
1130 * this page does not have to be written to disk.
1131 */
1132 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001133 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001134 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001135
1136 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001137write:
Chao Yucaf00472015-01-28 17:48:42 +08001138 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001139 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001140 if (f2fs_is_drop_cache(inode))
1141 goto out;
1142 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1143 available_free_memory(sbi, BASE_CHECK))
1144 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001145
Jaegeuk Kim39936832012-11-22 16:21:29 +09001146 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001147 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001148 if (unlikely(f2fs_cp_error(sbi)))
1149 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001150 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001151 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001152 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001153
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001154 /* we should bypass data pages to proceed the kworkder jobs */
1155 if (unlikely(f2fs_cp_error(sbi))) {
1156 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001157 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001158 }
1159
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001160 if (!wbc->for_reclaim)
1161 need_balance_fs = true;
1162 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001163 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001164
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001165 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001166 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001167 if (f2fs_has_inline_data(inode))
1168 err = f2fs_write_inline_data(inode, page);
1169 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001170 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001171 f2fs_unlock_op(sbi);
1172done:
1173 if (err && err != -ENOENT)
1174 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001175
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001176 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001177out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001178 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001179 if (err)
1180 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001181 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001182 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001183 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001184 if (wbc->for_reclaim)
1185 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001186 return 0;
1187
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001188redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001189 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001190 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001191}
1192
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001193static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1194 void *data)
1195{
1196 struct address_space *mapping = data;
1197 int ret = mapping->a_ops->writepage(page, wbc);
1198 mapping_set_error(mapping, ret);
1199 return ret;
1200}
1201
Chao Yu8f46dca2015-07-14 18:56:10 +08001202/*
1203 * This function was copied from write_cche_pages from mm/page-writeback.c.
1204 * The major change is making write step of cold data page separately from
1205 * warm/hot data page.
1206 */
1207static int f2fs_write_cache_pages(struct address_space *mapping,
1208 struct writeback_control *wbc, writepage_t writepage,
1209 void *data)
1210{
1211 int ret = 0;
1212 int done = 0;
1213 struct pagevec pvec;
1214 int nr_pages;
1215 pgoff_t uninitialized_var(writeback_index);
1216 pgoff_t index;
1217 pgoff_t end; /* Inclusive */
1218 pgoff_t done_index;
1219 int cycled;
1220 int range_whole = 0;
1221 int tag;
1222 int step = 0;
1223
1224 pagevec_init(&pvec, 0);
1225next:
1226 if (wbc->range_cyclic) {
1227 writeback_index = mapping->writeback_index; /* prev offset */
1228 index = writeback_index;
1229 if (index == 0)
1230 cycled = 1;
1231 else
1232 cycled = 0;
1233 end = -1;
1234 } else {
1235 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1236 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1237 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1238 range_whole = 1;
1239 cycled = 1; /* ignore range_cyclic tests */
1240 }
1241 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1242 tag = PAGECACHE_TAG_TOWRITE;
1243 else
1244 tag = PAGECACHE_TAG_DIRTY;
1245retry:
1246 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1247 tag_pages_for_writeback(mapping, index, end);
1248 done_index = index;
1249 while (!done && (index <= end)) {
1250 int i;
1251
1252 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1253 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1254 if (nr_pages == 0)
1255 break;
1256
1257 for (i = 0; i < nr_pages; i++) {
1258 struct page *page = pvec.pages[i];
1259
1260 if (page->index > end) {
1261 done = 1;
1262 break;
1263 }
1264
1265 done_index = page->index;
1266
1267 lock_page(page);
1268
1269 if (unlikely(page->mapping != mapping)) {
1270continue_unlock:
1271 unlock_page(page);
1272 continue;
1273 }
1274
1275 if (!PageDirty(page)) {
1276 /* someone wrote it for us */
1277 goto continue_unlock;
1278 }
1279
Tiezhu Yang737f1892015-07-17 12:56:00 +08001280 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001281 goto continue_unlock;
1282
1283 if (PageWriteback(page)) {
1284 if (wbc->sync_mode != WB_SYNC_NONE)
1285 f2fs_wait_on_page_writeback(page, DATA);
1286 else
1287 goto continue_unlock;
1288 }
1289
1290 BUG_ON(PageWriteback(page));
1291 if (!clear_page_dirty_for_io(page))
1292 goto continue_unlock;
1293
1294 ret = (*writepage)(page, wbc, data);
1295 if (unlikely(ret)) {
1296 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1297 unlock_page(page);
1298 ret = 0;
1299 } else {
1300 done_index = page->index + 1;
1301 done = 1;
1302 break;
1303 }
1304 }
1305
1306 if (--wbc->nr_to_write <= 0 &&
1307 wbc->sync_mode == WB_SYNC_NONE) {
1308 done = 1;
1309 break;
1310 }
1311 }
1312 pagevec_release(&pvec);
1313 cond_resched();
1314 }
1315
1316 if (step < 1) {
1317 step++;
1318 goto next;
1319 }
1320
1321 if (!cycled && !done) {
1322 cycled = 1;
1323 index = 0;
1324 end = writeback_index - 1;
1325 goto retry;
1326 }
1327 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1328 mapping->writeback_index = done_index;
1329
1330 return ret;
1331}
1332
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001333static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001334 struct writeback_control *wbc)
1335{
1336 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001337 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001338 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001339 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001340 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001341
Chao Yue5748432014-05-06 16:51:24 +08001342 trace_f2fs_writepages(mapping->host, wbc, DATA);
1343
P J Pcfb185a2013-04-03 11:38:00 +09001344 /* deal with chardevs and other special file */
1345 if (!mapping->a_ops->writepage)
1346 return 0;
1347
Chao Yu6a290542015-07-17 18:02:39 +08001348 /* skip writing if there is no dirty page in this inode */
1349 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1350 return 0;
1351
Jaegeuk Kima1257022015-10-08 10:40:07 -07001352 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1353 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1354 available_free_memory(sbi, DIRTY_DENTS))
1355 goto skip_write;
1356
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001357 /* during POR, we don't need to trigger writepage at all. */
1358 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1359 goto skip_write;
1360
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001361 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001362
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001363 if (!S_ISDIR(inode->i_mode)) {
1364 mutex_lock(&sbi->writepages);
1365 locked = true;
1366 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001367 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yubb96a8d2015-07-06 20:31:49 +08001368 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001369 if (locked)
1370 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001371
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001372 remove_dirty_dir_inode(inode);
1373
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001374 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001375 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001376
1377skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001378 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001379 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001380}
1381
Chao Yu3aab8f82014-07-02 13:25:04 +08001382static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1383{
1384 struct inode *inode = mapping->host;
1385
1386 if (to > inode->i_size) {
1387 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001388 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001389 }
1390}
1391
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001392static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1393 loff_t pos, unsigned len, unsigned flags,
1394 struct page **pagep, void **fsdata)
1395{
1396 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001397 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001398 struct page *page = NULL;
1399 struct page *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001400 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1401 struct dnode_of_data dn;
1402 int err = 0;
1403
Chao Yu62aed042014-05-06 16:46:04 +08001404 trace_f2fs_write_begin(inode, pos, len, flags);
1405
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001406 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001407
1408 /*
1409 * We should check this at this moment to avoid deadlock on inode page
1410 * and #0 page. The locking rule for inline_data conversion should be:
1411 * lock_page(page #0) -> lock_page(inode_page)
1412 */
1413 if (index != 0) {
1414 err = f2fs_convert_inline_inode(inode);
1415 if (err)
1416 goto fail;
1417 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001418repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001419 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001420 if (!page) {
1421 err = -ENOMEM;
1422 goto fail;
1423 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001424
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001425 *pagep = page;
1426
Gu Zhenge4795562013-09-27 18:08:30 +08001427 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001428
1429 /* check inline_data */
1430 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001431 if (IS_ERR(ipage)) {
1432 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001433 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001434 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001435
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001436 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001437
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001438 if (f2fs_has_inline_data(inode)) {
1439 if (pos + len <= MAX_INLINE_DATA) {
1440 read_inline_data(page, ipage);
1441 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1442 sync_inode_page(&dn);
1443 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001444 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001445 err = f2fs_convert_inline_page(&dn, page);
1446 if (err)
1447 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001448 }
Fan Li759af1c2015-08-05 15:52:16 +08001449
1450 err = f2fs_get_block(&dn, index);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001451 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001452 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001453put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001454 f2fs_put_dnode(&dn);
1455 f2fs_unlock_op(sbi);
1456
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001457 f2fs_wait_on_page_writeback(page, DATA);
1458
Chao Yu08b39fb2015-10-08 13:27:34 +08001459 /* wait for GCed encrypted page writeback */
1460 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1461 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
1462
Chao Yu90d43882015-07-08 18:24:38 +08001463 if (len == PAGE_CACHE_SIZE)
1464 goto out_update;
1465 if (PageUptodate(page))
1466 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001467
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001468 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1469 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1470 unsigned end = start + len;
1471
1472 /* Reading beyond i_size is simple: memset to zero */
1473 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001474 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001475 }
1476
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001477 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001478 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1479 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001480 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001481 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001482 .type = DATA,
1483 .rw = READ_SYNC,
1484 .blk_addr = dn.data_blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001485 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001486 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001487 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001488 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001489 if (err)
1490 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001491
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001492 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001493 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001494 err = -EIO;
1495 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001496 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001497 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001498 f2fs_put_page(page, 1);
1499 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001500 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001501
1502 /* avoid symlink page */
1503 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1504 err = f2fs_decrypt_one(inode, page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001505 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001506 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001507 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001508 }
Chao Yu90d43882015-07-08 18:24:38 +08001509out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001510 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001511out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001512 clear_cold_data(page);
1513 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001514
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001515put_fail:
1516 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001517unlock_fail:
1518 f2fs_unlock_op(sbi);
Chao Yu3aab8f82014-07-02 13:25:04 +08001519fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001520 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001521 f2fs_write_failed(mapping, pos + len);
1522 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001523}
1524
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001525static int f2fs_write_end(struct file *file,
1526 struct address_space *mapping,
1527 loff_t pos, unsigned len, unsigned copied,
1528 struct page *page, void *fsdata)
1529{
1530 struct inode *inode = page->mapping->host;
1531
Chao Yudfb2bf32014-05-06 16:47:23 +08001532 trace_f2fs_write_end(inode, pos, len, copied);
1533
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001534 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001535
1536 if (pos + copied > i_size_read(inode)) {
1537 i_size_write(inode, pos + copied);
1538 mark_inode_dirty(inode);
1539 update_inode_page(inode);
1540 }
1541
Chao Yu75c3c8b2013-11-16 14:15:59 +08001542 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001543 return copied;
1544}
1545
Omar Sandoval6f673762015-03-16 04:33:52 -07001546static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1547 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001548{
1549 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001550
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001551 if (offset & blocksize_mask)
1552 return -EINVAL;
1553
Al Viro5b46f252014-03-16 18:07:34 -04001554 if (iov_iter_alignment(iter) & blocksize_mask)
1555 return -EINVAL;
1556
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001557 return 0;
1558}
1559
Omar Sandoval22c61862015-03-16 04:33:53 -07001560static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1561 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001562{
1563 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001564 struct address_space *mapping = file->f_mapping;
1565 struct inode *inode = mapping->host;
1566 size_t count = iov_iter_count(iter);
1567 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001568
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001569 /* we don't need to use inline_data strictly */
1570 if (f2fs_has_inline_data(inode)) {
1571 err = f2fs_convert_inline_inode(inode);
1572 if (err)
1573 return err;
1574 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001575
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001576 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1577 return 0;
1578
Chao Yuc15e8592015-08-07 18:39:32 +08001579 err = check_direct_IO(inode, iter, offset);
1580 if (err)
1581 return err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001582
Omar Sandoval6f673762015-03-16 04:33:52 -07001583 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001584
Chao Yuf9811702015-09-21 20:17:52 +08001585 if (iov_iter_rw(iter) == WRITE) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001586 __allocate_data_blocks(inode, offset, count);
Chao Yuf9811702015-09-21 20:17:52 +08001587 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1588 err = -EIO;
1589 goto out;
1590 }
1591 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001592
Chao Yue2b4e2b2015-08-19 19:11:19 +08001593 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
Chao Yuf9811702015-09-21 20:17:52 +08001594out:
Omar Sandoval6f673762015-03-16 04:33:52 -07001595 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001596 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001597
Omar Sandoval6f673762015-03-16 04:33:52 -07001598 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001599
Chao Yu3aab8f82014-07-02 13:25:04 +08001600 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001601}
1602
Chao Yu487261f2015-02-05 17:44:29 +08001603void f2fs_invalidate_page(struct page *page, unsigned int offset,
1604 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001605{
1606 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001607 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001608
Chao Yu487261f2015-02-05 17:44:29 +08001609 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1610 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001611 return;
1612
Chao Yu487261f2015-02-05 17:44:29 +08001613 if (PageDirty(page)) {
1614 if (inode->i_ino == F2FS_META_INO(sbi))
1615 dec_page_count(sbi, F2FS_DIRTY_META);
1616 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1617 dec_page_count(sbi, F2FS_DIRTY_NODES);
1618 else
1619 inode_dec_dirty_pages(inode);
1620 }
Chao Yudecd36b2015-08-07 18:42:09 +08001621
1622 /* This is atomic written page, keep Private */
1623 if (IS_ATOMIC_WRITTEN_PAGE(page))
1624 return;
1625
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001626 ClearPagePrivate(page);
1627}
1628
Chao Yu487261f2015-02-05 17:44:29 +08001629int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001630{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001631 /* If this is dirty page, keep PagePrivate */
1632 if (PageDirty(page))
1633 return 0;
1634
Chao Yudecd36b2015-08-07 18:42:09 +08001635 /* This is atomic written page, keep Private */
1636 if (IS_ATOMIC_WRITTEN_PAGE(page))
1637 return 0;
1638
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001639 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001640 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001641}
1642
1643static int f2fs_set_data_page_dirty(struct page *page)
1644{
1645 struct address_space *mapping = page->mapping;
1646 struct inode *inode = mapping->host;
1647
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001648 trace_f2fs_set_page_dirty(page, DATA);
1649
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001650 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001651
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001652 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001653 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1654 register_inmem_page(inode, page);
1655 return 1;
1656 }
1657 /*
1658 * Previously, this page has been registered, we just
1659 * return here.
1660 */
1661 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001662 }
1663
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001664 if (!PageDirty(page)) {
1665 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001666 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001667 return 1;
1668 }
1669 return 0;
1670}
1671
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001672static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1673{
Chao Yu454ae7e2014-04-22 13:34:01 +08001674 struct inode *inode = mapping->host;
1675
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001676 if (f2fs_has_inline_data(inode))
1677 return 0;
1678
1679 /* make sure allocating whole blocks */
1680 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1681 filemap_write_and_wait(mapping);
1682
Chao Yue2b4e2b2015-08-19 19:11:19 +08001683 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001684}
1685
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001686const struct address_space_operations f2fs_dblock_aops = {
1687 .readpage = f2fs_read_data_page,
1688 .readpages = f2fs_read_data_pages,
1689 .writepage = f2fs_write_data_page,
1690 .writepages = f2fs_write_data_pages,
1691 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001692 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001693 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001694 .invalidatepage = f2fs_invalidate_page,
1695 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001696 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001697 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001698};