18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * dir.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 1999 Al Smith 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/buffer_head.h> 98c2ecf20Sopenharmony_ci#include "efs.h" 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_cistatic int efs_readdir(struct file *, struct dir_context *); 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciconst struct file_operations efs_dir_operations = { 148c2ecf20Sopenharmony_ci .llseek = generic_file_llseek, 158c2ecf20Sopenharmony_ci .read = generic_read_dir, 168c2ecf20Sopenharmony_ci .iterate_shared = efs_readdir, 178c2ecf20Sopenharmony_ci}; 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciconst struct inode_operations efs_dir_inode_operations = { 208c2ecf20Sopenharmony_ci .lookup = efs_lookup, 218c2ecf20Sopenharmony_ci}; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic int efs_readdir(struct file *file, struct dir_context *ctx) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci struct inode *inode = file_inode(file); 268c2ecf20Sopenharmony_ci efs_block_t block; 278c2ecf20Sopenharmony_ci int slot; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci if (inode->i_size & (EFS_DIRBSIZE-1)) 308c2ecf20Sopenharmony_ci pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n", 318c2ecf20Sopenharmony_ci __func__); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci /* work out where this entry can be found */ 348c2ecf20Sopenharmony_ci block = ctx->pos >> EFS_DIRBSIZE_BITS; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci /* each block contains at most 256 slots */ 378c2ecf20Sopenharmony_ci slot = ctx->pos & 0xff; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci /* look at all blocks */ 408c2ecf20Sopenharmony_ci while (block < inode->i_blocks) { 418c2ecf20Sopenharmony_ci struct efs_dir *dirblock; 428c2ecf20Sopenharmony_ci struct buffer_head *bh; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* read the dir block */ 458c2ecf20Sopenharmony_ci bh = sb_bread(inode->i_sb, efs_bmap(inode, block)); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (!bh) { 488c2ecf20Sopenharmony_ci pr_err("%s(): failed to read dir block %d\n", 498c2ecf20Sopenharmony_ci __func__, block); 508c2ecf20Sopenharmony_ci break; 518c2ecf20Sopenharmony_ci } 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci dirblock = (struct efs_dir *) bh->b_data; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) { 568c2ecf20Sopenharmony_ci pr_err("%s(): invalid directory block\n", __func__); 578c2ecf20Sopenharmony_ci brelse(bh); 588c2ecf20Sopenharmony_ci break; 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci for (; slot < dirblock->slots; slot++) { 628c2ecf20Sopenharmony_ci struct efs_dentry *dirslot; 638c2ecf20Sopenharmony_ci efs_ino_t inodenum; 648c2ecf20Sopenharmony_ci const char *nameptr; 658c2ecf20Sopenharmony_ci int namelen; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (dirblock->space[slot] == 0) 688c2ecf20Sopenharmony_ci continue; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot)); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci inodenum = be32_to_cpu(dirslot->inode); 738c2ecf20Sopenharmony_ci namelen = dirslot->namelen; 748c2ecf20Sopenharmony_ci nameptr = dirslot->name; 758c2ecf20Sopenharmony_ci pr_debug("%s(): block %d slot %d/%d: inode %u, name \"%s\", namelen %u\n", 768c2ecf20Sopenharmony_ci __func__, block, slot, dirblock->slots-1, 778c2ecf20Sopenharmony_ci inodenum, nameptr, namelen); 788c2ecf20Sopenharmony_ci if (!namelen) 798c2ecf20Sopenharmony_ci continue; 808c2ecf20Sopenharmony_ci /* found the next entry */ 818c2ecf20Sopenharmony_ci ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci /* sanity check */ 848c2ecf20Sopenharmony_ci if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) { 858c2ecf20Sopenharmony_ci pr_warn("directory entry %d exceeds directory block\n", 868c2ecf20Sopenharmony_ci slot); 878c2ecf20Sopenharmony_ci continue; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci /* copy filename and data in dirslot */ 918c2ecf20Sopenharmony_ci if (!dir_emit(ctx, nameptr, namelen, inodenum, DT_UNKNOWN)) { 928c2ecf20Sopenharmony_ci brelse(bh); 938c2ecf20Sopenharmony_ci return 0; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci brelse(bh); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci slot = 0; 998c2ecf20Sopenharmony_ci block++; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot; 1028c2ecf20Sopenharmony_ci return 0; 1038c2ecf20Sopenharmony_ci} 104