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.
24 #include "ext4_utils/ext4_utils.h"
25 #include "ext4_utils/make_ext4fs.h"
30 #include <linux/capability.h>
32 #include <private/android_filesystem_capability.h>
35 #define XATTR_SELINUX_SUFFIX "selinux"
36 #define XATTR_CAPS_SUFFIX "capability"
39 #define S_IFLNK 0 /* used by make_link, not needed under mingw */
42 static struct block_allocation* saved_allocation_head = NULL;
44 struct block_allocation* get_saved_allocation_chain() {
45 return saved_allocation_head;
48 static u32 dentry_size(u32 entries, struct dentry *dentries)
52 unsigned int dentry_len;
54 for (i = 0; i < entries; i++) {
55 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
56 if (len % info.block_size + dentry_len > info.block_size)
57 len += info.block_size - (len % info.block_size);
64 static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
65 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
68 u8 name_len = strlen(name);
69 u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
70 struct ext4_dir_entry_2 *dentry;
72 u32 start_block = *offset / info.block_size;
73 u32 end_block = (*offset + rec_len - 1) / info.block_size;
74 if (start_block != end_block) {
75 /* Adding this dentry will cross a block boundary, so pad the previous
76 dentry to the block boundary */
78 critical_error("no prev");
79 prev->rec_len += end_block * info.block_size - *offset;
80 *offset = end_block * info.block_size;
83 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
84 dentry->inode = inode;
85 dentry->rec_len = rec_len;
86 dentry->name_len = name_len;
87 dentry->file_type = file_type;
88 memcpy(dentry->name, name, name_len);
94 /* Creates a directory structure for an array of directory entries, dentries,
95 and stores the location of the structure in an inode. The new inode's
96 .. link is set to dir_inode_num. Stores the location of the inode number
97 of each directory entry into dentries[i].inode, to be filled in later
98 when the inode for the entry is allocated. Returns the inode number of the
100 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
103 struct ext4_inode *inode;
110 struct ext4_dir_entry_2 *dentry;
112 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
113 len = blocks * info.block_size;
116 inode_num = allocate_inode(info);
118 dir_inode_num = EXT4_ROOT_INO;
119 inode_num = EXT4_ROOT_INO;
122 if (inode_num == EXT4_ALLOCATE_FAILED) {
123 error("failed to allocate inode\n");
124 return EXT4_ALLOCATE_FAILED;
127 add_directory(inode_num);
129 inode = get_inode(inode_num);
131 error("failed to get inode %u", inode_num);
132 return EXT4_ALLOCATE_FAILED;
135 data = inode_allocate_data_extents(inode, len, len);
137 error("failed to allocate %u extents", len);
138 return EXT4_ALLOCATE_FAILED;
141 inode->i_mode = S_IFDIR;
142 inode->i_links_count = dirs + 2;
143 inode->i_flags |= aux_info.default_i_flags;
147 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
149 error("failed to add . directory");
150 return EXT4_ALLOCATE_FAILED;
153 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
155 error("failed to add .. directory");
156 return EXT4_ALLOCATE_FAILED;
159 for (i = 0; i < entries; i++) {
160 dentry = add_dentry(data, &offset, dentry, 0,
161 dentries[i].filename, dentries[i].file_type);
162 if (offset > len || (offset == len && i != entries - 1))
163 critical_error("internal error: dentry for %s ends at %d, past %d\n",
164 dentries[i].filename, offset, len);
165 dentries[i].inode = &dentry->inode;
167 error("failed to add directory");
168 return EXT4_ALLOCATE_FAILED;
172 /* pad the last dentry out to the end of the block */
173 dentry->rec_len += len - offset;
178 /* Creates a file on disk. Returns the inode number of the new file */
179 u32 make_file(const char *filename, u64 len)
181 struct ext4_inode *inode;
184 inode_num = allocate_inode(info);
185 if (inode_num == EXT4_ALLOCATE_FAILED) {
186 error("failed to allocate inode\n");
187 return EXT4_ALLOCATE_FAILED;
190 inode = get_inode(inode_num);
192 error("failed to get inode %u", inode_num);
193 return EXT4_ALLOCATE_FAILED;
197 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
199 alloc->filename = strdup(filename);
200 alloc->next = saved_allocation_head;
201 saved_allocation_head = alloc;
205 inode->i_mode = S_IFREG;
206 inode->i_links_count = 1;
207 inode->i_flags |= aux_info.default_i_flags;
212 /* Creates a file on disk. Returns the inode number of the new file */
213 u32 make_link(const char *link)
215 struct ext4_inode *inode;
217 u32 len = strlen(link);
219 inode_num = allocate_inode(info);
220 if (inode_num == EXT4_ALLOCATE_FAILED) {
221 error("failed to allocate inode\n");
222 return EXT4_ALLOCATE_FAILED;
225 inode = get_inode(inode_num);
227 error("failed to get inode %u", inode_num);
228 return EXT4_ALLOCATE_FAILED;
231 inode->i_mode = S_IFLNK;
232 inode->i_links_count = 1;
233 inode->i_flags |= aux_info.default_i_flags;
234 inode->i_size_lo = len;
236 if (len + 1 <= sizeof(inode->i_block)) {
238 memcpy((char*)inode->i_block, link, len);
240 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
241 memcpy(data, link, len);
242 inode->i_blocks_lo = info.block_size / 512;
248 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
250 struct ext4_inode *inode = get_inode(inode_num);
255 inode->i_mode |= mode;
258 inode->i_mtime = mtime;
259 inode->i_atime = mtime;
260 inode->i_ctime = mtime;
266 * Returns the amount of free space available in the specified
269 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
271 end -= sizeof(uint32_t); /* Required four null bytes */
272 while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
273 end -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
274 entry = EXT4_XATTR_NEXT(entry);
277 if (((char *) entry) > end) {
278 error("unexpected read beyond end of xattr space");
282 return end - ((char *) entry);
286 * Returns a pointer to the free space immediately after the
289 static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
291 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
298 * assert that the elements in the ext4 xattr section are in sorted order
300 * The ext4 filesystem requires extended attributes to be sorted when
301 * they're not stored in the inode. The kernel ext4 code uses the following
304 * 1) First sort extended attributes by their name_index. For example,
305 * EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
306 * 2) If the name_indexes are equal, then sorting is based on the length
307 * of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
308 * XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
309 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
310 * which name comes first. For example, "selinux" would come before "yelinux".
312 * This method is intended to implement the sorting function defined in
313 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
315 static void xattr_assert_sane(struct ext4_xattr_entry *entry)
317 for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
318 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
319 if (IS_LAST_ENTRY(next)) {
323 int cmp = next->e_name_index - entry->e_name_index;
325 cmp = next->e_name_len - entry->e_name_len;
327 cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
329 error("BUG: extended attributes are not sorted\n");
333 error("BUG: duplicate extended attributes detected\n");
339 #define NAME_HASH_SHIFT 5
340 #define VALUE_HASH_SHIFT 16
342 static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
343 struct ext4_xattr_entry *entry)
346 char *name = entry->e_name;
349 for (n = 0; n < entry->e_name_len; n++) {
350 hash = (hash << NAME_HASH_SHIFT) ^
351 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
355 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
356 u32 *value = (u32 *)((char *)header +
357 le16_to_cpu(entry->e_value_offs));
358 for (n = (le32_to_cpu(entry->e_value_size) +
359 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
360 hash = (hash << VALUE_HASH_SHIFT) ^
361 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
362 le32_to_cpu(*value++);
365 entry->e_hash = cpu_to_le32(hash);
368 #undef NAME_HASH_SHIFT
369 #undef VALUE_HASH_SHIFT
371 static struct ext4_xattr_entry* xattr_addto_range(
374 struct ext4_xattr_entry *first,
380 size_t name_len = strlen(name);
384 size_t available_size = xattr_free_space(first, block_end);
385 size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
387 if (needed_size > available_size)
390 struct ext4_xattr_entry *new_entry = xattr_get_last(first);
391 memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
393 new_entry->e_name_len = name_len;
394 new_entry->e_name_index = name_index;
395 memcpy(new_entry->e_name, name, name_len);
396 new_entry->e_value_block = 0;
397 new_entry->e_value_size = cpu_to_le32(value_len);
399 char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
400 size_t e_value_offs = val - (char *) block_start;
402 new_entry->e_value_offs = cpu_to_le16(e_value_offs);
403 memset(val, 0, EXT4_XATTR_SIZE(value_len));
404 memcpy(val, value, value_len);
406 xattr_assert_sane(first);
410 static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
411 const char *name, const void *value, size_t value_len)
413 struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
414 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
415 char *block_end = ((char *) inode) + info.inode_size;
417 struct ext4_xattr_entry *result =
418 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
423 hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
424 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
429 static int xattr_addto_block(struct ext4_inode *inode, int name_index,
430 const char *name, const void *value, size_t value_len)
432 struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
436 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
437 char *block_end = ((char *) header) + info.block_size;
439 struct ext4_xattr_entry *result =
440 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
445 ext4_xattr_hash_entry(header, result);
450 static int xattr_add(u32 inode_num, int name_index, const char *name,
451 const void *value, size_t value_len)
456 struct ext4_inode *inode = get_inode(inode_num);
461 int result = xattr_addto_inode(inode, name_index, name, value, value_len);
463 result = xattr_addto_block(inode, name_index, name, value, value_len);
468 int inode_set_selinux(u32 inode_num, const char *secon)
473 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
474 XATTR_SELINUX_SUFFIX, secon, strlen(secon) + 1);
477 int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
478 if (capabilities == 0)
481 struct vfs_cap_data cap_data;
482 memset(&cap_data, 0, sizeof(cap_data));
484 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
485 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
486 cap_data.data[0].inheritable = 0;
487 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
488 cap_data.data[1].inheritable = 0;
490 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
491 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));