From patchwork Fri Nov 9 09:19:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Ivanov X-Patchwork-Id: 995402 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="dbDPxc0E"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="DBw7wfNK"; 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 42rvkx30d4z9s8r for ; Fri, 9 Nov 2018 20:20:20 +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: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:In-Reply-To: References:List-Owner; bh=vI07Wp8H6cSTvEO0BbUwa37WCc4jTGOdote5oem0XcM=; b=dbD Pxc0Eu2kixf302vgi8pBpqCuK3PtHepQhrsF1Eg8wQVPyf0n0h+IkXWAq9mkQ3HUFCtWvXn/lzn6L opfRWxRyTOHDvPYTXasAIXEBgeAI9EHYONRHgQogK/jnQ+L+nQG8PO28LYaADu6Rnc5XCc0Hx5DTk ogHg0PzGsqGuXSRDwP6Fv2yFJpA8TLgERtaqevzvmWFPb5dk8kwYQcLVuPNFcwxJ/xvMhF2NlYg1y V0OaoC8fPwtaCbxVQ19rNBK4XTXCKCwd+OCJyU5If49Ekkc5ibvkStyDQasv+rhSbIJ0PL0AWg+pR /ljAX2F7BRfUvzKITzw8ERB6/kS1f8w==; 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 1gL2xU-00021O-Q1; Fri, 09 Nov 2018 09:20:08 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gL2xS-0001QX-T8 for linux-um@bombadil.infradead.org; Fri, 09 Nov 2018 09:20:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=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:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=9K8ETLfOzgQyh/9BYJHmh9/Mzj5zTeEUyPhDiximGI8=; b=DBw7wfNKqzLJoHUAmNCk9RvBd YD8r+XwDZpD6gwkLD5Zp7iP+XhQgOwUFpyPRj/sCwwt4sVKgQ+t4Q4KhXB8vkiCvAidyoO2NK+AVl HXiNU2DCutmvnvOGbjO9HDvnqMg7q4PkP8GNxluOCDu6o6tyMlWG/WwfyZEYuwXly2i/1vMSmEIdb Dk8Dg1qKTHTH0ktF+I/TauFjNjEBJCM6KJHgEHbk1YNV9Sq4xSzJwQ4sK2WRO+kRnrRR14HZXxMZz 0E0HFplWE/VLzUvKNBMwmEvvKm6I5IZAtBKquA3K8+0KW9tHt/Nuz748kXh0FnhyQKTmsEERhPH3J y3ucwdZUQ==; Received: from ivanoab6.miniserver.com ([5.153.251.140] helo=www.kot-begemot.co.uk) by merlin.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gL2xP-0002DD-5d for linux-um@lists.infradead.org; Fri, 09 Nov 2018 09:20:04 +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 1gL2x9-0007Ii-7i; Fri, 09 Nov 2018 09:19:47 +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 1gL2x2-00031r-OY; Fri, 09 Nov 2018 09:19:42 +0000 From: anton.ivanov@cambridgegreys.com To: linux-um@lists.infradead.org Subject: [PATCH] Add DISCARD support to UML udb driver Date: Fri, 9 Nov 2018 09:19:37 +0000 Message-Id: <20181109091937.25470-1-anton.ivanov@cambridgegreys.com> X-Mailer: git-send-email 2.11.0 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-20181109_042003_366578_526AD134 X-CRM114-Status: GOOD ( 17.77 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on merlin.infradead.org summary: Content analysis details: (0.0 points) 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 1. Remove UBD specific opcode definitions and reuse the block-mq ones 2. Add DISCARD and ZERO fill support (tested with ext4). 3. Further cleanup on the request forming, request checking and handling of unexpected command codes. Signed-off-by: Anton Ivanov --- arch/um/drivers/ubd_kern.c | 69 ++++++++++++++++++++++++++++++--------------- arch/um/include/shared/os.h | 1 + arch/um/os-Linux/file.c | 12 +++++++- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/arch/um/drivers/ubd_kern.c b/arch/um/drivers/ubd_kern.c index 28c40624bcb6..503c1520ed8f 100644 --- a/arch/um/drivers/ubd_kern.c +++ b/arch/um/drivers/ubd_kern.c @@ -43,11 +43,9 @@ #include #include "cow.h" -enum ubd_req { UBD_READ, UBD_WRITE, UBD_FLUSH }; - struct io_thread_req { struct request *req; - enum ubd_req op; + int op; int fds[2]; unsigned long offsets[2]; unsigned long long offset; @@ -830,6 +828,10 @@ static int ubd_open_dev(struct ubd *ubd_dev) if(err < 0) goto error; ubd_dev->cow.fd = err; } + ubd_dev->queue->limits.discard_granularity = 1 << 9; + ubd_dev->queue->limits.discard_alignment = 1 << 9; + blk_queue_max_discard_sectors(ubd_dev->queue, 8 * sizeof(long)); + blk_queue_flag_set(QUEUE_FLAG_DISCARD|QUEUE_FLAG_NONROT, ubd_dev->queue); return 0; error: os_close_file(ubd_dev->fd); @@ -1277,7 +1279,7 @@ static void cowify_req(struct io_thread_req *req, unsigned long *bitmap, if(req->length > (sizeof(req->sector_mask) * 8) << 9) panic("Operation too long"); - if(req->op == UBD_READ) { + if (req->op == REQ_OP_READ) { for(i = 0; i < req->length >> 9; i++){ if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) ubd_set_bit(i, (unsigned char *) @@ -1307,20 +1309,21 @@ static int ubd_queue_one_vec(struct blk_mq_hw_ctx *hctx, struct request *req, io_req->fds[0] = dev->fd; io_req->error = 0; - if (req_op(req) == REQ_OP_FLUSH) { - io_req->op = UBD_FLUSH; - } else { + io_req->op = req_op(req); + + if (io_req->op != REQ_OP_FLUSH) { io_req->fds[1] = dev->fd; io_req->cow_offset = -1; io_req->offset = off; - io_req->length = bvec->bv_len; + if ((io_req->op == REQ_OP_READ) || (io_req->op == REQ_OP_WRITE)) { + io_req->length = bvec->bv_len; + io_req->buffer = page_address(bvec->bv_page) + bvec->bv_offset; + } else + io_req->length = blk_rq_bytes(req); io_req->sector_mask = 0; - io_req->op = rq_data_dir(req) == READ ? UBD_READ : UBD_WRITE; io_req->offsets[0] = 0; io_req->offsets[1] = dev->cow.data_offset; - io_req->buffer = page_address(bvec->bv_page) + bvec->bv_offset; io_req->sectorsize = 1 << 9; - if (dev->cow.file) { cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset, dev->cow.bitmap_len); @@ -1351,15 +1354,19 @@ static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx, if (req_op(req) == REQ_OP_FLUSH) { ret = ubd_queue_one_vec(hctx, req, 0, NULL); } else { - struct req_iterator iter; - struct bio_vec bvec; u64 off = (u64)blk_rq_pos(req) << 9; - rq_for_each_segment(bvec, req, iter) { - ret = ubd_queue_one_vec(hctx, req, off, &bvec); - if (ret < 0) - goto out; - off += bvec.bv_len; + if ((req_op(req) == REQ_OP_READ) || (req_op(req) == REQ_OP_WRITE)) { + struct req_iterator iter; + struct bio_vec bvec; + rq_for_each_segment(bvec, req, iter) { + ret = ubd_queue_one_vec(hctx, req, off, &bvec); + if (ret < 0) + goto out; + off += bvec.bv_len; + } + } else { + ret = ubd_queue_one_vec(hctx, req, off, NULL); } } out: @@ -1438,7 +1445,7 @@ static void do_io(struct io_thread_req *req) int n, nsectors, start, end, bit; __u64 off; - if (req->op == UBD_FLUSH) { + if (req->op == REQ_OP_FLUSH) { /* fds[0] is always either the rw image or our cow file */ n = os_sync_file(req->fds[0]); if (n != 0) { @@ -1462,9 +1469,9 @@ 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->op == UBD_READ){ + switch (req->op) { + case REQ_OP_READ: + buf = &req->buffer[start * req->sectorsize]; n = 0; do { buf = &buf[n]; @@ -1478,7 +1485,9 @@ 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: + buf = &req->buffer[start * req->sectorsize]; n = os_pwrite_file(req->fds[bit], buf, len, off); if(n != len){ printk("do_io - write failed err = %d " @@ -1486,6 +1495,20 @@ static void do_io(struct io_thread_req *req) req->error = 1; return; } + break; + 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]); + req->error = 1; + return; + } + break; + default: + req->error = 1; + return; } start = end; diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 048ae37eb5aa..ebf23012a59b 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -175,6 +175,7 @@ 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..d110cd7adca7 100644 --- a/arch/um/os-Linux/file.c +++ b/arch/um/os-Linux/file.c @@ -2,7 +2,9 @@ * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) * Licensed under the GPL */ - +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif #include #include #include @@ -301,6 +303,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) {