1*1d57628fSDongsheng Yang // SPDX-License-Identifier: GPL-2.0-or-later 2*1d57628fSDongsheng Yang 3*1d57628fSDongsheng Yang #include "cache.h" 4*1d57628fSDongsheng Yang #include "backing_dev.h" 5*1d57628fSDongsheng Yang #include "cache_dev.h" 6*1d57628fSDongsheng Yang #include "dm_pcache.h" 7*1d57628fSDongsheng Yang 8*1d57628fSDongsheng Yang static int cache_data_head_init(struct pcache_cache *cache) 9*1d57628fSDongsheng Yang { 10*1d57628fSDongsheng Yang struct pcache_cache_segment *next_seg; 11*1d57628fSDongsheng Yang struct pcache_cache_data_head *data_head; 12*1d57628fSDongsheng Yang 13*1d57628fSDongsheng Yang data_head = get_data_head(cache); 14*1d57628fSDongsheng Yang next_seg = get_cache_segment(cache); 15*1d57628fSDongsheng Yang if (!next_seg) 16*1d57628fSDongsheng Yang return -EBUSY; 17*1d57628fSDongsheng Yang 18*1d57628fSDongsheng Yang cache_seg_get(next_seg); 19*1d57628fSDongsheng Yang data_head->head_pos.cache_seg = next_seg; 20*1d57628fSDongsheng Yang data_head->head_pos.seg_off = 0; 21*1d57628fSDongsheng Yang 22*1d57628fSDongsheng Yang return 0; 23*1d57628fSDongsheng Yang } 24*1d57628fSDongsheng Yang 25*1d57628fSDongsheng Yang /** 26*1d57628fSDongsheng Yang * cache_data_alloc - Allocate data for a cache key. 27*1d57628fSDongsheng Yang * @cache: Pointer to the cache structure. 28*1d57628fSDongsheng Yang * @key: Pointer to the cache key to allocate data for. 29*1d57628fSDongsheng Yang * 30*1d57628fSDongsheng Yang * This function tries to allocate space from the cache segment specified by the 31*1d57628fSDongsheng Yang * data head. If the remaining space in the segment is insufficient to allocate 32*1d57628fSDongsheng Yang * the requested length for the cache key, it will allocate whatever is available 33*1d57628fSDongsheng Yang * and adjust the key's length accordingly. This function does not allocate 34*1d57628fSDongsheng Yang * space that crosses segment boundaries. 35*1d57628fSDongsheng Yang */ 36*1d57628fSDongsheng Yang static int cache_data_alloc(struct pcache_cache *cache, struct pcache_cache_key *key) 37*1d57628fSDongsheng Yang { 38*1d57628fSDongsheng Yang struct pcache_cache_data_head *data_head; 39*1d57628fSDongsheng Yang struct pcache_cache_pos *head_pos; 40*1d57628fSDongsheng Yang struct pcache_cache_segment *cache_seg; 41*1d57628fSDongsheng Yang u32 seg_remain; 42*1d57628fSDongsheng Yang u32 allocated = 0, to_alloc; 43*1d57628fSDongsheng Yang int ret = 0; 44*1d57628fSDongsheng Yang 45*1d57628fSDongsheng Yang preempt_disable(); 46*1d57628fSDongsheng Yang data_head = get_data_head(cache); 47*1d57628fSDongsheng Yang again: 48*1d57628fSDongsheng Yang to_alloc = key->len - allocated; 49*1d57628fSDongsheng Yang if (!data_head->head_pos.cache_seg) { 50*1d57628fSDongsheng Yang seg_remain = 0; 51*1d57628fSDongsheng Yang } else { 52*1d57628fSDongsheng Yang cache_pos_copy(&key->cache_pos, &data_head->head_pos); 53*1d57628fSDongsheng Yang key->seg_gen = key->cache_pos.cache_seg->gen; 54*1d57628fSDongsheng Yang 55*1d57628fSDongsheng Yang head_pos = &data_head->head_pos; 56*1d57628fSDongsheng Yang cache_seg = head_pos->cache_seg; 57*1d57628fSDongsheng Yang seg_remain = cache_seg_remain(head_pos); 58*1d57628fSDongsheng Yang } 59*1d57628fSDongsheng Yang 60*1d57628fSDongsheng Yang if (seg_remain > to_alloc) { 61*1d57628fSDongsheng Yang /* If remaining space in segment is sufficient for the cache key, allocate it. */ 62*1d57628fSDongsheng Yang cache_pos_advance(head_pos, to_alloc); 63*1d57628fSDongsheng Yang allocated += to_alloc; 64*1d57628fSDongsheng Yang cache_seg_get(cache_seg); 65*1d57628fSDongsheng Yang } else if (seg_remain) { 66*1d57628fSDongsheng Yang /* If remaining space is not enough, allocate the remaining space and adjust the cache key length. */ 67*1d57628fSDongsheng Yang cache_pos_advance(head_pos, seg_remain); 68*1d57628fSDongsheng Yang key->len = seg_remain; 69*1d57628fSDongsheng Yang 70*1d57628fSDongsheng Yang /* Get for key: obtain a reference to the cache segment for the key. */ 71*1d57628fSDongsheng Yang cache_seg_get(cache_seg); 72*1d57628fSDongsheng Yang /* Put for head_pos->cache_seg: release the reference for the current head's segment. */ 73*1d57628fSDongsheng Yang cache_seg_put(head_pos->cache_seg); 74*1d57628fSDongsheng Yang head_pos->cache_seg = NULL; 75*1d57628fSDongsheng Yang } else { 76*1d57628fSDongsheng Yang /* Initialize a new data head if no segment is available. */ 77*1d57628fSDongsheng Yang ret = cache_data_head_init(cache); 78*1d57628fSDongsheng Yang if (ret) 79*1d57628fSDongsheng Yang goto out; 80*1d57628fSDongsheng Yang 81*1d57628fSDongsheng Yang goto again; 82*1d57628fSDongsheng Yang } 83*1d57628fSDongsheng Yang 84*1d57628fSDongsheng Yang out: 85*1d57628fSDongsheng Yang preempt_enable(); 86*1d57628fSDongsheng Yang 87*1d57628fSDongsheng Yang return ret; 88*1d57628fSDongsheng Yang } 89*1d57628fSDongsheng Yang 90*1d57628fSDongsheng Yang static int cache_copy_from_req_bio(struct pcache_cache *cache, struct pcache_cache_key *key, 91*1d57628fSDongsheng Yang struct pcache_request *pcache_req, u32 bio_off) 92*1d57628fSDongsheng Yang { 93*1d57628fSDongsheng Yang struct pcache_cache_pos *pos = &key->cache_pos; 94*1d57628fSDongsheng Yang struct pcache_segment *segment; 95*1d57628fSDongsheng Yang 96*1d57628fSDongsheng Yang segment = &pos->cache_seg->segment; 97*1d57628fSDongsheng Yang 98*1d57628fSDongsheng Yang return segment_copy_from_bio(segment, pos->seg_off, key->len, pcache_req->bio, bio_off); 99*1d57628fSDongsheng Yang } 100*1d57628fSDongsheng Yang 101*1d57628fSDongsheng Yang static int cache_copy_to_req_bio(struct pcache_cache *cache, struct pcache_request *pcache_req, 102*1d57628fSDongsheng Yang u32 bio_off, u32 len, struct pcache_cache_pos *pos, u64 key_gen) 103*1d57628fSDongsheng Yang { 104*1d57628fSDongsheng Yang struct pcache_cache_segment *cache_seg = pos->cache_seg; 105*1d57628fSDongsheng Yang struct pcache_segment *segment = &cache_seg->segment; 106*1d57628fSDongsheng Yang int ret; 107*1d57628fSDongsheng Yang 108*1d57628fSDongsheng Yang spin_lock(&cache_seg->gen_lock); 109*1d57628fSDongsheng Yang if (key_gen < cache_seg->gen) { 110*1d57628fSDongsheng Yang spin_unlock(&cache_seg->gen_lock); 111*1d57628fSDongsheng Yang return -EINVAL; 112*1d57628fSDongsheng Yang } 113*1d57628fSDongsheng Yang 114*1d57628fSDongsheng Yang ret = segment_copy_to_bio(segment, pos->seg_off, len, pcache_req->bio, bio_off); 115*1d57628fSDongsheng Yang spin_unlock(&cache_seg->gen_lock); 116*1d57628fSDongsheng Yang 117*1d57628fSDongsheng Yang return ret; 118*1d57628fSDongsheng Yang } 119*1d57628fSDongsheng Yang 120*1d57628fSDongsheng Yang /** 121*1d57628fSDongsheng Yang * miss_read_end_req - Handle the end of a miss read request. 122*1d57628fSDongsheng Yang * @backing_req: Pointer to the request structure. 123*1d57628fSDongsheng Yang * @read_ret: Return value of read. 124*1d57628fSDongsheng Yang * 125*1d57628fSDongsheng Yang * This function is called when a backing request to read data from 126*1d57628fSDongsheng Yang * the backing_dev is completed. If the key associated with the request 127*1d57628fSDongsheng Yang * is empty (a placeholder), it allocates cache space for the key, 128*1d57628fSDongsheng Yang * copies the data read from the bio into the cache, and updates 129*1d57628fSDongsheng Yang * the key's status. If the key has been overwritten by a write 130*1d57628fSDongsheng Yang * request during this process, it will be deleted from the cache 131*1d57628fSDongsheng Yang * tree and no further action will be taken. 132*1d57628fSDongsheng Yang */ 133*1d57628fSDongsheng Yang static void miss_read_end_req(struct pcache_backing_dev_req *backing_req, int read_ret) 134*1d57628fSDongsheng Yang { 135*1d57628fSDongsheng Yang void *priv_data = backing_req->priv_data; 136*1d57628fSDongsheng Yang struct pcache_request *pcache_req = backing_req->req.upper_req; 137*1d57628fSDongsheng Yang struct pcache_cache *cache = backing_req->backing_dev->cache; 138*1d57628fSDongsheng Yang int ret; 139*1d57628fSDongsheng Yang 140*1d57628fSDongsheng Yang if (priv_data) { 141*1d57628fSDongsheng Yang struct pcache_cache_key *key; 142*1d57628fSDongsheng Yang struct pcache_cache_subtree *cache_subtree; 143*1d57628fSDongsheng Yang 144*1d57628fSDongsheng Yang key = (struct pcache_cache_key *)priv_data; 145*1d57628fSDongsheng Yang cache_subtree = key->cache_subtree; 146*1d57628fSDongsheng Yang 147*1d57628fSDongsheng Yang /* if this key was deleted from cache_subtree by a write, key->flags should be cleared, 148*1d57628fSDongsheng Yang * so if cache_key_empty() return true, this key is still in cache_subtree 149*1d57628fSDongsheng Yang */ 150*1d57628fSDongsheng Yang spin_lock(&cache_subtree->tree_lock); 151*1d57628fSDongsheng Yang if (cache_key_empty(key)) { 152*1d57628fSDongsheng Yang /* Check if the backing request was successful. */ 153*1d57628fSDongsheng Yang if (read_ret) { 154*1d57628fSDongsheng Yang cache_key_delete(key); 155*1d57628fSDongsheng Yang goto unlock; 156*1d57628fSDongsheng Yang } 157*1d57628fSDongsheng Yang 158*1d57628fSDongsheng Yang /* Allocate cache space for the key and copy data from the backing_dev. */ 159*1d57628fSDongsheng Yang ret = cache_data_alloc(cache, key); 160*1d57628fSDongsheng Yang if (ret) { 161*1d57628fSDongsheng Yang cache_key_delete(key); 162*1d57628fSDongsheng Yang goto unlock; 163*1d57628fSDongsheng Yang } 164*1d57628fSDongsheng Yang 165*1d57628fSDongsheng Yang ret = cache_copy_from_req_bio(cache, key, pcache_req, backing_req->req.bio_off); 166*1d57628fSDongsheng Yang if (ret) { 167*1d57628fSDongsheng Yang cache_seg_put(key->cache_pos.cache_seg); 168*1d57628fSDongsheng Yang cache_key_delete(key); 169*1d57628fSDongsheng Yang goto unlock; 170*1d57628fSDongsheng Yang } 171*1d57628fSDongsheng Yang key->flags &= ~PCACHE_CACHE_KEY_FLAGS_EMPTY; 172*1d57628fSDongsheng Yang key->flags |= PCACHE_CACHE_KEY_FLAGS_CLEAN; 173*1d57628fSDongsheng Yang 174*1d57628fSDongsheng Yang /* Append the key to the cache. */ 175*1d57628fSDongsheng Yang ret = cache_key_append(cache, key, false); 176*1d57628fSDongsheng Yang if (ret) { 177*1d57628fSDongsheng Yang cache_seg_put(key->cache_pos.cache_seg); 178*1d57628fSDongsheng Yang cache_key_delete(key); 179*1d57628fSDongsheng Yang goto unlock; 180*1d57628fSDongsheng Yang } 181*1d57628fSDongsheng Yang } 182*1d57628fSDongsheng Yang unlock: 183*1d57628fSDongsheng Yang spin_unlock(&cache_subtree->tree_lock); 184*1d57628fSDongsheng Yang cache_key_put(key); 185*1d57628fSDongsheng Yang } 186*1d57628fSDongsheng Yang } 187*1d57628fSDongsheng Yang 188*1d57628fSDongsheng Yang /** 189*1d57628fSDongsheng Yang * submit_cache_miss_req - Submit a backing request when cache data is missing 190*1d57628fSDongsheng Yang * @cache: The cache context that manages cache operations 191*1d57628fSDongsheng Yang * @backing_req: The cache request containing information about the read request 192*1d57628fSDongsheng Yang * 193*1d57628fSDongsheng Yang * This function is used to handle cases where a cache read request cannot locate 194*1d57628fSDongsheng Yang * the required data in the cache. When such a miss occurs during `cache_subtree_walk`, 195*1d57628fSDongsheng Yang * it triggers a backing read request to fetch data from the backing storage. 196*1d57628fSDongsheng Yang * 197*1d57628fSDongsheng Yang * If `pcache_req->priv_data` is set, it points to a `pcache_cache_key`, representing 198*1d57628fSDongsheng Yang * a new cache key to be inserted into the cache. The function calls `cache_key_insert` 199*1d57628fSDongsheng Yang * to attempt adding the key. On insertion failure, it releases the key reference and 200*1d57628fSDongsheng Yang * clears `priv_data` to avoid further processing. 201*1d57628fSDongsheng Yang */ 202*1d57628fSDongsheng Yang static void submit_cache_miss_req(struct pcache_cache *cache, struct pcache_backing_dev_req *backing_req) 203*1d57628fSDongsheng Yang { 204*1d57628fSDongsheng Yang if (backing_req->priv_data) { 205*1d57628fSDongsheng Yang struct pcache_cache_key *key; 206*1d57628fSDongsheng Yang 207*1d57628fSDongsheng Yang /* Attempt to insert the key into the cache if priv_data is set */ 208*1d57628fSDongsheng Yang key = (struct pcache_cache_key *)backing_req->priv_data; 209*1d57628fSDongsheng Yang cache_key_insert(&cache->req_key_tree, key, true); 210*1d57628fSDongsheng Yang } 211*1d57628fSDongsheng Yang backing_dev_req_submit(backing_req, false); 212*1d57628fSDongsheng Yang } 213*1d57628fSDongsheng Yang 214*1d57628fSDongsheng Yang static void cache_miss_req_free(struct pcache_backing_dev_req *backing_req) 215*1d57628fSDongsheng Yang { 216*1d57628fSDongsheng Yang struct pcache_cache_key *key; 217*1d57628fSDongsheng Yang 218*1d57628fSDongsheng Yang if (backing_req->priv_data) { 219*1d57628fSDongsheng Yang key = backing_req->priv_data; 220*1d57628fSDongsheng Yang backing_req->priv_data = NULL; 221*1d57628fSDongsheng Yang cache_key_put(key); /* for ->priv_data */ 222*1d57628fSDongsheng Yang cache_key_put(key); /* for init ref in alloc */ 223*1d57628fSDongsheng Yang } 224*1d57628fSDongsheng Yang 225*1d57628fSDongsheng Yang backing_dev_req_end(backing_req); 226*1d57628fSDongsheng Yang } 227*1d57628fSDongsheng Yang 228*1d57628fSDongsheng Yang static struct pcache_backing_dev_req *cache_miss_req_alloc(struct pcache_cache *cache, 229*1d57628fSDongsheng Yang struct pcache_request *parent, 230*1d57628fSDongsheng Yang gfp_t gfp_mask) 231*1d57628fSDongsheng Yang { 232*1d57628fSDongsheng Yang struct pcache_backing_dev *backing_dev = cache->backing_dev; 233*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 234*1d57628fSDongsheng Yang struct pcache_cache_key *key = NULL; 235*1d57628fSDongsheng Yang struct pcache_backing_dev_req_opts req_opts = { 0 }; 236*1d57628fSDongsheng Yang 237*1d57628fSDongsheng Yang req_opts.type = BACKING_DEV_REQ_TYPE_REQ; 238*1d57628fSDongsheng Yang req_opts.gfp_mask = gfp_mask; 239*1d57628fSDongsheng Yang req_opts.req.upper_req = parent; 240*1d57628fSDongsheng Yang 241*1d57628fSDongsheng Yang backing_req = backing_dev_req_alloc(backing_dev, &req_opts); 242*1d57628fSDongsheng Yang if (!backing_req) 243*1d57628fSDongsheng Yang return NULL; 244*1d57628fSDongsheng Yang 245*1d57628fSDongsheng Yang key = cache_key_alloc(&cache->req_key_tree, gfp_mask); 246*1d57628fSDongsheng Yang if (!key) 247*1d57628fSDongsheng Yang goto free_backing_req; 248*1d57628fSDongsheng Yang 249*1d57628fSDongsheng Yang cache_key_get(key); 250*1d57628fSDongsheng Yang backing_req->priv_data = key; 251*1d57628fSDongsheng Yang 252*1d57628fSDongsheng Yang return backing_req; 253*1d57628fSDongsheng Yang 254*1d57628fSDongsheng Yang free_backing_req: 255*1d57628fSDongsheng Yang cache_miss_req_free(backing_req); 256*1d57628fSDongsheng Yang return NULL; 257*1d57628fSDongsheng Yang } 258*1d57628fSDongsheng Yang 259*1d57628fSDongsheng Yang static void cache_miss_req_init(struct pcache_cache *cache, 260*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req, 261*1d57628fSDongsheng Yang struct pcache_request *parent, 262*1d57628fSDongsheng Yang u32 off, u32 len, bool insert_key) 263*1d57628fSDongsheng Yang { 264*1d57628fSDongsheng Yang struct pcache_cache_key *key; 265*1d57628fSDongsheng Yang struct pcache_backing_dev_req_opts req_opts = { 0 }; 266*1d57628fSDongsheng Yang 267*1d57628fSDongsheng Yang req_opts.type = BACKING_DEV_REQ_TYPE_REQ; 268*1d57628fSDongsheng Yang req_opts.req.upper_req = parent; 269*1d57628fSDongsheng Yang req_opts.req.req_off = off; 270*1d57628fSDongsheng Yang req_opts.req.len = len; 271*1d57628fSDongsheng Yang req_opts.end_fn = miss_read_end_req; 272*1d57628fSDongsheng Yang 273*1d57628fSDongsheng Yang backing_dev_req_init(backing_req, &req_opts); 274*1d57628fSDongsheng Yang 275*1d57628fSDongsheng Yang if (insert_key) { 276*1d57628fSDongsheng Yang key = backing_req->priv_data; 277*1d57628fSDongsheng Yang key->off = parent->off + off; 278*1d57628fSDongsheng Yang key->len = len; 279*1d57628fSDongsheng Yang key->flags |= PCACHE_CACHE_KEY_FLAGS_EMPTY; 280*1d57628fSDongsheng Yang } else { 281*1d57628fSDongsheng Yang key = backing_req->priv_data; 282*1d57628fSDongsheng Yang backing_req->priv_data = NULL; 283*1d57628fSDongsheng Yang cache_key_put(key); 284*1d57628fSDongsheng Yang cache_key_put(key); 285*1d57628fSDongsheng Yang } 286*1d57628fSDongsheng Yang } 287*1d57628fSDongsheng Yang 288*1d57628fSDongsheng Yang static struct pcache_backing_dev_req *get_pre_alloc_req(struct pcache_cache_subtree_walk_ctx *ctx) 289*1d57628fSDongsheng Yang { 290*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 291*1d57628fSDongsheng Yang struct pcache_request *pcache_req = ctx->pcache_req; 292*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 293*1d57628fSDongsheng Yang 294*1d57628fSDongsheng Yang if (ctx->pre_alloc_req) { 295*1d57628fSDongsheng Yang backing_req = ctx->pre_alloc_req; 296*1d57628fSDongsheng Yang ctx->pre_alloc_req = NULL; 297*1d57628fSDongsheng Yang 298*1d57628fSDongsheng Yang return backing_req; 299*1d57628fSDongsheng Yang } 300*1d57628fSDongsheng Yang 301*1d57628fSDongsheng Yang return cache_miss_req_alloc(cache, pcache_req, GFP_NOWAIT); 302*1d57628fSDongsheng Yang } 303*1d57628fSDongsheng Yang 304*1d57628fSDongsheng Yang /* 305*1d57628fSDongsheng Yang * In the process of walking the cache tree to locate cached data, this 306*1d57628fSDongsheng Yang * function handles the situation where the requested data range lies 307*1d57628fSDongsheng Yang * entirely before an existing cache node (`key_tmp`). This outcome 308*1d57628fSDongsheng Yang * signifies that the target data is absent from the cache (cache miss). 309*1d57628fSDongsheng Yang * 310*1d57628fSDongsheng Yang * To fulfill this portion of the read request, the function creates a 311*1d57628fSDongsheng Yang * backing request (`backing_req`) for the missing data range represented 312*1d57628fSDongsheng Yang * by `key`. It then appends this request to the submission list in the 313*1d57628fSDongsheng Yang * `ctx`, which will later be processed to retrieve the data from backing 314*1d57628fSDongsheng Yang * storage. After setting up the backing request, `req_done` in `ctx` is 315*1d57628fSDongsheng Yang * updated to reflect the length of the handled range, and the range 316*1d57628fSDongsheng Yang * in `key` is adjusted by trimming off the portion that is now handled. 317*1d57628fSDongsheng Yang * 318*1d57628fSDongsheng Yang * The scenario handled here: 319*1d57628fSDongsheng Yang * 320*1d57628fSDongsheng Yang * |--------| key_tmp (existing cached range) 321*1d57628fSDongsheng Yang * |====| key (requested range, preceding key_tmp) 322*1d57628fSDongsheng Yang * 323*1d57628fSDongsheng Yang * Since `key` is before `key_tmp`, it signifies that the requested data 324*1d57628fSDongsheng Yang * range is missing in the cache (cache miss) and needs retrieval from 325*1d57628fSDongsheng Yang * backing storage. 326*1d57628fSDongsheng Yang */ 327*1d57628fSDongsheng Yang static int read_before(struct pcache_cache_key *key, struct pcache_cache_key *key_tmp, 328*1d57628fSDongsheng Yang struct pcache_cache_subtree_walk_ctx *ctx) 329*1d57628fSDongsheng Yang { 330*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 331*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 332*1d57628fSDongsheng Yang 333*1d57628fSDongsheng Yang /* 334*1d57628fSDongsheng Yang * In this scenario, `key` represents a range that precedes `key_tmp`, 335*1d57628fSDongsheng Yang * meaning the requested data range is missing from the cache tree 336*1d57628fSDongsheng Yang * and must be retrieved from the backing_dev. 337*1d57628fSDongsheng Yang */ 338*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 339*1d57628fSDongsheng Yang if (!backing_req) 340*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 341*1d57628fSDongsheng Yang 342*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, key->len, true); 343*1d57628fSDongsheng Yang 344*1d57628fSDongsheng Yang list_add(&backing_req->node, ctx->submit_req_list); 345*1d57628fSDongsheng Yang ctx->req_done += key->len; 346*1d57628fSDongsheng Yang cache_key_cutfront(key, key->len); 347*1d57628fSDongsheng Yang 348*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_OK; 349*1d57628fSDongsheng Yang } 350*1d57628fSDongsheng Yang 351*1d57628fSDongsheng Yang /* 352*1d57628fSDongsheng Yang * During cache_subtree_walk, this function manages a scenario where part of the 353*1d57628fSDongsheng Yang * requested data range overlaps with an existing cache node (`key_tmp`). 354*1d57628fSDongsheng Yang * 355*1d57628fSDongsheng Yang * |----------------| key_tmp (existing cached range) 356*1d57628fSDongsheng Yang * |===========| key (requested range, overlapping the tail of key_tmp) 357*1d57628fSDongsheng Yang */ 358*1d57628fSDongsheng Yang static int read_overlap_tail(struct pcache_cache_key *key, struct pcache_cache_key *key_tmp, 359*1d57628fSDongsheng Yang struct pcache_cache_subtree_walk_ctx *ctx) 360*1d57628fSDongsheng Yang { 361*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 362*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 363*1d57628fSDongsheng Yang u32 io_len; 364*1d57628fSDongsheng Yang int ret; 365*1d57628fSDongsheng Yang 366*1d57628fSDongsheng Yang /* 367*1d57628fSDongsheng Yang * Calculate the length of the non-overlapping portion of `key` 368*1d57628fSDongsheng Yang * before `key_tmp`, representing the data missing in the cache. 369*1d57628fSDongsheng Yang */ 370*1d57628fSDongsheng Yang io_len = cache_key_lstart(key_tmp) - cache_key_lstart(key); 371*1d57628fSDongsheng Yang if (io_len) { 372*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 373*1d57628fSDongsheng Yang if (!backing_req) 374*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 375*1d57628fSDongsheng Yang 376*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, io_len, true); 377*1d57628fSDongsheng Yang 378*1d57628fSDongsheng Yang list_add(&backing_req->node, ctx->submit_req_list); 379*1d57628fSDongsheng Yang ctx->req_done += io_len; 380*1d57628fSDongsheng Yang cache_key_cutfront(key, io_len); 381*1d57628fSDongsheng Yang } 382*1d57628fSDongsheng Yang 383*1d57628fSDongsheng Yang /* 384*1d57628fSDongsheng Yang * Handle the overlapping portion by calculating the length of 385*1d57628fSDongsheng Yang * the remaining data in `key` that coincides with `key_tmp`. 386*1d57628fSDongsheng Yang */ 387*1d57628fSDongsheng Yang io_len = cache_key_lend(key) - cache_key_lstart(key_tmp); 388*1d57628fSDongsheng Yang if (cache_key_empty(key_tmp)) { 389*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 390*1d57628fSDongsheng Yang if (!backing_req) 391*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 392*1d57628fSDongsheng Yang 393*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, io_len, false); 394*1d57628fSDongsheng Yang submit_cache_miss_req(cache, backing_req); 395*1d57628fSDongsheng Yang } else { 396*1d57628fSDongsheng Yang ret = cache_copy_to_req_bio(ctx->cache_tree->cache, ctx->pcache_req, ctx->req_done, 397*1d57628fSDongsheng Yang io_len, &key_tmp->cache_pos, key_tmp->seg_gen); 398*1d57628fSDongsheng Yang if (ret) { 399*1d57628fSDongsheng Yang if (ret == -EINVAL) { 400*1d57628fSDongsheng Yang cache_key_delete(key_tmp); 401*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_RESEARCH; 402*1d57628fSDongsheng Yang } 403*1d57628fSDongsheng Yang 404*1d57628fSDongsheng Yang ctx->ret = ret; 405*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_ERR; 406*1d57628fSDongsheng Yang } 407*1d57628fSDongsheng Yang } 408*1d57628fSDongsheng Yang 409*1d57628fSDongsheng Yang ctx->req_done += io_len; 410*1d57628fSDongsheng Yang cache_key_cutfront(key, io_len); 411*1d57628fSDongsheng Yang 412*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_OK; 413*1d57628fSDongsheng Yang } 414*1d57628fSDongsheng Yang 415*1d57628fSDongsheng Yang /* 416*1d57628fSDongsheng Yang * |----| key_tmp (existing cached range) 417*1d57628fSDongsheng Yang * |==========| key (requested range) 418*1d57628fSDongsheng Yang */ 419*1d57628fSDongsheng Yang static int read_overlap_contain(struct pcache_cache_key *key, struct pcache_cache_key *key_tmp, 420*1d57628fSDongsheng Yang struct pcache_cache_subtree_walk_ctx *ctx) 421*1d57628fSDongsheng Yang { 422*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 423*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 424*1d57628fSDongsheng Yang u32 io_len; 425*1d57628fSDongsheng Yang int ret; 426*1d57628fSDongsheng Yang 427*1d57628fSDongsheng Yang /* 428*1d57628fSDongsheng Yang * Calculate the non-overlapping part of `key` before `key_tmp` 429*1d57628fSDongsheng Yang * to identify the missing data length. 430*1d57628fSDongsheng Yang */ 431*1d57628fSDongsheng Yang io_len = cache_key_lstart(key_tmp) - cache_key_lstart(key); 432*1d57628fSDongsheng Yang if (io_len) { 433*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 434*1d57628fSDongsheng Yang if (!backing_req) 435*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 436*1d57628fSDongsheng Yang 437*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, io_len, true); 438*1d57628fSDongsheng Yang 439*1d57628fSDongsheng Yang list_add(&backing_req->node, ctx->submit_req_list); 440*1d57628fSDongsheng Yang 441*1d57628fSDongsheng Yang ctx->req_done += io_len; 442*1d57628fSDongsheng Yang cache_key_cutfront(key, io_len); 443*1d57628fSDongsheng Yang } 444*1d57628fSDongsheng Yang 445*1d57628fSDongsheng Yang /* 446*1d57628fSDongsheng Yang * Handle the overlapping portion between `key` and `key_tmp`. 447*1d57628fSDongsheng Yang */ 448*1d57628fSDongsheng Yang io_len = key_tmp->len; 449*1d57628fSDongsheng Yang if (cache_key_empty(key_tmp)) { 450*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 451*1d57628fSDongsheng Yang if (!backing_req) 452*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 453*1d57628fSDongsheng Yang 454*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, io_len, false); 455*1d57628fSDongsheng Yang submit_cache_miss_req(cache, backing_req); 456*1d57628fSDongsheng Yang } else { 457*1d57628fSDongsheng Yang ret = cache_copy_to_req_bio(ctx->cache_tree->cache, ctx->pcache_req, ctx->req_done, 458*1d57628fSDongsheng Yang io_len, &key_tmp->cache_pos, key_tmp->seg_gen); 459*1d57628fSDongsheng Yang if (ret) { 460*1d57628fSDongsheng Yang if (ret == -EINVAL) { 461*1d57628fSDongsheng Yang cache_key_delete(key_tmp); 462*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_RESEARCH; 463*1d57628fSDongsheng Yang } 464*1d57628fSDongsheng Yang 465*1d57628fSDongsheng Yang ctx->ret = ret; 466*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_ERR; 467*1d57628fSDongsheng Yang } 468*1d57628fSDongsheng Yang } 469*1d57628fSDongsheng Yang 470*1d57628fSDongsheng Yang ctx->req_done += io_len; 471*1d57628fSDongsheng Yang cache_key_cutfront(key, io_len); 472*1d57628fSDongsheng Yang 473*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_OK; 474*1d57628fSDongsheng Yang } 475*1d57628fSDongsheng Yang 476*1d57628fSDongsheng Yang /* 477*1d57628fSDongsheng Yang * |-----------| key_tmp (existing cached range) 478*1d57628fSDongsheng Yang * |====| key (requested range, fully within key_tmp) 479*1d57628fSDongsheng Yang * 480*1d57628fSDongsheng Yang * If `key_tmp` contains valid cached data, this function copies the relevant 481*1d57628fSDongsheng Yang * portion to the request's bio. Otherwise, it sends a backing request to 482*1d57628fSDongsheng Yang * fetch the required data range. 483*1d57628fSDongsheng Yang */ 484*1d57628fSDongsheng Yang static int read_overlap_contained(struct pcache_cache_key *key, struct pcache_cache_key *key_tmp, 485*1d57628fSDongsheng Yang struct pcache_cache_subtree_walk_ctx *ctx) 486*1d57628fSDongsheng Yang { 487*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 488*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 489*1d57628fSDongsheng Yang struct pcache_cache_pos pos; 490*1d57628fSDongsheng Yang int ret; 491*1d57628fSDongsheng Yang 492*1d57628fSDongsheng Yang /* 493*1d57628fSDongsheng Yang * Check if `key_tmp` is empty, indicating a miss. If so, initiate 494*1d57628fSDongsheng Yang * a backing request to fetch the required data for `key`. 495*1d57628fSDongsheng Yang */ 496*1d57628fSDongsheng Yang if (cache_key_empty(key_tmp)) { 497*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 498*1d57628fSDongsheng Yang if (!backing_req) 499*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 500*1d57628fSDongsheng Yang 501*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, key->len, false); 502*1d57628fSDongsheng Yang submit_cache_miss_req(cache, backing_req); 503*1d57628fSDongsheng Yang } else { 504*1d57628fSDongsheng Yang cache_pos_copy(&pos, &key_tmp->cache_pos); 505*1d57628fSDongsheng Yang cache_pos_advance(&pos, cache_key_lstart(key) - cache_key_lstart(key_tmp)); 506*1d57628fSDongsheng Yang 507*1d57628fSDongsheng Yang ret = cache_copy_to_req_bio(ctx->cache_tree->cache, ctx->pcache_req, ctx->req_done, 508*1d57628fSDongsheng Yang key->len, &pos, key_tmp->seg_gen); 509*1d57628fSDongsheng Yang if (ret) { 510*1d57628fSDongsheng Yang if (ret == -EINVAL) { 511*1d57628fSDongsheng Yang cache_key_delete(key_tmp); 512*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_RESEARCH; 513*1d57628fSDongsheng Yang } 514*1d57628fSDongsheng Yang 515*1d57628fSDongsheng Yang ctx->ret = ret; 516*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_ERR; 517*1d57628fSDongsheng Yang } 518*1d57628fSDongsheng Yang } 519*1d57628fSDongsheng Yang 520*1d57628fSDongsheng Yang ctx->req_done += key->len; 521*1d57628fSDongsheng Yang cache_key_cutfront(key, key->len); 522*1d57628fSDongsheng Yang 523*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_OK; 524*1d57628fSDongsheng Yang } 525*1d57628fSDongsheng Yang 526*1d57628fSDongsheng Yang /* 527*1d57628fSDongsheng Yang * |--------| key_tmp (existing cached range) 528*1d57628fSDongsheng Yang * |==========| key (requested range, overlapping the head of key_tmp) 529*1d57628fSDongsheng Yang */ 530*1d57628fSDongsheng Yang static int read_overlap_head(struct pcache_cache_key *key, struct pcache_cache_key *key_tmp, 531*1d57628fSDongsheng Yang struct pcache_cache_subtree_walk_ctx *ctx) 532*1d57628fSDongsheng Yang { 533*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 534*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req; 535*1d57628fSDongsheng Yang struct pcache_cache_pos pos; 536*1d57628fSDongsheng Yang u32 io_len; 537*1d57628fSDongsheng Yang int ret; 538*1d57628fSDongsheng Yang 539*1d57628fSDongsheng Yang io_len = cache_key_lend(key_tmp) - cache_key_lstart(key); 540*1d57628fSDongsheng Yang 541*1d57628fSDongsheng Yang if (cache_key_empty(key_tmp)) { 542*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 543*1d57628fSDongsheng Yang if (!backing_req) 544*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 545*1d57628fSDongsheng Yang 546*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, io_len, false); 547*1d57628fSDongsheng Yang submit_cache_miss_req(cache, backing_req); 548*1d57628fSDongsheng Yang } else { 549*1d57628fSDongsheng Yang cache_pos_copy(&pos, &key_tmp->cache_pos); 550*1d57628fSDongsheng Yang cache_pos_advance(&pos, cache_key_lstart(key) - cache_key_lstart(key_tmp)); 551*1d57628fSDongsheng Yang 552*1d57628fSDongsheng Yang ret = cache_copy_to_req_bio(ctx->cache_tree->cache, ctx->pcache_req, ctx->req_done, 553*1d57628fSDongsheng Yang io_len, &pos, key_tmp->seg_gen); 554*1d57628fSDongsheng Yang if (ret) { 555*1d57628fSDongsheng Yang if (ret == -EINVAL) { 556*1d57628fSDongsheng Yang cache_key_delete(key_tmp); 557*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_RESEARCH; 558*1d57628fSDongsheng Yang } 559*1d57628fSDongsheng Yang 560*1d57628fSDongsheng Yang ctx->ret = ret; 561*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_ERR; 562*1d57628fSDongsheng Yang } 563*1d57628fSDongsheng Yang } 564*1d57628fSDongsheng Yang 565*1d57628fSDongsheng Yang ctx->req_done += io_len; 566*1d57628fSDongsheng Yang cache_key_cutfront(key, io_len); 567*1d57628fSDongsheng Yang 568*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_OK; 569*1d57628fSDongsheng Yang } 570*1d57628fSDongsheng Yang 571*1d57628fSDongsheng Yang /** 572*1d57628fSDongsheng Yang * read_walk_finally - Finalizes the cache read tree walk by submitting any 573*1d57628fSDongsheng Yang * remaining backing requests 574*1d57628fSDongsheng Yang * @ctx: Context structure holding information about the cache, 575*1d57628fSDongsheng Yang * read request, and submission list 576*1d57628fSDongsheng Yang * @ret: the return value after this walk. 577*1d57628fSDongsheng Yang * 578*1d57628fSDongsheng Yang * This function is called at the end of the `cache_subtree_walk` during a 579*1d57628fSDongsheng Yang * cache read operation. It completes the walk by checking if any data 580*1d57628fSDongsheng Yang * requested by `key` was not found in the cache tree, and if so, it sends 581*1d57628fSDongsheng Yang * a backing request to retrieve that data. Then, it iterates through the 582*1d57628fSDongsheng Yang * submission list of backing requests created during the walk, removing 583*1d57628fSDongsheng Yang * each request from the list and submitting it. 584*1d57628fSDongsheng Yang * 585*1d57628fSDongsheng Yang * The scenario managed here includes: 586*1d57628fSDongsheng Yang * - Sending a backing request for the remaining length of `key` if it was 587*1d57628fSDongsheng Yang * not fulfilled by existing cache entries. 588*1d57628fSDongsheng Yang * - Iterating through `ctx->submit_req_list` to submit each backing request 589*1d57628fSDongsheng Yang * enqueued during the walk. 590*1d57628fSDongsheng Yang * 591*1d57628fSDongsheng Yang * This ensures all necessary backing requests for cache misses are submitted 592*1d57628fSDongsheng Yang * to the backing storage to retrieve any data that could not be found in 593*1d57628fSDongsheng Yang * the cache. 594*1d57628fSDongsheng Yang */ 595*1d57628fSDongsheng Yang static int read_walk_finally(struct pcache_cache_subtree_walk_ctx *ctx, int ret) 596*1d57628fSDongsheng Yang { 597*1d57628fSDongsheng Yang struct pcache_cache *cache = ctx->cache_tree->cache; 598*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req, *next_req; 599*1d57628fSDongsheng Yang struct pcache_cache_key *key = ctx->key; 600*1d57628fSDongsheng Yang 601*1d57628fSDongsheng Yang list_for_each_entry_safe(backing_req, next_req, ctx->submit_req_list, node) { 602*1d57628fSDongsheng Yang list_del_init(&backing_req->node); 603*1d57628fSDongsheng Yang submit_cache_miss_req(ctx->cache_tree->cache, backing_req); 604*1d57628fSDongsheng Yang } 605*1d57628fSDongsheng Yang 606*1d57628fSDongsheng Yang if (ret != SUBTREE_WALK_RET_OK) 607*1d57628fSDongsheng Yang return ret; 608*1d57628fSDongsheng Yang 609*1d57628fSDongsheng Yang if (key->len) { 610*1d57628fSDongsheng Yang backing_req = get_pre_alloc_req(ctx); 611*1d57628fSDongsheng Yang if (!backing_req) 612*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_NEED_REQ; 613*1d57628fSDongsheng Yang 614*1d57628fSDongsheng Yang cache_miss_req_init(cache, backing_req, ctx->pcache_req, ctx->req_done, key->len, true); 615*1d57628fSDongsheng Yang submit_cache_miss_req(cache, backing_req); 616*1d57628fSDongsheng Yang ctx->req_done += key->len; 617*1d57628fSDongsheng Yang } 618*1d57628fSDongsheng Yang 619*1d57628fSDongsheng Yang return SUBTREE_WALK_RET_OK; 620*1d57628fSDongsheng Yang } 621*1d57628fSDongsheng Yang 622*1d57628fSDongsheng Yang /* 623*1d57628fSDongsheng Yang * This function is used within `cache_subtree_walk` to determine whether the 624*1d57628fSDongsheng Yang * read operation has covered the requested data length. It compares the 625*1d57628fSDongsheng Yang * amount of data processed (`ctx->req_done`) with the total data length 626*1d57628fSDongsheng Yang * specified in the original request (`ctx->pcache_req->data_len`). 627*1d57628fSDongsheng Yang * 628*1d57628fSDongsheng Yang * If `req_done` meets or exceeds the required data length, the function 629*1d57628fSDongsheng Yang * returns `true`, indicating the walk is complete. Otherwise, it returns `false`, 630*1d57628fSDongsheng Yang * signaling that additional data processing is needed to fulfill the request. 631*1d57628fSDongsheng Yang */ 632*1d57628fSDongsheng Yang static bool read_walk_done(struct pcache_cache_subtree_walk_ctx *ctx) 633*1d57628fSDongsheng Yang { 634*1d57628fSDongsheng Yang return (ctx->req_done >= ctx->pcache_req->data_len); 635*1d57628fSDongsheng Yang } 636*1d57628fSDongsheng Yang 637*1d57628fSDongsheng Yang /** 638*1d57628fSDongsheng Yang * cache_read - Process a read request by traversing the cache tree 639*1d57628fSDongsheng Yang * @cache: Cache structure holding cache trees and related configurations 640*1d57628fSDongsheng Yang * @pcache_req: Request structure with information about the data to read 641*1d57628fSDongsheng Yang * 642*1d57628fSDongsheng Yang * This function attempts to fulfill a read request by traversing the cache tree(s) 643*1d57628fSDongsheng Yang * to locate cached data for the requested range. If parts of the data are missing 644*1d57628fSDongsheng Yang * in the cache, backing requests are generated to retrieve the required segments. 645*1d57628fSDongsheng Yang * 646*1d57628fSDongsheng Yang * The function operates by initializing a key for the requested data range and 647*1d57628fSDongsheng Yang * preparing a context (`walk_ctx`) to manage the cache tree traversal. The context 648*1d57628fSDongsheng Yang * includes pointers to functions (e.g., `read_before`, `read_overlap_tail`) that handle 649*1d57628fSDongsheng Yang * specific conditions encountered during the traversal. The `walk_finally` and `walk_done` 650*1d57628fSDongsheng Yang * functions manage the end stages of the traversal, while the `delete_key_list` and 651*1d57628fSDongsheng Yang * `submit_req_list` lists track any keys to be deleted or requests to be submitted. 652*1d57628fSDongsheng Yang * 653*1d57628fSDongsheng Yang * The function first calculates the requested range and checks if it fits within the 654*1d57628fSDongsheng Yang * current cache tree (based on the tree's size limits). It then locks the cache tree 655*1d57628fSDongsheng Yang * and performs a search to locate any matching keys. If there are outdated keys, 656*1d57628fSDongsheng Yang * these are deleted, and the search is restarted to ensure accurate data retrieval. 657*1d57628fSDongsheng Yang * 658*1d57628fSDongsheng Yang * If the requested range spans multiple cache trees, the function moves on to the 659*1d57628fSDongsheng Yang * next tree once the current range has been processed. This continues until the 660*1d57628fSDongsheng Yang * entire requested data length has been handled. 661*1d57628fSDongsheng Yang */ 662*1d57628fSDongsheng Yang static int cache_read(struct pcache_cache *cache, struct pcache_request *pcache_req) 663*1d57628fSDongsheng Yang { 664*1d57628fSDongsheng Yang struct pcache_cache_key key_data = { .off = pcache_req->off, .len = pcache_req->data_len }; 665*1d57628fSDongsheng Yang struct pcache_cache_subtree *cache_subtree; 666*1d57628fSDongsheng Yang struct pcache_cache_key *key_tmp = NULL, *key_next; 667*1d57628fSDongsheng Yang struct rb_node *prev_node = NULL; 668*1d57628fSDongsheng Yang struct pcache_cache_key *key = &key_data; 669*1d57628fSDongsheng Yang struct pcache_cache_subtree_walk_ctx walk_ctx = { 0 }; 670*1d57628fSDongsheng Yang struct pcache_backing_dev_req *backing_req, *next_req; 671*1d57628fSDongsheng Yang LIST_HEAD(delete_key_list); 672*1d57628fSDongsheng Yang LIST_HEAD(submit_req_list); 673*1d57628fSDongsheng Yang int ret; 674*1d57628fSDongsheng Yang 675*1d57628fSDongsheng Yang walk_ctx.cache_tree = &cache->req_key_tree; 676*1d57628fSDongsheng Yang walk_ctx.req_done = 0; 677*1d57628fSDongsheng Yang walk_ctx.pcache_req = pcache_req; 678*1d57628fSDongsheng Yang walk_ctx.before = read_before; 679*1d57628fSDongsheng Yang walk_ctx.overlap_tail = read_overlap_tail; 680*1d57628fSDongsheng Yang walk_ctx.overlap_head = read_overlap_head; 681*1d57628fSDongsheng Yang walk_ctx.overlap_contain = read_overlap_contain; 682*1d57628fSDongsheng Yang walk_ctx.overlap_contained = read_overlap_contained; 683*1d57628fSDongsheng Yang walk_ctx.walk_finally = read_walk_finally; 684*1d57628fSDongsheng Yang walk_ctx.walk_done = read_walk_done; 685*1d57628fSDongsheng Yang walk_ctx.delete_key_list = &delete_key_list; 686*1d57628fSDongsheng Yang walk_ctx.submit_req_list = &submit_req_list; 687*1d57628fSDongsheng Yang 688*1d57628fSDongsheng Yang next: 689*1d57628fSDongsheng Yang key->off = pcache_req->off + walk_ctx.req_done; 690*1d57628fSDongsheng Yang key->len = pcache_req->data_len - walk_ctx.req_done; 691*1d57628fSDongsheng Yang if (key->len > PCACHE_CACHE_SUBTREE_SIZE - (key->off & PCACHE_CACHE_SUBTREE_SIZE_MASK)) 692*1d57628fSDongsheng Yang key->len = PCACHE_CACHE_SUBTREE_SIZE - (key->off & PCACHE_CACHE_SUBTREE_SIZE_MASK); 693*1d57628fSDongsheng Yang 694*1d57628fSDongsheng Yang cache_subtree = get_subtree(&cache->req_key_tree, key->off); 695*1d57628fSDongsheng Yang spin_lock(&cache_subtree->tree_lock); 696*1d57628fSDongsheng Yang search: 697*1d57628fSDongsheng Yang prev_node = cache_subtree_search(cache_subtree, key, NULL, NULL, &delete_key_list); 698*1d57628fSDongsheng Yang if (!list_empty(&delete_key_list)) { 699*1d57628fSDongsheng Yang list_for_each_entry_safe(key_tmp, key_next, &delete_key_list, list_node) { 700*1d57628fSDongsheng Yang list_del_init(&key_tmp->list_node); 701*1d57628fSDongsheng Yang cache_key_delete(key_tmp); 702*1d57628fSDongsheng Yang } 703*1d57628fSDongsheng Yang goto search; 704*1d57628fSDongsheng Yang } 705*1d57628fSDongsheng Yang 706*1d57628fSDongsheng Yang walk_ctx.start_node = prev_node; 707*1d57628fSDongsheng Yang walk_ctx.key = key; 708*1d57628fSDongsheng Yang 709*1d57628fSDongsheng Yang ret = cache_subtree_walk(&walk_ctx); 710*1d57628fSDongsheng Yang if (ret == SUBTREE_WALK_RET_RESEARCH) 711*1d57628fSDongsheng Yang goto search; 712*1d57628fSDongsheng Yang spin_unlock(&cache_subtree->tree_lock); 713*1d57628fSDongsheng Yang 714*1d57628fSDongsheng Yang if (ret == SUBTREE_WALK_RET_ERR) { 715*1d57628fSDongsheng Yang ret = walk_ctx.ret; 716*1d57628fSDongsheng Yang goto out; 717*1d57628fSDongsheng Yang } 718*1d57628fSDongsheng Yang 719*1d57628fSDongsheng Yang if (ret == SUBTREE_WALK_RET_NEED_REQ) { 720*1d57628fSDongsheng Yang walk_ctx.pre_alloc_req = cache_miss_req_alloc(cache, pcache_req, GFP_NOIO); 721*1d57628fSDongsheng Yang pcache_dev_debug(CACHE_TO_PCACHE(cache), "allocate pre_alloc_req with GFP_NOIO"); 722*1d57628fSDongsheng Yang } 723*1d57628fSDongsheng Yang 724*1d57628fSDongsheng Yang if (walk_ctx.req_done < pcache_req->data_len) 725*1d57628fSDongsheng Yang goto next; 726*1d57628fSDongsheng Yang ret = 0; 727*1d57628fSDongsheng Yang out: 728*1d57628fSDongsheng Yang if (walk_ctx.pre_alloc_req) 729*1d57628fSDongsheng Yang cache_miss_req_free(walk_ctx.pre_alloc_req); 730*1d57628fSDongsheng Yang 731*1d57628fSDongsheng Yang list_for_each_entry_safe(backing_req, next_req, &submit_req_list, node) { 732*1d57628fSDongsheng Yang list_del_init(&backing_req->node); 733*1d57628fSDongsheng Yang backing_dev_req_end(backing_req); 734*1d57628fSDongsheng Yang } 735*1d57628fSDongsheng Yang 736*1d57628fSDongsheng Yang return ret; 737*1d57628fSDongsheng Yang } 738*1d57628fSDongsheng Yang 739*1d57628fSDongsheng Yang static int cache_write(struct pcache_cache *cache, struct pcache_request *pcache_req) 740*1d57628fSDongsheng Yang { 741*1d57628fSDongsheng Yang struct pcache_cache_subtree *cache_subtree; 742*1d57628fSDongsheng Yang struct pcache_cache_key *key; 743*1d57628fSDongsheng Yang u64 offset = pcache_req->off; 744*1d57628fSDongsheng Yang u32 length = pcache_req->data_len; 745*1d57628fSDongsheng Yang u32 io_done = 0; 746*1d57628fSDongsheng Yang int ret; 747*1d57628fSDongsheng Yang 748*1d57628fSDongsheng Yang while (true) { 749*1d57628fSDongsheng Yang if (io_done >= length) 750*1d57628fSDongsheng Yang break; 751*1d57628fSDongsheng Yang 752*1d57628fSDongsheng Yang key = cache_key_alloc(&cache->req_key_tree, GFP_NOIO); 753*1d57628fSDongsheng Yang key->off = offset + io_done; 754*1d57628fSDongsheng Yang key->len = length - io_done; 755*1d57628fSDongsheng Yang if (key->len > PCACHE_CACHE_SUBTREE_SIZE - (key->off & PCACHE_CACHE_SUBTREE_SIZE_MASK)) 756*1d57628fSDongsheng Yang key->len = PCACHE_CACHE_SUBTREE_SIZE - (key->off & PCACHE_CACHE_SUBTREE_SIZE_MASK); 757*1d57628fSDongsheng Yang 758*1d57628fSDongsheng Yang ret = cache_data_alloc(cache, key); 759*1d57628fSDongsheng Yang if (ret) { 760*1d57628fSDongsheng Yang cache_key_put(key); 761*1d57628fSDongsheng Yang goto err; 762*1d57628fSDongsheng Yang } 763*1d57628fSDongsheng Yang 764*1d57628fSDongsheng Yang ret = cache_copy_from_req_bio(cache, key, pcache_req, io_done); 765*1d57628fSDongsheng Yang if (ret) { 766*1d57628fSDongsheng Yang cache_seg_put(key->cache_pos.cache_seg); 767*1d57628fSDongsheng Yang cache_key_put(key); 768*1d57628fSDongsheng Yang goto err; 769*1d57628fSDongsheng Yang } 770*1d57628fSDongsheng Yang 771*1d57628fSDongsheng Yang cache_subtree = get_subtree(&cache->req_key_tree, key->off); 772*1d57628fSDongsheng Yang spin_lock(&cache_subtree->tree_lock); 773*1d57628fSDongsheng Yang cache_key_insert(&cache->req_key_tree, key, true); 774*1d57628fSDongsheng Yang ret = cache_key_append(cache, key, pcache_req->bio->bi_opf & REQ_FUA); 775*1d57628fSDongsheng Yang if (ret) { 776*1d57628fSDongsheng Yang cache_seg_put(key->cache_pos.cache_seg); 777*1d57628fSDongsheng Yang cache_key_delete(key); 778*1d57628fSDongsheng Yang goto unlock; 779*1d57628fSDongsheng Yang } 780*1d57628fSDongsheng Yang 781*1d57628fSDongsheng Yang io_done += key->len; 782*1d57628fSDongsheng Yang spin_unlock(&cache_subtree->tree_lock); 783*1d57628fSDongsheng Yang } 784*1d57628fSDongsheng Yang 785*1d57628fSDongsheng Yang return 0; 786*1d57628fSDongsheng Yang unlock: 787*1d57628fSDongsheng Yang spin_unlock(&cache_subtree->tree_lock); 788*1d57628fSDongsheng Yang err: 789*1d57628fSDongsheng Yang return ret; 790*1d57628fSDongsheng Yang } 791*1d57628fSDongsheng Yang 792*1d57628fSDongsheng Yang /** 793*1d57628fSDongsheng Yang * cache_flush - Flush all ksets to persist any pending cache data 794*1d57628fSDongsheng Yang * @cache: Pointer to the cache structure 795*1d57628fSDongsheng Yang * 796*1d57628fSDongsheng Yang * This function iterates through all ksets associated with the provided `cache` 797*1d57628fSDongsheng Yang * and ensures that any data marked for persistence is written to media. For each 798*1d57628fSDongsheng Yang * kset, it acquires the kset lock, then invokes `cache_kset_close`, which handles 799*1d57628fSDongsheng Yang * the persistence logic for that kset. 800*1d57628fSDongsheng Yang * 801*1d57628fSDongsheng Yang * If `cache_kset_close` encounters an error, the function exits immediately with 802*1d57628fSDongsheng Yang * the respective error code, preventing the flush operation from proceeding to 803*1d57628fSDongsheng Yang * subsequent ksets. 804*1d57628fSDongsheng Yang */ 805*1d57628fSDongsheng Yang int cache_flush(struct pcache_cache *cache) 806*1d57628fSDongsheng Yang { 807*1d57628fSDongsheng Yang struct pcache_cache_kset *kset; 808*1d57628fSDongsheng Yang u32 i, ret; 809*1d57628fSDongsheng Yang 810*1d57628fSDongsheng Yang for (i = 0; i < cache->n_ksets; i++) { 811*1d57628fSDongsheng Yang kset = get_kset(cache, i); 812*1d57628fSDongsheng Yang 813*1d57628fSDongsheng Yang spin_lock(&kset->kset_lock); 814*1d57628fSDongsheng Yang ret = cache_kset_close(cache, kset); 815*1d57628fSDongsheng Yang spin_unlock(&kset->kset_lock); 816*1d57628fSDongsheng Yang 817*1d57628fSDongsheng Yang if (ret) 818*1d57628fSDongsheng Yang return ret; 819*1d57628fSDongsheng Yang } 820*1d57628fSDongsheng Yang 821*1d57628fSDongsheng Yang return 0; 822*1d57628fSDongsheng Yang } 823*1d57628fSDongsheng Yang 824*1d57628fSDongsheng Yang int pcache_cache_handle_req(struct pcache_cache *cache, struct pcache_request *pcache_req) 825*1d57628fSDongsheng Yang { 826*1d57628fSDongsheng Yang struct bio *bio = pcache_req->bio; 827*1d57628fSDongsheng Yang 828*1d57628fSDongsheng Yang if (unlikely(bio->bi_opf & REQ_PREFLUSH)) 829*1d57628fSDongsheng Yang return cache_flush(cache); 830*1d57628fSDongsheng Yang 831*1d57628fSDongsheng Yang if (bio_data_dir(bio) == READ) 832*1d57628fSDongsheng Yang return cache_read(cache, pcache_req); 833*1d57628fSDongsheng Yang 834*1d57628fSDongsheng Yang return cache_write(cache, pcache_req); 835*1d57628fSDongsheng Yang } 836