~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/f2fs/data.c

Version: ~ [ linux-5.19-rc8 ] ~ [ linux-5.18.14 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.57 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.133 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.207 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.253 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.289 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.324 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp