OSDN Git Service

Linux 4.4.187
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / f2fs / segment.h
1 /*
2  * fs/f2fs/segment.h
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/blkdev.h>
12 #include <linux/backing-dev.h>
13
14 /* constant macro */
15 #define NULL_SEGNO                      ((unsigned int)(~0))
16 #define NULL_SECNO                      ((unsigned int)(~0))
17
18 #define DEF_RECLAIM_PREFREE_SEGMENTS    5       /* 5% over total segments */
19
20 #define F2FS_MIN_SEGMENTS       9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
21
22 /* L: Logical segment # in volume, R: Relative segment # in main area */
23 #define GET_L2R_SEGNO(free_i, segno)    (segno - free_i->start_segno)
24 #define GET_R2L_SEGNO(free_i, segno)    (segno + free_i->start_segno)
25
26 #define IS_DATASEG(t)   (t <= CURSEG_COLD_DATA)
27 #define IS_NODESEG(t)   (t >= CURSEG_HOT_NODE)
28
29 #define IS_CURSEG(sbi, seg)                                             \
30         ((seg == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) ||      \
31          (seg == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) ||     \
32          (seg == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) ||     \
33          (seg == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) ||      \
34          (seg == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) ||     \
35          (seg == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno))
36
37 #define IS_CURSEC(sbi, secno)                                           \
38         ((secno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno /              \
39           sbi->segs_per_sec) || \
40          (secno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno /             \
41           sbi->segs_per_sec) || \
42          (secno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno /             \
43           sbi->segs_per_sec) || \
44          (secno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno /              \
45           sbi->segs_per_sec) || \
46          (secno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno /             \
47           sbi->segs_per_sec) || \
48          (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno /             \
49           sbi->segs_per_sec))   \
50
51 #define MAIN_BLKADDR(sbi)                                               \
52         (SM_I(sbi) ? SM_I(sbi)->main_blkaddr :                          \
53                 le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
54 #define SEG0_BLKADDR(sbi)                                               \
55         (SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr :                          \
56                 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
57
58 #define MAIN_SEGS(sbi)  (SM_I(sbi)->main_segments)
59 #define MAIN_SECS(sbi)  (sbi->total_sections)
60
61 #define TOTAL_SEGS(sbi)                                                 \
62         (SM_I(sbi) ? SM_I(sbi)->segment_count :                                 \
63                 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
64 #define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << sbi->log_blocks_per_seg)
65
66 #define MAX_BLKADDR(sbi)        (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
67 #define SEGMENT_SIZE(sbi)       (1ULL << (sbi->log_blocksize +          \
68                                         sbi->log_blocks_per_seg))
69
70 #define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) +                    \
71          (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
72
73 #define NEXT_FREE_BLKADDR(sbi, curseg)                                  \
74         (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
75
76 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)     ((blk_addr) - SEG0_BLKADDR(sbi))
77 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)                              \
78         (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
79 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)                             \
80         (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
81
82 #define GET_SEGNO(sbi, blk_addr)                                        \
83         ((!is_valid_data_blkaddr(sbi, blk_addr)) ?                      \
84         NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi),                 \
85                 GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
86 #define GET_SECNO(sbi, segno)                                   \
87         ((segno) / sbi->segs_per_sec)
88 #define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
89         ((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
90
91 #define GET_SUM_BLOCK(sbi, segno)                               \
92         ((sbi->sm_info->ssa_blkaddr) + segno)
93
94 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
95 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
96
97 #define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
98         (segno % sit_i->sents_per_block)
99 #define SIT_BLOCK_OFFSET(segno)                                 \
100         (segno / SIT_ENTRY_PER_BLOCK)
101 #define START_SEGNO(segno)              \
102         (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
103 #define SIT_BLK_CNT(sbi)                        \
104         ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
105 #define f2fs_bitmap_size(nr)                    \
106         (BITS_TO_LONGS(nr) * sizeof(unsigned long))
107
108 #define SECTOR_FROM_BLOCK(blk_addr)                                     \
109         (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
110 #define SECTOR_TO_BLOCK(sectors)                                        \
111         (sectors >> F2FS_LOG_SECTORS_PER_BLOCK)
112 #define MAX_BIO_BLOCKS(sbi)                                             \
113         ((int)min((int)max_hw_blocks(sbi), BIO_MAX_PAGES))
114
115 /*
116  * indicate a block allocation direction: RIGHT and LEFT.
117  * RIGHT means allocating new sections towards the end of volume.
118  * LEFT means the opposite direction.
119  */
120 enum {
121         ALLOC_RIGHT = 0,
122         ALLOC_LEFT
123 };
124
125 /*
126  * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
127  * LFS writes data sequentially with cleaning operations.
128  * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
129  */
130 enum {
131         LFS = 0,
132         SSR
133 };
134
135 /*
136  * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
137  * GC_CB is based on cost-benefit algorithm.
138  * GC_GREEDY is based on greedy algorithm.
139  */
140 enum {
141         GC_CB = 0,
142         GC_GREEDY
143 };
144
145 /*
146  * BG_GC means the background cleaning job.
147  * FG_GC means the on-demand cleaning job.
148  * FORCE_FG_GC means on-demand cleaning job in background.
149  */
150 enum {
151         BG_GC = 0,
152         FG_GC,
153         FORCE_FG_GC,
154 };
155
156 /* for a function parameter to select a victim segment */
157 struct victim_sel_policy {
158         int alloc_mode;                 /* LFS or SSR */
159         int gc_mode;                    /* GC_CB or GC_GREEDY */
160         unsigned long *dirty_segmap;    /* dirty segment bitmap */
161         unsigned int max_search;        /* maximum # of segments to search */
162         unsigned int offset;            /* last scanned bitmap offset */
163         unsigned int ofs_unit;          /* bitmap search unit */
164         unsigned int min_cost;          /* minimum cost */
165         unsigned int min_segno;         /* segment # having min. cost */
166 };
167
168 struct seg_entry {
169         unsigned short valid_blocks;    /* # of valid blocks */
170         unsigned char *cur_valid_map;   /* validity bitmap of blocks */
171         /*
172          * # of valid blocks and the validity bitmap stored in the the last
173          * checkpoint pack. This information is used by the SSR mode.
174          */
175         unsigned short ckpt_valid_blocks;
176         unsigned char *ckpt_valid_map;
177         unsigned char *discard_map;
178         unsigned char type;             /* segment type like CURSEG_XXX_TYPE */
179         unsigned long long mtime;       /* modification time of the segment */
180 };
181
182 struct sec_entry {
183         unsigned int valid_blocks;      /* # of valid blocks in a section */
184 };
185
186 struct segment_allocation {
187         void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
188 };
189
190 /*
191  * this value is set in page as a private data which indicate that
192  * the page is atomically written, and it is in inmem_pages list.
193  */
194 #define ATOMIC_WRITTEN_PAGE             0x0000ffff
195
196 #define IS_ATOMIC_WRITTEN_PAGE(page)                    \
197                 (page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
198
199 struct inmem_pages {
200         struct list_head list;
201         struct page *page;
202 };
203
204 struct sit_info {
205         const struct segment_allocation *s_ops;
206
207         block_t sit_base_addr;          /* start block address of SIT area */
208         block_t sit_blocks;             /* # of blocks used by SIT area */
209         block_t written_valid_blocks;   /* # of valid blocks in main area */
210         char *sit_bitmap;               /* SIT bitmap pointer */
211         unsigned int bitmap_size;       /* SIT bitmap size */
212
213         unsigned long *tmp_map;                 /* bitmap for temporal use */
214         unsigned long *dirty_sentries_bitmap;   /* bitmap for dirty sentries */
215         unsigned int dirty_sentries;            /* # of dirty sentries */
216         unsigned int sents_per_block;           /* # of SIT entries per block */
217         struct mutex sentry_lock;               /* to protect SIT cache */
218         struct seg_entry *sentries;             /* SIT segment-level cache */
219         struct sec_entry *sec_entries;          /* SIT section-level cache */
220
221         /* for cost-benefit algorithm in cleaning procedure */
222         unsigned long long elapsed_time;        /* elapsed time after mount */
223         unsigned long long mounted_time;        /* mount time */
224         unsigned long long min_mtime;           /* min. modification time */
225         unsigned long long max_mtime;           /* max. modification time */
226 };
227
228 struct free_segmap_info {
229         unsigned int start_segno;       /* start segment number logically */
230         unsigned int free_segments;     /* # of free segments */
231         unsigned int free_sections;     /* # of free sections */
232         spinlock_t segmap_lock;         /* free segmap lock */
233         unsigned long *free_segmap;     /* free segment bitmap */
234         unsigned long *free_secmap;     /* free section bitmap */
235 };
236
237 /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
238 enum dirty_type {
239         DIRTY_HOT_DATA,         /* dirty segments assigned as hot data logs */
240         DIRTY_WARM_DATA,        /* dirty segments assigned as warm data logs */
241         DIRTY_COLD_DATA,        /* dirty segments assigned as cold data logs */
242         DIRTY_HOT_NODE,         /* dirty segments assigned as hot node logs */
243         DIRTY_WARM_NODE,        /* dirty segments assigned as warm node logs */
244         DIRTY_COLD_NODE,        /* dirty segments assigned as cold node logs */
245         DIRTY,                  /* to count # of dirty segments */
246         PRE,                    /* to count # of entirely obsolete segments */
247         NR_DIRTY_TYPE
248 };
249
250 struct dirty_seglist_info {
251         const struct victim_selection *v_ops;   /* victim selction operation */
252         unsigned long *dirty_segmap[NR_DIRTY_TYPE];
253         struct mutex seglist_lock;              /* lock for segment bitmaps */
254         int nr_dirty[NR_DIRTY_TYPE];            /* # of dirty segments */
255         unsigned long *victim_secmap;           /* background GC victims */
256 };
257
258 /* victim selection function for cleaning and SSR */
259 struct victim_selection {
260         int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
261                                                         int, int, char);
262 };
263
264 /* for active log information */
265 struct curseg_info {
266         struct mutex curseg_mutex;              /* lock for consistency */
267         struct f2fs_summary_block *sum_blk;     /* cached summary block */
268         unsigned char alloc_type;               /* current allocation type */
269         unsigned int segno;                     /* current segment number */
270         unsigned short next_blkoff;             /* next block offset to write */
271         unsigned int zone;                      /* current zone number */
272         unsigned int next_segno;                /* preallocated segment */
273 };
274
275 struct sit_entry_set {
276         struct list_head set_list;      /* link with all sit sets */
277         unsigned int start_segno;       /* start segno of sits in set */
278         unsigned int entry_cnt;         /* the # of sit entries in set */
279 };
280
281 /*
282  * inline functions
283  */
284 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
285 {
286         return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
287 }
288
289 static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
290                                                 unsigned int segno)
291 {
292         struct sit_info *sit_i = SIT_I(sbi);
293         return &sit_i->sentries[segno];
294 }
295
296 static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
297                                                 unsigned int segno)
298 {
299         struct sit_info *sit_i = SIT_I(sbi);
300         return &sit_i->sec_entries[GET_SECNO(sbi, segno)];
301 }
302
303 static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
304                                 unsigned int segno, int section)
305 {
306         /*
307          * In order to get # of valid blocks in a section instantly from many
308          * segments, f2fs manages two counting structures separately.
309          */
310         if (section > 1)
311                 return get_sec_entry(sbi, segno)->valid_blocks;
312         else
313                 return get_seg_entry(sbi, segno)->valid_blocks;
314 }
315
316 static inline void seg_info_from_raw_sit(struct seg_entry *se,
317                                         struct f2fs_sit_entry *rs)
318 {
319         se->valid_blocks = GET_SIT_VBLOCKS(rs);
320         se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
321         memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
322         memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
323         se->type = GET_SIT_TYPE(rs);
324         se->mtime = le64_to_cpu(rs->mtime);
325 }
326
327 static inline void seg_info_to_raw_sit(struct seg_entry *se,
328                                         struct f2fs_sit_entry *rs)
329 {
330         unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
331                                         se->valid_blocks;
332         rs->vblocks = cpu_to_le16(raw_vblocks);
333         memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
334         memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
335         se->ckpt_valid_blocks = se->valid_blocks;
336         rs->mtime = cpu_to_le64(se->mtime);
337 }
338
339 static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
340                 unsigned int max, unsigned int segno)
341 {
342         unsigned int ret;
343         spin_lock(&free_i->segmap_lock);
344         ret = find_next_bit(free_i->free_segmap, max, segno);
345         spin_unlock(&free_i->segmap_lock);
346         return ret;
347 }
348
349 static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
350 {
351         struct free_segmap_info *free_i = FREE_I(sbi);
352         unsigned int secno = segno / sbi->segs_per_sec;
353         unsigned int start_segno = secno * sbi->segs_per_sec;
354         unsigned int next;
355
356         spin_lock(&free_i->segmap_lock);
357         clear_bit(segno, free_i->free_segmap);
358         free_i->free_segments++;
359
360         next = find_next_bit(free_i->free_segmap,
361                         start_segno + sbi->segs_per_sec, start_segno);
362         if (next >= start_segno + sbi->segs_per_sec) {
363                 clear_bit(secno, free_i->free_secmap);
364                 free_i->free_sections++;
365         }
366         spin_unlock(&free_i->segmap_lock);
367 }
368
369 static inline void __set_inuse(struct f2fs_sb_info *sbi,
370                 unsigned int segno)
371 {
372         struct free_segmap_info *free_i = FREE_I(sbi);
373         unsigned int secno = segno / sbi->segs_per_sec;
374         set_bit(segno, free_i->free_segmap);
375         free_i->free_segments--;
376         if (!test_and_set_bit(secno, free_i->free_secmap))
377                 free_i->free_sections--;
378 }
379
380 static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
381                 unsigned int segno)
382 {
383         struct free_segmap_info *free_i = FREE_I(sbi);
384         unsigned int secno = segno / sbi->segs_per_sec;
385         unsigned int start_segno = secno * sbi->segs_per_sec;
386         unsigned int next;
387
388         spin_lock(&free_i->segmap_lock);
389         if (test_and_clear_bit(segno, free_i->free_segmap)) {
390                 free_i->free_segments++;
391
392                 if (IS_CURSEC(sbi, secno))
393                         goto skip_free;
394                 next = find_next_bit(free_i->free_segmap,
395                                 start_segno + sbi->segs_per_sec, start_segno);
396                 if (next >= start_segno + sbi->segs_per_sec) {
397                         if (test_and_clear_bit(secno, free_i->free_secmap))
398                                 free_i->free_sections++;
399                 }
400         }
401 skip_free:
402         spin_unlock(&free_i->segmap_lock);
403 }
404
405 static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
406                 unsigned int segno)
407 {
408         struct free_segmap_info *free_i = FREE_I(sbi);
409         unsigned int secno = segno / sbi->segs_per_sec;
410         spin_lock(&free_i->segmap_lock);
411         if (!test_and_set_bit(segno, free_i->free_segmap)) {
412                 free_i->free_segments--;
413                 if (!test_and_set_bit(secno, free_i->free_secmap))
414                         free_i->free_sections--;
415         }
416         spin_unlock(&free_i->segmap_lock);
417 }
418
419 static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
420                 void *dst_addr)
421 {
422         struct sit_info *sit_i = SIT_I(sbi);
423         memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
424 }
425
426 static inline block_t written_block_count(struct f2fs_sb_info *sbi)
427 {
428         return SIT_I(sbi)->written_valid_blocks;
429 }
430
431 static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
432 {
433         return FREE_I(sbi)->free_segments;
434 }
435
436 static inline int reserved_segments(struct f2fs_sb_info *sbi)
437 {
438         return SM_I(sbi)->reserved_segments;
439 }
440
441 static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
442 {
443         return FREE_I(sbi)->free_sections;
444 }
445
446 static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
447 {
448         return DIRTY_I(sbi)->nr_dirty[PRE];
449 }
450
451 static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
452 {
453         return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
454                 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
455                 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
456                 DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
457                 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
458                 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
459 }
460
461 static inline int overprovision_segments(struct f2fs_sb_info *sbi)
462 {
463         return SM_I(sbi)->ovp_segments;
464 }
465
466 static inline int overprovision_sections(struct f2fs_sb_info *sbi)
467 {
468         return ((unsigned int) overprovision_segments(sbi)) / sbi->segs_per_sec;
469 }
470
471 static inline int reserved_sections(struct f2fs_sb_info *sbi)
472 {
473         return ((unsigned int) reserved_segments(sbi)) / sbi->segs_per_sec;
474 }
475
476 static inline bool need_SSR(struct f2fs_sb_info *sbi)
477 {
478         int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
479         int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
480         return free_sections(sbi) <= (node_secs + 2 * dent_secs +
481                                                 reserved_sections(sbi) + 1);
482 }
483
484 static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, int freed)
485 {
486         int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
487         int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
488
489         if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
490                 return false;
491
492         return (free_sections(sbi) + freed) <= (node_secs + 2 * dent_secs +
493                                                 reserved_sections(sbi));
494 }
495
496 static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
497 {
498         return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
499 }
500
501 static inline int utilization(struct f2fs_sb_info *sbi)
502 {
503         return div_u64((u64)valid_user_blocks(sbi) * 100,
504                                         sbi->user_block_count);
505 }
506
507 /*
508  * Sometimes f2fs may be better to drop out-of-place update policy.
509  * And, users can control the policy through sysfs entries.
510  * There are five policies with triggering conditions as follows.
511  * F2FS_IPU_FORCE - all the time,
512  * F2FS_IPU_SSR - if SSR mode is activated,
513  * F2FS_IPU_UTIL - if FS utilization is over threashold,
514  * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
515  *                     threashold,
516  * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
517  *                     storages. IPU will be triggered only if the # of dirty
518  *                     pages over min_fsync_blocks.
519  * F2FS_IPUT_DISABLE - disable IPU. (=default option)
520  */
521 #define DEF_MIN_IPU_UTIL        70
522 #define DEF_MIN_FSYNC_BLOCKS    8
523
524 enum {
525         F2FS_IPU_FORCE,
526         F2FS_IPU_SSR,
527         F2FS_IPU_UTIL,
528         F2FS_IPU_SSR_UTIL,
529         F2FS_IPU_FSYNC,
530 };
531
532 static inline bool need_inplace_update(struct inode *inode)
533 {
534         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
535         unsigned int policy = SM_I(sbi)->ipu_policy;
536
537         /* IPU can be done only for the user data */
538         if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
539                 return false;
540
541         if (policy & (0x1 << F2FS_IPU_FORCE))
542                 return true;
543         if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
544                 return true;
545         if (policy & (0x1 << F2FS_IPU_UTIL) &&
546                         utilization(sbi) > SM_I(sbi)->min_ipu_util)
547                 return true;
548         if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && need_SSR(sbi) &&
549                         utilization(sbi) > SM_I(sbi)->min_ipu_util)
550                 return true;
551
552         /* this is only set during fdatasync */
553         if (policy & (0x1 << F2FS_IPU_FSYNC) &&
554                         is_inode_flag_set(F2FS_I(inode), FI_NEED_IPU))
555                 return true;
556
557         return false;
558 }
559
560 static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
561                 int type)
562 {
563         struct curseg_info *curseg = CURSEG_I(sbi, type);
564         return curseg->segno;
565 }
566
567 static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
568                 int type)
569 {
570         struct curseg_info *curseg = CURSEG_I(sbi, type);
571         return curseg->alloc_type;
572 }
573
574 static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
575 {
576         struct curseg_info *curseg = CURSEG_I(sbi, type);
577         return curseg->next_blkoff;
578 }
579
580 static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
581 {
582         f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
583 }
584
585 static inline void verify_block_addr(struct f2fs_io_info *fio, block_t blk_addr)
586 {
587         struct f2fs_sb_info *sbi = fio->sbi;
588
589         if (__is_meta_io(fio))
590                 verify_blkaddr(sbi, blk_addr, META_GENERIC);
591         else
592                 verify_blkaddr(sbi, blk_addr, DATA_GENERIC);
593 }
594
595 /*
596  * Summary block is always treated as an invalid block
597  */
598 static inline int check_block_count(struct f2fs_sb_info *sbi,
599                 int segno, struct f2fs_sit_entry *raw_sit)
600 {
601         bool is_valid  = test_bit_le(0, raw_sit->valid_map) ? true : false;
602         int valid_blocks = 0;
603         int cur_pos = 0, next_pos;
604
605         /* check bitmap with valid block count */
606         do {
607                 if (is_valid) {
608                         next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
609                                         sbi->blocks_per_seg,
610                                         cur_pos);
611                         valid_blocks += next_pos - cur_pos;
612                 } else
613                         next_pos = find_next_bit_le(&raw_sit->valid_map,
614                                         sbi->blocks_per_seg,
615                                         cur_pos);
616                 cur_pos = next_pos;
617                 is_valid = !is_valid;
618         } while (cur_pos < sbi->blocks_per_seg);
619
620         if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
621                 f2fs_msg(sbi->sb, KERN_ERR,
622                                 "Mismatch valid blocks %d vs. %d",
623                                         GET_SIT_VBLOCKS(raw_sit), valid_blocks);
624                 set_sbi_flag(sbi, SBI_NEED_FSCK);
625                 return -EINVAL;
626         }
627
628         /* check segment usage, and check boundary of a given segment number */
629         if (unlikely(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg
630                                         || segno > TOTAL_SEGS(sbi) - 1)) {
631                 f2fs_msg(sbi->sb, KERN_ERR,
632                                 "Wrong valid blocks %d or segno %u",
633                                         GET_SIT_VBLOCKS(raw_sit), segno);
634                 set_sbi_flag(sbi, SBI_NEED_FSCK);
635                 return -EINVAL;
636         }
637         return 0;
638 }
639
640 static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
641                                                 unsigned int start)
642 {
643         struct sit_info *sit_i = SIT_I(sbi);
644         unsigned int offset = SIT_BLOCK_OFFSET(start);
645         block_t blk_addr = sit_i->sit_base_addr + offset;
646
647         check_seg_range(sbi, start);
648
649         /* calculate sit block address */
650         if (f2fs_test_bit(offset, sit_i->sit_bitmap))
651                 blk_addr += sit_i->sit_blocks;
652
653         return blk_addr;
654 }
655
656 static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
657                                                 pgoff_t block_addr)
658 {
659         struct sit_info *sit_i = SIT_I(sbi);
660         block_addr -= sit_i->sit_base_addr;
661         if (block_addr < sit_i->sit_blocks)
662                 block_addr += sit_i->sit_blocks;
663         else
664                 block_addr -= sit_i->sit_blocks;
665
666         return block_addr + sit_i->sit_base_addr;
667 }
668
669 static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
670 {
671         unsigned int block_off = SIT_BLOCK_OFFSET(start);
672
673         f2fs_change_bit(block_off, sit_i->sit_bitmap);
674 }
675
676 static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
677 {
678         struct sit_info *sit_i = SIT_I(sbi);
679         return sit_i->elapsed_time + CURRENT_TIME_SEC.tv_sec -
680                                                 sit_i->mounted_time;
681 }
682
683 static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
684                         unsigned int ofs_in_node, unsigned char version)
685 {
686         sum->nid = cpu_to_le32(nid);
687         sum->ofs_in_node = cpu_to_le16(ofs_in_node);
688         sum->version = version;
689 }
690
691 static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
692 {
693         return __start_cp_addr(sbi) +
694                 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
695 }
696
697 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
698 {
699         return __start_cp_addr(sbi) +
700                 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
701                                 - (base + 1) + type;
702 }
703
704 static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
705 {
706         if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
707                 return true;
708         return false;
709 }
710
711 static inline unsigned int max_hw_blocks(struct f2fs_sb_info *sbi)
712 {
713         struct block_device *bdev = sbi->sb->s_bdev;
714         struct request_queue *q = bdev_get_queue(bdev);
715         return SECTOR_TO_BLOCK(queue_max_sectors(q));
716 }
717
718 /*
719  * It is very important to gather dirty pages and write at once, so that we can
720  * submit a big bio without interfering other data writes.
721  * By default, 512 pages for directory data,
722  * 512 pages (2MB) * 3 for three types of nodes, and
723  * max_bio_blocks for meta are set.
724  */
725 static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
726 {
727         if (sbi->sb->s_bdi->wb.dirty_exceeded)
728                 return 0;
729
730         if (type == DATA)
731                 return sbi->blocks_per_seg;
732         else if (type == NODE)
733                 return 3 * sbi->blocks_per_seg;
734         else if (type == META)
735                 return MAX_BIO_BLOCKS(sbi);
736         else
737                 return 0;
738 }
739
740 /*
741  * When writing pages, it'd better align nr_to_write for segment size.
742  */
743 static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
744                                         struct writeback_control *wbc)
745 {
746         long nr_to_write, desired;
747
748         if (wbc->sync_mode != WB_SYNC_NONE)
749                 return 0;
750
751         nr_to_write = wbc->nr_to_write;
752
753         if (type == DATA)
754                 desired = 4096;
755         else if (type == NODE)
756                 desired = 3 * max_hw_blocks(sbi);
757         else
758                 desired = MAX_BIO_BLOCKS(sbi);
759
760         wbc->nr_to_write = desired;
761         return desired - nr_to_write;
762 }