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