Skip to content

Commit 6d0eb64

Browse files
committed
block: Add permissions to blk_new()
We want every user to be specific about the permissions it needs, so we'll pass the initial permissions as parameters to blk_new(). A user only needs to call blk_set_perm() if it wants to change the permissions after the fact. The permissions are stored in the BlockBackend and applied whenever a BlockDriverState should be attached in blk_insert_bs(). This does not include actually choosing the right set of permissions everywhere yet. Instead, the usual FIXME comment is added to each place and will be addressed in individual patches. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Acked-by: Fam Zheng <famz@redhat.com>
1 parent 981776b commit 6d0eb64

File tree

18 files changed

+53
-30
lines changed

18 files changed

+53
-30
lines changed

block.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,
21932193
goto fail;
21942194
}
21952195
if (file_bs != NULL) {
2196-
file = blk_new();
2196+
file = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
21972197
blk_insert_bs(file, file_bs);
21982198
bdrv_unref(file_bs);
21992199

block/backup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,8 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
624624
goto error;
625625
}
626626

627-
job->target = blk_new();
627+
/* FIXME Use real permissions */
628+
job->target = blk_new(0, BLK_PERM_ALL);
628629
blk_insert_bs(job->target, target);
629630

630631
job->on_source_error = on_source_error;

block/block-backend.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,23 @@ static const BdrvChildRole child_root = {
120120

121121
/*
122122
* Create a new BlockBackend with a reference count of one.
123-
* Store an error through @errp on failure, unless it's null.
123+
*
124+
* @perm is a bitmasks of BLK_PERM_* constants which describes the permissions
125+
* to request for a block driver node that is attached to this BlockBackend.
126+
* @shared_perm is a bitmask which describes which permissions may be granted
127+
* to other users of the attached node.
128+
* Both sets of permissions can be changed later using blk_set_perm().
129+
*
124130
* Return the new BlockBackend on success, null on failure.
125131
*/
126-
BlockBackend *blk_new(void)
132+
BlockBackend *blk_new(uint64_t perm, uint64_t shared_perm)
127133
{
128134
BlockBackend *blk;
129135

130136
blk = g_new0(BlockBackend, 1);
131137
blk->refcnt = 1;
132-
blk->perm = 0;
133-
blk->shared_perm = BLK_PERM_ALL;
138+
blk->perm = perm;
139+
blk->shared_perm = shared_perm;
134140
blk_set_enable_write_cache(blk, true);
135141

136142
qemu_co_queue_init(&blk->public.throttled_reqs[0]);
@@ -161,7 +167,7 @@ BlockBackend *blk_new_open(const char *filename, const char *reference,
161167
BlockBackend *blk;
162168
BlockDriverState *bs;
163169

164-
blk = blk_new();
170+
blk = blk_new(0, BLK_PERM_ALL);
165171
bs = bdrv_open(filename, reference, options, flags, errp);
166172
if (!bs) {
167173
blk_unref(blk);
@@ -505,9 +511,10 @@ void blk_remove_bs(BlockBackend *blk)
505511
void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs)
506512
{
507513
bdrv_ref(bs);
508-
/* FIXME Use real permissions */
514+
/* FIXME Error handling */
509515
blk->root = bdrv_root_attach_child(bs, "root", &child_root,
510-
0, BLK_PERM_ALL, blk, &error_abort);
516+
blk->perm, blk->shared_perm, blk,
517+
&error_abort);
511518

512519
notifier_list_notify(&blk->insert_bs_notifiers, blk);
513520
if (blk->public.throttle_state) {

block/commit.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,12 @@ void commit_start(const char *job_id, BlockDriverState *bs,
275275
block_job_add_bdrv(&s->common, overlay_bs);
276276
}
277277

278-
s->base = blk_new();
278+
/* FIXME Use real permissions */
279+
s->base = blk_new(0, BLK_PERM_ALL);
279280
blk_insert_bs(s->base, base);
280281

281-
s->top = blk_new();
282+
/* FIXME Use real permissions */
283+
s->top = blk_new(0, BLK_PERM_ALL);
282284
blk_insert_bs(s->top, top);
283285

284286
s->active = bs;
@@ -328,10 +330,12 @@ int bdrv_commit(BlockDriverState *bs)
328330
}
329331
}
330332

331-
src = blk_new();
333+
/* FIXME Use real permissions */
334+
src = blk_new(0, BLK_PERM_ALL);
332335
blk_insert_bs(src, bs);
333336

334-
backing = blk_new();
337+
/* FIXME Use real permissions */
338+
backing = blk_new(0, BLK_PERM_ALL);
335339
blk_insert_bs(backing, bs->backing->bs);
336340

337341
length = blk_getlength(src);

block/mirror.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,8 @@ static void mirror_start_job(const char *job_id, BlockDriverState *bs,
10171017
return;
10181018
}
10191019

1020-
s->target = blk_new();
1020+
/* FIXME Use real permissions */
1021+
s->target = blk_new(0, BLK_PERM_ALL);
10211022
blk_insert_bs(s->target, target);
10221023

10231024
s->replaces = g_strdup(replaces);

block/qcow2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3262,7 +3262,7 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
32623262
}
32633263

32643264
if (new_size) {
3265-
BlockBackend *blk = blk_new();
3265+
BlockBackend *blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
32663266
blk_insert_bs(blk, bs);
32673267
ret = blk_truncate(blk, new_size);
32683268
blk_unref(blk);

blockdev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
558558
if ((!file || !*file) && !qdict_size(bs_opts)) {
559559
BlockBackendRootState *blk_rs;
560560

561-
blk = blk_new();
561+
blk = blk_new(0, BLK_PERM_ALL);
562562
blk_rs = blk_get_root_state(blk);
563563
blk_rs->open_flags = bdrv_flags;
564564
blk_rs->read_only = read_only;
@@ -2890,7 +2890,7 @@ void qmp_block_resize(bool has_device, const char *device,
28902890
goto out;
28912891
}
28922892

2893-
blk = blk_new();
2893+
blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
28942894
blk_insert_bs(blk, bs);
28952895

28962896
/* complete all in-flight operations before resizing the device */

blockjob.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
159159
}
160160
}
161161

162-
blk = blk_new();
162+
/* FIXME Use real permissions */
163+
blk = blk_new(0, BLK_PERM_ALL);
163164
blk_insert_bs(blk, bs);
164165

165166
job = g_malloc0(driver->instance_size);

hmp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,8 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict)
20502050
if (!blk) {
20512051
BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
20522052
if (bs) {
2053-
blk = local_blk = blk_new();
2053+
/* FIXME Use real permissions */
2054+
blk = local_blk = blk_new(0, BLK_PERM_ALL);
20542055
blk_insert_bs(blk, bs);
20552056
} else {
20562057
goto fail;

hw/block/fdc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ static int floppy_drive_init(DeviceState *qdev)
533533

534534
if (!dev->conf.blk) {
535535
/* Anonymous BlockBackend for an empty drive */
536-
dev->conf.blk = blk_new();
536+
/* FIXME Use real permissions */
537+
dev->conf.blk = blk_new(0, BLK_PERM_ALL);
537538
ret = blk_attach_dev(dev->conf.blk, qdev);
538539
assert(ret == 0);
539540
}

0 commit comments

Comments
 (0)