Consolidate moved variables.
[dragonfly.git] / sys / boot / common / ufsread.c
CommitLineData
5ee58eed
MD
1/*-
2 * Copyright (c) 2002 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9 * research program
10 *
11 * Copyright (c) 1998 Robert Nordier
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms are freely
15 * permitted provided that the above copyright notice and this
16 * paragraph and the following disclaimer are duplicated in all
17 * such forms.
18 *
19 * This software is provided "AS IS" and without any express or
20 * implied warranties, including, without limitation, the implied
21 * warranties of merchantability and fitness for a particular
22 * purpose.
23 *
24 * $FreeBSD: src/sys/boot/common/ufsread.c,v 1.12 2003/08/25 23:30:41 obrien Exp $
9c92cc07 25 * $DragonFly: src/sys/boot/common/ufsread.c,v 1.5 2008/09/13 11:46:28 corecode Exp $
5ee58eed
MD
26 */
27
b349062c 28#include <vfs/ufs/dir.h>
5ee58eed
MD
29#include "dinode.h"
30#include "fs.h"
31#ifdef __i386__
32/* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
33 (see sys/ufs/ffs/fs.h rev 1.39) so that i386 boot loader (boot2) can
34 support both UFS1 and UFS2 again. */
35#undef cgbase
36#define cgbase(fs, c) ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
37#endif
38
39/*
40 * We use 4k `virtual' blocks for filesystem data, whatever the actual
41 * filesystem block size. FFS blocks are always a multiple of 4k.
42 */
43#define VBLKSHIFT 12
44#define VBLKSIZE (1 << VBLKSHIFT)
45#define VBLKMASK (VBLKSIZE - 1)
46#define DBPERVBLK (VBLKSIZE / DEV_BSIZE)
47#define INDIRPERVBLK(fs) (NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
48#define IPERVBLK(fs) (INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
49#define INO_TO_VBA(fs, ipervblk, x) \
50 (fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \
51 (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
52#define INO_TO_VBO(ipervblk, x) ((x) % ipervblk)
53#define FS_TO_VBA(fs, fsb, off) (fsbtodb(fs, fsb) + \
54 ((off) / VBLKSIZE) * DBPERVBLK)
55#define FS_TO_VBO(fs, fsb, off) ((off) & VBLKMASK)
56
57/* Buffers that must not span a 64k boundary. */
58struct dmadat {
59 char blkbuf[VBLKSIZE]; /* filesystem blocks */
60 char indbuf[VBLKSIZE]; /* indir blocks */
61 char sbbuf[SBLOCKSIZE]; /* superblock */
62 char secbuf[DEV_BSIZE]; /* for MBR/disklabel */
63};
64static struct dmadat *dmadat;
65
66static ino_t lookup(const char *);
67static ssize_t fsread(ino_t, void *, size_t);
68
5ee58eed
MD
69static __inline__ int
70fsfind(const char *name, ino_t * ino)
71{
72 char buf[DEV_BSIZE];
b349062c 73 struct direct *d;
5ee58eed
MD
74 char *s;
75 ssize_t n;
76
77 fs_off = 0;
78 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
79 for (s = buf; s < buf + DEV_BSIZE;) {
80 d = (void *)s;
81 if (ls)
82 printf("%s ", d->d_name);
83 else if (!strcmp(name, d->d_name)) {
ca0ebb6b 84 *ino = d->d_ino;
5ee58eed
MD
85 return d->d_type;
86 }
87 s += d->d_reclen;
88 }
89 if (n != -1 && ls)
90 printf("\n");
91 return 0;
92}
93
94static ino_t
95lookup(const char *path)
96{
97 char name[MAXNAMLEN + 1];
98 const char *s;
99 ino_t ino;
100 ssize_t n;
101 int dt;
102
103 ino = ROOTINO;
104 dt = DT_DIR;
105 name[0] = '/';
106 name[1] = '\0';
107 for (;;) {
108 if (*path == '/')
109 path++;
110 if (!*path)
111 break;
112 for (s = path; *s && *s != '/'; s++);
113 if ((n = s - path) > MAXNAMLEN)
114 return 0;
115 ls = *path == '?' && n == 1 && !*s;
116 memcpy(name, path, n);
117 name[n] = 0;
118 if (dt != DT_DIR) {
119 printf("%s: not a directory.\n", name);
120 return (0);
121 }
122 if ((dt = fsfind(name, &ino)) <= 0)
123 break;
124 path = s;
125 }
126 return dt == DT_REG ? ino : 0;
127}
128
129/*
130 * Possible superblock locations ordered from most to least likely.
131 */
132static int sblock_try[] = SBLOCKSEARCH;
133
134#if defined(UFS2_ONLY)
135#define DIP(field) dp2.field
136#elif defined(UFS1_ONLY)
137#define DIP(field) dp1.field
138#else
139#define DIP(field) fs->fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
140#endif
141
142static ssize_t
143fsread(ino_t inode, void *buf, size_t nbyte)
144{
145#ifndef UFS2_ONLY
146 static struct ufs1_dinode dp1;
147#endif
148#ifndef UFS1_ONLY
149 static struct ufs2_dinode dp2;
150#endif
151 static ino_t inomap;
152 char *blkbuf;
153 void *indbuf;
154 struct fs *fs;
155 char *s;
156 size_t n, nb, size, off, vboff;
157 ufs_lbn_t lbn;
158 ufs2_daddr_t addr, vbaddr;
159 static ufs2_daddr_t blkmap, indmap;
160 u_int u;
161
162
163 blkbuf = dmadat->blkbuf;
164 indbuf = dmadat->indbuf;
165 fs = (struct fs *)dmadat->sbbuf;
166 if (!dsk_meta) {
167 inomap = 0;
168 for (n = 0; sblock_try[n] != -1; n++) {
169 if (dskread(fs, sblock_try[n] / DEV_BSIZE,
170 SBLOCKSIZE / DEV_BSIZE))
171 return -1;
172 if ((
173#if defined(UFS1_ONLY)
174 fs->fs_magic == FS_UFS1_MAGIC
175#elif defined(UFS2_ONLY)
176 (fs->fs_magic == FS_UFS2_MAGIC &&
177 fs->fs_sblockloc == sblock_try[n])
178#else
179 fs->fs_magic == FS_UFS1_MAGIC ||
180 (fs->fs_magic == FS_UFS2_MAGIC &&
181 fs->fs_sblockloc == sblock_try[n])
182#endif
183 ) &&
184 fs->fs_bsize <= MAXBSIZE &&
185 fs->fs_bsize >= sizeof(struct fs))
186 break;
187 }
188 if (sblock_try[n] == -1) {
189 printf("Not ufs\n");
190 return -1;
191 }
192 dsk_meta++;
193 }
194 if (!inode)
195 return 0;
196 if (inomap != inode) {
197 n = IPERVBLK(fs);
198 if (dskread(blkbuf, INO_TO_VBA(fs, n, inode), DBPERVBLK))
199 return -1;
200 n = INO_TO_VBO(n, inode);
201#if defined(UFS1_ONLY)
202 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
203#elif defined(UFS2_ONLY)
204 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
205#else
206 if (fs->fs_magic == FS_UFS1_MAGIC)
207 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
208 else
209 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
210#endif
211 inomap = inode;
212 fs_off = 0;
213 blkmap = indmap = 0;
214 }
215 s = buf;
216 size = DIP(di_size);
217 n = size - fs_off;
218 if (nbyte > n)
219 nbyte = n;
220 nb = nbyte;
221 while (nb) {
222 lbn = lblkno(fs, fs_off);
223 off = blkoff(fs, fs_off);
224 if (lbn < NDADDR) {
225 addr = DIP(di_db[lbn]);
226 } else if (lbn < NDADDR + NINDIR(fs)) {
227 n = INDIRPERVBLK(fs);
228 addr = DIP(di_ib[0]);
c7401c3b 229 u = (u_int)(lbn - NDADDR) / n * DBPERVBLK;
5ee58eed
MD
230 vbaddr = fsbtodb(fs, addr) + u;
231 if (indmap != vbaddr) {
232 if (dskread(indbuf, vbaddr, DBPERVBLK))
233 return -1;
234 indmap = vbaddr;
235 }
236 n = (lbn - NDADDR) & (n - 1);
237#if defined(UFS1_ONLY)
238 addr = ((ufs1_daddr_t *)indbuf)[n];
239#elif defined(UFS2_ONLY)
240 addr = ((ufs2_daddr_t *)indbuf)[n];
241#else
242 if (fs->fs_magic == FS_UFS1_MAGIC)
243 addr = ((ufs1_daddr_t *)indbuf)[n];
244 else
245 addr = ((ufs2_daddr_t *)indbuf)[n];
246#endif
247 } else {
248 return -1;
249 }
250 vbaddr = fsbtodb(fs, addr) + (off >> VBLKSHIFT) * DBPERVBLK;
251 vboff = off & VBLKMASK;
252 n = sblksize(fs, size, lbn) - (off & ~VBLKMASK);
253 if (n > VBLKSIZE)
254 n = VBLKSIZE;
255 if (blkmap != vbaddr) {
256 if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
257 return -1;
258 blkmap = vbaddr;
259 }
260 n -= vboff;
261 if (n > nb)
262 n = nb;
263 memcpy(s, blkbuf + vboff, n);
264 s += n;
265 fs_off += n;
266 nb -= n;
267 }
268 return nbyte;
269}