^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 1999 Al Smith
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "efs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static int efs_readdir(struct file *, struct dir_context *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) const struct file_operations efs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) .iterate_shared = efs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const struct inode_operations efs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) .lookup = efs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int efs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) efs_block_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (inode->i_size & (EFS_DIRBSIZE-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* work out where this entry can be found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) block = ctx->pos >> EFS_DIRBSIZE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* each block contains at most 256 slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) slot = ctx->pos & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* look at all blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) while (block < inode->i_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct efs_dir *dirblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* read the dir block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pr_err("%s(): failed to read dir block %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) __func__, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dirblock = (struct efs_dir *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) pr_err("%s(): invalid directory block\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for (; slot < dirblock->slots; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct efs_dentry *dirslot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) efs_ino_t inodenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char *nameptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (dirblock->space[slot] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) inodenum = be32_to_cpu(dirslot->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) namelen = dirslot->namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) nameptr = dirslot->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) pr_debug("%s(): block %d slot %d/%d: inode %u, name \"%s\", namelen %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) __func__, block, slot, dirblock->slots-1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) inodenum, nameptr, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (!namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* found the next entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pr_warn("directory entry %d exceeds directory block\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* copy filename and data in dirslot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!dir_emit(ctx, nameptr, namelen, inodenum, DT_UNKNOWN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) block++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }