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

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

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

~ [ 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