Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB0BD36C9E5 for ; Fri, 24 Apr 2026 14:05:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777039544; cv=none; b=tGTIdaRoJZd9/xQxOH60N1sMKZWcEECdOdTD95EIoPWltPof5Ka3YjkuZHh0A+84QFW0OzfPJvKZuHBTU8FhFZHSFus9Y2zZeUaxlLpgGX3F4RUy3GTBCpRx+OYOu+cfrUc5shABMj/EjmJAoyVOsrr51OX78wFillfaZa3VgjU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777039544; c=relaxed/simple; bh=rIvSv9IUrm7xQjEGEvyRnm+rZMxFuAThUOH9tG631e0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gyCrENNImZyxFeVam2j9ou0KbjmCsZ0qaFpsutmePXkJqILpU1RRbo9o8VqadHpXaC4nHcZhUJBFvlbgKPlt91Df9xCpd0fNScCjxXfwIXGFs2UhMVAiYQaiHEa3+AXZehAj3n3vRN8+gWBBW5e3u29ziewNeiZoio39l/MOk3Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=permerror header.from=versity.com; spf=pass smtp.mailfrom=versity.com; dkim=pass (2048-bit key) header.d=versity.com header.i=@versity.com header.b=DuQaBtPY; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=permerror header.from=versity.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=versity.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=versity.com header.i=@versity.com header.b="DuQaBtPY" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-ba6838e76f2so759177866b.2 for ; Fri, 24 Apr 2026 07:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=versity.com; s=google; t=1777039541; x=1777644341; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=lWbkxVc3AoUtnWvCJ+KOeY3kaEK2eHTtU5o3WRuvNqA=; b=DuQaBtPYt5BfAA4f5kJdB+A2d2rgfEzS35vkRLfwbkq6Dq5vKKKdI8LV30jNkIJySp HLegDL6vv/Ilpz6Eg98FncDUhGsDyedUknFAs+T5ttZN1iNkba6m3Q5QTaZq5Ba8+94l OHLLdOLkbs6DC3kAdIOiKUX8VqnrWYdbnPA4EzzXwP0Hycb+nT+mXVVli63WbSF0ns6q KKQ6wkeIISjg0zYL3PigrKq/oJH/KCfrekTmniyWGKT/mh6eyZ020eyXAF2sJioC/fGI PLd71JsrvM1FRBFvbG4uPR+tm+eiSmR5CqDKG9StbILqeEzIz3/MexhUGE6+zHG2QKKd FaDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777039541; x=1777644341; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=lWbkxVc3AoUtnWvCJ+KOeY3kaEK2eHTtU5o3WRuvNqA=; b=r7GdJ/U/VQp8G7beYH2WoQWSJ6Awg/kDIyWgiZZCviH/64fxymLOtQRG2hr4G37VqV S/m6AlMdgkJJ6PheFghAMxWAEkru1BZghaKiHiLp4F7TCseT84sb7m/qkIpzW5V2QHQ7 r8uIb8/Uf3FpfPU8yZcubFV6tArP96otO525oVbepJyK50d2QQnS+/APmtxZGuyBrRWg UxfEXVojIBo+h1dX7yANYK8GzvCMMyMbd4k3rU+WuiiQaXHXNLRUdOa8gO5nPgPihjFM MMWwIv7jpzJoKs7y8oAhECJTsZOfakBcChy55atNFceZJDVgHjF4oKoDTtStXploJNLW k8Tw== X-Gm-Message-State: AOJu0YytoEQte3ZDfscR0VKVaZmt4yQLFCxOCbWfCQJiddExsfIy3fME s0g2P99NFUv3pBu+KUgbU5S4TAYlHodx1uPyO7H5EC1dmSdDYe5wS1eJKMwEMw5gGJKkX2ll8ur sNymk X-Gm-Gg: AeBDietSMPhM06cfqX/jzjkn76lN0Y6xgWi39KyrM+NSTeaXA7UZJAJmY7C/jT/sL5o CovGRaHPYmoD6+X4wdraHL+oSrF5iMzPRXhfbAqP1QOvYpemNeGvPj9J/OW36ZfpKL8hR22dVAs FfEHSSk0BByr+pb6BVtVHiaADozo687gAEaG9YOs4kCNeQQRGSVdUYxYDAezxgqLMYwScS04xDI JBc0Edif5M2X54nOwQn5j/SQo/1wajDn79IqTbaxCibfYZ3S51Bnj5YgEGPqQERAE8UnWglm+qU rNbhg+P6e5sPAs1jdA/PUN1iPUN/5jpi1iU3o1B3rfZfKQMOsz/VqNRir9AmMME4XKFobviOhhk Lvc++oGW6IxyI33c7f1cZi9dRNFcEaHQcdkdN6lHhoLtttIvoVmWrtj9NuGxPuDKvufnrQ4ZSao Dx7LMIACNCJIoIxhnUTcarxMou/Nzwy6T1VnR77SvmLySNjFs9564/do381h3VZgtlip4lh48tn ytxaUyjkhAr6shIlR9jtXah5MF5cMZjfiSCqg== X-Received: by 2002:a17:907:9446:b0:ba5:a3a0:4a5f with SMTP id a640c23a62f3a-ba5a3a05c33mr1361178966b.8.1777039538873; Fri, 24 Apr 2026 07:05:38 -0700 (PDT) Received: from localhost.localdomain (46-117-212-87.ftth.glasoperator.nl. [87.212.117.46]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ba454d1bbb1sm766348966b.38.2026.04.24.07.05.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 07:05:36 -0700 (PDT) From: Valerie Aurora To: rpdfs-devel@lists.linux.dev Subject: [PATCH 6/8] rpdfs: add file data allocation and lookup routines Date: Fri, 24 Apr 2026 16:05:18 +0200 Message-ID: <20260424140520.3443624-7-val@versity.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260424140520.3443624-1-val@versity.com> References: <20260424140520.3443624-1-val@versity.com> Precedence: bulk X-Mailing-List: rpdfs-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add routines to lookup and allocate file data and mapping blocks. Signed-off-by: Valerie Aurora --- fs/rpdfs/data.c | 220 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) diff --git a/fs/rpdfs/data.c b/fs/rpdfs/data.c index b7538004d14a..3a6e9560b32f 100644 --- a/fs/rpdfs/data.c +++ b/fs/rpdfs/data.c @@ -88,3 +88,223 @@ void rpdfs_data_root_init(struct rpdfs_data_root *data) data->ref.bnr = 0; data->ref.alloc_counter = 0; } + +static int alloc_block_ref(struct rpdfs_fs_info *rfi, struct rpdfs_transaction *txn, + struct rpdfs_block_ref *ref, struct rpdfs_block_handle **blk_hnd) +{ + int ret; + + ret = rpdfs_txn_acquire_alloc(rfi, txn, blk_hnd); + if (ret < 0) + goto out; + + memset((*blk_hnd)->data, 0, RPDFS_BLOCK_SIZE); + + ref->bnr = cpu_to_le64((*blk_hnd)->bnr); + ref->alloc_counter = 0; /* XXX */ +out: + rpdfs_prd("ret %d bnr %llu", ret, ref->bnr); + return ret; +} + +static int grow_height(struct rpdfs_fs_info *rfi, struct rpdfs_transaction *txn, + struct inode *inode, struct rpdfs_block_handle *inode_hnd, int height) +{ + struct rpdfs_inode_info *ri = RPDFS_I(inode); + struct rpdfs_block_handle *hnd = NULL; + struct rpdfs_block_ref *ref; + struct rpdfs_map_block *iblk; + struct rpdfs_data_root *dr; + struct rpdfs_block_ref orig_root; + u8 level; + int ret; + + dr = &ri->data_root; + + rpdfs_prd("dr->height %u goal height %u", dr->height, height); + + ret = 0; + if (height > dr->height) { + /* start at root and fill in till we get to existing tree */ + level = height; + orig_root = dr->ref; /* save current root of tree */ + ref = &dr->ref; + + while (level-- > dr->height) { + rpdfs_prd("allocing block at level %d", level + 1); + hnd = NULL; + ret = alloc_block_ref(rfi, txn, &dr->ref, &hnd); + if (ret < 0) + goto out; + iblk = hnd->data; + /* growing height will always index old data to 0 */ + ref = &iblk->refs[0]; + rpdfs_block_release(rfi, &hnd); + } + rpdfs_prd("grafting old height %d tree to new %d height tree", + dr->height, height); + /* now put existing tree in mapping block */ + *ref = orig_root; + /* and update the height in the data root in the inode */ + ri->data_root.height = height; + } +out: + return ret; +} + +/* + * Return an array of block references in mapping blocks, beginning with + * the one containing the requested offset. If it is a write, allocate + * the mapping blocks for that file data offset if necessary. If it is a + * read for an unallocated but valid offset, return a null range. Actual + * data block allocation occurs in the caller. This is so we don't have + * to traverse the mapping blocks for every data block access. + * + * This function is only called after checking that a read is from a + * valid range of the file. + * + * XXX: Takes write access on map blocks on write even when it only + * needs to read them. Redescend with write when we know we have to + * allocate? + */ +static int get_or_alloc_map_blk(struct rpdfs_fs_info *rfi, struct rpdfs_transaction *txn, + struct inode *inode, struct rpdfs_block_handle *inode_hnd, + u64 lblk, rbaf_t rbaf, struct rpdfs_block_handle **hnd_ret, + struct rpdfs_block_ref **refs_ret, int *nr_ret) +{ + struct rpdfs_inode_info *ri = RPDFS_I(inode); + struct rpdfs_block_handle *parent_hnd; + struct rpdfs_block_handle *blk_hnd = NULL; + struct rpdfs_block_ref *refs; + struct rpdfs_map_block *iblk; + struct rpdfs_data_root *dr; + rbaf_t map_rbaf; + u8 min_height; + u8 level; + u8 ind; + u64 bnr; + int write; + int nr; + int ret; + + map_rbaf = rbaf & RBAF_WRITE ? RBAF_WRITE : 0; + write = rbaf & RBAF_WRITE ? 1 : 0; + + dr = &ri->data_root; + min_height = height_from_lblk(lblk); + + rpdfs_prd("ino %llu dr->height %u dr->ref.bnr %llu lblk %llu lblk height %u map_rbaf %x write %d", + ri->ig.ino, dr->height, dr->ref.bnr, lblk, min_height, map_rbaf, write); + + /* grow the height of existing data, if any */ + if ((min_height > 1) && + (min_height > dr->height)) { + ret = grow_height(rfi, txn, inode, inode_hnd, min_height); + if (ret < 0) + goto out; + } + + /* start with the root of the mapping tree in the inode */ + parent_hnd = inode_hnd; + refs = &dr->ref; + bnr = le64_to_cpu(dr->ref.bnr); + ind = 0; + nr = 1; + ret = 0; + + level = dr->height; + + /* allocate all map blocks but not the data block itself */ + while (level-- > 1) { + rpdfs_prd("level %d bnr %llu", level + 1, bnr); + + if (bnr == 0 && !write) { + rpdfs_prd("read of unallocated block"); + break; + } + + if (bnr) { + /* TODO: be smarter about needing write */ + ret = rpdfs_block_acquire(rfi, txn, bnr, &blk_hnd, map_rbaf); + if (ret < 0) + goto out; + } else { + + ret = alloc_block_ref(rfi, txn, refs, &blk_hnd); + if (ret < 0) + goto out; /* XXX do error/recovery now */ + } + + iblk = blk_hnd->data; + + /* look up next block reference */ + ind = calc_ref_ind(lblk, level); + bnr = le64_to_cpu(iblk->refs[ind].bnr); + + if (parent_hnd != inode_hnd) + rpdfs_block_release(rfi, &parent_hnd); + + parent_hnd = blk_hnd; + refs = &iblk->refs[ind]; + nr = RPDFS_DATA_REFS_PER_BLK - ind; + blk_hnd = NULL; + }; + + /* XXX make this a struct? */ + *hnd_ret = parent_hnd; + *refs_ret = refs; + *nr_ret = nr; + + if (dr->height < min_height) + dr->height = min_height; + +out: + rpdfs_prd("dr->height %u bnr %llu ind %d refs %p nr %d", + dr->height, *refs_ret ? (*refs_ret)[0].bnr : 0, ind, refs_ret, *nr_ret); + return ret; +} + +static int get_or_alloc_file_block(struct rpdfs_fs_info *rfi, struct rpdfs_transaction *txn, + struct inode *inode, struct rpdfs_block_handle *inode_hnd, + u64 lblk, rbaf_t rbaf, struct rpdfs_block_handle **hnd_ret) +{ + struct rpdfs_block_handle *parent_hnd = NULL; + struct rpdfs_block_handle *blk_hnd = NULL; + struct rpdfs_block_ref *refs; + int write; + int nr; + u64 bnr; + int ret; + + write = rbaf & RBAF_WRITE ? 1 : 0; + + rpdfs_prd("ino %llu lblk %llu rbaf %x", rpdfs_inode_ino(inode), lblk, rbaf); + + ret = get_or_alloc_map_blk(rfi, txn, inode, inode_hnd, lblk, rbaf, &parent_hnd, &refs, &nr); + if (ret < 0) + goto out; + + bnr = le64_to_cpu(refs[0].bnr); + + if (bnr == 0) { + /* read from/write to unallocated range */ + if (write) { + ret = alloc_block_ref(rfi, txn, refs, &blk_hnd); + if (ret < 0) + goto out; + } + } else { + ret = rpdfs_block_acquire(rfi, txn, bnr, &blk_hnd, rbaf); + if (ret < 0) + goto out; + } + + if (parent_hnd != inode_hnd) + rpdfs_block_release(rfi, &parent_hnd); + + *hnd_ret = blk_hnd; +out: + rpdfs_prd("ret %d ino %llu lblk %llu bnr %llu *hnd_ret %p", + ret, rpdfs_inode_ino(inode), lblk, bnr, *hnd_ret); + return ret; +} -- 2.49.0