forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/fs/f2fs/data.c
....@@ -14,16 +14,17 @@
1414 #include <linux/pagevec.h>
1515 #include <linux/blkdev.h>
1616 #include <linux/bio.h>
17
+#include <linux/blk-crypto.h>
1718 #include <linux/swap.h>
1819 #include <linux/prefetch.h>
1920 #include <linux/uio.h>
2021 #include <linux/cleancache.h>
2122 #include <linux/sched/signal.h>
23
+#include <linux/fiemap.h>
2224
2325 #include "f2fs.h"
2426 #include "node.h"
2527 #include "segment.h"
26
-#include "trace.h"
2728 #include <trace/events/f2fs.h>
2829 #include <trace/events/android_fs.h>
2930
....@@ -49,27 +50,6 @@
4950 bioset_exit(&f2fs_bioset);
5051 }
5152
52
-static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
53
- unsigned int nr_iovecs)
54
-{
55
- return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
56
-}
57
-
58
-struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
59
-{
60
- if (noio) {
61
- /* No failure on bio allocation */
62
- return __f2fs_bio_alloc(GFP_NOIO, npages);
63
- }
64
-
65
- if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
66
- f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
67
- return NULL;
68
- }
69
-
70
- return __f2fs_bio_alloc(GFP_KERNEL, npages);
71
-}
72
-
7353 static bool __is_cp_guaranteed(struct page *page)
7454 {
7555 struct address_space *mapping = page->mapping;
....@@ -79,18 +59,19 @@
7959 if (!mapping)
8060 return false;
8161
82
- if (f2fs_is_compressed_page(page))
83
- return false;
84
-
8562 inode = mapping->host;
8663 sbi = F2FS_I_SB(inode);
8764
8865 if (inode->i_ino == F2FS_META_INO(sbi) ||
89
- inode->i_ino == F2FS_NODE_INO(sbi) ||
90
- S_ISDIR(inode->i_mode) ||
91
- (S_ISREG(inode->i_mode) &&
66
+ inode->i_ino == F2FS_NODE_INO(sbi) ||
67
+ S_ISDIR(inode->i_mode))
68
+ return true;
69
+
70
+ if (f2fs_is_compressed_page(page))
71
+ return false;
72
+ if ((S_ISREG(inode->i_mode) &&
9273 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
93
- is_cold_data(page))
74
+ page_private_gcing(page))
9475 return true;
9576 return false;
9677 }
....@@ -114,10 +95,21 @@
11495
11596 /* postprocessing steps for read bios */
11697 enum bio_post_read_step {
117
- STEP_DECRYPT,
118
- STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
119
- STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
120
- STEP_VERITY,
98
+#ifdef CONFIG_FS_ENCRYPTION
99
+ STEP_DECRYPT = 1 << 0,
100
+#else
101
+ STEP_DECRYPT = 0, /* compile out the decryption-related code */
102
+#endif
103
+#ifdef CONFIG_F2FS_FS_COMPRESSION
104
+ STEP_DECOMPRESS = 1 << 1,
105
+#else
106
+ STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
107
+#endif
108
+#ifdef CONFIG_FS_VERITY
109
+ STEP_VERITY = 1 << 2,
110
+#else
111
+ STEP_VERITY = 0, /* compile out the verity-related code */
112
+#endif
121113 };
122114
123115 struct bio_post_read_ctx {
....@@ -127,25 +119,27 @@
127119 unsigned int enabled_steps;
128120 };
129121
130
-static void __read_end_io(struct bio *bio, bool compr, bool verity)
122
+static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
131123 {
132
- struct page *page;
133124 struct bio_vec *bv;
134
- int i;
125
+ struct bvec_iter_all iter_all;
135126
136
- bio_for_each_segment_all(bv, bio, i) {
137
- page = bv->bv_page;
127
+ /*
128
+ * Update and unlock the bio's pagecache pages, and put the
129
+ * decompression context for any compressed pages.
130
+ */
131
+ bio_for_each_segment_all(bv, bio, iter_all) {
132
+ struct page *page = bv->bv_page;
138133
139
-#ifdef CONFIG_F2FS_FS_COMPRESSION
140
- if (compr && f2fs_is_compressed_page(page)) {
141
- f2fs_decompress_pages(bio, page, verity);
134
+ if (f2fs_is_compressed_page(page)) {
135
+ if (bio->bi_status)
136
+ f2fs_end_read_compressed_page(page, true, 0,
137
+ in_task);
138
+ f2fs_put_page_dic(page, in_task);
142139 continue;
143140 }
144
- if (verity)
145
- continue;
146
-#endif
147141
148
- /* PG_error was set if any post_read step failed */
142
+ /* PG_error was set if decryption or verity failed. */
149143 if (bio->bi_status || PageError(page)) {
150144 ClearPageUptodate(page);
151145 /* will re-read again later */
....@@ -156,106 +150,109 @@
156150 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
157151 unlock_page(page);
158152 }
153
+
154
+ if (bio->bi_private)
155
+ mempool_free(bio->bi_private, bio_post_read_ctx_pool);
156
+ bio_put(bio);
159157 }
160158
161
-static void f2fs_release_read_bio(struct bio *bio);
162
-static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
163
-{
164
- if (!compr)
165
- __read_end_io(bio, false, verity);
166
- f2fs_release_read_bio(bio);
167
-}
168
-
169
-static void f2fs_decompress_bio(struct bio *bio, bool verity)
170
-{
171
- __read_end_io(bio, true, verity);
172
-}
173
-
174
-static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
175
-
176
-static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
177
-{
178
- fscrypt_decrypt_bio(ctx->bio);
179
-}
180
-
181
-static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
182
-{
183
- f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
184
-}
185
-
186
-#ifdef CONFIG_F2FS_FS_COMPRESSION
187
-static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
188
-{
189
- f2fs_decompress_end_io(rpages, cluster_size, false, true);
190
-}
191
-
192
-static void f2fs_verify_bio(struct bio *bio)
193
-{
194
- struct bio_vec *bv;
195
- int i;
196
-
197
- bio_for_each_segment_all(bv, bio, i) {
198
- struct page *page = bv->bv_page;
199
- struct decompress_io_ctx *dic;
200
-
201
- dic = (struct decompress_io_ctx *)page_private(page);
202
-
203
- if (dic) {
204
- if (refcount_dec_not_one(&dic->ref))
205
- continue;
206
- f2fs_verify_pages(dic->rpages,
207
- dic->cluster_size);
208
- f2fs_free_dic(dic);
209
- continue;
210
- }
211
-
212
- if (bio->bi_status || PageError(page))
213
- goto clear_uptodate;
214
-
215
- if (fsverity_verify_page(page)) {
216
- SetPageUptodate(page);
217
- goto unlock;
218
- }
219
-clear_uptodate:
220
- ClearPageUptodate(page);
221
- ClearPageError(page);
222
-unlock:
223
- dec_page_count(F2FS_P_SB(page), __read_io_type(page));
224
- unlock_page(page);
225
- }
226
-}
227
-#endif
228
-
229
-static void f2fs_verity_work(struct work_struct *work)
159
+static void f2fs_verify_bio(struct work_struct *work)
230160 {
231161 struct bio_post_read_ctx *ctx =
232162 container_of(work, struct bio_post_read_ctx, work);
233163 struct bio *bio = ctx->bio;
234
-#ifdef CONFIG_F2FS_FS_COMPRESSION
235
- unsigned int enabled_steps = ctx->enabled_steps;
236
-#endif
164
+ bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
237165
238166 /*
239167 * fsverity_verify_bio() may call readpages() again, and while verity
240
- * will be disabled for this, decryption may still be needed, resulting
241
- * in another bio_post_read_ctx being allocated. So to prevent
242
- * deadlocks we need to release the current ctx to the mempool first.
243
- * This assumes that verity is the last post-read step.
168
+ * will be disabled for this, decryption and/or decompression may still
169
+ * be needed, resulting in another bio_post_read_ctx being allocated.
170
+ * So to prevent deadlocks we need to release the current ctx to the
171
+ * mempool first. This assumes that verity is the last post-read step.
244172 */
245173 mempool_free(ctx, bio_post_read_ctx_pool);
246174 bio->bi_private = NULL;
247175
248
-#ifdef CONFIG_F2FS_FS_COMPRESSION
249
- /* previous step is decompression */
250
- if (enabled_steps & (1 << STEP_DECOMPRESS)) {
251
- f2fs_verify_bio(bio);
252
- f2fs_release_read_bio(bio);
253
- return;
254
- }
255
-#endif
176
+ /*
177
+ * Verify the bio's pages with fs-verity. Exclude compressed pages,
178
+ * as those were handled separately by f2fs_end_read_compressed_page().
179
+ */
180
+ if (may_have_compressed_pages) {
181
+ struct bio_vec *bv;
182
+ struct bvec_iter_all iter_all;
256183
257
- fsverity_verify_bio(bio);
258
- __f2fs_read_end_io(bio, false, false);
184
+ bio_for_each_segment_all(bv, bio, iter_all) {
185
+ struct page *page = bv->bv_page;
186
+
187
+ if (!f2fs_is_compressed_page(page) &&
188
+ !PageError(page) && !fsverity_verify_page(page))
189
+ SetPageError(page);
190
+ }
191
+ } else {
192
+ fsverity_verify_bio(bio);
193
+ }
194
+
195
+ f2fs_finish_read_bio(bio, true);
196
+}
197
+
198
+/*
199
+ * If the bio's data needs to be verified with fs-verity, then enqueue the
200
+ * verity work for the bio. Otherwise finish the bio now.
201
+ *
202
+ * Note that to avoid deadlocks, the verity work can't be done on the
203
+ * decryption/decompression workqueue. This is because verifying the data pages
204
+ * can involve reading verity metadata pages from the file, and these verity
205
+ * metadata pages may be encrypted and/or compressed.
206
+ */
207
+static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)
208
+{
209
+ struct bio_post_read_ctx *ctx = bio->bi_private;
210
+
211
+ if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
212
+ INIT_WORK(&ctx->work, f2fs_verify_bio);
213
+ fsverity_enqueue_verify_work(&ctx->work);
214
+ } else {
215
+ f2fs_finish_read_bio(bio, in_task);
216
+ }
217
+}
218
+
219
+/*
220
+ * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
221
+ * remaining page was read by @ctx->bio.
222
+ *
223
+ * Note that a bio may span clusters (even a mix of compressed and uncompressed
224
+ * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
225
+ * that the bio includes at least one compressed page. The actual decompression
226
+ * is done on a per-cluster basis, not a per-bio basis.
227
+ */
228
+static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,
229
+ bool in_task)
230
+{
231
+ struct bio_vec *bv;
232
+ struct bvec_iter_all iter_all;
233
+ bool all_compressed = true;
234
+ block_t blkaddr = SECTOR_TO_BLOCK(ctx->bio->bi_iter.bi_sector);
235
+
236
+ bio_for_each_segment_all(bv, ctx->bio, iter_all) {
237
+ struct page *page = bv->bv_page;
238
+
239
+ /* PG_error was set if decryption failed. */
240
+ if (f2fs_is_compressed_page(page))
241
+ f2fs_end_read_compressed_page(page, PageError(page),
242
+ blkaddr, in_task);
243
+ else
244
+ all_compressed = false;
245
+
246
+ blkaddr++;
247
+ }
248
+
249
+ /*
250
+ * Optimization: if all the bio's pages are compressed, then scheduling
251
+ * the per-bio verity work is unnecessary, as verity will be fully
252
+ * handled at the compression cluster level.
253
+ */
254
+ if (all_compressed)
255
+ ctx->enabled_steps &= ~STEP_VERITY;
259256 }
260257
261258 static void f2fs_post_read_work(struct work_struct *work)
....@@ -263,107 +260,75 @@
263260 struct bio_post_read_ctx *ctx =
264261 container_of(work, struct bio_post_read_ctx, work);
265262
266
- if (ctx->enabled_steps & (1 << STEP_DECRYPT))
267
- f2fs_decrypt_work(ctx);
263
+ if (ctx->enabled_steps & STEP_DECRYPT)
264
+ fscrypt_decrypt_bio(ctx->bio);
268265
269
- if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
270
- f2fs_decompress_work(ctx);
266
+ if (ctx->enabled_steps & STEP_DECOMPRESS)
267
+ f2fs_handle_step_decompress(ctx, true);
271268
272
- if (ctx->enabled_steps & (1 << STEP_VERITY)) {
273
- INIT_WORK(&ctx->work, f2fs_verity_work);
274
- fsverity_enqueue_verify_work(&ctx->work);
275
- return;
276
- }
277
-
278
- __f2fs_read_end_io(ctx->bio,
279
- ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
280
-}
281
-
282
-static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
283
- struct work_struct *work)
284
-{
285
- queue_work(sbi->post_read_wq, work);
286
-}
287
-
288
-static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
289
-{
290
- /*
291
- * We use different work queues for decryption and for verity because
292
- * verity may require reading metadata pages that need decryption, and
293
- * we shouldn't recurse to the same workqueue.
294
- */
295
-
296
- if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
297
- ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
298
- INIT_WORK(&ctx->work, f2fs_post_read_work);
299
- f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
300
- return;
301
- }
302
-
303
- if (ctx->enabled_steps & (1 << STEP_VERITY)) {
304
- INIT_WORK(&ctx->work, f2fs_verity_work);
305
- fsverity_enqueue_verify_work(&ctx->work);
306
- return;
307
- }
308
-
309
- __f2fs_read_end_io(ctx->bio, false, false);
310
-}
311
-
312
-static bool f2fs_bio_post_read_required(struct bio *bio)
313
-{
314
- return bio->bi_private;
269
+ f2fs_verify_and_finish_bio(ctx->bio, true);
315270 }
316271
317272 static void f2fs_read_end_io(struct bio *bio)
318273 {
319
- struct page *first_page = bio->bi_io_vec[0].bv_page;
320
- struct f2fs_sb_info *sbi = F2FS_P_SB(first_page);
274
+ struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
275
+ struct bio_post_read_ctx *ctx = bio->bi_private;
276
+ bool intask = in_task();
321277
322278 if (time_to_inject(sbi, FAULT_READ_IO)) {
323279 f2fs_show_injection_info(sbi, FAULT_READ_IO);
324280 bio->bi_status = BLK_STS_IOERR;
325281 }
326282
327
- if (f2fs_bio_post_read_required(bio)) {
328
- struct bio_post_read_ctx *ctx = bio->bi_private;
329
-
330
- bio_post_read_processing(ctx);
283
+ if (bio->bi_status) {
284
+ f2fs_finish_read_bio(bio, intask);
331285 return;
332286 }
333287
334
- if (first_page != NULL &&
335
- __read_io_type(first_page) == F2FS_RD_DATA) {
336
- trace_android_fs_dataread_end(first_page->mapping->host,
337
- page_offset(first_page),
338
- bio->bi_iter.bi_size);
288
+ if (ctx) {
289
+ unsigned int enabled_steps = ctx->enabled_steps &
290
+ (STEP_DECRYPT | STEP_DECOMPRESS);
291
+
292
+ /*
293
+ * If we have only decompression step between decompression and
294
+ * decrypt, we don't need post processing for this.
295
+ */
296
+ if (enabled_steps == STEP_DECOMPRESS &&
297
+ !f2fs_low_mem_mode(sbi)) {
298
+ f2fs_handle_step_decompress(ctx, intask);
299
+ } else if (enabled_steps) {
300
+ INIT_WORK(&ctx->work, f2fs_post_read_work);
301
+ queue_work(ctx->sbi->post_read_wq, &ctx->work);
302
+ return;
303
+ }
339304 }
340305
341
- __f2fs_read_end_io(bio, false, false);
306
+ f2fs_verify_and_finish_bio(bio, intask);
342307 }
343308
344309 static void f2fs_write_end_io(struct bio *bio)
345310 {
346311 struct f2fs_sb_info *sbi = bio->bi_private;
347312 struct bio_vec *bvec;
348
- int i;
313
+ struct bvec_iter_all iter_all;
349314
350315 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
351316 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
352317 bio->bi_status = BLK_STS_IOERR;
353318 }
354319
355
- bio_for_each_segment_all(bvec, bio, i) {
320
+ bio_for_each_segment_all(bvec, bio, iter_all) {
356321 struct page *page = bvec->bv_page;
357322 enum count_type type = WB_DATA_TYPE(page);
358323
359
- if (IS_DUMMY_WRITTEN_PAGE(page)) {
360
- set_page_private(page, (unsigned long)NULL);
361
- ClearPagePrivate(page);
324
+ if (page_private_dummy(page)) {
325
+ clear_page_private_dummy(page);
362326 unlock_page(page);
363327 mempool_free(page, sbi->write_io_dummy);
364328
365329 if (unlikely(bio->bi_status))
366
- f2fs_stop_checkpoint(sbi, true);
330
+ f2fs_stop_checkpoint(sbi, true,
331
+ STOP_CP_REASON_WRITE_FAIL);
367332 continue;
368333 }
369334
....@@ -379,7 +344,8 @@
379344 if (unlikely(bio->bi_status)) {
380345 mapping_set_error(page->mapping, -EIO);
381346 if (type == F2FS_WB_CP_DATA)
382
- f2fs_stop_checkpoint(sbi, true);
347
+ f2fs_stop_checkpoint(sbi, true,
348
+ STOP_CP_REASON_WRITE_FAIL);
383349 }
384350
385351 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
....@@ -388,7 +354,7 @@
388354 dec_page_count(sbi, type);
389355 if (f2fs_in_warm_node_list(sbi, page))
390356 f2fs_del_fsync_node_entry(sbi, page);
391
- clear_cold_data(page);
357
+ clear_page_private_gcing(page);
392358 end_page_writeback(page);
393359 }
394360 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
....@@ -449,7 +415,7 @@
449415 struct f2fs_sb_info *sbi = fio->sbi;
450416 struct bio *bio;
451417
452
- bio = f2fs_bio_alloc(sbi, npages, true);
418
+ bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
453419
454420 f2fs_target_device(sbi, fio->new_blkaddr, bio);
455421 if (is_read_io(fio->op)) {
....@@ -510,7 +476,7 @@
510476 if (f2fs_lfs_mode(sbi) && current->plug)
511477 blk_finish_plug(current->plug);
512478
513
- if (F2FS_IO_ALIGNED(sbi))
479
+ if (!F2FS_IO_ALIGNED(sbi))
514480 goto submit_io;
515481
516482 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
....@@ -526,10 +492,11 @@
526492 GFP_NOIO | __GFP_NOFAIL);
527493 f2fs_bug_on(sbi, !page);
528494
529
- zero_user_segment(page, 0, PAGE_SIZE);
530
- SetPagePrivate(page);
531
- set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
532495 lock_page(page);
496
+
497
+ zero_user_segment(page, 0, PAGE_SIZE);
498
+ set_page_private_dummy(page);
499
+
533500 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
534501 f2fs_bug_on(sbi, 1);
535502 }
....@@ -546,32 +513,6 @@
546513 else
547514 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
548515 submit_bio(bio);
549
-}
550
-
551
-static void __f2fs_submit_read_bio(struct f2fs_sb_info *sbi,
552
- struct bio *bio, enum page_type type)
553
-{
554
- if (trace_android_fs_dataread_start_enabled() && (type == DATA)) {
555
- struct page *first_page = bio->bi_io_vec[0].bv_page;
556
-
557
- if (first_page != NULL &&
558
- __read_io_type(first_page) == F2FS_RD_DATA) {
559
- char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
560
-
561
- path = android_fstrace_get_pathname(pathbuf,
562
- MAX_TRACE_PATHBUF_LEN,
563
- first_page->mapping->host);
564
-
565
- trace_android_fs_dataread_start(
566
- first_page->mapping->host,
567
- page_offset(first_page),
568
- bio->bi_iter.bi_size,
569
- current->pid,
570
- path,
571
- current->comm);
572
- }
573
- }
574
- __submit_bio(sbi, bio, type);
575516 }
576517
577518 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
....@@ -631,7 +572,7 @@
631572 struct page *page, nid_t ino)
632573 {
633574 struct bio_vec *bvec;
634
- int i;
575
+ struct bvec_iter_all iter_all;
635576
636577 if (!bio)
637578 return false;
....@@ -639,7 +580,7 @@
639580 if (!inode && !page && !ino)
640581 return true;
641582
642
- bio_for_each_segment_all(bvec, bio, i) {
583
+ bio_for_each_segment_all(bvec, bio, iter_all) {
643584 struct page *target = bvec->bv_page;
644585
645586 if (fscrypt_is_bounce_page(target)) {
....@@ -670,7 +611,7 @@
670611 enum page_type btype = PAGE_TYPE_OF_BIO(type);
671612 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
672613
673
- down_write(&io->io_rwsem);
614
+ f2fs_down_write(&io->io_rwsem);
674615
675616 /* change META to META_FLUSH in the checkpoint procedure */
676617 if (type >= META_FLUSH) {
....@@ -681,7 +622,7 @@
681622 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
682623 }
683624 __submit_merged_bio(io);
684
- up_write(&io->io_rwsem);
625
+ f2fs_up_write(&io->io_rwsem);
685626 }
686627
687628 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
....@@ -696,9 +637,9 @@
696637 enum page_type btype = PAGE_TYPE_OF_BIO(type);
697638 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
698639
699
- down_read(&io->io_rwsem);
640
+ f2fs_down_read(&io->io_rwsem);
700641 ret = __has_merged_page(io->bio, inode, page, ino);
701
- up_read(&io->io_rwsem);
642
+ f2fs_up_read(&io->io_rwsem);
702643 }
703644 if (ret)
704645 __f2fs_submit_merged_write(sbi, type, temp);
....@@ -744,7 +685,6 @@
744685 return -EFSCORRUPTED;
745686
746687 trace_f2fs_submit_page_bio(page, fio);
747
- f2fs_trace_ios(fio, 0);
748688
749689 /* Allocate a new bio */
750690 bio = __bio_alloc(fio, 1);
....@@ -758,7 +698,7 @@
758698 }
759699
760700 if (fio->io_wbc && !is_read_io(fio->op))
761
- wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
701
+ wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
762702
763703 __attach_io_flag(fio);
764704 bio_set_op_attrs(bio, fio->op, fio->op_flags);
....@@ -766,16 +706,16 @@
766706 inc_page_count(fio->sbi, is_read_io(fio->op) ?
767707 __read_io_type(page): WB_DATA_TYPE(fio->page));
768708
769
- if (is_read_io(fio->op))
770
- __f2fs_submit_read_bio(fio->sbi, bio, fio->type);
771
- else
772
- __submit_bio(fio->sbi, bio, fio->type);
709
+ __submit_bio(fio->sbi, bio, fio->type);
773710 return 0;
774711 }
775712
776713 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
777714 block_t last_blkaddr, block_t cur_blkaddr)
778715 {
716
+ if (unlikely(sbi->max_io_bytes &&
717
+ bio->bi_iter.bi_size >= sbi->max_io_bytes))
718
+ return false;
779719 if (last_blkaddr + 1 != cur_blkaddr)
780720 return false;
781721 return __same_bdev(sbi, cur_blkaddr, bio);
....@@ -823,9 +763,9 @@
823763 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
824764 f2fs_bug_on(sbi, 1);
825765
826
- down_write(&io->bio_list_lock);
766
+ f2fs_down_write(&io->bio_list_lock);
827767 list_add_tail(&be->list, &io->bio_list);
828
- up_write(&io->bio_list_lock);
768
+ f2fs_up_write(&io->bio_list_lock);
829769 }
830770
831771 static void del_bio_entry(struct bio_entry *be)
....@@ -847,16 +787,17 @@
847787 struct list_head *head = &io->bio_list;
848788 struct bio_entry *be;
849789
850
- down_write(&io->bio_list_lock);
790
+ f2fs_down_write(&io->bio_list_lock);
851791 list_for_each_entry(be, head, list) {
852792 if (be->bio != *bio)
853793 continue;
854794
855795 found = true;
856796
857
- if (page_is_mergeable(sbi, *bio, *fio->last_block,
858
- fio->new_blkaddr) &&
859
- f2fs_crypt_mergeable_bio(*bio,
797
+ f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
798
+ *fio->last_block,
799
+ fio->new_blkaddr));
800
+ if (f2fs_crypt_mergeable_bio(*bio,
860801 fio->page->mapping->host,
861802 fio->page->index, fio) &&
862803 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
....@@ -870,7 +811,7 @@
870811 __submit_bio(sbi, *bio, DATA);
871812 break;
872813 }
873
- up_write(&io->bio_list_lock);
814
+ f2fs_up_write(&io->bio_list_lock);
874815 }
875816
876817 if (ret) {
....@@ -896,7 +837,7 @@
896837 if (list_empty(head))
897838 continue;
898839
899
- down_read(&io->bio_list_lock);
840
+ f2fs_down_read(&io->bio_list_lock);
900841 list_for_each_entry(be, head, list) {
901842 if (target)
902843 found = (target == be->bio);
....@@ -906,14 +847,14 @@
906847 if (found)
907848 break;
908849 }
909
- up_read(&io->bio_list_lock);
850
+ f2fs_up_read(&io->bio_list_lock);
910851
911852 if (!found)
912853 continue;
913854
914855 found = false;
915856
916
- down_write(&io->bio_list_lock);
857
+ f2fs_down_write(&io->bio_list_lock);
917858 list_for_each_entry(be, head, list) {
918859 if (target)
919860 found = (target == be->bio);
....@@ -926,7 +867,7 @@
926867 break;
927868 }
928869 }
929
- up_write(&io->bio_list_lock);
870
+ f2fs_up_write(&io->bio_list_lock);
930871 }
931872
932873 if (found)
....@@ -948,15 +889,16 @@
948889 return -EFSCORRUPTED;
949890
950891 trace_f2fs_submit_page_bio(page, fio);
951
- f2fs_trace_ios(fio, 0);
952892
893
+ if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
894
+ fio->new_blkaddr))
895
+ f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
953896 alloc_new:
954897 if (!bio) {
955898 bio = __bio_alloc(fio, BIO_MAX_PAGES);
956
- f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
957
- fio->page->index, fio,
958
- GFP_NOIO);
959899 __attach_io_flag(fio);
900
+ f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
901
+ fio->page->index, fio, GFP_NOIO);
960902 bio_set_op_attrs(bio, fio->op, fio->op_flags);
961903
962904 add_bio_entry(fio->sbi, bio, page, fio->temp);
....@@ -966,7 +908,7 @@
966908 }
967909
968910 if (fio->io_wbc)
969
- wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
911
+ wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
970912
971913 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
972914
....@@ -985,7 +927,7 @@
985927
986928 f2fs_bug_on(sbi, is_read_io(fio->op));
987929
988
- down_write(&io->io_rwsem);
930
+ f2fs_down_write(&io->io_rwsem);
989931 next:
990932 if (fio->in_list) {
991933 spin_lock(&io->io_lock);
....@@ -1017,7 +959,7 @@
1017959 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
1018960 fio->new_blkaddr) ||
1019961 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
1020
- fio->page->index, fio)))
962
+ bio_page->index, fio)))
1021963 __submit_merged_bio(io);
1022964 alloc_new:
1023965 if (io->bio == NULL) {
....@@ -1030,8 +972,7 @@
1030972 }
1031973 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
1032974 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
1033
- fio->page->index, fio,
1034
- GFP_NOIO);
975
+ bio_page->index, fio, GFP_NOIO);
1035976 io->fio = *fio;
1036977 }
1037978
....@@ -1041,10 +982,9 @@
1041982 }
1042983
1043984 if (fio->io_wbc)
1044
- wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
985
+ wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
1045986
1046987 io->last_block_in_bio = fio->new_blkaddr;
1047
- f2fs_trace_ios(fio, 0);
1048988
1049989 trace_f2fs_submit_page_write(fio->page, fio);
1050990 skip:
....@@ -1054,13 +994,7 @@
1054994 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1055995 !f2fs_is_checkpoint_ready(sbi))
1056996 __submit_merged_bio(io);
1057
- up_write(&io->io_rwsem);
1058
-}
1059
-
1060
-static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
1061
-{
1062
- return fsverity_active(inode) &&
1063
- idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
997
+ f2fs_up_write(&io->io_rwsem);
1064998 }
1065999
10661000 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
....@@ -1072,8 +1006,9 @@
10721006 struct bio_post_read_ctx *ctx;
10731007 unsigned int post_read_steps = 0;
10741008
1075
- bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
1076
- for_write);
1009
+ bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
1010
+ min_t(int, nr_pages, BIO_MAX_PAGES),
1011
+ &f2fs_bioset);
10771012 if (!bio)
10781013 return ERR_PTR(-ENOMEM);
10791014
....@@ -1084,13 +1019,19 @@
10841019 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
10851020
10861021 if (fscrypt_inode_uses_fs_layer_crypto(inode))
1087
- post_read_steps |= 1 << STEP_DECRYPT;
1088
- if (f2fs_compressed_file(inode))
1089
- post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1090
- if (f2fs_need_verity(inode, first_idx))
1091
- post_read_steps |= 1 << STEP_VERITY;
1022
+ post_read_steps |= STEP_DECRYPT;
10921023
1093
- if (post_read_steps) {
1024
+ if (f2fs_need_verity(inode, first_idx))
1025
+ post_read_steps |= STEP_VERITY;
1026
+
1027
+ /*
1028
+ * STEP_DECOMPRESS is handled specially, since a compressed file might
1029
+ * contain both compressed and uncompressed clusters. We'll allocate a
1030
+ * bio_post_read_ctx if the file is compressed, but the caller is
1031
+ * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1032
+ */
1033
+
1034
+ if (post_read_steps || f2fs_compressed_file(inode)) {
10941035 /* Due to the mempool, this never fails. */
10951036 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
10961037 ctx->bio = bio;
....@@ -1102,21 +1043,15 @@
11021043 return bio;
11031044 }
11041045
1105
-static void f2fs_release_read_bio(struct bio *bio)
1106
-{
1107
- if (bio->bi_private)
1108
- mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1109
- bio_put(bio);
1110
-}
1111
-
11121046 /* This can handle encryption stuffs */
11131047 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1114
- block_t blkaddr, bool for_write)
1048
+ block_t blkaddr, int op_flags, bool for_write)
11151049 {
11161050 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
11171051 struct bio *bio;
11181052
1119
- bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index, for_write);
1053
+ bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1054
+ page->index, for_write);
11201055 if (IS_ERR(bio))
11211056 return PTR_ERR(bio);
11221057
....@@ -1130,7 +1065,7 @@
11301065 ClearPageError(page);
11311066 inc_page_count(sbi, F2FS_RD_DATA);
11321067 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1133
- __f2fs_submit_read_bio(sbi, bio, DATA);
1068
+ __submit_bio(sbi, bio, DATA);
11341069 return 0;
11351070 }
11361071
....@@ -1166,7 +1101,7 @@
11661101 {
11671102 dn->data_blkaddr = blkaddr;
11681103 f2fs_set_data_blkaddr(dn);
1169
- f2fs_update_extent_cache(dn);
1104
+ f2fs_update_read_extent_cache(dn);
11701105 }
11711106
11721107 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
....@@ -1190,6 +1125,7 @@
11901125
11911126 for (; count > 0; dn->ofs_in_node++) {
11921127 block_t blkaddr = f2fs_data_blkaddr(dn);
1128
+
11931129 if (blkaddr == NULL_ADDR) {
11941130 dn->data_blkaddr = NEW_ADDR;
11951131 __set_data_blkaddr(dn);
....@@ -1231,10 +1167,10 @@
12311167
12321168 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
12331169 {
1234
- struct extent_info ei = {0,0,0};
1170
+ struct extent_info ei = {0, };
12351171 struct inode *inode = dn->inode;
12361172
1237
- if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1173
+ if (f2fs_lookup_read_extent_cache(inode, index, &ei)) {
12381174 dn->data_blkaddr = ei.blk + index - ei.fofs;
12391175 return 0;
12401176 }
....@@ -1248,14 +1184,14 @@
12481184 struct address_space *mapping = inode->i_mapping;
12491185 struct dnode_of_data dn;
12501186 struct page *page;
1251
- struct extent_info ei = {0,0,0};
1187
+ struct extent_info ei = {0, };
12521188 int err;
12531189
12541190 page = f2fs_grab_cache_page(mapping, index, for_write);
12551191 if (!page)
12561192 return ERR_PTR(-ENOMEM);
12571193
1258
- if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1194
+ if (f2fs_lookup_read_extent_cache(inode, index, &ei)) {
12591195 dn.data_blkaddr = ei.blk + index - ei.fofs;
12601196 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
12611197 DATA_GENERIC_ENHANCE_READ)) {
....@@ -1303,7 +1239,8 @@
13031239 return page;
13041240 }
13051241
1306
- err = f2fs_submit_page_read(inode, page, dn.data_blkaddr, for_write);
1242
+ err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1243
+ op_flags, for_write);
13071244 if (err)
13081245 goto put_err;
13091246 return page;
....@@ -1437,7 +1374,7 @@
14371374 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
14381375 return -EPERM;
14391376
1440
- err = f2fs_get_node_info(sbi, dn->nid, &ni);
1377
+ err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
14411378 if (err)
14421379 return err;
14431380
....@@ -1452,10 +1389,12 @@
14521389 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
14531390 old_blkaddr = dn->data_blkaddr;
14541391 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1455
- &sum, seg_type, NULL, false);
1456
- if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1392
+ &sum, seg_type, NULL);
1393
+ if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
14571394 invalidate_mapping_pages(META_MAPPING(sbi),
14581395 old_blkaddr, old_blkaddr);
1396
+ f2fs_invalidate_compress_page(sbi, old_blkaddr);
1397
+ }
14591398 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
14601399
14611400 /*
....@@ -1512,13 +1451,13 @@
15121451 return err;
15131452 }
15141453
1515
-void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1454
+void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
15161455 {
15171456 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
15181457 if (lock)
1519
- down_read(&sbi->node_change);
1458
+ f2fs_down_read(&sbi->node_change);
15201459 else
1521
- up_read(&sbi->node_change);
1460
+ f2fs_up_read(&sbi->node_change);
15221461 } else {
15231462 if (lock)
15241463 f2fs_lock_op(sbi);
....@@ -1543,7 +1482,7 @@
15431482 int err = 0, ofs = 1;
15441483 unsigned int ofs_in_node, last_ofs_in_node;
15451484 blkcnt_t prealloc;
1546
- struct extent_info ei = {0,0,0};
1485
+ struct extent_info ei = {0, };
15471486 block_t blkaddr;
15481487 unsigned int start_pgofs;
15491488
....@@ -1557,7 +1496,7 @@
15571496 pgofs = (pgoff_t)map->m_lblk;
15581497 end = pgofs + maxblocks;
15591498
1560
- if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1499
+ if (!create && f2fs_lookup_read_extent_cache(inode, pgofs, &ei)) {
15611500 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
15621501 map->m_may_create)
15631502 goto next_dnode;
....@@ -1577,7 +1516,7 @@
15771516
15781517 next_dnode:
15791518 if (map->m_may_create)
1580
- __do_map_lock(sbi, flag, true);
1519
+ f2fs_do_map_lock(sbi, flag, true);
15811520
15821521 /* When reading holes, we need its node page */
15831522 set_new_dnode(&dn, inode, NULL, NULL, 0);
....@@ -1585,7 +1524,21 @@
15851524 if (err) {
15861525 if (flag == F2FS_GET_BLOCK_BMAP)
15871526 map->m_pblk = 0;
1527
+
15881528 if (err == -ENOENT) {
1529
+ /*
1530
+ * There is one exceptional case that read_node_page()
1531
+ * may return -ENOENT due to filesystem has been
1532
+ * shutdown or cp_error, so force to convert error
1533
+ * number to EIO for such case.
1534
+ */
1535
+ if (map->m_may_create &&
1536
+ (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1537
+ f2fs_cp_error(sbi))) {
1538
+ err = -EIO;
1539
+ goto unlock_out;
1540
+ }
1541
+
15891542 err = 0;
15901543 if (map->m_next_pgofs)
15911544 *map->m_next_pgofs =
....@@ -1717,7 +1670,7 @@
17171670 if (map->m_flags & F2FS_MAP_MAPPED) {
17181671 unsigned int ofs = start_pgofs - map->m_lblk;
17191672
1720
- f2fs_update_extent_cache_range(&dn,
1673
+ f2fs_update_read_extent_cache_range(&dn,
17211674 start_pgofs, map->m_pblk + ofs,
17221675 map->m_len - ofs);
17231676 }
....@@ -1726,7 +1679,7 @@
17261679 f2fs_put_dnode(&dn);
17271680
17281681 if (map->m_may_create) {
1729
- __do_map_lock(sbi, flag, false);
1682
+ f2fs_do_map_lock(sbi, flag, false);
17301683 f2fs_balance_fs(sbi, dn.node_changed);
17311684 }
17321685 goto next_dnode;
....@@ -1742,7 +1695,7 @@
17421695 if (map->m_flags & F2FS_MAP_MAPPED) {
17431696 unsigned int ofs = start_pgofs - map->m_lblk;
17441697
1745
- f2fs_update_extent_cache_range(&dn,
1698
+ f2fs_update_read_extent_cache_range(&dn,
17461699 start_pgofs, map->m_pblk + ofs,
17471700 map->m_len - ofs);
17481701 }
....@@ -1752,7 +1705,7 @@
17521705 f2fs_put_dnode(&dn);
17531706 unlock_out:
17541707 if (map->m_may_create) {
1755
- __do_map_lock(sbi, flag, false);
1708
+ f2fs_do_map_lock(sbi, flag, false);
17561709 f2fs_balance_fs(sbi, dn.node_changed);
17571710 }
17581711 out:
....@@ -1786,6 +1739,16 @@
17861739 return true;
17871740 }
17881741
1742
+static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1743
+{
1744
+ return (bytes >> inode->i_blkbits);
1745
+}
1746
+
1747
+static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1748
+{
1749
+ return (blks << inode->i_blkbits);
1750
+}
1751
+
17891752 static int __get_data_block(struct inode *inode, sector_t iblock,
17901753 struct buffer_head *bh, int create, int flag,
17911754 pgoff_t *next_pgofs, int seg_type, bool may_write)
....@@ -1794,7 +1757,7 @@
17941757 int err;
17951758
17961759 map.m_lblk = iblock;
1797
- map.m_len = bh->b_size >> inode->i_blkbits;
1760
+ map.m_len = bytes_to_blks(inode, bh->b_size);
17981761 map.m_next_pgofs = next_pgofs;
17991762 map.m_next_extent = NULL;
18001763 map.m_seg_type = seg_type;
....@@ -1804,18 +1767,9 @@
18041767 if (!err) {
18051768 map_bh(bh, inode->i_sb, map.m_pblk);
18061769 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1807
- bh->b_size = (u64)map.m_len << inode->i_blkbits;
1770
+ bh->b_size = blks_to_bytes(inode, map.m_len);
18081771 }
18091772 return err;
1810
-}
1811
-
1812
-static int get_data_block(struct inode *inode, sector_t iblock,
1813
- struct buffer_head *bh_result, int create, int flag,
1814
- pgoff_t *next_pgofs)
1815
-{
1816
- return __get_data_block(inode, iblock, bh_result, create,
1817
- flag, next_pgofs,
1818
- NO_CHECK_TYPE, create);
18191773 }
18201774
18211775 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
....@@ -1824,7 +1778,7 @@
18241778 return __get_data_block(inode, iblock, bh_result, create,
18251779 F2FS_GET_BLOCK_DIO, NULL,
18261780 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1827
- IS_SWAPFILE(inode) ? false : true);
1781
+ true);
18281782 }
18291783
18301784 static int get_data_block_dio(struct inode *inode, sector_t iblock,
....@@ -1834,28 +1788,6 @@
18341788 F2FS_GET_BLOCK_DIO, NULL,
18351789 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
18361790 false);
1837
-}
1838
-
1839
-static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1840
- struct buffer_head *bh_result, int create)
1841
-{
1842
- /* Block number less than F2FS MAX BLOCKS */
1843
- if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1844
- return -EFBIG;
1845
-
1846
- return __get_data_block(inode, iblock, bh_result, create,
1847
- F2FS_GET_BLOCK_BMAP, NULL,
1848
- NO_CHECK_TYPE, create);
1849
-}
1850
-
1851
-static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1852
-{
1853
- return (offset >> inode->i_blkbits);
1854
-}
1855
-
1856
-static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1857
-{
1858
- return (blk << inode->i_blkbits);
18591791 }
18601792
18611793 static int f2fs_xattr_fiemap(struct inode *inode,
....@@ -1877,13 +1809,13 @@
18771809 if (!page)
18781810 return -ENOMEM;
18791811
1880
- err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1812
+ err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
18811813 if (err) {
18821814 f2fs_put_page(page, 1);
18831815 return err;
18841816 }
18851817
1886
- phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1818
+ phys = blks_to_bytes(inode, ni.blk_addr);
18871819 offset = offsetof(struct f2fs_inode, i_addr) +
18881820 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
18891821 get_inline_xattr_addrs(inode));
....@@ -1899,6 +1831,7 @@
18991831 flags |= FIEMAP_EXTENT_LAST;
19001832
19011833 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1834
+ trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
19021835 if (err || err == 1)
19031836 return err;
19041837 }
....@@ -1908,13 +1841,13 @@
19081841 if (!page)
19091842 return -ENOMEM;
19101843
1911
- err = f2fs_get_node_info(sbi, xnid, &ni);
1844
+ err = f2fs_get_node_info(sbi, xnid, &ni, false);
19121845 if (err) {
19131846 f2fs_put_page(page, 1);
19141847 return err;
19151848 }
19161849
1917
- phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1850
+ phys = blks_to_bytes(inode, ni.blk_addr);
19181851 len = inode->i_sb->s_blocksize;
19191852
19201853 f2fs_put_page(page, 1);
....@@ -1922,8 +1855,10 @@
19221855 flags = FIEMAP_EXTENT_LAST;
19231856 }
19241857
1925
- if (phys)
1858
+ if (phys) {
19261859 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1860
+ trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1861
+ }
19271862
19281863 return (err < 0 ? err : 0);
19291864 }
....@@ -1950,14 +1885,16 @@
19501885 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
19511886 u64 start, u64 len)
19521887 {
1953
- struct buffer_head map_bh;
1888
+ struct f2fs_map_blocks map;
19541889 sector_t start_blk, last_blk;
19551890 pgoff_t next_pgofs;
19561891 u64 logical = 0, phys = 0, size = 0;
19571892 u32 flags = 0;
19581893 int ret = 0;
1959
- bool compr_cluster = false;
1894
+ bool compr_cluster = false, compr_appended;
19601895 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1896
+ unsigned int count_in_cluster = 0;
1897
+ loff_t maxbytes;
19611898
19621899 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
19631900 ret = f2fs_precache_extents(inode);
....@@ -1965,11 +1902,20 @@
19651902 return ret;
19661903 }
19671904
1968
- ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1905
+ ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
19691906 if (ret)
19701907 return ret;
19711908
19721909 inode_lock(inode);
1910
+
1911
+ maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1912
+ if (start > maxbytes) {
1913
+ ret = -EFBIG;
1914
+ goto out;
1915
+ }
1916
+
1917
+ if (len > maxbytes || (maxbytes - len) < start)
1918
+ len = maxbytes - start;
19731919
19741920 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
19751921 ret = f2fs_xattr_fiemap(inode, fieinfo);
....@@ -1982,41 +1928,55 @@
19821928 goto out;
19831929 }
19841930
1985
- if (logical_to_blk(inode, len) == 0)
1986
- len = blk_to_logical(inode, 1);
1931
+ if (bytes_to_blks(inode, len) == 0)
1932
+ len = blks_to_bytes(inode, 1);
19871933
1988
- start_blk = logical_to_blk(inode, start);
1989
- last_blk = logical_to_blk(inode, start + len - 1);
1934
+ start_blk = bytes_to_blks(inode, start);
1935
+ last_blk = bytes_to_blks(inode, start + len - 1);
19901936
19911937 next:
1992
- memset(&map_bh, 0, sizeof(struct buffer_head));
1993
- map_bh.b_size = len;
1938
+ memset(&map, 0, sizeof(map));
1939
+ map.m_lblk = start_blk;
1940
+ map.m_len = bytes_to_blks(inode, len);
1941
+ map.m_next_pgofs = &next_pgofs;
1942
+ map.m_seg_type = NO_CHECK_TYPE;
19941943
1995
- if (compr_cluster)
1996
- map_bh.b_size = blk_to_logical(inode, cluster_size - 1);
1944
+ if (compr_cluster) {
1945
+ map.m_lblk += 1;
1946
+ map.m_len = cluster_size - count_in_cluster;
1947
+ }
19971948
1998
- ret = get_data_block(inode, start_blk, &map_bh, 0,
1999
- F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1949
+ ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
20001950 if (ret)
20011951 goto out;
20021952
20031953 /* HOLE */
2004
- if (!buffer_mapped(&map_bh)) {
1954
+ if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
20051955 start_blk = next_pgofs;
20061956
2007
- if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1957
+ if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
20081958 max_inode_blocks(inode)))
20091959 goto prep_next;
20101960
20111961 flags |= FIEMAP_EXTENT_LAST;
20121962 }
20131963
1964
+ compr_appended = false;
1965
+ /* In a case of compressed cluster, append this to the last extent */
1966
+ if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
1967
+ !(map.m_flags & F2FS_MAP_FLAGS))) {
1968
+ compr_appended = true;
1969
+ goto skip_fill;
1970
+ }
1971
+
20141972 if (size) {
1973
+ flags |= FIEMAP_EXTENT_MERGED;
20151974 if (IS_ENCRYPTED(inode))
20161975 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
20171976
20181977 ret = fiemap_fill_next_extent(fieinfo, logical,
20191978 phys, size, flags);
1979
+ trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
20201980 if (ret)
20211981 goto out;
20221982 size = 0;
....@@ -2025,38 +1985,36 @@
20251985 if (start_blk > last_blk)
20261986 goto out;
20271987
2028
- if (compr_cluster) {
2029
- compr_cluster = false;
2030
-
2031
-
2032
- logical = blk_to_logical(inode, start_blk - 1);
2033
- phys = blk_to_logical(inode, map_bh.b_blocknr);
2034
- size = blk_to_logical(inode, cluster_size);
2035
-
2036
- flags |= FIEMAP_EXTENT_ENCODED;
2037
-
2038
- start_blk += cluster_size - 1;
2039
-
2040
- if (start_blk > last_blk)
2041
- goto out;
2042
-
2043
- goto prep_next;
2044
- }
2045
-
2046
- if (map_bh.b_blocknr == COMPRESS_ADDR) {
1988
+skip_fill:
1989
+ if (map.m_pblk == COMPRESS_ADDR) {
20471990 compr_cluster = true;
2048
- start_blk++;
2049
- goto prep_next;
1991
+ count_in_cluster = 1;
1992
+ } else if (compr_appended) {
1993
+ unsigned int appended_blks = cluster_size -
1994
+ count_in_cluster + 1;
1995
+ size += blks_to_bytes(inode, appended_blks);
1996
+ start_blk += appended_blks;
1997
+ compr_cluster = false;
1998
+ } else {
1999
+ logical = blks_to_bytes(inode, start_blk);
2000
+ phys = __is_valid_data_blkaddr(map.m_pblk) ?
2001
+ blks_to_bytes(inode, map.m_pblk) : 0;
2002
+ size = blks_to_bytes(inode, map.m_len);
2003
+ flags = 0;
2004
+
2005
+ if (compr_cluster) {
2006
+ flags = FIEMAP_EXTENT_ENCODED;
2007
+ count_in_cluster += map.m_len;
2008
+ if (count_in_cluster == cluster_size) {
2009
+ compr_cluster = false;
2010
+ size += blks_to_bytes(inode, 1);
2011
+ }
2012
+ } else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
2013
+ flags = FIEMAP_EXTENT_UNWRITTEN;
2014
+ }
2015
+
2016
+ start_blk += bytes_to_blks(inode, size);
20502017 }
2051
-
2052
- logical = blk_to_logical(inode, start_blk);
2053
- phys = blk_to_logical(inode, map_bh.b_blocknr);
2054
- size = map_bh.b_size;
2055
- flags = 0;
2056
- if (buffer_unwritten(&map_bh))
2057
- flags = FIEMAP_EXTENT_UNWRITTEN;
2058
-
2059
- start_blk += logical_to_blk(inode, size);
20602018
20612019 prep_next:
20622020 cond_resched();
....@@ -2089,8 +2047,7 @@
20892047 bool is_readahead)
20902048 {
20912049 struct bio *bio = *bio_ret;
2092
- const unsigned blkbits = inode->i_blkbits;
2093
- const unsigned blocksize = 1 << blkbits;
2050
+ const unsigned blocksize = blks_to_bytes(inode, 1);
20942051 sector_t block_in_file;
20952052 sector_t last_block;
20962053 sector_t last_block_in_file;
....@@ -2099,8 +2056,8 @@
20992056
21002057 block_in_file = (sector_t)page_index(page);
21012058 last_block = block_in_file + nr_pages;
2102
- last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
2103
- blkbits;
2059
+ last_block_in_file = bytes_to_blks(inode,
2060
+ f2fs_readpage_limit(inode) + blocksize - 1);
21042061 if (last_block > last_block_in_file)
21052062 last_block = last_block_in_file;
21062063
....@@ -2163,7 +2120,7 @@
21632120 *last_block_in_bio, block_nr) ||
21642121 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
21652122 submit_and_realloc:
2166
- __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2123
+ __submit_bio(F2FS_I_SB(inode), bio, DATA);
21672124 bio = NULL;
21682125 }
21692126 if (bio == NULL) {
....@@ -2193,7 +2150,7 @@
21932150 goto out;
21942151 confused:
21952152 if (bio) {
2196
- __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2153
+ __submit_bio(F2FS_I_SB(inode), bio, DATA);
21972154 bio = NULL;
21982155 }
21992156 unlock_page(page);
....@@ -2213,16 +2170,17 @@
22132170 struct bio *bio = *bio_ret;
22142171 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
22152172 sector_t last_block_in_file;
2216
- const unsigned blkbits = inode->i_blkbits;
2217
- const unsigned blocksize = 1 << blkbits;
2173
+ const unsigned blocksize = blks_to_bytes(inode, 1);
22182174 struct decompress_io_ctx *dic = NULL;
2175
+ struct extent_info ei = {};
2176
+ bool from_dnode = true;
22192177 int i;
22202178 int ret = 0;
22212179
22222180 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
22232181
2224
- last_block_in_file = (f2fs_readpage_limit(inode) +
2225
- blocksize - 1) >> blkbits;
2182
+ last_block_in_file = bytes_to_blks(inode,
2183
+ f2fs_readpage_limit(inode) + blocksize - 1);
22262184
22272185 /* get rid of pages beyond EOF */
22282186 for (i = 0; i < cc->cluster_size; i++) {
....@@ -2238,6 +2196,8 @@
22382196 continue;
22392197 }
22402198 unlock_page(page);
2199
+ if (for_write)
2200
+ put_page(page);
22412201 cc->rpages[i] = NULL;
22422202 cc->nr_rpages--;
22432203 }
....@@ -2246,20 +2206,26 @@
22462206 if (f2fs_cluster_is_empty(cc))
22472207 goto out;
22482208
2209
+ if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei))
2210
+ from_dnode = false;
2211
+
2212
+ if (!from_dnode)
2213
+ goto skip_reading_dnode;
2214
+
22492215 set_new_dnode(&dn, inode, NULL, NULL, 0);
22502216 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
22512217 if (ret)
22522218 goto out;
22532219
2254
- /* cluster was overwritten as normal cluster */
2255
- if (dn.data_blkaddr != COMPRESS_ADDR)
2256
- goto out;
2220
+ f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
22572221
2222
+skip_reading_dnode:
22582223 for (i = 1; i < cc->cluster_size; i++) {
22592224 block_t blkaddr;
22602225
2261
- blkaddr = data_blkaddr(dn.inode, dn.node_page,
2262
- dn.ofs_in_node + i);
2226
+ blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2227
+ dn.ofs_in_node + i) :
2228
+ ei.blk + i - 1;
22632229
22642230 if (!__is_valid_data_blkaddr(blkaddr))
22652231 break;
....@@ -2269,6 +2235,9 @@
22692235 goto out_put_dnode;
22702236 }
22712237 cc->nr_cpages++;
2238
+
2239
+ if (!from_dnode && i >= ei.c_len)
2240
+ break;
22722241 }
22732242
22742243 /* nothing to decompress */
....@@ -2283,13 +2252,22 @@
22832252 goto out_put_dnode;
22842253 }
22852254
2286
- for (i = 0; i < dic->nr_cpages; i++) {
2255
+ for (i = 0; i < cc->nr_cpages; i++) {
22872256 struct page *page = dic->cpages[i];
22882257 block_t blkaddr;
22892258 struct bio_post_read_ctx *ctx;
22902259
2291
- blkaddr = data_blkaddr(dn.inode, dn.node_page,
2292
- dn.ofs_in_node + i + 1);
2260
+ blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2261
+ dn.ofs_in_node + i + 1) :
2262
+ ei.blk + i;
2263
+
2264
+ f2fs_wait_on_block_writeback(inode, blkaddr);
2265
+
2266
+ if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2267
+ if (atomic_dec_and_test(&dic->remaining_pages))
2268
+ f2fs_decompress_cluster(dic, true);
2269
+ continue;
2270
+ }
22932271
22942272 if (bio && (!page_is_mergeable(sbi, bio,
22952273 *last_block_in_bio, blkaddr) ||
....@@ -2305,29 +2283,19 @@
23052283 page->index, for_write);
23062284 if (IS_ERR(bio)) {
23072285 ret = PTR_ERR(bio);
2308
- dic->failed = true;
2309
- if (refcount_sub_and_test(dic->nr_cpages - i,
2310
- &dic->ref)) {
2311
- f2fs_decompress_end_io(dic->rpages,
2312
- cc->cluster_size, true,
2313
- false);
2314
- f2fs_free_dic(dic);
2315
- }
2286
+ f2fs_decompress_end_io(dic, ret, true);
23162287 f2fs_put_dnode(&dn);
23172288 *bio_ret = NULL;
23182289 return ret;
23192290 }
23202291 }
23212292
2322
- f2fs_wait_on_block_writeback(inode, blkaddr);
2323
-
23242293 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
23252294 goto submit_and_realloc;
23262295
2327
- /* tag STEP_DECOMPRESS to handle IO in wq */
23282296 ctx = bio->bi_private;
2329
- if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2330
- ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2297
+ ctx->enabled_steps |= STEP_DECOMPRESS;
2298
+ refcount_inc(&dic->refcnt);
23312299
23322300 inc_page_count(sbi, F2FS_RD_DATA);
23332301 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
....@@ -2336,15 +2304,23 @@
23362304 *last_block_in_bio = blkaddr;
23372305 }
23382306
2339
- f2fs_put_dnode(&dn);
2307
+ if (from_dnode)
2308
+ f2fs_put_dnode(&dn);
23402309
23412310 *bio_ret = bio;
23422311 return 0;
23432312
23442313 out_put_dnode:
2345
- f2fs_put_dnode(&dn);
2314
+ if (from_dnode)
2315
+ f2fs_put_dnode(&dn);
23462316 out:
2347
- f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2317
+ for (i = 0; i < cc->cluster_size; i++) {
2318
+ if (cc->rpages[i]) {
2319
+ ClearPageUptodate(cc->rpages[i]);
2320
+ ClearPageError(cc->rpages[i]);
2321
+ unlock_page(cc->rpages[i]);
2322
+ }
2323
+ }
23482324 *bio_ret = bio;
23492325 return ret;
23502326 }
....@@ -2353,19 +2329,12 @@
23532329 /*
23542330 * This function was originally taken from fs/mpage.c, and customized for f2fs.
23552331 * Major change was from block_size == page_size in f2fs by default.
2356
- *
2357
- * Note that the aops->readpages() function is ONLY used for read-ahead. If
2358
- * this function ever deviates from doing just read-ahead, it should either
2359
- * use ->readpage() or do the necessary surgery to decouple ->readpages()
2360
- * from read-ahead.
23612332 */
2362
-int f2fs_mpage_readpages(struct address_space *mapping,
2363
- struct list_head *pages, struct page *page,
2364
- unsigned nr_pages, bool is_readahead)
2333
+static int f2fs_mpage_readpages(struct inode *inode,
2334
+ struct readahead_control *rac, struct page *page)
23652335 {
23662336 struct bio *bio = NULL;
23672337 sector_t last_block_in_bio = 0;
2368
- struct inode *inode = mapping->host;
23692338 struct f2fs_map_blocks map;
23702339 #ifdef CONFIG_F2FS_FS_COMPRESSION
23712340 struct compress_ctx cc = {
....@@ -2379,6 +2348,7 @@
23792348 .nr_cpages = 0,
23802349 };
23812350 #endif
2351
+ unsigned nr_pages = rac ? readahead_count(rac) : 1;
23822352 unsigned max_nr_pages = nr_pages;
23832353 int ret = 0;
23842354
....@@ -2392,15 +2362,9 @@
23922362 map.m_may_create = false;
23932363
23942364 for (; nr_pages; nr_pages--) {
2395
- if (pages) {
2396
- page = list_last_entry(pages, struct page, lru);
2397
-
2365
+ if (rac) {
2366
+ page = readahead_page(rac);
23982367 prefetchw(&page->flags);
2399
- list_del(&page->lru);
2400
- if (add_to_page_cache_lru(page, mapping,
2401
- page_index(page),
2402
- readahead_gfp_mask(mapping)))
2403
- goto next_page;
24042368 }
24052369
24062370 #ifdef CONFIG_F2FS_FS_COMPRESSION
....@@ -2410,8 +2374,8 @@
24102374 ret = f2fs_read_multi_pages(&cc, &bio,
24112375 max_nr_pages,
24122376 &last_block_in_bio,
2413
- is_readahead, false);
2414
- f2fs_destroy_compress_ctx(&cc);
2377
+ rac != NULL, false);
2378
+ f2fs_destroy_compress_ctx(&cc, false);
24152379 if (ret)
24162380 goto set_error_page;
24172381 }
....@@ -2433,7 +2397,7 @@
24332397 #endif
24342398
24352399 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2436
- &bio, &last_block_in_bio, is_readahead);
2400
+ &bio, &last_block_in_bio, rac);
24372401 if (ret) {
24382402 #ifdef CONFIG_F2FS_FS_COMPRESSION
24392403 set_error_page:
....@@ -2442,8 +2406,10 @@
24422406 zero_user_segment(page, 0, PAGE_SIZE);
24432407 unlock_page(page);
24442408 }
2409
+#ifdef CONFIG_F2FS_FS_COMPRESSION
24452410 next_page:
2446
- if (pages)
2411
+#endif
2412
+ if (rac)
24472413 put_page(page);
24482414
24492415 #ifdef CONFIG_F2FS_FS_COMPRESSION
....@@ -2453,16 +2419,15 @@
24532419 ret = f2fs_read_multi_pages(&cc, &bio,
24542420 max_nr_pages,
24552421 &last_block_in_bio,
2456
- is_readahead, false);
2457
- f2fs_destroy_compress_ctx(&cc);
2422
+ rac != NULL, false);
2423
+ f2fs_destroy_compress_ctx(&cc, false);
24582424 }
24592425 }
24602426 #endif
24612427 }
2462
- BUG_ON(pages && !list_empty(pages));
24632428 if (bio)
2464
- __f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
2465
- return pages ? 0 : ret;
2429
+ __submit_bio(F2FS_I_SB(inode), bio, DATA);
2430
+ return ret;
24662431 }
24672432
24682433 static int f2fs_read_data_page(struct file *file, struct page *page)
....@@ -2481,28 +2446,24 @@
24812446 if (f2fs_has_inline_data(inode))
24822447 ret = f2fs_read_inline_data(inode, page);
24832448 if (ret == -EAGAIN)
2484
- ret = f2fs_mpage_readpages(page_file_mapping(page),
2485
- NULL, page, 1, false);
2449
+ ret = f2fs_mpage_readpages(inode, NULL, page);
24862450 return ret;
24872451 }
24882452
2489
-static int f2fs_read_data_pages(struct file *file,
2490
- struct address_space *mapping,
2491
- struct list_head *pages, unsigned nr_pages)
2453
+static void f2fs_readahead(struct readahead_control *rac)
24922454 {
2493
- struct inode *inode = mapping->host;
2494
- struct page *page = list_last_entry(pages, struct page, lru);
2455
+ struct inode *inode = rac->mapping->host;
24952456
2496
- trace_f2fs_readpages(inode, page, nr_pages);
2457
+ trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
24972458
24982459 if (!f2fs_is_compress_backend_ready(inode))
2499
- return 0;
2460
+ return;
25002461
25012462 /* If the file has inline data, skip readpages */
25022463 if (f2fs_has_inline_data(inode))
2503
- return 0;
2464
+ return;
25042465
2505
- return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
2466
+ f2fs_mpage_readpages(inode, rac, NULL);
25062467 }
25072468
25082469 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
....@@ -2552,6 +2513,9 @@
25522513 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
25532514 unsigned int policy = SM_I(sbi)->ipu_policy;
25542515
2516
+ if (policy & (0x1 << F2FS_IPU_HONOR_OPU_WRITE) &&
2517
+ is_inode_flag_set(inode, FI_OPU_WRITE))
2518
+ return false;
25552519 if (policy & (0x1 << F2FS_IPU_FORCE))
25562520 return true;
25572521 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
....@@ -2586,11 +2550,15 @@
25862550
25872551 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
25882552 {
2553
+ /* swap file is migrating in aligned write mode */
2554
+ if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2555
+ return false;
2556
+
25892557 if (f2fs_is_pinned_file(inode))
25902558 return true;
25912559
25922560 /* if this is cold file, we should overwrite to avoid fragmentation */
2593
- if (file_is_cold(inode))
2561
+ if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE))
25942562 return true;
25952563
25962564 return check_inplace_update_policy(inode, fio);
....@@ -2613,10 +2581,18 @@
26132581 return true;
26142582 if (f2fs_is_atomic_file(inode))
26152583 return true;
2584
+
2585
+ /* swap file is migrating in aligned write mode */
2586
+ if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2587
+ return true;
2588
+
2589
+ if (is_inode_flag_set(inode, FI_OPU_WRITE))
2590
+ return true;
2591
+
26162592 if (fio) {
2617
- if (is_cold_data(fio->page))
2593
+ if (page_private_gcing(fio->page))
26182594 return true;
2619
- if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2595
+ if (page_private_dummy(fio->page))
26202596 return true;
26212597 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
26222598 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
....@@ -2640,14 +2616,14 @@
26402616 struct page *page = fio->page;
26412617 struct inode *inode = page->mapping->host;
26422618 struct dnode_of_data dn;
2643
- struct extent_info ei = {0,0,0};
2619
+ struct extent_info ei = {0, };
26442620 struct node_info ni;
26452621 bool ipu_force = false;
26462622 int err = 0;
26472623
26482624 set_new_dnode(&dn, inode, NULL, NULL, 0);
26492625 if (need_inplace_update(fio) &&
2650
- f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2626
+ f2fs_lookup_read_extent_cache(inode, page->index, &ei)) {
26512627 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
26522628
26532629 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
....@@ -2672,7 +2648,7 @@
26722648 /* This page is already truncated */
26732649 if (fio->old_blkaddr == NULL_ADDR) {
26742650 ClearPageUptodate(page);
2675
- clear_cold_data(page);
2651
+ clear_page_private_gcing(page);
26762652 goto out_writepage;
26772653 }
26782654 got_it:
....@@ -2719,7 +2695,7 @@
27192695 fio->need_lock = LOCK_REQ;
27202696 }
27212697
2722
- err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2698
+ err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
27232699 if (err)
27242700 goto out_writepage;
27252701
....@@ -2754,7 +2730,8 @@
27542730 sector_t *last_block,
27552731 struct writeback_control *wbc,
27562732 enum iostat_type io_type,
2757
- int compr_blocks)
2733
+ int compr_blocks,
2734
+ bool allow_balance)
27582735 {
27592736 struct inode *inode = page->mapping->host;
27602737 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
....@@ -2777,6 +2754,7 @@
27772754 .submitted = false,
27782755 .compr_blocks = compr_blocks,
27792756 .need_lock = LOCK_RETRY,
2757
+ .post_read = f2fs_post_read_required(inode),
27802758 .io_type = io_type,
27812759 .io_wbc = wbc,
27822760 .bio = bio,
....@@ -2825,8 +2803,20 @@
28252803
28262804 /* Dentry/quota blocks are controlled by checkpoint */
28272805 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2806
+ /*
2807
+ * We need to wait for node_write to avoid block allocation during
2808
+ * checkpoint. This can only happen to quota writes which can cause
2809
+ * the below discard race condition.
2810
+ */
2811
+ if (IS_NOQUOTA(inode))
2812
+ f2fs_down_read(&sbi->node_write);
2813
+
28282814 fio.need_lock = LOCK_DONE;
28292815 err = f2fs_do_write_data_page(&fio);
2816
+
2817
+ if (IS_NOQUOTA(inode))
2818
+ f2fs_up_read(&sbi->node_write);
2819
+
28302820 goto done;
28312821 }
28322822
....@@ -2869,7 +2859,7 @@
28692859 inode_dec_dirty_pages(inode);
28702860 if (err) {
28712861 ClearPageUptodate(page);
2872
- clear_cold_data(page);
2862
+ clear_page_private_gcing(page);
28732863 }
28742864
28752865 if (wbc->for_reclaim) {
....@@ -2880,7 +2870,7 @@
28802870 }
28812871 unlock_page(page);
28822872 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2883
- !F2FS_I(inode)->cp_task)
2873
+ !F2FS_I(inode)->wb_task && allow_balance)
28842874 f2fs_balance_fs(sbi, need_balance_fs);
28852875
28862876 if (unlikely(f2fs_cp_error(sbi))) {
....@@ -2927,7 +2917,7 @@
29272917 #endif
29282918
29292919 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2930
- wbc, FS_DATA_IO, 0);
2920
+ wbc, FS_DATA_IO, 0, true);
29312921 }
29322922
29332923 /*
....@@ -2962,12 +2952,11 @@
29622952 };
29632953 #endif
29642954 int nr_pages;
2965
- pgoff_t uninitialized_var(writeback_index);
29662955 pgoff_t index;
29672956 pgoff_t end; /* Inclusive */
29682957 pgoff_t done_index;
29692958 int range_whole = 0;
2970
- int tag;
2959
+ xa_mark_t tag;
29712960 int nwritten = 0;
29722961 int submitted = 0;
29732962 int i;
....@@ -2981,8 +2970,7 @@
29812970 clear_inode_flag(mapping->host, FI_HOT_DATA);
29822971
29832972 if (wbc->range_cyclic) {
2984
- writeback_index = mapping->writeback_index; /* prev offset */
2985
- index = writeback_index;
2973
+ index = mapping->writeback_index; /* prev offset */
29862974 end = -1;
29872975 } else {
29882976 index = wbc->range_start >> PAGE_SHIFT;
....@@ -3097,7 +3085,8 @@
30973085 }
30983086 #endif
30993087 ret = f2fs_write_single_data_page(page, &submitted,
3100
- &bio, &last_block, wbc, io_type, 0);
3088
+ &bio, &last_block, wbc, io_type,
3089
+ 0, true);
31013090 if (ret == AOP_WRITEPAGE_ACTIVATE)
31023091 unlock_page(page);
31033092 #ifdef CONFIG_F2FS_FS_COMPRESSION
....@@ -3152,6 +3141,8 @@
31523141 retry = 0;
31533142 }
31543143 }
3144
+ if (f2fs_compressed_file(inode))
3145
+ f2fs_destroy_compress_ctx(&cc, false);
31553146 #endif
31563147 if (retry) {
31573148 index = 0;
....@@ -3177,7 +3168,7 @@
31773168 struct writeback_control *wbc)
31783169 {
31793170 /* to avoid deadlock in path of data flush */
3180
- if (F2FS_I(inode)->cp_task)
3171
+ if (F2FS_I(inode)->wb_task)
31813172 return false;
31823173
31833174 if (!S_ISREG(inode->i_mode))
....@@ -3185,7 +3176,7 @@
31853176 if (IS_NOQUOTA(inode))
31863177 return false;
31873178
3188
- if (f2fs_compressed_file(inode))
3179
+ if (f2fs_need_compress_data(inode))
31893180 return true;
31903181 if (wbc->sync_mode != WB_SYNC_ALL)
31913182 return true;
....@@ -3222,8 +3213,8 @@
32223213 f2fs_available_free_memory(sbi, DIRTY_DENTS))
32233214 goto skip_write;
32243215
3225
- /* skip writing during file defragment */
3226
- if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3216
+ /* skip writing in file defragment preparing stage */
3217
+ if (is_inode_flag_set(inode, FI_SKIP_WRITES))
32273218 goto skip_write;
32283219
32293220 trace_f2fs_writepages(mapping->host, wbc, DATA);
....@@ -3231,8 +3222,12 @@
32313222 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
32323223 if (wbc->sync_mode == WB_SYNC_ALL)
32333224 atomic_inc(&sbi->wb_sync_req[DATA]);
3234
- else if (atomic_read(&sbi->wb_sync_req[DATA]))
3225
+ else if (atomic_read(&sbi->wb_sync_req[DATA])) {
3226
+ /* to avoid potential deadlock */
3227
+ if (current->plug)
3228
+ blk_finish_plug(current->plug);
32353229 goto skip_write;
3230
+ }
32363231
32373232 if (__should_serialize_io(inode, wbc)) {
32383233 mutex_lock(&sbi->writepages);
....@@ -3282,14 +3277,14 @@
32823277
32833278 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
32843279 if (to > i_size && !f2fs_verity_in_progress(inode)) {
3285
- down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3286
- down_write(&F2FS_I(inode)->i_mmap_sem);
3280
+ f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3281
+ f2fs_down_write(&F2FS_I(inode)->i_mmap_sem);
32873282
32883283 truncate_pagecache(inode, i_size);
32893284 f2fs_truncate_blocks(inode, i_size, true);
32903285
3291
- up_write(&F2FS_I(inode)->i_mmap_sem);
3292
- up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3286
+ f2fs_up_write(&F2FS_I(inode)->i_mmap_sem);
3287
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
32933288 }
32943289 }
32953290
....@@ -3302,7 +3297,7 @@
33023297 struct dnode_of_data dn;
33033298 struct page *ipage;
33043299 bool locked = false;
3305
- struct extent_info ei = {0,0,0};
3300
+ struct extent_info ei = {0, };
33063301 int err = 0;
33073302 int flag;
33083303
....@@ -3323,7 +3318,7 @@
33233318
33243319 if (f2fs_has_inline_data(inode) ||
33253320 (pos & PAGE_MASK) >= i_size_read(inode)) {
3326
- __do_map_lock(sbi, flag, true);
3321
+ f2fs_do_map_lock(sbi, flag, true);
33273322 locked = true;
33283323 }
33293324
....@@ -3342,7 +3337,7 @@
33423337 f2fs_do_read_inline_data(page, ipage);
33433338 set_inode_flag(inode, FI_DATA_EXIST);
33443339 if (inode->i_nlink)
3345
- set_inline_node(ipage);
3340
+ set_page_private_inline(ipage);
33463341 } else {
33473342 err = f2fs_convert_inline_page(&dn, page);
33483343 if (err)
....@@ -3353,14 +3348,14 @@
33533348 } else if (locked) {
33543349 err = f2fs_get_block(&dn, index);
33553350 } else {
3356
- if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3351
+ if (f2fs_lookup_read_extent_cache(inode, index, &ei)) {
33573352 dn.data_blkaddr = ei.blk + index - ei.fofs;
33583353 } else {
33593354 /* hole case */
33603355 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
33613356 if (err || dn.data_blkaddr == NULL_ADDR) {
33623357 f2fs_put_dnode(&dn);
3363
- __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3358
+ f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
33643359 true);
33653360 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
33663361 locked = true;
....@@ -3376,7 +3371,7 @@
33763371 f2fs_put_dnode(&dn);
33773372 unlock_out:
33783373 if (locked)
3379
- __do_map_lock(sbi, flag, false);
3374
+ f2fs_do_map_lock(sbi, flag, false);
33803375 return err;
33813376 }
33823377
....@@ -3392,7 +3387,13 @@
33923387 block_t blkaddr = NULL_ADDR;
33933388 int err = 0;
33943389
3395
- if (trace_android_fs_datawrite_start_enabled()) {
3390
+ /*
3391
+ * Should avoid quota operations which can make deadlock:
3392
+ * kswapd -> f2fs_evict_inode -> dquot_drop ->
3393
+ * f2fs_dquot_commit -> f2fs_write_begin ->
3394
+ * d_obtain_alias -> __d_alloc -> kmem_cache_alloc(GFP_KERNEL)
3395
+ */
3396
+ if (trace_android_fs_datawrite_start_enabled() && !IS_NOQUOTA(inode)) {
33963397 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
33973398
33983399 path = android_fstrace_get_pathname(pathbuf,
....@@ -3433,6 +3434,9 @@
34333434 int ret;
34343435
34353436 *fsdata = NULL;
3437
+
3438
+ if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
3439
+ goto repeat;
34363440
34373441 ret = f2fs_prepare_compress_overwrite(inode, pagep,
34383442 index, fsdata);
....@@ -3498,7 +3502,7 @@
34983502 err = -EFSCORRUPTED;
34993503 goto fail;
35003504 }
3501
- err = f2fs_submit_page_read(inode, page, blkaddr, true);
3505
+ err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
35023506 if (err)
35033507 goto fail;
35043508
....@@ -3549,6 +3553,10 @@
35493553 if (f2fs_compressed_file(inode) && fsdata) {
35503554 f2fs_compress_write_end(inode, fsdata, page->index, copied);
35513555 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3556
+
3557
+ if (pos + copied > i_size_read(inode) &&
3558
+ !f2fs_verity_in_progress(inode))
3559
+ f2fs_i_size_write(inode, pos + copied);
35523560 return copied;
35533561 }
35543562 #endif
....@@ -3600,7 +3608,7 @@
36003608 bio->bi_private = dio->orig_private;
36013609 bio->bi_end_io = dio->orig_end_io;
36023610
3603
- kvfree(dio);
3611
+ kfree(dio);
36043612
36053613 bio_endio(bio);
36063614 }
....@@ -3686,21 +3694,21 @@
36863694 iocb->ki_hint = WRITE_LIFE_NOT_SET;
36873695
36883696 if (iocb->ki_flags & IOCB_NOWAIT) {
3689
- if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3697
+ if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[rw])) {
36903698 iocb->ki_hint = hint;
36913699 err = -EAGAIN;
36923700 goto out;
36933701 }
3694
- if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3695
- up_read(&fi->i_gc_rwsem[rw]);
3702
+ if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
3703
+ f2fs_up_read(&fi->i_gc_rwsem[rw]);
36963704 iocb->ki_hint = hint;
36973705 err = -EAGAIN;
36983706 goto out;
36993707 }
37003708 } else {
3701
- down_read(&fi->i_gc_rwsem[rw]);
3709
+ f2fs_down_read(&fi->i_gc_rwsem[rw]);
37023710 if (do_opu)
3703
- down_read(&fi->i_gc_rwsem[READ]);
3711
+ f2fs_down_read(&fi->i_gc_rwsem[READ]);
37043712 }
37053713
37063714 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
....@@ -3710,9 +3718,9 @@
37103718 DIO_SKIP_HOLES);
37113719
37123720 if (do_opu)
3713
- up_read(&fi->i_gc_rwsem[READ]);
3721
+ f2fs_up_read(&fi->i_gc_rwsem[READ]);
37143722
3715
- up_read(&fi->i_gc_rwsem[rw]);
3723
+ f2fs_up_read(&fi->i_gc_rwsem[rw]);
37163724
37173725 if (rw == WRITE) {
37183726 if (whint_mode == WHINT_MODE_OFF)
....@@ -3722,12 +3730,18 @@
37223730 err);
37233731 if (!do_opu)
37243732 set_inode_flag(inode, FI_UPDATE_WRITE);
3733
+ } else if (err == -EIOCBQUEUED) {
3734
+ f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3735
+ count - iov_iter_count(iter));
37253736 } else if (err < 0) {
37263737 f2fs_write_failed(mapping, offset + count);
37273738 }
37283739 } else {
37293740 if (err > 0)
37303741 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3742
+ else if (err == -EIOCBQUEUED)
3743
+ f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3744
+ count - iov_iter_count(iter));
37313745 }
37323746
37333747 out:
....@@ -3764,12 +3778,20 @@
37643778 }
37653779 }
37663780
3767
- clear_cold_data(page);
3781
+ clear_page_private_gcing(page);
37683782
3769
- if (IS_ATOMIC_WRITTEN_PAGE(page))
3783
+ if (test_opt(sbi, COMPRESS_CACHE)) {
3784
+ if (f2fs_compressed_file(inode))
3785
+ f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3786
+ if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3787
+ clear_page_private_data(page);
3788
+ }
3789
+
3790
+ if (page_private_atomic(page))
37703791 return f2fs_drop_inmem_page(inode, page);
37713792
3772
- f2fs_clear_page_private(page);
3793
+ detach_page_private(page);
3794
+ set_page_private(page, 0);
37733795 }
37743796
37753797 int f2fs_release_page(struct page *page, gfp_t wait)
....@@ -3779,11 +3801,23 @@
37793801 return 0;
37803802
37813803 /* This is atomic written page, keep Private */
3782
- if (IS_ATOMIC_WRITTEN_PAGE(page))
3804
+ if (page_private_atomic(page))
37833805 return 0;
37843806
3785
- clear_cold_data(page);
3786
- f2fs_clear_page_private(page);
3807
+ if (test_opt(F2FS_P_SB(page), COMPRESS_CACHE)) {
3808
+ struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3809
+ struct inode *inode = page->mapping->host;
3810
+
3811
+ if (f2fs_compressed_file(inode))
3812
+ f2fs_invalidate_compress_pages(sbi, inode->i_ino);
3813
+ if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3814
+ clear_page_private_data(page);
3815
+ }
3816
+
3817
+ clear_page_private_gcing(page);
3818
+
3819
+ detach_page_private(page);
3820
+ set_page_private(page, 0);
37873821 return 1;
37883822 }
37893823
....@@ -3799,7 +3833,7 @@
37993833 return __set_page_dirty_nobuffers(page);
38003834
38013835 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3802
- if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3836
+ if (!page_private_atomic(page)) {
38033837 f2fs_register_inmem_page(inode, page);
38043838 return 1;
38053839 }
....@@ -3841,10 +3875,9 @@
38413875 }
38423876
38433877 f2fs_put_dnode(&dn);
3844
-
38453878 return blknr;
38463879 #else
3847
- return -EOPNOTSUPP;
3880
+ return 0;
38483881 #endif
38493882 }
38503883
....@@ -3852,18 +3885,36 @@
38523885 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
38533886 {
38543887 struct inode *inode = mapping->host;
3888
+ sector_t blknr = 0;
38553889
38563890 if (f2fs_has_inline_data(inode))
3857
- return 0;
3891
+ goto out;
38583892
38593893 /* make sure allocating whole blocks */
38603894 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
38613895 filemap_write_and_wait(mapping);
38623896
3863
- if (f2fs_compressed_file(inode))
3864
- return f2fs_bmap_compress(inode, block);
3897
+ /* Block number less than F2FS MAX BLOCKS */
3898
+ if (unlikely(block >= max_file_blocks(inode)))
3899
+ goto out;
38653900
3866
- return generic_block_bmap(mapping, block, get_data_block_bmap);
3901
+ if (f2fs_compressed_file(inode)) {
3902
+ blknr = f2fs_bmap_compress(inode, block);
3903
+ } else {
3904
+ struct f2fs_map_blocks map;
3905
+
3906
+ memset(&map, 0, sizeof(map));
3907
+ map.m_lblk = block;
3908
+ map.m_len = 1;
3909
+ map.m_next_pgofs = NULL;
3910
+ map.m_seg_type = NO_CHECK_TYPE;
3911
+
3912
+ if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3913
+ blknr = map.m_pblk;
3914
+ }
3915
+out:
3916
+ trace_f2fs_bmap(inode, block, blknr);
3917
+ return blknr;
38673918 }
38683919
38693920 #ifdef CONFIG_MIGRATION
....@@ -3874,7 +3925,7 @@
38743925 {
38753926 int rc, extra_count;
38763927 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3877
- bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3928
+ bool atomic_written = page_private_atomic(page);
38783929
38793930 BUG_ON(PageWriteback(page));
38803931
....@@ -3889,7 +3940,7 @@
38893940 /* one extra reference was held for atomic_write page */
38903941 extra_count = atomic_written ? 1 : 0;
38913942 rc = migrate_page_move_mapping(mapping, newpage,
3892
- page, NULL, mode, extra_count);
3943
+ page, extra_count);
38933944 if (rc != MIGRATEPAGE_SUCCESS) {
38943945 if (atomic_written)
38953946 mutex_unlock(&fi->inmem_lock);
....@@ -3898,6 +3949,7 @@
38983949
38993950 if (atomic_written) {
39003951 struct inmem_pages *cur;
3952
+
39013953 list_for_each_entry(cur, &fi->inmem_pages, list)
39023954 if (cur->page == page) {
39033955 cur->page = newpage;
....@@ -3908,9 +3960,16 @@
39083960 get_page(newpage);
39093961 }
39103962
3963
+ /* guarantee to start from no stale private field */
3964
+ set_page_private(newpage, 0);
39113965 if (PagePrivate(page)) {
3912
- f2fs_set_page_private(newpage, page_private(page));
3913
- f2fs_clear_page_private(page);
3966
+ set_page_private(newpage, page_private(page));
3967
+ SetPagePrivate(newpage);
3968
+ get_page(newpage);
3969
+
3970
+ set_page_private(page, 0);
3971
+ ClearPagePrivate(page);
3972
+ put_page(page);
39143973 }
39153974
39163975 if (mode != MIGRATE_SYNC_NO_COPY)
....@@ -3923,97 +3982,172 @@
39233982 #endif
39243983
39253984 #ifdef CONFIG_SWAP
3926
-/* Copied from generic_swapfile_activate() to check any holes */
3985
+static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3986
+ unsigned int blkcnt)
3987
+{
3988
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3989
+ unsigned int blkofs;
3990
+ unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3991
+ unsigned int secidx = start_blk / blk_per_sec;
3992
+ unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3993
+ int ret = 0;
3994
+
3995
+ f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3996
+ f2fs_down_write(&F2FS_I(inode)->i_mmap_sem);
3997
+
3998
+ set_inode_flag(inode, FI_ALIGNED_WRITE);
3999
+ set_inode_flag(inode, FI_OPU_WRITE);
4000
+
4001
+ for (; secidx < end_sec; secidx++) {
4002
+ f2fs_down_write(&sbi->pin_sem);
4003
+
4004
+ f2fs_lock_op(sbi);
4005
+ f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
4006
+ f2fs_unlock_op(sbi);
4007
+
4008
+ set_inode_flag(inode, FI_SKIP_WRITES);
4009
+
4010
+ for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
4011
+ struct page *page;
4012
+ unsigned int blkidx = secidx * blk_per_sec + blkofs;
4013
+
4014
+ page = f2fs_get_lock_data_page(inode, blkidx, true);
4015
+ if (IS_ERR(page)) {
4016
+ f2fs_up_write(&sbi->pin_sem);
4017
+ ret = PTR_ERR(page);
4018
+ goto done;
4019
+ }
4020
+
4021
+ set_page_dirty(page);
4022
+ f2fs_put_page(page, 1);
4023
+ }
4024
+
4025
+ clear_inode_flag(inode, FI_SKIP_WRITES);
4026
+
4027
+ ret = filemap_fdatawrite(inode->i_mapping);
4028
+
4029
+ f2fs_up_write(&sbi->pin_sem);
4030
+
4031
+ if (ret)
4032
+ break;
4033
+ }
4034
+
4035
+done:
4036
+ clear_inode_flag(inode, FI_SKIP_WRITES);
4037
+ clear_inode_flag(inode, FI_OPU_WRITE);
4038
+ clear_inode_flag(inode, FI_ALIGNED_WRITE);
4039
+
4040
+ f2fs_up_write(&F2FS_I(inode)->i_mmap_sem);
4041
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
4042
+
4043
+ return ret;
4044
+}
4045
+
39274046 static int check_swap_activate(struct swap_info_struct *sis,
39284047 struct file *swap_file, sector_t *span)
39294048 {
39304049 struct address_space *mapping = swap_file->f_mapping;
39314050 struct inode *inode = mapping->host;
3932
- unsigned blocks_per_page;
3933
- unsigned long page_no;
3934
- unsigned blkbits;
3935
- sector_t probe_block;
3936
- sector_t last_block;
3937
- sector_t lowest_block = -1;
3938
- sector_t highest_block = 0;
4051
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4052
+ sector_t cur_lblock;
4053
+ sector_t last_lblock;
4054
+ sector_t pblock;
4055
+ sector_t lowest_pblock = -1;
4056
+ sector_t highest_pblock = 0;
39394057 int nr_extents = 0;
3940
- int ret;
3941
-
3942
- blkbits = inode->i_blkbits;
3943
- blocks_per_page = PAGE_SIZE >> blkbits;
4058
+ unsigned long nr_pblocks;
4059
+ unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
4060
+ unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
4061
+ unsigned int not_aligned = 0;
4062
+ int ret = 0;
39444063
39454064 /*
39464065 * Map all the blocks into the extent list. This code doesn't try
39474066 * to be very smart.
39484067 */
3949
- probe_block = 0;
3950
- page_no = 0;
3951
- last_block = i_size_read(inode) >> blkbits;
3952
- while ((probe_block + blocks_per_page) <= last_block &&
3953
- page_no < sis->max) {
3954
- unsigned block_in_page;
3955
- sector_t first_block;
4068
+ cur_lblock = 0;
4069
+ last_lblock = bytes_to_blks(inode, i_size_read(inode));
39564070
4071
+ while (cur_lblock < last_lblock && cur_lblock < sis->max) {
4072
+ struct f2fs_map_blocks map;
4073
+retry:
39574074 cond_resched();
39584075
3959
- first_block = bmap(inode, probe_block);
3960
- if (first_block == 0)
3961
- goto bad_bmap;
4076
+ memset(&map, 0, sizeof(map));
4077
+ map.m_lblk = cur_lblock;
4078
+ map.m_len = last_lblock - cur_lblock;
4079
+ map.m_next_pgofs = NULL;
4080
+ map.m_next_extent = NULL;
4081
+ map.m_seg_type = NO_CHECK_TYPE;
4082
+ map.m_may_create = false;
39624083
3963
- /*
3964
- * It must be PAGE_SIZE aligned on-disk
3965
- */
3966
- if (first_block & (blocks_per_page - 1)) {
3967
- probe_block++;
3968
- goto reprobe;
4084
+ ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
4085
+ if (ret)
4086
+ goto out;
4087
+
4088
+ /* hole */
4089
+ if (!(map.m_flags & F2FS_MAP_FLAGS)) {
4090
+ f2fs_err(sbi, "Swapfile has holes");
4091
+ ret = -EINVAL;
4092
+ goto out;
39694093 }
39704094
3971
- for (block_in_page = 1; block_in_page < blocks_per_page;
3972
- block_in_page++) {
3973
- sector_t block;
4095
+ pblock = map.m_pblk;
4096
+ nr_pblocks = map.m_len;
39744097
3975
- block = bmap(inode, probe_block + block_in_page);
3976
- if (block == 0)
3977
- goto bad_bmap;
3978
- if (block != first_block + block_in_page) {
3979
- /* Discontiguity */
3980
- probe_block++;
3981
- goto reprobe;
4098
+ if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
4099
+ nr_pblocks & sec_blks_mask) {
4100
+ not_aligned++;
4101
+
4102
+ nr_pblocks = roundup(nr_pblocks, blks_per_sec);
4103
+ if (cur_lblock + nr_pblocks > sis->max)
4104
+ nr_pblocks -= blks_per_sec;
4105
+
4106
+ if (!nr_pblocks) {
4107
+ /* this extent is last one */
4108
+ nr_pblocks = map.m_len;
4109
+ f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
4110
+ goto next;
39824111 }
3983
- }
39844112
3985
- first_block >>= (PAGE_SHIFT - blkbits);
3986
- if (page_no) { /* exclude the header page */
3987
- if (first_block < lowest_block)
3988
- lowest_block = first_block;
3989
- if (first_block > highest_block)
3990
- highest_block = first_block;
4113
+ ret = f2fs_migrate_blocks(inode, cur_lblock,
4114
+ nr_pblocks);
4115
+ if (ret)
4116
+ goto out;
4117
+ goto retry;
4118
+ }
4119
+next:
4120
+ if (cur_lblock + nr_pblocks >= sis->max)
4121
+ nr_pblocks = sis->max - cur_lblock;
4122
+
4123
+ if (cur_lblock) { /* exclude the header page */
4124
+ if (pblock < lowest_pblock)
4125
+ lowest_pblock = pblock;
4126
+ if (pblock + nr_pblocks - 1 > highest_pblock)
4127
+ highest_pblock = pblock + nr_pblocks - 1;
39914128 }
39924129
39934130 /*
39944131 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
39954132 */
3996
- ret = add_swap_extent(sis, page_no, 1, first_block);
4133
+ ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
39974134 if (ret < 0)
39984135 goto out;
39994136 nr_extents += ret;
4000
- page_no++;
4001
- probe_block += blocks_per_page;
4002
-reprobe:
4003
- continue;
4137
+ cur_lblock += nr_pblocks;
40044138 }
40054139 ret = nr_extents;
4006
- *span = 1 + highest_block - lowest_block;
4007
- if (page_no == 0)
4008
- page_no = 1; /* force Empty message */
4009
- sis->max = page_no;
4010
- sis->pages = page_no - 1;
4011
- sis->highest_bit = page_no - 1;
4140
+ *span = 1 + highest_pblock - lowest_pblock;
4141
+ if (cur_lblock == 0)
4142
+ cur_lblock = 1; /* force Empty message */
4143
+ sis->max = cur_lblock;
4144
+ sis->pages = cur_lblock - 1;
4145
+ sis->highest_bit = cur_lblock - 1;
40124146 out:
4147
+ if (not_aligned)
4148
+ f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
4149
+ not_aligned, blks_per_sec * F2FS_BLKSIZE);
40134150 return ret;
4014
-bad_bmap:
4015
- pr_err("swapon: swapfile has holes\n");
4016
- return -EINVAL;
40174151 }
40184152
40194153 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
....@@ -4028,19 +4162,26 @@
40284162 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
40294163 return -EROFS;
40304164
4165
+ if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
4166
+ f2fs_err(F2FS_I_SB(inode),
4167
+ "Swapfile not supported in LFS mode");
4168
+ return -EINVAL;
4169
+ }
4170
+
40314171 ret = f2fs_convert_inline_inode(inode);
40324172 if (ret)
40334173 return ret;
40344174
4035
- if (f2fs_disable_compressed_file(inode))
4175
+ if (!f2fs_disable_compressed_file(inode))
40364176 return -EINVAL;
4177
+
4178
+ f2fs_precache_extents(inode);
40374179
40384180 ret = check_swap_activate(sis, file, span);
40394181 if (ret < 0)
40404182 return ret;
40414183
40424184 set_inode_flag(inode, FI_PIN_FILE);
4043
- f2fs_precache_extents(inode);
40444185 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
40454186 return ret;
40464187 }
....@@ -4065,7 +4206,7 @@
40654206
40664207 const struct address_space_operations f2fs_dblock_aops = {
40674208 .readpage = f2fs_read_data_page,
4068
- .readpages = f2fs_read_data_pages,
4209
+ .readahead = f2fs_readahead,
40694210 .writepage = f2fs_write_data_page,
40704211 .writepages = f2fs_write_data_pages,
40714212 .write_begin = f2fs_write_begin,
....@@ -4082,13 +4223,13 @@
40824223 #endif
40834224 };
40844225
4085
-void f2fs_clear_radix_tree_dirty_tag(struct page *page)
4226
+void f2fs_clear_page_cache_dirty_tag(struct page *page)
40864227 {
40874228 struct address_space *mapping = page_mapping(page);
40884229 unsigned long flags;
40894230
40904231 xa_lock_irqsave(&mapping->i_pages, flags);
4091
- radix_tree_tag_clear(&mapping->i_pages, page_index(page),
4232
+ __xa_clear_mark(&mapping->i_pages, page_index(page),
40924233 PAGECACHE_TAG_DIRTY);
40934234 xa_unlock_irqrestore(&mapping->i_pages, flags);
40944235 }