From patchwork Fri Jun 23 13:34:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 9806585 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 99E1A60349 for ; Fri, 23 Jun 2017 13:34:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A136285E2 for ; Fri, 23 Jun 2017 13:34:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E99B28776; Fri, 23 Jun 2017 13:34:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 091B9285E2 for ; Fri, 23 Jun 2017 13:34:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753786AbdFWNew (ORCPT ); Fri, 23 Jun 2017 09:34:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48332 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751183AbdFWNeu (ORCPT ); Fri, 23 Jun 2017 09:34:50 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 73FA37F417; Fri, 23 Jun 2017 13:34:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 73FA37F417 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=agruenba@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 73FA37F417 Received: from nux.redhat.com (ovpn-116-35.ams2.redhat.com [10.36.116.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 269F452351; Fri, 23 Jun 2017 13:34:48 +0000 (UTC) From: Andreas Gruenbacher To: linux-fsdevel@vger.kernel.org Cc: Andreas Gruenbacher , linux-xfs@vger.kernel.org, cluster-devel@redhat.com, Jan Kara Subject: [PATCH v2 1/6] vfs: Add iomap_seek_hole_data helper Date: Fri, 23 Jun 2017 15:34:39 +0200 Message-Id: <1498224884-346-2-git-send-email-agruenba@redhat.com> In-Reply-To: <1498224884-346-1-git-send-email-agruenba@redhat.com> References: <1498224884-346-1-git-send-email-agruenba@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 23 Jun 2017 13:34:50 +0000 (UTC) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Filesystems can use this for implementing lseek SEEK_HOLE / SEEK_DATA support via iomap. Signed-off-by: Andreas Gruenbacher --- fs/iomap.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/iomap.h | 3 ++ 2 files changed, 92 insertions(+) diff --git a/fs/iomap.c b/fs/iomap.c index 4b10892..781f0a0 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -584,6 +584,95 @@ int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi, } EXPORT_SYMBOL_GPL(iomap_fiemap); +static loff_t +iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length, + void *data, struct iomap *iomap) +{ + if (iomap->type == IOMAP_HOLE) + goto found; + length = iomap->offset + iomap->length - offset; + if (iomap->type != IOMAP_UNWRITTEN) + return length; + offset = page_cache_seek_hole_data(inode, offset, length, SEEK_HOLE); + if (offset < 0) + return length; +found: + *(loff_t *)data = offset; + return 0; +} + +static loff_t +iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length, + void *data, struct iomap *iomap) +{ + if (iomap->type != IOMAP_HOLE && iomap->type != IOMAP_UNWRITTEN) + goto found; + length = iomap->offset + iomap->length - offset; + if (iomap->type != IOMAP_UNWRITTEN) + return length; + offset = page_cache_seek_hole_data(inode, offset, length, SEEK_DATA); + if (offset < 0) + return length; +found: + *(loff_t *)data = offset; + return 0; +} + +/* + * Filesystem helper for lseek SEEK_HOLE / SEEK_DATA. + */ +loff_t +iomap_seek_hole_data(struct inode *inode, loff_t offset, + int whence, const struct iomap_ops *ops) +{ + static loff_t (*actor)(struct inode *, loff_t, loff_t, void *, + struct iomap *); + loff_t size = i_size_read(inode); + loff_t length; + + /* Nothing to be found beyond the end of the file. */ + if (offset >= size) + return -ENXIO; + length = size - offset; + + switch(whence) { + case SEEK_HOLE: + actor = iomap_seek_hole_actor; + break; + + case SEEK_DATA: + actor = iomap_seek_data_actor; + break; + } + + while (length > 0) { + loff_t ret; + + ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops, + &offset, actor); + if (ret <= 0) { + if (ret < 0) + return ret; + break; + } + offset += ret; + length -= ret; + } + + if (length <= 0) { + /* There is an implicit hole at the end of the file. */ + if (whence != SEEK_HOLE) + return -ENXIO; + + /* The last segment can extend beyond the end of the file. */ + if (offset > size) + offset = size; + } + + return offset; +} +EXPORT_SYMBOL_GPL(iomap_seek_hole_data); + /* * Private flags for iomap_dio, must not overlap with the public ones in * iomap.h: diff --git a/include/linux/iomap.h b/include/linux/iomap.h index f753e78..ff89026 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -83,6 +83,9 @@ int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, int iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops); int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, loff_t start, loff_t len, const struct iomap_ops *ops); +loff_t iomap_seek_hole_data(struct inode *inode, loff_t offset, + int whence, const struct iomap_ops *ops); + /* * Flags for direct I/O ->end_io: