blob: 548efd0ee98cd9e8e5449abde56d0c2821afaed4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 "affs.h"
17
18static int affs_readdir(struct file *, void *, filldir_t);
19
20struct file_operations affs_dir_operations = {
21 .read = generic_read_dir,
22 .readdir = affs_readdir,
23 .fsync = file_fsync,
24};
25
26/*
27 * directories can handle most operations...
28 */
29struct inode_operations affs_dir_inode_operations = {
30 .create = affs_create,
31 .lookup = affs_lookup,
32 .link = affs_link,
33 .unlink = affs_unlink,
34 .symlink = affs_symlink,
35 .mkdir = affs_mkdir,
36 .rmdir = affs_rmdir,
37 .rename = affs_rename,
38 .setattr = affs_notify_change,
39};
40
41static int
42affs_readdir(struct file *filp, void *dirent, filldir_t filldir)
43{
44 struct inode *inode = filp->f_dentry->d_inode;
45 struct super_block *sb = inode->i_sb;
46 struct buffer_head *dir_bh;
47 struct buffer_head *fh_bh;
48 unsigned char *name;
49 int namelen;
50 u32 i;
51 int hash_pos;
52 int chain_pos;
53 u32 f_pos;
54 u32 ino;
55 int stored;
56 int res;
57
58 pr_debug("AFFS: readdir(ino=%lu,f_pos=%lx)\n",inode->i_ino,(unsigned long)filp->f_pos);
59
60 stored = 0;
61 res = -EIO;
62 dir_bh = NULL;
63 fh_bh = NULL;
64 f_pos = filp->f_pos;
65
66 if (f_pos == 0) {
67 filp->private_data = (void *)0;
68 if (filldir(dirent, ".", 1, f_pos, inode->i_ino, DT_DIR) < 0)
69 return 0;
70 filp->f_pos = f_pos = 1;
71 stored++;
72 }
73 if (f_pos == 1) {
74 if (filldir(dirent, "..", 2, f_pos, parent_ino(filp->f_dentry), DT_DIR) < 0)
75 return stored;
76 filp->f_pos = f_pos = 2;
77 stored++;
78 }
79
80 affs_lock_dir(inode);
81 chain_pos = (f_pos - 2) & 0xffff;
82 hash_pos = (f_pos - 2) >> 16;
83 if (chain_pos == 0xffff) {
84 affs_warning(sb, "readdir", "More than 65535 entries in chain");
85 chain_pos = 0;
86 hash_pos++;
87 filp->f_pos = ((hash_pos << 16) | chain_pos) + 2;
88 }
89 dir_bh = affs_bread(sb, inode->i_ino);
90 if (!dir_bh)
91 goto readdir_out;
92
93 /* If the directory hasn't changed since the last call to readdir(),
94 * we can jump directly to where we left off.
95 */
96 ino = (u32)(long)filp->private_data;
97 if (ino && filp->f_version == inode->i_version) {
98 pr_debug("AFFS: readdir() left off=%d\n", ino);
99 goto inside;
100 }
101
102 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
103 for (i = 0; ino && i < chain_pos; i++) {
104 fh_bh = affs_bread(sb, ino);
105 if (!fh_bh) {
106 affs_error(sb, "readdir","Cannot read block %d", i);
107 goto readdir_out;
108 }
109 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
110 affs_brelse(fh_bh);
111 fh_bh = NULL;
112 }
113 if (ino)
114 goto inside;
115 hash_pos++;
116
117 for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
118 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
119 if (!ino)
120 continue;
121 f_pos = (hash_pos << 16) + 2;
122inside:
123 do {
124 fh_bh = affs_bread(sb, ino);
125 if (!fh_bh) {
126 affs_error(sb, "readdir","Cannot read block %d", ino);
127 goto readdir_done;
128 }
129
130 namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], (u8)30);
131 name = AFFS_TAIL(sb, fh_bh)->name + 1;
132 pr_debug("AFFS: readdir(): filldir(\"%.*s\", ino=%u), hash=%d, f_pos=%x\n",
133 namelen, name, ino, hash_pos, f_pos);
134 if (filldir(dirent, name, namelen, f_pos, ino, DT_UNKNOWN) < 0)
135 goto readdir_done;
136 stored++;
137 f_pos++;
138 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
139 affs_brelse(fh_bh);
140 fh_bh = NULL;
141 } while (ino);
142 }
143readdir_done:
144 filp->f_pos = f_pos;
145 filp->f_version = inode->i_version;
146 filp->private_data = (void *)(long)ino;
147 res = stored;
148
149readdir_out:
150 affs_brelse(dir_bh);
151 affs_brelse(fh_bh);
152 affs_unlock_dir(inode);
153 pr_debug("AFFS: readdir()=%d\n", stored);
154 return res;
155}