OSDN Git Service

ext4_utils: Export headers for libext4_utils* libs.
[android-x86/system-extras.git] / ext4_utils / 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 "contents.h"
18
19 #include <sys/stat.h>
20 #include <string.h>
21 #include <stdio.h>
22
23 #include "allocate.h"
24 #include "ext4_utils/ext4_utils.h"
25 #include "ext4_utils/make_ext4fs.h"
26 #include "extent.h"
27 #include "indirect.h"
28
29 #ifdef __ANDROID__
30 #include <linux/capability.h>
31 #else
32 #include <private/android_filesystem_capability.h>
33 #endif
34
35 #define XATTR_SELINUX_SUFFIX "selinux"
36 #define XATTR_CAPS_SUFFIX "capability"
37
38 #ifdef _WIN32
39 #define S_IFLNK 0  /* used by make_link, not needed under mingw */
40 #endif
41
42 static struct block_allocation* saved_allocation_head = NULL;
43
44 struct block_allocation* get_saved_allocation_chain() {
45         return saved_allocation_head;
46 }
47
48 static u32 dentry_size(u32 entries, struct dentry *dentries)
49 {
50         u32 len = 24;
51         unsigned int i;
52         unsigned int dentry_len;
53
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);
58                 len += dentry_len;
59         }
60
61         return len;
62 }
63
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,
66                 u8 file_type)
67 {
68         u8 name_len = strlen(name);
69         u16 rec_len = 8 + EXT4_ALIGN(name_len, 4);
70         struct ext4_dir_entry_2 *dentry;
71
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 */
77                 if (!prev)
78                         critical_error("no prev");
79                 prev->rec_len += end_block * info.block_size - *offset;
80                 *offset = end_block * info.block_size;
81         }
82
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);
89
90         *offset += rec_len;
91         return dentry;
92 }
93
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
99    new directory */
100 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
101         u32 dirs)
102 {
103         struct ext4_inode *inode;
104         u32 blocks;
105         u32 len;
106         u32 offset = 0;
107         u32 inode_num;
108         u8 *data;
109         unsigned int i;
110         struct ext4_dir_entry_2 *dentry;
111
112         blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
113         len = blocks * info.block_size;
114
115         if (dir_inode_num) {
116                 inode_num = allocate_inode(info);
117         } else {
118                 dir_inode_num = EXT4_ROOT_INO;
119                 inode_num = EXT4_ROOT_INO;
120         }
121
122         if (inode_num == EXT4_ALLOCATE_FAILED) {
123                 error("failed to allocate inode\n");
124                 return EXT4_ALLOCATE_FAILED;
125         }
126
127         add_directory(inode_num);
128
129         inode = get_inode(inode_num);
130         if (inode == NULL) {
131                 error("failed to get inode %u", inode_num);
132                 return EXT4_ALLOCATE_FAILED;
133         }
134
135         data = inode_allocate_data_extents(inode, len, len);
136         if (data == NULL) {
137                 error("failed to allocate %u extents", len);
138                 return EXT4_ALLOCATE_FAILED;
139         }
140
141         inode->i_mode = S_IFDIR;
142         inode->i_links_count = dirs + 2;
143         inode->i_flags |= aux_info.default_i_flags;
144
145         dentry = NULL;
146
147         dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
148         if (!dentry) {
149                 error("failed to add . directory");
150                 return EXT4_ALLOCATE_FAILED;
151         }
152
153         dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
154         if (!dentry) {
155                 error("failed to add .. directory");
156                 return EXT4_ALLOCATE_FAILED;
157         }
158
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;
166                 if (!dentry) {
167                         error("failed to add directory");
168                         return EXT4_ALLOCATE_FAILED;
169                 }
170         }
171
172         /* pad the last dentry out to the end of the block */
173         dentry->rec_len += len - offset;
174
175         return inode_num;
176 }
177
178 /* Creates a file on disk.  Returns the inode number of the new file */
179 u32 make_file(const char *filename, u64 len)
180 {
181         struct ext4_inode *inode;
182         u32 inode_num;
183
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;
188         }
189
190         inode = get_inode(inode_num);
191         if (inode == NULL) {
192                 error("failed to get inode %u", inode_num);
193                 return EXT4_ALLOCATE_FAILED;
194         }
195
196         if (len > 0) {
197                 struct block_allocation* alloc = inode_allocate_file_extents(inode, len, filename);
198                 if (alloc) {
199                         alloc->filename = strdup(filename);
200                         alloc->next = saved_allocation_head;
201                         saved_allocation_head = alloc;
202                 }
203         }
204
205         inode->i_mode = S_IFREG;
206         inode->i_links_count = 1;
207         inode->i_flags |= aux_info.default_i_flags;
208
209         return inode_num;
210 }
211
212 /* Creates a file on disk.  Returns the inode number of the new file */
213 u32 make_link(const char *link)
214 {
215         struct ext4_inode *inode;
216         u32 inode_num;
217         u32 len = strlen(link);
218
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;
223         }
224
225         inode = get_inode(inode_num);
226         if (inode == NULL) {
227                 error("failed to get inode %u", inode_num);
228                 return EXT4_ALLOCATE_FAILED;
229         }
230
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;
235
236         if (len + 1 <= sizeof(inode->i_block)) {
237                 /* Fast symlink */
238                 memcpy((char*)inode->i_block, link, len);
239         } else {
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;
243         }
244
245         return inode_num;
246 }
247
248 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
249 {
250         struct ext4_inode *inode = get_inode(inode_num);
251
252         if (!inode)
253                 return -1;
254
255         inode->i_mode |= mode;
256         inode->i_uid = uid;
257         inode->i_gid = gid;
258         inode->i_mtime = mtime;
259         inode->i_atime = mtime;
260         inode->i_ctime = mtime;
261
262         return 0;
263 }
264
265 /*
266  * Returns the amount of free space available in the specified
267  * xattr region
268  */
269 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
270 {
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);
275         }
276
277         if (((char *) entry) > end) {
278                 error("unexpected read beyond end of xattr space");
279                 return 0;
280         }
281
282         return end - ((char *) entry);
283 }
284
285 /*
286  * Returns a pointer to the free space immediately after the
287  * last xattr element
288  */
289 static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
290 {
291         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
292                 // skip entry
293         }
294         return entry;
295 }
296
297 /*
298  * assert that the elements in the ext4 xattr section are in sorted order
299  *
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
302  * sorting algorithm:
303  *
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".
311  *
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().
314  */
315 static void xattr_assert_sane(struct ext4_xattr_entry *entry)
316 {
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)) {
320                         return;
321                 }
322
323                 int cmp = next->e_name_index - entry->e_name_index;
324                 if (cmp == 0)
325                         cmp = next->e_name_len - entry->e_name_len;
326                 if (cmp == 0)
327                         cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
328                 if (cmp < 0) {
329                         error("BUG: extended attributes are not sorted\n");
330                         return;
331                 }
332                 if (cmp == 0) {
333                         error("BUG: duplicate extended attributes detected\n");
334                         return;
335                 }
336         }
337 }
338
339 #define NAME_HASH_SHIFT 5
340 #define VALUE_HASH_SHIFT 16
341
342 static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
343                 struct ext4_xattr_entry *entry)
344 {
345         u32 hash = 0;
346         char *name = entry->e_name;
347         int n;
348
349         for (n = 0; n < entry->e_name_len; n++) {
350                 hash = (hash << NAME_HASH_SHIFT) ^
351                         (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
352                         *name++;
353         }
354
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++);
363                 }
364         }
365         entry->e_hash = cpu_to_le32(hash);
366 }
367
368 #undef NAME_HASH_SHIFT
369 #undef VALUE_HASH_SHIFT
370
371 static struct ext4_xattr_entry* xattr_addto_range(
372                 void *block_start,
373                 void *block_end,
374                 struct ext4_xattr_entry *first,
375                 int name_index,
376                 const char *name,
377                 const void *value,
378                 size_t value_len)
379 {
380         size_t name_len = strlen(name);
381         if (name_len > 255)
382                 return NULL;
383
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);
386
387         if (needed_size > available_size)
388                 return NULL;
389
390         struct ext4_xattr_entry *new_entry = xattr_get_last(first);
391         memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
392
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);
398
399         char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
400         size_t e_value_offs = val - (char *) block_start;
401
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);
405
406         xattr_assert_sane(first);
407         return new_entry;
408 }
409
410 static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
411                 const char *name, const void *value, size_t value_len)
412 {
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;
416
417         struct ext4_xattr_entry *result =
418                 xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
419
420         if (result == NULL)
421                 return -1;
422
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);
425
426         return 0;
427 }
428
429 static int xattr_addto_block(struct ext4_inode *inode, int name_index,
430                 const char *name, const void *value, size_t value_len)
431 {
432         struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
433         if (!header)
434                 return -1;
435
436         struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
437         char *block_end = ((char *) header) + info.block_size;
438
439         struct ext4_xattr_entry *result =
440                 xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
441
442         if (result == NULL)
443                 return -1;
444
445         ext4_xattr_hash_entry(header, result);
446         return 0;
447 }
448
449
450 static int xattr_add(u32 inode_num, int name_index, const char *name,
451                 const void *value, size_t value_len)
452 {
453         if (!value)
454                 return 0;
455
456         struct ext4_inode *inode = get_inode(inode_num);
457
458         if (!inode)
459                 return -1;
460
461         int result = xattr_addto_inode(inode, name_index, name, value, value_len);
462         if (result != 0) {
463                 result = xattr_addto_block(inode, name_index, name, value, value_len);
464         }
465         return result;
466 }
467
468 int inode_set_selinux(u32 inode_num, const char *secon)
469 {
470         if (!secon)
471                 return 0;
472
473         return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
474                 XATTR_SELINUX_SUFFIX, secon, strlen(secon) + 1);
475 }
476
477 int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
478         if (capabilities == 0)
479                 return 0;
480
481         struct vfs_cap_data cap_data;
482         memset(&cap_data, 0, sizeof(cap_data));
483
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;
489
490         return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
491                 XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
492 }