* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / efs / dir.c
blob29f2ebbc3f339bcb400b938363b7d152f977f8ed
1 /*
2 * dir.c
4 * Copyright (c) 1999 Al Smith
5 */
7 #include <linux/efs_fs.h>
9 static int efs_readdir(struct file *, void *, filldir_t);
11 static struct file_operations efs_dir_operations = {
12 NULL, /* lseek */
13 NULL, /* read */
14 NULL, /* write */
15 efs_readdir, /* readdir */
16 NULL, /* poll */
17 NULL, /* ioctl */
18 NULL, /* mmap */
19 NULL, /* open */
20 NULL, /* flush */
21 NULL, /* release */
22 NULL, /* fsync */
23 NULL, /* fasync */
24 NULL, /* check_media_change */
25 NULL /* revalidate */
28 extern int efs_get_block(struct inode *, long, struct buffer_head *, int);
30 struct inode_operations efs_dir_inode_operations = {
31 &efs_dir_operations, /* default directory file-ops */
32 NULL, /* create */
33 efs_lookup, /* lookup */
34 NULL, /* link */
35 NULL, /* unlink */
36 NULL, /* symlink */
37 NULL, /* mkdir */
38 NULL, /* rmdir */
39 NULL, /* mknod */
40 NULL, /* rename */
41 NULL, /* readlink */
42 NULL, /* follow_link */
43 efs_get_block, /* get_block */
44 NULL, /* readpage */
45 NULL, /* writepage */
46 NULL, /* flushpage */
47 NULL, /* truncate */
48 NULL, /* permission */
49 NULL, /* smap */
50 NULL /* revalidate */
53 static int efs_readdir(struct file *filp, void *dirent, filldir_t filldir) {
54 struct inode *inode = filp->f_dentry->d_inode;
55 struct buffer_head *bh;
57 struct efs_dir *dirblock;
58 struct efs_dentry *dirslot;
59 efs_ino_t inodenum;
60 efs_block_t block;
61 int slot, namelen;
62 char *nameptr;
64 if (!inode || !S_ISDIR(inode->i_mode))
65 return -EBADF;
67 if (inode->i_size & (EFS_DIRBSIZE-1))
68 printk(KERN_WARNING "EFS: WARNING: readdir(): directory size not a multiple of EFS_DIRBSIZE\n");
70 /* work out where this entry can be found */
71 block = filp->f_pos >> EFS_DIRBSIZE_BITS;
73 /* each block contains at most 256 slots */
74 slot = filp->f_pos & 0xff;
76 /* look at all blocks */
77 while (block < inode->i_blocks) {
78 /* read the dir block */
79 bh = bread(inode->i_dev, efs_bmap(inode, block), EFS_DIRBSIZE);
81 if (!bh) {
82 printk(KERN_ERR "EFS: readdir(): failed to read dir block %d\n", block);
83 break;
86 dirblock = (struct efs_dir *) bh->b_data;
88 if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
89 printk(KERN_ERR "EFS: readdir(): invalid directory block\n");
90 brelse(bh);
91 break;
94 while (slot < dirblock->slots) {
95 if (dirblock->space[slot] == 0) {
96 slot++;
97 continue;
100 dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
102 inodenum = be32_to_cpu(dirslot->inode);
103 namelen = dirslot->namelen;
104 nameptr = dirslot->name;
106 #ifdef DEBUG
107 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);
108 #endif
109 if (namelen > 0) {
110 /* found the next entry */
111 filp->f_pos = (block << EFS_DIRBSIZE_BITS) | slot;
113 /* copy filename and data in dirslot */
114 filldir(dirent, nameptr, namelen, filp->f_pos, inodenum);
116 /* sanity check */
117 if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) {
118 printk(KERN_WARNING "EFS: directory entry %d exceeds directory block\n", slot);
119 slot++;
120 continue;
123 /* store position of next slot */
124 if (++slot == dirblock->slots) {
125 slot = 0;
126 block++;
128 brelse(bh);
129 filp->f_pos = (block << EFS_DIRBSIZE_BITS) | slot;
130 return 0;
132 slot++;
134 brelse(bh);
136 slot = 0;
137 block++;
140 filp->f_pos = (block << EFS_DIRBSIZE_BITS) | slot;
141 return 0;