Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp701832lqh; Thu, 28 Mar 2024 13:40:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWc3RiQRv1Kt81UfHCSwC90sH0iCIO2oc/KQxE92XzloGtuy8JEVHBAXxbrG5QKtI45aYc1xnGZRk3f10/Y9w6s8lZOLRAmz3zpCpu1Vg== X-Google-Smtp-Source: AGHT+IHG7CZREKgKxPrCdAt9W+yl0pSmec6yeZuz/thkNS+F7/eBtQL5Afae/Q6N8THOZQwqpOw9 X-Received: by 2002:a0c:ea85:0:b0:698:6fbd:6c33 with SMTP id d5-20020a0cea85000000b006986fbd6c33mr315826qvp.33.1711658429827; Thu, 28 Mar 2024 13:40:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711658429; cv=pass; d=google.com; s=arc-20160816; b=RyrO2OGTbCSzocYcfX2Kc5l19TfzzIDVDz4bdG5EXVQ9L1PKcfEsAsTGSM0yUXVDk/ fNmeshthZr3YKXz/GON0h1iMNFJpFvnq3goXZWx3WKtcD+yOa9EYijZk20Whm6xT+ENi 2tTE+y9RrfEDrqdi0kMIOJLItZBxV7oEPVgkJJVgVXb25Q1PAO4GpJabkGcC+y+zQOTs ZQdQsQ03/DJxnKZxmws+l/1qC8lgDE24KHAkvehIJXszkCflvGRH/y5cJsssEs/EVmE1 7vbegWqSmF7e8/Sgtx6SMr8N7EYbo4043bP3UW9pGlSBMTOJmisljpYt8yXiYkrz+XYQ DpiA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ifdaXHJafNhgJEodv1uxro7ZmRQAOz8+u7BNkXOalBo=; fh=RMJuV2Y3SJza63ZzuPXP6nO1gt49mM9k7teQJSpE9RE=; b=hSzWoJpDJ0W+9kUJfKwkAIsyYOaBQanOgm1+faM3N+xXM1Le9XBYEdWJXCJlA+W5KM pyxfZSs/dg2s1/qGs2JLOYd5forbMWRZ7DXDlczoSUY7jdnLcBEfEqUtbWggBvZDukFc V5gcTjijFLaApxpbp6MJWOQI4gq2PnoscjGFsTqUmXOIekreXzDXt5w6UF7R9A+IdPHr /SsH1ZP+Xh7CCFNUotxQ3jlOhXvIEhoi9np3bQGf1b8uHOUQ0HYSuACTqHCg2lVY++Ok 383orwNct+6tKtu4pe+uwumN/hocz2QeW29NXR6w98YPsuDRYYqULELhyH0lHkzjtVuu 4K8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QEZ1G5FZ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-123599-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123599-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id f8-20020a0cbec8000000b0069616879b6esi2149976qvj.37.2024.03.28.13.40.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 13:40:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123599-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QEZ1G5FZ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-123599-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123599-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 75FD21C3030E for ; Thu, 28 Mar 2024 20:40:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4B66113A88D; Thu, 28 Mar 2024 20:39:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QEZ1G5FZ" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3EDA13A3E7 for ; Thu, 28 Mar 2024 20:39:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711658393; cv=none; b=FhbI493x39Nohwa261lbC7UAHmLI2ho69qUV6yhnSiFWc7LvsDacX6DnJzcgqayU0J996ItfOqJ32MUDTBsOHTbsS0KxkKKAmiws1JD7J/HfQ3tuydFz/rIHGKcZX+pcLmVwyayvdL1ISBcrPBNsshTbmrvYa107zPQJmc++n2s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711658393; c=relaxed/simple; bh=Z/9BALdmqq/q3tmjj2QgJEd4wnfZA9Ii706QeBSvW/w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=McWFrD6XiM+Y7FMby4Z6DAaFZXBrM3kIqC2SrHBMZX84tNJkWCXS+bjfmT5Yu1n11bYZXoUOu5UY/dDrESKa7n3BXVp79gKZsPz3hmAo1C2aLwXY3GWOB2NSLvcHJF0Kn3HHfRCd4lr4+WOndrxaSEZ8crfHD/gwuKD1GlOymSk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QEZ1G5FZ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711658390; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ifdaXHJafNhgJEodv1uxro7ZmRQAOz8+u7BNkXOalBo=; b=QEZ1G5FZ2vWF9jfR84zg1Rvli180LBl1uvUGYmw5BbSAGIQvdphmUSZUiJOnQFok0DweJB autHHgHYoPl8HVi8ooQuWy/uLT9FqAf1p76ymFeiWxMCKKVwCIqRFxXf8eERfxccchhnLX 4FYv46cyIND5SRki7Gzvvk7hR473rFI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-492-EC1kfjvMNx-NTQDJ-Tmc6A-1; Thu, 28 Mar 2024 16:39:46 -0400 X-MC-Unique: EC1kfjvMNx-NTQDJ-Tmc6A-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CAC9A1C2CDF2; Thu, 28 Mar 2024 20:39:45 +0000 (UTC) Received: from localhost (unknown [10.39.194.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id C12A3492BDA; Thu, 28 Mar 2024 20:39:44 +0000 (UTC) From: Stefan Hajnoczi To: linux-block@vger.kernel.org Cc: linux-kernel@vger.kernel.org, eblake@redhat.com, Alasdair Kergon , Mikulas Patocka , dm-devel@lists.linux.dev, David Teigland , Mike Snitzer , Jens Axboe , Christoph Hellwig , Joe Thornber , Stefan Hajnoczi Subject: [RFC 1/9] block: add llseek(SEEK_HOLE/SEEK_DATA) support Date: Thu, 28 Mar 2024 16:39:02 -0400 Message-ID: <20240328203910.2370087-2-stefanha@redhat.com> In-Reply-To: <20240328203910.2370087-1-stefanha@redhat.com> References: <20240328203910.2370087-1-stefanha@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 The SEEK_HOLE/SEEK_DATA interface is used by userspace applications to detect sparseness. This makes copying and backup applications faster and reduces space consumption because only ranges that do not contain data can be skipped. Handle SEEK_HOLE/SEEK_DATA for block devices. No block drivers implement the new callback yet so the entire block device will appear to contain data. Later patches will add support to drivers so this actually becomes useful. Signed-off-by: Stefan Hajnoczi --- include/linux/blkdev.h | 7 +++++++ block/fops.c | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index c3e8f7cf96be9..eecfbf9c27fc4 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -332,6 +332,9 @@ int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op, int blk_revalidate_disk_zones(struct gendisk *disk, void (*update_driver_data)(struct gendisk *disk)); +loff_t blkdev_seek_hole_data(struct block_device *bdev, loff_t offset, + int whence); + /* * Independent access ranges: struct blk_independent_access_range describes * a range of contiguous sectors that can be accessed using device command @@ -1432,6 +1435,10 @@ struct block_device_operations { * driver. */ int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector); + + /* Like llseek(SEEK_HOLE/SEEK_DATA). This callback may be NULL. */ + loff_t (*seek_hole_data)(struct block_device *bdev, loff_t offset, + int whence); }; #ifdef CONFIG_COMPAT diff --git a/block/fops.c b/block/fops.c index 679d9b752fe82..8ffbfec6b4c25 100644 --- a/block/fops.c +++ b/block/fops.c @@ -523,6 +523,43 @@ const struct address_space_operations def_blk_aops = { }; #endif /* CONFIG_BUFFER_HEAD */ +/* Like llseek(SEEK_HOLE/SEEK_DATA) */ +loff_t blkdev_seek_hole_data(struct block_device *bdev, loff_t offset, + int whence) +{ + const struct block_device_operations *fops = bdev->bd_disk->fops; + loff_t size; + + if (fops->seek_hole_data) + return fops->seek_hole_data(bdev, offset, whence); + + size = bdev_nr_bytes(bdev); + + switch (whence) { + case SEEK_DATA: + if ((unsigned long long)offset >= size) + return -ENXIO; + return offset; + case SEEK_HOLE: + if ((unsigned long long)offset >= size) + return -ENXIO; + return size; + default: + return -EINVAL; + } +} + +static loff_t blkdev_llseek_hole_data(struct file *file, loff_t offset, + int whence) +{ + struct block_device *bdev = file_bdev(file); + + offset = blkdev_seek_hole_data(bdev, offset, whence); + if (offset >= 0) + offset = vfs_setpos(file, offset, bdev_nr_bytes(bdev)); + return offset; +} + /* * for a block special file file_inode(file)->i_size is zero * so we compute the size by hand (just as in block_read/write above) @@ -533,7 +570,11 @@ static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence) loff_t retval; inode_lock(bd_inode); - retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); + if (whence == SEEK_HOLE || whence == SEEK_DATA) + retval = blkdev_llseek_hole_data(file, offset, whence); + else + retval = fixed_size_llseek(file, offset, whence, + i_size_read(bd_inode)); inode_unlock(bd_inode); return retval; } -- 2.44.0