ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / affs / dir.c
1 /*
2  *  linux/fs/affs/dir.c
3  *
4  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
5  *
6  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
7  *
8  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
9  *
10  *  (C) 1991  Linus Torvalds - minix filesystem
11  *
12  *  affs directory handling functions
13  *
14  */
15
16 #include <asm/uaccess.h>
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/affs_fs.h>
21 #include <linux/stat.h>
22 #include <linux/string.h>
23 #include <linux/mm.h>
24 #include <linux/amigaffs.h>
25 #include <linux/smp_lock.h>
26 #include <linux/buffer_head.h>
27
28 static int affs_readdir(struct file *, void *, filldir_t);
29
30 struct file_operations affs_dir_operations = {
31         .read           = generic_read_dir,
32         .readdir        = affs_readdir,
33         .fsync          = file_fsync,
34 };
35
36 /*
37  * directories can handle most operations...
38  */
39 struct inode_operations affs_dir_inode_operations = {
40         .create         = affs_create,
41         .lookup         = affs_lookup,
42         .link           = affs_link,
43         .unlink         = affs_unlink,
44         .symlink        = affs_symlink,
45         .mkdir          = affs_mkdir,
46         .rmdir          = affs_rmdir,
47         .rename         = affs_rename,
48         .setattr        = affs_notify_change,
49 };
50
51 static int
52 affs_readdir(struct file *filp, void *dirent, filldir_t filldir)
53 {
54         struct inode            *inode = filp->f_dentry->d_inode;
55         struct super_block      *sb = inode->i_sb;
56         struct buffer_head      *dir_bh;
57         struct buffer_head      *fh_bh;
58         unsigned char           *name;
59         int                      namelen;
60         u32                      i;
61         int                      hash_pos;
62         int                      chain_pos;
63         u32                      f_pos;
64         u32                      ino;
65         int                      stored;
66         int                      res;
67
68         pr_debug("AFFS: readdir(ino=%lu,f_pos=%lx)\n",inode->i_ino,(unsigned long)filp->f_pos);
69
70         stored = 0;
71         res    = -EIO;
72         dir_bh = NULL;
73         fh_bh  = NULL;
74         f_pos  = filp->f_pos;
75
76         if (f_pos == 0) {
77                 filp->private_data = (void *)0;
78                 if (filldir(dirent, ".", 1, f_pos, inode->i_ino, DT_DIR) < 0)
79                         return 0;
80                 filp->f_pos = f_pos = 1;
81                 stored++;
82         }
83         if (f_pos == 1) {
84                 if (filldir(dirent, "..", 2, f_pos, parent_ino(filp->f_dentry), DT_DIR) < 0)
85                         return stored;
86                 filp->f_pos = f_pos = 2;
87                 stored++;
88         }
89
90         affs_lock_dir(inode);
91         chain_pos = (f_pos - 2) & 0xffff;
92         hash_pos  = (f_pos - 2) >> 16;
93         if (chain_pos == 0xffff) {
94                 affs_warning(sb, "readdir", "More than 65535 entries in chain");
95                 chain_pos = 0;
96                 hash_pos++;
97                 filp->f_pos = ((hash_pos << 16) | chain_pos) + 2;
98         }
99         dir_bh = affs_bread(sb, inode->i_ino);
100         if (!dir_bh)
101                 goto readdir_out;
102
103         /* If the directory hasn't changed since the last call to readdir(),
104          * we can jump directly to where we left off.
105          */
106         ino = (u32)(long)filp->private_data;
107         if (ino && filp->f_version == inode->i_version) {
108                 pr_debug("AFFS: readdir() left off=%d\n", ino);
109                 goto inside;
110         }
111
112         ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
113         for (i = 0; ino && i < chain_pos; i++) {
114                 fh_bh = affs_bread(sb, ino);
115                 if (!fh_bh) {
116                         affs_error(sb, "readdir","Cannot read block %d", i);
117                         goto readdir_out;
118                 }
119                 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
120                 affs_brelse(fh_bh);
121                 fh_bh = NULL;
122         }
123         if (ino)
124                 goto inside;
125         hash_pos++;
126
127         for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
128                 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
129                 if (!ino)
130                         continue;
131                 f_pos = (hash_pos << 16) + 2;
132 inside:
133                 do {
134                         fh_bh = affs_bread(sb, ino);
135                         if (!fh_bh) {
136                                 affs_error(sb, "readdir","Cannot read block %d", ino);
137                                 goto readdir_done;
138                         }
139
140                         namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], (u8)30);
141                         name = AFFS_TAIL(sb, fh_bh)->name + 1;
142                         pr_debug("AFFS: readdir(): filldir(\"%.*s\", ino=%u), hash=%d, f_pos=%x\n",
143                                  namelen, name, ino, hash_pos, f_pos);
144                         if (filldir(dirent, name, namelen, f_pos, ino, DT_UNKNOWN) < 0)
145                                 goto readdir_done;
146                         stored++;
147                         f_pos++;
148                         ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
149                         affs_brelse(fh_bh);
150                         fh_bh = NULL;
151                 } while (ino);
152         }
153 readdir_done:
154         filp->f_pos = f_pos;
155         filp->f_version = inode->i_version;
156         filp->private_data = (void *)(long)ino;
157         res = stored;
158
159 readdir_out:
160         affs_brelse(dir_bh);
161         affs_brelse(fh_bh);
162         affs_unlock_dir(inode);
163         pr_debug("AFFS: readdir()=%d\n", stored);
164         return res;
165 }