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"
19 #include "ext4_extents.h"
22 #include <sparse/sparse.h>
28 /* Creates data buffers for the first backing_len bytes of a block allocation
29 and queues them to be written */
30 static u8 *extent_create_backing(struct block_allocation *alloc,
33 u8 *data = calloc(backing_len, 1);
35 critical_error_errno("calloc");
38 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
42 get_region(alloc, ®ion_block, ®ion_len);
44 len = min(region_len * info.block_size, backing_len);
46 sparse_file_add_data(info.sparse_file, ptr, len, region_block);
54 /* Queues each chunk of a file to be written to contiguous data block
56 static void extent_create_backing_file(struct block_allocation *alloc,
57 u64 backing_len, const char *filename)
60 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
64 get_region(alloc, ®ion_block, ®ion_len);
66 len = min(region_len * info.block_size, backing_len);
68 sparse_file_add_file(info.sparse_file, filename, offset, len,
75 static struct block_allocation *do_inode_allocate_extents(
76 struct ext4_inode *inode, u64 len)
78 u32 block_len = DIV_ROUND_UP(len, info.block_size);
79 struct block_allocation *alloc = allocate_blocks(block_len + 1);
82 struct ext4_extent *extent;
86 error("Failed to allocate %d blocks\n", block_len + 1);
90 int allocation_len = block_allocation_num_regions(alloc);
91 if (allocation_len <= 3) {
92 reduce_allocation(alloc, 1);
94 reserve_oob_blocks(alloc, 1);
95 extent_block = get_oob_block(alloc, 0);
99 struct ext4_extent_header *hdr =
100 (struct ext4_extent_header *)&inode->i_block[0];
101 hdr->eh_magic = EXT4_EXT_MAGIC;
102 hdr->eh_entries = allocation_len;
104 hdr->eh_generation = 0;
107 extent = (struct ext4_extent *)&inode->i_block[3];
109 struct ext4_extent_header *hdr =
110 (struct ext4_extent_header *)&inode->i_block[0];
111 hdr->eh_magic = EXT4_EXT_MAGIC;
114 hdr->eh_generation = 0;
117 struct ext4_extent_idx *idx =
118 (struct ext4_extent_idx *)&inode->i_block[3];
120 idx->ei_leaf_lo = extent_block;
124 u8 *data = calloc(info.block_size, 1);
126 critical_error_errno("calloc");
128 sparse_file_add_data(info.sparse_file, data, info.block_size,
131 if (((int)(info.block_size - sizeof(struct ext4_extent_header) /
132 sizeof(struct ext4_extent))) < allocation_len) {
133 error("File size %llu is too big to fit in a single extent block\n",
138 hdr = (struct ext4_extent_header *)data;
139 hdr->eh_magic = EXT4_EXT_MAGIC;
140 hdr->eh_entries = allocation_len;
141 hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) /
142 sizeof(struct ext4_extent);
143 hdr->eh_generation = 0;
146 extent = (struct ext4_extent *)(data +
147 sizeof(struct ext4_extent_header));
150 for (; !last_region(alloc); extent++, get_next_region(alloc)) {
154 get_region(alloc, ®ion_block, ®ion_len);
155 extent->ee_block = file_block;
156 extent->ee_len = region_len;
157 extent->ee_start_hi = 0;
158 extent->ee_start_lo = region_block;
159 file_block += region_len;
165 blocks = (u64)block_len * info.block_size / 512;
167 inode->i_flags |= EXT4_EXTENTS_FL;
168 inode->i_size_lo = len;
169 inode->i_size_high = len >> 32;
170 inode->i_blocks_lo = blocks;
171 inode->osd2.linux2.l_i_blocks_high = blocks >> 32;
178 /* Allocates enough blocks to hold len bytes, with backing_len bytes in a data
179 buffer, and connects them to an inode. Returns a pointer to the data
181 u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len,
184 struct block_allocation *alloc;
187 alloc = do_inode_allocate_extents(inode, len);
189 error("failed to allocate extents for %llu bytes", len);
194 data = extent_create_backing(alloc, backing_len);
196 error("failed to create backing for %llu bytes", backing_len);
204 /* Allocates enough blocks to hold len bytes, queues them to be written
205 from a file, and connects them to an inode. */
206 void inode_allocate_file_extents(struct ext4_inode *inode, u64 len,
207 const char *filename)
209 struct block_allocation *alloc;
211 alloc = do_inode_allocate_extents(inode, len);
213 error("failed to allocate extents for %llu bytes", len);
217 extent_create_backing_file(alloc, len, filename);
222 /* Allocates enough blocks to hold len bytes and connects them to an inode */
223 void inode_allocate_extents(struct ext4_inode *inode, u64 len)
225 struct block_allocation *alloc;
227 alloc = do_inode_allocate_extents(inode, len);
229 error("failed to allocate extents for %llu bytes", len);