]>
Commit | Line | Data |
---|---|---|
0a8165d7 | 1 | /* |
eb47b800 JK |
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> | |
8f46dcae | 17 | #include <linux/pagevec.h> |
eb47b800 JK |
18 | #include <linux/blkdev.h> |
19 | #include <linux/bio.h> | |
690e4a3e | 20 | #include <linux/prefetch.h> |
e2e40f2c | 21 | #include <linux/uio.h> |
fe76b796 JK |
22 | #include <linux/mm.h> |
23 | #include <linux/memcontrol.h> | |
f1e88660 | 24 | #include <linux/cleancache.h> |
eb47b800 JK |
25 | |
26 | #include "f2fs.h" | |
27 | #include "node.h" | |
28 | #include "segment.h" | |
db9f7c1a | 29 | #include "trace.h" |
848753aa | 30 | #include <trace/events/f2fs.h> |
eb47b800 | 31 | |
36951b38 CY |
32 | static bool __is_cp_guaranteed(struct page *page) |
33 | { | |
34 | struct address_space *mapping = page->mapping; | |
35 | struct inode *inode; | |
36 | struct f2fs_sb_info *sbi; | |
37 | ||
38 | if (!mapping) | |
39 | return false; | |
40 | ||
41 | inode = mapping->host; | |
42 | sbi = F2FS_I_SB(inode); | |
43 | ||
44 | if (inode->i_ino == F2FS_META_INO(sbi) || | |
45 | inode->i_ino == F2FS_NODE_INO(sbi) || | |
46 | S_ISDIR(inode->i_mode) || | |
47 | is_cold_data(page)) | |
48 | return true; | |
49 | return false; | |
50 | } | |
51 | ||
4246a0b6 | 52 | static void f2fs_read_end_io(struct bio *bio) |
93dfe2ac | 53 | { |
f568849e LT |
54 | struct bio_vec *bvec; |
55 | int i; | |
93dfe2ac | 56 | |
8b038c70 | 57 | #ifdef CONFIG_F2FS_FAULT_INJECTION |
1ecc0c5c | 58 | if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) |
8b038c70 CY |
59 | bio->bi_error = -EIO; |
60 | #endif | |
61 | ||
4375a336 | 62 | if (f2fs_bio_encrypted(bio)) { |
4246a0b6 | 63 | if (bio->bi_error) { |
0b81d077 | 64 | fscrypt_release_ctx(bio->bi_private); |
4375a336 | 65 | } else { |
0b81d077 | 66 | fscrypt_decrypt_bio_pages(bio->bi_private, bio); |
4375a336 JK |
67 | return; |
68 | } | |
69 | } | |
70 | ||
12377024 CY |
71 | bio_for_each_segment_all(bvec, bio, i) { |
72 | struct page *page = bvec->bv_page; | |
f1e88660 | 73 | |
4246a0b6 | 74 | if (!bio->bi_error) { |
237c0790 JK |
75 | if (!PageUptodate(page)) |
76 | SetPageUptodate(page); | |
f1e88660 JK |
77 | } else { |
78 | ClearPageUptodate(page); | |
79 | SetPageError(page); | |
80 | } | |
81 | unlock_page(page); | |
82 | } | |
f1e88660 JK |
83 | bio_put(bio); |
84 | } | |
85 | ||
4246a0b6 | 86 | static void f2fs_write_end_io(struct bio *bio) |
93dfe2ac | 87 | { |
1b1f559f | 88 | struct f2fs_sb_info *sbi = bio->bi_private; |
f568849e LT |
89 | struct bio_vec *bvec; |
90 | int i; | |
93dfe2ac | 91 | |
f568849e | 92 | bio_for_each_segment_all(bvec, bio, i) { |
93dfe2ac | 93 | struct page *page = bvec->bv_page; |
36951b38 | 94 | enum count_type type = WB_DATA_TYPE(page); |
93dfe2ac | 95 | |
0b81d077 | 96 | fscrypt_pullback_bio_page(&page, true); |
4375a336 | 97 | |
4246a0b6 | 98 | if (unlikely(bio->bi_error)) { |
5114a97a | 99 | mapping_set_error(page->mapping, -EIO); |
38f91ca8 | 100 | f2fs_stop_checkpoint(sbi, true); |
93dfe2ac | 101 | } |
36951b38 CY |
102 | dec_page_count(sbi, type); |
103 | clear_cold_data(page); | |
93dfe2ac | 104 | end_page_writeback(page); |
f568849e | 105 | } |
36951b38 | 106 | if (!get_pages(sbi, F2FS_WB_CP_DATA) && |
f5730184 | 107 | wq_has_sleeper(&sbi->cp_wait)) |
93dfe2ac JK |
108 | wake_up(&sbi->cp_wait); |
109 | ||
110 | bio_put(bio); | |
111 | } | |
112 | ||
3c62be17 JK |
113 | /* |
114 | * Return true, if pre_bio's bdev is same as its target device. | |
115 | */ | |
116 | struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, | |
117 | block_t blk_addr, struct bio *bio) | |
118 | { | |
119 | struct block_device *bdev = sbi->sb->s_bdev; | |
120 | int i; | |
121 | ||
122 | for (i = 0; i < sbi->s_ndevs; i++) { | |
123 | if (FDEV(i).start_blk <= blk_addr && | |
124 | FDEV(i).end_blk >= blk_addr) { | |
125 | blk_addr -= FDEV(i).start_blk; | |
126 | bdev = FDEV(i).bdev; | |
127 | break; | |
128 | } | |
129 | } | |
130 | if (bio) { | |
131 | bio->bi_bdev = bdev; | |
132 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); | |
133 | } | |
134 | return bdev; | |
135 | } | |
136 | ||
137 | int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) | |
138 | { | |
139 | int i; | |
140 | ||
141 | for (i = 0; i < sbi->s_ndevs; i++) | |
142 | if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) | |
143 | return i; | |
144 | return 0; | |
145 | } | |
146 | ||
147 | static bool __same_bdev(struct f2fs_sb_info *sbi, | |
148 | block_t blk_addr, struct bio *bio) | |
149 | { | |
150 | return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev; | |
151 | } | |
152 | ||
940a6d34 GZ |
153 | /* |
154 | * Low-level block read/write IO operations. | |
155 | */ | |
156 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, | |
157 | int npages, bool is_read) | |
158 | { | |
159 | struct bio *bio; | |
160 | ||
740432f8 | 161 | bio = f2fs_bio_alloc(npages); |
940a6d34 | 162 | |
3c62be17 | 163 | f2fs_target_device(sbi, blk_addr, bio); |
940a6d34 | 164 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
12377024 | 165 | bio->bi_private = is_read ? NULL : sbi; |
940a6d34 GZ |
166 | |
167 | return bio; | |
168 | } | |
169 | ||
4fc29c1a LT |
170 | static inline void __submit_bio(struct f2fs_sb_info *sbi, |
171 | struct bio *bio, enum page_type type) | |
f5730184 | 172 | { |
4fc29c1a | 173 | if (!is_read_io(bio_op(bio))) { |
0bfd7a09 | 174 | if (f2fs_sb_mounted_blkzoned(sbi->sb) && |
52763a4b | 175 | current->plug && (type == DATA || type == NODE)) |
19a5f5e2 JK |
176 | blk_finish_plug(current->plug); |
177 | } | |
554b5125 JK |
178 | if (is_read_io(bio_op(bio))) |
179 | trace_f2fs_submit_read_bio(sbi->sb, type, bio); | |
180 | else | |
181 | trace_f2fs_submit_write_bio(sbi->sb, type, bio); | |
4e49ea4a | 182 | submit_bio(bio); |
f5730184 JK |
183 | } |
184 | ||
458e6197 | 185 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
93dfe2ac | 186 | { |
458e6197 | 187 | struct f2fs_io_info *fio = &io->fio; |
93dfe2ac JK |
188 | |
189 | if (!io->bio) | |
190 | return; | |
191 | ||
554b5125 JK |
192 | bio_set_op_attrs(io->bio, fio->op, fio->op_flags); |
193 | ||
04d328de | 194 | if (is_read_io(fio->op)) |
554b5125 | 195 | trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); |
6a8f8ca5 | 196 | else |
554b5125 | 197 | trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); |
04d328de | 198 | |
4fc29c1a | 199 | __submit_bio(io->sbi, io->bio, fio->type); |
93dfe2ac JK |
200 | io->bio = NULL; |
201 | } | |
202 | ||
0c3a5797 CY |
203 | static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode, |
204 | struct page *page, nid_t ino) | |
0fd785eb | 205 | { |
0fd785eb CY |
206 | struct bio_vec *bvec; |
207 | struct page *target; | |
208 | int i; | |
209 | ||
0c3a5797 | 210 | if (!io->bio) |
0fd785eb | 211 | return false; |
0c3a5797 CY |
212 | |
213 | if (!inode && !page && !ino) | |
214 | return true; | |
0fd785eb CY |
215 | |
216 | bio_for_each_segment_all(bvec, io->bio, i) { | |
217 | ||
0b81d077 | 218 | if (bvec->bv_page->mapping) |
0fd785eb | 219 | target = bvec->bv_page; |
0b81d077 JK |
220 | else |
221 | target = fscrypt_control_page(bvec->bv_page); | |
0fd785eb | 222 | |
0c3a5797 CY |
223 | if (inode && inode == target->mapping->host) |
224 | return true; | |
225 | if (page && page == target) | |
226 | return true; | |
227 | if (ino && ino == ino_of_node(target)) | |
0fd785eb | 228 | return true; |
0fd785eb CY |
229 | } |
230 | ||
0fd785eb CY |
231 | return false; |
232 | } | |
233 | ||
0c3a5797 CY |
234 | static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode, |
235 | struct page *page, nid_t ino, | |
236 | enum page_type type) | |
237 | { | |
238 | enum page_type btype = PAGE_TYPE_OF_BIO(type); | |
239 | struct f2fs_bio_info *io = &sbi->write_io[btype]; | |
240 | bool ret; | |
241 | ||
242 | down_read(&io->io_rwsem); | |
243 | ret = __has_merged_page(io, inode, page, ino); | |
244 | up_read(&io->io_rwsem); | |
245 | return ret; | |
246 | } | |
247 | ||
248 | static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, | |
249 | struct inode *inode, struct page *page, | |
250 | nid_t ino, enum page_type type, int rw) | |
93dfe2ac JK |
251 | { |
252 | enum page_type btype = PAGE_TYPE_OF_BIO(type); | |
253 | struct f2fs_bio_info *io; | |
254 | ||
255 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; | |
256 | ||
df0f8dc0 | 257 | down_write(&io->io_rwsem); |
458e6197 | 258 | |
0c3a5797 CY |
259 | if (!__has_merged_page(io, inode, page, ino)) |
260 | goto out; | |
261 | ||
458e6197 JK |
262 | /* change META to META_FLUSH in the checkpoint procedure */ |
263 | if (type >= META_FLUSH) { | |
264 | io->fio.type = META_FLUSH; | |
04d328de | 265 | io->fio.op = REQ_OP_WRITE; |
70fd7614 CH |
266 | io->fio.op_flags = REQ_PREFLUSH | REQ_META | REQ_PRIO; |
267 | if (!test_opt(sbi, NOBARRIER)) | |
268 | io->fio.op_flags |= REQ_FUA; | |
458e6197 JK |
269 | } |
270 | __submit_merged_bio(io); | |
0c3a5797 | 271 | out: |
df0f8dc0 | 272 | up_write(&io->io_rwsem); |
93dfe2ac JK |
273 | } |
274 | ||
0c3a5797 CY |
275 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type, |
276 | int rw) | |
277 | { | |
278 | __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw); | |
279 | } | |
280 | ||
281 | void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi, | |
282 | struct inode *inode, struct page *page, | |
283 | nid_t ino, enum page_type type, int rw) | |
284 | { | |
285 | if (has_merged_page(sbi, inode, page, ino, type)) | |
286 | __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw); | |
287 | } | |
288 | ||
406657dd CY |
289 | void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi) |
290 | { | |
291 | f2fs_submit_merged_bio(sbi, DATA, WRITE); | |
292 | f2fs_submit_merged_bio(sbi, NODE, WRITE); | |
293 | f2fs_submit_merged_bio(sbi, META, WRITE); | |
294 | } | |
295 | ||
93dfe2ac JK |
296 | /* |
297 | * Fill the locked page with data located in the block address. | |
298 | * Return unlocked page. | |
299 | */ | |
05ca3632 | 300 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
93dfe2ac | 301 | { |
93dfe2ac | 302 | struct bio *bio; |
0b81d077 JK |
303 | struct page *page = fio->encrypted_page ? |
304 | fio->encrypted_page : fio->page; | |
93dfe2ac | 305 | |
2ace38e0 | 306 | trace_f2fs_submit_page_bio(page, fio); |
05ca3632 | 307 | f2fs_trace_ios(fio, 0); |
93dfe2ac JK |
308 | |
309 | /* Allocate a new bio */ | |
04d328de | 310 | bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op)); |
93dfe2ac | 311 | |
09cbfeaf | 312 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
93dfe2ac | 313 | bio_put(bio); |
93dfe2ac JK |
314 | return -EFAULT; |
315 | } | |
04d328de | 316 | bio_set_op_attrs(bio, fio->op, fio->op_flags); |
93dfe2ac | 317 | |
4fc29c1a | 318 | __submit_bio(fio->sbi, bio, fio->type); |
93dfe2ac JK |
319 | return 0; |
320 | } | |
321 | ||
05ca3632 | 322 | void f2fs_submit_page_mbio(struct f2fs_io_info *fio) |
93dfe2ac | 323 | { |
05ca3632 | 324 | struct f2fs_sb_info *sbi = fio->sbi; |
458e6197 | 325 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
93dfe2ac | 326 | struct f2fs_bio_info *io; |
04d328de | 327 | bool is_read = is_read_io(fio->op); |
4375a336 | 328 | struct page *bio_page; |
93dfe2ac | 329 | |
940a6d34 | 330 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
93dfe2ac | 331 | |
7a9d7548 CY |
332 | if (fio->old_blkaddr != NEW_ADDR) |
333 | verify_block_addr(sbi, fio->old_blkaddr); | |
334 | verify_block_addr(sbi, fio->new_blkaddr); | |
93dfe2ac | 335 | |
36951b38 CY |
336 | bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
337 | ||
338 | if (!is_read) | |
339 | inc_page_count(sbi, WB_DATA_TYPE(bio_page)); | |
340 | ||
df0f8dc0 | 341 | down_write(&io->io_rwsem); |
93dfe2ac | 342 | |
7a9d7548 | 343 | if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 || |
3c62be17 JK |
344 | (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) || |
345 | !__same_bdev(sbi, fio->new_blkaddr, io->bio))) | |
458e6197 | 346 | __submit_merged_bio(io); |
93dfe2ac JK |
347 | alloc_new: |
348 | if (io->bio == NULL) { | |
7a9d7548 | 349 | io->bio = __bio_alloc(sbi, fio->new_blkaddr, |
664ba972 | 350 | BIO_MAX_PAGES, is_read); |
458e6197 | 351 | io->fio = *fio; |
93dfe2ac JK |
352 | } |
353 | ||
09cbfeaf KS |
354 | if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < |
355 | PAGE_SIZE) { | |
458e6197 | 356 | __submit_merged_bio(io); |
93dfe2ac JK |
357 | goto alloc_new; |
358 | } | |
359 | ||
7a9d7548 | 360 | io->last_block_in_bio = fio->new_blkaddr; |
05ca3632 | 361 | f2fs_trace_ios(fio, 0); |
93dfe2ac | 362 | |
df0f8dc0 | 363 | up_write(&io->io_rwsem); |
05ca3632 | 364 | trace_f2fs_submit_page_mbio(fio->page, fio); |
93dfe2ac JK |
365 | } |
366 | ||
46008c6d CY |
367 | static void __set_data_blkaddr(struct dnode_of_data *dn) |
368 | { | |
369 | struct f2fs_node *rn = F2FS_NODE(dn->node_page); | |
370 | __le32 *addr_array; | |
371 | ||
372 | /* Get physical address of data block */ | |
373 | addr_array = blkaddr_in_node(rn); | |
374 | addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); | |
375 | } | |
376 | ||
0a8165d7 | 377 | /* |
eb47b800 JK |
378 | * Lock ordering for the change of data block address: |
379 | * ->data_page | |
380 | * ->node_page | |
381 | * update block addresses in the node page | |
382 | */ | |
216a620a | 383 | void set_data_blkaddr(struct dnode_of_data *dn) |
eb47b800 | 384 | { |
46008c6d CY |
385 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true); |
386 | __set_data_blkaddr(dn); | |
387 | if (set_page_dirty(dn->node_page)) | |
12719ae1 | 388 | dn->node_changed = true; |
eb47b800 JK |
389 | } |
390 | ||
f28b3434 CY |
391 | void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
392 | { | |
393 | dn->data_blkaddr = blkaddr; | |
394 | set_data_blkaddr(dn); | |
395 | f2fs_update_extent_cache(dn); | |
396 | } | |
397 | ||
46008c6d CY |
398 | /* dn->ofs_in_node will be returned with up-to-date last block pointer */ |
399 | int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) | |
eb47b800 | 400 | { |
4081363f | 401 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
eb47b800 | 402 | |
46008c6d CY |
403 | if (!count) |
404 | return 0; | |
405 | ||
91942321 | 406 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
eb47b800 | 407 | return -EPERM; |
46008c6d | 408 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count))) |
eb47b800 JK |
409 | return -ENOSPC; |
410 | ||
46008c6d CY |
411 | trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, |
412 | dn->ofs_in_node, count); | |
413 | ||
414 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true); | |
415 | ||
416 | for (; count > 0; dn->ofs_in_node++) { | |
417 | block_t blkaddr = | |
418 | datablock_addr(dn->node_page, dn->ofs_in_node); | |
419 | if (blkaddr == NULL_ADDR) { | |
420 | dn->data_blkaddr = NEW_ADDR; | |
421 | __set_data_blkaddr(dn); | |
422 | count--; | |
423 | } | |
424 | } | |
425 | ||
426 | if (set_page_dirty(dn->node_page)) | |
427 | dn->node_changed = true; | |
eb47b800 JK |
428 | return 0; |
429 | } | |
430 | ||
46008c6d CY |
431 | /* Should keep dn->ofs_in_node unchanged */ |
432 | int reserve_new_block(struct dnode_of_data *dn) | |
433 | { | |
434 | unsigned int ofs_in_node = dn->ofs_in_node; | |
435 | int ret; | |
436 | ||
437 | ret = reserve_new_blocks(dn, 1); | |
438 | dn->ofs_in_node = ofs_in_node; | |
439 | return ret; | |
440 | } | |
441 | ||
b600965c HL |
442 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
443 | { | |
444 | bool need_put = dn->inode_page ? false : true; | |
445 | int err; | |
446 | ||
447 | err = get_dnode_of_data(dn, index, ALLOC_NODE); | |
448 | if (err) | |
449 | return err; | |
a8865372 | 450 | |
b600965c HL |
451 | if (dn->data_blkaddr == NULL_ADDR) |
452 | err = reserve_new_block(dn); | |
a8865372 | 453 | if (err || need_put) |
b600965c HL |
454 | f2fs_put_dnode(dn); |
455 | return err; | |
456 | } | |
457 | ||
759af1c9 | 458 | int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) |
eb47b800 | 459 | { |
028a41e8 | 460 | struct extent_info ei; |
759af1c9 | 461 | struct inode *inode = dn->inode; |
028a41e8 | 462 | |
759af1c9 FL |
463 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
464 | dn->data_blkaddr = ei.blk + index - ei.fofs; | |
465 | return 0; | |
429511cd | 466 | } |
028a41e8 | 467 | |
759af1c9 | 468 | return f2fs_reserve_block(dn, index); |
eb47b800 JK |
469 | } |
470 | ||
a56c7c6f | 471 | struct page *get_read_data_page(struct inode *inode, pgoff_t index, |
04d328de | 472 | int op_flags, bool for_write) |
eb47b800 | 473 | { |
eb47b800 JK |
474 | struct address_space *mapping = inode->i_mapping; |
475 | struct dnode_of_data dn; | |
476 | struct page *page; | |
cb3bc9ee | 477 | struct extent_info ei; |
eb47b800 | 478 | int err; |
cf04e8eb | 479 | struct f2fs_io_info fio = { |
05ca3632 | 480 | .sbi = F2FS_I_SB(inode), |
cf04e8eb | 481 | .type = DATA, |
04d328de MC |
482 | .op = REQ_OP_READ, |
483 | .op_flags = op_flags, | |
4375a336 | 484 | .encrypted_page = NULL, |
cf04e8eb | 485 | }; |
eb47b800 | 486 | |
4375a336 JK |
487 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
488 | return read_mapping_page(mapping, index, NULL); | |
489 | ||
a56c7c6f | 490 | page = f2fs_grab_cache_page(mapping, index, for_write); |
650495de JK |
491 | if (!page) |
492 | return ERR_PTR(-ENOMEM); | |
493 | ||
cb3bc9ee CY |
494 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
495 | dn.data_blkaddr = ei.blk + index - ei.fofs; | |
496 | goto got_it; | |
497 | } | |
498 | ||
eb47b800 | 499 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
266e97a8 | 500 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
86531d6b JK |
501 | if (err) |
502 | goto put_err; | |
eb47b800 JK |
503 | f2fs_put_dnode(&dn); |
504 | ||
6bacf52f | 505 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
86531d6b JK |
506 | err = -ENOENT; |
507 | goto put_err; | |
650495de | 508 | } |
cb3bc9ee | 509 | got_it: |
43f3eae1 JK |
510 | if (PageUptodate(page)) { |
511 | unlock_page(page); | |
eb47b800 | 512 | return page; |
43f3eae1 | 513 | } |
eb47b800 | 514 | |
d59ff4df JK |
515 | /* |
516 | * A new dentry page is allocated but not able to be written, since its | |
517 | * new inode page couldn't be allocated due to -ENOSPC. | |
518 | * In such the case, its blkaddr can be remained as NEW_ADDR. | |
519 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. | |
520 | */ | |
521 | if (dn.data_blkaddr == NEW_ADDR) { | |
09cbfeaf | 522 | zero_user_segment(page, 0, PAGE_SIZE); |
237c0790 JK |
523 | if (!PageUptodate(page)) |
524 | SetPageUptodate(page); | |
43f3eae1 | 525 | unlock_page(page); |
d59ff4df JK |
526 | return page; |
527 | } | |
eb47b800 | 528 | |
7a9d7548 | 529 | fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr; |
05ca3632 JK |
530 | fio.page = page; |
531 | err = f2fs_submit_page_bio(&fio); | |
393ff91f | 532 | if (err) |
86531d6b | 533 | goto put_err; |
43f3eae1 | 534 | return page; |
86531d6b JK |
535 | |
536 | put_err: | |
537 | f2fs_put_page(page, 1); | |
538 | return ERR_PTR(err); | |
43f3eae1 JK |
539 | } |
540 | ||
541 | struct page *find_data_page(struct inode *inode, pgoff_t index) | |
542 | { | |
543 | struct address_space *mapping = inode->i_mapping; | |
544 | struct page *page; | |
545 | ||
546 | page = find_get_page(mapping, index); | |
547 | if (page && PageUptodate(page)) | |
548 | return page; | |
549 | f2fs_put_page(page, 0); | |
550 | ||
70fd7614 | 551 | page = get_read_data_page(inode, index, 0, false); |
43f3eae1 JK |
552 | if (IS_ERR(page)) |
553 | return page; | |
554 | ||
555 | if (PageUptodate(page)) | |
556 | return page; | |
557 | ||
558 | wait_on_page_locked(page); | |
559 | if (unlikely(!PageUptodate(page))) { | |
560 | f2fs_put_page(page, 0); | |
561 | return ERR_PTR(-EIO); | |
562 | } | |
563 | return page; | |
564 | } | |
565 | ||
566 | /* | |
567 | * If it tries to access a hole, return an error. | |
568 | * Because, the callers, functions in dir.c and GC, should be able to know | |
569 | * whether this page exists or not. | |
570 | */ | |
a56c7c6f JK |
571 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index, |
572 | bool for_write) | |
43f3eae1 JK |
573 | { |
574 | struct address_space *mapping = inode->i_mapping; | |
575 | struct page *page; | |
576 | repeat: | |
70fd7614 | 577 | page = get_read_data_page(inode, index, 0, for_write); |
43f3eae1 JK |
578 | if (IS_ERR(page)) |
579 | return page; | |
393ff91f | 580 | |
43f3eae1 | 581 | /* wait for read completion */ |
393ff91f | 582 | lock_page(page); |
6bacf52f | 583 | if (unlikely(page->mapping != mapping)) { |
afcb7ca0 JK |
584 | f2fs_put_page(page, 1); |
585 | goto repeat; | |
eb47b800 | 586 | } |
1563ac75 CY |
587 | if (unlikely(!PageUptodate(page))) { |
588 | f2fs_put_page(page, 1); | |
589 | return ERR_PTR(-EIO); | |
590 | } | |
eb47b800 JK |
591 | return page; |
592 | } | |
593 | ||
0a8165d7 | 594 | /* |
eb47b800 JK |
595 | * Caller ensures that this data page is never allocated. |
596 | * A new zero-filled data page is allocated in the page cache. | |
39936837 | 597 | * |
4f4124d0 CY |
598 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
599 | * f2fs_unlock_op(). | |
470f00e9 CY |
600 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
601 | * ipage should be released by this function. | |
eb47b800 | 602 | */ |
64aa7ed9 | 603 | struct page *get_new_data_page(struct inode *inode, |
a8865372 | 604 | struct page *ipage, pgoff_t index, bool new_i_size) |
eb47b800 | 605 | { |
eb47b800 JK |
606 | struct address_space *mapping = inode->i_mapping; |
607 | struct page *page; | |
608 | struct dnode_of_data dn; | |
609 | int err; | |
7612118a | 610 | |
a56c7c6f | 611 | page = f2fs_grab_cache_page(mapping, index, true); |
470f00e9 CY |
612 | if (!page) { |
613 | /* | |
614 | * before exiting, we should make sure ipage will be released | |
615 | * if any error occur. | |
616 | */ | |
617 | f2fs_put_page(ipage, 1); | |
01f28610 | 618 | return ERR_PTR(-ENOMEM); |
470f00e9 | 619 | } |
eb47b800 | 620 | |
a8865372 | 621 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
b600965c | 622 | err = f2fs_reserve_block(&dn, index); |
01f28610 JK |
623 | if (err) { |
624 | f2fs_put_page(page, 1); | |
eb47b800 | 625 | return ERR_PTR(err); |
a8865372 | 626 | } |
01f28610 JK |
627 | if (!ipage) |
628 | f2fs_put_dnode(&dn); | |
eb47b800 JK |
629 | |
630 | if (PageUptodate(page)) | |
01f28610 | 631 | goto got_it; |
eb47b800 JK |
632 | |
633 | if (dn.data_blkaddr == NEW_ADDR) { | |
09cbfeaf | 634 | zero_user_segment(page, 0, PAGE_SIZE); |
237c0790 JK |
635 | if (!PageUptodate(page)) |
636 | SetPageUptodate(page); | |
eb47b800 | 637 | } else { |
4375a336 | 638 | f2fs_put_page(page, 1); |
a8865372 | 639 | |
7612118a JK |
640 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
641 | f2fs_bug_on(F2FS_I_SB(inode), ipage); | |
642 | page = get_lock_data_page(inode, index, true); | |
4375a336 | 643 | if (IS_ERR(page)) |
7612118a | 644 | return page; |
eb47b800 | 645 | } |
01f28610 | 646 | got_it: |
9edcdabf | 647 | if (new_i_size && i_size_read(inode) < |
ee6d182f | 648 | ((loff_t)(index + 1) << PAGE_SHIFT)) |
fc9581c8 | 649 | f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); |
eb47b800 JK |
650 | return page; |
651 | } | |
652 | ||
bfad7c2d JK |
653 | static int __allocate_data_block(struct dnode_of_data *dn) |
654 | { | |
4081363f | 655 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
bfad7c2d | 656 | struct f2fs_summary sum; |
bfad7c2d | 657 | struct node_info ni; |
976e4c50 | 658 | pgoff_t fofs; |
46008c6d | 659 | blkcnt_t count = 1; |
bfad7c2d | 660 | |
91942321 | 661 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
bfad7c2d | 662 | return -EPERM; |
df6136ef CY |
663 | |
664 | dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); | |
665 | if (dn->data_blkaddr == NEW_ADDR) | |
666 | goto alloc; | |
667 | ||
46008c6d | 668 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count))) |
bfad7c2d JK |
669 | return -ENOSPC; |
670 | ||
df6136ef | 671 | alloc: |
bfad7c2d JK |
672 | get_node_info(sbi, dn->nid, &ni); |
673 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); | |
674 | ||
df6136ef | 675 | allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr, |
6ae1be13 | 676 | &sum, CURSEG_WARM_DATA); |
216a620a | 677 | set_data_blkaddr(dn); |
bfad7c2d | 678 | |
976e4c50 | 679 | /* update i_size */ |
81ca7350 | 680 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) + |
976e4c50 | 681 | dn->ofs_in_node; |
09cbfeaf | 682 | if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT)) |
fc9581c8 | 683 | f2fs_i_size_write(dn->inode, |
09cbfeaf | 684 | ((loff_t)(fofs + 1) << PAGE_SHIFT)); |
bfad7c2d JK |
685 | return 0; |
686 | } | |
687 | ||
c040ff9d JK |
688 | static inline bool __force_buffered_io(struct inode *inode, int rw) |
689 | { | |
690 | return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) || | |
691 | (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) || | |
692 | F2FS_I_SB(inode)->s_ndevs); | |
693 | } | |
694 | ||
a7de6086 | 695 | int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) |
59b802e5 | 696 | { |
b439b103 | 697 | struct inode *inode = file_inode(iocb->ki_filp); |
5b8db7fa | 698 | struct f2fs_map_blocks map; |
a7de6086 | 699 | int err = 0; |
59b802e5 | 700 | |
0080c507 | 701 | map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos); |
dfd02e4d CY |
702 | map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from)); |
703 | if (map.m_len > map.m_lblk) | |
704 | map.m_len -= map.m_lblk; | |
705 | else | |
706 | map.m_len = 0; | |
707 | ||
da85985c | 708 | map.m_next_pgofs = NULL; |
2a340760 | 709 | |
24b84912 | 710 | if (iocb->ki_flags & IOCB_DIRECT) { |
a7de6086 JK |
711 | err = f2fs_convert_inline_inode(inode); |
712 | if (err) | |
713 | return err; | |
c040ff9d JK |
714 | return f2fs_map_blocks(inode, &map, 1, |
715 | __force_buffered_io(inode, WRITE) ? | |
716 | F2FS_GET_BLOCK_PRE_AIO : | |
717 | F2FS_GET_BLOCK_PRE_DIO); | |
24b84912 JK |
718 | } |
719 | if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) { | |
a7de6086 JK |
720 | err = f2fs_convert_inline_inode(inode); |
721 | if (err) | |
722 | return err; | |
b439b103 | 723 | } |
24b84912 JK |
724 | if (!f2fs_has_inline_data(inode)) |
725 | return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO); | |
a7de6086 | 726 | return err; |
59b802e5 JK |
727 | } |
728 | ||
0a8165d7 | 729 | /* |
003a3e1d JK |
730 | * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with |
731 | * f2fs_map_blocks structure. | |
4f4124d0 CY |
732 | * If original data blocks are allocated, then give them to blockdev. |
733 | * Otherwise, | |
734 | * a. preallocate requested block addresses | |
735 | * b. do not use extent cache for better performance | |
736 | * c. give the block addresses to blockdev | |
eb47b800 | 737 | */ |
d323d005 | 738 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, |
e2b4e2bc | 739 | int create, int flag) |
eb47b800 | 740 | { |
003a3e1d | 741 | unsigned int maxblocks = map->m_len; |
eb47b800 | 742 | struct dnode_of_data dn; |
f9811703 | 743 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
ac6f1999 | 744 | int mode = create ? ALLOC_NODE : LOOKUP_NODE; |
46008c6d | 745 | pgoff_t pgofs, end_offset, end; |
bfad7c2d | 746 | int err = 0, ofs = 1; |
46008c6d CY |
747 | unsigned int ofs_in_node, last_ofs_in_node; |
748 | blkcnt_t prealloc; | |
a2e7d1bf | 749 | struct extent_info ei; |
7df3a431 | 750 | block_t blkaddr; |
eb47b800 | 751 | |
dfd02e4d CY |
752 | if (!maxblocks) |
753 | return 0; | |
754 | ||
003a3e1d JK |
755 | map->m_len = 0; |
756 | map->m_flags = 0; | |
757 | ||
758 | /* it only supports block size == page size */ | |
759 | pgofs = (pgoff_t)map->m_lblk; | |
46008c6d | 760 | end = pgofs + maxblocks; |
eb47b800 | 761 | |
24b84912 | 762 | if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
003a3e1d JK |
763 | map->m_pblk = ei.blk + pgofs - ei.fofs; |
764 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); | |
765 | map->m_flags = F2FS_MAP_MAPPED; | |
bfad7c2d | 766 | goto out; |
a2e7d1bf | 767 | } |
bfad7c2d | 768 | |
4fe71e88 | 769 | next_dnode: |
59b802e5 | 770 | if (create) |
3104af35 | 771 | f2fs_lock_op(sbi); |
eb47b800 JK |
772 | |
773 | /* When reading holes, we need its node page */ | |
774 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
bfad7c2d | 775 | err = get_dnode_of_data(&dn, pgofs, mode); |
1ec79083 | 776 | if (err) { |
43473f96 CY |
777 | if (flag == F2FS_GET_BLOCK_BMAP) |
778 | map->m_pblk = 0; | |
da85985c | 779 | if (err == -ENOENT) { |
bfad7c2d | 780 | err = 0; |
da85985c CY |
781 | if (map->m_next_pgofs) |
782 | *map->m_next_pgofs = | |
783 | get_next_page_offset(&dn, pgofs); | |
784 | } | |
bfad7c2d | 785 | goto unlock_out; |
848753aa | 786 | } |
973163fc | 787 | |
46008c6d | 788 | prealloc = 0; |
230436b3 | 789 | last_ofs_in_node = ofs_in_node = dn.ofs_in_node; |
81ca7350 | 790 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
4fe71e88 CY |
791 | |
792 | next_block: | |
793 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); | |
794 | ||
795 | if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) { | |
973163fc | 796 | if (create) { |
f9811703 CY |
797 | if (unlikely(f2fs_cp_error(sbi))) { |
798 | err = -EIO; | |
4fe71e88 | 799 | goto sync_out; |
f9811703 | 800 | } |
24b84912 | 801 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
46008c6d CY |
802 | if (blkaddr == NULL_ADDR) { |
803 | prealloc++; | |
804 | last_ofs_in_node = dn.ofs_in_node; | |
805 | } | |
24b84912 JK |
806 | } else { |
807 | err = __allocate_data_block(&dn); | |
6f2d8ed6 | 808 | if (!err) |
91942321 | 809 | set_inode_flag(inode, FI_APPEND_WRITE); |
24b84912 | 810 | } |
973163fc | 811 | if (err) |
4fe71e88 | 812 | goto sync_out; |
973163fc | 813 | map->m_flags = F2FS_MAP_NEW; |
4fe71e88 | 814 | blkaddr = dn.data_blkaddr; |
973163fc | 815 | } else { |
43473f96 CY |
816 | if (flag == F2FS_GET_BLOCK_BMAP) { |
817 | map->m_pblk = 0; | |
818 | goto sync_out; | |
819 | } | |
da85985c CY |
820 | if (flag == F2FS_GET_BLOCK_FIEMAP && |
821 | blkaddr == NULL_ADDR) { | |
822 | if (map->m_next_pgofs) | |
823 | *map->m_next_pgofs = pgofs + 1; | |
824 | } | |
973163fc | 825 | if (flag != F2FS_GET_BLOCK_FIEMAP || |
43473f96 | 826 | blkaddr != NEW_ADDR) |
4fe71e88 | 827 | goto sync_out; |
e2b4e2bc | 828 | } |
e2b4e2bc | 829 | } |
eb47b800 | 830 | |
46008c6d CY |
831 | if (flag == F2FS_GET_BLOCK_PRE_AIO) |
832 | goto skip; | |
833 | ||
4fe71e88 CY |
834 | if (map->m_len == 0) { |
835 | /* preallocated unwritten block should be mapped for fiemap. */ | |
836 | if (blkaddr == NEW_ADDR) | |
837 | map->m_flags |= F2FS_MAP_UNWRITTEN; | |
838 | map->m_flags |= F2FS_MAP_MAPPED; | |
839 | ||
840 | map->m_pblk = blkaddr; | |
841 | map->m_len = 1; | |
842 | } else if ((map->m_pblk != NEW_ADDR && | |
843 | blkaddr == (map->m_pblk + ofs)) || | |
b439b103 | 844 | (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) || |
46008c6d | 845 | flag == F2FS_GET_BLOCK_PRE_DIO) { |
4fe71e88 CY |
846 | ofs++; |
847 | map->m_len++; | |
848 | } else { | |
849 | goto sync_out; | |
850 | } | |
bfad7c2d | 851 | |
46008c6d | 852 | skip: |
bfad7c2d JK |
853 | dn.ofs_in_node++; |
854 | pgofs++; | |
855 | ||
46008c6d CY |
856 | /* preallocate blocks in batch for one dnode page */ |
857 | if (flag == F2FS_GET_BLOCK_PRE_AIO && | |
858 | (pgofs == end || dn.ofs_in_node == end_offset)) { | |
7df3a431 | 859 | |
46008c6d CY |
860 | dn.ofs_in_node = ofs_in_node; |
861 | err = reserve_new_blocks(&dn, prealloc); | |
862 | if (err) | |
863 | goto sync_out; | |
bfad7c2d | 864 | |
46008c6d CY |
865 | map->m_len += dn.ofs_in_node - ofs_in_node; |
866 | if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { | |
867 | err = -ENOSPC; | |
868 | goto sync_out; | |
3104af35 | 869 | } |
46008c6d CY |
870 | dn.ofs_in_node = end_offset; |
871 | } | |
872 | ||
873 | if (pgofs >= end) | |
874 | goto sync_out; | |
875 | else if (dn.ofs_in_node < end_offset) | |
876 | goto next_block; | |
877 | ||
46008c6d CY |
878 | f2fs_put_dnode(&dn); |
879 | ||
880 | if (create) { | |
881 | f2fs_unlock_op(sbi); | |
6f2d8ed6 | 882 | f2fs_balance_fs(sbi, dn.node_changed); |
eb47b800 | 883 | } |
46008c6d | 884 | goto next_dnode; |
7df3a431 | 885 | |
bfad7c2d | 886 | sync_out: |
eb47b800 | 887 | f2fs_put_dnode(&dn); |
bfad7c2d | 888 | unlock_out: |
2a340760 | 889 | if (create) { |
3104af35 | 890 | f2fs_unlock_op(sbi); |
6f2d8ed6 | 891 | f2fs_balance_fs(sbi, dn.node_changed); |
2a340760 | 892 | } |
bfad7c2d | 893 | out: |
003a3e1d | 894 | trace_f2fs_map_blocks(inode, map, err); |
bfad7c2d | 895 | return err; |
eb47b800 JK |
896 | } |
897 | ||
003a3e1d | 898 | static int __get_data_block(struct inode *inode, sector_t iblock, |
da85985c CY |
899 | struct buffer_head *bh, int create, int flag, |
900 | pgoff_t *next_pgofs) | |
003a3e1d JK |
901 | { |
902 | struct f2fs_map_blocks map; | |
a7de6086 | 903 | int err; |
003a3e1d JK |
904 | |
905 | map.m_lblk = iblock; | |
906 | map.m_len = bh->b_size >> inode->i_blkbits; | |
da85985c | 907 | map.m_next_pgofs = next_pgofs; |
003a3e1d | 908 | |
a7de6086 JK |
909 | err = f2fs_map_blocks(inode, &map, create, flag); |
910 | if (!err) { | |
003a3e1d JK |
911 | map_bh(bh, inode->i_sb, map.m_pblk); |
912 | bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; | |
913 | bh->b_size = map.m_len << inode->i_blkbits; | |
914 | } | |
a7de6086 | 915 | return err; |
003a3e1d JK |
916 | } |
917 | ||
ccfb3000 | 918 | static int get_data_block(struct inode *inode, sector_t iblock, |
da85985c CY |
919 | struct buffer_head *bh_result, int create, int flag, |
920 | pgoff_t *next_pgofs) | |
e2b4e2bc | 921 | { |
da85985c CY |
922 | return __get_data_block(inode, iblock, bh_result, create, |
923 | flag, next_pgofs); | |
e2b4e2bc CY |
924 | } |
925 | ||
926 | static int get_data_block_dio(struct inode *inode, sector_t iblock, | |
ccfb3000 JK |
927 | struct buffer_head *bh_result, int create) |
928 | { | |
e2b4e2bc | 929 | return __get_data_block(inode, iblock, bh_result, create, |
da85985c | 930 | F2FS_GET_BLOCK_DIO, NULL); |
ccfb3000 JK |
931 | } |
932 | ||
e2b4e2bc | 933 | static int get_data_block_bmap(struct inode *inode, sector_t iblock, |
ccfb3000 JK |
934 | struct buffer_head *bh_result, int create) |
935 | { | |
179448bf | 936 | /* Block number less than F2FS MAX BLOCKS */ |
e0afc4d6 | 937 | if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) |
179448bf YH |
938 | return -EFBIG; |
939 | ||
e2b4e2bc | 940 | return __get_data_block(inode, iblock, bh_result, create, |
da85985c | 941 | F2FS_GET_BLOCK_BMAP, NULL); |
ccfb3000 JK |
942 | } |
943 | ||
7f63eb77 JK |
944 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
945 | { | |
946 | return (offset >> inode->i_blkbits); | |
947 | } | |
948 | ||
949 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) | |
950 | { | |
951 | return (blk << inode->i_blkbits); | |
952 | } | |
953 | ||
9ab70134 JK |
954 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
955 | u64 start, u64 len) | |
956 | { | |
7f63eb77 JK |
957 | struct buffer_head map_bh; |
958 | sector_t start_blk, last_blk; | |
da85985c | 959 | pgoff_t next_pgofs; |
7f63eb77 JK |
960 | u64 logical = 0, phys = 0, size = 0; |
961 | u32 flags = 0; | |
7f63eb77 JK |
962 | int ret = 0; |
963 | ||
964 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); | |
965 | if (ret) | |
966 | return ret; | |
967 | ||
67f8cf3c JK |
968 | if (f2fs_has_inline_data(inode)) { |
969 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); | |
970 | if (ret != -EAGAIN) | |
971 | return ret; | |
972 | } | |
973 | ||
5955102c | 974 | inode_lock(inode); |
de1475cc | 975 | |
7f63eb77 JK |
976 | if (logical_to_blk(inode, len) == 0) |
977 | len = blk_to_logical(inode, 1); | |
978 | ||
979 | start_blk = logical_to_blk(inode, start); | |
980 | last_blk = logical_to_blk(inode, start + len - 1); | |
9a950d52 | 981 | |
7f63eb77 JK |
982 | next: |
983 | memset(&map_bh, 0, sizeof(struct buffer_head)); | |
984 | map_bh.b_size = len; | |
985 | ||
e2b4e2bc | 986 | ret = get_data_block(inode, start_blk, &map_bh, 0, |
da85985c | 987 | F2FS_GET_BLOCK_FIEMAP, &next_pgofs); |
7f63eb77 JK |
988 | if (ret) |
989 | goto out; | |
990 | ||
991 | /* HOLE */ | |
992 | if (!buffer_mapped(&map_bh)) { | |
da85985c | 993 | start_blk = next_pgofs; |
58736fa6 CY |
994 | |
995 | if (blk_to_logical(inode, start_blk) < blk_to_logical(inode, | |
996 | F2FS_I_SB(inode)->max_file_blocks)) | |
9a950d52 | 997 | goto prep_next; |
58736fa6 | 998 | |
9a950d52 FL |
999 | flags |= FIEMAP_EXTENT_LAST; |
1000 | } | |
7f63eb77 | 1001 | |
da5af127 CY |
1002 | if (size) { |
1003 | if (f2fs_encrypted_inode(inode)) | |
1004 | flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; | |
1005 | ||
9a950d52 FL |
1006 | ret = fiemap_fill_next_extent(fieinfo, logical, |
1007 | phys, size, flags); | |
da5af127 | 1008 | } |
7f63eb77 | 1009 | |
9a950d52 FL |
1010 | if (start_blk > last_blk || ret) |
1011 | goto out; | |
7f63eb77 | 1012 | |
9a950d52 FL |
1013 | logical = blk_to_logical(inode, start_blk); |
1014 | phys = blk_to_logical(inode, map_bh.b_blocknr); | |
1015 | size = map_bh.b_size; | |
1016 | flags = 0; | |
1017 | if (buffer_unwritten(&map_bh)) | |
1018 | flags = FIEMAP_EXTENT_UNWRITTEN; | |
7f63eb77 | 1019 | |
9a950d52 | 1020 | start_blk += logical_to_blk(inode, size); |
7f63eb77 | 1021 | |
9a950d52 | 1022 | prep_next: |
7f63eb77 JK |
1023 | cond_resched(); |
1024 | if (fatal_signal_pending(current)) | |
1025 | ret = -EINTR; | |
1026 | else | |
1027 | goto next; | |
1028 | out: | |
1029 | if (ret == 1) | |
1030 | ret = 0; | |
1031 | ||
5955102c | 1032 | inode_unlock(inode); |
7f63eb77 | 1033 | return ret; |
9ab70134 JK |
1034 | } |
1035 | ||
6a7a3aed WY |
1036 | static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr, |
1037 | unsigned nr_pages) | |
78682f79 CY |
1038 | { |
1039 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1040 | struct fscrypt_ctx *ctx = NULL; | |
78682f79 CY |
1041 | struct bio *bio; |
1042 | ||
1043 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { | |
1044 | ctx = fscrypt_get_ctx(inode, GFP_NOFS); | |
1045 | if (IS_ERR(ctx)) | |
1046 | return ERR_CAST(ctx); | |
1047 | ||
1048 | /* wait the page to be moved by cleaning */ | |
1049 | f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr); | |
1050 | } | |
1051 | ||
1052 | bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES)); | |
1053 | if (!bio) { | |
1054 | if (ctx) | |
1055 | fscrypt_release_ctx(ctx); | |
1056 | return ERR_PTR(-ENOMEM); | |
1057 | } | |
3c62be17 | 1058 | f2fs_target_device(sbi, blkaddr, bio); |
78682f79 CY |
1059 | bio->bi_end_io = f2fs_read_end_io; |
1060 | bio->bi_private = ctx; | |
1061 | ||
1062 | return bio; | |
1063 | } | |
1064 | ||
f1e88660 JK |
1065 | /* |
1066 | * This function was originally taken from fs/mpage.c, and customized for f2fs. | |
1067 | * Major change was from block_size == page_size in f2fs by default. | |
1068 | */ | |
1069 | static int f2fs_mpage_readpages(struct address_space *mapping, | |
1070 | struct list_head *pages, struct page *page, | |
1071 | unsigned nr_pages) | |
1072 | { | |
1073 | struct bio *bio = NULL; | |
1074 | unsigned page_idx; | |
1075 | sector_t last_block_in_bio = 0; | |
1076 | struct inode *inode = mapping->host; | |
1077 | const unsigned blkbits = inode->i_blkbits; | |
1078 | const unsigned blocksize = 1 << blkbits; | |
1079 | sector_t block_in_file; | |
1080 | sector_t last_block; | |
1081 | sector_t last_block_in_file; | |
1082 | sector_t block_nr; | |
f1e88660 JK |
1083 | struct f2fs_map_blocks map; |
1084 | ||
1085 | map.m_pblk = 0; | |
1086 | map.m_lblk = 0; | |
1087 | map.m_len = 0; | |
1088 | map.m_flags = 0; | |
da85985c | 1089 | map.m_next_pgofs = NULL; |
f1e88660 JK |
1090 | |
1091 | for (page_idx = 0; nr_pages; page_idx++, nr_pages--) { | |
1092 | ||
1093 | prefetchw(&page->flags); | |
1094 | if (pages) { | |
1095 | page = list_entry(pages->prev, struct page, lru); | |
1096 | list_del(&page->lru); | |
1097 | if (add_to_page_cache_lru(page, mapping, | |
8a5c743e MH |
1098 | page->index, |
1099 | readahead_gfp_mask(mapping))) | |
f1e88660 JK |
1100 | goto next_page; |
1101 | } | |
1102 | ||
1103 | block_in_file = (sector_t)page->index; | |
1104 | last_block = block_in_file + nr_pages; | |
1105 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> | |
1106 | blkbits; | |
1107 | if (last_block > last_block_in_file) | |
1108 | last_block = last_block_in_file; | |
1109 | ||
1110 | /* | |
1111 | * Map blocks using the previous result first. | |
1112 | */ | |
1113 | if ((map.m_flags & F2FS_MAP_MAPPED) && | |
1114 | block_in_file > map.m_lblk && | |
1115 | block_in_file < (map.m_lblk + map.m_len)) | |
1116 | goto got_it; | |
1117 | ||
1118 | /* | |
1119 | * Then do more f2fs_map_blocks() calls until we are | |
1120 | * done with this page. | |
1121 | */ | |
1122 | map.m_flags = 0; | |
1123 | ||
1124 | if (block_in_file < last_block) { | |
1125 | map.m_lblk = block_in_file; | |
1126 | map.m_len = last_block - block_in_file; | |
1127 | ||
46c9e141 | 1128 | if (f2fs_map_blocks(inode, &map, 0, |
da85985c | 1129 | F2FS_GET_BLOCK_READ)) |
f1e88660 JK |
1130 | goto set_error_page; |
1131 | } | |
1132 | got_it: | |
1133 | if ((map.m_flags & F2FS_MAP_MAPPED)) { | |
1134 | block_nr = map.m_pblk + block_in_file - map.m_lblk; | |
1135 | SetPageMappedToDisk(page); | |
1136 | ||
1137 | if (!PageUptodate(page) && !cleancache_get_page(page)) { | |
1138 | SetPageUptodate(page); | |
1139 | goto confused; | |
1140 | } | |
1141 | } else { | |
09cbfeaf | 1142 | zero_user_segment(page, 0, PAGE_SIZE); |
237c0790 JK |
1143 | if (!PageUptodate(page)) |
1144 | SetPageUptodate(page); | |
f1e88660 JK |
1145 | unlock_page(page); |
1146 | goto next_page; | |
1147 | } | |
1148 | ||
1149 | /* | |
1150 | * This page will go to BIO. Do we need to send this | |
1151 | * BIO off first? | |
1152 | */ | |
3c62be17 JK |
1153 | if (bio && (last_block_in_bio != block_nr - 1 || |
1154 | !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) { | |
f1e88660 | 1155 | submit_and_realloc: |
4fc29c1a | 1156 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
f1e88660 JK |
1157 | bio = NULL; |
1158 | } | |
1159 | if (bio == NULL) { | |
78682f79 | 1160 | bio = f2fs_grab_bio(inode, block_nr, nr_pages); |
1d353eb7 JK |
1161 | if (IS_ERR(bio)) { |
1162 | bio = NULL; | |
f1e88660 | 1163 | goto set_error_page; |
4375a336 | 1164 | } |
04d328de | 1165 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
f1e88660 JK |
1166 | } |
1167 | ||
1168 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) | |
1169 | goto submit_and_realloc; | |
1170 | ||
1171 | last_block_in_bio = block_nr; | |
1172 | goto next_page; | |
1173 | set_error_page: | |
1174 | SetPageError(page); | |
09cbfeaf | 1175 | zero_user_segment(page, 0, PAGE_SIZE); |
f1e88660 JK |
1176 | unlock_page(page); |
1177 | goto next_page; | |
1178 | confused: | |
1179 | if (bio) { | |
4fc29c1a | 1180 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
f1e88660 JK |
1181 | bio = NULL; |
1182 | } | |
1183 | unlock_page(page); | |
1184 | next_page: | |
1185 | if (pages) | |
09cbfeaf | 1186 | put_page(page); |
f1e88660 JK |
1187 | } |
1188 | BUG_ON(pages && !list_empty(pages)); | |
1189 | if (bio) | |
4fc29c1a | 1190 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
f1e88660 JK |
1191 | return 0; |
1192 | } | |
1193 | ||
eb47b800 JK |
1194 | static int f2fs_read_data_page(struct file *file, struct page *page) |
1195 | { | |
9ffe0fb5 | 1196 | struct inode *inode = page->mapping->host; |
b3d208f9 | 1197 | int ret = -EAGAIN; |
9ffe0fb5 | 1198 | |
c20e89cd CY |
1199 | trace_f2fs_readpage(page, DATA); |
1200 | ||
e1c42045 | 1201 | /* If the file has inline data, try to read it directly */ |
9ffe0fb5 HL |
1202 | if (f2fs_has_inline_data(inode)) |
1203 | ret = f2fs_read_inline_data(inode, page); | |
b3d208f9 | 1204 | if (ret == -EAGAIN) |
f1e88660 | 1205 | ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1); |
9ffe0fb5 | 1206 | return ret; |
eb47b800 JK |
1207 | } |
1208 | ||
1209 | static int f2fs_read_data_pages(struct file *file, | |
1210 | struct address_space *mapping, | |
1211 | struct list_head *pages, unsigned nr_pages) | |
1212 | { | |
9ffe0fb5 | 1213 | struct inode *inode = file->f_mapping->host; |
b8c29400 CY |
1214 | struct page *page = list_entry(pages->prev, struct page, lru); |
1215 | ||
1216 | trace_f2fs_readpages(inode, page, nr_pages); | |
9ffe0fb5 HL |
1217 | |
1218 | /* If the file has inline data, skip readpages */ | |
1219 | if (f2fs_has_inline_data(inode)) | |
1220 | return 0; | |
1221 | ||
f1e88660 | 1222 | return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages); |
eb47b800 JK |
1223 | } |
1224 | ||
05ca3632 | 1225 | int do_write_data_page(struct f2fs_io_info *fio) |
eb47b800 | 1226 | { |
05ca3632 | 1227 | struct page *page = fio->page; |
eb47b800 | 1228 | struct inode *inode = page->mapping->host; |
eb47b800 JK |
1229 | struct dnode_of_data dn; |
1230 | int err = 0; | |
1231 | ||
1232 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
266e97a8 | 1233 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
eb47b800 JK |
1234 | if (err) |
1235 | return err; | |
1236 | ||
28bc106b | 1237 | fio->old_blkaddr = dn.data_blkaddr; |
eb47b800 JK |
1238 | |
1239 | /* This page is already truncated */ | |
7a9d7548 | 1240 | if (fio->old_blkaddr == NULL_ADDR) { |
2bca1e23 | 1241 | ClearPageUptodate(page); |
eb47b800 | 1242 | goto out_writepage; |
2bca1e23 | 1243 | } |
eb47b800 | 1244 | |
4375a336 | 1245 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { |
b32e4482 | 1246 | gfp_t gfp_flags = GFP_NOFS; |
08b39fbd CY |
1247 | |
1248 | /* wait for GCed encrypted page writeback */ | |
1249 | f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode), | |
7a9d7548 | 1250 | fio->old_blkaddr); |
b32e4482 JK |
1251 | retry_encrypt: |
1252 | fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, | |
9c4bb8a3 DG |
1253 | PAGE_SIZE, 0, |
1254 | fio->page->index, | |
1255 | gfp_flags); | |
4375a336 JK |
1256 | if (IS_ERR(fio->encrypted_page)) { |
1257 | err = PTR_ERR(fio->encrypted_page); | |
b32e4482 JK |
1258 | if (err == -ENOMEM) { |
1259 | /* flush pending ios and wait for a while */ | |
1260 | f2fs_flush_merged_bios(F2FS_I_SB(inode)); | |
1261 | congestion_wait(BLK_RW_ASYNC, HZ/50); | |
1262 | gfp_flags |= __GFP_NOFAIL; | |
1263 | err = 0; | |
1264 | goto retry_encrypt; | |
1265 | } | |
4375a336 JK |
1266 | goto out_writepage; |
1267 | } | |
1268 | } | |
1269 | ||
eb47b800 JK |
1270 | set_page_writeback(page); |
1271 | ||
1272 | /* | |
1273 | * If current allocation needs SSR, | |
1274 | * it had better in-place writes for updated data. | |
1275 | */ | |
7a9d7548 | 1276 | if (unlikely(fio->old_blkaddr != NEW_ADDR && |
b25958b6 | 1277 | !is_cold_data(page) && |
2da3e027 | 1278 | !IS_ATOMIC_WRITTEN_PAGE(page) && |
b25958b6 | 1279 | need_inplace_update(inode))) { |
05ca3632 | 1280 | rewrite_data_page(fio); |
91942321 | 1281 | set_inode_flag(inode, FI_UPDATE_WRITE); |
8ce67cb0 | 1282 | trace_f2fs_do_write_data_page(page, IPU); |
eb47b800 | 1283 | } else { |
05ca3632 | 1284 | write_data_page(&dn, fio); |
8ce67cb0 | 1285 | trace_f2fs_do_write_data_page(page, OPU); |
91942321 | 1286 | set_inode_flag(inode, FI_APPEND_WRITE); |
3c6c2beb | 1287 | if (page->index == 0) |
91942321 | 1288 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); |
eb47b800 JK |
1289 | } |
1290 | out_writepage: | |
1291 | f2fs_put_dnode(&dn); | |
1292 | return err; | |
1293 | } | |
1294 | ||
1295 | static int f2fs_write_data_page(struct page *page, | |
1296 | struct writeback_control *wbc) | |
1297 | { | |
1298 | struct inode *inode = page->mapping->host; | |
4081363f | 1299 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
eb47b800 JK |
1300 | loff_t i_size = i_size_read(inode); |
1301 | const pgoff_t end_index = ((unsigned long long) i_size) | |
09cbfeaf | 1302 | >> PAGE_SHIFT; |
26de9b11 | 1303 | loff_t psize = (page->index + 1) << PAGE_SHIFT; |
9ffe0fb5 | 1304 | unsigned offset = 0; |
39936837 | 1305 | bool need_balance_fs = false; |
eb47b800 | 1306 | int err = 0; |
458e6197 | 1307 | struct f2fs_io_info fio = { |
05ca3632 | 1308 | .sbi = sbi, |
458e6197 | 1309 | .type = DATA, |
04d328de | 1310 | .op = REQ_OP_WRITE, |
7637241e | 1311 | .op_flags = wbc_to_write_flags(wbc), |
05ca3632 | 1312 | .page = page, |
4375a336 | 1313 | .encrypted_page = NULL, |
458e6197 | 1314 | }; |
eb47b800 | 1315 | |
ecda0de3 CY |
1316 | trace_f2fs_writepage(page, DATA); |
1317 | ||
eb47b800 | 1318 | if (page->index < end_index) |
39936837 | 1319 | goto write; |
eb47b800 JK |
1320 | |
1321 | /* | |
1322 | * If the offset is out-of-range of file size, | |
1323 | * this page does not have to be written to disk. | |
1324 | */ | |
09cbfeaf | 1325 | offset = i_size & (PAGE_SIZE - 1); |
76f60268 | 1326 | if ((page->index >= end_index + 1) || !offset) |
39936837 | 1327 | goto out; |
eb47b800 | 1328 | |
09cbfeaf | 1329 | zero_user_segment(page, offset, PAGE_SIZE); |
39936837 | 1330 | write: |
caf0047e | 1331 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
eb47b800 | 1332 | goto redirty_out; |
1e84371f JK |
1333 | if (f2fs_is_drop_cache(inode)) |
1334 | goto out; | |
e6e5f561 JK |
1335 | /* we should not write 0'th page having journal header */ |
1336 | if (f2fs_is_volatile_file(inode) && (!page->index || | |
1337 | (!wbc->for_reclaim && | |
1338 | available_free_memory(sbi, BASE_CHECK)))) | |
1e84371f | 1339 | goto redirty_out; |
eb47b800 | 1340 | |
cf779cab JK |
1341 | /* we should bypass data pages to proceed the kworkder jobs */ |
1342 | if (unlikely(f2fs_cp_error(sbi))) { | |
7f319975 | 1343 | mapping_set_error(page->mapping, -EIO); |
a7ffdbe2 | 1344 | goto out; |
cf779cab JK |
1345 | } |
1346 | ||
39936837 | 1347 | /* Dentry blocks are controlled by checkpoint */ |
eb47b800 | 1348 | if (S_ISDIR(inode->i_mode)) { |
05ca3632 | 1349 | err = do_write_data_page(&fio); |
8618b881 JK |
1350 | goto done; |
1351 | } | |
9ffe0fb5 | 1352 | |
8618b881 | 1353 | if (!wbc->for_reclaim) |
39936837 | 1354 | need_balance_fs = true; |
7f3037a5 | 1355 | else if (has_not_enough_free_secs(sbi, 0, 0)) |
39936837 | 1356 | goto redirty_out; |
eb47b800 | 1357 | |
b3d208f9 | 1358 | err = -EAGAIN; |
8618b881 | 1359 | f2fs_lock_op(sbi); |
b3d208f9 JK |
1360 | if (f2fs_has_inline_data(inode)) |
1361 | err = f2fs_write_inline_data(inode, page); | |
1362 | if (err == -EAGAIN) | |
05ca3632 | 1363 | err = do_write_data_page(&fio); |
26de9b11 JK |
1364 | if (F2FS_I(inode)->last_disk_size < psize) |
1365 | F2FS_I(inode)->last_disk_size = psize; | |
8618b881 JK |
1366 | f2fs_unlock_op(sbi); |
1367 | done: | |
1368 | if (err && err != -ENOENT) | |
1369 | goto redirty_out; | |
eb47b800 | 1370 | |
39936837 | 1371 | out: |
a7ffdbe2 | 1372 | inode_dec_dirty_pages(inode); |
2bca1e23 JK |
1373 | if (err) |
1374 | ClearPageUptodate(page); | |
0c3a5797 CY |
1375 | |
1376 | if (wbc->for_reclaim) { | |
1377 | f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE); | |
1378 | remove_dirty_inode(inode); | |
1379 | } | |
1380 | ||
eb47b800 | 1381 | unlock_page(page); |
2c4db1a6 | 1382 | f2fs_balance_fs(sbi, need_balance_fs); |
0c3a5797 CY |
1383 | |
1384 | if (unlikely(f2fs_cp_error(sbi))) | |
2aea39ec | 1385 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
0c3a5797 | 1386 | |
eb47b800 JK |
1387 | return 0; |
1388 | ||
eb47b800 | 1389 | redirty_out: |
76f60268 | 1390 | redirty_page_for_writepage(wbc, page); |
0002b61b CY |
1391 | if (!err) |
1392 | return AOP_WRITEPAGE_ACTIVATE; | |
b230e6ca JK |
1393 | unlock_page(page); |
1394 | return err; | |
fa9150a8 NJ |
1395 | } |
1396 | ||
8f46dcae CY |
1397 | /* |
1398 | * This function was copied from write_cche_pages from mm/page-writeback.c. | |
1399 | * The major change is making write step of cold data page separately from | |
1400 | * warm/hot data page. | |
1401 | */ | |
1402 | static int f2fs_write_cache_pages(struct address_space *mapping, | |
b230e6ca | 1403 | struct writeback_control *wbc) |
8f46dcae CY |
1404 | { |
1405 | int ret = 0; | |
1406 | int done = 0; | |
1407 | struct pagevec pvec; | |
1408 | int nr_pages; | |
1409 | pgoff_t uninitialized_var(writeback_index); | |
1410 | pgoff_t index; | |
1411 | pgoff_t end; /* Inclusive */ | |
1412 | pgoff_t done_index; | |
1413 | int cycled; | |
1414 | int range_whole = 0; | |
1415 | int tag; | |
6ca56ca4 | 1416 | int nwritten = 0; |
8f46dcae CY |
1417 | |
1418 | pagevec_init(&pvec, 0); | |
46ae957f | 1419 | |
8f46dcae CY |
1420 | if (wbc->range_cyclic) { |
1421 | writeback_index = mapping->writeback_index; /* prev offset */ | |
1422 | index = writeback_index; | |
1423 | if (index == 0) | |
1424 | cycled = 1; | |
1425 | else | |
1426 | cycled = 0; | |
1427 | end = -1; | |
1428 | } else { | |
09cbfeaf KS |
1429 | index = wbc->range_start >> PAGE_SHIFT; |
1430 | end = wbc->range_end >> PAGE_SHIFT; | |
8f46dcae CY |
1431 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
1432 | range_whole = 1; | |
1433 | cycled = 1; /* ignore range_cyclic tests */ | |
1434 | } | |
1435 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | |
1436 | tag = PAGECACHE_TAG_TOWRITE; | |
1437 | else | |
1438 | tag = PAGECACHE_TAG_DIRTY; | |
1439 | retry: | |
1440 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | |
1441 | tag_pages_for_writeback(mapping, index, end); | |
1442 | done_index = index; | |
1443 | while (!done && (index <= end)) { | |
1444 | int i; | |
1445 | ||
1446 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, | |
1447 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1); | |
1448 | if (nr_pages == 0) | |
1449 | break; | |
1450 | ||
1451 | for (i = 0; i < nr_pages; i++) { | |
1452 | struct page *page = pvec.pages[i]; | |
1453 | ||
1454 | if (page->index > end) { | |
1455 | done = 1; | |
1456 | break; | |
1457 | } | |
1458 | ||
1459 | done_index = page->index; | |
1460 | ||
1461 | lock_page(page); | |
1462 | ||
1463 | if (unlikely(page->mapping != mapping)) { | |
1464 | continue_unlock: | |
1465 | unlock_page(page); | |
1466 | continue; | |
1467 | } | |
1468 | ||
1469 | if (!PageDirty(page)) { | |
1470 | /* someone wrote it for us */ | |
1471 | goto continue_unlock; | |
1472 | } | |
1473 | ||
8f46dcae CY |
1474 | if (PageWriteback(page)) { |
1475 | if (wbc->sync_mode != WB_SYNC_NONE) | |
fec1d657 JK |
1476 | f2fs_wait_on_page_writeback(page, |
1477 | DATA, true); | |
8f46dcae CY |
1478 | else |
1479 | goto continue_unlock; | |
1480 | } | |
1481 | ||
1482 | BUG_ON(PageWriteback(page)); | |
1483 | if (!clear_page_dirty_for_io(page)) | |
1484 | goto continue_unlock; | |
1485 | ||
b230e6ca | 1486 | ret = mapping->a_ops->writepage(page, wbc); |
8f46dcae | 1487 | if (unlikely(ret)) { |
0002b61b CY |
1488 | /* |
1489 | * keep nr_to_write, since vfs uses this to | |
1490 | * get # of written pages. | |
1491 | */ | |
1492 | if (ret == AOP_WRITEPAGE_ACTIVATE) { | |
1493 | unlock_page(page); | |
1494 | ret = 0; | |
1495 | continue; | |
1496 | } | |
b230e6ca JK |
1497 | done_index = page->index + 1; |
1498 | done = 1; | |
1499 | break; | |
6ca56ca4 CY |
1500 | } else { |
1501 | nwritten++; | |
8f46dcae CY |
1502 | } |
1503 | ||
1504 | if (--wbc->nr_to_write <= 0 && | |
1505 | wbc->sync_mode == WB_SYNC_NONE) { | |
1506 | done = 1; | |
1507 | break; | |
1508 | } | |
1509 | } | |
1510 | pagevec_release(&pvec); | |
1511 | cond_resched(); | |
1512 | } | |
1513 | ||
8f46dcae CY |
1514 | if (!cycled && !done) { |
1515 | cycled = 1; | |
1516 | index = 0; | |
1517 | end = writeback_index - 1; | |
1518 | goto retry; | |
1519 | } | |
1520 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | |
1521 | mapping->writeback_index = done_index; | |
1522 | ||
6ca56ca4 CY |
1523 | if (nwritten) |
1524 | f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host, | |
1525 | NULL, 0, DATA, WRITE); | |
1526 | ||
8f46dcae CY |
1527 | return ret; |
1528 | } | |
1529 | ||
25ca923b | 1530 | static int f2fs_write_data_pages(struct address_space *mapping, |
eb47b800 JK |
1531 | struct writeback_control *wbc) |
1532 | { | |
1533 | struct inode *inode = mapping->host; | |
4081363f | 1534 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
9dfa1baf | 1535 | struct blk_plug plug; |
eb47b800 | 1536 | int ret; |
eb47b800 | 1537 | |
cfb185a1 | 1538 | /* deal with chardevs and other special file */ |
1539 | if (!mapping->a_ops->writepage) | |
1540 | return 0; | |
1541 | ||
6a290544 CY |
1542 | /* skip writing if there is no dirty page in this inode */ |
1543 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) | |
1544 | return 0; | |
1545 | ||
a1257023 JK |
1546 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
1547 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && | |
1548 | available_free_memory(sbi, DIRTY_DENTS)) | |
1549 | goto skip_write; | |
1550 | ||
d323d005 | 1551 | /* skip writing during file defragment */ |
91942321 | 1552 | if (is_inode_flag_set(inode, FI_DO_DEFRAG)) |
d323d005 CY |
1553 | goto skip_write; |
1554 | ||
d5669f7b JK |
1555 | /* during POR, we don't need to trigger writepage at all. */ |
1556 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) | |
1557 | goto skip_write; | |
1558 | ||
d31c7c3f YH |
1559 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
1560 | ||
9dfa1baf | 1561 | blk_start_plug(&plug); |
b230e6ca | 1562 | ret = f2fs_write_cache_pages(mapping, wbc); |
9dfa1baf | 1563 | blk_finish_plug(&plug); |
28ea6162 JK |
1564 | /* |
1565 | * if some pages were truncated, we cannot guarantee its mapping->host | |
1566 | * to detect pending bios. | |
1567 | */ | |
458e6197 | 1568 | |
c227f912 | 1569 | remove_dirty_inode(inode); |
eb47b800 | 1570 | return ret; |
d3baf95d JK |
1571 | |
1572 | skip_write: | |
a7ffdbe2 | 1573 | wbc->pages_skipped += get_dirty_pages(inode); |
d31c7c3f | 1574 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
d3baf95d | 1575 | return 0; |
eb47b800 JK |
1576 | } |
1577 | ||
3aab8f82 CY |
1578 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
1579 | { | |
1580 | struct inode *inode = mapping->host; | |
819d9153 | 1581 | loff_t i_size = i_size_read(inode); |
3aab8f82 | 1582 | |
819d9153 JK |
1583 | if (to > i_size) { |
1584 | truncate_pagecache(inode, i_size); | |
1585 | truncate_blocks(inode, i_size, true); | |
3aab8f82 CY |
1586 | } |
1587 | } | |
1588 | ||
2aadac08 JK |
1589 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
1590 | struct page *page, loff_t pos, unsigned len, | |
1591 | block_t *blk_addr, bool *node_changed) | |
1592 | { | |
1593 | struct inode *inode = page->mapping->host; | |
1594 | pgoff_t index = page->index; | |
1595 | struct dnode_of_data dn; | |
1596 | struct page *ipage; | |
b4d07a3e JK |
1597 | bool locked = false; |
1598 | struct extent_info ei; | |
2aadac08 JK |
1599 | int err = 0; |
1600 | ||
24b84912 JK |
1601 | /* |
1602 | * we already allocated all the blocks, so we don't need to get | |
1603 | * the block addresses when there is no need to fill the page. | |
1604 | */ | |
5d4c0af4 | 1605 | if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE) |
24b84912 JK |
1606 | return 0; |
1607 | ||
b4d07a3e | 1608 | if (f2fs_has_inline_data(inode) || |
09cbfeaf | 1609 | (pos & PAGE_MASK) >= i_size_read(inode)) { |
b4d07a3e JK |
1610 | f2fs_lock_op(sbi); |
1611 | locked = true; | |
1612 | } | |
1613 | restart: | |
2aadac08 JK |
1614 | /* check inline_data */ |
1615 | ipage = get_node_page(sbi, inode->i_ino); | |
1616 | if (IS_ERR(ipage)) { | |
1617 | err = PTR_ERR(ipage); | |
1618 | goto unlock_out; | |
1619 | } | |
1620 | ||
1621 | set_new_dnode(&dn, inode, ipage, ipage, 0); | |
1622 | ||
1623 | if (f2fs_has_inline_data(inode)) { | |
1624 | if (pos + len <= MAX_INLINE_DATA) { | |
1625 | read_inline_data(page, ipage); | |
91942321 | 1626 | set_inode_flag(inode, FI_DATA_EXIST); |
ab47036d CY |
1627 | if (inode->i_nlink) |
1628 | set_inline_node(ipage); | |
2aadac08 JK |
1629 | } else { |
1630 | err = f2fs_convert_inline_page(&dn, page); | |
1631 | if (err) | |
b4d07a3e JK |
1632 | goto out; |
1633 | if (dn.data_blkaddr == NULL_ADDR) | |
1634 | err = f2fs_get_block(&dn, index); | |
1635 | } | |
1636 | } else if (locked) { | |
1637 | err = f2fs_get_block(&dn, index); | |
1638 | } else { | |
1639 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { | |
1640 | dn.data_blkaddr = ei.blk + index - ei.fofs; | |
1641 | } else { | |
b4d07a3e JK |
1642 | /* hole case */ |
1643 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); | |
4da7bf5a | 1644 | if (err || dn.data_blkaddr == NULL_ADDR) { |
b4d07a3e JK |
1645 | f2fs_put_dnode(&dn); |
1646 | f2fs_lock_op(sbi); | |
1647 | locked = true; | |
1648 | goto restart; | |
1649 | } | |
2aadac08 JK |
1650 | } |
1651 | } | |
b4d07a3e | 1652 | |
2aadac08 JK |
1653 | /* convert_inline_page can make node_changed */ |
1654 | *blk_addr = dn.data_blkaddr; | |
1655 | *node_changed = dn.node_changed; | |
b4d07a3e | 1656 | out: |
2aadac08 JK |
1657 | f2fs_put_dnode(&dn); |
1658 | unlock_out: | |
b4d07a3e JK |
1659 | if (locked) |
1660 | f2fs_unlock_op(sbi); | |
2aadac08 JK |
1661 | return err; |
1662 | } | |
1663 | ||
eb47b800 JK |
1664 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
1665 | loff_t pos, unsigned len, unsigned flags, | |
1666 | struct page **pagep, void **fsdata) | |
1667 | { | |
1668 | struct inode *inode = mapping->host; | |
4081363f | 1669 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
86531d6b | 1670 | struct page *page = NULL; |
09cbfeaf | 1671 | pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; |
2aadac08 JK |
1672 | bool need_balance = false; |
1673 | block_t blkaddr = NULL_ADDR; | |
eb47b800 JK |
1674 | int err = 0; |
1675 | ||
62aed044 CY |
1676 | trace_f2fs_write_begin(inode, pos, len, flags); |
1677 | ||
5f727395 JK |
1678 | /* |
1679 | * We should check this at this moment to avoid deadlock on inode page | |
1680 | * and #0 page. The locking rule for inline_data conversion should be: | |
1681 | * lock_page(page #0) -> lock_page(inode_page) | |
1682 | */ | |
1683 | if (index != 0) { | |
1684 | err = f2fs_convert_inline_inode(inode); | |
1685 | if (err) | |
1686 | goto fail; | |
1687 | } | |
afcb7ca0 | 1688 | repeat: |
eb47b800 | 1689 | page = grab_cache_page_write_begin(mapping, index, flags); |
3aab8f82 CY |
1690 | if (!page) { |
1691 | err = -ENOMEM; | |
1692 | goto fail; | |
1693 | } | |
d5f66990 | 1694 | |
eb47b800 JK |
1695 | *pagep = page; |
1696 | ||
2aadac08 JK |
1697 | err = prepare_write_begin(sbi, page, pos, len, |
1698 | &blkaddr, &need_balance); | |
9ba69cf9 | 1699 | if (err) |
2aadac08 | 1700 | goto fail; |
9ba69cf9 | 1701 | |
7f3037a5 | 1702 | if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) { |
2a340760 | 1703 | unlock_page(page); |
2c4db1a6 | 1704 | f2fs_balance_fs(sbi, true); |
2a340760 JK |
1705 | lock_page(page); |
1706 | if (page->mapping != mapping) { | |
1707 | /* The page got truncated from under us */ | |
1708 | f2fs_put_page(page, 1); | |
1709 | goto repeat; | |
1710 | } | |
1711 | } | |
1712 | ||
fec1d657 | 1713 | f2fs_wait_on_page_writeback(page, DATA, false); |
b3d208f9 | 1714 | |
08b39fbd CY |
1715 | /* wait for GCed encrypted page writeback */ |
1716 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) | |
2aadac08 | 1717 | f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr); |
08b39fbd | 1718 | |
649d7df2 JK |
1719 | if (len == PAGE_SIZE || PageUptodate(page)) |
1720 | return 0; | |
eb47b800 | 1721 | |
746e2403 YH |
1722 | if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) { |
1723 | zero_user_segment(page, len, PAGE_SIZE); | |
1724 | return 0; | |
1725 | } | |
1726 | ||
2aadac08 | 1727 | if (blkaddr == NEW_ADDR) { |
09cbfeaf | 1728 | zero_user_segment(page, 0, PAGE_SIZE); |
649d7df2 | 1729 | SetPageUptodate(page); |
eb47b800 | 1730 | } else { |
78682f79 | 1731 | struct bio *bio; |
d54c795b | 1732 | |
78682f79 CY |
1733 | bio = f2fs_grab_bio(inode, blkaddr, 1); |
1734 | if (IS_ERR(bio)) { | |
1735 | err = PTR_ERR(bio); | |
3aab8f82 | 1736 | goto fail; |
eb47b800 | 1737 | } |
70fd7614 | 1738 | bio->bi_opf = REQ_OP_READ; |
78682f79 CY |
1739 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
1740 | bio_put(bio); | |
1741 | err = -EFAULT; | |
1742 | goto fail; | |
1743 | } | |
1744 | ||
4fc29c1a | 1745 | __submit_bio(sbi, bio, DATA); |
d54c795b | 1746 | |
393ff91f | 1747 | lock_page(page); |
6bacf52f | 1748 | if (unlikely(page->mapping != mapping)) { |
afcb7ca0 JK |
1749 | f2fs_put_page(page, 1); |
1750 | goto repeat; | |
eb47b800 | 1751 | } |
1563ac75 CY |
1752 | if (unlikely(!PageUptodate(page))) { |
1753 | err = -EIO; | |
1754 | goto fail; | |
4375a336 | 1755 | } |
eb47b800 | 1756 | } |
eb47b800 | 1757 | return 0; |
9ba69cf9 | 1758 | |
3aab8f82 | 1759 | fail: |
86531d6b | 1760 | f2fs_put_page(page, 1); |
3aab8f82 CY |
1761 | f2fs_write_failed(mapping, pos + len); |
1762 | return err; | |
eb47b800 JK |
1763 | } |
1764 | ||
a1dd3c13 JK |
1765 | static int f2fs_write_end(struct file *file, |
1766 | struct address_space *mapping, | |
1767 | loff_t pos, unsigned len, unsigned copied, | |
1768 | struct page *page, void *fsdata) | |
1769 | { | |
1770 | struct inode *inode = page->mapping->host; | |
1771 | ||
dfb2bf38 CY |
1772 | trace_f2fs_write_end(inode, pos, len, copied); |
1773 | ||
649d7df2 JK |
1774 | /* |
1775 | * This should be come from len == PAGE_SIZE, and we expect copied | |
1776 | * should be PAGE_SIZE. Otherwise, we treat it with zero copied and | |
1777 | * let generic_perform_write() try to copy data again through copied=0. | |
1778 | */ | |
1779 | if (!PageUptodate(page)) { | |
746e2403 | 1780 | if (unlikely(copied != len)) |
649d7df2 JK |
1781 | copied = 0; |
1782 | else | |
1783 | SetPageUptodate(page); | |
1784 | } | |
1785 | if (!copied) | |
1786 | goto unlock_out; | |
1787 | ||
34ba94ba | 1788 | set_page_dirty(page); |
a1dd3c13 | 1789 | |
fc9581c8 JK |
1790 | if (pos + copied > i_size_read(inode)) |
1791 | f2fs_i_size_write(inode, pos + copied); | |
649d7df2 | 1792 | unlock_out: |
3024c9a1 | 1793 | f2fs_put_page(page, 1); |
d0239e1b | 1794 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
a1dd3c13 JK |
1795 | return copied; |
1796 | } | |
1797 | ||
6f673763 OS |
1798 | static int check_direct_IO(struct inode *inode, struct iov_iter *iter, |
1799 | loff_t offset) | |
944fcfc1 JK |
1800 | { |
1801 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; | |
944fcfc1 | 1802 | |
944fcfc1 JK |
1803 | if (offset & blocksize_mask) |
1804 | return -EINVAL; | |
1805 | ||
5b46f25d AV |
1806 | if (iov_iter_alignment(iter) & blocksize_mask) |
1807 | return -EINVAL; | |
1808 | ||
944fcfc1 JK |
1809 | return 0; |
1810 | } | |
1811 | ||
c8b8e32d | 1812 | static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
eb47b800 | 1813 | { |
b439b103 | 1814 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
3aab8f82 CY |
1815 | struct inode *inode = mapping->host; |
1816 | size_t count = iov_iter_count(iter); | |
c8b8e32d | 1817 | loff_t offset = iocb->ki_pos; |
82e0a5aa | 1818 | int rw = iov_iter_rw(iter); |
3aab8f82 | 1819 | int err; |
944fcfc1 | 1820 | |
b439b103 | 1821 | err = check_direct_IO(inode, iter, offset); |
b9d777b8 JK |
1822 | if (err) |
1823 | return err; | |
9ffe0fb5 | 1824 | |
c040ff9d | 1825 | if (__force_buffered_io(inode, rw)) |
36abef4e | 1826 | return 0; |
fcc85a4d | 1827 | |
5302fb00 | 1828 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
70407fad | 1829 | |
82e0a5aa | 1830 | down_read(&F2FS_I(inode)->dio_rwsem[rw]); |
c8b8e32d | 1831 | err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio); |
82e0a5aa CY |
1832 | up_read(&F2FS_I(inode)->dio_rwsem[rw]); |
1833 | ||
1834 | if (rw == WRITE) { | |
6bfc4919 | 1835 | if (err > 0) |
91942321 | 1836 | set_inode_flag(inode, FI_UPDATE_WRITE); |
6bfc4919 JK |
1837 | else if (err < 0) |
1838 | f2fs_write_failed(mapping, offset + count); | |
1839 | } | |
70407fad | 1840 | |
5302fb00 | 1841 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
70407fad | 1842 | |
3aab8f82 | 1843 | return err; |
eb47b800 JK |
1844 | } |
1845 | ||
487261f3 CY |
1846 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
1847 | unsigned int length) | |
eb47b800 JK |
1848 | { |
1849 | struct inode *inode = page->mapping->host; | |
487261f3 | 1850 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
a7ffdbe2 | 1851 | |
487261f3 | 1852 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
09cbfeaf | 1853 | (offset % PAGE_SIZE || length != PAGE_SIZE)) |
a7ffdbe2 JK |
1854 | return; |
1855 | ||
487261f3 | 1856 | if (PageDirty(page)) { |
933439c8 | 1857 | if (inode->i_ino == F2FS_META_INO(sbi)) { |
487261f3 | 1858 | dec_page_count(sbi, F2FS_DIRTY_META); |
933439c8 | 1859 | } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { |
487261f3 | 1860 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
933439c8 | 1861 | } else { |
487261f3 | 1862 | inode_dec_dirty_pages(inode); |
933439c8 CY |
1863 | remove_dirty_inode(inode); |
1864 | } | |
487261f3 | 1865 | } |
decd36b6 CY |
1866 | |
1867 | /* This is atomic written page, keep Private */ | |
1868 | if (IS_ATOMIC_WRITTEN_PAGE(page)) | |
1869 | return; | |
1870 | ||
23dc974e | 1871 | set_page_private(page, 0); |
eb47b800 JK |
1872 | ClearPagePrivate(page); |
1873 | } | |
1874 | ||
487261f3 | 1875 | int f2fs_release_page(struct page *page, gfp_t wait) |
eb47b800 | 1876 | { |
f68daeeb JK |
1877 | /* If this is dirty page, keep PagePrivate */ |
1878 | if (PageDirty(page)) | |
1879 | return 0; | |
1880 | ||
decd36b6 CY |
1881 | /* This is atomic written page, keep Private */ |
1882 | if (IS_ATOMIC_WRITTEN_PAGE(page)) | |
1883 | return 0; | |
1884 | ||
23dc974e | 1885 | set_page_private(page, 0); |
eb47b800 | 1886 | ClearPagePrivate(page); |
c3850aa1 | 1887 | return 1; |
eb47b800 JK |
1888 | } |
1889 | ||
fe76b796 JK |
1890 | /* |
1891 | * This was copied from __set_page_dirty_buffers which gives higher performance | |
1892 | * in very high speed storages. (e.g., pmem) | |
1893 | */ | |
1894 | void f2fs_set_page_dirty_nobuffers(struct page *page) | |
1895 | { | |
1896 | struct address_space *mapping = page->mapping; | |
1897 | unsigned long flags; | |
1898 | ||
1899 | if (unlikely(!mapping)) | |
1900 | return; | |
1901 | ||
1902 | spin_lock(&mapping->private_lock); | |
1903 | lock_page_memcg(page); | |
1904 | SetPageDirty(page); | |
1905 | spin_unlock(&mapping->private_lock); | |
1906 | ||
1907 | spin_lock_irqsave(&mapping->tree_lock, flags); | |
1908 | WARN_ON_ONCE(!PageUptodate(page)); | |
1909 | account_page_dirtied(page, mapping); | |
1910 | radix_tree_tag_set(&mapping->page_tree, | |
1911 | page_index(page), PAGECACHE_TAG_DIRTY); | |
1912 | spin_unlock_irqrestore(&mapping->tree_lock, flags); | |
1913 | unlock_page_memcg(page); | |
1914 | ||
1915 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | |
1916 | return; | |
1917 | } | |
1918 | ||
eb47b800 JK |
1919 | static int f2fs_set_data_page_dirty(struct page *page) |
1920 | { | |
1921 | struct address_space *mapping = page->mapping; | |
1922 | struct inode *inode = mapping->host; | |
1923 | ||
26c6b887 JK |
1924 | trace_f2fs_set_page_dirty(page, DATA); |
1925 | ||
237c0790 JK |
1926 | if (!PageUptodate(page)) |
1927 | SetPageUptodate(page); | |
34ba94ba | 1928 | |
1e84371f | 1929 | if (f2fs_is_atomic_file(inode)) { |
decd36b6 CY |
1930 | if (!IS_ATOMIC_WRITTEN_PAGE(page)) { |
1931 | register_inmem_page(inode, page); | |
1932 | return 1; | |
1933 | } | |
1934 | /* | |
1935 | * Previously, this page has been registered, we just | |
1936 | * return here. | |
1937 | */ | |
1938 | return 0; | |
34ba94ba JK |
1939 | } |
1940 | ||
eb47b800 | 1941 | if (!PageDirty(page)) { |
fe76b796 | 1942 | f2fs_set_page_dirty_nobuffers(page); |
a7ffdbe2 | 1943 | update_dirty_page(inode, page); |
eb47b800 JK |
1944 | return 1; |
1945 | } | |
1946 | return 0; | |
1947 | } | |
1948 | ||
c01e54b7 JK |
1949 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
1950 | { | |
454ae7e5 CY |
1951 | struct inode *inode = mapping->host; |
1952 | ||
1d373a0e JK |
1953 | if (f2fs_has_inline_data(inode)) |
1954 | return 0; | |
1955 | ||
1956 | /* make sure allocating whole blocks */ | |
1957 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | |
1958 | filemap_write_and_wait(mapping); | |
1959 | ||
e2b4e2bc | 1960 | return generic_block_bmap(mapping, block, get_data_block_bmap); |
429511cd CY |
1961 | } |
1962 | ||
5b7a487c WG |
1963 | #ifdef CONFIG_MIGRATION |
1964 | #include <linux/migrate.h> | |
1965 | ||
1966 | int f2fs_migrate_page(struct address_space *mapping, | |
1967 | struct page *newpage, struct page *page, enum migrate_mode mode) | |
1968 | { | |
1969 | int rc, extra_count; | |
1970 | struct f2fs_inode_info *fi = F2FS_I(mapping->host); | |
1971 | bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page); | |
1972 | ||
1973 | BUG_ON(PageWriteback(page)); | |
1974 | ||
1975 | /* migrating an atomic written page is safe with the inmem_lock hold */ | |
1976 | if (atomic_written && !mutex_trylock(&fi->inmem_lock)) | |
1977 | return -EAGAIN; | |
1978 | ||
1979 | /* | |
1980 | * A reference is expected if PagePrivate set when move mapping, | |
1981 | * however F2FS breaks this for maintaining dirty page counts when | |
1982 | * truncating pages. So here adjusting the 'extra_count' make it work. | |
1983 | */ | |
1984 | extra_count = (atomic_written ? 1 : 0) - page_has_private(page); | |
1985 | rc = migrate_page_move_mapping(mapping, newpage, | |
1986 | page, NULL, mode, extra_count); | |
1987 | if (rc != MIGRATEPAGE_SUCCESS) { | |
1988 | if (atomic_written) | |
1989 | mutex_unlock(&fi->inmem_lock); | |
1990 | return rc; | |
1991 | } | |
1992 | ||
1993 | if (atomic_written) { | |
1994 | struct inmem_pages *cur; | |
1995 | list_for_each_entry(cur, &fi->inmem_pages, list) | |
1996 | if (cur->page == page) { | |
1997 | cur->page = newpage; | |
1998 | break; | |
1999 | } | |
2000 | mutex_unlock(&fi->inmem_lock); | |
2001 | put_page(page); | |
2002 | get_page(newpage); | |
2003 | } | |
2004 | ||
2005 | if (PagePrivate(page)) | |
2006 | SetPagePrivate(newpage); | |
2007 | set_page_private(newpage, page_private(page)); | |
2008 | ||
2009 | migrate_page_copy(newpage, page); | |
2010 | ||
2011 | return MIGRATEPAGE_SUCCESS; | |
2012 | } | |
2013 | #endif | |
2014 | ||
eb47b800 JK |
2015 | const struct address_space_operations f2fs_dblock_aops = { |
2016 | .readpage = f2fs_read_data_page, | |
2017 | .readpages = f2fs_read_data_pages, | |
2018 | .writepage = f2fs_write_data_page, | |
2019 | .writepages = f2fs_write_data_pages, | |
2020 | .write_begin = f2fs_write_begin, | |
a1dd3c13 | 2021 | .write_end = f2fs_write_end, |
eb47b800 | 2022 | .set_page_dirty = f2fs_set_data_page_dirty, |
487261f3 CY |
2023 | .invalidatepage = f2fs_invalidate_page, |
2024 | .releasepage = f2fs_release_page, | |
eb47b800 | 2025 | .direct_IO = f2fs_direct_IO, |
c01e54b7 | 2026 | .bmap = f2fs_bmap, |
5b7a487c WG |
2027 | #ifdef CONFIG_MIGRATION |
2028 | .migratepage = f2fs_migrate_page, | |
2029 | #endif | |
eb47b800 | 2030 | }; |