2 * Copyright (C) 2014 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #define _LARGEFILE64_SOURCE
32 #include <asm/types.h>
39 #include <string.h> /* memset() */
40 #include <sys/param.h>
42 #include <sys/types.h>
46 #include <f2fs_format_utils.h>
48 #include <sparse/sparse.h>
50 struct selabel_handle;
52 #include "make_f2fs.h"
58 /* These match the Linux definitions of these flags.
59 L_xx is defined to avoid conflicting with the win32 versions.
61 #define L_S_IRUSR 00400
62 #define L_S_IWUSR 00200
63 #define L_S_IXUSR 00100
64 #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
68 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
72 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
73 #define S_ISUID 0004000
74 #define S_ISGID 0002000
75 #define S_ISVTX 0001000
79 #include <selinux/selinux.h>
80 #include <selinux/label.h>
86 struct f2fs_configuration c;
87 struct sparse_file *f2fs_sparse_file;
92 struct buf_item *next;
95 struct buf_item *buf_list;
97 static int __get_device_fd(__u64 *offset)
99 __u64 blk_addr = *offset >> F2FS_BLKSIZE_BITS;
102 for (i = 0; i < c.ndevs; i++) {
103 if (c.devices[i].start_blkaddr <= blk_addr &&
104 c.devices[i].end_blkaddr >= blk_addr) {
106 c.devices[i].start_blkaddr << F2FS_BLKSIZE_BITS;
107 return c.devices[i].fd;
113 static int dev_write_fd(void *buf, __u64 offset, size_t len)
115 int fd = __get_device_fd(&offset);
117 if (lseek64(fd, (off64_t)offset, SEEK_SET) < 0)
119 ssize_t written = write(fd, buf, len);
122 if ((size_t)written != len)
127 void flush_sparse_buffs()
130 struct buf_item *bi = buf_list;
131 buf_list = buf_list->next;
137 static int dev_write_sparse(void *buf, __u64 byte_offset, size_t byte_len)
139 struct buf_item *bi = calloc(1, sizeof(struct buf_item));
144 bi->buf = malloc(byte_len);
145 if (bi->buf == NULL) {
151 memcpy(bi->buf, buf, byte_len);
155 sparse_file_add_data(f2fs_sparse_file, bi->buf, byte_len, byte_offset/F2FS_BLKSIZE);
159 void f2fs_finalize_device(void)
163 int f2fs_trim_devices(void)
171 int dev_read_version(void *buf, __u64 offset, size_t len)
176 int dev_read(void *buf, __u64 offset, size_t len)
181 int dev_readahead(__u64 offset, size_t len)
186 int dev_write(void *buf, __u64 offset, size_t len)
188 int fd = __get_device_fd(&offset);
191 return dev_write_fd(buf, offset, len);
193 return dev_write_sparse(buf, offset, len);
197 int dev_write_block(void *buf, __u64 blk_addr)
199 assert(false); // Must not be invoked.
203 int dev_write_dump(void *buf, __u64 offset, size_t len)
205 assert(false); // Must not be invoked.
209 int dev_fill(void *buf, __u64 offset, size_t len)
211 int fd = __get_device_fd(&offset);
214 return dev_write_fd(buf, offset, len);
216 // sparse file fills with zero by default.
217 // return sparse_file_add_fill(f2fs_sparse_file, ((__u8*)(bi->buf))[0], byte_len, byte_offset/F2FS_BLKSIZE);
221 int dev_fill_block(void *buf, __u64 blk_addr)
223 assert(false); // Must not be invoked.
227 int dev_read_block(void *buf, __u64 blk_addr)
229 assert(false); // Must not be invoked.
233 int dev_read_blocks(void *buf, __u64 addr, __u32 nr_blks)
235 assert(false); // Must not be invoked.
239 int dev_reada_block(__u64 blk_addr)
241 assert(false); // Must not be invoked.