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