Skip to content

Commit

Permalink
Merge branch 'libblkid/align' of https://github.com/t-8ch/util-linux
Browse files Browse the repository at this point in the history
* 'libblkid/align' of https://github.com/t-8ch/util-linux:
  libblkid: (probe) remove chunking from blkid_probe_get_idmag()
  libblkid: (probe) read data in chunks
  swapon: (tests) abort test on failing commands
  libblkid: reset errno before calling probefuncs
  libfdisk: reset errno before calling read()
  • Loading branch information
karelzak committed Oct 4, 2023
2 parents 6720eb3 + 51f9e09 commit 762898e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions libblkid/src/blkidP.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ struct blkid_struct_probe
int fd; /* device file descriptor */
uint64_t off; /* begin of data on the device */
uint64_t size; /* end of data on the device */
uint64_t io_size; /* optimal size of IO */

dev_t devno; /* device number (st.st_rdev) */
dev_t disk_devno; /* devno of the whole-disk or 0 */
Expand Down
1 change: 1 addition & 0 deletions libblkid/src/partitions/partitions.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ static int idinfo_probe(blkid_probe pr, const struct blkid_idinfo *id,
if (id->probefunc) {
DBG(LOWPROBE, ul_debug(
"%s: ---> call probefunc()", id->name));
errno = 0;
rc = id->probefunc(pr, mag);
blkid_probe_prune_buffers(pr);
if (rc < 0) {
Expand Down
51 changes: 39 additions & 12 deletions libblkid/src/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ blkid_probe blkid_clone_probe(blkid_probe parent)
pr->fd = parent->fd;
pr->off = parent->off;
pr->size = parent->size;
pr->io_size = parent->io_size;
pr->devno = parent->devno;
pr->disk_devno = parent->disk_devno;
pr->blkssz = parent->blkssz;
Expand Down Expand Up @@ -729,13 +730,21 @@ static int hide_buffer(blkid_probe pr, uint64_t off, uint64_t len)
const unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64_t len)
{
struct blkid_bufinfo *bf = NULL;
uint64_t real_off = pr->off + off;
uint64_t real_off, bias;

bias = off % pr->io_size;
off -= bias;
len += bias;
if (len % pr->io_size)
len += pr->io_size - (len % pr->io_size);

real_off = pr->off + off;

/*
DBG(BUFFER, ul_debug("\t>>>> off=%ju, real-off=%ju (probe <%ju..%ju>, len=%ju",
off, real_off, pr->off, pr->off + pr->size, len));
*/
if (pr->size == 0) {
if (pr->size == 0 || pr->io_size == 0) {
errno = EINVAL;
return NULL;
}
Expand Down Expand Up @@ -788,7 +797,7 @@ const unsigned char *blkid_probe_get_buffer(blkid_probe pr, uint64_t off, uint64
assert(bf->off + bf->len >= real_off + len);

errno = 0;
return real_off ? bf->data + (real_off - bf->off) : bf->data;
return real_off ? bf->data + (real_off - bf->off + bias) : bf->data + bias;
}

/**
Expand Down Expand Up @@ -953,6 +962,22 @@ static void cdrom_size_correction(blkid_probe pr, uint64_t last_written)

#endif

static uint64_t blkid_get_io_size(int fd)
{
static const int ioctls[] = { BLKIOOPT, BLKIOMIN, BLKBSZGET };
unsigned int s;
size_t i;
int r;

for (i = 0; i < ARRAY_SIZE(ioctls); i++) {
r = ioctl(fd, ioctls[i], &s);
if (r == 0 && is_power_of_2(s) && s >= DEFAULT_SECTOR_SIZE)
return min(s, 1U << 16);
}

return DEFAULT_SECTOR_SIZE;
}

/**
* blkid_probe_set_device:
* @pr: probe
Expand Down Expand Up @@ -996,6 +1021,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
pr->fd = fd;
pr->off = (uint64_t) off;
pr->size = 0;
pr->io_size = DEFAULT_SECTOR_SIZE;
pr->devno = 0;
pr->disk_devno = 0;
pr->mode = 0;
Expand Down Expand Up @@ -1159,8 +1185,11 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
}
# endif

DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64", zonesize=%"PRIu64,
pr->off, pr->size, pr->zone_size));
if (S_ISBLK(sb.st_mode) && !is_floppy && !blkid_probe_is_tiny(pr))
pr->io_size = blkid_get_io_size(fd);

DBG(LOWPROBE, ul_debug("ready for low-probing, offset=%"PRIu64", size=%"PRIu64", zonesize=%"PRIu64", iosize=%"PRIu64,
pr->off, pr->size, pr->zone_size, pr->io_size));
DBG(LOWPROBE, ul_debug("whole-disk: %s, regfile: %s",
blkid_probe_is_wholedisk(pr) ?"YES" : "NO",
S_ISREG(pr->mode) ? "YES" : "NO"));
Expand Down Expand Up @@ -1251,21 +1280,19 @@ int blkid_probe_get_idmag(blkid_probe pr, const struct blkid_idinfo *id,
kboff = ((mag->zonenum * pr->zone_size) >> 10) + mag->kboff_inzone;

if (kboff >= 0)
off = hint_offset + ((kboff + (mag->sboff >> 10)) << 10);
off = hint_offset + (kboff << 10) + mag->sboff;
else
off = pr->size - (-kboff << 10);
buf = blkid_probe_get_buffer(pr, off, 1024);
off = pr->size - (-kboff << 10) + mag->sboff;
buf = blkid_probe_get_buffer(pr, off, mag->len);

if (!buf && errno)
return -errno;

if (buf && !memcmp(mag->magic,
buf + (mag->sboff & 0x3ff), mag->len)) {

if (buf && !memcmp(mag->magic, buf, mag->len)) {
DBG(LOWPROBE, ul_debug("\tmagic sboff=%u, kboff=%ld",
mag->sboff, kboff));
if (offset)
*offset = off + (mag->sboff & 0x3ff);
*offset = off;
if (res)
*res = mag;
return BLKID_PROBE_OK;
Expand Down
1 change: 1 addition & 0 deletions libblkid/src/superblocks/superblocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ static int superblocks_probe(blkid_probe pr, struct blkid_chain *chn)
/* final check by probing function */
if (id->probefunc) {
DBG(LOWPROBE, ul_debug("\tcall probefunc()"));
errno = 0;
rc = id->probefunc(pr, mag);
blkid_probe_prune_buffers(pr);
if (rc != BLKID_PROBE_OK) {
Expand Down
1 change: 1 addition & 0 deletions libblkid/src/topology/topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static int topology_probe(blkid_probe pr, struct blkid_chain *chn)

if (id->probefunc) {
DBG(LOWPROBE, ul_debug("%s: call probefunc()", id->name));
errno = 0;
rc = id->probefunc(pr, NULL);
blkid_probe_prune_buffers(pr);
if (rc != 0)
Expand Down
4 changes: 2 additions & 2 deletions tests/ts/swapon/devname
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ $TS_CMD_MKSWAP $DEVICE > /dev/null 2>> $TS_OUTPUT \

ts_device_has "TYPE" "swap" $DEVICE || ts_die "Cannot find swap on $DEVICE"

$TS_CMD_SWAPON $DEVICE >> $TS_OUTPUT 2>> $TS_ERRLOG
$TS_CMD_SWAPON $DEVICE >> $TS_OUTPUT 2>> $TS_ERRLOG || ts_die "Swapon failed"

grep -q "^$DEVICE\b" /proc/swaps || ts_die "Cannot find $DEVICE in /proc/swaps"

$TS_CMD_SWAPOFF $DEVICE
$TS_CMD_SWAPOFF $DEVICE || ts_die "Swapoff failed"

# swapon/mkswap warns if system sets different permissions for loop devices
sed --in-place '/insecure permissions .*, 0660 suggested/d' $TS_OUTPUT
Expand Down

0 comments on commit 762898e

Please sign in to comment.