From patchwork Fri Jun 16 14:51:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 9791905 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 068886038E for ; Fri, 16 Jun 2017 14:51:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC29228111 for ; Fri, 16 Jun 2017 14:51:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0E3B285C2; Fri, 16 Jun 2017 14:51:43 +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=ham 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 7466128111 for ; Fri, 16 Jun 2017 14:51:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754438AbdFPOvl (ORCPT ); Fri, 16 Jun 2017 10:51:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45310 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754429AbdFPOvj (ORCPT ); Fri, 16 Jun 2017 10:51:39 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6802B80C12; Fri, 16 Jun 2017 14:51:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6802B80C12 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=agruenba@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6802B80C12 Received: from nux.redhat.com (ovpn-116-41.ams2.redhat.com [10.36.116.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id A5DBE17B58; Fri, 16 Jun 2017 14:51:37 +0000 (UTC) From: Andreas Gruenbacher To: linux-fsdevel@vger.kernel.org Cc: Andreas Gruenbacher , Bob Peterson , linux-xfs@vger.kernel.org, cluster-devel@redhat.com Subject: [PATCH 4/7] vfs: Add iomap_seek_hole_data helper Date: Fri, 16 Jun 2017 16:51:17 +0200 Message-Id: <1497624680-16685-5-git-send-email-agruenba@redhat.com> In-Reply-To: <1497624680-16685-1-git-send-email-agruenba@redhat.com> References: <1497624680-16685-1-git-send-email-agruenba@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 16 Jun 2017 14:51:39 +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. The __iomap_seek_hole_data helper takes an additional size argument and doesn't reposition the file offset; this is for internal use for xfs quota files which don't maintain the inode size. Signed-off-by: Andreas Gruenbacher --- fs/iomap.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/iomap.h | 6 ++++ 2 files changed, 87 insertions(+) diff --git a/fs/iomap.c b/fs/iomap.c index 4b10892..cf01694 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -584,6 +584,87 @@ 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) + return 0; + return iomap->offset + iomap->length - offset; +} + +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) + return 0; + return iomap->offset + iomap->length - offset; +} + +loff_t +__iomap_seek_hole_data(struct inode *inode, loff_t offset, loff_t size, + int whence, const struct iomap_ops *ops) +{ + static loff_t (*actor)(struct inode *, loff_t, loff_t, void *, + struct iomap *); + loff_t len = size - offset; + loff_t ret; + + /* Nothing to be found beyond the end of the file. */ + if (len <= 0) + return -ENXIO; + + switch(whence) { + case SEEK_HOLE: + actor = iomap_seek_hole_actor; + break; + + case SEEK_DATA: + actor = iomap_seek_data_actor; + break; + } + + while (len > 0) { + ret = iomap_apply(inode, offset, len, IOMAP_REPORT, ops, + NULL, actor); + if (ret <= 0) { + if (ret < 0) + return ret; + break; + } + offset += ret; + len -= ret; + } + + if (len <= 0) { + /* There is an implicit hole at the end of the file. */ + if (whence != SEEK_HOLE) + offset = -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); + +loff_t +iomap_seek_hole_data(struct file *file, loff_t offset, int whence, + const struct iomap_ops *ops) +{ + struct inode *inode = file->f_mapping->host; + + offset = __iomap_seek_hole_data(inode, offset, i_size_read(inode), + whence, ops); + if (offset <= 0) + return offset; + return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); +} +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 9d64933..49b6ec0 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -85,6 +85,12 @@ 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); +struct file; +loff_t __iomap_seek_hole_data(struct inode *inode, loff_t pos, loff_t size, + int whence, const struct iomap_ops *ops); +loff_t iomap_seek_hole_data(struct file *file, loff_t pos, + int whence, const struct iomap_ops *ops); + /* * Flags for direct I/O ->end_io: */