Skip to content

Commit 805f11a

Browse files
um: ubd: Add REQ_FLUSH suppport
UML's block device driver does not support write barriers, to support this this patch adds REQ_FLUSH suppport. Every time the block layer sends a REQ_FLUSH we fsync() now our backing file to guarantee data consistency. Reported-and-tested-by: Richard W.M. Jones <rjones@redhat.com> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent f75b1b1 commit 805f11a

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎arch/um/drivers/ubd_kern.c‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <os.h>
4242
#include "cow.h"
4343

44-
enum ubd_req { UBD_READ, UBD_WRITE };
44+
enum ubd_req { UBD_READ, UBD_WRITE, UBD_FLUSH };
4545

4646
struct io_thread_req {
4747
struct request *req;
@@ -866,6 +866,7 @@ static int ubd_add(int n, char **error_out)
866866
goto out;
867867
}
868868
ubd_dev->queue->queuedata = ubd_dev;
869+
blk_queue_flush(ubd_dev->queue, REQ_FLUSH);
869870

870871
blk_queue_max_segments(ubd_dev->queue, MAX_SG);
871872
err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
@@ -1238,6 +1239,19 @@ static void prepare_request(struct request *req, struct io_thread_req *io_req,
12381239

12391240
}
12401241

1242+
/* Called with dev->lock held */
1243+
static void prepare_flush_request(struct request *req,
1244+
struct io_thread_req *io_req)
1245+
{
1246+
struct gendisk *disk = req->rq_disk;
1247+
struct ubd *ubd_dev = disk->private_data;
1248+
1249+
io_req->req = req;
1250+
io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
1251+
ubd_dev->fd;
1252+
io_req->op = UBD_FLUSH;
1253+
}
1254+
12411255
/* Called with dev->lock held */
12421256
static void do_ubd_request(struct request_queue *q)
12431257
{
@@ -1259,6 +1273,20 @@ static void do_ubd_request(struct request_queue *q)
12591273
}
12601274

12611275
req = dev->request;
1276+
1277+
if (req->cmd_flags & REQ_FLUSH) {
1278+
io_req = kmalloc(sizeof(struct io_thread_req),
1279+
GFP_ATOMIC);
1280+
if (io_req == NULL) {
1281+
if (list_empty(&dev->restart))
1282+
list_add(&dev->restart, &restart);
1283+
return;
1284+
}
1285+
prepare_flush_request(req, io_req);
1286+
os_write_file(thread_fd, &io_req,
1287+
sizeof(struct io_thread_req *));
1288+
}
1289+
12621290
while(dev->start_sg < dev->end_sg){
12631291
struct scatterlist *sg = &dev->sg[dev->start_sg];
12641292

@@ -1367,6 +1395,17 @@ static void do_io(struct io_thread_req *req)
13671395
int err;
13681396
__u64 off;
13691397

1398+
if (req->op == UBD_FLUSH) {
1399+
/* fds[0] is always either the rw image or our cow file */
1400+
n = os_sync_file(req->fds[0]);
1401+
if (n != 0) {
1402+
printk("do_io - sync failed err = %d "
1403+
"fd = %d\n", -n, req->fds[0]);
1404+
req->error = 1;
1405+
}
1406+
return;
1407+
}
1408+
13701409
nsectors = req->length / req->sectorsize;
13711410
start = 0;
13721411
do {

‎arch/um/include/shared/os.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ extern int os_seek_file(int fd, unsigned long long offset);
141141
extern int os_open_file(const char *file, struct openflags flags, int mode);
142142
extern int os_read_file(int fd, void *buf, int len);
143143
extern int os_write_file(int fd, const void *buf, int count);
144+
extern int os_sync_file(int fd);
144145
extern int os_file_size(const char *file, unsigned long long *size_out);
145146
extern int os_file_modtime(const char *file, unsigned long *modtime);
146147
extern int os_pipe(int *fd, int stream, int close_on_exec);

‎arch/um/os-Linux/file.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ int os_write_file(int fd, const void *buf, int len)
266266
return n;
267267
}
268268

269+
int os_sync_file(int fd)
270+
{
271+
int n = fsync(fd);
272+
273+
if (n < 0)
274+
return -errno;
275+
return n;
276+
}
277+
269278
int os_file_size(const char *file, unsigned long long *size_out)
270279
{
271280
struct uml_stat buf;

0 commit comments

Comments
 (0)