1ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/*
2ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Copyright (C) 2010 The Android Open Source Project
3ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *
4ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Licensed under the Apache License, Version 2.0 (the "License");
5ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * you may not use this file except in compliance with the License.
6ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * You may obtain a copy of the License at
7ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *
8ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *      http://www.apache.org/licenses/LICENSE-2.0
9ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross *
10ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * Unless required by applicable law or agreed to in writing, software
11ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * distributed under the License is distributed on an "AS IS" BASIS,
12ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * See the License for the specific language governing permissions and
14ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross * limitations under the License.
15ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */
16ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
1733f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "make_ext4fs.h"
1833f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "ext4_utils.h"
1933f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "allocate.h"
2033f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "contents.h"
2133f96c66e9a1f2e266a75e5e84c091dffa6ef118Colin Cross#include "uuid.h"
22dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross#include "wipe.h"
23dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross
24dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross#include <sparse/sparse.h>
25ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
264605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#include <assert.h>
27ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <dirent.h>
280349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau#include <fcntl.h>
29ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#include <libgen.h>
30881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross#include <stdio.h>
31881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross#include <stdlib.h>
32881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross#include <string.h>
33881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross#include <unistd.h>
34881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross#include <sys/stat.h>
35881cca2f88ddcce86483b3ba95546b5641de8c0eColin Cross#include <sys/types.h>
36ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
374605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifdef USE_MINGW
384605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll
394605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#include <winsock2.h>
404605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll
414605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll/* These match the Linux definitions of these flags.
424605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll   L_xx is defined to avoid conflicting with the win32 versions.
434605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll*/
444605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define L_S_IRUSR 00400
454605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define L_S_IWUSR 00200
464605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define L_S_IXUSR 00100
474605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
484605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IRGRP 00040
494605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IWGRP 00020
504605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IXGRP 00010
514605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
524605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IROTH 00004
534605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IWOTH 00002
544605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IXOTH 00001
554605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
564605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_ISUID 0004000
574605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_ISGID 0002000
584605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#define S_ISVTX 0001000
594605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll
600349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau#else
610349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau
620349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau#define O_BINARY 0
630349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau
644605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif
654605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll
66ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* TODO: Not implemented:
67ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross   Allocating blocks in the same block group as the file inode
68ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross   Hash or binary tree directories
697a8bee7653c393d8da0e28668cb51d3ccab793e8Colin Cross   Special files: sockets, devices, fifos
70ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross */
71ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
72ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic int filter_dot(const struct dirent *d)
73ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
74ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
75ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
76ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
77ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic u32 build_default_directory_structure()
78ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
79ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 inode;
80ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 root_inode;
81ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct dentry dentries = {
82ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			.filename = "lost+found",
83ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			.file_type = EXT4_FT_DIR,
84ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			.mode = S_IRWXU,
85ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			.uid = 0,
86de61f980c7b034eefac6e0ace718b3c1eb3f6252Colin Cross			.gid = 0,
87de61f980c7b034eefac6e0ace718b3c1eb3f6252Colin Cross			.mtime = 0,
88ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	};
89ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	root_inode = make_directory(0, 1, &dentries, 1);
90ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	inode = make_directory(root_inode, 0, NULL, 0);
91ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	*dentries.inode = inode;
9275249edab0b22ea9aae9c7278b9f2c196c7d25d4Ken Sumrall	inode_set_permissions(inode, dentries.mode,
9375249edab0b22ea9aae9c7278b9f2c196c7d25d4Ken Sumrall		dentries.uid, dentries.gid, dentries.mtime);
94ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
95ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return root_inode;
96ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
97ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
984605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifndef USE_MINGW
99ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross/* Read a local directory and create the same tree in the generated filesystem.
100ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross   Calls itself recursively with each directory in the given directory */
101ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic u32 build_directory_structure(const char *full_path, const char *dir_path,
1022e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root		u32 dir_inode, fs_config_func_t fs_config_func,
1032e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root		struct selabel_handle *sehnd)
104ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
105ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	int entries = 0;
106ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct dentry *dentries;
107ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct dirent **namelist;
108ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	struct stat stat;
109ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	int ret;
110ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	int i;
111ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 inode;
112ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 entry_inode;
113ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 dirs = 0;
114ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
1158aef66d2125af8de7672a12895276802fcc1948fColin Cross	entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
116ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (entries < 0) {
117ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		error_errno("scandir");
118ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		return EXT4_ALLOCATE_FAILED;
119ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	}
120ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
121ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	dentries = calloc(entries, sizeof(struct dentry));
122ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (dentries == NULL)
123ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		critical_error_errno("malloc");
124ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
125ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	for (i = 0; i < entries; i++) {
126ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		dentries[i].filename = strdup(namelist[i]->d_name);
127ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		if (dentries[i].filename == NULL)
128ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			critical_error_errno("strdup");
129ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
130ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
131ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
132ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
133ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		free(namelist[i]);
134ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
135ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		ret = lstat(dentries[i].full_path, &stat);
136ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		if (ret < 0) {
137ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			error_errno("lstat");
138ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			i--;
139ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			entries--;
140ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			continue;
141ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		}
142ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
143ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		dentries[i].size = stat.st_size;
144ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
145de61f980c7b034eefac6e0ace718b3c1eb3f6252Colin Cross		dentries[i].mtime = stat.st_mtime;
14668e3dfd81ddb9367a0c3e0c72148c23a3227ed48Kenny Root		if (fs_config_func != NULL) {
147ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#ifdef ANDROID
148ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			unsigned int mode = 0;
149ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			unsigned int uid = 0;
150ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			unsigned int gid = 0;
151ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			int dir = S_ISDIR(stat.st_mode);
15268e3dfd81ddb9367a0c3e0c72148c23a3227ed48Kenny Root			fs_config_func(dentries[i].path, dir, &uid, &gid, &mode);
153ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].mode = mode;
154ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].uid = uid;
155ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].gid = gid;
156ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#else
157ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			error("can't set android permissions - built without android support");
158ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross#endif
159ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		}
160b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#ifdef HAVE_SELINUX
161b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		if (sehnd) {
162b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			char *sepath = NULL;
163b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			asprintf(&sepath, "/%s", dentries[i].path);
164b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) {
165b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley				error("cannot lookup security context for %s", sepath);
166b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			}
167b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			if (dentries[i].secon)
168b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley				printf("Labeling %s as %s\n", sepath, dentries[i].secon);
169b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			free(sepath);
170b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		}
171b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#endif
172ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
173ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		if (S_ISREG(stat.st_mode)) {
174ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_REG_FILE;
175ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (S_ISDIR(stat.st_mode)) {
176ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_DIR;
177ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dirs++;
178ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (S_ISCHR(stat.st_mode)) {
179ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_CHRDEV;
180ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (S_ISBLK(stat.st_mode)) {
181ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_BLKDEV;
182ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (S_ISFIFO(stat.st_mode)) {
183ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_FIFO;
184ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (S_ISSOCK(stat.st_mode)) {
185ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_SOCK;
186ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (S_ISLNK(stat.st_mode)) {
187ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].file_type = EXT4_FT_SYMLINK;
188ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			dentries[i].link = calloc(info.block_size, 1);
189ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
190ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else {
191ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			error("unknown file type on %s", dentries[i].path);
192ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			i--;
193ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			entries--;
194ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		}
195ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	}
196ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	free(namelist);
197ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
198ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	inode = make_directory(dir_inode, entries, dentries, dirs);
199ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
200ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	for (i = 0; i < entries; i++) {
201ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		if (dentries[i].file_type == EXT4_FT_REG_FILE) {
202ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			entry_inode = make_file(dentries[i].full_path, dentries[i].size);
203ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (dentries[i].file_type == EXT4_FT_DIR) {
204ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			entry_inode = build_directory_structure(dentries[i].full_path,
2052e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root					dentries[i].path, inode, fs_config_func, sehnd);
206ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
207ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			entry_inode = make_link(dentries[i].full_path, dentries[i].link);
208ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		} else {
209ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			error("unknown file type on %s", dentries[i].path);
210ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			entry_inode = 0;
211ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		}
212ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		*dentries[i].inode = entry_inode;
213ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
214ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		ret = inode_set_permissions(entry_inode, dentries[i].mode,
215de61f980c7b034eefac6e0ace718b3c1eb3f6252Colin Cross			dentries[i].uid, dentries[i].gid,
216de61f980c7b034eefac6e0ace718b3c1eb3f6252Colin Cross			dentries[i].mtime);
217ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		if (ret)
218ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			error("failed to set permissions on %s\n", dentries[i].path);
219b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		ret = inode_set_selinux(entry_inode, dentries[i].secon);
220b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		if (ret)
221b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			error("failed to set SELinux context on %s\n", dentries[i].path);
222ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
223ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		free(dentries[i].path);
224ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		free(dentries[i].full_path);
225ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		free(dentries[i].link);
226ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		free((void *)dentries[i].filename);
227b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		free(dentries[i].secon);
228ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	}
229ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
230ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	free(dentries);
231ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return inode;
232ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
2334605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif
234ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
235ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic u32 compute_block_size()
236ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
237ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return 4096;
238ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
239ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
240e4b5ae8ab07e698b95f004c9226000b02f853abcColin Crossstatic u32 compute_journal_blocks()
241e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross{
242e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
243e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	if (journal_blocks < 1024)
244e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross		journal_blocks = 1024;
245e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	if (journal_blocks > 32768)
246e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross		journal_blocks = 32768;
247e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	return journal_blocks;
248e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross}
249e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross
250ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic u32 compute_blocks_per_group()
251ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
252ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return info.block_size * 8;
253ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
254ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
255ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic u32 compute_inodes()
256ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
257ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return DIV_ROUND_UP(info.len, info.block_size) / 4;
258ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
259ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
260ec0a2e83dc66d67addeb90e83144187691852a3eColin Crossstatic u32 compute_inodes_per_group()
261ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
262ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
263ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
26496cc54a9bbc788673b7a38c23160d137211fd983Colin Cross	u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
265107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	inodes = ALIGN(inodes, (info.block_size / info.inode_size));
266107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall
267107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	/* After properly rounding up the number of inodes/group,
268107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	 * make sure to update the total inodes field in the info struct.
269107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	 */
270107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	info.inodes = inodes * block_groups;
271107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall
272107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	return inodes;
273ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
274ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
27522742ce739a046a079b2e1b03342a25472dfa352Colin Crossstatic u32 compute_bg_desc_reserve_blocks()
27622742ce739a046a079b2e1b03342a25472dfa352Colin Cross{
27722742ce739a046a079b2e1b03342a25472dfa352Colin Cross	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
27822742ce739a046a079b2e1b03342a25472dfa352Colin Cross	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
27922742ce739a046a079b2e1b03342a25472dfa352Colin Cross	u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
28022742ce739a046a079b2e1b03342a25472dfa352Colin Cross			info.block_size);
28122742ce739a046a079b2e1b03342a25472dfa352Colin Cross
28222742ce739a046a079b2e1b03342a25472dfa352Colin Cross	u32 bg_desc_reserve_blocks =
28322742ce739a046a079b2e1b03342a25472dfa352Colin Cross			DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
28422742ce739a046a079b2e1b03342a25472dfa352Colin Cross					info.block_size) - bg_desc_blocks;
28522742ce739a046a079b2e1b03342a25472dfa352Colin Cross
28622742ce739a046a079b2e1b03342a25472dfa352Colin Cross	if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
28722742ce739a046a079b2e1b03342a25472dfa352Colin Cross		bg_desc_reserve_blocks = info.block_size / sizeof(u32);
28822742ce739a046a079b2e1b03342a25472dfa352Colin Cross
28922742ce739a046a079b2e1b03342a25472dfa352Colin Cross	return bg_desc_reserve_blocks;
29022742ce739a046a079b2e1b03342a25472dfa352Colin Cross}
29122742ce739a046a079b2e1b03342a25472dfa352Colin Cross
292263eefd9fb9608432636c903423e43848e69f39dDoug Zongkervoid reset_ext4fs_info() {
293263eefd9fb9608432636c903423e43848e69f39dDoug Zongker    // Reset all the global data structures used by make_ext4fs so it
294263eefd9fb9608432636c903423e43848e69f39dDoug Zongker    // can be called again.
295263eefd9fb9608432636c903423e43848e69f39dDoug Zongker    memset(&info, 0, sizeof(info));
296263eefd9fb9608432636c903423e43848e69f39dDoug Zongker    memset(&aux_info, 0, sizeof(aux_info));
297f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross
298f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross    if (info.sparse_file) {
299f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross        sparse_file_destroy(info.sparse_file);
300f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross        info.sparse_file = NULL;
301f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross    }
302ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
303ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
3046ece70806883534d29a74b9785871505f71ecc1fStephen Smalleyint make_ext4fs(const char *filename, s64 len,
3056ece70806883534d29a74b9785871505f71ecc1fStephen Smalley                const char *mountpoint, struct selabel_handle *sehnd)
306983fb19d83d2391b19b289fc150495d8642378c4Ken Sumrall{
3070349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	int fd;
3080349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	int status;
3090349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau
3100349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	reset_ext4fs_info();
3110349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	info.len = len;
3120349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau
3130349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
3140349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	if (fd < 0) {
3150349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau		error_errno("open");
3160349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau		return EXIT_FAILURE;
3170349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	}
3180349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau
3192e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root	status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, sehnd);
3200349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	close(fd);
3210349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau
3220349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	return status;
323983fb19d83d2391b19b289fc150495d8642378c4Ken Sumrall}
324983fb19d83d2391b19b289fc150495d8642378c4Ken Sumrall
3250349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazauint make_ext4fs_internal(int fd, const char *directory,
32668e3dfd81ddb9367a0c3e0c72148c23a3227ed48Kenny Root                         char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
3272e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root                         int crc, int wipe, int init_itabs, struct selabel_handle *sehnd)
328ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross{
3290349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	u32 root_inode_num;
3300349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau	u16 root_mode;
331ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
3322ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall	if (setjmp(setjmp_env))
3332ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall		return EXIT_FAILURE; /* Handle a call to longjmp() */
3342ae7663e1e064000356ee9e49ebd08bdddc545f9Ken Sumrall
335435a8b61e925e3efb22fce08612efe210e83f791Ken Sumrall	if (info.len <= 0)
3360349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau		info.len = get_file_size(fd);
337ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
338ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.len <= 0) {
339ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		fprintf(stderr, "Need size of filesystem\n");
3400349bd9f14d252673a7a25767da4a80121aaaaf2Anatol Pomazau		return EXIT_FAILURE;
341ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	}
342ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
343ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.block_size <= 0)
344ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		info.block_size = compute_block_size();
345ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
34688833a6277f305b6a03ff29d8e9ffe717ddb4045Ken Sumrall	/* Round down the filesystem length to be a multiple of the block size */
34788833a6277f305b6a03ff29d8e9ffe717ddb4045Ken Sumrall	info.len &= ~((u64)info.block_size - 1);
34888833a6277f305b6a03ff29d8e9ffe717ddb4045Ken Sumrall
349e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	if (info.journal_blocks == 0)
350e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross		info.journal_blocks = compute_journal_blocks();
351e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross
352e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	if (info.no_journal == 0)
353e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross		info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
354e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	else
355e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross		info.journal_blocks = 0;
356e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross
357ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.blocks_per_group <= 0)
358ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		info.blocks_per_group = compute_blocks_per_group();
359ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
360ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.inodes <= 0)
361ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		info.inodes = compute_inodes();
362ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
363ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.inode_size <= 0)
364ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		info.inode_size = 256;
365ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
366ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.label == NULL)
367ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		info.label = "";
368ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
369ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	info.inodes_per_group = compute_inodes_per_group();
370ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
371ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	info.feat_compat |=
372ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			EXT4_FEATURE_COMPAT_RESIZE_INODE;
373ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
374ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	info.feat_ro_compat |=
375ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
376ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
377ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
378ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	info.feat_incompat |=
379ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			EXT4_FEATURE_INCOMPAT_EXTENTS |
380ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			EXT4_FEATURE_INCOMPAT_FILETYPE;
381ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
382ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
38322742ce739a046a079b2e1b03342a25472dfa352Colin Cross	info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
38422742ce739a046a079b2e1b03342a25472dfa352Colin Cross
385ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("Creating filesystem with parameters:\n");
386ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Size: %llu\n", info.len);
387ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Block size: %d\n", info.block_size);
388ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Blocks per group: %d\n", info.blocks_per_group);
389ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Inodes per group: %d\n", info.inodes_per_group);
390ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Inode size: %d\n", info.inode_size);
391e4b5ae8ab07e698b95f004c9226000b02f853abcColin Cross	printf("    Journal blocks: %d\n", info.journal_blocks);
392ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Label: %s\n", info.label);
393ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
394ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	ext4_create_fs_aux_info();
395ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
396ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Blocks: %llu\n", aux_info.len_blocks);
397ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("    Block groups: %d\n", aux_info.groups);
39822742ce739a046a079b2e1b03342a25472dfa352Colin Cross	printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
399ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
400f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross	info.sparse_file = sparse_file_new(info.block_size, info.len);
401f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross
402ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	block_allocator_init();
403ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
404ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	ext4_fill_in_sb();
405ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
406ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
407ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		error("failed to reserve first 10 inodes");
408ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
409ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
410ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		ext4_create_journal_inode();
411ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
412ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
413ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		ext4_create_resize_inode();
414ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
4154605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#ifdef USE_MINGW
4164605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll	// Windows needs only 'create an empty fs image' functionality
4174605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll	assert(!directory);
4184605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll	root_inode_num = build_default_directory_structure();
4194605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#else
420ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	if (directory)
4212e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root		root_inode_num = build_directory_structure(directory, mountpoint, 0,
4222e5c52322d54d0f98d36b499fcaa31a0e84ca87cKenny Root                        fs_config_func, sehnd);
423ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	else
424ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross		root_inode_num = build_default_directory_structure();
4254605b3fb8a00fa37f617a8d0fe3a095d0503a845Raphael Moll#endif
426263eefd9fb9608432636c903423e43848e69f39dDoug Zongker
427ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
428de61f980c7b034eefac6e0ace718b3c1eb3f6252Colin Cross	inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
429ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
430b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#ifdef HAVE_SELINUX
431b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley	if (sehnd) {
432b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		char *sepath = NULL;
433b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		char *secontext = NULL;
4346ece70806883534d29a74b9785871505f71ecc1fStephen Smalley
4356ece70806883534d29a74b9785871505f71ecc1fStephen Smalley		if (mountpoint[0] == '/')
4366ece70806883534d29a74b9785871505f71ecc1fStephen Smalley			sepath = strdup(mountpoint);
4376ece70806883534d29a74b9785871505f71ecc1fStephen Smalley		else
4386ece70806883534d29a74b9785871505f71ecc1fStephen Smalley			asprintf(&sepath, "/%s", mountpoint);
4396ece70806883534d29a74b9785871505f71ecc1fStephen Smalley		if (!sepath)
4406ece70806883534d29a74b9785871505f71ecc1fStephen Smalley			critical_error_errno("malloc");
441b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		if (selabel_lookup(sehnd, &secontext, sepath, S_IFDIR) < 0) {
442b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			error("cannot lookup security context for %s", sepath);
443b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		}
444b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		if (secontext) {
445b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			printf("Labeling %s as %s\n", sepath, secontext);
446b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley			inode_set_selinux(root_inode_num, secontext);
447b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		}
448b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		free(sepath);
449b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley		freecon(secontext);
450b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley	}
451b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#endif
452b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley
453ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	ext4_update_free();
454ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
455107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall	if (init_itabs)
456107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall		init_unused_inode_tables();
457107a9f161babc20daf915311146b0e864d3b4157Ken Sumrall
458b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross	ext4_queue_sb();
459b781330b1acae2e5706bbda8d81e5f7575f40e2aColin Cross
460ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
461ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
462ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			aux_info.sb->s_inodes_count,
463ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
464ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross			aux_info.sb->s_blocks_count_lo);
465ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
466dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross	if (wipe)
467dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross		wipe_block_device(fd, info.len);
468dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross
469dc5abeee1e6fc4827ee0d5ece12aaed2dd56f4c7Colin Cross	write_ext4_image(fd, gzip, sparse, crc);
470ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross
471f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross	sparse_file_destroy(info.sparse_file);
472f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross	info.sparse_file = NULL;
473f0ee37ffded79afdb03e15ae3a69969d2b7e6079Colin Cross
474ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross	return 0;
475ec0a2e83dc66d67addeb90e83144187691852a3eColin Cross}
476