From nobody Mon Nov 25 16:51:29 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 63C4921315F for ; Fri, 25 Oct 2024 20:43:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889016; cv=none; b=hplR4bd7xSTV+fqqb5ng4gnY7rlDMS7NWlz6MnkfVa95TrqfnsHeUi++5HEzYMBoyFNfBUL/ddUbxWjg8jIu4wpZnRhBg4BJCQJ3quZWKU+0mqdki4aw9emhER4GX8txRbQSOWqQVK0+uNvMehzTb6FZYhIjoz1y/aFmSYJq9Uc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729889016; c=relaxed/simple; bh=7ZySsHs1NU8PoQuSd+KdGRbzuCkvw3E5vKW3LI25o2o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RrhSyae2hSZ9brhdwKaSVQDFBDUteRfof1dG4IDtnhOfrs8yV5SPOSfSAK6YWiSvzC27kTh7c+beXgljrh7fXfai/rJa7ezUco77BbvMsUm3UqCuQ+W7hSlsPHHN8yqrBE2rVJqflP6mlxiBJTDju3SJxLNcEImYkXW58/GiCLw= 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=Xevvcx+t; arc=none smtp.client-ip=170.10.129.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="Xevvcx+t" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729889010; 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=vLbtrOh1733yY2WemQ2OwCF/jFd7ANzNknd3htBmCuM=; b=Xevvcx+te34n8EgK9SD1HgADTp4avtE731LrAP8RmTscpTFpeRZh1CmAZRJWKsV6munFL7 pH82L8oBCf4o6aAZTwUAUtqDjI9gR+vZs+lFNFsyqtWRTmH5ABkeuaZEkfk2q6mj9IcWZp iiQ03E/CTPfPnWJNJhtD4O05YzijgkE= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-319-m7HTlPNcOOCiAh8C74QeVg-1; Fri, 25 Oct 2024 16:43:27 -0400 X-MC-Unique: m7HTlPNcOOCiAh8C74QeVg-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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 mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3EAE41955D63; Fri, 25 Oct 2024 20:43:23 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.231]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 60AC51956088; Fri, 25 Oct 2024 20:43:18 +0000 (UTC) From: David Howells To: Christian Brauner , Steve French , Matthew Wilcox Cc: David Howells , Jeff Layton , Gao Xiang , Dominique Martinet , Marc Dionne , Paulo Alcantara , Shyam Prasad N , Tom Talpey , Eric Van Hensbergen , Ilya Dryomov , netfs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 28/31] afs: Use the contained hashtable to search a directory Date: Fri, 25 Oct 2024 21:39:55 +0100 Message-ID: <20241025204008.4076565-29-dhowells@redhat.com> In-Reply-To: <20241025204008.4076565-1-dhowells@redhat.com> References: <20241025204008.4076565-1-dhowells@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: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" Each directory image contains a hashtable with 128 buckets to speed up searching. Currently, kafs does not use this, but rather iterates over all the occupied slots in the image as it can share this with readdir. Switch kafs to use the hashtable for lookups to reduce the latency. Care must be taken that the hash chains are acyclic. Signed-off-by: David Howells cc: Marc Dionne cc: linux-afs@lists.infradead.org --- fs/afs/Makefile | 1 + fs/afs/dir.c | 42 ++++---- fs/afs/dir_edit.c | 133 +++++++++++++++++--------- fs/afs/dir_search.c | 227 ++++++++++++++++++++++++++++++++++++++++++++ fs/afs/internal.h | 18 ++++ 5 files changed, 348 insertions(+), 73 deletions(-) create mode 100644 fs/afs/dir_search.c diff --git a/fs/afs/Makefile b/fs/afs/Makefile index dcdc0f1bb76f..5efd7e13b304 100644 --- a/fs/afs/Makefile +++ b/fs/afs/Makefile @@ -11,6 +11,7 @@ kafs-y :=3D \ cmservice.o \ dir.o \ dir_edit.o \ + dir_search.o \ dir_silly.o \ dynroot.o \ file.o \ diff --git a/fs/afs/dir.c b/fs/afs/dir.c index aa589f0d55bf..6a4fc1cffb7e 100644 --- a/fs/afs/dir.c +++ b/fs/afs/dir.c @@ -88,8 +88,6 @@ struct afs_lookup_one_cookie { struct afs_lookup_cookie { struct dir_context ctx; struct qstr name; - bool found; - bool one_only; unsigned short nr_fids; struct afs_fid fids[50]; }; @@ -309,7 +307,7 @@ ssize_t afs_read_single(struct afs_vnode *dvnode, struc= t file *file) * Read the directory into a folio_queue buffer in one go, scrubbing the * previous contents. We return -ESTALE if the caller needs to call us ag= ain. */ -static ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file) +ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file) __acquires(&dvnode->validate_lock) { ssize_t ret; @@ -643,19 +641,10 @@ static bool afs_lookup_filldir(struct dir_context *ct= x, const char *name, BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) !=3D 2048); BUILD_BUG_ON(sizeof(union afs_xdr_dirent) !=3D 32); =20 - if (cookie->found) { - if (cookie->nr_fids < 50) { - cookie->fids[cookie->nr_fids].vnode =3D ino; - cookie->fids[cookie->nr_fids].unique =3D dtype; - cookie->nr_fids++; - } - } else if (cookie->name.len =3D=3D nlen && - memcmp(cookie->name.name, name, nlen) =3D=3D 0) { - cookie->fids[1].vnode =3D ino; - cookie->fids[1].unique =3D dtype; - cookie->found =3D 1; - if (cookie->one_only) - return false; + if (cookie->nr_fids < 50) { + cookie->fids[cookie->nr_fids].vnode =3D ino; + cookie->fids[cookie->nr_fids].unique =3D dtype; + cookie->nr_fids++; } =20 return cookie->nr_fids < 50; @@ -783,6 +772,7 @@ static struct inode *afs_do_lookup(struct inode *dir, s= truct dentry *dentry) struct afs_vnode *dvnode =3D AFS_FS_I(dir), *vnode; struct inode *inode =3D NULL, *ti; afs_dataversion_t data_version =3D READ_ONCE(dvnode->status.data_version); + bool supports_ibulk; long ret; int i; =20 @@ -799,19 +789,19 @@ static struct inode *afs_do_lookup(struct inode *dir,= struct dentry *dentry) cookie->nr_fids =3D 2; /* slot 1 is saved for the fid we actually want * and slot 0 for the directory */ =20 - if (!afs_server_supports_ibulk(dvnode)) - cookie->one_only =3D true; - - /* search the directory */ - ret =3D afs_dir_iterate(dir, &cookie->ctx, NULL, &data_version); + /* Search the directory for the named entry using the hash table... */ + ret =3D afs_dir_search(dvnode, &dentry->d_name, &cookie->fids[1], &data_v= ersion); if (ret < 0) goto out; =20 - dentry->d_fsdata =3D (void *)(unsigned long)data_version; + supports_ibulk =3D afs_server_supports_ibulk(dvnode); + if (supports_ibulk) { + /* ...then scan linearly from that point for entries to lookup-ahead. */ + cookie->ctx.pos =3D (ret + 1) * AFS_DIR_DIRENT_SIZE; + afs_dir_iterate(dir, &cookie->ctx, NULL, &data_version); + } =20 - ret =3D -ENOENT; - if (!cookie->found) - goto out; + dentry->d_fsdata =3D (void *)(unsigned long)data_version; =20 /* Check to see if we already have an inode for the primary fid. */ inode =3D ilookup5(dir->i_sb, cookie->fids[1].vnode, @@ -870,7 +860,7 @@ static struct inode *afs_do_lookup(struct inode *dir, s= truct dentry *dentry) * the whole operation. */ afs_op_set_error(op, -ENOTSUPP); - if (!cookie->one_only) { + if (supports_ibulk) { op->ops =3D &afs_inline_bulk_status_operation; afs_begin_vnode_operation(op); afs_wait_for_operation(op); diff --git a/fs/afs/dir_edit.c b/fs/afs/dir_edit.c index f4037973416e..c5c06f2b273f 100644 --- a/fs/afs/dir_edit.c +++ b/fs/afs/dir_edit.c @@ -245,7 +245,7 @@ void afs_edit_dir_add(struct afs_vnode *vnode, union afs_xdr_dir_block *meta, *block; union afs_xdr_dirent *de; struct afs_dir_iter iter =3D { .dvnode =3D vnode }; - unsigned int need_slots, nr_blocks, b; + unsigned int nr_blocks, b, entry; loff_t i_size; int slot; =20 @@ -263,7 +263,7 @@ void afs_edit_dir_add(struct afs_vnode *vnode, return; =20 /* Work out how many slots we're going to need. */ - need_slots =3D afs_dir_calc_slots(name->len); + iter.nr_slots =3D afs_dir_calc_slots(name->len); =20 if (i_size =3D=3D 0) goto new_directory; @@ -281,7 +281,7 @@ void afs_edit_dir_add(struct afs_vnode *vnode, =20 /* Lower dir blocks have a counter in the header we can check. */ if (b < AFS_DIR_BLOCKS_WITH_CTR && - meta->meta.alloc_ctrs[b] < need_slots) + meta->meta.alloc_ctrs[b] < iter.nr_slots) continue; =20 block =3D afs_dir_get_block(&iter, b); @@ -308,7 +308,7 @@ void afs_edit_dir_add(struct afs_vnode *vnode, /* We need to try and find one or more consecutive slots to * hold the entry. */ - slot =3D afs_find_contig_bits(block, need_slots); + slot =3D afs_find_contig_bits(block, iter.nr_slots); if (slot >=3D 0) { _debug("slot %u", slot); goto found_space; @@ -347,12 +347,18 @@ void afs_edit_dir_add(struct afs_vnode *vnode, de->u.name[name->len] =3D 0; =20 /* Adjust the bitmap. */ - afs_set_contig_bits(block, slot, need_slots); - kunmap_local(block); + afs_set_contig_bits(block, slot, iter.nr_slots); =20 /* Adjust the allocation counter. */ if (b < AFS_DIR_BLOCKS_WITH_CTR) - meta->meta.alloc_ctrs[b] -=3D need_slots; + meta->meta.alloc_ctrs[b] -=3D iter.nr_slots; + + /* Adjust the hash chain. */ + entry =3D b * AFS_DIR_SLOTS_PER_BLOCK + slot; + iter.bucket =3D afs_dir_hash_name(name); + de->u.hash_next =3D meta->meta.hashtable[iter.bucket]; + meta->meta.hashtable[iter.bucket] =3D htons(entry); + kunmap_local(block); =20 inode_inc_iversion_raw(&vnode->netfs.inode); afs_stat_v(vnode, n_dir_cr); @@ -387,12 +393,13 @@ void afs_edit_dir_add(struct afs_vnode *vnode, void afs_edit_dir_remove(struct afs_vnode *vnode, struct qstr *name, enum afs_edit_dir_reason why) { - union afs_xdr_dir_block *meta, *block; - union afs_xdr_dirent *de; + union afs_xdr_dir_block *meta, *block, *pblock; + union afs_xdr_dirent *de, *pde; struct afs_dir_iter iter =3D { .dvnode =3D vnode }; - unsigned int need_slots, nr_blocks, b; + struct afs_fid fid; + unsigned int b, slot, entry; loff_t i_size; - int slot; + __be16 next; =20 _enter(",,{%d,%s},", name->len, name->name); =20 @@ -403,59 +410,89 @@ void afs_edit_dir_remove(struct afs_vnode *vnode, afs_invalidate_dir(vnode, afs_dir_invalid_edit_rem_bad_size); return; } - nr_blocks =3D i_size / AFS_DIR_BLOCK_SIZE; =20 - meta =3D afs_dir_get_block(&iter, 0); - if (!meta) + if (!afs_dir_init_iter(&iter, name)) return; =20 - /* Work out how many slots we're going to discard. */ - need_slots =3D afs_dir_calc_slots(name->len); - - /* Find a block that has sufficient slots available. Each folio - * contains two or more directory blocks. - */ - for (b =3D 0; b < nr_blocks; b++) { - block =3D afs_dir_get_block(&iter, b); - if (!block) - goto error; - - /* Abandon the edit if we got a callback break. */ - if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags)) - goto already_invalidated; - - if (b > AFS_DIR_BLOCKS_WITH_CTR || - meta->meta.alloc_ctrs[b] <=3D AFS_DIR_SLOTS_PER_BLOCK - 1 - need_slo= ts) { - slot =3D afs_dir_scan_block(block, name, b); - if (slot >=3D 0) - goto found_dirent; - } + meta =3D afs_dir_find_block(&iter, 0); + if (!meta) + return; =20 - kunmap_local(block); + /* Find the entry in the blob. */ + entry =3D afs_dir_search_bucket(&iter, name, &fid); + if (entry < 0) { + /* Didn't find the dirent to clobber. Re-download. */ + trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_noent, + 0, 0, 0, 0, name->name); + afs_invalidate_dir(vnode, afs_dir_invalid_edit_rem_wrong_name); + goto out_unmap; } =20 - /* Didn't find the dirent to clobber. Download the directory again. */ - trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_noent, - 0, 0, 0, 0, name->name); - afs_invalidate_dir(vnode, afs_dir_invalid_edit_rem_wrong_name); - goto out_unmap; + b =3D entry / AFS_DIR_SLOTS_PER_BLOCK; + slot =3D entry % AFS_DIR_SLOTS_PER_BLOCK; =20 -found_dirent: + block =3D afs_dir_find_block(&iter, b); + if (!block) + goto error; + if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags)) + goto already_invalidated; + + /* Check and clear the entry. */ de =3D &block->dirents[slot]; + if (de->u.valid !=3D 1) + goto error_unmap; =20 trace_afs_edit_dir(vnode, why, afs_edit_dir_delete, b, slot, ntohl(de->u.vnode), ntohl(de->u.unique), name->name); =20 - memset(de, 0, sizeof(*de) * need_slots); - /* Adjust the bitmap. */ - afs_clear_contig_bits(block, slot, need_slots); - kunmap_local(block); + afs_clear_contig_bits(block, slot, iter.nr_slots); =20 /* Adjust the allocation counter. */ if (b < AFS_DIR_BLOCKS_WITH_CTR) - meta->meta.alloc_ctrs[b] +=3D need_slots; + meta->meta.alloc_ctrs[b] +=3D iter.nr_slots; + + /* Clear the constituent entries. */ + next =3D de->u.hash_next; + memset(de, 0, sizeof(*de) * iter.nr_slots); + kunmap_local(block); + + /* Adjust the hash chain: if iter->prev_entry is 0, the hashtable head + * index is previous; otherwise it's slot number of the previous entry. + */ + if (!iter.prev_entry) { + __be16 prev_next =3D meta->meta.hashtable[iter.bucket]; + + if (unlikely(prev_next !=3D htons(entry))) { + pr_warn("%llx:%llx:%x: not head of chain b=3D%x p=3D%x,%x e=3D%x %*s", + vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique, + iter.bucket, iter.prev_entry, prev_next, entry, + name->len, name->name); + goto error; + } + meta->meta.hashtable[iter.bucket] =3D next; + } else { + unsigned int pb =3D iter.prev_entry / AFS_DIR_SLOTS_PER_BLOCK; + unsigned int ps =3D iter.prev_entry % AFS_DIR_SLOTS_PER_BLOCK; + __be16 prev_next; + + pblock =3D afs_dir_find_block(&iter, pb); + if (!pblock) + goto error; + pde =3D &pblock->dirents[ps]; + prev_next =3D pde->u.hash_next; + if (prev_next !=3D htons(entry)) { + kunmap_local(pblock); + pr_warn("%llx:%llx:%x: not prev in chain b=3D%x p=3D%x,%x e=3D%x %*s", + vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique, + iter.bucket, iter.prev_entry, prev_next, entry, + name->len, name->name); + goto error; + } + pde->u.hash_next =3D next; + kunmap_local(pblock); + } =20 netfs_single_mark_inode_dirty(&vnode->netfs.inode); =20 @@ -474,6 +511,8 @@ void afs_edit_dir_remove(struct afs_vnode *vnode, 0, 0, 0, 0, name->name); goto out_unmap; =20 +error_unmap: + kunmap_local(block); error: trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_error, 0, 0, 0, 0, name->name); diff --git a/fs/afs/dir_search.c b/fs/afs/dir_search.c new file mode 100644 index 000000000000..b25bd892db4d --- /dev/null +++ b/fs/afs/dir_search.c @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Search a directory's hash table. + * + * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + * + * https://tools.ietf.org/html/draft-keiser-afs3-directory-object-00 + */ + +#include +#include +#include +#include +#include "internal.h" +#include "afs_fs.h" +#include "xdr_fs.h" + +/* + * Calculate the name hash. + */ +unsigned int afs_dir_hash_name(const struct qstr *name) +{ + const unsigned char *p =3D name->name; + unsigned int hash =3D 0, i; + int bucket; + + for (i =3D 0; i < name->len; i++) + hash =3D (hash * 173) + p[i]; + bucket =3D hash & (AFS_DIR_HASHTBL_SIZE - 1); + if (hash > INT_MAX) { + bucket =3D AFS_DIR_HASHTBL_SIZE - bucket; + bucket &=3D (AFS_DIR_HASHTBL_SIZE - 1); + } + return bucket; +} + +/* + * Reset a directory iterator. + */ +static bool afs_dir_reset_iter(struct afs_dir_iter *iter) +{ + unsigned long long i_size =3D i_size_read(&iter->dvnode->netfs.inode); + unsigned int nblocks; + + /* Work out the maximum number of steps we can take. */ + nblocks =3D umin(i_size / AFS_DIR_BLOCK_SIZE, AFS_DIR_MAX_BLOCKS); + if (!nblocks) + return false; + iter->loop_check =3D nblocks * (AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BL= OCKS); + iter->prev_entry =3D 0; /* Hash head is previous */ + return true; +} + +/* + * Initialise a directory iterator for looking up a name. + */ +bool afs_dir_init_iter(struct afs_dir_iter *iter, const struct qstr *name) +{ + iter->nr_slots =3D afs_dir_calc_slots(name->len); + iter->bucket =3D afs_dir_hash_name(name); + return afs_dir_reset_iter(iter); +} + +/* + * Get a specific block. + */ +union afs_xdr_dir_block *afs_dir_find_block(struct afs_dir_iter *iter, siz= e_t block) +{ + struct folio_queue *fq =3D iter->fq; + struct afs_vnode *dvnode =3D iter->dvnode; + struct folio *folio; + size_t blpos =3D block * AFS_DIR_BLOCK_SIZE; + size_t blend =3D (block + 1) * AFS_DIR_BLOCK_SIZE, fpos =3D iter->fpos; + int slot =3D iter->fq_slot; + + _enter("%zx,%d", block, slot); + + if (iter->block) { + kunmap_local(iter->block); + iter->block =3D NULL; + } + + if (dvnode->directory_size < blend) + goto fail; + + if (!fq || blpos < fpos) { + fq =3D dvnode->directory; + slot =3D 0; + fpos =3D 0; + } + + /* Search the folio queue for the folio containing the block... */ + for (; fq; fq =3D fq->next) { + for (; slot < folioq_count(fq); slot++) { + size_t fsize =3D folioq_folio_size(fq, slot); + + if (blend <=3D fpos + fsize) { + /* ... and then return the mapped block. */ + folio =3D folioq_folio(fq, slot); + if (WARN_ON_ONCE(folio_pos(folio) !=3D fpos)) + goto fail; + iter->fq =3D fq; + iter->fq_slot =3D slot; + iter->fpos =3D fpos; + iter->block =3D kmap_local_folio(folio, blpos - fpos); + return iter->block; + } + fpos +=3D fsize; + } + slot =3D 0; + } + +fail: + iter->fq =3D NULL; + iter->fq_slot =3D 0; + afs_invalidate_dir(dvnode, afs_dir_invalid_edit_get_block); + return NULL; +} + +/* + * Search through a directory bucket. + */ +int afs_dir_search_bucket(struct afs_dir_iter *iter, const struct qstr *na= me, + struct afs_fid *_fid) +{ + const union afs_xdr_dir_block *meta; + unsigned int entry; + int ret =3D -ESTALE; + + meta =3D afs_dir_find_block(iter, 0); + if (!meta) + return -ESTALE; + + entry =3D ntohs(meta->meta.hashtable[iter->bucket & (AFS_DIR_HASHTBL_SIZE= - 1)]); + _enter("%x,%x", iter->bucket, entry); + + while (entry) { + const union afs_xdr_dir_block *block; + const union afs_xdr_dirent *dire; + unsigned int blnum =3D entry / AFS_DIR_SLOTS_PER_BLOCK; + unsigned int slot =3D entry % AFS_DIR_SLOTS_PER_BLOCK; + unsigned int resv =3D (blnum =3D=3D 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_R= ESV_BLOCKS); + + _debug("search %x", entry); + + if (slot < resv) { + kdebug("slot out of range h=3D%x rs=3D%2x sl=3D%2x-%2x", + iter->bucket, resv, slot, slot + iter->nr_slots - 1); + goto bad; + } + + block =3D afs_dir_find_block(iter, blnum); + if (!block) + goto bad; + dire =3D &block->dirents[slot]; + + if (slot + iter->nr_slots <=3D AFS_DIR_SLOTS_PER_BLOCK && + memcmp(dire->u.name, name->name, name->len) =3D=3D 0 && + dire->u.name[name->len] =3D=3D '\0') { + _fid->vnode =3D ntohl(dire->u.vnode); + _fid->unique =3D ntohl(dire->u.unique); + ret =3D entry; + goto found; + } + + iter->prev_entry =3D entry; + entry =3D ntohs(dire->u.hash_next); + if (!--iter->loop_check) { + kdebug("dir chain loop h=3D%x", iter->bucket); + goto bad; + } + } + + ret =3D -ENOENT; +found: + if (iter->block) { + kunmap_local(iter->block); + iter->block =3D NULL; + } + +bad: + if (ret =3D=3D -ESTALE) + afs_invalidate_dir(iter->dvnode, afs_dir_invalid_iter_stale); + _leave(" =3D %d", ret); + return ret; +} + +/* + * Search the appropriate hash chain in the contents of an AFS directory. + */ +int afs_dir_search(struct afs_vnode *dvnode, struct qstr *name, + struct afs_fid *_fid, afs_dataversion_t *_dir_version) +{ + struct afs_dir_iter iter =3D { .dvnode =3D dvnode, }; + int ret, retry_limit =3D 3; + + _enter("{%lu},,,", dvnode->netfs.inode.i_ino); + + if (!afs_dir_init_iter(&iter, name)) + return -ENOENT; + do { + if (--retry_limit < 0) { + pr_warn("afs_read_dir(): Too many retries\n"); + ret =3D -ESTALE; + break; + } + ret =3D afs_read_dir(dvnode, NULL); + if (ret < 0) { + if (ret !=3D -ESTALE) + break; + if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) { + ret =3D -ESTALE; + break; + } + continue; + } + *_dir_version =3D inode_peek_iversion_raw(&dvnode->netfs.inode); + + ret =3D afs_dir_search_bucket(&iter, name, _fid); + up_read(&dvnode->validate_lock); + if (ret =3D=3D -ESTALE) + afs_dir_reset_iter(&iter); + } while (ret =3D=3D -ESTALE); + + _leave(" =3D %d", ret); + return ret; +} diff --git a/fs/afs/internal.h b/fs/afs/internal.h index 3acf1445e444..0737d729f4e6 100644 --- a/fs/afs/internal.h +++ b/fs/afs/internal.h @@ -974,9 +974,14 @@ static inline void afs_invalidate_cache(struct afs_vno= de *vnode, unsigned int fl */ struct afs_dir_iter { struct afs_vnode *dvnode; + union afs_xdr_dir_block *block; struct folio_queue *fq; unsigned int fpos; int fq_slot; + unsigned int loop_check; + u8 nr_slots; + u8 bucket; + unsigned int prev_entry; }; =20 #include @@ -1061,6 +1066,8 @@ extern const struct address_space_operations afs_dir_= aops; extern const struct dentry_operations afs_fs_dentry_operations; =20 ssize_t afs_read_single(struct afs_vnode *dvnode, struct file *file); +ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file) + __acquires(&dvnode->validate_lock); extern void afs_d_release(struct dentry *); extern void afs_check_for_remote_deletion(struct afs_operation *); int afs_single_writepages(struct address_space *mapping, @@ -1076,6 +1083,17 @@ void afs_edit_dir_update_dotdot(struct afs_vnode *vn= ode, struct afs_vnode *new_d enum afs_edit_dir_reason why); void afs_mkdir_init_dir(struct afs_vnode *dvnode, struct afs_vnode *parent= _vnode); =20 +/* + * dir_search.c + */ +unsigned int afs_dir_hash_name(const struct qstr *name); +bool afs_dir_init_iter(struct afs_dir_iter *iter, const struct qstr *name); +union afs_xdr_dir_block *afs_dir_find_block(struct afs_dir_iter *iter, siz= e_t block); +int afs_dir_search_bucket(struct afs_dir_iter *iter, const struct qstr *na= me, + struct afs_fid *_fid); +int afs_dir_search(struct afs_vnode *dvnode, struct qstr *name, + struct afs_fid *_fid, afs_dataversion_t *_dir_version); + /* * dir_silly.c */