blob: f892ac7c2a35e0094a314eeded06a974154e46d7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * dir.c
4 *
5 * Copyright (c) 1999 Al Smith
6 */
7
8#include <linux/buffer_head.h>
Christoph Hellwig45254b42008-02-23 15:23:51 -08009#include "efs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Al Viro7aa123a2013-05-16 01:41:10 -040011static int efs_readdir(struct file *, struct dir_context *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080013const struct file_operations efs_dir_operations = {
Al Viroe7ec9522009-06-16 23:35:46 -040014 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -040016 .iterate_shared = efs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017};
18
Arjan van de Ven754661f2007-02-12 00:55:38 -080019const struct inode_operations efs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 .lookup = efs_lookup,
21};
22
Al Viro7aa123a2013-05-16 01:41:10 -040023static int efs_readdir(struct file *file, struct dir_context *ctx)
24{
25 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 efs_block_t block;
Al Viro7aa123a2013-05-16 01:41:10 -040027 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 if (inode->i_size & (EFS_DIRBSIZE-1))
Fabian Frederickf403d1d2014-06-04 16:12:12 -070030 pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
31 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 /* work out where this entry can be found */
Al Viro7aa123a2013-05-16 01:41:10 -040034 block = ctx->pos >> EFS_DIRBSIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 /* each block contains at most 256 slots */
Al Viro7aa123a2013-05-16 01:41:10 -040037 slot = ctx->pos & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 /* look at all blocks */
40 while (block < inode->i_blocks) {
Al Viro7aa123a2013-05-16 01:41:10 -040041 struct efs_dir *dirblock;
42 struct buffer_head *bh;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 /* read the dir block */
45 bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
46
47 if (!bh) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070048 pr_err("%s(): failed to read dir block %d\n",
49 __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 break;
51 }
52
53 dirblock = (struct efs_dir *) bh->b_data;
54
55 if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070056 pr_err("%s(): invalid directory block\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 brelse(bh);
58 break;
59 }
60
Al Viro7aa123a2013-05-16 01:41:10 -040061 for (; slot < dirblock->slots; slot++) {
62 struct efs_dentry *dirslot;
63 efs_ino_t inodenum;
64 const char *nameptr;
65 int namelen;
66
67 if (dirblock->space[slot] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
71
72 inodenum = be32_to_cpu(dirslot->inode);
73 namelen = dirslot->namelen;
74 nameptr = dirslot->name;
Fabian Frederickd1826f22014-06-04 16:12:13 -070075 pr_debug("%s(): block %d slot %d/%d: inode %u, name \"%s\", namelen %u\n",
76 __func__, block, slot, dirblock->slots-1,
77 inodenum, nameptr, namelen);
Al Viro7aa123a2013-05-16 01:41:10 -040078 if (!namelen)
79 continue;
80 /* found the next entry */
81 ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Al Viro7aa123a2013-05-16 01:41:10 -040083 /* sanity check */
84 if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070085 pr_warn("directory entry %d exceeds directory block\n",
86 slot);
Al Viro7aa123a2013-05-16 01:41:10 -040087 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Al Viro7aa123a2013-05-16 01:41:10 -040089
90 /* copy filename and data in dirslot */
91 if (!dir_emit(ctx, nameptr, namelen, inodenum, DT_UNKNOWN)) {
92 brelse(bh);
93 return 0;
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96 brelse(bh);
97
98 slot = 0;
99 block++;
100 }
Al Viro7aa123a2013-05-16 01:41:10 -0400101 ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}