contents.c revision 4df62f342dbbe2f5cca831ce789dc0426d32ec03
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#include <linux/capability.h>
21#include <linux/xattr.h>
22
23#include "ext4_utils.h"
24#include "ext4.h"
25#include "make_ext4fs.h"
26#include "allocate.h"
27#include "contents.h"
28#include "extent.h"
29#include "indirect.h"
30#include "xattr.h"
31
32#ifdef USE_MINGW
33#define S_IFLNK 0  /* used by make_link, not needed under mingw */
34#endif
35
36static u32 dentry_size(u32 entries, struct dentry *dentries)
37{
38	u32 len = 24;
39	unsigned int i;
40	unsigned int dentry_len;
41
42	for (i = 0; i < entries; i++) {
43		dentry_len = 8 + ALIGN(strlen(dentries[i].filename), 4);
44		if (len % info.block_size + dentry_len > info.block_size)
45			len += info.block_size - (len % info.block_size);
46		len += dentry_len;
47	}
48
49	return len;
50}
51
52static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
53		struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
54		u8 file_type)
55{
56	u8 name_len = strlen(name);
57	u16 rec_len = 8 + ALIGN(name_len, 4);
58	struct ext4_dir_entry_2 *dentry;
59
60	u32 start_block = *offset / info.block_size;
61	u32 end_block = (*offset + rec_len - 1) / info.block_size;
62	if (start_block != end_block) {
63		/* Adding this dentry will cross a block boundary, so pad the previous
64		   dentry to the block boundary */
65		if (!prev)
66			critical_error("no prev");
67		prev->rec_len += end_block * info.block_size - *offset;
68		*offset = end_block * info.block_size;
69	}
70
71	dentry = (struct ext4_dir_entry_2 *)(data + *offset);
72	dentry->inode = inode;
73	dentry->rec_len = rec_len;
74	dentry->name_len = name_len;
75	dentry->file_type = file_type;
76	memcpy(dentry->name, name, name_len);
77
78	*offset += rec_len;
79	return dentry;
80}
81
82/* Creates a directory structure for an array of directory entries, dentries,
83   and stores the location of the structure in an inode.  The new inode's
84   .. link is set to dir_inode_num.  Stores the location of the inode number
85   of each directory entry into dentries[i].inode, to be filled in later
86   when the inode for the entry is allocated.  Returns the inode number of the
87   new directory */
88u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
89	u32 dirs)
90{
91	struct ext4_inode *inode;
92	u32 blocks;
93	u32 len;
94	u32 offset = 0;
95	u32 inode_num;
96	u8 *data;
97	unsigned int i;
98	struct ext4_dir_entry_2 *dentry;
99
100	blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
101	len = blocks * info.block_size;
102
103	if (dir_inode_num) {
104		inode_num = allocate_inode(info);
105	} else {
106		dir_inode_num = EXT4_ROOT_INO;
107		inode_num = EXT4_ROOT_INO;
108	}
109
110	if (inode_num == EXT4_ALLOCATE_FAILED) {
111		error("failed to allocate inode\n");
112		return EXT4_ALLOCATE_FAILED;
113	}
114
115	add_directory(inode_num);
116
117	inode = get_inode(inode_num);
118	if (inode == NULL) {
119		error("failed to get inode %u", inode_num);
120		return EXT4_ALLOCATE_FAILED;
121	}
122
123	data = inode_allocate_data_extents(inode, len, len);
124	if (data == NULL) {
125		error("failed to allocate %u extents", len);
126		return EXT4_ALLOCATE_FAILED;
127	}
128
129	inode->i_mode = S_IFDIR;
130	inode->i_links_count = dirs + 2;
131	inode->i_flags |= aux_info.default_i_flags;
132
133	dentry = NULL;
134
135	dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
136	if (!dentry) {
137		error("failed to add . directory");
138		return EXT4_ALLOCATE_FAILED;
139	}
140
141	dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
142	if (!dentry) {
143		error("failed to add .. directory");
144		return EXT4_ALLOCATE_FAILED;
145	}
146
147	for (i = 0; i < entries; i++) {
148		dentry = add_dentry(data, &offset, dentry, 0,
149				dentries[i].filename, dentries[i].file_type);
150		if (offset > len || (offset == len && i != entries - 1))
151			critical_error("internal error: dentry for %s ends at %d, past %d\n",
152				dentries[i].filename, offset, len);
153		dentries[i].inode = &dentry->inode;
154		if (!dentry) {
155			error("failed to add directory");
156			return EXT4_ALLOCATE_FAILED;
157		}
158	}
159
160	/* pad the last dentry out to the end of the block */
161	dentry->rec_len += len - offset;
162
163	return inode_num;
164}
165
166/* Creates a file on disk.  Returns the inode number of the new file */
167u32 make_file(const char *filename, u64 len)
168{
169	struct ext4_inode *inode;
170	u32 inode_num;
171
172	inode_num = allocate_inode(info);
173	if (inode_num == EXT4_ALLOCATE_FAILED) {
174		error("failed to allocate inode\n");
175		return EXT4_ALLOCATE_FAILED;
176	}
177
178	inode = get_inode(inode_num);
179	if (inode == NULL) {
180		error("failed to get inode %u", inode_num);
181		return EXT4_ALLOCATE_FAILED;
182	}
183
184	if (len > 0)
185		inode_allocate_file_extents(inode, len, filename);
186
187	inode->i_mode = S_IFREG;
188	inode->i_links_count = 1;
189	inode->i_flags |= aux_info.default_i_flags;
190
191	return inode_num;
192}
193
194/* Creates a file on disk.  Returns the inode number of the new file */
195u32 make_link(const char *link)
196{
197	struct ext4_inode *inode;
198	u32 inode_num;
199	u32 len = strlen(link);
200
201	inode_num = allocate_inode(info);
202	if (inode_num == EXT4_ALLOCATE_FAILED) {
203		error("failed to allocate inode\n");
204		return EXT4_ALLOCATE_FAILED;
205	}
206
207	inode = get_inode(inode_num);
208	if (inode == NULL) {
209		error("failed to get inode %u", inode_num);
210		return EXT4_ALLOCATE_FAILED;
211	}
212
213	inode->i_mode = S_IFLNK;
214	inode->i_links_count = 1;
215	inode->i_flags |= aux_info.default_i_flags;
216	inode->i_size_lo = len;
217
218	if (len + 1 <= sizeof(inode->i_block)) {
219		/* Fast symlink */
220		memcpy((char*)inode->i_block, link, len);
221	} else {
222		u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
223		memcpy(data, link, len);
224		inode->i_blocks_lo = info.block_size / 512;
225	}
226
227	return inode_num;
228}
229
230int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
231{
232	struct ext4_inode *inode = get_inode(inode_num);
233
234	if (!inode)
235		return -1;
236
237	inode->i_mode |= mode;
238	inode->i_uid = uid;
239	inode->i_gid = gid;
240	inode->i_mtime = mtime;
241	inode->i_atime = mtime;
242	inode->i_ctime = mtime;
243
244	return 0;
245}
246
247/*
248 * Returns the amount of free space available in the specified
249 * xattr region
250 */
251static size_t xattr_free_space(struct ext4_xattr_entry *entry, char *end)
252{
253	while(!IS_LAST_ENTRY(entry) && (((char *) entry) < end)) {
254		end   -= EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
255		entry  = EXT4_XATTR_NEXT(entry);
256	}
257
258	if (((char *) entry) > end) {
259		error("unexpected read beyond end of xattr space");
260		return 0;
261	}
262
263	return end - ((char *) entry);
264}
265
266/*
267 * Returns a pointer to the free space immediately after the
268 * last xattr element
269 */
270static struct ext4_xattr_entry* xattr_get_last(struct ext4_xattr_entry *entry)
271{
272	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
273		// skip entry
274	}
275	return entry;
276}
277
278/*
279 * assert that the elements in the ext4 xattr section are in sorted order
280 *
281 * The ext4 filesystem requires extended attributes to be sorted when
282 * they're not stored in the inode. The kernel ext4 code uses the following
283 * sorting algorithm:
284 *
285 * 1) First sort extended attributes by their name_index. For example,
286 *    EXT4_XATTR_INDEX_USER (1) comes before EXT4_XATTR_INDEX_SECURITY (6).
287 * 2) If the name_indexes are equal, then sorting is based on the length
288 *    of the name. For example, XATTR_SELINUX_SUFFIX ("selinux") comes before
289 *    XATTR_CAPS_SUFFIX ("capability") because "selinux" is shorter than "capability"
290 * 3) If the name_index and name_length are equal, then memcmp() is used to determine
291 *    which name comes first. For example, "selinux" would come before "yelinux".
292 *
293 * This method is intended to implement the sorting function defined in
294 * the Linux kernel file fs/ext4/xattr.c function ext4_xattr_find_entry().
295 */
296static void xattr_assert_sane(struct ext4_xattr_entry *entry)
297{
298	for( ; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
299		struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
300		if (IS_LAST_ENTRY(next)) {
301			return;
302		}
303
304		int cmp = next->e_name_index - entry->e_name_index;
305		if (cmp == 0)
306			cmp = next->e_name_len - entry->e_name_len;
307		if (cmp == 0)
308			cmp = memcmp(next->e_name, entry->e_name, next->e_name_len);
309		if (cmp < 0) {
310			error("BUG: extended attributes are not sorted\n");
311			return;
312		}
313		if (cmp == 0) {
314			error("BUG: duplicate extended attributes detected\n");
315			return;
316		}
317	}
318}
319
320#define NAME_HASH_SHIFT 5
321#define VALUE_HASH_SHIFT 16
322
323static void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
324		struct ext4_xattr_entry *entry)
325{
326	__u32 hash = 0;
327	char *name = entry->e_name;
328	int n;
329
330	for (n = 0; n < entry->e_name_len; n++) {
331		hash = (hash << NAME_HASH_SHIFT) ^
332			(hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
333			*name++;
334	}
335
336	if (entry->e_value_block == 0 && entry->e_value_size != 0) {
337		__le32 *value = (__le32 *)((char *)header +
338			le16_to_cpu(entry->e_value_offs));
339		for (n = (le32_to_cpu(entry->e_value_size) +
340			EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
341			hash = (hash << VALUE_HASH_SHIFT) ^
342				(hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
343				le32_to_cpu(*value++);
344		}
345	}
346	entry->e_hash = cpu_to_le32(hash);
347}
348
349#undef NAME_HASH_SHIFT
350#undef VALUE_HASH_SHIFT
351
352static struct ext4_xattr_entry* xattr_addto_range(
353		void *block_start,
354		void *block_end,
355		struct ext4_xattr_entry *first,
356		int name_index,
357		const char *name,
358		const void *value,
359		size_t value_len)
360{
361	size_t name_len = strlen(name);
362	if (name_len > 255)
363		return NULL;
364
365	size_t available_size = xattr_free_space(first, block_end);
366	size_t needed_size = EXT4_XATTR_LEN(name_len) + EXT4_XATTR_SIZE(value_len);
367
368	if (needed_size > available_size)
369		return NULL;
370
371	struct ext4_xattr_entry *new_entry = xattr_get_last(first);
372	memset(new_entry, 0, EXT4_XATTR_LEN(name_len));
373
374	new_entry->e_name_len = name_len;
375	new_entry->e_name_index = name_index;
376	memcpy(new_entry->e_name, name, name_len);
377	new_entry->e_value_block = 0;
378	new_entry->e_value_size = cpu_to_le32(value_len);
379
380	char *val = (char *) new_entry + available_size - EXT4_XATTR_SIZE(value_len);
381	size_t e_value_offs = val - (char *) block_start;
382
383	new_entry->e_value_offs = cpu_to_le16(e_value_offs);
384	memset(val, 0, EXT4_XATTR_SIZE(value_len));
385	memcpy(val, value, value_len);
386
387	xattr_assert_sane(first);
388	return new_entry;
389}
390
391static int xattr_addto_inode(struct ext4_inode *inode, int name_index,
392		const char *name, const void *value, size_t value_len)
393{
394	struct ext4_xattr_ibody_header *hdr = (struct ext4_xattr_ibody_header *) (inode + 1);
395	struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (hdr + 1);
396	char *block_end = ((char *) inode) + info.inode_size;
397
398	struct ext4_xattr_entry *result =
399		xattr_addto_range(first, block_end, first, name_index, name, value, value_len);
400
401	if (result == NULL)
402		return -1;
403
404	hdr->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
405	inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
406
407	return 0;
408}
409
410static int xattr_addto_block(struct ext4_inode *inode, int name_index,
411		const char *name, const void *value, size_t value_len)
412{
413	struct ext4_xattr_header *header = get_xattr_block_for_inode(inode);
414	if (!header)
415		return -1;
416
417	struct ext4_xattr_entry *first = (struct ext4_xattr_entry *) (header + 1);
418	char *block_end = ((char *) header) + info.block_size;
419
420	struct ext4_xattr_entry *result =
421		xattr_addto_range(header, block_end, first, name_index, name, value, value_len);
422
423	if (result == NULL)
424		return -1;
425
426	ext4_xattr_hash_entry(header, result);
427	return 0;
428}
429
430
431static int xattr_add(u32 inode_num, int name_index, const char *name,
432		const void *value, size_t value_len)
433{
434	if (!value)
435		return 0;
436
437	struct ext4_inode *inode = get_inode(inode_num);
438
439	if (!inode)
440		return -1;
441
442	int result = xattr_addto_inode(inode, name_index, name, value, value_len);
443	if (result != 0) {
444		result = xattr_addto_block(inode, name_index, name, value, value_len);
445	}
446	return result;
447}
448
449int inode_set_selinux(u32 inode_num, const char *secon)
450{
451	return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
452		XATTR_SELINUX_SUFFIX, secon, strlen(secon) + 1);
453}
454
455int inode_set_capabilities(u32 inode_num, uint64_t capabilities) {
456	if (capabilities == 0)
457		return 0;
458
459	struct vfs_cap_data cap_data;
460	memset(&cap_data, 0, sizeof(cap_data));
461
462	cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
463	cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
464	cap_data.data[0].inheritable = 0;
465	cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
466	cap_data.data[1].inheritable = 0;
467
468	return xattr_add(inode_num, EXT4_XATTR_INDEX_SECURITY,
469		XATTR_CAPS_SUFFIX, &cap_data, sizeof(cap_data));
470}
471
472