1*59bfda1fSAndroid Build Coastguard Worker /**
2*59bfda1fSAndroid Build Coastguard Worker * mount.c
3*59bfda1fSAndroid Build Coastguard Worker *
4*59bfda1fSAndroid Build Coastguard Worker * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5*59bfda1fSAndroid Build Coastguard Worker * http://www.samsung.com/
6*59bfda1fSAndroid Build Coastguard Worker *
7*59bfda1fSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
8*59bfda1fSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License version 2 as
9*59bfda1fSAndroid Build Coastguard Worker * published by the Free Software Foundation.
10*59bfda1fSAndroid Build Coastguard Worker */
11*59bfda1fSAndroid Build Coastguard Worker #include "fsck.h"
12*59bfda1fSAndroid Build Coastguard Worker #include "node.h"
13*59bfda1fSAndroid Build Coastguard Worker #include "xattr.h"
14*59bfda1fSAndroid Build Coastguard Worker #include "quota.h"
15*59bfda1fSAndroid Build Coastguard Worker #include <locale.h>
16*59bfda1fSAndroid Build Coastguard Worker #include <stdbool.h>
17*59bfda1fSAndroid Build Coastguard Worker #include <time.h>
18*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_LINUX_POSIX_ACL_H
19*59bfda1fSAndroid Build Coastguard Worker #include <linux/posix_acl.h>
20*59bfda1fSAndroid Build Coastguard Worker #endif
21*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_ACL_H
22*59bfda1fSAndroid Build Coastguard Worker #include <sys/acl.h>
23*59bfda1fSAndroid Build Coastguard Worker #endif
24*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_UUID_UUID_H
25*59bfda1fSAndroid Build Coastguard Worker #include <uuid/uuid.h>
26*59bfda1fSAndroid Build Coastguard Worker #endif
27*59bfda1fSAndroid Build Coastguard Worker
28*59bfda1fSAndroid Build Coastguard Worker #ifndef ACL_UNDEFINED_TAG
29*59bfda1fSAndroid Build Coastguard Worker #define ACL_UNDEFINED_TAG (0x00)
30*59bfda1fSAndroid Build Coastguard Worker #define ACL_USER_OBJ (0x01)
31*59bfda1fSAndroid Build Coastguard Worker #define ACL_USER (0x02)
32*59bfda1fSAndroid Build Coastguard Worker #define ACL_GROUP_OBJ (0x04)
33*59bfda1fSAndroid Build Coastguard Worker #define ACL_GROUP (0x08)
34*59bfda1fSAndroid Build Coastguard Worker #define ACL_MASK (0x10)
35*59bfda1fSAndroid Build Coastguard Worker #define ACL_OTHER (0x20)
36*59bfda1fSAndroid Build Coastguard Worker #endif
37*59bfda1fSAndroid Build Coastguard Worker
38*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_LINUX_BLKZONED_H
39*59bfda1fSAndroid Build Coastguard Worker
get_device_idx(struct f2fs_sb_info * sbi,uint32_t segno)40*59bfda1fSAndroid Build Coastguard Worker static int get_device_idx(struct f2fs_sb_info *sbi, uint32_t segno)
41*59bfda1fSAndroid Build Coastguard Worker {
42*59bfda1fSAndroid Build Coastguard Worker block_t seg_start_blkaddr;
43*59bfda1fSAndroid Build Coastguard Worker int i;
44*59bfda1fSAndroid Build Coastguard Worker
45*59bfda1fSAndroid Build Coastguard Worker seg_start_blkaddr = SM_I(sbi)->main_blkaddr +
46*59bfda1fSAndroid Build Coastguard Worker segno * DEFAULT_BLOCKS_PER_SEGMENT;
47*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < c.ndevs; i++)
48*59bfda1fSAndroid Build Coastguard Worker if (c.devices[i].start_blkaddr <= seg_start_blkaddr &&
49*59bfda1fSAndroid Build Coastguard Worker c.devices[i].end_blkaddr > seg_start_blkaddr)
50*59bfda1fSAndroid Build Coastguard Worker return i;
51*59bfda1fSAndroid Build Coastguard Worker return 0;
52*59bfda1fSAndroid Build Coastguard Worker }
53*59bfda1fSAndroid Build Coastguard Worker
get_zone_idx_from_dev(struct f2fs_sb_info * sbi,uint32_t segno,uint32_t dev_idx)54*59bfda1fSAndroid Build Coastguard Worker static int get_zone_idx_from_dev(struct f2fs_sb_info *sbi,
55*59bfda1fSAndroid Build Coastguard Worker uint32_t segno, uint32_t dev_idx)
56*59bfda1fSAndroid Build Coastguard Worker {
57*59bfda1fSAndroid Build Coastguard Worker block_t seg_start_blkaddr = START_BLOCK(sbi, segno);
58*59bfda1fSAndroid Build Coastguard Worker
59*59bfda1fSAndroid Build Coastguard Worker return (seg_start_blkaddr - c.devices[dev_idx].start_blkaddr) /
60*59bfda1fSAndroid Build Coastguard Worker (sbi->segs_per_sec * sbi->blocks_per_seg);
61*59bfda1fSAndroid Build Coastguard Worker }
62*59bfda1fSAndroid Build Coastguard Worker
is_usable_seg(struct f2fs_sb_info * sbi,unsigned int segno)63*59bfda1fSAndroid Build Coastguard Worker bool is_usable_seg(struct f2fs_sb_info *sbi, unsigned int segno)
64*59bfda1fSAndroid Build Coastguard Worker {
65*59bfda1fSAndroid Build Coastguard Worker block_t seg_start = START_BLOCK(sbi, segno);
66*59bfda1fSAndroid Build Coastguard Worker unsigned int dev_idx = get_device_idx(sbi, segno);
67*59bfda1fSAndroid Build Coastguard Worker unsigned int zone_idx = get_zone_idx_from_dev(sbi, segno, dev_idx);
68*59bfda1fSAndroid Build Coastguard Worker unsigned int sec_start_blkaddr = START_BLOCK(sbi,
69*59bfda1fSAndroid Build Coastguard Worker GET_SEG_FROM_SEC(sbi, segno / sbi->segs_per_sec));
70*59bfda1fSAndroid Build Coastguard Worker
71*59bfda1fSAndroid Build Coastguard Worker if (zone_idx < c.devices[dev_idx].nr_rnd_zones)
72*59bfda1fSAndroid Build Coastguard Worker return true;
73*59bfda1fSAndroid Build Coastguard Worker
74*59bfda1fSAndroid Build Coastguard Worker if (c.devices[dev_idx].zoned_model != F2FS_ZONED_HM)
75*59bfda1fSAndroid Build Coastguard Worker return true;
76*59bfda1fSAndroid Build Coastguard Worker
77*59bfda1fSAndroid Build Coastguard Worker return seg_start < sec_start_blkaddr +
78*59bfda1fSAndroid Build Coastguard Worker c.devices[dev_idx].zone_cap_blocks[zone_idx];
79*59bfda1fSAndroid Build Coastguard Worker }
80*59bfda1fSAndroid Build Coastguard Worker
get_usable_seg_count(struct f2fs_sb_info * sbi)81*59bfda1fSAndroid Build Coastguard Worker unsigned int get_usable_seg_count(struct f2fs_sb_info *sbi)
82*59bfda1fSAndroid Build Coastguard Worker {
83*59bfda1fSAndroid Build Coastguard Worker unsigned int i, usable_seg_count = 0;
84*59bfda1fSAndroid Build Coastguard Worker
85*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < MAIN_SEGS(sbi); i++)
86*59bfda1fSAndroid Build Coastguard Worker if (is_usable_seg(sbi, i))
87*59bfda1fSAndroid Build Coastguard Worker usable_seg_count++;
88*59bfda1fSAndroid Build Coastguard Worker
89*59bfda1fSAndroid Build Coastguard Worker return usable_seg_count;
90*59bfda1fSAndroid Build Coastguard Worker }
91*59bfda1fSAndroid Build Coastguard Worker
92*59bfda1fSAndroid Build Coastguard Worker #else
93*59bfda1fSAndroid Build Coastguard Worker
is_usable_seg(struct f2fs_sb_info * UNUSED (sbi),unsigned int UNUSED (segno))94*59bfda1fSAndroid Build Coastguard Worker bool is_usable_seg(struct f2fs_sb_info *UNUSED(sbi), unsigned int UNUSED(segno))
95*59bfda1fSAndroid Build Coastguard Worker {
96*59bfda1fSAndroid Build Coastguard Worker return true;
97*59bfda1fSAndroid Build Coastguard Worker }
98*59bfda1fSAndroid Build Coastguard Worker
get_usable_seg_count(struct f2fs_sb_info * sbi)99*59bfda1fSAndroid Build Coastguard Worker unsigned int get_usable_seg_count(struct f2fs_sb_info *sbi)
100*59bfda1fSAndroid Build Coastguard Worker {
101*59bfda1fSAndroid Build Coastguard Worker return MAIN_SEGS(sbi);
102*59bfda1fSAndroid Build Coastguard Worker }
103*59bfda1fSAndroid Build Coastguard Worker
104*59bfda1fSAndroid Build Coastguard Worker #endif
105*59bfda1fSAndroid Build Coastguard Worker
get_free_segments(struct f2fs_sb_info * sbi)106*59bfda1fSAndroid Build Coastguard Worker u32 get_free_segments(struct f2fs_sb_info *sbi)
107*59bfda1fSAndroid Build Coastguard Worker {
108*59bfda1fSAndroid Build Coastguard Worker u32 i, free_segs = 0;
109*59bfda1fSAndroid Build Coastguard Worker
110*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < MAIN_SEGS(sbi); i++) {
111*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se = get_seg_entry(sbi, i);
112*59bfda1fSAndroid Build Coastguard Worker
113*59bfda1fSAndroid Build Coastguard Worker if (se->valid_blocks == 0x0 && !IS_CUR_SEGNO(sbi, i) &&
114*59bfda1fSAndroid Build Coastguard Worker is_usable_seg(sbi, i))
115*59bfda1fSAndroid Build Coastguard Worker free_segs++;
116*59bfda1fSAndroid Build Coastguard Worker }
117*59bfda1fSAndroid Build Coastguard Worker return free_segs;
118*59bfda1fSAndroid Build Coastguard Worker }
119*59bfda1fSAndroid Build Coastguard Worker
update_free_segments(struct f2fs_sb_info * sbi)120*59bfda1fSAndroid Build Coastguard Worker void update_free_segments(struct f2fs_sb_info *sbi)
121*59bfda1fSAndroid Build Coastguard Worker {
122*59bfda1fSAndroid Build Coastguard Worker char *progress = "-*|*-";
123*59bfda1fSAndroid Build Coastguard Worker static int i = 0;
124*59bfda1fSAndroid Build Coastguard Worker
125*59bfda1fSAndroid Build Coastguard Worker if (c.dbg_lv)
126*59bfda1fSAndroid Build Coastguard Worker return;
127*59bfda1fSAndroid Build Coastguard Worker
128*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\r [ %c ] Free segments: 0x%x", progress[i % 5], SM_I(sbi)->free_segments);
129*59bfda1fSAndroid Build Coastguard Worker fflush(stdout);
130*59bfda1fSAndroid Build Coastguard Worker i++;
131*59bfda1fSAndroid Build Coastguard Worker }
132*59bfda1fSAndroid Build Coastguard Worker
133*59bfda1fSAndroid Build Coastguard Worker #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
print_acl(const u8 * value,int size)134*59bfda1fSAndroid Build Coastguard Worker static void print_acl(const u8 *value, int size)
135*59bfda1fSAndroid Build Coastguard Worker {
136*59bfda1fSAndroid Build Coastguard Worker const struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
137*59bfda1fSAndroid Build Coastguard Worker const struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
138*59bfda1fSAndroid Build Coastguard Worker const u8 *end = value + size;
139*59bfda1fSAndroid Build Coastguard Worker int i, count;
140*59bfda1fSAndroid Build Coastguard Worker
141*59bfda1fSAndroid Build Coastguard Worker if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION)) {
142*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid ACL version [0x%x : 0x%x]\n",
143*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(hdr->a_version), F2FS_ACL_VERSION);
144*59bfda1fSAndroid Build Coastguard Worker return;
145*59bfda1fSAndroid Build Coastguard Worker }
146*59bfda1fSAndroid Build Coastguard Worker
147*59bfda1fSAndroid Build Coastguard Worker count = f2fs_acl_count(size);
148*59bfda1fSAndroid Build Coastguard Worker if (count <= 0) {
149*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid ACL value size %d\n", size);
150*59bfda1fSAndroid Build Coastguard Worker return;
151*59bfda1fSAndroid Build Coastguard Worker }
152*59bfda1fSAndroid Build Coastguard Worker
153*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < count; i++) {
154*59bfda1fSAndroid Build Coastguard Worker if ((u8 *)entry > end) {
155*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid ACL entries count %d\n", count);
156*59bfda1fSAndroid Build Coastguard Worker return;
157*59bfda1fSAndroid Build Coastguard Worker }
158*59bfda1fSAndroid Build Coastguard Worker
159*59bfda1fSAndroid Build Coastguard Worker switch (le16_to_cpu(entry->e_tag)) {
160*59bfda1fSAndroid Build Coastguard Worker case ACL_USER_OBJ:
161*59bfda1fSAndroid Build Coastguard Worker case ACL_GROUP_OBJ:
162*59bfda1fSAndroid Build Coastguard Worker case ACL_MASK:
163*59bfda1fSAndroid Build Coastguard Worker case ACL_OTHER:
164*59bfda1fSAndroid Build Coastguard Worker MSG(0, "tag:0x%x perm:0x%x\n",
165*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_tag),
166*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_perm));
167*59bfda1fSAndroid Build Coastguard Worker entry = (struct f2fs_acl_entry *)((char *)entry +
168*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_acl_entry_short));
169*59bfda1fSAndroid Build Coastguard Worker break;
170*59bfda1fSAndroid Build Coastguard Worker case ACL_USER:
171*59bfda1fSAndroid Build Coastguard Worker MSG(0, "tag:0x%x perm:0x%x uid:%u\n",
172*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_tag),
173*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_perm),
174*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(entry->e_id));
175*59bfda1fSAndroid Build Coastguard Worker entry = (struct f2fs_acl_entry *)((char *)entry +
176*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_acl_entry));
177*59bfda1fSAndroid Build Coastguard Worker break;
178*59bfda1fSAndroid Build Coastguard Worker case ACL_GROUP:
179*59bfda1fSAndroid Build Coastguard Worker MSG(0, "tag:0x%x perm:0x%x gid:%u\n",
180*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_tag),
181*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_perm),
182*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(entry->e_id));
183*59bfda1fSAndroid Build Coastguard Worker entry = (struct f2fs_acl_entry *)((char *)entry +
184*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_acl_entry));
185*59bfda1fSAndroid Build Coastguard Worker break;
186*59bfda1fSAndroid Build Coastguard Worker default:
187*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Unknown ACL tag 0x%x\n",
188*59bfda1fSAndroid Build Coastguard Worker le16_to_cpu(entry->e_tag));
189*59bfda1fSAndroid Build Coastguard Worker return;
190*59bfda1fSAndroid Build Coastguard Worker }
191*59bfda1fSAndroid Build Coastguard Worker }
192*59bfda1fSAndroid Build Coastguard Worker }
193*59bfda1fSAndroid Build Coastguard Worker #endif /* HAVE_LINUX_POSIX_ACL_H || HAVE_SYS_ACL_H */
194*59bfda1fSAndroid Build Coastguard Worker
print_xattr_entry(const struct f2fs_xattr_entry * ent)195*59bfda1fSAndroid Build Coastguard Worker static void print_xattr_entry(const struct f2fs_xattr_entry *ent)
196*59bfda1fSAndroid Build Coastguard Worker {
197*59bfda1fSAndroid Build Coastguard Worker const u8 *value = (const u8 *)&ent->e_name[ent->e_name_len];
198*59bfda1fSAndroid Build Coastguard Worker const int size = le16_to_cpu(ent->e_value_size);
199*59bfda1fSAndroid Build Coastguard Worker char *enc_name = F2FS_XATTR_NAME_ENCRYPTION_CONTEXT;
200*59bfda1fSAndroid Build Coastguard Worker u32 enc_name_len = strlen(enc_name);
201*59bfda1fSAndroid Build Coastguard Worker const union fscrypt_context *ctx;
202*59bfda1fSAndroid Build Coastguard Worker const struct fsverity_descriptor_location *dloc;
203*59bfda1fSAndroid Build Coastguard Worker int i;
204*59bfda1fSAndroid Build Coastguard Worker
205*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\nxattr: e_name_index:%d e_name:", ent->e_name_index);
206*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < ent->e_name_len; i++)
207*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%c", ent->e_name[i]);
208*59bfda1fSAndroid Build Coastguard Worker MSG(0, " e_name_len:%d e_value_size:%d e_value:\n",
209*59bfda1fSAndroid Build Coastguard Worker ent->e_name_len, size);
210*59bfda1fSAndroid Build Coastguard Worker
211*59bfda1fSAndroid Build Coastguard Worker switch (ent->e_name_index) {
212*59bfda1fSAndroid Build Coastguard Worker #if defined(HAVE_LINUX_POSIX_ACL_H) || defined(HAVE_SYS_ACL_H)
213*59bfda1fSAndroid Build Coastguard Worker case F2FS_XATTR_INDEX_POSIX_ACL_ACCESS:
214*59bfda1fSAndroid Build Coastguard Worker case F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT:
215*59bfda1fSAndroid Build Coastguard Worker print_acl(value, size);
216*59bfda1fSAndroid Build Coastguard Worker return;
217*59bfda1fSAndroid Build Coastguard Worker #endif
218*59bfda1fSAndroid Build Coastguard Worker case F2FS_XATTR_INDEX_ENCRYPTION:
219*59bfda1fSAndroid Build Coastguard Worker if (ent->e_name_len != enc_name_len ||
220*59bfda1fSAndroid Build Coastguard Worker memcmp(ent->e_name, enc_name, enc_name_len))
221*59bfda1fSAndroid Build Coastguard Worker break;
222*59bfda1fSAndroid Build Coastguard Worker ctx = (const union fscrypt_context *)value;
223*59bfda1fSAndroid Build Coastguard Worker if (size == 0 || size != fscrypt_context_size(ctx))
224*59bfda1fSAndroid Build Coastguard Worker break;
225*59bfda1fSAndroid Build Coastguard Worker switch (ctx->version) {
226*59bfda1fSAndroid Build Coastguard Worker case FSCRYPT_CONTEXT_V1:
227*59bfda1fSAndroid Build Coastguard Worker MSG(0, "format: %d\n", ctx->version);
228*59bfda1fSAndroid Build Coastguard Worker MSG(0, "contents_encryption_mode: 0x%x\n", ctx->v1.contents_encryption_mode);
229*59bfda1fSAndroid Build Coastguard Worker MSG(0, "filenames_encryption_mode: 0x%x\n", ctx->v1.filenames_encryption_mode);
230*59bfda1fSAndroid Build Coastguard Worker MSG(0, "flags: 0x%x\n", ctx->v1.flags);
231*59bfda1fSAndroid Build Coastguard Worker MSG(0, "master_key_descriptor: ");
232*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < FSCRYPT_KEY_DESCRIPTOR_SIZE; i++)
233*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%02X", ctx->v1.master_key_descriptor[i]);
234*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\nnonce: ");
235*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < FSCRYPT_FILE_NONCE_SIZE; i++)
236*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%02X", ctx->v1.nonce[i]);
237*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
238*59bfda1fSAndroid Build Coastguard Worker return;
239*59bfda1fSAndroid Build Coastguard Worker case FSCRYPT_CONTEXT_V2:
240*59bfda1fSAndroid Build Coastguard Worker MSG(0, "format: %d\n", ctx->version);
241*59bfda1fSAndroid Build Coastguard Worker MSG(0, "contents_encryption_mode: 0x%x\n", ctx->v2.contents_encryption_mode);
242*59bfda1fSAndroid Build Coastguard Worker MSG(0, "filenames_encryption_mode: 0x%x\n", ctx->v2.filenames_encryption_mode);
243*59bfda1fSAndroid Build Coastguard Worker MSG(0, "flags: 0x%x\n", ctx->v2.flags);
244*59bfda1fSAndroid Build Coastguard Worker MSG(0, "master_key_identifier: ");
245*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < FSCRYPT_KEY_IDENTIFIER_SIZE; i++)
246*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%02X", ctx->v2.master_key_identifier[i]);
247*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\nnonce: ");
248*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < FSCRYPT_FILE_NONCE_SIZE; i++)
249*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%02X", ctx->v2.nonce[i]);
250*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
251*59bfda1fSAndroid Build Coastguard Worker return;
252*59bfda1fSAndroid Build Coastguard Worker }
253*59bfda1fSAndroid Build Coastguard Worker break;
254*59bfda1fSAndroid Build Coastguard Worker case F2FS_XATTR_INDEX_VERITY:
255*59bfda1fSAndroid Build Coastguard Worker dloc = (const struct fsverity_descriptor_location *)value;
256*59bfda1fSAndroid Build Coastguard Worker if (ent->e_name_len != strlen(F2FS_XATTR_NAME_VERITY) ||
257*59bfda1fSAndroid Build Coastguard Worker memcmp(ent->e_name, F2FS_XATTR_NAME_VERITY,
258*59bfda1fSAndroid Build Coastguard Worker ent->e_name_len))
259*59bfda1fSAndroid Build Coastguard Worker break;
260*59bfda1fSAndroid Build Coastguard Worker if (size != sizeof(*dloc))
261*59bfda1fSAndroid Build Coastguard Worker break;
262*59bfda1fSAndroid Build Coastguard Worker MSG(0, "version: %u\n", le32_to_cpu(dloc->version));
263*59bfda1fSAndroid Build Coastguard Worker MSG(0, "size: %u\n", le32_to_cpu(dloc->size));
264*59bfda1fSAndroid Build Coastguard Worker MSG(0, "pos: %"PRIu64"\n", le64_to_cpu(dloc->pos));
265*59bfda1fSAndroid Build Coastguard Worker return;
266*59bfda1fSAndroid Build Coastguard Worker }
267*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < size; i++)
268*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%02X", value[i]);
269*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
270*59bfda1fSAndroid Build Coastguard Worker }
271*59bfda1fSAndroid Build Coastguard Worker
print_inode_info(struct f2fs_sb_info * sbi,struct f2fs_node * node,int name)272*59bfda1fSAndroid Build Coastguard Worker void print_inode_info(struct f2fs_sb_info *sbi,
273*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node, int name)
274*59bfda1fSAndroid Build Coastguard Worker {
275*59bfda1fSAndroid Build Coastguard Worker struct f2fs_inode *inode = &node->i;
276*59bfda1fSAndroid Build Coastguard Worker void *xattr_addr;
277*59bfda1fSAndroid Build Coastguard Worker void *last_base_addr;
278*59bfda1fSAndroid Build Coastguard Worker struct f2fs_xattr_entry *ent;
279*59bfda1fSAndroid Build Coastguard Worker char en[F2FS_PRINT_NAMELEN];
280*59bfda1fSAndroid Build Coastguard Worker unsigned int i = 0;
281*59bfda1fSAndroid Build Coastguard Worker u32 namelen = le32_to_cpu(inode->i_namelen);
282*59bfda1fSAndroid Build Coastguard Worker int enc_name = file_enc_name(inode);
283*59bfda1fSAndroid Build Coastguard Worker int ofs = get_extra_isize(node);
284*59bfda1fSAndroid Build Coastguard Worker
285*59bfda1fSAndroid Build Coastguard Worker pretty_print_filename(inode->i_name, namelen, en, enc_name);
286*59bfda1fSAndroid Build Coastguard Worker if (name && en[0]) {
287*59bfda1fSAndroid Build Coastguard Worker MSG(0, " - File name : %s%s\n", en,
288*59bfda1fSAndroid Build Coastguard Worker enc_name ? " <encrypted>" : "");
289*59bfda1fSAndroid Build Coastguard Worker setlocale(LC_ALL, "");
290*59bfda1fSAndroid Build Coastguard Worker MSG(0, " - File size : %'" PRIu64 " (bytes)\n",
291*59bfda1fSAndroid Build Coastguard Worker le64_to_cpu(inode->i_size));
292*59bfda1fSAndroid Build Coastguard Worker return;
293*59bfda1fSAndroid Build Coastguard Worker }
294*59bfda1fSAndroid Build Coastguard Worker
295*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_mode);
296*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_advise);
297*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_uid);
298*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_gid);
299*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_links);
300*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_size);
301*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_blocks);
302*59bfda1fSAndroid Build Coastguard Worker
303*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_atime);
304*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_atime_nsec);
305*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_ctime);
306*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_ctime_nsec);
307*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_mtime);
308*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_mtime_nsec);
309*59bfda1fSAndroid Build Coastguard Worker
310*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_generation);
311*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_current_depth);
312*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_xattr_nid);
313*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_flags);
314*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_inline);
315*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_pino);
316*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_dir_level);
317*59bfda1fSAndroid Build Coastguard Worker
318*59bfda1fSAndroid Build Coastguard Worker if (en[0]) {
319*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_namelen);
320*59bfda1fSAndroid Build Coastguard Worker printf("%-30s\t\t[%s]\n", "i_name", en);
321*59bfda1fSAndroid Build Coastguard Worker }
322*59bfda1fSAndroid Build Coastguard Worker
323*59bfda1fSAndroid Build Coastguard Worker printf("i_ext: fofs:%x blkaddr:%x len:%x\n",
324*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(inode->i_ext.fofs),
325*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(inode->i_ext.blk_addr),
326*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(inode->i_ext.len));
327*59bfda1fSAndroid Build Coastguard Worker
328*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_EXTRA_ATTR) {
329*59bfda1fSAndroid Build Coastguard Worker DISP_u16(inode, i_extra_isize);
330*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)
331*59bfda1fSAndroid Build Coastguard Worker DISP_u16(inode, i_inline_xattr_size);
332*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_PRJQUOTA)
333*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_projid);
334*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_INODE_CHKSUM)
335*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_inode_checksum);
336*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_INODE_CRTIME) {
337*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_crtime);
338*59bfda1fSAndroid Build Coastguard Worker DISP_u32(inode, i_crtime_nsec);
339*59bfda1fSAndroid Build Coastguard Worker }
340*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_COMPRESSION) {
341*59bfda1fSAndroid Build Coastguard Worker DISP_u64(inode, i_compr_blocks);
342*59bfda1fSAndroid Build Coastguard Worker DISP_u8(inode, i_compress_algorithm);
343*59bfda1fSAndroid Build Coastguard Worker DISP_u8(inode, i_log_cluster_size);
344*59bfda1fSAndroid Build Coastguard Worker DISP_u16(inode, i_compress_flag);
345*59bfda1fSAndroid Build Coastguard Worker }
346*59bfda1fSAndroid Build Coastguard Worker }
347*59bfda1fSAndroid Build Coastguard Worker
348*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < ADDRS_PER_INODE(inode); i++) {
349*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr;
350*59bfda1fSAndroid Build Coastguard Worker char *flag = "";
351*59bfda1fSAndroid Build Coastguard Worker
352*59bfda1fSAndroid Build Coastguard Worker if (i + ofs >= DEF_ADDRS_PER_INODE)
353*59bfda1fSAndroid Build Coastguard Worker break;
354*59bfda1fSAndroid Build Coastguard Worker
355*59bfda1fSAndroid Build Coastguard Worker blkaddr = le32_to_cpu(inode->i_addr[i + ofs]);
356*59bfda1fSAndroid Build Coastguard Worker
357*59bfda1fSAndroid Build Coastguard Worker if (blkaddr == 0x0)
358*59bfda1fSAndroid Build Coastguard Worker continue;
359*59bfda1fSAndroid Build Coastguard Worker if (blkaddr == COMPRESS_ADDR)
360*59bfda1fSAndroid Build Coastguard Worker flag = "cluster flag";
361*59bfda1fSAndroid Build Coastguard Worker else if (blkaddr == NEW_ADDR)
362*59bfda1fSAndroid Build Coastguard Worker flag = "reserved flag";
363*59bfda1fSAndroid Build Coastguard Worker printf("i_addr[0x%x] %-16s\t\t[0x%8x : %u]\n", i + ofs, flag,
364*59bfda1fSAndroid Build Coastguard Worker blkaddr, blkaddr);
365*59bfda1fSAndroid Build Coastguard Worker }
366*59bfda1fSAndroid Build Coastguard Worker
367*59bfda1fSAndroid Build Coastguard Worker DISP_u32(F2FS_INODE_NIDS(inode), i_nid[0]); /* direct */
368*59bfda1fSAndroid Build Coastguard Worker DISP_u32(F2FS_INODE_NIDS(inode), i_nid[1]); /* direct */
369*59bfda1fSAndroid Build Coastguard Worker DISP_u32(F2FS_INODE_NIDS(inode), i_nid[2]); /* indirect */
370*59bfda1fSAndroid Build Coastguard Worker DISP_u32(F2FS_INODE_NIDS(inode), i_nid[3]); /* indirect */
371*59bfda1fSAndroid Build Coastguard Worker DISP_u32(F2FS_INODE_NIDS(inode), i_nid[4]); /* double indirect */
372*59bfda1fSAndroid Build Coastguard Worker
373*59bfda1fSAndroid Build Coastguard Worker xattr_addr = read_all_xattrs(sbi, node, true);
374*59bfda1fSAndroid Build Coastguard Worker if (!xattr_addr)
375*59bfda1fSAndroid Build Coastguard Worker goto out;
376*59bfda1fSAndroid Build Coastguard Worker
377*59bfda1fSAndroid Build Coastguard Worker last_base_addr = (void *)xattr_addr + XATTR_SIZE(&node->i);
378*59bfda1fSAndroid Build Coastguard Worker
379*59bfda1fSAndroid Build Coastguard Worker list_for_each_xattr(ent, xattr_addr) {
380*59bfda1fSAndroid Build Coastguard Worker if ((void *)(ent) + sizeof(__u32) > last_base_addr ||
381*59bfda1fSAndroid Build Coastguard Worker (void *)XATTR_NEXT_ENTRY(ent) > last_base_addr) {
382*59bfda1fSAndroid Build Coastguard Worker MSG(0, "xattr entry crosses the end of xattr space\n");
383*59bfda1fSAndroid Build Coastguard Worker break;
384*59bfda1fSAndroid Build Coastguard Worker }
385*59bfda1fSAndroid Build Coastguard Worker print_xattr_entry(ent);
386*59bfda1fSAndroid Build Coastguard Worker }
387*59bfda1fSAndroid Build Coastguard Worker free(xattr_addr);
388*59bfda1fSAndroid Build Coastguard Worker
389*59bfda1fSAndroid Build Coastguard Worker out:
390*59bfda1fSAndroid Build Coastguard Worker printf("\n");
391*59bfda1fSAndroid Build Coastguard Worker }
392*59bfda1fSAndroid Build Coastguard Worker
print_node_info(struct f2fs_sb_info * sbi,struct f2fs_node * node_block,int verbose)393*59bfda1fSAndroid Build Coastguard Worker void print_node_info(struct f2fs_sb_info *sbi,
394*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node_block, int verbose)
395*59bfda1fSAndroid Build Coastguard Worker {
396*59bfda1fSAndroid Build Coastguard Worker nid_t ino = le32_to_cpu(F2FS_NODE_FOOTER(node_block)->ino);
397*59bfda1fSAndroid Build Coastguard Worker nid_t nid = le32_to_cpu(F2FS_NODE_FOOTER(node_block)->nid);
398*59bfda1fSAndroid Build Coastguard Worker /* Is this inode? */
399*59bfda1fSAndroid Build Coastguard Worker if (ino == nid) {
400*59bfda1fSAndroid Build Coastguard Worker DBG(verbose, "Node ID [0x%x:%u] is inode\n", nid, nid);
401*59bfda1fSAndroid Build Coastguard Worker print_inode_info(sbi, node_block, verbose);
402*59bfda1fSAndroid Build Coastguard Worker } else {
403*59bfda1fSAndroid Build Coastguard Worker int i;
404*59bfda1fSAndroid Build Coastguard Worker u32 *dump_blk = (u32 *)node_block;
405*59bfda1fSAndroid Build Coastguard Worker DBG(verbose,
406*59bfda1fSAndroid Build Coastguard Worker "Node ID [0x%x:%u] is direct node or indirect node.\n",
407*59bfda1fSAndroid Build Coastguard Worker nid, nid);
408*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < DEF_ADDRS_PER_BLOCK; i++)
409*59bfda1fSAndroid Build Coastguard Worker MSG(verbose, "[%d]\t\t\t[0x%8x : %d]\n",
410*59bfda1fSAndroid Build Coastguard Worker i, dump_blk[i], dump_blk[i]);
411*59bfda1fSAndroid Build Coastguard Worker }
412*59bfda1fSAndroid Build Coastguard Worker }
413*59bfda1fSAndroid Build Coastguard Worker
print_extention_list(struct f2fs_super_block * sb,int cold)414*59bfda1fSAndroid Build Coastguard Worker void print_extention_list(struct f2fs_super_block *sb, int cold)
415*59bfda1fSAndroid Build Coastguard Worker {
416*59bfda1fSAndroid Build Coastguard Worker int start, end, i;
417*59bfda1fSAndroid Build Coastguard Worker
418*59bfda1fSAndroid Build Coastguard Worker if (cold) {
419*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, extension_count);
420*59bfda1fSAndroid Build Coastguard Worker
421*59bfda1fSAndroid Build Coastguard Worker start = 0;
422*59bfda1fSAndroid Build Coastguard Worker end = le32_to_cpu(sb->extension_count);
423*59bfda1fSAndroid Build Coastguard Worker } else {
424*59bfda1fSAndroid Build Coastguard Worker DISP_u8(sb, hot_ext_count);
425*59bfda1fSAndroid Build Coastguard Worker
426*59bfda1fSAndroid Build Coastguard Worker start = le32_to_cpu(sb->extension_count);
427*59bfda1fSAndroid Build Coastguard Worker end = start + sb->hot_ext_count;
428*59bfda1fSAndroid Build Coastguard Worker }
429*59bfda1fSAndroid Build Coastguard Worker
430*59bfda1fSAndroid Build Coastguard Worker printf("%s file extentsions\n", cold ? "cold" : "hot");
431*59bfda1fSAndroid Build Coastguard Worker
432*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < end - start; i++) {
433*59bfda1fSAndroid Build Coastguard Worker if (c.layout) {
434*59bfda1fSAndroid Build Coastguard Worker printf("%-30s %-8.8s\n", "extension_list",
435*59bfda1fSAndroid Build Coastguard Worker sb->extension_list[start + i]);
436*59bfda1fSAndroid Build Coastguard Worker } else {
437*59bfda1fSAndroid Build Coastguard Worker if (i % 4 == 0)
438*59bfda1fSAndroid Build Coastguard Worker printf("%-30s\t\t[", "");
439*59bfda1fSAndroid Build Coastguard Worker
440*59bfda1fSAndroid Build Coastguard Worker printf("%-8.8s", sb->extension_list[start + i]);
441*59bfda1fSAndroid Build Coastguard Worker
442*59bfda1fSAndroid Build Coastguard Worker if (i % 4 == 4 - 1)
443*59bfda1fSAndroid Build Coastguard Worker printf("]\n");
444*59bfda1fSAndroid Build Coastguard Worker }
445*59bfda1fSAndroid Build Coastguard Worker }
446*59bfda1fSAndroid Build Coastguard Worker
447*59bfda1fSAndroid Build Coastguard Worker for (; i < round_up(end - start, 4) * 4; i++) {
448*59bfda1fSAndroid Build Coastguard Worker printf("%-8.8s", "");
449*59bfda1fSAndroid Build Coastguard Worker if (i % 4 == 4 - 1)
450*59bfda1fSAndroid Build Coastguard Worker printf("]\n");
451*59bfda1fSAndroid Build Coastguard Worker }
452*59bfda1fSAndroid Build Coastguard Worker }
453*59bfda1fSAndroid Build Coastguard Worker
DISP_label(const char * name)454*59bfda1fSAndroid Build Coastguard Worker static void DISP_label(const char *name)
455*59bfda1fSAndroid Build Coastguard Worker {
456*59bfda1fSAndroid Build Coastguard Worker char buffer[MAX_VOLUME_NAME];
457*59bfda1fSAndroid Build Coastguard Worker
458*59bfda1fSAndroid Build Coastguard Worker utf16_to_utf8(buffer, name, MAX_VOLUME_NAME, MAX_VOLUME_NAME);
459*59bfda1fSAndroid Build Coastguard Worker if (c.layout)
460*59bfda1fSAndroid Build Coastguard Worker printf("%-30s %s\n", "Filesystem volume name:", buffer);
461*59bfda1fSAndroid Build Coastguard Worker else
462*59bfda1fSAndroid Build Coastguard Worker printf("%-30s" "\t\t[%s]\n", "volum_name", buffer);
463*59bfda1fSAndroid Build Coastguard Worker }
464*59bfda1fSAndroid Build Coastguard Worker
465*59bfda1fSAndroid Build Coastguard Worker void print_sb_debug_info(struct f2fs_super_block *sb);
print_raw_sb_info(struct f2fs_super_block * sb)466*59bfda1fSAndroid Build Coastguard Worker void print_raw_sb_info(struct f2fs_super_block *sb)
467*59bfda1fSAndroid Build Coastguard Worker {
468*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_LIBUUID
469*59bfda1fSAndroid Build Coastguard Worker char uuid[40];
470*59bfda1fSAndroid Build Coastguard Worker char encrypt_pw_salt[40];
471*59bfda1fSAndroid Build Coastguard Worker #endif
472*59bfda1fSAndroid Build Coastguard Worker int i;
473*59bfda1fSAndroid Build Coastguard Worker
474*59bfda1fSAndroid Build Coastguard Worker if (c.layout)
475*59bfda1fSAndroid Build Coastguard Worker goto printout;
476*59bfda1fSAndroid Build Coastguard Worker if (!c.dbg_lv)
477*59bfda1fSAndroid Build Coastguard Worker return;
478*59bfda1fSAndroid Build Coastguard Worker
479*59bfda1fSAndroid Build Coastguard Worker printf("\n");
480*59bfda1fSAndroid Build Coastguard Worker printf("+--------------------------------------------------------+\n");
481*59bfda1fSAndroid Build Coastguard Worker printf("| Super block |\n");
482*59bfda1fSAndroid Build Coastguard Worker printf("+--------------------------------------------------------+\n");
483*59bfda1fSAndroid Build Coastguard Worker printout:
484*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, magic);
485*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, major_ver);
486*59bfda1fSAndroid Build Coastguard Worker
487*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, minor_ver);
488*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, log_sectorsize);
489*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, log_sectors_per_block);
490*59bfda1fSAndroid Build Coastguard Worker
491*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, log_blocksize);
492*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, log_blocks_per_seg);
493*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segs_per_sec);
494*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, secs_per_zone);
495*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, checksum_offset);
496*59bfda1fSAndroid Build Coastguard Worker DISP_u64(sb, block_count);
497*59bfda1fSAndroid Build Coastguard Worker
498*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, section_count);
499*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment_count);
500*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment_count_ckpt);
501*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment_count_sit);
502*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment_count_nat);
503*59bfda1fSAndroid Build Coastguard Worker
504*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment_count_ssa);
505*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment_count_main);
506*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, segment0_blkaddr);
507*59bfda1fSAndroid Build Coastguard Worker
508*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, cp_blkaddr);
509*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, sit_blkaddr);
510*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, nat_blkaddr);
511*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, ssa_blkaddr);
512*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, main_blkaddr);
513*59bfda1fSAndroid Build Coastguard Worker
514*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, root_ino);
515*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, node_ino);
516*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, meta_ino);
517*59bfda1fSAndroid Build Coastguard Worker
518*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_LIBUUID
519*59bfda1fSAndroid Build Coastguard Worker uuid_unparse(sb->uuid, uuid);
520*59bfda1fSAndroid Build Coastguard Worker DISP_raw_str("%-.36s", uuid);
521*59bfda1fSAndroid Build Coastguard Worker #endif
522*59bfda1fSAndroid Build Coastguard Worker
523*59bfda1fSAndroid Build Coastguard Worker DISP_label((const char *)sb->volume_name);
524*59bfda1fSAndroid Build Coastguard Worker
525*59bfda1fSAndroid Build Coastguard Worker print_extention_list(sb, 1);
526*59bfda1fSAndroid Build Coastguard Worker print_extention_list(sb, 0);
527*59bfda1fSAndroid Build Coastguard Worker
528*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, cp_payload);
529*59bfda1fSAndroid Build Coastguard Worker
530*59bfda1fSAndroid Build Coastguard Worker DISP_str("%-.252s", sb, version);
531*59bfda1fSAndroid Build Coastguard Worker DISP_str("%-.252s", sb, init_version);
532*59bfda1fSAndroid Build Coastguard Worker
533*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, feature);
534*59bfda1fSAndroid Build Coastguard Worker DISP_u8(sb, encryption_level);
535*59bfda1fSAndroid Build Coastguard Worker
536*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_LIBUUID
537*59bfda1fSAndroid Build Coastguard Worker uuid_unparse(sb->encrypt_pw_salt, encrypt_pw_salt);
538*59bfda1fSAndroid Build Coastguard Worker DISP_raw_str("%-.36s", encrypt_pw_salt);
539*59bfda1fSAndroid Build Coastguard Worker #endif
540*59bfda1fSAndroid Build Coastguard Worker
541*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < MAX_DEVICES; i++) {
542*59bfda1fSAndroid Build Coastguard Worker if (!sb->devs[i].path[0])
543*59bfda1fSAndroid Build Coastguard Worker break;
544*59bfda1fSAndroid Build Coastguard Worker DISP_str("%s", sb, devs[i].path);
545*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, devs[i].total_segments);
546*59bfda1fSAndroid Build Coastguard Worker }
547*59bfda1fSAndroid Build Coastguard Worker
548*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, qf_ino[USRQUOTA]);
549*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, qf_ino[GRPQUOTA]);
550*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, qf_ino[PRJQUOTA]);
551*59bfda1fSAndroid Build Coastguard Worker
552*59bfda1fSAndroid Build Coastguard Worker DISP_u16(sb, s_encoding);
553*59bfda1fSAndroid Build Coastguard Worker DISP_u32(sb, crc);
554*59bfda1fSAndroid Build Coastguard Worker
555*59bfda1fSAndroid Build Coastguard Worker print_sb_debug_info(sb);
556*59bfda1fSAndroid Build Coastguard Worker
557*59bfda1fSAndroid Build Coastguard Worker printf("\n");
558*59bfda1fSAndroid Build Coastguard Worker }
559*59bfda1fSAndroid Build Coastguard Worker
print_ckpt_info(struct f2fs_sb_info * sbi)560*59bfda1fSAndroid Build Coastguard Worker void print_ckpt_info(struct f2fs_sb_info *sbi)
561*59bfda1fSAndroid Build Coastguard Worker {
562*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
563*59bfda1fSAndroid Build Coastguard Worker
564*59bfda1fSAndroid Build Coastguard Worker if (c.layout)
565*59bfda1fSAndroid Build Coastguard Worker goto printout;
566*59bfda1fSAndroid Build Coastguard Worker if (!c.dbg_lv)
567*59bfda1fSAndroid Build Coastguard Worker return;
568*59bfda1fSAndroid Build Coastguard Worker
569*59bfda1fSAndroid Build Coastguard Worker printf("\n");
570*59bfda1fSAndroid Build Coastguard Worker printf("+--------------------------------------------------------+\n");
571*59bfda1fSAndroid Build Coastguard Worker printf("| Checkpoint |\n");
572*59bfda1fSAndroid Build Coastguard Worker printf("+--------------------------------------------------------+\n");
573*59bfda1fSAndroid Build Coastguard Worker printout:
574*59bfda1fSAndroid Build Coastguard Worker DISP_u64(cp, checkpoint_ver);
575*59bfda1fSAndroid Build Coastguard Worker DISP_u64(cp, user_block_count);
576*59bfda1fSAndroid Build Coastguard Worker DISP_u64(cp, valid_block_count);
577*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, rsvd_segment_count);
578*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, overprov_segment_count);
579*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, free_segment_count);
580*59bfda1fSAndroid Build Coastguard Worker
581*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]);
582*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]);
583*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]);
584*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_node_segno[0]);
585*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_node_segno[1]);
586*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_node_segno[2]);
587*59bfda1fSAndroid Build Coastguard Worker
588*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_node_blkoff[0]);
589*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_node_blkoff[1]);
590*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_node_blkoff[2]);
591*59bfda1fSAndroid Build Coastguard Worker
592*59bfda1fSAndroid Build Coastguard Worker
593*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]);
594*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]);
595*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]);
596*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_data_segno[0]);
597*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_data_segno[1]);
598*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_data_segno[2]);
599*59bfda1fSAndroid Build Coastguard Worker
600*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_data_blkoff[0]);
601*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_data_blkoff[1]);
602*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cur_data_blkoff[2]);
603*59bfda1fSAndroid Build Coastguard Worker
604*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, ckpt_flags);
605*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cp_pack_total_block_count);
606*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, cp_pack_start_sum);
607*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, valid_node_count);
608*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, valid_inode_count);
609*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, next_free_nid);
610*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, sit_ver_bitmap_bytesize);
611*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, nat_ver_bitmap_bytesize);
612*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, checksum_offset);
613*59bfda1fSAndroid Build Coastguard Worker DISP_u64(cp, elapsed_time);
614*59bfda1fSAndroid Build Coastguard Worker
615*59bfda1fSAndroid Build Coastguard Worker DISP_u32(cp, sit_nat_version_bitmap[0]);
616*59bfda1fSAndroid Build Coastguard Worker printf("\n\n");
617*59bfda1fSAndroid Build Coastguard Worker }
618*59bfda1fSAndroid Build Coastguard Worker
print_cp_state(u32 flag)619*59bfda1fSAndroid Build Coastguard Worker void print_cp_state(u32 flag)
620*59bfda1fSAndroid Build Coastguard Worker {
621*59bfda1fSAndroid Build Coastguard Worker if (c.show_file_map)
622*59bfda1fSAndroid Build Coastguard Worker return;
623*59bfda1fSAndroid Build Coastguard Worker
624*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: checkpoint state = %x : ", flag);
625*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_QUOTA_NEED_FSCK_FLAG)
626*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " quota_need_fsck");
627*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_LARGE_NAT_BITMAP_FLAG)
628*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " large_nat_bitmap");
629*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_NOCRC_RECOVERY_FLAG)
630*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " allow_nocrc");
631*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_TRIMMED_FLAG)
632*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " trimmed");
633*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_NAT_BITS_FLAG)
634*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " nat_bits");
635*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_CRC_RECOVERY_FLAG)
636*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " crc");
637*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_FASTBOOT_FLAG)
638*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " fastboot");
639*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_FSCK_FLAG)
640*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " fsck");
641*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_ERROR_FLAG)
642*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " error");
643*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_COMPACT_SUM_FLAG)
644*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " compacted_summary");
645*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_ORPHAN_PRESENT_FLAG)
646*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " orphan_inodes");
647*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_DISABLED_FLAG)
648*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " disabled");
649*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_RESIZEFS_FLAG)
650*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " resizefs");
651*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_UMOUNT_FLAG)
652*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " unmount");
653*59bfda1fSAndroid Build Coastguard Worker else
654*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s", " sudden-power-off");
655*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
656*59bfda1fSAndroid Build Coastguard Worker }
657*59bfda1fSAndroid Build Coastguard Worker
658*59bfda1fSAndroid Build Coastguard Worker extern struct feature feature_table[];
print_sb_state(struct f2fs_super_block * sb)659*59bfda1fSAndroid Build Coastguard Worker void print_sb_state(struct f2fs_super_block *sb)
660*59bfda1fSAndroid Build Coastguard Worker {
661*59bfda1fSAndroid Build Coastguard Worker unsigned int f = get_sb(feature);
662*59bfda1fSAndroid Build Coastguard Worker char *name;
663*59bfda1fSAndroid Build Coastguard Worker int i;
664*59bfda1fSAndroid Build Coastguard Worker
665*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: superblock features = %x : ", f);
666*59bfda1fSAndroid Build Coastguard Worker
667*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < MAX_NR_FEATURE; i++) {
668*59bfda1fSAndroid Build Coastguard Worker unsigned int bit = 1 << i;
669*59bfda1fSAndroid Build Coastguard Worker
670*59bfda1fSAndroid Build Coastguard Worker if (!(f & bit))
671*59bfda1fSAndroid Build Coastguard Worker continue;
672*59bfda1fSAndroid Build Coastguard Worker
673*59bfda1fSAndroid Build Coastguard Worker name = feature_name(feature_table, bit);
674*59bfda1fSAndroid Build Coastguard Worker if (!name)
675*59bfda1fSAndroid Build Coastguard Worker continue;
676*59bfda1fSAndroid Build Coastguard Worker
677*59bfda1fSAndroid Build Coastguard Worker MSG(0, " %s", name);
678*59bfda1fSAndroid Build Coastguard Worker }
679*59bfda1fSAndroid Build Coastguard Worker
680*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
681*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: superblock encrypt level = %d, salt = ",
682*59bfda1fSAndroid Build Coastguard Worker sb->encryption_level);
683*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < 16; i++)
684*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%02x", sb->encrypt_pw_salt[i]);
685*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
686*59bfda1fSAndroid Build Coastguard Worker }
687*59bfda1fSAndroid Build Coastguard Worker
688*59bfda1fSAndroid Build Coastguard Worker static char *stop_reason_str[] = {
689*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_SHUTDOWN] = "shutdown",
690*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_FAULT_INJECT] = "fault_inject",
691*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_META_PAGE] = "meta_page",
692*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_WRITE_FAIL] = "write_fail",
693*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_CORRUPTED_SUMMARY] = "corrupted_summary",
694*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_UPDATE_INODE] = "update_inode",
695*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_FLUSH_FAIL] = "flush_fail",
696*59bfda1fSAndroid Build Coastguard Worker [STOP_CP_REASON_NO_SEGMENT] = "no_segment",
697*59bfda1fSAndroid Build Coastguard Worker };
698*59bfda1fSAndroid Build Coastguard Worker
print_sb_stop_reason(struct f2fs_super_block * sb)699*59bfda1fSAndroid Build Coastguard Worker void print_sb_stop_reason(struct f2fs_super_block *sb)
700*59bfda1fSAndroid Build Coastguard Worker {
701*59bfda1fSAndroid Build Coastguard Worker u8 *reason = sb->s_stop_reason;
702*59bfda1fSAndroid Build Coastguard Worker int i;
703*59bfda1fSAndroid Build Coastguard Worker
704*59bfda1fSAndroid Build Coastguard Worker if (!(c.invalid_sb & SB_FORCE_STOP))
705*59bfda1fSAndroid Build Coastguard Worker return;
706*59bfda1fSAndroid Build Coastguard Worker
707*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: checkpoint stop reason: ");
708*59bfda1fSAndroid Build Coastguard Worker
709*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < STOP_CP_REASON_MAX; i++) {
710*59bfda1fSAndroid Build Coastguard Worker if (reason[i])
711*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s(%d) ", stop_reason_str[i], reason[i]);
712*59bfda1fSAndroid Build Coastguard Worker }
713*59bfda1fSAndroid Build Coastguard Worker
714*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
715*59bfda1fSAndroid Build Coastguard Worker }
716*59bfda1fSAndroid Build Coastguard Worker
717*59bfda1fSAndroid Build Coastguard Worker static char *errors_str[] = {
718*59bfda1fSAndroid Build Coastguard Worker [ERROR_CORRUPTED_CLUSTER] = "corrupted_cluster",
719*59bfda1fSAndroid Build Coastguard Worker [ERROR_FAIL_DECOMPRESSION] = "fail_decompression",
720*59bfda1fSAndroid Build Coastguard Worker [ERROR_INVALID_BLKADDR] = "invalid_blkaddr",
721*59bfda1fSAndroid Build Coastguard Worker [ERROR_CORRUPTED_DIRENT] = "corrupted_dirent",
722*59bfda1fSAndroid Build Coastguard Worker [ERROR_CORRUPTED_INODE] = "corrupted_inode",
723*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_SUMMARY] = "inconsistent_summary",
724*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_FOOTER] = "inconsistent_footer",
725*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_SUM_TYPE] = "inconsistent_sum_type",
726*59bfda1fSAndroid Build Coastguard Worker [ERROR_CORRUPTED_JOURNAL] = "corrupted_journal",
727*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_NODE_COUNT] = "inconsistent_node_count",
728*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_BLOCK_COUNT] = "inconsistent_block_count",
729*59bfda1fSAndroid Build Coastguard Worker [ERROR_INVALID_CURSEG] = "invalid_curseg",
730*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_SIT] = "inconsistent_sit",
731*59bfda1fSAndroid Build Coastguard Worker [ERROR_CORRUPTED_VERITY_XATTR] = "corrupted_verity_xattr",
732*59bfda1fSAndroid Build Coastguard Worker [ERROR_CORRUPTED_XATTR] = "corrupted_xattr",
733*59bfda1fSAndroid Build Coastguard Worker [ERROR_INVALID_NODE_REFERENCE] = "invalid_node_reference",
734*59bfda1fSAndroid Build Coastguard Worker [ERROR_INCONSISTENT_NAT] = "inconsistent_nat",
735*59bfda1fSAndroid Build Coastguard Worker };
736*59bfda1fSAndroid Build Coastguard Worker
print_sb_errors(struct f2fs_super_block * sb)737*59bfda1fSAndroid Build Coastguard Worker void print_sb_errors(struct f2fs_super_block *sb)
738*59bfda1fSAndroid Build Coastguard Worker {
739*59bfda1fSAndroid Build Coastguard Worker u8 *errors = sb->s_errors;
740*59bfda1fSAndroid Build Coastguard Worker int i;
741*59bfda1fSAndroid Build Coastguard Worker
742*59bfda1fSAndroid Build Coastguard Worker if (!(c.invalid_sb & SB_FS_ERRORS))
743*59bfda1fSAndroid Build Coastguard Worker return;
744*59bfda1fSAndroid Build Coastguard Worker
745*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: fs errors: ");
746*59bfda1fSAndroid Build Coastguard Worker
747*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < ERROR_MAX; i++) {
748*59bfda1fSAndroid Build Coastguard Worker if (test_bit_le(i, errors))
749*59bfda1fSAndroid Build Coastguard Worker MSG(0, "%s ", errors_str[i]);
750*59bfda1fSAndroid Build Coastguard Worker }
751*59bfda1fSAndroid Build Coastguard Worker
752*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\n");
753*59bfda1fSAndroid Build Coastguard Worker }
754*59bfda1fSAndroid Build Coastguard Worker
print_sb_debug_info(struct f2fs_super_block * sb)755*59bfda1fSAndroid Build Coastguard Worker void print_sb_debug_info(struct f2fs_super_block *sb)
756*59bfda1fSAndroid Build Coastguard Worker {
757*59bfda1fSAndroid Build Coastguard Worker u8 *reason = sb->s_stop_reason;
758*59bfda1fSAndroid Build Coastguard Worker u8 *errors = sb->s_errors;
759*59bfda1fSAndroid Build Coastguard Worker int i;
760*59bfda1fSAndroid Build Coastguard Worker
761*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < STOP_CP_REASON_MAX; i++) {
762*59bfda1fSAndroid Build Coastguard Worker if (!reason[i])
763*59bfda1fSAndroid Build Coastguard Worker continue;
764*59bfda1fSAndroid Build Coastguard Worker if (c.layout)
765*59bfda1fSAndroid Build Coastguard Worker printf("%-30s %s(%s, %d)\n", "", "stop_reason",
766*59bfda1fSAndroid Build Coastguard Worker stop_reason_str[i], reason[i]);
767*59bfda1fSAndroid Build Coastguard Worker else
768*59bfda1fSAndroid Build Coastguard Worker printf("%-30s\t\t[%-20s : %u]\n", "",
769*59bfda1fSAndroid Build Coastguard Worker stop_reason_str[i], reason[i]);
770*59bfda1fSAndroid Build Coastguard Worker }
771*59bfda1fSAndroid Build Coastguard Worker
772*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < ERROR_MAX; i++) {
773*59bfda1fSAndroid Build Coastguard Worker if (!test_bit_le(i, errors))
774*59bfda1fSAndroid Build Coastguard Worker continue;
775*59bfda1fSAndroid Build Coastguard Worker if (c.layout)
776*59bfda1fSAndroid Build Coastguard Worker printf("%-30s %s(%s)\n", "", "errors", errors_str[i]);
777*59bfda1fSAndroid Build Coastguard Worker else
778*59bfda1fSAndroid Build Coastguard Worker printf("%-30s\t\t[%-20s]\n", "", errors_str[i]);
779*59bfda1fSAndroid Build Coastguard Worker }
780*59bfda1fSAndroid Build Coastguard Worker }
781*59bfda1fSAndroid Build Coastguard Worker
f2fs_is_valid_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)782*59bfda1fSAndroid Build Coastguard Worker bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
783*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr, int type)
784*59bfda1fSAndroid Build Coastguard Worker {
785*59bfda1fSAndroid Build Coastguard Worker switch (type) {
786*59bfda1fSAndroid Build Coastguard Worker case META_NAT:
787*59bfda1fSAndroid Build Coastguard Worker break;
788*59bfda1fSAndroid Build Coastguard Worker case META_SIT:
789*59bfda1fSAndroid Build Coastguard Worker if (blkaddr >= SIT_BLK_CNT(sbi))
790*59bfda1fSAndroid Build Coastguard Worker return 0;
791*59bfda1fSAndroid Build Coastguard Worker break;
792*59bfda1fSAndroid Build Coastguard Worker case META_SSA:
793*59bfda1fSAndroid Build Coastguard Worker if (blkaddr >= MAIN_BLKADDR(sbi) ||
794*59bfda1fSAndroid Build Coastguard Worker blkaddr < SM_I(sbi)->ssa_blkaddr)
795*59bfda1fSAndroid Build Coastguard Worker return 0;
796*59bfda1fSAndroid Build Coastguard Worker break;
797*59bfda1fSAndroid Build Coastguard Worker case META_CP:
798*59bfda1fSAndroid Build Coastguard Worker if (blkaddr >= SIT_I(sbi)->sit_base_addr ||
799*59bfda1fSAndroid Build Coastguard Worker blkaddr < __start_cp_addr(sbi))
800*59bfda1fSAndroid Build Coastguard Worker return 0;
801*59bfda1fSAndroid Build Coastguard Worker break;
802*59bfda1fSAndroid Build Coastguard Worker case META_POR:
803*59bfda1fSAndroid Build Coastguard Worker case DATA_GENERIC:
804*59bfda1fSAndroid Build Coastguard Worker if (blkaddr >= MAX_BLKADDR(sbi) ||
805*59bfda1fSAndroid Build Coastguard Worker blkaddr < MAIN_BLKADDR(sbi))
806*59bfda1fSAndroid Build Coastguard Worker return 0;
807*59bfda1fSAndroid Build Coastguard Worker break;
808*59bfda1fSAndroid Build Coastguard Worker default:
809*59bfda1fSAndroid Build Coastguard Worker ASSERT(0);
810*59bfda1fSAndroid Build Coastguard Worker }
811*59bfda1fSAndroid Build Coastguard Worker
812*59bfda1fSAndroid Build Coastguard Worker return 1;
813*59bfda1fSAndroid Build Coastguard Worker }
814*59bfda1fSAndroid Build Coastguard Worker
815*59bfda1fSAndroid Build Coastguard Worker static inline block_t current_sit_addr(struct f2fs_sb_info *sbi,
816*59bfda1fSAndroid Build Coastguard Worker unsigned int start);
817*59bfda1fSAndroid Build Coastguard Worker
818*59bfda1fSAndroid Build Coastguard Worker /*
819*59bfda1fSAndroid Build Coastguard Worker * Readahead CP/NAT/SIT/SSA pages
820*59bfda1fSAndroid Build Coastguard Worker */
f2fs_ra_meta_pages(struct f2fs_sb_info * sbi,block_t start,int nrpages,int type)821*59bfda1fSAndroid Build Coastguard Worker int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
822*59bfda1fSAndroid Build Coastguard Worker int type)
823*59bfda1fSAndroid Build Coastguard Worker {
824*59bfda1fSAndroid Build Coastguard Worker block_t blkno = start;
825*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr, start_blk = 0, len = 0;
826*59bfda1fSAndroid Build Coastguard Worker
827*59bfda1fSAndroid Build Coastguard Worker for (; nrpages-- > 0; blkno++) {
828*59bfda1fSAndroid Build Coastguard Worker
829*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
830*59bfda1fSAndroid Build Coastguard Worker goto out;
831*59bfda1fSAndroid Build Coastguard Worker
832*59bfda1fSAndroid Build Coastguard Worker switch (type) {
833*59bfda1fSAndroid Build Coastguard Worker case META_NAT:
834*59bfda1fSAndroid Build Coastguard Worker if (blkno >= NAT_BLOCK_OFFSET(NM_I(sbi)->max_nid))
835*59bfda1fSAndroid Build Coastguard Worker blkno = 0;
836*59bfda1fSAndroid Build Coastguard Worker /* get nat block addr */
837*59bfda1fSAndroid Build Coastguard Worker blkaddr = current_nat_addr(sbi,
838*59bfda1fSAndroid Build Coastguard Worker blkno * NAT_ENTRY_PER_BLOCK, NULL);
839*59bfda1fSAndroid Build Coastguard Worker break;
840*59bfda1fSAndroid Build Coastguard Worker case META_SIT:
841*59bfda1fSAndroid Build Coastguard Worker /* get sit block addr */
842*59bfda1fSAndroid Build Coastguard Worker blkaddr = current_sit_addr(sbi,
843*59bfda1fSAndroid Build Coastguard Worker blkno * SIT_ENTRY_PER_BLOCK);
844*59bfda1fSAndroid Build Coastguard Worker break;
845*59bfda1fSAndroid Build Coastguard Worker case META_SSA:
846*59bfda1fSAndroid Build Coastguard Worker case META_CP:
847*59bfda1fSAndroid Build Coastguard Worker case META_POR:
848*59bfda1fSAndroid Build Coastguard Worker blkaddr = blkno;
849*59bfda1fSAndroid Build Coastguard Worker break;
850*59bfda1fSAndroid Build Coastguard Worker default:
851*59bfda1fSAndroid Build Coastguard Worker ASSERT(0);
852*59bfda1fSAndroid Build Coastguard Worker }
853*59bfda1fSAndroid Build Coastguard Worker
854*59bfda1fSAndroid Build Coastguard Worker if (!len) {
855*59bfda1fSAndroid Build Coastguard Worker start_blk = blkaddr;
856*59bfda1fSAndroid Build Coastguard Worker len = 1;
857*59bfda1fSAndroid Build Coastguard Worker } else if (start_blk + len == blkaddr) {
858*59bfda1fSAndroid Build Coastguard Worker len++;
859*59bfda1fSAndroid Build Coastguard Worker } else {
860*59bfda1fSAndroid Build Coastguard Worker dev_readahead(start_blk << F2FS_BLKSIZE_BITS,
861*59bfda1fSAndroid Build Coastguard Worker len << F2FS_BLKSIZE_BITS);
862*59bfda1fSAndroid Build Coastguard Worker }
863*59bfda1fSAndroid Build Coastguard Worker }
864*59bfda1fSAndroid Build Coastguard Worker out:
865*59bfda1fSAndroid Build Coastguard Worker if (len)
866*59bfda1fSAndroid Build Coastguard Worker dev_readahead(start_blk << F2FS_BLKSIZE_BITS,
867*59bfda1fSAndroid Build Coastguard Worker len << F2FS_BLKSIZE_BITS);
868*59bfda1fSAndroid Build Coastguard Worker return blkno - start;
869*59bfda1fSAndroid Build Coastguard Worker }
870*59bfda1fSAndroid Build Coastguard Worker
update_superblock(struct f2fs_super_block * sb,int sb_mask)871*59bfda1fSAndroid Build Coastguard Worker void update_superblock(struct f2fs_super_block *sb, int sb_mask)
872*59bfda1fSAndroid Build Coastguard Worker {
873*59bfda1fSAndroid Build Coastguard Worker int addr, ret;
874*59bfda1fSAndroid Build Coastguard Worker uint8_t *buf;
875*59bfda1fSAndroid Build Coastguard Worker u32 old_crc, new_crc;
876*59bfda1fSAndroid Build Coastguard Worker
877*59bfda1fSAndroid Build Coastguard Worker buf = calloc(F2FS_BLKSIZE, 1);
878*59bfda1fSAndroid Build Coastguard Worker ASSERT(buf);
879*59bfda1fSAndroid Build Coastguard Worker
880*59bfda1fSAndroid Build Coastguard Worker if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) {
881*59bfda1fSAndroid Build Coastguard Worker old_crc = get_sb(crc);
882*59bfda1fSAndroid Build Coastguard Worker new_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, sb,
883*59bfda1fSAndroid Build Coastguard Worker SB_CHKSUM_OFFSET);
884*59bfda1fSAndroid Build Coastguard Worker set_sb(crc, new_crc);
885*59bfda1fSAndroid Build Coastguard Worker MSG(1, "Info: SB CRC is updated (0x%x -> 0x%x)\n",
886*59bfda1fSAndroid Build Coastguard Worker old_crc, new_crc);
887*59bfda1fSAndroid Build Coastguard Worker }
888*59bfda1fSAndroid Build Coastguard Worker
889*59bfda1fSAndroid Build Coastguard Worker memcpy(buf + F2FS_SUPER_OFFSET, sb, sizeof(*sb));
890*59bfda1fSAndroid Build Coastguard Worker for (addr = SB0_ADDR; addr < SB_MAX_ADDR; addr++) {
891*59bfda1fSAndroid Build Coastguard Worker if (SB_MASK(addr) & sb_mask) {
892*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(buf, addr, WRITE_LIFE_NONE);
893*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
894*59bfda1fSAndroid Build Coastguard Worker }
895*59bfda1fSAndroid Build Coastguard Worker }
896*59bfda1fSAndroid Build Coastguard Worker
897*59bfda1fSAndroid Build Coastguard Worker free(buf);
898*59bfda1fSAndroid Build Coastguard Worker DBG(0, "Info: Done to update superblock\n");
899*59bfda1fSAndroid Build Coastguard Worker }
900*59bfda1fSAndroid Build Coastguard Worker
sanity_check_area_boundary(struct f2fs_super_block * sb,enum SB_ADDR sb_addr)901*59bfda1fSAndroid Build Coastguard Worker static inline int sanity_check_area_boundary(struct f2fs_super_block *sb,
902*59bfda1fSAndroid Build Coastguard Worker enum SB_ADDR sb_addr)
903*59bfda1fSAndroid Build Coastguard Worker {
904*59bfda1fSAndroid Build Coastguard Worker u32 segment0_blkaddr = get_sb(segment0_blkaddr);
905*59bfda1fSAndroid Build Coastguard Worker u32 cp_blkaddr = get_sb(cp_blkaddr);
906*59bfda1fSAndroid Build Coastguard Worker u32 sit_blkaddr = get_sb(sit_blkaddr);
907*59bfda1fSAndroid Build Coastguard Worker u32 nat_blkaddr = get_sb(nat_blkaddr);
908*59bfda1fSAndroid Build Coastguard Worker u32 ssa_blkaddr = get_sb(ssa_blkaddr);
909*59bfda1fSAndroid Build Coastguard Worker u32 main_blkaddr = get_sb(main_blkaddr);
910*59bfda1fSAndroid Build Coastguard Worker u32 segment_count_ckpt = get_sb(segment_count_ckpt);
911*59bfda1fSAndroid Build Coastguard Worker u32 segment_count_sit = get_sb(segment_count_sit);
912*59bfda1fSAndroid Build Coastguard Worker u32 segment_count_nat = get_sb(segment_count_nat);
913*59bfda1fSAndroid Build Coastguard Worker u32 segment_count_ssa = get_sb(segment_count_ssa);
914*59bfda1fSAndroid Build Coastguard Worker u32 segment_count_main = get_sb(segment_count_main);
915*59bfda1fSAndroid Build Coastguard Worker u32 segment_count = get_sb(segment_count);
916*59bfda1fSAndroid Build Coastguard Worker u32 log_blocks_per_seg = get_sb(log_blocks_per_seg);
917*59bfda1fSAndroid Build Coastguard Worker u64 main_end_blkaddr = main_blkaddr +
918*59bfda1fSAndroid Build Coastguard Worker (segment_count_main << log_blocks_per_seg);
919*59bfda1fSAndroid Build Coastguard Worker u64 seg_end_blkaddr = segment0_blkaddr +
920*59bfda1fSAndroid Build Coastguard Worker (segment_count << log_blocks_per_seg);
921*59bfda1fSAndroid Build Coastguard Worker
922*59bfda1fSAndroid Build Coastguard Worker if (segment0_blkaddr != cp_blkaddr) {
923*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tMismatch segment0(%u) cp_blkaddr(%u)\n",
924*59bfda1fSAndroid Build Coastguard Worker segment0_blkaddr, cp_blkaddr);
925*59bfda1fSAndroid Build Coastguard Worker return -1;
926*59bfda1fSAndroid Build Coastguard Worker }
927*59bfda1fSAndroid Build Coastguard Worker
928*59bfda1fSAndroid Build Coastguard Worker if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
929*59bfda1fSAndroid Build Coastguard Worker sit_blkaddr) {
930*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong CP boundary, start(%u) end(%u) blocks(%u)\n",
931*59bfda1fSAndroid Build Coastguard Worker cp_blkaddr, sit_blkaddr,
932*59bfda1fSAndroid Build Coastguard Worker segment_count_ckpt << log_blocks_per_seg);
933*59bfda1fSAndroid Build Coastguard Worker return -1;
934*59bfda1fSAndroid Build Coastguard Worker }
935*59bfda1fSAndroid Build Coastguard Worker
936*59bfda1fSAndroid Build Coastguard Worker if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
937*59bfda1fSAndroid Build Coastguard Worker nat_blkaddr) {
938*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong SIT boundary, start(%u) end(%u) blocks(%u)\n",
939*59bfda1fSAndroid Build Coastguard Worker sit_blkaddr, nat_blkaddr,
940*59bfda1fSAndroid Build Coastguard Worker segment_count_sit << log_blocks_per_seg);
941*59bfda1fSAndroid Build Coastguard Worker return -1;
942*59bfda1fSAndroid Build Coastguard Worker }
943*59bfda1fSAndroid Build Coastguard Worker
944*59bfda1fSAndroid Build Coastguard Worker if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
945*59bfda1fSAndroid Build Coastguard Worker ssa_blkaddr) {
946*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong NAT boundary, start(%u) end(%u) blocks(%u)\n",
947*59bfda1fSAndroid Build Coastguard Worker nat_blkaddr, ssa_blkaddr,
948*59bfda1fSAndroid Build Coastguard Worker segment_count_nat << log_blocks_per_seg);
949*59bfda1fSAndroid Build Coastguard Worker return -1;
950*59bfda1fSAndroid Build Coastguard Worker }
951*59bfda1fSAndroid Build Coastguard Worker
952*59bfda1fSAndroid Build Coastguard Worker if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
953*59bfda1fSAndroid Build Coastguard Worker main_blkaddr) {
954*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong SSA boundary, start(%u) end(%u) blocks(%u)\n",
955*59bfda1fSAndroid Build Coastguard Worker ssa_blkaddr, main_blkaddr,
956*59bfda1fSAndroid Build Coastguard Worker segment_count_ssa << log_blocks_per_seg);
957*59bfda1fSAndroid Build Coastguard Worker return -1;
958*59bfda1fSAndroid Build Coastguard Worker }
959*59bfda1fSAndroid Build Coastguard Worker
960*59bfda1fSAndroid Build Coastguard Worker if (main_end_blkaddr > seg_end_blkaddr) {
961*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong MAIN_AREA, start(%u) end(%u) block(%u)\n",
962*59bfda1fSAndroid Build Coastguard Worker main_blkaddr,
963*59bfda1fSAndroid Build Coastguard Worker segment0_blkaddr +
964*59bfda1fSAndroid Build Coastguard Worker (segment_count << log_blocks_per_seg),
965*59bfda1fSAndroid Build Coastguard Worker segment_count_main << log_blocks_per_seg);
966*59bfda1fSAndroid Build Coastguard Worker return -1;
967*59bfda1fSAndroid Build Coastguard Worker } else if (main_end_blkaddr < seg_end_blkaddr) {
968*59bfda1fSAndroid Build Coastguard Worker set_sb(segment_count, (main_end_blkaddr -
969*59bfda1fSAndroid Build Coastguard Worker segment0_blkaddr) >> log_blocks_per_seg);
970*59bfda1fSAndroid Build Coastguard Worker
971*59bfda1fSAndroid Build Coastguard Worker update_superblock(sb, SB_MASK(sb_addr));
972*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Fix alignment: start(%u) end(%u) block(%u)\n",
973*59bfda1fSAndroid Build Coastguard Worker main_blkaddr,
974*59bfda1fSAndroid Build Coastguard Worker segment0_blkaddr +
975*59bfda1fSAndroid Build Coastguard Worker (segment_count << log_blocks_per_seg),
976*59bfda1fSAndroid Build Coastguard Worker segment_count_main << log_blocks_per_seg);
977*59bfda1fSAndroid Build Coastguard Worker }
978*59bfda1fSAndroid Build Coastguard Worker return 0;
979*59bfda1fSAndroid Build Coastguard Worker }
980*59bfda1fSAndroid Build Coastguard Worker
verify_sb_chksum(struct f2fs_super_block * sb)981*59bfda1fSAndroid Build Coastguard Worker static int verify_sb_chksum(struct f2fs_super_block *sb)
982*59bfda1fSAndroid Build Coastguard Worker {
983*59bfda1fSAndroid Build Coastguard Worker if (SB_CHKSUM_OFFSET != get_sb(checksum_offset)) {
984*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tInvalid SB CRC offset: %u\n",
985*59bfda1fSAndroid Build Coastguard Worker get_sb(checksum_offset));
986*59bfda1fSAndroid Build Coastguard Worker return -1;
987*59bfda1fSAndroid Build Coastguard Worker }
988*59bfda1fSAndroid Build Coastguard Worker if (f2fs_crc_valid(get_sb(crc), sb,
989*59bfda1fSAndroid Build Coastguard Worker get_sb(checksum_offset))) {
990*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tInvalid SB CRC: 0x%x\n", get_sb(crc));
991*59bfda1fSAndroid Build Coastguard Worker return -1;
992*59bfda1fSAndroid Build Coastguard Worker }
993*59bfda1fSAndroid Build Coastguard Worker return 0;
994*59bfda1fSAndroid Build Coastguard Worker }
995*59bfda1fSAndroid Build Coastguard Worker
sanity_check_raw_super(struct f2fs_super_block * sb,enum SB_ADDR sb_addr)996*59bfda1fSAndroid Build Coastguard Worker int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
997*59bfda1fSAndroid Build Coastguard Worker {
998*59bfda1fSAndroid Build Coastguard Worker unsigned int blocksize;
999*59bfda1fSAndroid Build Coastguard Worker unsigned int segment_count, segs_per_sec, secs_per_zone, segs_per_zone;
1000*59bfda1fSAndroid Build Coastguard Worker unsigned int total_sections, blocks_per_seg;
1001*59bfda1fSAndroid Build Coastguard Worker
1002*59bfda1fSAndroid Build Coastguard Worker if (F2FS_SUPER_MAGIC != get_sb(magic)) {
1003*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Magic Mismatch, valid(0x%x) - read(0x%x)\n",
1004*59bfda1fSAndroid Build Coastguard Worker F2FS_SUPER_MAGIC, get_sb(magic));
1005*59bfda1fSAndroid Build Coastguard Worker return -1;
1006*59bfda1fSAndroid Build Coastguard Worker }
1007*59bfda1fSAndroid Build Coastguard Worker
1008*59bfda1fSAndroid Build Coastguard Worker if ((get_sb(feature) & F2FS_FEATURE_SB_CHKSUM) &&
1009*59bfda1fSAndroid Build Coastguard Worker verify_sb_chksum(sb))
1010*59bfda1fSAndroid Build Coastguard Worker return -1;
1011*59bfda1fSAndroid Build Coastguard Worker
1012*59bfda1fSAndroid Build Coastguard Worker blocksize = 1 << get_sb(log_blocksize);
1013*59bfda1fSAndroid Build Coastguard Worker if (c.sparse_mode && F2FS_BLKSIZE != blocksize) {
1014*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid blocksize (%u), does not equal sparse file blocksize (%u)",
1015*59bfda1fSAndroid Build Coastguard Worker F2FS_BLKSIZE, blocksize);
1016*59bfda1fSAndroid Build Coastguard Worker }
1017*59bfda1fSAndroid Build Coastguard Worker if (blocksize < F2FS_MIN_BLKSIZE || blocksize > F2FS_MAX_BLKSIZE) {
1018*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid blocksize (%u), must be between 4KB and 16KB\n",
1019*59bfda1fSAndroid Build Coastguard Worker blocksize);
1020*59bfda1fSAndroid Build Coastguard Worker return -1;
1021*59bfda1fSAndroid Build Coastguard Worker }
1022*59bfda1fSAndroid Build Coastguard Worker c.blksize_bits = get_sb(log_blocksize);
1023*59bfda1fSAndroid Build Coastguard Worker c.blksize = blocksize;
1024*59bfda1fSAndroid Build Coastguard Worker c.sectors_per_blk = F2FS_BLKSIZE / c.sector_size;
1025*59bfda1fSAndroid Build Coastguard Worker check_block_struct_sizes();
1026*59bfda1fSAndroid Build Coastguard Worker
1027*59bfda1fSAndroid Build Coastguard Worker /* check log blocks per segment */
1028*59bfda1fSAndroid Build Coastguard Worker if (get_sb(log_blocks_per_seg) != 9) {
1029*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid log blocks per segment (%u)\n",
1030*59bfda1fSAndroid Build Coastguard Worker get_sb(log_blocks_per_seg));
1031*59bfda1fSAndroid Build Coastguard Worker return -1;
1032*59bfda1fSAndroid Build Coastguard Worker }
1033*59bfda1fSAndroid Build Coastguard Worker
1034*59bfda1fSAndroid Build Coastguard Worker /* Currently, support powers of 2 from 512 to BLOCK SIZE bytes sector size */
1035*59bfda1fSAndroid Build Coastguard Worker if (get_sb(log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE ||
1036*59bfda1fSAndroid Build Coastguard Worker get_sb(log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE) {
1037*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid log sectorsize (%u)\n", get_sb(log_sectorsize));
1038*59bfda1fSAndroid Build Coastguard Worker return -1;
1039*59bfda1fSAndroid Build Coastguard Worker }
1040*59bfda1fSAndroid Build Coastguard Worker
1041*59bfda1fSAndroid Build Coastguard Worker if (get_sb(log_sectors_per_block) + get_sb(log_sectorsize) !=
1042*59bfda1fSAndroid Build Coastguard Worker F2FS_MAX_LOG_SECTOR_SIZE) {
1043*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid log sectors per block(%u) log sectorsize(%u)\n",
1044*59bfda1fSAndroid Build Coastguard Worker get_sb(log_sectors_per_block),
1045*59bfda1fSAndroid Build Coastguard Worker get_sb(log_sectorsize));
1046*59bfda1fSAndroid Build Coastguard Worker return -1;
1047*59bfda1fSAndroid Build Coastguard Worker }
1048*59bfda1fSAndroid Build Coastguard Worker
1049*59bfda1fSAndroid Build Coastguard Worker segment_count = get_sb(segment_count);
1050*59bfda1fSAndroid Build Coastguard Worker segs_per_sec = get_sb(segs_per_sec);
1051*59bfda1fSAndroid Build Coastguard Worker secs_per_zone = get_sb(secs_per_zone);
1052*59bfda1fSAndroid Build Coastguard Worker total_sections = get_sb(section_count);
1053*59bfda1fSAndroid Build Coastguard Worker segs_per_zone = segs_per_sec * secs_per_zone;
1054*59bfda1fSAndroid Build Coastguard Worker
1055*59bfda1fSAndroid Build Coastguard Worker /* blocks_per_seg should be 512, given the above check */
1056*59bfda1fSAndroid Build Coastguard Worker blocks_per_seg = 1 << get_sb(log_blocks_per_seg);
1057*59bfda1fSAndroid Build Coastguard Worker
1058*59bfda1fSAndroid Build Coastguard Worker if (segment_count > F2FS_MAX_SEGMENT ||
1059*59bfda1fSAndroid Build Coastguard Worker segment_count < F2FS_MIN_SEGMENTS) {
1060*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tInvalid segment count (%u)\n", segment_count);
1061*59bfda1fSAndroid Build Coastguard Worker return -1;
1062*59bfda1fSAndroid Build Coastguard Worker }
1063*59bfda1fSAndroid Build Coastguard Worker
1064*59bfda1fSAndroid Build Coastguard Worker if (!(get_sb(feature) & F2FS_FEATURE_RO) &&
1065*59bfda1fSAndroid Build Coastguard Worker (total_sections > segment_count ||
1066*59bfda1fSAndroid Build Coastguard Worker total_sections < F2FS_MIN_SEGMENTS ||
1067*59bfda1fSAndroid Build Coastguard Worker segs_per_sec > segment_count || !segs_per_sec)) {
1068*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tInvalid segment/section count (%u, %u x %u)\n",
1069*59bfda1fSAndroid Build Coastguard Worker segment_count, total_sections, segs_per_sec);
1070*59bfda1fSAndroid Build Coastguard Worker return 1;
1071*59bfda1fSAndroid Build Coastguard Worker }
1072*59bfda1fSAndroid Build Coastguard Worker
1073*59bfda1fSAndroid Build Coastguard Worker if ((segment_count / segs_per_sec) < total_sections) {
1074*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Small segment_count (%u < %u * %u)\n",
1075*59bfda1fSAndroid Build Coastguard Worker segment_count, segs_per_sec, total_sections);
1076*59bfda1fSAndroid Build Coastguard Worker return 1;
1077*59bfda1fSAndroid Build Coastguard Worker }
1078*59bfda1fSAndroid Build Coastguard Worker
1079*59bfda1fSAndroid Build Coastguard Worker if (segment_count > (get_sb(block_count) >> 9)) {
1080*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Wrong segment_count / block_count (%u > %llu)\n",
1081*59bfda1fSAndroid Build Coastguard Worker segment_count, get_sb(block_count));
1082*59bfda1fSAndroid Build Coastguard Worker return 1;
1083*59bfda1fSAndroid Build Coastguard Worker }
1084*59bfda1fSAndroid Build Coastguard Worker
1085*59bfda1fSAndroid Build Coastguard Worker if (sb->devs[0].path[0]) {
1086*59bfda1fSAndroid Build Coastguard Worker unsigned int dev_segs = le32_to_cpu(sb->devs[0].total_segments);
1087*59bfda1fSAndroid Build Coastguard Worker int i = 1;
1088*59bfda1fSAndroid Build Coastguard Worker
1089*59bfda1fSAndroid Build Coastguard Worker while (i < MAX_DEVICES && sb->devs[i].path[0]) {
1090*59bfda1fSAndroid Build Coastguard Worker dev_segs += le32_to_cpu(sb->devs[i].total_segments);
1091*59bfda1fSAndroid Build Coastguard Worker i++;
1092*59bfda1fSAndroid Build Coastguard Worker }
1093*59bfda1fSAndroid Build Coastguard Worker if (segment_count != dev_segs / segs_per_zone * segs_per_zone) {
1094*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Segment count (%u) mismatch with total segments from devices (%u)",
1095*59bfda1fSAndroid Build Coastguard Worker segment_count, dev_segs);
1096*59bfda1fSAndroid Build Coastguard Worker return 1;
1097*59bfda1fSAndroid Build Coastguard Worker }
1098*59bfda1fSAndroid Build Coastguard Worker }
1099*59bfda1fSAndroid Build Coastguard Worker
1100*59bfda1fSAndroid Build Coastguard Worker if (secs_per_zone > total_sections || !secs_per_zone) {
1101*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Wrong secs_per_zone / total_sections (%u, %u)\n",
1102*59bfda1fSAndroid Build Coastguard Worker secs_per_zone, total_sections);
1103*59bfda1fSAndroid Build Coastguard Worker return 1;
1104*59bfda1fSAndroid Build Coastguard Worker }
1105*59bfda1fSAndroid Build Coastguard Worker if (get_sb(extension_count) > F2FS_MAX_EXTENSION ||
1106*59bfda1fSAndroid Build Coastguard Worker sb->hot_ext_count > F2FS_MAX_EXTENSION ||
1107*59bfda1fSAndroid Build Coastguard Worker get_sb(extension_count) +
1108*59bfda1fSAndroid Build Coastguard Worker sb->hot_ext_count > F2FS_MAX_EXTENSION) {
1109*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Corrupted extension count (%u + %u > %u)\n",
1110*59bfda1fSAndroid Build Coastguard Worker get_sb(extension_count),
1111*59bfda1fSAndroid Build Coastguard Worker sb->hot_ext_count,
1112*59bfda1fSAndroid Build Coastguard Worker F2FS_MAX_EXTENSION);
1113*59bfda1fSAndroid Build Coastguard Worker return 1;
1114*59bfda1fSAndroid Build Coastguard Worker }
1115*59bfda1fSAndroid Build Coastguard Worker
1116*59bfda1fSAndroid Build Coastguard Worker if (get_sb(cp_payload) > (blocks_per_seg - F2FS_CP_PACKS)) {
1117*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Insane cp_payload (%u > %u)\n",
1118*59bfda1fSAndroid Build Coastguard Worker get_sb(cp_payload), blocks_per_seg - F2FS_CP_PACKS);
1119*59bfda1fSAndroid Build Coastguard Worker return 1;
1120*59bfda1fSAndroid Build Coastguard Worker }
1121*59bfda1fSAndroid Build Coastguard Worker
1122*59bfda1fSAndroid Build Coastguard Worker /* check reserved ino info */
1123*59bfda1fSAndroid Build Coastguard Worker if (get_sb(node_ino) != 1 || get_sb(meta_ino) != 2 ||
1124*59bfda1fSAndroid Build Coastguard Worker get_sb(root_ino) != 3) {
1125*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)\n",
1126*59bfda1fSAndroid Build Coastguard Worker get_sb(node_ino), get_sb(meta_ino), get_sb(root_ino));
1127*59bfda1fSAndroid Build Coastguard Worker return -1;
1128*59bfda1fSAndroid Build Coastguard Worker }
1129*59bfda1fSAndroid Build Coastguard Worker
1130*59bfda1fSAndroid Build Coastguard Worker /* Check zoned block device feature */
1131*59bfda1fSAndroid Build Coastguard Worker if (c.devices[0].zoned_model != F2FS_ZONED_NONE &&
1132*59bfda1fSAndroid Build Coastguard Worker !(get_sb(feature) & F2FS_FEATURE_BLKZONED)) {
1133*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tMissing zoned block device feature\n");
1134*59bfda1fSAndroid Build Coastguard Worker return -1;
1135*59bfda1fSAndroid Build Coastguard Worker }
1136*59bfda1fSAndroid Build Coastguard Worker
1137*59bfda1fSAndroid Build Coastguard Worker if (sanity_check_area_boundary(sb, sb_addr))
1138*59bfda1fSAndroid Build Coastguard Worker return -1;
1139*59bfda1fSAndroid Build Coastguard Worker return 0;
1140*59bfda1fSAndroid Build Coastguard Worker }
1141*59bfda1fSAndroid Build Coastguard Worker
1142*59bfda1fSAndroid Build Coastguard Worker #define CHECK_PERIOD (3600 * 24 * 30) // one month by default
1143*59bfda1fSAndroid Build Coastguard Worker
validate_super_block(struct f2fs_sb_info * sbi,enum SB_ADDR sb_addr)1144*59bfda1fSAndroid Build Coastguard Worker int validate_super_block(struct f2fs_sb_info *sbi, enum SB_ADDR sb_addr)
1145*59bfda1fSAndroid Build Coastguard Worker {
1146*59bfda1fSAndroid Build Coastguard Worker char buf[F2FS_BLKSIZE];
1147*59bfda1fSAndroid Build Coastguard Worker
1148*59bfda1fSAndroid Build Coastguard Worker sbi->raw_super = malloc(sizeof(struct f2fs_super_block));
1149*59bfda1fSAndroid Build Coastguard Worker if (!sbi->raw_super)
1150*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1151*59bfda1fSAndroid Build Coastguard Worker
1152*59bfda1fSAndroid Build Coastguard Worker if (dev_read_block(buf, sb_addr))
1153*59bfda1fSAndroid Build Coastguard Worker return -1;
1154*59bfda1fSAndroid Build Coastguard Worker
1155*59bfda1fSAndroid Build Coastguard Worker memcpy(sbi->raw_super, buf + F2FS_SUPER_OFFSET,
1156*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_super_block));
1157*59bfda1fSAndroid Build Coastguard Worker
1158*59bfda1fSAndroid Build Coastguard Worker if (!sanity_check_raw_super(sbi->raw_super, sb_addr)) {
1159*59bfda1fSAndroid Build Coastguard Worker /* get kernel version */
1160*59bfda1fSAndroid Build Coastguard Worker if (c.kd >= 0) {
1161*59bfda1fSAndroid Build Coastguard Worker dev_read_version(c.version, 0, VERSION_NAME_LEN);
1162*59bfda1fSAndroid Build Coastguard Worker get_kernel_version(c.version);
1163*59bfda1fSAndroid Build Coastguard Worker } else {
1164*59bfda1fSAndroid Build Coastguard Worker get_kernel_uname_version(c.version);
1165*59bfda1fSAndroid Build Coastguard Worker }
1166*59bfda1fSAndroid Build Coastguard Worker
1167*59bfda1fSAndroid Build Coastguard Worker /* build sb version */
1168*59bfda1fSAndroid Build Coastguard Worker memcpy(c.sb_version, sbi->raw_super->version, VERSION_NAME_LEN);
1169*59bfda1fSAndroid Build Coastguard Worker get_kernel_version(c.sb_version);
1170*59bfda1fSAndroid Build Coastguard Worker memcpy(c.init_version, sbi->raw_super->init_version,
1171*59bfda1fSAndroid Build Coastguard Worker VERSION_NAME_LEN);
1172*59bfda1fSAndroid Build Coastguard Worker get_kernel_version(c.init_version);
1173*59bfda1fSAndroid Build Coastguard Worker
1174*59bfda1fSAndroid Build Coastguard Worker if (is_checkpoint_stop(sbi->raw_super, false))
1175*59bfda1fSAndroid Build Coastguard Worker c.invalid_sb |= SB_FORCE_STOP;
1176*59bfda1fSAndroid Build Coastguard Worker if (is_checkpoint_stop(sbi->raw_super, true))
1177*59bfda1fSAndroid Build Coastguard Worker c.invalid_sb |= SB_ABNORMAL_STOP;
1178*59bfda1fSAndroid Build Coastguard Worker if (is_inconsistent_error(sbi->raw_super))
1179*59bfda1fSAndroid Build Coastguard Worker c.invalid_sb |= SB_FS_ERRORS;
1180*59bfda1fSAndroid Build Coastguard Worker
1181*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: MKFS version\n \"%s\"\n", c.init_version);
1182*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: FSCK version\n from \"%s\"\n to \"%s\"\n",
1183*59bfda1fSAndroid Build Coastguard Worker c.sb_version, c.version);
1184*59bfda1fSAndroid Build Coastguard Worker print_sb_state(sbi->raw_super);
1185*59bfda1fSAndroid Build Coastguard Worker print_sb_stop_reason(sbi->raw_super);
1186*59bfda1fSAndroid Build Coastguard Worker print_sb_errors(sbi->raw_super);
1187*59bfda1fSAndroid Build Coastguard Worker return 0;
1188*59bfda1fSAndroid Build Coastguard Worker }
1189*59bfda1fSAndroid Build Coastguard Worker
1190*59bfda1fSAndroid Build Coastguard Worker free(sbi->raw_super);
1191*59bfda1fSAndroid Build Coastguard Worker sbi->raw_super = NULL;
1192*59bfda1fSAndroid Build Coastguard Worker c.invalid_sb |= SB_INVALID;
1193*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tCan't find a valid F2FS superblock at 0x%x\n", sb_addr);
1194*59bfda1fSAndroid Build Coastguard Worker
1195*59bfda1fSAndroid Build Coastguard Worker return -EINVAL;
1196*59bfda1fSAndroid Build Coastguard Worker }
1197*59bfda1fSAndroid Build Coastguard Worker
init_sb_info(struct f2fs_sb_info * sbi)1198*59bfda1fSAndroid Build Coastguard Worker int init_sb_info(struct f2fs_sb_info *sbi)
1199*59bfda1fSAndroid Build Coastguard Worker {
1200*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1201*59bfda1fSAndroid Build Coastguard Worker u64 total_sectors;
1202*59bfda1fSAndroid Build Coastguard Worker int i;
1203*59bfda1fSAndroid Build Coastguard Worker
1204*59bfda1fSAndroid Build Coastguard Worker sbi->log_sectors_per_block = get_sb(log_sectors_per_block);
1205*59bfda1fSAndroid Build Coastguard Worker sbi->log_blocksize = get_sb(log_blocksize);
1206*59bfda1fSAndroid Build Coastguard Worker sbi->blocksize = 1 << sbi->log_blocksize;
1207*59bfda1fSAndroid Build Coastguard Worker sbi->log_blocks_per_seg = get_sb(log_blocks_per_seg);
1208*59bfda1fSAndroid Build Coastguard Worker sbi->blocks_per_seg = 1 << sbi->log_blocks_per_seg;
1209*59bfda1fSAndroid Build Coastguard Worker sbi->segs_per_sec = get_sb(segs_per_sec);
1210*59bfda1fSAndroid Build Coastguard Worker sbi->secs_per_zone = get_sb(secs_per_zone);
1211*59bfda1fSAndroid Build Coastguard Worker sbi->total_sections = get_sb(section_count);
1212*59bfda1fSAndroid Build Coastguard Worker sbi->total_node_count = (get_sb(segment_count_nat) / 2) *
1213*59bfda1fSAndroid Build Coastguard Worker sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
1214*59bfda1fSAndroid Build Coastguard Worker sbi->root_ino_num = get_sb(root_ino);
1215*59bfda1fSAndroid Build Coastguard Worker sbi->node_ino_num = get_sb(node_ino);
1216*59bfda1fSAndroid Build Coastguard Worker sbi->meta_ino_num = get_sb(meta_ino);
1217*59bfda1fSAndroid Build Coastguard Worker sbi->cur_victim_sec = NULL_SEGNO;
1218*59bfda1fSAndroid Build Coastguard Worker
1219*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < MAX_DEVICES; i++) {
1220*59bfda1fSAndroid Build Coastguard Worker if (!sb->devs[i].path[0])
1221*59bfda1fSAndroid Build Coastguard Worker break;
1222*59bfda1fSAndroid Build Coastguard Worker
1223*59bfda1fSAndroid Build Coastguard Worker if (i) {
1224*59bfda1fSAndroid Build Coastguard Worker c.devices[i].path = strdup((char *)sb->devs[i].path);
1225*59bfda1fSAndroid Build Coastguard Worker if (get_device_info(i))
1226*59bfda1fSAndroid Build Coastguard Worker ASSERT(0);
1227*59bfda1fSAndroid Build Coastguard Worker } else if (c.func != INJECT) {
1228*59bfda1fSAndroid Build Coastguard Worker ASSERT(!strcmp((char *)sb->devs[i].path,
1229*59bfda1fSAndroid Build Coastguard Worker (char *)c.devices[i].path));
1230*59bfda1fSAndroid Build Coastguard Worker }
1231*59bfda1fSAndroid Build Coastguard Worker
1232*59bfda1fSAndroid Build Coastguard Worker c.devices[i].total_segments =
1233*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(sb->devs[i].total_segments);
1234*59bfda1fSAndroid Build Coastguard Worker if (i)
1235*59bfda1fSAndroid Build Coastguard Worker c.devices[i].start_blkaddr =
1236*59bfda1fSAndroid Build Coastguard Worker c.devices[i - 1].end_blkaddr + 1;
1237*59bfda1fSAndroid Build Coastguard Worker c.devices[i].end_blkaddr = c.devices[i].start_blkaddr +
1238*59bfda1fSAndroid Build Coastguard Worker c.devices[i].total_segments *
1239*59bfda1fSAndroid Build Coastguard Worker c.blks_per_seg - 1;
1240*59bfda1fSAndroid Build Coastguard Worker if (i == 0)
1241*59bfda1fSAndroid Build Coastguard Worker c.devices[i].end_blkaddr += get_sb(segment0_blkaddr);
1242*59bfda1fSAndroid Build Coastguard Worker
1243*59bfda1fSAndroid Build Coastguard Worker if (c.zoned_model == F2FS_ZONED_NONE) {
1244*59bfda1fSAndroid Build Coastguard Worker if (c.devices[i].zoned_model == F2FS_ZONED_HM)
1245*59bfda1fSAndroid Build Coastguard Worker c.zoned_model = F2FS_ZONED_HM;
1246*59bfda1fSAndroid Build Coastguard Worker else if (c.devices[i].zoned_model == F2FS_ZONED_HA &&
1247*59bfda1fSAndroid Build Coastguard Worker c.zoned_model != F2FS_ZONED_HM)
1248*59bfda1fSAndroid Build Coastguard Worker c.zoned_model = F2FS_ZONED_HA;
1249*59bfda1fSAndroid Build Coastguard Worker }
1250*59bfda1fSAndroid Build Coastguard Worker
1251*59bfda1fSAndroid Build Coastguard Worker c.ndevs = i + 1;
1252*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Device[%d] : %s blkaddr = %"PRIx64"--%"PRIx64"\n",
1253*59bfda1fSAndroid Build Coastguard Worker i, c.devices[i].path,
1254*59bfda1fSAndroid Build Coastguard Worker c.devices[i].start_blkaddr,
1255*59bfda1fSAndroid Build Coastguard Worker c.devices[i].end_blkaddr);
1256*59bfda1fSAndroid Build Coastguard Worker }
1257*59bfda1fSAndroid Build Coastguard Worker
1258*59bfda1fSAndroid Build Coastguard Worker total_sectors = get_sb(block_count) << sbi->log_sectors_per_block;
1259*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Segments per section = %d\n", sbi->segs_per_sec);
1260*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Sections per zone = %d\n", sbi->secs_per_zone);
1261*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: total FS sectors = %"PRIu64" (%"PRIu64" MB)\n",
1262*59bfda1fSAndroid Build Coastguard Worker total_sectors, total_sectors >>
1263*59bfda1fSAndroid Build Coastguard Worker (20 - get_sb(log_sectorsize)));
1264*59bfda1fSAndroid Build Coastguard Worker return 0;
1265*59bfda1fSAndroid Build Coastguard Worker }
1266*59bfda1fSAndroid Build Coastguard Worker
verify_checksum_chksum(struct f2fs_checkpoint * cp)1267*59bfda1fSAndroid Build Coastguard Worker static int verify_checksum_chksum(struct f2fs_checkpoint *cp)
1268*59bfda1fSAndroid Build Coastguard Worker {
1269*59bfda1fSAndroid Build Coastguard Worker unsigned int chksum_offset = get_cp(checksum_offset);
1270*59bfda1fSAndroid Build Coastguard Worker unsigned int crc, cal_crc;
1271*59bfda1fSAndroid Build Coastguard Worker
1272*59bfda1fSAndroid Build Coastguard Worker if (chksum_offset < CP_MIN_CHKSUM_OFFSET ||
1273*59bfda1fSAndroid Build Coastguard Worker chksum_offset > CP_CHKSUM_OFFSET) {
1274*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tInvalid CP CRC offset: %u\n", chksum_offset);
1275*59bfda1fSAndroid Build Coastguard Worker return -1;
1276*59bfda1fSAndroid Build Coastguard Worker }
1277*59bfda1fSAndroid Build Coastguard Worker
1278*59bfda1fSAndroid Build Coastguard Worker crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp + chksum_offset));
1279*59bfda1fSAndroid Build Coastguard Worker cal_crc = f2fs_checkpoint_chksum(cp);
1280*59bfda1fSAndroid Build Coastguard Worker if (cal_crc != crc) {
1281*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tInvalid CP CRC: offset:%u, crc:0x%x, calc:0x%x\n",
1282*59bfda1fSAndroid Build Coastguard Worker chksum_offset, crc, cal_crc);
1283*59bfda1fSAndroid Build Coastguard Worker return -1;
1284*59bfda1fSAndroid Build Coastguard Worker }
1285*59bfda1fSAndroid Build Coastguard Worker return 0;
1286*59bfda1fSAndroid Build Coastguard Worker }
1287*59bfda1fSAndroid Build Coastguard Worker
get_checkpoint_version(block_t cp_addr)1288*59bfda1fSAndroid Build Coastguard Worker static void *get_checkpoint_version(block_t cp_addr)
1289*59bfda1fSAndroid Build Coastguard Worker {
1290*59bfda1fSAndroid Build Coastguard Worker void *cp_page;
1291*59bfda1fSAndroid Build Coastguard Worker
1292*59bfda1fSAndroid Build Coastguard Worker cp_page = malloc(F2FS_BLKSIZE);
1293*59bfda1fSAndroid Build Coastguard Worker ASSERT(cp_page);
1294*59bfda1fSAndroid Build Coastguard Worker
1295*59bfda1fSAndroid Build Coastguard Worker if (dev_read_block(cp_page, cp_addr) < 0)
1296*59bfda1fSAndroid Build Coastguard Worker ASSERT(0);
1297*59bfda1fSAndroid Build Coastguard Worker
1298*59bfda1fSAndroid Build Coastguard Worker if (verify_checksum_chksum((struct f2fs_checkpoint *)cp_page))
1299*59bfda1fSAndroid Build Coastguard Worker goto out;
1300*59bfda1fSAndroid Build Coastguard Worker return cp_page;
1301*59bfda1fSAndroid Build Coastguard Worker out:
1302*59bfda1fSAndroid Build Coastguard Worker free(cp_page);
1303*59bfda1fSAndroid Build Coastguard Worker return NULL;
1304*59bfda1fSAndroid Build Coastguard Worker }
1305*59bfda1fSAndroid Build Coastguard Worker
validate_checkpoint(struct f2fs_sb_info * sbi,block_t cp_addr,unsigned long long * version)1306*59bfda1fSAndroid Build Coastguard Worker void *validate_checkpoint(struct f2fs_sb_info *sbi, block_t cp_addr,
1307*59bfda1fSAndroid Build Coastguard Worker unsigned long long *version)
1308*59bfda1fSAndroid Build Coastguard Worker {
1309*59bfda1fSAndroid Build Coastguard Worker void *cp_page_1, *cp_page_2;
1310*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp;
1311*59bfda1fSAndroid Build Coastguard Worker unsigned long long cur_version = 0, pre_version = 0;
1312*59bfda1fSAndroid Build Coastguard Worker
1313*59bfda1fSAndroid Build Coastguard Worker /* Read the 1st cp block in this CP pack */
1314*59bfda1fSAndroid Build Coastguard Worker cp_page_1 = get_checkpoint_version(cp_addr);
1315*59bfda1fSAndroid Build Coastguard Worker if (!cp_page_1)
1316*59bfda1fSAndroid Build Coastguard Worker return NULL;
1317*59bfda1fSAndroid Build Coastguard Worker
1318*59bfda1fSAndroid Build Coastguard Worker cp = (struct f2fs_checkpoint *)cp_page_1;
1319*59bfda1fSAndroid Build Coastguard Worker if (get_cp(cp_pack_total_block_count) > sbi->blocks_per_seg)
1320*59bfda1fSAndroid Build Coastguard Worker goto invalid_cp1;
1321*59bfda1fSAndroid Build Coastguard Worker
1322*59bfda1fSAndroid Build Coastguard Worker pre_version = get_cp(checkpoint_ver);
1323*59bfda1fSAndroid Build Coastguard Worker
1324*59bfda1fSAndroid Build Coastguard Worker /* Read the 2nd cp block in this CP pack */
1325*59bfda1fSAndroid Build Coastguard Worker cp_addr += get_cp(cp_pack_total_block_count) - 1;
1326*59bfda1fSAndroid Build Coastguard Worker cp_page_2 = get_checkpoint_version(cp_addr);
1327*59bfda1fSAndroid Build Coastguard Worker if (!cp_page_2)
1328*59bfda1fSAndroid Build Coastguard Worker goto invalid_cp1;
1329*59bfda1fSAndroid Build Coastguard Worker
1330*59bfda1fSAndroid Build Coastguard Worker cp = (struct f2fs_checkpoint *)cp_page_2;
1331*59bfda1fSAndroid Build Coastguard Worker cur_version = get_cp(checkpoint_ver);
1332*59bfda1fSAndroid Build Coastguard Worker
1333*59bfda1fSAndroid Build Coastguard Worker if (cur_version == pre_version) {
1334*59bfda1fSAndroid Build Coastguard Worker *version = cur_version;
1335*59bfda1fSAndroid Build Coastguard Worker free(cp_page_2);
1336*59bfda1fSAndroid Build Coastguard Worker return cp_page_1;
1337*59bfda1fSAndroid Build Coastguard Worker }
1338*59bfda1fSAndroid Build Coastguard Worker
1339*59bfda1fSAndroid Build Coastguard Worker free(cp_page_2);
1340*59bfda1fSAndroid Build Coastguard Worker invalid_cp1:
1341*59bfda1fSAndroid Build Coastguard Worker free(cp_page_1);
1342*59bfda1fSAndroid Build Coastguard Worker return NULL;
1343*59bfda1fSAndroid Build Coastguard Worker }
1344*59bfda1fSAndroid Build Coastguard Worker
get_valid_checkpoint(struct f2fs_sb_info * sbi)1345*59bfda1fSAndroid Build Coastguard Worker int get_valid_checkpoint(struct f2fs_sb_info *sbi)
1346*59bfda1fSAndroid Build Coastguard Worker {
1347*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1348*59bfda1fSAndroid Build Coastguard Worker void *cp1, *cp2, *cur_page;
1349*59bfda1fSAndroid Build Coastguard Worker unsigned long blk_size = sbi->blocksize;
1350*59bfda1fSAndroid Build Coastguard Worker unsigned long long cp1_version = 0, cp2_version = 0, version;
1351*59bfda1fSAndroid Build Coastguard Worker unsigned long long cp_start_blk_no;
1352*59bfda1fSAndroid Build Coastguard Worker unsigned int cp_payload, cp_blks;
1353*59bfda1fSAndroid Build Coastguard Worker int ret;
1354*59bfda1fSAndroid Build Coastguard Worker
1355*59bfda1fSAndroid Build Coastguard Worker cp_payload = get_sb(cp_payload);
1356*59bfda1fSAndroid Build Coastguard Worker if (cp_payload > F2FS_BLK_ALIGN(MAX_CP_PAYLOAD))
1357*59bfda1fSAndroid Build Coastguard Worker return -EINVAL;
1358*59bfda1fSAndroid Build Coastguard Worker
1359*59bfda1fSAndroid Build Coastguard Worker cp_blks = 1 + cp_payload;
1360*59bfda1fSAndroid Build Coastguard Worker sbi->ckpt = malloc(cp_blks * blk_size);
1361*59bfda1fSAndroid Build Coastguard Worker if (!sbi->ckpt)
1362*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1363*59bfda1fSAndroid Build Coastguard Worker /*
1364*59bfda1fSAndroid Build Coastguard Worker * Finding out valid cp block involves read both
1365*59bfda1fSAndroid Build Coastguard Worker * sets( cp pack1 and cp pack 2)
1366*59bfda1fSAndroid Build Coastguard Worker */
1367*59bfda1fSAndroid Build Coastguard Worker cp_start_blk_no = get_sb(cp_blkaddr);
1368*59bfda1fSAndroid Build Coastguard Worker cp1 = validate_checkpoint(sbi, cp_start_blk_no, &cp1_version);
1369*59bfda1fSAndroid Build Coastguard Worker
1370*59bfda1fSAndroid Build Coastguard Worker /* The second checkpoint pack should start at the next segment */
1371*59bfda1fSAndroid Build Coastguard Worker cp_start_blk_no += 1 << get_sb(log_blocks_per_seg);
1372*59bfda1fSAndroid Build Coastguard Worker cp2 = validate_checkpoint(sbi, cp_start_blk_no, &cp2_version);
1373*59bfda1fSAndroid Build Coastguard Worker
1374*59bfda1fSAndroid Build Coastguard Worker if (cp1 && cp2) {
1375*59bfda1fSAndroid Build Coastguard Worker if (ver_after(cp2_version, cp1_version)) {
1376*59bfda1fSAndroid Build Coastguard Worker cur_page = cp2;
1377*59bfda1fSAndroid Build Coastguard Worker sbi->cur_cp = 2;
1378*59bfda1fSAndroid Build Coastguard Worker version = cp2_version;
1379*59bfda1fSAndroid Build Coastguard Worker } else {
1380*59bfda1fSAndroid Build Coastguard Worker cur_page = cp1;
1381*59bfda1fSAndroid Build Coastguard Worker sbi->cur_cp = 1;
1382*59bfda1fSAndroid Build Coastguard Worker version = cp1_version;
1383*59bfda1fSAndroid Build Coastguard Worker }
1384*59bfda1fSAndroid Build Coastguard Worker } else if (cp1) {
1385*59bfda1fSAndroid Build Coastguard Worker cur_page = cp1;
1386*59bfda1fSAndroid Build Coastguard Worker sbi->cur_cp = 1;
1387*59bfda1fSAndroid Build Coastguard Worker version = cp1_version;
1388*59bfda1fSAndroid Build Coastguard Worker } else if (cp2) {
1389*59bfda1fSAndroid Build Coastguard Worker cur_page = cp2;
1390*59bfda1fSAndroid Build Coastguard Worker sbi->cur_cp = 2;
1391*59bfda1fSAndroid Build Coastguard Worker version = cp2_version;
1392*59bfda1fSAndroid Build Coastguard Worker } else
1393*59bfda1fSAndroid Build Coastguard Worker goto fail_no_cp;
1394*59bfda1fSAndroid Build Coastguard Worker
1395*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: CKPT version = %llx\n", version);
1396*59bfda1fSAndroid Build Coastguard Worker
1397*59bfda1fSAndroid Build Coastguard Worker memcpy(sbi->ckpt, cur_page, blk_size);
1398*59bfda1fSAndroid Build Coastguard Worker
1399*59bfda1fSAndroid Build Coastguard Worker if (cp_blks > 1) {
1400*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
1401*59bfda1fSAndroid Build Coastguard Worker unsigned long long cp_blk_no;
1402*59bfda1fSAndroid Build Coastguard Worker
1403*59bfda1fSAndroid Build Coastguard Worker cp_blk_no = get_sb(cp_blkaddr);
1404*59bfda1fSAndroid Build Coastguard Worker if (cur_page == cp2)
1405*59bfda1fSAndroid Build Coastguard Worker cp_blk_no += 1 << get_sb(log_blocks_per_seg);
1406*59bfda1fSAndroid Build Coastguard Worker
1407*59bfda1fSAndroid Build Coastguard Worker /* copy sit bitmap */
1408*59bfda1fSAndroid Build Coastguard Worker for (i = 1; i < cp_blks; i++) {
1409*59bfda1fSAndroid Build Coastguard Worker unsigned char *ckpt = (unsigned char *)sbi->ckpt;
1410*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(cur_page, cp_blk_no + i);
1411*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
1412*59bfda1fSAndroid Build Coastguard Worker memcpy(ckpt + i * blk_size, cur_page, blk_size);
1413*59bfda1fSAndroid Build Coastguard Worker }
1414*59bfda1fSAndroid Build Coastguard Worker }
1415*59bfda1fSAndroid Build Coastguard Worker if (cp1)
1416*59bfda1fSAndroid Build Coastguard Worker free(cp1);
1417*59bfda1fSAndroid Build Coastguard Worker if (cp2)
1418*59bfda1fSAndroid Build Coastguard Worker free(cp2);
1419*59bfda1fSAndroid Build Coastguard Worker return 0;
1420*59bfda1fSAndroid Build Coastguard Worker
1421*59bfda1fSAndroid Build Coastguard Worker fail_no_cp:
1422*59bfda1fSAndroid Build Coastguard Worker free(sbi->ckpt);
1423*59bfda1fSAndroid Build Coastguard Worker sbi->ckpt = NULL;
1424*59bfda1fSAndroid Build Coastguard Worker return -EINVAL;
1425*59bfda1fSAndroid Build Coastguard Worker }
1426*59bfda1fSAndroid Build Coastguard Worker
is_checkpoint_stop(struct f2fs_super_block * sb,bool abnormal)1427*59bfda1fSAndroid Build Coastguard Worker bool is_checkpoint_stop(struct f2fs_super_block *sb, bool abnormal)
1428*59bfda1fSAndroid Build Coastguard Worker {
1429*59bfda1fSAndroid Build Coastguard Worker int i;
1430*59bfda1fSAndroid Build Coastguard Worker
1431*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < STOP_CP_REASON_MAX; i++) {
1432*59bfda1fSAndroid Build Coastguard Worker if (abnormal && i == STOP_CP_REASON_SHUTDOWN)
1433*59bfda1fSAndroid Build Coastguard Worker continue;
1434*59bfda1fSAndroid Build Coastguard Worker if (sb->s_stop_reason[i])
1435*59bfda1fSAndroid Build Coastguard Worker return true;
1436*59bfda1fSAndroid Build Coastguard Worker }
1437*59bfda1fSAndroid Build Coastguard Worker
1438*59bfda1fSAndroid Build Coastguard Worker return false;
1439*59bfda1fSAndroid Build Coastguard Worker }
1440*59bfda1fSAndroid Build Coastguard Worker
is_inconsistent_error(struct f2fs_super_block * sb)1441*59bfda1fSAndroid Build Coastguard Worker bool is_inconsistent_error(struct f2fs_super_block *sb)
1442*59bfda1fSAndroid Build Coastguard Worker {
1443*59bfda1fSAndroid Build Coastguard Worker int i;
1444*59bfda1fSAndroid Build Coastguard Worker
1445*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < MAX_F2FS_ERRORS; i++) {
1446*59bfda1fSAndroid Build Coastguard Worker if (sb->s_errors[i])
1447*59bfda1fSAndroid Build Coastguard Worker return true;
1448*59bfda1fSAndroid Build Coastguard Worker }
1449*59bfda1fSAndroid Build Coastguard Worker
1450*59bfda1fSAndroid Build Coastguard Worker return false;
1451*59bfda1fSAndroid Build Coastguard Worker }
1452*59bfda1fSAndroid Build Coastguard Worker
1453*59bfda1fSAndroid Build Coastguard Worker /*
1454*59bfda1fSAndroid Build Coastguard Worker * For a return value of 1, caller should further check for c.fix_on state
1455*59bfda1fSAndroid Build Coastguard Worker * and take appropriate action.
1456*59bfda1fSAndroid Build Coastguard Worker */
f2fs_should_proceed(struct f2fs_super_block * sb,u32 flag)1457*59bfda1fSAndroid Build Coastguard Worker static int f2fs_should_proceed(struct f2fs_super_block *sb, u32 flag)
1458*59bfda1fSAndroid Build Coastguard Worker {
1459*59bfda1fSAndroid Build Coastguard Worker if (!c.fix_on && (c.auto_fix || c.preen_mode)) {
1460*59bfda1fSAndroid Build Coastguard Worker if (flag & CP_FSCK_FLAG ||
1461*59bfda1fSAndroid Build Coastguard Worker flag & CP_DISABLED_FLAG ||
1462*59bfda1fSAndroid Build Coastguard Worker flag & CP_QUOTA_NEED_FSCK_FLAG ||
1463*59bfda1fSAndroid Build Coastguard Worker c.invalid_sb & SB_NEED_FIX ||
1464*59bfda1fSAndroid Build Coastguard Worker (exist_qf_ino(sb) && (flag & CP_ERROR_FLAG))) {
1465*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
1466*59bfda1fSAndroid Build Coastguard Worker } else if (!c.preen_mode) {
1467*59bfda1fSAndroid Build Coastguard Worker print_cp_state(flag);
1468*59bfda1fSAndroid Build Coastguard Worker return 0;
1469*59bfda1fSAndroid Build Coastguard Worker }
1470*59bfda1fSAndroid Build Coastguard Worker }
1471*59bfda1fSAndroid Build Coastguard Worker return 1;
1472*59bfda1fSAndroid Build Coastguard Worker }
1473*59bfda1fSAndroid Build Coastguard Worker
sanity_check_ckpt(struct f2fs_sb_info * sbi)1474*59bfda1fSAndroid Build Coastguard Worker int sanity_check_ckpt(struct f2fs_sb_info *sbi)
1475*59bfda1fSAndroid Build Coastguard Worker {
1476*59bfda1fSAndroid Build Coastguard Worker unsigned int total, fsmeta;
1477*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1478*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1479*59bfda1fSAndroid Build Coastguard Worker unsigned int flag = get_cp(ckpt_flags);
1480*59bfda1fSAndroid Build Coastguard Worker unsigned int ovp_segments, reserved_segments;
1481*59bfda1fSAndroid Build Coastguard Worker unsigned int main_segs, blocks_per_seg;
1482*59bfda1fSAndroid Build Coastguard Worker unsigned int sit_segs, nat_segs;
1483*59bfda1fSAndroid Build Coastguard Worker unsigned int sit_bitmap_size, nat_bitmap_size;
1484*59bfda1fSAndroid Build Coastguard Worker unsigned int log_blocks_per_seg;
1485*59bfda1fSAndroid Build Coastguard Worker unsigned int segment_count_main;
1486*59bfda1fSAndroid Build Coastguard Worker unsigned int cp_pack_start_sum, cp_payload;
1487*59bfda1fSAndroid Build Coastguard Worker block_t user_block_count;
1488*59bfda1fSAndroid Build Coastguard Worker int i;
1489*59bfda1fSAndroid Build Coastguard Worker
1490*59bfda1fSAndroid Build Coastguard Worker total = get_sb(segment_count);
1491*59bfda1fSAndroid Build Coastguard Worker fsmeta = get_sb(segment_count_ckpt);
1492*59bfda1fSAndroid Build Coastguard Worker sit_segs = get_sb(segment_count_sit);
1493*59bfda1fSAndroid Build Coastguard Worker fsmeta += sit_segs;
1494*59bfda1fSAndroid Build Coastguard Worker nat_segs = get_sb(segment_count_nat);
1495*59bfda1fSAndroid Build Coastguard Worker fsmeta += nat_segs;
1496*59bfda1fSAndroid Build Coastguard Worker fsmeta += get_cp(rsvd_segment_count);
1497*59bfda1fSAndroid Build Coastguard Worker fsmeta += get_sb(segment_count_ssa);
1498*59bfda1fSAndroid Build Coastguard Worker
1499*59bfda1fSAndroid Build Coastguard Worker if (fsmeta >= total)
1500*59bfda1fSAndroid Build Coastguard Worker return 1;
1501*59bfda1fSAndroid Build Coastguard Worker
1502*59bfda1fSAndroid Build Coastguard Worker ovp_segments = get_cp(overprov_segment_count);
1503*59bfda1fSAndroid Build Coastguard Worker reserved_segments = get_cp(rsvd_segment_count);
1504*59bfda1fSAndroid Build Coastguard Worker
1505*59bfda1fSAndroid Build Coastguard Worker if (!(get_sb(feature) & F2FS_FEATURE_RO) &&
1506*59bfda1fSAndroid Build Coastguard Worker (fsmeta < F2FS_MIN_SEGMENT || ovp_segments == 0 ||
1507*59bfda1fSAndroid Build Coastguard Worker reserved_segments == 0)) {
1508*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong layout: check mkfs.f2fs version\n");
1509*59bfda1fSAndroid Build Coastguard Worker return 1;
1510*59bfda1fSAndroid Build Coastguard Worker }
1511*59bfda1fSAndroid Build Coastguard Worker
1512*59bfda1fSAndroid Build Coastguard Worker user_block_count = get_cp(user_block_count);
1513*59bfda1fSAndroid Build Coastguard Worker segment_count_main = get_sb(segment_count_main) +
1514*59bfda1fSAndroid Build Coastguard Worker ((get_sb(feature) & F2FS_FEATURE_RO) ? 1 : 0);
1515*59bfda1fSAndroid Build Coastguard Worker log_blocks_per_seg = get_sb(log_blocks_per_seg);
1516*59bfda1fSAndroid Build Coastguard Worker if (!user_block_count || user_block_count >=
1517*59bfda1fSAndroid Build Coastguard Worker segment_count_main << log_blocks_per_seg) {
1518*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tWrong user_block_count(%u)\n", user_block_count);
1519*59bfda1fSAndroid Build Coastguard Worker
1520*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_should_proceed(sb, flag))
1521*59bfda1fSAndroid Build Coastguard Worker return 1;
1522*59bfda1fSAndroid Build Coastguard Worker if (!c.fix_on)
1523*59bfda1fSAndroid Build Coastguard Worker return 1;
1524*59bfda1fSAndroid Build Coastguard Worker
1525*59bfda1fSAndroid Build Coastguard Worker if (flag & (CP_FSCK_FLAG | CP_RESIZEFS_FLAG)) {
1526*59bfda1fSAndroid Build Coastguard Worker u32 valid_user_block_cnt;
1527*59bfda1fSAndroid Build Coastguard Worker u32 seg_cnt_main = get_sb(segment_count) -
1528*59bfda1fSAndroid Build Coastguard Worker (get_sb(segment_count_ckpt) +
1529*59bfda1fSAndroid Build Coastguard Worker get_sb(segment_count_sit) +
1530*59bfda1fSAndroid Build Coastguard Worker get_sb(segment_count_nat) +
1531*59bfda1fSAndroid Build Coastguard Worker get_sb(segment_count_ssa));
1532*59bfda1fSAndroid Build Coastguard Worker
1533*59bfda1fSAndroid Build Coastguard Worker /* validate segment_count_main in sb first */
1534*59bfda1fSAndroid Build Coastguard Worker if (seg_cnt_main != get_sb(segment_count_main)) {
1535*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Inconsistent segment_cnt_main %u in sb\n",
1536*59bfda1fSAndroid Build Coastguard Worker segment_count_main << log_blocks_per_seg);
1537*59bfda1fSAndroid Build Coastguard Worker return 1;
1538*59bfda1fSAndroid Build Coastguard Worker }
1539*59bfda1fSAndroid Build Coastguard Worker valid_user_block_cnt = ((get_sb(segment_count_main) -
1540*59bfda1fSAndroid Build Coastguard Worker get_cp(overprov_segment_count)) * c.blks_per_seg);
1541*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Fix wrong user_block_count in CP: (%u) -> (%u)\n",
1542*59bfda1fSAndroid Build Coastguard Worker user_block_count, valid_user_block_cnt);
1543*59bfda1fSAndroid Build Coastguard Worker set_cp(user_block_count, valid_user_block_cnt);
1544*59bfda1fSAndroid Build Coastguard Worker c.bug_on = 1;
1545*59bfda1fSAndroid Build Coastguard Worker }
1546*59bfda1fSAndroid Build Coastguard Worker }
1547*59bfda1fSAndroid Build Coastguard Worker
1548*59bfda1fSAndroid Build Coastguard Worker main_segs = get_sb(segment_count_main);
1549*59bfda1fSAndroid Build Coastguard Worker blocks_per_seg = sbi->blocks_per_seg;
1550*59bfda1fSAndroid Build Coastguard Worker
1551*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
1552*59bfda1fSAndroid Build Coastguard Worker if (get_cp(cur_node_segno[i]) >= main_segs ||
1553*59bfda1fSAndroid Build Coastguard Worker get_cp(cur_node_blkoff[i]) >= blocks_per_seg)
1554*59bfda1fSAndroid Build Coastguard Worker return 1;
1555*59bfda1fSAndroid Build Coastguard Worker }
1556*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
1557*59bfda1fSAndroid Build Coastguard Worker if (get_cp(cur_data_segno[i]) >= main_segs ||
1558*59bfda1fSAndroid Build Coastguard Worker get_cp(cur_data_blkoff[i]) >= blocks_per_seg)
1559*59bfda1fSAndroid Build Coastguard Worker return 1;
1560*59bfda1fSAndroid Build Coastguard Worker }
1561*59bfda1fSAndroid Build Coastguard Worker
1562*59bfda1fSAndroid Build Coastguard Worker sit_bitmap_size = get_cp(sit_ver_bitmap_bytesize);
1563*59bfda1fSAndroid Build Coastguard Worker nat_bitmap_size = get_cp(nat_ver_bitmap_bytesize);
1564*59bfda1fSAndroid Build Coastguard Worker
1565*59bfda1fSAndroid Build Coastguard Worker if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
1566*59bfda1fSAndroid Build Coastguard Worker nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
1567*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong bitmap size: sit(%u), nat(%u)\n",
1568*59bfda1fSAndroid Build Coastguard Worker sit_bitmap_size, nat_bitmap_size);
1569*59bfda1fSAndroid Build Coastguard Worker return 1;
1570*59bfda1fSAndroid Build Coastguard Worker }
1571*59bfda1fSAndroid Build Coastguard Worker
1572*59bfda1fSAndroid Build Coastguard Worker cp_pack_start_sum = __start_sum_addr(sbi);
1573*59bfda1fSAndroid Build Coastguard Worker cp_payload = __cp_payload(sbi);
1574*59bfda1fSAndroid Build Coastguard Worker if (cp_pack_start_sum < cp_payload + 1 ||
1575*59bfda1fSAndroid Build Coastguard Worker cp_pack_start_sum > blocks_per_seg - 1 -
1576*59bfda1fSAndroid Build Coastguard Worker NR_CURSEG_TYPE) {
1577*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tWrong cp_pack_start_sum(%u) or cp_payload(%u)\n",
1578*59bfda1fSAndroid Build Coastguard Worker cp_pack_start_sum, cp_payload);
1579*59bfda1fSAndroid Build Coastguard Worker if (get_sb(feature) & F2FS_FEATURE_SB_CHKSUM)
1580*59bfda1fSAndroid Build Coastguard Worker return 1;
1581*59bfda1fSAndroid Build Coastguard Worker set_sb(cp_payload, cp_pack_start_sum - 1);
1582*59bfda1fSAndroid Build Coastguard Worker update_superblock(sb, SB_MASK_ALL);
1583*59bfda1fSAndroid Build Coastguard Worker }
1584*59bfda1fSAndroid Build Coastguard Worker
1585*59bfda1fSAndroid Build Coastguard Worker return 0;
1586*59bfda1fSAndroid Build Coastguard Worker }
1587*59bfda1fSAndroid Build Coastguard Worker
current_nat_addr(struct f2fs_sb_info * sbi,nid_t start,int * pack)1588*59bfda1fSAndroid Build Coastguard Worker pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start, int *pack)
1589*59bfda1fSAndroid Build Coastguard Worker {
1590*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
1591*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_off;
1592*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_addr;
1593*59bfda1fSAndroid Build Coastguard Worker int seg_off;
1594*59bfda1fSAndroid Build Coastguard Worker
1595*59bfda1fSAndroid Build Coastguard Worker block_off = NAT_BLOCK_OFFSET(start);
1596*59bfda1fSAndroid Build Coastguard Worker seg_off = block_off >> sbi->log_blocks_per_seg;
1597*59bfda1fSAndroid Build Coastguard Worker
1598*59bfda1fSAndroid Build Coastguard Worker block_addr = (pgoff_t)(nm_i->nat_blkaddr +
1599*59bfda1fSAndroid Build Coastguard Worker (seg_off << sbi->log_blocks_per_seg << 1) +
1600*59bfda1fSAndroid Build Coastguard Worker (block_off & ((1 << sbi->log_blocks_per_seg) -1)));
1601*59bfda1fSAndroid Build Coastguard Worker if (pack)
1602*59bfda1fSAndroid Build Coastguard Worker *pack = 1;
1603*59bfda1fSAndroid Build Coastguard Worker
1604*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(block_off, nm_i->nat_bitmap)) {
1605*59bfda1fSAndroid Build Coastguard Worker block_addr += sbi->blocks_per_seg;
1606*59bfda1fSAndroid Build Coastguard Worker if (pack)
1607*59bfda1fSAndroid Build Coastguard Worker *pack = 2;
1608*59bfda1fSAndroid Build Coastguard Worker }
1609*59bfda1fSAndroid Build Coastguard Worker
1610*59bfda1fSAndroid Build Coastguard Worker return block_addr;
1611*59bfda1fSAndroid Build Coastguard Worker }
1612*59bfda1fSAndroid Build Coastguard Worker
1613*59bfda1fSAndroid Build Coastguard Worker /* will not init nid_bitmap from nat */
f2fs_early_init_nid_bitmap(struct f2fs_sb_info * sbi)1614*59bfda1fSAndroid Build Coastguard Worker static int f2fs_early_init_nid_bitmap(struct f2fs_sb_info *sbi)
1615*59bfda1fSAndroid Build Coastguard Worker {
1616*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
1617*59bfda1fSAndroid Build Coastguard Worker int nid_bitmap_size = (nm_i->max_nid + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
1618*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1619*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *sum = curseg->sum_blk;
1620*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(sum);
1621*59bfda1fSAndroid Build Coastguard Worker nid_t nid;
1622*59bfda1fSAndroid Build Coastguard Worker int i;
1623*59bfda1fSAndroid Build Coastguard Worker
1624*59bfda1fSAndroid Build Coastguard Worker if (!(c.func == SLOAD || c.func == FSCK))
1625*59bfda1fSAndroid Build Coastguard Worker return 0;
1626*59bfda1fSAndroid Build Coastguard Worker
1627*59bfda1fSAndroid Build Coastguard Worker nm_i->nid_bitmap = (char *)calloc(nid_bitmap_size, 1);
1628*59bfda1fSAndroid Build Coastguard Worker if (!nm_i->nid_bitmap)
1629*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1630*59bfda1fSAndroid Build Coastguard Worker
1631*59bfda1fSAndroid Build Coastguard Worker /* arbitrarily set 0 bit */
1632*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(0, nm_i->nid_bitmap);
1633*59bfda1fSAndroid Build Coastguard Worker
1634*59bfda1fSAndroid Build Coastguard Worker if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) {
1635*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to "
1636*59bfda1fSAndroid Build Coastguard Worker "NAT_JOURNAL_ENTRIES(%zu)\n",
1637*59bfda1fSAndroid Build Coastguard Worker nats_in_cursum(journal), NAT_JOURNAL_ENTRIES);
1638*59bfda1fSAndroid Build Coastguard Worker journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES);
1639*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
1640*59bfda1fSAndroid Build Coastguard Worker }
1641*59bfda1fSAndroid Build Coastguard Worker
1642*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nats_in_cursum(journal); i++) {
1643*59bfda1fSAndroid Build Coastguard Worker block_t addr;
1644*59bfda1fSAndroid Build Coastguard Worker
1645*59bfda1fSAndroid Build Coastguard Worker addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
1646*59bfda1fSAndroid Build Coastguard Worker if (addr != NULL_ADDR &&
1647*59bfda1fSAndroid Build Coastguard Worker !f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC)) {
1648*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
1649*59bfda1fSAndroid Build Coastguard Worker journal->n_nats = cpu_to_le16(i);
1650*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
1651*59bfda1fSAndroid Build Coastguard Worker continue;
1652*59bfda1fSAndroid Build Coastguard Worker }
1653*59bfda1fSAndroid Build Coastguard Worker
1654*59bfda1fSAndroid Build Coastguard Worker nid = le32_to_cpu(nid_in_journal(journal, i));
1655*59bfda1fSAndroid Build Coastguard Worker if (!IS_VALID_NID(sbi, nid)) {
1656*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid);
1657*59bfda1fSAndroid Build Coastguard Worker journal->n_nats = cpu_to_le16(i);
1658*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
1659*59bfda1fSAndroid Build Coastguard Worker continue;
1660*59bfda1fSAndroid Build Coastguard Worker }
1661*59bfda1fSAndroid Build Coastguard Worker if (addr != NULL_ADDR)
1662*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(nid, nm_i->nid_bitmap);
1663*59bfda1fSAndroid Build Coastguard Worker }
1664*59bfda1fSAndroid Build Coastguard Worker return 0;
1665*59bfda1fSAndroid Build Coastguard Worker }
1666*59bfda1fSAndroid Build Coastguard Worker
1667*59bfda1fSAndroid Build Coastguard Worker /* will init nid_bitmap from nat */
f2fs_late_init_nid_bitmap(struct f2fs_sb_info * sbi)1668*59bfda1fSAndroid Build Coastguard Worker static int f2fs_late_init_nid_bitmap(struct f2fs_sb_info *sbi)
1669*59bfda1fSAndroid Build Coastguard Worker {
1670*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
1671*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block;
1672*59bfda1fSAndroid Build Coastguard Worker block_t start_blk;
1673*59bfda1fSAndroid Build Coastguard Worker nid_t nid;
1674*59bfda1fSAndroid Build Coastguard Worker
1675*59bfda1fSAndroid Build Coastguard Worker if (!(c.func == SLOAD || c.func == FSCK))
1676*59bfda1fSAndroid Build Coastguard Worker return 0;
1677*59bfda1fSAndroid Build Coastguard Worker
1678*59bfda1fSAndroid Build Coastguard Worker nat_block = malloc(F2FS_BLKSIZE);
1679*59bfda1fSAndroid Build Coastguard Worker if (!nat_block) {
1680*59bfda1fSAndroid Build Coastguard Worker free(nm_i->nid_bitmap);
1681*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1682*59bfda1fSAndroid Build Coastguard Worker }
1683*59bfda1fSAndroid Build Coastguard Worker
1684*59bfda1fSAndroid Build Coastguard Worker f2fs_ra_meta_pages(sbi, 0, NAT_BLOCK_OFFSET(nm_i->max_nid),
1685*59bfda1fSAndroid Build Coastguard Worker META_NAT);
1686*59bfda1fSAndroid Build Coastguard Worker for (nid = 0; nid < nm_i->max_nid; nid++) {
1687*59bfda1fSAndroid Build Coastguard Worker if (!(nid % NAT_ENTRY_PER_BLOCK)) {
1688*59bfda1fSAndroid Build Coastguard Worker int ret;
1689*59bfda1fSAndroid Build Coastguard Worker
1690*59bfda1fSAndroid Build Coastguard Worker start_blk = current_nat_addr(sbi, nid, NULL);
1691*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, start_blk);
1692*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
1693*59bfda1fSAndroid Build Coastguard Worker }
1694*59bfda1fSAndroid Build Coastguard Worker
1695*59bfda1fSAndroid Build Coastguard Worker if (nat_block->entries[nid % NAT_ENTRY_PER_BLOCK].block_addr)
1696*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(nid, nm_i->nid_bitmap);
1697*59bfda1fSAndroid Build Coastguard Worker }
1698*59bfda1fSAndroid Build Coastguard Worker
1699*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
1700*59bfda1fSAndroid Build Coastguard Worker return 0;
1701*59bfda1fSAndroid Build Coastguard Worker }
1702*59bfda1fSAndroid Build Coastguard Worker
update_nat_bits_flags(struct f2fs_super_block * sb,struct f2fs_checkpoint * cp,u32 flags)1703*59bfda1fSAndroid Build Coastguard Worker u32 update_nat_bits_flags(struct f2fs_super_block *sb,
1704*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp, u32 flags)
1705*59bfda1fSAndroid Build Coastguard Worker {
1706*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_bits_bytes, nat_bits_blocks;
1707*59bfda1fSAndroid Build Coastguard Worker
1708*59bfda1fSAndroid Build Coastguard Worker nat_bits_bytes = get_sb(segment_count_nat) << 5;
1709*59bfda1fSAndroid Build Coastguard Worker nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
1710*59bfda1fSAndroid Build Coastguard Worker F2FS_BLKSIZE - 1);
1711*59bfda1fSAndroid Build Coastguard Worker if (get_cp(cp_pack_total_block_count) <=
1712*59bfda1fSAndroid Build Coastguard Worker (1 << get_sb(log_blocks_per_seg)) - nat_bits_blocks)
1713*59bfda1fSAndroid Build Coastguard Worker flags |= CP_NAT_BITS_FLAG;
1714*59bfda1fSAndroid Build Coastguard Worker else
1715*59bfda1fSAndroid Build Coastguard Worker flags &= (~CP_NAT_BITS_FLAG);
1716*59bfda1fSAndroid Build Coastguard Worker
1717*59bfda1fSAndroid Build Coastguard Worker return flags;
1718*59bfda1fSAndroid Build Coastguard Worker }
1719*59bfda1fSAndroid Build Coastguard Worker
1720*59bfda1fSAndroid Build Coastguard Worker /* should call flush_journal_entries() bfore this */
write_nat_bits(struct f2fs_sb_info * sbi,struct f2fs_super_block * sb,struct f2fs_checkpoint * cp,int set)1721*59bfda1fSAndroid Build Coastguard Worker void write_nat_bits(struct f2fs_sb_info *sbi,
1722*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb, struct f2fs_checkpoint *cp, int set)
1723*59bfda1fSAndroid Build Coastguard Worker {
1724*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
1725*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_blocks = get_sb(segment_count_nat) <<
1726*59bfda1fSAndroid Build Coastguard Worker (get_sb(log_blocks_per_seg) - 1);
1727*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_bits_bytes = nat_blocks >> 3;
1728*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) +
1729*59bfda1fSAndroid Build Coastguard Worker 8 + F2FS_BLKSIZE - 1);
1730*59bfda1fSAndroid Build Coastguard Worker unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits;
1731*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block;
1732*59bfda1fSAndroid Build Coastguard Worker uint32_t i, j;
1733*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr;
1734*59bfda1fSAndroid Build Coastguard Worker int ret;
1735*59bfda1fSAndroid Build Coastguard Worker
1736*59bfda1fSAndroid Build Coastguard Worker nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
1737*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_bits);
1738*59bfda1fSAndroid Build Coastguard Worker
1739*59bfda1fSAndroid Build Coastguard Worker nat_block = malloc(F2FS_BLKSIZE);
1740*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_block);
1741*59bfda1fSAndroid Build Coastguard Worker
1742*59bfda1fSAndroid Build Coastguard Worker full_nat_bits = nat_bits + 8;
1743*59bfda1fSAndroid Build Coastguard Worker empty_nat_bits = full_nat_bits + nat_bits_bytes;
1744*59bfda1fSAndroid Build Coastguard Worker
1745*59bfda1fSAndroid Build Coastguard Worker memset(full_nat_bits, 0, nat_bits_bytes);
1746*59bfda1fSAndroid Build Coastguard Worker memset(empty_nat_bits, 0, nat_bits_bytes);
1747*59bfda1fSAndroid Build Coastguard Worker
1748*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nat_blocks; i++) {
1749*59bfda1fSAndroid Build Coastguard Worker int seg_off = i >> get_sb(log_blocks_per_seg);
1750*59bfda1fSAndroid Build Coastguard Worker int valid = 0;
1751*59bfda1fSAndroid Build Coastguard Worker
1752*59bfda1fSAndroid Build Coastguard Worker blkaddr = (pgoff_t)(get_sb(nat_blkaddr) +
1753*59bfda1fSAndroid Build Coastguard Worker (seg_off << get_sb(log_blocks_per_seg) << 1) +
1754*59bfda1fSAndroid Build Coastguard Worker (i & ((1 << get_sb(log_blocks_per_seg)) - 1)));
1755*59bfda1fSAndroid Build Coastguard Worker
1756*59bfda1fSAndroid Build Coastguard Worker /*
1757*59bfda1fSAndroid Build Coastguard Worker * Should consider new nat_blocks is larger than old
1758*59bfda1fSAndroid Build Coastguard Worker * nm_i->nat_blocks, since nm_i->nat_bitmap is based on
1759*59bfda1fSAndroid Build Coastguard Worker * old one.
1760*59bfda1fSAndroid Build Coastguard Worker */
1761*59bfda1fSAndroid Build Coastguard Worker if (i < nm_i->nat_blocks && f2fs_test_bit(i, nm_i->nat_bitmap))
1762*59bfda1fSAndroid Build Coastguard Worker blkaddr += (1 << get_sb(log_blocks_per_seg));
1763*59bfda1fSAndroid Build Coastguard Worker
1764*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, blkaddr);
1765*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
1766*59bfda1fSAndroid Build Coastguard Worker
1767*59bfda1fSAndroid Build Coastguard Worker for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) {
1768*59bfda1fSAndroid Build Coastguard Worker if ((i == 0 && j == 0) ||
1769*59bfda1fSAndroid Build Coastguard Worker nat_block->entries[j].block_addr != NULL_ADDR)
1770*59bfda1fSAndroid Build Coastguard Worker valid++;
1771*59bfda1fSAndroid Build Coastguard Worker }
1772*59bfda1fSAndroid Build Coastguard Worker if (valid == 0)
1773*59bfda1fSAndroid Build Coastguard Worker test_and_set_bit_le(i, empty_nat_bits);
1774*59bfda1fSAndroid Build Coastguard Worker else if (valid == NAT_ENTRY_PER_BLOCK)
1775*59bfda1fSAndroid Build Coastguard Worker test_and_set_bit_le(i, full_nat_bits);
1776*59bfda1fSAndroid Build Coastguard Worker }
1777*59bfda1fSAndroid Build Coastguard Worker *(__le64 *)nat_bits = get_cp_crc(cp);
1778*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
1779*59bfda1fSAndroid Build Coastguard Worker
1780*59bfda1fSAndroid Build Coastguard Worker blkaddr = get_sb(segment0_blkaddr) + (set <<
1781*59bfda1fSAndroid Build Coastguard Worker get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1782*59bfda1fSAndroid Build Coastguard Worker
1783*59bfda1fSAndroid Build Coastguard Worker DBG(1, "\tWriting NAT bits pages, at offset 0x%08x\n", blkaddr);
1784*59bfda1fSAndroid Build Coastguard Worker
1785*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nat_bits_blocks; i++) {
1786*59bfda1fSAndroid Build Coastguard Worker if (dev_write_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i,
1787*59bfda1fSAndroid Build Coastguard Worker WRITE_LIFE_NONE))
1788*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tError: write NAT bits to disk!!!\n");
1789*59bfda1fSAndroid Build Coastguard Worker }
1790*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Write valid nat_bits in checkpoint\n");
1791*59bfda1fSAndroid Build Coastguard Worker
1792*59bfda1fSAndroid Build Coastguard Worker free(nat_bits);
1793*59bfda1fSAndroid Build Coastguard Worker }
1794*59bfda1fSAndroid Build Coastguard Worker
check_nat_bits(struct f2fs_sb_info * sbi,struct f2fs_super_block * sb,struct f2fs_checkpoint * cp)1795*59bfda1fSAndroid Build Coastguard Worker static int check_nat_bits(struct f2fs_sb_info *sbi,
1796*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb, struct f2fs_checkpoint *cp)
1797*59bfda1fSAndroid Build Coastguard Worker {
1798*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
1799*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_blocks = get_sb(segment_count_nat) <<
1800*59bfda1fSAndroid Build Coastguard Worker (get_sb(log_blocks_per_seg) - 1);
1801*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_bits_bytes = nat_blocks >> 3;
1802*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) +
1803*59bfda1fSAndroid Build Coastguard Worker 8 + F2FS_BLKSIZE - 1);
1804*59bfda1fSAndroid Build Coastguard Worker unsigned char *nat_bits, *full_nat_bits, *empty_nat_bits;
1805*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1806*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
1807*59bfda1fSAndroid Build Coastguard Worker uint32_t i, j;
1808*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr;
1809*59bfda1fSAndroid Build Coastguard Worker int err = 0;
1810*59bfda1fSAndroid Build Coastguard Worker
1811*59bfda1fSAndroid Build Coastguard Worker nat_bits = calloc(F2FS_BLKSIZE, nat_bits_blocks);
1812*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_bits);
1813*59bfda1fSAndroid Build Coastguard Worker
1814*59bfda1fSAndroid Build Coastguard Worker full_nat_bits = nat_bits + 8;
1815*59bfda1fSAndroid Build Coastguard Worker empty_nat_bits = full_nat_bits + nat_bits_bytes;
1816*59bfda1fSAndroid Build Coastguard Worker
1817*59bfda1fSAndroid Build Coastguard Worker blkaddr = get_sb(segment0_blkaddr) + (sbi->cur_cp <<
1818*59bfda1fSAndroid Build Coastguard Worker get_sb(log_blocks_per_seg)) - nat_bits_blocks;
1819*59bfda1fSAndroid Build Coastguard Worker
1820*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nat_bits_blocks; i++) {
1821*59bfda1fSAndroid Build Coastguard Worker if (dev_read_block(nat_bits + i * F2FS_BLKSIZE, blkaddr + i))
1822*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tError: read NAT bits to disk!!!\n");
1823*59bfda1fSAndroid Build Coastguard Worker }
1824*59bfda1fSAndroid Build Coastguard Worker
1825*59bfda1fSAndroid Build Coastguard Worker if (*(__le64 *)nat_bits != get_cp_crc(cp) || nats_in_cursum(journal)) {
1826*59bfda1fSAndroid Build Coastguard Worker /*
1827*59bfda1fSAndroid Build Coastguard Worker * if there is a journal, f2fs was not shutdown cleanly. Let's
1828*59bfda1fSAndroid Build Coastguard Worker * flush them with nat_bits.
1829*59bfda1fSAndroid Build Coastguard Worker */
1830*59bfda1fSAndroid Build Coastguard Worker if (c.fix_on)
1831*59bfda1fSAndroid Build Coastguard Worker err = -1;
1832*59bfda1fSAndroid Build Coastguard Worker /* Otherwise, kernel will disable nat_bits */
1833*59bfda1fSAndroid Build Coastguard Worker goto out;
1834*59bfda1fSAndroid Build Coastguard Worker }
1835*59bfda1fSAndroid Build Coastguard Worker
1836*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nat_blocks; i++) {
1837*59bfda1fSAndroid Build Coastguard Worker uint32_t start_nid = i * NAT_ENTRY_PER_BLOCK;
1838*59bfda1fSAndroid Build Coastguard Worker uint32_t valid = 0;
1839*59bfda1fSAndroid Build Coastguard Worker int empty = test_bit_le(i, empty_nat_bits);
1840*59bfda1fSAndroid Build Coastguard Worker int full = test_bit_le(i, full_nat_bits);
1841*59bfda1fSAndroid Build Coastguard Worker
1842*59bfda1fSAndroid Build Coastguard Worker for (j = 0; j < NAT_ENTRY_PER_BLOCK; j++) {
1843*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(start_nid + j, nm_i->nid_bitmap))
1844*59bfda1fSAndroid Build Coastguard Worker valid++;
1845*59bfda1fSAndroid Build Coastguard Worker }
1846*59bfda1fSAndroid Build Coastguard Worker if (valid == 0) {
1847*59bfda1fSAndroid Build Coastguard Worker if (!empty || full) {
1848*59bfda1fSAndroid Build Coastguard Worker err = -1;
1849*59bfda1fSAndroid Build Coastguard Worker goto out;
1850*59bfda1fSAndroid Build Coastguard Worker }
1851*59bfda1fSAndroid Build Coastguard Worker } else if (valid == NAT_ENTRY_PER_BLOCK) {
1852*59bfda1fSAndroid Build Coastguard Worker if (empty || !full) {
1853*59bfda1fSAndroid Build Coastguard Worker err = -1;
1854*59bfda1fSAndroid Build Coastguard Worker goto out;
1855*59bfda1fSAndroid Build Coastguard Worker }
1856*59bfda1fSAndroid Build Coastguard Worker } else {
1857*59bfda1fSAndroid Build Coastguard Worker if (empty || full) {
1858*59bfda1fSAndroid Build Coastguard Worker err = -1;
1859*59bfda1fSAndroid Build Coastguard Worker goto out;
1860*59bfda1fSAndroid Build Coastguard Worker }
1861*59bfda1fSAndroid Build Coastguard Worker }
1862*59bfda1fSAndroid Build Coastguard Worker }
1863*59bfda1fSAndroid Build Coastguard Worker out:
1864*59bfda1fSAndroid Build Coastguard Worker free(nat_bits);
1865*59bfda1fSAndroid Build Coastguard Worker if (!err) {
1866*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Checked valid nat_bits in checkpoint\n");
1867*59bfda1fSAndroid Build Coastguard Worker } else {
1868*59bfda1fSAndroid Build Coastguard Worker c.bug_nat_bits = 1;
1869*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Corrupted valid nat_bits in checkpoint\n");
1870*59bfda1fSAndroid Build Coastguard Worker }
1871*59bfda1fSAndroid Build Coastguard Worker return err;
1872*59bfda1fSAndroid Build Coastguard Worker }
1873*59bfda1fSAndroid Build Coastguard Worker
init_node_manager(struct f2fs_sb_info * sbi)1874*59bfda1fSAndroid Build Coastguard Worker int init_node_manager(struct f2fs_sb_info *sbi)
1875*59bfda1fSAndroid Build Coastguard Worker {
1876*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1877*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1878*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
1879*59bfda1fSAndroid Build Coastguard Worker unsigned char *version_bitmap;
1880*59bfda1fSAndroid Build Coastguard Worker unsigned int nat_segs;
1881*59bfda1fSAndroid Build Coastguard Worker
1882*59bfda1fSAndroid Build Coastguard Worker nm_i->nat_blkaddr = get_sb(nat_blkaddr);
1883*59bfda1fSAndroid Build Coastguard Worker
1884*59bfda1fSAndroid Build Coastguard Worker /* segment_count_nat includes pair segment so divide to 2. */
1885*59bfda1fSAndroid Build Coastguard Worker nat_segs = get_sb(segment_count_nat) >> 1;
1886*59bfda1fSAndroid Build Coastguard Worker nm_i->nat_blocks = nat_segs << get_sb(log_blocks_per_seg);
1887*59bfda1fSAndroid Build Coastguard Worker nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
1888*59bfda1fSAndroid Build Coastguard Worker nm_i->fcnt = 0;
1889*59bfda1fSAndroid Build Coastguard Worker nm_i->nat_cnt = 0;
1890*59bfda1fSAndroid Build Coastguard Worker nm_i->init_scan_nid = get_cp(next_free_nid);
1891*59bfda1fSAndroid Build Coastguard Worker nm_i->next_scan_nid = get_cp(next_free_nid);
1892*59bfda1fSAndroid Build Coastguard Worker
1893*59bfda1fSAndroid Build Coastguard Worker nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1894*59bfda1fSAndroid Build Coastguard Worker
1895*59bfda1fSAndroid Build Coastguard Worker nm_i->nat_bitmap = malloc(nm_i->bitmap_size);
1896*59bfda1fSAndroid Build Coastguard Worker if (!nm_i->nat_bitmap)
1897*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1898*59bfda1fSAndroid Build Coastguard Worker version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1899*59bfda1fSAndroid Build Coastguard Worker if (!version_bitmap)
1900*59bfda1fSAndroid Build Coastguard Worker return -EFAULT;
1901*59bfda1fSAndroid Build Coastguard Worker
1902*59bfda1fSAndroid Build Coastguard Worker /* copy version bitmap */
1903*59bfda1fSAndroid Build Coastguard Worker memcpy(nm_i->nat_bitmap, version_bitmap, nm_i->bitmap_size);
1904*59bfda1fSAndroid Build Coastguard Worker return f2fs_early_init_nid_bitmap(sbi);
1905*59bfda1fSAndroid Build Coastguard Worker }
1906*59bfda1fSAndroid Build Coastguard Worker
build_node_manager(struct f2fs_sb_info * sbi)1907*59bfda1fSAndroid Build Coastguard Worker int build_node_manager(struct f2fs_sb_info *sbi)
1908*59bfda1fSAndroid Build Coastguard Worker {
1909*59bfda1fSAndroid Build Coastguard Worker int err;
1910*59bfda1fSAndroid Build Coastguard Worker sbi->nm_info = malloc(sizeof(struct f2fs_nm_info));
1911*59bfda1fSAndroid Build Coastguard Worker if (!sbi->nm_info)
1912*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1913*59bfda1fSAndroid Build Coastguard Worker
1914*59bfda1fSAndroid Build Coastguard Worker err = init_node_manager(sbi);
1915*59bfda1fSAndroid Build Coastguard Worker if (err)
1916*59bfda1fSAndroid Build Coastguard Worker return err;
1917*59bfda1fSAndroid Build Coastguard Worker
1918*59bfda1fSAndroid Build Coastguard Worker return 0;
1919*59bfda1fSAndroid Build Coastguard Worker }
1920*59bfda1fSAndroid Build Coastguard Worker
build_sit_info(struct f2fs_sb_info * sbi)1921*59bfda1fSAndroid Build Coastguard Worker int build_sit_info(struct f2fs_sb_info *sbi)
1922*59bfda1fSAndroid Build Coastguard Worker {
1923*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1924*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1925*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i;
1926*59bfda1fSAndroid Build Coastguard Worker unsigned int sit_segs;
1927*59bfda1fSAndroid Build Coastguard Worker int start;
1928*59bfda1fSAndroid Build Coastguard Worker char *src_bitmap, *dst_bitmap;
1929*59bfda1fSAndroid Build Coastguard Worker unsigned char *bitmap;
1930*59bfda1fSAndroid Build Coastguard Worker unsigned int bitmap_size;
1931*59bfda1fSAndroid Build Coastguard Worker
1932*59bfda1fSAndroid Build Coastguard Worker sit_i = malloc(sizeof(struct sit_info));
1933*59bfda1fSAndroid Build Coastguard Worker if (!sit_i) {
1934*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Malloc failed for build_sit_info!\n");
1935*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1936*59bfda1fSAndroid Build Coastguard Worker }
1937*59bfda1fSAndroid Build Coastguard Worker
1938*59bfda1fSAndroid Build Coastguard Worker SM_I(sbi)->sit_info = sit_i;
1939*59bfda1fSAndroid Build Coastguard Worker
1940*59bfda1fSAndroid Build Coastguard Worker sit_i->sentries = calloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry), 1);
1941*59bfda1fSAndroid Build Coastguard Worker if (!sit_i->sentries) {
1942*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Calloc failed for build_sit_info!\n");
1943*59bfda1fSAndroid Build Coastguard Worker goto free_sit_info;
1944*59bfda1fSAndroid Build Coastguard Worker }
1945*59bfda1fSAndroid Build Coastguard Worker
1946*59bfda1fSAndroid Build Coastguard Worker bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE;
1947*59bfda1fSAndroid Build Coastguard Worker
1948*59bfda1fSAndroid Build Coastguard Worker if (need_fsync_data_record(sbi))
1949*59bfda1fSAndroid Build Coastguard Worker bitmap_size += bitmap_size;
1950*59bfda1fSAndroid Build Coastguard Worker
1951*59bfda1fSAndroid Build Coastguard Worker sit_i->bitmap = calloc(bitmap_size, 1);
1952*59bfda1fSAndroid Build Coastguard Worker if (!sit_i->bitmap) {
1953*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Calloc failed for build_sit_info!!\n");
1954*59bfda1fSAndroid Build Coastguard Worker goto free_sentries;
1955*59bfda1fSAndroid Build Coastguard Worker }
1956*59bfda1fSAndroid Build Coastguard Worker
1957*59bfda1fSAndroid Build Coastguard Worker bitmap = sit_i->bitmap;
1958*59bfda1fSAndroid Build Coastguard Worker
1959*59bfda1fSAndroid Build Coastguard Worker for (start = 0; start < MAIN_SEGS(sbi); start++) {
1960*59bfda1fSAndroid Build Coastguard Worker sit_i->sentries[start].cur_valid_map = bitmap;
1961*59bfda1fSAndroid Build Coastguard Worker bitmap += SIT_VBLOCK_MAP_SIZE;
1962*59bfda1fSAndroid Build Coastguard Worker
1963*59bfda1fSAndroid Build Coastguard Worker if (need_fsync_data_record(sbi)) {
1964*59bfda1fSAndroid Build Coastguard Worker sit_i->sentries[start].ckpt_valid_map = bitmap;
1965*59bfda1fSAndroid Build Coastguard Worker bitmap += SIT_VBLOCK_MAP_SIZE;
1966*59bfda1fSAndroid Build Coastguard Worker }
1967*59bfda1fSAndroid Build Coastguard Worker }
1968*59bfda1fSAndroid Build Coastguard Worker
1969*59bfda1fSAndroid Build Coastguard Worker sit_segs = get_sb(segment_count_sit) >> 1;
1970*59bfda1fSAndroid Build Coastguard Worker bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1971*59bfda1fSAndroid Build Coastguard Worker src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1972*59bfda1fSAndroid Build Coastguard Worker
1973*59bfda1fSAndroid Build Coastguard Worker dst_bitmap = malloc(bitmap_size);
1974*59bfda1fSAndroid Build Coastguard Worker if (!dst_bitmap) {
1975*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Malloc failed for build_sit_info!!\n");
1976*59bfda1fSAndroid Build Coastguard Worker goto free_validity_maps;
1977*59bfda1fSAndroid Build Coastguard Worker }
1978*59bfda1fSAndroid Build Coastguard Worker
1979*59bfda1fSAndroid Build Coastguard Worker memcpy(dst_bitmap, src_bitmap, bitmap_size);
1980*59bfda1fSAndroid Build Coastguard Worker
1981*59bfda1fSAndroid Build Coastguard Worker sit_i->sit_base_addr = get_sb(sit_blkaddr);
1982*59bfda1fSAndroid Build Coastguard Worker sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1983*59bfda1fSAndroid Build Coastguard Worker sit_i->written_valid_blocks = get_cp(valid_block_count);
1984*59bfda1fSAndroid Build Coastguard Worker sit_i->sit_bitmap = dst_bitmap;
1985*59bfda1fSAndroid Build Coastguard Worker sit_i->bitmap_size = bitmap_size;
1986*59bfda1fSAndroid Build Coastguard Worker sit_i->dirty_sentries = 0;
1987*59bfda1fSAndroid Build Coastguard Worker sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1988*59bfda1fSAndroid Build Coastguard Worker sit_i->elapsed_time = get_cp(elapsed_time);
1989*59bfda1fSAndroid Build Coastguard Worker return 0;
1990*59bfda1fSAndroid Build Coastguard Worker
1991*59bfda1fSAndroid Build Coastguard Worker free_validity_maps:
1992*59bfda1fSAndroid Build Coastguard Worker free(sit_i->bitmap);
1993*59bfda1fSAndroid Build Coastguard Worker free_sentries:
1994*59bfda1fSAndroid Build Coastguard Worker free(sit_i->sentries);
1995*59bfda1fSAndroid Build Coastguard Worker free_sit_info:
1996*59bfda1fSAndroid Build Coastguard Worker free(sit_i);
1997*59bfda1fSAndroid Build Coastguard Worker
1998*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
1999*59bfda1fSAndroid Build Coastguard Worker }
2000*59bfda1fSAndroid Build Coastguard Worker
reset_curseg(struct f2fs_sb_info * sbi,int type)2001*59bfda1fSAndroid Build Coastguard Worker void reset_curseg(struct f2fs_sb_info *sbi, int type)
2002*59bfda1fSAndroid Build Coastguard Worker {
2003*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, type);
2004*59bfda1fSAndroid Build Coastguard Worker struct summary_footer *sum_footer;
2005*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2006*59bfda1fSAndroid Build Coastguard Worker
2007*59bfda1fSAndroid Build Coastguard Worker sum_footer = F2FS_SUMMARY_BLOCK_FOOTER(curseg->sum_blk);
2008*59bfda1fSAndroid Build Coastguard Worker memset(sum_footer, 0, sizeof(struct summary_footer));
2009*59bfda1fSAndroid Build Coastguard Worker if (IS_DATASEG(type))
2010*59bfda1fSAndroid Build Coastguard Worker SET_SUM_TYPE(curseg->sum_blk, SUM_TYPE_DATA);
2011*59bfda1fSAndroid Build Coastguard Worker if (IS_NODESEG(type))
2012*59bfda1fSAndroid Build Coastguard Worker SET_SUM_TYPE(curseg->sum_blk, SUM_TYPE_NODE);
2013*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, curseg->segno);
2014*59bfda1fSAndroid Build Coastguard Worker se->type = se->orig_type = type;
2015*59bfda1fSAndroid Build Coastguard Worker se->dirty = 1;
2016*59bfda1fSAndroid Build Coastguard Worker }
2017*59bfda1fSAndroid Build Coastguard Worker
read_compacted_summaries(struct f2fs_sb_info * sbi)2018*59bfda1fSAndroid Build Coastguard Worker static void read_compacted_summaries(struct f2fs_sb_info *sbi)
2019*59bfda1fSAndroid Build Coastguard Worker {
2020*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg;
2021*59bfda1fSAndroid Build Coastguard Worker unsigned int i, j, offset;
2022*59bfda1fSAndroid Build Coastguard Worker block_t start;
2023*59bfda1fSAndroid Build Coastguard Worker char *kaddr;
2024*59bfda1fSAndroid Build Coastguard Worker int ret;
2025*59bfda1fSAndroid Build Coastguard Worker
2026*59bfda1fSAndroid Build Coastguard Worker start = start_sum_block(sbi);
2027*59bfda1fSAndroid Build Coastguard Worker
2028*59bfda1fSAndroid Build Coastguard Worker kaddr = malloc(F2FS_BLKSIZE);
2029*59bfda1fSAndroid Build Coastguard Worker ASSERT(kaddr);
2030*59bfda1fSAndroid Build Coastguard Worker
2031*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(kaddr, start++);
2032*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2033*59bfda1fSAndroid Build Coastguard Worker
2034*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2035*59bfda1fSAndroid Build Coastguard Worker memcpy(&F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk)->n_nats, kaddr, SUM_JOURNAL_SIZE);
2036*59bfda1fSAndroid Build Coastguard Worker
2037*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2038*59bfda1fSAndroid Build Coastguard Worker memcpy(&F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk)->n_sits, kaddr + SUM_JOURNAL_SIZE,
2039*59bfda1fSAndroid Build Coastguard Worker SUM_JOURNAL_SIZE);
2040*59bfda1fSAndroid Build Coastguard Worker
2041*59bfda1fSAndroid Build Coastguard Worker offset = 2 * SUM_JOURNAL_SIZE;
2042*59bfda1fSAndroid Build Coastguard Worker for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2043*59bfda1fSAndroid Build Coastguard Worker unsigned short blk_off;
2044*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, i);
2045*59bfda1fSAndroid Build Coastguard Worker
2046*59bfda1fSAndroid Build Coastguard Worker reset_curseg(sbi, i);
2047*59bfda1fSAndroid Build Coastguard Worker
2048*59bfda1fSAndroid Build Coastguard Worker if (curseg->alloc_type == SSR)
2049*59bfda1fSAndroid Build Coastguard Worker blk_off = sbi->blocks_per_seg;
2050*59bfda1fSAndroid Build Coastguard Worker else
2051*59bfda1fSAndroid Build Coastguard Worker blk_off = curseg->next_blkoff;
2052*59bfda1fSAndroid Build Coastguard Worker
2053*59bfda1fSAndroid Build Coastguard Worker ASSERT(blk_off <= ENTRIES_IN_SUM);
2054*59bfda1fSAndroid Build Coastguard Worker
2055*59bfda1fSAndroid Build Coastguard Worker for (j = 0; j < blk_off; j++) {
2056*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary *s;
2057*59bfda1fSAndroid Build Coastguard Worker s = (struct f2fs_summary *)(kaddr + offset);
2058*59bfda1fSAndroid Build Coastguard Worker curseg->sum_blk->entries[j] = *s;
2059*59bfda1fSAndroid Build Coastguard Worker offset += SUMMARY_SIZE;
2060*59bfda1fSAndroid Build Coastguard Worker if (offset + SUMMARY_SIZE <=
2061*59bfda1fSAndroid Build Coastguard Worker F2FS_BLKSIZE - SUM_FOOTER_SIZE)
2062*59bfda1fSAndroid Build Coastguard Worker continue;
2063*59bfda1fSAndroid Build Coastguard Worker memset(kaddr, 0, F2FS_BLKSIZE);
2064*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(kaddr, start++);
2065*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2066*59bfda1fSAndroid Build Coastguard Worker offset = 0;
2067*59bfda1fSAndroid Build Coastguard Worker }
2068*59bfda1fSAndroid Build Coastguard Worker }
2069*59bfda1fSAndroid Build Coastguard Worker free(kaddr);
2070*59bfda1fSAndroid Build Coastguard Worker }
2071*59bfda1fSAndroid Build Coastguard Worker
restore_node_summary(struct f2fs_sb_info * sbi,unsigned int segno,struct f2fs_summary_block * sum_blk)2072*59bfda1fSAndroid Build Coastguard Worker static void restore_node_summary(struct f2fs_sb_info *sbi,
2073*59bfda1fSAndroid Build Coastguard Worker unsigned int segno, struct f2fs_summary_block *sum_blk)
2074*59bfda1fSAndroid Build Coastguard Worker {
2075*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node_blk;
2076*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary *sum_entry;
2077*59bfda1fSAndroid Build Coastguard Worker block_t addr;
2078*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
2079*59bfda1fSAndroid Build Coastguard Worker int ret;
2080*59bfda1fSAndroid Build Coastguard Worker
2081*59bfda1fSAndroid Build Coastguard Worker node_blk = malloc(F2FS_BLKSIZE);
2082*59bfda1fSAndroid Build Coastguard Worker ASSERT(node_blk);
2083*59bfda1fSAndroid Build Coastguard Worker
2084*59bfda1fSAndroid Build Coastguard Worker /* scan the node segment */
2085*59bfda1fSAndroid Build Coastguard Worker addr = START_BLOCK(sbi, segno);
2086*59bfda1fSAndroid Build Coastguard Worker sum_entry = &sum_blk->entries[0];
2087*59bfda1fSAndroid Build Coastguard Worker
2088*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < sbi->blocks_per_seg; i++, sum_entry++) {
2089*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(node_blk, addr);
2090*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2091*59bfda1fSAndroid Build Coastguard Worker sum_entry->nid = F2FS_NODE_FOOTER(node_blk)->nid;
2092*59bfda1fSAndroid Build Coastguard Worker addr++;
2093*59bfda1fSAndroid Build Coastguard Worker }
2094*59bfda1fSAndroid Build Coastguard Worker free(node_blk);
2095*59bfda1fSAndroid Build Coastguard Worker }
2096*59bfda1fSAndroid Build Coastguard Worker
read_normal_summaries(struct f2fs_sb_info * sbi,int type)2097*59bfda1fSAndroid Build Coastguard Worker static void read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2098*59bfda1fSAndroid Build Coastguard Worker {
2099*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2100*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *sum_blk;
2101*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg;
2102*59bfda1fSAndroid Build Coastguard Worker unsigned int segno = 0;
2103*59bfda1fSAndroid Build Coastguard Worker block_t blk_addr = 0;
2104*59bfda1fSAndroid Build Coastguard Worker int ret;
2105*59bfda1fSAndroid Build Coastguard Worker
2106*59bfda1fSAndroid Build Coastguard Worker if (IS_DATASEG(type)) {
2107*59bfda1fSAndroid Build Coastguard Worker segno = get_cp(cur_data_segno[type]);
2108*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
2109*59bfda1fSAndroid Build Coastguard Worker blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2110*59bfda1fSAndroid Build Coastguard Worker else
2111*59bfda1fSAndroid Build Coastguard Worker blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2112*59bfda1fSAndroid Build Coastguard Worker } else {
2113*59bfda1fSAndroid Build Coastguard Worker segno = get_cp(cur_node_segno[type - CURSEG_HOT_NODE]);
2114*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
2115*59bfda1fSAndroid Build Coastguard Worker blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2116*59bfda1fSAndroid Build Coastguard Worker type - CURSEG_HOT_NODE);
2117*59bfda1fSAndroid Build Coastguard Worker else
2118*59bfda1fSAndroid Build Coastguard Worker blk_addr = GET_SUM_BLKADDR(sbi, segno);
2119*59bfda1fSAndroid Build Coastguard Worker }
2120*59bfda1fSAndroid Build Coastguard Worker
2121*59bfda1fSAndroid Build Coastguard Worker sum_blk = malloc(F2FS_BLKSIZE);
2122*59bfda1fSAndroid Build Coastguard Worker ASSERT(sum_blk);
2123*59bfda1fSAndroid Build Coastguard Worker
2124*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(sum_blk, blk_addr);
2125*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2126*59bfda1fSAndroid Build Coastguard Worker
2127*59bfda1fSAndroid Build Coastguard Worker if (IS_NODESEG(type) && !is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
2128*59bfda1fSAndroid Build Coastguard Worker restore_node_summary(sbi, segno, sum_blk);
2129*59bfda1fSAndroid Build Coastguard Worker
2130*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, type);
2131*59bfda1fSAndroid Build Coastguard Worker memcpy(curseg->sum_blk, sum_blk, F2FS_BLKSIZE);
2132*59bfda1fSAndroid Build Coastguard Worker reset_curseg(sbi, type);
2133*59bfda1fSAndroid Build Coastguard Worker free(sum_blk);
2134*59bfda1fSAndroid Build Coastguard Worker }
2135*59bfda1fSAndroid Build Coastguard Worker
update_sum_entry(struct f2fs_sb_info * sbi,block_t blk_addr,struct f2fs_summary * sum)2136*59bfda1fSAndroid Build Coastguard Worker void update_sum_entry(struct f2fs_sb_info *sbi, block_t blk_addr,
2137*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary *sum)
2138*59bfda1fSAndroid Build Coastguard Worker {
2139*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2140*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *sum_blk;
2141*59bfda1fSAndroid Build Coastguard Worker u32 segno, offset;
2142*59bfda1fSAndroid Build Coastguard Worker int type, ret;
2143*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2144*59bfda1fSAndroid Build Coastguard Worker
2145*59bfda1fSAndroid Build Coastguard Worker if (get_sb(feature) & F2FS_FEATURE_RO)
2146*59bfda1fSAndroid Build Coastguard Worker return;
2147*59bfda1fSAndroid Build Coastguard Worker
2148*59bfda1fSAndroid Build Coastguard Worker segno = GET_SEGNO(sbi, blk_addr);
2149*59bfda1fSAndroid Build Coastguard Worker offset = OFFSET_IN_SEG(sbi, blk_addr);
2150*59bfda1fSAndroid Build Coastguard Worker
2151*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
2152*59bfda1fSAndroid Build Coastguard Worker
2153*59bfda1fSAndroid Build Coastguard Worker sum_blk = get_sum_block(sbi, segno, &type);
2154*59bfda1fSAndroid Build Coastguard Worker memcpy(&sum_blk->entries[offset], sum, sizeof(*sum));
2155*59bfda1fSAndroid Build Coastguard Worker F2FS_SUMMARY_BLOCK_FOOTER(sum_blk)->entry_type = IS_NODESEG(se->type) ? SUM_TYPE_NODE :
2156*59bfda1fSAndroid Build Coastguard Worker SUM_TYPE_DATA;
2157*59bfda1fSAndroid Build Coastguard Worker
2158*59bfda1fSAndroid Build Coastguard Worker /* write SSA all the time */
2159*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(sum_blk, GET_SUM_BLKADDR(sbi, segno),
2160*59bfda1fSAndroid Build Coastguard Worker WRITE_LIFE_NONE);
2161*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2162*59bfda1fSAndroid Build Coastguard Worker
2163*59bfda1fSAndroid Build Coastguard Worker if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
2164*59bfda1fSAndroid Build Coastguard Worker type == SEG_TYPE_MAX)
2165*59bfda1fSAndroid Build Coastguard Worker free(sum_blk);
2166*59bfda1fSAndroid Build Coastguard Worker }
2167*59bfda1fSAndroid Build Coastguard Worker
restore_curseg_summaries(struct f2fs_sb_info * sbi)2168*59bfda1fSAndroid Build Coastguard Worker static void restore_curseg_summaries(struct f2fs_sb_info *sbi)
2169*59bfda1fSAndroid Build Coastguard Worker {
2170*59bfda1fSAndroid Build Coastguard Worker int type = CURSEG_HOT_DATA;
2171*59bfda1fSAndroid Build Coastguard Worker
2172*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
2173*59bfda1fSAndroid Build Coastguard Worker read_compacted_summaries(sbi);
2174*59bfda1fSAndroid Build Coastguard Worker type = CURSEG_HOT_NODE;
2175*59bfda1fSAndroid Build Coastguard Worker }
2176*59bfda1fSAndroid Build Coastguard Worker
2177*59bfda1fSAndroid Build Coastguard Worker for (; type <= CURSEG_COLD_NODE; type++)
2178*59bfda1fSAndroid Build Coastguard Worker read_normal_summaries(sbi, type);
2179*59bfda1fSAndroid Build Coastguard Worker }
2180*59bfda1fSAndroid Build Coastguard Worker
build_curseg(struct f2fs_sb_info * sbi)2181*59bfda1fSAndroid Build Coastguard Worker static int build_curseg(struct f2fs_sb_info *sbi)
2182*59bfda1fSAndroid Build Coastguard Worker {
2183*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2184*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *array;
2185*59bfda1fSAndroid Build Coastguard Worker unsigned short blk_off;
2186*59bfda1fSAndroid Build Coastguard Worker unsigned int segno;
2187*59bfda1fSAndroid Build Coastguard Worker int i;
2188*59bfda1fSAndroid Build Coastguard Worker
2189*59bfda1fSAndroid Build Coastguard Worker array = malloc(sizeof(*array) * NR_CURSEG_TYPE);
2190*59bfda1fSAndroid Build Coastguard Worker if (!array) {
2191*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Malloc failed for build_curseg!\n");
2192*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
2193*59bfda1fSAndroid Build Coastguard Worker }
2194*59bfda1fSAndroid Build Coastguard Worker
2195*59bfda1fSAndroid Build Coastguard Worker SM_I(sbi)->curseg_array = array;
2196*59bfda1fSAndroid Build Coastguard Worker
2197*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NR_CURSEG_TYPE; i++) {
2198*59bfda1fSAndroid Build Coastguard Worker array[i].sum_blk = calloc(F2FS_BLKSIZE, 1);
2199*59bfda1fSAndroid Build Coastguard Worker if (!array[i].sum_blk) {
2200*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Calloc failed for build_curseg!!\n");
2201*59bfda1fSAndroid Build Coastguard Worker goto seg_cleanup;
2202*59bfda1fSAndroid Build Coastguard Worker }
2203*59bfda1fSAndroid Build Coastguard Worker
2204*59bfda1fSAndroid Build Coastguard Worker if (i <= CURSEG_COLD_DATA) {
2205*59bfda1fSAndroid Build Coastguard Worker blk_off = get_cp(cur_data_blkoff[i]);
2206*59bfda1fSAndroid Build Coastguard Worker segno = get_cp(cur_data_segno[i]);
2207*59bfda1fSAndroid Build Coastguard Worker }
2208*59bfda1fSAndroid Build Coastguard Worker if (i > CURSEG_COLD_DATA) {
2209*59bfda1fSAndroid Build Coastguard Worker blk_off = get_cp(cur_node_blkoff[i - CURSEG_HOT_NODE]);
2210*59bfda1fSAndroid Build Coastguard Worker segno = get_cp(cur_node_segno[i - CURSEG_HOT_NODE]);
2211*59bfda1fSAndroid Build Coastguard Worker }
2212*59bfda1fSAndroid Build Coastguard Worker ASSERT(segno < MAIN_SEGS(sbi));
2213*59bfda1fSAndroid Build Coastguard Worker ASSERT(blk_off < DEFAULT_BLOCKS_PER_SEGMENT);
2214*59bfda1fSAndroid Build Coastguard Worker
2215*59bfda1fSAndroid Build Coastguard Worker array[i].segno = segno;
2216*59bfda1fSAndroid Build Coastguard Worker array[i].zone = GET_ZONENO_FROM_SEGNO(sbi, segno);
2217*59bfda1fSAndroid Build Coastguard Worker array[i].next_segno = NULL_SEGNO;
2218*59bfda1fSAndroid Build Coastguard Worker array[i].next_blkoff = blk_off;
2219*59bfda1fSAndroid Build Coastguard Worker array[i].alloc_type = cp->alloc_type[i];
2220*59bfda1fSAndroid Build Coastguard Worker }
2221*59bfda1fSAndroid Build Coastguard Worker restore_curseg_summaries(sbi);
2222*59bfda1fSAndroid Build Coastguard Worker return 0;
2223*59bfda1fSAndroid Build Coastguard Worker
2224*59bfda1fSAndroid Build Coastguard Worker seg_cleanup:
2225*59bfda1fSAndroid Build Coastguard Worker for(--i ; i >=0; --i)
2226*59bfda1fSAndroid Build Coastguard Worker free(array[i].sum_blk);
2227*59bfda1fSAndroid Build Coastguard Worker free(array);
2228*59bfda1fSAndroid Build Coastguard Worker
2229*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
2230*59bfda1fSAndroid Build Coastguard Worker }
2231*59bfda1fSAndroid Build Coastguard Worker
check_seg_range(struct f2fs_sb_info * sbi,unsigned int segno)2232*59bfda1fSAndroid Build Coastguard Worker static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
2233*59bfda1fSAndroid Build Coastguard Worker {
2234*59bfda1fSAndroid Build Coastguard Worker unsigned int end_segno = SM_I(sbi)->segment_count - 1;
2235*59bfda1fSAndroid Build Coastguard Worker ASSERT(segno <= end_segno);
2236*59bfda1fSAndroid Build Coastguard Worker }
2237*59bfda1fSAndroid Build Coastguard Worker
current_sit_addr(struct f2fs_sb_info * sbi,unsigned int segno)2238*59bfda1fSAndroid Build Coastguard Worker static inline block_t current_sit_addr(struct f2fs_sb_info *sbi,
2239*59bfda1fSAndroid Build Coastguard Worker unsigned int segno)
2240*59bfda1fSAndroid Build Coastguard Worker {
2241*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
2242*59bfda1fSAndroid Build Coastguard Worker unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
2243*59bfda1fSAndroid Build Coastguard Worker block_t blk_addr = sit_i->sit_base_addr + offset;
2244*59bfda1fSAndroid Build Coastguard Worker
2245*59bfda1fSAndroid Build Coastguard Worker check_seg_range(sbi, segno);
2246*59bfda1fSAndroid Build Coastguard Worker
2247*59bfda1fSAndroid Build Coastguard Worker /* calculate sit block address */
2248*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(offset, sit_i->sit_bitmap))
2249*59bfda1fSAndroid Build Coastguard Worker blk_addr += sit_i->sit_blocks;
2250*59bfda1fSAndroid Build Coastguard Worker
2251*59bfda1fSAndroid Build Coastguard Worker return blk_addr;
2252*59bfda1fSAndroid Build Coastguard Worker }
2253*59bfda1fSAndroid Build Coastguard Worker
get_current_sit_page(struct f2fs_sb_info * sbi,unsigned int segno,struct f2fs_sit_block * sit_blk)2254*59bfda1fSAndroid Build Coastguard Worker void get_current_sit_page(struct f2fs_sb_info *sbi,
2255*59bfda1fSAndroid Build Coastguard Worker unsigned int segno, struct f2fs_sit_block *sit_blk)
2256*59bfda1fSAndroid Build Coastguard Worker {
2257*59bfda1fSAndroid Build Coastguard Worker block_t blk_addr = current_sit_addr(sbi, segno);
2258*59bfda1fSAndroid Build Coastguard Worker
2259*59bfda1fSAndroid Build Coastguard Worker ASSERT(dev_read_block(sit_blk, blk_addr) >= 0);
2260*59bfda1fSAndroid Build Coastguard Worker }
2261*59bfda1fSAndroid Build Coastguard Worker
rewrite_current_sit_page(struct f2fs_sb_info * sbi,unsigned int segno,struct f2fs_sit_block * sit_blk)2262*59bfda1fSAndroid Build Coastguard Worker void rewrite_current_sit_page(struct f2fs_sb_info *sbi,
2263*59bfda1fSAndroid Build Coastguard Worker unsigned int segno, struct f2fs_sit_block *sit_blk)
2264*59bfda1fSAndroid Build Coastguard Worker {
2265*59bfda1fSAndroid Build Coastguard Worker block_t blk_addr = current_sit_addr(sbi, segno);
2266*59bfda1fSAndroid Build Coastguard Worker
2267*59bfda1fSAndroid Build Coastguard Worker ASSERT(dev_write_block(sit_blk, blk_addr, WRITE_LIFE_NONE) >= 0);
2268*59bfda1fSAndroid Build Coastguard Worker }
2269*59bfda1fSAndroid Build Coastguard Worker
check_block_count(struct f2fs_sb_info * sbi,unsigned int segno,struct f2fs_sit_entry * raw_sit)2270*59bfda1fSAndroid Build Coastguard Worker void check_block_count(struct f2fs_sb_info *sbi,
2271*59bfda1fSAndroid Build Coastguard Worker unsigned int segno, struct f2fs_sit_entry *raw_sit)
2272*59bfda1fSAndroid Build Coastguard Worker {
2273*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sm_info *sm_info = SM_I(sbi);
2274*59bfda1fSAndroid Build Coastguard Worker unsigned int end_segno = sm_info->segment_count - 1;
2275*59bfda1fSAndroid Build Coastguard Worker int valid_blocks = 0;
2276*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
2277*59bfda1fSAndroid Build Coastguard Worker
2278*59bfda1fSAndroid Build Coastguard Worker /* check segment usage */
2279*59bfda1fSAndroid Build Coastguard Worker if (GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg)
2280*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("Invalid SIT vblocks: segno=0x%x, %u",
2281*59bfda1fSAndroid Build Coastguard Worker segno, GET_SIT_VBLOCKS(raw_sit));
2282*59bfda1fSAndroid Build Coastguard Worker
2283*59bfda1fSAndroid Build Coastguard Worker /* check boundary of a given segment number */
2284*59bfda1fSAndroid Build Coastguard Worker if (segno > end_segno)
2285*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("Invalid SEGNO: 0x%x", segno);
2286*59bfda1fSAndroid Build Coastguard Worker
2287*59bfda1fSAndroid Build Coastguard Worker /* check bitmap with valid block count */
2288*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++)
2289*59bfda1fSAndroid Build Coastguard Worker valid_blocks += get_bits_in_byte(raw_sit->valid_map[i]);
2290*59bfda1fSAndroid Build Coastguard Worker
2291*59bfda1fSAndroid Build Coastguard Worker if (GET_SIT_VBLOCKS(raw_sit) != valid_blocks)
2292*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("Wrong SIT valid blocks: segno=0x%x, %u vs. %u",
2293*59bfda1fSAndroid Build Coastguard Worker segno, GET_SIT_VBLOCKS(raw_sit), valid_blocks);
2294*59bfda1fSAndroid Build Coastguard Worker
2295*59bfda1fSAndroid Build Coastguard Worker if (GET_SIT_TYPE(raw_sit) >= NO_CHECK_TYPE)
2296*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("Wrong SIT type: segno=0x%x, %u",
2297*59bfda1fSAndroid Build Coastguard Worker segno, GET_SIT_TYPE(raw_sit));
2298*59bfda1fSAndroid Build Coastguard Worker }
2299*59bfda1fSAndroid Build Coastguard Worker
__seg_info_from_raw_sit(struct seg_entry * se,struct f2fs_sit_entry * raw_sit)2300*59bfda1fSAndroid Build Coastguard Worker void __seg_info_from_raw_sit(struct seg_entry *se,
2301*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_entry *raw_sit)
2302*59bfda1fSAndroid Build Coastguard Worker {
2303*59bfda1fSAndroid Build Coastguard Worker se->valid_blocks = GET_SIT_VBLOCKS(raw_sit);
2304*59bfda1fSAndroid Build Coastguard Worker memcpy(se->cur_valid_map, raw_sit->valid_map, SIT_VBLOCK_MAP_SIZE);
2305*59bfda1fSAndroid Build Coastguard Worker se->type = GET_SIT_TYPE(raw_sit);
2306*59bfda1fSAndroid Build Coastguard Worker se->orig_type = GET_SIT_TYPE(raw_sit);
2307*59bfda1fSAndroid Build Coastguard Worker se->mtime = le64_to_cpu(raw_sit->mtime);
2308*59bfda1fSAndroid Build Coastguard Worker }
2309*59bfda1fSAndroid Build Coastguard Worker
seg_info_from_raw_sit(struct f2fs_sb_info * sbi,struct seg_entry * se,struct f2fs_sit_entry * raw_sit)2310*59bfda1fSAndroid Build Coastguard Worker void seg_info_from_raw_sit(struct f2fs_sb_info *sbi, struct seg_entry *se,
2311*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_entry *raw_sit)
2312*59bfda1fSAndroid Build Coastguard Worker {
2313*59bfda1fSAndroid Build Coastguard Worker __seg_info_from_raw_sit(se, raw_sit);
2314*59bfda1fSAndroid Build Coastguard Worker
2315*59bfda1fSAndroid Build Coastguard Worker if (!need_fsync_data_record(sbi))
2316*59bfda1fSAndroid Build Coastguard Worker return;
2317*59bfda1fSAndroid Build Coastguard Worker se->ckpt_valid_blocks = se->valid_blocks;
2318*59bfda1fSAndroid Build Coastguard Worker memcpy(se->ckpt_valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2319*59bfda1fSAndroid Build Coastguard Worker se->ckpt_type = se->type;
2320*59bfda1fSAndroid Build Coastguard Worker }
2321*59bfda1fSAndroid Build Coastguard Worker
get_seg_entry(struct f2fs_sb_info * sbi,unsigned int segno)2322*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
2323*59bfda1fSAndroid Build Coastguard Worker unsigned int segno)
2324*59bfda1fSAndroid Build Coastguard Worker {
2325*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
2326*59bfda1fSAndroid Build Coastguard Worker return &sit_i->sentries[segno];
2327*59bfda1fSAndroid Build Coastguard Worker }
2328*59bfda1fSAndroid Build Coastguard Worker
get_seg_vblocks(struct f2fs_sb_info * sbi,struct seg_entry * se)2329*59bfda1fSAndroid Build Coastguard Worker unsigned short get_seg_vblocks(struct f2fs_sb_info *sbi, struct seg_entry *se)
2330*59bfda1fSAndroid Build Coastguard Worker {
2331*59bfda1fSAndroid Build Coastguard Worker if (!need_fsync_data_record(sbi))
2332*59bfda1fSAndroid Build Coastguard Worker return se->valid_blocks;
2333*59bfda1fSAndroid Build Coastguard Worker else
2334*59bfda1fSAndroid Build Coastguard Worker return se->ckpt_valid_blocks;
2335*59bfda1fSAndroid Build Coastguard Worker }
2336*59bfda1fSAndroid Build Coastguard Worker
get_seg_bitmap(struct f2fs_sb_info * sbi,struct seg_entry * se)2337*59bfda1fSAndroid Build Coastguard Worker unsigned char *get_seg_bitmap(struct f2fs_sb_info *sbi, struct seg_entry *se)
2338*59bfda1fSAndroid Build Coastguard Worker {
2339*59bfda1fSAndroid Build Coastguard Worker if (!need_fsync_data_record(sbi))
2340*59bfda1fSAndroid Build Coastguard Worker return se->cur_valid_map;
2341*59bfda1fSAndroid Build Coastguard Worker else
2342*59bfda1fSAndroid Build Coastguard Worker return se->ckpt_valid_map;
2343*59bfda1fSAndroid Build Coastguard Worker }
2344*59bfda1fSAndroid Build Coastguard Worker
get_seg_type(struct f2fs_sb_info * sbi,struct seg_entry * se)2345*59bfda1fSAndroid Build Coastguard Worker unsigned char get_seg_type(struct f2fs_sb_info *sbi, struct seg_entry *se)
2346*59bfda1fSAndroid Build Coastguard Worker {
2347*59bfda1fSAndroid Build Coastguard Worker if (!need_fsync_data_record(sbi))
2348*59bfda1fSAndroid Build Coastguard Worker return se->type;
2349*59bfda1fSAndroid Build Coastguard Worker else
2350*59bfda1fSAndroid Build Coastguard Worker return se->ckpt_type;
2351*59bfda1fSAndroid Build Coastguard Worker }
2352*59bfda1fSAndroid Build Coastguard Worker
get_sum_block(struct f2fs_sb_info * sbi,unsigned int segno,int * ret_type)2353*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *get_sum_block(struct f2fs_sb_info *sbi,
2354*59bfda1fSAndroid Build Coastguard Worker unsigned int segno, int *ret_type)
2355*59bfda1fSAndroid Build Coastguard Worker {
2356*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *sum_blk;
2357*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg;
2358*59bfda1fSAndroid Build Coastguard Worker int type, ret;
2359*59bfda1fSAndroid Build Coastguard Worker u64 ssa_blk;
2360*59bfda1fSAndroid Build Coastguard Worker
2361*59bfda1fSAndroid Build Coastguard Worker *ret_type= SEG_TYPE_MAX;
2362*59bfda1fSAndroid Build Coastguard Worker
2363*59bfda1fSAndroid Build Coastguard Worker ssa_blk = GET_SUM_BLKADDR(sbi, segno);
2364*59bfda1fSAndroid Build Coastguard Worker for (type = 0; type < NR_CURSEG_NODE_TYPE; type++) {
2365*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, CURSEG_HOT_NODE + type);
2366*59bfda1fSAndroid Build Coastguard Worker if (segno == curseg->segno) {
2367*59bfda1fSAndroid Build Coastguard Worker if (!IS_SUM_NODE_SEG(curseg->sum_blk)) {
2368*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("segno [0x%x] indicates a data "
2369*59bfda1fSAndroid Build Coastguard Worker "segment, but should be node",
2370*59bfda1fSAndroid Build Coastguard Worker segno);
2371*59bfda1fSAndroid Build Coastguard Worker *ret_type = -SEG_TYPE_CUR_NODE;
2372*59bfda1fSAndroid Build Coastguard Worker } else {
2373*59bfda1fSAndroid Build Coastguard Worker *ret_type = SEG_TYPE_CUR_NODE;
2374*59bfda1fSAndroid Build Coastguard Worker }
2375*59bfda1fSAndroid Build Coastguard Worker return curseg->sum_blk;
2376*59bfda1fSAndroid Build Coastguard Worker }
2377*59bfda1fSAndroid Build Coastguard Worker }
2378*59bfda1fSAndroid Build Coastguard Worker
2379*59bfda1fSAndroid Build Coastguard Worker for (type = 0; type < NR_CURSEG_DATA_TYPE; type++) {
2380*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, type);
2381*59bfda1fSAndroid Build Coastguard Worker if (segno == curseg->segno) {
2382*59bfda1fSAndroid Build Coastguard Worker if (IS_SUM_NODE_SEG(curseg->sum_blk)) {
2383*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("segno [0x%x] indicates a node "
2384*59bfda1fSAndroid Build Coastguard Worker "segment, but should be data",
2385*59bfda1fSAndroid Build Coastguard Worker segno);
2386*59bfda1fSAndroid Build Coastguard Worker *ret_type = -SEG_TYPE_CUR_DATA;
2387*59bfda1fSAndroid Build Coastguard Worker } else {
2388*59bfda1fSAndroid Build Coastguard Worker *ret_type = SEG_TYPE_CUR_DATA;
2389*59bfda1fSAndroid Build Coastguard Worker }
2390*59bfda1fSAndroid Build Coastguard Worker return curseg->sum_blk;
2391*59bfda1fSAndroid Build Coastguard Worker }
2392*59bfda1fSAndroid Build Coastguard Worker }
2393*59bfda1fSAndroid Build Coastguard Worker
2394*59bfda1fSAndroid Build Coastguard Worker sum_blk = calloc(F2FS_BLKSIZE, 1);
2395*59bfda1fSAndroid Build Coastguard Worker ASSERT(sum_blk);
2396*59bfda1fSAndroid Build Coastguard Worker
2397*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(sum_blk, ssa_blk);
2398*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2399*59bfda1fSAndroid Build Coastguard Worker
2400*59bfda1fSAndroid Build Coastguard Worker if (IS_SUM_NODE_SEG(sum_blk))
2401*59bfda1fSAndroid Build Coastguard Worker *ret_type = SEG_TYPE_NODE;
2402*59bfda1fSAndroid Build Coastguard Worker else if (IS_SUM_DATA_SEG(sum_blk))
2403*59bfda1fSAndroid Build Coastguard Worker *ret_type = SEG_TYPE_DATA;
2404*59bfda1fSAndroid Build Coastguard Worker
2405*59bfda1fSAndroid Build Coastguard Worker return sum_blk;
2406*59bfda1fSAndroid Build Coastguard Worker }
2407*59bfda1fSAndroid Build Coastguard Worker
get_sum_entry(struct f2fs_sb_info * sbi,u32 blk_addr,struct f2fs_summary * sum_entry)2408*59bfda1fSAndroid Build Coastguard Worker int get_sum_entry(struct f2fs_sb_info *sbi, u32 blk_addr,
2409*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary *sum_entry)
2410*59bfda1fSAndroid Build Coastguard Worker {
2411*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *sum_blk;
2412*59bfda1fSAndroid Build Coastguard Worker u32 segno, offset;
2413*59bfda1fSAndroid Build Coastguard Worker int type;
2414*59bfda1fSAndroid Build Coastguard Worker
2415*59bfda1fSAndroid Build Coastguard Worker segno = GET_SEGNO(sbi, blk_addr);
2416*59bfda1fSAndroid Build Coastguard Worker offset = OFFSET_IN_SEG(sbi, blk_addr);
2417*59bfda1fSAndroid Build Coastguard Worker
2418*59bfda1fSAndroid Build Coastguard Worker sum_blk = get_sum_block(sbi, segno, &type);
2419*59bfda1fSAndroid Build Coastguard Worker memcpy(sum_entry, &(sum_blk->entries[offset]),
2420*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_summary));
2421*59bfda1fSAndroid Build Coastguard Worker if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
2422*59bfda1fSAndroid Build Coastguard Worker type == SEG_TYPE_MAX)
2423*59bfda1fSAndroid Build Coastguard Worker free(sum_blk);
2424*59bfda1fSAndroid Build Coastguard Worker return type;
2425*59bfda1fSAndroid Build Coastguard Worker }
2426*59bfda1fSAndroid Build Coastguard Worker
get_nat_entry(struct f2fs_sb_info * sbi,nid_t nid,struct f2fs_nat_entry * raw_nat)2427*59bfda1fSAndroid Build Coastguard Worker static void get_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
2428*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_entry *raw_nat)
2429*59bfda1fSAndroid Build Coastguard Worker {
2430*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block;
2431*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_addr;
2432*59bfda1fSAndroid Build Coastguard Worker int entry_off;
2433*59bfda1fSAndroid Build Coastguard Worker int ret;
2434*59bfda1fSAndroid Build Coastguard Worker
2435*59bfda1fSAndroid Build Coastguard Worker if (lookup_nat_in_journal(sbi, nid, raw_nat) >= 0)
2436*59bfda1fSAndroid Build Coastguard Worker return;
2437*59bfda1fSAndroid Build Coastguard Worker
2438*59bfda1fSAndroid Build Coastguard Worker nat_block = (struct f2fs_nat_block *)calloc(F2FS_BLKSIZE, 1);
2439*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_block);
2440*59bfda1fSAndroid Build Coastguard Worker
2441*59bfda1fSAndroid Build Coastguard Worker entry_off = nid % NAT_ENTRY_PER_BLOCK;
2442*59bfda1fSAndroid Build Coastguard Worker block_addr = current_nat_addr(sbi, nid, NULL);
2443*59bfda1fSAndroid Build Coastguard Worker
2444*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, block_addr);
2445*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2446*59bfda1fSAndroid Build Coastguard Worker
2447*59bfda1fSAndroid Build Coastguard Worker memcpy(raw_nat, &nat_block->entries[entry_off],
2448*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_nat_entry));
2449*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
2450*59bfda1fSAndroid Build Coastguard Worker }
2451*59bfda1fSAndroid Build Coastguard Worker
update_data_blkaddr(struct f2fs_sb_info * sbi,nid_t nid,u16 ofs_in_node,block_t newaddr,struct f2fs_node * node_blk)2452*59bfda1fSAndroid Build Coastguard Worker void update_data_blkaddr(struct f2fs_sb_info *sbi, nid_t nid,
2453*59bfda1fSAndroid Build Coastguard Worker u16 ofs_in_node, block_t newaddr, struct f2fs_node *node_blk)
2454*59bfda1fSAndroid Build Coastguard Worker {
2455*59bfda1fSAndroid Build Coastguard Worker struct node_info ni;
2456*59bfda1fSAndroid Build Coastguard Worker block_t oldaddr, startaddr, endaddr;
2457*59bfda1fSAndroid Build Coastguard Worker bool node_blk_alloced = false;
2458*59bfda1fSAndroid Build Coastguard Worker int ret;
2459*59bfda1fSAndroid Build Coastguard Worker
2460*59bfda1fSAndroid Build Coastguard Worker if (node_blk == NULL) {
2461*59bfda1fSAndroid Build Coastguard Worker node_blk = (struct f2fs_node *)calloc(F2FS_BLKSIZE, 1);
2462*59bfda1fSAndroid Build Coastguard Worker ASSERT(node_blk);
2463*59bfda1fSAndroid Build Coastguard Worker
2464*59bfda1fSAndroid Build Coastguard Worker get_node_info(sbi, nid, &ni);
2465*59bfda1fSAndroid Build Coastguard Worker
2466*59bfda1fSAndroid Build Coastguard Worker /* read node_block */
2467*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(node_blk, ni.blk_addr);
2468*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2469*59bfda1fSAndroid Build Coastguard Worker node_blk_alloced = true;
2470*59bfda1fSAndroid Build Coastguard Worker }
2471*59bfda1fSAndroid Build Coastguard Worker
2472*59bfda1fSAndroid Build Coastguard Worker /* check its block address */
2473*59bfda1fSAndroid Build Coastguard Worker if (IS_INODE(node_blk)) {
2474*59bfda1fSAndroid Build Coastguard Worker int ofs = get_extra_isize(node_blk);
2475*59bfda1fSAndroid Build Coastguard Worker
2476*59bfda1fSAndroid Build Coastguard Worker oldaddr = le32_to_cpu(node_blk->i.i_addr[ofs + ofs_in_node]);
2477*59bfda1fSAndroid Build Coastguard Worker node_blk->i.i_addr[ofs + ofs_in_node] = cpu_to_le32(newaddr);
2478*59bfda1fSAndroid Build Coastguard Worker if (node_blk_alloced) {
2479*59bfda1fSAndroid Build Coastguard Worker ret = update_inode(sbi, node_blk, &ni.blk_addr);
2480*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2481*59bfda1fSAndroid Build Coastguard Worker }
2482*59bfda1fSAndroid Build Coastguard Worker } else {
2483*59bfda1fSAndroid Build Coastguard Worker oldaddr = le32_to_cpu(node_blk->dn.addr[ofs_in_node]);
2484*59bfda1fSAndroid Build Coastguard Worker node_blk->dn.addr[ofs_in_node] = cpu_to_le32(newaddr);
2485*59bfda1fSAndroid Build Coastguard Worker if (node_blk_alloced) {
2486*59bfda1fSAndroid Build Coastguard Worker ret = update_block(sbi, node_blk, &ni.blk_addr, NULL);
2487*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2488*59bfda1fSAndroid Build Coastguard Worker }
2489*59bfda1fSAndroid Build Coastguard Worker
2490*59bfda1fSAndroid Build Coastguard Worker /* change node_blk with inode to update extent cache entry */
2491*59bfda1fSAndroid Build Coastguard Worker get_node_info(sbi, le32_to_cpu(F2FS_NODE_FOOTER(node_blk)->ino),
2492*59bfda1fSAndroid Build Coastguard Worker &ni);
2493*59bfda1fSAndroid Build Coastguard Worker
2494*59bfda1fSAndroid Build Coastguard Worker /* read inode block */
2495*59bfda1fSAndroid Build Coastguard Worker if (!node_blk_alloced) {
2496*59bfda1fSAndroid Build Coastguard Worker node_blk = (struct f2fs_node *)calloc(F2FS_BLKSIZE, 1);
2497*59bfda1fSAndroid Build Coastguard Worker ASSERT(node_blk);
2498*59bfda1fSAndroid Build Coastguard Worker
2499*59bfda1fSAndroid Build Coastguard Worker node_blk_alloced = true;
2500*59bfda1fSAndroid Build Coastguard Worker }
2501*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(node_blk, ni.blk_addr);
2502*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2503*59bfda1fSAndroid Build Coastguard Worker }
2504*59bfda1fSAndroid Build Coastguard Worker
2505*59bfda1fSAndroid Build Coastguard Worker /* check extent cache entry */
2506*59bfda1fSAndroid Build Coastguard Worker startaddr = le32_to_cpu(node_blk->i.i_ext.blk_addr);
2507*59bfda1fSAndroid Build Coastguard Worker endaddr = startaddr + le32_to_cpu(node_blk->i.i_ext.len);
2508*59bfda1fSAndroid Build Coastguard Worker if (oldaddr >= startaddr && oldaddr < endaddr) {
2509*59bfda1fSAndroid Build Coastguard Worker node_blk->i.i_ext.len = 0;
2510*59bfda1fSAndroid Build Coastguard Worker
2511*59bfda1fSAndroid Build Coastguard Worker /* update inode block */
2512*59bfda1fSAndroid Build Coastguard Worker if (node_blk_alloced)
2513*59bfda1fSAndroid Build Coastguard Worker ASSERT(update_inode(sbi, node_blk, &ni.blk_addr) >= 0);
2514*59bfda1fSAndroid Build Coastguard Worker }
2515*59bfda1fSAndroid Build Coastguard Worker
2516*59bfda1fSAndroid Build Coastguard Worker if (node_blk_alloced)
2517*59bfda1fSAndroid Build Coastguard Worker free(node_blk);
2518*59bfda1fSAndroid Build Coastguard Worker }
2519*59bfda1fSAndroid Build Coastguard Worker
update_nat_blkaddr(struct f2fs_sb_info * sbi,nid_t ino,nid_t nid,block_t newaddr)2520*59bfda1fSAndroid Build Coastguard Worker void update_nat_blkaddr(struct f2fs_sb_info *sbi, nid_t ino,
2521*59bfda1fSAndroid Build Coastguard Worker nid_t nid, block_t newaddr)
2522*59bfda1fSAndroid Build Coastguard Worker {
2523*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block = NULL;
2524*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2525*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
2526*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_entry *entry;
2527*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_addr;
2528*59bfda1fSAndroid Build Coastguard Worker int entry_off;
2529*59bfda1fSAndroid Build Coastguard Worker int ret, i;
2530*59bfda1fSAndroid Build Coastguard Worker
2531*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nats_in_cursum(journal); i++) {
2532*59bfda1fSAndroid Build Coastguard Worker if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
2533*59bfda1fSAndroid Build Coastguard Worker entry = &nat_in_journal(journal, i);
2534*59bfda1fSAndroid Build Coastguard Worker entry->block_addr = cpu_to_le32(newaddr);
2535*59bfda1fSAndroid Build Coastguard Worker if (ino)
2536*59bfda1fSAndroid Build Coastguard Worker entry->ino = cpu_to_le32(ino);
2537*59bfda1fSAndroid Build Coastguard Worker MSG(0, "update nat(nid:%d) blkaddr [0x%x] in journal\n",
2538*59bfda1fSAndroid Build Coastguard Worker nid, newaddr);
2539*59bfda1fSAndroid Build Coastguard Worker goto update_cache;
2540*59bfda1fSAndroid Build Coastguard Worker }
2541*59bfda1fSAndroid Build Coastguard Worker }
2542*59bfda1fSAndroid Build Coastguard Worker
2543*59bfda1fSAndroid Build Coastguard Worker nat_block = (struct f2fs_nat_block *)calloc(F2FS_BLKSIZE, 1);
2544*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_block);
2545*59bfda1fSAndroid Build Coastguard Worker
2546*59bfda1fSAndroid Build Coastguard Worker entry_off = nid % NAT_ENTRY_PER_BLOCK;
2547*59bfda1fSAndroid Build Coastguard Worker block_addr = current_nat_addr(sbi, nid, NULL);
2548*59bfda1fSAndroid Build Coastguard Worker
2549*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, block_addr);
2550*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2551*59bfda1fSAndroid Build Coastguard Worker
2552*59bfda1fSAndroid Build Coastguard Worker entry = &nat_block->entries[entry_off];
2553*59bfda1fSAndroid Build Coastguard Worker if (ino)
2554*59bfda1fSAndroid Build Coastguard Worker entry->ino = cpu_to_le32(ino);
2555*59bfda1fSAndroid Build Coastguard Worker entry->block_addr = cpu_to_le32(newaddr);
2556*59bfda1fSAndroid Build Coastguard Worker
2557*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(nat_block, block_addr, WRITE_LIFE_NONE);
2558*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2559*59bfda1fSAndroid Build Coastguard Worker update_cache:
2560*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK)
2561*59bfda1fSAndroid Build Coastguard Worker F2FS_FSCK(sbi)->entries[nid] = *entry;
2562*59bfda1fSAndroid Build Coastguard Worker
2563*59bfda1fSAndroid Build Coastguard Worker if (nat_block)
2564*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
2565*59bfda1fSAndroid Build Coastguard Worker }
2566*59bfda1fSAndroid Build Coastguard Worker
get_node_info(struct f2fs_sb_info * sbi,nid_t nid,struct node_info * ni)2567*59bfda1fSAndroid Build Coastguard Worker void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
2568*59bfda1fSAndroid Build Coastguard Worker {
2569*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_entry raw_nat;
2570*59bfda1fSAndroid Build Coastguard Worker
2571*59bfda1fSAndroid Build Coastguard Worker ni->nid = nid;
2572*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK && F2FS_FSCK(sbi)->nr_nat_entries) {
2573*59bfda1fSAndroid Build Coastguard Worker node_info_from_raw_nat(ni, &(F2FS_FSCK(sbi)->entries[nid]));
2574*59bfda1fSAndroid Build Coastguard Worker if (ni->blk_addr)
2575*59bfda1fSAndroid Build Coastguard Worker return;
2576*59bfda1fSAndroid Build Coastguard Worker /* nat entry is not cached, read it */
2577*59bfda1fSAndroid Build Coastguard Worker }
2578*59bfda1fSAndroid Build Coastguard Worker
2579*59bfda1fSAndroid Build Coastguard Worker get_nat_entry(sbi, nid, &raw_nat);
2580*59bfda1fSAndroid Build Coastguard Worker node_info_from_raw_nat(ni, &raw_nat);
2581*59bfda1fSAndroid Build Coastguard Worker }
2582*59bfda1fSAndroid Build Coastguard Worker
build_sit_entries(struct f2fs_sb_info * sbi)2583*59bfda1fSAndroid Build Coastguard Worker static int build_sit_entries(struct f2fs_sb_info *sbi)
2584*59bfda1fSAndroid Build Coastguard Worker {
2585*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
2586*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2587*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
2588*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_block *sit_blk;
2589*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2590*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_entry sit;
2591*59bfda1fSAndroid Build Coastguard Worker int sit_blk_cnt = SIT_BLK_CNT(sbi);
2592*59bfda1fSAndroid Build Coastguard Worker unsigned int i, segno, end;
2593*59bfda1fSAndroid Build Coastguard Worker unsigned int readed, start_blk = 0;
2594*59bfda1fSAndroid Build Coastguard Worker
2595*59bfda1fSAndroid Build Coastguard Worker sit_blk = calloc(F2FS_BLKSIZE, 1);
2596*59bfda1fSAndroid Build Coastguard Worker if (!sit_blk) {
2597*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Calloc failed for build_sit_entries!\n");
2598*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
2599*59bfda1fSAndroid Build Coastguard Worker }
2600*59bfda1fSAndroid Build Coastguard Worker
2601*59bfda1fSAndroid Build Coastguard Worker do {
2602*59bfda1fSAndroid Build Coastguard Worker readed = f2fs_ra_meta_pages(sbi, start_blk, MAX_RA_BLOCKS,
2603*59bfda1fSAndroid Build Coastguard Worker META_SIT);
2604*59bfda1fSAndroid Build Coastguard Worker
2605*59bfda1fSAndroid Build Coastguard Worker segno = start_blk * sit_i->sents_per_block;
2606*59bfda1fSAndroid Build Coastguard Worker end = (start_blk + readed) * sit_i->sents_per_block;
2607*59bfda1fSAndroid Build Coastguard Worker
2608*59bfda1fSAndroid Build Coastguard Worker for (; segno < end && segno < MAIN_SEGS(sbi); segno++) {
2609*59bfda1fSAndroid Build Coastguard Worker se = &sit_i->sentries[segno];
2610*59bfda1fSAndroid Build Coastguard Worker
2611*59bfda1fSAndroid Build Coastguard Worker get_current_sit_page(sbi, segno, sit_blk);
2612*59bfda1fSAndroid Build Coastguard Worker sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2613*59bfda1fSAndroid Build Coastguard Worker
2614*59bfda1fSAndroid Build Coastguard Worker check_block_count(sbi, segno, &sit);
2615*59bfda1fSAndroid Build Coastguard Worker seg_info_from_raw_sit(sbi, se, &sit);
2616*59bfda1fSAndroid Build Coastguard Worker if (se->valid_blocks == 0x0 &&
2617*59bfda1fSAndroid Build Coastguard Worker is_usable_seg(sbi, segno) &&
2618*59bfda1fSAndroid Build Coastguard Worker !IS_CUR_SEGNO(sbi, segno))
2619*59bfda1fSAndroid Build Coastguard Worker SM_I(sbi)->free_segments++;
2620*59bfda1fSAndroid Build Coastguard Worker }
2621*59bfda1fSAndroid Build Coastguard Worker start_blk += readed;
2622*59bfda1fSAndroid Build Coastguard Worker } while (start_blk < sit_blk_cnt);
2623*59bfda1fSAndroid Build Coastguard Worker
2624*59bfda1fSAndroid Build Coastguard Worker
2625*59bfda1fSAndroid Build Coastguard Worker free(sit_blk);
2626*59bfda1fSAndroid Build Coastguard Worker
2627*59bfda1fSAndroid Build Coastguard Worker if (sits_in_cursum(journal) > SIT_JOURNAL_ENTRIES) {
2628*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tError: build_sit_entries truncate n_sits(%u) to "
2629*59bfda1fSAndroid Build Coastguard Worker "SIT_JOURNAL_ENTRIES(%zu)\n",
2630*59bfda1fSAndroid Build Coastguard Worker sits_in_cursum(journal), SIT_JOURNAL_ENTRIES);
2631*59bfda1fSAndroid Build Coastguard Worker journal->n_sits = cpu_to_le16(SIT_JOURNAL_ENTRIES);
2632*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
2633*59bfda1fSAndroid Build Coastguard Worker }
2634*59bfda1fSAndroid Build Coastguard Worker
2635*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < sits_in_cursum(journal); i++) {
2636*59bfda1fSAndroid Build Coastguard Worker segno = le32_to_cpu(segno_in_journal(journal, i));
2637*59bfda1fSAndroid Build Coastguard Worker
2638*59bfda1fSAndroid Build Coastguard Worker if (segno >= MAIN_SEGS(sbi)) {
2639*59bfda1fSAndroid Build Coastguard Worker MSG(0, "\tError: build_sit_entries: segno(%u) is invalid!!!\n", segno);
2640*59bfda1fSAndroid Build Coastguard Worker journal->n_sits = cpu_to_le16(i);
2641*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
2642*59bfda1fSAndroid Build Coastguard Worker continue;
2643*59bfda1fSAndroid Build Coastguard Worker }
2644*59bfda1fSAndroid Build Coastguard Worker
2645*59bfda1fSAndroid Build Coastguard Worker se = &sit_i->sentries[segno];
2646*59bfda1fSAndroid Build Coastguard Worker sit = sit_in_journal(journal, i);
2647*59bfda1fSAndroid Build Coastguard Worker
2648*59bfda1fSAndroid Build Coastguard Worker check_block_count(sbi, segno, &sit);
2649*59bfda1fSAndroid Build Coastguard Worker seg_info_from_raw_sit(sbi, se, &sit);
2650*59bfda1fSAndroid Build Coastguard Worker }
2651*59bfda1fSAndroid Build Coastguard Worker return 0;
2652*59bfda1fSAndroid Build Coastguard Worker }
2653*59bfda1fSAndroid Build Coastguard Worker
early_build_segment_manager(struct f2fs_sb_info * sbi)2654*59bfda1fSAndroid Build Coastguard Worker static int early_build_segment_manager(struct f2fs_sb_info *sbi)
2655*59bfda1fSAndroid Build Coastguard Worker {
2656*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2657*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2658*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sm_info *sm_info;
2659*59bfda1fSAndroid Build Coastguard Worker
2660*59bfda1fSAndroid Build Coastguard Worker sm_info = malloc(sizeof(struct f2fs_sm_info));
2661*59bfda1fSAndroid Build Coastguard Worker if (!sm_info) {
2662*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: Malloc failed for build_segment_manager!\n");
2663*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
2664*59bfda1fSAndroid Build Coastguard Worker }
2665*59bfda1fSAndroid Build Coastguard Worker
2666*59bfda1fSAndroid Build Coastguard Worker /* init sm info */
2667*59bfda1fSAndroid Build Coastguard Worker sbi->sm_info = sm_info;
2668*59bfda1fSAndroid Build Coastguard Worker sm_info->seg0_blkaddr = get_sb(segment0_blkaddr);
2669*59bfda1fSAndroid Build Coastguard Worker sm_info->main_blkaddr = get_sb(main_blkaddr);
2670*59bfda1fSAndroid Build Coastguard Worker sm_info->segment_count = get_sb(segment_count);
2671*59bfda1fSAndroid Build Coastguard Worker sm_info->reserved_segments = get_cp(rsvd_segment_count);
2672*59bfda1fSAndroid Build Coastguard Worker sm_info->ovp_segments = get_cp(overprov_segment_count);
2673*59bfda1fSAndroid Build Coastguard Worker sm_info->main_segments = get_sb(segment_count_main);
2674*59bfda1fSAndroid Build Coastguard Worker sm_info->ssa_blkaddr = get_sb(ssa_blkaddr);
2675*59bfda1fSAndroid Build Coastguard Worker sm_info->free_segments = 0;
2676*59bfda1fSAndroid Build Coastguard Worker
2677*59bfda1fSAndroid Build Coastguard Worker if (build_sit_info(sbi) || build_curseg(sbi)) {
2678*59bfda1fSAndroid Build Coastguard Worker free(sm_info);
2679*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
2680*59bfda1fSAndroid Build Coastguard Worker }
2681*59bfda1fSAndroid Build Coastguard Worker
2682*59bfda1fSAndroid Build Coastguard Worker return 0;
2683*59bfda1fSAndroid Build Coastguard Worker }
2684*59bfda1fSAndroid Build Coastguard Worker
late_build_segment_manager(struct f2fs_sb_info * sbi)2685*59bfda1fSAndroid Build Coastguard Worker static int late_build_segment_manager(struct f2fs_sb_info *sbi)
2686*59bfda1fSAndroid Build Coastguard Worker {
2687*59bfda1fSAndroid Build Coastguard Worker if (sbi->seg_manager_done)
2688*59bfda1fSAndroid Build Coastguard Worker return 1; /* this function was already called */
2689*59bfda1fSAndroid Build Coastguard Worker
2690*59bfda1fSAndroid Build Coastguard Worker sbi->seg_manager_done = true;
2691*59bfda1fSAndroid Build Coastguard Worker if (build_sit_entries(sbi)) {
2692*59bfda1fSAndroid Build Coastguard Worker free (sbi->sm_info);
2693*59bfda1fSAndroid Build Coastguard Worker return -ENOMEM;
2694*59bfda1fSAndroid Build Coastguard Worker }
2695*59bfda1fSAndroid Build Coastguard Worker
2696*59bfda1fSAndroid Build Coastguard Worker return 0;
2697*59bfda1fSAndroid Build Coastguard Worker }
2698*59bfda1fSAndroid Build Coastguard Worker
build_sit_area_bitmap(struct f2fs_sb_info * sbi)2699*59bfda1fSAndroid Build Coastguard Worker void build_sit_area_bitmap(struct f2fs_sb_info *sbi)
2700*59bfda1fSAndroid Build Coastguard Worker {
2701*59bfda1fSAndroid Build Coastguard Worker struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2702*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sm_info *sm_i = SM_I(sbi);
2703*59bfda1fSAndroid Build Coastguard Worker unsigned int segno = 0;
2704*59bfda1fSAndroid Build Coastguard Worker char *ptr = NULL;
2705*59bfda1fSAndroid Build Coastguard Worker u32 sum_vblocks = 0;
2706*59bfda1fSAndroid Build Coastguard Worker u32 free_segs = 0;
2707*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2708*59bfda1fSAndroid Build Coastguard Worker
2709*59bfda1fSAndroid Build Coastguard Worker fsck->sit_area_bitmap_sz = sm_i->main_segments * SIT_VBLOCK_MAP_SIZE;
2710*59bfda1fSAndroid Build Coastguard Worker fsck->sit_area_bitmap = calloc(1, fsck->sit_area_bitmap_sz);
2711*59bfda1fSAndroid Build Coastguard Worker ASSERT(fsck->sit_area_bitmap);
2712*59bfda1fSAndroid Build Coastguard Worker ptr = fsck->sit_area_bitmap;
2713*59bfda1fSAndroid Build Coastguard Worker
2714*59bfda1fSAndroid Build Coastguard Worker ASSERT(fsck->sit_area_bitmap_sz == fsck->main_area_bitmap_sz);
2715*59bfda1fSAndroid Build Coastguard Worker
2716*59bfda1fSAndroid Build Coastguard Worker for (segno = 0; segno < MAIN_SEGS(sbi); segno++) {
2717*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
2718*59bfda1fSAndroid Build Coastguard Worker
2719*59bfda1fSAndroid Build Coastguard Worker memcpy(ptr, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2720*59bfda1fSAndroid Build Coastguard Worker ptr += SIT_VBLOCK_MAP_SIZE;
2721*59bfda1fSAndroid Build Coastguard Worker
2722*59bfda1fSAndroid Build Coastguard Worker if (se->valid_blocks == 0x0 && is_usable_seg(sbi, segno)) {
2723*59bfda1fSAndroid Build Coastguard Worker if (!IS_CUR_SEGNO(sbi, segno))
2724*59bfda1fSAndroid Build Coastguard Worker free_segs++;
2725*59bfda1fSAndroid Build Coastguard Worker } else {
2726*59bfda1fSAndroid Build Coastguard Worker sum_vblocks += se->valid_blocks;
2727*59bfda1fSAndroid Build Coastguard Worker }
2728*59bfda1fSAndroid Build Coastguard Worker }
2729*59bfda1fSAndroid Build Coastguard Worker fsck->chk.sit_valid_blocks = sum_vblocks;
2730*59bfda1fSAndroid Build Coastguard Worker fsck->chk.sit_free_segs = free_segs;
2731*59bfda1fSAndroid Build Coastguard Worker
2732*59bfda1fSAndroid Build Coastguard Worker DBG(1, "Blocks [0x%x : %d] Free Segs [0x%x : %d]\n\n",
2733*59bfda1fSAndroid Build Coastguard Worker sum_vblocks, sum_vblocks,
2734*59bfda1fSAndroid Build Coastguard Worker free_segs, free_segs);
2735*59bfda1fSAndroid Build Coastguard Worker }
2736*59bfda1fSAndroid Build Coastguard Worker
rewrite_sit_area_bitmap(struct f2fs_sb_info * sbi)2737*59bfda1fSAndroid Build Coastguard Worker void rewrite_sit_area_bitmap(struct f2fs_sb_info *sbi)
2738*59bfda1fSAndroid Build Coastguard Worker {
2739*59bfda1fSAndroid Build Coastguard Worker struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2740*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2741*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
2742*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_block *sit_blk;
2743*59bfda1fSAndroid Build Coastguard Worker unsigned int segno = 0;
2744*59bfda1fSAndroid Build Coastguard Worker struct f2fs_summary_block *sum = curseg->sum_blk;
2745*59bfda1fSAndroid Build Coastguard Worker char *ptr = NULL;
2746*59bfda1fSAndroid Build Coastguard Worker
2747*59bfda1fSAndroid Build Coastguard Worker sit_blk = calloc(F2FS_BLKSIZE, 1);
2748*59bfda1fSAndroid Build Coastguard Worker ASSERT(sit_blk);
2749*59bfda1fSAndroid Build Coastguard Worker /* remove sit journal */
2750*59bfda1fSAndroid Build Coastguard Worker F2FS_SUMMARY_BLOCK_JOURNAL(sum)->n_sits = 0;
2751*59bfda1fSAndroid Build Coastguard Worker
2752*59bfda1fSAndroid Build Coastguard Worker ptr = fsck->main_area_bitmap;
2753*59bfda1fSAndroid Build Coastguard Worker
2754*59bfda1fSAndroid Build Coastguard Worker for (segno = 0; segno < MAIN_SEGS(sbi); segno++) {
2755*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_entry *sit;
2756*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2757*59bfda1fSAndroid Build Coastguard Worker u16 valid_blocks = 0;
2758*59bfda1fSAndroid Build Coastguard Worker u16 type;
2759*59bfda1fSAndroid Build Coastguard Worker int i;
2760*59bfda1fSAndroid Build Coastguard Worker
2761*59bfda1fSAndroid Build Coastguard Worker get_current_sit_page(sbi, segno, sit_blk);
2762*59bfda1fSAndroid Build Coastguard Worker sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2763*59bfda1fSAndroid Build Coastguard Worker memcpy(sit->valid_map, ptr, SIT_VBLOCK_MAP_SIZE);
2764*59bfda1fSAndroid Build Coastguard Worker
2765*59bfda1fSAndroid Build Coastguard Worker /* update valid block count */
2766*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < SIT_VBLOCK_MAP_SIZE; i++)
2767*59bfda1fSAndroid Build Coastguard Worker valid_blocks += get_bits_in_byte(sit->valid_map[i]);
2768*59bfda1fSAndroid Build Coastguard Worker
2769*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
2770*59bfda1fSAndroid Build Coastguard Worker memcpy(se->cur_valid_map, ptr, SIT_VBLOCK_MAP_SIZE);
2771*59bfda1fSAndroid Build Coastguard Worker se->valid_blocks = valid_blocks;
2772*59bfda1fSAndroid Build Coastguard Worker type = se->type;
2773*59bfda1fSAndroid Build Coastguard Worker if (type >= NO_CHECK_TYPE) {
2774*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("Invalid type and valid blocks=%x,%x",
2775*59bfda1fSAndroid Build Coastguard Worker segno, valid_blocks);
2776*59bfda1fSAndroid Build Coastguard Worker type = 0;
2777*59bfda1fSAndroid Build Coastguard Worker }
2778*59bfda1fSAndroid Build Coastguard Worker sit->vblocks = cpu_to_le16((type << SIT_VBLOCKS_SHIFT) |
2779*59bfda1fSAndroid Build Coastguard Worker valid_blocks);
2780*59bfda1fSAndroid Build Coastguard Worker rewrite_current_sit_page(sbi, segno, sit_blk);
2781*59bfda1fSAndroid Build Coastguard Worker
2782*59bfda1fSAndroid Build Coastguard Worker ptr += SIT_VBLOCK_MAP_SIZE;
2783*59bfda1fSAndroid Build Coastguard Worker }
2784*59bfda1fSAndroid Build Coastguard Worker
2785*59bfda1fSAndroid Build Coastguard Worker free(sit_blk);
2786*59bfda1fSAndroid Build Coastguard Worker }
2787*59bfda1fSAndroid Build Coastguard Worker
flush_sit_journal_entries(struct f2fs_sb_info * sbi)2788*59bfda1fSAndroid Build Coastguard Worker int flush_sit_journal_entries(struct f2fs_sb_info *sbi)
2789*59bfda1fSAndroid Build Coastguard Worker {
2790*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2791*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
2792*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
2793*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_block *sit_blk;
2794*59bfda1fSAndroid Build Coastguard Worker unsigned int segno;
2795*59bfda1fSAndroid Build Coastguard Worker int i;
2796*59bfda1fSAndroid Build Coastguard Worker
2797*59bfda1fSAndroid Build Coastguard Worker sit_blk = calloc(F2FS_BLKSIZE, 1);
2798*59bfda1fSAndroid Build Coastguard Worker ASSERT(sit_blk);
2799*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < sits_in_cursum(journal); i++) {
2800*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_entry *sit;
2801*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2802*59bfda1fSAndroid Build Coastguard Worker
2803*59bfda1fSAndroid Build Coastguard Worker segno = segno_in_journal(journal, i);
2804*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
2805*59bfda1fSAndroid Build Coastguard Worker
2806*59bfda1fSAndroid Build Coastguard Worker get_current_sit_page(sbi, segno, sit_blk);
2807*59bfda1fSAndroid Build Coastguard Worker sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2808*59bfda1fSAndroid Build Coastguard Worker
2809*59bfda1fSAndroid Build Coastguard Worker memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2810*59bfda1fSAndroid Build Coastguard Worker sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2811*59bfda1fSAndroid Build Coastguard Worker se->valid_blocks);
2812*59bfda1fSAndroid Build Coastguard Worker sit->mtime = cpu_to_le64(se->mtime);
2813*59bfda1fSAndroid Build Coastguard Worker
2814*59bfda1fSAndroid Build Coastguard Worker rewrite_current_sit_page(sbi, segno, sit_blk);
2815*59bfda1fSAndroid Build Coastguard Worker }
2816*59bfda1fSAndroid Build Coastguard Worker
2817*59bfda1fSAndroid Build Coastguard Worker free(sit_blk);
2818*59bfda1fSAndroid Build Coastguard Worker journal->n_sits = 0;
2819*59bfda1fSAndroid Build Coastguard Worker return i;
2820*59bfda1fSAndroid Build Coastguard Worker }
2821*59bfda1fSAndroid Build Coastguard Worker
flush_nat_journal_entries(struct f2fs_sb_info * sbi)2822*59bfda1fSAndroid Build Coastguard Worker int flush_nat_journal_entries(struct f2fs_sb_info *sbi)
2823*59bfda1fSAndroid Build Coastguard Worker {
2824*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2825*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
2826*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block;
2827*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_addr;
2828*59bfda1fSAndroid Build Coastguard Worker int entry_off;
2829*59bfda1fSAndroid Build Coastguard Worker nid_t nid;
2830*59bfda1fSAndroid Build Coastguard Worker int ret;
2831*59bfda1fSAndroid Build Coastguard Worker int i = 0;
2832*59bfda1fSAndroid Build Coastguard Worker
2833*59bfda1fSAndroid Build Coastguard Worker nat_block = (struct f2fs_nat_block *)calloc(F2FS_BLKSIZE, 1);
2834*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_block);
2835*59bfda1fSAndroid Build Coastguard Worker next:
2836*59bfda1fSAndroid Build Coastguard Worker if (i >= nats_in_cursum(journal)) {
2837*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
2838*59bfda1fSAndroid Build Coastguard Worker journal->n_nats = 0;
2839*59bfda1fSAndroid Build Coastguard Worker return i;
2840*59bfda1fSAndroid Build Coastguard Worker }
2841*59bfda1fSAndroid Build Coastguard Worker
2842*59bfda1fSAndroid Build Coastguard Worker nid = le32_to_cpu(nid_in_journal(journal, i));
2843*59bfda1fSAndroid Build Coastguard Worker
2844*59bfda1fSAndroid Build Coastguard Worker entry_off = nid % NAT_ENTRY_PER_BLOCK;
2845*59bfda1fSAndroid Build Coastguard Worker block_addr = current_nat_addr(sbi, nid, NULL);
2846*59bfda1fSAndroid Build Coastguard Worker
2847*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, block_addr);
2848*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2849*59bfda1fSAndroid Build Coastguard Worker
2850*59bfda1fSAndroid Build Coastguard Worker memcpy(&nat_block->entries[entry_off], &nat_in_journal(journal, i),
2851*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_nat_entry));
2852*59bfda1fSAndroid Build Coastguard Worker
2853*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(nat_block, block_addr, WRITE_LIFE_NONE);
2854*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
2855*59bfda1fSAndroid Build Coastguard Worker i++;
2856*59bfda1fSAndroid Build Coastguard Worker goto next;
2857*59bfda1fSAndroid Build Coastguard Worker }
2858*59bfda1fSAndroid Build Coastguard Worker
flush_journal_entries(struct f2fs_sb_info * sbi)2859*59bfda1fSAndroid Build Coastguard Worker void flush_journal_entries(struct f2fs_sb_info *sbi)
2860*59bfda1fSAndroid Build Coastguard Worker {
2861*59bfda1fSAndroid Build Coastguard Worker int n_nats = flush_nat_journal_entries(sbi);
2862*59bfda1fSAndroid Build Coastguard Worker int n_sits = flush_sit_journal_entries(sbi);
2863*59bfda1fSAndroid Build Coastguard Worker
2864*59bfda1fSAndroid Build Coastguard Worker if (n_nats || n_sits) {
2865*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: flush_journal_entries() n_nats: %d, n_sits: %d\n",
2866*59bfda1fSAndroid Build Coastguard Worker n_nats, n_sits);
2867*59bfda1fSAndroid Build Coastguard Worker write_checkpoints(sbi);
2868*59bfda1fSAndroid Build Coastguard Worker }
2869*59bfda1fSAndroid Build Coastguard Worker }
2870*59bfda1fSAndroid Build Coastguard Worker
flush_sit_entries(struct f2fs_sb_info * sbi)2871*59bfda1fSAndroid Build Coastguard Worker void flush_sit_entries(struct f2fs_sb_info *sbi)
2872*59bfda1fSAndroid Build Coastguard Worker {
2873*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
2874*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_block *sit_blk;
2875*59bfda1fSAndroid Build Coastguard Worker unsigned int segno = 0;
2876*59bfda1fSAndroid Build Coastguard Worker
2877*59bfda1fSAndroid Build Coastguard Worker sit_blk = calloc(F2FS_BLKSIZE, 1);
2878*59bfda1fSAndroid Build Coastguard Worker ASSERT(sit_blk);
2879*59bfda1fSAndroid Build Coastguard Worker /* update free segments */
2880*59bfda1fSAndroid Build Coastguard Worker for (segno = 0; segno < MAIN_SEGS(sbi); segno++) {
2881*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sit_entry *sit;
2882*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
2883*59bfda1fSAndroid Build Coastguard Worker
2884*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
2885*59bfda1fSAndroid Build Coastguard Worker
2886*59bfda1fSAndroid Build Coastguard Worker if (!se->dirty)
2887*59bfda1fSAndroid Build Coastguard Worker continue;
2888*59bfda1fSAndroid Build Coastguard Worker
2889*59bfda1fSAndroid Build Coastguard Worker get_current_sit_page(sbi, segno, sit_blk);
2890*59bfda1fSAndroid Build Coastguard Worker sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, segno)];
2891*59bfda1fSAndroid Build Coastguard Worker memcpy(sit->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
2892*59bfda1fSAndroid Build Coastguard Worker sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2893*59bfda1fSAndroid Build Coastguard Worker se->valid_blocks);
2894*59bfda1fSAndroid Build Coastguard Worker rewrite_current_sit_page(sbi, segno, sit_blk);
2895*59bfda1fSAndroid Build Coastguard Worker }
2896*59bfda1fSAndroid Build Coastguard Worker
2897*59bfda1fSAndroid Build Coastguard Worker free(sit_blk);
2898*59bfda1fSAndroid Build Coastguard Worker }
2899*59bfda1fSAndroid Build Coastguard Worker
relocate_curseg_offset(struct f2fs_sb_info * sbi,int type)2900*59bfda1fSAndroid Build Coastguard Worker int relocate_curseg_offset(struct f2fs_sb_info *sbi, int type)
2901*59bfda1fSAndroid Build Coastguard Worker {
2902*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, type);
2903*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
2904*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
2905*59bfda1fSAndroid Build Coastguard Worker
2906*59bfda1fSAndroid Build Coastguard Worker if (c.zoned_model == F2FS_ZONED_HM)
2907*59bfda1fSAndroid Build Coastguard Worker return -EINVAL;
2908*59bfda1fSAndroid Build Coastguard Worker
2909*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < sbi->blocks_per_seg; i++) {
2910*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_test_bit(i, (const char *)se->cur_valid_map))
2911*59bfda1fSAndroid Build Coastguard Worker break;
2912*59bfda1fSAndroid Build Coastguard Worker }
2913*59bfda1fSAndroid Build Coastguard Worker
2914*59bfda1fSAndroid Build Coastguard Worker if (i == sbi->blocks_per_seg)
2915*59bfda1fSAndroid Build Coastguard Worker return -EINVAL;
2916*59bfda1fSAndroid Build Coastguard Worker
2917*59bfda1fSAndroid Build Coastguard Worker DBG(1, "Update curseg[%d].next_blkoff %u -> %u, alloc_type %s -> SSR\n",
2918*59bfda1fSAndroid Build Coastguard Worker type, curseg->next_blkoff, i,
2919*59bfda1fSAndroid Build Coastguard Worker curseg->alloc_type == LFS ? "LFS" : "SSR");
2920*59bfda1fSAndroid Build Coastguard Worker
2921*59bfda1fSAndroid Build Coastguard Worker curseg->next_blkoff = i;
2922*59bfda1fSAndroid Build Coastguard Worker curseg->alloc_type = SSR;
2923*59bfda1fSAndroid Build Coastguard Worker
2924*59bfda1fSAndroid Build Coastguard Worker return 0;
2925*59bfda1fSAndroid Build Coastguard Worker }
2926*59bfda1fSAndroid Build Coastguard Worker
set_section_type(struct f2fs_sb_info * sbi,unsigned int segno,int type)2927*59bfda1fSAndroid Build Coastguard Worker void set_section_type(struct f2fs_sb_info *sbi, unsigned int segno, int type)
2928*59bfda1fSAndroid Build Coastguard Worker {
2929*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
2930*59bfda1fSAndroid Build Coastguard Worker
2931*59bfda1fSAndroid Build Coastguard Worker if (sbi->segs_per_sec == 1)
2932*59bfda1fSAndroid Build Coastguard Worker return;
2933*59bfda1fSAndroid Build Coastguard Worker
2934*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < sbi->segs_per_sec; i++) {
2935*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se = get_seg_entry(sbi, segno + i);
2936*59bfda1fSAndroid Build Coastguard Worker
2937*59bfda1fSAndroid Build Coastguard Worker se->type = se->orig_type = type;
2938*59bfda1fSAndroid Build Coastguard Worker se->dirty = 1;
2939*59bfda1fSAndroid Build Coastguard Worker }
2940*59bfda1fSAndroid Build Coastguard Worker }
2941*59bfda1fSAndroid Build Coastguard Worker
2942*59bfda1fSAndroid Build Coastguard Worker #ifdef HAVE_LINUX_BLKZONED_H
2943*59bfda1fSAndroid Build Coastguard Worker
write_pointer_at_zone_start(struct f2fs_sb_info * sbi,unsigned int zone_segno)2944*59bfda1fSAndroid Build Coastguard Worker static bool write_pointer_at_zone_start(struct f2fs_sb_info *sbi,
2945*59bfda1fSAndroid Build Coastguard Worker unsigned int zone_segno)
2946*59bfda1fSAndroid Build Coastguard Worker {
2947*59bfda1fSAndroid Build Coastguard Worker uint64_t sector;
2948*59bfda1fSAndroid Build Coastguard Worker struct blk_zone blkz;
2949*59bfda1fSAndroid Build Coastguard Worker block_t block = START_BLOCK(sbi, zone_segno);
2950*59bfda1fSAndroid Build Coastguard Worker int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2951*59bfda1fSAndroid Build Coastguard Worker int ret, j;
2952*59bfda1fSAndroid Build Coastguard Worker
2953*59bfda1fSAndroid Build Coastguard Worker for (j = 0; j < MAX_DEVICES; j++) {
2954*59bfda1fSAndroid Build Coastguard Worker if (!c.devices[j].path)
2955*59bfda1fSAndroid Build Coastguard Worker break;
2956*59bfda1fSAndroid Build Coastguard Worker if (c.devices[j].start_blkaddr <= block &&
2957*59bfda1fSAndroid Build Coastguard Worker block <= c.devices[j].end_blkaddr)
2958*59bfda1fSAndroid Build Coastguard Worker break;
2959*59bfda1fSAndroid Build Coastguard Worker }
2960*59bfda1fSAndroid Build Coastguard Worker
2961*59bfda1fSAndroid Build Coastguard Worker if (j >= MAX_DEVICES)
2962*59bfda1fSAndroid Build Coastguard Worker return false;
2963*59bfda1fSAndroid Build Coastguard Worker
2964*59bfda1fSAndroid Build Coastguard Worker if (c.devices[j].zoned_model != F2FS_ZONED_HM)
2965*59bfda1fSAndroid Build Coastguard Worker return true;
2966*59bfda1fSAndroid Build Coastguard Worker
2967*59bfda1fSAndroid Build Coastguard Worker sector = (block - c.devices[j].start_blkaddr) << log_sectors_per_block;
2968*59bfda1fSAndroid Build Coastguard Worker ret = f2fs_report_zone(j, sector, &blkz);
2969*59bfda1fSAndroid Build Coastguard Worker if (ret)
2970*59bfda1fSAndroid Build Coastguard Worker return false;
2971*59bfda1fSAndroid Build Coastguard Worker
2972*59bfda1fSAndroid Build Coastguard Worker if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
2973*59bfda1fSAndroid Build Coastguard Worker return true;
2974*59bfda1fSAndroid Build Coastguard Worker
2975*59bfda1fSAndroid Build Coastguard Worker return blk_zone_sector(&blkz) == blk_zone_wp_sector(&blkz);
2976*59bfda1fSAndroid Build Coastguard Worker }
2977*59bfda1fSAndroid Build Coastguard Worker
2978*59bfda1fSAndroid Build Coastguard Worker #else
2979*59bfda1fSAndroid Build Coastguard Worker
write_pointer_at_zone_start(struct f2fs_sb_info * UNUSED (sbi),unsigned int UNUSED (zone_segno))2980*59bfda1fSAndroid Build Coastguard Worker static bool write_pointer_at_zone_start(struct f2fs_sb_info *UNUSED(sbi),
2981*59bfda1fSAndroid Build Coastguard Worker unsigned int UNUSED(zone_segno))
2982*59bfda1fSAndroid Build Coastguard Worker {
2983*59bfda1fSAndroid Build Coastguard Worker return true;
2984*59bfda1fSAndroid Build Coastguard Worker }
2985*59bfda1fSAndroid Build Coastguard Worker
2986*59bfda1fSAndroid Build Coastguard Worker #endif
2987*59bfda1fSAndroid Build Coastguard Worker
zero_journal_entries_with_type(struct f2fs_sb_info * sbi,int type)2988*59bfda1fSAndroid Build Coastguard Worker static void zero_journal_entries_with_type(struct f2fs_sb_info *sbi, int type)
2989*59bfda1fSAndroid Build Coastguard Worker {
2990*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal =
2991*59bfda1fSAndroid Build Coastguard Worker F2FS_SUMMARY_BLOCK_JOURNAL(CURSEG_I(sbi, type)->sum_blk);
2992*59bfda1fSAndroid Build Coastguard Worker
2993*59bfda1fSAndroid Build Coastguard Worker if (type == CURSEG_HOT_DATA)
2994*59bfda1fSAndroid Build Coastguard Worker journal->n_nats = 0;
2995*59bfda1fSAndroid Build Coastguard Worker else if (type == CURSEG_COLD_DATA)
2996*59bfda1fSAndroid Build Coastguard Worker journal->n_sits = 0;
2997*59bfda1fSAndroid Build Coastguard Worker }
2998*59bfda1fSAndroid Build Coastguard Worker
find_next_free_block(struct f2fs_sb_info * sbi,u64 * to,int left,int want_type,bool new_sec)2999*59bfda1fSAndroid Build Coastguard Worker int find_next_free_block(struct f2fs_sb_info *sbi, u64 *to, int left,
3000*59bfda1fSAndroid Build Coastguard Worker int want_type, bool new_sec)
3001*59bfda1fSAndroid Build Coastguard Worker {
3002*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3003*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
3004*59bfda1fSAndroid Build Coastguard Worker u32 segno;
3005*59bfda1fSAndroid Build Coastguard Worker u32 offset;
3006*59bfda1fSAndroid Build Coastguard Worker int not_enough = 0;
3007*59bfda1fSAndroid Build Coastguard Worker u64 end_blkaddr = (get_sb(segment_count_main) <<
3008*59bfda1fSAndroid Build Coastguard Worker get_sb(log_blocks_per_seg)) + get_sb(main_blkaddr);
3009*59bfda1fSAndroid Build Coastguard Worker
3010*59bfda1fSAndroid Build Coastguard Worker if (c.zoned_model == F2FS_ZONED_HM && !new_sec) {
3011*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, want_type);
3012*59bfda1fSAndroid Build Coastguard Worker unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
3013*59bfda1fSAndroid Build Coastguard Worker char buf[F2FS_BLKSIZE];
3014*59bfda1fSAndroid Build Coastguard Worker u64 ssa_blk;
3015*59bfda1fSAndroid Build Coastguard Worker int ret;
3016*59bfda1fSAndroid Build Coastguard Worker
3017*59bfda1fSAndroid Build Coastguard Worker *to = NEXT_FREE_BLKADDR(sbi, curseg);
3018*59bfda1fSAndroid Build Coastguard Worker curseg->next_blkoff++;
3019*59bfda1fSAndroid Build Coastguard Worker
3020*59bfda1fSAndroid Build Coastguard Worker if (curseg->next_blkoff == sbi->blocks_per_seg) {
3021*59bfda1fSAndroid Build Coastguard Worker segno = curseg->segno + 1;
3022*59bfda1fSAndroid Build Coastguard Worker if (!(segno % segs_per_zone)) {
3023*59bfda1fSAndroid Build Coastguard Worker u64 new_blkaddr = SM_I(sbi)->main_blkaddr;
3024*59bfda1fSAndroid Build Coastguard Worker
3025*59bfda1fSAndroid Build Coastguard Worker ret = find_next_free_block(sbi, &new_blkaddr, 0,
3026*59bfda1fSAndroid Build Coastguard Worker want_type, true);
3027*59bfda1fSAndroid Build Coastguard Worker if (ret)
3028*59bfda1fSAndroid Build Coastguard Worker return ret;
3029*59bfda1fSAndroid Build Coastguard Worker segno = GET_SEGNO(sbi, new_blkaddr);
3030*59bfda1fSAndroid Build Coastguard Worker }
3031*59bfda1fSAndroid Build Coastguard Worker
3032*59bfda1fSAndroid Build Coastguard Worker ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3033*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(curseg->sum_blk, ssa_blk,
3034*59bfda1fSAndroid Build Coastguard Worker WRITE_LIFE_NONE);
3035*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3036*59bfda1fSAndroid Build Coastguard Worker
3037*59bfda1fSAndroid Build Coastguard Worker curseg->segno = segno;
3038*59bfda1fSAndroid Build Coastguard Worker curseg->next_blkoff = 0;
3039*59bfda1fSAndroid Build Coastguard Worker curseg->alloc_type = LFS;
3040*59bfda1fSAndroid Build Coastguard Worker
3041*59bfda1fSAndroid Build Coastguard Worker ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3042*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(&buf, ssa_blk);
3043*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3044*59bfda1fSAndroid Build Coastguard Worker
3045*59bfda1fSAndroid Build Coastguard Worker memcpy(curseg->sum_blk, &buf, SUM_ENTRIES_SIZE);
3046*59bfda1fSAndroid Build Coastguard Worker
3047*59bfda1fSAndroid Build Coastguard Worker reset_curseg(sbi, want_type);
3048*59bfda1fSAndroid Build Coastguard Worker zero_journal_entries_with_type(sbi, want_type);
3049*59bfda1fSAndroid Build Coastguard Worker }
3050*59bfda1fSAndroid Build Coastguard Worker
3051*59bfda1fSAndroid Build Coastguard Worker return 0;
3052*59bfda1fSAndroid Build Coastguard Worker }
3053*59bfda1fSAndroid Build Coastguard Worker
3054*59bfda1fSAndroid Build Coastguard Worker if (*to > 0)
3055*59bfda1fSAndroid Build Coastguard Worker *to -= left;
3056*59bfda1fSAndroid Build Coastguard Worker if (SM_I(sbi)->free_segments <= SM_I(sbi)->reserved_segments + 1)
3057*59bfda1fSAndroid Build Coastguard Worker not_enough = 1;
3058*59bfda1fSAndroid Build Coastguard Worker
3059*59bfda1fSAndroid Build Coastguard Worker while (*to >= SM_I(sbi)->main_blkaddr && *to < end_blkaddr) {
3060*59bfda1fSAndroid Build Coastguard Worker unsigned short vblocks;
3061*59bfda1fSAndroid Build Coastguard Worker unsigned char *bitmap;
3062*59bfda1fSAndroid Build Coastguard Worker unsigned char type;
3063*59bfda1fSAndroid Build Coastguard Worker
3064*59bfda1fSAndroid Build Coastguard Worker segno = GET_SEGNO(sbi, *to);
3065*59bfda1fSAndroid Build Coastguard Worker offset = OFFSET_IN_SEG(sbi, *to);
3066*59bfda1fSAndroid Build Coastguard Worker
3067*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
3068*59bfda1fSAndroid Build Coastguard Worker
3069*59bfda1fSAndroid Build Coastguard Worker vblocks = get_seg_vblocks(sbi, se);
3070*59bfda1fSAndroid Build Coastguard Worker bitmap = get_seg_bitmap(sbi, se);
3071*59bfda1fSAndroid Build Coastguard Worker type = get_seg_type(sbi, se);
3072*59bfda1fSAndroid Build Coastguard Worker
3073*59bfda1fSAndroid Build Coastguard Worker if (vblocks == sbi->blocks_per_seg) {
3074*59bfda1fSAndroid Build Coastguard Worker next_segment:
3075*59bfda1fSAndroid Build Coastguard Worker *to = left ? START_BLOCK(sbi, segno) - 1:
3076*59bfda1fSAndroid Build Coastguard Worker START_BLOCK(sbi, segno + 1);
3077*59bfda1fSAndroid Build Coastguard Worker continue;
3078*59bfda1fSAndroid Build Coastguard Worker }
3079*59bfda1fSAndroid Build Coastguard Worker if (!(get_sb(feature) & F2FS_FEATURE_RO) &&
3080*59bfda1fSAndroid Build Coastguard Worker IS_CUR_SEGNO(sbi, segno))
3081*59bfda1fSAndroid Build Coastguard Worker goto next_segment;
3082*59bfda1fSAndroid Build Coastguard Worker if (vblocks == 0 && not_enough)
3083*59bfda1fSAndroid Build Coastguard Worker goto next_segment;
3084*59bfda1fSAndroid Build Coastguard Worker
3085*59bfda1fSAndroid Build Coastguard Worker if (vblocks == 0 && !(segno % sbi->segs_per_sec)) {
3086*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se2;
3087*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
3088*59bfda1fSAndroid Build Coastguard Worker
3089*59bfda1fSAndroid Build Coastguard Worker for (i = 1; i < sbi->segs_per_sec; i++) {
3090*59bfda1fSAndroid Build Coastguard Worker se2 = get_seg_entry(sbi, segno + i);
3091*59bfda1fSAndroid Build Coastguard Worker if (get_seg_vblocks(sbi, se2))
3092*59bfda1fSAndroid Build Coastguard Worker break;
3093*59bfda1fSAndroid Build Coastguard Worker }
3094*59bfda1fSAndroid Build Coastguard Worker
3095*59bfda1fSAndroid Build Coastguard Worker if (i == sbi->segs_per_sec &&
3096*59bfda1fSAndroid Build Coastguard Worker write_pointer_at_zone_start(sbi, segno)) {
3097*59bfda1fSAndroid Build Coastguard Worker set_section_type(sbi, segno, want_type);
3098*59bfda1fSAndroid Build Coastguard Worker return 0;
3099*59bfda1fSAndroid Build Coastguard Worker }
3100*59bfda1fSAndroid Build Coastguard Worker }
3101*59bfda1fSAndroid Build Coastguard Worker
3102*59bfda1fSAndroid Build Coastguard Worker if (type != want_type)
3103*59bfda1fSAndroid Build Coastguard Worker goto next_segment;
3104*59bfda1fSAndroid Build Coastguard Worker else if (!new_sec &&
3105*59bfda1fSAndroid Build Coastguard Worker !f2fs_test_bit(offset, (const char *)bitmap))
3106*59bfda1fSAndroid Build Coastguard Worker return 0;
3107*59bfda1fSAndroid Build Coastguard Worker
3108*59bfda1fSAndroid Build Coastguard Worker *to = left ? *to - 1: *to + 1;
3109*59bfda1fSAndroid Build Coastguard Worker }
3110*59bfda1fSAndroid Build Coastguard Worker return -1;
3111*59bfda1fSAndroid Build Coastguard Worker }
3112*59bfda1fSAndroid Build Coastguard Worker
move_one_curseg_info(struct f2fs_sb_info * sbi,u64 from,int left,int i)3113*59bfda1fSAndroid Build Coastguard Worker void move_one_curseg_info(struct f2fs_sb_info *sbi, u64 from, int left,
3114*59bfda1fSAndroid Build Coastguard Worker int i)
3115*59bfda1fSAndroid Build Coastguard Worker {
3116*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3117*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, i);
3118*59bfda1fSAndroid Build Coastguard Worker char buf[F2FS_BLKSIZE];
3119*59bfda1fSAndroid Build Coastguard Worker u32 old_segno;
3120*59bfda1fSAndroid Build Coastguard Worker u64 ssa_blk, to;
3121*59bfda1fSAndroid Build Coastguard Worker int ret;
3122*59bfda1fSAndroid Build Coastguard Worker
3123*59bfda1fSAndroid Build Coastguard Worker if ((get_sb(feature) & F2FS_FEATURE_RO)) {
3124*59bfda1fSAndroid Build Coastguard Worker if (i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
3125*59bfda1fSAndroid Build Coastguard Worker return;
3126*59bfda1fSAndroid Build Coastguard Worker
3127*59bfda1fSAndroid Build Coastguard Worker if (i == CURSEG_HOT_DATA) {
3128*59bfda1fSAndroid Build Coastguard Worker left = 0;
3129*59bfda1fSAndroid Build Coastguard Worker from = SM_I(sbi)->main_blkaddr;
3130*59bfda1fSAndroid Build Coastguard Worker } else {
3131*59bfda1fSAndroid Build Coastguard Worker left = 1;
3132*59bfda1fSAndroid Build Coastguard Worker from = __end_block_addr(sbi);
3133*59bfda1fSAndroid Build Coastguard Worker }
3134*59bfda1fSAndroid Build Coastguard Worker goto bypass_ssa;
3135*59bfda1fSAndroid Build Coastguard Worker }
3136*59bfda1fSAndroid Build Coastguard Worker
3137*59bfda1fSAndroid Build Coastguard Worker /* update original SSA too */
3138*59bfda1fSAndroid Build Coastguard Worker ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3139*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(curseg->sum_blk, ssa_blk, WRITE_LIFE_NONE);
3140*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3141*59bfda1fSAndroid Build Coastguard Worker bypass_ssa:
3142*59bfda1fSAndroid Build Coastguard Worker to = from;
3143*59bfda1fSAndroid Build Coastguard Worker ret = find_next_free_block(sbi, &to, left, i,
3144*59bfda1fSAndroid Build Coastguard Worker c.zoned_model == F2FS_ZONED_HM);
3145*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret == 0);
3146*59bfda1fSAndroid Build Coastguard Worker
3147*59bfda1fSAndroid Build Coastguard Worker old_segno = curseg->segno;
3148*59bfda1fSAndroid Build Coastguard Worker curseg->segno = GET_SEGNO(sbi, to);
3149*59bfda1fSAndroid Build Coastguard Worker curseg->next_blkoff = OFFSET_IN_SEG(sbi, to);
3150*59bfda1fSAndroid Build Coastguard Worker curseg->alloc_type = c.zoned_model == F2FS_ZONED_HM ? LFS : SSR;
3151*59bfda1fSAndroid Build Coastguard Worker
3152*59bfda1fSAndroid Build Coastguard Worker /* update new segno */
3153*59bfda1fSAndroid Build Coastguard Worker ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3154*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(buf, ssa_blk);
3155*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3156*59bfda1fSAndroid Build Coastguard Worker
3157*59bfda1fSAndroid Build Coastguard Worker memcpy(curseg->sum_blk, buf, SUM_ENTRIES_SIZE);
3158*59bfda1fSAndroid Build Coastguard Worker
3159*59bfda1fSAndroid Build Coastguard Worker /* update se->types */
3160*59bfda1fSAndroid Build Coastguard Worker reset_curseg(sbi, i);
3161*59bfda1fSAndroid Build Coastguard Worker if (c.zoned_model == F2FS_ZONED_HM)
3162*59bfda1fSAndroid Build Coastguard Worker zero_journal_entries_with_type(sbi, i);
3163*59bfda1fSAndroid Build Coastguard Worker
3164*59bfda1fSAndroid Build Coastguard Worker FIX_MSG("Move curseg[%d] %x -> %x after %"PRIx64"\n",
3165*59bfda1fSAndroid Build Coastguard Worker i, old_segno, curseg->segno, from);
3166*59bfda1fSAndroid Build Coastguard Worker }
3167*59bfda1fSAndroid Build Coastguard Worker
move_curseg_info(struct f2fs_sb_info * sbi,u64 from,int left)3168*59bfda1fSAndroid Build Coastguard Worker void move_curseg_info(struct f2fs_sb_info *sbi, u64 from, int left)
3169*59bfda1fSAndroid Build Coastguard Worker {
3170*59bfda1fSAndroid Build Coastguard Worker int i;
3171*59bfda1fSAndroid Build Coastguard Worker
3172*59bfda1fSAndroid Build Coastguard Worker /* update summary blocks having nullified journal entries */
3173*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NO_CHECK_TYPE; i++)
3174*59bfda1fSAndroid Build Coastguard Worker move_one_curseg_info(sbi, from, left, i);
3175*59bfda1fSAndroid Build Coastguard Worker }
3176*59bfda1fSAndroid Build Coastguard Worker
update_curseg_info(struct f2fs_sb_info * sbi,int type)3177*59bfda1fSAndroid Build Coastguard Worker void update_curseg_info(struct f2fs_sb_info *sbi, int type)
3178*59bfda1fSAndroid Build Coastguard Worker {
3179*59bfda1fSAndroid Build Coastguard Worker if (!relocate_curseg_offset(sbi, type))
3180*59bfda1fSAndroid Build Coastguard Worker return;
3181*59bfda1fSAndroid Build Coastguard Worker move_one_curseg_info(sbi, SM_I(sbi)->main_blkaddr, 0, type);
3182*59bfda1fSAndroid Build Coastguard Worker }
3183*59bfda1fSAndroid Build Coastguard Worker
zero_journal_entries(struct f2fs_sb_info * sbi)3184*59bfda1fSAndroid Build Coastguard Worker void zero_journal_entries(struct f2fs_sb_info *sbi)
3185*59bfda1fSAndroid Build Coastguard Worker {
3186*59bfda1fSAndroid Build Coastguard Worker int i;
3187*59bfda1fSAndroid Build Coastguard Worker
3188*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NO_CHECK_TYPE; i++)
3189*59bfda1fSAndroid Build Coastguard Worker F2FS_SUMMARY_BLOCK_JOURNAL(CURSEG_I(sbi, i)->sum_blk)->n_nats = 0;
3190*59bfda1fSAndroid Build Coastguard Worker }
3191*59bfda1fSAndroid Build Coastguard Worker
write_curseg_info(struct f2fs_sb_info * sbi)3192*59bfda1fSAndroid Build Coastguard Worker void write_curseg_info(struct f2fs_sb_info *sbi)
3193*59bfda1fSAndroid Build Coastguard Worker {
3194*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3195*59bfda1fSAndroid Build Coastguard Worker int i;
3196*59bfda1fSAndroid Build Coastguard Worker
3197*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NO_CHECK_TYPE; i++) {
3198*59bfda1fSAndroid Build Coastguard Worker cp->alloc_type[i] = CURSEG_I(sbi, i)->alloc_type;
3199*59bfda1fSAndroid Build Coastguard Worker if (i < CURSEG_HOT_NODE) {
3200*59bfda1fSAndroid Build Coastguard Worker set_cp(cur_data_segno[i], CURSEG_I(sbi, i)->segno);
3201*59bfda1fSAndroid Build Coastguard Worker set_cp(cur_data_blkoff[i],
3202*59bfda1fSAndroid Build Coastguard Worker CURSEG_I(sbi, i)->next_blkoff);
3203*59bfda1fSAndroid Build Coastguard Worker } else {
3204*59bfda1fSAndroid Build Coastguard Worker int n = i - CURSEG_HOT_NODE;
3205*59bfda1fSAndroid Build Coastguard Worker
3206*59bfda1fSAndroid Build Coastguard Worker set_cp(cur_node_segno[n], CURSEG_I(sbi, i)->segno);
3207*59bfda1fSAndroid Build Coastguard Worker set_cp(cur_node_blkoff[n],
3208*59bfda1fSAndroid Build Coastguard Worker CURSEG_I(sbi, i)->next_blkoff);
3209*59bfda1fSAndroid Build Coastguard Worker }
3210*59bfda1fSAndroid Build Coastguard Worker }
3211*59bfda1fSAndroid Build Coastguard Worker }
3212*59bfda1fSAndroid Build Coastguard Worker
save_curseg_warm_node_info(struct f2fs_sb_info * sbi)3213*59bfda1fSAndroid Build Coastguard Worker void save_curseg_warm_node_info(struct f2fs_sb_info *sbi)
3214*59bfda1fSAndroid Build Coastguard Worker {
3215*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
3216*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *saved_curseg = &SM_I(sbi)->saved_curseg_warm_node;
3217*59bfda1fSAndroid Build Coastguard Worker
3218*59bfda1fSAndroid Build Coastguard Worker saved_curseg->alloc_type = curseg->alloc_type;
3219*59bfda1fSAndroid Build Coastguard Worker saved_curseg->segno = curseg->segno;
3220*59bfda1fSAndroid Build Coastguard Worker saved_curseg->next_blkoff = curseg->next_blkoff;
3221*59bfda1fSAndroid Build Coastguard Worker }
3222*59bfda1fSAndroid Build Coastguard Worker
restore_curseg_warm_node_info(struct f2fs_sb_info * sbi)3223*59bfda1fSAndroid Build Coastguard Worker void restore_curseg_warm_node_info(struct f2fs_sb_info *sbi)
3224*59bfda1fSAndroid Build Coastguard Worker {
3225*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
3226*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *saved_curseg = &SM_I(sbi)->saved_curseg_warm_node;
3227*59bfda1fSAndroid Build Coastguard Worker
3228*59bfda1fSAndroid Build Coastguard Worker curseg->alloc_type = saved_curseg->alloc_type;
3229*59bfda1fSAndroid Build Coastguard Worker curseg->segno = saved_curseg->segno;
3230*59bfda1fSAndroid Build Coastguard Worker curseg->next_blkoff = saved_curseg->next_blkoff;
3231*59bfda1fSAndroid Build Coastguard Worker }
3232*59bfda1fSAndroid Build Coastguard Worker
lookup_nat_in_journal(struct f2fs_sb_info * sbi,u32 nid,struct f2fs_nat_entry * raw_nat)3233*59bfda1fSAndroid Build Coastguard Worker int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid,
3234*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_entry *raw_nat)
3235*59bfda1fSAndroid Build Coastguard Worker {
3236*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3237*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
3238*59bfda1fSAndroid Build Coastguard Worker int i = 0;
3239*59bfda1fSAndroid Build Coastguard Worker
3240*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nats_in_cursum(journal); i++) {
3241*59bfda1fSAndroid Build Coastguard Worker if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
3242*59bfda1fSAndroid Build Coastguard Worker memcpy(raw_nat, &nat_in_journal(journal, i),
3243*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_nat_entry));
3244*59bfda1fSAndroid Build Coastguard Worker DBG(3, "==> Found nid [0x%x] in nat cache\n", nid);
3245*59bfda1fSAndroid Build Coastguard Worker return i;
3246*59bfda1fSAndroid Build Coastguard Worker }
3247*59bfda1fSAndroid Build Coastguard Worker }
3248*59bfda1fSAndroid Build Coastguard Worker return -1;
3249*59bfda1fSAndroid Build Coastguard Worker }
3250*59bfda1fSAndroid Build Coastguard Worker
nullify_nat_entry(struct f2fs_sb_info * sbi,u32 nid)3251*59bfda1fSAndroid Build Coastguard Worker void nullify_nat_entry(struct f2fs_sb_info *sbi, u32 nid)
3252*59bfda1fSAndroid Build Coastguard Worker {
3253*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3254*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
3255*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block;
3256*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_addr;
3257*59bfda1fSAndroid Build Coastguard Worker int entry_off;
3258*59bfda1fSAndroid Build Coastguard Worker int ret;
3259*59bfda1fSAndroid Build Coastguard Worker int i = 0;
3260*59bfda1fSAndroid Build Coastguard Worker
3261*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK)
3262*59bfda1fSAndroid Build Coastguard Worker F2FS_FSCK(sbi)->entries[nid].block_addr = 0;
3263*59bfda1fSAndroid Build Coastguard Worker
3264*59bfda1fSAndroid Build Coastguard Worker /* check in journal */
3265*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nats_in_cursum(journal); i++) {
3266*59bfda1fSAndroid Build Coastguard Worker if (le32_to_cpu(nid_in_journal(journal, i)) == nid) {
3267*59bfda1fSAndroid Build Coastguard Worker memset(&nat_in_journal(journal, i), 0,
3268*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_nat_entry));
3269*59bfda1fSAndroid Build Coastguard Worker FIX_MSG("Remove nid [0x%x] in nat journal", nid);
3270*59bfda1fSAndroid Build Coastguard Worker return;
3271*59bfda1fSAndroid Build Coastguard Worker }
3272*59bfda1fSAndroid Build Coastguard Worker }
3273*59bfda1fSAndroid Build Coastguard Worker nat_block = (struct f2fs_nat_block *)calloc(F2FS_BLKSIZE, 1);
3274*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_block);
3275*59bfda1fSAndroid Build Coastguard Worker
3276*59bfda1fSAndroid Build Coastguard Worker entry_off = nid % NAT_ENTRY_PER_BLOCK;
3277*59bfda1fSAndroid Build Coastguard Worker block_addr = current_nat_addr(sbi, nid, NULL);
3278*59bfda1fSAndroid Build Coastguard Worker
3279*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, block_addr);
3280*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3281*59bfda1fSAndroid Build Coastguard Worker
3282*59bfda1fSAndroid Build Coastguard Worker if (nid == F2FS_NODE_INO(sbi) || nid == F2FS_META_INO(sbi)) {
3283*59bfda1fSAndroid Build Coastguard Worker FIX_MSG("nid [0x%x] block_addr= 0x%x -> 0x1", nid,
3284*59bfda1fSAndroid Build Coastguard Worker le32_to_cpu(nat_block->entries[entry_off].block_addr));
3285*59bfda1fSAndroid Build Coastguard Worker nat_block->entries[entry_off].block_addr = cpu_to_le32(0x1);
3286*59bfda1fSAndroid Build Coastguard Worker } else {
3287*59bfda1fSAndroid Build Coastguard Worker memset(&nat_block->entries[entry_off], 0,
3288*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_nat_entry));
3289*59bfda1fSAndroid Build Coastguard Worker FIX_MSG("Remove nid [0x%x] in NAT", nid);
3290*59bfda1fSAndroid Build Coastguard Worker }
3291*59bfda1fSAndroid Build Coastguard Worker
3292*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(nat_block, block_addr, WRITE_LIFE_NONE);
3293*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3294*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
3295*59bfda1fSAndroid Build Coastguard Worker }
3296*59bfda1fSAndroid Build Coastguard Worker
duplicate_checkpoint(struct f2fs_sb_info * sbi)3297*59bfda1fSAndroid Build Coastguard Worker void duplicate_checkpoint(struct f2fs_sb_info *sbi)
3298*59bfda1fSAndroid Build Coastguard Worker {
3299*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3300*59bfda1fSAndroid Build Coastguard Worker unsigned long long dst, src;
3301*59bfda1fSAndroid Build Coastguard Worker void *buf;
3302*59bfda1fSAndroid Build Coastguard Worker unsigned int seg_size = 1 << get_sb(log_blocks_per_seg);
3303*59bfda1fSAndroid Build Coastguard Worker int ret;
3304*59bfda1fSAndroid Build Coastguard Worker
3305*59bfda1fSAndroid Build Coastguard Worker if (sbi->cp_backuped)
3306*59bfda1fSAndroid Build Coastguard Worker return;
3307*59bfda1fSAndroid Build Coastguard Worker
3308*59bfda1fSAndroid Build Coastguard Worker buf = malloc(F2FS_BLKSIZE * seg_size);
3309*59bfda1fSAndroid Build Coastguard Worker ASSERT(buf);
3310*59bfda1fSAndroid Build Coastguard Worker
3311*59bfda1fSAndroid Build Coastguard Worker if (sbi->cur_cp == 1) {
3312*59bfda1fSAndroid Build Coastguard Worker src = get_sb(cp_blkaddr);
3313*59bfda1fSAndroid Build Coastguard Worker dst = src + seg_size;
3314*59bfda1fSAndroid Build Coastguard Worker } else {
3315*59bfda1fSAndroid Build Coastguard Worker dst = get_sb(cp_blkaddr);
3316*59bfda1fSAndroid Build Coastguard Worker src = dst + seg_size;
3317*59bfda1fSAndroid Build Coastguard Worker }
3318*59bfda1fSAndroid Build Coastguard Worker
3319*59bfda1fSAndroid Build Coastguard Worker ret = dev_read(buf, src << F2FS_BLKSIZE_BITS,
3320*59bfda1fSAndroid Build Coastguard Worker seg_size << F2FS_BLKSIZE_BITS);
3321*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3322*59bfda1fSAndroid Build Coastguard Worker
3323*59bfda1fSAndroid Build Coastguard Worker ret = dev_write(buf, dst << F2FS_BLKSIZE_BITS,
3324*59bfda1fSAndroid Build Coastguard Worker seg_size << F2FS_BLKSIZE_BITS, WRITE_LIFE_NONE);
3325*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3326*59bfda1fSAndroid Build Coastguard Worker
3327*59bfda1fSAndroid Build Coastguard Worker free(buf);
3328*59bfda1fSAndroid Build Coastguard Worker
3329*59bfda1fSAndroid Build Coastguard Worker ret = f2fs_fsync_device();
3330*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3331*59bfda1fSAndroid Build Coastguard Worker
3332*59bfda1fSAndroid Build Coastguard Worker sbi->cp_backuped = 1;
3333*59bfda1fSAndroid Build Coastguard Worker
3334*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Duplicate valid checkpoint to mirror position "
3335*59bfda1fSAndroid Build Coastguard Worker "%llu -> %llu\n", src, dst);
3336*59bfda1fSAndroid Build Coastguard Worker }
3337*59bfda1fSAndroid Build Coastguard Worker
write_checkpoint(struct f2fs_sb_info * sbi)3338*59bfda1fSAndroid Build Coastguard Worker void write_checkpoint(struct f2fs_sb_info *sbi)
3339*59bfda1fSAndroid Build Coastguard Worker {
3340*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3341*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3342*59bfda1fSAndroid Build Coastguard Worker block_t orphan_blks = 0;
3343*59bfda1fSAndroid Build Coastguard Worker unsigned long long cp_blk_no;
3344*59bfda1fSAndroid Build Coastguard Worker u32 flags = c.roll_forward ? 0 : CP_UMOUNT_FLAG;
3345*59bfda1fSAndroid Build Coastguard Worker int i, ret;
3346*59bfda1fSAndroid Build Coastguard Worker uint32_t crc = 0;
3347*59bfda1fSAndroid Build Coastguard Worker
3348*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
3349*59bfda1fSAndroid Build Coastguard Worker orphan_blks = __start_sum_addr(sbi) - 1;
3350*59bfda1fSAndroid Build Coastguard Worker flags |= CP_ORPHAN_PRESENT_FLAG;
3351*59bfda1fSAndroid Build Coastguard Worker }
3352*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
3353*59bfda1fSAndroid Build Coastguard Worker flags |= CP_TRIMMED_FLAG;
3354*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
3355*59bfda1fSAndroid Build Coastguard Worker flags |= CP_DISABLED_FLAG;
3356*59bfda1fSAndroid Build Coastguard Worker if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
3357*59bfda1fSAndroid Build Coastguard Worker flags |= CP_LARGE_NAT_BITMAP_FLAG;
3358*59bfda1fSAndroid Build Coastguard Worker set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
3359*59bfda1fSAndroid Build Coastguard Worker } else {
3360*59bfda1fSAndroid Build Coastguard Worker set_cp(checksum_offset, CP_CHKSUM_OFFSET);
3361*59bfda1fSAndroid Build Coastguard Worker }
3362*59bfda1fSAndroid Build Coastguard Worker
3363*59bfda1fSAndroid Build Coastguard Worker set_cp(free_segment_count, get_free_segments(sbi));
3364*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK) {
3365*59bfda1fSAndroid Build Coastguard Worker struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3366*59bfda1fSAndroid Build Coastguard Worker
3367*59bfda1fSAndroid Build Coastguard Worker set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
3368*59bfda1fSAndroid Build Coastguard Worker set_cp(valid_node_count, fsck->chk.valid_node_cnt);
3369*59bfda1fSAndroid Build Coastguard Worker set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
3370*59bfda1fSAndroid Build Coastguard Worker } else {
3371*59bfda1fSAndroid Build Coastguard Worker set_cp(valid_block_count, sbi->total_valid_block_count);
3372*59bfda1fSAndroid Build Coastguard Worker set_cp(valid_node_count, sbi->total_valid_node_count);
3373*59bfda1fSAndroid Build Coastguard Worker set_cp(valid_inode_count, sbi->total_valid_inode_count);
3374*59bfda1fSAndroid Build Coastguard Worker }
3375*59bfda1fSAndroid Build Coastguard Worker set_cp(cp_pack_total_block_count, 8 + orphan_blks + get_sb(cp_payload));
3376*59bfda1fSAndroid Build Coastguard Worker
3377*59bfda1fSAndroid Build Coastguard Worker flags = update_nat_bits_flags(sb, cp, flags);
3378*59bfda1fSAndroid Build Coastguard Worker set_cp(ckpt_flags, flags);
3379*59bfda1fSAndroid Build Coastguard Worker
3380*59bfda1fSAndroid Build Coastguard Worker crc = f2fs_checkpoint_chksum(cp);
3381*59bfda1fSAndroid Build Coastguard Worker *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
3382*59bfda1fSAndroid Build Coastguard Worker cpu_to_le32(crc);
3383*59bfda1fSAndroid Build Coastguard Worker
3384*59bfda1fSAndroid Build Coastguard Worker cp_blk_no = get_sb(cp_blkaddr);
3385*59bfda1fSAndroid Build Coastguard Worker if (sbi->cur_cp == 2)
3386*59bfda1fSAndroid Build Coastguard Worker cp_blk_no += 1 << get_sb(log_blocks_per_seg);
3387*59bfda1fSAndroid Build Coastguard Worker
3388*59bfda1fSAndroid Build Coastguard Worker /* write the first cp */
3389*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(cp, cp_blk_no++, WRITE_LIFE_NONE);
3390*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3391*59bfda1fSAndroid Build Coastguard Worker
3392*59bfda1fSAndroid Build Coastguard Worker /* skip payload */
3393*59bfda1fSAndroid Build Coastguard Worker cp_blk_no += get_sb(cp_payload);
3394*59bfda1fSAndroid Build Coastguard Worker /* skip orphan blocks */
3395*59bfda1fSAndroid Build Coastguard Worker cp_blk_no += orphan_blks;
3396*59bfda1fSAndroid Build Coastguard Worker
3397*59bfda1fSAndroid Build Coastguard Worker /* update summary blocks having nullified journal entries */
3398*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NO_CHECK_TYPE; i++) {
3399*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, i);
3400*59bfda1fSAndroid Build Coastguard Worker u64 ssa_blk;
3401*59bfda1fSAndroid Build Coastguard Worker
3402*59bfda1fSAndroid Build Coastguard Worker if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
3403*59bfda1fSAndroid Build Coastguard Worker continue;
3404*59bfda1fSAndroid Build Coastguard Worker
3405*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(curseg->sum_blk, cp_blk_no++,
3406*59bfda1fSAndroid Build Coastguard Worker WRITE_LIFE_NONE);
3407*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3408*59bfda1fSAndroid Build Coastguard Worker
3409*59bfda1fSAndroid Build Coastguard Worker if (!(get_sb(feature) & F2FS_FEATURE_RO)) {
3410*59bfda1fSAndroid Build Coastguard Worker /* update original SSA too */
3411*59bfda1fSAndroid Build Coastguard Worker ssa_blk = GET_SUM_BLKADDR(sbi, curseg->segno);
3412*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(curseg->sum_blk, ssa_blk,
3413*59bfda1fSAndroid Build Coastguard Worker WRITE_LIFE_NONE);
3414*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3415*59bfda1fSAndroid Build Coastguard Worker }
3416*59bfda1fSAndroid Build Coastguard Worker }
3417*59bfda1fSAndroid Build Coastguard Worker
3418*59bfda1fSAndroid Build Coastguard Worker /* Write nat bits */
3419*59bfda1fSAndroid Build Coastguard Worker if (flags & CP_NAT_BITS_FLAG)
3420*59bfda1fSAndroid Build Coastguard Worker write_nat_bits(sbi, sb, cp, sbi->cur_cp);
3421*59bfda1fSAndroid Build Coastguard Worker
3422*59bfda1fSAndroid Build Coastguard Worker /* in case of sudden power off */
3423*59bfda1fSAndroid Build Coastguard Worker ret = f2fs_fsync_device();
3424*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3425*59bfda1fSAndroid Build Coastguard Worker
3426*59bfda1fSAndroid Build Coastguard Worker /* write the last cp */
3427*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(cp, cp_blk_no++, WRITE_LIFE_NONE);
3428*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3429*59bfda1fSAndroid Build Coastguard Worker
3430*59bfda1fSAndroid Build Coastguard Worker ret = f2fs_fsync_device();
3431*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3432*59bfda1fSAndroid Build Coastguard Worker
3433*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: write_checkpoint() cur_cp:%d\n", sbi->cur_cp);
3434*59bfda1fSAndroid Build Coastguard Worker }
3435*59bfda1fSAndroid Build Coastguard Worker
write_checkpoints(struct f2fs_sb_info * sbi)3436*59bfda1fSAndroid Build Coastguard Worker void write_checkpoints(struct f2fs_sb_info *sbi)
3437*59bfda1fSAndroid Build Coastguard Worker {
3438*59bfda1fSAndroid Build Coastguard Worker /* copy valid checkpoint to its mirror position */
3439*59bfda1fSAndroid Build Coastguard Worker duplicate_checkpoint(sbi);
3440*59bfda1fSAndroid Build Coastguard Worker
3441*59bfda1fSAndroid Build Coastguard Worker /* repair checkpoint at CP #0 position */
3442*59bfda1fSAndroid Build Coastguard Worker sbi->cur_cp = 1;
3443*59bfda1fSAndroid Build Coastguard Worker write_checkpoint(sbi);
3444*59bfda1fSAndroid Build Coastguard Worker }
3445*59bfda1fSAndroid Build Coastguard Worker
write_raw_cp_blocks(struct f2fs_sb_info * sbi,struct f2fs_checkpoint * cp,int which)3446*59bfda1fSAndroid Build Coastguard Worker void write_raw_cp_blocks(struct f2fs_sb_info *sbi,
3447*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp, int which)
3448*59bfda1fSAndroid Build Coastguard Worker {
3449*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3450*59bfda1fSAndroid Build Coastguard Worker uint32_t crc;
3451*59bfda1fSAndroid Build Coastguard Worker block_t cp_blkaddr;
3452*59bfda1fSAndroid Build Coastguard Worker int ret;
3453*59bfda1fSAndroid Build Coastguard Worker
3454*59bfda1fSAndroid Build Coastguard Worker crc = f2fs_checkpoint_chksum(cp);
3455*59bfda1fSAndroid Build Coastguard Worker *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
3456*59bfda1fSAndroid Build Coastguard Worker cpu_to_le32(crc);
3457*59bfda1fSAndroid Build Coastguard Worker
3458*59bfda1fSAndroid Build Coastguard Worker cp_blkaddr = get_sb(cp_blkaddr);
3459*59bfda1fSAndroid Build Coastguard Worker if (which == 2)
3460*59bfda1fSAndroid Build Coastguard Worker cp_blkaddr += 1 << get_sb(log_blocks_per_seg);
3461*59bfda1fSAndroid Build Coastguard Worker
3462*59bfda1fSAndroid Build Coastguard Worker /* write the first cp block in this CP pack */
3463*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(cp, cp_blkaddr, WRITE_LIFE_NONE);
3464*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3465*59bfda1fSAndroid Build Coastguard Worker
3466*59bfda1fSAndroid Build Coastguard Worker /* write the second cp block in this CP pack */
3467*59bfda1fSAndroid Build Coastguard Worker cp_blkaddr += get_cp(cp_pack_total_block_count) - 1;
3468*59bfda1fSAndroid Build Coastguard Worker ret = dev_write_block(cp, cp_blkaddr, WRITE_LIFE_NONE);
3469*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3470*59bfda1fSAndroid Build Coastguard Worker }
3471*59bfda1fSAndroid Build Coastguard Worker
build_nat_area_bitmap(struct f2fs_sb_info * sbi)3472*59bfda1fSAndroid Build Coastguard Worker void build_nat_area_bitmap(struct f2fs_sb_info *sbi)
3473*59bfda1fSAndroid Build Coastguard Worker {
3474*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3475*59bfda1fSAndroid Build Coastguard Worker struct f2fs_journal *journal = F2FS_SUMMARY_BLOCK_JOURNAL(curseg->sum_blk);
3476*59bfda1fSAndroid Build Coastguard Worker struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3477*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3478*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
3479*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_block *nat_block;
3480*59bfda1fSAndroid Build Coastguard Worker struct node_info ni;
3481*59bfda1fSAndroid Build Coastguard Worker u32 nid, nr_nat_blks;
3482*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_off;
3483*59bfda1fSAndroid Build Coastguard Worker pgoff_t block_addr;
3484*59bfda1fSAndroid Build Coastguard Worker int seg_off;
3485*59bfda1fSAndroid Build Coastguard Worker int ret;
3486*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
3487*59bfda1fSAndroid Build Coastguard Worker
3488*59bfda1fSAndroid Build Coastguard Worker nat_block = (struct f2fs_nat_block *)calloc(F2FS_BLKSIZE, 1);
3489*59bfda1fSAndroid Build Coastguard Worker ASSERT(nat_block);
3490*59bfda1fSAndroid Build Coastguard Worker
3491*59bfda1fSAndroid Build Coastguard Worker /* Alloc & build nat entry bitmap */
3492*59bfda1fSAndroid Build Coastguard Worker nr_nat_blks = (get_sb(segment_count_nat) / 2) <<
3493*59bfda1fSAndroid Build Coastguard Worker sbi->log_blocks_per_seg;
3494*59bfda1fSAndroid Build Coastguard Worker
3495*59bfda1fSAndroid Build Coastguard Worker fsck->nr_nat_entries = nr_nat_blks * NAT_ENTRY_PER_BLOCK;
3496*59bfda1fSAndroid Build Coastguard Worker fsck->nat_area_bitmap_sz = (fsck->nr_nat_entries + 7) / 8;
3497*59bfda1fSAndroid Build Coastguard Worker fsck->nat_area_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
3498*59bfda1fSAndroid Build Coastguard Worker ASSERT(fsck->nat_area_bitmap);
3499*59bfda1fSAndroid Build Coastguard Worker
3500*59bfda1fSAndroid Build Coastguard Worker fsck->entries = calloc(sizeof(struct f2fs_nat_entry),
3501*59bfda1fSAndroid Build Coastguard Worker fsck->nr_nat_entries);
3502*59bfda1fSAndroid Build Coastguard Worker ASSERT(fsck->entries);
3503*59bfda1fSAndroid Build Coastguard Worker
3504*59bfda1fSAndroid Build Coastguard Worker for (block_off = 0; block_off < nr_nat_blks; block_off++) {
3505*59bfda1fSAndroid Build Coastguard Worker
3506*59bfda1fSAndroid Build Coastguard Worker seg_off = block_off >> sbi->log_blocks_per_seg;
3507*59bfda1fSAndroid Build Coastguard Worker block_addr = (pgoff_t)(nm_i->nat_blkaddr +
3508*59bfda1fSAndroid Build Coastguard Worker (seg_off << sbi->log_blocks_per_seg << 1) +
3509*59bfda1fSAndroid Build Coastguard Worker (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
3510*59bfda1fSAndroid Build Coastguard Worker
3511*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
3512*59bfda1fSAndroid Build Coastguard Worker block_addr += sbi->blocks_per_seg;
3513*59bfda1fSAndroid Build Coastguard Worker
3514*59bfda1fSAndroid Build Coastguard Worker ret = dev_read_block(nat_block, block_addr);
3515*59bfda1fSAndroid Build Coastguard Worker ASSERT(ret >= 0);
3516*59bfda1fSAndroid Build Coastguard Worker
3517*59bfda1fSAndroid Build Coastguard Worker nid = block_off * NAT_ENTRY_PER_BLOCK;
3518*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NAT_ENTRY_PER_BLOCK; i++) {
3519*59bfda1fSAndroid Build Coastguard Worker ni.nid = nid + i;
3520*59bfda1fSAndroid Build Coastguard Worker
3521*59bfda1fSAndroid Build Coastguard Worker if ((nid + i) == F2FS_NODE_INO(sbi) ||
3522*59bfda1fSAndroid Build Coastguard Worker (nid + i) == F2FS_META_INO(sbi)) {
3523*59bfda1fSAndroid Build Coastguard Worker /*
3524*59bfda1fSAndroid Build Coastguard Worker * block_addr of node/meta inode should be 0x1.
3525*59bfda1fSAndroid Build Coastguard Worker * Set this bit, and fsck_verify will fix it.
3526*59bfda1fSAndroid Build Coastguard Worker */
3527*59bfda1fSAndroid Build Coastguard Worker if (le32_to_cpu(nat_block->entries[i].block_addr) != 0x1) {
3528*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tError: ino[0x%x] block_addr[0x%x] is invalid\n",
3529*59bfda1fSAndroid Build Coastguard Worker nid + i, le32_to_cpu(nat_block->entries[i].block_addr));
3530*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(nid + i, fsck->nat_area_bitmap);
3531*59bfda1fSAndroid Build Coastguard Worker }
3532*59bfda1fSAndroid Build Coastguard Worker continue;
3533*59bfda1fSAndroid Build Coastguard Worker }
3534*59bfda1fSAndroid Build Coastguard Worker
3535*59bfda1fSAndroid Build Coastguard Worker node_info_from_raw_nat(&ni, &nat_block->entries[i]);
3536*59bfda1fSAndroid Build Coastguard Worker if (ni.blk_addr == 0x0)
3537*59bfda1fSAndroid Build Coastguard Worker continue;
3538*59bfda1fSAndroid Build Coastguard Worker if (ni.ino == 0x0) {
3539*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]"
3540*59bfda1fSAndroid Build Coastguard Worker " is invalid\n", ni.ino, ni.blk_addr);
3541*59bfda1fSAndroid Build Coastguard Worker }
3542*59bfda1fSAndroid Build Coastguard Worker if (ni.ino == (nid + i)) {
3543*59bfda1fSAndroid Build Coastguard Worker fsck->nat_valid_inode_cnt++;
3544*59bfda1fSAndroid Build Coastguard Worker DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
3545*59bfda1fSAndroid Build Coastguard Worker }
3546*59bfda1fSAndroid Build Coastguard Worker if (nid + i == 0) {
3547*59bfda1fSAndroid Build Coastguard Worker /*
3548*59bfda1fSAndroid Build Coastguard Worker * nat entry [0] must be null. If
3549*59bfda1fSAndroid Build Coastguard Worker * it is corrupted, set its bit in
3550*59bfda1fSAndroid Build Coastguard Worker * nat_area_bitmap, fsck_verify will
3551*59bfda1fSAndroid Build Coastguard Worker * nullify it
3552*59bfda1fSAndroid Build Coastguard Worker */
3553*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("Invalid nat entry[0]: "
3554*59bfda1fSAndroid Build Coastguard Worker "blk_addr[0x%x]\n", ni.blk_addr);
3555*59bfda1fSAndroid Build Coastguard Worker fsck->chk.valid_nat_entry_cnt--;
3556*59bfda1fSAndroid Build Coastguard Worker }
3557*59bfda1fSAndroid Build Coastguard Worker
3558*59bfda1fSAndroid Build Coastguard Worker DBG(3, "nid[0x%8x] addr[0x%16x] ino[0x%8x]\n",
3559*59bfda1fSAndroid Build Coastguard Worker nid + i, ni.blk_addr, ni.ino);
3560*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(nid + i, fsck->nat_area_bitmap);
3561*59bfda1fSAndroid Build Coastguard Worker fsck->chk.valid_nat_entry_cnt++;
3562*59bfda1fSAndroid Build Coastguard Worker
3563*59bfda1fSAndroid Build Coastguard Worker fsck->entries[nid + i] = nat_block->entries[i];
3564*59bfda1fSAndroid Build Coastguard Worker }
3565*59bfda1fSAndroid Build Coastguard Worker }
3566*59bfda1fSAndroid Build Coastguard Worker
3567*59bfda1fSAndroid Build Coastguard Worker /* Traverse nat journal, update the corresponding entries */
3568*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < nats_in_cursum(journal); i++) {
3569*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nat_entry raw_nat;
3570*59bfda1fSAndroid Build Coastguard Worker nid = le32_to_cpu(nid_in_journal(journal, i));
3571*59bfda1fSAndroid Build Coastguard Worker ni.nid = nid;
3572*59bfda1fSAndroid Build Coastguard Worker
3573*59bfda1fSAndroid Build Coastguard Worker DBG(3, "==> Found nid [0x%x] in nat cache, update it\n", nid);
3574*59bfda1fSAndroid Build Coastguard Worker
3575*59bfda1fSAndroid Build Coastguard Worker /* Clear the original bit and count */
3576*59bfda1fSAndroid Build Coastguard Worker if (fsck->entries[nid].block_addr != 0x0) {
3577*59bfda1fSAndroid Build Coastguard Worker fsck->chk.valid_nat_entry_cnt--;
3578*59bfda1fSAndroid Build Coastguard Worker f2fs_clear_bit(nid, fsck->nat_area_bitmap);
3579*59bfda1fSAndroid Build Coastguard Worker if (fsck->entries[nid].ino == nid)
3580*59bfda1fSAndroid Build Coastguard Worker fsck->nat_valid_inode_cnt--;
3581*59bfda1fSAndroid Build Coastguard Worker }
3582*59bfda1fSAndroid Build Coastguard Worker
3583*59bfda1fSAndroid Build Coastguard Worker /* Use nat entries in journal */
3584*59bfda1fSAndroid Build Coastguard Worker memcpy(&raw_nat, &nat_in_journal(journal, i),
3585*59bfda1fSAndroid Build Coastguard Worker sizeof(struct f2fs_nat_entry));
3586*59bfda1fSAndroid Build Coastguard Worker node_info_from_raw_nat(&ni, &raw_nat);
3587*59bfda1fSAndroid Build Coastguard Worker if (ni.blk_addr != 0x0) {
3588*59bfda1fSAndroid Build Coastguard Worker if (ni.ino == 0x0)
3589*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tError: ino[0x%8x] or blk_addr[0x%16x]"
3590*59bfda1fSAndroid Build Coastguard Worker " is invalid\n", ni.ino, ni.blk_addr);
3591*59bfda1fSAndroid Build Coastguard Worker if (ni.ino == nid) {
3592*59bfda1fSAndroid Build Coastguard Worker fsck->nat_valid_inode_cnt++;
3593*59bfda1fSAndroid Build Coastguard Worker DBG(3, "ino[0x%8x] maybe is inode\n", ni.ino);
3594*59bfda1fSAndroid Build Coastguard Worker }
3595*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(nid, fsck->nat_area_bitmap);
3596*59bfda1fSAndroid Build Coastguard Worker fsck->chk.valid_nat_entry_cnt++;
3597*59bfda1fSAndroid Build Coastguard Worker DBG(3, "nid[0x%x] in nat cache\n", nid);
3598*59bfda1fSAndroid Build Coastguard Worker }
3599*59bfda1fSAndroid Build Coastguard Worker fsck->entries[nid] = raw_nat;
3600*59bfda1fSAndroid Build Coastguard Worker }
3601*59bfda1fSAndroid Build Coastguard Worker free(nat_block);
3602*59bfda1fSAndroid Build Coastguard Worker
3603*59bfda1fSAndroid Build Coastguard Worker DBG(1, "valid nat entries (block_addr != 0x0) [0x%8x : %u]\n",
3604*59bfda1fSAndroid Build Coastguard Worker fsck->chk.valid_nat_entry_cnt,
3605*59bfda1fSAndroid Build Coastguard Worker fsck->chk.valid_nat_entry_cnt);
3606*59bfda1fSAndroid Build Coastguard Worker }
3607*59bfda1fSAndroid Build Coastguard Worker
check_sector_size(struct f2fs_super_block * sb)3608*59bfda1fSAndroid Build Coastguard Worker static int check_sector_size(struct f2fs_super_block *sb)
3609*59bfda1fSAndroid Build Coastguard Worker {
3610*59bfda1fSAndroid Build Coastguard Worker uint32_t log_sectorsize, log_sectors_per_block;
3611*59bfda1fSAndroid Build Coastguard Worker
3612*59bfda1fSAndroid Build Coastguard Worker log_sectorsize = log_base_2(c.sector_size);
3613*59bfda1fSAndroid Build Coastguard Worker log_sectors_per_block = log_base_2(c.sectors_per_blk);
3614*59bfda1fSAndroid Build Coastguard Worker
3615*59bfda1fSAndroid Build Coastguard Worker if (log_sectorsize == get_sb(log_sectorsize) &&
3616*59bfda1fSAndroid Build Coastguard Worker log_sectors_per_block == get_sb(log_sectors_per_block))
3617*59bfda1fSAndroid Build Coastguard Worker return 0;
3618*59bfda1fSAndroid Build Coastguard Worker
3619*59bfda1fSAndroid Build Coastguard Worker set_sb(log_sectorsize, log_sectorsize);
3620*59bfda1fSAndroid Build Coastguard Worker set_sb(log_sectors_per_block, log_sectors_per_block);
3621*59bfda1fSAndroid Build Coastguard Worker
3622*59bfda1fSAndroid Build Coastguard Worker update_superblock(sb, SB_MASK_ALL);
3623*59bfda1fSAndroid Build Coastguard Worker return 0;
3624*59bfda1fSAndroid Build Coastguard Worker }
3625*59bfda1fSAndroid Build Coastguard Worker
tune_sb_features(struct f2fs_sb_info * sbi)3626*59bfda1fSAndroid Build Coastguard Worker static int tune_sb_features(struct f2fs_sb_info *sbi)
3627*59bfda1fSAndroid Build Coastguard Worker {
3628*59bfda1fSAndroid Build Coastguard Worker int sb_changed = 0;
3629*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
3630*59bfda1fSAndroid Build Coastguard Worker
3631*59bfda1fSAndroid Build Coastguard Worker if (!(get_sb(feature) & F2FS_FEATURE_ENCRYPT) &&
3632*59bfda1fSAndroid Build Coastguard Worker c.feature & F2FS_FEATURE_ENCRYPT) {
3633*59bfda1fSAndroid Build Coastguard Worker sb->feature = cpu_to_le32(get_sb(feature) |
3634*59bfda1fSAndroid Build Coastguard Worker F2FS_FEATURE_ENCRYPT);
3635*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Set Encryption feature\n");
3636*59bfda1fSAndroid Build Coastguard Worker sb_changed = 1;
3637*59bfda1fSAndroid Build Coastguard Worker }
3638*59bfda1fSAndroid Build Coastguard Worker if (!(get_sb(feature) & F2FS_FEATURE_CASEFOLD) &&
3639*59bfda1fSAndroid Build Coastguard Worker c.feature & F2FS_FEATURE_CASEFOLD) {
3640*59bfda1fSAndroid Build Coastguard Worker if (!c.s_encoding) {
3641*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("ERROR: Must specify encoding to enable casefolding.\n");
3642*59bfda1fSAndroid Build Coastguard Worker return -1;
3643*59bfda1fSAndroid Build Coastguard Worker }
3644*59bfda1fSAndroid Build Coastguard Worker sb->feature = cpu_to_le32(get_sb(feature) |
3645*59bfda1fSAndroid Build Coastguard Worker F2FS_FEATURE_CASEFOLD);
3646*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: Set Casefold feature\n");
3647*59bfda1fSAndroid Build Coastguard Worker sb_changed = 1;
3648*59bfda1fSAndroid Build Coastguard Worker }
3649*59bfda1fSAndroid Build Coastguard Worker /* TODO: quota needs to allocate inode numbers */
3650*59bfda1fSAndroid Build Coastguard Worker
3651*59bfda1fSAndroid Build Coastguard Worker c.feature = get_sb(feature);
3652*59bfda1fSAndroid Build Coastguard Worker if (!sb_changed)
3653*59bfda1fSAndroid Build Coastguard Worker return 0;
3654*59bfda1fSAndroid Build Coastguard Worker
3655*59bfda1fSAndroid Build Coastguard Worker update_superblock(sb, SB_MASK_ALL);
3656*59bfda1fSAndroid Build Coastguard Worker return 0;
3657*59bfda1fSAndroid Build Coastguard Worker }
3658*59bfda1fSAndroid Build Coastguard Worker
get_fsync_inode(struct list_head * head,nid_t ino)3659*59bfda1fSAndroid Build Coastguard Worker static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
3660*59bfda1fSAndroid Build Coastguard Worker nid_t ino)
3661*59bfda1fSAndroid Build Coastguard Worker {
3662*59bfda1fSAndroid Build Coastguard Worker struct fsync_inode_entry *entry;
3663*59bfda1fSAndroid Build Coastguard Worker
3664*59bfda1fSAndroid Build Coastguard Worker list_for_each_entry(entry, head, list)
3665*59bfda1fSAndroid Build Coastguard Worker if (entry->ino == ino)
3666*59bfda1fSAndroid Build Coastguard Worker return entry;
3667*59bfda1fSAndroid Build Coastguard Worker
3668*59bfda1fSAndroid Build Coastguard Worker return NULL;
3669*59bfda1fSAndroid Build Coastguard Worker }
3670*59bfda1fSAndroid Build Coastguard Worker
add_fsync_inode(struct list_head * head,nid_t ino)3671*59bfda1fSAndroid Build Coastguard Worker static struct fsync_inode_entry *add_fsync_inode(struct list_head *head,
3672*59bfda1fSAndroid Build Coastguard Worker nid_t ino)
3673*59bfda1fSAndroid Build Coastguard Worker {
3674*59bfda1fSAndroid Build Coastguard Worker struct fsync_inode_entry *entry;
3675*59bfda1fSAndroid Build Coastguard Worker
3676*59bfda1fSAndroid Build Coastguard Worker entry = calloc(sizeof(struct fsync_inode_entry), 1);
3677*59bfda1fSAndroid Build Coastguard Worker if (!entry)
3678*59bfda1fSAndroid Build Coastguard Worker return NULL;
3679*59bfda1fSAndroid Build Coastguard Worker entry->ino = ino;
3680*59bfda1fSAndroid Build Coastguard Worker list_add_tail(&entry->list, head);
3681*59bfda1fSAndroid Build Coastguard Worker return entry;
3682*59bfda1fSAndroid Build Coastguard Worker }
3683*59bfda1fSAndroid Build Coastguard Worker
del_fsync_inode(struct fsync_inode_entry * entry)3684*59bfda1fSAndroid Build Coastguard Worker static void del_fsync_inode(struct fsync_inode_entry *entry)
3685*59bfda1fSAndroid Build Coastguard Worker {
3686*59bfda1fSAndroid Build Coastguard Worker list_del(&entry->list);
3687*59bfda1fSAndroid Build Coastguard Worker free(entry);
3688*59bfda1fSAndroid Build Coastguard Worker }
3689*59bfda1fSAndroid Build Coastguard Worker
destroy_fsync_dnodes(struct list_head * head)3690*59bfda1fSAndroid Build Coastguard Worker static void destroy_fsync_dnodes(struct list_head *head)
3691*59bfda1fSAndroid Build Coastguard Worker {
3692*59bfda1fSAndroid Build Coastguard Worker struct fsync_inode_entry *entry, *tmp;
3693*59bfda1fSAndroid Build Coastguard Worker
3694*59bfda1fSAndroid Build Coastguard Worker list_for_each_entry_safe(entry, tmp, head, list)
3695*59bfda1fSAndroid Build Coastguard Worker del_fsync_inode(entry);
3696*59bfda1fSAndroid Build Coastguard Worker }
3697*59bfda1fSAndroid Build Coastguard Worker
loop_node_chain_fix(block_t blkaddr_fast,struct f2fs_node * node_blk_fast,block_t blkaddr,struct f2fs_node * node_blk)3698*59bfda1fSAndroid Build Coastguard Worker static int loop_node_chain_fix(block_t blkaddr_fast,
3699*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node_blk_fast,
3700*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr, struct f2fs_node *node_blk)
3701*59bfda1fSAndroid Build Coastguard Worker {
3702*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr_entry, blkaddr_tmp;
3703*59bfda1fSAndroid Build Coastguard Worker enum rw_hint whint;
3704*59bfda1fSAndroid Build Coastguard Worker int err;
3705*59bfda1fSAndroid Build Coastguard Worker
3706*59bfda1fSAndroid Build Coastguard Worker /* find the entry point of the looped node chain */
3707*59bfda1fSAndroid Build Coastguard Worker while (blkaddr_fast != blkaddr) {
3708*59bfda1fSAndroid Build Coastguard Worker err = dev_read_block(node_blk_fast, blkaddr_fast);
3709*59bfda1fSAndroid Build Coastguard Worker if (err)
3710*59bfda1fSAndroid Build Coastguard Worker return err;
3711*59bfda1fSAndroid Build Coastguard Worker blkaddr_fast = next_blkaddr_of_node(node_blk_fast);
3712*59bfda1fSAndroid Build Coastguard Worker
3713*59bfda1fSAndroid Build Coastguard Worker err = dev_read_block(node_blk, blkaddr);
3714*59bfda1fSAndroid Build Coastguard Worker if (err)
3715*59bfda1fSAndroid Build Coastguard Worker return err;
3716*59bfda1fSAndroid Build Coastguard Worker blkaddr = next_blkaddr_of_node(node_blk);
3717*59bfda1fSAndroid Build Coastguard Worker }
3718*59bfda1fSAndroid Build Coastguard Worker blkaddr_entry = blkaddr;
3719*59bfda1fSAndroid Build Coastguard Worker
3720*59bfda1fSAndroid Build Coastguard Worker /* find the last node of the chain */
3721*59bfda1fSAndroid Build Coastguard Worker do {
3722*59bfda1fSAndroid Build Coastguard Worker blkaddr_tmp = blkaddr;
3723*59bfda1fSAndroid Build Coastguard Worker err = dev_read_block(node_blk, blkaddr);
3724*59bfda1fSAndroid Build Coastguard Worker if (err)
3725*59bfda1fSAndroid Build Coastguard Worker return err;
3726*59bfda1fSAndroid Build Coastguard Worker blkaddr = next_blkaddr_of_node(node_blk);
3727*59bfda1fSAndroid Build Coastguard Worker } while (blkaddr != blkaddr_entry);
3728*59bfda1fSAndroid Build Coastguard Worker
3729*59bfda1fSAndroid Build Coastguard Worker /* fix the blkaddr of last node with NULL_ADDR. */
3730*59bfda1fSAndroid Build Coastguard Worker F2FS_NODE_FOOTER(node_blk)->next_blkaddr = NULL_ADDR;
3731*59bfda1fSAndroid Build Coastguard Worker whint = f2fs_io_type_to_rw_hint(CURSEG_WARM_NODE);
3732*59bfda1fSAndroid Build Coastguard Worker if (IS_INODE(node_blk))
3733*59bfda1fSAndroid Build Coastguard Worker err = write_inode(node_blk, blkaddr_tmp, whint);
3734*59bfda1fSAndroid Build Coastguard Worker else
3735*59bfda1fSAndroid Build Coastguard Worker err = dev_write_block(node_blk, blkaddr_tmp, whint);
3736*59bfda1fSAndroid Build Coastguard Worker if (!err)
3737*59bfda1fSAndroid Build Coastguard Worker FIX_MSG("Fix looped node chain on blkaddr %u\n",
3738*59bfda1fSAndroid Build Coastguard Worker blkaddr_tmp);
3739*59bfda1fSAndroid Build Coastguard Worker return err;
3740*59bfda1fSAndroid Build Coastguard Worker }
3741*59bfda1fSAndroid Build Coastguard Worker
3742*59bfda1fSAndroid Build Coastguard Worker /* Detect looped node chain with Floyd's cycle detection algorithm. */
sanity_check_node_chain(struct f2fs_sb_info * sbi,block_t * blkaddr_fast,struct f2fs_node * node_blk_fast,block_t blkaddr,struct f2fs_node * node_blk,bool * is_detecting)3743*59bfda1fSAndroid Build Coastguard Worker static int sanity_check_node_chain(struct f2fs_sb_info *sbi,
3744*59bfda1fSAndroid Build Coastguard Worker block_t *blkaddr_fast, struct f2fs_node *node_blk_fast,
3745*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr, struct f2fs_node *node_blk,
3746*59bfda1fSAndroid Build Coastguard Worker bool *is_detecting)
3747*59bfda1fSAndroid Build Coastguard Worker {
3748*59bfda1fSAndroid Build Coastguard Worker int i, err;
3749*59bfda1fSAndroid Build Coastguard Worker
3750*59bfda1fSAndroid Build Coastguard Worker if (!*is_detecting)
3751*59bfda1fSAndroid Build Coastguard Worker return 0;
3752*59bfda1fSAndroid Build Coastguard Worker
3753*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < 2; i++) {
3754*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_is_valid_blkaddr(sbi, *blkaddr_fast, META_POR)) {
3755*59bfda1fSAndroid Build Coastguard Worker *is_detecting = false;
3756*59bfda1fSAndroid Build Coastguard Worker return 0;
3757*59bfda1fSAndroid Build Coastguard Worker }
3758*59bfda1fSAndroid Build Coastguard Worker
3759*59bfda1fSAndroid Build Coastguard Worker err = dev_read_block(node_blk_fast, *blkaddr_fast);
3760*59bfda1fSAndroid Build Coastguard Worker if (err)
3761*59bfda1fSAndroid Build Coastguard Worker return err;
3762*59bfda1fSAndroid Build Coastguard Worker
3763*59bfda1fSAndroid Build Coastguard Worker if (!is_recoverable_dnode(sbi, node_blk_fast)) {
3764*59bfda1fSAndroid Build Coastguard Worker *is_detecting = false;
3765*59bfda1fSAndroid Build Coastguard Worker return 0;
3766*59bfda1fSAndroid Build Coastguard Worker }
3767*59bfda1fSAndroid Build Coastguard Worker
3768*59bfda1fSAndroid Build Coastguard Worker *blkaddr_fast = next_blkaddr_of_node(node_blk_fast);
3769*59bfda1fSAndroid Build Coastguard Worker }
3770*59bfda1fSAndroid Build Coastguard Worker
3771*59bfda1fSAndroid Build Coastguard Worker if (*blkaddr_fast != blkaddr)
3772*59bfda1fSAndroid Build Coastguard Worker return 0;
3773*59bfda1fSAndroid Build Coastguard Worker
3774*59bfda1fSAndroid Build Coastguard Worker ASSERT_MSG("\tdetect looped node chain, blkaddr:%u\n", blkaddr);
3775*59bfda1fSAndroid Build Coastguard Worker
3776*59bfda1fSAndroid Build Coastguard Worker /* return -ELOOP will coninue fsck rather than exiting directly */
3777*59bfda1fSAndroid Build Coastguard Worker if (!c.fix_on)
3778*59bfda1fSAndroid Build Coastguard Worker return -ELOOP;
3779*59bfda1fSAndroid Build Coastguard Worker
3780*59bfda1fSAndroid Build Coastguard Worker err = loop_node_chain_fix(NEXT_FREE_BLKADDR(sbi,
3781*59bfda1fSAndroid Build Coastguard Worker CURSEG_I(sbi, CURSEG_WARM_NODE)),
3782*59bfda1fSAndroid Build Coastguard Worker node_blk_fast, blkaddr, node_blk);
3783*59bfda1fSAndroid Build Coastguard Worker if (err)
3784*59bfda1fSAndroid Build Coastguard Worker return err;
3785*59bfda1fSAndroid Build Coastguard Worker
3786*59bfda1fSAndroid Build Coastguard Worker /* Since we call get_fsync_inode() to ensure there are no
3787*59bfda1fSAndroid Build Coastguard Worker * duplicate inodes in the inode_list even if there are
3788*59bfda1fSAndroid Build Coastguard Worker * duplicate blkaddr, we can continue running after fixing the
3789*59bfda1fSAndroid Build Coastguard Worker * looped node chain.
3790*59bfda1fSAndroid Build Coastguard Worker */
3791*59bfda1fSAndroid Build Coastguard Worker *is_detecting = false;
3792*59bfda1fSAndroid Build Coastguard Worker
3793*59bfda1fSAndroid Build Coastguard Worker return 0;
3794*59bfda1fSAndroid Build Coastguard Worker }
3795*59bfda1fSAndroid Build Coastguard Worker
find_fsync_inode(struct f2fs_sb_info * sbi,struct list_head * head)3796*59bfda1fSAndroid Build Coastguard Worker static int find_fsync_inode(struct f2fs_sb_info *sbi, struct list_head *head)
3797*59bfda1fSAndroid Build Coastguard Worker {
3798*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg;
3799*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node_blk, *node_blk_fast;
3800*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr, blkaddr_fast;
3801*59bfda1fSAndroid Build Coastguard Worker bool is_detecting = true;
3802*59bfda1fSAndroid Build Coastguard Worker int err = 0;
3803*59bfda1fSAndroid Build Coastguard Worker
3804*59bfda1fSAndroid Build Coastguard Worker node_blk = calloc(F2FS_BLKSIZE, 1);
3805*59bfda1fSAndroid Build Coastguard Worker node_blk_fast = calloc(F2FS_BLKSIZE, 1);
3806*59bfda1fSAndroid Build Coastguard Worker ASSERT(node_blk && node_blk_fast);
3807*59bfda1fSAndroid Build Coastguard Worker
3808*59bfda1fSAndroid Build Coastguard Worker /* get node pages in the current segment */
3809*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
3810*59bfda1fSAndroid Build Coastguard Worker blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3811*59bfda1fSAndroid Build Coastguard Worker blkaddr_fast = blkaddr;
3812*59bfda1fSAndroid Build Coastguard Worker
3813*59bfda1fSAndroid Build Coastguard Worker while (1) {
3814*59bfda1fSAndroid Build Coastguard Worker struct fsync_inode_entry *entry;
3815*59bfda1fSAndroid Build Coastguard Worker
3816*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
3817*59bfda1fSAndroid Build Coastguard Worker break;
3818*59bfda1fSAndroid Build Coastguard Worker
3819*59bfda1fSAndroid Build Coastguard Worker err = dev_read_block(node_blk, blkaddr);
3820*59bfda1fSAndroid Build Coastguard Worker if (err)
3821*59bfda1fSAndroid Build Coastguard Worker break;
3822*59bfda1fSAndroid Build Coastguard Worker
3823*59bfda1fSAndroid Build Coastguard Worker if (!is_recoverable_dnode(sbi, node_blk))
3824*59bfda1fSAndroid Build Coastguard Worker break;
3825*59bfda1fSAndroid Build Coastguard Worker
3826*59bfda1fSAndroid Build Coastguard Worker if (!is_fsync_dnode(node_blk))
3827*59bfda1fSAndroid Build Coastguard Worker goto next;
3828*59bfda1fSAndroid Build Coastguard Worker
3829*59bfda1fSAndroid Build Coastguard Worker entry = get_fsync_inode(head, ino_of_node(node_blk));
3830*59bfda1fSAndroid Build Coastguard Worker if (!entry) {
3831*59bfda1fSAndroid Build Coastguard Worker entry = add_fsync_inode(head, ino_of_node(node_blk));
3832*59bfda1fSAndroid Build Coastguard Worker if (!entry) {
3833*59bfda1fSAndroid Build Coastguard Worker err = -1;
3834*59bfda1fSAndroid Build Coastguard Worker break;
3835*59bfda1fSAndroid Build Coastguard Worker }
3836*59bfda1fSAndroid Build Coastguard Worker }
3837*59bfda1fSAndroid Build Coastguard Worker entry->blkaddr = blkaddr;
3838*59bfda1fSAndroid Build Coastguard Worker
3839*59bfda1fSAndroid Build Coastguard Worker if (IS_INODE(node_blk) && is_dent_dnode(node_blk))
3840*59bfda1fSAndroid Build Coastguard Worker entry->last_dentry = blkaddr;
3841*59bfda1fSAndroid Build Coastguard Worker next:
3842*59bfda1fSAndroid Build Coastguard Worker blkaddr = next_blkaddr_of_node(node_blk);
3843*59bfda1fSAndroid Build Coastguard Worker
3844*59bfda1fSAndroid Build Coastguard Worker err = sanity_check_node_chain(sbi, &blkaddr_fast,
3845*59bfda1fSAndroid Build Coastguard Worker node_blk_fast, blkaddr, node_blk,
3846*59bfda1fSAndroid Build Coastguard Worker &is_detecting);
3847*59bfda1fSAndroid Build Coastguard Worker if (err)
3848*59bfda1fSAndroid Build Coastguard Worker break;
3849*59bfda1fSAndroid Build Coastguard Worker }
3850*59bfda1fSAndroid Build Coastguard Worker
3851*59bfda1fSAndroid Build Coastguard Worker free(node_blk_fast);
3852*59bfda1fSAndroid Build Coastguard Worker free(node_blk);
3853*59bfda1fSAndroid Build Coastguard Worker return err;
3854*59bfda1fSAndroid Build Coastguard Worker }
3855*59bfda1fSAndroid Build Coastguard Worker
do_record_fsync_data(struct f2fs_sb_info * sbi,struct f2fs_node * node_blk,block_t blkaddr)3856*59bfda1fSAndroid Build Coastguard Worker static int do_record_fsync_data(struct f2fs_sb_info *sbi,
3857*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node_blk,
3858*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr)
3859*59bfda1fSAndroid Build Coastguard Worker {
3860*59bfda1fSAndroid Build Coastguard Worker unsigned int segno, offset;
3861*59bfda1fSAndroid Build Coastguard Worker struct seg_entry *se;
3862*59bfda1fSAndroid Build Coastguard Worker unsigned int ofs_in_node = 0;
3863*59bfda1fSAndroid Build Coastguard Worker unsigned int start, end;
3864*59bfda1fSAndroid Build Coastguard Worker int err = 0, recorded = 0;
3865*59bfda1fSAndroid Build Coastguard Worker
3866*59bfda1fSAndroid Build Coastguard Worker segno = GET_SEGNO(sbi, blkaddr);
3867*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
3868*59bfda1fSAndroid Build Coastguard Worker offset = OFFSET_IN_SEG(sbi, blkaddr);
3869*59bfda1fSAndroid Build Coastguard Worker
3870*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(offset, (char *)se->cur_valid_map))
3871*59bfda1fSAndroid Build Coastguard Worker return 1;
3872*59bfda1fSAndroid Build Coastguard Worker
3873*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(offset, (char *)se->ckpt_valid_map))
3874*59bfda1fSAndroid Build Coastguard Worker return 1;
3875*59bfda1fSAndroid Build Coastguard Worker
3876*59bfda1fSAndroid Build Coastguard Worker if (!se->ckpt_valid_blocks)
3877*59bfda1fSAndroid Build Coastguard Worker se->ckpt_type = CURSEG_WARM_NODE;
3878*59bfda1fSAndroid Build Coastguard Worker
3879*59bfda1fSAndroid Build Coastguard Worker se->ckpt_valid_blocks++;
3880*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(offset, (char *)se->ckpt_valid_map);
3881*59bfda1fSAndroid Build Coastguard Worker
3882*59bfda1fSAndroid Build Coastguard Worker MSG(1, "do_record_fsync_data: [node] ino = %u, nid = %u, blkaddr = %u\n",
3883*59bfda1fSAndroid Build Coastguard Worker ino_of_node(node_blk), ofs_of_node(node_blk), blkaddr);
3884*59bfda1fSAndroid Build Coastguard Worker
3885*59bfda1fSAndroid Build Coastguard Worker /* inline data */
3886*59bfda1fSAndroid Build Coastguard Worker if (IS_INODE(node_blk) && (node_blk->i.i_inline & F2FS_INLINE_DATA))
3887*59bfda1fSAndroid Build Coastguard Worker return 0;
3888*59bfda1fSAndroid Build Coastguard Worker /* xattr node */
3889*59bfda1fSAndroid Build Coastguard Worker if (ofs_of_node(node_blk) == XATTR_NODE_OFFSET)
3890*59bfda1fSAndroid Build Coastguard Worker return 0;
3891*59bfda1fSAndroid Build Coastguard Worker
3892*59bfda1fSAndroid Build Coastguard Worker /* step 3: recover data indices */
3893*59bfda1fSAndroid Build Coastguard Worker start = start_bidx_of_node(ofs_of_node(node_blk), node_blk);
3894*59bfda1fSAndroid Build Coastguard Worker end = start + ADDRS_PER_PAGE(sbi, node_blk, NULL);
3895*59bfda1fSAndroid Build Coastguard Worker
3896*59bfda1fSAndroid Build Coastguard Worker for (; start < end; start++, ofs_in_node++) {
3897*59bfda1fSAndroid Build Coastguard Worker blkaddr = datablock_addr(node_blk, ofs_in_node);
3898*59bfda1fSAndroid Build Coastguard Worker
3899*59bfda1fSAndroid Build Coastguard Worker if (!is_valid_data_blkaddr(blkaddr))
3900*59bfda1fSAndroid Build Coastguard Worker continue;
3901*59bfda1fSAndroid Build Coastguard Worker
3902*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR)) {
3903*59bfda1fSAndroid Build Coastguard Worker err = -1;
3904*59bfda1fSAndroid Build Coastguard Worker goto out;
3905*59bfda1fSAndroid Build Coastguard Worker }
3906*59bfda1fSAndroid Build Coastguard Worker
3907*59bfda1fSAndroid Build Coastguard Worker segno = GET_SEGNO(sbi, blkaddr);
3908*59bfda1fSAndroid Build Coastguard Worker se = get_seg_entry(sbi, segno);
3909*59bfda1fSAndroid Build Coastguard Worker offset = OFFSET_IN_SEG(sbi, blkaddr);
3910*59bfda1fSAndroid Build Coastguard Worker
3911*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(offset, (char *)se->cur_valid_map))
3912*59bfda1fSAndroid Build Coastguard Worker continue;
3913*59bfda1fSAndroid Build Coastguard Worker if (f2fs_test_bit(offset, (char *)se->ckpt_valid_map))
3914*59bfda1fSAndroid Build Coastguard Worker continue;
3915*59bfda1fSAndroid Build Coastguard Worker
3916*59bfda1fSAndroid Build Coastguard Worker if (!se->ckpt_valid_blocks)
3917*59bfda1fSAndroid Build Coastguard Worker se->ckpt_type = CURSEG_WARM_DATA;
3918*59bfda1fSAndroid Build Coastguard Worker
3919*59bfda1fSAndroid Build Coastguard Worker se->ckpt_valid_blocks++;
3920*59bfda1fSAndroid Build Coastguard Worker f2fs_set_bit(offset, (char *)se->ckpt_valid_map);
3921*59bfda1fSAndroid Build Coastguard Worker
3922*59bfda1fSAndroid Build Coastguard Worker MSG(1, "do_record_fsync_data: [data] ino = %u, nid = %u, blkaddr = %u\n",
3923*59bfda1fSAndroid Build Coastguard Worker ino_of_node(node_blk), ofs_of_node(node_blk), blkaddr);
3924*59bfda1fSAndroid Build Coastguard Worker
3925*59bfda1fSAndroid Build Coastguard Worker recorded++;
3926*59bfda1fSAndroid Build Coastguard Worker }
3927*59bfda1fSAndroid Build Coastguard Worker out:
3928*59bfda1fSAndroid Build Coastguard Worker MSG(1, "recover_data: ino = %u, nid = %u, recorded = %d, err = %d\n",
3929*59bfda1fSAndroid Build Coastguard Worker ino_of_node(node_blk), ofs_of_node(node_blk),
3930*59bfda1fSAndroid Build Coastguard Worker recorded, err);
3931*59bfda1fSAndroid Build Coastguard Worker return err;
3932*59bfda1fSAndroid Build Coastguard Worker }
3933*59bfda1fSAndroid Build Coastguard Worker
traverse_dnodes(struct f2fs_sb_info * sbi,struct list_head * inode_list)3934*59bfda1fSAndroid Build Coastguard Worker static int traverse_dnodes(struct f2fs_sb_info *sbi,
3935*59bfda1fSAndroid Build Coastguard Worker struct list_head *inode_list)
3936*59bfda1fSAndroid Build Coastguard Worker {
3937*59bfda1fSAndroid Build Coastguard Worker struct curseg_info *curseg;
3938*59bfda1fSAndroid Build Coastguard Worker struct f2fs_node *node_blk;
3939*59bfda1fSAndroid Build Coastguard Worker block_t blkaddr;
3940*59bfda1fSAndroid Build Coastguard Worker int err = 0;
3941*59bfda1fSAndroid Build Coastguard Worker
3942*59bfda1fSAndroid Build Coastguard Worker /* get node pages in the current segment */
3943*59bfda1fSAndroid Build Coastguard Worker curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
3944*59bfda1fSAndroid Build Coastguard Worker blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3945*59bfda1fSAndroid Build Coastguard Worker
3946*59bfda1fSAndroid Build Coastguard Worker node_blk = calloc(F2FS_BLKSIZE, 1);
3947*59bfda1fSAndroid Build Coastguard Worker ASSERT(node_blk);
3948*59bfda1fSAndroid Build Coastguard Worker
3949*59bfda1fSAndroid Build Coastguard Worker while (1) {
3950*59bfda1fSAndroid Build Coastguard Worker struct fsync_inode_entry *entry;
3951*59bfda1fSAndroid Build Coastguard Worker
3952*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
3953*59bfda1fSAndroid Build Coastguard Worker break;
3954*59bfda1fSAndroid Build Coastguard Worker
3955*59bfda1fSAndroid Build Coastguard Worker err = dev_read_block(node_blk, blkaddr);
3956*59bfda1fSAndroid Build Coastguard Worker if (err)
3957*59bfda1fSAndroid Build Coastguard Worker break;
3958*59bfda1fSAndroid Build Coastguard Worker
3959*59bfda1fSAndroid Build Coastguard Worker if (!is_recoverable_dnode(sbi, node_blk))
3960*59bfda1fSAndroid Build Coastguard Worker break;
3961*59bfda1fSAndroid Build Coastguard Worker
3962*59bfda1fSAndroid Build Coastguard Worker entry = get_fsync_inode(inode_list,
3963*59bfda1fSAndroid Build Coastguard Worker ino_of_node(node_blk));
3964*59bfda1fSAndroid Build Coastguard Worker if (!entry)
3965*59bfda1fSAndroid Build Coastguard Worker goto next;
3966*59bfda1fSAndroid Build Coastguard Worker
3967*59bfda1fSAndroid Build Coastguard Worker err = do_record_fsync_data(sbi, node_blk, blkaddr);
3968*59bfda1fSAndroid Build Coastguard Worker if (err) {
3969*59bfda1fSAndroid Build Coastguard Worker if (err > 0)
3970*59bfda1fSAndroid Build Coastguard Worker err = 0;
3971*59bfda1fSAndroid Build Coastguard Worker break;
3972*59bfda1fSAndroid Build Coastguard Worker }
3973*59bfda1fSAndroid Build Coastguard Worker
3974*59bfda1fSAndroid Build Coastguard Worker if (entry->blkaddr == blkaddr)
3975*59bfda1fSAndroid Build Coastguard Worker del_fsync_inode(entry);
3976*59bfda1fSAndroid Build Coastguard Worker next:
3977*59bfda1fSAndroid Build Coastguard Worker blkaddr = next_blkaddr_of_node(node_blk);
3978*59bfda1fSAndroid Build Coastguard Worker }
3979*59bfda1fSAndroid Build Coastguard Worker
3980*59bfda1fSAndroid Build Coastguard Worker free(node_blk);
3981*59bfda1fSAndroid Build Coastguard Worker return err;
3982*59bfda1fSAndroid Build Coastguard Worker }
3983*59bfda1fSAndroid Build Coastguard Worker
record_fsync_data(struct f2fs_sb_info * sbi)3984*59bfda1fSAndroid Build Coastguard Worker static int record_fsync_data(struct f2fs_sb_info *sbi)
3985*59bfda1fSAndroid Build Coastguard Worker {
3986*59bfda1fSAndroid Build Coastguard Worker struct list_head inode_list = LIST_HEAD_INIT(inode_list);
3987*59bfda1fSAndroid Build Coastguard Worker int ret;
3988*59bfda1fSAndroid Build Coastguard Worker
3989*59bfda1fSAndroid Build Coastguard Worker if (!need_fsync_data_record(sbi))
3990*59bfda1fSAndroid Build Coastguard Worker return 0;
3991*59bfda1fSAndroid Build Coastguard Worker
3992*59bfda1fSAndroid Build Coastguard Worker ret = find_fsync_inode(sbi, &inode_list);
3993*59bfda1fSAndroid Build Coastguard Worker if (ret)
3994*59bfda1fSAndroid Build Coastguard Worker goto out;
3995*59bfda1fSAndroid Build Coastguard Worker
3996*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK && inode_list.next != &inode_list)
3997*59bfda1fSAndroid Build Coastguard Worker c.roll_forward = 1;
3998*59bfda1fSAndroid Build Coastguard Worker
3999*59bfda1fSAndroid Build Coastguard Worker ret = late_build_segment_manager(sbi);
4000*59bfda1fSAndroid Build Coastguard Worker if (ret < 0) {
4001*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("late_build_segment_manager failed\n");
4002*59bfda1fSAndroid Build Coastguard Worker goto out;
4003*59bfda1fSAndroid Build Coastguard Worker }
4004*59bfda1fSAndroid Build Coastguard Worker
4005*59bfda1fSAndroid Build Coastguard Worker ret = traverse_dnodes(sbi, &inode_list);
4006*59bfda1fSAndroid Build Coastguard Worker out:
4007*59bfda1fSAndroid Build Coastguard Worker destroy_fsync_dnodes(&inode_list);
4008*59bfda1fSAndroid Build Coastguard Worker return ret;
4009*59bfda1fSAndroid Build Coastguard Worker }
4010*59bfda1fSAndroid Build Coastguard Worker
f2fs_do_mount(struct f2fs_sb_info * sbi)4011*59bfda1fSAndroid Build Coastguard Worker int f2fs_do_mount(struct f2fs_sb_info *sbi)
4012*59bfda1fSAndroid Build Coastguard Worker {
4013*59bfda1fSAndroid Build Coastguard Worker struct f2fs_checkpoint *cp = NULL;
4014*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = NULL;
4015*59bfda1fSAndroid Build Coastguard Worker int num_cache_entry = c.cache_config.num_cache_entry;
4016*59bfda1fSAndroid Build Coastguard Worker int ret;
4017*59bfda1fSAndroid Build Coastguard Worker
4018*59bfda1fSAndroid Build Coastguard Worker /* Must not initiate cache until block size is known */
4019*59bfda1fSAndroid Build Coastguard Worker c.cache_config.num_cache_entry = 0;
4020*59bfda1fSAndroid Build Coastguard Worker
4021*59bfda1fSAndroid Build Coastguard Worker sbi->active_logs = NR_CURSEG_TYPE;
4022*59bfda1fSAndroid Build Coastguard Worker ret = validate_super_block(sbi, SB0_ADDR);
4023*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4024*59bfda1fSAndroid Build Coastguard Worker if (!c.sparse_mode) {
4025*59bfda1fSAndroid Build Coastguard Worker /* Assuming 4K Block Size */
4026*59bfda1fSAndroid Build Coastguard Worker c.blksize_bits = 12;
4027*59bfda1fSAndroid Build Coastguard Worker c.blksize = 1 << c.blksize_bits;
4028*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Looking for secondary superblock assuming 4K Block Size\n");
4029*59bfda1fSAndroid Build Coastguard Worker }
4030*59bfda1fSAndroid Build Coastguard Worker ret = validate_super_block(sbi, SB1_ADDR);
4031*59bfda1fSAndroid Build Coastguard Worker if (ret && !c.sparse_mode) {
4032*59bfda1fSAndroid Build Coastguard Worker /* Trying 16K Block Size */
4033*59bfda1fSAndroid Build Coastguard Worker c.blksize_bits = 14;
4034*59bfda1fSAndroid Build Coastguard Worker c.blksize = 1 << c.blksize_bits;
4035*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Looking for secondary superblock assuming 16K Block Size\n");
4036*59bfda1fSAndroid Build Coastguard Worker ret = validate_super_block(sbi, SB1_ADDR);
4037*59bfda1fSAndroid Build Coastguard Worker }
4038*59bfda1fSAndroid Build Coastguard Worker if (ret)
4039*59bfda1fSAndroid Build Coastguard Worker return -1;
4040*59bfda1fSAndroid Build Coastguard Worker }
4041*59bfda1fSAndroid Build Coastguard Worker sb = F2FS_RAW_SUPER(sbi);
4042*59bfda1fSAndroid Build Coastguard Worker c.cache_config.num_cache_entry = num_cache_entry;
4043*59bfda1fSAndroid Build Coastguard Worker
4044*59bfda1fSAndroid Build Coastguard Worker ret = check_sector_size(sb);
4045*59bfda1fSAndroid Build Coastguard Worker if (ret)
4046*59bfda1fSAndroid Build Coastguard Worker return -1;
4047*59bfda1fSAndroid Build Coastguard Worker
4048*59bfda1fSAndroid Build Coastguard Worker print_raw_sb_info(sb);
4049*59bfda1fSAndroid Build Coastguard Worker
4050*59bfda1fSAndroid Build Coastguard Worker init_sb_info(sbi);
4051*59bfda1fSAndroid Build Coastguard Worker
4052*59bfda1fSAndroid Build Coastguard Worker ret = get_valid_checkpoint(sbi);
4053*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4054*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("Can't find valid checkpoint\n");
4055*59bfda1fSAndroid Build Coastguard Worker return -1;
4056*59bfda1fSAndroid Build Coastguard Worker }
4057*59bfda1fSAndroid Build Coastguard Worker
4058*59bfda1fSAndroid Build Coastguard Worker c.bug_on = 0;
4059*59bfda1fSAndroid Build Coastguard Worker
4060*59bfda1fSAndroid Build Coastguard Worker if (sanity_check_ckpt(sbi)) {
4061*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("Checkpoint is polluted\n");
4062*59bfda1fSAndroid Build Coastguard Worker return -1;
4063*59bfda1fSAndroid Build Coastguard Worker }
4064*59bfda1fSAndroid Build Coastguard Worker cp = F2FS_CKPT(sbi);
4065*59bfda1fSAndroid Build Coastguard Worker
4066*59bfda1fSAndroid Build Coastguard Worker if (c.func != FSCK && c.func != DUMP && c.func != INJECT &&
4067*59bfda1fSAndroid Build Coastguard Worker !is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG)) {
4068*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("Mount unclean image to replay log first\n");
4069*59bfda1fSAndroid Build Coastguard Worker return -1;
4070*59bfda1fSAndroid Build Coastguard Worker }
4071*59bfda1fSAndroid Build Coastguard Worker
4072*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK) {
4073*59bfda1fSAndroid Build Coastguard Worker #if defined(__APPLE__)
4074*59bfda1fSAndroid Build Coastguard Worker if (!c.no_kernel_check &&
4075*59bfda1fSAndroid Build Coastguard Worker memcmp(c.sb_version, c.version, VERSION_NAME_LEN)) {
4076*59bfda1fSAndroid Build Coastguard Worker c.auto_fix = 0;
4077*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
4078*59bfda1fSAndroid Build Coastguard Worker memcpy(sbi->raw_super->version,
4079*59bfda1fSAndroid Build Coastguard Worker c.version, VERSION_NAME_LEN);
4080*59bfda1fSAndroid Build Coastguard Worker update_superblock(sbi->raw_super, SB_MASK_ALL);
4081*59bfda1fSAndroid Build Coastguard Worker }
4082*59bfda1fSAndroid Build Coastguard Worker #else
4083*59bfda1fSAndroid Build Coastguard Worker if (!c.no_kernel_check) {
4084*59bfda1fSAndroid Build Coastguard Worker u32 prev_time, cur_time, time_diff;
4085*59bfda1fSAndroid Build Coastguard Worker __le32 *ver_ts_ptr = (__le32 *)(sbi->raw_super->version
4086*59bfda1fSAndroid Build Coastguard Worker + VERSION_NAME_LEN);
4087*59bfda1fSAndroid Build Coastguard Worker
4088*59bfda1fSAndroid Build Coastguard Worker cur_time = (u32)get_cp(elapsed_time);
4089*59bfda1fSAndroid Build Coastguard Worker prev_time = le32_to_cpu(*ver_ts_ptr);
4090*59bfda1fSAndroid Build Coastguard Worker
4091*59bfda1fSAndroid Build Coastguard Worker MSG(0, "Info: version timestamp cur: %u, prev: %u\n",
4092*59bfda1fSAndroid Build Coastguard Worker cur_time, prev_time);
4093*59bfda1fSAndroid Build Coastguard Worker if (!memcmp(c.sb_version, c.version,
4094*59bfda1fSAndroid Build Coastguard Worker VERSION_NAME_LEN)) {
4095*59bfda1fSAndroid Build Coastguard Worker /* valid prev_time */
4096*59bfda1fSAndroid Build Coastguard Worker if (prev_time != 0 && cur_time > prev_time) {
4097*59bfda1fSAndroid Build Coastguard Worker time_diff = cur_time - prev_time;
4098*59bfda1fSAndroid Build Coastguard Worker if (time_diff < CHECK_PERIOD)
4099*59bfda1fSAndroid Build Coastguard Worker goto out;
4100*59bfda1fSAndroid Build Coastguard Worker c.auto_fix = 0;
4101*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
4102*59bfda1fSAndroid Build Coastguard Worker }
4103*59bfda1fSAndroid Build Coastguard Worker } else {
4104*59bfda1fSAndroid Build Coastguard Worker memcpy(sbi->raw_super->version,
4105*59bfda1fSAndroid Build Coastguard Worker c.version, VERSION_NAME_LEN);
4106*59bfda1fSAndroid Build Coastguard Worker }
4107*59bfda1fSAndroid Build Coastguard Worker
4108*59bfda1fSAndroid Build Coastguard Worker *ver_ts_ptr = cpu_to_le32(cur_time);
4109*59bfda1fSAndroid Build Coastguard Worker update_superblock(sbi->raw_super, SB_MASK_ALL);
4110*59bfda1fSAndroid Build Coastguard Worker }
4111*59bfda1fSAndroid Build Coastguard Worker #endif
4112*59bfda1fSAndroid Build Coastguard Worker }
4113*59bfda1fSAndroid Build Coastguard Worker out:
4114*59bfda1fSAndroid Build Coastguard Worker print_ckpt_info(sbi);
4115*59bfda1fSAndroid Build Coastguard Worker
4116*59bfda1fSAndroid Build Coastguard Worker if (c.quota_fix) {
4117*59bfda1fSAndroid Build Coastguard Worker if (get_cp(ckpt_flags) & CP_QUOTA_NEED_FSCK_FLAG)
4118*59bfda1fSAndroid Build Coastguard Worker c.fix_on = 1;
4119*59bfda1fSAndroid Build Coastguard Worker }
4120*59bfda1fSAndroid Build Coastguard Worker if (c.layout)
4121*59bfda1fSAndroid Build Coastguard Worker return 1;
4122*59bfda1fSAndroid Build Coastguard Worker
4123*59bfda1fSAndroid Build Coastguard Worker if (tune_sb_features(sbi))
4124*59bfda1fSAndroid Build Coastguard Worker return -1;
4125*59bfda1fSAndroid Build Coastguard Worker
4126*59bfda1fSAndroid Build Coastguard Worker /* precompute checksum seed for metadata */
4127*59bfda1fSAndroid Build Coastguard Worker if (c.feature & F2FS_FEATURE_INODE_CHKSUM)
4128*59bfda1fSAndroid Build Coastguard Worker c.chksum_seed = f2fs_cal_crc32(~0, sb->uuid, sizeof(sb->uuid));
4129*59bfda1fSAndroid Build Coastguard Worker
4130*59bfda1fSAndroid Build Coastguard Worker sbi->total_valid_node_count = get_cp(valid_node_count);
4131*59bfda1fSAndroid Build Coastguard Worker sbi->total_valid_inode_count = get_cp(valid_inode_count);
4132*59bfda1fSAndroid Build Coastguard Worker sbi->user_block_count = get_cp(user_block_count);
4133*59bfda1fSAndroid Build Coastguard Worker sbi->total_valid_block_count = get_cp(valid_block_count);
4134*59bfda1fSAndroid Build Coastguard Worker sbi->last_valid_block_count = sbi->total_valid_block_count;
4135*59bfda1fSAndroid Build Coastguard Worker sbi->alloc_valid_block_count = 0;
4136*59bfda1fSAndroid Build Coastguard Worker
4137*59bfda1fSAndroid Build Coastguard Worker if (early_build_segment_manager(sbi)) {
4138*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("early_build_segment_manager failed\n");
4139*59bfda1fSAndroid Build Coastguard Worker return -1;
4140*59bfda1fSAndroid Build Coastguard Worker }
4141*59bfda1fSAndroid Build Coastguard Worker
4142*59bfda1fSAndroid Build Coastguard Worker if (build_node_manager(sbi)) {
4143*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("build_node_manager failed\n");
4144*59bfda1fSAndroid Build Coastguard Worker return -1;
4145*59bfda1fSAndroid Build Coastguard Worker }
4146*59bfda1fSAndroid Build Coastguard Worker
4147*59bfda1fSAndroid Build Coastguard Worker ret = record_fsync_data(sbi);
4148*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4149*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("record_fsync_data failed\n");
4150*59bfda1fSAndroid Build Coastguard Worker if (ret != -ELOOP)
4151*59bfda1fSAndroid Build Coastguard Worker return -1;
4152*59bfda1fSAndroid Build Coastguard Worker }
4153*59bfda1fSAndroid Build Coastguard Worker
4154*59bfda1fSAndroid Build Coastguard Worker if (!f2fs_should_proceed(sb, get_cp(ckpt_flags)))
4155*59bfda1fSAndroid Build Coastguard Worker return 1;
4156*59bfda1fSAndroid Build Coastguard Worker
4157*59bfda1fSAndroid Build Coastguard Worker if (late_build_segment_manager(sbi) < 0) {
4158*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("late_build_segment_manager failed\n");
4159*59bfda1fSAndroid Build Coastguard Worker return -1;
4160*59bfda1fSAndroid Build Coastguard Worker }
4161*59bfda1fSAndroid Build Coastguard Worker
4162*59bfda1fSAndroid Build Coastguard Worker if (f2fs_late_init_nid_bitmap(sbi)) {
4163*59bfda1fSAndroid Build Coastguard Worker ERR_MSG("f2fs_late_init_nid_bitmap failed\n");
4164*59bfda1fSAndroid Build Coastguard Worker return -1;
4165*59bfda1fSAndroid Build Coastguard Worker }
4166*59bfda1fSAndroid Build Coastguard Worker
4167*59bfda1fSAndroid Build Coastguard Worker /* Check nat_bits */
4168*59bfda1fSAndroid Build Coastguard Worker if (c.func == FSCK && is_set_ckpt_flags(cp, CP_NAT_BITS_FLAG)) {
4169*59bfda1fSAndroid Build Coastguard Worker if (check_nat_bits(sbi, sb, cp) && c.fix_on)
4170*59bfda1fSAndroid Build Coastguard Worker write_nat_bits(sbi, sb, cp, sbi->cur_cp);
4171*59bfda1fSAndroid Build Coastguard Worker }
4172*59bfda1fSAndroid Build Coastguard Worker return 0;
4173*59bfda1fSAndroid Build Coastguard Worker }
4174*59bfda1fSAndroid Build Coastguard Worker
f2fs_do_umount(struct f2fs_sb_info * sbi)4175*59bfda1fSAndroid Build Coastguard Worker void f2fs_do_umount(struct f2fs_sb_info *sbi)
4176*59bfda1fSAndroid Build Coastguard Worker {
4177*59bfda1fSAndroid Build Coastguard Worker struct sit_info *sit_i = SIT_I(sbi);
4178*59bfda1fSAndroid Build Coastguard Worker struct f2fs_sm_info *sm_i = SM_I(sbi);
4179*59bfda1fSAndroid Build Coastguard Worker struct f2fs_nm_info *nm_i = NM_I(sbi);
4180*59bfda1fSAndroid Build Coastguard Worker unsigned int i;
4181*59bfda1fSAndroid Build Coastguard Worker
4182*59bfda1fSAndroid Build Coastguard Worker /* free nm_info */
4183*59bfda1fSAndroid Build Coastguard Worker if (c.func == SLOAD || c.func == FSCK)
4184*59bfda1fSAndroid Build Coastguard Worker free(nm_i->nid_bitmap);
4185*59bfda1fSAndroid Build Coastguard Worker free(nm_i->nat_bitmap);
4186*59bfda1fSAndroid Build Coastguard Worker free(sbi->nm_info);
4187*59bfda1fSAndroid Build Coastguard Worker
4188*59bfda1fSAndroid Build Coastguard Worker /* free sit_info */
4189*59bfda1fSAndroid Build Coastguard Worker free(sit_i->bitmap);
4190*59bfda1fSAndroid Build Coastguard Worker free(sit_i->sit_bitmap);
4191*59bfda1fSAndroid Build Coastguard Worker free(sit_i->sentries);
4192*59bfda1fSAndroid Build Coastguard Worker free(sm_i->sit_info);
4193*59bfda1fSAndroid Build Coastguard Worker
4194*59bfda1fSAndroid Build Coastguard Worker /* free sm_info */
4195*59bfda1fSAndroid Build Coastguard Worker for (i = 0; i < NR_CURSEG_TYPE; i++)
4196*59bfda1fSAndroid Build Coastguard Worker free(sm_i->curseg_array[i].sum_blk);
4197*59bfda1fSAndroid Build Coastguard Worker
4198*59bfda1fSAndroid Build Coastguard Worker free(sm_i->curseg_array);
4199*59bfda1fSAndroid Build Coastguard Worker free(sbi->sm_info);
4200*59bfda1fSAndroid Build Coastguard Worker
4201*59bfda1fSAndroid Build Coastguard Worker free(sbi->ckpt);
4202*59bfda1fSAndroid Build Coastguard Worker free(sbi->raw_super);
4203*59bfda1fSAndroid Build Coastguard Worker }
4204*59bfda1fSAndroid Build Coastguard Worker
4205*59bfda1fSAndroid Build Coastguard Worker #ifdef WITH_ANDROID
f2fs_sparse_initialize_meta(struct f2fs_sb_info * sbi)4206*59bfda1fSAndroid Build Coastguard Worker int f2fs_sparse_initialize_meta(struct f2fs_sb_info *sbi)
4207*59bfda1fSAndroid Build Coastguard Worker {
4208*59bfda1fSAndroid Build Coastguard Worker struct f2fs_super_block *sb = sbi->raw_super;
4209*59bfda1fSAndroid Build Coastguard Worker uint32_t sit_seg_count, sit_size;
4210*59bfda1fSAndroid Build Coastguard Worker uint32_t nat_seg_count, nat_size;
4211*59bfda1fSAndroid Build Coastguard Worker uint64_t sit_seg_addr, nat_seg_addr, payload_addr;
4212*59bfda1fSAndroid Build Coastguard Worker uint32_t seg_size = 1 << get_sb(log_blocks_per_seg);
4213*59bfda1fSAndroid Build Coastguard Worker int ret;
4214*59bfda1fSAndroid Build Coastguard Worker
4215*59bfda1fSAndroid Build Coastguard Worker if (!c.sparse_mode)
4216*59bfda1fSAndroid Build Coastguard Worker return 0;
4217*59bfda1fSAndroid Build Coastguard Worker
4218*59bfda1fSAndroid Build Coastguard Worker sit_seg_addr = get_sb(sit_blkaddr);
4219*59bfda1fSAndroid Build Coastguard Worker sit_seg_count = get_sb(segment_count_sit);
4220*59bfda1fSAndroid Build Coastguard Worker sit_size = sit_seg_count * seg_size;
4221*59bfda1fSAndroid Build Coastguard Worker
4222*59bfda1fSAndroid Build Coastguard Worker DBG(1, "\tSparse: filling sit area at block offset: 0x%08"PRIx64" len: %u\n",
4223*59bfda1fSAndroid Build Coastguard Worker sit_seg_addr, sit_size);
4224*59bfda1fSAndroid Build Coastguard Worker ret = dev_fill(NULL, sit_seg_addr * F2FS_BLKSIZE,
4225*59bfda1fSAndroid Build Coastguard Worker sit_size * F2FS_BLKSIZE, WRITE_LIFE_NONE);
4226*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4227*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: While zeroing out the sit area "
4228*59bfda1fSAndroid Build Coastguard Worker "on disk!!!\n");
4229*59bfda1fSAndroid Build Coastguard Worker return -1;
4230*59bfda1fSAndroid Build Coastguard Worker }
4231*59bfda1fSAndroid Build Coastguard Worker
4232*59bfda1fSAndroid Build Coastguard Worker nat_seg_addr = get_sb(nat_blkaddr);
4233*59bfda1fSAndroid Build Coastguard Worker nat_seg_count = get_sb(segment_count_nat);
4234*59bfda1fSAndroid Build Coastguard Worker nat_size = nat_seg_count * seg_size;
4235*59bfda1fSAndroid Build Coastguard Worker
4236*59bfda1fSAndroid Build Coastguard Worker DBG(1, "\tSparse: filling nat area at block offset 0x%08"PRIx64" len: %u\n",
4237*59bfda1fSAndroid Build Coastguard Worker nat_seg_addr, nat_size);
4238*59bfda1fSAndroid Build Coastguard Worker ret = dev_fill(NULL, nat_seg_addr * F2FS_BLKSIZE,
4239*59bfda1fSAndroid Build Coastguard Worker nat_size * F2FS_BLKSIZE, WRITE_LIFE_NONE);
4240*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4241*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: While zeroing out the nat area "
4242*59bfda1fSAndroid Build Coastguard Worker "on disk!!!\n");
4243*59bfda1fSAndroid Build Coastguard Worker return -1;
4244*59bfda1fSAndroid Build Coastguard Worker }
4245*59bfda1fSAndroid Build Coastguard Worker
4246*59bfda1fSAndroid Build Coastguard Worker payload_addr = get_sb(segment0_blkaddr) + 1;
4247*59bfda1fSAndroid Build Coastguard Worker
4248*59bfda1fSAndroid Build Coastguard Worker DBG(1, "\tSparse: filling bitmap area at block offset 0x%08"PRIx64" len: %u\n",
4249*59bfda1fSAndroid Build Coastguard Worker payload_addr, get_sb(cp_payload));
4250*59bfda1fSAndroid Build Coastguard Worker ret = dev_fill(NULL, payload_addr * F2FS_BLKSIZE,
4251*59bfda1fSAndroid Build Coastguard Worker get_sb(cp_payload) * F2FS_BLKSIZE, WRITE_LIFE_NONE);
4252*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4253*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: While zeroing out the nat/sit bitmap area "
4254*59bfda1fSAndroid Build Coastguard Worker "on disk!!!\n");
4255*59bfda1fSAndroid Build Coastguard Worker return -1;
4256*59bfda1fSAndroid Build Coastguard Worker }
4257*59bfda1fSAndroid Build Coastguard Worker
4258*59bfda1fSAndroid Build Coastguard Worker payload_addr += seg_size;
4259*59bfda1fSAndroid Build Coastguard Worker
4260*59bfda1fSAndroid Build Coastguard Worker DBG(1, "\tSparse: filling bitmap area at block offset 0x%08"PRIx64" len: %u\n",
4261*59bfda1fSAndroid Build Coastguard Worker payload_addr, get_sb(cp_payload));
4262*59bfda1fSAndroid Build Coastguard Worker ret = dev_fill(NULL, payload_addr * F2FS_BLKSIZE,
4263*59bfda1fSAndroid Build Coastguard Worker get_sb(cp_payload) * F2FS_BLKSIZE, WRITE_LIFE_NONE);
4264*59bfda1fSAndroid Build Coastguard Worker if (ret) {
4265*59bfda1fSAndroid Build Coastguard Worker MSG(1, "\tError: While zeroing out the nat/sit bitmap area "
4266*59bfda1fSAndroid Build Coastguard Worker "on disk!!!\n");
4267*59bfda1fSAndroid Build Coastguard Worker return -1;
4268*59bfda1fSAndroid Build Coastguard Worker }
4269*59bfda1fSAndroid Build Coastguard Worker return 0;
4270*59bfda1fSAndroid Build Coastguard Worker }
4271*59bfda1fSAndroid Build Coastguard Worker #else
f2fs_sparse_initialize_meta(struct f2fs_sb_info * sbi)4272*59bfda1fSAndroid Build Coastguard Worker int f2fs_sparse_initialize_meta(struct f2fs_sb_info *sbi) { return 0; }
4273*59bfda1fSAndroid Build Coastguard Worker #endif
4274