3912
|
1 |
/*
|
|
2 |
* GRUB -- GRand Unified Bootloader
|
|
3 |
* Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; either version 2 of the License, or
|
|
8 |
* (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
18 |
*/
|
|
19 |
/*
|
|
20 |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
|
|
21 |
* Use is subject to license terms.
|
|
22 |
*/
|
|
23 |
#pragma ident "%Z%%M% %I% %E% SMI"
|
|
24 |
|
|
25 |
/*
|
|
26 |
* The zfs plug-in routines for GRUB are:
|
|
27 |
*
|
|
28 |
* zfs_mount() - locates a valid uberblock of the root pool and reads
|
|
29 |
* in its MOS at the memory address MOS.
|
|
30 |
*
|
|
31 |
* zfs_open() - locates a plain file object by following the MOS
|
|
32 |
* and places its dnode at the memory address DNODE.
|
|
33 |
*
|
|
34 |
* zfs_read() - read in the data blocks pointed by the DNODE.
|
|
35 |
*
|
|
36 |
* ZFS_SCRATCH is used as a working area.
|
|
37 |
*
|
|
38 |
* (memory addr) MOS DNODE ZFS_SCRATCH
|
|
39 |
* | | |
|
|
40 |
* +-------V---------V----------V---------------+
|
|
41 |
* memory | | dnode | dnode | scratch |
|
|
42 |
* | | 512B | 512B | area |
|
|
43 |
* +--------------------------------------------+
|
|
44 |
*/
|
|
45 |
|
|
46 |
#ifdef FSYS_ZFS
|
|
47 |
|
|
48 |
#include "shared.h"
|
|
49 |
#include "filesys.h"
|
|
50 |
#include "fsys_zfs.h"
|
|
51 |
|
|
52 |
/* cache for a file block of the currently zfs_open()-ed file */
|
|
53 |
static void *file_buf = NULL;
|
|
54 |
static uint64_t file_start = 0;
|
|
55 |
static uint64_t file_end = 0;
|
|
56 |
|
|
57 |
/* cache for a dnode block */
|
|
58 |
static dnode_phys_t *dnode_buf = NULL;
|
|
59 |
static dnode_phys_t *dnode_mdn = NULL;
|
|
60 |
static uint64_t dnode_start = 0;
|
|
61 |
static uint64_t dnode_end = 0;
|
|
62 |
|
|
63 |
static char *stackbase;
|
|
64 |
|
|
65 |
decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
|
|
66 |
{
|
|
67 |
{"noop", 0},
|
|
68 |
{"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
|
|
69 |
{"off", 0},
|
|
70 |
{"lzjb", lzjb_decompress} /* ZIO_COMPRESS_LZJB */
|
|
71 |
};
|
|
72 |
|
|
73 |
/*
|
|
74 |
* Our own version of bcmp().
|
|
75 |
*/
|
|
76 |
static int
|
|
77 |
zfs_bcmp(const void *s1, const void *s2, size_t n)
|
|
78 |
{
|
|
79 |
const uchar_t *ps1 = s1;
|
|
80 |
const uchar_t *ps2 = s2;
|
|
81 |
|
|
82 |
if (s1 != s2 && n != 0) {
|
|
83 |
do {
|
|
84 |
if (*ps1++ != *ps2++)
|
|
85 |
return (1);
|
|
86 |
} while (--n != 0);
|
|
87 |
}
|
|
88 |
|
|
89 |
return (0);
|
|
90 |
}
|
|
91 |
|
|
92 |
/*
|
|
93 |
* Our own version of log2(). Same thing as highbit()-1.
|
|
94 |
*/
|
|
95 |
static int
|
|
96 |
zfs_log2(uint64_t num)
|
|
97 |
{
|
|
98 |
int i = 0;
|
|
99 |
|
|
100 |
while (num > 1) {
|
|
101 |
i++;
|
|
102 |
num = num >> 1;
|
|
103 |
}
|
|
104 |
|
|
105 |
return (i);
|
|
106 |
}
|
|
107 |
|
|
108 |
/* Checksum Functions */
|
|
109 |
static void
|
|
110 |
zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
|
|
111 |
{
|
|
112 |
ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
|
|
113 |
}
|
|
114 |
|
|
115 |
/* Checksum Table and Values */
|
|
116 |
zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
|
|
117 |
NULL, NULL, 0, 0, "inherit",
|
|
118 |
NULL, NULL, 0, 0, "on",
|
|
119 |
zio_checksum_off, zio_checksum_off, 0, 0, "off",
|
|
120 |
zio_checksum_SHA256, zio_checksum_SHA256, 1, 1, "label",
|
|
121 |
zio_checksum_SHA256, zio_checksum_SHA256, 1, 1, "gang_header",
|
|
122 |
fletcher_2_native, fletcher_2_byteswap, 0, 1, "zilog",
|
|
123 |
fletcher_2_native, fletcher_2_byteswap, 0, 0, "fletcher2",
|
|
124 |
fletcher_4_native, fletcher_4_byteswap, 1, 0, "fletcher4",
|
|
125 |
zio_checksum_SHA256, zio_checksum_SHA256, 1, 0, "SHA256",
|
|
126 |
};
|
|
127 |
|
|
128 |
/*
|
|
129 |
* zio_checksum_verify: Provides support for checksum verification.
|
|
130 |
*
|
|
131 |
* Fletcher2, Fletcher4, and SHA256 are supported.
|
|
132 |
*
|
|
133 |
* Return:
|
|
134 |
* -1 = Failure
|
|
135 |
* 0 = Success
|
|
136 |
*/
|
|
137 |
static int
|
|
138 |
zio_checksum_verify(blkptr_t *bp, char *data, int size)
|
|
139 |
{
|
|
140 |
zio_cksum_t zc = bp->blk_cksum;
|
|
141 |
uint32_t checksum = BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER :
|
|
142 |
BP_GET_CHECKSUM(bp);
|
|
143 |
int byteswap = BP_SHOULD_BYTESWAP(bp);
|
|
144 |
zio_block_tail_t *zbt = (zio_block_tail_t *)(data + size) - 1;
|
|
145 |
zio_checksum_info_t *ci = &zio_checksum_table[checksum];
|
|
146 |
zio_cksum_t actual_cksum, expected_cksum;
|
|
147 |
|
|
148 |
/* byteswap is not supported */
|
|
149 |
if (byteswap)
|
|
150 |
return (-1);
|
|
151 |
|
|
152 |
if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
|
|
153 |
return (-1);
|
|
154 |
|
|
155 |
if (ci->ci_zbt) {
|
|
156 |
if (checksum == ZIO_CHECKSUM_GANG_HEADER) {
|
|
157 |
/*
|
|
158 |
* 'gang blocks' is not supported.
|
|
159 |
*/
|
|
160 |
return (-1);
|
|
161 |
}
|
|
162 |
|
|
163 |
if (zbt->zbt_magic == BSWAP_64(ZBT_MAGIC)) {
|
|
164 |
/* byte swapping is not supported */
|
|
165 |
return (-1);
|
|
166 |
} else {
|
|
167 |
expected_cksum = zbt->zbt_cksum;
|
|
168 |
zbt->zbt_cksum = zc;
|
|
169 |
ci->ci_func[0](data, size, &actual_cksum);
|
|
170 |
zbt->zbt_cksum = expected_cksum;
|
|
171 |
}
|
|
172 |
zc = expected_cksum;
|
|
173 |
|
|
174 |
} else {
|
|
175 |
if (BP_IS_GANG(bp))
|
|
176 |
return (-1);
|
|
177 |
ci->ci_func[byteswap](data, size, &actual_cksum);
|
|
178 |
}
|
|
179 |
|
|
180 |
if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
|
|
181 |
(actual_cksum.zc_word[1] - zc.zc_word[1]) |
|
|
182 |
(actual_cksum.zc_word[2] - zc.zc_word[2]) |
|
|
183 |
(actual_cksum.zc_word[3] - zc.zc_word[3]))
|
|
184 |
return (-1);
|
|
185 |
|
|
186 |
return (0);
|
|
187 |
}
|
|
188 |
|
|
189 |
/*
|
|
190 |
* vdev_label_offset takes "offset" (the offset within a vdev_label) and
|
|
191 |
* returns its physical disk offset (starting from the beginning of the vdev).
|
|
192 |
*
|
|
193 |
* Input:
|
|
194 |
* psize : Physical size of this vdev
|
|
195 |
* l : Label Number (0-3)
|
|
196 |
* offset : The offset with a vdev_label in which we want the physical
|
|
197 |
* address
|
|
198 |
* Return:
|
|
199 |
* Success : physical disk offset
|
|
200 |
* Failure : errnum = ERR_BAD_ARGUMENT, return value is meaningless
|
|
201 |
*/
|
|
202 |
uint64_t
|
|
203 |
vdev_label_offset(uint64_t psize, int l, uint64_t offset)
|
|
204 |
{
|
|
205 |
/* XXX Need to add back label support! */
|
|
206 |
if (l >= VDEV_LABELS/2 || offset > sizeof (vdev_label_t)) {
|
|
207 |
errnum = ERR_BAD_ARGUMENT;
|
|
208 |
return (0);
|
|
209 |
}
|
|
210 |
|
|
211 |
return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
|
|
212 |
0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
|
|
213 |
|
|
214 |
}
|
|
215 |
|
|
216 |
/*
|
|
217 |
* vdev_uberblock_compare takes two uberblock structures and returns an integer
|
|
218 |
* indicating the more recent of the two.
|
|
219 |
* Return Value = 1 if ub2 is more recent
|
|
220 |
* Return Value = -1 if ub1 is more recent
|
|
221 |
* The most recent uberblock is determined using its transaction number and
|
|
222 |
* timestamp. The uberblock with the highest transaction number is
|
|
223 |
* considered "newer". If the transaction numbers of the two blocks match, the
|
|
224 |
* timestamps are compared to determine the "newer" of the two.
|
|
225 |
*/
|
|
226 |
static int
|
|
227 |
vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
|
|
228 |
{
|
|
229 |
if (ub1->ub_txg < ub2->ub_txg)
|
|
230 |
return (-1);
|
|
231 |
if (ub1->ub_txg > ub2->ub_txg)
|
|
232 |
return (1);
|
|
233 |
|
|
234 |
if (ub1->ub_timestamp < ub2->ub_timestamp)
|
|
235 |
return (-1);
|
|
236 |
if (ub1->ub_timestamp > ub2->ub_timestamp)
|
|
237 |
return (1);
|
|
238 |
|
|
239 |
return (0);
|
|
240 |
}
|
|
241 |
|
|
242 |
/*
|
|
243 |
* Three pieces of information are needed to verify an uberblock: the magic
|
|
244 |
* number, the version number, and the checksum.
|
|
245 |
*
|
|
246 |
* Currently Implemented: version number, magic number
|
|
247 |
* Need to Implement: checksum
|
|
248 |
*
|
|
249 |
* Return:
|
|
250 |
* 0 - Success
|
|
251 |
* -1 - Failure
|
|
252 |
*/
|
|
253 |
static int
|
|
254 |
uberblock_verify(uberblock_phys_t *ub, int offset)
|
|
255 |
{
|
|
256 |
|
|
257 |
uberblock_t *uber = &ub->ubp_uberblock;
|
|
258 |
blkptr_t bp;
|
|
259 |
|
|
260 |
BP_ZERO(&bp);
|
|
261 |
BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
|
|
262 |
BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
|
|
263 |
ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
|
|
264 |
|
|
265 |
if (zio_checksum_verify(&bp, (char *)ub, UBERBLOCK_SIZE) != 0)
|
|
266 |
return (-1);
|
|
267 |
|
|
268 |
if (uber->ub_magic == UBERBLOCK_MAGIC &&
|
4577
|
269 |
uber->ub_version >= SPA_VERSION_1 &&
|
|
270 |
uber->ub_version <= SPA_VERSION)
|
3912
|
271 |
return (0);
|
|
272 |
|
|
273 |
return (-1);
|
|
274 |
}
|
|
275 |
|
|
276 |
/*
|
|
277 |
* Find the best uberblock.
|
|
278 |
* Return:
|
|
279 |
* Success - Pointer to the best uberblock.
|
|
280 |
* Failure - NULL
|
|
281 |
*/
|
|
282 |
static uberblock_phys_t *
|
|
283 |
find_bestub(uberblock_phys_t *ub_array, int label)
|
|
284 |
{
|
|
285 |
uberblock_phys_t *ubbest = NULL;
|
|
286 |
int i, offset;
|
|
287 |
|
|
288 |
for (i = 0; i < (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT); i++) {
|
|
289 |
offset = vdev_label_offset(0, label, VDEV_UBERBLOCK_OFFSET(i));
|
|
290 |
if (errnum == ERR_BAD_ARGUMENT)
|
|
291 |
return (NULL);
|
|
292 |
if (uberblock_verify(&ub_array[i], offset) == 0) {
|
|
293 |
if (ubbest == NULL) {
|
|
294 |
ubbest = &ub_array[i];
|
4577
|
295 |
} else if (vdev_uberblock_compare(
|
|
296 |
&(ub_array[i].ubp_uberblock),
|
|
297 |
&(ubbest->ubp_uberblock)) > 0) {
|
|
298 |
ubbest = &ub_array[i];
|
3912
|
299 |
}
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
return (ubbest);
|
|
304 |
}
|
|
305 |
|
|
306 |
/*
|
|
307 |
* Read in a block and put its uncompressed data in buf.
|
|
308 |
*
|
|
309 |
* Return:
|
|
310 |
* 0 - success
|
|
311 |
* errnum - failure
|
|
312 |
*/
|
|
313 |
static int
|
|
314 |
zio_read(blkptr_t *bp, void *buf, char *stack)
|
|
315 |
{
|
|
316 |
uint64_t offset, sector;
|
|
317 |
int psize, lsize;
|
|
318 |
int i, comp, cksum;
|
|
319 |
|
|
320 |
psize = BP_GET_PSIZE(bp);
|
|
321 |
lsize = BP_GET_LSIZE(bp);
|
|
322 |
comp = BP_GET_COMPRESS(bp);
|
|
323 |
cksum = BP_GET_CHECKSUM(bp);
|
|
324 |
|
|
325 |
/* pick a good dva from the block pointer */
|
|
326 |
for (i = 0; i < SPA_DVAS_PER_BP; i++) {
|
|
327 |
|
|
328 |
if (bp->blk_dva[i].dva_word[0] == 0 &&
|
|
329 |
bp->blk_dva[i].dva_word[1] == 0)
|
|
330 |
continue;
|
|
331 |
|
|
332 |
/* read in a block */
|
|
333 |
offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
|
|
334 |
sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
|
|
335 |
|
|
336 |
if (comp != ZIO_COMPRESS_OFF) {
|
|
337 |
|
|
338 |
if (devread(sector, 0, psize, stack) == 0)
|
|
339 |
continue;
|
|
340 |
if (zio_checksum_verify(bp, stack, psize) != 0)
|
|
341 |
continue;
|
|
342 |
decomp_table[comp].decomp_func(stack, buf, psize,
|
|
343 |
lsize);
|
|
344 |
} else {
|
|
345 |
if (devread(sector, 0, psize, buf) == 0)
|
|
346 |
continue;
|
|
347 |
if (zio_checksum_verify(bp, buf, psize) != 0)
|
|
348 |
continue;
|
|
349 |
}
|
|
350 |
return (0);
|
|
351 |
}
|
|
352 |
|
|
353 |
return (ERR_FSYS_CORRUPT);
|
|
354 |
}
|
|
355 |
|
|
356 |
/*
|
|
357 |
* Get the block from a block id.
|
|
358 |
* push the block onto the stack.
|
|
359 |
*
|
|
360 |
* Return:
|
|
361 |
* 0 - success
|
|
362 |
* errnum - failure
|
|
363 |
*/
|
|
364 |
static int
|
|
365 |
dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
|
|
366 |
{
|
|
367 |
int idx, level;
|
|
368 |
blkptr_t *bp_array = dn->dn_blkptr;
|
|
369 |
int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
|
|
370 |
blkptr_t *bp, *tmpbuf;
|
|
371 |
|
|
372 |
bp = (blkptr_t *)stack;
|
|
373 |
stack += sizeof (blkptr_t);
|
|
374 |
|
|
375 |
tmpbuf = (blkptr_t *)stack;
|
|
376 |
stack += 1<<dn->dn_indblkshift;
|
|
377 |
|
|
378 |
for (level = dn->dn_nlevels - 1; level >= 0; level--) {
|
|
379 |
idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
|
|
380 |
*bp = bp_array[idx];
|
|
381 |
if (level == 0)
|
|
382 |
tmpbuf = buf;
|
|
383 |
if (errnum = zio_read(bp, tmpbuf, stack))
|
|
384 |
return (errnum);
|
|
385 |
|
|
386 |
bp_array = tmpbuf;
|
|
387 |
}
|
|
388 |
|
|
389 |
return (0);
|
|
390 |
}
|
|
391 |
|
|
392 |
/*
|
|
393 |
* mzap_lookup: Looks up property described by "name" and returns the value
|
|
394 |
* in "value".
|
|
395 |
*
|
|
396 |
* Return:
|
|
397 |
* 0 - success
|
|
398 |
* errnum - failure
|
|
399 |
*/
|
|
400 |
static int
|
|
401 |
mzap_lookup(mzap_phys_t *zapobj, int objsize, char *name,
|
|
402 |
uint64_t *value)
|
|
403 |
{
|
|
404 |
int i, chunks;
|
|
405 |
mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
|
|
406 |
|
|
407 |
chunks = objsize/MZAP_ENT_LEN - 1;
|
|
408 |
for (i = 0; i < chunks; i++) {
|
|
409 |
if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
|
|
410 |
*value = mzap_ent[i].mze_value;
|
|
411 |
return (0);
|
|
412 |
}
|
|
413 |
}
|
|
414 |
|
|
415 |
return (ERR_FSYS_CORRUPT);
|
|
416 |
}
|
|
417 |
|
|
418 |
static uint64_t
|
|
419 |
zap_hash(uint64_t salt, const char *name)
|
|
420 |
{
|
|
421 |
static uint64_t table[256];
|
|
422 |
const uint8_t *cp;
|
|
423 |
uint8_t c;
|
|
424 |
uint64_t crc = salt;
|
|
425 |
|
|
426 |
if (table[128] == 0) {
|
|
427 |
uint64_t *ct;
|
|
428 |
int i, j;
|
|
429 |
for (i = 0; i < 256; i++) {
|
|
430 |
for (ct = table + i, *ct = i, j = 8; j > 0; j--)
|
|
431 |
*ct = (*ct >> 1) ^ (-(*ct & 1) &
|
|
432 |
ZFS_CRC64_POLY);
|
|
433 |
}
|
|
434 |
}
|
|
435 |
|
|
436 |
if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
|
|
437 |
errnum = ERR_FSYS_CORRUPT;
|
|
438 |
return (0);
|
|
439 |
}
|
|
440 |
|
|
441 |
for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
|
|
442 |
crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
|
|
443 |
|
|
444 |
/*
|
|
445 |
* Only use 28 bits, since we need 4 bits in the cookie for the
|
|
446 |
* collision differentiator. We MUST use the high bits, since
|
|
447 |
* those are the onces that we first pay attention to when
|
|
448 |
* chosing the bucket.
|
|
449 |
*/
|
|
450 |
crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
|
|
451 |
|
|
452 |
return (crc);
|
|
453 |
}
|
|
454 |
|
|
455 |
/*
|
|
456 |
* Only to be used on 8-bit arrays.
|
|
457 |
* array_len is actual len in bytes (not encoded le_value_length).
|
|
458 |
* buf is null-terminated.
|
|
459 |
*/
|
|
460 |
static int
|
|
461 |
zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
|
|
462 |
int array_len, const char *buf)
|
|
463 |
{
|
|
464 |
int bseen = 0;
|
|
465 |
|
|
466 |
while (bseen < array_len) {
|
|
467 |
struct zap_leaf_array *la =
|
|
468 |
&ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
|
|
469 |
int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
|
|
470 |
|
|
471 |
if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
|
|
472 |
return (0);
|
|
473 |
|
|
474 |
if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
|
|
475 |
break;
|
|
476 |
chunk = la->la_next;
|
|
477 |
bseen += toread;
|
|
478 |
}
|
|
479 |
return (bseen == array_len);
|
|
480 |
}
|
|
481 |
|
|
482 |
/*
|
|
483 |
* Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
|
|
484 |
* value for the property "name".
|
|
485 |
*
|
|
486 |
* Return:
|
|
487 |
* 0 - success
|
|
488 |
* errnum - failure
|
|
489 |
*/
|
|
490 |
int
|
|
491 |
zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
|
|
492 |
const char *name, uint64_t *value)
|
|
493 |
{
|
|
494 |
uint16_t chunk;
|
|
495 |
struct zap_leaf_entry *le;
|
|
496 |
|
|
497 |
/* Verify if this is a valid leaf block */
|
|
498 |
if (l->l_hdr.lh_block_type != ZBT_LEAF)
|
|
499 |
return (ERR_FSYS_CORRUPT);
|
|
500 |
if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
|
|
501 |
return (ERR_FSYS_CORRUPT);
|
|
502 |
|
|
503 |
for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
|
|
504 |
chunk != CHAIN_END; chunk = le->le_next) {
|
|
505 |
|
|
506 |
if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
|
|
507 |
return (ERR_FSYS_CORRUPT);
|
|
508 |
|
|
509 |
le = ZAP_LEAF_ENTRY(l, blksft, chunk);
|
|
510 |
|
|
511 |
/* Verify the chunk entry */
|
|
512 |
if (le->le_type != ZAP_CHUNK_ENTRY)
|
|
513 |
return (ERR_FSYS_CORRUPT);
|
|
514 |
|
|
515 |
if (le->le_hash != h)
|
|
516 |
continue;
|
|
517 |
|
|
518 |
if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
|
|
519 |
le->le_name_length, name)) {
|
|
520 |
|
|
521 |
struct zap_leaf_array *la;
|
|
522 |
uint8_t *ip;
|
|
523 |
|
|
524 |
if (le->le_int_size != 8 || le->le_value_length != 1)
|
|
525 |
return (ERR_FSYS_CORRUPT);
|
|
526 |
|
|
527 |
/* get the uint64_t property value */
|
|
528 |
la = &ZAP_LEAF_CHUNK(l, blksft,
|
|
529 |
le->le_value_chunk).l_array;
|
|
530 |
ip = la->la_array;
|
|
531 |
|
|
532 |
*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
|
|
533 |
(uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
|
|
534 |
(uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
|
|
535 |
(uint64_t)ip[6] << 8 | (uint64_t)ip[7];
|
|
536 |
|
|
537 |
return (0);
|
|
538 |
}
|
|
539 |
}
|
|
540 |
|
|
541 |
return (ERR_FSYS_CORRUPT);
|
|
542 |
}
|
|
543 |
|
|
544 |
/*
|
|
545 |
* Fat ZAP lookup
|
|
546 |
*
|
|
547 |
* Return:
|
|
548 |
* 0 - success
|
|
549 |
* errnum - failure
|
|
550 |
*/
|
|
551 |
int
|
|
552 |
fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
|
|
553 |
char *name, uint64_t *value, char *stack)
|
|
554 |
{
|
|
555 |
zap_leaf_phys_t *l;
|
|
556 |
uint64_t hash, idx, blkid;
|
|
557 |
int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
|
|
558 |
|
|
559 |
/* Verify if this is a fat zap header block */
|
|
560 |
if (zap->zap_magic != (uint64_t)ZAP_MAGIC)
|
|
561 |
return (ERR_FSYS_CORRUPT);
|
|
562 |
|
|
563 |
hash = zap_hash(zap->zap_salt, name);
|
|
564 |
if (errnum)
|
|
565 |
return (errnum);
|
|
566 |
|
|
567 |
/* get block id from index */
|
|
568 |
if (zap->zap_ptrtbl.zt_numblks != 0) {
|
|
569 |
/* external pointer tables not supported */
|
|
570 |
return (ERR_FSYS_CORRUPT);
|
|
571 |
}
|
|
572 |
idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
|
|
573 |
blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
|
|
574 |
|
|
575 |
/* Get the leaf block */
|
|
576 |
l = (zap_leaf_phys_t *)stack;
|
|
577 |
stack += 1<<blksft;
|
|
578 |
if (errnum = dmu_read(zap_dnode, blkid, l, stack))
|
|
579 |
return (errnum);
|
|
580 |
|
|
581 |
return (zap_leaf_lookup(l, blksft, hash, name, value));
|
|
582 |
}
|
|
583 |
|
|
584 |
/*
|
|
585 |
* Read in the data of a zap object and find the value for a matching
|
|
586 |
* property name.
|
|
587 |
*
|
|
588 |
* Return:
|
|
589 |
* 0 - success
|
|
590 |
* errnum - failure
|
|
591 |
*/
|
|
592 |
static int
|
|
593 |
zap_lookup(dnode_phys_t *zap_dnode, char *name, uint64_t *val, char *stack)
|
|
594 |
{
|
|
595 |
uint64_t block_type;
|
|
596 |
int size;
|
|
597 |
void *zapbuf;
|
|
598 |
|
|
599 |
/* Read in the first block of the zap object data. */
|
|
600 |
zapbuf = stack;
|
|
601 |
size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
|
|
602 |
stack += size;
|
|
603 |
if (errnum = dmu_read(zap_dnode, 0, zapbuf, stack))
|
|
604 |
return (errnum);
|
|
605 |
|
|
606 |
block_type = *((uint64_t *)zapbuf);
|
|
607 |
|
|
608 |
if (block_type == ZBT_MICRO) {
|
|
609 |
return (mzap_lookup(zapbuf, size, name, val));
|
|
610 |
} else if (block_type == ZBT_HEADER) {
|
|
611 |
/* this is a fat zap */
|
|
612 |
return (fzap_lookup(zap_dnode, zapbuf, name,
|
|
613 |
val, stack));
|
|
614 |
}
|
|
615 |
|
|
616 |
return (ERR_FSYS_CORRUPT);
|
|
617 |
}
|
|
618 |
|
|
619 |
/*
|
|
620 |
* Get the dnode of an object number from the metadnode of an object set.
|
|
621 |
*
|
|
622 |
* Input
|
|
623 |
* mdn - metadnode to get the object dnode
|
|
624 |
* objnum - object number for the object dnode
|
|
625 |
* buf - data buffer that holds the returning dnode
|
|
626 |
* stack - scratch area
|
|
627 |
*
|
|
628 |
* Return:
|
|
629 |
* 0 - success
|
|
630 |
* errnum - failure
|
|
631 |
*/
|
|
632 |
static int
|
|
633 |
dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
|
|
634 |
char *stack)
|
|
635 |
{
|
|
636 |
uint64_t blkid, blksz; /* the block id this object dnode is in */
|
|
637 |
int epbs; /* shift of number of dnodes in a block */
|
|
638 |
int idx; /* index within a block */
|
|
639 |
dnode_phys_t *dnbuf;
|
|
640 |
|
|
641 |
blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
|
|
642 |
epbs = zfs_log2(blksz) - DNODE_SHIFT;
|
|
643 |
blkid = objnum >> epbs;
|
|
644 |
idx = objnum & ((1<<epbs)-1);
|
|
645 |
|
|
646 |
if (dnode_buf != NULL && dnode_mdn == mdn &&
|
|
647 |
objnum >= dnode_start && objnum < dnode_end) {
|
|
648 |
grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
|
|
649 |
VERIFY_DN_TYPE(buf, type);
|
|
650 |
return (0);
|
|
651 |
}
|
|
652 |
|
|
653 |
if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
|
|
654 |
dnbuf = dnode_buf;
|
|
655 |
dnode_mdn = mdn;
|
|
656 |
dnode_start = blkid << epbs;
|
|
657 |
dnode_end = (blkid + 1) << epbs;
|
|
658 |
} else {
|
|
659 |
dnbuf = (dnode_phys_t *)stack;
|
|
660 |
stack += blksz;
|
|
661 |
}
|
|
662 |
|
|
663 |
if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
|
|
664 |
return (errnum);
|
|
665 |
|
|
666 |
grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
|
|
667 |
VERIFY_DN_TYPE(buf, type);
|
|
668 |
|
|
669 |
return (0);
|
|
670 |
}
|
|
671 |
|
|
672 |
/*
|
|
673 |
* Check if this is the "menu.lst" file.
|
|
674 |
* str starts with '/'.
|
|
675 |
*/
|
|
676 |
static int
|
|
677 |
is_menu_lst(char *str)
|
|
678 |
{
|
|
679 |
char *tptr;
|
|
680 |
|
|
681 |
if ((tptr = grub_strstr(str, "menu.lst")) &&
|
|
682 |
(tptr[8] == '\0' || tptr[8] == ' ') &&
|
|
683 |
*(tptr-1) == '/')
|
|
684 |
return (1);
|
|
685 |
|
|
686 |
return (0);
|
|
687 |
}
|
|
688 |
|
|
689 |
/*
|
|
690 |
* Get the file dnode for a given file name where mdn is the meta dnode
|
|
691 |
* for this ZFS object set. When found, place the file dnode in dn.
|
|
692 |
* The 'path' argument will be mangled.
|
|
693 |
*
|
|
694 |
* Return:
|
|
695 |
* 0 - success
|
|
696 |
* errnum - failure
|
|
697 |
*/
|
|
698 |
static int
|
|
699 |
dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
|
|
700 |
char *stack)
|
|
701 |
{
|
4577
|
702 |
uint64_t objnum, version;
|
3912
|
703 |
char *cname, ch;
|
|
704 |
|
|
705 |
if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
|
|
706 |
dn, stack))
|
|
707 |
return (errnum);
|
|
708 |
|
4577
|
709 |
if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
|
|
710 |
return (errnum);
|
|
711 |
if (version > ZPL_VERSION)
|
|
712 |
return (-1);
|
|
713 |
|
3912
|
714 |
if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
|
|
715 |
return (errnum);
|
|
716 |
|
|
717 |
if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
|
|
718 |
dn, stack))
|
|
719 |
return (errnum);
|
|
720 |
|
|
721 |
/* skip leading slashes */
|
|
722 |
while (*path == '/')
|
|
723 |
path++;
|
|
724 |
|
|
725 |
while (*path && !isspace(*path)) {
|
|
726 |
|
|
727 |
/* get the next component name */
|
|
728 |
cname = path;
|
|
729 |
while (*path && !isspace(*path) && *path != '/')
|
|
730 |
path++;
|
|
731 |
ch = *path;
|
|
732 |
*path = 0; /* ensure null termination */
|
|
733 |
|
|
734 |
if (errnum = zap_lookup(dn, cname, &objnum, stack))
|
|
735 |
return (errnum);
|
|
736 |
|
4577
|
737 |
objnum = ZFS_DIRENT_OBJ(objnum);
|
3912
|
738 |
if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
|
|
739 |
return (errnum);
|
|
740 |
|
|
741 |
*path = ch;
|
|
742 |
while (*path == '/')
|
|
743 |
path++;
|
|
744 |
}
|
|
745 |
|
|
746 |
/* We found the dnode for this file. Verify if it is a plain file. */
|
|
747 |
VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
|
|
748 |
|
|
749 |
return (0);
|
|
750 |
}
|
|
751 |
|
|
752 |
/*
|
|
753 |
* Get the default 'bootfs' property value from the rootpool.
|
|
754 |
*
|
|
755 |
* Return:
|
|
756 |
* 0 - success
|
|
757 |
* errnum -failure
|
|
758 |
*/
|
|
759 |
static int
|
|
760 |
get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
|
|
761 |
{
|
|
762 |
uint64_t objnum = 0;
|
|
763 |
dnode_phys_t *dn = (dnode_phys_t *)stack;
|
|
764 |
stack += DNODE_SIZE;
|
|
765 |
|
|
766 |
if (dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
|
|
767 |
DMU_OT_OBJECT_DIRECTORY, dn, stack))
|
|
768 |
return (ERR_FILESYSTEM_NOT_FOUND);
|
|
769 |
|
|
770 |
/*
|
|
771 |
* find the object number for 'pool_props', and get the dnode
|
|
772 |
* of the 'pool_props'.
|
|
773 |
*/
|
|
774 |
if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
|
|
775 |
return (ERR_FILESYSTEM_NOT_FOUND);
|
|
776 |
|
|
777 |
if (dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
|
|
778 |
return (ERR_FILESYSTEM_NOT_FOUND);
|
|
779 |
|
|
780 |
if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
|
|
781 |
return (ERR_FILESYSTEM_NOT_FOUND);
|
|
782 |
|
|
783 |
if (!objnum)
|
|
784 |
return (ERR_FILESYSTEM_NOT_FOUND);
|
|
785 |
|
|
786 |
*obj = objnum;
|
|
787 |
return (0);
|
|
788 |
}
|
|
789 |
|
|
790 |
/*
|
|
791 |
* Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
|
|
792 |
* e.g. pool/rootfs, or a given object number (obj), e.g. the object number
|
|
793 |
* of pool/rootfs.
|
|
794 |
*
|
|
795 |
* If no fsname and no obj are given, return the DSL_DIR metadnode.
|
|
796 |
* If fsname is given, return its metadnode and its matching object number.
|
|
797 |
* If only obj is given, return the metadnode for this object number.
|
|
798 |
*
|
|
799 |
* Return:
|
|
800 |
* 0 - success
|
|
801 |
* errnum - failure
|
|
802 |
*/
|
|
803 |
static int
|
|
804 |
get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
|
|
805 |
dnode_phys_t *mdn, char *stack)
|
|
806 |
{
|
|
807 |
uint64_t objnum, headobj;
|
|
808 |
char *cname, ch;
|
|
809 |
blkptr_t *bp;
|
|
810 |
objset_phys_t *osp;
|
|
811 |
|
|
812 |
if (fsname == NULL && obj) {
|
|
813 |
headobj = *obj;
|
|
814 |
goto skip;
|
|
815 |
}
|
|
816 |
|
|
817 |
if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
|
|
818 |
DMU_OT_OBJECT_DIRECTORY, mdn, stack))
|
|
819 |
return (errnum);
|
|
820 |
|
|
821 |
if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
|
|
822 |
stack))
|
|
823 |
return (errnum);
|
|
824 |
|
|
825 |
if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, stack))
|
|
826 |
return (errnum);
|
|
827 |
|
|
828 |
if (fsname == NULL) {
|
|
829 |
headobj =
|
|
830 |
((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
|
|
831 |
goto skip;
|
|
832 |
}
|
|
833 |
|
|
834 |
/* take out the pool name */
|
|
835 |
while (*fsname && !isspace(*fsname) && *fsname != '/')
|
|
836 |
fsname++;
|
|
837 |
|
|
838 |
while (*fsname && !isspace(*fsname)) {
|
|
839 |
uint64_t childobj;
|
|
840 |
|
|
841 |
while (*fsname == '/')
|
|
842 |
fsname++;
|
|
843 |
|
|
844 |
cname = fsname;
|
|
845 |
while (*fsname && !isspace(*fsname) && *fsname != '/')
|
|
846 |
fsname++;
|
|
847 |
ch = *fsname;
|
|
848 |
*fsname = 0;
|
|
849 |
|
|
850 |
childobj =
|
|
851 |
((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
|
|
852 |
if (errnum = dnode_get(mosmdn, childobj,
|
|
853 |
DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
|
|
854 |
return (errnum);
|
|
855 |
|
|
856 |
if (errnum = zap_lookup(mdn, cname, &objnum, stack))
|
|
857 |
return (errnum);
|
|
858 |
|
|
859 |
if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR,
|
|
860 |
mdn, stack))
|
|
861 |
return (errnum);
|
|
862 |
|
|
863 |
*fsname = ch;
|
|
864 |
}
|
|
865 |
headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
|
|
866 |
if (obj)
|
|
867 |
*obj = headobj;
|
|
868 |
|
|
869 |
skip:
|
|
870 |
if (errnum = dnode_get(mosmdn, headobj, DMU_OT_DSL_DATASET, mdn, stack))
|
|
871 |
return (errnum);
|
|
872 |
|
|
873 |
/* TODO: Add snapshot support here - for fsname=snapshot-name */
|
|
874 |
|
|
875 |
bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
|
|
876 |
osp = (objset_phys_t *)stack;
|
|
877 |
stack += sizeof (objset_phys_t);
|
|
878 |
if (errnum = zio_read(bp, osp, stack))
|
|
879 |
return (errnum);
|
|
880 |
|
|
881 |
grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
|
|
882 |
|
|
883 |
return (0);
|
|
884 |
}
|
|
885 |
|
|
886 |
/*
|
|
887 |
* Parse the packed nvlist and search for the string value of a given name.
|
|
888 |
*
|
|
889 |
* An XDR packed nvlist is encoded as (from nvs_xdr_create) :
|
|
890 |
*
|
|
891 |
* encoding method/host endian (4 bytes)
|
|
892 |
* nvl_version (4 bytes)
|
|
893 |
* nvl_nvflag (4 bytes)
|
|
894 |
* encoded nvpairs:
|
|
895 |
* encoded size of the nvpair (4 bytes)
|
|
896 |
* decoded size of the nvpair (4 bytes)
|
|
897 |
* name string size (4 bytes)
|
|
898 |
* name string data (sizeof(NV_ALIGN4(string))
|
|
899 |
* data type (4 bytes)
|
|
900 |
* # of elements in the nvpair (4 bytes)
|
|
901 |
* data
|
|
902 |
* 2 zero's for the last nvpair
|
|
903 |
* (end of the entire list) (8 bytes)
|
|
904 |
*
|
|
905 |
* Return:
|
|
906 |
* 0 - success
|
|
907 |
* 1 - failure
|
|
908 |
*/
|
|
909 |
int
|
|
910 |
nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype)
|
|
911 |
{
|
|
912 |
int name_len, type, nelm, slen, encode_size;
|
|
913 |
char *nvpair, *nvp_name, *strval = val;
|
|
914 |
uint64_t *intval = val;
|
|
915 |
|
|
916 |
/* Verify if the 1st and 2nd byte in the nvlist are valid. */
|
|
917 |
if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
|
|
918 |
return (1);
|
|
919 |
|
|
920 |
/* skip the header, nvl_version, and nvl_nvflag */
|
|
921 |
nvlist = nvlist + 4 * 3;
|
|
922 |
|
|
923 |
/*
|
|
924 |
* Loop thru the nvpair list
|
|
925 |
* The XDR representation of an integer is in big-endian byte order.
|
|
926 |
*/
|
|
927 |
while (encode_size = BSWAP_32(*(uint32_t *)nvlist)) {
|
|
928 |
|
|
929 |
nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
|
|
930 |
|
|
931 |
name_len = BSWAP_32(*(uint32_t *)nvpair);
|
|
932 |
nvpair += 4;
|
|
933 |
|
|
934 |
nvp_name = nvpair;
|
|
935 |
nvpair = nvpair + ((name_len + 3) & ~3); /* align */
|
|
936 |
|
|
937 |
type = BSWAP_32(*(uint32_t *)nvpair);
|
|
938 |
nvpair += 4;
|
|
939 |
|
|
940 |
if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
|
|
941 |
type == valtype) {
|
|
942 |
|
|
943 |
if ((nelm = BSWAP_32(*(uint32_t *)nvpair)) != 1)
|
|
944 |
return (1);
|
|
945 |
nvpair += 4;
|
|
946 |
|
|
947 |
switch (valtype) {
|
|
948 |
case DATA_TYPE_STRING:
|
|
949 |
slen = BSWAP_32(*(uint32_t *)nvpair);
|
|
950 |
nvpair += 4;
|
|
951 |
grub_memmove(strval, nvpair, slen);
|
|
952 |
strval[slen] = '\0';
|
|
953 |
return (0);
|
|
954 |
|
|
955 |
case DATA_TYPE_UINT64:
|
|
956 |
*intval = BSWAP_64(*(uint64_t *)nvpair);
|
|
957 |
return (0);
|
|
958 |
}
|
|
959 |
}
|
|
960 |
|
|
961 |
nvlist += encode_size; /* goto the next nvpair */
|
|
962 |
}
|
|
963 |
|
|
964 |
return (1);
|
|
965 |
}
|
|
966 |
|
|
967 |
/*
|
|
968 |
* Get the pool name of the root pool from the vdev nvpair list of the label.
|
|
969 |
*
|
|
970 |
* Return:
|
|
971 |
* 0 - success
|
|
972 |
* errnum - failure
|
|
973 |
*/
|
|
974 |
int
|
|
975 |
get_pool_name_value(int label, char *name, void *value, int valtype,
|
|
976 |
char *stack)
|
|
977 |
{
|
|
978 |
vdev_phys_t *vdev;
|
|
979 |
uint64_t sector;
|
|
980 |
|
|
981 |
sector = (label * sizeof (vdev_label_t) + VDEV_SKIP_SIZE +
|
|
982 |
VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT;
|
|
983 |
|
|
984 |
/* Read in the vdev name-value pair list (112K). */
|
|
985 |
if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
|
|
986 |
return (ERR_READ);
|
|
987 |
|
|
988 |
vdev = (vdev_phys_t *)stack;
|
|
989 |
|
|
990 |
if (nvlist_lookup_value(vdev->vp_nvlist, name, value, valtype))
|
|
991 |
return (ERR_FSYS_CORRUPT);
|
|
992 |
else
|
|
993 |
return (0);
|
|
994 |
}
|
|
995 |
|
|
996 |
/*
|
|
997 |
* zfs_mount() locates a valid uberblock of the root pool and read in its MOS
|
|
998 |
* to the memory address MOS.
|
|
999 |
*
|
|
1000 |
* Return:
|
|
1001 |
* 1 - success
|
|
1002 |
* 0 - failure
|
|
1003 |
*/
|
|
1004 |
int
|
|
1005 |
zfs_mount(void)
|
|
1006 |
{
|
|
1007 |
char *stack;
|
|
1008 |
int label = 0;
|
|
1009 |
uberblock_phys_t *ub_array, *ubbest = NULL;
|
|
1010 |
objset_phys_t *osp;
|
|
1011 |
|
|
1012 |
/* if zfs is already mounted, don't do it again */
|
|
1013 |
if (is_zfs_mount == 1)
|
|
1014 |
return (1);
|
|
1015 |
|
|
1016 |
stackbase = ZFS_SCRATCH;
|
|
1017 |
stack = stackbase;
|
|
1018 |
ub_array = (uberblock_phys_t *)stack;
|
|
1019 |
stack += VDEV_UBERBLOCK_RING;
|
|
1020 |
|
|
1021 |
osp = (objset_phys_t *)stack;
|
|
1022 |
stack += sizeof (objset_phys_t);
|
|
1023 |
|
|
1024 |
/* XXX add back labels support? */
|
|
1025 |
for (label = 0; ubbest == NULL && label < (VDEV_LABELS/2); label++) {
|
|
1026 |
uint64_t sector = (label * sizeof (vdev_label_t) +
|
|
1027 |
VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE +
|
|
1028 |
VDEV_PHYS_SIZE) >> SPA_MINBLOCKSHIFT;
|
|
1029 |
|
|
1030 |
/* Read in the uberblock ring (128K). */
|
|
1031 |
if (devread(sector, 0, VDEV_UBERBLOCK_RING,
|
|
1032 |
(char *)ub_array) == 0)
|
|
1033 |
continue;
|
|
1034 |
|
|
1035 |
if ((ubbest = find_bestub(ub_array, label)) != NULL &&
|
|
1036 |
zio_read(&ubbest->ubp_uberblock.ub_rootbp, osp, stack)
|
|
1037 |
== 0) {
|
|
1038 |
uint64_t pool_state;
|
|
1039 |
|
|
1040 |
VERIFY_OS_TYPE(osp, DMU_OST_META);
|
|
1041 |
|
|
1042 |
/* Got the MOS. Save it at the memory addr MOS. */
|
|
1043 |
grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
|
|
1044 |
|
|
1045 |
if (get_pool_name_value(label, ZPOOL_CONFIG_POOL_STATE,
|
|
1046 |
&pool_state, DATA_TYPE_UINT64, stack))
|
|
1047 |
return (0);
|
|
1048 |
|
|
1049 |
if (pool_state == POOL_STATE_DESTROYED)
|
|
1050 |
return (0);
|
|
1051 |
|
|
1052 |
if (get_pool_name_value(label, ZPOOL_CONFIG_POOL_NAME,
|
|
1053 |
current_rootpool, DATA_TYPE_STRING, stack))
|
|
1054 |
return (0);
|
|
1055 |
|
|
1056 |
is_zfs_mount = 1;
|
|
1057 |
return (1);
|
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
return (0);
|
|
1062 |
}
|
|
1063 |
|
|
1064 |
/*
|
|
1065 |
* zfs_open() locates a file in the rootpool by following the
|
|
1066 |
* MOS and places the dnode of the file in the memory address DNODE.
|
|
1067 |
*
|
|
1068 |
* Return:
|
|
1069 |
* 1 - success
|
|
1070 |
* 0 - failure
|
|
1071 |
*/
|
|
1072 |
int
|
|
1073 |
zfs_open(char *filename)
|
|
1074 |
{
|
|
1075 |
char *stack;
|
|
1076 |
dnode_phys_t *mdn;
|
|
1077 |
|
|
1078 |
file_buf = NULL;
|
|
1079 |
stackbase = ZFS_SCRATCH;
|
|
1080 |
stack = stackbase;
|
|
1081 |
|
|
1082 |
mdn = (dnode_phys_t *)stack;
|
|
1083 |
stack += sizeof (dnode_phys_t);
|
|
1084 |
|
|
1085 |
dnode_mdn = NULL;
|
|
1086 |
dnode_buf = (dnode_phys_t *)stack;
|
|
1087 |
stack += 1<<DNODE_BLOCK_SHIFT;
|
|
1088 |
|
|
1089 |
/*
|
|
1090 |
* menu.lst is placed at the root pool filesystem level,
|
|
1091 |
* do not goto 'current_bootfs'.
|
|
1092 |
*/
|
|
1093 |
if (is_menu_lst(filename)) {
|
|
1094 |
if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
|
|
1095 |
return (0);
|
|
1096 |
|
|
1097 |
current_bootfs_obj = 0;
|
|
1098 |
} else {
|
|
1099 |
if (current_bootfs[0] == '\0') {
|
|
1100 |
/* Get the default root filesystem object number */
|
|
1101 |
if (get_default_bootfsobj(MOS,
|
|
1102 |
¤t_bootfs_obj, stack)) {
|
|
1103 |
errnum = ERR_FILESYSTEM_NOT_FOUND;
|
|
1104 |
return (0);
|
|
1105 |
}
|
|
1106 |
|
|
1107 |
if (errnum = get_objset_mdn(MOS, NULL,
|
|
1108 |
¤t_bootfs_obj, mdn, stack))
|
|
1109 |
return (0);
|
|
1110 |
} else {
|
|
1111 |
if (errnum = get_objset_mdn(MOS,
|
|
1112 |
current_bootfs, ¤t_bootfs_obj, mdn, stack))
|
|
1113 |
return (0);
|
|
1114 |
}
|
|
1115 |
}
|
|
1116 |
|
|
1117 |
if (dnode_get_path(mdn, filename, DNODE, stack)) {
|
|
1118 |
errnum = ERR_FILE_NOT_FOUND;
|
|
1119 |
return (0);
|
|
1120 |
}
|
|
1121 |
|
|
1122 |
/* get the file size and set the file position to 0 */
|
|
1123 |
filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
|
|
1124 |
filepos = 0;
|
|
1125 |
|
|
1126 |
dnode_buf = NULL;
|
|
1127 |
return (1);
|
|
1128 |
}
|
|
1129 |
|
|
1130 |
/*
|
|
1131 |
* zfs_read reads in the data blocks pointed by the DNODE.
|
|
1132 |
*
|
|
1133 |
* Return:
|
|
1134 |
* len - the length successfully read in to the buffer
|
|
1135 |
* 0 - failure
|
|
1136 |
*/
|
|
1137 |
int
|
|
1138 |
zfs_read(char *buf, int len)
|
|
1139 |
{
|
|
1140 |
char *stack;
|
|
1141 |
char *tmpbuf;
|
|
1142 |
int blksz, length, movesize;
|
|
1143 |
|
|
1144 |
if (file_buf == NULL) {
|
|
1145 |
file_buf = stackbase;
|
|
1146 |
stackbase += SPA_MAXBLOCKSIZE;
|
|
1147 |
file_start = file_end = 0;
|
|
1148 |
}
|
|
1149 |
stack = stackbase;
|
|
1150 |
|
|
1151 |
/*
|
|
1152 |
* If offset is in memory, move it into the buffer provided and return.
|
|
1153 |
*/
|
|
1154 |
if (filepos >= file_start && filepos+len <= file_end) {
|
|
1155 |
grub_memmove(buf, file_buf + filepos - file_start, len);
|
|
1156 |
filepos += len;
|
|
1157 |
return (len);
|
|
1158 |
}
|
|
1159 |
|
|
1160 |
blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
|
|
1161 |
|
|
1162 |
/*
|
|
1163 |
* Entire Dnode is too big to fit into the space available. We
|
|
1164 |
* will need to read it in chunks. This could be optimized to
|
|
1165 |
* read in as large a chunk as there is space available, but for
|
|
1166 |
* now, this only reads in one data block at a time.
|
|
1167 |
*/
|
|
1168 |
length = len;
|
|
1169 |
while (length) {
|
|
1170 |
/*
|
|
1171 |
* Find requested blkid and the offset within that block.
|
|
1172 |
*/
|
|
1173 |
uint64_t blkid = filepos / blksz;
|
|
1174 |
|
|
1175 |
if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
|
|
1176 |
return (0);
|
|
1177 |
|
|
1178 |
file_start = blkid * blksz;
|
|
1179 |
file_end = file_start + blksz;
|
|
1180 |
|
|
1181 |
movesize = MIN(length, file_end - filepos);
|
|
1182 |
|
|
1183 |
grub_memmove(buf, file_buf + filepos - file_start,
|
|
1184 |
movesize);
|
|
1185 |
buf += movesize;
|
|
1186 |
length -= movesize;
|
|
1187 |
filepos += movesize;
|
|
1188 |
}
|
|
1189 |
|
|
1190 |
return (len);
|
|
1191 |
}
|
|
1192 |
|
|
1193 |
/*
|
|
1194 |
* No-Op
|
|
1195 |
*/
|
|
1196 |
int
|
|
1197 |
zfs_embed(int *start_sector, int needed_sectors)
|
|
1198 |
{
|
|
1199 |
return (1);
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
#endif /* FSYS_ZFS */
|