Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2306575imn; Mon, 1 Aug 2022 20:07:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tzTJHTcWOBzBjXwTbCtPtsEbwoSukZrps0Dwj9x8lTb0mHTeAX11H4vW/2vFsNdDg9W00W X-Received: by 2002:a17:906:ef8b:b0:72b:58da:b115 with SMTP id ze11-20020a170906ef8b00b0072b58dab115mr14123616ejb.417.1659409657185; Mon, 01 Aug 2022 20:07:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659409657; cv=none; d=google.com; s=arc-20160816; b=inNgREMJPthTmjDDzwzzrWhkrcUxX19J6Dbf5TWhiFmg9ng2raXoSwEaK776HnfXs2 ba11Kad/zOkM8+1hQIT5aURIZsNcq5/ffKgjAHGbepLfj4F3GNrXXhLwA8IO3Z7eqcDr voD/tG7gNO11edlYnpR8mOo1I+mczSYGN/MNCq0rfoMLdqGcAAZejyrdy29H/zoIqNkM UMRZTQ0Zue/tMLmJR3jpKF587pivxTSxv00FcXFvIhVltmIsP96+XXHqTZI3LmqrM2G+ RU6yETr/w+dJOa2IHKZRcXDKGWFSiS+UxO56zglDb49P6tsOOJ4eG+fIeUDMW4YUOpFc HTuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=eFre63nUsbsh5IIAthvdk7tz3ecaVgX58XkGCdA8Rq8=; b=q/9BLsrscGDvjtABsDwfoKwIXq9S/mGUe+1SAqPexxS14drksgQfaw+Zh+iWelnuz2 u50Qla+4kecwCyjJ84YlV4WPiGhHLIccV4jF2dnGMmPO5iKAOaru6Zi5gz8WX1rvA6rX qefHBkj/3mVJHDGGrgPzybYd7t+FrpJ0ZgPAwL4ogAI8sNa8IQl2nLBHRmub7OqC86SK kjIXC/onBW4dPuNKHCrQCFZT6DlViSqGihPYepQ2ikQNeGHK8dJrHJfYnvX+zkiZzliz KUieh7+t6fe6pRMPDROAKiULpcf+4fqtmCmCiUAerykiroYNNyIcoVjdrXQ5LckCumqN DhfA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sg42-20020a170907a42a00b0072b9b3d42cbsi4136008ejc.525.2022.08.01.20.07.12; Mon, 01 Aug 2022 20:07:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235627AbiHBDEJ (ORCPT + 99 others); Mon, 1 Aug 2022 23:04:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235233AbiHBDDw (ORCPT ); Mon, 1 Aug 2022 23:03:52 -0400 Received: from out30-57.freemail.mail.aliyun.com (out30-57.freemail.mail.aliyun.com [115.124.30.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FB30201AD for ; Mon, 1 Aug 2022 20:03:50 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045168;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0VL9XT1b_1659409427; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VL9XT1b_1659409427) by smtp.aliyun-inc.com; Tue, 02 Aug 2022 11:03:48 +0800 From: Jingbo Xu To: dhowells@redhat.com, linux-cachefs@redhat.com Cc: linux-kernel@vger.kernel.org, xiang@kernel.org Subject: [PATCH RFC 6/9] cachefiles: check content map on read/write Date: Tue, 2 Aug 2022 11:03:39 +0800 Message-Id: <20220802030342.46302-7-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220802030342.46302-1-jefflexu@linux.alibaba.com> References: <20220802030342.46302-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org cachefiles_find_next_granule()/cachefiles_find_next_hole() are used to check if the requested range has been cached or not. The return value of these two functions imitates that of SEEK_[DATA|HOLE], so that the existing codes can be resued as much as possible. Signed-off-by: Jingbo Xu --- fs/cachefiles/content-map.c | 30 ++++++++++++++++++++++++++++++ fs/cachefiles/internal.h | 4 ++++ fs/cachefiles/io.c | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/fs/cachefiles/content-map.c b/fs/cachefiles/content-map.c index 877ff79e181b..949ec5d9e4c9 100644 --- a/fs/cachefiles/content-map.c +++ b/fs/cachefiles/content-map.c @@ -220,3 +220,33 @@ void cachefiles_mark_content_map(struct cachefiles_object *object, read_unlock_bh(&object->content_map_lock); } +loff_t cachefiles_find_next_granule(struct cachefiles_object *object, + loff_t start) +{ + unsigned long size, granule = start / CACHEFILES_GRAN_SIZE; + loff_t result; + + read_lock_bh(&object->content_map_lock); + size = object->content_map_size * BITS_PER_BYTE; + result = find_next_bit(object->content_map, size, granule); + read_unlock_bh(&object->content_map_lock); + + if (result == size) + return -ENXIO; + return result * CACHEFILES_GRAN_SIZE; +} + +loff_t cachefiles_find_next_hole(struct cachefiles_object *object, + loff_t start) +{ + unsigned long size, granule = start / CACHEFILES_GRAN_SIZE; + loff_t result; + + read_lock_bh(&object->content_map_lock); + size = object->content_map_size * BITS_PER_BYTE; + result = find_next_zero_bit(object->content_map, size, granule); + read_unlock_bh(&object->content_map_lock); + + return min_t(loff_t, result * CACHEFILES_GRAN_SIZE, + object->cookie->object_size); +} diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index c252746c8f9b..506700809a6d 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -183,6 +183,10 @@ extern int cachefiles_load_content_map(struct cachefiles_object *object); extern void cachefiles_save_content_map(struct cachefiles_object *object); extern void cachefiles_mark_content_map(struct cachefiles_object *object, loff_t start, loff_t len); +extern loff_t cachefiles_find_next_granule(struct cachefiles_object *object, + loff_t start); +extern loff_t cachefiles_find_next_hole(struct cachefiles_object *object, + loff_t start); /* * daemon.c diff --git a/fs/cachefiles/io.c b/fs/cachefiles/io.c index 27171fac649e..5c7c84cdafea 100644 --- a/fs/cachefiles/io.c +++ b/fs/cachefiles/io.c @@ -30,6 +30,32 @@ struct cachefiles_kiocb { u64 b_writing; }; +static loff_t cachefiles_seek_data(struct cachefiles_object *object, + struct file *file, loff_t start) +{ + switch (object->content_info) { + case CACHEFILES_CONTENT_MAP: + return cachefiles_find_next_granule(object, start); + case CACHEFILES_CONTENT_BACKFS_MAP: + return vfs_llseek(file, start, SEEK_DATA); + default: + return -EINVAL; + } +} + +static loff_t cachefiles_seek_hole(struct cachefiles_object *object, + struct file *file, loff_t start) +{ + switch (object->content_info) { + case CACHEFILES_CONTENT_MAP: + return cachefiles_find_next_hole(object, start); + case CACHEFILES_CONTENT_BACKFS_MAP: + return vfs_llseek(file, start, SEEK_HOLE); + default: + return -EINVAL; + } +} + static inline void cachefiles_put_kiocb(struct cachefiles_kiocb *ki) { if (refcount_dec_and_test(&ki->ki_refcnt)) { @@ -103,7 +129,7 @@ static int cachefiles_read(struct netfs_cache_resources *cres, off2 = cachefiles_inject_read_error(); if (off2 == 0) - off2 = vfs_llseek(file, off, SEEK_DATA); + off2 = cachefiles_seek_data(object, file, off); if (off2 < 0 && off2 >= (loff_t)-MAX_ERRNO && off2 != -ENXIO) { skipped = 0; ret = off2; @@ -442,7 +468,7 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest * retry: off = cachefiles_inject_read_error(); if (off == 0) - off = vfs_llseek(file, subreq->start, SEEK_DATA); + off = cachefiles_seek_data(object, file, subreq->start); if (off < 0 && off >= (loff_t)-MAX_ERRNO) { if (off == (loff_t)-ENXIO) { why = cachefiles_trace_read_seek_nxio; @@ -468,7 +494,7 @@ static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest * to = cachefiles_inject_read_error(); if (to == 0) - to = vfs_llseek(file, subreq->start, SEEK_HOLE); + to = cachefiles_seek_hole(object, file, subreq->start); if (to < 0 && to >= (loff_t)-MAX_ERRNO) { trace_cachefiles_io_error(object, file_inode(file), to, cachefiles_trace_seek_error); @@ -537,7 +563,7 @@ int __cachefiles_prepare_write(struct cachefiles_object *object, pos = cachefiles_inject_read_error(); if (pos == 0) - pos = vfs_llseek(file, *_start, SEEK_DATA); + pos = cachefiles_seek_data(object, file, *_start); if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) { if (pos == -ENXIO) goto check_space; /* Unallocated tail */ @@ -558,7 +584,7 @@ int __cachefiles_prepare_write(struct cachefiles_object *object, pos = cachefiles_inject_read_error(); if (pos == 0) - pos = vfs_llseek(file, *_start, SEEK_HOLE); + pos = cachefiles_seek_hole(object, file, *_start); if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) { trace_cachefiles_io_error(object, file_inode(file), pos, cachefiles_trace_seek_error); -- 2.27.0