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