liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
 * libf2fs.c
 *
 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 *
 * Dual licensed under the GPL or LGPL version 2 licenses.
 */
#define _LARGEFILE64_SOURCE
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#include <time.h>
#ifndef ANDROID_WINDOWS_HOST
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#endif
#ifdef HAVE_LINUX_HDREG_H
#include <linux/hdreg.h>
#endif
 
#include <f2fs_fs.h>
 
struct f2fs_configuration c;
 
#ifdef WITH_ANDROID
#include <sparse/sparse.h>
struct sparse_file *f2fs_sparse_file;
static char **blocks;
u_int64_t blocks_count;
#endif
 
static int __get_device_fd(__u64 *offset)
{
   __u64 blk_addr = *offset >> F2FS_BLKSIZE_BITS;
   int i;
 
   for (i = 0; i < c.ndevs; i++) {
       if (c.devices[i].start_blkaddr <= blk_addr &&
               c.devices[i].end_blkaddr >= blk_addr) {
           *offset -=
               c.devices[i].start_blkaddr << F2FS_BLKSIZE_BITS;
           return c.devices[i].fd;
       }
   }
   return -1;
}
 
#ifndef HAVE_LSEEK64
typedef off_t    off64_t;
 
static inline off64_t lseek64(int fd, __u64 offset, int set)
{
   return lseek(fd, offset, set);
}
#endif
 
/*
 * IO interfaces
 */
int dev_read_version(void *buf, __u64 offset, size_t len)
{
   if (c.sparse_mode)
       return 0;
   if (lseek64(c.kd, (off64_t)offset, SEEK_SET) < 0)
       return -1;
   if (read(c.kd, buf, len) < 0)
       return -1;
   return 0;
}
 
#ifdef WITH_ANDROID
static int sparse_read_blk(__u64 block, int count, void *buf)
{
   int i;
   char *out = buf;
   __u64 cur_block;
 
   for (i = 0; i < count; ++i) {
       cur_block = block + i;
       if (blocks[cur_block])
           memcpy(out + (i * F2FS_BLKSIZE),
                   blocks[cur_block], F2FS_BLKSIZE);
       else if (blocks)
           memset(out + (i * F2FS_BLKSIZE), 0, F2FS_BLKSIZE);
   }
   return 0;
}
 
static int sparse_write_blk(__u64 block, int count, const void *buf)
{
   int i;
   __u64 cur_block;
   const char *in = buf;
 
   for (i = 0; i < count; ++i) {
       cur_block = block + i;
       if (!blocks[cur_block]) {
           blocks[cur_block] = calloc(1, F2FS_BLKSIZE);
           if (!blocks[cur_block])
               return -ENOMEM;
       }
       memcpy(blocks[cur_block], in + (i * F2FS_BLKSIZE),
               F2FS_BLKSIZE);
   }
   return 0;
}
 
#ifdef SPARSE_CALLBACK_USES_SIZE_T
static int sparse_import_segment(void *UNUSED(priv), const void *data,
       size_t len, unsigned int block, unsigned int nr_blocks)
#else
static int sparse_import_segment(void *UNUSED(priv), const void *data, int len,
       unsigned int block, unsigned int nr_blocks)
#endif
{
   /* Ignore chunk headers, only write the data */
   if (!nr_blocks || len % F2FS_BLKSIZE)
       return 0;
 
   return sparse_write_blk(block, nr_blocks, data);
}
 
static int sparse_merge_blocks(uint64_t start, uint64_t num)
{
   char *buf;
   uint64_t i;
 
   buf = calloc(num, F2FS_BLKSIZE);
   if (!buf) {
       fprintf(stderr, "failed to alloc %llu\n",
           (unsigned long long)num * F2FS_BLKSIZE);
       return -ENOMEM;
   }
 
   for (i = 0; i < num; i++) {
       memcpy(buf + i * F2FS_BLKSIZE, blocks[start + i], F2FS_BLKSIZE);
       free(blocks[start + i]);
       blocks[start + i] = NULL;
   }
 
   /* free_sparse_blocks will release this buf. */
   blocks[start] = buf;
 
   return sparse_file_add_data(f2fs_sparse_file, blocks[start],
                   F2FS_BLKSIZE * num, start);
}
#else
static int sparse_read_blk(__u64 block, int count, void *buf) { return 0; }
static int sparse_write_blk(__u64 block, int count, const void *buf) { return 0; }
#endif
 
int dev_read(void *buf, __u64 offset, size_t len)
{
   int fd;
 
   if (c.sparse_mode)
       return sparse_read_blk(offset / F2FS_BLKSIZE,
                   len / F2FS_BLKSIZE, buf);
 
   fd = __get_device_fd(&offset);
   if (fd < 0)
       return fd;
 
   if (lseek64(fd, (off64_t)offset, SEEK_SET) < 0)
       return -1;
   if (read(fd, buf, len) < 0)
       return -1;
   return 0;
}
 
#ifdef POSIX_FADV_WILLNEED
int dev_readahead(__u64 offset, size_t len)
#else
int dev_readahead(__u64 offset, size_t UNUSED(len))
#endif
{
   int fd = __get_device_fd(&offset);
 
   if (fd < 0)
       return fd;
#ifdef POSIX_FADV_WILLNEED
   return posix_fadvise(fd, offset, len, POSIX_FADV_WILLNEED);
#else
   return 0;
#endif
}
 
int dev_write(void *buf, __u64 offset, size_t len)
{
   int fd;
 
   if (c.dry_run)
       return 0;
 
   if (c.sparse_mode)
       return sparse_write_blk(offset / F2FS_BLKSIZE,
                   len / F2FS_BLKSIZE, buf);
 
   fd = __get_device_fd(&offset);
   if (fd < 0)
       return fd;
 
   if (lseek64(fd, (off64_t)offset, SEEK_SET) < 0)
       return -1;
   if (write(fd, buf, len) < 0)
       return -1;
   return 0;
}
 
