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.
20 #include "ext4_utils.h"
22 #include "ext4_extents.h"
23 #include "backed_block.h"
27 /* Creates data buffers for the first backing_len bytes of a block allocation
28 and queues them to be written */
29 static u8 *extent_create_backing(struct block_allocation *alloc,
32 u8 *data = calloc(backing_len, 1);
34 critical_error_errno("calloc");
37 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
41 get_region(alloc, ®ion_block, ®ion_len);
43 len = min(region_len * info.block_size, backing_len);
45 queue_data_block(ptr, len, region_block);
53 /* Queues each chunk of a file to be written to contiguous data block
55 static void extent_create_backing_file(struct block_allocation *alloc,
56 u64 backing_len, const char *filename)
59 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
63 get_region(alloc, ®ion_block, ®ion_len);
65 len = min(region_len * info.block_size, backing_len);
67 queue_data_file(filename, offset, len, region_block);
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 queue_data_block(data, info.block_size, extent_block);
128 if (((int)(info.block_size - sizeof(struct ext4_extent_header) /
129 sizeof(struct ext4_extent))) < allocation_len) {
130 error("File size %llu is too big to fit in a single extent block\n",
135 hdr = (struct ext4_extent_header *)data;
136 hdr->eh_magic = EXT4_EXT_MAGIC;
137 hdr->eh_entries = allocation_len;
138 hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) /
139 sizeof(struct ext4_extent);
140 hdr->eh_generation = 0;
143 extent = (struct ext4_extent *)(data +
144 sizeof(struct ext4_extent_header));
147 for (; !last_region(alloc); extent++, get_next_region(alloc)) {
151 get_region(alloc, ®ion_block, ®ion_len);
152 extent->ee_block = file_block;
153 extent->ee_len = region_len;
154 extent->ee_start_hi = 0;
155 extent->ee_start_lo = region_block;
156 file_block += region_len;
162 blocks = (u64)block_len * info.block_size / 512;
164 inode->i_flags |= EXT4_EXTENTS_FL;
165 inode->i_size_lo = len;
166 inode->i_size_high = len >> 32;
167 inode->i_blocks_lo = blocks;
168 inode->osd2.linux2.l_i_blocks_high = blocks >> 32;
175 /* Allocates enough blocks to hold len bytes, with backing_len bytes in a data
176 buffer, and connects them to an inode. Returns a pointer to the data
178 u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len,
181 struct block_allocation *alloc;
184 alloc = do_inode_allocate_extents(inode, len);
186 error("failed to allocate extents for %llu bytes", len);
191 data = extent_create_backing(alloc, backing_len);
193 error("failed to create backing for %llu bytes", backing_len);
201 /* Allocates enough blocks to hold len bytes, queues them to be written
202 from a file, and connects them to an inode. */
203 void inode_allocate_file_extents(struct ext4_inode *inode, u64 len,
204 const char *filename)
206 struct block_allocation *alloc;
208 alloc = do_inode_allocate_extents(inode, len);
210 error("failed to allocate extents for %llu bytes", len);
214 extent_create_backing_file(alloc, len, filename);
219 /* Allocates enough blocks to hold len bytes and connects them to an inode */
220 void inode_allocate_extents(struct ext4_inode *inode, u64 len)
222 struct block_allocation *alloc;
224 alloc = do_inode_allocate_extents(inode, len);
226 error("failed to allocate extents for %llu bytes", len);