2015-12-10 00:18:44 +00:00
|
|
|
/**
|
|
|
|
* sload.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Huawei Ltd.
|
|
|
|
* Witten by:
|
|
|
|
* Hou Pengyang <houpengyang@huawei.com>
|
|
|
|
* Liu Shuoran <liushuoran@huawei.com>
|
|
|
|
* Jaegeuk Kim <jaegeuk@kernel.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
2021-12-15 02:45:47 +00:00
|
|
|
#ifndef _GNU_SOURCE
|
2015-12-10 00:18:44 +00:00
|
|
|
#define _GNU_SOURCE
|
2021-12-15 02:45:47 +00:00
|
|
|
#endif
|
2015-12-10 00:18:44 +00:00
|
|
|
#include "fsck.h"
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <dirent.h>
|
2017-11-14 18:53:32 +00:00
|
|
|
#ifdef HAVE_MNTENT_H
|
2015-12-10 00:18:44 +00:00
|
|
|
#include <mntent.h>
|
2017-11-14 18:53:32 +00:00
|
|
|
#endif
|
2016-10-22 20:06:54 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBSELINUX
|
2017-11-30 01:21:12 +00:00
|
|
|
static struct selabel_handle *sehnd = NULL;
|
2016-10-22 20:06:54 +00:00
|
|
|
#endif
|
2015-12-10 00:18:44 +00:00
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
typedef void (*fs_config_f)(const char *path, int dir,
|
|
|
|
const char *target_out_path,
|
|
|
|
unsigned *uid, unsigned *gid,
|
|
|
|
unsigned *mode, uint64_t *capabilities);
|
|
|
|
|
2022-04-21 22:18:22 +00:00
|
|
|
#ifndef _WIN32
|
2017-11-30 01:21:12 +00:00
|
|
|
static fs_config_f fs_config_func = NULL;
|
|
|
|
|
2022-04-21 22:18:24 +00:00
|
|
|
#ifdef HAVE_SELINUX_ANDROID_H
|
2017-11-30 01:21:12 +00:00
|
|
|
#include <selinux/android.h>
|
2015-12-10 00:18:44 +00:00
|
|
|
#include <private/android_filesystem_config.h>
|
2017-11-30 01:21:12 +00:00
|
|
|
#include <private/canned_fs_config.h>
|
2020-03-04 21:41:55 +00:00
|
|
|
#include <private/fs_config.h>
|
2015-12-10 00:18:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int filter_dot(const struct dirent *d)
|
|
|
|
{
|
|
|
|
return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
|
|
|
|
}
|
|
|
|
|
2019-08-05 01:44:59 +00:00
|
|
|
static int f2fs_make_directory(struct f2fs_sb_info *sbi,
|
2015-12-10 00:18:44 +00:00
|
|
|
int entries, struct dentry *de)
|
|
|
|
{
|
2019-08-05 01:44:59 +00:00
|
|
|
int ret = 0;
|
2015-12-10 00:18:44 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
if (de[i].file_type == F2FS_FT_DIR)
|
2019-08-05 01:44:59 +00:00
|
|
|
ret = f2fs_mkdir(sbi, de + i);
|
2015-12-10 00:18:44 +00:00
|
|
|
else if (de[i].file_type == F2FS_FT_REG_FILE)
|
2019-08-05 01:44:59 +00:00
|
|
|
ret = f2fs_create(sbi, de + i);
|
2015-12-10 00:18:44 +00:00
|
|
|
else if (de[i].file_type == F2FS_FT_SYMLINK)
|
2019-08-05 01:44:59 +00:00
|
|
|
ret = f2fs_symlink(sbi, de + i);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
break;
|
2015-12-10 00:18:44 +00:00
|
|
|
}
|
2019-08-05 01:44:59 +00:00
|
|
|
|
|
|
|
return ret;
|
2015-12-10 00:18:44 +00:00
|
|
|
}
|
2022-04-21 22:18:22 +00:00
|
|
|
#endif
|
2015-12-10 00:18:44 +00:00
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
#ifdef HAVE_LIBSELINUX
|
|
|
|
static int set_selinux_xattr(struct f2fs_sb_info *sbi, const char *path,
|
|
|
|
nid_t ino, int mode)
|
|
|
|
{
|
|
|
|
char *secontext = NULL;
|
|
|
|
char *mnt_path = NULL;
|
|
|
|
|
|
|
|
if (!sehnd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (asprintf(&mnt_path, "%s%s", c.mount_point, path) <= 0) {
|
|
|
|
ERR_MSG("cannot allocate security path for %s%s\n",
|
|
|
|
c.mount_point, path);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set root inode selinux context */
|
|
|
|
if (selabel_lookup(sehnd, &secontext, mnt_path, mode) < 0) {
|
|
|
|
ERR_MSG("cannot lookup security context for %s\n", mnt_path);
|
|
|
|
free(mnt_path);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secontext) {
|
|
|
|
MSG(2, "%s (%d) -> SELinux context = %s\n",
|
|
|
|
mnt_path, ino, secontext);
|
|
|
|
inode_set_selinux(sbi, ino, secontext);
|
|
|
|
}
|
|
|
|
freecon(secontext);
|
|
|
|
free(mnt_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define set_selinux_xattr(...) 0
|
|
|
|
#endif
|
|
|
|
|
2022-04-21 22:18:22 +00:00
|
|
|
#ifndef _WIN32
|
2017-11-30 01:21:12 +00:00
|
|
|
static int set_perms_and_caps(struct dentry *de)
|
|
|
|
{
|
|
|
|
uint64_t capabilities = 0;
|
|
|
|
unsigned int uid = 0, gid = 0, imode = 0;
|
|
|
|
char *mnt_path = NULL;
|
2020-08-17 05:01:30 +00:00
|
|
|
char *mount_path = c.mount_point;
|
2017-11-30 01:21:12 +00:00
|
|
|
|
2020-08-17 05:01:30 +00:00
|
|
|
/*
|
|
|
|
* de->path already has "/" in the beginning of it.
|
|
|
|
* Need to remove "/" when c.mount_point is "/", not to add it twice.
|
|
|
|
*/
|
|
|
|
if (strlen(c.mount_point) == 1 && c.mount_point[0] == '/')
|
|
|
|
mount_path = "";
|
|
|
|
|
|
|
|
if (asprintf(&mnt_path, "%s%s", mount_path, de->path) <= 0) {
|
2017-11-30 01:21:12 +00:00
|
|
|
ERR_MSG("cannot allocate mount path for %s%s\n",
|
2020-08-17 05:01:30 +00:00
|
|
|
mount_path, de->path);
|
2017-11-30 01:21:12 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Permissions */
|
|
|
|
if (fs_config_func != NULL) {
|
2018-05-07 18:54:14 +00:00
|
|
|
fs_config_func(mnt_path, de->file_type == F2FS_FT_DIR,
|
2017-11-30 01:21:12 +00:00
|
|
|
c.target_out_dir, &uid, &gid, &imode,
|
|
|
|
&capabilities);
|
|
|
|
de->uid = uid & 0xffff;
|
|
|
|
de->gid = gid & 0xffff;
|
|
|
|
de->mode = (de->mode & S_IFMT) | (imode & 0xffff);
|
|
|
|
de->capabilities = capabilities;
|
|
|
|
}
|
|
|
|
MSG(2, "%s -> mode = 0x%x, uid = 0x%x, gid = 0x%x, "
|
|
|
|
"capabilities = 0x%"PRIx64"\n",
|
|
|
|
mnt_path, de->mode, de->uid, de->gid, de->capabilities);
|
|
|
|
free(mnt_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_inode_metadata(struct dentry *de)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = lstat(de->full_path, &stat);
|
|
|
|
if (ret < 0) {
|
|
|
|
ERR_MSG("lstat failure\n");
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISREG(stat.st_mode)) {
|
2020-12-10 15:28:11 +00:00
|
|
|
if (stat.st_nlink > 1) {
|
|
|
|
/*
|
|
|
|
* This file might have multiple links to it, so remember
|
|
|
|
* device and inode.
|
|
|
|
*/
|
|
|
|
de->from_devino = stat.st_dev;
|
|
|
|
de->from_devino <<= 32;
|
|
|
|
de->from_devino |= stat.st_ino;
|
|
|
|
}
|
2017-11-30 01:21:12 +00:00
|
|
|
de->file_type = F2FS_FT_REG_FILE;
|
|
|
|
} else if (S_ISDIR(stat.st_mode)) {
|
|
|
|
de->file_type = F2FS_FT_DIR;
|
|
|
|
} else if (S_ISCHR(stat.st_mode)) {
|
|
|
|
de->file_type = F2FS_FT_CHRDEV;
|
|
|
|
} else if (S_ISBLK(stat.st_mode)) {
|
|
|
|
de->file_type = F2FS_FT_BLKDEV;
|
|
|
|
} else if (S_ISFIFO(stat.st_mode)) {
|
|
|
|
de->file_type = F2FS_FT_FIFO;
|
|
|
|
} else if (S_ISSOCK(stat.st_mode)) {
|
|
|
|
de->file_type = F2FS_FT_SOCK;
|
|
|
|
} else if (S_ISLNK(stat.st_mode)) {
|
|
|
|
de->file_type = F2FS_FT_SYMLINK;
|
|
|
|
de->link = calloc(F2FS_BLKSIZE, 1);
|
|
|
|
ASSERT(de->link);
|
|
|
|
ret = readlink(de->full_path, de->link, F2FS_BLKSIZE - 1);
|
|
|
|
ASSERT(ret >= 0);
|
|
|
|
} else {
|
|
|
|
ERR_MSG("unknown file type on %s", de->path);
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
de->size = stat.st_size;
|
|
|
|
de->mode = stat.st_mode &
|
2018-05-03 11:25:06 +00:00
|
|
|
(S_IFMT|S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
|
2017-11-30 01:21:12 +00:00
|
|
|
if (c.fixed_time == -1 && c.from_dir)
|
|
|
|
de->mtime = stat.st_mtime;
|
|
|
|
else
|
|
|
|
de->mtime = c.fixed_time;
|
|
|
|
|
2021-03-15 22:08:53 +00:00
|
|
|
if (c.preserve_perms) {
|
|
|
|
de->uid = stat.st_uid;
|
|
|
|
de->gid = stat.st_gid;
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
set_perms_and_caps(de);
|
|
|
|
}
|
|
|
|
|
2015-12-10 00:18:44 +00:00
|
|
|
static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
|
|
|
|
const char *dir_path, const char *target_out_dir,
|
2017-11-30 01:21:12 +00:00
|
|
|
nid_t dir_ino)
|
2015-12-10 00:18:44 +00:00
|
|
|
{
|
|
|
|
int entries = 0;
|
|
|
|
struct dentry *dentries;
|
|
|
|
struct dirent **namelist = NULL;
|
2019-08-05 01:44:59 +00:00
|
|
|
int i = 0, ret = 0;
|
2015-12-10 00:18:44 +00:00
|
|
|
|
|
|
|
entries = scandir(full_path, &namelist, filter_dot, (void *)alphasort);
|
|
|
|
if (entries < 0) {
|
|
|
|
ERR_MSG("No entries in %s\n", full_path);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
dentries = calloc(entries, sizeof(struct dentry));
|
2019-08-05 01:44:59 +00:00
|
|
|
ASSERT(dentries);
|
2015-12-10 00:18:44 +00:00
|
|
|
|
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
dentries[i].name = (unsigned char *)strdup(namelist[i]->d_name);
|
|
|
|
if (dentries[i].name == NULL) {
|
|
|
|
ERR_MSG("Skip: ENOMEM\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dentries[i].len = strlen((char *)dentries[i].name);
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
ret = asprintf(&dentries[i].path, "%s%s",
|
2015-12-10 00:18:44 +00:00
|
|
|
dir_path, namelist[i]->d_name);
|
|
|
|
ASSERT(ret > 0);
|
|
|
|
ret = asprintf(&dentries[i].full_path, "%s/%s",
|
|
|
|
full_path, namelist[i]->d_name);
|
|
|
|
ASSERT(ret > 0);
|
|
|
|
free(namelist[i]);
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
set_inode_metadata(dentries + i);
|
2015-12-10 00:18:44 +00:00
|
|
|
|
|
|
|
dentries[i].pino = dir_ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(namelist);
|
|
|
|
|
2019-08-05 01:44:59 +00:00
|
|
|
ret = f2fs_make_directory(sbi, entries, dentries);
|
|
|
|
if (ret)
|
|
|
|
goto out_free;
|
2015-12-10 00:18:44 +00:00
|
|
|
|
|
|
|
for (i = 0; i < entries; i++) {
|
|
|
|
if (dentries[i].file_type == F2FS_FT_REG_FILE) {
|
|
|
|
f2fs_build_file(sbi, dentries + i);
|
|
|
|
} else if (dentries[i].file_type == F2FS_FT_DIR) {
|
|
|
|
char *subdir_full_path = NULL;
|
2017-11-30 01:21:12 +00:00
|
|
|
char *subdir_dir_path = NULL;
|
2015-12-10 00:18:44 +00:00
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
ret = asprintf(&subdir_full_path, "%s",
|
2015-12-10 00:18:44 +00:00
|
|
|
dentries[i].full_path);
|
|
|
|
ASSERT(ret > 0);
|
|
|
|
ret = asprintf(&subdir_dir_path, "%s/",
|
|
|
|
dentries[i].path);
|
|
|
|
ASSERT(ret > 0);
|
|
|
|
|
2019-08-05 01:44:59 +00:00
|
|
|
ret = build_directory(sbi, subdir_full_path,
|
|
|
|
subdir_dir_path,
|
|
|
|
target_out_dir,
|
|
|
|
dentries[i].ino);
|
2015-12-10 00:18:44 +00:00
|
|
|
free(subdir_full_path);
|
|
|
|
free(subdir_dir_path);
|
2019-08-05 01:44:59 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto out_free;
|
2015-12-10 00:18:44 +00:00
|
|
|
} else if (dentries[i].file_type == F2FS_FT_SYMLINK) {
|
|
|
|
/*
|
|
|
|
* It is already done in f2fs_make_directory
|
|
|
|
* f2fs_make_symlink(sbi, dir_ino, &dentries[i]);
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
MSG(1, "Error unknown file type\n");
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
ret = set_selinux_xattr(sbi, dentries[i].path,
|
|
|
|
dentries[i].ino, dentries[i].mode);
|
|
|
|
if (ret)
|
2019-08-05 01:44:57 +00:00
|
|
|
goto out_free;
|
2016-10-22 20:06:54 +00:00
|
|
|
|
2015-12-10 00:18:44 +00:00
|
|
|
free(dentries[i].path);
|
|
|
|
free(dentries[i].full_path);
|
|
|
|
free((void *)dentries[i].name);
|
|
|
|
}
|
2019-08-05 01:44:57 +00:00
|
|
|
out_free:
|
|
|
|
for (; i < entries; i++) {
|
|
|
|
free(dentries[i].path);
|
|
|
|
free(dentries[i].full_path);
|
|
|
|
free((void *)dentries[i].name);
|
|
|
|
}
|
2015-12-10 00:18:44 +00:00
|
|
|
|
|
|
|
free(dentries);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-04-21 22:18:22 +00:00
|
|
|
#else
|
|
|
|
static int build_directory(struct f2fs_sb_info *sbi, const char *full_path,
|
|
|
|
const char *dir_path, const char *target_out_dir,
|
|
|
|
nid_t dir_ino)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-10 00:18:44 +00:00
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
static int configure_files(void)
|
2015-12-10 00:18:44 +00:00
|
|
|
{
|
2017-11-30 01:21:12 +00:00
|
|
|
#ifdef HAVE_LIBSELINUX
|
|
|
|
if (!c.nr_opt)
|
|
|
|
goto skip;
|
|
|
|
#if !defined(__ANDROID__)
|
|
|
|
sehnd = selabel_open(SELABEL_CTX_FILE, c.seopt_file, c.nr_opt);
|
|
|
|
if (!sehnd) {
|
|
|
|
ERR_MSG("Failed to open file contexts \"%s\"",
|
|
|
|
c.seopt_file[0].value);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
sehnd = selinux_android_file_context_handle();
|
|
|
|
if (!sehnd) {
|
2022-08-26 22:45:34 +00:00
|
|
|
ERR_MSG("Failed to get android file_contexts\n");
|
2017-11-30 01:21:12 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
skip:
|
|
|
|
#endif
|
2022-04-21 22:18:24 +00:00
|
|
|
#ifdef HAVE_SELINUX_ANDROID_H
|
2017-11-30 01:21:12 +00:00
|
|
|
/* Load the FS config */
|
|
|
|
if (c.fs_config_file) {
|
|
|
|
int ret = load_canned_fs_config(c.fs_config_file);
|
2015-12-10 00:18:44 +00:00
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
ERR_MSG("Failed to load fs_config \"%s\"",
|
|
|
|
c.fs_config_file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
fs_config_func = canned_fs_config;
|
|
|
|
} else {
|
|
|
|
fs_config_func = fs_config;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-10 00:18:44 +00:00
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
int f2fs_sload(struct f2fs_sb_info *sbi)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-06-30 21:41:38 +00:00
|
|
|
/* this requires for the below sanity checks */
|
|
|
|
fsck_init(sbi);
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
ret = configure_files();
|
2015-12-10 00:18:44 +00:00
|
|
|
if (ret) {
|
2017-11-30 01:21:12 +00:00
|
|
|
ERR_MSG("Failed to configure files\n");
|
2015-12-10 00:18:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
/* flush NAT/SIT journal entries */
|
|
|
|
flush_journal_entries(sbi);
|
|
|
|
|
2020-12-10 15:28:11 +00:00
|
|
|
/* initialize empty hardlink cache */
|
|
|
|
sbi->hardlink_cache = 0;
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
ret = build_directory(sbi, c.from_dir, "/",
|
|
|
|
c.target_out_dir, F2FS_ROOT_INO(sbi));
|
2015-12-10 00:18:44 +00:00
|
|
|
if (ret) {
|
|
|
|
ERR_MSG("Failed to build due to %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-30 01:21:12 +00:00
|
|
|
ret = set_selinux_xattr(sbi, c.mount_point,
|
|
|
|
F2FS_ROOT_INO(sbi), S_IFDIR);
|
|
|
|
if (ret) {
|
|
|
|
ERR_MSG("Failed to set selinux for root: %d\n", ret);
|
|
|
|
return ret;
|
2015-12-10 00:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update curseg info; can update sit->types */
|
2018-05-09 02:48:33 +00:00
|
|
|
move_curseg_info(sbi, SM_I(sbi)->main_blkaddr, 0);
|
2015-12-10 00:18:44 +00:00
|
|
|
zero_journal_entries(sbi);
|
|
|
|
write_curseg_info(sbi);
|
|
|
|
|
|
|
|
/* flush dirty sit entries */
|
|
|
|
flush_sit_entries(sbi);
|
|
|
|
|
|
|
|
write_checkpoint(sbi);
|
|
|
|
return 0;
|
|
|
|
}
|