dir.c 2.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 * dir.c
 *
 * Copyright (c) 1999 Al Smith
 */

#include <linux/buffer_head.h>
8
#include "efs.h"
L
Linus Torvalds 已提交
9

A
Al Viro 已提交
10
static int efs_readdir(struct file *, struct dir_context *);
L
Linus Torvalds 已提交
11

12
const struct file_operations efs_dir_operations = {
A
Al Viro 已提交
13
	.llseek		= generic_file_llseek,
L
Linus Torvalds 已提交
14
	.read		= generic_read_dir,
A
Al Viro 已提交
15
	.iterate	= efs_readdir,
L
Linus Torvalds 已提交
16 17
};

18
const struct inode_operations efs_dir_inode_operations = {
L
Linus Torvalds 已提交
19 20 21
	.lookup		= efs_lookup,
};

A
Al Viro 已提交
22 23 24
static int efs_readdir(struct file *file, struct dir_context *ctx)
{
	struct inode *inode = file_inode(file);
L
Linus Torvalds 已提交
25
	efs_block_t		block;
A
Al Viro 已提交
26
	int			slot;
L
Linus Torvalds 已提交
27 28

	if (inode->i_size & (EFS_DIRBSIZE-1))
29
		pr_warn("EFS: WARNING: readdir(): directory size not a multiple of EFS_DIRBSIZE\n");
L
Linus Torvalds 已提交
30 31

	/* work out where this entry can be found */
A
Al Viro 已提交
32
	block = ctx->pos >> EFS_DIRBSIZE_BITS;
L
Linus Torvalds 已提交
33 34

	/* each block contains at most 256 slots */
A
Al Viro 已提交
35
	slot  = ctx->pos & 0xff;
L
Linus Torvalds 已提交
36 37 38

	/* look at all blocks */
	while (block < inode->i_blocks) {
A
Al Viro 已提交
39 40 41
		struct efs_dir		*dirblock;
		struct buffer_head *bh;

L
Linus Torvalds 已提交
42 43 44 45
		/* read the dir block */
		bh = sb_bread(inode->i_sb, efs_bmap(inode, block));

		if (!bh) {
46
			pr_err("EFS: readdir(): failed to read dir block %d\n", block);
L
Linus Torvalds 已提交
47 48 49 50 51 52
			break;
		}

		dirblock = (struct efs_dir *) bh->b_data; 

		if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
53
			pr_err("EFS: readdir(): invalid directory block\n");
L
Linus Torvalds 已提交
54 55 56 57
			brelse(bh);
			break;
		}

A
Al Viro 已提交
58 59 60 61 62 63 64
		for (; slot < dirblock->slots; slot++) {
			struct efs_dentry *dirslot;
			efs_ino_t inodenum;
			const char *nameptr;
			int namelen;

			if (dirblock->space[slot] == 0)
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75
				continue;

			dirslot  = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));

			inodenum = be32_to_cpu(dirslot->inode);
			namelen  = dirslot->namelen;
			nameptr  = dirslot->name;

#ifdef DEBUG
			printk(KERN_DEBUG "EFS: readdir(): block %d slot %d/%d: inode %u, name \"%s\", namelen %u\n", block, slot, dirblock->slots-1, inodenum, nameptr, namelen);
#endif
A
Al Viro 已提交
76 77 78 79 80 81 82
			if (!namelen)
				continue;
			/* found the next entry */
			ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;

			/* sanity check */
			if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) {
83
				pr_warn("EFS: directory entry %d exceeds directory block\n", slot);
A
Al Viro 已提交
84 85 86 87 88
				continue;
			}

			/* copy filename and data in dirslot */
			if (!dir_emit(ctx, nameptr, namelen, inodenum, DT_UNKNOWN)) {
L
Linus Torvalds 已提交
89
				brelse(bh);
A
Al Viro 已提交
90
				return 0;
L
Linus Torvalds 已提交
91 92 93 94 95 96 97
			}
		}
		brelse(bh);

		slot = 0;
		block++;
	}
A
Al Viro 已提交
98
	ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
L
Linus Torvalds 已提交
99 100 101
	return 0;
}