2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "ext4_utils.h"
20 #include <sparse/sparse.h>
26 /* Creates data buffers for the first backing_len bytes of a block allocation
27 and queues them to be written */
28 static u8 *extent_create_backing(struct block_allocation *alloc,
31 u8 *data = calloc(backing_len, 1);
33 critical_error_errno("calloc");
36 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
40 get_region(alloc, ®ion_block, ®ion_len);
42 len = min(region_len * info.block_size, backing_len);
44 sparse_file_add_data(info.sparse_file, ptr, len, region_block);
52 /* Queues each chunk of a file to be written to contiguous data block
54 static void extent_create_backing_file(struct block_allocation *alloc,
55 u64 backing_len, const char *filename)
58 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
62 get_region(alloc, ®ion_block, ®ion_len);
64 len = min(region_len * info.block_size, backing_len);
66 sparse_file_add_file(info.sparse_file, filename, offset, len,
73 static struct block_allocation *do_inode_allocate_extents(
74 struct ext4_inode *inode, u64 len)
76 u32 block_len = DIV_ROUND_UP(len, info.block_size);
77 struct block_allocation *alloc = allocate_blocks(block_len + 1);
80 struct ext4_extent *extent;
84 error("Failed to allocate %d blocks\n", block_len + 1);
88 int allocation_len = block_allocation_num_regions(alloc);
89 if (allocation_len <= 3) {
90 reduce_allocation(alloc, 1);
92 reserve_oob_blocks(alloc, 1);
93 extent_block = get_oob_block(alloc, 0);
97 struct ext4_extent_header *hdr =
98 (struct ext4_extent_header *)&inode->i_block[0];
99 hdr->eh_magic = EXT4_EXT_MAGIC;
100 hdr->eh_entries = allocation_len;
102 hdr->eh_generation = 0;
105 extent = (struct ext4_extent *)&inode->i_block[3];
107 struct ext4_extent_header *hdr =
108 (struct ext4_extent_header *)&inode->i_block[0];
109 hdr->eh_magic = EXT4_EXT_MAGIC;
112 hdr->eh_generation = 0;
115 struct ext4_extent_idx *idx =
116 (struct ext4_extent_idx *)&inode->i_block[3];
118 idx->ei_leaf_lo = extent_block;
122 u8 *data = calloc(info.block_size, 1);
124 critical_error_errno("calloc");
126 sparse_file_add_data(info.sparse_file, data, info.block_size,
129 if (((int)(info.block_size - sizeof(struct ext4_extent_header) /
130 sizeof(struct ext4_extent))) < allocation_len) {
131 error("File size %llu is too big to fit in a single extent block\n",
136 hdr = (struct ext4_extent_header *)data;
137 hdr->eh_magic = EXT4_EXT_MAGIC;
138 hdr->eh_entries = allocation_len;
139 hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) /
140 sizeof(struct ext4_extent);
141 hdr->eh_generation = 0;
144 extent = (struct ext4_extent *)(data +
145 sizeof(struct ext4_extent_header));
148 for (; !last_region(alloc); extent++, get_next_region(alloc)) {
152 get_region(alloc, ®ion_block, ®ion_len);
153 extent->ee_block = file_block;
154 extent->ee_len = region_len;
155 extent->ee_start_hi = 0;
156 extent->ee_start_lo = region_block;
157 file_block += region_len;
163 blocks = (u64)block_len * info.block_size / 512;
165 inode->i_flags |= EXT4_EXTENTS_FL;
166 inode->i_size_lo = len;
167 inode->i_size_high = len >> 32;
168 inode->i_blocks_lo = blocks;
169 inode->osd2.linux2.l_i_blocks_high = blocks >> 32;
176 /* Allocates enough blocks to hold len bytes, with backing_len bytes in a data
177 buffer, and connects them to an inode. Returns a pointer to the data
179 u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len,
182 struct block_allocation *alloc;
185 alloc = do_inode_allocate_extents(inode, len);
187 error("failed to allocate extents for %llu bytes", len);
192 data = extent_create_backing(alloc, backing_len);
194 error("failed to create backing for %llu bytes", backing_len);
202 /* Allocates enough blocks to hold len bytes, queues them to be written
203 from a file, and connects them to an inode. */
204 void inode_allocate_file_extents(struct ext4_inode *inode, u64 len,
205 const char *filename)
207 struct block_allocation *alloc;
209 alloc = do_inode_allocate_extents(inode, len);
211 error("failed to allocate extents for %llu bytes", len);
215 extent_create_backing_file(alloc, len, filename);
220 /* Allocates enough blocks to hold len bytes and connects them to an inode */
221 void inode_allocate_extents(struct ext4_inode *inode, u64 len)
223 struct block_allocation *alloc;
225 alloc = do_inode_allocate_extents(inode, len);
227 error("failed to allocate extents for %llu bytes", len);