blob: c4c79885c99344aa9e999b5562fea34954356276 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002 * fs/f2fs/node.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/* start node id of a node block dedicated to the given node id */
12#define START_NID(nid) ((nid / NAT_ENTRY_PER_BLOCK) * NAT_ENTRY_PER_BLOCK)
13
14/* node block offset on the NAT area dedicated to the given start node id */
15#define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
16
17/* # of pages to perform readahead before building free nids */
18#define FREE_NID_PAGES 4
19
20/* maximum # of free node ids to produce during build_free_nids */
21#define MAX_FREE_NIDS (NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES)
22
23/* maximum readahead size for node during getting data blocks */
24#define MAX_RA_NODE 128
25
26/* maximum cached nat entries to manage memory footprint */
27#define NM_WOUT_THRESHOLD (64 * NAT_ENTRY_PER_BLOCK)
28
29/* vector size for gang look-up from nat cache that consists of radix tree */
30#define NATVEC_SIZE 64
31
Jaegeuk Kim56ae6742013-03-31 12:47:20 +090032/* return value for read_node_page */
33#define LOCKED_PAGE 1
34
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090035/*
36 * For node information
37 */
38struct node_info {
39 nid_t nid; /* node id */
40 nid_t ino; /* inode number of the node's owner */
41 block_t blk_addr; /* block address of the node */
42 unsigned char version; /* version of the node */
43};
44
45struct nat_entry {
46 struct list_head list; /* for clean or dirty nat list */
47 bool checkpointed; /* whether it is checkpointed or not */
48 struct node_info ni; /* in-memory node information */
49};
50
51#define nat_get_nid(nat) (nat->ni.nid)
52#define nat_set_nid(nat, n) (nat->ni.nid = n)
53#define nat_get_blkaddr(nat) (nat->ni.blk_addr)
54#define nat_set_blkaddr(nat, b) (nat->ni.blk_addr = b)
55#define nat_get_ino(nat) (nat->ni.ino)
56#define nat_set_ino(nat, i) (nat->ni.ino = i)
57#define nat_get_version(nat) (nat->ni.version)
58#define nat_set_version(nat, v) (nat->ni.version = v)
59
60#define __set_nat_cache_dirty(nm_i, ne) \
61 list_move_tail(&ne->list, &nm_i->dirty_nat_entries);
62#define __clear_nat_cache_dirty(nm_i, ne) \
63 list_move_tail(&ne->list, &nm_i->nat_entries);
64#define inc_node_version(version) (++version)
65
66static inline void node_info_from_raw_nat(struct node_info *ni,
67 struct f2fs_nat_entry *raw_ne)
68{
69 ni->ino = le32_to_cpu(raw_ne->ino);
70 ni->blk_addr = le32_to_cpu(raw_ne->block_addr);
71 ni->version = raw_ne->version;
72}
73
74/*
75 * For free nid mangement
76 */
77enum nid_state {
78 NID_NEW, /* newly added to free nid list */
79 NID_ALLOC /* it is allocated */
80};
81
82struct free_nid {
83 struct list_head list; /* for free node id list */
84 nid_t nid; /* node id */
85 int state; /* in use or not: NID_NEW or NID_ALLOC */
86};
87
88static inline int next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
89{
90 struct f2fs_nm_info *nm_i = NM_I(sbi);
91 struct free_nid *fnid;
92
93 if (nm_i->fcnt <= 0)
94 return -1;
95 spin_lock(&nm_i->free_nid_list_lock);
96 fnid = list_entry(nm_i->free_nid_list.next, struct free_nid, list);
97 *nid = fnid->nid;
98 spin_unlock(&nm_i->free_nid_list_lock);
99 return 0;
100}
101
102/*
103 * inline functions
104 */
105static inline void get_nat_bitmap(struct f2fs_sb_info *sbi, void *addr)
106{
107 struct f2fs_nm_info *nm_i = NM_I(sbi);
108 memcpy(addr, nm_i->nat_bitmap, nm_i->bitmap_size);
109}
110
111static inline pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start)
112{
113 struct f2fs_nm_info *nm_i = NM_I(sbi);
114 pgoff_t block_off;
115 pgoff_t block_addr;
116 int seg_off;
117
118 block_off = NAT_BLOCK_OFFSET(start);
119 seg_off = block_off >> sbi->log_blocks_per_seg;
120
121 block_addr = (pgoff_t)(nm_i->nat_blkaddr +
122 (seg_off << sbi->log_blocks_per_seg << 1) +
123 (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
124
125 if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
126 block_addr += sbi->blocks_per_seg;
127
128 return block_addr;
129}
130
131static inline pgoff_t next_nat_addr(struct f2fs_sb_info *sbi,
132 pgoff_t block_addr)
133{
134 struct f2fs_nm_info *nm_i = NM_I(sbi);
135
136 block_addr -= nm_i->nat_blkaddr;
137 if ((block_addr >> sbi->log_blocks_per_seg) % 2)
138 block_addr -= sbi->blocks_per_seg;
139 else
140 block_addr += sbi->blocks_per_seg;
141
142 return block_addr + nm_i->nat_blkaddr;
143}
144
145static inline void set_to_next_nat(struct f2fs_nm_info *nm_i, nid_t start_nid)
146{
147 unsigned int block_off = NAT_BLOCK_OFFSET(start_nid);
148
149 if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
150 f2fs_clear_bit(block_off, nm_i->nat_bitmap);
151 else
152 f2fs_set_bit(block_off, nm_i->nat_bitmap);
153}
154
155static inline void fill_node_footer(struct page *page, nid_t nid,
156 nid_t ino, unsigned int ofs, bool reset)
157{
Gu Zheng45590712013-07-15 17:57:38 +0800158 struct f2fs_node *rn = F2FS_NODE(page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900159 if (reset)
160 memset(rn, 0, sizeof(*rn));
161 rn->footer.nid = cpu_to_le32(nid);
162 rn->footer.ino = cpu_to_le32(ino);
163 rn->footer.flag = cpu_to_le32(ofs << OFFSET_BIT_SHIFT);
164}
165
166static inline void copy_node_footer(struct page *dst, struct page *src)
167{
Gu Zheng45590712013-07-15 17:57:38 +0800168 struct f2fs_node *src_rn = F2FS_NODE(src);
169 struct f2fs_node *dst_rn = F2FS_NODE(dst);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900170 memcpy(&dst_rn->footer, &src_rn->footer, sizeof(struct node_footer));
171}
172
173static inline void fill_node_footer_blkaddr(struct page *page, block_t blkaddr)
174{
175 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
176 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Gu Zheng45590712013-07-15 17:57:38 +0800177 struct f2fs_node *rn = F2FS_NODE(page);
178
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900179 rn->footer.cp_ver = ckpt->checkpoint_ver;
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900180 rn->footer.next_blkaddr = cpu_to_le32(blkaddr);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900181}
182
183static inline nid_t ino_of_node(struct page *node_page)
184{
Gu Zheng45590712013-07-15 17:57:38 +0800185 struct f2fs_node *rn = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900186 return le32_to_cpu(rn->footer.ino);
187}
188
189static inline nid_t nid_of_node(struct page *node_page)
190{
Gu Zheng45590712013-07-15 17:57:38 +0800191 struct f2fs_node *rn = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900192 return le32_to_cpu(rn->footer.nid);
193}
194
195static inline unsigned int ofs_of_node(struct page *node_page)
196{
Gu Zheng45590712013-07-15 17:57:38 +0800197 struct f2fs_node *rn = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900198 unsigned flag = le32_to_cpu(rn->footer.flag);
199 return flag >> OFFSET_BIT_SHIFT;
200}
201
202static inline unsigned long long cpver_of_node(struct page *node_page)
203{
Gu Zheng45590712013-07-15 17:57:38 +0800204 struct f2fs_node *rn = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900205 return le64_to_cpu(rn->footer.cp_ver);
206}
207
208static inline block_t next_blkaddr_of_node(struct page *node_page)
209{
Gu Zheng45590712013-07-15 17:57:38 +0800210 struct f2fs_node *rn = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900211 return le32_to_cpu(rn->footer.next_blkaddr);
212}
213
214/*
215 * f2fs assigns the following node offsets described as (num).
216 * N = NIDS_PER_BLOCK
217 *
218 * Inode block (0)
219 * |- direct node (1)
220 * |- direct node (2)
221 * |- indirect node (3)
222 * | `- direct node (4 => 4 + N - 1)
223 * |- indirect node (4 + N)
224 * | `- direct node (5 + N => 5 + 2N - 1)
225 * `- double indirect node (5 + 2N)
226 * `- indirect node (6 + 2N)
Chao Yu4f4124d2013-12-21 18:02:14 +0800227 * `- direct node
228 * ......
229 * `- indirect node ((6 + 2N) + x(N + 1))
230 * `- direct node
231 * ......
232 * `- indirect node ((6 + 2N) + (N - 1)(N + 1))
233 * `- direct node
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900234 */
235static inline bool IS_DNODE(struct page *node_page)
236{
237 unsigned int ofs = ofs_of_node(node_page);
Jaegeuk Kimdbe6a5f2013-08-09 08:14:06 +0900238
239 if (ofs == XATTR_NODE_OFFSET)
240 return false;
241
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900242 if (ofs == 3 || ofs == 4 + NIDS_PER_BLOCK ||
243 ofs == 5 + 2 * NIDS_PER_BLOCK)
244 return false;
245 if (ofs >= 6 + 2 * NIDS_PER_BLOCK) {
246 ofs -= 6 + 2 * NIDS_PER_BLOCK;
Zhihui Zhang33151012013-04-07 12:57:04 -0400247 if (!((long int)ofs % (NIDS_PER_BLOCK + 1)))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900248 return false;
249 }
250 return true;
251}
252
253static inline void set_nid(struct page *p, int off, nid_t nid, bool i)
254{
Gu Zheng45590712013-07-15 17:57:38 +0800255 struct f2fs_node *rn = F2FS_NODE(p);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900256
257 wait_on_page_writeback(p);
258
259 if (i)
260 rn->i.i_nid[off - NODE_DIR1_BLOCK] = cpu_to_le32(nid);
261 else
262 rn->in.nid[off] = cpu_to_le32(nid);
263 set_page_dirty(p);
264}
265
266static inline nid_t get_nid(struct page *p, int off, bool i)
267{
Gu Zheng45590712013-07-15 17:57:38 +0800268 struct f2fs_node *rn = F2FS_NODE(p);
269
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900270 if (i)
271 return le32_to_cpu(rn->i.i_nid[off - NODE_DIR1_BLOCK]);
272 return le32_to_cpu(rn->in.nid[off]);
273}
274
275/*
276 * Coldness identification:
277 * - Mark cold files in f2fs_inode_info
278 * - Mark cold node blocks in their node footer
279 * - Mark cold data pages in page cache
280 */
Namjae Jeona06a2412013-05-23 22:58:40 +0900281static inline int is_file(struct inode *inode, int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900282{
Namjae Jeona06a2412013-05-23 22:58:40 +0900283 return F2FS_I(inode)->i_advise & type;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900284}
285
Namjae Jeona06a2412013-05-23 22:58:40 +0900286static inline void set_file(struct inode *inode, int type)
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900287{
Namjae Jeona06a2412013-05-23 22:58:40 +0900288 F2FS_I(inode)->i_advise |= type;
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900289}
290
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900291static inline void clear_file(struct inode *inode, int type)
292{
293 F2FS_I(inode)->i_advise &= ~type;
294}
295
296#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
297#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
298#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
299#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
300#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
301#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
Jaegeuk Kim953a3e22013-03-21 15:21:57 +0900302
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900303static inline int is_cold_data(struct page *page)
304{
305 return PageChecked(page);
306}
307
308static inline void set_cold_data(struct page *page)
309{
310 SetPageChecked(page);
311}
312
313static inline void clear_cold_data(struct page *page)
314{
315 ClearPageChecked(page);
316}
317
Namjae Jeona06a2412013-05-23 22:58:40 +0900318static inline int is_node(struct page *page, int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900319{
Gu Zheng45590712013-07-15 17:57:38 +0800320 struct f2fs_node *rn = F2FS_NODE(page);
Namjae Jeona06a2412013-05-23 22:58:40 +0900321 return le32_to_cpu(rn->footer.flag) & (1 << type);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900322}
323
Namjae Jeona06a2412013-05-23 22:58:40 +0900324#define is_cold_node(page) is_node(page, COLD_BIT_SHIFT)
325#define is_fsync_dnode(page) is_node(page, FSYNC_BIT_SHIFT)
326#define is_dent_dnode(page) is_node(page, DENT_BIT_SHIFT)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900327
328static inline void set_cold_node(struct inode *inode, struct page *page)
329{
Gu Zheng45590712013-07-15 17:57:38 +0800330 struct f2fs_node *rn = F2FS_NODE(page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900331 unsigned int flag = le32_to_cpu(rn->footer.flag);
332
333 if (S_ISDIR(inode->i_mode))
334 flag &= ~(0x1 << COLD_BIT_SHIFT);
335 else
336 flag |= (0x1 << COLD_BIT_SHIFT);
337 rn->footer.flag = cpu_to_le32(flag);
338}
339
Namjae Jeona06a2412013-05-23 22:58:40 +0900340static inline void set_mark(struct page *page, int mark, int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900341{
Gu Zheng45590712013-07-15 17:57:38 +0800342 struct f2fs_node *rn = F2FS_NODE(page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900343 unsigned int flag = le32_to_cpu(rn->footer.flag);
344 if (mark)
Namjae Jeona06a2412013-05-23 22:58:40 +0900345 flag |= (0x1 << type);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900346 else
Namjae Jeona06a2412013-05-23 22:58:40 +0900347 flag &= ~(0x1 << type);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900348 rn->footer.flag = cpu_to_le32(flag);
349}
Namjae Jeona06a2412013-05-23 22:58:40 +0900350#define set_dentry_mark(page, mark) set_mark(page, mark, DENT_BIT_SHIFT)
351#define set_fsync_mark(page, mark) set_mark(page, mark, FSYNC_BIT_SHIFT)