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