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