Files
s390-tools/zipl/src/fs-map.c
Eduard Shishkin c39722aff2 zipl/src: Implement '--dry-run' option for ngdumps
Use the directory specified by the shell environment variable TMPDIR
for temporary objects creation and ngdump job simulation. If TMPDIR is
not set, then use "/tmp" for the mentioned purposes;

While running ngdump job in 'dry-run' mode:
. Don't format/mount the target dump device. Instead, create the
  bootmap file and the meta-file at the temporary mount point without
  mounting anything to it. Thus, the mentioned files to be acrually
  created in the "proxy" file system owning the temporary mount point;
. Retrieve base disk info from the read-only dump device and
  complete that info with the block size of the proxy file system;

Separate the steps on retrieving/setting file system block size
into a dedicated procedure;

Use definitions instead of hardcoded file names;

Fix a bug in an error path (accessing freed memory);

Make misc_open_simulate() and misc_open_exclusive() static;

Update man pages with the requirements on the system environment
(resources) for ngdump job being executed in dry-run mode;

Provide hints for user (in stderr) in case when ngdump job in dry-run
mode failed due to inappropriate system environment.

Tested-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Reviewed-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2025-04-07 17:42:18 +02:00

128 lines
2.8 KiB
C

/*
* zipl - zSeries Initial Program Loader tool
*
* Functions to map logical addresses to physical ones
*
* Copyright IBM Corp. 2001, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <unistd.h>
#include <linux/fs.h>
#include <linux/fiemap.h>
#include "disk.h"
#include "error.h"
#include "misc.h"
#ifndef REISERFS_SUPER_MAGIC
#define REISERFS_SUPER_MAGIC 0x52654973
#endif /* not REISERFS_SUPER_MAGIC */
#ifndef REISERFS_IOC_UNPACK
#define REISERFS_IOC_UNPACK _IOW(0xCD, 1, long)
#endif /* not REISERFS_IOC_UNPACK */
/**
* Filesystem-specific hook.
* Do whatever needed before mapping logical address to physical one
*/
static int fs_premap_hook(int fd)
{
struct statfs buf;
/* Get file system type */
if (fstatfs(fd, &buf)) {
error_reason(strerror(errno));
return -1;
}
switch (buf.f_type) {
case REISERFS_SUPER_MAGIC:
/* Files on ReiserFS need unpacking */
if (ioctl(fd, REISERFS_IOC_UNPACK, 1)) {
error_reason("Could not unpack ReiserFS file");
return -1;
}
break;
default:
break;
}
return 0;
}
/**
* Find a disk address for a file system block,
* where a specified byte of the file body is located
*
* FD: file descriptor
* OFFSET: offset of the byte in the file body
* MAPPED: resulted disk address
* FS_BLOCK_SIZE : file system block size in bytes
*/
int fs_map(int fd, uint64_t offset, blocknum_t *mapped,
int fs_block_size)
{
struct fiemap *fiemap;
int fiemap_size;
int map_offset;
int block;
if (fs_premap_hook(fd))
return -1;
/*
* First try FIEMAP, more complicated to set up
*/
fiemap_size = sizeof(struct fiemap) + sizeof(struct fiemap_extent);
fiemap = misc_malloc(fiemap_size);
if (!fiemap)
return -1;
memset(fiemap, 0, fiemap_size);
fiemap->fm_extent_count = 1;
fiemap->fm_flags = FIEMAP_FLAG_SYNC;
fiemap->fm_start = offset;
fiemap->fm_length = fs_block_size;
if (ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap)) {
/* FIEMAP failed, fall back to FIBMAP */
block = offset / fs_block_size;
if (ioctl(fd, FIBMAP, &block)) {
error_reason(FS_MAP_ERROR);
free(fiemap);
return -1;
}
*mapped = block;
} else {
if (fiemap->fm_mapped_extents) {
if (fiemap->fm_extents[0].fe_flags &
FIEMAP_EXTENT_ENCODED) {
error_reason("File mapping is encoded");
free(fiemap);
return -1;
}
/*
* returned extent may start prior to our request
*/
map_offset = fiemap->fm_start -
fiemap->fm_extents[0].fe_logical;
*mapped = fiemap->fm_extents[0].fe_physical +
map_offset;
/* set mapped to fs block units */
*mapped = *mapped / fs_block_size;
} else {
*mapped = 0;
}
}
free(fiemap);
return 0;
}