]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/boot/common/ufsread.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / boot / common / ufsread.c
1 /*-
2  * Copyright (c) 2002 McAfee, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and McAfee Research,, the Security Research Division of
7  * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8  * part of the DARPA CHATS research program
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 1998 Robert Nordier
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms are freely
36  * permitted provided that the above copyright notice and this
37  * paragraph and the following disclaimer are duplicated in all
38  * such forms.
39  *
40  * This software is provided "AS IS" and without any express or
41  * implied warranties, including, without limitation, the implied
42  * warranties of merchantability and fitness for a particular
43  * purpose.
44  */
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.h>
52 #ifdef UFS_SMALL_CGBASE
53 /* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
54    (see sys/ufs/ffs/fs.h rev 1.39) so that small boot loaders (e.g. boot2) can
55    support both UFS1 and UFS2. */
56 #undef cgbase
57 #define cgbase(fs, c)   ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
58 #endif
59
60 /*
61  * We use 4k `virtual' blocks for filesystem data, whatever the actual
62  * filesystem block size. FFS blocks are always a multiple of 4k.
63  */
64 #define VBLKSHIFT       12
65 #define VBLKSIZE        (1 << VBLKSHIFT)
66 #define VBLKMASK        (VBLKSIZE - 1)
67 #define DBPERVBLK       (VBLKSIZE / DEV_BSIZE)
68 #define INDIRPERVBLK(fs) (NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
69 #define IPERVBLK(fs)    (INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
70 #define INO_TO_VBA(fs, ipervblk, x) \
71     (fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \
72     (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
73 #define INO_TO_VBO(ipervblk, x) ((x) % ipervblk)
74 #define FS_TO_VBA(fs, fsb, off) (fsbtodb(fs, fsb) + \
75     ((off) / VBLKSIZE) * DBPERVBLK)
76 #define FS_TO_VBO(fs, fsb, off) ((off) & VBLKMASK)
77
78 /* Buffers that must not span a 64k boundary. */
79 struct dmadat {
80         char blkbuf[VBLKSIZE];  /* filesystem blocks */
81         char indbuf[VBLKSIZE];  /* indir blocks */
82         char sbbuf[SBLOCKSIZE]; /* superblock */
83         char secbuf[DEV_BSIZE]; /* for MBR/disklabel */
84 };
85 static struct dmadat *dmadat;
86
87 static ino_t lookup(const char *);
88 static ssize_t fsread(ino_t, void *, size_t);
89
90 static int ls, dsk_meta;
91 static uint32_t fs_off;
92
93 static __inline int
94 fsfind(const char *name, ino_t * ino)
95 {
96         char buf[DEV_BSIZE];
97         struct direct *d;
98         char *s;
99         ssize_t n;
100
101         fs_off = 0;
102         while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
103                 for (s = buf; s < buf + DEV_BSIZE;) {
104                         d = (void *)s;
105                         if (ls)
106                                 printf("%s ", d->d_name);
107                         else if (!strcmp(name, d->d_name)) {
108                                 *ino = d->d_ino;
109                                 return d->d_type;
110                         }
111                         s += d->d_reclen;
112                 }
113         if (n != -1 && ls)
114                 printf("\n");
115         return 0;
116 }
117
118 static ino_t
119 lookup(const char *path)
120 {
121         char name[MAXNAMLEN + 1];
122         const char *s;
123         ino_t ino;
124         ssize_t n;
125         int dt;
126
127         ino = ROOTINO;
128         dt = DT_DIR;
129         name[0] = '/';
130         name[1] = '\0';
131         for (;;) {
132                 if (*path == '/')
133                         path++;
134                 if (!*path)
135                         break;
136                 for (s = path; *s && *s != '/'; s++);
137                 if ((n = s - path) > MAXNAMLEN)
138                         return 0;
139                 ls = *path == '?' && n == 1 && !*s;
140                 memcpy(name, path, n);
141                 name[n] = 0;
142                 if (dt != DT_DIR) {
143                         printf("%s: not a directory.\n", name);
144                         return (0);
145                 }
146                 if ((dt = fsfind(name, &ino)) <= 0)
147                         break;
148                 path = s;
149         }
150         return dt == DT_REG ? ino : 0;
151 }
152
153 /*
154  * Possible superblock locations ordered from most to least likely.
155  */
156 static int sblock_try[] = SBLOCKSEARCH;
157
158 #if defined(UFS2_ONLY)
159 #define DIP(field) dp2.field
160 #elif defined(UFS1_ONLY)
161 #define DIP(field) dp1.field
162 #else
163 #define DIP(field) fs->fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
164 #endif
165
166 static ssize_t
167 fsread(ino_t inode, void *buf, size_t nbyte)
168 {
169 #ifndef UFS2_ONLY
170         static struct ufs1_dinode dp1;
171 #endif
172 #ifndef UFS1_ONLY
173         static struct ufs2_dinode dp2;
174 #endif
175         static ino_t inomap;
176         char *blkbuf;
177         void *indbuf;
178         struct fs *fs;
179         char *s;
180         size_t n, nb, size, off, vboff;
181         ufs_lbn_t lbn;
182         ufs2_daddr_t addr, vbaddr;
183         static ufs2_daddr_t blkmap, indmap;
184         u_int u;
185
186
187         blkbuf = dmadat->blkbuf;
188         indbuf = dmadat->indbuf;
189         fs = (struct fs *)dmadat->sbbuf;
190         if (!dsk_meta) {
191                 inomap = 0;
192                 for (n = 0; sblock_try[n] != -1; n++) {
193                         if (dskread(fs, sblock_try[n] / DEV_BSIZE,
194                             SBLOCKSIZE / DEV_BSIZE))
195                                 return -1;
196                         if ((
197 #if defined(UFS1_ONLY)
198                              fs->fs_magic == FS_UFS1_MAGIC
199 #elif defined(UFS2_ONLY)
200                             (fs->fs_magic == FS_UFS2_MAGIC &&
201                             fs->fs_sblockloc == sblock_try[n])
202 #else
203                              fs->fs_magic == FS_UFS1_MAGIC ||
204                             (fs->fs_magic == FS_UFS2_MAGIC &&
205                             fs->fs_sblockloc == sblock_try[n])
206 #endif
207                             ) &&
208                             fs->fs_bsize <= MAXBSIZE &&
209                             fs->fs_bsize >= sizeof(struct fs))
210                                 break;
211                 }
212                 if (sblock_try[n] == -1) {
213                         printf("Not ufs\n");
214                         return -1;
215                 }
216                 dsk_meta++;
217         }
218         if (!inode)
219                 return 0;
220         if (inomap != inode) {
221                 n = IPERVBLK(fs);
222                 if (dskread(blkbuf, INO_TO_VBA(fs, n, inode), DBPERVBLK))
223                         return -1;
224                 n = INO_TO_VBO(n, inode);
225 #if defined(UFS1_ONLY)
226                 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
227 #elif defined(UFS2_ONLY)
228                 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
229 #else
230                 if (fs->fs_magic == FS_UFS1_MAGIC)
231                         dp1 = ((struct ufs1_dinode *)blkbuf)[n];
232                 else
233                         dp2 = ((struct ufs2_dinode *)blkbuf)[n];
234 #endif
235                 inomap = inode;
236                 fs_off = 0;
237                 blkmap = indmap = 0;
238         }
239         s = buf;
240         size = DIP(di_size);
241         n = size - fs_off;
242         if (nbyte > n)
243                 nbyte = n;
244         nb = nbyte;
245         while (nb) {
246                 lbn = lblkno(fs, fs_off);
247                 off = blkoff(fs, fs_off);
248                 if (lbn < NDADDR) {
249                         addr = DIP(di_db[lbn]);
250                 } else if (lbn < NDADDR + NINDIR(fs)) {
251                         n = INDIRPERVBLK(fs);
252                         addr = DIP(di_ib[0]);
253                         u = (u_int)(lbn - NDADDR) / n * DBPERVBLK;
254                         vbaddr = fsbtodb(fs, addr) + u;
255                         if (indmap != vbaddr) {
256                                 if (dskread(indbuf, vbaddr, DBPERVBLK))
257                                         return -1;
258                                 indmap = vbaddr;
259                         }
260                         n = (lbn - NDADDR) & (n - 1);
261 #if defined(UFS1_ONLY)
262                         addr = ((ufs1_daddr_t *)indbuf)[n];
263 #elif defined(UFS2_ONLY)
264                         addr = ((ufs2_daddr_t *)indbuf)[n];
265 #else
266                         if (fs->fs_magic == FS_UFS1_MAGIC)
267                                 addr = ((ufs1_daddr_t *)indbuf)[n];
268                         else
269                                 addr = ((ufs2_daddr_t *)indbuf)[n];
270 #endif
271                 } else {
272                         return -1;
273                 }
274                 vbaddr = fsbtodb(fs, addr) + (off >> VBLKSHIFT) * DBPERVBLK;
275                 vboff = off & VBLKMASK;
276                 n = sblksize(fs, size, lbn) - (off & ~VBLKMASK);
277                 if (n > VBLKSIZE)
278                         n = VBLKSIZE;
279                 if (blkmap != vbaddr) {
280                         if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
281                                 return -1;
282                         blkmap = vbaddr;
283                 }
284                 n -= vboff;
285                 if (n > nb)
286                         n = nb;
287                 memcpy(s, blkbuf + vboff, n);
288                 s += n;
289                 fs_off += n;
290                 nb -= n;
291         }
292         return nbyte;
293 }