Remove references to O_BINARY
[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 #ifdef HAVE_ANDROID_OS
22 #include <linux/capability.h>
23 #else
24 #include <private/android_filesystem_capability.h>
25 #endif
26
27 #define XATTR_SELINUX_SUFFIX "selinux"
28 #define XATTR_CAPS_SUFFIX "capability"
29
30 #include "ext4_utils.h"
31 #include "make_ext4fs.h"
32 #include "allocate.h"
33 #include "contents.h"
34 #include "extent.h"
35 #include "indirect.h"
36
37 #ifdef USE_MINGW
38 #define S_IFLNK 0  /* used by make_link, not needed under mingw */
39 #endif
40
41 static struct block_allocation* saved_allocation_head = NULL;
42
43 struct block_allocation* get_saved_allocation_chain() {
44         return saved_allocation_head;
45 }
46
47 static u32 dentry_size(u32 entries, struct dentry *dentries)
48 {
49         u32 len = 24;
50         unsigned int i;
51         unsigned int dentry_len;
52
53         for (i = 0; i < entries; i++) {
54                 dentry_len = 8 + EXT4_ALIGN(strlen(dentries[i].filename), 4);
55                 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
63 static 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);
68         u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
69         struct ext4_dir_entry_2 *dentry;
70
71         u32 start_block = *offset / info.block_size;
72         u32 end_block = (*offset + rec_len - 1) / info.block_size;
73         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 */
99 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
100         u32 dirs)
101 {
102         struct ext4_inode *inode;
103         u32 blocks;
104         u32 len;
105         u32 offset = 0;
106         u32 inode_num;
107         u8 *data;
108         unsigned int i;
109         struct ext4_dir_entry_2 *dentry;
110
111         blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
112         len = blocks * info.block_size;
113
114         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) {
136                 error("failed to allocate %u extents", len);
137                 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++) {
159                 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);
164                 dentries[i].inode = &dentry->inode;
165                 if (!dentry) {
166                         error("failed to add directory");
167                         return EXT4_ALLOCATE_FAILED;
168                 }
169         }
170
171         /* pad the last dentry out to the end of the block */
172         dentry->rec_len += len - offset;
173
174         return inode_num;
175 }
176
177 /* Creates a file on disk.  Returns the inode number of the new file */
178 u32 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
195         if (len > 0) {
196                 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
197                 if (alloc) {
198                         alloc->filename = strdup(filename);
199                         alloc->next = saved_allocation_head;
200                         saved_allocation_head = alloc;
201                 }
202         }
203
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 */
212 u32 make_link(const char *link)
213 {
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
247 /* Creates a special file on disk.  Returns the inode number of the new file */
248 u32 make_special(const char *path)
249 {
250         struct ext4_inode *inode;
251         struct stat s;
252         u32 inode_num;
253
254         if (stat(path, &s)) {
255                 error("failed to stat file\n");
256                 return EXT4_ALLOCATE_FAILED;
257         }
258
259         inode_num = allocate_inode(info);
260         if (inode_num == EXT4_ALLOCATE_FAILED) {
261                 error("failed to allocate inode\n");
262                 return EXT4_ALLOCATE_FAILED;
263         }
264
265         inode = get_inode(inode_num);
266         if (inode == NULL) {
267                 error("failed to get inode %u", inode_num);
268                 return EXT4_ALLOCATE_FAILED;
269         }
270
271         inode->i_mode = s.st_mode & S_IFMT;
272         inode->i_links_count = 1;
273         inode->i_flags |= aux_info.default_i_flags;
274
275         ((u8 *)inode->i_block)[0] = major(s.st_rdev);
276         ((u8 *)inode->i_block)[1] = minor(s.st_rdev);
277
278         return inode_num;
279 }
280
281 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
282 {
283         struct ext4_inode *inode = get_inode(inode_num);
284
285         if (!inode)
286                 return -1;
287
288         inode->i_mode |= mode;
289         inode->i_uid = uid;
290         inode->i_gid = gid;
291         inode->i_mtime = mtime;
292         inode->i_atime = mtime;
293         inode->i_ctime = mtime;
294
295         return 0;
296 }
297
298 /*
299  * Returns the amount of free space available in the specified
300  * xattr region
301  */
302 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
303 {
304         while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
305                 end   -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
306                 entry  = EXT4_XATTR_NEXT(entry);
307         }
308
309         if (((char *) entry) > end) {
310                 error("unexpected read beyond end of xattr space");
311                 return 0;
312         }
313
314         return end - ((char *) entry);
315 }
316
317 /*
318  * Returns a pointer to the free space immediately after the
319  * last xattr element
320  */
321 static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
322 {
323         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
324                 // skip entry
325         }
326         return entry;
327 }
328
329 /*
330  * assert that the elements in the ext4 xattr section are in sorted order
331  *
332  * The ext4 filesystem requires extended attributes to be sorted when
333  * they're not stored in the inode. The kernel ext4 code uses the following
334  * sorting algorithm:
335  *
336  * 1) First sort extended attributes by their name_index. For example,
337  *    EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
338  * 2) If the name_indexes are equal, then sorting is based on the length
339  *    of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
340  *    XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
341  * 3) If the name_index and name_length are equal, then memcmp() is used to determine
342  *    which name comes first. For example, "selinux" would come before "yelinux".
343  *
344  * This method is intended to implement the sorting function defined in
345  * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
346  */
347 static void xattr_assert_sane(struct ext4_xattr_entry *entry)
348 {
349         for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
350                 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
351                 if (IS_LAST_ENTRY(next)) {
352                         return;
353                 }
354
355                 int cmp = next->e_name_index - entry->e_name_index;
356                 if (cmp == 0)
357                         cmp = next->e_name_len - entry->e_name_len;
358                 if (cmp == 0)
359                         cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
360                 if (cmp < 0) {
361                         error("BUG: extended attributes are not sorted\n");
362                         return;
363                 }
364                 if (cmp == 0) {
365                         error("BUG: duplicate extended attributes detected\n");
366                         return;
367                 }
368         }
369 }
370
371 #define NAME_HASH_SHIFT 5
372 #define VALUE_HASH_SHIFT 16
373
374 static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
375                 struct ext4_xattr_entry *entry)
376 {
377         u32 hash = 0;
378         char *name = entry->e_name;
379         int n;
380
381         for (n = 0; n < entry->e_name_len; n++) {
382                 hash = (hash << NAME_HASH_SHIFT) ^
383                         (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
384                         *name++;
385         }
386
387         if (entry->e_value_block == 0 && entry->e_value_size != 0) {
388                 u32 *value = (u32 *)((char *)header +
389                         le16_to_cpu(entry->e_value_offs));
390                 for (n = (le32_to_cpu(entry->e_value_size) +
391                         EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
392                         hash = (hash << VALUE_HASH_SHIFT) ^
393                                 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
394                                 le32_to_cpu(*value++);
395                 }
396         }
397         entry->e_hash = cpu_to_le32(hash);
398 }
399
400 #undef NAME_HASH_SHIFT
401 #undef VALUE_HASH_SHIFT
402
403 static struct ext4_xattr_entry* xattr_addto_range(
404                 void *block_start,
405                 void *block_end,
406                 struct ext4_xattr_entry *first,
407                 int name_index,
408                 const char *name,
409                 const void *value,
410                 size_t value_len)
411 {
412         size_t name_len = strlen(name);
413         if (name_len > 255)
414                 return NULL;
415
416         size_t available_size = xattr_free_space(first, block_end);
417         size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
418
419         if (needed_size > available_size)
420                 return NULL;
421
422         struct ext4_xattr_entry *new_entry = xattr_get_last(first);
423         memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
424
425         new_entry->e_name_len = name_len;
426         new_entry->e_name_index = name_index;
427         memcpy(new_entry->e_name, name, name_len);
428         new_entry->e_value_block = 0;
429         new_entry->e_value_size = cpu_to_le32(value_len);
430
431         char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
432         size_t e_value_offs = val - (char *) block_start;
433
434         new_entry->e_value_offs = cpu_to_le16(e_value_offs);
435         memset(val, 0, EXT4_XATTR_SIZE(value_len));
436         memcpy(val, value, value_len);
437
438         xattr_assert_sane(first);
439         return new_entry;
440 }
441
442 static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
443                 const char *name, const void *value, size_t value_len)
444 {
445         struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
446         struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
447         char *block_end = ((char *) inode) + info.inode_size;
448
449         struct ext4_xattr_entry *result =
450                 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
451
452         if (result == NULL)
453                 return -1;
454
455         hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
456         inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
457
458         return 0;
459 }
460
461 static int xattr_addto_block(struct ext4_inode *inode, int name_index,
462                 const char *name, const void *value, size_t value_len)
463 {
464         struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
465         if (!header)
466                 return -1;
467
468         struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
469         char *block_end = ((char *) header) + info.block_size;
470
471         struct ext4_xattr_entry *result =
472                 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
473
474         if (result == NULL)
475                 return -1;
476
477         ext4_xattr_hash_entry(header, result);
478         return 0;
479 }
480
481
482 static int xattr_add(u32 inode_num, int name_index, const char *name,
483                 const void *value, size_t value_len)
484 {
485         if (!value)
486                 return 0;
487
488         struct ext4_inode *inode = get_inode(inode_num);
489
490         if (!inode)
491                 return -1;
492
493         int result = xattr_addto_inode(inode, name_index, name, value, value_len);
494         if (result != 0) {
495                 result = xattr_addto_block(inode, name_index, name, value, value_len);
496         }
497         return result;
498 }
499
500 int inode_set_selinux(u32 inode_num, const char *secon)
501 {
502         if (!secon)
503                 return 0;
504
505         return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
506                 XATTR_SELINUX_SUFFIX, secon, strlen(secon) + 1);
507 }
508
509 int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
510         if (capabilities == 0)
511                 return 0;
512
513         struct vfs_cap_data cap_data;
514         memset(&cap_data, 0, sizeof(cap_data));
515
516         cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
517         cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
518         cap_data.data[0].inheritable = 0;
519         cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
520         cap_data.data[1].inheritable = 0;
521
522         return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
523                 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
524 }