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.
21 #include <private/android_filesystem_capability.h>
23 #define XATTR_SELINUX_SUFFIX "selinux"
24 #define XATTR_CAPS_SUFFIX "capability"
26 #include "ext4_utils.h"
27 #include "make_ext4fs.h"
34 #define S_IFLNK 0 /* used by make_link, not needed under mingw */
37 static struct block_allocation* saved_allocation_head = NULL;
39 struct block_allocation* get_saved_allocation_chain() {
40 return saved_allocation_head;
43 static u32 dentry_size(u32 entries, struct dentry *dentries)
47 unsigned int dentry_len;
49 for (i = 0; i < entries; i++) {
50 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
51 if (len % info.block_size + dentry_len > info.block_size)
52 len += info.block_size - (len % info.block_size);
59 static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
60 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
63 u8 name_len = strlen(name);
64 u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
65 struct ext4_dir_entry_2 *dentry;
67 u32 start_block = *offset / info.block_size;
68 u32 end_block = (*offset + rec_len - 1) / info.block_size;
69 if (start_block != end_block) {
70 /* Adding this dentry will cross a block boundary, so pad the previous
71 dentry to the block boundary */
73 critical_error("no prev");
74 prev->rec_len += end_block * info.block_size - *offset;
75 *offset = end_block * info.block_size;
78 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
79 dentry->inode = inode;
80 dentry->rec_len = rec_len;
81 dentry->name_len = name_len;
82 dentry->file_type = file_type;
83 memcpy(dentry->name, name, name_len);
89 /* Creates a directory structure for an array of directory entries, dentries,
90 and stores the location of the structure in an inode. The new inode's
91 .. link is set to dir_inode_num. Stores the location of the inode number
92 of each directory entry into dentries[i].inode, to be filled in later
93 when the inode for the entry is allocated. Returns the inode number of the
95 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
98 struct ext4_inode *inode;
105 struct ext4_dir_entry_2 *dentry;
107 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
108 len = blocks * info.block_size;
111 inode_num = allocate_inode(info);
113 dir_inode_num = EXT4_ROOT_INO;
114 inode_num = EXT4_ROOT_INO;
117 if (inode_num == EXT4_ALLOCATE_FAILED) {
118 error("failed to allocate inode\n");
119 return EXT4_ALLOCATE_FAILED;
122 add_directory(inode_num);
124 inode = get_inode(inode_num);
126 error("failed to get inode %u", inode_num);
127 return EXT4_ALLOCATE_FAILED;
130 data = inode_allocate_data_extents(inode, len, len);
132 error("failed to allocate %u extents", len);
133 return EXT4_ALLOCATE_FAILED;
136 inode->i_mode = S_IFDIR;
137 inode->i_links_count = dirs + 2;
138 inode->i_flags |= aux_info.default_i_flags;
142 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
144 error("failed to add . directory");
145 return EXT4_ALLOCATE_FAILED;
148 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
150 error("failed to add .. directory");
151 return EXT4_ALLOCATE_FAILED;
154 for (i = 0; i < entries; i++) {
155 dentry = add_dentry(data, &offset, dentry, 0,
156 dentries[i].filename, dentries[i].file_type);
157 if (offset > len || (offset == len && i != entries - 1))
158 critical_error("internal error: dentry for %s ends at %d, past %d\n",
159 dentries[i].filename, offset, len);
160 dentries[i].inode = &dentry->inode;
162 error("failed to add directory");
163 return EXT4_ALLOCATE_FAILED;
167 /* pad the last dentry out to the end of the block */
168 dentry->rec_len += len - offset;
173 /* Creates a file on disk. Returns the inode number of the new file */
174 u32 make_file(const char *filename, u64 len)
176 struct ext4_inode *inode;
179 inode_num = allocate_inode(info);
180 if (inode_num == EXT4_ALLOCATE_FAILED) {
181 error("failed to allocate inode\n");
182 return EXT4_ALLOCATE_FAILED;
185 inode = get_inode(inode_num);
187 error("failed to get inode %u", inode_num);
188 return EXT4_ALLOCATE_FAILED;
192 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
194 alloc->filename = strdup(filename);
195 alloc->next = saved_allocation_head;
196 saved_allocation_head = alloc;
200 inode->i_mode = S_IFREG;
201 inode->i_links_count = 1;
202 inode->i_flags |= aux_info.default_i_flags;
207 /* Creates a file on disk. Returns the inode number of the new file */
208 u32 make_link(const char *link)
210 struct ext4_inode *inode;
212 u32 len = strlen(link);
214 inode_num = allocate_inode(info);
215 if (inode_num == EXT4_ALLOCATE_FAILED) {
216 error("failed to allocate inode\n");
217 return EXT4_ALLOCATE_FAILED;
220 inode = get_inode(inode_num);
222 error("failed to get inode %u", inode_num);
223 return EXT4_ALLOCATE_FAILED;
226 inode->i_mode = S_IFLNK;
227 inode->i_links_count = 1;
228 inode->i_flags |= aux_info.default_i_flags;
229 inode->i_size_lo = len;
231 if (len + 1 <= sizeof(inode->i_block)) {
233 memcpy((char*)inode->i_block, link, len);
235 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
236 memcpy(data, link, len);
237 inode->i_blocks_lo = info.block_size / 512;
243 /* Creates a special file on disk. Returns the inode number of the new file */
244 u32 make_special(const char *path)
246 struct ext4_inode *inode;
250 if (stat(path, &s)) {
251 error("failed to stat file\n");
252 return EXT4_ALLOCATE_FAILED;
255 inode_num = allocate_inode(info);
256 if (inode_num == EXT4_ALLOCATE_FAILED) {
257 error("failed to allocate inode\n");
258 return EXT4_ALLOCATE_FAILED;
261 inode = get_inode(inode_num);
263 error("failed to get inode %u", inode_num);
264 return EXT4_ALLOCATE_FAILED;
267 inode->i_mode = s.st_mode & S_IFMT;
268 inode->i_links_count = 1;
269 inode->i_flags |= aux_info.default_i_flags;
271 ((u8 *)inode->i_block)[0] = major(s.st_rdev);
272 ((u8 *)inode->i_block)[1] = minor(s.st_rdev);
277 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
279 struct ext4_inode *inode = get_inode(inode_num);
284 inode->i_mode |= mode;
287 inode->i_mtime = mtime;
288 inode->i_atime = mtime;
289 inode->i_ctime = mtime;
295 * Returns the amount of free space available in the specified
298 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
300 while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
301 end -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
302 entry = EXT4_XATTR_NEXT(entry);
305 if (((char *) entry) > end) {
306 error("unexpected read beyond end of xattr space");
310 return end - ((char *) entry);
314 * Returns a pointer to the free space immediately after the
317 static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
319 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
326 * assert that the elements in the ext4 xattr section are in sorted order
328 * The ext4 filesystem requires extended attributes to be sorted when
329 * they're not stored in the inode. The kernel ext4 code uses the following
332 * 1) First sort extended attributes by their name_index. For example,
333 * EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
334 * 2) If the name_indexes are equal, then sorting is based on the length
335 * of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
336 * XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
337 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
338 * which name comes first. For example, "selinux" would come before "yelinux".
340 * This method is intended to implement the sorting function defined in
341 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
343 static void xattr_assert_sane(struct ext4_xattr_entry *entry)
345 for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
346 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
347 if (IS_LAST_ENTRY(next)) {
351 int cmp = next->e_name_index - entry->e_name_index;
353 cmp = next->e_name_len - entry->e_name_len;
355 cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
357 error("BUG: extended attributes are not sorted\n");
361 error("BUG: duplicate extended attributes detected\n");
367 #define NAME_HASH_SHIFT 5
368 #define VALUE_HASH_SHIFT 16
370 static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
371 struct ext4_xattr_entry *entry)
374 char *name = entry->e_name;
377 for (n = 0; n < entry->e_name_len; n++) {
378 hash = (hash << NAME_HASH_SHIFT) ^
379 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
383 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
384 u32 *value = (u32 *)((char *)header +
385 le16_to_cpu(entry->e_value_offs));
386 for (n = (le32_to_cpu(entry->e_value_size) +
387 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
388 hash = (hash << VALUE_HASH_SHIFT) ^
389 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
390 le32_to_cpu(*value++);
393 entry->e_hash = cpu_to_le32(hash);
396 #undef NAME_HASH_SHIFT
397 #undef VALUE_HASH_SHIFT
399 static struct ext4_xattr_entry* xattr_addto_range(
402 struct ext4_xattr_entry *first,
408 size_t name_len = strlen(name);
412 size_t available_size = xattr_free_space(first, block_end);
413 size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
415 if (needed_size > available_size)
418 struct ext4_xattr_entry *new_entry = xattr_get_last(first);
419 memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
421 new_entry->e_name_len = name_len;
422 new_entry->e_name_index = name_index;
423 memcpy(new_entry->e_name, name, name_len);
424 new_entry->e_value_block = 0;
425 new_entry->e_value_size = cpu_to_le32(value_len);
427 char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
428 size_t e_value_offs = val - (char *) block_start;
430 new_entry->e_value_offs = cpu_to_le16(e_value_offs);
431 memset(val, 0, EXT4_XATTR_SIZE(value_len));
432 memcpy(val, value, value_len);
434 xattr_assert_sane(first);
438 static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
439 const char *name, const void *value, size_t value_len)
441 struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
442 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
443 char *block_end = ((char *) inode) + info.inode_size;
445 struct ext4_xattr_entry *result =
446 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
451 hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
452 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
457 static int xattr_addto_block(struct ext4_inode *inode, int name_index,
458 const char *name, const void *value, size_t value_len)
460 struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
464 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
465 char *block_end = ((char *) header) + info.block_size;
467 struct ext4_xattr_entry *result =
468 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
473 ext4_xattr_hash_entry(header, result);
478 static int xattr_add(u32 inode_num, int name_index, const char *name,
479 const void *value, size_t value_len)
484 struct ext4_inode *inode = get_inode(inode_num);
489 int result = xattr_addto_inode(inode, name_index, name, value, value_len);
491 result = xattr_addto_block(inode, name_index, name, value, value_len);
496 int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
497 if (capabilities == 0)
500 struct vfs_cap_data cap_data;
501 memset(&cap_data, 0, sizeof(cap_data));
503 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
504 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
505 cap_data.data[0].inheritable = 0;
506 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
507 cap_data.data[1].inheritable = 0;
509 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
510 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));