OSDN Git Service

586e97e6a5fb574ef88a49a3169c20ca12188062
[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 <sys/stat.h>
18 #include <string.h>
19 #include <stdio.h>
20 #if defined(__linux__)
21 #include <linux/capability.h>
22 #include <linux/xattr.h>
23 #else
24 #include <stdint.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
30
31 struct vfs_cap_data {
32         uint32_t magic_etc;
33         struct {
34                 uint32_t permitted;
35                 uint32_t inheritable;
36         } data[VFS_CAP_U32];
37 };
38 #define XATTR_SELINUX_SUFFIX "selinux"
39 #define XATTR_CAPS_SUFFIX "capability"
40 #endif /* !defined(__linux__) */
41
42 #include "ext4_utils.h"
43 #include "ext4.h"
44 #include "make_ext4fs.h"
45 #include "allocate.h"
46 #include "contents.h"
47 #include "extent.h"
48 #include "indirect.h"
49 #include "xattr.h"
50
51 #ifdef USE_MINGW
52 #define S_IFLNK 0  /* used by make_link, not needed under mingw */
53 #endif
54
55 static u32 dentry_size(u32 entries, struct dentry *dentries)
56 {
57         u32 len = 24;
58         unsigned int i;
59         unsigned int dentry_len;
60
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);
65                 len += dentry_len;
66         }
67
68         return len;
69 }
70
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,
73                 u8 file_type)
74 {
75         u8 name_len = strlen(name);
76         u16 rec_len = 8 + ALIGN(name_len, 4);
77         struct ext4_dir_entry_2 *dentry;
78
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 */
84                 if (!prev)
85                         critical_error("no prev");
86                 prev->rec_len += end_block * info.block_size - *offset;
87                 *offset = end_block * info.block_size;
88         }
89
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);
96
97         *offset += rec_len;
98         return dentry;
99 }
100
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
106    new directory */
107 u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
108         u32 dirs)
109 {
110         struct ext4_inode *inode;
111         u32 blocks;
112         u32 len;
113         u32 offset = 0;
114         u32 inode_num;
115         u8 *data;
116         unsigned int i;
117         struct ext4_dir_entry_2 *dentry;
118
119         blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
120         len = blocks * info.block_size;
121
122         if (dir_inode_num) {
123                 inode_num = allocate_inode(info);
124         } else {
125                 dir_inode_num = EXT4_ROOT_INO;
126                 inode_num = EXT4_ROOT_INO;
127         }
128
129         if (inode_num == EXT4_ALLOCATE_FAILED) {
130                 error("failed to allocate inode\n");
131                 return EXT4_ALLOCATE_FAILED;
132         }
133
134         add_directory(inode_num);
135
136         inode = get_inode(inode_num);
137         if (inode == NULL) {
138                 error("failed to get inode %u", inode_num);
139                 return EXT4_ALLOCATE_FAILED;
140         }
141
142         data = inode_allocate_data_extents(inode, len, len);
143         if (data == NULL) {
144                 error("failed to allocate %u extents", len);
145                 return EXT4_ALLOCATE_FAILED;
146         }
147
148         inode->i_mode = S_IFDIR;
149         inode->i_links_count = dirs + 2;
150         inode->i_flags |= aux_info.default_i_flags;
151
152         dentry = NULL;
153
154         dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
155         if (!dentry) {
156                 error("failed to add . directory");
157                 return EXT4_ALLOCATE_FAILED;
158         }
159
160         dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
161         if (!dentry) {
162                 error("failed to add .. directory");
163                 return EXT4_ALLOCATE_FAILED;
164         }
165
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;
173                 if (!dentry) {
174                         error("failed to add directory");
175                         return EXT4_ALLOCATE_FAILED;
176                 }
177         }
178
179         /* pad the last dentry out to the end of the block */
180         dentry->rec_len += len - offset;
181
182         return inode_num;
183 }
184
185 /* Creates a file on disk.  Returns the inode number of the new file */
186 u32 make_file(const char *filename, u64 len)
187 {
188         struct ext4_inode *inode;
189         u32 inode_num;
190
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;
195         }
196
197         inode = get_inode(inode_num);
198         if (inode == NULL) {
199                 error("failed to get inode %u", inode_num);
200                 return EXT4_ALLOCATE_FAILED;
201         }
202
203         if (len > 0)
204                 inode_allocate_file_extents(inode, len, filename);
205
206         inode->i_mode = S_IFREG;
207         inode->i_links_count = 1;
208         inode->i_flags |= aux_info.default_i_flags;
209
210         return inode_num;
211 }
212
213 /* Creates a file on disk.  Returns the inode number of the new file */
214 u32 make_link(const char *link)
215 {
216         struct ext4_inode *inode;
217         u32 inode_num;
218         u32 len = strlen(link);
219
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;
224         }
225
226         inode = get_inode(inode_num);
227         if (inode == NULL) {
228                 error("failed to get inode %u", inode_num);
229                 return EXT4_ALLOCATE_FAILED;
230         }
231
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;
236
237         if (len + 1 <= sizeof(inode->i_block)) {
238                 /* Fast symlink */
239                 memcpy((char*)inode->i_block, link, len);
240         } else {
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;
244         }
245
246         return inode_num;
247 }
248
249 int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
250 {
251         struct ext4_inode *inode = get_inode(inode_num);
252
253         if (!inode)
254                 return -1;
255
256         inode->i_mode |= mode;
257         inode->i_uid = uid;
258         inode->i_gid = gid;
259         inode->i_mtime = mtime;
260         inode->i_atime = mtime;
261         inode->i_ctime = mtime;
262
263         return 0;
264 }
265
266 /*
267  * Returns the amount of free space available in the specified
268  * xattr region
269  */
270 static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
271 {
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                 __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++);
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 }
493