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.
20 #if defined(__linux__)
21 #include <linux/capability.h>
22 #include <linux/xattr.h>
25 #define VFS_CAP_FLAGS_EFFECTIVE 0x000001
26 #define VFS_CAP_U32_2 2
27 #define VFS_CAP_U32 VFS_CAP_U32_2
28 #define VFS_CAP_REVISION_2 0x02000000
29 #define VFS_CAP_REVISION VFS_CAP_REVISION_2
38 #define XATTR_SELINUX_SUFFIX "selinux"
39 #define XATTR_CAPS_SUFFIX "capability"
40 #endif /* !defined(__linux__) */
42 #include "ext4_utils.h"
44 #include "make_ext4fs.h"
52 #define S_IFLNK 0 /* used by make_link, not needed under mingw */
55 static u32 dentry_size(u32 entries, struct dentry *dentries)
59 unsigned int dentry_len;
61 for (i = 0; i < entries; i++) {
62 dentry_len = 8 + ALIGN(strlen(dentries[i].filename), 4);
63 if (len % info.block_size + dentry_len > info.block_size)
64 len += info.block_size - (len % info.block_size);
71 static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
72 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
75 u8 name_len = strlen(name);
76 u16 rec_len = 8 + ALIGN(name_len, 4);
77 struct ext4_dir_entry_2 *dentry;
79 u32 start_block = *offset / info.block_size;
80 u32 end_block = (*offset + rec_len - 1) / info.block_size;
81 if (start_block != end_block) {
82 /* Adding this dentry will cross a block boundary, so pad the previous
83 dentry to the block boundary */
85 critical_error("no prev");
86 prev->rec_len += end_block * info.block_size - *offset;
87 *offset = end_block * info.block_size;
90 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
91 dentry->inode = inode;
92 dentry->rec_len = rec_len;
93 dentry->name_len = name_len;
94 dentry->file_type = file_type;
95 memcpy(dentry->name, name, name_len);
101 /* Creates a directory structure for an array of directory entries, dentries,
102 and stores the location of the structure in an inode. The new inode's
103 .. link is set to dir_inode_num. Stores the location of the inode number
104 of each directory entry into dentries[i].inode, to be filled in later
105 when the inode for the entry is allocated. Returns the inode number of the
107 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
110 struct ext4_inode *inode;
117 struct ext4_dir_entry_2 *dentry;
119 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
120 len = blocks * info.block_size;
123 inode_num = allocate_inode(info);
125 dir_inode_num = EXT4_ROOT_INO;
126 inode_num = EXT4_ROOT_INO;
129 if (inode_num == EXT4_ALLOCATE_FAILED) {
130 error("failed to allocate inode\n");
131 return EXT4_ALLOCATE_FAILED;
134 add_directory(inode_num);
136 inode = get_inode(inode_num);
138 error("failed to get inode %u", inode_num);
139 return EXT4_ALLOCATE_FAILED;
142 data = inode_allocate_data_extents(inode, len, len);
144 error("failed to allocate %u extents", len);
145 return EXT4_ALLOCATE_FAILED;
148 inode->i_mode = S_IFDIR;
149 inode->i_links_count = dirs + 2;
150 inode->i_flags |= aux_info.default_i_flags;
154 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
156 error("failed to add . directory");
157 return EXT4_ALLOCATE_FAILED;
160 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
162 error("failed to add .. directory");
163 return EXT4_ALLOCATE_FAILED;
166 for (i = 0; i < entries; i++) {
167 dentry = add_dentry(data, &offset, dentry, 0,
168 dentries[i].filename, dentries[i].file_type);
169 if (offset > len || (offset == len && i != entries - 1))
170 critical_error("internal error: dentry for %s ends at %d, past %d\n",
171 dentries[i].filename, offset, len);
172 dentries[i].inode = &dentry->inode;
174 error("failed to add directory");
175 return EXT4_ALLOCATE_FAILED;
179 /* pad the last dentry out to the end of the block */
180 dentry->rec_len += len - offset;
185 /* Creates a file on disk. Returns the inode number of the new file */
186 u32 make_file(const char *filename, u64 len)
188 struct ext4_inode *inode;
191 inode_num = allocate_inode(info);
192 if (inode_num == EXT4_ALLOCATE_FAILED) {
193 error("failed to allocate inode\n");
194 return EXT4_ALLOCATE_FAILED;
197 inode = get_inode(inode_num);
199 error("failed to get inode %u", inode_num);
200 return EXT4_ALLOCATE_FAILED;
204 inode_allocate_file_extents(inode, len, filename);
206 inode->i_mode = S_IFREG;
207 inode->i_links_count = 1;
208 inode->i_flags |= aux_info.default_i_flags;
213 /* Creates a file on disk. Returns the inode number of the new file */
214 u32 make_link(const char *link)
216 struct ext4_inode *inode;
218 u32 len = strlen(link);
220 inode_num = allocate_inode(info);
221 if (inode_num == EXT4_ALLOCATE_FAILED) {
222 error("failed to allocate inode\n");
223 return EXT4_ALLOCATE_FAILED;
226 inode = get_inode(inode_num);
228 error("failed to get inode %u", inode_num);
229 return EXT4_ALLOCATE_FAILED;
232 inode->i_mode = S_IFLNK;
233 inode->i_links_count = 1;
234 inode->i_flags |= aux_info.default_i_flags;
235 inode->i_size_lo = len;
237 if (len + 1 <= sizeof(inode->i_block)) {
239 memcpy((char*)inode->i_block, link, len);
241 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
242 memcpy(data, link, len);
243 inode->i_blocks_lo = info.block_size / 512;
249 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
251 struct ext4_inode *inode = get_inode(inode_num);
256 inode->i_mode |= mode;
259 inode->i_mtime = mtime;
260 inode->i_atime = mtime;
261 inode->i_ctime = mtime;
267 * Returns the amount of free space available in the specified
270 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
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 __le32 *value = (__le32 *)((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));