int dev_write_block(void *buf, __u64 blk_addr)
{
   return dev_write(buf, blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
}
 
int dev_write_dump(void *buf, __u64 offset, size_t len)
{
   if (lseek64(c.dump_fd, (off64_t)offset, SEEK_SET) < 0)
       return -1;
   if (write(c.dump_fd, buf, len) < 0)
       return -1;
   return 0;
}
 
int dev_fill(void *buf, __u64 offset, size_t len)
{
   int fd;
 
   if (c.sparse_mode)
       return 0;
 
   fd = __get_device_fd(&offset);
   if (fd < 0)
       return fd;
 
   /* Only allow fill to zero */
   if (*((__u8*)buf))
       return -1;
   if (lseek64(fd, (off64_t)offset, SEEK_SET) < 0)
       return -1;
   if (write(fd, buf, len) < 0)
       return -1;
   return 0;
}
 
int dev_fill_block(void *buf, __u64 blk_addr)
{
   return dev_fill(buf, blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
}
 
int dev_read_block(void *buf, __u64 blk_addr)
{
   return dev_read(buf, blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
}
 
int dev_reada_block(__u64 blk_addr)
{
   return dev_readahead(blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
}
 
int f2fs_fsync_device(void)
{
#ifndef ANDROID_WINDOWS_HOST
   int i;
 
   for (i = 0; i < c.ndevs; i++) {
       if (fsync(c.devices[i].fd) < 0) {
           MSG(0, "\tError: Could not conduct fsync!!!\n");
           return -1;
       }
   }
#endif
   return 0;
}
 
int f2fs_init_sparse_file(void)
{
#ifdef WITH_ANDROID
   if (c.func == MKFS) {
       f2fs_sparse_file = sparse_file_new(F2FS_BLKSIZE, c.device_size);
   } else {
       f2fs_sparse_file = sparse_file_import(c.devices[0].fd,
                           true, false);
       if (!f2fs_sparse_file)
           return -1;
 
       c.device_size = sparse_file_len(f2fs_sparse_file, 0, 0);
       c.device_size &= (~((u_int64_t)(F2FS_BLKSIZE - 1)));
   }
 
   if (sparse_file_block_size(f2fs_sparse_file) != F2FS_BLKSIZE) {
       MSG(0, "\tError: Corrupted sparse file\n");
       return -1;
   }
   blocks_count = c.device_size / F2FS_BLKSIZE;
   blocks = calloc(blocks_count, sizeof(char *));
   if (!blocks) {
       MSG(0, "\tError: Calloc Failed for blocks!!!\n");
       return -1;
   }
 
   return sparse_file_foreach_chunk(f2fs_sparse_file, true, false,
               sparse_import_segment, NULL);
#else
   MSG(0, "\tError: Sparse mode is only supported for android\n");
   return -1;
#endif
}
 
#define MAX_CHUNK_SIZE (1 * 1024 * 1024 * 1024ULL)
int f2fs_finalize_device(void)
{
   int i;
   int ret = 0;
 
#ifdef WITH_ANDROID
   if (c.sparse_mode) {
       int64_t chunk_start = (blocks[0] == NULL) ? -1 : 0;
       uint64_t j;
 
       if (c.func != MKFS) {
           sparse_file_destroy(f2fs_sparse_file);
           ret = ftruncate(c.devices[0].fd, 0);
           ASSERT(!ret);
           lseek(c.devices[0].fd, 0, SEEK_SET);
           f2fs_sparse_file = sparse_file_new(F2FS_BLKSIZE,
                           c.device_size);
       }
 
       for (j = 0; j < blocks_count; ++j) {
           if (!blocks[j] && chunk_start != -1) {
               ret = sparse_merge_blocks(chunk_start,
                           j - chunk_start);
               chunk_start = -1;
           } else if (blocks[j] && chunk_start == -1) {
               chunk_start = j;
           } else if (blocks[j] && (chunk_start != -1) &&
                (j + 1 - chunk_start >=
                   (MAX_CHUNK_SIZE / F2FS_BLKSIZE))) {
               ret = sparse_merge_blocks(chunk_start,
                             j + 1 - chunk_start);
               chunk_start = -1;
           }
           ASSERT(!ret);
       }
       if (chunk_start != -1) {
           ret = sparse_merge_blocks(chunk_start,
                       blocks_count - chunk_start);
           ASSERT(!ret);
       }
 
       sparse_file_write(f2fs_sparse_file, c.devices[0].fd,
               /*gzip*/0, /*sparse*/1, /*crc*/0);
 
       sparse_file_destroy(f2fs_sparse_file);
       for (j = 0; j < blocks_count; j++)
           free(blocks[j]);
       free(blocks);
       blocks = NULL;
       f2fs_sparse_file = NULL;
   }
#endif
   /*
    * We should call fsync() to flush out all the dirty pages
    * in the block device page cache.
    */
   for (i = 0; i < c.ndevs; i++) {
#ifndef ANDROID_WINDOWS_HOST
       ret = fsync(c.devices[i].fd);
       if (ret < 0) {
           MSG(0, "\tError: Could not conduct fsync!!!\n");
           break;
       }
#endif
       ret = close(c.devices[i].fd);
       if (ret < 0) {
           MSG(0, "\tError: Failed to close device file!!!\n");
           break;
       }
       free(c.devices[i].path);
   }
   close(c.kd);
 
   return ret;
}