1 // SPDX-License-Identifier: GPL-2.0
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 <linux/iversion.h>
18 #include <linux/filelock.h>
19 #include "affs.h"
20
21 struct affs_dir_data {
22 unsigned long ino;
23 u64 cookie;
24 };
25
26 static int affs_readdir(struct file *, struct dir_context *);
27
affs_dir_llseek(struct file * file,loff_t offset,int whence)28 static loff_t affs_dir_llseek(struct file *file, loff_t offset, int whence)
29 {
30 struct affs_dir_data *data = file->private_data;
31
32 return generic_llseek_cookie(file, offset, whence, &data->cookie);
33 }
34
affs_dir_open(struct inode * inode,struct file * file)35 static int affs_dir_open(struct inode *inode, struct file *file)
36 {
37 struct affs_dir_data *data;
38
39 data = kzalloc_obj(struct affs_dir_data);
40 if (!data)
41 return -ENOMEM;
42 file->private_data = data;
43 return 0;
44 }
45
affs_dir_release(struct inode * inode,struct file * file)46 static int affs_dir_release(struct inode *inode, struct file *file)
47 {
48 kfree(file->private_data);
49 return 0;
50 }
51
52 const struct file_operations affs_dir_operations = {
53 .open = affs_dir_open,
54 .read = generic_read_dir,
55 .llseek = affs_dir_llseek,
56 .iterate_shared = affs_readdir,
57 .fsync = affs_file_fsync,
58 .release = affs_dir_release,
59 .setlease = generic_setlease,
60 };
61
62 /*
63 * directories can handle most operations...
64 */
65 const struct inode_operations affs_dir_inode_operations = {
66 .create = affs_create,
67 .lookup = affs_lookup,
68 .link = affs_link,
69 .unlink = affs_unlink,
70 .symlink = affs_symlink,
71 .mkdir = affs_mkdir,
72 .rmdir = affs_rmdir,
73 .rename = affs_rename2,
74 .setattr = affs_notify_change,
75 };
76
77 static int
affs_readdir(struct file * file,struct dir_context * ctx)78 affs_readdir(struct file *file, struct dir_context *ctx)
79 {
80 struct inode *inode = file_inode(file);
81 struct affs_dir_data *data = file->private_data;
82 struct super_block *sb = inode->i_sb;
83 struct buffer_head *dir_bh = NULL;
84 struct buffer_head *fh_bh = NULL;
85 unsigned char *name;
86 int namelen;
87 u32 i;
88 int hash_pos;
89 int chain_pos;
90 u32 ino;
91 int error = 0;
92
93 pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
94
95 if (ctx->pos < 2) {
96 data->ino = 0;
97 if (!dir_emit_dots(file, ctx))
98 return 0;
99 }
100
101 affs_lock_dir(inode);
102 chain_pos = (ctx->pos - 2) & 0xffff;
103 hash_pos = (ctx->pos - 2) >> 16;
104 if (chain_pos == 0xffff) {
105 affs_warning(sb, "readdir", "More than 65535 entries in chain");
106 chain_pos = 0;
107 hash_pos++;
108 ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
109 }
110 dir_bh = affs_bread(sb, inode->i_ino);
111 if (!dir_bh)
112 goto out_unlock_dir;
113
114 /* If the directory hasn't changed since the last call to readdir(),
115 * we can jump directly to where we left off.
116 */
117 ino = data->ino;
118 if (ino && inode_eq_iversion(inode, data->cookie)) {
119 pr_debug("readdir() left off=%d\n", ino);
120 goto inside;
121 }
122
123 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
124 for (i = 0; ino && i < chain_pos; i++) {
125 fh_bh = affs_bread(sb, ino);
126 if (!fh_bh) {
127 affs_error(sb, "readdir","Cannot read block %d", i);
128 error = -EIO;
129 goto out_brelse_dir;
130 }
131 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
132 affs_brelse(fh_bh);
133 fh_bh = NULL;
134 }
135 if (ino)
136 goto inside;
137 hash_pos++;
138
139 for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
140 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
141 if (!ino)
142 continue;
143 ctx->pos = (hash_pos << 16) + 2;
144 inside:
145 do {
146 fh_bh = affs_bread(sb, ino);
147 if (!fh_bh) {
148 affs_error(sb, "readdir",
149 "Cannot read block %d", ino);
150 break;
151 }
152
153 namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
154 (u8)AFFSNAMEMAX);
155 name = AFFS_TAIL(sb, fh_bh)->name + 1;
156 pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
157 namelen, name, ino, hash_pos, ctx->pos);
158
159 if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
160 goto done;
161 ctx->pos++;
162 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
163 affs_brelse(fh_bh);
164 fh_bh = NULL;
165 } while (ino);
166 }
167 done:
168 data->cookie = inode_query_iversion(inode);
169 data->ino = ino;
170 affs_brelse(fh_bh);
171
172 out_brelse_dir:
173 affs_brelse(dir_bh);
174
175 out_unlock_dir:
176 affs_unlock_dir(inode);
177 return error;
178 }
179