Remove MinGW support code
[project/make_ext4fs.git] / contents.c
1 /*
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
17 #include <sys/stat.h>
18 #include <string.h>
19 #include <stdio.h>
20
21 #include <private/android_filesystem_capability.h>
22
23 #define XATTR_SELINUX_SUFFIX "selinux"
24 #define XATTR_CAPS_SUFFIX "capability"
25
26 #include "ext4_utils.h"
27 #include "make_ext4fs.h"
28 #include "allocate.h"
29 #include "contents.h"
30 #include "extent.h"
31 #include "indirect.h"
32
33 static struct block_allocation* saved_allocation_head = NULL;
34
35 struct block_allocation* get_saved_allocation_chain() {
36 return saved_allocation_head;
37 }
38
39 static u32 dentry_size(u32 entries, struct dentry *dentries)
40 {
41 u32 len = 24;
42 unsigned int i;
43 unsigned int dentry_len;
44
45 for (i = 0; i < entries; i++) {
46 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
47 if (len % info.block_size + dentry_len > info.block_size)
48 len += info.block_size - (len % info.block_size);
49 len += dentry_len;
50 }
51
52 return len;
53 }
54
55 static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
56 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
57 u8 file_type)
58 {
59 u8 name_len = strlen(name);
60 u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
61 struct ext4_dir_entry_2 *dentry;
62
63 u32 start_block = *offset / info.block_size;
64 u32 end_block = (*offset + rec_len - 1) / info.block_size;
65 if (start_block != end_block) {
66 /* Adding this dentry will cross a block boundary, so pad the previous
67 dentry to the block boundary */
68 if (!prev)
69 critical_error("no prev");
70 prev->rec_len += end_block * info.block_size - *offset;
71 *offset = end_block * info.block_size;
72 }
73
74 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
75 dentry->inode = inode;
76 dentry->rec_len = rec_len;
77 dentry->name_len = name_len;
78 dentry->file_type = file_type;
79 memcpy(dentry->name, name, name_len);
80
81 *offset += rec_len;
82 return dentry;
83 }
84
85 /* Creates a directory structure for an array of directory entries, dentries,
86 and stores the location of the structure in an inode. The new inode's
87 .. link is set to dir_inode_num. Stores the location of the inode number
88 of each directory entry into dentries[i].inode, to be filled in later
89 when the inode for the entry is allocated. Returns the inode number of the
90 new directory */
91 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
92 u32 dirs)
93 {
94 struct ext4_inode *inode;
95 u32 blocks;
96 u32 len;
97 u32 offset = 0;
98 u32 inode_num;
99 u8 *data;
100 unsigned int i;
101 struct ext4_dir_entry_2 *dentry;
102
103 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
104 len = blocks * info.block_size;
105
106 if (dir_inode_num) {
107 inode_num = allocate_inode(info);
108 } else {
109 dir_inode_num = EXT4_ROOT_INO;
110 inode_num = EXT4_ROOT_INO;
111 }
112
113 if (inode_num == EXT4_ALLOCATE_FAILED) {
114 error("failed to allocate inode\n");
115 return EXT4_ALLOCATE_FAILED;
116 }
117
118 add_directory(inode_num);
119
120 inode = get_inode(inode_num);
121 if (inode == NULL) {
122 error("failed to get inode %u", inode_num);
123 return EXT4_ALLOCATE_FAILED;
124 }
125
126 data = inode_allocate_data_extents(inode, len, len);
127 if (data == NULL) {
128 error("failed to allocate %u extents", len);
129 return EXT4_ALLOCATE_FAILED;
130 }
131
132 inode->i_mode = S_IFDIR;
133 inode->i_links_count = dirs + 2;
134 inode->i_flags |= aux_info.default_i_flags;
135
136 dentry = NULL;
137
138 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
139 if (!dentry) {
140 error("failed to add . directory");
141 return EXT4_ALLOCATE_FAILED;
142 }
143
144 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
145 if (!dentry) {
146 error("failed to add .. directory");
147 return EXT4_ALLOCATE_FAILED;
148 }
149
150 for (i = 0; i < entries; i++) {
151 dentry = add_dentry(data, &offset, dentry, 0,
152 dentries[i].filename, dentries[i].file_type);
153 if (offset > len || (offset == len && i != entries - 1))
154 critical_error("internal error: dentry for %s ends at %d, past %d\n",
155 dentries[i].filename, offset, len);
156 dentries[i].inode = &dentry->inode;
157 if (!dentry) {
158 error("failed to add directory");
159 return EXT4_ALLOCATE_FAILED;
160 }
161 }
162
163 /* pad the last dentry out to the end of the block */
164 dentry->rec_len += len - offset;
165
166 return inode_num;
167 }
168
169 /* Creates a file on disk. Returns the inode number of the new file */
170 u32 make_file(const char *filename, u64 len)
171 {
172 struct ext4_inode *inode;
173 u32 inode_num;
174
175 inode_num = allocate_inode(info);
176 if (inode_num == EXT4_ALLOCATE_FAILED) {
177 error("failed to allocate inode\n");
178 return EXT4_ALLOCATE_FAILED;
179 }
180
181 inode = get_inode(inode_num);
182 if (inode == NULL) {
183 error("failed to get inode %u", inode_num);
184 return EXT4_ALLOCATE_FAILED;
185 }
186
187 if (len > 0) {
188 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
189 if (alloc) {
190 alloc->filename = strdup(filename);
191 alloc->next = saved_allocation_head;
192 saved_allocation_head = alloc;
193 }
194 }
195
196 inode->i_mode = S_IFREG;
197 inode->i_links_count = 1;
198 inode->i_flags |= aux_info.default_i_flags;
199
200 return inode_num;
201 }
202
203 /* Creates a file on disk. Returns the inode number of the new file */
204 u32 make_link(const char *link)
205 {
206 struct ext4_inode *inode;
207 u32 inode_num;
208 u32 len = strlen(link);
209
210 inode_num = allocate_inode(info);
211 if (inode_num == EXT4_ALLOCATE_FAILED) {
212 error("failed to allocate inode\n");
213 return EXT4_ALLOCATE_FAILED;
214 }
215
216 inode = get_inode(inode_num);
217 if (inode == NULL) {
218 error("failed to get inode %u", inode_num);
219 return EXT4_ALLOCATE_FAILED;
220 }
221
222 inode->i_mode = S_IFLNK;
223 inode->i_links_count = 1;
224 inode->i_flags |= aux_info.default_i_flags;
225 inode->i_size_lo = len;
226
227 if (len + 1 <= sizeof(inode->i_block)) {
228 /* Fast symlink */
229 memcpy((char*)inode->i_block, link, len);
230 } else {
231 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
232 memcpy(data, link, len);
233 inode->i_blocks_lo = info.block_size / 512;
234 }
235
236 return inode_num;
237 }
238
239 /* Creates a special file on disk. Returns the inode number of the new file */
240 u32 make_special(const char *path)
241 {
242 struct ext4_inode *inode;
243 struct stat s;
244 u32 inode_num;
245
246 if (stat(path, &s)) {
247 error("failed to stat file\n");
248 return EXT4_ALLOCATE_FAILED;
249 }
250
251 inode_num = allocate_inode(info);
252 if (inode_num == EXT4_ALLOCATE_FAILED) {
253 error("failed to allocate inode\n");
254 return EXT4_ALLOCATE_FAILED;
255 }
256
257 inode = get_inode(inode_num);
258 if (inode == NULL) {
259 error("failed to get inode %u", inode_num);
260 return EXT4_ALLOCATE_FAILED;
261 }
262
263 inode->i_mode = s.st_mode & S_IFMT;
264 inode->i_links_count = 1;
265 inode->i_flags |= aux_info.default_i_flags;
266
267 ((u8 *)inode->i_block)[0] = major(s.st_rdev);
268 ((u8 *)inode->i_block)[1] = minor(s.st_rdev);
269
270 return inode_num;
271 }
272
273 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
274 {
275 struct ext4_inode *inode = get_inode(inode_num);
276
277 if (!inode)
278 return -1;
279
280 inode->i_mode |= mode;
281 inode->i_uid = uid;
282 inode->i_gid = gid;
283 inode->i_mtime = mtime;
284 inode->i_atime = mtime;
285 inode->i_ctime = mtime;
286
287 return 0;
288 }
289
290 /*
291 * Returns the amount of free space available in the specified
292 * xattr region
293 */
294 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
295 {
296 while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
297 end -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
298 entry = EXT4_XATTR_NEXT(entry);
299 }
300
301 if (((char *) entry) > end) {
302 error("unexpected read beyond end of xattr space");
303 return 0;
304 }
305
306 return end - ((char *) entry);
307 }
308
309 /*
310 * Returns a pointer to the free space immediately after the
311 * last xattr element
312 */
313 static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
314 {
315 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
316 // skip entry
317 }
318 return entry;
319 }
320
321 /*
322 * assert that the elements in the ext4 xattr section are in sorted order
323 *
324 * The ext4 filesystem requires extended attributes to be sorted when
325 * they're not stored in the inode. The kernel ext4 code uses the following
326 * sorting algorithm:
327 *
328 * 1) First sort extended attributes by their name_index. For example,
329 * EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
330 * 2) If the name_indexes are equal, then sorting is based on the length
331 * of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
332 * XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
333 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
334 * which name comes first. For example, "selinux" would come before "yelinux".
335 *
336 * This method is intended to implement the sorting function defined in
337 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
338 */
339 static void xattr_assert_sane(struct ext4_xattr_entry *entry)
340 {
341 for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
342 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
343 if (IS_LAST_ENTRY(next)) {
344 return;
345 }
346
347 int cmp = next->e_name_index - entry->e_name_index;
348 if (cmp == 0)
349 cmp = next->e_name_len - entry->e_name_len;
350 if (cmp == 0)
351 cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
352 if (cmp < 0) {
353 error("BUG: extended attributes are not sorted\n");
354 return;
355 }
356 if (cmp == 0) {
357 error("BUG: duplicate extended attributes detected\n");
358 return;
359 }
360 }
361 }
362
363 #define NAME_HASH_SHIFT 5
364 #define VALUE_HASH_SHIFT 16
365
366 static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
367 struct ext4_xattr_entry *entry)
368 {
369 u32 hash = 0;
370 char *name = entry->e_name;
371 int n;
372
373 for (n = 0; n < entry->e_name_len; n++) {
374 hash = (hash << NAME_HASH_SHIFT) ^
375 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
376 *name++;
377 }
378
379 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
380 u32 *value = (u32 *)((char *)header +
381 le16_to_cpu(entry->e_value_offs));
382 for (n = (le32_to_cpu(entry->e_value_size) +
383 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
384 hash = (hash << VALUE_HASH_SHIFT) ^
385 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
386 le32_to_cpu(*value++);
387 }
388 }
389 entry->e_hash = cpu_to_le32(hash);
390 }
391
392 #undef NAME_HASH_SHIFT
393 #undef VALUE_HASH_SHIFT
394
395 static struct ext4_xattr_entry* xattr_addto_range(
396 void *block_start,
397 void *block_end,
398 struct ext4_xattr_entry *first,
399 int name_index,
400 const char *name,
401 const void *value,
402 size_t value_len)
403 {
404 size_t name_len = strlen(name);
405 if (name_len > 255)
406 return NULL;
407
408 size_t available_size = xattr_free_space(first, block_end);
409 size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
410
411 if (needed_size > available_size)
412 return NULL;
413
414 struct ext4_xattr_entry *new_entry = xattr_get_last(first);
415 memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
416
417 new_entry->e_name_len = name_len;
418 new_entry->e_name_index = name_index;
419 memcpy(new_entry->e_name, name, name_len);
420 new_entry->e_value_block = 0;
421 new_entry->e_value_size = cpu_to_le32(value_len);
422
423 char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
424 size_t e_value_offs = val - (char *) block_start;
425
426 new_entry->e_value_offs = cpu_to_le16(e_value_offs);
427 memset(val, 0, EXT4_XATTR_SIZE(value_len));
428 memcpy(val, value, value_len);
429
430 xattr_assert_sane(first);
431 return new_entry;
432 }
433
434 static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
435 const char *name, const void *value, size_t value_len)
436 {
437 struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
438 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
439 char *block_end = ((char *) inode) + info.inode_size;
440
441 struct ext4_xattr_entry *result =
442 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
443
444 if (result == NULL)
445 return -1;
446
447 hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
448 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
449
450 return 0;
451 }
452
453 static int xattr_addto_block(struct ext4_inode *inode, int name_index,
454 const char *name, const void *value, size_t value_len)
455 {
456 struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
457 if (!header)
458 return -1;
459
460 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
461 char *block_end = ((char *) header) + info.block_size;
462
463 struct ext4_xattr_entry *result =
464 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
465
466 if (result == NULL)
467 return -1;
468
469 ext4_xattr_hash_entry(header, result);
470 return 0;
471 }
472
473
474 static int xattr_add(u32 inode_num, int name_index, const char *name,
475 const void *value, size_t value_len)
476 {
477 if (!value)
478 return 0;
479
480 struct ext4_inode *inode = get_inode(inode_num);
481
482 if (!inode)
483 return -1;
484
485 int result = xattr_addto_inode(inode, name_index, name, value, value_len);
486 if (result != 0) {
487 result = xattr_addto_block(inode, name_index, name, value, value_len);
488 }
489 return result;
490 }
491
492 int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
493 if (capabilities == 0)
494 return 0;
495
496 struct vfs_cap_data cap_data;
497 memset(&cap_data, 0, sizeof(cap_data));
498
499 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
500 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
501 cap_data.data[0].inheritable = 0;
502 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
503 cap_data.data[1].inheritable = 0;
504
505 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
506 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
507 }