blob: 410a61aa611e59a24237c4ae9f154567126283bf [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002/*
3 *
4 * ZFS filesystem ported to u-boot by
5 * Jorgen Lundman <lundman at lundman.net>
6 *
7 * GRUB -- GRand Unified Bootloader
8 * Copyright (C) 1999,2000,2001,2002,2003,2004
9 * Free Software Foundation, Inc.
10 * Copyright 2004 Sun Microsystems, Inc.
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000011 */
12
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000014#include <malloc.h>
15#include <linux/stat.h>
16#include <linux/time.h>
17#include <linux/ctype.h>
18#include <asm/byteorder.h>
WHR1466e062024-05-01 00:40:38 +080019#include <u-boot/zlib.h>
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000020#include "zfs_common.h"
Alejandro Mery624c7212012-10-31 08:21:33 +000021#include "div64.h"
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000022
Simon Glass4101f682016-02-29 15:25:34 -070023struct blk_desc *zfs_dev_desc;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000024
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 */
37
38#include <zfs/zfs.h>
39#include <zfs/zio.h>
40#include <zfs/dnode.h>
41#include <zfs/uberblock_impl.h>
42#include <zfs/vdev_impl.h>
43#include <zfs/zio_checksum.h>
44#include <zfs/zap_impl.h>
45#include <zfs/zap_leaf.h>
46#include <zfs/zfs_znode.h>
47#include <zfs/dmu.h>
48#include <zfs/dmu_objset.h>
49#include <zfs/sa_impl.h>
50#include <zfs/dsl_dir.h>
51#include <zfs/dsl_dataset.h>
52
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000053#define ZPOOL_PROP_BOOTFS "bootfs"
54
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000055/*
56 * For nvlist manipulation. (from nvpair.h)
57 */
58#define NV_ENCODE_NATIVE 0
59#define NV_ENCODE_XDR 1
60#define NV_BIG_ENDIAN 0
61#define NV_LITTLE_ENDIAN 1
62#define DATA_TYPE_UINT64 8
63#define DATA_TYPE_STRING 9
64#define DATA_TYPE_NVLIST 19
65#define DATA_TYPE_NVLIST_ARRAY 20
66
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +000067/*
68 * Macros to get fields in a bp or DVA.
69 */
70#define P2PHASE(x, align) ((x) & ((align) - 1))
71#define DVA_OFFSET_TO_PHYS_SECTOR(offset) \
72 ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT)
73
74/*
75 * return x rounded down to an align boundary
76 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
77 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
78 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
79 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
80 */
81#define P2ALIGN(x, align) ((x) & -(align))
82
83/*
84 * FAT ZAP data structures
85 */
86#define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */
87#define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
88#define CHAIN_END 0xffff /* end of the chunk chain */
89
90/*
91 * The amount of space within the chunk available for the array is:
92 * chunk size - space for type (1) - space for next pointer (2)
93 */
94#define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
95
96#define ZAP_LEAF_HASH_SHIFT(bs) (bs - 5)
97#define ZAP_LEAF_HASH_NUMENTRIES(bs) (1 << ZAP_LEAF_HASH_SHIFT(bs))
98#define LEAF_HASH(bs, h) \
99 ((ZAP_LEAF_HASH_NUMENTRIES(bs)-1) & \
100 ((h) >> (64 - ZAP_LEAF_HASH_SHIFT(bs)-l->l_hdr.lh_prefix_len)))
101
102/*
103 * The amount of space available for chunks is:
104 * block size shift - hash entry size (2) * number of hash
105 * entries - header space (2*chunksize)
106 */
107#define ZAP_LEAF_NUMCHUNKS(bs) \
108 (((1<<bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(bs)) / \
109 ZAP_LEAF_CHUNKSIZE - 2)
110
111/*
112 * The chunks start immediately after the hash table. The end of the
113 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
114 * chunk_t.
115 */
116#define ZAP_LEAF_CHUNK(l, bs, idx) \
117 ((zap_leaf_chunk_t *)(l->l_hash + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx]
118#define ZAP_LEAF_ENTRY(l, bs, idx) (&ZAP_LEAF_CHUNK(l, bs, idx).l_entry)
119
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000120/*
121 * Decompression Entry - lzjb
122 */
123#ifndef NBBY
124#define NBBY 8
125#endif
126
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000127typedef int zfs_decomp_func_t(void *s_start, void *d_start,
128 uint32_t s_len, uint32_t d_len);
129typedef struct decomp_entry {
130 char *name;
131 zfs_decomp_func_t *decomp_func;
132} decomp_entry_t;
133
134typedef struct dnode_end {
135 dnode_phys_t dn;
136 zfs_endian_t endian;
137} dnode_end_t;
138
139struct zfs_data {
140 /* cache for a file block of the currently zfs_open()-ed file */
141 char *file_buf;
142 uint64_t file_start;
143 uint64_t file_end;
144
145 /* XXX: ashift is per vdev, not per pool. We currently only ever touch
146 * a single vdev, but when/if raid-z or stripes are supported, this
147 * may need revision.
148 */
149 uint64_t vdev_ashift;
150 uint64_t label_txg;
151 uint64_t pool_guid;
152
153 /* cache for a dnode block */
154 dnode_phys_t *dnode_buf;
155 dnode_phys_t *dnode_mdn;
156 uint64_t dnode_start;
157 uint64_t dnode_end;
158 zfs_endian_t dnode_endian;
159
160 uberblock_t current_uberblock;
161
162 dnode_end_t mos;
163 dnode_end_t mdn;
164 dnode_end_t dnode;
165
166 uint64_t vdev_phys_sector;
167
168 int (*userhook)(const char *, const struct zfs_dirhook_info *);
169 struct zfs_dirhook_info *dirinfo;
170
171};
172
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000173static int
174zlib_decompress(void *s, void *d,
175 uint32_t slen, uint32_t dlen)
176{
WHR1466e062024-05-01 00:40:38 +0800177 uLongf z_dest_len = dlen;
178 if (uncompress(d, &z_dest_len, s, slen) != Z_OK)
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000179 return ZFS_ERR_BAD_FS;
180 return ZFS_ERR_NONE;
181}
182
183static decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = {
184 {"inherit", NULL}, /* ZIO_COMPRESS_INHERIT */
185 {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
186 {"off", NULL}, /* ZIO_COMPRESS_OFF */
187 {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
188 {"empty", NULL}, /* ZIO_COMPRESS_EMPTY */
189 {"gzip-1", zlib_decompress}, /* ZIO_COMPRESS_GZIP1 */
190 {"gzip-2", zlib_decompress}, /* ZIO_COMPRESS_GZIP2 */
191 {"gzip-3", zlib_decompress}, /* ZIO_COMPRESS_GZIP3 */
192 {"gzip-4", zlib_decompress}, /* ZIO_COMPRESS_GZIP4 */
193 {"gzip-5", zlib_decompress}, /* ZIO_COMPRESS_GZIP5 */
194 {"gzip-6", zlib_decompress}, /* ZIO_COMPRESS_GZIP6 */
195 {"gzip-7", zlib_decompress}, /* ZIO_COMPRESS_GZIP7 */
196 {"gzip-8", zlib_decompress}, /* ZIO_COMPRESS_GZIP8 */
197 {"gzip-9", zlib_decompress}, /* ZIO_COMPRESS_GZIP9 */
198};
199
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000200static int zio_read_data(blkptr_t *bp, zfs_endian_t endian,
201 void *buf, struct zfs_data *data);
202
203static int
204zio_read(blkptr_t *bp, zfs_endian_t endian, void **buf,
205 size_t *size, struct zfs_data *data);
206
207/*
208 * Our own version of log2(). Same thing as highbit()-1.
209 */
210static int
211zfs_log2(uint64_t num)
212{
213 int i = 0;
214
215 while (num > 1) {
216 i++;
217 num = num >> 1;
218 }
219
220 return i;
221}
222
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000223/* Checksum Functions */
224static void
225zio_checksum_off(const void *buf __attribute__ ((unused)),
226 uint64_t size __attribute__ ((unused)),
227 zfs_endian_t endian __attribute__ ((unused)),
228 zio_cksum_t *zcp)
229{
230 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
231}
232
233/* Checksum Table and Values */
234static zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
235 {NULL, 0, 0, "inherit"},
236 {NULL, 0, 0, "on"},
237 {zio_checksum_off, 0, 0, "off"},
238 {zio_checksum_SHA256, 1, 1, "label"},
239 {zio_checksum_SHA256, 1, 1, "gang_header"},
240 {NULL, 0, 0, "zilog"},
241 {fletcher_2_endian, 0, 0, "fletcher2"},
242 {fletcher_4_endian, 1, 0, "fletcher4"},
243 {zio_checksum_SHA256, 1, 0, "SHA256"},
244 {NULL, 0, 0, "zilog2"},
245};
246
247/*
248 * zio_checksum_verify: Provides support for checksum verification.
249 *
250 * Fletcher2, Fletcher4, and SHA256 are supported.
251 *
252 */
253static int
254zio_checksum_verify(zio_cksum_t zc, uint32_t checksum,
255 zfs_endian_t endian, char *buf, int size)
256{
257 zio_eck_t *zec = (zio_eck_t *) (buf + size) - 1;
258 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
259 zio_cksum_t actual_cksum, expected_cksum;
260
261 if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL) {
262 printf("zfs unknown checksum function %d\n", checksum);
263 return ZFS_ERR_NOT_IMPLEMENTED_YET;
264 }
265
266 if (ci->ci_eck) {
267 expected_cksum = zec->zec_cksum;
268 zec->zec_cksum = zc;
269 ci->ci_func(buf, size, endian, &actual_cksum);
270 zec->zec_cksum = expected_cksum;
271 zc = expected_cksum;
272 } else {
273 ci->ci_func(buf, size, endian, &actual_cksum);
274 }
275
276 if ((actual_cksum.zc_word[0] != zc.zc_word[0])
277 || (actual_cksum.zc_word[1] != zc.zc_word[1])
278 || (actual_cksum.zc_word[2] != zc.zc_word[2])
279 || (actual_cksum.zc_word[3] != zc.zc_word[3])) {
280 return ZFS_ERR_BAD_FS;
281 }
282
283 return ZFS_ERR_NONE;
284}
285
286/*
287 * vdev_uberblock_compare takes two uberblock structures and returns an integer
288 * indicating the more recent of the two.
289 * Return Value = 1 if ub2 is more recent
290 * Return Value = -1 if ub1 is more recent
291 * The most recent uberblock is determined using its transaction number and
292 * timestamp. The uberblock with the highest transaction number is
293 * considered "newer". If the transaction numbers of the two blocks match, the
294 * timestamps are compared to determine the "newer" of the two.
295 */
296static int
297vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
298{
299 zfs_endian_t ub1_endian, ub2_endian;
300 if (zfs_to_cpu64(ub1->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC)
301 ub1_endian = LITTLE_ENDIAN;
302 else
303 ub1_endian = BIG_ENDIAN;
304 if (zfs_to_cpu64(ub2->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC)
305 ub2_endian = LITTLE_ENDIAN;
306 else
307 ub2_endian = BIG_ENDIAN;
308
309 if (zfs_to_cpu64(ub1->ub_txg, ub1_endian)
310 < zfs_to_cpu64(ub2->ub_txg, ub2_endian))
311 return -1;
312 if (zfs_to_cpu64(ub1->ub_txg, ub1_endian)
313 > zfs_to_cpu64(ub2->ub_txg, ub2_endian))
314 return 1;
315
316 if (zfs_to_cpu64(ub1->ub_timestamp, ub1_endian)
317 < zfs_to_cpu64(ub2->ub_timestamp, ub2_endian))
318 return -1;
319 if (zfs_to_cpu64(ub1->ub_timestamp, ub1_endian)
320 > zfs_to_cpu64(ub2->ub_timestamp, ub2_endian))
321 return 1;
322
323 return 0;
324}
325
WHRcd85e0d2024-05-01 00:28:32 +0800326static inline int
327is_supported_spa_version(uint64_t version) {
328 return version == FEATURES_SUPPORTED_SPA_VERSION ||
329 (version > 0 && version <= SPA_VERSION);
330}
331
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000332/*
333 * Three pieces of information are needed to verify an uberblock: the magic
334 * number, the version number, and the checksum.
335 *
336 * Currently Implemented: version number, magic number, label txg
337 * Need to Implement: checksum
338 *
339 */
340static int
341uberblock_verify(uberblock_t *uber, int offset, struct zfs_data *data)
342{
343 int err;
344 zfs_endian_t endian = UNKNOWN_ENDIAN;
345 zio_cksum_t zc;
346
347 if (uber->ub_txg < data->label_txg) {
348 debug("ignoring partially written label: uber_txg < label_txg %llu %llu\n",
349 uber->ub_txg, data->label_txg);
350 return ZFS_ERR_BAD_FS;
351 }
352
WHRcd85e0d2024-05-01 00:28:32 +0800353 if (zfs_to_cpu64(uber->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC &&
354 is_supported_spa_version(zfs_to_cpu64(uber->ub_version, LITTLE_ENDIAN)))
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000355 endian = LITTLE_ENDIAN;
356
WHRcd85e0d2024-05-01 00:28:32 +0800357 if (zfs_to_cpu64(uber->ub_magic, BIG_ENDIAN) == UBERBLOCK_MAGIC &&
358 is_supported_spa_version(zfs_to_cpu64(uber->ub_version, BIG_ENDIAN)))
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000359 endian = BIG_ENDIAN;
360
361 if (endian == UNKNOWN_ENDIAN) {
362 printf("invalid uberblock magic\n");
363 return ZFS_ERR_BAD_FS;
364 }
365
366 memset(&zc, 0, sizeof(zc));
367 zc.zc_word[0] = cpu_to_zfs64(offset, endian);
368 err = zio_checksum_verify(zc, ZIO_CHECKSUM_LABEL, endian,
369 (char *) uber, UBERBLOCK_SIZE(data->vdev_ashift));
370
371 if (!err) {
372 /* Check that the data pointed by the rootbp is usable. */
373 void *osp = NULL;
374 size_t ospsize;
375 err = zio_read(&uber->ub_rootbp, endian, &osp, &ospsize, data);
376 free(osp);
377
378 if (!err && ospsize < OBJSET_PHYS_SIZE_V14) {
379 printf("uberblock rootbp points to invalid data\n");
380 return ZFS_ERR_BAD_FS;
381 }
382 }
383
384 return err;
385}
386
387/*
388 * Find the best uberblock.
389 * Return:
390 * Success - Pointer to the best uberblock.
391 * Failure - NULL
392 */
393static uberblock_t *find_bestub(char *ub_array, struct zfs_data *data)
394{
395 const uint64_t sector = data->vdev_phys_sector;
396 uberblock_t *ubbest = NULL;
397 uberblock_t *ubnext;
398 unsigned int i, offset, pickedub = 0;
399 int err = ZFS_ERR_NONE;
400
401 const unsigned int UBCOUNT = UBERBLOCK_COUNT(data->vdev_ashift);
402 const uint64_t UBBYTES = UBERBLOCK_SIZE(data->vdev_ashift);
403
404 for (i = 0; i < UBCOUNT; i++) {
405 ubnext = (uberblock_t *) (i * UBBYTES + ub_array);
406 offset = (sector << SPA_MINBLOCKSHIFT) + VDEV_PHYS_SIZE + (i * UBBYTES);
407
408 err = uberblock_verify(ubnext, offset, data);
409 if (err)
410 continue;
411
412 if (ubbest == NULL || vdev_uberblock_compare(ubnext, ubbest) > 0) {
413 ubbest = ubnext;
414 pickedub = i;
415 }
416 }
417
418 if (ubbest)
419 debug("zfs Found best uberblock at idx %d, txg %llu\n",
420 pickedub, (unsigned long long) ubbest->ub_txg);
421
422 return ubbest;
423}
424
425static inline size_t
426get_psize(blkptr_t *bp, zfs_endian_t endian)
427{
428 return (((zfs_to_cpu64((bp)->blk_prop, endian) >> 16) & 0xffff) + 1)
429 << SPA_MINBLOCKSHIFT;
430}
431
432static uint64_t
433dva_get_offset(dva_t *dva, zfs_endian_t endian)
434{
435 return zfs_to_cpu64((dva)->dva_word[1],
436 endian) << SPA_MINBLOCKSHIFT;
437}
438
439/*
440 * Read a block of data based on the gang block address dva,
441 * and put its data in buf.
442 *
443 */
444static int
445zio_read_gang(blkptr_t *bp, zfs_endian_t endian, dva_t *dva, void *buf,
446 struct zfs_data *data)
447{
448 zio_gbh_phys_t *zio_gb;
449 uint64_t offset, sector;
450 unsigned i;
451 int err;
452 zio_cksum_t zc;
453
454 memset(&zc, 0, sizeof(zc));
455
456 zio_gb = malloc(SPA_GANGBLOCKSIZE);
457 if (!zio_gb)
458 return ZFS_ERR_OUT_OF_MEMORY;
459
460 offset = dva_get_offset(dva, endian);
461 sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
462
463 /* read in the gang block header */
464 err = zfs_devread(sector, 0, SPA_GANGBLOCKSIZE, (char *) zio_gb);
465
466 if (err) {
467 free(zio_gb);
468 return err;
469 }
470
471 /* XXX */
472 /* self checksuming the gang block header */
473 ZIO_SET_CHECKSUM(&zc, DVA_GET_VDEV(dva),
474 dva_get_offset(dva, endian), bp->blk_birth, 0);
475 err = zio_checksum_verify(zc, ZIO_CHECKSUM_GANG_HEADER, endian,
476 (char *) zio_gb, SPA_GANGBLOCKSIZE);
477 if (err) {
478 free(zio_gb);
479 return err;
480 }
481
482 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
483
484 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
485 if (zio_gb->zg_blkptr[i].blk_birth == 0)
486 continue;
487
488 err = zio_read_data(&zio_gb->zg_blkptr[i], endian, buf, data);
489 if (err) {
490 free(zio_gb);
491 return err;
492 }
493 buf = (char *) buf + get_psize(&zio_gb->zg_blkptr[i], endian);
494 }
495 free(zio_gb);
496 return ZFS_ERR_NONE;
497}
498
499/*
500 * Read in a block of raw data to buf.
501 */
502static int
503zio_read_data(blkptr_t *bp, zfs_endian_t endian, void *buf,
504 struct zfs_data *data)
505{
506 int i, psize;
507 int err = ZFS_ERR_NONE;
508
509 psize = get_psize(bp, endian);
510
511 /* pick a good dva from the block pointer */
512 for (i = 0; i < SPA_DVAS_PER_BP; i++) {
513 uint64_t offset, sector;
514
515 if (bp->blk_dva[i].dva_word[0] == 0 && bp->blk_dva[i].dva_word[1] == 0)
516 continue;
517
518 if ((zfs_to_cpu64(bp->blk_dva[i].dva_word[1], endian)>>63) & 1) {
519 err = zio_read_gang(bp, endian, &bp->blk_dva[i], buf, data);
520 } else {
521 /* read in a data block */
522 offset = dva_get_offset(&bp->blk_dva[i], endian);
523 sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
524
525 err = zfs_devread(sector, 0, psize, buf);
526 }
527
528 if (!err) {
529 /*Check the underlying checksum before we rule this DVA as "good"*/
530 uint32_t checkalgo = (zfs_to_cpu64((bp)->blk_prop, endian) >> 40) & 0xff;
531
532 err = zio_checksum_verify(bp->blk_cksum, checkalgo, endian, buf, psize);
533 if (!err)
534 return ZFS_ERR_NONE;
535 }
536
537 /* If read failed or checksum bad, reset the error. Hopefully we've got some more DVA's to try.*/
538 }
539
540 if (!err) {
541 printf("couldn't find a valid DVA\n");
542 err = ZFS_ERR_BAD_FS;
543 }
544
545 return err;
546}
547
548/*
549 * Read in a block of data, verify its checksum, decompress if needed,
550 * and put the uncompressed data in buf.
551 */
552static int
553zio_read(blkptr_t *bp, zfs_endian_t endian, void **buf,
554 size_t *size, struct zfs_data *data)
555{
556 size_t lsize, psize;
557 unsigned int comp;
558 char *compbuf = NULL;
559 int err;
560
561 *buf = NULL;
562
563 comp = (zfs_to_cpu64((bp)->blk_prop, endian)>>32) & 0xff;
564 lsize = (BP_IS_HOLE(bp) ? 0 :
565 (((zfs_to_cpu64((bp)->blk_prop, endian) & 0xffff) + 1)
566 << SPA_MINBLOCKSHIFT));
567 psize = get_psize(bp, endian);
568
569 if (size)
570 *size = lsize;
571
572 if (comp >= ZIO_COMPRESS_FUNCTIONS) {
573 printf("compression algorithm %u not supported\n", (unsigned int) comp);
574 return ZFS_ERR_NOT_IMPLEMENTED_YET;
575 }
576
577 if (comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL) {
578 printf("compression algorithm %s not supported\n", decomp_table[comp].name);
579 return ZFS_ERR_NOT_IMPLEMENTED_YET;
580 }
581
582 if (comp != ZIO_COMPRESS_OFF) {
583 compbuf = malloc(psize);
584 if (!compbuf)
585 return ZFS_ERR_OUT_OF_MEMORY;
586 } else {
587 compbuf = *buf = malloc(lsize);
588 }
589
590 err = zio_read_data(bp, endian, compbuf, data);
591 if (err) {
592 free(compbuf);
593 *buf = NULL;
594 return err;
595 }
596
597 if (comp != ZIO_COMPRESS_OFF) {
598 *buf = malloc(lsize);
599 if (!*buf) {
600 free(compbuf);
601 return ZFS_ERR_OUT_OF_MEMORY;
602 }
603
604 err = decomp_table[comp].decomp_func(compbuf, *buf, psize, lsize);
605 free(compbuf);
606 if (err) {
607 free(*buf);
608 *buf = NULL;
609 return err;
610 }
611 }
612
613 return ZFS_ERR_NONE;
614}
615
616/*
617 * Get the block from a block id.
618 * push the block onto the stack.
619 *
620 */
621static int
622dmu_read(dnode_end_t *dn, uint64_t blkid, void **buf,
623 zfs_endian_t *endian_out, struct zfs_data *data)
624{
625 int idx, level;
626 blkptr_t *bp_array = dn->dn.dn_blkptr;
627 int epbs = dn->dn.dn_indblkshift - SPA_BLKPTRSHIFT;
628 blkptr_t *bp;
629 void *tmpbuf = 0;
630 zfs_endian_t endian;
631 int err = ZFS_ERR_NONE;
632
633 bp = malloc(sizeof(blkptr_t));
634 if (!bp)
635 return ZFS_ERR_OUT_OF_MEMORY;
636
637 endian = dn->endian;
638 for (level = dn->dn.dn_nlevels - 1; level >= 0; level--) {
639 idx = (blkid >> (epbs * level)) & ((1 << epbs) - 1);
640 *bp = bp_array[idx];
641 if (bp_array != dn->dn.dn_blkptr) {
642 free(bp_array);
643 bp_array = 0;
644 }
645
646 if (BP_IS_HOLE(bp)) {
647 size_t size = zfs_to_cpu16(dn->dn.dn_datablkszsec,
648 dn->endian)
649 << SPA_MINBLOCKSHIFT;
650 *buf = malloc(size);
mwleeds@mailtundra.com0c17f852024-04-06 18:47:25 -0700651 if (!*buf) {
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000652 err = ZFS_ERR_OUT_OF_MEMORY;
653 break;
654 }
655 memset(*buf, 0, size);
656 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
657 break;
658 }
659 if (level == 0) {
660 err = zio_read(bp, endian, buf, 0, data);
661 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
662 break;
663 }
664 err = zio_read(bp, endian, &tmpbuf, 0, data);
665 endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
666 if (err)
667 break;
668 bp_array = tmpbuf;
669 }
670 if (bp_array != dn->dn.dn_blkptr)
671 free(bp_array);
672 if (endian_out)
673 *endian_out = endian;
674
675 free(bp);
676 return err;
677}
678
679/*
680 * mzap_lookup: Looks up property described by "name" and returns the value
681 * in "value".
682 */
683static int
684mzap_lookup(mzap_phys_t *zapobj, zfs_endian_t endian,
685 int objsize, char *name, uint64_t * value)
686{
687 int i, chunks;
688 mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
689
690 chunks = objsize / MZAP_ENT_LEN - 1;
691 for (i = 0; i < chunks; i++) {
692 if (strcmp(mzap_ent[i].mze_name, name) == 0) {
693 *value = zfs_to_cpu64(mzap_ent[i].mze_value, endian);
694 return ZFS_ERR_NONE;
695 }
696 }
697
698 printf("couldn't find '%s'\n", name);
699 return ZFS_ERR_FILE_NOT_FOUND;
700}
701
702static int
703mzap_iterate(mzap_phys_t *zapobj, zfs_endian_t endian, int objsize,
704 int (*hook)(const char *name,
705 uint64_t val,
706 struct zfs_data *data),
707 struct zfs_data *data)
708{
709 int i, chunks;
710 mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
711
712 chunks = objsize / MZAP_ENT_LEN - 1;
713 for (i = 0; i < chunks; i++) {
714 if (hook(mzap_ent[i].mze_name,
715 zfs_to_cpu64(mzap_ent[i].mze_value, endian),
716 data))
717 return 1;
718 }
719
720 return 0;
721}
722
723static uint64_t
724zap_hash(uint64_t salt, const char *name)
725{
726 static uint64_t table[256];
727 const uint8_t *cp;
728 uint8_t c;
729 uint64_t crc = salt;
730
731 if (table[128] == 0) {
Jorgen Lundmane183de02014-11-07 10:08:35 +0900732 uint64_t *ct = NULL;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000733 int i, j;
734 for (i = 0; i < 256; i++) {
735 for (ct = table + i, *ct = i, j = 8; j > 0; j--)
736 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
737 }
738 }
739
740 for (cp = (const uint8_t *) name; (c = *cp) != '\0'; cp++)
741 crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
742
743 /*
744 * Only use 28 bits, since we need 4 bits in the cookie for the
745 * collision differentiator. We MUST use the high bits, since
746 * those are the onces that we first pay attention to when
747 * chosing the bucket.
748 */
749 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
750
751 return crc;
752}
753
754/*
755 * Only to be used on 8-bit arrays.
756 * array_len is actual len in bytes (not encoded le_value_length).
757 * buf is null-terminated.
758 */
759/* XXX */
760static int
761zap_leaf_array_equal(zap_leaf_phys_t *l, zfs_endian_t endian,
762 int blksft, int chunk, int array_len, const char *buf)
763{
764 int bseen = 0;
765
766 while (bseen < array_len) {
767 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900768 int toread = min(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000769
770 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
771 return 0;
772
773 if (memcmp(la->la_array, buf + bseen, toread) != 0)
774 break;
775 chunk = zfs_to_cpu16(la->la_next, endian);
776 bseen += toread;
777 }
778 return (bseen == array_len);
779}
780
781/* XXX */
782static int
783zap_leaf_array_get(zap_leaf_phys_t *l, zfs_endian_t endian, int blksft,
784 int chunk, int array_len, char *buf)
785{
786 int bseen = 0;
787
788 while (bseen < array_len) {
789 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900790 int toread = min(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000791
792 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
793 /* Don't use errno because this error is to be ignored. */
794 return ZFS_ERR_BAD_FS;
795
796 memcpy(buf + bseen, la->la_array, toread);
797 chunk = zfs_to_cpu16(la->la_next, endian);
798 bseen += toread;
799 }
800 return ZFS_ERR_NONE;
801}
802
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000803/*
804 * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
805 * value for the property "name".
806 *
807 */
808/* XXX */
809static int
810zap_leaf_lookup(zap_leaf_phys_t *l, zfs_endian_t endian,
811 int blksft, uint64_t h,
812 const char *name, uint64_t *value)
813{
814 uint16_t chunk;
815 struct zap_leaf_entry *le;
816
817 /* Verify if this is a valid leaf block */
818 if (zfs_to_cpu64(l->l_hdr.lh_block_type, endian) != ZBT_LEAF) {
819 printf("invalid leaf type\n");
820 return ZFS_ERR_BAD_FS;
821 }
822 if (zfs_to_cpu32(l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) {
823 printf("invalid leaf magic\n");
824 return ZFS_ERR_BAD_FS;
825 }
826
827 for (chunk = zfs_to_cpu16(l->l_hash[LEAF_HASH(blksft, h)], endian);
828 chunk != CHAIN_END; chunk = le->le_next) {
829
830 if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft)) {
831 printf("invalid chunk number\n");
832 return ZFS_ERR_BAD_FS;
833 }
834
835 le = ZAP_LEAF_ENTRY(l, blksft, chunk);
836
837 /* Verify the chunk entry */
838 if (le->le_type != ZAP_CHUNK_ENTRY) {
839 printf("invalid chunk entry\n");
840 return ZFS_ERR_BAD_FS;
841 }
842
843 if (zfs_to_cpu64(le->le_hash, endian) != h)
844 continue;
845
846 if (zap_leaf_array_equal(l, endian, blksft,
847 zfs_to_cpu16(le->le_name_chunk, endian),
848 zfs_to_cpu16(le->le_name_length, endian),
849 name)) {
850 struct zap_leaf_array *la;
851
852 if (le->le_int_size != 8 || le->le_value_length != 1) {
853 printf("invalid leaf chunk entry\n");
854 return ZFS_ERR_BAD_FS;
855 }
856 /* get the uint64_t property value */
857 la = &ZAP_LEAF_CHUNK(l, blksft, le->le_value_chunk).l_array;
858
859 *value = be64_to_cpu(la->la_array64);
860
861 return ZFS_ERR_NONE;
862 }
863 }
864
865 printf("couldn't find '%s'\n", name);
866 return ZFS_ERR_FILE_NOT_FOUND;
867}
868
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +0000869/* Verify if this is a fat zap header block */
870static int
871zap_verify(zap_phys_t *zap)
872{
873 if (zap->zap_magic != (uint64_t) ZAP_MAGIC) {
874 printf("bad ZAP magic\n");
875 return ZFS_ERR_BAD_FS;
876 }
877
878 if (zap->zap_flags != 0) {
879 printf("bad ZAP flags\n");
880 return ZFS_ERR_BAD_FS;
881 }
882
883 if (zap->zap_salt == 0) {
884 printf("bad ZAP salt\n");
885 return ZFS_ERR_BAD_FS;
886 }
887
888 return ZFS_ERR_NONE;
889}
890
891/*
892 * Fat ZAP lookup
893 *
894 */
895/* XXX */
896static int
897fzap_lookup(dnode_end_t *zap_dnode, zap_phys_t *zap,
898 char *name, uint64_t *value, struct zfs_data *data)
899{
900 void *l;
901 uint64_t hash, idx, blkid;
902 int blksft = zfs_log2(zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec,
903 zap_dnode->endian) << DNODE_SHIFT);
904 int err;
905 zfs_endian_t leafendian;
906
907 err = zap_verify(zap);
908 if (err)
909 return err;
910
911 hash = zap_hash(zap->zap_salt, name);
912
913 /* get block id from index */
914 if (zap->zap_ptrtbl.zt_numblks != 0) {
915 printf("external pointer tables not supported\n");
916 return ZFS_ERR_NOT_IMPLEMENTED_YET;
917 }
918 idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
919 blkid = ((uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))];
920
921 /* Get the leaf block */
922 if ((1U << blksft) < sizeof(zap_leaf_phys_t)) {
923 printf("ZAP leaf is too small\n");
924 return ZFS_ERR_BAD_FS;
925 }
926 err = dmu_read(zap_dnode, blkid, &l, &leafendian, data);
927 if (err)
928 return err;
929
930 err = zap_leaf_lookup(l, leafendian, blksft, hash, name, value);
931 free(l);
932 return err;
933}
934
935/* XXX */
936static int
937fzap_iterate(dnode_end_t *zap_dnode, zap_phys_t *zap,
938 int (*hook)(const char *name,
939 uint64_t val,
940 struct zfs_data *data),
941 struct zfs_data *data)
942{
943 zap_leaf_phys_t *l;
944 void *l_in;
945 uint64_t idx, blkid;
946 uint16_t chunk;
947 int blksft = zfs_log2(zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec,
948 zap_dnode->endian) << DNODE_SHIFT);
949 int err;
950 zfs_endian_t endian;
951
952 if (zap_verify(zap))
953 return 0;
954
955 /* get block id from index */
956 if (zap->zap_ptrtbl.zt_numblks != 0) {
957 printf("external pointer tables not supported\n");
958 return 0;
959 }
960 /* Get the leaf block */
961 if ((1U << blksft) < sizeof(zap_leaf_phys_t)) {
962 printf("ZAP leaf is too small\n");
963 return 0;
964 }
965 for (idx = 0; idx < zap->zap_ptrtbl.zt_numblks; idx++) {
966 blkid = ((uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))];
967
968 err = dmu_read(zap_dnode, blkid, &l_in, &endian, data);
969 l = l_in;
970 if (err)
971 continue;
972
973 /* Verify if this is a valid leaf block */
974 if (zfs_to_cpu64(l->l_hdr.lh_block_type, endian) != ZBT_LEAF) {
975 free(l);
976 continue;
977 }
978 if (zfs_to_cpu32(l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) {
979 free(l);
980 continue;
981 }
982
983 for (chunk = 0; chunk < ZAP_LEAF_NUMCHUNKS(blksft); chunk++) {
984 char *buf;
985 struct zap_leaf_array *la;
986 struct zap_leaf_entry *le;
987 uint64_t val;
988 le = ZAP_LEAF_ENTRY(l, blksft, chunk);
989
990 /* Verify the chunk entry */
991 if (le->le_type != ZAP_CHUNK_ENTRY)
992 continue;
993
994 buf = malloc(zfs_to_cpu16(le->le_name_length, endian)
995 + 1);
996 if (zap_leaf_array_get(l, endian, blksft, le->le_name_chunk,
997 le->le_name_length, buf)) {
998 free(buf);
999 continue;
1000 }
1001 buf[le->le_name_length] = 0;
1002
1003 if (le->le_int_size != 8
1004 || zfs_to_cpu16(le->le_value_length, endian) != 1)
1005 continue;
1006
1007 /* get the uint64_t property value */
1008 la = &ZAP_LEAF_CHUNK(l, blksft, le->le_value_chunk).l_array;
1009 val = be64_to_cpu(la->la_array64);
1010 if (hook(buf, val, data))
1011 return 1;
1012 free(buf);
1013 }
1014 }
1015 return 0;
1016}
1017
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001018/*
1019 * Read in the data of a zap object and find the value for a matching
1020 * property name.
1021 *
1022 */
1023static int
1024zap_lookup(dnode_end_t *zap_dnode, char *name, uint64_t *val,
1025 struct zfs_data *data)
1026{
1027 uint64_t block_type;
1028 int size;
1029 void *zapbuf;
1030 int err;
1031 zfs_endian_t endian;
1032
1033 /* Read in the first block of the zap object data. */
1034 size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec,
1035 zap_dnode->endian) << SPA_MINBLOCKSHIFT;
1036 err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data);
1037 if (err)
1038 return err;
1039 block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian);
1040
1041 if (block_type == ZBT_MICRO) {
1042 err = (mzap_lookup(zapbuf, endian, size, name, val));
1043 free(zapbuf);
1044 return err;
1045 } else if (block_type == ZBT_HEADER) {
1046 /* this is a fat zap */
1047 err = (fzap_lookup(zap_dnode, zapbuf, name, val, data));
1048 free(zapbuf);
1049 return err;
1050 }
1051
1052 printf("unknown ZAP type\n");
Jorgen Lundmane183de02014-11-07 10:08:35 +09001053 free(zapbuf);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001054 return ZFS_ERR_BAD_FS;
1055}
1056
1057static int
1058zap_iterate(dnode_end_t *zap_dnode,
1059 int (*hook)(const char *name, uint64_t val,
1060 struct zfs_data *data),
1061 struct zfs_data *data)
1062{
1063 uint64_t block_type;
1064 int size;
1065 void *zapbuf;
1066 int err;
1067 int ret;
1068 zfs_endian_t endian;
1069
1070 /* Read in the first block of the zap object data. */
1071 size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT;
1072 err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data);
1073 if (err)
1074 return 0;
1075 block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian);
1076
1077 if (block_type == ZBT_MICRO) {
1078 ret = mzap_iterate(zapbuf, endian, size, hook, data);
1079 free(zapbuf);
1080 return ret;
1081 } else if (block_type == ZBT_HEADER) {
1082 /* this is a fat zap */
1083 ret = fzap_iterate(zap_dnode, zapbuf, hook, data);
1084 free(zapbuf);
1085 return ret;
1086 }
1087 printf("unknown ZAP type\n");
Jorgen Lundmane183de02014-11-07 10:08:35 +09001088 free(zapbuf);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001089 return 0;
1090}
1091
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001092/*
1093 * Get the dnode of an object number from the metadnode of an object set.
1094 *
1095 * Input
1096 * mdn - metadnode to get the object dnode
1097 * objnum - object number for the object dnode
1098 * buf - data buffer that holds the returning dnode
1099 */
1100static int
1101dnode_get(dnode_end_t *mdn, uint64_t objnum, uint8_t type,
1102 dnode_end_t *buf, struct zfs_data *data)
1103{
1104 uint64_t blkid, blksz; /* the block id this object dnode is in */
1105 int epbs; /* shift of number of dnodes in a block */
1106 int idx; /* index within a block */
1107 void *dnbuf;
1108 int err;
1109 zfs_endian_t endian;
1110
1111 blksz = zfs_to_cpu16(mdn->dn.dn_datablkszsec,
1112 mdn->endian) << SPA_MINBLOCKSHIFT;
1113
1114 epbs = zfs_log2(blksz) - DNODE_SHIFT;
1115 blkid = objnum >> epbs;
1116 idx = objnum & ((1 << epbs) - 1);
1117
1118 if (data->dnode_buf != NULL && memcmp(data->dnode_mdn, mdn,
1119 sizeof(*mdn)) == 0
1120 && objnum >= data->dnode_start && objnum < data->dnode_end) {
1121 memmove(&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE);
1122 buf->endian = data->dnode_endian;
1123 if (type && buf->dn.dn_type != type) {
1124 printf("incorrect dnode type: %02X != %02x\n", buf->dn.dn_type, type);
1125 return ZFS_ERR_BAD_FS;
1126 }
1127 return ZFS_ERR_NONE;
1128 }
1129
1130 err = dmu_read(mdn, blkid, &dnbuf, &endian, data);
1131 if (err)
1132 return err;
1133
1134 free(data->dnode_buf);
1135 free(data->dnode_mdn);
1136 data->dnode_mdn = malloc(sizeof(*mdn));
1137 if (!data->dnode_mdn) {
1138 data->dnode_buf = 0;
1139 } else {
1140 memcpy(data->dnode_mdn, mdn, sizeof(*mdn));
1141 data->dnode_buf = dnbuf;
1142 data->dnode_start = blkid << epbs;
1143 data->dnode_end = (blkid + 1) << epbs;
1144 data->dnode_endian = endian;
1145 }
1146
1147 memmove(&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE);
1148 buf->endian = endian;
1149 if (type && buf->dn.dn_type != type) {
1150 printf("incorrect dnode type\n");
1151 return ZFS_ERR_BAD_FS;
1152 }
1153
1154 return ZFS_ERR_NONE;
1155}
1156
1157/*
1158 * Get the file dnode for a given file name where mdn is the meta dnode
1159 * for this ZFS object set. When found, place the file dnode in dn.
1160 * The 'path' argument will be mangled.
1161 *
1162 */
1163static int
1164dnode_get_path(dnode_end_t *mdn, const char *path_in, dnode_end_t *dn,
1165 struct zfs_data *data)
1166{
1167 uint64_t objnum, version;
1168 char *cname, ch;
1169 int err = ZFS_ERR_NONE;
1170 char *path, *path_buf;
1171 struct dnode_chain {
1172 struct dnode_chain *next;
1173 dnode_end_t dn;
1174 };
1175 struct dnode_chain *dnode_path = 0, *dn_new, *root;
1176
1177 dn_new = malloc(sizeof(*dn_new));
1178 if (!dn_new)
1179 return ZFS_ERR_OUT_OF_MEMORY;
1180 dn_new->next = 0;
1181 dnode_path = root = dn_new;
1182
1183 err = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
1184 &(dnode_path->dn), data);
1185 if (err) {
1186 free(dn_new);
1187 return err;
1188 }
1189
1190 err = zap_lookup(&(dnode_path->dn), ZPL_VERSION_STR, &version, data);
1191 if (err) {
1192 free(dn_new);
1193 return err;
1194 }
1195 if (version > ZPL_VERSION) {
1196 free(dn_new);
1197 printf("too new ZPL version\n");
1198 return ZFS_ERR_NOT_IMPLEMENTED_YET;
1199 }
1200
1201 err = zap_lookup(&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data);
1202 if (err) {
1203 free(dn_new);
1204 return err;
1205 }
1206
1207 err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data);
1208 if (err) {
1209 free(dn_new);
1210 return err;
1211 }
1212
1213 path = path_buf = strdup(path_in);
1214 if (!path_buf) {
1215 free(dn_new);
1216 return ZFS_ERR_OUT_OF_MEMORY;
1217 }
1218
1219 while (1) {
1220 /* skip leading slashes */
1221 while (*path == '/')
1222 path++;
1223 if (!*path)
1224 break;
1225 /* get the next component name */
1226 cname = path;
1227 while (*path && *path != '/')
1228 path++;
1229 /* Skip dot. */
1230 if (cname + 1 == path && cname[0] == '.')
1231 continue;
1232 /* Handle double dot. */
1233 if (cname + 2 == path && cname[0] == '.' && cname[1] == '.') {
1234 if (dn_new->next) {
1235 dn_new = dnode_path;
1236 dnode_path = dn_new->next;
1237 free(dn_new);
1238 } else {
1239 printf("can't resolve ..\n");
1240 err = ZFS_ERR_FILE_NOT_FOUND;
1241 break;
1242 }
1243 continue;
1244 }
1245
1246 ch = *path;
1247 *path = 0; /* ensure null termination */
1248
1249 if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) {
1250 free(path_buf);
1251 printf("not a directory\n");
1252 return ZFS_ERR_BAD_FILE_TYPE;
1253 }
1254 err = zap_lookup(&(dnode_path->dn), cname, &objnum, data);
1255 if (err)
1256 break;
1257
1258 dn_new = malloc(sizeof(*dn_new));
1259 if (!dn_new) {
1260 err = ZFS_ERR_OUT_OF_MEMORY;
1261 break;
1262 }
1263 dn_new->next = dnode_path;
1264 dnode_path = dn_new;
1265
1266 objnum = ZFS_DIRENT_OBJ(objnum);
1267 err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data);
1268 if (err)
1269 break;
1270
1271 *path = ch;
1272 }
1273
1274 if (!err)
1275 memcpy(dn, &(dnode_path->dn), sizeof(*dn));
1276
1277 while (dnode_path) {
1278 dn_new = dnode_path->next;
1279 free(dnode_path);
1280 dnode_path = dn_new;
1281 }
1282 free(path_buf);
1283 return err;
1284}
1285
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001286/*
1287 * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1288 * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1289 * of pool/rootfs.
1290 *
1291 * If no fsname and no obj are given, return the DSL_DIR metadnode.
1292 * If fsname is given, return its metadnode and its matching object number.
1293 * If only obj is given, return the metadnode for this object number.
1294 *
1295 */
1296static int
1297get_filesystem_dnode(dnode_end_t *mosmdn, char *fsname,
1298 dnode_end_t *mdn, struct zfs_data *data)
1299{
1300 uint64_t objnum;
1301 int err;
1302
1303 err = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1304 DMU_OT_OBJECT_DIRECTORY, mdn, data);
1305 if (err)
1306 return err;
1307
1308 err = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum, data);
1309 if (err)
1310 return err;
1311
1312 err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
1313 if (err)
1314 return err;
1315
1316 while (*fsname) {
1317 uint64_t childobj;
1318 char *cname, ch;
1319
1320 while (*fsname == '/')
1321 fsname++;
1322
1323 if (!*fsname || *fsname == '@')
1324 break;
1325
1326 cname = fsname;
1327 while (*fsname && !isspace(*fsname) && *fsname != '/')
1328 fsname++;
1329 ch = *fsname;
1330 *fsname = 0;
1331
1332 childobj = zfs_to_cpu64((((dsl_dir_phys_t *) DN_BONUS(&mdn->dn)))->dd_child_dir_zapobj, mdn->endian);
1333 err = dnode_get(mosmdn, childobj,
1334 DMU_OT_DSL_DIR_CHILD_MAP, mdn, data);
1335 if (err)
1336 return err;
1337
1338 err = zap_lookup(mdn, cname, &objnum, data);
1339 if (err)
1340 return err;
1341
1342 err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
1343 if (err)
1344 return err;
1345
1346 *fsname = ch;
1347 }
1348 return ZFS_ERR_NONE;
1349}
1350
1351static int
1352make_mdn(dnode_end_t *mdn, struct zfs_data *data)
1353{
1354 void *osp;
1355 blkptr_t *bp;
1356 size_t ospsize;
1357 int err;
1358
1359 bp = &(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_bp);
1360 err = zio_read(bp, mdn->endian, &osp, &ospsize, data);
1361 if (err)
1362 return err;
1363 if (ospsize < OBJSET_PHYS_SIZE_V14) {
1364 free(osp);
1365 printf("too small osp\n");
1366 return ZFS_ERR_BAD_FS;
1367 }
1368
1369 mdn->endian = (zfs_to_cpu64(bp->blk_prop, mdn->endian)>>63) & 1;
1370 memmove((char *) &(mdn->dn),
1371 (char *) &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE);
1372 free(osp);
1373 return ZFS_ERR_NONE;
1374}
1375
1376static int
1377dnode_get_fullpath(const char *fullpath, dnode_end_t *mdn,
1378 uint64_t *mdnobj, dnode_end_t *dn, int *isfs,
1379 struct zfs_data *data)
1380{
1381 char *fsname, *snapname;
1382 const char *ptr_at, *filename;
1383 uint64_t headobj;
1384 int err;
1385
1386 ptr_at = strchr(fullpath, '@');
1387 if (!ptr_at) {
1388 *isfs = 1;
1389 filename = 0;
1390 snapname = 0;
1391 fsname = strdup(fullpath);
1392 } else {
1393 const char *ptr_slash = strchr(ptr_at, '/');
1394
1395 *isfs = 0;
1396 fsname = malloc(ptr_at - fullpath + 1);
1397 if (!fsname)
1398 return ZFS_ERR_OUT_OF_MEMORY;
1399 memcpy(fsname, fullpath, ptr_at - fullpath);
1400 fsname[ptr_at - fullpath] = 0;
1401 if (ptr_at[1] && ptr_at[1] != '/') {
1402 snapname = malloc(ptr_slash - ptr_at);
1403 if (!snapname) {
1404 free(fsname);
1405 return ZFS_ERR_OUT_OF_MEMORY;
1406 }
1407 memcpy(snapname, ptr_at + 1, ptr_slash - ptr_at - 1);
1408 snapname[ptr_slash - ptr_at - 1] = 0;
1409 } else {
1410 snapname = 0;
1411 }
1412 if (ptr_slash)
1413 filename = ptr_slash;
1414 else
1415 filename = "/";
1416 printf("zfs fsname = '%s' snapname='%s' filename = '%s'\n",
1417 fsname, snapname, filename);
1418 }
1419
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001420 err = get_filesystem_dnode(&(data->mos), fsname, dn, data);
1421
1422 if (err) {
1423 free(fsname);
1424 free(snapname);
1425 return err;
1426 }
1427
1428 headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&dn->dn))->dd_head_dataset_obj, dn->endian);
1429
1430 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
1431 if (err) {
1432 free(fsname);
1433 free(snapname);
1434 return err;
1435 }
1436
1437 if (snapname) {
1438 uint64_t snapobj;
1439
1440 snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_snapnames_zapobj, mdn->endian);
1441
1442 err = dnode_get(&(data->mos), snapobj,
1443 DMU_OT_DSL_DS_SNAP_MAP, mdn, data);
1444 if (!err)
1445 err = zap_lookup(mdn, snapname, &headobj, data);
1446 if (!err)
1447 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
1448 if (err) {
1449 free(fsname);
1450 free(snapname);
1451 return err;
1452 }
1453 }
1454
1455 if (mdnobj)
1456 *mdnobj = headobj;
1457
1458 make_mdn(mdn, data);
1459
1460 if (*isfs) {
1461 free(fsname);
1462 free(snapname);
1463 return ZFS_ERR_NONE;
1464 }
1465 err = dnode_get_path(mdn, filename, dn, data);
1466 free(fsname);
1467 free(snapname);
1468 return err;
1469}
1470
1471/*
1472 * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1473 *
1474 * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1475 *
1476 * encoding method/host endian (4 bytes)
1477 * nvl_version (4 bytes)
1478 * nvl_nvflag (4 bytes)
1479 * encoded nvpairs:
1480 * encoded size of the nvpair (4 bytes)
1481 * decoded size of the nvpair (4 bytes)
1482 * name string size (4 bytes)
1483 * name string data (sizeof(NV_ALIGN4(string))
1484 * data type (4 bytes)
1485 * # of elements in the nvpair (4 bytes)
1486 * data
1487 * 2 zero's for the last nvpair
1488 * (end of the entire list) (8 bytes)
1489 *
1490 */
1491
1492static int
1493nvlist_find_value(char *nvlist, char *name, int valtype, char **val,
1494 size_t *size_out, size_t *nelm_out)
1495{
1496 int name_len, type, encode_size;
1497 char *nvpair, *nvp_name;
1498
1499 /* Verify if the 1st and 2nd byte in the nvlist are valid. */
1500 /* NOTE: independently of what endianness header announces all
1501 subsequent values are big-endian. */
1502 if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN
1503 && nvlist[1] != NV_BIG_ENDIAN)) {
1504 printf("zfs incorrect nvlist header\n");
1505 return ZFS_ERR_BAD_FS;
1506 }
1507
1508 /* skip the header, nvl_version, and nvl_nvflag */
1509 nvlist = nvlist + 4 * 3;
1510 /*
1511 * Loop thru the nvpair list
1512 * The XDR representation of an integer is in big-endian byte order.
1513 */
1514 while ((encode_size = be32_to_cpu(*(uint32_t *) nvlist))) {
1515 int nelm;
1516
1517 nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
1518
1519 name_len = be32_to_cpu(*(uint32_t *) nvpair);
1520 nvpair += 4;
1521
1522 nvp_name = nvpair;
1523 nvpair = nvpair + ((name_len + 3) & ~3); /* align */
1524
1525 type = be32_to_cpu(*(uint32_t *) nvpair);
1526 nvpair += 4;
1527
1528 nelm = be32_to_cpu(*(uint32_t *) nvpair);
1529 if (nelm < 1) {
1530 printf("empty nvpair\n");
1531 return ZFS_ERR_BAD_FS;
1532 }
1533
1534 nvpair += 4;
1535
1536 if ((strncmp(nvp_name, name, name_len) == 0) && type == valtype) {
1537 *val = nvpair;
1538 *size_out = encode_size;
1539 if (nelm_out)
1540 *nelm_out = nelm;
1541 return 1;
1542 }
1543
1544 nvlist += encode_size; /* goto the next nvpair */
1545 }
1546 return 0;
1547}
1548
mwleeds@mailtundra.com1fe745b2024-04-06 18:47:27 -07001549int is_word_aligned_ptr(void *ptr) {
1550 return ((uintptr_t)ptr & (sizeof(void *) - 1)) == 0;
1551}
1552
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001553int
1554zfs_nvlist_lookup_uint64(char *nvlist, char *name, uint64_t *out)
1555{
1556 char *nvpair;
1557 size_t size;
1558 int found;
1559
1560 found = nvlist_find_value(nvlist, name, DATA_TYPE_UINT64, &nvpair, &size, 0);
1561 if (!found)
1562 return 0;
1563 if (size < sizeof(uint64_t)) {
1564 printf("invalid uint64\n");
1565 return ZFS_ERR_BAD_FS;
1566 }
1567
mwleeds@mailtundra.com1fe745b2024-04-06 18:47:27 -07001568 /* On arm64, calling be64_to_cpu() on a value stored at a memory address
1569 * that's not 8-byte aligned causes the CPU to reset. Avoid that by copying the
1570 * value somewhere else if needed.
1571 */
1572 if (!is_word_aligned_ptr((void *)nvpair)) {
1573 uint64_t *alignedptr = malloc(sizeof(uint64_t));
1574 if (!alignedptr)
1575 return 0;
1576 memcpy(alignedptr, nvpair, sizeof(uint64_t));
1577 *out = be64_to_cpu(*alignedptr);
1578 free(alignedptr);
1579 return 1;
1580 }
1581
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001582 *out = be64_to_cpu(*(uint64_t *) nvpair);
1583 return 1;
1584}
1585
1586char *
1587zfs_nvlist_lookup_string(char *nvlist, char *name)
1588{
1589 char *nvpair;
1590 char *ret;
1591 size_t slen;
1592 size_t size;
1593 int found;
1594
1595 found = nvlist_find_value(nvlist, name, DATA_TYPE_STRING, &nvpair, &size, 0);
1596 if (!found)
1597 return 0;
1598 if (size < 4) {
1599 printf("invalid string\n");
1600 return 0;
1601 }
1602 slen = be32_to_cpu(*(uint32_t *) nvpair);
1603 if (slen > size - 4)
1604 slen = size - 4;
1605 ret = malloc(slen + 1);
1606 if (!ret)
1607 return 0;
1608 memcpy(ret, nvpair + 4, slen);
1609 ret[slen] = 0;
1610 return ret;
1611}
1612
1613char *
1614zfs_nvlist_lookup_nvlist(char *nvlist, char *name)
1615{
1616 char *nvpair;
1617 char *ret;
1618 size_t size;
1619 int found;
1620
1621 found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
1622 &size, 0);
1623 if (!found)
1624 return 0;
mwleeds@mailtundra.com6bbd7e82024-04-06 18:47:26 -07001625
1626 /* Allocate 12 bytes in addition to the nvlist size: One uint32 before the
1627 * nvlist to hold the encoding method, and two zero uint32's after the
1628 * nvlist as the NULL terminator.
1629 */
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001630 ret = calloc(1, size + 3 * sizeof(uint32_t));
1631 if (!ret)
1632 return 0;
1633 memcpy(ret, nvlist, sizeof(uint32_t));
1634
1635 memcpy(ret + sizeof(uint32_t), nvpair, size);
1636 return ret;
1637}
1638
1639int
1640zfs_nvlist_lookup_nvlist_array_get_nelm(char *nvlist, char *name)
1641{
1642 char *nvpair;
1643 size_t nelm, size;
1644 int found;
1645
1646 found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
1647 &size, &nelm);
1648 if (!found)
1649 return -1;
1650 return nelm;
1651}
1652
1653char *
1654zfs_nvlist_lookup_nvlist_array(char *nvlist, char *name,
1655 size_t index)
1656{
1657 char *nvpair, *nvpairptr;
1658 int found;
1659 char *ret;
1660 size_t size;
1661 unsigned i;
1662 size_t nelm;
1663
1664 found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
1665 &size, &nelm);
1666 if (!found)
1667 return 0;
1668 if (index >= nelm) {
1669 printf("trying to lookup past nvlist array\n");
1670 return 0;
1671 }
1672
1673 nvpairptr = nvpair;
1674
1675 for (i = 0; i < index; i++) {
1676 uint32_t encode_size;
1677
1678 /* skip the header, nvl_version, and nvl_nvflag */
1679 nvpairptr = nvpairptr + 4 * 2;
1680
1681 while (nvpairptr < nvpair + size
1682 && (encode_size = be32_to_cpu(*(uint32_t *) nvpairptr)))
1683 nvlist += encode_size; /* goto the next nvpair */
1684
1685 nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1686 }
1687
1688 if (nvpairptr >= nvpair + size
1689 || nvpairptr + be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2))
1690 >= nvpair + size) {
1691 printf("incorrect nvlist array\n");
1692 return 0;
1693 }
1694
1695 ret = calloc(1, be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2))
1696 + 3 * sizeof(uint32_t));
1697 if (!ret)
1698 return 0;
1699 memcpy(ret, nvlist, sizeof(uint32_t));
1700
1701 memcpy(ret + sizeof(uint32_t), nvpairptr, size);
1702 return ret;
1703}
1704
1705static int
1706int_zfs_fetch_nvlist(struct zfs_data *data, char **nvlist)
1707{
1708 int err;
1709
1710 *nvlist = malloc(VDEV_PHYS_SIZE);
1711 /* Read in the vdev name-value pair list (112K). */
1712 err = zfs_devread(data->vdev_phys_sector, 0, VDEV_PHYS_SIZE, *nvlist);
1713 if (err) {
1714 free(*nvlist);
1715 *nvlist = 0;
1716 return err;
1717 }
1718 return ZFS_ERR_NONE;
1719}
1720
1721/*
1722 * Check the disk label information and retrieve needed vdev name-value pairs.
1723 *
1724 */
1725static int
1726check_pool_label(struct zfs_data *data)
1727{
1728 uint64_t pool_state;
1729 char *nvlist; /* for the pool */
1730 char *vdevnvlist; /* for the vdev */
1731 uint64_t diskguid;
1732 uint64_t version;
1733 int found;
1734 int err;
1735
1736 err = int_zfs_fetch_nvlist(data, &nvlist);
1737 if (err)
1738 return err;
1739
1740 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_STATE,
1741 &pool_state);
1742 if (!found) {
1743 free(nvlist);
1744 printf("zfs pool state not found\n");
1745 return ZFS_ERR_BAD_FS;
1746 }
1747
1748 if (pool_state == POOL_STATE_DESTROYED) {
1749 free(nvlist);
1750 printf("zpool is marked as destroyed\n");
1751 return ZFS_ERR_BAD_FS;
1752 }
1753
1754 data->label_txg = 0;
1755 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_TXG,
1756 &data->label_txg);
1757 if (!found) {
1758 free(nvlist);
1759 printf("zfs pool txg not found\n");
1760 return ZFS_ERR_BAD_FS;
1761 }
1762
1763 /* not an active device */
1764 if (data->label_txg == 0) {
1765 free(nvlist);
1766 printf("zpool is not active\n");
1767 return ZFS_ERR_BAD_FS;
1768 }
1769
1770 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_VERSION,
1771 &version);
1772 if (!found) {
1773 free(nvlist);
1774 printf("zpool config version not found\n");
1775 return ZFS_ERR_BAD_FS;
1776 }
1777
WHRcd85e0d2024-05-01 00:28:32 +08001778 if (!is_supported_spa_version(version)) {
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001779 free(nvlist);
1780 printf("SPA version too new %llu > %llu\n",
1781 (unsigned long long) version,
1782 (unsigned long long) SPA_VERSION);
1783 return ZFS_ERR_NOT_IMPLEMENTED_YET;
1784 }
1785
1786 vdevnvlist = zfs_nvlist_lookup_nvlist(nvlist, ZPOOL_CONFIG_VDEV_TREE);
1787 if (!vdevnvlist) {
1788 free(nvlist);
1789 printf("ZFS config vdev tree not found\n");
1790 return ZFS_ERR_BAD_FS;
1791 }
1792
1793 found = zfs_nvlist_lookup_uint64(vdevnvlist, ZPOOL_CONFIG_ASHIFT,
1794 &data->vdev_ashift);
1795 free(vdevnvlist);
1796 if (!found) {
1797 free(nvlist);
1798 printf("ZPOOL config ashift not found\n");
1799 return ZFS_ERR_BAD_FS;
1800 }
1801
1802 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_GUID, &diskguid);
1803 if (!found) {
1804 free(nvlist);
1805 printf("ZPOOL config guid not found\n");
1806 return ZFS_ERR_BAD_FS;
1807 }
1808
1809 found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_GUID, &data->pool_guid);
1810 if (!found) {
1811 free(nvlist);
1812 printf("ZPOOL config pool guid not found\n");
1813 return ZFS_ERR_BAD_FS;
1814 }
1815
1816 free(nvlist);
1817
1818 printf("ZFS Pool GUID: %llu (%016llx) Label: GUID: %llu (%016llx), txg: %llu, SPA v%llu, ashift: %llu\n",
1819 (unsigned long long) data->pool_guid,
1820 (unsigned long long) data->pool_guid,
1821 (unsigned long long) diskguid,
1822 (unsigned long long) diskguid,
1823 (unsigned long long) data->label_txg,
1824 (unsigned long long) version,
1825 (unsigned long long) data->vdev_ashift);
1826
1827 return ZFS_ERR_NONE;
1828}
1829
1830/*
1831 * vdev_label_start returns the physical disk offset (in bytes) of
1832 * label "l".
1833 */
1834static uint64_t vdev_label_start(uint64_t psize, int l)
1835{
1836 return (l * sizeof(vdev_label_t) + (l < VDEV_LABELS / 2 ?
1837 0 : psize -
1838 VDEV_LABELS * sizeof(vdev_label_t)));
1839}
1840
1841void
1842zfs_unmount(struct zfs_data *data)
1843{
1844 free(data->dnode_buf);
1845 free(data->dnode_mdn);
1846 free(data->file_buf);
1847 free(data);
1848}
1849
1850/*
1851 * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1852 * to the memory address MOS.
1853 *
1854 */
1855struct zfs_data *
1856zfs_mount(device_t dev)
1857{
1858 struct zfs_data *data = 0;
1859 int label = 0, bestlabel = -1;
1860 char *ub_array;
1861 uberblock_t *ubbest;
1862 uberblock_t *ubcur = NULL;
1863 void *osp = 0;
1864 size_t ospsize;
1865 int err;
1866
1867 data = malloc(sizeof(*data));
1868 if (!data)
1869 return 0;
1870 memset(data, 0, sizeof(*data));
1871
1872 ub_array = malloc(VDEV_UBERBLOCK_RING);
1873 if (!ub_array) {
1874 zfs_unmount(data);
1875 return 0;
1876 }
1877
1878 ubbest = malloc(sizeof(*ubbest));
1879 if (!ubbest) {
Jorgen Lundmane183de02014-11-07 10:08:35 +09001880 free(ub_array);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001881 zfs_unmount(data);
1882 return 0;
1883 }
1884 memset(ubbest, 0, sizeof(*ubbest));
1885
1886 /*
1887 * some eltorito stacks don't give us a size and
1888 * we end up setting the size to MAXUINT, further
1889 * some of these devices stop working once a single
1890 * read past the end has been issued. Checking
1891 * for a maximum part_length and skipping the backup
1892 * labels at the end of the slice/partition/device
1893 * avoids breaking down on such devices.
1894 */
1895 const int vdevnum =
1896 dev->part_length == 0 ?
1897 VDEV_LABELS / 2 : VDEV_LABELS;
1898
1899 /* Size in bytes of the device (disk or partition) aligned to label size*/
1900 uint64_t device_size =
1901 dev->part_length << SECTOR_BITS;
1902
1903 const uint64_t alignedbytes =
1904 P2ALIGN(device_size, (uint64_t) sizeof(vdev_label_t));
1905
1906 for (label = 0; label < vdevnum; label++) {
1907 uint64_t labelstartbytes = vdev_label_start(alignedbytes, label);
1908 uint64_t labelstart = labelstartbytes >> SECTOR_BITS;
1909
1910 debug("zfs reading label %d at sector %llu (byte %llu)\n",
1911 label, (unsigned long long) labelstart,
1912 (unsigned long long) labelstartbytes);
1913
1914 data->vdev_phys_sector = labelstart +
1915 ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SECTOR_BITS);
1916
1917 err = check_pool_label(data);
1918 if (err) {
1919 printf("zfs error checking label %d\n", label);
1920 continue;
1921 }
1922
1923 /* Read in the uberblock ring (128K). */
1924 err = zfs_devread(data->vdev_phys_sector +
1925 (VDEV_PHYS_SIZE >> SECTOR_BITS),
1926 0, VDEV_UBERBLOCK_RING, ub_array);
1927 if (err) {
1928 printf("zfs error reading uberblock ring for label %d\n", label);
1929 continue;
1930 }
1931
1932 ubcur = find_bestub(ub_array, data);
1933 if (!ubcur) {
1934 printf("zfs No good uberblocks found in label %d\n", label);
1935 continue;
1936 }
1937
1938 if (vdev_uberblock_compare(ubcur, ubbest) > 0) {
1939 /* Looks like the block is good, so use it.*/
1940 memcpy(ubbest, ubcur, sizeof(*ubbest));
1941 bestlabel = label;
1942 debug("zfs Current best uberblock found in label %d\n", label);
1943 }
1944 }
1945 free(ub_array);
1946
1947 /* We zero'd the structure to begin with. If we never assigned to it,
1948 magic will still be zero. */
1949 if (!ubbest->ub_magic) {
1950 printf("couldn't find a valid ZFS label\n");
1951 zfs_unmount(data);
1952 free(ubbest);
1953 return 0;
1954 }
1955
1956 debug("zfs ubbest %p in label %d\n", ubbest, bestlabel);
1957
1958 zfs_endian_t ub_endian =
1959 zfs_to_cpu64(ubbest->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC
1960 ? LITTLE_ENDIAN : BIG_ENDIAN;
1961
1962 debug("zfs endian set to %s\n", !ub_endian ? "big" : "little");
1963
1964 err = zio_read(&ubbest->ub_rootbp, ub_endian, &osp, &ospsize, data);
1965
1966 if (err) {
1967 printf("couldn't zio_read object directory\n");
1968 zfs_unmount(data);
Jorgen Lundmane183de02014-11-07 10:08:35 +09001969 free(osp);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00001970 free(ubbest);
1971 return 0;
1972 }
1973
1974 if (ospsize < OBJSET_PHYS_SIZE_V14) {
1975 printf("osp too small\n");
1976 zfs_unmount(data);
1977 free(osp);
1978 free(ubbest);
1979 return 0;
1980 }
1981
1982 /* Got the MOS. Save it at the memory addr MOS. */
1983 memmove(&(data->mos.dn), &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE);
1984 data->mos.endian =
1985 (zfs_to_cpu64(ubbest->ub_rootbp.blk_prop, ub_endian) >> 63) & 1;
1986 memmove(&(data->current_uberblock), ubbest, sizeof(uberblock_t));
1987
1988 free(osp);
1989 free(ubbest);
1990
1991 return data;
1992}
1993
1994int
1995zfs_fetch_nvlist(device_t dev, char **nvlist)
1996{
1997 struct zfs_data *zfs;
1998 int err;
1999
2000 zfs = zfs_mount(dev);
2001 if (!zfs)
2002 return ZFS_ERR_BAD_FS;
2003 err = int_zfs_fetch_nvlist(zfs, nvlist);
2004 zfs_unmount(zfs);
2005 return err;
2006}
2007
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002008/*
2009 * zfs_open() locates a file in the rootpool by following the
2010 * MOS and places the dnode of the file in the memory address DNODE.
2011 */
2012int
2013zfs_open(struct zfs_file *file, const char *fsfilename)
2014{
2015 struct zfs_data *data;
2016 int err;
2017 int isfs;
2018
2019 data = zfs_mount(file->device);
2020 if (!data)
2021 return ZFS_ERR_BAD_FS;
2022
2023 err = dnode_get_fullpath(fsfilename, &(data->mdn), 0,
2024 &(data->dnode), &isfs, data);
2025 if (err) {
2026 zfs_unmount(data);
2027 return err;
2028 }
2029
2030 if (isfs) {
2031 zfs_unmount(data);
2032 printf("Missing @ or / separator\n");
2033 return ZFS_ERR_FILE_NOT_FOUND;
2034 }
2035
2036 /* We found the dnode for this file. Verify if it is a plain file. */
2037 if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS) {
2038 zfs_unmount(data);
2039 printf("not a file\n");
2040 return ZFS_ERR_BAD_FILE_TYPE;
2041 }
2042
2043 /* get the file size and set the file position to 0 */
2044
2045 /*
2046 * For DMU_OT_SA we will need to locate the SIZE attribute
2047 * attribute, which could be either in the bonus buffer
2048 * or the "spill" block.
2049 */
2050 if (data->dnode.dn.dn_bonustype == DMU_OT_SA) {
2051 void *sahdrp;
2052 int hdrsize;
2053
2054 if (data->dnode.dn.dn_bonuslen != 0) {
2055 sahdrp = (sa_hdr_phys_t *) DN_BONUS(&data->dnode.dn);
2056 } else if (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
2057 blkptr_t *bp = &data->dnode.dn.dn_spill;
2058
2059 err = zio_read(bp, data->dnode.endian, &sahdrp, NULL, data);
2060 if (err)
2061 return err;
2062 } else {
2063 printf("filesystem is corrupt :(\n");
2064 return ZFS_ERR_BAD_FS;
2065 }
2066
2067 hdrsize = SA_HDR_SIZE(((sa_hdr_phys_t *) sahdrp));
2068 file->size = *(uint64_t *) ((char *) sahdrp + hdrsize + SA_SIZE_OFFSET);
Jorgen Lundmane183de02014-11-07 10:08:35 +09002069 if ((data->dnode.dn.dn_bonuslen == 0) &&
2070 (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2071 free(sahdrp);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002072 } else {
2073 file->size = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&data->dnode.dn))->zp_size, data->dnode.endian);
2074 }
2075
2076 file->data = data;
2077 file->offset = 0;
2078
2079 return ZFS_ERR_NONE;
2080}
2081
2082uint64_t
2083zfs_read(zfs_file_t file, char *buf, uint64_t len)
2084{
2085 struct zfs_data *data = (struct zfs_data *) file->data;
2086 int blksz, movesize;
2087 uint64_t length;
2088 int64_t red;
2089 int err;
2090
2091 if (data->file_buf == NULL) {
2092 data->file_buf = malloc(SPA_MAXBLOCKSIZE);
2093 if (!data->file_buf)
2094 return -1;
2095 data->file_start = data->file_end = 0;
2096 }
2097
2098 /*
2099 * If offset is in memory, move it into the buffer provided and return.
2100 */
2101 if (file->offset >= data->file_start
2102 && file->offset + len <= data->file_end) {
2103 memmove(buf, data->file_buf + file->offset - data->file_start,
2104 len);
2105 return len;
2106 }
2107
2108 blksz = zfs_to_cpu16(data->dnode.dn.dn_datablkszsec,
2109 data->dnode.endian) << SPA_MINBLOCKSHIFT;
2110
2111 /*
2112 * Entire Dnode is too big to fit into the space available. We
2113 * will need to read it in chunks. This could be optimized to
2114 * read in as large a chunk as there is space available, but for
2115 * now, this only reads in one data block at a time.
2116 */
2117 length = len;
2118 red = 0;
2119 while (length) {
2120 void *t;
2121 /*
2122 * Find requested blkid and the offset within that block.
2123 */
Alejandro Mery624c7212012-10-31 08:21:33 +00002124 uint64_t blkid = file->offset + red;
mwleeds@mailtundra.com730c69f2024-04-06 18:47:29 -07002125 uint64_t blkoff = do_div(blkid, blksz);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002126 free(data->file_buf);
2127 data->file_buf = 0;
2128
2129 err = dmu_read(&(data->dnode), blkid, &t,
2130 0, data);
2131 data->file_buf = t;
2132 if (err)
2133 return -1;
2134
2135 data->file_start = blkid * blksz;
2136 data->file_end = data->file_start + blksz;
2137
Masahiro Yamadac79cba32014-09-18 13:28:06 +09002138 movesize = min(length, data->file_end - (int)file->offset - red);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002139
mwleeds@mailtundra.com730c69f2024-04-06 18:47:29 -07002140 memmove(buf, data->file_buf + blkoff, movesize);
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002141 buf += movesize;
2142 length -= movesize;
2143 red += movesize;
2144 }
2145
2146 return len;
2147}
2148
2149int
2150zfs_close(zfs_file_t file)
2151{
2152 zfs_unmount((struct zfs_data *) file->data);
2153 return ZFS_ERR_NONE;
2154}
2155
2156int
2157zfs_getmdnobj(device_t dev, const char *fsfilename,
2158 uint64_t *mdnobj)
2159{
2160 struct zfs_data *data;
2161 int err;
2162 int isfs;
2163
2164 data = zfs_mount(dev);
2165 if (!data)
2166 return ZFS_ERR_BAD_FS;
2167
2168 err = dnode_get_fullpath(fsfilename, &(data->mdn), mdnobj,
2169 &(data->dnode), &isfs, data);
2170 zfs_unmount(data);
2171 return err;
2172}
2173
2174static void
2175fill_fs_info(struct zfs_dirhook_info *info,
2176 dnode_end_t mdn, struct zfs_data *data)
2177{
2178 int err;
2179 dnode_end_t dn;
2180 uint64_t objnum;
2181 uint64_t headobj;
2182
2183 memset(info, 0, sizeof(*info));
2184
2185 info->dir = 1;
2186
2187 if (mdn.dn.dn_type == DMU_OT_DSL_DIR) {
2188 headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&mdn.dn))->dd_head_dataset_obj, mdn.endian);
2189
2190 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &mdn, data);
2191 if (err) {
2192 printf("zfs failed here 1\n");
2193 return;
2194 }
2195 }
2196 make_mdn(&mdn, data);
2197 err = dnode_get(&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
2198 &dn, data);
2199 if (err) {
2200 printf("zfs failed here 2\n");
2201 return;
2202 }
2203
2204 err = zap_lookup(&dn, ZFS_ROOT_OBJ, &objnum, data);
2205 if (err) {
2206 printf("zfs failed here 3\n");
2207 return;
2208 }
2209
2210 err = dnode_get(&mdn, objnum, 0, &dn, data);
2211 if (err) {
2212 printf("zfs failed here 4\n");
2213 return;
2214 }
2215
2216 info->mtimeset = 1;
2217 info->mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian);
2218
2219 return;
2220}
2221
2222static int iterate_zap(const char *name, uint64_t val, struct zfs_data *data)
2223{
2224 struct zfs_dirhook_info info;
2225 dnode_end_t dn;
2226
2227 memset(&info, 0, sizeof(info));
2228
2229 dnode_get(&(data->mdn), val, 0, &dn, data);
2230 info.mtimeset = 1;
2231 info.mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian);
2232 info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS);
2233 debug("zfs type=%d, name=%s\n",
2234 (int)dn.dn.dn_type, (char *)name);
2235 if (!data->userhook)
2236 return 0;
2237 return data->userhook(name, &info);
2238}
2239
2240static int iterate_zap_fs(const char *name, uint64_t val, struct zfs_data *data)
2241{
2242 struct zfs_dirhook_info info;
2243 dnode_end_t mdn;
2244 int err;
2245 err = dnode_get(&(data->mos), val, 0, &mdn, data);
2246 if (err)
2247 return 0;
2248 if (mdn.dn.dn_type != DMU_OT_DSL_DIR)
2249 return 0;
2250
2251 fill_fs_info(&info, mdn, data);
2252
2253 if (!data->userhook)
2254 return 0;
2255 return data->userhook(name, &info);
2256}
2257
2258static int iterate_zap_snap(const char *name, uint64_t val, struct zfs_data *data)
2259{
2260 struct zfs_dirhook_info info;
2261 char *name2;
2262 int ret = 0;
2263 dnode_end_t mdn;
2264 int err;
2265
2266 err = dnode_get(&(data->mos), val, 0, &mdn, data);
2267 if (err)
2268 return 0;
2269
2270 if (mdn.dn.dn_type != DMU_OT_DSL_DATASET)
2271 return 0;
2272
2273 fill_fs_info(&info, mdn, data);
2274
2275 name2 = malloc(strlen(name) + 2);
2276 name2[0] = '@';
2277 memcpy(name2 + 1, name, strlen(name) + 1);
2278 if (data->userhook)
2279 ret = data->userhook(name2, &info);
2280 free(name2);
2281 return ret;
2282}
2283
2284int
2285zfs_ls(device_t device, const char *path,
2286 int (*hook)(const char *, const struct zfs_dirhook_info *))
2287{
2288 struct zfs_data *data;
2289 int err;
2290 int isfs;
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002291
2292 data = zfs_mount(device);
2293 if (!data)
2294 return ZFS_ERR_BAD_FS;
2295
2296 data->userhook = hook;
2297
2298 err = dnode_get_fullpath(path, &(data->mdn), 0, &(data->dnode), &isfs, data);
2299 if (err) {
2300 zfs_unmount(data);
2301 return err;
2302 }
2303 if (isfs) {
2304 uint64_t childobj, headobj;
2305 uint64_t snapobj;
2306 dnode_end_t dn;
2307 struct zfs_dirhook_info info;
2308
2309 fill_fs_info(&info, data->dnode, data);
2310 hook("@", &info);
2311
2312 childobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_child_dir_zapobj, data->dnode.endian);
2313 headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_head_dataset_obj, data->dnode.endian);
2314 err = dnode_get(&(data->mos), childobj,
2315 DMU_OT_DSL_DIR_CHILD_MAP, &dn, data);
2316 if (err) {
2317 zfs_unmount(data);
2318 return err;
2319 }
2320
Jorgen Lundman4d3c95f2012-07-19 20:48:25 +00002321 zap_iterate(&dn, iterate_zap_fs, data);
2322
2323 err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &dn, data);
2324 if (err) {
2325 zfs_unmount(data);
2326 return err;
2327 }
2328
2329 snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&dn.dn))->ds_snapnames_zapobj, dn.endian);
2330
2331 err = dnode_get(&(data->mos), snapobj,
2332 DMU_OT_DSL_DS_SNAP_MAP, &dn, data);
2333 if (err) {
2334 zfs_unmount(data);
2335 return err;
2336 }
2337
2338 zap_iterate(&dn, iterate_zap_snap, data);
2339 } else {
2340 if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) {
2341 zfs_unmount(data);
2342 printf("not a directory\n");
2343 return ZFS_ERR_BAD_FILE_TYPE;
2344 }
2345 zap_iterate(&(data->dnode), iterate_zap, data);
2346 }
2347 zfs_unmount(data);
2348 return ZFS_ERR_NONE;
2349}