minixfs3.c revision 1.1.6.3 1 1.1.6.3 yamt /* $NetBSD: minixfs3.c,v 1.1.6.3 2014/05/22 11:41:04 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.3 yamt #define FSBTODB(fs, indp) MFS_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 static int read_inode(ino32_t, struct open_file *);
173 1.1.6.2 yamt static int block_map(struct open_file *, block_t, block_t *);
174 1.1.6.2 yamt static int buf_read_file(struct open_file *, void *, size_t *);
175 1.1.6.2 yamt static int search_directory(const char *, int, struct open_file *, ino32_t *);
176 1.1.6.2 yamt static int read_sblock(struct open_file *, struct mfs_sblock *);
177 1.1.6.2 yamt
178 1.1.6.2 yamt /*
179 1.1.6.2 yamt * Read a new inode into a file structure.
180 1.1.6.2 yamt */
181 1.1.6.2 yamt static int
182 1.1.6.2 yamt read_inode(ino32_t inumber, struct open_file *f)
183 1.1.6.2 yamt {
184 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
185 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
186 1.1.6.2 yamt char *buf;
187 1.1.6.2 yamt size_t rsize;
188 1.1.6.2 yamt int rc;
189 1.1.6.2 yamt daddr_t inode_sector;
190 1.1.6.2 yamt struct mfs_dinode *dip;
191 1.1.6.2 yamt
192 1.1.6.2 yamt inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
193 1.1.6.2 yamt
194 1.1.6.2 yamt /*
195 1.1.6.2 yamt * Read inode and save it.
196 1.1.6.2 yamt */
197 1.1.6.2 yamt buf = fp->f_buf;
198 1.1.6.2 yamt twiddle();
199 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
200 1.1.6.2 yamt inode_sector, fs->mfs_block_size, buf, &rsize);
201 1.1.6.2 yamt if (rc)
202 1.1.6.2 yamt return rc;
203 1.1.6.2 yamt if (rsize != fs->mfs_block_size)
204 1.1.6.2 yamt return EIO;
205 1.1.6.2 yamt
206 1.1.6.2 yamt dip = (struct mfs_dinode *)(buf +
207 1.1.6.2 yamt INODE_SIZE * ino_to_fsbo(fs, inumber));
208 1.1.6.2 yamt mfs_iload(dip, &fp->f_di);
209 1.1.6.2 yamt
210 1.1.6.2 yamt /*
211 1.1.6.2 yamt * Clear out the old buffers
212 1.1.6.2 yamt */
213 1.1.6.2 yamt fp->f_ind_cache_block = ~0;
214 1.1.6.2 yamt fp->f_buf_blkno = -1;
215 1.1.6.2 yamt return rc;
216 1.1.6.2 yamt }
217 1.1.6.2 yamt
218 1.1.6.2 yamt /*
219 1.1.6.2 yamt * Given an offset in a file, find the disk block number (not zone!)
220 1.1.6.2 yamt * that contains that block.
221 1.1.6.2 yamt */
222 1.1.6.2 yamt static int
223 1.1.6.2 yamt block_map(struct open_file *f, block_t file_block, block_t *disk_block_p)
224 1.1.6.2 yamt {
225 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
226 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
227 1.1.6.2 yamt uint level;
228 1.1.6.2 yamt block_t ind_cache;
229 1.1.6.2 yamt block_t ind_block_num;
230 1.1.6.2 yamt zone_t zone;
231 1.1.6.2 yamt size_t rsize;
232 1.1.6.2 yamt int rc;
233 1.1.6.2 yamt int boff;
234 1.1.6.2 yamt int scale = fs->mfs_log_zone_size; /* for block-zone conversion */
235 1.1.6.2 yamt block_t *buf = (void *)fp->f_buf;
236 1.1.6.2 yamt
237 1.1.6.2 yamt /*
238 1.1.6.2 yamt * Index structure of an inode:
239 1.1.6.2 yamt *
240 1.1.6.2 yamt * mdi_blocks[0..NR_DZONES-1]
241 1.1.6.2 yamt * hold zone numbers for zones
242 1.1.6.2 yamt * 0..NR_DZONES-1
243 1.1.6.2 yamt *
244 1.1.6.2 yamt * mdi_blocks[NR_DZONES+0]
245 1.1.6.2 yamt * block NDADDR+0 is the single indirect block
246 1.1.6.2 yamt * holds zone numbers for zones
247 1.1.6.3 yamt * NR_DZONES .. NR_DZONES + MFS_NINDIR(fs)-1
248 1.1.6.2 yamt *
249 1.1.6.2 yamt * mdi_blocks[NR_DZONES+1]
250 1.1.6.2 yamt * block NDADDR+1 is the double indirect block
251 1.1.6.2 yamt * holds zone numbers for INDEX blocks for zones
252 1.1.6.3 yamt * NR_DZONES + MFS_NINDIR(fs) ..
253 1.1.6.3 yamt * NR_TZONES + MFS_NINDIR(fs) + MFS_NINDIR(fs)**2 - 1
254 1.1.6.2 yamt */
255 1.1.6.2 yamt
256 1.1.6.2 yamt zone = file_block >> scale;
257 1.1.6.2 yamt boff = (int) (file_block - (zone << scale) ); /* relative blk in zone */
258 1.1.6.2 yamt
259 1.1.6.2 yamt if (zone < NR_DZONES) {
260 1.1.6.2 yamt /* Direct zone */
261 1.1.6.2 yamt zone_t z = fs2h32(fp->f_di.mdi_zone[zone]);
262 1.1.6.2 yamt if (z == NO_ZONE) {
263 1.1.6.2 yamt *disk_block_p = NO_BLOCK;
264 1.1.6.2 yamt return 0;
265 1.1.6.2 yamt }
266 1.1.6.2 yamt *disk_block_p = (block_t) ((z << scale) + boff);
267 1.1.6.2 yamt return 0;
268 1.1.6.2 yamt }
269 1.1.6.2 yamt
270 1.1.6.2 yamt zone -= NR_DZONES;
271 1.1.6.2 yamt
272 1.1.6.2 yamt ind_cache = zone >> LN2_IND_CACHE_SZ;
273 1.1.6.2 yamt if (ind_cache == fp->f_ind_cache_block) {
274 1.1.6.2 yamt *disk_block_p =
275 1.1.6.2 yamt fs2h32(fp->f_ind_cache[zone & IND_CACHE_MASK]);
276 1.1.6.2 yamt return 0;
277 1.1.6.2 yamt }
278 1.1.6.2 yamt
279 1.1.6.2 yamt for (level = 0;;) {
280 1.1.6.2 yamt level += fp->f_nishift;
281 1.1.6.2 yamt
282 1.1.6.2 yamt if (zone < (block_t)1 << level)
283 1.1.6.2 yamt break;
284 1.1.6.2 yamt if (level > NIADDR * fp->f_nishift)
285 1.1.6.2 yamt /* Zone number too high */
286 1.1.6.2 yamt return EFBIG;
287 1.1.6.2 yamt zone -= (block_t)1 << level;
288 1.1.6.2 yamt }
289 1.1.6.2 yamt
290 1.1.6.2 yamt ind_block_num =
291 1.1.6.2 yamt fs2h32(fp->f_di.mdi_zone[NR_DZONES + (level / fp->f_nishift - 1)]);
292 1.1.6.2 yamt
293 1.1.6.2 yamt for (;;) {
294 1.1.6.2 yamt level -= fp->f_nishift;
295 1.1.6.2 yamt if (ind_block_num == 0) {
296 1.1.6.2 yamt *disk_block_p = NO_BLOCK; /* missing */
297 1.1.6.2 yamt return 0;
298 1.1.6.2 yamt }
299 1.1.6.2 yamt
300 1.1.6.2 yamt twiddle();
301 1.1.6.2 yamt /*
302 1.1.6.2 yamt * If we were feeling brave, we could work out the number
303 1.1.6.2 yamt * of the disk sector and read a single disk sector instead
304 1.1.6.2 yamt * of a filesystem block.
305 1.1.6.2 yamt * However we don't do this very often anyway...
306 1.1.6.2 yamt */
307 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
308 1.1.6.2 yamt FSBTODB(fs, ind_block_num), fs->mfs_block_size,
309 1.1.6.2 yamt buf, &rsize);
310 1.1.6.2 yamt if (rc)
311 1.1.6.2 yamt return rc;
312 1.1.6.2 yamt if (rsize != fs->mfs_block_size)
313 1.1.6.2 yamt return EIO;
314 1.1.6.2 yamt
315 1.1.6.2 yamt ind_block_num = fs2h32(buf[zone >> level]);
316 1.1.6.2 yamt if (level == 0)
317 1.1.6.2 yamt break;
318 1.1.6.2 yamt zone &= (1 << level) - 1;
319 1.1.6.2 yamt }
320 1.1.6.2 yamt
321 1.1.6.2 yamt /* Save the part of the block that contains this sector */
322 1.1.6.2 yamt memcpy(fp->f_ind_cache, &buf[zone & ~IND_CACHE_MASK],
323 1.1.6.2 yamt IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
324 1.1.6.2 yamt fp->f_ind_cache_block = ind_cache;
325 1.1.6.2 yamt
326 1.1.6.2 yamt zone = (zone_t)ind_block_num;
327 1.1.6.2 yamt *disk_block_p = (block_t)((zone << scale) + boff);
328 1.1.6.2 yamt return 0;
329 1.1.6.2 yamt }
330 1.1.6.2 yamt
331 1.1.6.2 yamt /*
332 1.1.6.2 yamt * Read a portion of a file into an internal buffer.
333 1.1.6.2 yamt * Return the location in the buffer and the amount in the buffer.
334 1.1.6.2 yamt */
335 1.1.6.2 yamt static int
336 1.1.6.2 yamt buf_read_file(struct open_file *f, void *v, size_t *size_p)
337 1.1.6.2 yamt {
338 1.1.6.2 yamt char **buf_p = v;
339 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
340 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
341 1.1.6.2 yamt long off;
342 1.1.6.2 yamt block_t file_block;
343 1.1.6.3 yamt block_t disk_block = 0; /* XXX: gcc */
344 1.1.6.2 yamt size_t block_size;
345 1.1.6.2 yamt int rc;
346 1.1.6.2 yamt
347 1.1.6.3 yamt off = mfs_blkoff(fs, fp->f_seekp);
348 1.1.6.3 yamt file_block = mfs_lblkno(fs, fp->f_seekp);
349 1.1.6.2 yamt block_size = fs->mfs_block_size;
350 1.1.6.2 yamt
351 1.1.6.2 yamt if (file_block != fp->f_buf_blkno) {
352 1.1.6.2 yamt rc = block_map(f, file_block, &disk_block);
353 1.1.6.2 yamt if (rc)
354 1.1.6.2 yamt return rc;
355 1.1.6.2 yamt
356 1.1.6.2 yamt if (disk_block == 0) {
357 1.1.6.2 yamt memset(fp->f_buf, 0, block_size);
358 1.1.6.2 yamt fp->f_buf_size = block_size;
359 1.1.6.2 yamt } else {
360 1.1.6.2 yamt twiddle();
361 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
362 1.1.6.2 yamt FSBTODB(fs, disk_block),
363 1.1.6.2 yamt block_size, fp->f_buf, &fp->f_buf_size);
364 1.1.6.2 yamt if (rc)
365 1.1.6.2 yamt return rc;
366 1.1.6.2 yamt }
367 1.1.6.2 yamt
368 1.1.6.2 yamt fp->f_buf_blkno = file_block;
369 1.1.6.2 yamt }
370 1.1.6.2 yamt
371 1.1.6.2 yamt /*
372 1.1.6.2 yamt * Return address of byte in buffer corresponding to
373 1.1.6.2 yamt * offset, and size of remainder of buffer after that
374 1.1.6.2 yamt * byte.
375 1.1.6.2 yamt */
376 1.1.6.2 yamt *buf_p = fp->f_buf + off;
377 1.1.6.2 yamt *size_p = block_size - off;
378 1.1.6.2 yamt
379 1.1.6.2 yamt /*
380 1.1.6.2 yamt * But truncate buffer at end of file.
381 1.1.6.2 yamt */
382 1.1.6.2 yamt if (*size_p > fp->f_di.mdi_size - fp->f_seekp)
383 1.1.6.2 yamt *size_p = fp->f_di.mdi_size - fp->f_seekp;
384 1.1.6.2 yamt
385 1.1.6.2 yamt return 0;
386 1.1.6.2 yamt }
387 1.1.6.2 yamt
388 1.1.6.2 yamt /*
389 1.1.6.2 yamt * Search a directory for a name and return its
390 1.1.6.2 yamt * inode number.
391 1.1.6.2 yamt */
392 1.1.6.2 yamt static int
393 1.1.6.2 yamt search_directory(const char *name, int length, struct open_file *f,
394 1.1.6.2 yamt ino32_t *inumber_p)
395 1.1.6.2 yamt {
396 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
397 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
398 1.1.6.2 yamt struct mfs_direct *dp;
399 1.1.6.2 yamt struct mfs_direct *dbuf;
400 1.1.6.2 yamt size_t buf_size;
401 1.1.6.2 yamt int namlen;
402 1.1.6.2 yamt int rc;
403 1.1.6.2 yamt
404 1.1.6.2 yamt fp->f_seekp = 0;
405 1.1.6.2 yamt
406 1.1.6.2 yamt while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
407 1.1.6.2 yamt rc = buf_read_file(f, (void *)&dbuf, &buf_size);
408 1.1.6.2 yamt if (rc)
409 1.1.6.2 yamt return rc;
410 1.1.6.2 yamt if (buf_size == 0)
411 1.1.6.2 yamt return EIO;
412 1.1.6.2 yamt
413 1.1.6.2 yamt /* XXX we assume, that buf_read_file reads an fs block and
414 1.1.6.2 yamt * doesn't truncate buffer. Currently i_size in MFS doesn't
415 1.1.6.2 yamt * the same as size of allocated blocks, it makes buf_read_file
416 1.1.6.2 yamt * to truncate buf_size.
417 1.1.6.2 yamt */
418 1.1.6.2 yamt if (buf_size < fs->mfs_block_size)
419 1.1.6.2 yamt buf_size = fs->mfs_block_size;
420 1.1.6.2 yamt
421 1.1.6.2 yamt for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
422 1.1.6.2 yamt char *cp;
423 1.1.6.2 yamt if (fs2h32(dp->mfsd_ino) == (ino32_t) 0)
424 1.1.6.2 yamt continue;
425 1.1.6.2 yamt /* Compute the length of the name */
426 1.1.6.2 yamt cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
427 1.1.6.2 yamt if (cp == NULL)
428 1.1.6.2 yamt namlen = sizeof(dp->mfsd_name);
429 1.1.6.2 yamt else
430 1.1.6.2 yamt namlen = cp - (dp->mfsd_name);
431 1.1.6.2 yamt
432 1.1.6.2 yamt if (namlen == length &&
433 1.1.6.2 yamt !memcmp(name, dp->mfsd_name, length)) {
434 1.1.6.2 yamt /* found entry */
435 1.1.6.2 yamt *inumber_p = fs2h32(dp->mfsd_ino);
436 1.1.6.2 yamt return 0;
437 1.1.6.2 yamt }
438 1.1.6.2 yamt }
439 1.1.6.2 yamt fp->f_seekp += buf_size;
440 1.1.6.2 yamt }
441 1.1.6.2 yamt return ENOENT;
442 1.1.6.2 yamt }
443 1.1.6.2 yamt
444 1.1.6.2 yamt int
445 1.1.6.2 yamt read_sblock(struct open_file *f, struct mfs_sblock *fs)
446 1.1.6.2 yamt {
447 1.1.6.2 yamt static uint8_t sbbuf[MINBSIZE];
448 1.1.6.2 yamt size_t buf_size;
449 1.1.6.2 yamt int rc;
450 1.1.6.2 yamt
451 1.1.6.2 yamt /* We must read amount multiple of sector size, hence we can't
452 1.1.6.2 yamt * read SBSIZE and read MINBSIZE.
453 1.1.6.2 yamt */
454 1.1.6.2 yamt if (SBSIZE > MINBSIZE)
455 1.1.6.2 yamt return EINVAL;
456 1.1.6.2 yamt
457 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
458 1.1.6.2 yamt SUPER_BLOCK_OFF / DEV_BSIZE, MINBSIZE, sbbuf, &buf_size);
459 1.1.6.2 yamt if (rc)
460 1.1.6.2 yamt return rc;
461 1.1.6.2 yamt
462 1.1.6.2 yamt if (buf_size != MINBSIZE)
463 1.1.6.2 yamt return EIO;
464 1.1.6.2 yamt
465 1.1.6.2 yamt mfs_sbload((void *)sbbuf, fs);
466 1.1.6.2 yamt
467 1.1.6.2 yamt if (fs->mfs_magic != SUPER_MAGIC)
468 1.1.6.2 yamt return EINVAL;
469 1.1.6.2 yamt if (fs->mfs_block_size < MINBSIZE)
470 1.1.6.2 yamt return EINVAL;
471 1.1.6.2 yamt if ((fs->mfs_block_size % 512) != 0)
472 1.1.6.2 yamt return EINVAL;
473 1.1.6.2 yamt if (SBSIZE > fs->mfs_block_size)
474 1.1.6.2 yamt return EINVAL;
475 1.1.6.2 yamt if ((fs->mfs_block_size % INODE_SIZE) != 0)
476 1.1.6.2 yamt return EINVAL;
477 1.1.6.2 yamt
478 1.1.6.2 yamt /* For even larger disks, a similar problem occurs with s_firstdatazone.
479 1.1.6.2 yamt * If the on-disk field contains zero, we assume that the value was too
480 1.1.6.2 yamt * large to fit, and compute it on the fly.
481 1.1.6.2 yamt */
482 1.1.6.2 yamt if (fs->mfs_firstdatazone_old == 0) {
483 1.1.6.2 yamt block_t offset;
484 1.1.6.2 yamt offset = START_BLOCK + fs->mfs_imap_blocks + fs->mfs_zmap_blocks;
485 1.1.6.2 yamt offset += (fs->mfs_ninodes + fs->mfs_inodes_per_block - 1) /
486 1.1.6.2 yamt fs->mfs_inodes_per_block;
487 1.1.6.2 yamt
488 1.1.6.2 yamt fs->mfs_firstdatazone =
489 1.1.6.2 yamt (offset + (1 << fs->mfs_log_zone_size) - 1) >>
490 1.1.6.2 yamt fs->mfs_log_zone_size;
491 1.1.6.2 yamt } else {
492 1.1.6.2 yamt fs->mfs_firstdatazone = (zone_t) fs->mfs_firstdatazone_old;
493 1.1.6.2 yamt }
494 1.1.6.2 yamt
495 1.1.6.2 yamt if (fs->mfs_imap_blocks < 1 || fs->mfs_zmap_blocks < 1
496 1.1.6.2 yamt || fs->mfs_ninodes < 1 || fs->mfs_zones < 1
497 1.1.6.2 yamt || fs->mfs_firstdatazone <= 4
498 1.1.6.2 yamt || fs->mfs_firstdatazone >= fs->mfs_zones
499 1.1.6.2 yamt || (unsigned) fs->mfs_log_zone_size > 4)
500 1.1.6.2 yamt return EINVAL;
501 1.1.6.2 yamt
502 1.1.6.2 yamt /* compute in-memory mfs_sblock values */
503 1.1.6.2 yamt fs->mfs_inodes_per_block = fs->mfs_block_size / INODE_SIZE;
504 1.1.6.2 yamt
505 1.1.6.2 yamt
506 1.1.6.2 yamt {
507 1.1.6.2 yamt int32_t mult = fs->mfs_block_size >> LOG_MINBSIZE;
508 1.1.6.2 yamt int ln2 = LOG_MINBSIZE;
509 1.1.6.2 yamt
510 1.1.6.2 yamt for (; mult != 1; ln2++)
511 1.1.6.2 yamt mult >>= 1;
512 1.1.6.2 yamt
513 1.1.6.2 yamt fs->mfs_bshift = ln2;
514 1.1.6.2 yamt /* XXX assume hw bsize = 512 */
515 1.1.6.2 yamt fs->mfs_fsbtodb = ln2 - LOG_MINBSIZE + 1;
516 1.1.6.2 yamt }
517 1.1.6.2 yamt
518 1.1.6.2 yamt fs->mfs_qbmask = fs->mfs_block_size - 1;
519 1.1.6.2 yamt fs->mfs_bmask = ~fs->mfs_qbmask;
520 1.1.6.2 yamt
521 1.1.6.2 yamt return 0;
522 1.1.6.2 yamt }
523 1.1.6.2 yamt
524 1.1.6.2 yamt /*
525 1.1.6.2 yamt * Open a file.
526 1.1.6.2 yamt */
527 1.1.6.2 yamt __compactcall int
528 1.1.6.2 yamt minixfs3_open(const char *path, struct open_file *f)
529 1.1.6.2 yamt {
530 1.1.6.2 yamt #ifndef LIBSA_FS_SINGLECOMPONENT
531 1.1.6.2 yamt const char *cp, *ncp;
532 1.1.6.2 yamt int c;
533 1.1.6.2 yamt #endif
534 1.1.6.2 yamt ino32_t inumber;
535 1.1.6.2 yamt struct file *fp;
536 1.1.6.2 yamt struct mfs_sblock *fs;
537 1.1.6.2 yamt int rc;
538 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SYMLINK
539 1.1.6.2 yamt ino32_t parent_inumber;
540 1.1.6.2 yamt int nlinks = 0;
541 1.1.6.2 yamt char namebuf[MAXPATHLEN+1];
542 1.1.6.2 yamt char *buf;
543 1.1.6.2 yamt #endif
544 1.1.6.2 yamt
545 1.1.6.2 yamt /* allocate file system specific data structure */
546 1.1.6.2 yamt fp = alloc(sizeof(struct file));
547 1.1.6.2 yamt memset(fp, 0, sizeof(struct file));
548 1.1.6.2 yamt f->f_fsdata = (void *)fp;
549 1.1.6.2 yamt
550 1.1.6.2 yamt /* allocate space and read super block */
551 1.1.6.2 yamt fs = alloc(sizeof(*fs));
552 1.1.6.2 yamt memset(fs, 0, sizeof(*fs));
553 1.1.6.2 yamt fp->f_fs = fs;
554 1.1.6.2 yamt twiddle();
555 1.1.6.2 yamt
556 1.1.6.2 yamt rc = read_sblock(f, fs);
557 1.1.6.2 yamt if (rc)
558 1.1.6.2 yamt goto out;
559 1.1.6.2 yamt
560 1.1.6.2 yamt /* alloc a block sized buffer used for all fs transfers */
561 1.1.6.2 yamt fp->f_buf = alloc(fs->mfs_block_size);
562 1.1.6.2 yamt
563 1.1.6.2 yamt /*
564 1.1.6.2 yamt * Calculate indirect block levels.
565 1.1.6.2 yamt */
566 1.1.6.2 yamt {
567 1.1.6.2 yamt int32_t mult;
568 1.1.6.2 yamt int ln2;
569 1.1.6.2 yamt
570 1.1.6.2 yamt /*
571 1.1.6.2 yamt * We note that the number of indirect blocks is always
572 1.1.6.2 yamt * a power of 2. This lets us use shifts and masks instead
573 1.1.6.2 yamt * of divide and remainder and avoinds pulling in the
574 1.1.6.2 yamt * 64bit division routine into the boot code.
575 1.1.6.2 yamt */
576 1.1.6.3 yamt mult = MFS_NINDIR(fs);
577 1.1.6.2 yamt #ifdef DEBUG
578 1.1.6.2 yamt if (!powerof2(mult)) {
579 1.1.6.2 yamt /* Hummm was't a power of 2 */
580 1.1.6.2 yamt rc = EINVAL;
581 1.1.6.2 yamt goto out;
582 1.1.6.2 yamt }
583 1.1.6.2 yamt #endif
584 1.1.6.2 yamt for (ln2 = 0; mult != 1; ln2++)
585 1.1.6.2 yamt mult >>= 1;
586 1.1.6.2 yamt
587 1.1.6.2 yamt fp->f_nishift = ln2;
588 1.1.6.2 yamt }
589 1.1.6.2 yamt
590 1.1.6.2 yamt inumber = ROOT_INODE;
591 1.1.6.2 yamt if ((rc = read_inode(inumber, f)) != 0)
592 1.1.6.2 yamt goto out;
593 1.1.6.2 yamt
594 1.1.6.2 yamt #ifndef LIBSA_FS_SINGLECOMPONENT
595 1.1.6.2 yamt cp = path;
596 1.1.6.2 yamt while (*cp) {
597 1.1.6.2 yamt
598 1.1.6.2 yamt /*
599 1.1.6.2 yamt * Remove extra separators
600 1.1.6.2 yamt */
601 1.1.6.2 yamt while (*cp == '/')
602 1.1.6.2 yamt cp++;
603 1.1.6.2 yamt if (*cp == '\0')
604 1.1.6.2 yamt break;
605 1.1.6.2 yamt
606 1.1.6.2 yamt /*
607 1.1.6.2 yamt * Check that current node is a directory.
608 1.1.6.2 yamt */
609 1.1.6.2 yamt if ((fp->f_di.mdi_mode & I_TYPE) != I_DIRECTORY) {
610 1.1.6.2 yamt rc = ENOTDIR;
611 1.1.6.2 yamt goto out;
612 1.1.6.2 yamt }
613 1.1.6.2 yamt
614 1.1.6.2 yamt /*
615 1.1.6.2 yamt * Get next component of path name.
616 1.1.6.2 yamt */
617 1.1.6.2 yamt ncp = cp;
618 1.1.6.2 yamt while ((c = *cp) != '\0' && c != '/')
619 1.1.6.2 yamt cp++;
620 1.1.6.2 yamt
621 1.1.6.2 yamt /*
622 1.1.6.2 yamt * Look up component in current directory.
623 1.1.6.2 yamt * Save directory inumber in case we find a
624 1.1.6.2 yamt * symbolic link.
625 1.1.6.2 yamt */
626 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SYMLINK
627 1.1.6.2 yamt parent_inumber = inumber;
628 1.1.6.2 yamt #endif
629 1.1.6.2 yamt rc = search_directory(ncp, cp - ncp, f, &inumber);
630 1.1.6.2 yamt if (rc)
631 1.1.6.2 yamt goto out;
632 1.1.6.2 yamt
633 1.1.6.2 yamt /*
634 1.1.6.2 yamt * Open next component.
635 1.1.6.2 yamt */
636 1.1.6.2 yamt if ((rc = read_inode(inumber, f)) != 0)
637 1.1.6.2 yamt goto out;
638 1.1.6.2 yamt
639 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SYMLINK
640 1.1.6.2 yamt /*
641 1.1.6.2 yamt * Check for symbolic link.
642 1.1.6.2 yamt */
643 1.1.6.2 yamt if ((fp->f_di.mdi_mode & I_TYPE) == I_SYMBOLIC_LINK) {
644 1.1.6.2 yamt int link_len = fp->f_di.mdi_size;
645 1.1.6.2 yamt int len;
646 1.1.6.2 yamt size_t buf_size;
647 1.1.6.2 yamt block_t disk_block;
648 1.1.6.2 yamt
649 1.1.6.2 yamt len = strlen(cp);
650 1.1.6.2 yamt
651 1.1.6.2 yamt if (link_len + len > MAXPATHLEN ||
652 1.1.6.2 yamt ++nlinks > MAXSYMLINKS) {
653 1.1.6.2 yamt rc = ENOENT;
654 1.1.6.2 yamt goto out;
655 1.1.6.2 yamt }
656 1.1.6.2 yamt
657 1.1.6.2 yamt memmove(&namebuf[link_len], cp, len + 1);
658 1.1.6.2 yamt
659 1.1.6.2 yamt /*
660 1.1.6.2 yamt * Read file for symbolic link
661 1.1.6.2 yamt */
662 1.1.6.2 yamt buf = fp->f_buf;
663 1.1.6.2 yamt rc = block_map(f, (block_t)0, &disk_block);
664 1.1.6.2 yamt if (rc)
665 1.1.6.2 yamt goto out;
666 1.1.6.2 yamt
667 1.1.6.2 yamt twiddle();
668 1.1.6.2 yamt rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
669 1.1.6.2 yamt F_READ, FSBTODB(fs, disk_block),
670 1.1.6.2 yamt fs->mfs_block_size, buf, &buf_size);
671 1.1.6.2 yamt if (rc)
672 1.1.6.2 yamt goto out;
673 1.1.6.2 yamt
674 1.1.6.2 yamt memcpy(namebuf, buf, link_len);
675 1.1.6.2 yamt
676 1.1.6.2 yamt /*
677 1.1.6.2 yamt * If relative pathname, restart at parent directory.
678 1.1.6.2 yamt * If absolute pathname, restart at root.
679 1.1.6.2 yamt */
680 1.1.6.2 yamt cp = namebuf;
681 1.1.6.2 yamt if (*cp != '/')
682 1.1.6.2 yamt inumber = parent_inumber;
683 1.1.6.2 yamt else
684 1.1.6.2 yamt inumber = (ino32_t) ROOT_INODE;
685 1.1.6.2 yamt
686 1.1.6.2 yamt if ((rc = read_inode(inumber, f)) != 0)
687 1.1.6.2 yamt goto out;
688 1.1.6.2 yamt }
689 1.1.6.2 yamt #endif /* !LIBSA_NO_FS_SYMLINK */
690 1.1.6.2 yamt }
691 1.1.6.2 yamt
692 1.1.6.2 yamt /*
693 1.1.6.2 yamt * Found terminal component.
694 1.1.6.2 yamt */
695 1.1.6.2 yamt rc = 0;
696 1.1.6.2 yamt
697 1.1.6.2 yamt #else /* !LIBSA_FS_SINGLECOMPONENT */
698 1.1.6.2 yamt
699 1.1.6.2 yamt /* look up component in the current (root) directory */
700 1.1.6.2 yamt rc = search_directory(path, strlen(path), f, &inumber);
701 1.1.6.2 yamt if (rc)
702 1.1.6.2 yamt goto out;
703 1.1.6.2 yamt
704 1.1.6.2 yamt /* open it */
705 1.1.6.2 yamt rc = read_inode(inumber, f);
706 1.1.6.2 yamt
707 1.1.6.2 yamt #endif /* !LIBSA_FS_SINGLECOMPONENT */
708 1.1.6.2 yamt
709 1.1.6.2 yamt fp->f_seekp = 0; /* reset seek pointer */
710 1.1.6.2 yamt
711 1.1.6.2 yamt out:
712 1.1.6.2 yamt if (rc)
713 1.1.6.2 yamt minixfs3_close(f);
714 1.1.6.2 yamt
715 1.1.6.2 yamt return rc;
716 1.1.6.2 yamt }
717 1.1.6.2 yamt
718 1.1.6.2 yamt __compactcall int
719 1.1.6.2 yamt minixfs3_close(struct open_file *f)
720 1.1.6.2 yamt {
721 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
722 1.1.6.2 yamt
723 1.1.6.2 yamt f->f_fsdata = NULL;
724 1.1.6.2 yamt if (fp == NULL)
725 1.1.6.2 yamt return 0;
726 1.1.6.2 yamt
727 1.1.6.2 yamt if (fp->f_buf)
728 1.1.6.2 yamt dealloc(fp->f_buf, fp->f_fs->mfs_block_size);
729 1.1.6.2 yamt dealloc(fp->f_fs, sizeof(*fp->f_fs));
730 1.1.6.2 yamt dealloc(fp, sizeof(struct file));
731 1.1.6.2 yamt return 0;
732 1.1.6.2 yamt }
733 1.1.6.2 yamt
734 1.1.6.2 yamt /*
735 1.1.6.2 yamt * Copy a portion of a file into kernel memory.
736 1.1.6.2 yamt * Cross block boundaries when necessary.
737 1.1.6.2 yamt */
738 1.1.6.2 yamt __compactcall int
739 1.1.6.2 yamt minixfs3_read(struct open_file *f, void *start, size_t size, size_t *resid)
740 1.1.6.2 yamt {
741 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
742 1.1.6.2 yamt size_t csize;
743 1.1.6.2 yamt char *buf;
744 1.1.6.2 yamt size_t buf_size;
745 1.1.6.2 yamt int rc = 0;
746 1.1.6.2 yamt char *addr = start;
747 1.1.6.2 yamt
748 1.1.6.2 yamt while (size != 0) {
749 1.1.6.2 yamt if (fp->f_seekp >= (off_t)fp->f_di.mdi_size)
750 1.1.6.2 yamt break;
751 1.1.6.2 yamt
752 1.1.6.2 yamt rc = buf_read_file(f, &buf, &buf_size);
753 1.1.6.2 yamt if (rc)
754 1.1.6.2 yamt break;
755 1.1.6.2 yamt
756 1.1.6.2 yamt csize = size;
757 1.1.6.2 yamt if (csize > buf_size)
758 1.1.6.2 yamt csize = buf_size;
759 1.1.6.2 yamt
760 1.1.6.2 yamt memcpy(addr, buf, csize);
761 1.1.6.2 yamt
762 1.1.6.2 yamt fp->f_seekp += csize;
763 1.1.6.2 yamt addr += csize;
764 1.1.6.2 yamt size -= csize;
765 1.1.6.2 yamt }
766 1.1.6.2 yamt
767 1.1.6.2 yamt if (resid)
768 1.1.6.2 yamt *resid = size;
769 1.1.6.2 yamt return rc;
770 1.1.6.2 yamt }
771 1.1.6.2 yamt
772 1.1.6.2 yamt /*
773 1.1.6.2 yamt * Not implemented.
774 1.1.6.2 yamt */
775 1.1.6.2 yamt #ifndef LIBSA_NO_FS_WRITE
776 1.1.6.2 yamt __compactcall int
777 1.1.6.2 yamt minixfs3_write(struct open_file *f, void *start, size_t size, size_t *resid)
778 1.1.6.2 yamt {
779 1.1.6.2 yamt
780 1.1.6.2 yamt return EROFS;
781 1.1.6.2 yamt }
782 1.1.6.2 yamt #endif /* !LIBSA_NO_FS_WRITE */
783 1.1.6.2 yamt
784 1.1.6.2 yamt #ifndef LIBSA_NO_FS_SEEK
785 1.1.6.2 yamt __compactcall off_t
786 1.1.6.2 yamt minixfs3_seek(struct open_file *f, off_t offset, int where)
787 1.1.6.2 yamt {
788 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
789 1.1.6.2 yamt
790 1.1.6.2 yamt switch (where) {
791 1.1.6.2 yamt case SEEK_SET:
792 1.1.6.2 yamt fp->f_seekp = offset;
793 1.1.6.2 yamt break;
794 1.1.6.2 yamt case SEEK_CUR:
795 1.1.6.2 yamt fp->f_seekp += offset;
796 1.1.6.2 yamt break;
797 1.1.6.2 yamt case SEEK_END:
798 1.1.6.2 yamt fp->f_seekp = fp->f_di.mdi_size - offset;
799 1.1.6.2 yamt break;
800 1.1.6.2 yamt default:
801 1.1.6.2 yamt return -1;
802 1.1.6.2 yamt }
803 1.1.6.2 yamt return fp->f_seekp;
804 1.1.6.2 yamt }
805 1.1.6.2 yamt #endif /* !LIBSA_NO_FS_SEEK */
806 1.1.6.2 yamt
807 1.1.6.2 yamt __compactcall int
808 1.1.6.2 yamt minixfs3_stat(struct open_file *f, struct stat *sb)
809 1.1.6.2 yamt {
810 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
811 1.1.6.2 yamt
812 1.1.6.2 yamt /* only important stuff */
813 1.1.6.2 yamt memset(sb, 0, sizeof *sb);
814 1.1.6.2 yamt sb->st_mode = fp->f_di.mdi_mode;
815 1.1.6.2 yamt sb->st_uid = fp->f_di.mdi_uid;
816 1.1.6.2 yamt sb->st_gid = fp->f_di.mdi_gid;
817 1.1.6.2 yamt sb->st_size = fp->f_di.mdi_size;
818 1.1.6.2 yamt return 0;
819 1.1.6.2 yamt }
820 1.1.6.2 yamt
821 1.1.6.2 yamt #if defined(LIBSA_ENABLE_LS_OP)
822 1.1.6.3 yamt #include "ls.h"
823 1.1.6.2 yamt __compactcall void
824 1.1.6.2 yamt minixfs3_ls(struct open_file *f, const char *pattern)
825 1.1.6.2 yamt {
826 1.1.6.2 yamt struct file *fp = (struct file *)f->f_fsdata;
827 1.1.6.2 yamt struct mfs_sblock *fs = fp->f_fs;
828 1.1.6.2 yamt struct mfs_direct *dp;
829 1.1.6.2 yamt struct mfs_direct *dbuf;
830 1.1.6.2 yamt size_t buf_size;
831 1.1.6.3 yamt lsentry_t *names = 0;
832 1.1.6.2 yamt
833 1.1.6.2 yamt fp->f_seekp = 0;
834 1.1.6.2 yamt while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
835 1.1.6.2 yamt int rc = buf_read_file(f, &dbuf, &buf_size);
836 1.1.6.2 yamt if (rc)
837 1.1.6.2 yamt goto out;
838 1.1.6.2 yamt
839 1.1.6.2 yamt /* XXX we assume, that buf_read_file reads an fs block and
840 1.1.6.2 yamt * doesn't truncate buffer. Currently i_size in MFS doesn't
841 1.1.6.2 yamt * the same as size of allocated blocks, it makes buf_read_file
842 1.1.6.2 yamt * to truncate buf_size.
843 1.1.6.2 yamt */
844 1.1.6.2 yamt if (buf_size < fs->mfs_block_size)
845 1.1.6.2 yamt buf_size = fs->mfs_block_size;
846 1.1.6.2 yamt
847 1.1.6.2 yamt for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) {
848 1.1.6.2 yamt char *cp;
849 1.1.6.2 yamt int namlen;
850 1.1.6.2 yamt
851 1.1.6.2 yamt if (fs2h32(dp->mfsd_ino) == 0)
852 1.1.6.2 yamt continue;
853 1.1.6.2 yamt
854 1.1.6.2 yamt /* Compute the length of the name,
855 1.1.6.2 yamt * We don't use strlen and strcpy, because original MFS
856 1.1.6.2 yamt * code doesn't.
857 1.1.6.2 yamt */
858 1.1.6.2 yamt cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name));
859 1.1.6.2 yamt if (cp == NULL)
860 1.1.6.2 yamt namlen = sizeof(dp->mfsd_name);
861 1.1.6.2 yamt else
862 1.1.6.2 yamt namlen = cp - (dp->mfsd_name);
863 1.1.6.2 yamt
864 1.1.6.3 yamt lsadd(&names, pattern, dp->mfsd_name, namlen,
865 1.1.6.3 yamt fs2h32(dp->mfsd_ino), "?");
866 1.1.6.2 yamt }
867 1.1.6.2 yamt fp->f_seekp += buf_size;
868 1.1.6.2 yamt }
869 1.1.6.3 yamt lsprint(names);
870 1.1.6.3 yamt out: lsfree(names);
871 1.1.6.2 yamt }
872 1.1.6.2 yamt #endif
873 1.1.6.2 yamt
874 1.1.6.2 yamt /*
875 1.1.6.2 yamt * byte swap functions for big endian machines
876 1.1.6.2 yamt * (mfs is always little endian)
877 1.1.6.2 yamt */
878 1.1.6.2 yamt
879 1.1.6.2 yamt /* These functions are only needed if native byte order is not big endian */
880 1.1.6.2 yamt #if BYTE_ORDER == BIG_ENDIAN
881 1.1.6.2 yamt void
882 1.1.6.2 yamt minixfs3_sb_bswap(struct mfs_sblock *old, struct mfs_sblock *new)
883 1.1.6.2 yamt {
884 1.1.6.2 yamt new->mfs_ninodes = bswap32(old->mfs_ninodes);
885 1.1.6.2 yamt new->mfs_nzones = bswap16(old->mfs_nzones);
886 1.1.6.2 yamt new->mfs_imap_blocks = bswap16(old->mfs_imap_blocks);
887 1.1.6.2 yamt new->mfs_zmap_blocks = bswap16(old->mfs_zmap_blocks);
888 1.1.6.2 yamt new->mfs_firstdatazone_old = bswap16(old->mfs_firstdatazone_old);
889 1.1.6.2 yamt new->mfs_log_zone_size = bswap16(old->mfs_log_zone_size);
890 1.1.6.2 yamt new->mfs_max_size = bswap32(old->mfs_max_size);
891 1.1.6.2 yamt new->mfs_zones = bswap32(old->mfs_zones);
892 1.1.6.2 yamt new->mfs_magic = bswap16(old->mfs_magic);
893 1.1.6.2 yamt new->mfs_block_size = bswap16(old->mfs_block_size);
894 1.1.6.2 yamt new->mfs_disk_version = old->mfs_disk_version;
895 1.1.6.2 yamt }
896 1.1.6.2 yamt
897 1.1.6.2 yamt void minixfs3_i_bswap(struct mfs_dinode *old, struct mfs_dinode *new)
898 1.1.6.2 yamt {
899 1.1.6.2 yamt int i;
900 1.1.6.2 yamt
901 1.1.6.2 yamt new->mdi_mode = bswap16(old->mdi_mode);
902 1.1.6.2 yamt new->mdi_nlinks = bswap16(old->mdi_nlinks);
903 1.1.6.2 yamt new->mdi_uid = bswap16(old->mdi_uid);
904 1.1.6.2 yamt new->mdi_gid = bswap16(old->mdi_gid);
905 1.1.6.2 yamt new->mdi_size = bswap32(old->mdi_size);
906 1.1.6.2 yamt new->mdi_atime = bswap32(old->mdi_atime);
907 1.1.6.2 yamt new->mdi_mtime = bswap32(old->mdi_mtime);
908 1.1.6.2 yamt new->mdi_ctime = bswap32(old->mdi_ctime);
909 1.1.6.2 yamt
910 1.1.6.2 yamt /* We don't swap here, because indirects must be swapped later
911 1.1.6.2 yamt * anyway, hence everything is done by block_map().
912 1.1.6.2 yamt */
913 1.1.6.2 yamt for (i = 0; i < NR_TZONES; i++)
914 1.1.6.2 yamt new->mdi_zone[i] = old->mdi_zone[i];
915 1.1.6.2 yamt }
916 1.1.6.2 yamt #endif
917