xref: /freebsd/stand/libsa/ufsread.c (revision 3e15b01d)
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 <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ffs/fs.h>
49 
50 #ifdef UFS_SMALL_CGBASE
51 /* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
52    (see sys/ufs/ffs/fs.h rev 1.39) so that small boot loaders (e.g. boot2) can
53    support both UFS1 and UFS2. */
54 #undef cgbase
55 #define cgbase(fs, c)   ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
56 #endif
57 
58 typedef	uint32_t	ufs_ino_t;
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 ufs_ino_t lookup(const char *);
88 static ssize_t fsread(ufs_ino_t, void *, size_t);
89 
90 static uint8_t ls, dsk_meta;
91 static uint32_t fs_off;
92 
93 static __inline uint8_t
fsfind(const char * name,ufs_ino_t * ino)94 fsfind(const char *name, ufs_ino_t * ino)
95 {
96 	static char buf[DEV_BSIZE];
97 	static 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 			memcpy(&d, s, sizeof(struct direct));
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 ufs_ino_t
lookup(const char * path)119 lookup(const char *path)
120 {
121 	static char name[UFS_MAXNAMLEN + 1];
122 	const char *s;
123 	ufs_ino_t ino;
124 	ssize_t n;
125 	uint8_t dt;
126 
127 	ino = UFS_ROOTINO;
128 	dt = DT_DIR;
129 	for (;;) {
130 		if (*path == '/')
131 			path++;
132 		if (!*path)
133 			break;
134 		for (s = path; *s && *s != '/'; s++);
135 		if ((n = s - path) > UFS_MAXNAMLEN)
136 			return 0;
137 		ls = *path == '?' && n == 1 && !*s;
138 		memcpy(name, path, n);
139 		name[n] = 0;
140 		if (dt != DT_DIR) {
141 			printf("%s: not a directory.\n", name);
142 			return (0);
143 		}
144 		if ((dt = fsfind(name, &ino)) <= 0)
145 			break;
146 		path = s;
147 	}
148 	return dt == DT_REG ? ino : 0;
149 }
150 
151 /*
152  * Possible superblock locations ordered from most to least likely.
153  */
154 static int sblock_try[] = SBLOCKSEARCH;
155 
156 #if defined(UFS2_ONLY)
157 #define DIP(field) dp2.field
158 #elif defined(UFS1_ONLY)
159 #define DIP(field) dp1.field
160 #else
161 #define DIP(field) fs.fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
162 #endif
163 
164 static ssize_t
fsread_size(ufs_ino_t inode,void * buf,size_t nbyte,size_t * fsizep)165 fsread_size(ufs_ino_t inode, void *buf, size_t nbyte, size_t *fsizep)
166 {
167 #ifndef UFS2_ONLY
168 	static struct ufs1_dinode dp1;
169 	ufs1_daddr_t addr1;
170 #endif
171 #ifndef UFS1_ONLY
172 	static struct ufs2_dinode dp2;
173 #endif
174 	static struct fs fs;
175 	static ufs_ino_t inomap;
176 	char *blkbuf;
177 	void *indbuf;
178 	char *s;
179 	size_t n, nb, size, off, vboff;
180 	ufs_lbn_t lbn;
181 	ufs2_daddr_t addr2, vbaddr;
182 	static ufs2_daddr_t blkmap, indmap;
183 	u_int u;
184 
185 	/* Basic parameter validation. */
186 	if ((buf == NULL && nbyte != 0) || dmadat == NULL)
187 		return (-1);
188 
189 	blkbuf = dmadat->blkbuf;
190 	indbuf = dmadat->indbuf;
191 
192 	/*
193 	 * Force probe if inode is zero to ensure we have a valid fs, otherwise
194 	 * when probing multiple paritions, reads from subsequent parititions
195 	 * will incorrectly succeed.
196 	 */
197 	if (!dsk_meta || inode == 0) {
198 		inomap = 0;
199 		dsk_meta = 0;
200 		for (n = 0; sblock_try[n] != -1; n++) {
201 			if (dskread(dmadat->sbbuf, sblock_try[n] / DEV_BSIZE,
202 			    SBLOCKSIZE / DEV_BSIZE))
203 				return -1;
204 			memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
205 			if ((
206 #if defined(UFS1_ONLY)
207 			    fs.fs_magic == FS_UFS1_MAGIC
208 #elif defined(UFS2_ONLY)
209 			    (fs.fs_magic == FS_UFS2_MAGIC &&
210 			    fs.fs_sblockloc == sblock_try[n])
211 #else
212 			    fs.fs_magic == FS_UFS1_MAGIC ||
213 			    (fs.fs_magic == FS_UFS2_MAGIC &&
214 			    fs.fs_sblockloc == sblock_try[n])
215 #endif
216 			    ) &&
217 			    fs.fs_bsize <= MAXBSIZE &&
218 			    fs.fs_bsize >= (int32_t)sizeof(struct fs))
219 				break;
220 		}
221 		if (sblock_try[n] == -1) {
222 			return -1;
223 		}
224 		dsk_meta++;
225 	} else
226 		memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
227 	if (!inode)
228 		return 0;
229 	if (inomap != inode) {
230 		n = IPERVBLK(&fs);
231 		if (dskread(blkbuf, INO_TO_VBA(&fs, n, inode), DBPERVBLK))
232 			return -1;
233 		n = INO_TO_VBO(n, inode);
234 #if defined(UFS1_ONLY)
235 		memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
236 		    sizeof(dp1));
237 #elif defined(UFS2_ONLY)
238 		memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
239 		    sizeof(dp2));
240 #else
241 		if (fs.fs_magic == FS_UFS1_MAGIC)
242 			memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
243 			    sizeof(dp1));
244 		else
245 			memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
246 			    sizeof(dp2));
247 #endif
248 		inomap = inode;
249 		fs_off = 0;
250 		blkmap = indmap = 0;
251 	}
252 	s = buf;
253 	size = DIP(di_size);
254 	n = size - fs_off;
255 	if (nbyte > n)
256 		nbyte = n;
257 	nb = nbyte;
258 	while (nb) {
259 		lbn = lblkno(&fs, fs_off);
260 		off = blkoff(&fs, fs_off);
261 		if (lbn < UFS_NDADDR) {
262 			addr2 = DIP(di_db[lbn]);
263 		} else if (lbn < UFS_NDADDR + NINDIR(&fs)) {
264 			n = INDIRPERVBLK(&fs);
265 			addr2 = DIP(di_ib[0]);
266 			u = (u_int)(lbn - UFS_NDADDR) / n * DBPERVBLK;
267 			vbaddr = fsbtodb(&fs, addr2) + u;
268 			if (indmap != vbaddr) {
269 				if (dskread(indbuf, vbaddr, DBPERVBLK))
270 					return -1;
271 				indmap = vbaddr;
272 			}
273 			n = (lbn - UFS_NDADDR) & (n - 1);
274 #if defined(UFS1_ONLY)
275 			memcpy(&addr1, (ufs1_daddr_t *)indbuf + n,
276 			    sizeof(ufs1_daddr_t));
277 			addr2 = addr1;
278 #elif defined(UFS2_ONLY)
279 			memcpy(&addr2, (ufs2_daddr_t *)indbuf + n,
280 			    sizeof(ufs2_daddr_t));
281 #else
282 			if (fs.fs_magic == FS_UFS1_MAGIC) {
283 				memcpy(&addr1, (ufs1_daddr_t *)indbuf + n,
284 				    sizeof(ufs1_daddr_t));
285 				addr2 = addr1;
286 			} else
287 				memcpy(&addr2, (ufs2_daddr_t *)indbuf + n,
288 				    sizeof(ufs2_daddr_t));
289 #endif
290 		} else
291 			return -1;
292 		vbaddr = fsbtodb(&fs, addr2) + (off >> VBLKSHIFT) * DBPERVBLK;
293 		vboff = off & VBLKMASK;
294 		n = sblksize(&fs, (off_t)size, lbn) - (off & ~VBLKMASK);
295 		if (n > VBLKSIZE)
296 			n = VBLKSIZE;
297 		if (blkmap != vbaddr) {
298 			if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
299 				return -1;
300 			blkmap = vbaddr;
301 		}
302 		n -= vboff;
303 		if (n > nb)
304 			n = nb;
305 		memcpy(s, blkbuf + vboff, n);
306 		s += n;
307 		fs_off += n;
308 		nb -= n;
309 	}
310 
311 	if (fsizep != NULL)
312 		*fsizep = size;
313 
314 	return nbyte;
315 }
316 
317 static ssize_t
fsread(ufs_ino_t inode,void * buf,size_t nbyte)318 fsread(ufs_ino_t inode, void *buf, size_t nbyte)
319 {
320 
321 	return fsread_size(inode, buf, nbyte, NULL);
322 }
323 
324