cdc_ncm: fix divide-by-zero caused by invalid wMaxPacketSize
[linux/fpc-iii.git] / fs / adfs / dir_fplus.c
blobd56924c11b17f0f61f5d7ef1825db7c63a024529
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/adfs/dir_fplus.c
5 * Copyright (C) 1997-1999 Russell King
6 */
7 #include <linux/slab.h>
8 #include "adfs.h"
9 #include "dir_fplus.h"
11 static int
12 adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
14 struct adfs_bigdirheader *h;
15 struct adfs_bigdirtail *t;
16 unsigned long block;
17 unsigned int blk, size;
18 int i, ret = -EIO;
20 dir->nr_buffers = 0;
22 /* start off using fixed bh set - only alloc for big dirs */
23 dir->bh_fplus = &dir->bh[0];
25 block = __adfs_block_map(sb, id, 0);
26 if (!block) {
27 adfs_error(sb, "dir object %X has a hole at offset 0", id);
28 goto out;
31 dir->bh_fplus[0] = sb_bread(sb, block);
32 if (!dir->bh_fplus[0])
33 goto out;
34 dir->nr_buffers += 1;
36 h = (struct adfs_bigdirheader *)dir->bh_fplus[0]->b_data;
37 size = le32_to_cpu(h->bigdirsize);
38 if (size != sz) {
39 adfs_msg(sb, KERN_WARNING,
40 "directory header size %X does not match directory size %X",
41 size, sz);
44 if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
45 h->bigdirversion[2] != 0 || size & 2047 ||
46 h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME)) {
47 adfs_error(sb, "dir %06x has malformed header", id);
48 goto out;
51 size >>= sb->s_blocksize_bits;
52 if (size > ARRAY_SIZE(dir->bh)) {
53 /* this directory is too big for fixed bh set, must allocate */
54 struct buffer_head **bh_fplus =
55 kcalloc(size, sizeof(struct buffer_head *),
56 GFP_KERNEL);
57 if (!bh_fplus) {
58 adfs_msg(sb, KERN_ERR,
59 "not enough memory for dir object %X (%d blocks)",
60 id, size);
61 ret = -ENOMEM;
62 goto out;
64 dir->bh_fplus = bh_fplus;
65 /* copy over the pointer to the block that we've already read */
66 dir->bh_fplus[0] = dir->bh[0];
69 for (blk = 1; blk < size; blk++) {
70 block = __adfs_block_map(sb, id, blk);
71 if (!block) {
72 adfs_error(sb, "dir object %X has a hole at offset %d", id, blk);
73 goto out;
76 dir->bh_fplus[blk] = sb_bread(sb, block);
77 if (!dir->bh_fplus[blk]) {
78 adfs_error(sb, "dir object %x failed read for offset %d, mapped block %lX",
79 id, blk, block);
80 goto out;
83 dir->nr_buffers += 1;
86 t = (struct adfs_bigdirtail *)
87 (dir->bh_fplus[size - 1]->b_data + (sb->s_blocksize - 8));
89 if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
90 t->bigdirendmasseq != h->startmasseq ||
91 t->reserved[0] != 0 || t->reserved[1] != 0) {
92 adfs_error(sb, "dir %06x has malformed tail", id);
93 goto out;
96 dir->parent_id = le32_to_cpu(h->bigdirparent);
97 dir->sb = sb;
98 return 0;
100 out:
101 if (dir->bh_fplus) {
102 for (i = 0; i < dir->nr_buffers; i++)
103 brelse(dir->bh_fplus[i]);
105 if (&dir->bh[0] != dir->bh_fplus)
106 kfree(dir->bh_fplus);
108 dir->bh_fplus = NULL;
111 dir->nr_buffers = 0;
112 dir->sb = NULL;
113 return ret;
116 static int
117 adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
119 struct adfs_bigdirheader *h =
120 (struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
121 int ret = -ENOENT;
123 if (fpos <= le32_to_cpu(h->bigdirentries)) {
124 dir->pos = fpos;
125 ret = 0;
128 return ret;
131 static void
132 dir_memcpy(struct adfs_dir *dir, unsigned int offset, void *to, int len)
134 struct super_block *sb = dir->sb;
135 unsigned int buffer, partial, remainder;
137 buffer = offset >> sb->s_blocksize_bits;
138 offset &= sb->s_blocksize - 1;
140 partial = sb->s_blocksize - offset;
142 if (partial >= len)
143 memcpy(to, dir->bh_fplus[buffer]->b_data + offset, len);
144 else {
145 char *c = (char *)to;
147 remainder = len - partial;
149 memcpy(c,
150 dir->bh_fplus[buffer]->b_data + offset,
151 partial);
153 memcpy(c + partial,
154 dir->bh_fplus[buffer + 1]->b_data,
155 remainder);
159 static int
160 adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
162 struct adfs_bigdirheader *h =
163 (struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
164 struct adfs_bigdirentry bde;
165 unsigned int offset;
166 int ret = -ENOENT;
168 if (dir->pos >= le32_to_cpu(h->bigdirentries))
169 goto out;
171 offset = offsetof(struct adfs_bigdirheader, bigdirname);
172 offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
173 offset += dir->pos * sizeof(struct adfs_bigdirentry);
175 dir_memcpy(dir, offset, &bde, sizeof(struct adfs_bigdirentry));
177 obj->loadaddr = le32_to_cpu(bde.bigdirload);
178 obj->execaddr = le32_to_cpu(bde.bigdirexec);
179 obj->size = le32_to_cpu(bde.bigdirlen);
180 obj->indaddr = le32_to_cpu(bde.bigdirindaddr);
181 obj->attr = le32_to_cpu(bde.bigdirattr);
182 obj->name_len = le32_to_cpu(bde.bigdirobnamelen);
184 offset = offsetof(struct adfs_bigdirheader, bigdirname);
185 offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
186 offset += le32_to_cpu(h->bigdirentries) * sizeof(struct adfs_bigdirentry);
187 offset += le32_to_cpu(bde.bigdirobnameptr);
189 dir_memcpy(dir, offset, obj->name, obj->name_len);
190 adfs_object_fixup(dir, obj);
192 dir->pos += 1;
193 ret = 0;
194 out:
195 return ret;
198 static int
199 adfs_fplus_sync(struct adfs_dir *dir)
201 int err = 0;
202 int i;
204 for (i = dir->nr_buffers - 1; i >= 0; i--) {
205 struct buffer_head *bh = dir->bh_fplus[i];
206 sync_dirty_buffer(bh);
207 if (buffer_req(bh) && !buffer_uptodate(bh))
208 err = -EIO;
211 return err;
214 static void
215 adfs_fplus_free(struct adfs_dir *dir)
217 int i;
219 if (dir->bh_fplus) {
220 for (i = 0; i < dir->nr_buffers; i++)
221 brelse(dir->bh_fplus[i]);
223 if (&dir->bh[0] != dir->bh_fplus)
224 kfree(dir->bh_fplus);
226 dir->bh_fplus = NULL;
229 dir->nr_buffers = 0;
230 dir->sb = NULL;
233 const struct adfs_dir_ops adfs_fplus_dir_ops = {
234 .read = adfs_fplus_read,
235 .setpos = adfs_fplus_setpos,
236 .getnext = adfs_fplus_getnext,
237 .sync = adfs_fplus_sync,
238 .free = adfs_fplus_free