Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 "affs.h" | |
17 | ||
0edf977d | 18 | static int affs_readdir(struct file *, struct dir_context *); |
1da177e4 | 19 | |
4b6f5d20 | 20 | const struct file_operations affs_dir_operations = { |
1da177e4 | 21 | .read = generic_read_dir, |
59af1584 | 22 | .llseek = generic_file_llseek, |
0edf977d | 23 | .iterate = affs_readdir, |
c4758795 | 24 | .fsync = affs_file_fsync, |
1da177e4 LT |
25 | }; |
26 | ||
27 | /* | |
28 | * directories can handle most operations... | |
29 | */ | |
754661f1 | 30 | const struct inode_operations affs_dir_inode_operations = { |
1da177e4 LT |
31 | .create = affs_create, |
32 | .lookup = affs_lookup, | |
33 | .link = affs_link, | |
34 | .unlink = affs_unlink, | |
35 | .symlink = affs_symlink, | |
36 | .mkdir = affs_mkdir, | |
37 | .rmdir = affs_rmdir, | |
38 | .rename = affs_rename, | |
39 | .setattr = affs_notify_change, | |
40 | }; | |
41 | ||
42 | static int | |
0edf977d | 43 | affs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4 | 44 | { |
0edf977d | 45 | struct inode *inode = file_inode(file); |
1da177e4 | 46 | struct super_block *sb = inode->i_sb; |
0edf977d AV |
47 | struct buffer_head *dir_bh = NULL; |
48 | struct buffer_head *fh_bh = NULL; | |
1da177e4 LT |
49 | unsigned char *name; |
50 | int namelen; | |
51 | u32 i; | |
52 | int hash_pos; | |
53 | int chain_pos; | |
1da177e4 | 54 | u32 ino; |
d40c4d46 | 55 | int error = 0; |
1da177e4 | 56 | |
08fe100d | 57 | pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos); |
1da177e4 | 58 | |
0edf977d AV |
59 | if (ctx->pos < 2) { |
60 | file->private_data = (void *)0; | |
61 | if (!dir_emit_dots(file, ctx)) | |
1da177e4 | 62 | return 0; |
1da177e4 LT |
63 | } |
64 | ||
65 | affs_lock_dir(inode); | |
0edf977d AV |
66 | chain_pos = (ctx->pos - 2) & 0xffff; |
67 | hash_pos = (ctx->pos - 2) >> 16; | |
1da177e4 LT |
68 | if (chain_pos == 0xffff) { |
69 | affs_warning(sb, "readdir", "More than 65535 entries in chain"); | |
70 | chain_pos = 0; | |
71 | hash_pos++; | |
0edf977d | 72 | ctx->pos = ((hash_pos << 16) | chain_pos) + 2; |
1da177e4 LT |
73 | } |
74 | dir_bh = affs_bread(sb, inode->i_ino); | |
75 | if (!dir_bh) | |
d40c4d46 | 76 | goto out_unlock_dir; |
1da177e4 LT |
77 | |
78 | /* If the directory hasn't changed since the last call to readdir(), | |
79 | * we can jump directly to where we left off. | |
80 | */ | |
0edf977d AV |
81 | ino = (u32)(long)file->private_data; |
82 | if (ino && file->f_version == inode->i_version) { | |
9606d9aa | 83 | pr_debug("readdir() left off=%d\n", ino); |
1da177e4 LT |
84 | goto inside; |
85 | } | |
86 | ||
87 | ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]); | |
88 | for (i = 0; ino && i < chain_pos; i++) { | |
89 | fh_bh = affs_bread(sb, ino); | |
90 | if (!fh_bh) { | |
91 | affs_error(sb, "readdir","Cannot read block %d", i); | |
d40c4d46 FF |
92 | error = -EIO; |
93 | goto out_brelse_dir; | |
1da177e4 LT |
94 | } |
95 | ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain); | |
96 | affs_brelse(fh_bh); | |
97 | fh_bh = NULL; | |
98 | } | |
99 | if (ino) | |
100 | goto inside; | |
101 | hash_pos++; | |
102 | ||
103 | for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) { | |
104 | ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]); | |
105 | if (!ino) | |
106 | continue; | |
0edf977d | 107 | ctx->pos = (hash_pos << 16) + 2; |
1da177e4 LT |
108 | inside: |
109 | do { | |
110 | fh_bh = affs_bread(sb, ino); | |
111 | if (!fh_bh) { | |
d40c4d46 FF |
112 | affs_error(sb, "readdir", |
113 | "Cannot read block %d", ino); | |
0edf977d | 114 | break; |
1da177e4 LT |
115 | } |
116 | ||
f157853e FF |
117 | namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], |
118 | (u8)AFFSNAMEMAX); | |
1da177e4 | 119 | name = AFFS_TAIL(sb, fh_bh)->name + 1; |
08fe100d GU |
120 | pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n", |
121 | namelen, name, ino, hash_pos, ctx->pos); | |
d40c4d46 | 122 | |
0edf977d | 123 | if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN)) |
d40c4d46 | 124 | goto done; |
0edf977d | 125 | ctx->pos++; |
1da177e4 LT |
126 | ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain); |
127 | affs_brelse(fh_bh); | |
128 | fh_bh = NULL; | |
129 | } while (ino); | |
130 | } | |
d40c4d46 | 131 | done: |
0edf977d AV |
132 | file->f_version = inode->i_version; |
133 | file->private_data = (void *)(long)ino; | |
d40c4d46 | 134 | affs_brelse(fh_bh); |
1da177e4 | 135 | |
d40c4d46 | 136 | out_brelse_dir: |
1da177e4 | 137 | affs_brelse(dir_bh); |
d40c4d46 FF |
138 | |
139 | out_unlock_dir: | |
1da177e4 | 140 | affs_unlock_dir(inode); |
d40c4d46 | 141 | return error; |
1da177e4 | 142 | } |