ufs.c revision 1.84 1 /* $NetBSD: ufs.c,v 1.84 2022/04/27 11:48:26 rin Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *
35 * Copyright (c) 1990, 1991 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Author: David Golub
39 *
40 * Permission to use, copy, modify and distribute this software and its
41 * documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
45 *
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
48 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 *
52 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
56 *
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
59 */
60
61 /*
62 * Stand-alone file reading package for UFS and LFS filesystems.
63 */
64
65 #include <sys/param.h>
66 #include <sys/time.h>
67 #include <ufs/ufs/dinode.h>
68 #include <ufs/ufs/dir.h>
69 #ifdef LIBSA_LFS
70 #include <sys/queue.h>
71 #include <sys/condvar.h>
72 #include <sys/mount.h> /* XXX for MNAMELEN */
73 #include <ufs/lfs/lfs.h>
74 #else
75 #include <ufs/ffs/fs.h>
76 #endif
77 #ifdef _STANDALONE
78 #include <lib/libkern/libkern.h>
79 #else
80 #include <string.h>
81 #endif
82
83 #include "stand.h"
84 #ifdef LIBSA_LFS
85 #include "lfs.h"
86 #else
87 #include "ufs.h"
88 #endif
89
90 /* If this file is compiled by itself, build ufs (aka ffsv1) support */
91 #if !defined(LIBSA_FFSv2) && !defined(LIBSA_LFS)
92 #define LIBSA_FFSv1
93 #endif
94
95 #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
96 #define LIBSA_NO_FS_SYMLINK
97 #endif
98 #if defined(COMPAT_UFS) && defined(LIBSA_NO_COMPAT_UFS)
99 #undef COMPAT_UFS
100 #endif
101
102 #ifdef LIBSA_LFS
103 /* Do not (yet) support FFS_EI on LFS. */
104 #undef LIBSA_FFS_EI
105 /*
106 * In-core LFS superblock - just the on-disk one.
107 */
108 struct salfs {
109 union {
110 struct dlfs u_32;
111 struct dlfs64 u_64;
112 } lfs_dlfs_u;
113 unsigned lfs_is64 : 1,
114 lfs_dobyteswap : 1,
115 lfs_hasolddirfmt : 1;
116 };
117 /* Get lfs accessors that use struct salfs. */
118 #define STRUCT_LFS struct salfs
119 #include <ufs/lfs/lfs_accessors.h>
120
121 /* override this to avoid a mess with the dinode accessors */
122 #define lfs_dino_getsize(fs, dp) ((dp)->di_size)
123
124 typedef struct salfs FS;
125 #define fs_magic lfs_dlfs_u.u_32.dlfs_magic
126 #define fs_maxsymlinklen lfs_dlfs_u.u_32.dlfs_maxsymlinklen
127 #define lfs_version lfs_dlfs_u.u_32.dlfs_version
128
129 #define SBLOCKSIZE LFS_SBPAD
130 #define SBLOCKOFFSET LFS_LABELPAD
131 #else
132 /* NB ufs2 doesn't use the common superblock code... */
133 typedef struct fs FS;
134 #define SBLOCKOFFSET SBLOCK_UFS1
135 #endif
136
137 #if defined(LIBSA_NO_TWIDDLE)
138 #define twiddle()
139 #endif
140
141 #undef cgstart
142 #if defined(LIBSA_FFSv2)
143 #define cgstart(fc, c) cgstart_ufs2((fs), (c))
144 #else
145 #define cgstart(fc, c) cgstart_ufs1((fs), (c))
146 #endif
147
148 #ifndef ufs_dinode
149 #define ufs_dinode ufs1_dinode
150 #endif
151 #ifndef indp_t
152 #define indp_t int32_t
153 #endif
154 typedef uint32_t ino32_t;
155
156 #ifndef FSBTODB
157 #define FSBTODB(fs, indp) FFS_FSBTODB(fs, indp)
158 #endif
159 #ifndef FS_MAGIC
160 #define FS_MAGIC FS_UFS1_MAGIC
161 #endif
162 #ifndef UFS_NINDIR
163 #define UFS_NINDIR FFS_NINDIR
164 #endif
165 #ifndef ufs_blkoff
166 #define ufs_blkoff ffs_blkoff
167 #endif
168 #ifndef ufs_lblkno
169 #define ufs_lblkno ffs_lblkno
170 #endif
171 #ifndef ufs_dinode_swap
172 #define ufs_dinode_swap ffs_dinode1_swap
173 #endif
174 #ifndef ufs_indp_swap
175 #define ufs_indp_swap bswap32
176 #endif
177
178 /*
179 * To avoid having a lot of filesystem-block sized buffers lurking (which
180 * could be 32k) we only keep a few entries of the indirect block map.
181 * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block
182 * ~13 times pulling in a 6M kernel.
183 * The cache size must be smaller than the smallest filesystem block,
184 * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks).
185 */
186 #define LN2_IND_CACHE_SZ 6
187 #define IND_CACHE_SZ (1 << LN2_IND_CACHE_SZ)
188 #define IND_CACHE_MASK (IND_CACHE_SZ - 1)
189
190 /*
191 * In-core open file.
192 */
193 struct file {
194 off_t f_seekp; /* seek pointer */
195 FS *f_fs; /* pointer to super-block */
196 struct ufs_dinode f_di; /* copy of on-disk inode */
197 uint f_nishift; /* for blocks in indirect block */
198 indp_t f_ind_cache_block;
199 indp_t f_ind_cache[IND_CACHE_SZ];
200
201 char *f_buf; /* buffer for data block */
202 size_t f_buf_size; /* size of data block */
203 daddr_t f_buf_blkno; /* block number of data block */
204 #if defined(LIBSA_FFS_EI)
205 bool f_swapped; /* FFS is other endian */
206 #endif
207 };
208
209 static int read_inode(ino32_t, struct open_file *);
210 static int block_map(struct open_file *, indp_t, indp_t *);
211 static int buf_read_file(struct open_file *, char **, size_t *);
212 static int search_directory(const char *, int, struct open_file *, ino32_t *);
213 #ifdef LIBSA_FFSv1
214 static void ffs_oldfscompat(FS *);
215 #endif
216
217 static __inline__ bool
218 ffs_is_magic(FS *fs)
219 {
220 return fs->fs_magic == FS_MAGIC;
221 }
222
223 static __inline__ void
224 ffs_fix_magic_swapped(struct file *fp, FS *fs)
225 {
226 #ifdef LIBSA_FFS_EI
227 fp->f_swapped = fs->fs_magic == bswap32(FS_MAGIC);
228 if (fp->f_swapped)
229 {
230 ffs_sb_swap(fs, fs);
231 }
232 #endif
233 }
234
235 #ifdef LIBSA_FFS_EI
236 static __inline__ bool
237 ffs_swapped(struct file *fp)
238 {
239 return fp->f_swapped;
240 }
241 #endif
242
243 static __inline__ uint16_t
244 ffs_get_reclen(struct file *fp, struct direct *dp)
245 {
246 #ifdef LIBSA_FFS_EI
247 if (ffs_swapped(fp))
248 return bswap16(dp->d_reclen);
249 #endif
250 return dp->d_reclen;
251 }
252
253 static __inline__ uint32_t
254 ffs_get_ino(struct file *fp, struct direct *dp)
255 {
256 #ifdef LIBSA_FFS_EI
257 if (ffs_swapped(fp))
258 return bswap32(dp->d_ino);
259 #endif
260 return dp->d_ino;
261 }
262
263
264 #ifdef LIBSA_LFS
265 /*
266 * Find an inode's block. Look it up in the ifile. Whee!
267 */
268 static int
269 find_inode_sector(ino32_t inumber, struct open_file *f, daddr_t *isp)
270 {
271 struct file *fp = (struct file *)f->f_fsdata;
272 FS *fs = fp->f_fs;
273 daddr_t ifileent_blkno;
274 char *ent_in_buf;
275 size_t buf_after_ent;
276 size_t entsize;
277 int rc;
278
279 rc = read_inode(LFS_IFILE_INUM, f);
280 if (rc)
281 return rc;
282
283 entsize = fs->lfs_is64 ? sizeof(IFILE64) :
284 (lfs_sb_getversion(fs) > 1 ? sizeof(IFILE32) : sizeof(IFILE_V1));
285 ifileent_blkno =
286 (inumber / lfs_sb_getifpb(fs)) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs);
287 fp->f_seekp = (off_t)ifileent_blkno * lfs_sb_getbsize(fs) +
288 (inumber % lfs_sb_getifpb(fs)) * entsize;
289 rc = buf_read_file(f, &ent_in_buf, &buf_after_ent);
290 if (rc)
291 return rc;
292 /* make sure something's not badly wrong, but don't panic. */
293 if (buf_after_ent < entsize)
294 return EINVAL;
295
296 *isp = FSBTODB(fs, lfs_if_getdaddr(fs, (IFILE *)ent_in_buf));
297 if (*isp == LFS_UNUSED_DADDR) /* again, something badly wrong */
298 return EINVAL;
299 return 0;
300 }
301 #endif
302
303 /*
304 * Read a new inode into a file structure.
305 */
306 static int
307 read_inode(ino32_t inumber, struct open_file *f)
308 {
309 struct file *fp = (struct file *)f->f_fsdata;
310 FS *fs = fp->f_fs;
311 char *buf;
312 size_t rsize;
313 int rc;
314 daddr_t inode_sector = 0; /* XXX: gcc */
315 #ifdef LIBSA_LFS
316 struct ufs_dinode *dip;
317 int cnt;
318 #endif
319
320 #ifdef LIBSA_LFS
321 if (inumber == LFS_IFILE_INUM)
322 inode_sector = FSBTODB(fs, lfs_sb_getidaddr(fs));
323 else if ((rc = find_inode_sector(inumber, f, &inode_sector)) != 0)
324 return rc;
325 #else
326 inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
327 #endif
328
329 /*
330 * Read inode and save it.
331 */
332 buf = fp->f_buf;
333 twiddle();
334 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
335 inode_sector, fs->fs_bsize, buf, &rsize);
336 if (rc)
337 return rc;
338 if (rsize != (size_t)fs->fs_bsize)
339 return EIO;
340
341 #ifdef LIBSA_LFS
342 cnt = INOPBx(fs);
343 dip = (struct ufs_dinode *)buf + (cnt - 1);
344 for (; dip->di_inumber != inumber; --dip) {
345 /* kernel code panics, but boot blocks which panic are Bad. */
346 if (--cnt == 0)
347 return EINVAL;
348 }
349 fp->f_di = *dip;
350 #else
351 fp->f_di = ((struct ufs_dinode *)buf)[ino_to_fsbo(fs, inumber)];
352 #ifdef LIBSA_FFS_EI
353 if (ffs_swapped(fp))
354 ufs_dinode_swap(&fp->f_di, &fp->f_di);
355 #endif
356 #endif
357
358 /*
359 * Clear out the old buffers
360 */
361 fp->f_ind_cache_block = ~0;
362 fp->f_buf_blkno = -1;
363 return rc;
364 }
365
366 /*
367 * Given an offset in a file, find the disk block number that
368 * contains that block.
369 */
370 static int
371 block_map(struct open_file *f, indp_t file_block, indp_t *disk_block_p)
372 {
373 struct file *fp = (struct file *)f->f_fsdata;
374 FS *fs = fp->f_fs;
375 uint level;
376 indp_t ind_cache;
377 indp_t ind_block_num;
378 size_t rsize;
379 int rc;
380 indp_t *buf = (void *)fp->f_buf;
381
382 /*
383 * Index structure of an inode:
384 *
385 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
386 * 0..UFS_NDADDR-1
387 *
388 * di_ib[0] index block 0 is the single indirect block
389 * holds block numbers for blocks
390 * UFS_NDADDR .. UFS_NDADDR + UFS_NINDIR(fs)-1
391 *
392 * di_ib[1] index block 1 is the double indirect block
393 * holds block numbers for INDEX blocks for blocks
394 * UFS_NDADDR + UFS_NINDIR(fs) ..
395 * UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2 - 1
396 *
397 * di_ib[2] index block 2 is the triple indirect block
398 * holds block numbers for double-indirect
399 * blocks for blocks
400 * UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2 ..
401 * UFS_NDADDR + UFS_NINDIR(fs) + UFS_NINDIR(fs)**2
402 * + UFS_NINDIR(fs)**3 - 1
403 */
404
405 if (file_block < UFS_NDADDR) {
406 /* Direct block. */
407 *disk_block_p = fp->f_di.di_db[file_block];
408 return 0;
409 }
410
411 file_block -= UFS_NDADDR;
412
413 ind_cache = file_block >> LN2_IND_CACHE_SZ;
414 if (ind_cache == fp->f_ind_cache_block) {
415 *disk_block_p = fp->f_ind_cache[file_block & IND_CACHE_MASK];
416 return 0;
417 }
418
419 for (level = 0;;) {
420 level += fp->f_nishift;
421 if (file_block < (indp_t)1 << level)
422 break;
423 if (level > UFS_NIADDR * fp->f_nishift)
424 /* Block number too high */
425 return EFBIG;
426 file_block -= (indp_t)1 << level;
427 }
428
429 ind_block_num = fp->f_di.di_ib[level / fp->f_nishift - 1];
430
431 for (;;) {
432 level -= fp->f_nishift;
433 if (ind_block_num == 0) {
434 *disk_block_p = 0; /* missing */
435 return 0;
436 }
437
438 twiddle();
439 /*
440 * If we were feeling brave, we could work out the number
441 * of the disk sector and read a single disk sector instead
442 * of a filesystem block.
443 * However we don't do this very often anyway...
444 */
445 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
446 FSBTODB(fp->f_fs, ind_block_num), fs->fs_bsize,
447 buf, &rsize);
448 if (rc)
449 return rc;
450 if (rsize != (size_t)fs->fs_bsize)
451 return EIO;
452 #ifdef LIBSA_FFS_EI
453 if (ffs_swapped(fp))
454 ind_block_num = ufs_indp_swap(buf[file_block >> level]);
455 else
456 #endif
457 ind_block_num = buf[file_block >> level];
458 if (level == 0)
459 break;
460 file_block &= (1 << level) - 1;
461 }
462
463 /* Save the part of the block that contains this sector */
464 #if defined(LIBSA_FFS_EI)
465 if (ffs_swapped(fp)) {
466 size_t i;
467
468 for (i = 0; i < IND_CACHE_SZ; i++) {
469 fp->f_ind_cache[i] = ufs_indp_swap(
470 buf[(file_block & ~IND_CACHE_MASK) + i]);
471 }
472 } else
473 #endif
474 memcpy(fp->f_ind_cache, &buf[file_block & ~IND_CACHE_MASK],
475 IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
476 fp->f_ind_cache_block = ind_cache;
477
478 *disk_block_p = ind_block_num;
479
480 return 0;
481 }
482
483 /*
484 * Read a portion of a file into an internal buffer.
485 * Return the location in the buffer and the amount in the buffer.
486 */
487 static int
488 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
489 {
490 struct file *fp = (struct file *)f->f_fsdata;
491 FS *fs = fp->f_fs;
492 long off;
493 indp_t file_block;
494 size_t block_size;
495 int rc;
496
497 off = ufs_blkoff(fs, fp->f_seekp);
498 file_block = ufs_lblkno(fs, fp->f_seekp);
499 #ifdef LIBSA_LFS
500 block_size = (size_t)dblksize(fs, &fp->f_di, (uint64_t)file_block);
501 #else
502 block_size = (size_t)ffs_sblksize(fs, (int64_t)fp->f_di.di_size, file_block);
503 #endif
504
505 if (file_block != fp->f_buf_blkno) {
506 indp_t disk_block = 0; /* XXX: gcc */
507 rc = block_map(f, file_block, &disk_block);
508 if (rc)
509 return rc;
510
511 if (disk_block == 0) {
512 memset(fp->f_buf, 0, block_size);
513 fp->f_buf_size = block_size;
514 } else {
515 twiddle();
516 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
517 FSBTODB(fs, disk_block),
518 block_size, fp->f_buf, &fp->f_buf_size);
519 if (rc)
520 return rc;
521 }
522
523 fp->f_buf_blkno = file_block;
524 }
525
526 /*
527 * Return address of byte in buffer corresponding to
528 * offset, and size of remainder of buffer after that
529 * byte.
530 */
531 *buf_p = fp->f_buf + off;
532 *size_p = block_size - off;
533
534 /*
535 * But truncate buffer at end of file.
536 */
537 if (*size_p > fp->f_di.di_size - fp->f_seekp)
538 *size_p = fp->f_di.di_size - fp->f_seekp;
539
540 return 0;
541 }
542
543 /*
544 * Search a directory for a name and return its
545 * inode number.
546 */
547 static int
548 search_directory(const char *name, int length, struct open_file *f,
549 ino32_t *inumber_p)
550 {
551 struct file *fp = (struct file *)f->f_fsdata;
552 struct direct *dp;
553 struct direct *edp;
554 char *buf;
555 size_t buf_size;
556 int namlen;
557 int rc;
558
559 fp->f_seekp = 0;
560 while (fp->f_seekp < (off_t)fp->f_di.di_size) {
561 rc = buf_read_file(f, &buf, &buf_size);
562 if (rc)
563 return rc;
564
565 dp = (struct direct *)buf;
566 edp = (struct direct *)(buf + buf_size);
567 for (; dp < edp;
568 dp = (void *)((char *)dp + ffs_get_reclen(fp, dp))) {
569 if (ffs_get_reclen(fp, dp) <= 0)
570 break;
571 if (ffs_get_ino(fp, dp) == (ino32_t)0)
572 continue;
573 #if BYTE_ORDER == LITTLE_ENDIAN
574 if (fp->f_fs->fs_maxsymlinklen <= 0)
575 namlen = dp->d_type;
576 else
577 #endif
578 namlen = dp->d_namlen;
579 if (namlen == length &&
580 !memcmp(name, dp->d_name, length)) {
581 /* found entry */
582 *inumber_p = ffs_get_ino(fp, dp);
583 return 0;
584 }
585 }
586 fp->f_seekp += buf_size;
587 }
588 return ENOENT;
589 }
590
591 static __inline__ int
592 ffs_find_superblock(struct open_file *f, FS *fs)
593 {
594 struct file *fp = (struct file *)f->f_fsdata;
595 int rc;
596 size_t buf_size;
597 #ifdef LIBSA_FFSv2
598 static daddr_t sblock_try[] = SBLOCKSEARCH;
599 int i;
600 #endif
601
602 #ifdef LIBSA_FFSv2
603 for (i = 0; sblock_try[i] != -1; i++) {
604 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
605 sblock_try[i] / GETSECSIZE(f), SBLOCKSIZE, fs, &buf_size);
606 if (rc)
607 return rc;
608 if (buf_size != SBLOCKSIZE)
609 return EINVAL;
610 ffs_fix_magic_swapped(fp, fs);
611 if (fs->fs_sblockloc != sblock_try[i])
612 /* an alternate superblock - try again */
613 continue;
614 if (ffs_is_magic(fs))
615 return 0;
616 }
617 return EINVAL;
618 #else /* LIBSA_FFSv2 */
619 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
620 SBLOCKOFFSET / GETSECSIZE(f), SBLOCKSIZE, fs, &buf_size);
621 if (rc)
622 return rc;
623 if (buf_size != SBLOCKSIZE)
624 return EINVAL;
625 ffs_fix_magic_swapped(fp, fs);
626
627 #ifdef LIBSA_LFS
628 if (fs->lfs_version != REQUIRED_LFS_VERSION)
629 return EINVAL;
630 #endif
631 if (!ffs_is_magic(fs))
632 return EINVAL;
633
634 return 0;
635 #endif /* !LIBSA_FFSv2 */
636 }
637
638 /*
639 * Open a file.
640 */
641 __compactcall int
642 ufs_open(const char *path, struct open_file *f)
643 {
644 #ifndef LIBSA_FS_SINGLECOMPONENT
645 const char *cp, *ncp;
646 int c;
647 #endif
648 ino32_t inumber;
649 struct file *fp;
650 FS *fs;
651 int rc;
652 #ifndef LIBSA_NO_FS_SYMLINK
653 ino32_t parent_inumber;
654 int nlinks = 0;
655 char namebuf[MAXPATHLEN+1];
656 char *buf;
657 #endif
658
659 /* allocate file system specific data structure */
660 fp = alloc(sizeof(struct file));
661 memset(fp, 0, sizeof(struct file));
662 f->f_fsdata = (void *)fp;
663
664 /* allocate space and read super block */
665 fs = alloc(SBLOCKSIZE);
666 fp->f_fs = fs;
667 twiddle();
668
669 rc = ffs_find_superblock(f, fs);
670 if (rc)
671 goto out;
672
673 #if defined(LIBSA_LFS) && REQUIRED_LFS_VERSION == 2
674 /*
675 * XXX We should check the second superblock and use the eldest
676 * of the two. See comments near the top of lfs_mountfs()
677 * in sys/ufs/lfs/lfs_vfsops.c.
678 * This may need a LIBSA_LFS_SMALL check as well.
679 */
680 #endif
681 #if defined(LIBSA_LFS)
682 fs->lfs_is64 = 0;
683 fs->lfs_dobyteswap = 0;
684 fs->lfs_hasolddirfmt = (fs->fs_maxsymlinklen <= 0);
685 #endif
686 #ifdef LIBSA_FFSv1
687 ffs_oldfscompat(fs);
688 #endif
689
690 if (fs->fs_bsize > MAXBSIZE ||
691 (size_t)fs->fs_bsize < sizeof(FS)) {
692 rc = EINVAL;
693 goto out;
694 }
695
696 /*
697 * Calculate indirect block levels.
698 */
699 {
700 indp_t mult;
701 int ln2;
702
703 /*
704 * We note that the number of indirect blocks is always
705 * a power of 2. This lets us use shifts and masks instead
706 * of divide and remainder and avoids pulling in the
707 * 64bit division routine into the boot code.
708 */
709 mult = UFS_NINDIR(fs);
710 #ifdef DEBUG
711 if (mult & (mult - 1)) {
712 /* Hummm was't a power of 2 */
713 rc = EINVAL;
714 goto out;
715 }
716 #endif
717 for (ln2 = 0; mult != 1; ln2++)
718 mult >>= 1;
719
720 fp->f_nishift = ln2;
721 }
722
723 /* alloc a block sized buffer used for all fs transfers */
724 fp->f_buf = alloc(fs->fs_bsize);
725 inumber = UFS_ROOTINO;
726 if ((rc = read_inode(inumber, f)) != 0)
727 goto out;
728
729 #ifndef LIBSA_FS_SINGLECOMPONENT
730 cp = path;
731 while (*cp) {
732
733 /*
734 * Remove extra separators
735 */
736 while (*cp == '/')
737 cp++;
738 if (*cp == '\0')
739 break;
740
741 /*
742 * Check that current node is a directory.
743 */
744 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
745 rc = ENOTDIR;
746 goto out;
747 }
748
749 /*
750 * Get next component of path name.
751 */
752 ncp = cp;
753 while ((c = *cp) != '\0' && c != '/')
754 cp++;
755
756 /*
757 * Look up component in current directory.
758 * Save directory inumber in case we find a
759 * symbolic link.
760 */
761 #ifndef LIBSA_NO_FS_SYMLINK
762 parent_inumber = inumber;
763 #endif
764 rc = search_directory(ncp, cp - ncp, f, &inumber);
765 if (rc)
766 goto out;
767
768 /*
769 * Open next component.
770 */
771 if ((rc = read_inode(inumber, f)) != 0)
772 goto out;
773
774 #ifndef LIBSA_NO_FS_SYMLINK
775 /*
776 * Check for symbolic link.
777 */
778 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
779 int link_len = fp->f_di.di_size;
780 int len;
781
782 len = strlen(cp);
783
784 if (link_len + len > MAXPATHLEN ||
785 ++nlinks > MAXSYMLINKS) {
786 rc = ENOENT;
787 goto out;
788 }
789
790 memmove(&namebuf[link_len], cp, len + 1);
791
792 if (link_len < fs->fs_maxsymlinklen) {
793 memcpy(namebuf, fp->f_di.di_db, link_len);
794 } else {
795 /*
796 * Read file for symbolic link
797 */
798 size_t buf_size;
799 indp_t disk_block;
800
801 buf = fp->f_buf;
802 rc = block_map(f, (indp_t)0, &disk_block);
803 if (rc)
804 goto out;
805
806 twiddle();
807 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
808 F_READ, FSBTODB(fs, disk_block),
809 fs->fs_bsize, buf, &buf_size);
810 if (rc)
811 goto out;
812
813 memcpy(namebuf, buf, link_len);
814 }
815
816 /*
817 * If relative pathname, restart at parent directory.
818 * If absolute pathname, restart at root.
819 */
820 cp = namebuf;
821 if (*cp != '/')
822 inumber = parent_inumber;
823 else
824 inumber = (ino32_t)UFS_ROOTINO;
825
826 if ((rc = read_inode(inumber, f)) != 0)
827 goto out;
828 }
829 #endif /* !LIBSA_NO_FS_SYMLINK */
830 }
831
832 /*
833 * Found terminal component.
834 */
835 rc = 0;
836
837 #else /* !LIBSA_FS_SINGLECOMPONENT */
838
839 /* look up component in the current (root) directory */
840 rc = search_directory(path, strlen(path), f, &inumber);
841 if (rc)
842 goto out;
843
844 /* open it */
845 rc = read_inode(inumber, f);
846
847 #endif /* !LIBSA_FS_SINGLECOMPONENT */
848
849 fp->f_seekp = 0; /* reset seek pointer */
850
851 out:
852 if (rc)
853 ufs_close(f);
854 else
855 #ifdef FSMOD
856 fsmod = FSMOD;
857 #else
858 fsmod = NULL;
859 #endif
860 return rc;
861 }
862
863 __compactcall int
864 ufs_close(struct open_file *f)
865 {
866 struct file *fp = (struct file *)f->f_fsdata;
867
868 f->f_fsdata = NULL;
869 if (fp == NULL)
870 return 0;
871
872 if (fp->f_buf)
873 dealloc(fp->f_buf, fp->f_fs->fs_bsize);
874 dealloc(fp->f_fs, SBLOCKSIZE);
875 dealloc(fp, sizeof(struct file));
876 return 0;
877 }
878
879 /*
880 * Copy a portion of a file into kernel memory.
881 * Cross block boundaries when necessary.
882 */
883 __compactcall int
884 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
885 {
886 struct file *fp = (struct file *)f->f_fsdata;
887 size_t csize;
888 char *buf;
889 size_t buf_size;
890 int rc = 0;
891 char *addr = start;
892
893 while (size != 0) {
894 if (fp->f_seekp >= (off_t)fp->f_di.di_size)
895 break;
896
897 rc = buf_read_file(f, &buf, &buf_size);
898 if (rc)
899 break;
900
901 csize = size;
902 if (csize > buf_size)
903 csize = buf_size;
904
905 memcpy(addr, buf, csize);
906
907 fp->f_seekp += csize;
908 addr += csize;
909 size -= csize;
910 }
911 if (resid)
912 *resid = size;
913 return rc;
914 }
915
916 /*
917 * Not implemented.
918 */
919 #ifndef LIBSA_NO_FS_WRITE
920 __compactcall int
921 ufs_write(struct open_file *f, void *start, size_t size, size_t *resid)
922 {
923
924 return EROFS;
925 }
926 #endif /* !LIBSA_NO_FS_WRITE */
927
928 #ifndef LIBSA_NO_FS_SEEK
929 __compactcall off_t
930 ufs_seek(struct open_file *f, off_t offset, int where)
931 {
932 struct file *fp = (struct file *)f->f_fsdata;
933
934 switch (where) {
935 case SEEK_SET:
936 fp->f_seekp = offset;
937 break;
938 case SEEK_CUR:
939 fp->f_seekp += offset;
940 break;
941 case SEEK_END:
942 fp->f_seekp = fp->f_di.di_size - offset;
943 break;
944 default:
945 return -1;
946 }
947 return fp->f_seekp;
948 }
949 #endif /* !LIBSA_NO_FS_SEEK */
950
951 __compactcall int
952 ufs_stat(struct open_file *f, struct stat *sb)
953 {
954 struct file *fp = (struct file *)f->f_fsdata;
955
956 /* only important stuff */
957 memset(sb, 0, sizeof *sb);
958 sb->st_mode = fp->f_di.di_mode;
959 sb->st_uid = fp->f_di.di_uid;
960 sb->st_gid = fp->f_di.di_gid;
961 sb->st_size = fp->f_di.di_size;
962 return 0;
963 }
964
965 #if defined(LIBSA_ENABLE_LS_OP)
966
967 #include "ls.h"
968
969 static const char *const typestr[] = {
970 "unknown",
971 "FIFO",
972 "CHR",
973 0,
974 "DIR",
975 0,
976 "BLK",
977 0,
978 "REG",
979 0,
980 "LNK",
981 0,
982 "SOCK",
983 0,
984 "WHT"
985 };
986
987 __compactcall void
988 ufs_ls(struct open_file *f, const char *pattern)
989 {
990 struct file *fp = (struct file *)f->f_fsdata;
991 char *buf;
992 size_t buf_size;
993 lsentry_t *names = NULL;
994
995 fp->f_seekp = 0;
996 while (fp->f_seekp < (off_t)fp->f_di.di_size) {
997 struct direct *dp, *edp;
998 int rc = buf_read_file(f, &buf, &buf_size);
999 if (rc)
1000 goto out;
1001 /* some firmware might use block size larger than DEV_BSIZE */
1002 if (buf_size < UFS_DIRBLKSIZ)
1003 goto out;
1004
1005 dp = (struct direct *)buf;
1006 edp = (struct direct *)(buf + buf_size);
1007
1008 for (; dp < edp;
1009 dp = (void *)((char *)dp + ffs_get_reclen(fp, dp))) {
1010 const char *t;
1011 if (ffs_get_ino(fp, dp) == 0)
1012 continue;
1013
1014 if (dp->d_type >= NELEM(typestr) ||
1015 !(t = typestr[dp->d_type])) {
1016 /*
1017 * This does not handle "old"
1018 * filesystems properly. On little
1019 * endian machines, we get a bogus
1020 * type name if the namlen matches a
1021 * valid type identifier. We could
1022 * check if we read namlen "0" and
1023 * handle this case specially, if
1024 * there were a pressing need...
1025 */
1026 printf("bad dir entry\n");
1027 goto out;
1028 }
1029 lsadd(&names, pattern, dp->d_name, strlen(dp->d_name),
1030 ffs_get_ino(fp, dp), t);
1031 }
1032 fp->f_seekp += buf_size;
1033 }
1034 lsprint(names);
1035 out: lsfree(names);
1036 }
1037 #endif /* LIBSA_ENABLE_LS_OP */
1038
1039 #ifdef LIBSA_FFSv1
1040 /*
1041 * Sanity checks for old file systems.
1042 *
1043 * XXX - goes away some day.
1044 * Stripped of stuff libsa doesn't need.....
1045 */
1046 static void
1047 ffs_oldfscompat(FS *fs)
1048 {
1049
1050 #ifdef COMPAT_UFS
1051 /*
1052 * Newer Solaris versions have a slightly incompatible
1053 * superblock - so always calculate this values on the fly, which
1054 * is good enough for libsa purposes
1055 */
1056 if (fs->fs_magic == FS_UFS1_MAGIC
1057 #ifndef COMPAT_SOLARIS_UFS
1058 && fs->fs_old_inodefmt < FS_44INODEFMT
1059 #endif
1060 ) {
1061 fs->fs_qbmask = ~fs->fs_bmask;
1062 fs->fs_qfmask = ~fs->fs_fmask;
1063 }
1064 #endif
1065 }
1066 #endif
1067