4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/aio.h>
16 #include <linux/writeback.h>
17 #include <linux/backing-dev.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.h>
26 #include <trace/events/f2fs.h>
28 static void f2fs_read_end_io(struct bio *bio, int err)
33 bio_for_each_segment_all(bvec, bio, i) {
34 struct page *page = bvec->bv_page;
37 SetPageUptodate(page);
39 ClearPageUptodate(page);
47 static void f2fs_write_end_io(struct bio *bio, int err)
49 struct f2fs_sb_info *sbi = bio->bi_private;
53 bio_for_each_segment_all(bvec, bio, i) {
54 struct page *page = bvec->bv_page;
58 set_bit(AS_EIO, &page->mapping->flags);
59 f2fs_stop_checkpoint(sbi);
61 end_page_writeback(page);
62 dec_page_count(sbi, F2FS_WRITEBACK);
65 if (!get_pages(sbi, F2FS_WRITEBACK) &&
66 !list_empty(&sbi->cp_wait.task_list))
67 wake_up(&sbi->cp_wait);
73 * Low-level block read/write IO operations.
75 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
76 int npages, bool is_read)
80 /* No failure on bio allocation */
81 bio = bio_alloc(GFP_NOIO, npages);
83 bio->bi_bdev = sbi->sb->s_bdev;
84 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
85 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
86 bio->bi_private = sbi;
91 static void __submit_merged_bio(struct f2fs_bio_info *io)
93 struct f2fs_io_info *fio = &io->fio;
98 if (is_read_io(fio->rw))
99 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
101 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
103 submit_bio(fio->rw, io->bio);
107 void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
108 enum page_type type, int rw)
110 enum page_type btype = PAGE_TYPE_OF_BIO(type);
111 struct f2fs_bio_info *io;
113 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
115 down_write(&io->io_rwsem);
117 /* change META to META_FLUSH in the checkpoint procedure */
118 if (type >= META_FLUSH) {
119 io->fio.type = META_FLUSH;
120 if (test_opt(sbi, NOBARRIER))
121 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
123 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
125 __submit_merged_bio(io);
126 up_write(&io->io_rwsem);
130 * Fill the locked page with data located in the block address.
131 * Return unlocked page.
133 int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
134 struct f2fs_io_info *fio)
138 trace_f2fs_submit_page_bio(page, fio);
139 f2fs_trace_ios(page, fio, 0);
141 /* Allocate a new bio */
142 bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw));
144 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
146 f2fs_put_page(page, 1);
150 submit_bio(fio->rw, bio);
154 void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
155 struct f2fs_io_info *fio)
157 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
158 struct f2fs_bio_info *io;
159 bool is_read = is_read_io(fio->rw);
161 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
163 verify_block_addr(sbi, fio->blk_addr);
165 down_write(&io->io_rwsem);
168 inc_page_count(sbi, F2FS_WRITEBACK);
170 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
171 io->fio.rw != fio->rw))
172 __submit_merged_bio(io);
174 if (io->bio == NULL) {
175 int bio_blocks = MAX_BIO_BLOCKS(sbi);
177 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
181 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
183 __submit_merged_bio(io);
187 io->last_block_in_bio = fio->blk_addr;
188 f2fs_trace_ios(page, fio, 0);
190 up_write(&io->io_rwsem);
191 trace_f2fs_submit_page_mbio(page, fio);
195 * Lock ordering for the change of data block address:
198 * update block addresses in the node page
200 static void __set_data_blkaddr(struct dnode_of_data *dn)
202 struct f2fs_node *rn;
204 struct page *node_page = dn->node_page;
205 unsigned int ofs_in_node = dn->ofs_in_node;
207 f2fs_wait_on_page_writeback(node_page, NODE);
209 rn = F2FS_NODE(node_page);
211 /* Get physical address of data block */
212 addr_array = blkaddr_in_node(rn);
213 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
214 set_page_dirty(node_page);
217 int reserve_new_block(struct dnode_of_data *dn)
219 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
221 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
223 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
226 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
228 dn->data_blkaddr = NEW_ADDR;
229 __set_data_blkaddr(dn);
230 mark_inode_dirty(dn->inode);
235 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
237 bool need_put = dn->inode_page ? false : true;
240 err = get_dnode_of_data(dn, index, ALLOC_NODE);
244 if (dn->data_blkaddr == NULL_ADDR)
245 err = reserve_new_block(dn);
251 static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
252 struct buffer_head *bh_result)
254 struct f2fs_inode_info *fi = F2FS_I(inode);
255 pgoff_t start_fofs, end_fofs;
256 block_t start_blkaddr;
258 if (is_inode_flag_set(fi, FI_NO_EXTENT))
261 read_lock(&fi->ext.ext_lock);
262 if (fi->ext.len == 0) {
263 read_unlock(&fi->ext.ext_lock);
267 stat_inc_total_hit(inode->i_sb);
269 start_fofs = fi->ext.fofs;
270 end_fofs = fi->ext.fofs + fi->ext.len - 1;
271 start_blkaddr = fi->ext.blk_addr;
273 if (pgofs >= start_fofs && pgofs <= end_fofs) {
274 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
277 clear_buffer_new(bh_result);
278 map_bh(bh_result, inode->i_sb,
279 start_blkaddr + pgofs - start_fofs);
280 count = end_fofs - pgofs + 1;
281 if (count < (UINT_MAX >> blkbits))
282 bh_result->b_size = (count << blkbits);
284 bh_result->b_size = UINT_MAX;
286 stat_inc_read_hit(inode->i_sb);
287 read_unlock(&fi->ext.ext_lock);
290 read_unlock(&fi->ext.ext_lock);
294 void update_extent_cache(struct dnode_of_data *dn)
296 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
297 pgoff_t fofs, start_fofs, end_fofs;
298 block_t start_blkaddr, end_blkaddr;
299 int need_update = true;
301 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
303 /* Update the page address in the parent node */
304 __set_data_blkaddr(dn);
306 if (is_inode_flag_set(fi, FI_NO_EXTENT))
309 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
312 write_lock(&fi->ext.ext_lock);
314 start_fofs = fi->ext.fofs;
315 end_fofs = fi->ext.fofs + fi->ext.len - 1;
316 start_blkaddr = fi->ext.blk_addr;
317 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
319 /* Drop and initialize the matched extent */
320 if (fi->ext.len == 1 && fofs == start_fofs)
324 if (fi->ext.len == 0) {
325 if (dn->data_blkaddr != NULL_ADDR) {
327 fi->ext.blk_addr = dn->data_blkaddr;
334 if (fofs == start_fofs - 1 && dn->data_blkaddr == start_blkaddr - 1) {
342 if (fofs == end_fofs + 1 && dn->data_blkaddr == end_blkaddr + 1) {
347 /* Split the existing extent */
348 if (fi->ext.len > 1 &&
349 fofs >= start_fofs && fofs <= end_fofs) {
350 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
351 fi->ext.len = fofs - start_fofs;
353 fi->ext.fofs = fofs + 1;
354 fi->ext.blk_addr = start_blkaddr +
355 fofs - start_fofs + 1;
356 fi->ext.len -= fofs - start_fofs + 1;
362 /* Finally, if the extent is very fragmented, let's drop the cache. */
363 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
365 set_inode_flag(fi, FI_NO_EXTENT);
369 write_unlock(&fi->ext.ext_lock);
375 struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
377 struct address_space *mapping = inode->i_mapping;
378 struct dnode_of_data dn;
381 struct f2fs_io_info fio = {
383 .rw = sync ? READ_SYNC : READA,
386 page = find_get_page(mapping, index);
387 if (page && PageUptodate(page))
389 f2fs_put_page(page, 0);
391 set_new_dnode(&dn, inode, NULL, NULL, 0);
392 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
397 if (dn.data_blkaddr == NULL_ADDR)
398 return ERR_PTR(-ENOENT);
400 /* By fallocate(), there is no cached page, but with NEW_ADDR */
401 if (unlikely(dn.data_blkaddr == NEW_ADDR))
402 return ERR_PTR(-EINVAL);
404 page = grab_cache_page(mapping, index);
406 return ERR_PTR(-ENOMEM);
408 if (PageUptodate(page)) {
413 fio.blk_addr = dn.data_blkaddr;
414 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
419 wait_on_page_locked(page);
420 if (unlikely(!PageUptodate(page))) {
421 f2fs_put_page(page, 0);
422 return ERR_PTR(-EIO);
429 * If it tries to access a hole, return an error.
430 * Because, the callers, functions in dir.c and GC, should be able to know
431 * whether this page exists or not.
433 struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
435 struct address_space *mapping = inode->i_mapping;
436 struct dnode_of_data dn;
439 struct f2fs_io_info fio = {
444 page = grab_cache_page(mapping, index);
446 return ERR_PTR(-ENOMEM);
448 set_new_dnode(&dn, inode, NULL, NULL, 0);
449 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
451 f2fs_put_page(page, 1);
456 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
457 f2fs_put_page(page, 1);
458 return ERR_PTR(-ENOENT);
461 if (PageUptodate(page))
465 * A new dentry page is allocated but not able to be written, since its
466 * new inode page couldn't be allocated due to -ENOSPC.
467 * In such the case, its blkaddr can be remained as NEW_ADDR.
468 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
470 if (dn.data_blkaddr == NEW_ADDR) {
471 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
472 SetPageUptodate(page);
476 fio.blk_addr = dn.data_blkaddr;
477 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
482 if (unlikely(!PageUptodate(page))) {
483 f2fs_put_page(page, 1);
484 return ERR_PTR(-EIO);
486 if (unlikely(page->mapping != mapping)) {
487 f2fs_put_page(page, 1);
494 * Caller ensures that this data page is never allocated.
495 * A new zero-filled data page is allocated in the page cache.
497 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
499 * Note that, ipage is set only by make_empty_dir.
501 struct page *get_new_data_page(struct inode *inode,
502 struct page *ipage, pgoff_t index, bool new_i_size)
504 struct address_space *mapping = inode->i_mapping;
506 struct dnode_of_data dn;
509 set_new_dnode(&dn, inode, ipage, NULL, 0);
510 err = f2fs_reserve_block(&dn, index);
514 page = grab_cache_page(mapping, index);
520 if (PageUptodate(page))
523 if (dn.data_blkaddr == NEW_ADDR) {
524 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
525 SetPageUptodate(page);
527 struct f2fs_io_info fio = {
530 .blk_addr = dn.data_blkaddr,
532 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
537 if (unlikely(!PageUptodate(page))) {
538 f2fs_put_page(page, 1);
542 if (unlikely(page->mapping != mapping)) {
543 f2fs_put_page(page, 1);
549 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
550 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
551 /* Only the directory inode sets new_i_size */
552 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
561 static int __allocate_data_block(struct dnode_of_data *dn)
563 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
564 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
565 struct f2fs_summary sum;
567 int seg = CURSEG_WARM_DATA;
570 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
572 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
575 get_node_info(sbi, dn->nid, &ni);
576 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
578 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
579 seg = CURSEG_DIRECT_IO;
581 allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
583 /* direct IO doesn't use extent cache to maximize the performance */
584 __set_data_blkaddr(dn);
587 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
589 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
590 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
596 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
597 * If original data blocks are allocated, then give them to blockdev.
599 * a. preallocate requested block addresses
600 * b. do not use extent cache for better performance
601 * c. give the block addresses to blockdev
603 static int __get_data_block(struct inode *inode, sector_t iblock,
604 struct buffer_head *bh_result, int create, bool fiemap)
606 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
607 unsigned maxblocks = bh_result->b_size >> blkbits;
608 struct dnode_of_data dn;
609 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
610 pgoff_t pgofs, end_offset;
611 int err = 0, ofs = 1;
612 bool allocated = false;
614 /* Get the page offset from the block offset(iblock) */
615 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
617 if (check_extent_cache(inode, pgofs, bh_result))
621 f2fs_balance_fs(F2FS_I_SB(inode));
622 f2fs_lock_op(F2FS_I_SB(inode));
625 /* When reading holes, we need its node page */
626 set_new_dnode(&dn, inode, NULL, NULL, 0);
627 err = get_dnode_of_data(&dn, pgofs, mode);
633 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
636 if (dn.data_blkaddr != NULL_ADDR) {
637 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
639 err = __allocate_data_block(&dn);
643 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
648 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
649 bh_result->b_size = (((size_t)1) << blkbits);
654 if (dn.ofs_in_node >= end_offset) {
656 sync_inode_page(&dn);
660 set_new_dnode(&dn, inode, NULL, NULL, 0);
661 err = get_dnode_of_data(&dn, pgofs, mode);
667 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
670 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
673 if (maxblocks > (bh_result->b_size >> blkbits)) {
674 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
675 if (blkaddr == NULL_ADDR && create) {
676 err = __allocate_data_block(&dn);
680 blkaddr = dn.data_blkaddr;
682 /* Give more consecutive addresses for the readahead */
683 if (blkaddr == (bh_result->b_blocknr + ofs)) {
687 bh_result->b_size += (((size_t)1) << blkbits);
693 sync_inode_page(&dn);
698 f2fs_unlock_op(F2FS_I_SB(inode));
700 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
704 static int get_data_block(struct inode *inode, sector_t iblock,
705 struct buffer_head *bh_result, int create)
707 return __get_data_block(inode, iblock, bh_result, create, false);
710 static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
711 struct buffer_head *bh_result, int create)
713 return __get_data_block(inode, iblock, bh_result, create, true);
716 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
719 return generic_block_fiemap(inode, fieinfo,
720 start, len, get_data_block_fiemap);
723 static int f2fs_read_data_page(struct file *file, struct page *page)
725 struct inode *inode = page->mapping->host;
728 trace_f2fs_readpage(page, DATA);
730 /* If the file has inline data, try to read it directly */
731 if (f2fs_has_inline_data(inode))
732 ret = f2fs_read_inline_data(inode, page);
734 ret = mpage_readpage(page, get_data_block);
739 static int f2fs_read_data_pages(struct file *file,
740 struct address_space *mapping,
741 struct list_head *pages, unsigned nr_pages)
743 struct inode *inode = file->f_mapping->host;
745 /* If the file has inline data, skip readpages */
746 if (f2fs_has_inline_data(inode))
749 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
752 int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
754 struct inode *inode = page->mapping->host;
755 struct dnode_of_data dn;
758 set_new_dnode(&dn, inode, NULL, NULL, 0);
759 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
763 fio->blk_addr = dn.data_blkaddr;
765 /* This page is already truncated */
766 if (fio->blk_addr == NULL_ADDR)
769 set_page_writeback(page);
772 * If current allocation needs SSR,
773 * it had better in-place writes for updated data.
775 if (unlikely(fio->blk_addr != NEW_ADDR &&
776 !is_cold_data(page) &&
777 need_inplace_update(inode))) {
778 rewrite_data_page(page, fio);
779 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
781 write_data_page(page, &dn, fio);
782 update_extent_cache(&dn);
783 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
790 static int f2fs_write_data_page(struct page *page,
791 struct writeback_control *wbc)
793 struct inode *inode = page->mapping->host;
794 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
795 loff_t i_size = i_size_read(inode);
796 const pgoff_t end_index = ((unsigned long long) i_size)
799 bool need_balance_fs = false;
801 struct f2fs_io_info fio = {
803 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
806 trace_f2fs_writepage(page, DATA);
808 if (page->index < end_index)
812 * If the offset is out-of-range of file size,
813 * this page does not have to be written to disk.
815 offset = i_size & (PAGE_CACHE_SIZE - 1);
816 if ((page->index >= end_index + 1) || !offset)
819 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
821 if (unlikely(sbi->por_doing))
823 if (f2fs_is_drop_cache(inode))
825 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
826 available_free_memory(sbi, BASE_CHECK))
829 /* Dentry blocks are controlled by checkpoint */
830 if (S_ISDIR(inode->i_mode)) {
831 if (unlikely(f2fs_cp_error(sbi)))
833 err = do_write_data_page(page, &fio);
837 /* we should bypass data pages to proceed the kworkder jobs */
838 if (unlikely(f2fs_cp_error(sbi))) {
844 if (!wbc->for_reclaim)
845 need_balance_fs = true;
846 else if (has_not_enough_free_secs(sbi, 0))
851 if (f2fs_has_inline_data(inode))
852 err = f2fs_write_inline_data(inode, page);
854 err = do_write_data_page(page, &fio);
857 if (err && err != -ENOENT)
860 clear_cold_data(page);
862 inode_dec_dirty_pages(inode);
865 f2fs_balance_fs(sbi);
866 if (wbc->for_reclaim)
867 f2fs_submit_merged_bio(sbi, DATA, WRITE);
871 redirty_page_for_writepage(wbc, page);
872 return AOP_WRITEPAGE_ACTIVATE;
875 static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
878 struct address_space *mapping = data;
879 int ret = mapping->a_ops->writepage(page, wbc);
880 mapping_set_error(mapping, ret);
884 static int f2fs_write_data_pages(struct address_space *mapping,
885 struct writeback_control *wbc)
887 struct inode *inode = mapping->host;
888 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
893 trace_f2fs_writepages(mapping->host, wbc, DATA);
895 /* deal with chardevs and other special file */
896 if (!mapping->a_ops->writepage)
899 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
900 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
901 available_free_memory(sbi, DIRTY_DENTS))
904 diff = nr_pages_to_write(sbi, DATA, wbc);
906 if (!S_ISDIR(inode->i_mode)) {
907 mutex_lock(&sbi->writepages);
910 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
912 mutex_unlock(&sbi->writepages);
914 f2fs_submit_merged_bio(sbi, DATA, WRITE);
916 remove_dirty_dir_inode(inode);
918 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
922 wbc->pages_skipped += get_dirty_pages(inode);
926 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
928 struct inode *inode = mapping->host;
930 if (to > inode->i_size) {
931 truncate_pagecache(inode, inode->i_size);
932 truncate_blocks(inode, inode->i_size, true);
936 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
937 loff_t pos, unsigned len, unsigned flags,
938 struct page **pagep, void **fsdata)
940 struct inode *inode = mapping->host;
941 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
942 struct page *page, *ipage;
943 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
944 struct dnode_of_data dn;
947 trace_f2fs_write_begin(inode, pos, len, flags);
949 f2fs_balance_fs(sbi);
952 * We should check this at this moment to avoid deadlock on inode page
953 * and #0 page. The locking rule for inline_data conversion should be:
954 * lock_page(page #0) -> lock_page(inode_page)
957 err = f2fs_convert_inline_inode(inode);
962 page = grab_cache_page_write_begin(mapping, index, flags);
972 /* check inline_data */
973 ipage = get_node_page(sbi, inode->i_ino);
975 err = PTR_ERR(ipage);
979 set_new_dnode(&dn, inode, ipage, ipage, 0);
981 if (f2fs_has_inline_data(inode)) {
982 if (pos + len <= MAX_INLINE_DATA) {
983 read_inline_data(page, ipage);
984 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
985 sync_inode_page(&dn);
988 err = f2fs_convert_inline_page(&dn, page);
992 err = f2fs_reserve_block(&dn, index);
999 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1002 f2fs_wait_on_page_writeback(page, DATA);
1004 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1005 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1006 unsigned end = start + len;
1008 /* Reading beyond i_size is simple: memset to zero */
1009 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
1013 if (dn.data_blkaddr == NEW_ADDR) {
1014 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1016 struct f2fs_io_info fio = {
1019 .blk_addr = dn.data_blkaddr,
1021 err = f2fs_submit_page_bio(sbi, page, &fio);
1026 if (unlikely(!PageUptodate(page))) {
1027 f2fs_put_page(page, 1);
1031 if (unlikely(page->mapping != mapping)) {
1032 f2fs_put_page(page, 1);
1037 SetPageUptodate(page);
1038 clear_cold_data(page);
1042 f2fs_put_dnode(&dn);
1044 f2fs_unlock_op(sbi);
1045 f2fs_put_page(page, 1);
1047 f2fs_write_failed(mapping, pos + len);
1051 static int f2fs_write_end(struct file *file,
1052 struct address_space *mapping,
1053 loff_t pos, unsigned len, unsigned copied,
1054 struct page *page, void *fsdata)
1056 struct inode *inode = page->mapping->host;
1058 trace_f2fs_write_end(inode, pos, len, copied);
1060 set_page_dirty(page);
1062 if (pos + copied > i_size_read(inode)) {
1063 i_size_write(inode, pos + copied);
1064 mark_inode_dirty(inode);
1065 update_inode_page(inode);
1068 f2fs_put_page(page, 1);
1072 static int check_direct_IO(struct inode *inode, int rw,
1073 struct iov_iter *iter, loff_t offset)
1075 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
1080 if (offset & blocksize_mask)
1083 if (iov_iter_alignment(iter) & blocksize_mask)
1089 static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
1090 struct iov_iter *iter, loff_t offset)
1092 struct file *file = iocb->ki_filp;
1093 struct address_space *mapping = file->f_mapping;
1094 struct inode *inode = mapping->host;
1095 size_t count = iov_iter_count(iter);
1098 /* we don't need to use inline_data strictly */
1099 if (f2fs_has_inline_data(inode)) {
1100 err = f2fs_convert_inline_inode(inode);
1105 if (check_direct_IO(inode, rw, iter, offset))
1108 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1110 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1111 if (err < 0 && (rw & WRITE))
1112 f2fs_write_failed(mapping, offset + count);
1114 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1119 static void f2fs_invalidate_data_page(struct page *page, unsigned int offset,
1120 unsigned int length)
1122 struct inode *inode = page->mapping->host;
1124 if (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE)
1127 if (PageDirty(page))
1128 inode_dec_dirty_pages(inode);
1129 ClearPagePrivate(page);
1132 static int f2fs_release_data_page(struct page *page, gfp_t wait)
1134 ClearPagePrivate(page);
1138 static int f2fs_set_data_page_dirty(struct page *page)
1140 struct address_space *mapping = page->mapping;
1141 struct inode *inode = mapping->host;
1143 trace_f2fs_set_page_dirty(page, DATA);
1145 SetPageUptodate(page);
1147 if (f2fs_is_atomic_file(inode)) {
1148 register_inmem_page(inode, page);
1152 mark_inode_dirty(inode);
1154 if (!PageDirty(page)) {
1155 __set_page_dirty_nobuffers(page);
1156 update_dirty_page(inode, page);
1162 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1164 struct inode *inode = mapping->host;
1166 /* we don't need to use inline_data strictly */
1167 if (f2fs_has_inline_data(inode)) {
1168 int err = f2fs_convert_inline_inode(inode);
1172 return generic_block_bmap(mapping, block, get_data_block);
1175 const struct address_space_operations f2fs_dblock_aops = {
1176 .readpage = f2fs_read_data_page,
1177 .readpages = f2fs_read_data_pages,
1178 .writepage = f2fs_write_data_page,
1179 .writepages = f2fs_write_data_pages,
1180 .write_begin = f2fs_write_begin,
1181 .write_end = f2fs_write_end,
1182 .set_page_dirty = f2fs_set_data_page_dirty,
1183 .invalidatepage = f2fs_invalidate_data_page,
1184 .releasepage = f2fs_release_data_page,
1185 .direct_IO = f2fs_direct_IO,