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