contents.c revision 2e5c52322d54d0f98d36b499fcaa31a0e84ca87c
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#include "ext4_utils.h"
22#include "ext4.h"
23#include "make_ext4fs.h"
24#include "allocate.h"
25#include "contents.h"
26#include "extent.h"
27#include "indirect.h"
28#include "xattr.h"
29
30#ifdef USE_MINGW
31#define S_IFLNK 0  /* used by make_link, not needed under mingw */
32#endif
33
34static u32 dentry_size(u32 entries, struct dentry *dentries)
35{
36	u32 len = 24;
37	unsigned int i;
38	unsigned int dentry_len;
39
40	for (i = 0; i < entries; i++) {
41		dentry_len = 8 + ALIGN(strlen(dentries[i].filename), 4);
42		if (len % info.block_size + dentry_len > info.block_size)
43			len += info.block_size - (len % info.block_size);
44		len += dentry_len;
45	}
46
47	/* include size of the dentry used to pad until the end of the block */
48	if (len % info.block_size + 8 > info.block_size)
49		len += info.block_size - (len % info.block_size);
50	len += 8;
51
52	return len;
53}
54
55static struct ext4_dir_entry_2 *add_dentry(u8 *data, u32 *offset,
56		struct ext4_dir_entry_2 *prev, u32 inode, const char *name,
57		u8 file_type)
58{
59	u8 name_len = strlen(name);
60	u16 rec_len = 8 + ALIGN(name_len, 4);
61	struct ext4_dir_entry_2 *dentry;
62
63	u32 start_block = *offset / info.block_size;
64	u32 end_block = (*offset + rec_len - 1) / info.block_size;
65	if (start_block != end_block) {
66		/* Adding this dentry will cross a block boundary, so pad the previous
67		   dentry to the block boundary */
68		if (!prev)
69			critical_error("no prev");
70		prev->rec_len += end_block * info.block_size - *offset;
71		*offset = end_block * info.block_size;
72	}
73
74	dentry = (struct ext4_dir_entry_2 *)(data + *offset);
75	dentry->inode = inode;
76	dentry->rec_len = rec_len;
77	dentry->name_len = name_len;
78	dentry->file_type = file_type;
79	memcpy(dentry->name, name, name_len);
80
81	*offset += rec_len;
82	return dentry;
83}
84
85/* Creates a directory structure for an array of directory entries, dentries,
86   and stores the location of the structure in an inode.  The new inode's
87   .. link is set to dir_inode_num.  Stores the location of the inode number
88   of each directory entry into dentries[i].inode, to be filled in later
89   when the inode for the entry is allocated.  Returns the inode number of the
90   new directory */
91u32 make_directory(u32 dir_inode_num, u32 entries, struct dentry *dentries,
92	u32 dirs)
93{
94	struct ext4_inode *inode;
95	u32 blocks;
96	u32 len;
97	u32 offset = 0;
98	u32 inode_num;
99	u8 *data;
100	unsigned int i;
101	struct ext4_dir_entry_2 *dentry;
102
103	blocks = DIV_ROUND_UP(dentry_size(entries, dentries), info.block_size);
104	len = blocks * info.block_size;
105
106	if (dir_inode_num) {
107		inode_num = allocate_inode(info);
108	} else {
109		dir_inode_num = EXT4_ROOT_INO;
110		inode_num = EXT4_ROOT_INO;
111	}
112
113	if (inode_num == EXT4_ALLOCATE_FAILED) {
114		error("failed to allocate inode\n");
115		return EXT4_ALLOCATE_FAILED;
116	}
117
118	add_directory(inode_num);
119
120	inode = get_inode(inode_num);
121	if (inode == NULL) {
122		error("failed to get inode %u", inode_num);
123		return EXT4_ALLOCATE_FAILED;
124	}
125
126	data = inode_allocate_data_extents(inode, len, len);
127	if (data == NULL) {
128		error("failed to allocate %u extents", len);
129		return EXT4_ALLOCATE_FAILED;
130	}
131
132	inode->i_mode = S_IFDIR;
133	inode->i_links_count = dirs + 2;
134	inode->i_flags |= aux_info.default_i_flags;
135
136	dentry = NULL;
137
138	dentry = add_dentry(data, &offset, NULL, inode_num, ".", EXT4_FT_DIR);
139	if (!dentry) {
140		error("failed to add . directory");
141		return EXT4_ALLOCATE_FAILED;
142	}
143
144	dentry = add_dentry(data, &offset, dentry, dir_inode_num, "..", EXT4_FT_DIR);
145	if (!dentry) {
146		error("failed to add .. directory");
147		return EXT4_ALLOCATE_FAILED;
148	}
149
150	for (i = 0; i < entries; i++) {
151		dentry = add_dentry(data, &offset, dentry, 0,
152				dentries[i].filename, dentries[i].file_type);
153		if (offset > len || (offset == len && i != entries - 1))
154			critical_error("internal error: dentry for %s ends at %d, past %d\n",
155				dentries[i].filename, offset, len);
156		dentries[i].inode = &dentry->inode;
157		if (!dentry) {
158			error("failed to add directory");
159			return EXT4_ALLOCATE_FAILED;
160		}
161	}
162
163	dentry = (struct ext4_dir_entry_2 *)(data + offset);
164	dentry->inode = 0;
165	dentry->rec_len = len - offset;
166	dentry->name_len = 0;
167	dentry->file_type = EXT4_FT_UNKNOWN;
168
169	return inode_num;
170}
171
172/* Creates a file on disk.  Returns the inode number of the new file */
173u32 make_file(const char *filename, u64 len)
174{
175	struct ext4_inode *inode;
176	u32 inode_num;
177
178	inode_num = allocate_inode(info);
179	if (inode_num == EXT4_ALLOCATE_FAILED) {
180		error("failed to allocate inode\n");
181		return EXT4_ALLOCATE_FAILED;
182	}
183
184	inode = get_inode(inode_num);
185	if (inode == NULL) {
186		error("failed to get inode %u", inode_num);
187		return EXT4_ALLOCATE_FAILED;
188	}
189
190	if (len > 0)
191		inode_allocate_file_extents(inode, len, filename);
192
193	inode->i_mode = S_IFREG;
194	inode->i_links_count = 1;
195	inode->i_flags |= aux_info.default_i_flags;
196
197	return inode_num;
198}
199
200/* Creates a file on disk.  Returns the inode number of the new file */
201u32 make_link(const char *filename, const char *link)
202{
203	struct ext4_inode *inode;
204	u32 inode_num;
205	u32 len = strlen(link);
206
207	inode_num = allocate_inode(info);
208	if (inode_num == EXT4_ALLOCATE_FAILED) {
209		error("failed to allocate inode\n");
210		return EXT4_ALLOCATE_FAILED;
211	}
212
213	inode = get_inode(inode_num);
214	if (inode == NULL) {
215		error("failed to get inode %u", inode_num);
216		return EXT4_ALLOCATE_FAILED;
217	}
218
219	inode->i_mode = S_IFLNK;
220	inode->i_links_count = 1;
221	inode->i_flags |= aux_info.default_i_flags;
222	inode->i_size_lo = len;
223
224	if (len + 1 <= sizeof(inode->i_block)) {
225		/* Fast symlink */
226		memcpy((char*)inode->i_block, link, len);
227	} else {
228		u8 *data = inode_allocate_data_indirect(inode, info.block_size, info.block_size);
229		memcpy(data, link, len);
230		inode->i_blocks_lo = info.block_size / 512;
231	}
232
233	return inode_num;
234}
235
236int inode_set_permissions(u32 inode_num, u16 mode, u16 uid, u16 gid, u32 mtime)
237{
238	struct ext4_inode *inode = get_inode(inode_num);
239
240	if (!inode)
241		return -1;
242
243	inode->i_mode |= mode;
244	inode->i_uid = uid;
245	inode->i_gid = gid;
246	inode->i_mtime = mtime;
247	inode->i_atime = mtime;
248	inode->i_ctime = mtime;
249
250	return 0;
251}
252
253#ifdef HAVE_SELINUX
254#define XATTR_SELINUX_SUFFIX "selinux"
255
256/* XXX */
257#define cpu_to_le32(x) (x)
258#define cpu_to_le16(x) (x)
259
260int inode_set_selinux(u32 inode_num, const char *secon)
261{
262	struct ext4_inode *inode = get_inode(inode_num);
263	u32 *hdr;
264	struct ext4_xattr_entry *entry;
265	size_t name_len = strlen(XATTR_SELINUX_SUFFIX);
266	size_t value_len = strlen(secon)+1;
267	size_t size, min_offs;
268	char *val;
269
270	if (!secon)
271		return 0;
272
273	if (!inode)
274		return -1;
275
276	hdr = (u32 *) (inode + 1);
277	*hdr = cpu_to_le32(EXT4_XATTR_MAGIC);
278	entry = (struct ext4_xattr_entry *) (hdr+1);
279	memset(entry, 0, EXT4_XATTR_LEN(name_len));
280	entry->e_name_index = EXT4_XATTR_INDEX_SECURITY;
281	entry->e_name_len = name_len;
282	memcpy(entry->e_name, XATTR_SELINUX_SUFFIX, name_len);
283	entry->e_value_size = cpu_to_le32(value_len);
284	min_offs = (char *)inode + info.inode_size - (char*) entry;
285	size = EXT4_XATTR_SIZE(value_len);
286	val = (char *)entry + min_offs - size;
287	entry->e_value_offs = cpu_to_le16(min_offs - size);
288	memset(val + size - EXT4_XATTR_PAD, 0, EXT4_XATTR_PAD);
289	memcpy(val, secon, value_len);
290	inode->i_extra_isize = cpu_to_le16(sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE);
291
292	return 0;
293}
294#else
295int inode_set_selinux(u32 inode_num, const char *secon)
296{
297	return 0;
298}
299#endif
300