Skip to content

Commit 53c0123

Browse files
committed
Merge remote-tracking branch 'remotes/stefanha-gitlab/tags/block-pull-request' into staging
Pull request # gpg: Signature made Thu 08 Jul 2021 14:11:37 BST # gpg: using RSA key 8695A8BFD3F97CDAAC35775A9CA4ABB381AB73C8 # gpg: Good signature from "Stefan Hajnoczi <[email protected]>" [full] # gpg: aka "Stefan Hajnoczi <[email protected]>" [full] # Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35 775A 9CA4 ABB3 81AB 73C8 * remotes/stefanha-gitlab/tags/block-pull-request: block/io: Merge discard request alignments block: Add backend_defaults property block/file-posix: Optimize for macOS util/async: print leaked BH name when AioContext finalizes util/async: add a human-readable name to BHs for debugging Signed-off-by: Peter Maydell <[email protected]>
2 parents 9db3065 + 9f460c6 commit 53c0123

File tree

10 files changed

+161
-17
lines changed

10 files changed

+161
-17
lines changed

block/file-posix.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#if defined(HAVE_HOST_BLOCK_DEVICE)
4747
#include <paths.h>
4848
#include <sys/param.h>
49+
#include <sys/mount.h>
4950
#include <IOKit/IOKitLib.h>
5051
#include <IOKit/IOBSD.h>
5152
#include <IOKit/storage/IOMediaBSDClient.h>
@@ -1254,6 +1255,15 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
12541255
return;
12551256
}
12561257

1258+
#if defined(__APPLE__) && (__MACH__)
1259+
struct statfs buf;
1260+
1261+
if (!fstatfs(s->fd, &buf)) {
1262+
bs->bl.opt_transfer = buf.f_iosize;
1263+
bs->bl.pdiscard_alignment = buf.f_bsize;
1264+
}
1265+
#endif
1266+
12571267
if (bs->sg || S_ISBLK(st.st_mode)) {
12581268
int ret = hdev_get_max_hw_transfer(s->fd, &st);
12591269

@@ -1591,6 +1601,7 @@ static int handle_aiocb_rw(void *opaque)
15911601
}
15921602
}
15931603

1604+
#if defined(CONFIG_FALLOCATE) || defined(BLKZEROOUT) || defined(BLKDISCARD)
15941605
static int translate_err(int err)
15951606
{
15961607
if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP ||
@@ -1599,6 +1610,7 @@ static int translate_err(int err)
15991610
}
16001611
return err;
16011612
}
1613+
#endif
16021614

16031615
#ifdef CONFIG_FALLOCATE
16041616
static int do_fallocate(int fd, int mode, off_t offset, off_t len)
@@ -1811,16 +1823,27 @@ static int handle_aiocb_discard(void *opaque)
18111823
}
18121824
} while (errno == EINTR);
18131825

1814-
ret = -errno;
1826+
ret = translate_err(-errno);
18151827
#endif
18161828
} else {
18171829
#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
18181830
ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
18191831
aiocb->aio_offset, aiocb->aio_nbytes);
1832+
ret = translate_err(-errno);
1833+
#elif defined(__APPLE__) && (__MACH__)
1834+
fpunchhole_t fpunchhole;
1835+
fpunchhole.fp_flags = 0;
1836+
fpunchhole.reserved = 0;
1837+
fpunchhole.fp_offset = aiocb->aio_offset;
1838+
fpunchhole.fp_length = aiocb->aio_nbytes;
1839+
if (fcntl(s->fd, F_PUNCHHOLE, &fpunchhole) == -1) {
1840+
ret = errno == ENODEV ? -ENOTSUP : -errno;
1841+
} else {
1842+
ret = 0;
1843+
}
18201844
#endif
18211845
}
18221846

1823-
ret = translate_err(ret);
18241847
if (ret == -ENOTSUP) {
18251848
s->has_discard = false;
18261849
}

block/io.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll)
125125

