From patchwork Mon Nov 12 17:42:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Ivanov X-Patchwork-Id: 996587 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cambridgegreys.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="qU4BgVkb"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="o4LVrmyi"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42tykv60nHz9s3l for ; Tue, 13 Nov 2018 04:42:31 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=4c+HNqNMsVK8mYlh+n9IiNSwEXLEVvMcNnYI53744Fw=; b=qU4BgVkbA3dsM0OBumj/icsOCH Sp2DWWKQzFdDP3iZuVPiPkCKVE3O59DClxBWIhxiGQv6gSlrlOMkVs+xvuoiU2FIw8EYd7LTaGD1U 5DJ2p+LArX5wSWb1/PFvhApBl27bWimjSY4Jq8kUE4Wb+azvpJlW4Bm9ehbxISl7Ht45yQWb8yVPn cIr0dDacHsdJuSheu8ZND782d5EzV605D5ZHuxvN/Hvpnw/RgfL8s+WAoHaG2BcZQuSSj/W6evo/d j7idXR28Cjd3CdHtJGq3GX761HAXqwlrVW4X4QFtAIfTpm12I/iog0EXdPheKZyHYfrHzg/Wg7DB0 T5g/2HAg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gMGEB-0000aq-Ul; Mon, 12 Nov 2018 17:42:23 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gMGEA-0000aT-AZ for linux-um@bombadil.infradead.org; Mon, 12 Nov 2018 17:42:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=GcTb9BUOexpiTaIG49Cd90OvppCrzolgLjvRIXKCvYc=; b=o4LVrmyieyxs+8EQwcbLbR6rH lDsx1OyohYluYP69SqG02pVR+C28uNUfTHBQqiLjUroZVw9OxLu2kJ3J3IdVvPMCRmFb6lo3hHM2H +F5eX0dCAx5/CElbryG1xGNUXUmGQukq1ILMDcQas85fyrU31YHcbsEOVcBZN05KHxUEryzd5E48N 2gJaMTsbLfCGpiKvPzuLk4TfEhQyJNBvIe3abXEXqqD5Iq9eyF+GqTiJMyZ4FJTq+kXGfiW87jYge PNTrFyFF6M3wWYs/VhBDYgwh0XZojvzvvGzv5Yz1MdRMeipS8+XuatEF5sC49cvArN6VJl3eTAYuD F7/8TlyFQ==; Received: from ivanoab6.miniserver.com ([5.153.251.140] helo=www.kot-begemot.co.uk) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gMGE6-0000Tp-Ui for linux-um@lists.infradead.org; Mon, 12 Nov 2018 17:42:20 +0000 Received: from [192.168.17.6] (helo=smaug.kot-begemot.co.uk) by www.kot-begemot.co.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gMGE5-0006m1-9i; Mon, 12 Nov 2018 17:42:17 +0000 Received: from amistad.kot-begemot.co.uk ([192.168.3.89]) by smaug.kot-begemot.co.uk with esmtp (Exim 4.89) (envelope-from ) id 1gMGDx-0006vn-N1; Mon, 12 Nov 2018 17:42:11 +0000 From: anton.ivanov@cambridgegreys.com To: linux-um@lists.infradead.org Subject: [PATCH 3/3] um: Add support for DISCARD in the UBD Driver Date: Mon, 12 Nov 2018 17:42:01 +0000 Message-Id: <20181112174201.12290-4-anton.ivanov@cambridgegreys.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181112174201.12290-1-anton.ivanov@cambridgegreys.com> References: <20181112174201.12290-1-anton.ivanov@cambridgegreys.com> X-Spam-Score: -1.0 X-Spam-Score: -1.0 X-Clacks-Overhead: GNU Terry Pratchett X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181112_174219_016454_B7C89279 X-CRM114-Status: GOOD ( 19.75 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.1 on casper.infradead.org summary: Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: axboe@kernel.dk, richard@nod.at, hch@lst.de, Anton Ivanov MIME-Version: 1.0 Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Anton Ivanov Support DISCARD and WRITE_ZEROES in the ubd driver using fallocate. Signed-off-by: Anton Ivanov --- arch/um/drivers/Kconfig | 22 ++++++++++++++ arch/um/drivers/ubd_kern.c | 71 +++++++++++++++++++++++++++++++++++++++------ arch/um/include/shared/os.h | 2 ++ arch/um/os-Linux/file.c | 8 +++++ 4 files changed, 94 insertions(+), 9 deletions(-) diff --git a/arch/um/drivers/Kconfig b/arch/um/drivers/Kconfig index 2b1aaf7755aa..a3407cca6bf9 100644 --- a/arch/um/drivers/Kconfig +++ b/arch/um/drivers/Kconfig @@ -1,5 +1,27 @@ # SPDX-License-Identifier: GPL-2.0 +menu "UML Block Devices" + +config BLK_DEV_UBD + bool "UBD Block Device" + default y + help + User Mode Linux virtual block device driver + +config BLK_DEV_UBD_SYNC + bool "Use Synchronous mode for UBD" + default n + help + Perform all disk operations synchronously (extremely slow). + +config BLK_DEV_UBD_DISCARD + bool "Enable DISCARD/TRIM support in UBD" + default y + help + Enable discard/trim support. Requires host kernel 3.x or above and + may not be supported on all host filesystems. +endmenu + menu "UML Character Devices" config STDERR_CONSOLE diff --git a/arch/um/drivers/ubd_kern.c b/arch/um/drivers/ubd_kern.c index 08b5ba9a5340..791d637ad4ef 100644 --- a/arch/um/drivers/ubd_kern.c +++ b/arch/um/drivers/ubd_kern.c @@ -514,9 +514,13 @@ static void ubd_handler(void) for (count = 0; count < n/sizeof(struct io_thread_req *); count++) { struct io_thread_req *io_req = (*irq_req_buffer)[count]; - if (!blk_update_request(io_req->req, io_req->error, io_req->length)) - __blk_mq_end_request(io_req->req, io_req->error); - + if ((io_req->error) || (io_req->buffer == NULL)) { + blk_mq_end_request(io_req->req, io_req->error); + } else { + if (!blk_update_request(io_req->req, io_req->error, io_req->length)) { + __blk_mq_end_request(io_req->req, io_req->error); + } + } kfree(io_req); } } @@ -775,6 +779,7 @@ static int ubd_open_dev(struct ubd *ubd_dev) char **back_ptr; int err, create_cow, *create_ptr; int fd; + int data; ubd_dev->openflags = ubd_dev->boot_openflags; create_cow = 0; @@ -829,6 +834,23 @@ static int ubd_open_dev(struct ubd *ubd_dev) if(err < 0) goto error; ubd_dev->cow.fd = err; } +#ifdef CONFIG_BLK_DEV_UBD_DISCARD + if (ubd_dev->cow.file != NULL) + fd = ubd_dev->cow.fd; + else + fd = ubd_dev->fd; + err = os_pread_file(fd, &data, sizeof(int), 0); + if (err == sizeof(int)) { + err = os_falloc_punch(fd, 0, sizeof(int)); + if (err == 0) { + ubd_dev->queue->limits.discard_granularity = UBD_SECTOR_SIZE; + ubd_dev->queue->limits.discard_alignment = UBD_SECTOR_SIZE; + blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST); + blk_queue_flag_set(QUEUE_FLAG_DISCARD, ubd_dev->queue); + os_pwrite_file(fd, &data, sizeof(int), 0); + } + } +#endif blk_queue_flag_set(QUEUE_FLAG_NONROT, ubd_dev->queue); return 0; error: @@ -1368,6 +1390,12 @@ static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx, } } break; +#ifdef CONFIG_BLK_DEV_UBD_DISCARD + case REQ_OP_DISCARD: + case REQ_OP_WRITE_ZEROES: + ret = ubd_queue_one_vec(hctx, req, (u64)blk_rq_pos(req) << 9, NULL); + break; +#endif default: WARN_ON_ONCE(1); spin_unlock_irq(&ubd_dev->lock); @@ -1429,6 +1457,7 @@ static int map_error(int error_code) switch (error_code) { case 0: return BLK_STS_OK; + case ENOSYS: case EOPNOTSUPP: return BLK_STS_NOTSUPP; case EPERM: @@ -1459,11 +1488,13 @@ static int update_bitmap(struct io_thread_req *req) static void do_io(struct io_thread_req *req) { - char *buf; + char *buf = NULL; unsigned long len; int n, nsectors, start, end, bit; __u64 off; + /* FLUSH is really a special case, we cannot "case" it with others */ + if (req_op(req->req) == REQ_OP_FLUSH) { /* fds[0] is always either the rw image or our cow file */ n = os_sync_file(req->fds[0]); @@ -1477,6 +1508,7 @@ static void do_io(struct io_thread_req *req) nsectors = req->length / req->sectorsize; start = 0; + do { bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask); end = start; @@ -1488,9 +1520,11 @@ static void do_io(struct io_thread_req *req) off = req->offset + req->offsets[bit] + start * req->sectorsize; len = (end - start) * req->sectorsize; - buf = &req->buffer[start * req->sectorsize]; + if (req->buffer != NULL) + buf = &req->buffer[start * req->sectorsize]; - if (req_op(req->req) == REQ_OP_READ) { + switch (req_op(req->req)) { + case REQ_OP_READ: n = 0; do { buf = &buf[n]; @@ -1504,7 +1538,8 @@ static void do_io(struct io_thread_req *req) } } while((n < len) && (n != 0)); if (n < len) memset(&buf[n], 0, len - n); - } else { + break; + case REQ_OP_WRITE: n = os_pwrite_file(req->fds[bit], buf, len, off); if(n != len){ printk("do_io - write failed err = %d " @@ -1512,12 +1547,30 @@ static void do_io(struct io_thread_req *req) req->error = map_error(-n); return; } + break; +#ifdef CONFIG_BLK_DEV_UBD_DISCARD + case REQ_OP_DISCARD: + case REQ_OP_WRITE_ZEROES: + n = os_falloc_punch(req->fds[bit], off, len); + if (n) { + printk("do_io - discard failed err = %d " + "fd = %d\n", -n, req->fds[bit]); + if (n == -EOPNOTSUPP) + req->error = map_error(-n); + return; + } + break; +#endif + default: + WARN_ON_ONCE(1); + req->error = BLK_STS_NOTSUPP; + return; } - start = end; } while(start < nsectors); - req->error = update_bitmap(req); + if (req_op(req->req) != REQ_OP_READ) + req->error = update_bitmap(req); } /* Changed in start_io_thread, which is serialized by being called only diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 048ae37eb5aa..9aa4b150e0a9 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -175,6 +175,8 @@ extern int os_fchange_dir(int fd); extern unsigned os_major(unsigned long long dev); extern unsigned os_minor(unsigned long long dev); extern unsigned long long os_makedev(unsigned major, unsigned minor); +extern int os_falloc_punch(int fd, unsigned long long offset, int count); + /* start_up.c */ extern void os_early_checks(void); diff --git a/arch/um/os-Linux/file.c b/arch/um/os-Linux/file.c index c0197097c86e..56e921ff3ff3 100644 --- a/arch/um/os-Linux/file.c +++ b/arch/um/os-Linux/file.c @@ -301,6 +301,14 @@ int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset) return n; } +int os_falloc_punch(int fd, unsigned long long offset, int len) +{ + int n = fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, len); + + if (n < 0) + return -errno; + return n; +} int os_file_size(const char *file, unsigned long long *size_out) {