From patchwork Thu Mar 28 20:39:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 13609591 X-Patchwork-Delegate: snitzer@redhat.com 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 347AA13A258 for ; Thu, 28 Mar 2024 20:39:48 +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=1711658390; cv=none; b=hcVL4jjMJ7VgRnG8HnpDwZiAUs3CUJfwmi2S9zQrEYF1Cq3yC2mR4WyCBQVJILyUh0LnS7IiIwnlLOOPD4PGlugY3iq37OpjvybRCGx9kcu/RU9i1kV/UF2ZSY+GikWT1TdzK6ZsNdJgiIyNo3a/cJksbG3cT1rzuG+VWn/L7Oo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711658390; c=relaxed/simple; bh=Z/9BALdmqq/q3tmjj2QgJEd4wnfZA9Ii706QeBSvW/w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Sn7tX/j9gmUx+HsaiM31PTtnR3UrYEILx4Mox7d2Kehz1uUVyLqs51e5JuMjIW1wz6u3iC/SIOc7gMLHHMz4nOhSgBmOF7q4sJxAVCZhDDigaRlRG8JwVbrY84jwEZt5I+77qkQBvrOLXAVLNI40/bss8AtIlePeR27mzdcUL2U= 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=Js0TgT0V; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Js0TgT0V" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711658388; 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=Js0TgT0VcuHLQUlyj+LRmqzC51YnCQljxOtkFslZzg6X4a9LkDG6l11HfiJNHud3lNTDaV 8uZPsd0y14LfKkqS1srU79Kwz9z3dP09qGNyeHk87klVULkNEWaVRj3znkxq6+Bf6GoTS+ KzMb29F4VehEOCt5/2S7fulpOhjs3mE= 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: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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; }