1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/data.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/writeback.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/prefetch.h>
18 #include <linux/uio.h>
19 #include <linux/cleancache.h>
20 #include <linux/sched/signal.h>
21
22 #include "f2fs.h"
23 #include "node.h"
24 #include "segment.h"
25 #include "trace.h"
26 #include <trace/events/f2fs.h>
27 #include <trace/events/android_fs.h>
28
29 #define NUM_PREALLOC_POST_READ_CTXS 128
30
31 static struct kmem_cache *bio_post_read_ctx_cache;
32 static mempool_t *bio_post_read_ctx_pool;
33
__is_cp_guaranteed(struct page * page)34 static bool __is_cp_guaranteed(struct page *page)
35 {
36 struct address_space *mapping = page->mapping;
37 struct inode *inode;
38 struct f2fs_sb_info *sbi;
39
40 if (!mapping)
41 return false;
42
43 inode = mapping->host;
44 sbi = F2FS_I_SB(inode);
45
46 if (inode->i_ino == F2FS_META_INO(sbi) ||
47 inode->i_ino == F2FS_NODE_INO(sbi) ||
48 S_ISDIR(inode->i_mode) ||
49 (S_ISREG(inode->i_mode) &&
50 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
51 is_cold_data(page))
52 return true;
53 return false;
54 }
55
__read_io_type(struct page * page)56 static enum count_type __read_io_type(struct page *page)
57 {
58 struct address_space *mapping = page->mapping;
59
60 if (mapping) {
61 struct inode *inode = mapping->host;
62 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
63
64 if (inode->i_ino == F2FS_META_INO(sbi))
65 return F2FS_RD_META;
66
67 if (inode->i_ino == F2FS_NODE_INO(sbi))
68 return F2FS_RD_NODE;
69 }
70 return F2FS_RD_DATA;
71 }
72
73 /* postprocessing steps for read bios */
74 enum bio_post_read_step {
75 STEP_INITIAL = 0,
76 STEP_DECRYPT,
77 };
78
79 struct bio_post_read_ctx {
80 struct bio *bio;
81 struct work_struct work;
82 unsigned int cur_step;
83 unsigned int enabled_steps;
84 };
85
__read_end_io(struct bio * bio)86 static void __read_end_io(struct bio *bio)
87 {
88 struct page *page;
89 struct bio_vec *bv;
90 int i;
91
92 bio_for_each_segment_all(bv, bio, i) {
93 page = bv->bv_page;
94
95 /* PG_error was set if any post_read step failed */
96 if (bio->bi_status || PageError(page)) {
97 ClearPageUptodate(page);
98 /* will re-read again later */
99 ClearPageError(page);
100 } else {
101 SetPageUptodate(page);
102 }
103 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
104 unlock_page(page);
105 }
106 if (bio->bi_private)
107 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
108 bio_put(bio);
109 }
110
111 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
112
decrypt_work(struct work_struct * work)113 static void decrypt_work(struct work_struct *work)
114 {
115 struct bio_post_read_ctx *ctx =
116 container_of(work, struct bio_post_read_ctx, work);
117
118 fscrypt_decrypt_bio(ctx->bio);
119
120 bio_post_read_processing(ctx);
121 }
122
bio_post_read_processing(struct bio_post_read_ctx * ctx)123 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
124 {
125 switch (++ctx->cur_step) {
126 case STEP_DECRYPT:
127 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
128 INIT_WORK(&ctx->work, decrypt_work);
129 fscrypt_enqueue_decrypt_work(&ctx->work);
130 return;
131 }
132 ctx->cur_step++;
133 /* fall-through */
134 default:
135 __read_end_io(ctx->bio);
136 }
137 }
138
f2fs_bio_post_read_required(struct bio * bio)139 static bool f2fs_bio_post_read_required(struct bio *bio)
140 {
141 return bio->bi_private && !bio->bi_status;
142 }
143
f2fs_read_end_io(struct bio * bio)144 static void f2fs_read_end_io(struct bio *bio)
145 {
146 struct page *first_page = bio->bi_io_vec[0].bv_page;
147
148 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_READ_IO)) {
149 f2fs_show_injection_info(FAULT_READ_IO);
150 bio->bi_status = BLK_STS_IOERR;
151 }
152
153 if (f2fs_bio_post_read_required(bio)) {
154 struct bio_post_read_ctx *ctx = bio->bi_private;
155
156 ctx->cur_step = STEP_INITIAL;
157 bio_post_read_processing(ctx);
158 return;
159 }
160
161 if (first_page != NULL &&
162 __read_io_type(first_page) == F2FS_RD_DATA) {
163 trace_android_fs_dataread_end(first_page->mapping->host,
164 page_offset(first_page),
165 bio->bi_iter.bi_size);
166 }
167
168 __read_end_io(bio);
169 }
170
f2fs_write_end_io(struct bio * bio)171 static void f2fs_write_end_io(struct bio *bio)
172 {
173 struct f2fs_sb_info *sbi = bio->bi_private;
174 struct bio_vec *bvec;
175 int i;
176
177 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
178 f2fs_show_injection_info(FAULT_WRITE_IO);
179 bio->bi_status = BLK_STS_IOERR;
180 }
181
182 bio_for_each_segment_all(bvec, bio, i) {
183 struct page *page = bvec->bv_page;
184 enum count_type type = WB_DATA_TYPE(page);
185
186 if (IS_DUMMY_WRITTEN_PAGE(page)) {
187 set_page_private(page, (unsigned long)NULL);
188 ClearPagePrivate(page);
189 unlock_page(page);
190 mempool_free(page, sbi->write_io_dummy);
191
192 if (unlikely(bio->bi_status))
193 f2fs_stop_checkpoint(sbi, true);
194 continue;
195 }
196
197 fscrypt_pullback_bio_page(&page, true);
198
199 if (unlikely(bio->bi_status)) {
200 mapping_set_error(page->mapping, -EIO);
201 if (type == F2FS_WB_CP_DATA)
202 f2fs_stop_checkpoint(sbi, true);
203 }
204
205 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
206 page->index != nid_of_node(page));
207
208 dec_page_count(sbi, type);
209 if (f2fs_in_warm_node_list(sbi, page))
210 f2fs_del_fsync_node_entry(sbi, page);
211 clear_cold_data(page);
212 end_page_writeback(page);
213 }
214 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
215 wq_has_sleeper(&sbi->cp_wait))
216 wake_up(&sbi->cp_wait);
217
218 bio_put(bio);
219 }
220
221 /*
222 * Return true, if pre_bio's bdev is same as its target device.
223 */
f2fs_target_device(struct f2fs_sb_info * sbi,block_t blk_addr,struct bio * bio)224 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
225 block_t blk_addr, struct bio *bio)
226 {
227 struct block_device *bdev = sbi->sb->s_bdev;
228 int i;
229
230 if (f2fs_is_multi_device(sbi)) {
231 for (i = 0; i < sbi->s_ndevs; i++) {
232 if (FDEV(i).start_blk <= blk_addr &&
233 FDEV(i).end_blk >= blk_addr) {
234 blk_addr -= FDEV(i).start_blk;
235 bdev = FDEV(i).bdev;
236 break;
237 }
238 }
239 }
240 if (bio) {
241 bio_set_dev(bio, bdev);
242 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
243 }
244 return bdev;
245 }
246
f2fs_target_device_index(struct f2fs_sb_info * sbi,block_t blkaddr)247 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
248 {
249 int i;
250
251 if (!f2fs_is_multi_device(sbi))
252 return 0;
253
254 for (i = 0; i < sbi->s_ndevs; i++)
255 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
256 return i;
257 return 0;
258 }
259
__same_bdev(struct f2fs_sb_info * sbi,block_t blk_addr,struct bio * bio)260 static bool __same_bdev(struct f2fs_sb_info *sbi,
261 block_t blk_addr, struct bio *bio)
262 {
263 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
264 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
265 }
266
267 /*
268 * Low-level block read/write IO operations.
269 */
__bio_alloc(struct f2fs_sb_info * sbi,block_t blk_addr,struct writeback_control * wbc,int npages,bool is_read,enum page_type type,enum temp_type temp)270 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
271 struct writeback_control *wbc,
272 int npages, bool is_read,
273 enum page_type type, enum temp_type temp)
274 {
275 struct bio *bio;
276
277 bio = f2fs_bio_alloc(sbi, npages, true);
278
279 f2fs_target_device(sbi, blk_addr, bio);
280 if (is_read) {
281 bio->bi_end_io = f2fs_read_end_io;
282 bio->bi_private = NULL;
283 } else {
284 bio->bi_end_io = f2fs_write_end_io;
285 bio->bi_private = sbi;
286 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
287 }
288 if (wbc)
289 wbc_init_bio(wbc, bio);
290
291 return bio;
292 }
293
__submit_bio(struct f2fs_sb_info * sbi,struct bio * bio,enum page_type type)294 static inline void __submit_bio(struct f2fs_sb_info *sbi,
295 struct bio *bio, enum page_type type)
296 {
297 if (!is_read_io(bio_op(bio))) {
298 unsigned int start;
299
300 if (type != DATA && type != NODE)
301 goto submit_io;
302
303 if (test_opt(sbi, LFS) && current->plug)
304 blk_finish_plug(current->plug);
305
306 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
307 start %= F2FS_IO_SIZE(sbi);
308
309 if (start == 0)
310 goto submit_io;
311
312 /* fill dummy pages */
313 for (; start < F2FS_IO_SIZE(sbi); start++) {
314 struct page *page =
315 mempool_alloc(sbi->write_io_dummy,
316 GFP_NOIO | __GFP_NOFAIL);
317 f2fs_bug_on(sbi, !page);
318
319 zero_user_segment(page, 0, PAGE_SIZE);
320 SetPagePrivate(page);
321 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
322 lock_page(page);
323 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
324 f2fs_bug_on(sbi, 1);
325 }
326 /*
327 * In the NODE case, we lose next block address chain. So, we
328 * need to do checkpoint in f2fs_sync_file.
329 */
330 if (type == NODE)
331 set_sbi_flag(sbi, SBI_NEED_CP);
332 }
333 submit_io:
334 if (is_read_io(bio_op(bio)))
335 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
336 else
337 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
338 submit_bio(bio);
339 }
340
__f2fs_submit_read_bio(struct f2fs_sb_info * sbi,struct bio * bio,enum page_type type)341 static void __f2fs_submit_read_bio(struct f2fs_sb_info *sbi,
342 struct bio *bio, enum page_type type)
343 {
344 if (trace_android_fs_dataread_start_enabled() && (type == DATA)) {
345 struct page *first_page = bio->bi_io_vec[0].bv_page;
346
347 if (first_page != NULL &&
348 __read_io_type(first_page) == F2FS_RD_DATA) {
349 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
350
351 path = android_fstrace_get_pathname(pathbuf,
352 MAX_TRACE_PATHBUF_LEN,
353 first_page->mapping->host);
354
355 trace_android_fs_dataread_start(
356 first_page->mapping->host,
357 page_offset(first_page),
358 bio->bi_iter.bi_size,
359 current->pid,
360 path,
361 current->comm);
362 }
363 }
364 __submit_bio(sbi, bio, type);
365 }
366
__submit_merged_bio(struct f2fs_bio_info * io)367 static void __submit_merged_bio(struct f2fs_bio_info *io)
368 {
369 struct f2fs_io_info *fio = &io->fio;
370
371 if (!io->bio)
372 return;
373
374 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
375
376 if (is_read_io(fio->op))
377 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
378 else
379 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
380
381 __submit_bio(io->sbi, io->bio, fio->type);
382 io->bio = NULL;
383 }
384
__has_merged_page(struct f2fs_bio_info * io,struct inode * inode,struct page * page,nid_t ino)385 static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
386 struct page *page, nid_t ino)
387 {
388 struct bio_vec *bvec;
389 struct page *target;
390 int i;
391
392 if (!io->bio)
393 return false;
394
395 if (!inode && !page && !ino)
396 return true;
397
398 bio_for_each_segment_all(bvec, io->bio, i) {
399
400 if (bvec->bv_page->mapping)
401 target = bvec->bv_page;
402 else
403 target = fscrypt_control_page(bvec->bv_page);
404
405 if (inode && inode == target->mapping->host)
406 return true;
407 if (page && page == target)
408 return true;
409 if (ino && ino == ino_of_node(target))
410 return true;
411 }
412
413 return false;
414 }
415
__f2fs_submit_merged_write(struct f2fs_sb_info * sbi,enum page_type type,enum temp_type temp)416 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
417 enum page_type type, enum temp_type temp)
418 {
419 enum page_type btype = PAGE_TYPE_OF_BIO(type);
420 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
421
422 down_write(&io->io_rwsem);
423
424 /* change META to META_FLUSH in the checkpoint procedure */
425 if (type >= META_FLUSH) {
426 io->fio.type = META_FLUSH;
427 io->fio.op = REQ_OP_WRITE;
428 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
429 if (!test_opt(sbi, NOBARRIER))
430 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
431 }
432 __submit_merged_bio(io);
433 up_write(&io->io_rwsem);
434 }
435
__submit_merged_write_cond(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page,nid_t ino,enum page_type type,bool force)436 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
437 struct inode *inode, struct page *page,
438 nid_t ino, enum page_type type, bool force)
439 {
440 enum temp_type temp;
441 bool ret = true;
442
443 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
444 if (!force) {
445 enum page_type btype = PAGE_TYPE_OF_BIO(type);
446 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
447
448 down_read(&io->io_rwsem);
449 ret = __has_merged_page(io, inode, page, ino);
450 up_read(&io->io_rwsem);
451 }
452 if (ret)
453 __f2fs_submit_merged_write(sbi, type, temp);
454
455 /* TODO: use HOT temp only for meta pages now. */
456 if (type >= META)
457 break;
458 }
459 }
460
f2fs_submit_merged_write(struct f2fs_sb_info * sbi,enum page_type type)461 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
462 {
463 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
464 }
465
f2fs_submit_merged_write_cond(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page,nid_t ino,enum page_type type)466 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
467 struct inode *inode, struct page *page,
468 nid_t ino, enum page_type type)
469 {
470 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
471 }
472
f2fs_flush_merged_writes(struct f2fs_sb_info * sbi)473 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
474 {
475 f2fs_submit_merged_write(sbi, DATA);
476 f2fs_submit_merged_write(sbi, NODE);
477 f2fs_submit_merged_write(sbi, META);
478 }
479
480 /*
481 * Fill the locked page with data located in the block address.
482 * A caller needs to unlock the page on failure.
483 */
f2fs_submit_page_bio(struct f2fs_io_info * fio)484 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
485 {
486 struct bio *bio;
487 struct page *page = fio->encrypted_page ?
488 fio->encrypted_page : fio->page;
489
490 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
491 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
492 return -EFSCORRUPTED;
493
494 trace_f2fs_submit_page_bio(page, fio);
495 f2fs_trace_ios(fio, 0);
496
497 /* Allocate a new bio */
498 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
499 1, is_read_io(fio->op), fio->type, fio->temp);
500
501 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
502 bio_put(bio);
503 return -EFAULT;
504 }
505
506 if (fio->io_wbc && !is_read_io(fio->op))
507 wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
508
509 bio_set_op_attrs(bio, fio->op, fio->op_flags);
510
511 inc_page_count(fio->sbi, is_read_io(fio->op) ?
512 __read_io_type(page): WB_DATA_TYPE(fio->page));
513
514 __f2fs_submit_read_bio(fio->sbi, bio, fio->type);
515 return 0;
516 }
517
f2fs_submit_page_write(struct f2fs_io_info * fio)518 void f2fs_submit_page_write(struct f2fs_io_info *fio)
519 {
520 struct f2fs_sb_info *sbi = fio->sbi;
521 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
522 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
523 struct page *bio_page;
524
525 f2fs_bug_on(sbi, is_read_io(fio->op));
526
527 down_write(&io->io_rwsem);
528 next:
529 if (fio->in_list) {
530 spin_lock(&io->io_lock);
531 if (list_empty(&io->io_list)) {
532 spin_unlock(&io->io_lock);
533 goto out;
534 }
535 fio = list_first_entry(&io->io_list,
536 struct f2fs_io_info, list);
537 list_del(&fio->list);
538 spin_unlock(&io->io_lock);
539 }
540
541 if (__is_valid_data_blkaddr(fio->old_blkaddr))
542 verify_block_addr(fio, fio->old_blkaddr);
543 verify_block_addr(fio, fio->new_blkaddr);
544
545 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
546
547 /* set submitted = true as a return value */
548 fio->submitted = true;
549
550 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
551
552 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
553 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
554 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
555 __submit_merged_bio(io);
556 alloc_new:
557 if (io->bio == NULL) {
558 if ((fio->type == DATA || fio->type == NODE) &&
559 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
560 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
561 fio->retry = true;
562 goto skip;
563 }
564 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
565 BIO_MAX_PAGES, false,
566 fio->type, fio->temp);
567 io->fio = *fio;
568 }
569
570 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
571 __submit_merged_bio(io);
572 goto alloc_new;
573 }
574
575 if (fio->io_wbc)
576 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
577
578 io->last_block_in_bio = fio->new_blkaddr;
579 f2fs_trace_ios(fio, 0);
580
581 trace_f2fs_submit_page_write(fio->page, fio);
582 skip:
583 if (fio->in_list)
584 goto next;
585 out:
586 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
587 f2fs_is_checkpoint_ready(sbi))
588 __submit_merged_bio(io);
589 up_write(&io->io_rwsem);
590 }
591
f2fs_grab_read_bio(struct inode * inode,block_t blkaddr,unsigned nr_pages,unsigned op_flag)592 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
593 unsigned nr_pages, unsigned op_flag)
594 {
595 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
596 struct bio *bio;
597 struct bio_post_read_ctx *ctx;
598 unsigned int post_read_steps = 0;
599
600 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
601 return ERR_PTR(-EFAULT);
602
603 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
604 if (!bio)
605 return ERR_PTR(-ENOMEM);
606 f2fs_target_device(sbi, blkaddr, bio);
607 bio->bi_end_io = f2fs_read_end_io;
608 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
609
610 if (f2fs_encrypted_file(inode))
611 post_read_steps |= 1 << STEP_DECRYPT;
612 if (post_read_steps) {
613 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
614 if (!ctx) {
615 bio_put(bio);
616 return ERR_PTR(-ENOMEM);
617 }
618 ctx->bio = bio;
619 ctx->enabled_steps = post_read_steps;
620 bio->bi_private = ctx;
621 }
622
623 return bio;
624 }
625
626 /* This can handle encryption stuffs */
f2fs_submit_page_read(struct inode * inode,struct page * page,block_t blkaddr)627 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
628 block_t blkaddr)
629 {
630 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
631
632 if (IS_ERR(bio))
633 return PTR_ERR(bio);
634
635 /* wait for GCed page writeback via META_MAPPING */
636 f2fs_wait_on_block_writeback(inode, blkaddr);
637
638 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
639 bio_put(bio);
640 return -EFAULT;
641 }
642 ClearPageError(page);
643 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
644 __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
645 return 0;
646 }
647
__set_data_blkaddr(struct dnode_of_data * dn)648 static void __set_data_blkaddr(struct dnode_of_data *dn)
649 {
650 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
651 __le32 *addr_array;
652 int base = 0;
653
654 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
655 base = get_extra_isize(dn->inode);
656
657 /* Get physical address of data block */
658 addr_array = blkaddr_in_node(rn);
659 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
660 }
661
662 /*
663 * Lock ordering for the change of data block address:
664 * ->data_page
665 * ->node_page
666 * update block addresses in the node page
667 */
f2fs_set_data_blkaddr(struct dnode_of_data * dn)668 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
669 {
670 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
671 __set_data_blkaddr(dn);
672 if (set_page_dirty(dn->node_page))
673 dn->node_changed = true;
674 }
675
f2fs_update_data_blkaddr(struct dnode_of_data * dn,block_t blkaddr)676 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
677 {
678 dn->data_blkaddr = blkaddr;
679 f2fs_set_data_blkaddr(dn);
680 f2fs_update_extent_cache(dn);
681 }
682
683 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
f2fs_reserve_new_blocks(struct dnode_of_data * dn,blkcnt_t count)684 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
685 {
686 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
687 int err;
688
689 if (!count)
690 return 0;
691
692 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
693 return -EPERM;
694 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
695 return err;
696
697 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
698 dn->ofs_in_node, count);
699
700 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
701
702 for (; count > 0; dn->ofs_in_node++) {
703 block_t blkaddr = datablock_addr(dn->inode,
704 dn->node_page, dn->ofs_in_node);
705 if (blkaddr == NULL_ADDR) {
706 dn->data_blkaddr = NEW_ADDR;
707 __set_data_blkaddr(dn);
708 count--;
709 }
710 }
711
712 if (set_page_dirty(dn->node_page))
713 dn->node_changed = true;
714 return 0;
715 }
716
717 /* Should keep dn->ofs_in_node unchanged */
f2fs_reserve_new_block(struct dnode_of_data * dn)718 int f2fs_reserve_new_block(struct dnode_of_data *dn)
719 {
720 unsigned int ofs_in_node = dn->ofs_in_node;
721 int ret;
722
723 ret = f2fs_reserve_new_blocks(dn, 1);
724 dn->ofs_in_node = ofs_in_node;
725 return ret;
726 }
727
f2fs_reserve_block(struct dnode_of_data * dn,pgoff_t index)728 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
729 {
730 bool need_put = dn->inode_page ? false : true;
731 int err;
732
733 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
734 if (err)
735 return err;
736
737 if (dn->data_blkaddr == NULL_ADDR)
738 err = f2fs_reserve_new_block(dn);
739 if (err || need_put)
740 f2fs_put_dnode(dn);
741 return err;
742 }
743
f2fs_get_block(struct dnode_of_data * dn,pgoff_t index)744 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
745 {
746 struct extent_info ei = {0,0,0};
747 struct inode *inode = dn->inode;
748
749 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
750 dn->data_blkaddr = ei.blk + index - ei.fofs;
751 return 0;
752 }
753
754 return f2fs_reserve_block(dn, index);
755 }
756
f2fs_get_read_data_page(struct inode * inode,pgoff_t index,int op_flags,bool for_write)757 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
758 int op_flags, bool for_write)
759 {
760 struct address_space *mapping = inode->i_mapping;
761 struct dnode_of_data dn;
762 struct page *page;
763 struct extent_info ei = {0,0,0};
764 int err;
765
766 page = f2fs_grab_cache_page(mapping, index, for_write);
767 if (!page)
768 return ERR_PTR(-ENOMEM);
769
770 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
771 dn.data_blkaddr = ei.blk + index - ei.fofs;
772 goto got_it;
773 }
774
775 set_new_dnode(&dn, inode, NULL, NULL, 0);
776 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
777 if (err)
778 goto put_err;
779 f2fs_put_dnode(&dn);
780
781 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
782 err = -ENOENT;
783 goto put_err;
784 }
785 got_it:
786 if (PageUptodate(page)) {
787 unlock_page(page);
788 return page;
789 }
790
791 /*
792 * A new dentry page is allocated but not able to be written, since its
793 * new inode page couldn't be allocated due to -ENOSPC.
794 * In such the case, its blkaddr can be remained as NEW_ADDR.
795 * see, f2fs_add_link -> f2fs_get_new_data_page ->
796 * f2fs_init_inode_metadata.
797 */
798 if (dn.data_blkaddr == NEW_ADDR) {
799 zero_user_segment(page, 0, PAGE_SIZE);
800 if (!PageUptodate(page))
801 SetPageUptodate(page);
802 unlock_page(page);
803 return page;
804 }
805
806 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
807 if (err)
808 goto put_err;
809 return page;
810
811 put_err:
812 f2fs_put_page(page, 1);
813 return ERR_PTR(err);
814 }
815
f2fs_find_data_page(struct inode * inode,pgoff_t index)816 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
817 {
818 struct address_space *mapping = inode->i_mapping;
819 struct page *page;
820
821 page = find_get_page(mapping, index);
822 if (page && PageUptodate(page))
823 return page;
824 f2fs_put_page(page, 0);
825
826 page = f2fs_get_read_data_page(inode, index, 0, false);
827 if (IS_ERR(page))
828 return page;
829
830 if (PageUptodate(page))
831 return page;
832
833 wait_on_page_locked(page);
834 if (unlikely(!PageUptodate(page))) {
835 f2fs_put_page(page, 0);
836 return ERR_PTR(-EIO);
837 }
838 return page;
839 }
840
841 /*
842 * If it tries to access a hole, return an error.
843 * Because, the callers, functions in dir.c and GC, should be able to know
844 * whether this page exists or not.
845 */
f2fs_get_lock_data_page(struct inode * inode,pgoff_t index,bool for_write)846 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
847 bool for_write)
848 {
849 struct address_space *mapping = inode->i_mapping;
850 struct page *page;
851 repeat:
852 page = f2fs_get_read_data_page(inode, index, 0, for_write);
853 if (IS_ERR(page))
854 return page;
855
856 /* wait for read completion */
857 lock_page(page);
858 if (unlikely(page->mapping != mapping)) {
859 f2fs_put_page(page, 1);
860 goto repeat;
861 }
862 if (unlikely(!PageUptodate(page))) {
863 f2fs_put_page(page, 1);
864 return ERR_PTR(-EIO);
865 }
866 return page;
867 }
868
869 /*
870 * Caller ensures that this data page is never allocated.
871 * A new zero-filled data page is allocated in the page cache.
872 *
873 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
874 * f2fs_unlock_op().
875 * Note that, ipage is set only by make_empty_dir, and if any error occur,
876 * ipage should be released by this function.
877 */
f2fs_get_new_data_page(struct inode * inode,struct page * ipage,pgoff_t index,bool new_i_size)878 struct page *f2fs_get_new_data_page(struct inode *inode,
879 struct page *ipage, pgoff_t index, bool new_i_size)
880 {
881 struct address_space *mapping = inode->i_mapping;
882 struct page *page;
883 struct dnode_of_data dn;
884 int err;
885
886 page = f2fs_grab_cache_page(mapping, index, true);
887 if (!page) {
888 /*
889 * before exiting, we should make sure ipage will be released
890 * if any error occur.
891 */
892 f2fs_put_page(ipage, 1);
893 return ERR_PTR(-ENOMEM);
894 }
895
896 set_new_dnode(&dn, inode, ipage, NULL, 0);
897 err = f2fs_reserve_block(&dn, index);
898 if (err) {
899 f2fs_put_page(page, 1);
900 return ERR_PTR(err);
901 }
902 if (!ipage)
903 f2fs_put_dnode(&dn);
904
905 if (PageUptodate(page))
906 goto got_it;
907
908 if (dn.data_blkaddr == NEW_ADDR) {
909 zero_user_segment(page, 0, PAGE_SIZE);
910 if (!PageUptodate(page))
911 SetPageUptodate(page);
912 } else {
913 f2fs_put_page(page, 1);
914
915 /* if ipage exists, blkaddr should be NEW_ADDR */
916 f2fs_bug_on(F2FS_I_SB(inode), ipage);
917 page = f2fs_get_lock_data_page(inode, index, true);
918 if (IS_ERR(page))
919 return page;
920 }
921 got_it:
922 if (new_i_size && i_size_read(inode) <
923 ((loff_t)(index + 1) << PAGE_SHIFT))
924 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
925 return page;
926 }
927
__allocate_data_block(struct dnode_of_data * dn,int seg_type)928 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
929 {
930 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
931 struct f2fs_summary sum;
932 struct node_info ni;
933 block_t old_blkaddr;
934 blkcnt_t count = 1;
935 int err;
936
937 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
938 return -EPERM;
939
940 err = f2fs_get_node_info(sbi, dn->nid, &ni);
941 if (err)
942 return err;
943
944 dn->data_blkaddr = datablock_addr(dn->inode,
945 dn->node_page, dn->ofs_in_node);
946 if (dn->data_blkaddr != NULL_ADDR)
947 goto alloc;
948
949 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
950 return err;
951
952 alloc:
953 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
954 old_blkaddr = dn->data_blkaddr;
955 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
956 &sum, seg_type, NULL, false);
957 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
958 invalidate_mapping_pages(META_MAPPING(sbi),
959 old_blkaddr, old_blkaddr);
960 f2fs_set_data_blkaddr(dn);
961
962 /*
963 * i_size will be updated by direct_IO. Otherwise, we'll get stale
964 * data from unwritten block via dio_read.
965 */
966 return 0;
967 }
968
f2fs_preallocate_blocks(struct kiocb * iocb,struct iov_iter * from)969 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
970 {
971 struct inode *inode = file_inode(iocb->ki_filp);
972 struct f2fs_map_blocks map;
973 int flag;
974 int err = 0;
975 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
976
977 /* convert inline data for Direct I/O*/
978 if (direct_io) {
979 err = f2fs_convert_inline_inode(inode);
980 if (err)
981 return err;
982 }
983
984 if (direct_io && allow_outplace_dio(inode, iocb, from))
985 return 0;
986
987 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
988 return 0;
989
990 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
991 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
992 if (map.m_len > map.m_lblk)
993 map.m_len -= map.m_lblk;
994 else
995 map.m_len = 0;
996
997 map.m_next_pgofs = NULL;
998 map.m_next_extent = NULL;
999 map.m_seg_type = NO_CHECK_TYPE;
1000 map.m_may_create = true;
1001
1002 if (direct_io) {
1003 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1004 flag = f2fs_force_buffered_io(inode, iocb, from) ?
1005 F2FS_GET_BLOCK_PRE_AIO :
1006 F2FS_GET_BLOCK_PRE_DIO;
1007 goto map_blocks;
1008 }
1009 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1010 err = f2fs_convert_inline_inode(inode);
1011 if (err)
1012 return err;
1013 }
1014 if (f2fs_has_inline_data(inode))
1015 return err;
1016
1017 flag = F2FS_GET_BLOCK_PRE_AIO;
1018
1019 map_blocks:
1020 err = f2fs_map_blocks(inode, &map, 1, flag);
1021 if (map.m_len > 0 && err == -ENOSPC) {
1022 if (!direct_io)
1023 set_inode_flag(inode, FI_NO_PREALLOC);
1024 err = 0;
1025 }
1026 return err;
1027 }
1028
__do_map_lock(struct f2fs_sb_info * sbi,int flag,bool lock)1029 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1030 {
1031 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1032 if (lock)
1033 down_read(&sbi->node_change);
1034 else
1035 up_read(&sbi->node_change);
1036 } else {
1037 if (lock)
1038 f2fs_lock_op(sbi);
1039 else
1040 f2fs_unlock_op(sbi);
1041 }
1042 }
1043
1044 /*
1045 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1046 * f2fs_map_blocks structure.
1047 * If original data blocks are allocated, then give them to blockdev.
1048 * Otherwise,
1049 * a. preallocate requested block addresses
1050 * b. do not use extent cache for better performance
1051 * c. give the block addresses to blockdev
1052 */
f2fs_map_blocks(struct inode * inode,struct f2fs_map_blocks * map,int create,int flag)1053 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1054 int create, int flag)
1055 {
1056 unsigned int maxblocks = map->m_len;
1057 struct dnode_of_data dn;
1058 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1059 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1060 pgoff_t pgofs, end_offset, end;
1061 int err = 0, ofs = 1;
1062 unsigned int ofs_in_node, last_ofs_in_node;
1063 blkcnt_t prealloc;
1064 struct extent_info ei = {0,0,0};
1065 block_t blkaddr;
1066 unsigned int start_pgofs;
1067
1068 if (!maxblocks)
1069 return 0;
1070
1071 map->m_len = 0;
1072 map->m_flags = 0;
1073
1074 /* it only supports block size == page size */
1075 pgofs = (pgoff_t)map->m_lblk;
1076 end = pgofs + maxblocks;
1077
1078 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1079 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1080 map->m_may_create)
1081 goto next_dnode;
1082
1083 map->m_pblk = ei.blk + pgofs - ei.fofs;
1084 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1085 map->m_flags = F2FS_MAP_MAPPED;
1086 if (map->m_next_extent)
1087 *map->m_next_extent = pgofs + map->m_len;
1088
1089 /* for hardware encryption, but to avoid potential issue in future */
1090 if (flag == F2FS_GET_BLOCK_DIO)
1091 f2fs_wait_on_block_writeback_range(inode,
1092 map->m_pblk, map->m_len);
1093 goto out;
1094 }
1095
1096 next_dnode:
1097 if (map->m_may_create)
1098 __do_map_lock(sbi, flag, true);
1099
1100 /* When reading holes, we need its node page */
1101 set_new_dnode(&dn, inode, NULL, NULL, 0);
1102 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1103 if (err) {
1104 if (flag == F2FS_GET_BLOCK_BMAP)
1105 map->m_pblk = 0;
1106 if (err == -ENOENT) {
1107 err = 0;
1108 if (map->m_next_pgofs)
1109 *map->m_next_pgofs =
1110 f2fs_get_next_page_offset(&dn, pgofs);
1111 if (map->m_next_extent)
1112 *map->m_next_extent =
1113 f2fs_get_next_page_offset(&dn, pgofs);
1114 }
1115 goto unlock_out;
1116 }
1117
1118 start_pgofs = pgofs;
1119 prealloc = 0;
1120 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1121 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1122
1123 next_block:
1124 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1125
1126 if (__is_valid_data_blkaddr(blkaddr) &&
1127 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1128 err = -EFSCORRUPTED;
1129 goto sync_out;
1130 }
1131
1132 if (is_valid_data_blkaddr(sbi, blkaddr)) {
1133 /* use out-place-update for driect IO under LFS mode */
1134 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1135 map->m_may_create) {
1136 err = __allocate_data_block(&dn, map->m_seg_type);
1137 if (!err) {
1138 blkaddr = dn.data_blkaddr;
1139 set_inode_flag(inode, FI_APPEND_WRITE);
1140 }
1141 }
1142 } else {
1143 if (create) {
1144 if (unlikely(f2fs_cp_error(sbi))) {
1145 err = -EIO;
1146 goto sync_out;
1147 }
1148 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1149 if (blkaddr == NULL_ADDR) {
1150 prealloc++;
1151 last_ofs_in_node = dn.ofs_in_node;
1152 }
1153 } else {
1154 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1155 flag != F2FS_GET_BLOCK_DIO);
1156 err = __allocate_data_block(&dn,
1157 map->m_seg_type);
1158 if (!err)
1159 set_inode_flag(inode, FI_APPEND_WRITE);
1160 }
1161 if (err)
1162 goto sync_out;
1163 map->m_flags |= F2FS_MAP_NEW;
1164 blkaddr = dn.data_blkaddr;
1165 } else {
1166 if (flag == F2FS_GET_BLOCK_BMAP) {
1167 map->m_pblk = 0;
1168 goto sync_out;
1169 }
1170 if (flag == F2FS_GET_BLOCK_PRECACHE)
1171 goto sync_out;
1172 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1173 blkaddr == NULL_ADDR) {
1174 if (map->m_next_pgofs)
1175 *map->m_next_pgofs = pgofs + 1;
1176 goto sync_out;
1177 }
1178 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1179 /* for defragment case */
1180 if (map->m_next_pgofs)
1181 *map->m_next_pgofs = pgofs + 1;
1182 goto sync_out;
1183 }
1184 }
1185 }
1186
1187 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1188 goto skip;
1189
1190 if (map->m_len == 0) {
1191 /* preallocated unwritten block should be mapped for fiemap. */
1192 if (blkaddr == NEW_ADDR)
1193 map->m_flags |= F2FS_MAP_UNWRITTEN;
1194 map->m_flags |= F2FS_MAP_MAPPED;
1195
1196 map->m_pblk = blkaddr;
1197 map->m_len = 1;
1198 } else if ((map->m_pblk != NEW_ADDR &&
1199 blkaddr == (map->m_pblk + ofs)) ||
1200 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1201 flag == F2FS_GET_BLOCK_PRE_DIO) {
1202 ofs++;
1203 map->m_len++;
1204 } else {
1205 goto sync_out;
1206 }
1207
1208 skip:
1209 dn.ofs_in_node++;
1210 pgofs++;
1211
1212 /* preallocate blocks in batch for one dnode page */
1213 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1214 (pgofs == end || dn.ofs_in_node == end_offset)) {
1215
1216 dn.ofs_in_node = ofs_in_node;
1217 err = f2fs_reserve_new_blocks(&dn, prealloc);
1218 if (err)
1219 goto sync_out;
1220
1221 map->m_len += dn.ofs_in_node - ofs_in_node;
1222 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1223 err = -ENOSPC;
1224 goto sync_out;
1225 }
1226 dn.ofs_in_node = end_offset;
1227 }
1228
1229 if (pgofs >= end)
1230 goto sync_out;
1231 else if (dn.ofs_in_node < end_offset)
1232 goto next_block;
1233
1234 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1235 if (map->m_flags & F2FS_MAP_MAPPED) {
1236 unsigned int ofs = start_pgofs - map->m_lblk;
1237
1238 f2fs_update_extent_cache_range(&dn,
1239 start_pgofs, map->m_pblk + ofs,
1240 map->m_len - ofs);
1241 }
1242 }
1243
1244 f2fs_put_dnode(&dn);
1245
1246 if (map->m_may_create) {
1247 __do_map_lock(sbi, flag, false);
1248 f2fs_balance_fs(sbi, dn.node_changed);
1249 }
1250 goto next_dnode;
1251
1252 sync_out:
1253
1254 /* for hardware encryption, but to avoid potential issue in future */
1255 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1256 f2fs_wait_on_block_writeback_range(inode,
1257 map->m_pblk, map->m_len);
1258
1259 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1260 if (map->m_flags & F2FS_MAP_MAPPED) {
1261 unsigned int ofs = start_pgofs - map->m_lblk;
1262
1263 f2fs_update_extent_cache_range(&dn,
1264 start_pgofs, map->m_pblk + ofs,
1265 map->m_len - ofs);
1266 }
1267 if (map->m_next_extent)
1268 *map->m_next_extent = pgofs + 1;
1269 }
1270 f2fs_put_dnode(&dn);
1271 unlock_out:
1272 if (map->m_may_create) {
1273 __do_map_lock(sbi, flag, false);
1274 f2fs_balance_fs(sbi, dn.node_changed);
1275 }
1276 out:
1277 trace_f2fs_map_blocks(inode, map, err);
1278 return err;
1279 }
1280
f2fs_overwrite_io(struct inode * inode,loff_t pos,size_t len)1281 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1282 {
1283 struct f2fs_map_blocks map;
1284 block_t last_lblk;
1285 int err;
1286
1287 if (pos + len > i_size_read(inode))
1288 return false;
1289
1290 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1291 map.m_next_pgofs = NULL;
1292 map.m_next_extent = NULL;
1293 map.m_seg_type = NO_CHECK_TYPE;
1294 map.m_may_create = false;
1295 last_lblk = F2FS_BLK_ALIGN(pos + len);
1296
1297 while (map.m_lblk < last_lblk) {
1298 map.m_len = last_lblk - map.m_lblk;
1299 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1300 if (err || map.m_len == 0)
1301 return false;
1302 map.m_lblk += map.m_len;
1303 }
1304 return true;
1305 }
1306
__get_data_block(struct inode * inode,sector_t iblock,struct buffer_head * bh,int create,int flag,pgoff_t * next_pgofs,int seg_type,bool may_write)1307 static int __get_data_block(struct inode *inode, sector_t iblock,
1308 struct buffer_head *bh, int create, int flag,
1309 pgoff_t *next_pgofs, int seg_type, bool may_write)
1310 {
1311 struct f2fs_map_blocks map;
1312 int err;
1313
1314 map.m_lblk = iblock;
1315 map.m_len = bh->b_size >> inode->i_blkbits;
1316 map.m_next_pgofs = next_pgofs;
1317 map.m_next_extent = NULL;
1318 map.m_seg_type = seg_type;
1319 map.m_may_create = may_write;
1320
1321 err = f2fs_map_blocks(inode, &map, create, flag);
1322 if (!err) {
1323 map_bh(bh, inode->i_sb, map.m_pblk);
1324 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1325 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1326 }
1327 return err;
1328 }
1329
get_data_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create,int flag,pgoff_t * next_pgofs)1330 static int get_data_block(struct inode *inode, sector_t iblock,
1331 struct buffer_head *bh_result, int create, int flag,
1332 pgoff_t *next_pgofs)
1333 {
1334 return __get_data_block(inode, iblock, bh_result, create,
1335 flag, next_pgofs,
1336 NO_CHECK_TYPE, create);
1337 }
1338
get_data_block_dio_write(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1339 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1340 struct buffer_head *bh_result, int create)
1341 {
1342 return __get_data_block(inode, iblock, bh_result, create,
1343 F2FS_GET_BLOCK_DIO, NULL,
1344 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1345 true);
1346 }
1347
get_data_block_dio(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1348 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1349 struct buffer_head *bh_result, int create)
1350 {
1351 return __get_data_block(inode, iblock, bh_result, create,
1352 F2FS_GET_BLOCK_DIO, NULL,
1353 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1354 false);
1355 }
1356
get_data_block_bmap(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1357 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1358 struct buffer_head *bh_result, int create)
1359 {
1360 /* Block number less than F2FS MAX BLOCKS */
1361 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1362 return -EFBIG;
1363
1364 return __get_data_block(inode, iblock, bh_result, create,
1365 F2FS_GET_BLOCK_BMAP, NULL,
1366 NO_CHECK_TYPE, create);
1367 }
1368
logical_to_blk(struct inode * inode,loff_t offset)1369 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1370 {
1371 return (offset >> inode->i_blkbits);
1372 }
1373
blk_to_logical(struct inode * inode,sector_t blk)1374 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1375 {
1376 return (blk << inode->i_blkbits);
1377 }
1378
f2fs_xattr_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo)1379 static int f2fs_xattr_fiemap(struct inode *inode,
1380 struct fiemap_extent_info *fieinfo)
1381 {
1382 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1383 struct page *page;
1384 struct node_info ni;
1385 __u64 phys = 0, len;
1386 __u32 flags;
1387 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1388 int err = 0;
1389
1390 if (f2fs_has_inline_xattr(inode)) {
1391 int offset;
1392
1393 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1394 inode->i_ino, false);
1395 if (!page)
1396 return -ENOMEM;
1397
1398 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1399 if (err) {
1400 f2fs_put_page(page, 1);
1401 return err;
1402 }
1403
1404 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1405 offset = offsetof(struct f2fs_inode, i_addr) +
1406 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1407 get_inline_xattr_addrs(inode));
1408
1409 phys += offset;
1410 len = inline_xattr_size(inode);
1411
1412 f2fs_put_page(page, 1);
1413
1414 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1415
1416 if (!xnid)
1417 flags |= FIEMAP_EXTENT_LAST;
1418
1419 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1420 if (err || err == 1)
1421 return err;
1422 }
1423
1424 if (xnid) {
1425 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1426 if (!page)
1427 return -ENOMEM;
1428
1429 err = f2fs_get_node_info(sbi, xnid, &ni);
1430 if (err) {
1431 f2fs_put_page(page, 1);
1432 return err;
1433 }
1434
1435 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1436 len = inode->i_sb->s_blocksize;
1437
1438 f2fs_put_page(page, 1);
1439
1440 flags = FIEMAP_EXTENT_LAST;
1441 }
1442
1443 if (phys)
1444 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1445
1446 return (err < 0 ? err : 0);
1447 }
1448
f2fs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)1449 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1450 u64 start, u64 len)
1451 {
1452 struct buffer_head map_bh;
1453 sector_t start_blk, last_blk;
1454 pgoff_t next_pgofs;
1455 u64 logical = 0, phys = 0, size = 0;
1456 u32 flags = 0;
1457 int ret = 0;
1458
1459 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1460 ret = f2fs_precache_extents(inode);
1461 if (ret)
1462 return ret;
1463 }
1464
1465 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1466 if (ret)
1467 return ret;
1468
1469 inode_lock(inode);
1470
1471 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1472 ret = f2fs_xattr_fiemap(inode, fieinfo);
1473 goto out;
1474 }
1475
1476 if (f2fs_has_inline_data(inode)) {
1477 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1478 if (ret != -EAGAIN)
1479 goto out;
1480 }
1481
1482 if (logical_to_blk(inode, len) == 0)
1483 len = blk_to_logical(inode, 1);
1484
1485 start_blk = logical_to_blk(inode, start);
1486 last_blk = logical_to_blk(inode, start + len - 1);
1487
1488 next:
1489 memset(&map_bh, 0, sizeof(struct buffer_head));
1490 map_bh.b_size = len;
1491
1492 ret = get_data_block(inode, start_blk, &map_bh, 0,
1493 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1494 if (ret)
1495 goto out;
1496
1497 /* HOLE */
1498 if (!buffer_mapped(&map_bh)) {
1499 start_blk = next_pgofs;
1500
1501 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1502 F2FS_I_SB(inode)->max_file_blocks))
1503 goto prep_next;
1504
1505 flags |= FIEMAP_EXTENT_LAST;
1506 }
1507
1508 if (size) {
1509 if (f2fs_encrypted_inode(inode))
1510 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1511
1512 ret = fiemap_fill_next_extent(fieinfo, logical,
1513 phys, size, flags);
1514 }
1515
1516 if (start_blk > last_blk || ret)
1517 goto out;
1518
1519 logical = blk_to_logical(inode, start_blk);
1520 phys = blk_to_logical(inode, map_bh.b_blocknr);
1521 size = map_bh.b_size;
1522 flags = 0;
1523 if (buffer_unwritten(&map_bh))
1524 flags = FIEMAP_EXTENT_UNWRITTEN;
1525
1526 start_blk += logical_to_blk(inode, size);
1527
1528 prep_next:
1529 cond_resched();
1530 if (fatal_signal_pending(current))
1531 ret = -EINTR;
1532 else
1533 goto next;
1534 out:
1535 if (ret == 1)
1536 ret = 0;
1537
1538 inode_unlock(inode);
1539 return ret;
1540 }
1541
1542 /*
1543 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1544 * Major change was from block_size == page_size in f2fs by default.
1545 *
1546 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1547 * this function ever deviates from doing just read-ahead, it should either
1548 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1549 * from read-ahead.
1550 */
f2fs_mpage_readpages(struct address_space * mapping,struct list_head * pages,struct page * page,unsigned nr_pages,bool is_readahead)1551 static int f2fs_mpage_readpages(struct address_space *mapping,
1552 struct list_head *pages, struct page *page,
1553 unsigned nr_pages, bool is_readahead)
1554 {
1555 struct bio *bio = NULL;
1556 sector_t last_block_in_bio = 0;
1557 struct inode *inode = mapping->host;
1558 const unsigned blkbits = inode->i_blkbits;
1559 const unsigned blocksize = 1 << blkbits;
1560 sector_t block_in_file;
1561 sector_t last_block;
1562 sector_t last_block_in_file;
1563 sector_t block_nr;
1564 struct f2fs_map_blocks map;
1565
1566 map.m_pblk = 0;
1567 map.m_lblk = 0;
1568 map.m_len = 0;
1569 map.m_flags = 0;
1570 map.m_next_pgofs = NULL;
1571 map.m_next_extent = NULL;
1572 map.m_seg_type = NO_CHECK_TYPE;
1573 map.m_may_create = false;
1574
1575 for (; nr_pages; nr_pages--) {
1576 if (pages) {
1577 page = list_last_entry(pages, struct page, lru);
1578
1579 prefetchw(&page->flags);
1580 list_del(&page->lru);
1581 if (add_to_page_cache_lru(page, mapping,
1582 page->index,
1583 readahead_gfp_mask(mapping)))
1584 goto next_page;
1585 }
1586
1587 block_in_file = (sector_t)page->index;
1588 last_block = block_in_file + nr_pages;
1589 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1590 blkbits;
1591 if (last_block > last_block_in_file)
1592 last_block = last_block_in_file;
1593
1594 /* just zeroing out page which is beyond EOF */
1595 if (block_in_file >= last_block)
1596 goto zero_out;
1597 /*
1598 * Map blocks using the previous result first.
1599 */
1600 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1601 block_in_file > map.m_lblk &&
1602 block_in_file < (map.m_lblk + map.m_len))
1603 goto got_it;
1604
1605 /*
1606 * Then do more f2fs_map_blocks() calls until we are
1607 * done with this page.
1608 */
1609 map.m_lblk = block_in_file;
1610 map.m_len = last_block - block_in_file;
1611
1612 if (f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT))
1613 goto set_error_page;
1614 got_it:
1615 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1616 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1617 SetPageMappedToDisk(page);
1618
1619 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1620 SetPageUptodate(page);
1621 goto confused;
1622 }
1623
1624 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1625 DATA_GENERIC))
1626 goto set_error_page;
1627 } else {
1628 zero_out:
1629 zero_user_segment(page, 0, PAGE_SIZE);
1630 if (!PageUptodate(page))
1631 SetPageUptodate(page);
1632 unlock_page(page);
1633 goto next_page;
1634 }
1635
1636 /*
1637 * This page will go to BIO. Do we need to send this
1638 * BIO off first?
1639 */
1640 if (bio && (last_block_in_bio != block_nr - 1 ||
1641 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
1642 submit_and_realloc:
1643 __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
1644 bio = NULL;
1645 }
1646 if (bio == NULL) {
1647 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1648 is_readahead ? REQ_RAHEAD : 0);
1649 if (IS_ERR(bio)) {
1650 bio = NULL;
1651 goto set_error_page;
1652 }
1653 }
1654
1655 /*
1656 * If the page is under writeback, we need to wait for
1657 * its completion to see the correct decrypted data.
1658 */
1659 f2fs_wait_on_block_writeback(inode, block_nr);
1660
1661 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1662 goto submit_and_realloc;
1663
1664 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1665 ClearPageError(page);
1666 last_block_in_bio = block_nr;
1667 goto next_page;
1668 set_error_page:
1669 SetPageError(page);
1670 zero_user_segment(page, 0, PAGE_SIZE);
1671 unlock_page(page);
1672 goto next_page;
1673 confused:
1674 if (bio) {
1675 __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
1676 bio = NULL;
1677 }
1678 unlock_page(page);
1679 next_page:
1680 if (pages)
1681 put_page(page);
1682 }
1683 BUG_ON(pages && !list_empty(pages));
1684 if (bio)
1685 __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
1686 return 0;
1687 }
1688
f2fs_read_data_page(struct file * file,struct page * page)1689 static int f2fs_read_data_page(struct file *file, struct page *page)
1690 {
1691 struct inode *inode = page->mapping->host;
1692 int ret = -EAGAIN;
1693
1694 trace_f2fs_readpage(page, DATA);
1695
1696 /* If the file has inline data, try to read it directly */
1697 if (f2fs_has_inline_data(inode))
1698 ret = f2fs_read_inline_data(inode, page);
1699 if (ret == -EAGAIN)
1700 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
1701 return ret;
1702 }
1703
f2fs_read_data_pages(struct file * file,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)1704 static int f2fs_read_data_pages(struct file *file,
1705 struct address_space *mapping,
1706 struct list_head *pages, unsigned nr_pages)
1707 {
1708 struct inode *inode = mapping->host;
1709 struct page *page = list_last_entry(pages, struct page, lru);
1710
1711 trace_f2fs_readpages(inode, page, nr_pages);
1712
1713 /* If the file has inline data, skip readpages */
1714 if (f2fs_has_inline_data(inode))
1715 return 0;
1716
1717 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
1718 }
1719
encrypt_one_page(struct f2fs_io_info * fio)1720 static int encrypt_one_page(struct f2fs_io_info *fio)
1721 {
1722 struct inode *inode = fio->page->mapping->host;
1723 struct page *mpage;
1724 gfp_t gfp_flags = GFP_NOFS;
1725
1726 if (!f2fs_encrypted_file(inode))
1727 return 0;
1728
1729 /* wait for GCed page writeback via META_MAPPING */
1730 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
1731
1732 retry_encrypt:
1733 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1734 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1735 if (IS_ERR(fio->encrypted_page)) {
1736 /* flush pending IOs and wait for a while in the ENOMEM case */
1737 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1738 f2fs_flush_merged_writes(fio->sbi);
1739 congestion_wait(BLK_RW_ASYNC, HZ/50);
1740 gfp_flags |= __GFP_NOFAIL;
1741 goto retry_encrypt;
1742 }
1743 return PTR_ERR(fio->encrypted_page);
1744 }
1745
1746 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1747 if (mpage) {
1748 if (PageUptodate(mpage))
1749 memcpy(page_address(mpage),
1750 page_address(fio->encrypted_page), PAGE_SIZE);
1751 f2fs_put_page(mpage, 1);
1752 }
1753 return 0;
1754 }
1755
check_inplace_update_policy(struct inode * inode,struct f2fs_io_info * fio)1756 static inline bool check_inplace_update_policy(struct inode *inode,
1757 struct f2fs_io_info *fio)
1758 {
1759 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1760 unsigned int policy = SM_I(sbi)->ipu_policy;
1761
1762 if (policy & (0x1 << F2FS_IPU_FORCE))
1763 return true;
1764 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
1765 return true;
1766 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1767 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1768 return true;
1769 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
1770 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1771 return true;
1772
1773 /*
1774 * IPU for rewrite async pages
1775 */
1776 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1777 fio && fio->op == REQ_OP_WRITE &&
1778 !(fio->op_flags & REQ_SYNC) &&
1779 !f2fs_encrypted_inode(inode))
1780 return true;
1781
1782 /* this is only set during fdatasync */
1783 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1784 is_inode_flag_set(inode, FI_NEED_IPU))
1785 return true;
1786
1787 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1788 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1789 return true;
1790
1791 return false;
1792 }
1793
f2fs_should_update_inplace(struct inode * inode,struct f2fs_io_info * fio)1794 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1795 {
1796 if (f2fs_is_pinned_file(inode))
1797 return true;
1798
1799 /* if this is cold file, we should overwrite to avoid fragmentation */
1800 if (file_is_cold(inode))
1801 return true;
1802
1803 return check_inplace_update_policy(inode, fio);
1804 }
1805
f2fs_should_update_outplace(struct inode * inode,struct f2fs_io_info * fio)1806 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1807 {
1808 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1809
1810 if (test_opt(sbi, LFS))
1811 return true;
1812 if (S_ISDIR(inode->i_mode))
1813 return true;
1814 if (IS_NOQUOTA(inode))
1815 return true;
1816 if (f2fs_is_atomic_file(inode))
1817 return true;
1818 if (fio) {
1819 if (is_cold_data(fio->page))
1820 return true;
1821 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1822 return true;
1823 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1824 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1825 return true;
1826 }
1827 return false;
1828 }
1829
need_inplace_update(struct f2fs_io_info * fio)1830 static inline bool need_inplace_update(struct f2fs_io_info *fio)
1831 {
1832 struct inode *inode = fio->page->mapping->host;
1833
1834 if (f2fs_should_update_outplace(inode, fio))
1835 return false;
1836
1837 return f2fs_should_update_inplace(inode, fio);
1838 }
1839
f2fs_do_write_data_page(struct f2fs_io_info * fio)1840 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
1841 {
1842 struct page *page = fio->page;
1843 struct inode *inode = page->mapping->host;
1844 struct dnode_of_data dn;
1845 struct extent_info ei = {0,0,0};
1846 struct node_info ni;
1847 bool ipu_force = false;
1848 int err = 0;
1849
1850 set_new_dnode(&dn, inode, NULL, NULL, 0);
1851 if (need_inplace_update(fio) &&
1852 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1853 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1854
1855 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1856 DATA_GENERIC))
1857 return -EFSCORRUPTED;
1858
1859 ipu_force = true;
1860 fio->need_lock = LOCK_DONE;
1861 goto got_it;
1862 }
1863
1864 /* Deadlock due to between page->lock and f2fs_lock_op */
1865 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1866 return -EAGAIN;
1867
1868 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1869 if (err)
1870 goto out;
1871
1872 fio->old_blkaddr = dn.data_blkaddr;
1873
1874 /* This page is already truncated */
1875 if (fio->old_blkaddr == NULL_ADDR) {
1876 ClearPageUptodate(page);
1877 clear_cold_data(page);
1878 goto out_writepage;
1879 }
1880 got_it:
1881 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1882 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1883 DATA_GENERIC)) {
1884 err = -EFSCORRUPTED;
1885 goto out_writepage;
1886 }
1887 /*
1888 * If current allocation needs SSR,
1889 * it had better in-place writes for updated data.
1890 */
1891 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
1892 need_inplace_update(fio))) {
1893 err = encrypt_one_page(fio);
1894 if (err)
1895 goto out_writepage;
1896
1897 set_page_writeback(page);
1898 ClearPageError(page);
1899 f2fs_put_dnode(&dn);
1900 if (fio->need_lock == LOCK_REQ)
1901 f2fs_unlock_op(fio->sbi);
1902 err = f2fs_inplace_write_data(fio);
1903 if (err) {
1904 if (f2fs_encrypted_file(inode))
1905 fscrypt_pullback_bio_page(&fio->encrypted_page,
1906 true);
1907 if (PageWriteback(page))
1908 end_page_writeback(page);
1909 }
1910 trace_f2fs_do_write_data_page(fio->page, IPU);
1911 set_inode_flag(inode, FI_UPDATE_WRITE);
1912 return err;
1913 }
1914
1915 if (fio->need_lock == LOCK_RETRY) {
1916 if (!f2fs_trylock_op(fio->sbi)) {
1917 err = -EAGAIN;
1918 goto out_writepage;
1919 }
1920 fio->need_lock = LOCK_REQ;
1921 }
1922
1923 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1924 if (err)
1925 goto out_writepage;
1926
1927 fio->version = ni.version;
1928
1929 err = encrypt_one_page(fio);
1930 if (err)
1931 goto out_writepage;
1932
1933 set_page_writeback(page);
1934 ClearPageError(page);
1935
1936 /* LFS mode write path */
1937 f2fs_outplace_write_data(&dn, fio);
1938 trace_f2fs_do_write_data_page(page, OPU);
1939 set_inode_flag(inode, FI_APPEND_WRITE);
1940 if (page->index == 0)
1941 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
1942 out_writepage:
1943 f2fs_put_dnode(&dn);
1944 out:
1945 if (fio->need_lock == LOCK_REQ)
1946 f2fs_unlock_op(fio->sbi);
1947 return err;
1948 }
1949
__write_data_page(struct page * page,bool * submitted,struct writeback_control * wbc,enum iostat_type io_type)1950 static int __write_data_page(struct page *page, bool *submitted,
1951 struct writeback_control *wbc,
1952 enum iostat_type io_type)
1953 {
1954 struct inode *inode = page->mapping->host;
1955 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1956 loff_t i_size = i_size_read(inode);
1957 const pgoff_t end_index = ((unsigned long long) i_size)
1958 >> PAGE_SHIFT;
1959 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
1960 unsigned offset = 0;
1961 bool need_balance_fs = false;
1962 int err = 0;
1963 struct f2fs_io_info fio = {
1964 .sbi = sbi,
1965 .ino = inode->i_ino,
1966 .type = DATA,
1967 .op = REQ_OP_WRITE,
1968 .op_flags = wbc_to_write_flags(wbc),
1969 .old_blkaddr = NULL_ADDR,
1970 .page = page,
1971 .encrypted_page = NULL,
1972 .submitted = false,
1973 .need_lock = LOCK_RETRY,
1974 .io_type = io_type,
1975 .io_wbc = wbc,
1976 };
1977
1978 trace_f2fs_writepage(page, DATA);
1979
1980 /* we should bypass data pages to proceed the kworkder jobs */
1981 if (unlikely(f2fs_cp_error(sbi))) {
1982 mapping_set_error(page->mapping, -EIO);
1983 /*
1984 * don't drop any dirty dentry pages for keeping lastest
1985 * directory structure.
1986 */
1987 if (S_ISDIR(inode->i_mode))
1988 goto redirty_out;
1989 goto out;
1990 }
1991
1992 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1993 goto redirty_out;
1994
1995 if (page->index < end_index)
1996 goto write;
1997
1998 /*
1999 * If the offset is out-of-range of file size,
2000 * this page does not have to be written to disk.
2001 */
2002 offset = i_size & (PAGE_SIZE - 1);
2003 if ((page->index >= end_index + 1) || !offset)
2004 goto out;
2005
2006 zero_user_segment(page, offset, PAGE_SIZE);
2007 write:
2008 if (f2fs_is_drop_cache(inode))
2009 goto out;
2010 /* we should not write 0'th page having journal header */
2011 if (f2fs_is_volatile_file(inode) && (!page->index ||
2012 (!wbc->for_reclaim &&
2013 f2fs_available_free_memory(sbi, BASE_CHECK))))
2014 goto redirty_out;
2015
2016 /* Dentry blocks are controlled by checkpoint */
2017 if (S_ISDIR(inode->i_mode)) {
2018 fio.need_lock = LOCK_DONE;
2019 err = f2fs_do_write_data_page(&fio);
2020 goto done;
2021 }
2022
2023 if (!wbc->for_reclaim)
2024 need_balance_fs = true;
2025 else if (has_not_enough_free_secs(sbi, 0, 0))
2026 goto redirty_out;
2027 else
2028 set_inode_flag(inode, FI_HOT_DATA);
2029
2030 err = -EAGAIN;
2031 if (f2fs_has_inline_data(inode)) {
2032 err = f2fs_write_inline_data(inode, page);
2033 if (!err)
2034 goto out;
2035 }
2036
2037 if (err == -EAGAIN) {
2038 err = f2fs_do_write_data_page(&fio);
2039 if (err == -EAGAIN) {
2040 fio.need_lock = LOCK_REQ;
2041 err = f2fs_do_write_data_page(&fio);
2042 }
2043 }
2044
2045 if (err) {
2046 file_set_keep_isize(inode);
2047 } else {
2048 down_write(&F2FS_I(inode)->i_sem);
2049 if (F2FS_I(inode)->last_disk_size < psize)
2050 F2FS_I(inode)->last_disk_size = psize;
2051 up_write(&F2FS_I(inode)->i_sem);
2052 }
2053
2054 done:
2055 if (err && err != -ENOENT)
2056 goto redirty_out;
2057
2058 out:
2059 inode_dec_dirty_pages(inode);
2060 if (err) {
2061 ClearPageUptodate(page);
2062 clear_cold_data(page);
2063 }
2064
2065 if (wbc->for_reclaim) {
2066 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2067 clear_inode_flag(inode, FI_HOT_DATA);
2068 f2fs_remove_dirty_inode(inode);
2069 submitted = NULL;
2070 }
2071
2072 unlock_page(page);
2073 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode))
2074 f2fs_balance_fs(sbi, need_balance_fs);
2075
2076 if (unlikely(f2fs_cp_error(sbi))) {
2077 f2fs_submit_merged_write(sbi, DATA);
2078 submitted = NULL;
2079 }
2080
2081 if (submitted)
2082 *submitted = fio.submitted;
2083
2084 return 0;
2085
2086 redirty_out:
2087 redirty_page_for_writepage(wbc, page);
2088 /*
2089 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2090 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2091 * file_write_and_wait_range() will see EIO error, which is critical
2092 * to return value of fsync() followed by atomic_write failure to user.
2093 */
2094 if (!err || wbc->for_reclaim)
2095 return AOP_WRITEPAGE_ACTIVATE;
2096 unlock_page(page);
2097 return err;
2098 }
2099
f2fs_write_data_page(struct page * page,struct writeback_control * wbc)2100 static int f2fs_write_data_page(struct page *page,
2101 struct writeback_control *wbc)
2102 {
2103 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
2104 }
2105
2106 /*
2107 * This function was copied from write_cche_pages from mm/page-writeback.c.
2108 * The major change is making write step of cold data page separately from
2109 * warm/hot data page.
2110 */
f2fs_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)2111 static int f2fs_write_cache_pages(struct address_space *mapping,
2112 struct writeback_control *wbc,
2113 enum iostat_type io_type)
2114 {
2115 int ret = 0;
2116 int done = 0;
2117 struct pagevec pvec;
2118 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2119 int nr_pages;
2120 pgoff_t uninitialized_var(writeback_index);
2121 pgoff_t index;
2122 pgoff_t end; /* Inclusive */
2123 pgoff_t done_index;
2124 int cycled;
2125 int range_whole = 0;
2126 int tag;
2127 int nwritten = 0;
2128
2129 pagevec_init(&pvec, 0);
2130
2131 if (get_dirty_pages(mapping->host) <=
2132 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2133 set_inode_flag(mapping->host, FI_HOT_DATA);
2134 else
2135 clear_inode_flag(mapping->host, FI_HOT_DATA);
2136
2137 if (wbc->range_cyclic) {
2138 writeback_index = mapping->writeback_index; /* prev offset */
2139 index = writeback_index;
2140 if (index == 0)
2141 cycled = 1;
2142 else
2143 cycled = 0;
2144 end = -1;
2145 } else {
2146 index = wbc->range_start >> PAGE_SHIFT;
2147 end = wbc->range_end >> PAGE_SHIFT;
2148 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2149 range_whole = 1;
2150 cycled = 1; /* ignore range_cyclic tests */
2151 }
2152 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2153 tag = PAGECACHE_TAG_TOWRITE;
2154 else
2155 tag = PAGECACHE_TAG_DIRTY;
2156 retry:
2157 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2158 tag_pages_for_writeback(mapping, index, end);
2159 done_index = index;
2160 while (!done && (index <= end)) {
2161 int i;
2162
2163 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2164 tag);
2165 if (nr_pages == 0)
2166 break;
2167
2168 for (i = 0; i < nr_pages; i++) {
2169 struct page *page = pvec.pages[i];
2170 bool submitted = false;
2171
2172 /* give a priority to WB_SYNC threads */
2173 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2174 wbc->sync_mode == WB_SYNC_NONE) {
2175 done = 1;
2176 break;
2177 }
2178
2179 done_index = page->index;
2180 retry_write:
2181 lock_page(page);
2182
2183 if (unlikely(page->mapping != mapping)) {
2184 continue_unlock:
2185 unlock_page(page);
2186 continue;
2187 }
2188
2189 if (!PageDirty(page)) {
2190 /* someone wrote it for us */
2191 goto continue_unlock;
2192 }
2193
2194 if (PageWriteback(page)) {
2195 if (wbc->sync_mode != WB_SYNC_NONE)
2196 f2fs_wait_on_page_writeback(page,
2197 DATA, true, true);
2198 else
2199 goto continue_unlock;
2200 }
2201
2202 if (!clear_page_dirty_for_io(page))
2203 goto continue_unlock;
2204
2205 ret = __write_data_page(page, &submitted, wbc, io_type);
2206 if (unlikely(ret)) {
2207 /*
2208 * keep nr_to_write, since vfs uses this to
2209 * get # of written pages.
2210 */
2211 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2212 unlock_page(page);
2213 ret = 0;
2214 continue;
2215 } else if (ret == -EAGAIN) {
2216 ret = 0;
2217 if (wbc->sync_mode == WB_SYNC_ALL) {
2218 cond_resched();
2219 congestion_wait(BLK_RW_ASYNC,
2220 HZ/50);
2221 goto retry_write;
2222 }
2223 continue;
2224 }
2225 done_index = page->index + 1;
2226 done = 1;
2227 break;
2228 } else if (submitted) {
2229 nwritten++;
2230 }
2231
2232 if (--wbc->nr_to_write <= 0 &&
2233 wbc->sync_mode == WB_SYNC_NONE) {
2234 done = 1;
2235 break;
2236 }
2237 }
2238 pagevec_release(&pvec);
2239 cond_resched();
2240 }
2241
2242 if (!cycled && !done) {
2243 cycled = 1;
2244 index = 0;
2245 end = writeback_index - 1;
2246 goto retry;
2247 }
2248 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2249 mapping->writeback_index = done_index;
2250
2251 if (nwritten)
2252 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2253 NULL, 0, DATA);
2254
2255 return ret;
2256 }
2257
__should_serialize_io(struct inode * inode,struct writeback_control * wbc)2258 static inline bool __should_serialize_io(struct inode *inode,
2259 struct writeback_control *wbc)
2260 {
2261 if (!S_ISREG(inode->i_mode))
2262 return false;
2263 if (IS_NOQUOTA(inode))
2264 return false;
2265 if (wbc->sync_mode != WB_SYNC_ALL)
2266 return true;
2267 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2268 return true;
2269 return false;
2270 }
2271
__f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)2272 static int __f2fs_write_data_pages(struct address_space *mapping,
2273 struct writeback_control *wbc,
2274 enum iostat_type io_type)
2275 {
2276 struct inode *inode = mapping->host;
2277 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2278 struct blk_plug plug;
2279 int ret;
2280 bool locked = false;
2281
2282 /* deal with chardevs and other special file */
2283 if (!mapping->a_ops->writepage)
2284 return 0;
2285
2286 /* skip writing if there is no dirty page in this inode */
2287 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2288 return 0;
2289
2290 /* during POR, we don't need to trigger writepage at all. */
2291 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2292 goto skip_write;
2293
2294 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2295 wbc->sync_mode == WB_SYNC_NONE &&
2296 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2297 f2fs_available_free_memory(sbi, DIRTY_DENTS))
2298 goto skip_write;
2299
2300 /* skip writing during file defragment */
2301 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2302 goto skip_write;
2303
2304 trace_f2fs_writepages(mapping->host, wbc, DATA);
2305
2306 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2307 if (wbc->sync_mode == WB_SYNC_ALL)
2308 atomic_inc(&sbi->wb_sync_req[DATA]);
2309 else if (atomic_read(&sbi->wb_sync_req[DATA]))
2310 goto skip_write;
2311
2312 if (__should_serialize_io(inode, wbc)) {
2313 mutex_lock(&sbi->writepages);
2314 locked = true;
2315 }
2316
2317 blk_start_plug(&plug);
2318 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2319 blk_finish_plug(&plug);
2320
2321 if (locked)
2322 mutex_unlock(&sbi->writepages);
2323
2324 if (wbc->sync_mode == WB_SYNC_ALL)
2325 atomic_dec(&sbi->wb_sync_req[DATA]);
2326 /*
2327 * if some pages were truncated, we cannot guarantee its mapping->host
2328 * to detect pending bios.
2329 */
2330
2331 f2fs_remove_dirty_inode(inode);
2332 return ret;
2333
2334 skip_write:
2335 wbc->pages_skipped += get_dirty_pages(inode);
2336 trace_f2fs_writepages(mapping->host, wbc, DATA);
2337 return 0;
2338 }
2339
f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc)2340 static int f2fs_write_data_pages(struct address_space *mapping,
2341 struct writeback_control *wbc)
2342 {
2343 struct inode *inode = mapping->host;
2344
2345 return __f2fs_write_data_pages(mapping, wbc,
2346 F2FS_I(inode)->cp_task == current ?
2347 FS_CP_DATA_IO : FS_DATA_IO);
2348 }
2349
f2fs_write_failed(struct address_space * mapping,loff_t to)2350 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2351 {
2352 struct inode *inode = mapping->host;
2353 loff_t i_size = i_size_read(inode);
2354
2355 if (to > i_size) {
2356 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2357 down_write(&F2FS_I(inode)->i_mmap_sem);
2358
2359 truncate_pagecache(inode, i_size);
2360 if (!IS_NOQUOTA(inode))
2361 f2fs_truncate_blocks(inode, i_size, true);
2362
2363 up_write(&F2FS_I(inode)->i_mmap_sem);
2364 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2365 }
2366 }
2367
prepare_write_begin(struct f2fs_sb_info * sbi,struct page * page,loff_t pos,unsigned len,block_t * blk_addr,bool * node_changed)2368 static int prepare_write_begin(struct f2fs_sb_info *sbi,
2369 struct page *page, loff_t pos, unsigned len,
2370 block_t *blk_addr, bool *node_changed)
2371 {
2372 struct inode *inode = page->mapping->host;
2373 pgoff_t index = page->index;
2374 struct dnode_of_data dn;
2375 struct page *ipage;
2376 bool locked = false;
2377 struct extent_info ei = {0,0,0};
2378 int err = 0;
2379 int flag;
2380
2381 /*
2382 * we already allocated all the blocks, so we don't need to get
2383 * the block addresses when there is no need to fill the page.
2384 */
2385 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2386 !is_inode_flag_set(inode, FI_NO_PREALLOC))
2387 return 0;
2388
2389 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2390 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2391 flag = F2FS_GET_BLOCK_DEFAULT;
2392 else
2393 flag = F2FS_GET_BLOCK_PRE_AIO;
2394
2395 if (f2fs_has_inline_data(inode) ||
2396 (pos & PAGE_MASK) >= i_size_read(inode)) {
2397 __do_map_lock(sbi, flag, true);
2398 locked = true;
2399 }
2400 restart:
2401 /* check inline_data */
2402 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2403 if (IS_ERR(ipage)) {
2404 err = PTR_ERR(ipage);
2405 goto unlock_out;
2406 }
2407
2408 set_new_dnode(&dn, inode, ipage, ipage, 0);
2409
2410 if (f2fs_has_inline_data(inode)) {
2411 if (pos + len <= MAX_INLINE_DATA(inode)) {
2412 f2fs_do_read_inline_data(page, ipage);
2413 set_inode_flag(inode, FI_DATA_EXIST);
2414 if (inode->i_nlink)
2415 set_inline_node(ipage);
2416 } else {
2417 err = f2fs_convert_inline_page(&dn, page);
2418 if (err)
2419 goto out;
2420 if (dn.data_blkaddr == NULL_ADDR)
2421 err = f2fs_get_block(&dn, index);
2422 }
2423 } else if (locked) {
2424 err = f2fs_get_block(&dn, index);
2425 } else {
2426 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2427 dn.data_blkaddr = ei.blk + index - ei.fofs;
2428 } else {
2429 /* hole case */
2430 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2431 if (err || dn.data_blkaddr == NULL_ADDR) {
2432 f2fs_put_dnode(&dn);
2433 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2434 true);
2435 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
2436 locked = true;
2437 goto restart;
2438 }
2439 }
2440 }
2441
2442 /* convert_inline_page can make node_changed */
2443 *blk_addr = dn.data_blkaddr;
2444 *node_changed = dn.node_changed;
2445 out:
2446 f2fs_put_dnode(&dn);
2447 unlock_out:
2448 if (locked)
2449 __do_map_lock(sbi, flag, false);
2450 return err;
2451 }
2452
f2fs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)2453 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2454 loff_t pos, unsigned len, unsigned flags,
2455 struct page **pagep, void **fsdata)
2456 {
2457 struct inode *inode = mapping->host;
2458 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2459 struct page *page = NULL;
2460 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2461 bool need_balance = false, drop_atomic = false;
2462 block_t blkaddr = NULL_ADDR;
2463 int err = 0;
2464
2465 if (trace_android_fs_datawrite_start_enabled()) {
2466 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
2467
2468 path = android_fstrace_get_pathname(pathbuf,
2469 MAX_TRACE_PATHBUF_LEN,
2470 inode);
2471 trace_android_fs_datawrite_start(inode, pos, len,
2472 current->pid, path,
2473 current->comm);
2474 }
2475 trace_f2fs_write_begin(inode, pos, len, flags);
2476
2477 err = f2fs_is_checkpoint_ready(sbi);
2478 if (err)
2479 goto fail;
2480
2481 if ((f2fs_is_atomic_file(inode) &&
2482 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2483 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2484 err = -ENOMEM;
2485 drop_atomic = true;
2486 goto fail;
2487 }
2488
2489 /*
2490 * We should check this at this moment to avoid deadlock on inode page
2491 * and #0 page. The locking rule for inline_data conversion should be:
2492 * lock_page(page #0) -> lock_page(inode_page)
2493 */
2494 if (index != 0) {
2495 err = f2fs_convert_inline_inode(inode);
2496 if (err)
2497 goto fail;
2498 }
2499 repeat:
2500 /*
2501 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2502 * wait_for_stable_page. Will wait that below with our IO control.
2503 */
2504 page = f2fs_pagecache_get_page(mapping, index,
2505 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2506 if (!page) {
2507 err = -ENOMEM;
2508 goto fail;
2509 }
2510
2511 *pagep = page;
2512
2513 err = prepare_write_begin(sbi, page, pos, len,
2514 &blkaddr, &need_balance);
2515 if (err)
2516 goto fail;
2517
2518 if (need_balance && !IS_NOQUOTA(inode) &&
2519 has_not_enough_free_secs(sbi, 0, 0)) {
2520 unlock_page(page);
2521 f2fs_balance_fs(sbi, true);
2522 lock_page(page);
2523 if (page->mapping != mapping) {
2524 /* The page got truncated from under us */
2525 f2fs_put_page(page, 1);
2526 goto repeat;
2527 }
2528 }
2529
2530 f2fs_wait_on_page_writeback(page, DATA, false, true);
2531
2532 if (len == PAGE_SIZE || PageUptodate(page))
2533 return 0;
2534
2535 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2536 zero_user_segment(page, len, PAGE_SIZE);
2537 return 0;
2538 }
2539
2540 if (blkaddr == NEW_ADDR) {
2541 zero_user_segment(page, 0, PAGE_SIZE);
2542 SetPageUptodate(page);
2543 } else {
2544 err = f2fs_submit_page_read(inode, page, blkaddr);
2545 if (err)
2546 goto fail;
2547
2548 lock_page(page);
2549 if (unlikely(page->mapping != mapping)) {
2550 f2fs_put_page(page, 1);
2551 goto repeat;
2552 }
2553 if (unlikely(!PageUptodate(page))) {
2554 err = -EIO;
2555 goto fail;
2556 }
2557 }
2558 return 0;
2559
2560 fail:
2561 f2fs_put_page(page, 1);
2562 f2fs_write_failed(mapping, pos + len);
2563 if (drop_atomic)
2564 f2fs_drop_inmem_pages_all(sbi, false);
2565 return err;
2566 }
2567
f2fs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)2568 static int f2fs_write_end(struct file *file,
2569 struct address_space *mapping,
2570 loff_t pos, unsigned len, unsigned copied,
2571 struct page *page, void *fsdata)
2572 {
2573 struct inode *inode = page->mapping->host;
2574
2575 trace_android_fs_datawrite_end(inode, pos, len);
2576 trace_f2fs_write_end(inode, pos, len, copied);
2577
2578 /*
2579 * This should be come from len == PAGE_SIZE, and we expect copied
2580 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2581 * let generic_perform_write() try to copy data again through copied=0.
2582 */
2583 if (!PageUptodate(page)) {
2584 if (unlikely(copied != len))
2585 copied = 0;
2586 else
2587 SetPageUptodate(page);
2588 }
2589 if (!copied)
2590 goto unlock_out;
2591
2592 set_page_dirty(page);
2593
2594 if (pos + copied > i_size_read(inode))
2595 f2fs_i_size_write(inode, pos + copied);
2596 unlock_out:
2597 f2fs_put_page(page, 1);
2598 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2599 return copied;
2600 }
2601
check_direct_IO(struct inode * inode,struct iov_iter * iter,loff_t offset)2602 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2603 loff_t offset)
2604 {
2605 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2606 unsigned blkbits = i_blkbits;
2607 unsigned blocksize_mask = (1 << blkbits) - 1;
2608 unsigned long align = offset | iov_iter_alignment(iter);
2609 struct block_device *bdev = inode->i_sb->s_bdev;
2610
2611 if (align & blocksize_mask) {
2612 if (bdev)
2613 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2614 blocksize_mask = (1 << blkbits) - 1;
2615 if (align & blocksize_mask)
2616 return -EINVAL;
2617 return 1;
2618 }
2619 return 0;
2620 }
2621
f2fs_dio_end_io(struct bio * bio)2622 static void f2fs_dio_end_io(struct bio *bio)
2623 {
2624 struct f2fs_private_dio *dio = bio->bi_private;
2625
2626 dec_page_count(F2FS_I_SB(dio->inode),
2627 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2628
2629 bio->bi_private = dio->orig_private;
2630 bio->bi_end_io = dio->orig_end_io;
2631
2632 kvfree(dio);
2633
2634 bio_endio(bio);
2635 }
2636
f2fs_dio_submit_bio(struct bio * bio,struct inode * inode,loff_t file_offset)2637 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2638 loff_t file_offset)
2639 {
2640 struct f2fs_private_dio *dio;
2641 bool write = (bio_op(bio) == REQ_OP_WRITE);
2642
2643 dio = f2fs_kzalloc(F2FS_I_SB(inode),
2644 sizeof(struct f2fs_private_dio), GFP_NOFS);
2645 if (!dio)
2646 goto out;
2647
2648 dio->inode = inode;
2649 dio->orig_end_io = bio->bi_end_io;
2650 dio->orig_private = bio->bi_private;
2651 dio->write = write;
2652
2653 bio->bi_end_io = f2fs_dio_end_io;
2654 bio->bi_private = dio;
2655
2656 inc_page_count(F2FS_I_SB(inode),
2657 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2658
2659 submit_bio(bio);
2660 return;
2661 out:
2662 bio->bi_status = BLK_STS_IOERR;
2663 bio_endio(bio);
2664 }
2665
f2fs_direct_IO(struct kiocb * iocb,struct iov_iter * iter)2666 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2667 {
2668 struct address_space *mapping = iocb->ki_filp->f_mapping;
2669 struct inode *inode = mapping->host;
2670 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2671 struct f2fs_inode_info *fi = F2FS_I(inode);
2672 size_t count = iov_iter_count(iter);
2673 loff_t offset = iocb->ki_pos;
2674 int rw = iov_iter_rw(iter);
2675 int err;
2676 enum rw_hint hint = iocb->ki_hint;
2677 int whint_mode = F2FS_OPTION(sbi).whint_mode;
2678 bool do_opu;
2679
2680 err = check_direct_IO(inode, iter, offset);
2681 if (err)
2682 return err < 0 ? err : 0;
2683
2684 if (f2fs_force_buffered_io(inode, iocb, iter))
2685 return 0;
2686
2687 do_opu = allow_outplace_dio(inode, iocb, iter);
2688
2689 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
2690
2691 if (trace_android_fs_dataread_start_enabled() &&
2692 (rw == READ)) {
2693 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
2694
2695 path = android_fstrace_get_pathname(pathbuf,
2696 MAX_TRACE_PATHBUF_LEN,
2697 inode);
2698 trace_android_fs_dataread_start(inode, offset,
2699 count, current->pid, path,
2700 current->comm);
2701 }
2702 if (trace_android_fs_datawrite_start_enabled() &&
2703 (rw == WRITE)) {
2704 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
2705
2706 path = android_fstrace_get_pathname(pathbuf,
2707 MAX_TRACE_PATHBUF_LEN,
2708 inode);
2709 trace_android_fs_datawrite_start(inode, offset, count,
2710 current->pid, path,
2711 current->comm);
2712 }
2713 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2714 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2715
2716 if (iocb->ki_flags & IOCB_NOWAIT) {
2717 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
2718 iocb->ki_hint = hint;
2719 err = -EAGAIN;
2720 goto out;
2721 }
2722 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
2723 up_read(&fi->i_gc_rwsem[rw]);
2724 iocb->ki_hint = hint;
2725 err = -EAGAIN;
2726 goto out;
2727 }
2728 } else {
2729 down_read(&fi->i_gc_rwsem[rw]);
2730 if (do_opu)
2731 down_read(&fi->i_gc_rwsem[READ]);
2732 }
2733
2734 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
2735 iter, rw == WRITE ? get_data_block_dio_write :
2736 get_data_block_dio, NULL, f2fs_dio_submit_bio,
2737 DIO_LOCKING | DIO_SKIP_HOLES);
2738
2739 if (do_opu)
2740 up_read(&fi->i_gc_rwsem[READ]);
2741
2742 up_read(&fi->i_gc_rwsem[rw]);
2743
2744 if (rw == WRITE) {
2745 if (whint_mode == WHINT_MODE_OFF)
2746 iocb->ki_hint = hint;
2747 if (err > 0) {
2748 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2749 err);
2750 if (!do_opu)
2751 set_inode_flag(inode, FI_UPDATE_WRITE);
2752 } else if (err < 0) {
2753 f2fs_write_failed(mapping, offset + count);
2754 }
2755 }
2756 out:
2757 if (trace_android_fs_dataread_start_enabled() &&
2758 (rw == READ))
2759 trace_android_fs_dataread_end(inode, offset, count);
2760 if (trace_android_fs_datawrite_start_enabled() &&
2761 (rw == WRITE))
2762 trace_android_fs_datawrite_end(inode, offset, count);
2763
2764 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2765
2766 return err;
2767 }
2768
f2fs_invalidate_page(struct page * page,unsigned int offset,unsigned int length)2769 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2770 unsigned int length)
2771 {
2772 struct inode *inode = page->mapping->host;
2773 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2774
2775 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2776 (offset % PAGE_SIZE || length != PAGE_SIZE))
2777 return;
2778
2779 if (PageDirty(page)) {
2780 if (inode->i_ino == F2FS_META_INO(sbi)) {
2781 dec_page_count(sbi, F2FS_DIRTY_META);
2782 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
2783 dec_page_count(sbi, F2FS_DIRTY_NODES);
2784 } else {
2785 inode_dec_dirty_pages(inode);
2786 f2fs_remove_dirty_inode(inode);
2787 }
2788 }
2789
2790 clear_cold_data(page);
2791
2792 if (IS_ATOMIC_WRITTEN_PAGE(page))
2793 return f2fs_drop_inmem_page(inode, page);
2794
2795 f2fs_clear_page_private(page);
2796 }
2797
f2fs_release_page(struct page * page,gfp_t wait)2798 int f2fs_release_page(struct page *page, gfp_t wait)
2799 {
2800 /* If this is dirty page, keep PagePrivate */
2801 if (PageDirty(page))
2802 return 0;
2803
2804 /* This is atomic written page, keep Private */
2805 if (IS_ATOMIC_WRITTEN_PAGE(page))
2806 return 0;
2807
2808 clear_cold_data(page);
2809 f2fs_clear_page_private(page);
2810 return 1;
2811 }
2812
f2fs_set_data_page_dirty(struct page * page)2813 static int f2fs_set_data_page_dirty(struct page *page)
2814 {
2815 struct address_space *mapping = page->mapping;
2816 struct inode *inode = mapping->host;
2817
2818 trace_f2fs_set_page_dirty(page, DATA);
2819
2820 if (!PageUptodate(page))
2821 SetPageUptodate(page);
2822
2823 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
2824 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2825 f2fs_register_inmem_page(inode, page);
2826 return 1;
2827 }
2828 /*
2829 * Previously, this page has been registered, we just
2830 * return here.
2831 */
2832 return 0;
2833 }
2834
2835 if (!PageDirty(page)) {
2836 __set_page_dirty_nobuffers(page);
2837 f2fs_update_dirty_page(inode, page);
2838 return 1;
2839 }
2840 return 0;
2841 }
2842
f2fs_bmap(struct address_space * mapping,sector_t block)2843 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2844 {
2845 struct inode *inode = mapping->host;
2846
2847 if (f2fs_has_inline_data(inode))
2848 return 0;
2849
2850 /* make sure allocating whole blocks */
2851 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2852 filemap_write_and_wait(mapping);
2853
2854 return generic_block_bmap(mapping, block, get_data_block_bmap);
2855 }
2856
2857 #ifdef CONFIG_MIGRATION
2858 #include <linux/migrate.h>
2859
f2fs_migrate_page(struct address_space * mapping,struct page * newpage,struct page * page,enum migrate_mode mode)2860 int f2fs_migrate_page(struct address_space *mapping,
2861 struct page *newpage, struct page *page, enum migrate_mode mode)
2862 {
2863 int rc, extra_count;
2864 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2865 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2866
2867 BUG_ON(PageWriteback(page));
2868
2869 /* migrating an atomic written page is safe with the inmem_lock hold */
2870 if (atomic_written) {
2871 if (mode != MIGRATE_SYNC)
2872 return -EBUSY;
2873 if (!mutex_trylock(&fi->inmem_lock))
2874 return -EAGAIN;
2875 }
2876
2877 /* one extra reference was held for atomic_write page */
2878 extra_count = atomic_written ? 1 : 0;
2879 rc = migrate_page_move_mapping(mapping, newpage,
2880 page, NULL, mode, extra_count);
2881 if (rc != MIGRATEPAGE_SUCCESS) {
2882 if (atomic_written)
2883 mutex_unlock(&fi->inmem_lock);
2884 return rc;
2885 }
2886
2887 if (atomic_written) {
2888 struct inmem_pages *cur;
2889 list_for_each_entry(cur, &fi->inmem_pages, list)
2890 if (cur->page == page) {
2891 cur->page = newpage;
2892 break;
2893 }
2894 mutex_unlock(&fi->inmem_lock);
2895 put_page(page);
2896 get_page(newpage);
2897 }
2898
2899 if (PagePrivate(page)) {
2900 f2fs_set_page_private(newpage, page_private(page));
2901 f2fs_clear_page_private(page);
2902 }
2903
2904 if (mode != MIGRATE_SYNC_NO_COPY)
2905 migrate_page_copy(newpage, page);
2906 else
2907 migrate_page_states(newpage, page);
2908
2909 return MIGRATEPAGE_SUCCESS;
2910 }
2911 #endif
2912
2913 const struct address_space_operations f2fs_dblock_aops = {
2914 .readpage = f2fs_read_data_page,
2915 .readpages = f2fs_read_data_pages,
2916 .writepage = f2fs_write_data_page,
2917 .writepages = f2fs_write_data_pages,
2918 .write_begin = f2fs_write_begin,
2919 .write_end = f2fs_write_end,
2920 .set_page_dirty = f2fs_set_data_page_dirty,
2921 .invalidatepage = f2fs_invalidate_page,
2922 .releasepage = f2fs_release_page,
2923 .direct_IO = f2fs_direct_IO,
2924 .bmap = f2fs_bmap,
2925 #ifdef CONFIG_MIGRATION
2926 .migratepage = f2fs_migrate_page,
2927 #endif
2928 };
2929
f2fs_clear_radix_tree_dirty_tag(struct page * page)2930 void f2fs_clear_radix_tree_dirty_tag(struct page *page)
2931 {
2932 struct address_space *mapping = page_mapping(page);
2933 unsigned long flags;
2934
2935 spin_lock_irqsave(&mapping->tree_lock, flags);
2936 radix_tree_tag_clear(&mapping->page_tree, page_index(page),
2937 PAGECACHE_TAG_DIRTY);
2938 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2939 }
2940
f2fs_init_post_read_processing(void)2941 int __init f2fs_init_post_read_processing(void)
2942 {
2943 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2944 if (!bio_post_read_ctx_cache)
2945 goto fail;
2946 bio_post_read_ctx_pool =
2947 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2948 bio_post_read_ctx_cache);
2949 if (!bio_post_read_ctx_pool)
2950 goto fail_free_cache;
2951 return 0;
2952
2953 fail_free_cache:
2954 kmem_cache_destroy(bio_post_read_ctx_cache);
2955 fail:
2956 return -ENOMEM;
2957 }
2958
f2fs_destroy_post_read_processing(void)2959 void __exit f2fs_destroy_post_read_processing(void)
2960 {
2961 mempool_destroy(bio_post_read_ctx_pool);
2962 kmem_cache_destroy(bio_post_read_ctx_cache);
2963 }
2964