blob: 905a378b2ed2db1e7680e13ed1647cdfac26d67a [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"
18#include "ext4.h"
19#include "ext4_extents.h"
Colin Crossec0a2e82010-06-11 14:21:37 -070020#include "extent.h"
21
Colin Crossdc5abee2012-04-23 23:20:48 -070022#include <sparse/sparse.h>
23
Colin Cross33f96c62010-12-22 18:40:14 -080024#include <stdlib.h>
25#include <stdio.h>
26
27
Colin Crossec0a2e82010-06-11 14:21:37 -070028/* Creates data buffers for the first backing_len bytes of a block allocation
29 and queues them to be written */
30static u8 *extent_create_backing(struct block_allocation *alloc,
Colin Cross8aef66d2010-06-20 23:22:12 -070031 u64 backing_len)
Colin Crossec0a2e82010-06-11 14:21:37 -070032{
33 u8 *data = calloc(backing_len, 1);
34 if (!data)
35 critical_error_errno("calloc");
36
37 u8 *ptr = data;
38 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
39 u32 region_block;
40 u32 region_len;
41 u32 len;
42 get_region(alloc, &region_block, &region_len);
43
44 len = min(region_len * info.block_size, backing_len);
45
46 queue_data_block(ptr, len, region_block);
47 ptr += len;
48 backing_len -= len;
49 }
50
51 return data;
52}
53
54/* Queues each chunk of a file to be written to contiguous data block
55 regions */
56static void extent_create_backing_file(struct block_allocation *alloc,
Colin Cross8aef66d2010-06-20 23:22:12 -070057 u64 backing_len, const char *filename)
Colin Crossec0a2e82010-06-11 14:21:37 -070058{
Colin Cross33f96c62010-12-22 18:40:14 -080059 off64_t offset = 0;
Colin Crossec0a2e82010-06-11 14:21:37 -070060 for (; alloc != NULL && backing_len > 0; get_next_region(alloc)) {
61 u32 region_block;
62 u32 region_len;
63 u32 len;
64 get_region(alloc, &region_block, &region_len);
65
66 len = min(region_len * info.block_size, backing_len);
67
68 queue_data_file(filename, offset, len, region_block);
69 offset += len;
70 backing_len -= len;
71 }
72}
73
74static struct block_allocation *do_inode_allocate_extents(
Colin Cross8aef66d2010-06-20 23:22:12 -070075 struct ext4_inode *inode, u64 len)
Colin Crossec0a2e82010-06-11 14:21:37 -070076{
77 u32 block_len = DIV_ROUND_UP(len, info.block_size);
78 struct block_allocation *alloc = allocate_blocks(block_len + 1);
79 u32 extent_block = 0;
80 u32 file_block = 0;
81 struct ext4_extent *extent;
82 u64 blocks;
83
84 if (alloc == NULL) {
85 error("Failed to allocate %d blocks\n", block_len + 1);
86 return NULL;
87 }
88
89 int allocation_len = block_allocation_num_regions(alloc);
90 if (allocation_len <= 3) {
91 reduce_allocation(alloc, 1);
92 } else {
93 reserve_oob_blocks(alloc, 1);
94 extent_block = get_oob_block(alloc, 0);
95 }
96
97 if (!extent_block) {
98 struct ext4_extent_header *hdr =
Colin Cross8aef66d2010-06-20 23:22:12 -070099 (struct ext4_extent_header *)&inode->i_block[0];
Colin Crossec0a2e82010-06-11 14:21:37 -0700100 hdr->eh_magic = EXT4_EXT_MAGIC;
101 hdr->eh_entries = allocation_len;
102 hdr->eh_max = 3;
103 hdr->eh_generation = 0;
104 hdr->eh_depth = 0;
105
106 extent = (struct ext4_extent *)&inode->i_block[3];
107 } else {
108 struct ext4_extent_header *hdr =
Colin Cross8aef66d2010-06-20 23:22:12 -0700109 (struct ext4_extent_header *)&inode->i_block[0];
Colin Crossec0a2e82010-06-11 14:21:37 -0700110 hdr->eh_magic = EXT4_EXT_MAGIC;
111 hdr->eh_entries = 1;
112 hdr->eh_max = 3;
113 hdr->eh_generation = 0;
114 hdr->eh_depth = 1;
115
116 struct ext4_extent_idx *idx =
Colin Cross8aef66d2010-06-20 23:22:12 -0700117 (struct ext4_extent_idx *)&inode->i_block[3];
Colin Crossec0a2e82010-06-11 14:21:37 -0700118 idx->ei_block = 0;
119 idx->ei_leaf_lo = extent_block;
120 idx->ei_leaf_hi = 0;
121 idx->ei_unused = 0;
122
123 u8 *data = calloc(info.block_size, 1);
124 if (!data)
125 critical_error_errno("calloc");
126
127 queue_data_block(data, info.block_size, extent_block);
128
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}