ufs.c revision 1.41 1 1.41 dsl /* $NetBSD: ufs.c,v 1.41 2003/08/27 22:42:08 dsl Exp $ */
2 1.8 cgd
3 1.1 brezak /*-
4 1.1 brezak * Copyright (c) 1993
5 1.1 brezak * The Regents of the University of California. All rights reserved.
6 1.1 brezak *
7 1.1 brezak * This code is derived from software contributed to Berkeley by
8 1.1 brezak * The Mach Operating System project at Carnegie-Mellon University.
9 1.1 brezak *
10 1.1 brezak * Redistribution and use in source and binary forms, with or without
11 1.1 brezak * modification, are permitted provided that the following conditions
12 1.1 brezak * are met:
13 1.1 brezak * 1. Redistributions of source code must retain the above copyright
14 1.1 brezak * notice, this list of conditions and the following disclaimer.
15 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 brezak * notice, this list of conditions and the following disclaimer in the
17 1.1 brezak * documentation and/or other materials provided with the distribution.
18 1.36 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 brezak * may be used to endorse or promote products derived from this software
20 1.1 brezak * without specific prior written permission.
21 1.1 brezak *
22 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 brezak * SUCH DAMAGE.
33 1.22 simonb *
34 1.1 brezak *
35 1.1 brezak * Copyright (c) 1990, 1991 Carnegie Mellon University
36 1.1 brezak * All Rights Reserved.
37 1.1 brezak *
38 1.1 brezak * Author: David Golub
39 1.22 simonb *
40 1.1 brezak * Permission to use, copy, modify and distribute this software and its
41 1.1 brezak * documentation is hereby granted, provided that both the copyright
42 1.1 brezak * notice and this permission notice appear in all copies of the
43 1.1 brezak * software, derivative works or modified versions, and any portions
44 1.1 brezak * thereof, and that both notices appear in supporting documentation.
45 1.22 simonb *
46 1.1 brezak * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 1.1 brezak * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
48 1.1 brezak * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 1.22 simonb *
50 1.1 brezak * Carnegie Mellon requests users of this software to return to
51 1.22 simonb *
52 1.1 brezak * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
53 1.1 brezak * School of Computer Science
54 1.1 brezak * Carnegie Mellon University
55 1.1 brezak * Pittsburgh PA 15213-3890
56 1.22 simonb *
57 1.1 brezak * any improvements or extensions that they make and grant Carnegie the
58 1.1 brezak * rights to redistribute these changes.
59 1.27 cgd */
60 1.27 cgd
61 1.27 cgd /*
62 1.37 dsl * Stand-alone file reading package for UFS and LFS filesystems.
63 1.1 brezak */
64 1.1 brezak
65 1.1 brezak #include <sys/param.h>
66 1.1 brezak #include <sys/time.h>
67 1.1 brezak #include <ufs/ufs/dinode.h>
68 1.1 brezak #include <ufs/ufs/dir.h>
69 1.35 dsl #ifdef LIBSA_LFS
70 1.35 dsl #include <sys/queue.h>
71 1.35 dsl #include <sys/mount.h> /* XXX for MNAMELEN */
72 1.35 dsl #include <ufs/lfs/lfs.h>
73 1.35 dsl #else
74 1.20 ross #include <ufs/ffs/fs.h>
75 1.35 dsl #endif
76 1.29 thorpej #ifdef _STANDALONE
77 1.29 thorpej #include <lib/libkern/libkern.h>
78 1.29 thorpej #else
79 1.29 thorpej #include <string.h>
80 1.29 thorpej #endif
81 1.2 glass
82 1.1 brezak #include "stand.h"
83 1.35 dsl #ifdef LIBSA_LFS
84 1.35 dsl #include "lfs.h"
85 1.35 dsl #else
86 1.21 pk #include "ufs.h"
87 1.35 dsl #endif
88 1.35 dsl
89 1.35 dsl /* If this file is compiled by itself, build ufs (aka ffsv1) support */
90 1.35 dsl #if !defined(LIBSA_FFSv2) && !defined(LIBSA_LFS)
91 1.35 dsl #define LIBSA_FFSv1
92 1.35 dsl #endif
93 1.23 cgd
94 1.23 cgd #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
95 1.23 cgd #define LIBSA_NO_FS_SYMLINK
96 1.25 simonb #endif
97 1.25 simonb #if defined(COMPAT_UFS) && defined(LIBSA_NO_COMPAT_UFS)
98 1.25 simonb #undef COMPAT_UFS
99 1.23 cgd #endif
100 1.23 cgd
101 1.35 dsl #ifdef LIBSA_LFS
102 1.35 dsl /*
103 1.35 dsl * In-core LFS superblock. This exists only to placate the macros in lfs.h,
104 1.35 dsl */
105 1.35 dsl struct fs {
106 1.35 dsl struct dlfs lfs_dlfs;
107 1.33 fvdl };
108 1.35 dsl #define fs_magic lfs_magic
109 1.35 dsl #define fs_maxsymlinklen lfs_maxsymlinklen
110 1.33 fvdl
111 1.35 dsl #define FS_MAGIC LFS_MAGIC
112 1.35 dsl #define SBLOCKSIZE LFS_SBPAD
113 1.35 dsl #define SBLOCKOFFSET LFS_LABELPAD
114 1.33 fvdl #else
115 1.35 dsl /* NB ufs2 doesn't use the common suberblock code... */
116 1.35 dsl #define FS_MAGIC FS_UFS1_MAGIC
117 1.35 dsl #define SBLOCKOFFSET SBLOCK_UFS1
118 1.35 dsl #endif
119 1.35 dsl
120 1.35 dsl #if defined(LIBSA_NO_TWIDDLE)
121 1.35 dsl #define twiddle()
122 1.35 dsl #endif
123 1.35 dsl
124 1.40 dsl #undef cgstart
125 1.40 dsl #if defined(LIBSA_FFSv2)
126 1.40 dsl #define cgstart(fc, c) cgstart_ufs2((fs), (c))
127 1.40 dsl #else
128 1.40 dsl #define cgstart(fc, c) cgstart_ufs1((fs), (c))
129 1.40 dsl #endif
130 1.40 dsl
131 1.35 dsl #ifndef ufs_dinode
132 1.35 dsl #define ufs_dinode ufs1_dinode
133 1.35 dsl #endif
134 1.35 dsl #ifndef indp_t
135 1.35 dsl #define indp_t uint32_t
136 1.35 dsl #endif
137 1.35 dsl #ifndef FSBTODB
138 1.40 dsl #define FSBTODB(fs, indp) fsbtodb(fs, indp)
139 1.33 fvdl #endif
140 1.23 cgd
141 1.1 brezak /*
142 1.40 dsl * To avoid having a lot of filesystem-block sized buffers lurking (which
143 1.40 dsl * could be 32k) we only keep a few entries of the indirect block map.
144 1.40 dsl * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block
145 1.40 dsl * ~13 times pulling in a 6M kernel.
146 1.40 dsl * The cache size must be smaller than the smallest filesystem block,
147 1.40 dsl * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks).
148 1.40 dsl */
149 1.40 dsl #define LN2_IND_CACHE_SZ 6
150 1.40 dsl #define IND_CACHE_SZ (1 << LN2_IND_CACHE_SZ)
151 1.40 dsl #define IND_CACHE_MASK (IND_CACHE_SZ - 1)
152 1.40 dsl
153 1.40 dsl /*
154 1.1 brezak * In-core open file.
155 1.1 brezak */
156 1.1 brezak struct file {
157 1.1 brezak off_t f_seekp; /* seek pointer */
158 1.1 brezak struct fs *f_fs; /* pointer to super-block */
159 1.35 dsl struct ufs_dinode f_di; /* copy of on-disk inode */
160 1.40 dsl uint f_nishift; /* for blocks in indirect block */
161 1.40 dsl indp_t f_ind_cache_block;
162 1.40 dsl indp_t f_ind_cache[IND_CACHE_SZ];
163 1.40 dsl
164 1.1 brezak char *f_buf; /* buffer for data block */
165 1.13 cgd size_t f_buf_size; /* size of data block */
166 1.1 brezak daddr_t f_buf_blkno; /* block number of data block */
167 1.1 brezak };
168 1.1 brezak
169 1.38 dsl static int read_inode(ino_t, struct open_file *);
170 1.40 dsl static int block_map(struct open_file *, indp_t, indp_t *);
171 1.38 dsl static int buf_read_file(struct open_file *, char **, size_t *);
172 1.38 dsl static int search_directory(const char *, int, struct open_file *, ino_t *);
173 1.35 dsl #ifdef LIBSA_FFSv1
174 1.38 dsl static void ffs_oldfscompat(struct fs *);
175 1.35 dsl #endif
176 1.35 dsl #ifdef LIBSA_FFSv2
177 1.38 dsl static int ffs_find_superblock(struct open_file *, struct fs *);
178 1.35 dsl #endif
179 1.35 dsl
180 1.35 dsl #ifdef LIBSA_LFS
181 1.35 dsl /*
182 1.35 dsl * Find an inode's block. Look it up in the ifile. Whee!
183 1.35 dsl */
184 1.35 dsl static int
185 1.35 dsl find_inode_sector(ino_t inumber, struct open_file *f, daddr_t *isp)
186 1.35 dsl {
187 1.35 dsl struct file *fp = (struct file *)f->f_fsdata;
188 1.35 dsl struct fs *fs = fp->f_fs;
189 1.35 dsl daddr_t ifileent_blkno;
190 1.35 dsl char *ent_in_buf;
191 1.35 dsl size_t buf_after_ent;
192 1.35 dsl int rc;
193 1.33 fvdl
194 1.35 dsl rc = read_inode(fs->lfs_ifile, f);
195 1.35 dsl if (rc)
196 1.35 dsl return (rc);
197 1.35 dsl
198 1.35 dsl ifileent_blkno =
199 1.35 dsl (inumber / fs->lfs_ifpb) + fs->lfs_cleansz + fs->lfs_segtabsz;
200 1.35 dsl fp->f_seekp = (off_t)ifileent_blkno * fs->fs_bsize +
201 1.35 dsl (inumber % fs->lfs_ifpb) * sizeof (IFILE_Vx);
202 1.35 dsl rc = buf_read_file(f, &ent_in_buf, &buf_after_ent);
203 1.35 dsl if (rc)
204 1.35 dsl return (rc);
205 1.35 dsl /* make sure something's not badly wrong, but don't panic. */
206 1.35 dsl if (buf_after_ent < sizeof (IFILE_Vx))
207 1.35 dsl return (EINVAL);
208 1.35 dsl
209 1.35 dsl *isp = FSBTODB(fs, ((IFILE_Vx *)ent_in_buf)->if_daddr);
210 1.35 dsl if (*isp == LFS_UNUSED_DADDR) /* again, something badly wrong */
211 1.35 dsl return (EINVAL);
212 1.35 dsl return (0);
213 1.35 dsl }
214 1.7 brezak #endif
215 1.7 brezak
216 1.1 brezak /*
217 1.1 brezak * Read a new inode into a file structure.
218 1.1 brezak */
219 1.1 brezak static int
220 1.37 dsl read_inode(ino_t inumber, struct open_file *f)
221 1.1 brezak {
222 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
223 1.30 augustss struct fs *fs = fp->f_fs;
224 1.1 brezak char *buf;
225 1.13 cgd size_t rsize;
226 1.1 brezak int rc;
227 1.35 dsl daddr_t inode_sector;
228 1.35 dsl #ifdef LIBSA_LFS
229 1.35 dsl struct ufs_dinode *dip;
230 1.35 dsl int cnt;
231 1.35 dsl #endif
232 1.35 dsl
233 1.35 dsl #ifdef LIBSA_LFS
234 1.35 dsl if (inumber == fs->lfs_ifile)
235 1.35 dsl inode_sector = FSBTODB(fs, fs->lfs_idaddr);
236 1.35 dsl else if ((rc = find_inode_sector(inumber, f, &inode_sector)) != 0)
237 1.35 dsl return (rc);
238 1.35 dsl #else
239 1.35 dsl inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber));
240 1.35 dsl #endif
241 1.1 brezak
242 1.1 brezak /*
243 1.1 brezak * Read inode and save it.
244 1.1 brezak */
245 1.40 dsl buf = fp->f_buf;
246 1.11 mycroft twiddle();
247 1.23 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
248 1.40 dsl inode_sector, fs->fs_bsize, buf, &rsize);
249 1.1 brezak if (rc)
250 1.40 dsl return rc;
251 1.40 dsl if (rsize != fs->fs_bsize)
252 1.40 dsl return EIO;
253 1.1 brezak
254 1.35 dsl #ifdef LIBSA_LFS
255 1.35 dsl cnt = INOPBx(fs);
256 1.40 dsl dip = (struct ufs_dinode *)buf + (cnt - 1);
257 1.40 dsl for (; dip->di_inumber != inumber; --dip) {
258 1.40 dsl /* kernel code panics, but boot blocks which panic are Bad. */
259 1.40 dsl if (--cnt == 0)
260 1.40 dsl return EINVAL;
261 1.1 brezak }
262 1.35 dsl fp->f_di = *dip;
263 1.35 dsl #else
264 1.35 dsl fp->f_di = ((struct ufs_dinode *)buf)[ino_to_fsbo(fs, inumber)];
265 1.35 dsl #endif
266 1.1 brezak
267 1.1 brezak /*
268 1.1 brezak * Clear out the old buffers
269 1.1 brezak */
270 1.40 dsl fp->f_ind_cache_block = ~0;
271 1.40 dsl fp->f_buf_blkno = -1;
272 1.22 simonb return (rc);
273 1.1 brezak }
274 1.1 brezak
275 1.1 brezak /*
276 1.1 brezak * Given an offset in a file, find the disk block number that
277 1.1 brezak * contains that block.
278 1.1 brezak */
279 1.1 brezak static int
280 1.40 dsl block_map(struct open_file *f, indp_t file_block, indp_t *disk_block_p)
281 1.1 brezak {
282 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
283 1.30 augustss struct fs *fs = fp->f_fs;
284 1.1 brezak int level;
285 1.40 dsl indp_t ind_cache;
286 1.40 dsl indp_t ind_block_num;
287 1.40 dsl size_t rsize;
288 1.1 brezak int rc;
289 1.40 dsl indp_t *buf = (void *)fp->f_buf;
290 1.1 brezak
291 1.1 brezak /*
292 1.1 brezak * Index structure of an inode:
293 1.1 brezak *
294 1.1 brezak * di_db[0..NDADDR-1] hold block numbers for blocks
295 1.1 brezak * 0..NDADDR-1
296 1.1 brezak *
297 1.1 brezak * di_ib[0] index block 0 is the single indirect block
298 1.1 brezak * holds block numbers for blocks
299 1.1 brezak * NDADDR .. NDADDR + NINDIR(fs)-1
300 1.1 brezak *
301 1.1 brezak * di_ib[1] index block 1 is the double indirect block
302 1.1 brezak * holds block numbers for INDEX blocks for blocks
303 1.1 brezak * NDADDR + NINDIR(fs) ..
304 1.1 brezak * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
305 1.1 brezak *
306 1.1 brezak * di_ib[2] index block 2 is the triple indirect block
307 1.1 brezak * holds block numbers for double-indirect
308 1.1 brezak * blocks for blocks
309 1.1 brezak * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
310 1.1 brezak * NDADDR + NINDIR(fs) + NINDIR(fs)**2
311 1.1 brezak * + NINDIR(fs)**3 - 1
312 1.1 brezak */
313 1.1 brezak
314 1.1 brezak if (file_block < NDADDR) {
315 1.1 brezak /* Direct block. */
316 1.35 dsl *disk_block_p = fp->f_di.di_db[file_block];
317 1.1 brezak return (0);
318 1.1 brezak }
319 1.1 brezak
320 1.1 brezak file_block -= NDADDR;
321 1.1 brezak
322 1.40 dsl ind_cache = file_block >> LN2_IND_CACHE_SZ;
323 1.40 dsl if (ind_cache == fp->f_ind_cache_block) {
324 1.40 dsl *disk_block_p = fp->f_ind_cache[file_block & IND_CACHE_MASK];
325 1.40 dsl return 0;
326 1.40 dsl }
327 1.40 dsl
328 1.40 dsl for (level = 0;;) {
329 1.40 dsl level += fp->f_nishift;
330 1.40 dsl if (file_block < (indp_t)1 << level)
331 1.40 dsl break;
332 1.40 dsl if (level > NIADDR * fp->f_nishift)
333 1.38 dsl /* Block number too high */
334 1.38 dsl return (EFBIG);
335 1.40 dsl file_block -= (indp_t)1 << level;
336 1.1 brezak }
337 1.1 brezak
338 1.40 dsl ind_block_num = fp->f_di.di_ib[level / fp->f_nishift - 1];
339 1.1 brezak
340 1.40 dsl for (;;) {
341 1.40 dsl level -= fp->f_nishift;
342 1.1 brezak if (ind_block_num == 0) {
343 1.1 brezak *disk_block_p = 0; /* missing */
344 1.1 brezak return (0);
345 1.1 brezak }
346 1.1 brezak
347 1.40 dsl twiddle();
348 1.40 dsl /*
349 1.40 dsl * If we were feeling brave, we could work out the number
350 1.40 dsl * of the disk sector and read a single disk sector instead
351 1.40 dsl * of a filesystem block.
352 1.40 dsl * However we don't do this very often anyway...
353 1.40 dsl */
354 1.40 dsl rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
355 1.40 dsl FSBTODB(fp->f_fs, ind_block_num), fs->fs_bsize,
356 1.40 dsl buf, &rsize);
357 1.40 dsl if (rc)
358 1.40 dsl return (rc);
359 1.40 dsl if (rsize != fs->fs_bsize)
360 1.40 dsl return EIO;
361 1.40 dsl ind_block_num = buf[file_block >> level];
362 1.40 dsl if (level == 0)
363 1.40 dsl break;
364 1.40 dsl file_block &= (1 << level) - 1;
365 1.40 dsl }
366 1.1 brezak
367 1.40 dsl /* Save the part of the block that contains this sector */
368 1.40 dsl memcpy(fp->f_ind_cache, &buf[file_block & ~IND_CACHE_MASK],
369 1.40 dsl IND_CACHE_SZ * sizeof fp->f_ind_cache[0]);
370 1.40 dsl fp->f_ind_cache_block = ind_cache;
371 1.1 brezak
372 1.1 brezak *disk_block_p = ind_block_num;
373 1.1 brezak
374 1.1 brezak return (0);
375 1.1 brezak }
376 1.1 brezak
377 1.1 brezak /*
378 1.40 dsl * Read a portion of a file into an internal buffer.
379 1.40 dsl * Return the location in the buffer and the amount in the buffer.
380 1.1 brezak */
381 1.1 brezak static int
382 1.37 dsl buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
383 1.1 brezak {
384 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
385 1.30 augustss struct fs *fs = fp->f_fs;
386 1.1 brezak long off;
387 1.40 dsl indp_t file_block;
388 1.40 dsl indp_t disk_block;
389 1.13 cgd size_t block_size;
390 1.1 brezak int rc;
391 1.1 brezak
392 1.1 brezak off = blkoff(fs, fp->f_seekp);
393 1.1 brezak file_block = lblkno(fs, fp->f_seekp);
394 1.35 dsl #ifdef LIBSA_LFS
395 1.35 dsl block_size = dblksize(fs, &fp->f_di, file_block);
396 1.35 dsl #else
397 1.35 dsl block_size = sblksize(fs, fp->f_di.di_size, file_block);
398 1.35 dsl #endif
399 1.1 brezak
400 1.1 brezak if (file_block != fp->f_buf_blkno) {
401 1.1 brezak rc = block_map(f, file_block, &disk_block);
402 1.1 brezak if (rc)
403 1.1 brezak return (rc);
404 1.1 brezak
405 1.1 brezak if (disk_block == 0) {
406 1.41 dsl memset(fp->f_buf, 0, block_size);
407 1.1 brezak fp->f_buf_size = block_size;
408 1.1 brezak } else {
409 1.11 mycroft twiddle();
410 1.23 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
411 1.35 dsl FSBTODB(fs, disk_block),
412 1.1 brezak block_size, fp->f_buf, &fp->f_buf_size);
413 1.1 brezak if (rc)
414 1.1 brezak return (rc);
415 1.1 brezak }
416 1.1 brezak
417 1.1 brezak fp->f_buf_blkno = file_block;
418 1.1 brezak }
419 1.1 brezak
420 1.1 brezak /*
421 1.1 brezak * Return address of byte in buffer corresponding to
422 1.1 brezak * offset, and size of remainder of buffer after that
423 1.1 brezak * byte.
424 1.1 brezak */
425 1.1 brezak *buf_p = fp->f_buf + off;
426 1.1 brezak *size_p = block_size - off;
427 1.1 brezak
428 1.1 brezak /*
429 1.1 brezak * But truncate buffer at end of file.
430 1.1 brezak */
431 1.35 dsl if (*size_p > fp->f_di.di_size - fp->f_seekp)
432 1.35 dsl *size_p = fp->f_di.di_size - fp->f_seekp;
433 1.1 brezak
434 1.1 brezak return (0);
435 1.1 brezak }
436 1.1 brezak
437 1.1 brezak /*
438 1.1 brezak * Search a directory for a name and return its
439 1.38 dsl * inode number.
440 1.1 brezak */
441 1.1 brezak static int
442 1.38 dsl search_directory(const char *name, int length, struct open_file *f,
443 1.38 dsl ino_t *inumber_p)
444 1.1 brezak {
445 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
446 1.30 augustss struct direct *dp;
447 1.1 brezak struct direct *edp;
448 1.1 brezak char *buf;
449 1.13 cgd size_t buf_size;
450 1.38 dsl int namlen;
451 1.1 brezak int rc;
452 1.1 brezak
453 1.1 brezak fp->f_seekp = 0;
454 1.35 dsl while (fp->f_seekp < fp->f_di.di_size) {
455 1.1 brezak rc = buf_read_file(f, &buf, &buf_size);
456 1.1 brezak if (rc)
457 1.1 brezak return (rc);
458 1.1 brezak
459 1.1 brezak dp = (struct direct *)buf;
460 1.1 brezak edp = (struct direct *)(buf + buf_size);
461 1.38 dsl for (;dp < edp; dp = (void *)((char *)dp + dp->d_reclen)) {
462 1.40 dsl if (dp->d_reclen <= 0)
463 1.40 dsl break;
464 1.1 brezak if (dp->d_ino == (ino_t)0)
465 1.38 dsl continue;
466 1.1 brezak #if BYTE_ORDER == LITTLE_ENDIAN
467 1.1 brezak if (fp->f_fs->fs_maxsymlinklen <= 0)
468 1.1 brezak namlen = dp->d_type;
469 1.1 brezak else
470 1.1 brezak #endif
471 1.1 brezak namlen = dp->d_namlen;
472 1.1 brezak if (namlen == length &&
473 1.38 dsl !memcmp(name, dp->d_name, length)) {
474 1.1 brezak /* found entry */
475 1.1 brezak *inumber_p = dp->d_ino;
476 1.1 brezak return (0);
477 1.1 brezak }
478 1.1 brezak }
479 1.1 brezak fp->f_seekp += buf_size;
480 1.1 brezak }
481 1.1 brezak return (ENOENT);
482 1.1 brezak }
483 1.1 brezak
484 1.35 dsl #ifdef LIBSA_FFSv2
485 1.33 fvdl
486 1.33 fvdl daddr_t sblock_try[] = SBLOCKSEARCH;
487 1.33 fvdl
488 1.33 fvdl static int
489 1.37 dsl ffs_find_superblock(struct open_file *f, struct fs *fs)
490 1.33 fvdl {
491 1.33 fvdl int i, rc;
492 1.33 fvdl size_t buf_size;
493 1.33 fvdl
494 1.33 fvdl for (i = 0; sblock_try[i] != -1; i++) {
495 1.33 fvdl rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
496 1.40 dsl sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, fs, &buf_size);
497 1.33 fvdl if (rc != 0 || buf_size != SBLOCKSIZE)
498 1.33 fvdl return rc;
499 1.33 fvdl if (fs->fs_magic == FS_UFS2_MAGIC) {
500 1.33 fvdl return 0;
501 1.33 fvdl }
502 1.33 fvdl }
503 1.33 fvdl return EINVAL;
504 1.33 fvdl }
505 1.33 fvdl
506 1.33 fvdl #endif
507 1.33 fvdl
508 1.1 brezak /*
509 1.1 brezak * Open a file.
510 1.1 brezak */
511 1.1 brezak int
512 1.38 dsl ufs_open(const char *path, struct open_file *f)
513 1.1 brezak {
514 1.23 cgd #ifndef LIBSA_FS_SINGLECOMPONENT
515 1.38 dsl const char *cp, *ncp;
516 1.30 augustss int c;
517 1.23 cgd #endif
518 1.23 cgd ino_t inumber;
519 1.1 brezak struct file *fp;
520 1.1 brezak struct fs *fs;
521 1.10 ws int rc;
522 1.23 cgd #ifndef LIBSA_NO_FS_SYMLINK
523 1.23 cgd ino_t parent_inumber;
524 1.10 ws int nlinks = 0;
525 1.1 brezak char namebuf[MAXPATHLEN+1];
526 1.40 dsl char *buf;
527 1.22 simonb #endif
528 1.1 brezak
529 1.1 brezak /* allocate file system specific data structure */
530 1.1 brezak fp = alloc(sizeof(struct file));
531 1.41 dsl memset(fp, 0, sizeof(struct file));
532 1.1 brezak f->f_fsdata = (void *)fp;
533 1.1 brezak
534 1.1 brezak /* allocate space and read super block */
535 1.33 fvdl fs = alloc(SBLOCKSIZE);
536 1.1 brezak fp->f_fs = fs;
537 1.11 mycroft twiddle();
538 1.33 fvdl
539 1.35 dsl #ifdef LIBSA_FFSv2
540 1.33 fvdl rc = ffs_find_superblock(f, fs);
541 1.33 fvdl if (rc)
542 1.33 fvdl goto out;
543 1.33 fvdl #else
544 1.35 dsl {
545 1.35 dsl size_t buf_size;
546 1.35 dsl rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
547 1.40 dsl SBLOCKOFFSET / DEV_BSIZE, SBLOCKSIZE, fs, &buf_size);
548 1.35 dsl if (rc)
549 1.35 dsl goto out;
550 1.35 dsl if (buf_size != SBLOCKSIZE ||
551 1.35 dsl #ifdef LIBSA_FFS
552 1.35 dsl fs->lfs_version != REQUIRED_LFS_VERSION ||
553 1.35 dsl #endif
554 1.35 dsl fs->fs_magic != FS_MAGIC) {
555 1.35 dsl rc = EINVAL;
556 1.35 dsl goto out;
557 1.35 dsl }
558 1.33 fvdl }
559 1.35 dsl #if defined(LIBSA_LFS) && REQUIRED_LFS_VERSION == 2
560 1.35 dsl /*
561 1.35 dsl * XXX We should check the second superblock and use the eldest
562 1.35 dsl * of the two. See comments near the top of lfs_mountfs()
563 1.35 dsl * in sys/ufs/lfs/lfs_vfsops.c.
564 1.35 dsl * This may need a LIBSA_LFS_SMALL check as well.
565 1.35 dsl */
566 1.35 dsl #endif
567 1.33 fvdl #endif
568 1.1 brezak
569 1.35 dsl #ifdef LIBSA_FFSv1
570 1.33 fvdl ffs_oldfscompat(fs);
571 1.35 dsl #endif
572 1.33 fvdl
573 1.33 fvdl if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
574 1.1 brezak rc = EINVAL;
575 1.1 brezak goto out;
576 1.1 brezak }
577 1.1 brezak
578 1.1 brezak /*
579 1.1 brezak * Calculate indirect block levels.
580 1.1 brezak */
581 1.1 brezak {
582 1.40 dsl indp_t mult;
583 1.35 dsl int ln2;
584 1.35 dsl
585 1.35 dsl /*
586 1.35 dsl * We note that the number of indirect blocks is always
587 1.35 dsl * a power of 2. This lets us use shifts and masks instead
588 1.35 dsl * of divide and remainder and avoinds pulling in the
589 1.35 dsl * 64bit division routine into the boot code.
590 1.35 dsl */
591 1.35 dsl mult = NINDIR(fs);
592 1.35 dsl #ifdef DEBUG
593 1.35 dsl if (mult & (mult - 1)) {
594 1.35 dsl /* Hummm was't a power of 2 */
595 1.35 dsl rc = EINVAL;
596 1.35 dsl goto out;
597 1.35 dsl }
598 1.35 dsl #endif
599 1.35 dsl for (ln2 = 0; mult != 1; ln2++)
600 1.35 dsl mult >>= 1;
601 1.1 brezak
602 1.40 dsl fp->f_nishift = ln2;
603 1.1 brezak }
604 1.1 brezak
605 1.40 dsl /* alloc a block sized buffer used for all fs transfers */
606 1.40 dsl fp->f_buf = alloc(fs->fs_bsize);
607 1.1 brezak inumber = ROOTINO;
608 1.1 brezak if ((rc = read_inode(inumber, f)) != 0)
609 1.1 brezak goto out;
610 1.1 brezak
611 1.23 cgd #ifndef LIBSA_FS_SINGLECOMPONENT
612 1.1 brezak cp = path;
613 1.1 brezak while (*cp) {
614 1.1 brezak
615 1.1 brezak /*
616 1.1 brezak * Remove extra separators
617 1.1 brezak */
618 1.1 brezak while (*cp == '/')
619 1.1 brezak cp++;
620 1.1 brezak if (*cp == '\0')
621 1.1 brezak break;
622 1.1 brezak
623 1.1 brezak /*
624 1.1 brezak * Check that current node is a directory.
625 1.1 brezak */
626 1.35 dsl if ((fp->f_di.di_mode & IFMT) != IFDIR) {
627 1.1 brezak rc = ENOTDIR;
628 1.1 brezak goto out;
629 1.1 brezak }
630 1.1 brezak
631 1.1 brezak /*
632 1.1 brezak * Get next component of path name.
633 1.1 brezak */
634 1.38 dsl ncp = cp;
635 1.38 dsl while ((c = *cp) != '\0' && c != '/')
636 1.38 dsl cp++;
637 1.1 brezak
638 1.1 brezak /*
639 1.1 brezak * Look up component in current directory.
640 1.1 brezak * Save directory inumber in case we find a
641 1.1 brezak * symbolic link.
642 1.1 brezak */
643 1.23 cgd #ifndef LIBSA_NO_FS_SYMLINK
644 1.1 brezak parent_inumber = inumber;
645 1.23 cgd #endif
646 1.38 dsl rc = search_directory(ncp, cp - ncp, f, &inumber);
647 1.1 brezak if (rc)
648 1.1 brezak goto out;
649 1.1 brezak
650 1.1 brezak /*
651 1.1 brezak * Open next component.
652 1.1 brezak */
653 1.1 brezak if ((rc = read_inode(inumber, f)) != 0)
654 1.1 brezak goto out;
655 1.1 brezak
656 1.23 cgd #ifndef LIBSA_NO_FS_SYMLINK
657 1.1 brezak /*
658 1.1 brezak * Check for symbolic link.
659 1.1 brezak */
660 1.35 dsl if ((fp->f_di.di_mode & IFMT) == IFLNK) {
661 1.35 dsl int link_len = fp->f_di.di_size;
662 1.10 ws int len;
663 1.10 ws
664 1.10 ws len = strlen(cp);
665 1.1 brezak
666 1.10 ws if (link_len + len > MAXPATHLEN ||
667 1.10 ws ++nlinks > MAXSYMLINKS) {
668 1.1 brezak rc = ENOENT;
669 1.1 brezak goto out;
670 1.1 brezak }
671 1.1 brezak
672 1.38 dsl memmove(&namebuf[link_len], cp, len + 1);
673 1.1 brezak
674 1.9 pk if (link_len < fs->fs_maxsymlinklen) {
675 1.38 dsl memcpy(namebuf, fp->f_di.di_db, link_len);
676 1.1 brezak } else {
677 1.1 brezak /*
678 1.1 brezak * Read file for symbolic link
679 1.1 brezak */
680 1.13 cgd size_t buf_size;
681 1.40 dsl indp_t disk_block;
682 1.9 pk
683 1.40 dsl buf = fp->f_buf;
684 1.40 dsl rc = block_map(f, (indp_t)0, &disk_block);
685 1.9 pk if (rc)
686 1.9 pk goto out;
687 1.22 simonb
688 1.11 mycroft twiddle();
689 1.23 cgd rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
690 1.35 dsl F_READ, FSBTODB(fs, disk_block),
691 1.9 pk fs->fs_bsize, buf, &buf_size);
692 1.1 brezak if (rc)
693 1.1 brezak goto out;
694 1.1 brezak
695 1.38 dsl memcpy(namebuf, buf, link_len);
696 1.1 brezak }
697 1.1 brezak
698 1.1 brezak /*
699 1.1 brezak * If relative pathname, restart at parent directory.
700 1.1 brezak * If absolute pathname, restart at root.
701 1.1 brezak */
702 1.1 brezak cp = namebuf;
703 1.1 brezak if (*cp != '/')
704 1.1 brezak inumber = parent_inumber;
705 1.1 brezak else
706 1.1 brezak inumber = (ino_t)ROOTINO;
707 1.1 brezak
708 1.9 pk if ((rc = read_inode(inumber, f)) != 0)
709 1.1 brezak goto out;
710 1.1 brezak }
711 1.23 cgd #endif /* !LIBSA_NO_FS_SYMLINK */
712 1.1 brezak }
713 1.1 brezak
714 1.1 brezak /*
715 1.1 brezak * Found terminal component.
716 1.1 brezak */
717 1.1 brezak rc = 0;
718 1.23 cgd
719 1.23 cgd #else /* !LIBSA_FS_SINGLECOMPONENT */
720 1.23 cgd
721 1.23 cgd /* look up component in the current (root) directory */
722 1.38 dsl rc = search_directory(path, strlen(path), f, &inumber);
723 1.23 cgd if (rc)
724 1.23 cgd goto out;
725 1.23 cgd
726 1.23 cgd /* open it */
727 1.23 cgd rc = read_inode(inumber, f);
728 1.23 cgd
729 1.23 cgd #endif /* !LIBSA_FS_SINGLECOMPONENT */
730 1.26 cgd
731 1.26 cgd fp->f_seekp = 0; /* reset seek pointer */
732 1.23 cgd
733 1.1 brezak out:
734 1.40 dsl if (rc)
735 1.38 dsl ufs_close(f);
736 1.1 brezak return (rc);
737 1.1 brezak }
738 1.1 brezak
739 1.1 brezak int
740 1.37 dsl ufs_close(struct open_file *f)
741 1.1 brezak {
742 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
743 1.1 brezak
744 1.38 dsl f->f_fsdata = NULL;
745 1.38 dsl if (fp == NULL)
746 1.1 brezak return (0);
747 1.1 brezak
748 1.1 brezak if (fp->f_buf)
749 1.1 brezak free(fp->f_buf, fp->f_fs->fs_bsize);
750 1.33 fvdl free(fp->f_fs, SBLOCKSIZE);
751 1.1 brezak free(fp, sizeof(struct file));
752 1.1 brezak return (0);
753 1.1 brezak }
754 1.1 brezak
755 1.1 brezak /*
756 1.1 brezak * Copy a portion of a file into kernel memory.
757 1.1 brezak * Cross block boundaries when necessary.
758 1.1 brezak */
759 1.1 brezak int
760 1.37 dsl ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
761 1.1 brezak {
762 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
763 1.30 augustss size_t csize;
764 1.1 brezak char *buf;
765 1.13 cgd size_t buf_size;
766 1.1 brezak int rc = 0;
767 1.30 augustss char *addr = start;
768 1.1 brezak
769 1.1 brezak while (size != 0) {
770 1.35 dsl if (fp->f_seekp >= fp->f_di.di_size)
771 1.1 brezak break;
772 1.1 brezak
773 1.1 brezak rc = buf_read_file(f, &buf, &buf_size);
774 1.1 brezak if (rc)
775 1.1 brezak break;
776 1.1 brezak
777 1.1 brezak csize = size;
778 1.1 brezak if (csize > buf_size)
779 1.1 brezak csize = buf_size;
780 1.1 brezak
781 1.38 dsl memcpy(addr, buf, csize);
782 1.1 brezak
783 1.1 brezak fp->f_seekp += csize;
784 1.12 pk addr += csize;
785 1.1 brezak size -= csize;
786 1.1 brezak }
787 1.1 brezak if (resid)
788 1.1 brezak *resid = size;
789 1.1 brezak return (rc);
790 1.1 brezak }
791 1.1 brezak
792 1.1 brezak /*
793 1.1 brezak * Not implemented.
794 1.1 brezak */
795 1.23 cgd #ifndef LIBSA_NO_FS_WRITE
796 1.1 brezak int
797 1.37 dsl ufs_write(struct open_file *f, void *start, size_t size, size_t *resid)
798 1.1 brezak {
799 1.1 brezak
800 1.1 brezak return (EROFS);
801 1.1 brezak }
802 1.23 cgd #endif /* !LIBSA_NO_FS_WRITE */
803 1.1 brezak
804 1.23 cgd #ifndef LIBSA_NO_FS_SEEK
805 1.1 brezak off_t
806 1.37 dsl ufs_seek(struct open_file *f, off_t offset, int where)
807 1.1 brezak {
808 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
809 1.1 brezak
810 1.1 brezak switch (where) {
811 1.1 brezak case SEEK_SET:
812 1.1 brezak fp->f_seekp = offset;
813 1.1 brezak break;
814 1.1 brezak case SEEK_CUR:
815 1.1 brezak fp->f_seekp += offset;
816 1.1 brezak break;
817 1.1 brezak case SEEK_END:
818 1.35 dsl fp->f_seekp = fp->f_di.di_size - offset;
819 1.1 brezak break;
820 1.1 brezak default:
821 1.1 brezak return (-1);
822 1.1 brezak }
823 1.1 brezak return (fp->f_seekp);
824 1.1 brezak }
825 1.23 cgd #endif /* !LIBSA_NO_FS_SEEK */
826 1.1 brezak
827 1.1 brezak int
828 1.37 dsl ufs_stat(struct open_file *f, struct stat *sb)
829 1.1 brezak {
830 1.30 augustss struct file *fp = (struct file *)f->f_fsdata;
831 1.1 brezak
832 1.1 brezak /* only important stuff */
833 1.40 dsl memset(sb, 0, sizeof *sb);
834 1.35 dsl sb->st_mode = fp->f_di.di_mode;
835 1.35 dsl sb->st_uid = fp->f_di.di_uid;
836 1.35 dsl sb->st_gid = fp->f_di.di_gid;
837 1.35 dsl sb->st_size = fp->f_di.di_size;
838 1.4 pk return (0);
839 1.4 pk }
840 1.4 pk
841 1.35 dsl #ifdef LIBSA_FFSv1
842 1.4 pk /*
843 1.4 pk * Sanity checks for old file systems.
844 1.4 pk *
845 1.4 pk * XXX - goes away some day.
846 1.40 dsl * Stripped of stuff libsa doesn't need.....
847 1.4 pk */
848 1.13 cgd static void
849 1.37 dsl ffs_oldfscompat(struct fs *fs)
850 1.4 pk {
851 1.4 pk
852 1.33 fvdl #ifdef COMPAT_UFS
853 1.33 fvdl if (fs->fs_magic == FS_UFS1_MAGIC &&
854 1.33 fvdl fs->fs_old_inodefmt < FS_44INODEFMT) {
855 1.33 fvdl fs->fs_qbmask = ~fs->fs_bmask;
856 1.33 fvdl fs->fs_qfmask = ~fs->fs_fmask;
857 1.33 fvdl }
858 1.33 fvdl #endif
859 1.1 brezak }
860 1.35 dsl #endif
861