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