From patchwork Tue Dec 15 15:43:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 7855781 Return-Path: X-Original-To: patchwork-linux-block@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 167FE9F387 for ; Tue, 15 Dec 2015 16:16:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 97AFC202FE for ; Tue, 15 Dec 2015 16:16:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 094F3202C8 for ; Tue, 15 Dec 2015 16:16:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965031AbbLOQQB (ORCPT ); Tue, 15 Dec 2015 11:16:01 -0500 Received: from mail-by2on0075.outbound.protection.outlook.com ([207.46.100.75]:59036 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S965158AbbLOQP7 (ORCPT ); Tue, 15 Dec 2015 11:15:59 -0500 Received: from BY1PR0201CA0037.namprd02.prod.outlook.com (10.160.191.175) by BL2PR02MB291.namprd02.prod.outlook.com (10.141.90.148) with Microsoft SMTP Server (TLS) id 15.1.337.19; Tue, 15 Dec 2015 15:43:53 +0000 Received: from BN1BFFO11FD031.protection.gbl (2a01:111:f400:7c10::1:110) by BY1PR0201CA0037.outlook.office365.com (2a01:111:e400:4814::47) with Microsoft SMTP Server (TLS) id 15.1.361.13 via Frontend Transport; Tue, 15 Dec 2015 15:43:52 +0000 Authentication-Results: spf=pass (sender IP is 63.163.107.173) smtp.mailfrom=sandisk.com; redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=bestguesspass action=none header.from=sandisk.com; Received-SPF: Pass (protection.outlook.com: domain of sandisk.com designates 63.163.107.173 as permitted sender) receiver=protection.outlook.com; client-ip=63.163.107.173; helo=milsmgep12.sandisk.com; Received: from milsmgep12.sandisk.com (63.163.107.173) by BN1BFFO11FD031.mail.protection.outlook.com (10.58.144.94) with Microsoft SMTP Server id 15.1.346.13 via Frontend Transport; Tue, 15 Dec 2015 15:43:51 +0000 Received: from MILHUBIP03.sdcorp.global.sandisk.com ( [172.22.12.162]) by milsmgep12.sandisk.com (Symantec Messaging Gateway) with SMTP id 2B.31.32178.73530765; Tue, 15 Dec 2015 07:43:51 -0800 (PST) Received: from milsmgip11.sandisk.com (10.177.8.100) by MILHUBIP03.sdcorp.global.sandisk.com (10.177.9.96) with Microsoft SMTP Server id 14.3.248.2; Tue, 15 Dec 2015 07:43:51 -0800 X-AuditID: ac160a69-f79f76d000007db2-6d-567035371965 Received: from [10.50.231.65] ( [10.177.8.100]) by milsmgip11.sandisk.com (Symantec Messaging Gateway) with SMTP id 91.38.15370.53530765; Tue, 15 Dec 2015 07:43:51 -0800 (PST) Subject: [PATCH 1/2] block: Move blkdev_issue_discard() definition To: Jens Axboe References: <567034B3.90104@sandisk.com> CC: Jan Kara , Christoph Hellwig , "Martin K. Petersen" , Mike Snitzer , "linux-block@vger.kernel.org" From: Bart Van Assche Message-ID: <56703535.2030800@sandisk.com> Date: Tue, 15 Dec 2015 16:43:49 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <567034B3.90104@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFuphkeLIzCtJLcpLzFFi42JZI8azSNfctCDM4N05BYv/e46xWaxcfZTJ Yvb0ZiaLvbe0LZYf/8dk0bbxK6MDm8fE5nfsHrtvNrB5fHx6i8Xj/b6rbB5nFhxh9/i8SS6A LYrLJiU1J7MstUjfLoErY8nEo0wFv4wrfj+9wNrA2KPZxc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uJERzMnJE7GJ9OND/EyMTBKdXAmGLG0bIhpbPL/mbGyz7GWXO1JCbnuvA/+Hhh 8ckTVxm/3W6ZaLle/1K2/Z68TR+yIuvedGss5Dqr+nzv/bDtMscNTiafvjF51erXjgzOd0uV nFd9mdz2e89Xe/FU2+aDLmJ7pxr0vek4elDTYG+HobLO5LdPnl/fnvBGXUAgdluH60JNxgKz YCWW4oxEQy3mouJEAJELzNoWAgAA X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BN1BFFO11FD031; 1:APtXqfTwqPDEj5XmyptZafjfs27R9CsmmBshOrIUU9+JqrF3U/BAISKV5qdeICq/Mgj/uZS0mrD4x0lSjuhU0WHD6v9fQ88Q0qqBoTHAUOumRROg7nheqUjAaHjcnv9UPDzJbqryS8qcRRr6Rxz3Koo+0LQvXB7v8B4k8mrr17PZ6p3cwyk0k0GHWohu2gBCwzR26C+SRzYVDt3C6mvYWHqGIO5kbzcX4A2Y4RDRq+tlPMBv9q1LA38p4Dp5YVksvXu5JdpF2OIjbtvS3rIlP8ndGgLmV2cgTb8B4qaBDMvI1YVv26MX8UofWuueH98qNwujt5rb9kg2pGaUuWjuSohLqTV0rQeIIKzzP/LkrB0K/Yj6tW6VnCTyRsxVvqU0bs4IixQoJXVriJIZkNwKRQ== X-Forefront-Antispam-Report: CIP:63.163.107.173; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(438002)(50944005)(199003)(189002)(4001350100001)(19580405001)(36756003)(11100500001)(2950100001)(77096005)(65956001)(50466002)(5008740100001)(65816999)(81156007)(92566002)(97736004)(47776003)(1220700001)(1096002)(110136002)(69596002)(59896002)(54356999)(50986999)(229853001)(5001960100002)(65806001)(189998001)(106466001)(23676002)(230700001)(99136001)(76176999)(575784001)(87266999)(19580395003)(117636001)(87936001)(86362001)(83506001)(586003)(64126003)(62816006); DIR:OUT; SFP:1101; SCL:1; SRVR:BL2PR02MB291; H:milsmgep12.sandisk.com; FPR:; SPF:Pass; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2PR02MB291; 2:ytX/dCPRuqsADW1hBBgmW20tKo/D63+cPOcMJtyk+ug6geLFgrL8tO6J2yhPlgvqgEHy+gOpE3vA7BksKLY7eOBdjFfSVHKSQMT54+9+bl2H8EKoCeW6WLdxj5xqlHrWcrq/2ogT7xNAuYwFa27h+g==; 3:uX6tqR1OgR9+0txloUk5Cc5HvOvT4o5Iatx6oLlaCTCt/o4PL0WsYSp4HAX0tiZQZZ/w1TBzAsTU4eB12y1SjOlRyeCE4fsuPYfoDtdwWx4nDn06pDHNL3tZddVaLA76EejKo8cFlkfM3kJ2pRfmMseC3Cqk3/8b6ltlTgTDHwE7rWYBXLKsUTnOmxxoCn7yJOhYH6Df08MNGkNPsH7Lp7L+962zSLUDLBuhTTi13nEWN4ePxUnsM1f2i6zfM4PyQi1HRmwrdY/Cg4tz5pCQjw==; 25:WPip5Kzbv9TpcUittISXO71MBzwUhKl0BT0f19kYCJ+tLh0FsDA22GUH+p1OuM5F52YIQ66PrPa4YoOFfn0rnXLde7kKPNNh2Xgbl0pgra92f+BrU3erR+01Gg7nEA+Qb6fitqS/Sep0y25vHUkl3QBkRThPCnse1j2wGZBeydgGy+yntDtUDBD4Fdzkfr+wAgr3A2jKzv5Ix7eMzKQGNHKj1wHVtgNwWU0umssxm2IFaMtJmYbQRctmevzfjrvbL3csFrzXJe52QQN7r4OCyg== X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(8251501001); SRVR:BL2PR02MB291; X-Microsoft-Exchange-Diagnostics: 1; BL2PR02MB291; 20:KGl/G86Cyd1HQAVFLpTbbPOyY2vni5lxBCFHbbJMfLD830JhZPllsLwf4Zcvm2NK3NHUCU71Ln2OxZiwnZ5Bfob2EdjZl5bKNZke3jSvjIx9q7s8JPyuudHDAMYZpGx2lpFoxQ6CPmGcsn4F8WphCn4eJDuA2VNYn/Oda6Fbizcq/9qwIZfKdxAUwF7ICQdKiMVvs/tOmaE/21WBNaP5tdZlUNYx0ulwcIsVEI3oU3vv17xdFlwHNdTJH5NW8fqsjxUaAsKOjPtFGCQPcyWEtX43NB1uGeqwcxdla4id+IhD+R3y5Vj9L9mzyHCQmPYB1PoCE7oWDchVRserGoNXaidspzBehhwj+1yH1+i1raHcUOcHBBfVXLedlOpPk5575+M29Qomy4LDN7Rot6JnhWJcpjPrJrYiY889ODx4AK4M1bsOyyxdVXI1Ss3O7U0yXSrDZSfkqKVvXTgpVuS18Q9hR3MMLuKbxgXeVwS6qYb2f85HA5FjemxJZyRW34IN X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(146099531331640)(42932892334569); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(520078)(10201501046)(3002001); SRVR:BL2PR02MB291; BCL:0; PCL:0; RULEID:; SRVR:BL2PR02MB291; X-Microsoft-Exchange-Diagnostics: 1; BL2PR02MB291; 4:8ssLcvXPKKmRC1pY8GpkQRv9zRBiHvfa1yb7pE4YKMfttSZFwDR007whEmQ6hxTtZ8O688OpaSgB3nUeO+ozhIZq1EBzWtXkp/CGji6qt4nsJkZxvMi38pQvMpwjD/jGg1nRR6R3ThVIqCJcPISUVpxkSmPEs4T2vV0VH/N1zxW6gLPhkwW94iWNohetLkVLAmnmxFvT5HFna4oKUPeZ5rcBg+9QQ6B1KxAIwlM4uAiJA26LzjYWliOoIQGRN1Lae+RG+pSHj/vgpX27RIpIg6PhxCfzOSnSYxyv3VO77hBGTnd6ZHacQcGu3VMK8IbRRuIFUKEuhk4fOMAID3gyDx0PslW8Q1RmljYfvhdZuPHN9rhVNwrRNhnQPvS71sMuF2GOkc8R1rVyyVAnSKhnPnFTSpS6xaOJ9T3+GZSnJtSx8EKpyL/IH03a0FX8Q4mb2Cz1CDDtbZFNDmLphp8Htg== X-Forefront-PRVS: 07915F544A X-Microsoft-Ex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aF+F2xC+sB8M5O74bvo92rQZr99Wo0a+mXk5EYfhJ4lc9cR+Ug==; 24:0ZsNYTtAN7IpGBri6R6IvtDgFHrApWhqQs1YGNPc/xx1HVoqKc7uNpYi+o60Z9cKs12MepxlIKaSeBHwyeFk+WZ1C3ZC+CvsTYtPXaniUYo=; 20:jheomQQC7f+yWB+MhX1qEF4zvwCnvPlEJR66RjOS3W0itts/rfELC8a/b76fhNTBxy/FxTHnq/d96XjiwFYgC3oJ+9+zPOJjbynhUzMZRK5TOoFsSBLu+ABT3m2FIAFksLRa31ame6lSc8xZ2AFi31QY6tjUG52vwxfqdpSK/Rmud9fVc1/zMRZSiDXz1cie3E/AmfVwLLMcc15OcctQp1ym6GSDN4OaLIKzGqoD+NMu1OXSWJUjgM5EMsivblxx SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Dec 2015 15:43:51.8961 (UTC) X-MS-Exchange-CrossTenant-Id: fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d; Ip=[63.163.107.173]; Helo=[milsmgep12.sandisk.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2PR02MB291 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Moving the blkdev_issue_discard() definition avoids that a forward declaration has to be introduced in the next patch. Signed-off-by: Bart Van Assche Cc: Jan Kara Cc: Christoph Hellwig Cc: Martin K. Petersen Cc: Ming Lin Cc: Mike Snitzer --- block/blk-lib.c | 208 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 9ebf653..22e9f0f 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -27,110 +27,6 @@ static void bio_batch_end_io(struct bio *bio) } /** - * blkdev_issue_discard - queue a discard - * @bdev: blockdev to issue discard for - * @sector: start sector - * @nr_sects: number of sectors to discard - * @gfp_mask: memory allocation flags (for bio_alloc) - * @flags: BLKDEV_IFL_* flags to control behaviour - * - * Description: - * Issue a discard request for the sectors in question. - */ -int blkdev_issue_discard(struct block_device *bdev, sector_t sector, - sector_t nr_sects, gfp_t gfp_mask, unsigned long flags) -{ - DECLARE_COMPLETION_ONSTACK(wait); - struct request_queue *q = bdev_get_queue(bdev); - int type = REQ_WRITE | REQ_DISCARD; - unsigned int granularity; - int alignment; - struct bio_batch bb; - struct bio *bio; - int ret = 0; - struct blk_plug plug; - - if (!q) - return -ENXIO; - - if (!blk_queue_discard(q)) - return -EOPNOTSUPP; - - /* Zero-sector (unknown) and one-sector granularities are the same. */ - granularity = max(q->limits.discard_granularity >> 9, 1U); - alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; - - if (flags & BLKDEV_DISCARD_SECURE) { - if (!blk_queue_secdiscard(q)) - return -EOPNOTSUPP; - type |= REQ_SECURE; - } - - atomic_set(&bb.done, 1); - bb.error = 0; - bb.wait = &wait; - - blk_start_plug(&plug); - while (nr_sects) { - unsigned int req_sects; - sector_t end_sect, tmp; - - bio = bio_alloc(gfp_mask, 1); - if (!bio) { - ret = -ENOMEM; - break; - } - - /* Make sure bi_size doesn't overflow */ - req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9); - - /* - * If splitting a request, and the next starting sector would be - * misaligned, stop the discard at the previous aligned sector. - */ - end_sect = sector + req_sects; - tmp = end_sect; - if (req_sects < nr_sects && - sector_div(tmp, granularity) != alignment) { - end_sect = end_sect - alignment; - sector_div(end_sect, granularity); - end_sect = end_sect * granularity + alignment; - req_sects = end_sect - sector; - } - - bio->bi_iter.bi_sector = sector; - bio->bi_end_io = bio_batch_end_io; - bio->bi_bdev = bdev; - bio->bi_private = &bb; - - bio->bi_iter.bi_size = req_sects << 9; - nr_sects -= req_sects; - sector = end_sect; - - atomic_inc(&bb.done); - submit_bio(type, bio); - - /* - * We can loop for a long time in here, if someone does - * full device discards (like mkfs). Be nice and allow - * us to schedule out to avoid softlocking if preempt - * is disabled. - */ - cond_resched(); - } - blk_finish_plug(&plug); - - /* Wait for bios in-flight */ - if (!atomic_dec_and_test(&bb.done)) - wait_for_completion_io(&wait); - - if (bb.error) - return bb.error; - return ret; -} -EXPORT_SYMBOL(blkdev_issue_discard); - -/** * blkdev_issue_write_same - queue a write same operation * @bdev: target blockdev * @sector: start sector @@ -262,6 +158,110 @@ static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, } /** + * blkdev_issue_discard - queue a discard + * @bdev: blockdev to issue discard for + * @sector: start sector + * @nr_sects: number of sectors to discard + * @gfp_mask: memory allocation flags (for bio_alloc) + * @flags: BLKDEV_IFL_* flags to control behaviour + * + * Description: + * Issue a discard request for the sectors in question. + */ +int blkdev_issue_discard(struct block_device *bdev, sector_t sector, + sector_t nr_sects, gfp_t gfp_mask, unsigned long flags) +{ + DECLARE_COMPLETION_ONSTACK(wait); + struct request_queue *q = bdev_get_queue(bdev); + int type = REQ_WRITE | REQ_DISCARD; + unsigned int granularity; + int alignment; + struct bio_batch bb; + struct bio *bio; + int ret = 0; + struct blk_plug plug; + + if (!q) + return -ENXIO; + + if (!blk_queue_discard(q)) + return -EOPNOTSUPP; + + /* Zero-sector (unknown) and one-sector granularities are the same. */ + granularity = max(q->limits.discard_granularity >> 9, 1U); + alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; + + if (flags & BLKDEV_DISCARD_SECURE) { + if (!blk_queue_secdiscard(q)) + return -EOPNOTSUPP; + type |= REQ_SECURE; + } + + atomic_set(&bb.done, 1); + bb.error = 0; + bb.wait = &wait; + + blk_start_plug(&plug); + while (nr_sects) { + unsigned int req_sects; + sector_t end_sect, tmp; + + bio = bio_alloc(gfp_mask, 1); + if (!bio) { + ret = -ENOMEM; + break; + } + + /* Make sure bi_size doesn't overflow */ + req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9); + + /* + * If splitting a request, and the next starting sector would be + * misaligned, stop the discard at the previous aligned sector. + */ + end_sect = sector + req_sects; + tmp = end_sect; + if (req_sects < nr_sects && + sector_div(tmp, granularity) != alignment) { + end_sect = end_sect - alignment; + sector_div(end_sect, granularity); + end_sect = end_sect * granularity + alignment; + req_sects = end_sect - sector; + } + + bio->bi_iter.bi_sector = sector; + bio->bi_end_io = bio_batch_end_io; + bio->bi_bdev = bdev; + bio->bi_private = &bb; + + bio->bi_iter.bi_size = req_sects << 9; + nr_sects -= req_sects; + sector = end_sect; + + atomic_inc(&bb.done); + submit_bio(type, bio); + + /* + * We can loop for a long time in here, if someone does + * full device discards (like mkfs). Be nice and allow + * us to schedule out to avoid softlocking if preempt + * is disabled. + */ + cond_resched(); + } + blk_finish_plug(&plug); + + /* Wait for bios in-flight */ + if (!atomic_dec_and_test(&bb.done)) + wait_for_completion_io(&wait); + + if (bb.error) + return bb.error; + return ret; +} +EXPORT_SYMBOL(blkdev_issue_discard); + +/** * blkdev_issue_zeroout - zero-fill a block range * @bdev: blockdev to write * @sector: start sector