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