minixfs3.c revision 1.1.6.2 1 1.1.6.2 yamt /* $NetBSD: minixfs3.c,v 1.1.6.2 2012/04/17 00:08:33 yamt Exp $ */
2 1.1.6.2 yamt
3 1.1.6.2 yamt /*-
4 1.1.6.2 yamt * Copyright (c) 2012
5 1.1.6.2 yamt * Vrije Universiteit, Amsterdam, The Netherlands. All rights reserved.
6 1.1.6.2 yamt *
7 1.1.6.2 yamt * Author: Evgeniy Ivanov (based on libsa/ext2fs.c).
8 1.1.6.2 yamt *
9 1.1.6.2 yamt * This code is derived from src/sys/lib/libsa/ext2fs.c contributed to
10 1.1.6.2 yamt * The NetBSD Foundation, see copyrights below.
11 1.1.6.2 yamt *
12 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
13 1.1.6.2 yamt * modification, are permitted provided that the following conditions
14 1.1.6.2 yamt * are met:
15 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
16 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
17 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
18 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
19 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
20 1.1.6.2 yamt *
21 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22 1.1.6.2 yamt * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 1.1.6.2 yamt * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE
25 1.1.6.2 yamt * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
32 1.1.6.2 yamt */
33 1.1.6.2 yamt
34 1.1.6.2 yamt /*
35 1.1.6.2 yamt * Copyright (c) 1997 Manuel Bouyer.
36 1.1.6.2 yamt *
37 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
38 1.1.6.2 yamt * modification, are permitted provided that the following conditions
39 1.1.6.2 yamt * are met:
40 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
41 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
42 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
43 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
44 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
45 1.1.6.2 yamt *
46 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 1.1.6.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 1.1.6.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 1.1.6.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50 1.1.6.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51 1.1.6.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 1.1.6.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 1.1.6.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 1.1.6.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55 1.1.6.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 1.1.6.2 yamt */
57 1.1.6.2 yamt
58 1.1.6.2 yamt /*-
59 1.1.6.2 yamt * Copyright (c) 1993
60 1.1.6.2 yamt * The Regents of the University of California. All rights reserved.
61 1.1.6.2 yamt *
62 1.1.6.2 yamt * This code is derived from software contributed to Berkeley by
63 1.1.6.2 yamt * The Mach Operating System project at Carnegie-Mellon University.
64 1.1.6.2 yamt *
65 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
66 1.1.6.2 yamt * modification, are permitted provided that the following conditions
67 1.1.6.2 yamt * are met:
68 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
69 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
70 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
71 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
72 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
73 1.1.6.2 yamt * 3. Neither the name of the University nor the names of its contributors
74 1.1.6.2 yamt * may be used to endorse or promote products derived from this software
75 1.1.6.2 yamt * without specific prior written permission.
76 1.1.6.2 yamt *
77 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 1.1.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 1.1.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 1.1.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 1.1.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 1.1.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 1.1.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 1.1.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 1.1.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 1.1.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 1.1.6.2 yamt * SUCH DAMAGE.
88 1.1.6.2 yamt *
89 1.1.6.2 yamt *
90 1.1.6.2 yamt * Copyright (c) 1990, 1991 Carnegie Mellon University
91 1.1.6.2 yamt * All Rights Reserved.
92 1.1.6.2 yamt *
93 1.1.6.2 yamt * Author: David Golub
94 1.1.6.2 yamt *
95 1.1.6.2 yamt * Permission to use, copy, modify and distribute this software and its
96 1.1.6.2 yamt * documentation is hereby granted, provided that both the copyright
97 1.1.6.2 yamt * notice and this permission notice appear in all copies of the
98 1.1.6.2 yamt * software, derivative works or modified versions, and any portions
99 1.1.6.2 yamt * thereof, and that both notices appear in supporting documentation.
100 1.1.6.2 yamt *
101 1.1.6.2 yamt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
102 1.1.6.2 yamt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
103 1.1.6.2 yamt * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
104 1.1.6.2 yamt *
105 1.1.6.2 yamt * Carnegie Mellon requests users of this software to return to
106 1.1.6.2 yamt *
107 1.1.6.2 yamt * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
108 1.1.6.2 yamt * School of Computer Science
109 1.1.6.2 yamt * Carnegie Mellon University
110 1.1.6.2 yamt * Pittsburgh PA 15213-3890
111 1.1.6.2 yamt *
112 1.1.6.2 yamt * any improvements or extensions that they make and grant Carnegie the
113 1.1.6.2 yamt * rights to redistribute these changes.
114 1.1.6.2 yamt */
115 1.1.6.2 yamt
116 1.1.6.2 yamt /*
117 1.1.6.2 yamt * Stand-alone file reading package for MFS file system.
118 1.1.6.2 yamt */
119 1.1.6.2 yamt
120 1.1.6.2 yamt #include <sys/param.h>
121 1.1.6.2 yamt #include <sys/time.h>
122 1.1.6.2 yamt #ifdef _STANDALONE
123 1.1.6.2 yamt #include <lib/libkern/libkern.h>
124 1.1.6.2 yamt #else
125 1.1.6.2 yamt #include <string.h>
126 1.1.6.2 yamt #endif
127 1.1.6.2 yamt
128 1.1.6.2 yamt #include "stand.h"
129 1.1.6.2 yamt #include "minixfs3.h"
130 1.1.6.2 yamt
131 1.1.6.2 yamt #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
132 1.1.6.2 yamt #define LIBSA_NO_FS_SYMLINK
133 1.1.6.2 yamt #endif
134 1.1.6.2 yamt
135 1.1.6.2 yamt #if defined(LIBSA_NO_TWIDDLE)
136 1.1.6.2 yamt #define twiddle()
137 1.1.6.2 yamt #endif
138 1.1.6.2 yamt
139 1.1.6.2 yamt typedef uint32_t ino32_t;
140 1.1.6.2 yamt #ifndef FSBTODB
141 1.1.6.2 yamt #define FSBTODB(fs, indp) fsbtodb(fs, indp)
142 1.1.6.2 yamt #endif
143 1.1.6.2 yamt
144 1.1.6.2 yamt /*
145 1.1.6.2 yamt * To avoid having a lot of filesystem-block sized buffers lurking (which
146 1.1.6.2 yamt * could be 32k) we only keep a few entries of the indirect block map.
147 1.1.6.2 yamt * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block
148 1.1.6.2 yamt * ~13 times pulling in a 6M kernel.
149 1.1.6.2 yamt * The cache size must be smaller than the smallest filesystem block,
150 1.1.6.2 yamt * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks).
151 1.1.6.2 yamt */
152 1.1.6.2 yamt #define LN2_IND_CACHE_SZ 6
153 1.1.6.2 yamt #define IND_CACHE_SZ (1 << LN2_IND_CACHE_SZ)
154 1.1.6.2 yamt #define IND_CACHE_MASK (IND_CACHE_SZ - 1)
155 1.1.6.2 yamt
156 1.1.6.2 yamt /*
157 1.1.6.2 yamt * In-core open file.
158 1.1.6.2 yamt */
159 1.1.6.2 yamt struct file {
160 1.1.6.2 yamt off_t f_seekp; /* seek pointer */
161 1.1.6.2 yamt struct mfs_sblock *f_fs; /* pointer to super-block */
162 1.1.6.2 yamt struct mfs_dinode f_di; /* copy of on-disk inode */
163 1.1.6.2 yamt uint f_nishift; /* for blocks in indirect block */
164 1.1.6.2 yamt block_t f_ind_cache_block;
165 1.1.6.2 yamt block_t f_ind_cache[IND_CACHE_SZ];
166 1.1.6.2 yamt
167 1.1.6.2 yamt char *f_buf; /* buffer for data block */
168 1.1.6.2 yamt size_t f_buf_size; /* size of data block */
169 1.1.6.2 yamt daddr_t f_buf_blkno; /* block number of data block */
170 1.1.6.2 yamt };
171 1.1.6.2 yamt
172 1.1.6.2 yamt #if defined(LIBSA_ENABLE_LS_OP)
173 1.1.6.2 yamt
174 1.1.6.2 yamt #define NELEM(x) (sizeof (x) / sizeof(*x))
175 1.1.6.2 yamt
176 1.1.6.2 yamt typedef struct entry_t entry_t;
177 1.1.6.2 yamt struct entry_t {
178 1.1.6.2 yamt entry_t *e_next;
179 1.1.6.2 yamt ino32_t e_ino;
180 1.1.6.2 yamt char e_name[1];
181 1.1.6.2 yamt };
182 1.1.6.2 yamt
183 1.1.6.2 yamt #endif /* LIBSA_ENABLE_LS_OP */
184 1.1.6.2 yamt
185 1.1.6.2 yamt
186 1.1.6.2 yamt static int read_inode(ino32_t, struct open_file *);
187 1.1.6.2 yamt static int block_map(struct open_file *, block_t, block_t *);
188 1.1.6.2 yamt static int buf_read_file(struct open_file *, void *, size_t *);
189 1.1.6.2 yamt static int search_directory(const char *, int, struct open_file *, ino32_t *);
190 1.1.6.2 yamt static int read_sblock(struct open_file *, struct mfs_sblock *);
191 1.1.6.2 yamt
192 1.1.6.2 yamt /*
193 1.1.6.2 yamt * Read a new inode into a file structure.
194 1.1.6.2 yamt */
195 1.1.6.2 yamt static int
196 1.1.6.2 yamt read_inode(ino32_t inumber, struct open_file *f)
197 1.1.6.2 yamt {
198 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
199 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
200 1.1.6.2 yamt char *buf;
201 1.1.6.2 yamt size_t rsize;
202 1.1.6.2 yamt int rc;
203 1.1.6.2 yamt daddr_t inode_sector;
204 1.1.6.2 yamt struct mfs_dinode *dip;
205 1.1.6.2 yamt
206 1.1.6.2 yamt inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
207 1.1.6.2 yamt
208 1.1.6.2 yamt /*
209 1.1.6.2 yamt * Read inode and save it.
210 1.1.6.2 yamt */
211 1.1.6.2 yamt buf = fp->f_buf;
212 1.1.6.2 yamt twiddle();
213 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
214 1.1.6.2 yamt inode_sector, fs->mfs_block_size, buf, &rsize);
215 1.1.6.2 yamt if (rc)
216 1.1.6.2 yamt return rc;
217 1.1.6.2 yamt if (rsize != fs->mfs_block_size)
218 1.1.6.2 yamt return EIO;
219 1.1.6.2 yamt
220 1.1.6.2 yamt dip = (struct mfs_dinode *)(buf +
221 1.1.6.2 yamt INODE_SIZE * ino_to_fsbo(fs, inumber));
222 1.1.6.2 yamt mfs_iload(dip, &fp->f_di);
223 1.1.6.2 yamt
224 1.1.6.2 yamt /*
225 1.1.6.2 yamt * Clear out the old buffers
226 1.1.6.2 yamt */
227 1.1.6.2 yamt fp->f_ind_cache_block = ~0;
228 1.1.6.2 yamt fp->f_buf_blkno = -1;
229 1.1.6.2 yamt return rc;
230 1.1.6.2 yamt }
231 1.1.6.2 yamt
232 1.1.6.2 yamt /*
233 1.1.6.2 yamt * Given an offset in a file, find the disk block number (not zone!)
234 1.1.6.2 yamt * that contains that block.
235 1.1.6.2 yamt */
236 1.1.6.2 yamt static int
237 1.1.6.2 yamt block_map(struct open_file *f, block_t file_block, block_t *disk_block_p)
238 1.1.6.2 yamt {
239 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
240 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
241 1.1.6.2 yamt uint level;
242 1.1.6.2 yamt block_t ind_cache;
243 1.1.6.2 yamt block_t ind_block_num;
244 1.1.6.2 yamt zone_t zone;
245 1.1.6.2 yamt size_t rsize;
246 1.1.6.2 yamt int rc;
247 1.1.6.2 yamt int boff;
248 1.1.6.2 yamt int scale = fs->mfs_log_zone_size; /* for block-zone conversion */
249 1.1.6.2 yamt block_t *buf = (void *)fp->f_buf;
250 1.1.6.2 yamt
251 1.1.6.2 yamt /*
252 1.1.6.2 yamt * Index structure of an inode:
253 1.1.6.2 yamt *
254 1.1.6.2 yamt * mdi_blocks[0..NR_DZONES-1]
255 1.1.6.2 yamt * hold zone numbers for zones
256 1.1.6.2 yamt * 0..NR_DZONES-1
257 1.1.6.2 yamt *
258 1.1.6.2 yamt * mdi_blocks[NR_DZONES+0]
259 1.1.6.2 yamt * block NDADDR+0 is the single indirect block
260 1.1.6.2 yamt * holds zone numbers for zones
261 1.1.6.2 yamt * NR_DZONES .. NR_DZONES + NINDIR(fs)-1
262 1.1.6.2 yamt *
263 1.1.6.2 yamt * mdi_blocks[NR_DZONES+1]
264 1.1.6.2 yamt * block NDADDR+1 is the double indirect block
265 1.1.6.2 yamt * holds zone numbers for INDEX blocks for zones
266 1.1.6.2 yamt * NR_DZONES + NINDIR(fs) ..
267 1.1.6.2 yamt * NR_TZONES + NINDIR(fs) + NINDIR(fs)**2 - 1
268 1.1.6.2 yamt */
269 1.1.6.2 yamt
270 1.1.6.2 yamt zone = file_block >> scale;
271 1.1.6.2 yamt boff = (int) (file_block - (zone << scale) ); /* relative blk in zone */
272 1.1.6.2 yamt
273 1.1.6.2 yamt if (zone < NR_DZONES) {
274 1.1.6.2 yamt /* Direct zone */
275 1.1.6.2 yamt zone_t z = fs2h32(fp->f_di.mdi_zone[zone]);
276 1.1.6.2 yamt if (z == NO_ZONE) {
277 1.1.6.2 yamt *disk_block_p = NO_BLOCK;
278 1.1.6.2 yamt return 0;
279 1.1.6.2 yamt }
280 1.1.6.2 yamt *disk_block_p = (block_t) ((z << scale) + boff);
281 1.1.6.2 yamt return 0;
282 1.1.6.2 yamt }
283 1.1.6.2 yamt
284 1.1.6.2 yamt zone -= NR_DZONES;
285 1.1.6.2 yamt
286 1.1.6.2 yamt ind_cache = zone >> LN2_IND_CACHE_SZ;
287 1.1.6.2 yamt if (ind_cache == fp->f_ind_cache_block) {
288 1.1.6.2 yamt *disk_block_p =
289 1.1.6.2 yamt fs2h32(fp->f_ind_cache[zone & IND_CACHE_MASK]);
290 1.1.6.2 yamt return 0;
291 1.1.6.2 yamt }
292 1.1.6.2 yamt
293 1.1.6.2 yamt for (level = 0;;) {
294 1.1.6.2 yamt level += fp->f_nishift;
295 1.1.6.2 yamt
296 1.1.6.2 yamt if (zone < (block_t)1 << level)
297 1.1.6.2 yamt break;
298 1.1.6.2 yamt if (level > NIADDR * fp->f_nishift)
299 1.1.6.2 yamt /* Zone number too high */
300 1.1.6.2 yamt return EFBIG;
301 1.1.6.2 yamt zone -= (block_t)1 << level;
302 1.1.6.2 yamt }
303 1.1.6.2 yamt
304 1.1.6.2 yamt ind_block_num =
305 1.1.6.2 yamt fs2h32(fp->f_di.mdi_zone[NR_DZONES + (level / fp->f_nishift - 1)]);
306 1.1.6.2 yamt
307 1.1.6.2 yamt for (;;) {
308 1.1.6.2 yamt level -= fp->f_nishift;
309 1.1.6.2 yamt if (ind_block_num == 0) {
310 1.1.6.2 yamt *disk_block_p = NO_BLOCK; /* missing */
311 1.1.6.2 yamt return 0;
312 1.1.6.2 yamt }
313 1.1.6.2 yamt
314 1.1.6.2 yamt twiddle();
315 1.1.6.2 yamt /*
316 1.1.6.2 yamt * If we were feeling brave, we could work out the number
317 1.1.6.2 yamt * of the disk sector and read a single disk sector instead
318 1.1.6.2 yamt * of a filesystem block.
319 1.1.6.2 yamt * However we don't do this very often anyway...
320 1.1.6.2 yamt */
321 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
322 1.1.6.2 yamt FSBTODB(fs, ind_block_num), fs->mfs_block_size,
323 1.1.6.2 yamt buf, &rsize);
324 1.1.6.2 yamt if (rc)
325 1.1.6.2 yamt return rc;
326 1.1.6.2 yamt if (rsize != fs->mfs_block_size)
327 1.1.6.2 yamt return EIO;
328 1.1.6.2 yamt
329 1.1.6.2 yamt ind_block_num = fs2h32(buf[zone >> level]);
330 1.1.6.2 yamt if (level == 0)
331 1.1.6.2 yamt break;
332 1.1.6.2 yamt zone &= (1 << level) - 1;
333 1.1.6.2 yamt }
334 1.1.6.2 yamt
335 1.1.6.2 yamt /* Save the part of the block that contains this sector */
336 1.1.6.2 yamt memcpy(fp->f_ind_cache, &buf[zone & ~IND_CACHE_MASK],
337 1.1.6.2 yamt IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
338 1.1.6.2 yamt fp->f_ind_cache_block = ind_cache;
339 1.1.6.2 yamt
340 1.1.6.2 yamt zone = (zone_t)ind_block_num;
341 1.1.6.2 yamt *disk_block_p = (block_t)((zone << scale) + boff);
342 1.1.6.2 yamt return 0;
343 1.1.6.2 yamt }
344 1.1.6.2 yamt
345 1.1.6.2 yamt /*
346 1.1.6.2 yamt * Read a portion of a file into an internal buffer.
347 1.1.6.2 yamt * Return the location in the buffer and the amount in the buffer.
348 1.1.6.2 yamt */
349 1.1.6.2 yamt static int
350 1.1.6.2 yamt buf_read_file(struct open_file *f, void *v, size_t *size_p)
351 1.1.6.2 yamt {
352 1.1.6.2 yamt char **buf_p = v;
353 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
354 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
355 1.1.6.2 yamt long off;
356 1.1.6.2 yamt block_t file_block;
357 1.1.6.2 yamt block_t disk_block;
358 1.1.6.2 yamt size_t block_size;
359 1.1.6.2 yamt int rc;
360 1.1.6.2 yamt
361 1.1.6.2 yamt off = blkoff(fs, fp->f_seekp);
362 1.1.6.2 yamt file_block = lblkno(fs, fp->f_seekp);
363 1.1.6.2 yamt block_size = fs->mfs_block_size;
364 1.1.6.2 yamt
365 1.1.6.2 yamt if (file_block != fp->f_buf_blkno) {
366 1.1.6.2 yamt rc = block_map(f, file_block, &disk_block);
367 1.1.6.2 yamt if (rc)
368 1.1.6.2 yamt return rc;
369 1.1.6.2 yamt
370 1.1.6.2 yamt if (disk_block == 0) {
371 1.1.6.2 yamt memset(fp->f_buf, 0, block_size);
372 1.1.6.2 yamt fp->f_buf_size = block_size;
373 1.1.6.2 yamt } else {
374 1.1.6.2 yamt twiddle();
375 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
376 1.1.6.2 yamt FSBTODB(fs, disk_block),
377 1.1.6.2 yamt block_size, fp->f_buf, &fp->f_buf_size);
378 1.1.6.2 yamt if (rc)
379 1.1.6.2 yamt return rc;
380 1.1.6.2 yamt }
381 1.1.6.2 yamt
382 1.1.6.2 yamt fp->f_buf_blkno = file_block;
383 1.1.6.2 yamt }
384 1.1.6.2 yamt
385 1.1.6.2 yamt /*
386 1.1.6.2 yamt * Return address of byte in buffer corresponding to
387 1.1.6.2 yamt * offset, and size of remainder of buffer after that
388 1.1.6.2 yamt * byte.
389 1.1.6.2 yamt */
390 1.1.6.2 yamt *buf_p = fp->f_buf + off;
391 1.1.6.2 yamt *size_p = block_size - off;
392 1.1.6.2 yamt
393 1.1.6.2 yamt /*
394 1.1.6.2 yamt * But truncate buffer at end of file.
395 1.1.6.2 yamt */
396 1.1.6.2 yamt if (*size_p > fp->f_di.mdi_size - fp->f_seekp)
397 1.1.6.2 yamt *size_p = fp->f_di.mdi_size - fp->f_seekp;
398 1.1.6.2 yamt
399 1.1.6.2 yamt return 0;
400 1.1.6.2 yamt }
401 1.1.6.2 yamt
402 1.1.6.2 yamt /*
403 1.1.6.2 yamt * Search a directory for a name and return its
404 1.1.6.2 yamt * inode number.
405 1.1.6.2 yamt */
406 1.1.6.2 yamt static int
407 1.1.6.2 yamt search_directory(const char *name, int length, struct open_file *f,
408 1.1.6.2 yamt ino32_t *inumber_p)
409 1.1.6.2 yamt {
410 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
411 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
412 1.1.6.2 yamt struct mfs_direct *dp;
413 1.1.6.2 yamt struct mfs_direct *dbuf;
414 1.1.6.2 yamt size_t buf_size;
415 1.1.6.2 yamt int namlen;
416 1.1.6.2 yamt int rc;
417 1.1.6.2 yamt
418 1.1.6.2 yamt fp->f_seekp = 0;
419 1.1.6.2 yamt
420 1.1.6.2 yamt while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
421 1.1.6.2 yamt rc = buf_read_file(f, (void *)&dbuf, &buf_size);
422 1.1.6.2 yamt if (rc)
423 1.1.6.2 yamt return rc;
424 1.1.6.2 yamt if (buf_size == 0)
425 1.1.6.2 yamt return EIO;
426 1.1.6.2 yamt
427 1.1.6.2 yamt /* XXX we assume, that buf_read_file reads an fs block and
428 1.1.6.2 yamt * doesn't truncate buffer. Currently i_size in MFS doesn't
429 1.1.6.2 yamt * the same as size of allocated blocks, it makes buf_read_file
430 1.1.6.2 yamt * to truncate buf_size.
431 1.1.6.2 yamt */
432 1.1.6.2 yamt if (buf_size < fs->mfs_block_size)
433 1.1.6.2 yamt buf_size = fs->mfs_block_size;
434 1.1.6.2 yamt
435 1.1.6.2 yamt for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
436 1.1.6.2 yamt char *cp;
437 1.1.6.2 yamt if (fs2h32(dp->mfsd_ino) == (ino32_t) 0)
438 1.1.6.2 yamt continue;
439 1.1.6.2 yamt /* Compute the length of the name */
440 1.1.6.2 yamt cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
441 1.1.6.2 yamt if (cp == NULL)
442 1.1.6.2 yamt namlen = sizeof(dp->mfsd_name);
443 1.1.6.2 yamt else
444 1.1.6.2 yamt namlen = cp - (dp->mfsd_name);
445 1.1.6.2 yamt
446 1.1.6.2 yamt if (namlen == length &&
447 1.1.6.2 yamt !memcmp(name, dp->mfsd_name, length)) {
448 1.1.6.2 yamt /* found entry */
449 1.1.6.2 yamt *inumber_p = fs2h32(dp->mfsd_ino);
450 1.1.6.2 yamt return 0;
451 1.1.6.2 yamt }
452 1.1.6.2 yamt }
453 1.1.6.2 yamt fp->f_seekp += buf_size;
454 1.1.6.2 yamt }
455 1.1.6.2 yamt return ENOENT;
456 1.1.6.2 yamt }
457 1.1.6.2 yamt
458 1.1.6.2 yamt int
459 1.1.6.2 yamt read_sblock(struct open_file *f, struct mfs_sblock *fs)
460 1.1.6.2 yamt {
461 1.1.6.2 yamt static uint8_t sbbuf[MINBSIZE];
462 1.1.6.2 yamt size_t buf_size;
463 1.1.6.2 yamt int rc;
464 1.1.6.2 yamt
465 1.1.6.2 yamt /* We must read amount multiple of sector size, hence we can't
466 1.1.6.2 yamt * read SBSIZE and read MINBSIZE.
467 1.1.6.2 yamt */
468 1.1.6.2 yamt if (SBSIZE > MINBSIZE)
469 1.1.6.2 yamt return EINVAL;
470 1.1.6.2 yamt
471 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
472 1.1.6.2 yamt SUPER_BLOCK_OFF / DEV_BSIZE, MINBSIZE, sbbuf, &buf_size);
473 1.1.6.2 yamt if (rc)
474 1.1.6.2 yamt return rc;
475 1.1.6.2 yamt
476 1.1.6.2 yamt if (buf_size != MINBSIZE)
477 1.1.6.2 yamt return EIO;
478 1.1.6.2 yamt
479 1.1.6.2 yamt mfs_sbload((void *)sbbuf, fs);
480 1.1.6.2 yamt
481 1.1.6.2 yamt if (fs->mfs_magic != SUPER_MAGIC)
482 1.1.6.2 yamt return EINVAL;
483 1.1.6.2 yamt if (fs->mfs_block_size < MINBSIZE)
484 1.1.6.2 yamt return EINVAL;
485 1.1.6.2 yamt if ((fs->mfs_block_size % 512) != 0)
486 1.1.6.2 yamt return EINVAL;
487 1.1.6.2 yamt if (SBSIZE > fs->mfs_block_size)
488 1.1.6.2 yamt return EINVAL;
489 1.1.6.2 yamt if ((fs->mfs_block_size % INODE_SIZE) != 0)
490 1.1.6.2 yamt return EINVAL;
491 1.1.6.2 yamt
492 1.1.6.2 yamt /* For even larger disks, a similar problem occurs with s_firstdatazone.
493 1.1.6.2 yamt * If the on-disk field contains zero, we assume that the value was too
494 1.1.6.2 yamt * large to fit, and compute it on the fly.
495 1.1.6.2 yamt */
496 1.1.6.2 yamt if (fs->mfs_firstdatazone_old == 0) {
497 1.1.6.2 yamt block_t offset;
498 1.1.6.2 yamt offset = START_BLOCK + fs->mfs_imap_blocks + fs->mfs_zmap_blocks;
499 1.1.6.2 yamt offset += (fs->mfs_ninodes + fs->mfs_inodes_per_block - 1) /
500 1.1.6.2 yamt fs->mfs_inodes_per_block;
501 1.1.6.2 yamt
502 1.1.6.2 yamt fs->mfs_firstdatazone =
503 1.1.6.2 yamt (offset + (1 << fs->mfs_log_zone_size) - 1) >>
504 1.1.6.2 yamt fs->mfs_log_zone_size;
505 1.1.6.2 yamt } else {
506 1.1.6.2 yamt fs->mfs_firstdatazone = (zone_t) fs->mfs_firstdatazone_old;
507 1.1.6.2 yamt }
508 1.1.6.2 yamt
509 1.1.6.2 yamt if (fs->mfs_imap_blocks < 1 || fs->mfs_zmap_blocks < 1
510 1.1.6.2 yamt || fs->mfs_ninodes < 1 || fs->mfs_zones < 1
511 1.1.6.2 yamt || fs->mfs_firstdatazone <= 4
512 1.1.6.2 yamt || fs->mfs_firstdatazone >= fs->mfs_zones
513 1.1.6.2 yamt || (unsigned) fs->mfs_log_zone_size > 4)
514 1.1.6.2 yamt return EINVAL;
515 1.1.6.2 yamt
516 1.1.6.2 yamt /* compute in-memory mfs_sblock values */
517 1.1.6.2 yamt fs->mfs_inodes_per_block = fs->mfs_block_size / INODE_SIZE;
518 1.1.6.2 yamt
519 1.1.6.2 yamt
520 1.1.6.2 yamt {
521 1.1.6.2 yamt int32_t mult = fs->mfs_block_size >> LOG_MINBSIZE;
522 1.1.6.2 yamt int ln2 = LOG_MINBSIZE;
523 1.1.6.2 yamt
524 1.1.6.2 yamt for (; mult != 1; ln2++)
525 1.1.6.2 yamt mult >>= 1;
526 1.1.6.2 yamt
527 1.1.6.2 yamt fs->mfs_bshift = ln2;
528 1.1.6.2 yamt /* XXX assume hw bsize = 512 */
529 1.1.6.2 yamt fs->mfs_fsbtodb = ln2 - LOG_MINBSIZE + 1;
530 1.1.6.2 yamt }
531 1.1.6.2 yamt
532 1.1.6.2 yamt fs->mfs_qbmask = fs->mfs_block_size - 1;
533 1.1.6.2 yamt fs->mfs_bmask = ~fs->mfs_qbmask;
534 1.1.6.2 yamt
535 1.1.6.2 yamt return 0;
536 1.1.6.2 yamt }
537 1.1.6.2 yamt
538 1.1.6.2 yamt /*
539 1.1.6.2 yamt * Open a file.
540 1.1.6.2 yamt */
541 1.1.6.2 yamt __compactcall int
542 1.1.6.2 yamt minixfs3_open(const char *path, struct open_file *f)
543 1.1.6.2 yamt {
544 1.1.6.2 yamt #ifndef LIBSA_FS_SINGLECOMPONENT
545 1.1.6.2 yamt const char *cp, *ncp;
546 1.1.6.2 yamt int c;
547 1.1.6.2 yamt #endif
548 1.1.6.2 yamt ino32_t inumber;
549 1.1.6.2 yamt struct file *fp;
550 1.1.6.2 yamt struct mfs_sblock *fs;
551 1.1.6.2 yamt int rc;
552 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SYMLINK
553 1.1.6.2 yamt ino32_t parent_inumber;
554 1.1.6.2 yamt int nlinks = 0;
555 1.1.6.2 yamt char namebuf[MAXPATHLEN+1];
556 1.1.6.2 yamt char *buf;
557 1.1.6.2 yamt #endif
558 1.1.6.2 yamt
559 1.1.6.2 yamt /* allocate file system specific data structure */
560 1.1.6.2 yamt fp = alloc(sizeof(struct file));
561 1.1.6.2 yamt memset(fp, 0, sizeof(struct file));
562 1.1.6.2 yamt f->f_fsdata = (void *)fp;
563 1.1.6.2 yamt
564 1.1.6.2 yamt /* allocate space and read super block */
565 1.1.6.2 yamt fs = alloc(sizeof(*fs));
566 1.1.6.2 yamt memset(fs, 0, sizeof(*fs));
567 1.1.6.2 yamt fp->f_fs = fs;
568 1.1.6.2 yamt twiddle();
569 1.1.6.2 yamt
570 1.1.6.2 yamt rc = read_sblock(f, fs);
571 1.1.6.2 yamt if (rc)
572 1.1.6.2 yamt goto out;
573 1.1.6.2 yamt
574 1.1.6.2 yamt /* alloc a block sized buffer used for all fs transfers */
575 1.1.6.2 yamt fp->f_buf = alloc(fs->mfs_block_size);
576 1.1.6.2 yamt
577 1.1.6.2 yamt /*
578 1.1.6.2 yamt * Calculate indirect block levels.
579 1.1.6.2 yamt */
580 1.1.6.2 yamt {
581 1.1.6.2 yamt int32_t mult;
582 1.1.6.2 yamt int ln2;
583 1.1.6.2 yamt
584 1.1.6.2 yamt /*
585 1.1.6.2 yamt * We note that the number of indirect blocks is always
586 1.1.6.2 yamt * a power of 2. This lets us use shifts and masks instead
587 1.1.6.2 yamt * of divide and remainder and avoinds pulling in the
588 1.1.6.2 yamt * 64bit division routine into the boot code.
589 1.1.6.2 yamt */
590 1.1.6.2 yamt mult = NINDIR(fs);
591 1.1.6.2 yamt #ifdef DEBUG
592 1.1.6.2 yamt if (!powerof2(mult)) {
593 1.1.6.2 yamt /* Hummm was't a power of 2 */
594 1.1.6.2 yamt rc = EINVAL;
595 1.1.6.2 yamt goto out;
596 1.1.6.2 yamt }
597 1.1.6.2 yamt #endif
598 1.1.6.2 yamt for (ln2 = 0; mult != 1; ln2++)
599 1.1.6.2 yamt mult >>= 1;
600 1.1.6.2 yamt
601 1.1.6.2 yamt fp->f_nishift = ln2;
602 1.1.6.2 yamt }
603 1.1.6.2 yamt
604 1.1.6.2 yamt inumber = ROOT_INODE;
605 1.1.6.2 yamt if ((rc = read_inode(inumber, f)) != 0)
606 1.1.6.2 yamt goto out;
607 1.1.6.2 yamt
608 1.1.6.2 yamt #ifndef LIBSA_FS_SINGLECOMPONENT
609 1.1.6.2 yamt cp = path;
610 1.1.6.2 yamt while (*cp) {
611 1.1.6.2 yamt
612 1.1.6.2 yamt /*
613 1.1.6.2 yamt * Remove extra separators
614 1.1.6.2 yamt */
615 1.1.6.2 yamt while (*cp == '/')
616 1.1.6.2 yamt cp++;
617 1.1.6.2 yamt if (*cp == '\0')
618 1.1.6.2 yamt break;
619 1.1.6.2 yamt
620 1.1.6.2 yamt /*
621 1.1.6.2 yamt * Check that current node is a directory.
622 1.1.6.2 yamt */
623 1.1.6.2 yamt if ((fp->f_di.mdi_mode & I_TYPE) != I_DIRECTORY) {
624 1.1.6.2 yamt rc = ENOTDIR;
625 1.1.6.2 yamt goto out;
626 1.1.6.2 yamt }
627 1.1.6.2 yamt
628 1.1.6.2 yamt /*
629 1.1.6.2 yamt * Get next component of path name.
630 1.1.6.2 yamt */
631 1.1.6.2 yamt ncp = cp;
632 1.1.6.2 yamt while ((c = *cp) != '\0' && c != '/')
633 1.1.6.2 yamt cp++;
634 1.1.6.2 yamt
635 1.1.6.2 yamt /*
636 1.1.6.2 yamt * Look up component in current directory.
637 1.1.6.2 yamt * Save directory inumber in case we find a
638 1.1.6.2 yamt * symbolic link.
639 1.1.6.2 yamt */
640 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SYMLINK
641 1.1.6.2 yamt parent_inumber = inumber;
642 1.1.6.2 yamt #endif
643 1.1.6.2 yamt rc = search_directory(ncp, cp - ncp, f, &inumber);
644 1.1.6.2 yamt if (rc)
645 1.1.6.2 yamt goto out;
646 1.1.6.2 yamt
647 1.1.6.2 yamt /*
648 1.1.6.2 yamt * Open next component.
649 1.1.6.2 yamt */
650 1.1.6.2 yamt if ((rc = read_inode(inumber, f)) != 0)
651 1.1.6.2 yamt goto out;
652 1.1.6.2 yamt
653 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SYMLINK
654 1.1.6.2 yamt /*
655 1.1.6.2 yamt * Check for symbolic link.
656 1.1.6.2 yamt */
657 1.1.6.2 yamt if ((fp->f_di.mdi_mode & I_TYPE) == I_SYMBOLIC_LINK) {
658 1.1.6.2 yamt int link_len = fp->f_di.mdi_size;
659 1.1.6.2 yamt int len;
660 1.1.6.2 yamt size_t buf_size;
661 1.1.6.2 yamt block_t disk_block;
662 1.1.6.2 yamt
663 1.1.6.2 yamt len = strlen(cp);
664 1.1.6.2 yamt
665 1.1.6.2 yamt if (link_len + len > MAXPATHLEN ||
666 1.1.6.2 yamt ++nlinks > MAXSYMLINKS) {
667 1.1.6.2 yamt rc = ENOENT;
668 1.1.6.2 yamt goto out;
669 1.1.6.2 yamt }
670 1.1.6.2 yamt
671 1.1.6.2 yamt memmove(&namebuf[link_len], cp, len + 1);
672 1.1.6.2 yamt
673 1.1.6.2 yamt /*
674 1.1.6.2 yamt * Read file for symbolic link
675 1.1.6.2 yamt */
676 1.1.6.2 yamt buf = fp->f_buf;
677 1.1.6.2 yamt rc = block_map(f, (block_t)0, &disk_block);
678 1.1.6.2 yamt if (rc)
679 1.1.6.2 yamt goto out;
680 1.1.6.2 yamt
681 1.1.6.2 yamt twiddle();
682 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
683 1.1.6.2 yamt F_READ, FSBTODB(fs, disk_block),
684 1.1.6.2 yamt fs->mfs_block_size, buf, &buf_size);
685 1.1.6.2 yamt if (rc)
686 1.1.6.2 yamt goto out;
687 1.1.6.2 yamt
688 1.1.6.2 yamt memcpy(namebuf, buf, link_len);
689 1.1.6.2 yamt
690 1.1.6.2 yamt /*
691 1.1.6.2 yamt * If relative pathname, restart at parent directory.
692 1.1.6.2 yamt * If absolute pathname, restart at root.
693 1.1.6.2 yamt */
694 1.1.6.2 yamt cp = namebuf;
695 1.1.6.2 yamt if (*cp != '/')
696 1.1.6.2 yamt inumber = parent_inumber;
697 1.1.6.2 yamt else
698 1.1.6.2 yamt inumber = (ino32_t) ROOT_INODE;
699 1.1.6.2 yamt
700 1.1.6.2 yamt if ((rc = read_inode(inumber, f)) != 0)
701 1.1.6.2 yamt goto out;
702 1.1.6.2 yamt }
703 1.1.6.2 yamt #endif /* !LIBSA_NO_FS_SYMLINK */
704 1.1.6.2 yamt }
705 1.1.6.2 yamt
706 1.1.6.2 yamt /*
707 1.1.6.2 yamt * Found terminal component.
708 1.1.6.2 yamt */
709 1.1.6.2 yamt rc = 0;
710 1.1.6.2 yamt
711 1.1.6.2 yamt #else /* !LIBSA_FS_SINGLECOMPONENT */
712 1.1.6.2 yamt
713 1.1.6.2 yamt /* look up component in the current (root) directory */
714 1.1.6.2 yamt rc = search_directory(path, strlen(path), f, &inumber);
715 1.1.6.2 yamt if (rc)
716 1.1.6.2 yamt goto out;
717 1.1.6.2 yamt
718 1.1.6.2 yamt /* open it */
719 1.1.6.2 yamt rc = read_inode(inumber, f);
720 1.1.6.2 yamt
721 1.1.6.2 yamt #endif /* !LIBSA_FS_SINGLECOMPONENT */
722 1.1.6.2 yamt
723 1.1.6.2 yamt fp->f_seekp = 0; /* reset seek pointer */
724 1.1.6.2 yamt
725 1.1.6.2 yamt out:
726 1.1.6.2 yamt if (rc)
727 1.1.6.2 yamt minixfs3_close(f);
728 1.1.6.2 yamt
729 1.1.6.2 yamt return rc;
730 1.1.6.2 yamt }
731 1.1.6.2 yamt
732 1.1.6.2 yamt __compactcall int
733 1.1.6.2 yamt minixfs3_close(struct open_file *f)
734 1.1.6.2 yamt {
735 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
736 1.1.6.2 yamt
737 1.1.6.2 yamt f->f_fsdata = NULL;
738 1.1.6.2 yamt if (fp == NULL)
739 1.1.6.2 yamt return 0;
740 1.1.6.2 yamt
741 1.1.6.2 yamt if (fp->f_buf)
742 1.1.6.2 yamt dealloc(fp->f_buf, fp->f_fs->mfs_block_size);
743 1.1.6.2 yamt dealloc(fp->f_fs, sizeof(*fp->f_fs));
744 1.1.6.2 yamt dealloc(fp, sizeof(struct file));
745 1.1.6.2 yamt return 0;
746 1.1.6.2 yamt }
747 1.1.6.2 yamt
748 1.1.6.2 yamt /*
749 1.1.6.2 yamt * Copy a portion of a file into kernel memory.
750 1.1.6.2 yamt * Cross block boundaries when necessary.
751 1.1.6.2 yamt */
752 1.1.6.2 yamt __compactcall int
753 1.1.6.2 yamt minixfs3_read(struct open_file *f, void *start, size_t size, size_t *resid)
754 1.1.6.2 yamt {
755 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
756 1.1.6.2 yamt size_t csize;
757 1.1.6.2 yamt char *buf;
758 1.1.6.2 yamt size_t buf_size;
759 1.1.6.2 yamt int rc = 0;
760 1.1.6.2 yamt char *addr = start;
761 1.1.6.2 yamt
762 1.1.6.2 yamt while (size != 0) {
763 1.1.6.2 yamt if (fp->f_seekp >= (off_t)fp->f_di.mdi_size)
764 1.1.6.2 yamt break;
765 1.1.6.2 yamt
766 1.1.6.2 yamt rc = buf_read_file(f, &buf, &buf_size);
767 1.1.6.2 yamt if (rc)
768 1.1.6.2 yamt break;
769 1.1.6.2 yamt
770 1.1.6.2 yamt csize = size;
771 1.1.6.2 yamt if (csize > buf_size)
772 1.1.6.2 yamt csize = buf_size;
773 1.1.6.2 yamt
774 1.1.6.2 yamt memcpy(addr, buf, csize);
775 1.1.6.2 yamt
776 1.1.6.2 yamt fp->f_seekp += csize;
777 1.1.6.2 yamt addr += csize;
778 1.1.6.2 yamt size -= csize;
779 1.1.6.2 yamt }
780 1.1.6.2 yamt
781 1.1.6.2 yamt if (resid)
782 1.1.6.2 yamt *resid = size;
783 1.1.6.2 yamt return rc;
784 1.1.6.2 yamt }
785 1.1.6.2 yamt
786 1.1.6.2 yamt /*
787 1.1.6.2 yamt * Not implemented.
788 1.1.6.2 yamt */
789 1.1.6.2 yamt #ifndef LIBSA_NO_FS_WRITE
790 1.1.6.2 yamt __compactcall int
791 1.1.6.2 yamt minixfs3_write(struct open_file *f, void *start, size_t size, size_t *resid)
792 1.1.6.2 yamt {
793 1.1.6.2 yamt
794 1.1.6.2 yamt return EROFS;
795 1.1.6.2 yamt }
796 1.1.6.2 yamt #endif /* !LIBSA_NO_FS_WRITE */
797 1.1.6.2 yamt
798 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SEEK
799 1.1.6.2 yamt __compactcall off_t
800 1.1.6.2 yamt minixfs3_seek(struct open_file *f, off_t offset, int where)
801 1.1.6.2 yamt {
802 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
803 1.1.6.2 yamt
804 1.1.6.2 yamt switch (where) {
805 1.1.6.2 yamt case SEEK_SET:
806 1.1.6.2 yamt fp->f_seekp = offset;
807 1.1.6.2 yamt break;
808 1.1.6.2 yamt case SEEK_CUR:
809 1.1.6.2 yamt fp->f_seekp += offset;
810 1.1.6.2 yamt break;
811 1.1.6.2 yamt case SEEK_END:
812 1.1.6.2 yamt fp->f_seekp = fp->f_di.mdi_size - offset;
813 1.1.6.2 yamt break;
814 1.1.6.2 yamt default:
815 1.1.6.2 yamt return -1;
816 1.1.6.2 yamt }
817 1.1.6.2 yamt return fp->f_seekp;
818 1.1.6.2 yamt }
819 1.1.6.2 yamt #endif /* !LIBSA_NO_FS_SEEK */
820 1.1.6.2 yamt
821 1.1.6.2 yamt __compactcall int
822 1.1.6.2 yamt minixfs3_stat(struct open_file *f, struct stat *sb)
823 1.1.6.2 yamt {
824 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
825 1.1.6.2 yamt
826 1.1.6.2 yamt /* only important stuff */
827 1.1.6.2 yamt memset(sb, 0, sizeof *sb);
828 1.1.6.2 yamt sb->st_mode = fp->f_di.mdi_mode;
829 1.1.6.2 yamt sb->st_uid = fp->f_di.mdi_uid;
830 1.1.6.2 yamt sb->st_gid = fp->f_di.mdi_gid;
831 1.1.6.2 yamt sb->st_size = fp->f_di.mdi_size;
832 1.1.6.2 yamt return 0;
833 1.1.6.2 yamt }
834 1.1.6.2 yamt
835 1.1.6.2 yamt #if defined(LIBSA_ENABLE_LS_OP)
836 1.1.6.2 yamt __compactcall void
837 1.1.6.2 yamt minixfs3_ls(struct open_file *f, const char *pattern)
838 1.1.6.2 yamt {
839 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
840 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
841 1.1.6.2 yamt struct mfs_direct *dp;
842 1.1.6.2 yamt struct mfs_direct *dbuf;
843 1.1.6.2 yamt size_t buf_size;
844 1.1.6.2 yamt entry_t *names = 0, *n, **np;
845 1.1.6.2 yamt
846 1.1.6.2 yamt fp->f_seekp = 0;
847 1.1.6.2 yamt while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
848 1.1.6.2 yamt int rc = buf_read_file(f, &dbuf, &buf_size);
849 1.1.6.2 yamt if (rc)
850 1.1.6.2 yamt goto out;
851 1.1.6.2 yamt
852 1.1.6.2 yamt /* XXX we assume, that buf_read_file reads an fs block and
853 1.1.6.2 yamt * doesn't truncate buffer. Currently i_size in MFS doesn't
854 1.1.6.2 yamt * the same as size of allocated blocks, it makes buf_read_file
855 1.1.6.2 yamt * to truncate buf_size.
856 1.1.6.2 yamt */
857 1.1.6.2 yamt if (buf_size < fs->mfs_block_size)
858 1.1.6.2 yamt buf_size = fs->mfs_block_size;
859 1.1.6.2 yamt
860 1.1.6.2 yamt for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
861 1.1.6.2 yamt char *cp;
862 1.1.6.2 yamt int namlen;
863 1.1.6.2 yamt
864 1.1.6.2 yamt if (fs2h32(dp->mfsd_ino) == 0)
865 1.1.6.2 yamt continue;
866 1.1.6.2 yamt
867 1.1.6.2 yamt if (pattern && !fnmatch(dp->mfsd_name, pattern))
868 1.1.6.2 yamt continue;
869 1.1.6.2 yamt
870 1.1.6.2 yamt /* Compute the length of the name,
871 1.1.6.2 yamt * We don't use strlen and strcpy, because original MFS
872 1.1.6.2 yamt * code doesn't.
873 1.1.6.2 yamt */
874 1.1.6.2 yamt cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
875 1.1.6.2 yamt if (cp == NULL)
876 1.1.6.2 yamt namlen = sizeof(dp->mfsd_name);
877 1.1.6.2 yamt else
878 1.1.6.2 yamt namlen = cp - (dp->mfsd_name);
879 1.1.6.2 yamt
880 1.1.6.2 yamt n = alloc(sizeof *n + namlen);
881 1.1.6.2 yamt if (!n) {
882 1.1.6.2 yamt printf("%d: %s\n",
883 1.1.6.2 yamt fs2h32(dp->mfsd_ino), dp->mfsd_name);
884 1.1.6.2 yamt continue;
885 1.1.6.2 yamt }
886 1.1.6.2 yamt n->e_ino = fs2h32(dp->mfsd_ino);
887 1.1.6.2 yamt strncpy(n->e_name, dp->mfsd_name, namlen);
888 1.1.6.2 yamt n->e_name[namlen] = '\0';
889 1.1.6.2 yamt for (np = &names; *np; np = &(*np)->e_next) {
890 1.1.6.2 yamt if (strcmp(n->e_name, (*np)->e_name) < 0)
891 1.1.6.2 yamt break;
892 1.1.6.2 yamt }
893 1.1.6.2 yamt n->e_next = *np;
894 1.1.6.2 yamt *np = n;
895 1.1.6.2 yamt }
896 1.1.6.2 yamt fp->f_seekp += buf_size;
897 1.1.6.2 yamt }
898 1.1.6.2 yamt
899 1.1.6.2 yamt if (names) {
900 1.1.6.2 yamt entry_t *p_names = names;
901 1.1.6.2 yamt do {
902 1.1.6.2 yamt n = p_names;
903 1.1.6.2 yamt printf("%d: %s\n",
904 1.1.6.2 yamt n->e_ino, n->e_name);
905 1.1.6.2 yamt p_names = n->e_next;
906 1.1.6.2 yamt } while (p_names);
907 1.1.6.2 yamt } else {
908 1.1.6.2 yamt printf("not found\n");
909 1.1.6.2 yamt }
910 1.1.6.2 yamt out:
911 1.1.6.2 yamt if (names) {
912 1.1.6.2 yamt do {
913 1.1.6.2 yamt n = names;
914 1.1.6.2 yamt names = n->e_next;
915 1.1.6.2 yamt dealloc(n, 0);
916 1.1.6.2 yamt } while (names);
917 1.1.6.2 yamt }
918 1.1.6.2 yamt return;
919 1.1.6.2 yamt }
920 1.1.6.2 yamt #endif
921 1.1.6.2 yamt
922 1.1.6.2 yamt /*
923 1.1.6.2 yamt * byte swap functions for big endian machines
924 1.1.6.2 yamt * (mfs is always little endian)
925 1.1.6.2 yamt */
926 1.1.6.2 yamt
927 1.1.6.2 yamt /* These functions are only needed if native byte order is not big endian */
928 1.1.6.2 yamt #if BYTE_ORDER == BIG_ENDIAN
929 1.1.6.2 yamt void
930 1.1.6.2 yamt minixfs3_sb_bswap(struct mfs_sblock *old, struct mfs_sblock *new)
931 1.1.6.2 yamt {
932 1.1.6.2 yamt new->mfs_ninodes = bswap32(old->mfs_ninodes);
933 1.1.6.2 yamt new->mfs_nzones = bswap16(old->mfs_nzones);
934 1.1.6.2 yamt new->mfs_imap_blocks = bswap16(old->mfs_imap_blocks);
935 1.1.6.2 yamt new->mfs_zmap_blocks = bswap16(old->mfs_zmap_blocks);
936 1.1.6.2 yamt new->mfs_firstdatazone_old = bswap16(old->mfs_firstdatazone_old);
937 1.1.6.2 yamt new->mfs_log_zone_size = bswap16(old->mfs_log_zone_size);
938 1.1.6.2 yamt new->mfs_max_size = bswap32(old->mfs_max_size);
939 1.1.6.2 yamt new->mfs_zones = bswap32(old->mfs_zones);
940 1.1.6.2 yamt new->mfs_magic = bswap16(old->mfs_magic);
941 1.1.6.2 yamt new->mfs_block_size = bswap16(old->mfs_block_size);
942 1.1.6.2 yamt new->mfs_disk_version = old->mfs_disk_version;
943 1.1.6.2 yamt }
944 1.1.6.2 yamt
945 1.1.6.2 yamt void minixfs3_i_bswap(struct mfs_dinode *old, struct mfs_dinode *new)
946 1.1.6.2 yamt {
947 1.1.6.2 yamt int i;
948 1.1.6.2 yamt
949 1.1.6.2 yamt new->mdi_mode = bswap16(old->mdi_mode);
950 1.1.6.2 yamt new->mdi_nlinks = bswap16(old->mdi_nlinks);
951 1.1.6.2 yamt new->mdi_uid = bswap16(old->mdi_uid);
952 1.1.6.2 yamt new->mdi_gid = bswap16(old->mdi_gid);
953 1.1.6.2 yamt new->mdi_size = bswap32(old->mdi_size);
954 1.1.6.2 yamt new->mdi_atime = bswap32(old->mdi_atime);
955 1.1.6.2 yamt new->mdi_mtime = bswap32(old->mdi_mtime);
956 1.1.6.2 yamt new->mdi_ctime = bswap32(old->mdi_ctime);
957 1.1.6.2 yamt
958 1.1.6.2 yamt /* We don't swap here, because indirects must be swapped later
959 1.1.6.2 yamt * anyway, hence everything is done by block_map().
960 1.1.6.2 yamt */
961 1.1.6.2 yamt for (i = 0; i < NR_TZONES; i++)
962 1.1.6.2 yamt new->mdi_zone[i] = old->mdi_zone[i];
963 1.1.6.2 yamt }
964 1.1.6.2 yamt #endif
965