blob: aeff0f6b7202be2eea84f4248b8b9b40fe228336 [file] [log] [blame]
Colin Crossec0a2e82010-06-11 14:21:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Colin Crossec0a2e82010-06-11 14:21:37 -070017#include "ext4_utils.h"
Colin Crossec0a2e82010-06-11 14:21:37 -070018#include "extent.h"
19
Colin Crossdc5abee2012-04-23 23:20:48 -070020#include <sparse/sparse.h>
21
Colin Cross33f96c62010-12-22 18:40:14 -080022#include <stdlib.h>
23#include <stdio.h>
24
25
Colin Crossec0a2e82010-06-11 14:21:37 -070026/* Creates data buffers for the first backing_len bytes of a block allocation
27 and queues them to be written */
28static u8 *extent_create_backing(struct block_allocation *alloc,
Colin Cross8aef66d2010-06-20 23:22:12 -070029 u64 backing_len)
Colin Crossec0a2e82010-06-11 14:21:37 -070030{
31 u8 *data = calloc(backing_len, 1);
32 if (!data)
33 critical_error_errno("calloc");
34
35 u8 *ptr = data;
36 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
37 u32 region_block;
38 u32 region_len;
39 u32 len;
40 get_region(alloc, &region_block, &region_len);
41
42 len = min(region_len * info.block_size, backing_len);
43
Colin Cross782879a2014-01-23 13:08:16 -080044 sparse_file_add_data(ext4_sparse_file, ptr, len, region_block);
Colin Crossec0a2e82010-06-11 14:21:37 -070045 ptr += len;
46 backing_len -= len;
47 }
48
49 return data;
50}
51
52/* Queues each chunk of a file to be written to contiguous data block
53 regions */
54static void extent_create_backing_file(struct block_allocation *alloc,
Colin Cross8aef66d2010-06-20 23:22:12 -070055 u64 backing_len, const char *filename)
Colin Crossec0a2e82010-06-11 14:21:37 -070056{
Colin Cross33f96c62010-12-22 18:40:14 -080057 off64_t offset = 0;
Colin Crossec0a2e82010-06-11 14:21:37 -070058 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
59 u32 region_block;
60 u32 region_len;
61 u32 len;
62 get_region(alloc, &region_block, &region_len);
63
64 len = min(region_len * info.block_size, backing_len);
65
Colin Cross782879a2014-01-23 13:08:16 -080066 sparse_file_add_file(ext4_sparse_file, filename, offset, len,
Colin Crossf0ee37f2012-04-24 17:48:43 -070067 region_block);
Colin Crossec0a2e82010-06-11 14:21:37 -070068 offset += len;
69 backing_len -= len;
70 }
71}
72
73static struct block_allocation *do_inode_allocate_extents(
Colin Cross8aef66d2010-06-20 23:22:12 -070074 struct ext4_inode *inode, u64 len)
Colin Crossec0a2e82010-06-11 14:21:37 -070075{
76 u32 block_len = DIV_ROUND_UP(len, info.block_size);
77 struct block_allocation *alloc = allocate_blocks(block_len + 1);
78 u32 extent_block = 0;
79 u32 file_block = 0;
80 struct ext4_extent *extent;
81 u64 blocks;
82
83 if (alloc == NULL) {
84 error("Failed to allocate %d blocks\n", block_len + 1);
85 return NULL;
86 }
87
88 int allocation_len = block_allocation_num_regions(alloc);
89 if (allocation_len <= 3) {
90 reduce_allocation(alloc, 1);
91 } else {
92 reserve_oob_blocks(alloc, 1);
93 extent_block = get_oob_block(alloc, 0);
94 }
95
96 if (!extent_block) {
97 struct ext4_extent_header *hdr =
Colin Cross8aef66d2010-06-20 23:22:12 -070098 (struct ext4_extent_header *)&inode->i_block[0];
Colin Crossec0a2e82010-06-11 14:21:37 -070099 hdr->eh_magic = EXT4_EXT_MAGIC;
100 hdr->eh_entries = allocation_len;
101 hdr->eh_max = 3;
102 hdr->eh_generation = 0;
103 hdr->eh_depth = 0;
104
105 extent = (struct ext4_extent *)&inode->i_block[3];
106 } else {
107 struct ext4_extent_header *hdr =
Colin Cross8aef66d2010-06-20 23:22:12 -0700108 (struct ext4_extent_header *)&inode->i_block[0];
Colin Crossec0a2e82010-06-11 14:21:37 -0700109 hdr->eh_magic = EXT4_EXT_MAGIC;
110 hdr->eh_entries = 1;
111 hdr->eh_max = 3;
112 hdr->eh_generation = 0;
113 hdr->eh_depth = 1;
114
115 struct ext4_extent_idx *idx =
Colin Cross8aef66d2010-06-20 23:22:12 -0700116 (struct ext4_extent_idx *)&inode->i_block[3];
Colin Crossec0a2e82010-06-11 14:21:37 -0700117 idx->ei_block = 0;
118 idx->ei_leaf_lo = extent_block;
119 idx->ei_leaf_hi = 0;
120 idx->ei_unused = 0;
121
122 u8 *data = calloc(info.block_size, 1);
123 if (!data)
124 critical_error_errno("calloc");
125
Colin Cross782879a2014-01-23 13:08:16 -0800126 sparse_file_add_data(ext4_sparse_file, data, info.block_size,
Colin Crossf0ee37f2012-04-24 17:48:43 -0700127 extent_block);
Colin Crossec0a2e82010-06-11 14:21:37 -0700128
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",
132 len);
133 return NULL;
134 }
135
136 hdr = (struct ext4_extent_header *)data;
137 hdr->eh_magic = EXT4_EXT_MAGIC;
138 hdr->eh_entries = allocation_len;
Colin Cross8aef66d2010-06-20 23:22:12 -0700139 hdr->eh_max = (info.block_size - sizeof(struct ext4_extent_header)) /
140 sizeof(struct ext4_extent);
Colin Crossec0a2e82010-06-11 14:21:37 -0700141 hdr->eh_generation = 0;
142 hdr->eh_depth = 0;
143
Colin Cross8aef66d2010-06-20 23:22:12 -0700144 extent = (struct ext4_extent *)(data +
145 sizeof(struct ext4_extent_header));
Colin Crossec0a2e82010-06-11 14:21:37 -0700146 }
147
148 for (; !last_region(alloc); extent++, get_next_region(alloc)) {
149 u32 region_block;
150 u32 region_len;
151
152 get_region(alloc, &region_block, &region_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;
158 }
159
160 if (extent_block)
161 block_len += 1;
162
163 blocks = (u64)block_len * info.block_size / 512;
164
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;
170
171 rewind_alloc(alloc);
172
173 return alloc;
174}
175
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
178 buffer. */
179u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len,
Colin Cross8aef66d2010-06-20 23:22:12 -0700180 u64 backing_len)
Colin Crossec0a2e82010-06-11 14:21:37 -0700181{
182 struct block_allocation *alloc;
183 u8 *data = NULL;
184
185 alloc = do_inode_allocate_extents(inode, len);
186 if (alloc == NULL) {
187 error("failed to allocate extents for %llu bytes", len);
188 return NULL;
189 }
190
191 if (backing_len) {
192 data = extent_create_backing(alloc, backing_len);
193 if (!data)
194 error("failed to create backing for %llu bytes", backing_len);
195 }
196
197 free_alloc(alloc);
198
199 return data;
200}
201
202/* Allocates enough blocks to hold len bytes, queues them to be written
203 from a file, and connects them to an inode. */
204void inode_allocate_file_extents(struct ext4_inode *inode, u64 len,
Colin Cross8aef66d2010-06-20 23:22:12 -0700205 const char *filename)
Colin Crossec0a2e82010-06-11 14:21:37 -0700206{
207 struct block_allocation *alloc;
208
209 alloc = do_inode_allocate_extents(inode, len);
210 if (alloc == NULL) {
211 error("failed to allocate extents for %llu bytes", len);
212 return;
213 }
214
215 extent_create_backing_file(alloc, len, filename);
216
217 free_alloc(alloc);
218}
219
220/* Allocates enough blocks to hold len bytes and connects them to an inode */
221void inode_allocate_extents(struct ext4_inode *inode, u64 len)
222{
223 struct block_allocation *alloc;
224
225 alloc = do_inode_allocate_extents(inode, len);
226 if (alloc == NULL) {
227 error("failed to allocate extents for %llu bytes", len);
228 return;
229 }
230
231 free_alloc(alloc);
232}