blob: 8b2b0fd01202537bbdd919806c37c0bcf29db0e1 [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
17#include <sys/stat.h>
18#include <string.h>
19#include <stdio.h>
Nick Kralevich3ebdcb82013-02-22 19:39:21 -080020
Nick Kralevich8a632322013-03-01 13:24:44 -080021#ifdef HAVE_ANDROID_OS
22#include <linux/capability.h>
23#else
24#include <private/android_filesystem_capability.h>
25#endif
26
Nick Kralevich3ebdcb82013-02-22 19:39:21 -080027#define XATTR_SELINUX_SUFFIX "selinux"
28#define XATTR_CAPS_SUFFIX "capability"
Colin Crossec0a2e82010-06-11 14:21:37 -070029
30#include "ext4_utils.h"
Colin Crossec0a2e82010-06-11 14:21:37 -070031#include "make_ext4fs.h"
32#include "allocate.h"
33#include "contents.h"
34#include "extent.h"
35#include "indirect.h"
36
Raphael Moll4605b3f2012-02-03 23:02:33 -080037#ifdef USE_MINGW
38#define S_IFLNK 0 /* used by make_link, not needed under mingw */
39#endif
40
Doug Zongkerbec598e2014-08-12 11:35:37 -070041static struct block_allocation* saved_allocation_head = NULL;
42
43struct block_allocation* get_saved_allocation_chain() {
44 return saved_allocation_head;
45}
46
Colin Crossec0a2e82010-06-11 14:21:37 -070047static u32 dentry_size(u32 entries, struct dentry *dentries)
48{
49 u32 len = 24;
50 unsigned int i;
Colin Cross6bd2b5d2010-08-05 11:56:38 -070051 unsigned int dentry_len;
Colin Crossec0a2e82010-06-11 14:21:37 -070052
53 for (i = 0; i < entries; i++) {
Paul Lawrence40ce87a2014-03-03 10:51:58 -080054 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
Colin Crossec0a2e82010-06-11 14:21:37 -070055 if (len % info.block_size + dentry_len > info.block_size)
56 len += info.block_size - (len % info.block_size);
57 len += dentry_len;
58 }
59
60 return len;
61}
62
Colin Crossec0a2e82010-06-11 14:21:37 -070063static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
64 struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
65 u8 file_type)
66{
67 u8 name_len = strlen(name);
Paul Lawrence40ce87a2014-03-03 10:51:58 -080068 u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
Colin Crossec0a2e82010-06-11 14:21:37 -070069 struct ext4_dir_entry_2 *dentry;
70
71 u32 start_block = *offset / info.block_size;
Colin Cross2ff1c5b2011-01-25 21:44:37 -080072 u32 end_block = (*offset + rec_len - 1) / info.block_size;
Colin Crossec0a2e82010-06-11 14:21:37 -070073 if (start_block != end_block) {
74 /* Adding this dentry will cross a block boundary, so pad the previous
75 dentry to the block boundary */
76 if (!prev)
77 critical_error("no prev");
78 prev->rec_len += end_block * info.block_size - *offset;
79 *offset = end_block * info.block_size;
80 }
81
82 dentry = (struct ext4_dir_entry_2 *)(data + *offset);
83 dentry->inode = inode;
84 dentry->rec_len = rec_len;
85 dentry->name_len = name_len;
86 dentry->file_type = file_type;
87 memcpy(dentry->name, name, name_len);
88
89 *offset += rec_len;
90 return dentry;
91}
92
93/* Creates a directory structure for an array of directory entries, dentries,
94 and stores the location of the structure in an inode. The new inode's
95 .. link is set to dir_inode_num. Stores the location of the inode number
96 of each directory entry into dentries[i].inode, to be filled in later
97 when the inode for the entry is allocated. Returns the inode number of the
98 new directory */
99u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
Colin Cross8aef66d2010-06-20 23:22:12 -0700100 u32 dirs)
Colin Crossec0a2e82010-06-11 14:21:37 -0700101{
102 struct ext4_inode *inode;
Colin Cross6bd2b5d2010-08-05 11:56:38 -0700103 u32 blocks;
104 u32 len;
Colin Crossec0a2e82010-06-11 14:21:37 -0700105 u32 offset = 0;
106 u32 inode_num;
107 u8 *data;
108 unsigned int i;
109 struct ext4_dir_entry_2 *dentry;
110
Colin Cross6bd2b5d2010-08-05 11:56:38 -0700111 blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
112 len = blocks * info.block_size;
113
Colin Crossec0a2e82010-06-11 14:21:37 -0700114 if (dir_inode_num) {
115 inode_num = allocate_inode(info);
116 } else {
117 dir_inode_num = EXT4_ROOT_INO;
118 inode_num = EXT4_ROOT_INO;
119 }
120
121 if (inode_num == EXT4_ALLOCATE_FAILED) {
122 error("failed to allocate inode\n");
123 return EXT4_ALLOCATE_FAILED;
124 }
125
126 add_directory(inode_num);
127
128 inode = get_inode(inode_num);
129 if (inode == NULL) {
130 error("failed to get inode %u", inode_num);
131 return EXT4_ALLOCATE_FAILED;
132 }
133
134 data = inode_allocate_data_extents(inode, len, len);
135 if (data == NULL) {
Colin Cross2e905e52010-12-29 14:20:53 -0800136 error("failed to allocate %u extents", len);
Colin Crossec0a2e82010-06-11 14:21:37 -0700137 return EXT4_ALLOCATE_FAILED;
138 }
139
140 inode->i_mode = S_IFDIR;
141 inode->i_links_count = dirs + 2;
142 inode->i_flags |= aux_info.default_i_flags;
143
144 dentry = NULL;
145
146 dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
147 if (!dentry) {
148 error("failed to add . directory");
149 return EXT4_ALLOCATE_FAILED;
150 }
151
152 dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
153 if (!dentry) {
154 error("failed to add .. directory");
155 return EXT4_ALLOCATE_FAILED;
156 }
157
158 for (i = 0; i < entries; i++) {
Colin Cross6bd2b5d2010-08-05 11:56:38 -0700159 dentry = add_dentry(data, &offset, dentry, 0,
160 dentries[i].filename, dentries[i].file_type);
161 if (offset > len || (offset == len && i != entries - 1))
162 critical_error("internal error: dentry for %s ends at %d, past %d\n",
163 dentries[i].filename, offset, len);
Colin Crossec0a2e82010-06-11 14:21:37 -0700164 dentries[i].inode = &dentry->inode;
165 if (!dentry) {
166 error("failed to add directory");
167 return EXT4_ALLOCATE_FAILED;
168 }
169 }
170
Colin Cross87489382012-11-20 14:07:39 -0800171 /* pad the last dentry out to the end of the block */
172 dentry->rec_len += len - offset;
Colin Crossec0a2e82010-06-11 14:21:37 -0700173
174 return inode_num;
175}
176
177/* Creates a file on disk. Returns the inode number of the new file */
178u32 make_file(const char *filename, u64 len)
179{
180 struct ext4_inode *inode;
181 u32 inode_num;
182
183 inode_num = allocate_inode(info);
184 if (inode_num == EXT4_ALLOCATE_FAILED) {
185 error("failed to allocate inode\n");
186 return EXT4_ALLOCATE_FAILED;
187 }
188
189 inode = get_inode(inode_num);
190 if (inode == NULL) {
191 error("failed to get inode %u", inode_num);
192 return EXT4_ALLOCATE_FAILED;
193 }
194
Doug Zongkerbec598e2014-08-12 11:35:37 -0700195 if (len > 0) {
196 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
Doug Zongker99221352014-08-12 16:55:56 -0700197 if (alloc) {
198 alloc->filename = strdup(filename);
199 alloc->next = saved_allocation_head;
200 saved_allocation_head = alloc;
201 }
Doug Zongkerbec598e2014-08-12 11:35:37 -0700202 }
Colin Crossec0a2e82010-06-11 14:21:37 -0700203
204 inode->i_mode = S_IFREG;
205 inode->i_links_count = 1;
206 inode->i_flags |= aux_info.default_i_flags;
207
208 return inode_num;
209}
210
211/* Creates a file on disk. Returns the inode number of the new file */
Nick Kralevich5446bde2013-02-19 19:05:47 -0800212u32 make_link(const char *link)
Colin Crossec0a2e82010-06-11 14:21:37 -0700213{
214 struct ext4_inode *inode;
215 u32 inode_num;
216 u32 len = strlen(link);
217
218 inode_num = allocate_inode(info);
219 if (inode_num == EXT4_ALLOCATE_FAILED) {
220 error("failed to allocate inode\n");
221 return EXT4_ALLOCATE_FAILED;
222 }
223
224 inode = get_inode(inode_num);
225 if (inode == NULL) {
226 error("failed to get inode %u", inode_num);
227 return EXT4_ALLOCATE_FAILED;
228 }
229
230 inode->i_mode = S_IFLNK;
231 inode->i_links_count = 1;
232 inode->i_flags |= aux_info.default_i_flags;
233 inode->i_size_lo = len;
234
235 if (len + 1 <= sizeof(inode->i_block)) {
236 /* Fast symlink */
237 memcpy((char*)inode->i_block, link, len);
238 } else {
239 u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
240 memcpy(data, link, len);
241 inode->i_blocks_lo = info.block_size / 512;
242 }
243
244 return inode_num;
245}
246
Colin Crossde61f982010-08-04 15:06:09 -0700247int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
Colin Crossec0a2e82010-06-11 14:21:37 -0700248{
249 struct ext4_inode *inode = get_inode(inode_num);
250
251 if (!inode)
252 return -1;
253
254 inode->i_mode |= mode;
255 inode->i_uid = uid;
256 inode->i_gid = gid;
Colin Crossde61f982010-08-04 15:06:09 -0700257 inode->i_mtime = mtime;
258 inode->i_atime = mtime;
259 inode->i_ctime = mtime;
Colin Crossec0a2e82010-06-11 14:21:37 -0700260
261 return 0;
262}
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500263
Nick Kralevich4df62f32013-02-07 14:21:34 -0800264/*
265 * Returns the amount of free space available in the specified
266 * xattr region
267 */
268static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500269{
Mark Salyzyn9036d552015-04-06 12:37:28 -0700270 end -= sizeof(uint32_t); /* Required four null bytes */
Nick Kralevich4df62f32013-02-07 14:21:34 -0800271 while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
272 end -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
273 entry = EXT4_XATTR_NEXT(entry);
274 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500275
Nick Kralevich4df62f32013-02-07 14:21:34 -0800276 if (((char *) entry) > end) {
277 error("unexpected read beyond end of xattr space");
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500278 return 0;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800279 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500280
Mark Salyzyn9036d552015-04-06 12:37:28 -0700281 return end - ((char *) entry);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800282}
283
284/*
285 * Returns a pointer to the free space immediately after the
286 * last xattr element
287 */
288static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
289{
290 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
291 // skip entry
292 }
293 return entry;
294}
295
296/*
297 * assert that the elements in the ext4 xattr section are in sorted order
298 *
299 * The ext4 filesystem requires extended attributes to be sorted when
300 * they're not stored in the inode. The kernel ext4 code uses the following
301 * sorting algorithm:
302 *
303 * 1) First sort extended attributes by their name_index. For example,
304 * EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
305 * 2) If the name_indexes are equal, then sorting is based on the length
306 * of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
307 * XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
308 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
309 * which name comes first. For example, "selinux" would come before "yelinux".
310 *
311 * This method is intended to implement the sorting function defined in
312 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
313 */
314static void xattr_assert_sane(struct ext4_xattr_entry *entry)
315{
316 for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
317 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
318 if (IS_LAST_ENTRY(next)) {
319 return;
320 }
321
322 int cmp = next->e_name_index - entry->e_name_index;
323 if (cmp == 0)
324 cmp = next->e_name_len - entry->e_name_len;
325 if (cmp == 0)
326 cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
327 if (cmp < 0) {
328 error("BUG: extended attributes are not sorted\n");
329 return;
330 }
331 if (cmp == 0) {
332 error("BUG: duplicate extended attributes detected\n");
333 return;
334 }
335 }
336}
337
338#define NAME_HASH_SHIFT 5
339#define VALUE_HASH_SHIFT 16
340
341static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
342 struct ext4_xattr_entry *entry)
343{
Colin Cross7900c772014-01-23 12:53:30 -0800344 u32 hash = 0;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800345 char *name = entry->e_name;
346 int n;
347
348 for (n = 0; n < entry->e_name_len; n++) {
349 hash = (hash << NAME_HASH_SHIFT) ^
350 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
351 *name++;
352 }
353
354 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
Colin Cross7900c772014-01-23 12:53:30 -0800355 u32 *value = (u32 *)((char *)header +
Nick Kralevich4df62f32013-02-07 14:21:34 -0800356 le16_to_cpu(entry->e_value_offs));
357 for (n = (le32_to_cpu(entry->e_value_size) +
358 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
359 hash = (hash << VALUE_HASH_SHIFT) ^
360 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
361 le32_to_cpu(*value++);
362 }
363 }
364 entry->e_hash = cpu_to_le32(hash);
365}
366
367#undef NAME_HASH_SHIFT
368#undef VALUE_HASH_SHIFT
369
370static struct ext4_xattr_entry* xattr_addto_range(
371 void *block_start,
372 void *block_end,
373 struct ext4_xattr_entry *first,
374 int name_index,
375 const char *name,
376 const void *value,
377 size_t value_len)
378{
379 size_t name_len = strlen(name);
380 if (name_len > 255)
381 return NULL;
382
383 size_t available_size = xattr_free_space(first, block_end);
384 size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
385
386 if (needed_size > available_size)
387 return NULL;
388
389 struct ext4_xattr_entry *new_entry = xattr_get_last(first);
390 memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
391
392 new_entry->e_name_len = name_len;
393 new_entry->e_name_index = name_index;
394 memcpy(new_entry->e_name, name, name_len);
395 new_entry->e_value_block = 0;
396 new_entry->e_value_size = cpu_to_le32(value_len);
397
398 char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
399 size_t e_value_offs = val - (char *) block_start;
400
401 new_entry->e_value_offs = cpu_to_le16(e_value_offs);
402 memset(val, 0, EXT4_XATTR_SIZE(value_len));
403 memcpy(val, value, value_len);
404
405 xattr_assert_sane(first);
406 return new_entry;
407}
408
409static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
410 const char *name, const void *value, size_t value_len)
411{
412 struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
413 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
414 char *block_end = ((char *) inode) + info.inode_size;
415
416 struct ext4_xattr_entry *result =
417 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
418
419 if (result == NULL)
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500420 return -1;
421
Nick Kralevich4df62f32013-02-07 14:21:34 -0800422 hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500423 inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
424
425 return 0;
426}
Nick Kralevich4df62f32013-02-07 14:21:34 -0800427
428static int xattr_addto_block(struct ext4_inode *inode, int name_index,
429 const char *name, const void *value, size_t value_len)
430{
431 struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
432 if (!header)
433 return -1;
434
435 struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
436 char *block_end = ((char *) header) + info.block_size;
437
438 struct ext4_xattr_entry *result =
439 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
440
441 if (result == NULL)
442 return -1;
443
444 ext4_xattr_hash_entry(header, result);
445 return 0;
446}
447
448
449static int xattr_add(u32 inode_num, int name_index, const char *name,
450 const void *value, size_t value_len)
451{
452 if (!value)
453 return 0;
454
455 struct ext4_inode *inode = get_inode(inode_num);
456
457 if (!inode)
458 return -1;
459
460 int result = xattr_addto_inode(inode, name_index, name, value, value_len);
461 if (result != 0) {
462 result = xattr_addto_block(inode, name_index, name, value, value_len);
463 }
464 return result;
465}
466
467int inode_set_selinux(u32 inode_num, const char *secon)
468{
Sungmin Choi192238d2013-02-25 15:37:51 +0900469 if (!secon)
470 return 0;
471
Nick Kralevich4df62f32013-02-07 14:21:34 -0800472 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
473 XATTR_SELINUX_SUFFIX, secon, strlen(secon) + 1);
474}
475
476int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
477 if (capabilities == 0)
478 return 0;
479
480 struct vfs_cap_data cap_data;
481 memset(&cap_data, 0, sizeof(cap_data));
482
483 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
484 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
485 cap_data.data[0].inheritable = 0;
486 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
487 cap_data.data[1].inheritable = 0;
488
489 return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
490 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
491}