126126
static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
127127
{
128+
dst->pdiscard_alignment = MAX(dst->pdiscard_alignment,
129+
src->pdiscard_alignment);
128130
dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
129131
dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
130132
dst->max_hw_transfer = MIN_NON_ZERO(dst->max_hw_transfer,

hw/block/block.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,58 @@ bool blkconf_blocksizes(BlockConf *conf, Error **errp)
6565
{
6666
BlockBackend *blk = conf->blk;
6767
BlockSizes blocksizes;
68-
int backend_ret;
68+
BlockDriverState *bs;
69+
bool use_blocksizes;
70+
bool use_bs;
71+
72+
switch (conf->backend_defaults) {
73+
case ON_OFF_AUTO_AUTO:
74+
use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes);
75+
use_bs = false;
76+
break;
77+
78+
case ON_OFF_AUTO_ON:
79+
use_blocksizes = !blk_probe_blocksizes(blk, &blocksizes);
80+
bs = blk_bs(blk);
81+
use_bs = bs;
82+
break;
83+
84+
case ON_OFF_AUTO_OFF:
85+
use_blocksizes = false;
86+
use_bs = false;
87+
break;
88+
89+
default:
90+
abort();
91+
}
6992

70-
backend_ret = blk_probe_blocksizes(blk, &blocksizes);
7193
/* fill in detected values if they are not defined via qemu command line */
7294
if (!conf->physical_block_size) {
73-
if (!backend_ret) {
95+
if (use_blocksizes) {
7496
conf->physical_block_size = blocksizes.phys;
7597
} else {
7698
conf->physical_block_size = BDRV_SECTOR_SIZE;
7799
}
78100
}
79101
if (!conf->logical_block_size) {
80-
if (!backend_ret) {
102+
if (use_blocksizes) {
81103
conf->logical_block_size = blocksizes.log;
82104
} else {
83105
conf->logical_block_size = BDRV_SECTOR_SIZE;
84106
}
85107
}
108+
if (use_bs) {
109+
if (!conf->opt_io_size) {
110+
conf->opt_io_size = bs->bl.opt_transfer;
111+
}
112+
if (conf->discard_granularity == -1) {
113+
if (bs->bl.pdiscard_alignment) {
114+
conf->discard_granularity = bs->bl.pdiscard_alignment;
115+
} else if (bs->bl.request_alignment != 1) {
116+
conf->discard_granularity = bs->bl.request_alignment;
117+
}
118+
}
119+
}
86120

87121
if (conf->logical_block_size > conf->physical_block_size) {
88122
error_setg(errp,

include/block/aio.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,45 @@ void aio_context_acquire(AioContext *ctx);
291291
/* Relinquish ownership of the AioContext. */
292292
void aio_context_release(AioContext *ctx);
293293

294+
/**
295+
* aio_bh_schedule_oneshot_full: Allocate a new bottom half structure that will
296+
* run only once and as soon as possible.
297+
*
298+
* @name: A human-readable identifier for debugging purposes.
299+
*/
300+
void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
301+
const char *name);
302+
294303
/**
295304
* aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
296305
* only once and as soon as possible.
306+
*
307+
* A convenience wrapper for aio_bh_schedule_oneshot_full() that uses cb as the
308+
* name string.
297309
*/
298-
void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
310+
#define aio_bh_schedule_oneshot(ctx, cb, opaque) \
311+
aio_bh_schedule_oneshot_full((ctx), (cb), (opaque), (stringify(cb)))
299312

300313
/**
301-
* aio_bh_new: Allocate a new bottom half structure.
314+
* aio_bh_new_full: Allocate a new bottom half structure.
302315
*
303316
* Bottom halves are lightweight callbacks whose invocation is guaranteed
304317
* to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
305318
* is opaque and must be allocated prior to its use.
319+
*
320+
* @name: A human-readable identifier for debugging purposes.
321+
*/
322+
QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
323+
const char *name);
324+
325+
/**
326+
* aio_bh_new: Allocate a new bottom half structure
327+
*
328+
* A convenience wrapper for aio_bh_new_full() that uses the cb as the name
329+
* string.
306330
*/
307-
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
331+
#define aio_bh_new(ctx, cb, opaque) \
332+
aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)))
308333

309334
/**
310335
* aio_notify: Force processing of pending events.

include/hw/block/block.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
typedef struct BlockConf {
2121
BlockBackend *blk;
22+
OnOffAuto backend_defaults;
2223
uint32_t physical_block_size;
2324
uint32_t logical_block_size;
2425
uint32_t min_io_size;
@@ -48,6 +49,8 @@ static inline unsigned int get_physical_block_exp(BlockConf *conf)
4849
}
4950

5051
#define DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf) \
52+
DEFINE_PROP_ON_OFF_AUTO("backend_defaults", _state, \
53+
_conf.backend_defaults, ON_OFF_AUTO_AUTO), \
5154
DEFINE_PROP_BLOCKSIZE("logical_block_size", _state, \
5255
_conf.logical_block_size), \
5356
DEFINE_PROP_BLOCKSIZE("physical_block_size", _state, \

include/qemu/main-loop.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);
294294

295295
void qemu_fd_register(int fd);
296296

297-
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
297+
#define qemu_bh_new(cb, opaque) \
298+
qemu_bh_new_full((cb), (opaque), (stringify(cb)))
299+
QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name);
298300
void qemu_bh_schedule_idle(QEMUBH *bh);
299301

300302
enum {

0 commit comments

Comments
 (0)