]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/boot/zfs/zfsimpl.c
MFC r234860 (by kientzle):
[FreeBSD/stable/9.git] / sys / boot / zfs / zfsimpl.c
1 /*-
2  * Copyright (c) 2007 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  *      Stand-alone ZFS file reader.
32  */
33
34 #include <sys/stat.h>
35 #include <sys/stdint.h>
36
37 #include "zfsimpl.h"
38 #include "zfssubr.c"
39
40
41 struct zfsmount {
42         const spa_t     *spa;
43         objset_phys_t   objset;
44         uint64_t        rootobj;
45 };
46
47 /*
48  * List of all vdevs, chained through v_alllink.
49  */
50 static vdev_list_t zfs_vdevs;
51
52 /*
53  * List of all pools, chained through spa_link.
54  */
55 static spa_list_t zfs_pools;
56
57 static uint64_t zfs_crc64_table[256];
58 static const dnode_phys_t *dnode_cache_obj = 0;
59 static uint64_t dnode_cache_bn;
60 static char *dnode_cache_buf;
61 static char *zap_scratch;
62 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
63
64 #define TEMP_SIZE       (1024 * 1024)
65
66 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
67 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
68 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
69
70 static void
71 zfs_init(void)
72 {
73         STAILQ_INIT(&zfs_vdevs);
74         STAILQ_INIT(&zfs_pools);
75
76         zfs_temp_buf = malloc(TEMP_SIZE);
77         zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
78         zfs_temp_ptr = zfs_temp_buf;
79         dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
80         zap_scratch = malloc(SPA_MAXBLOCKSIZE);
81
82         zfs_init_crc();
83 }
84
85 static void *
86 zfs_alloc(size_t size)
87 {
88         char *ptr;
89
90         if (zfs_temp_ptr + size > zfs_temp_end) {
91                 printf("ZFS: out of temporary buffer space\n");
92                 for (;;) ;
93         }
94         ptr = zfs_temp_ptr;
95         zfs_temp_ptr += size;
96
97         return (ptr);
98 }
99
100 static void
101 zfs_free(void *ptr, size_t size)
102 {
103
104         zfs_temp_ptr -= size;
105         if (zfs_temp_ptr != ptr) {
106                 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
107                 for (;;) ;
108         }
109 }
110
111 static int
112 xdr_int(const unsigned char **xdr, int *ip)
113 {
114         *ip = ((*xdr)[0] << 24)
115                 | ((*xdr)[1] << 16)
116                 | ((*xdr)[2] << 8)
117                 | ((*xdr)[3] << 0);
118         (*xdr) += 4;
119         return (0);
120 }
121
122 static int
123 xdr_u_int(const unsigned char **xdr, u_int *ip)
124 {
125         *ip = ((*xdr)[0] << 24)
126                 | ((*xdr)[1] << 16)
127                 | ((*xdr)[2] << 8)
128                 | ((*xdr)[3] << 0);
129         (*xdr) += 4;
130         return (0);
131 }
132
133 static int
134 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
135 {
136         u_int hi, lo;
137
138         xdr_u_int(xdr, &hi);
139         xdr_u_int(xdr, &lo);
140         *lp = (((uint64_t) hi) << 32) | lo;
141         return (0);
142 }
143
144 static int
145 nvlist_find(const unsigned char *nvlist, const char *name, int type,
146             int* elementsp, void *valuep)
147 {
148         const unsigned char *p, *pair;
149         int junk;
150         int encoded_size, decoded_size;
151
152         p = nvlist;
153         xdr_int(&p, &junk);
154         xdr_int(&p, &junk);
155
156         pair = p;
157         xdr_int(&p, &encoded_size);
158         xdr_int(&p, &decoded_size);
159         while (encoded_size && decoded_size) {
160                 int namelen, pairtype, elements;
161                 const char *pairname;
162
163                 xdr_int(&p, &namelen);
164                 pairname = (const char*) p;
165                 p += roundup(namelen, 4);
166                 xdr_int(&p, &pairtype);
167
168                 if (!memcmp(name, pairname, namelen) && type == pairtype) {
169                         xdr_int(&p, &elements);
170                         if (elementsp)
171                                 *elementsp = elements;
172                         if (type == DATA_TYPE_UINT64) {
173                                 xdr_uint64_t(&p, (uint64_t *) valuep);
174                                 return (0);
175                         } else if (type == DATA_TYPE_STRING) {
176                                 int len;
177                                 xdr_int(&p, &len);
178                                 (*(const char**) valuep) = (const char*) p;
179                                 return (0);
180                         } else if (type == DATA_TYPE_NVLIST
181                                    || type == DATA_TYPE_NVLIST_ARRAY) {
182                                 (*(const unsigned char**) valuep) =
183                                          (const unsigned char*) p;
184                                 return (0);
185                         } else {
186                                 return (EIO);
187                         }
188                 } else {
189                         /*
190                          * Not the pair we are looking for, skip to the next one.
191                          */
192                         p = pair + encoded_size;
193                 }
194
195                 pair = p;
196                 xdr_int(&p, &encoded_size);
197                 xdr_int(&p, &decoded_size);
198         }
199
200         return (EIO);
201 }
202
203 /*
204  * Return the next nvlist in an nvlist array.
205  */
206 static const unsigned char *
207 nvlist_next(const unsigned char *nvlist)
208 {
209         const unsigned char *p, *pair;
210         int junk;
211         int encoded_size, decoded_size;
212
213         p = nvlist;
214         xdr_int(&p, &junk);
215         xdr_int(&p, &junk);
216
217         pair = p;
218         xdr_int(&p, &encoded_size);
219         xdr_int(&p, &decoded_size);
220         while (encoded_size && decoded_size) {
221                 p = pair + encoded_size;
222
223                 pair = p;
224                 xdr_int(&p, &encoded_size);
225                 xdr_int(&p, &decoded_size);
226         }
227
228         return p;
229 }
230
231 #ifdef TEST
232
233 static const unsigned char *
234 nvlist_print(const unsigned char *nvlist, unsigned int indent)
235 {
236         static const char* typenames[] = {
237                 "DATA_TYPE_UNKNOWN",
238                 "DATA_TYPE_BOOLEAN",
239                 "DATA_TYPE_BYTE",
240                 "DATA_TYPE_INT16",
241                 "DATA_TYPE_UINT16",
242                 "DATA_TYPE_INT32",
243                 "DATA_TYPE_UINT32",
244                 "DATA_TYPE_INT64",
245                 "DATA_TYPE_UINT64",
246                 "DATA_TYPE_STRING",
247                 "DATA_TYPE_BYTE_ARRAY",
248                 "DATA_TYPE_INT16_ARRAY",
249                 "DATA_TYPE_UINT16_ARRAY",
250                 "DATA_TYPE_INT32_ARRAY",
251                 "DATA_TYPE_UINT32_ARRAY",
252                 "DATA_TYPE_INT64_ARRAY",
253                 "DATA_TYPE_UINT64_ARRAY",
254                 "DATA_TYPE_STRING_ARRAY",
255                 "DATA_TYPE_HRTIME",
256                 "DATA_TYPE_NVLIST",
257                 "DATA_TYPE_NVLIST_ARRAY",
258                 "DATA_TYPE_BOOLEAN_VALUE",
259                 "DATA_TYPE_INT8",
260                 "DATA_TYPE_UINT8",
261                 "DATA_TYPE_BOOLEAN_ARRAY",
262                 "DATA_TYPE_INT8_ARRAY",
263                 "DATA_TYPE_UINT8_ARRAY"
264         };
265
266         unsigned int i, j;
267         const unsigned char *p, *pair;
268         int junk;
269         int encoded_size, decoded_size;
270
271         p = nvlist;
272         xdr_int(&p, &junk);
273         xdr_int(&p, &junk);
274
275         pair = p;
276         xdr_int(&p, &encoded_size);
277         xdr_int(&p, &decoded_size);
278         while (encoded_size && decoded_size) {
279                 int namelen, pairtype, elements;
280                 const char *pairname;
281
282                 xdr_int(&p, &namelen);
283                 pairname = (const char*) p;
284                 p += roundup(namelen, 4);
285                 xdr_int(&p, &pairtype);
286
287                 for (i = 0; i < indent; i++)
288                         printf(" ");
289                 printf("%s %s", typenames[pairtype], pairname);
290
291                 xdr_int(&p, &elements);
292                 switch (pairtype) {
293                 case DATA_TYPE_UINT64: {
294                         uint64_t val;
295                         xdr_uint64_t(&p, &val);
296                         printf(" = 0x%jx\n", (uintmax_t)val);
297                         break;
298                 }
299
300                 case DATA_TYPE_STRING: {
301                         int len;
302                         xdr_int(&p, &len);
303                         printf(" = \"%s\"\n", p);
304                         break;
305                 }
306
307                 case DATA_TYPE_NVLIST:
308                         printf("\n");
309                         nvlist_print(p, indent + 1);
310                         break;
311
312                 case DATA_TYPE_NVLIST_ARRAY:
313                         for (j = 0; j < elements; j++) {
314                                 printf("[%d]\n", j);
315                                 p = nvlist_print(p, indent + 1);
316                                 if (j != elements - 1) {
317                                         for (i = 0; i < indent; i++)
318                                                 printf(" ");
319                                         printf("%s %s", typenames[pairtype], pairname);
320                                 }
321                         }
322                         break;
323
324                 default:
325                         printf("\n");
326                 }
327
328                 p = pair + encoded_size;
329
330                 pair = p;
331                 xdr_int(&p, &encoded_size);
332                 xdr_int(&p, &decoded_size);
333         }
334
335         return p;
336 }
337
338 #endif
339
340 static int
341 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
342     off_t offset, size_t size)
343 {
344         size_t psize;
345         int rc;
346
347         if (!vdev->v_phys_read)
348                 return (EIO);
349
350         if (bp) {
351                 psize = BP_GET_PSIZE(bp);
352         } else {
353                 psize = size;
354         }
355
356         /*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
357         rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
358         if (rc)
359                 return (rc);
360         if (bp && zio_checksum_verify(bp, buf))
361                 return (EIO);
362
363         return (0);
364 }
365
366 static int
367 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
368     off_t offset, size_t bytes)
369 {
370
371         return (vdev_read_phys(vdev, bp, buf,
372                 offset + VDEV_LABEL_START_SIZE, bytes));
373 }
374
375
376 static int
377 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
378     off_t offset, size_t bytes)
379 {
380         vdev_t *kid;
381         int rc;
382
383         rc = EIO;
384         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
385                 if (kid->v_state != VDEV_STATE_HEALTHY)
386                         continue;
387                 rc = kid->v_read(kid, bp, buf, offset, bytes);
388                 if (!rc)
389                         return (0);
390         }
391
392         return (rc);
393 }
394
395 static int
396 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
397     off_t offset, size_t bytes)
398 {
399         vdev_t *kid;
400
401         /*
402          * Here we should have two kids:
403          * First one which is the one we are replacing and we can trust
404          * only this one to have valid data, but it might not be present.
405          * Second one is that one we are replacing with. It is most likely
406          * healthy, but we can't trust it has needed data, so we won't use it.
407          */
408         kid = STAILQ_FIRST(&vdev->v_children);
409         if (kid == NULL)
410                 return (EIO);
411         if (kid->v_state != VDEV_STATE_HEALTHY)
412                 return (EIO);
413         return (kid->v_read(kid, bp, buf, offset, bytes));
414 }
415
416 static vdev_t *
417 vdev_find(uint64_t guid)
418 {
419         vdev_t *vdev;
420
421         STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
422                 if (vdev->v_guid == guid)
423                         return (vdev);
424
425         return (0);
426 }
427
428 static vdev_t *
429 vdev_create(uint64_t guid, vdev_read_t *read)
430 {
431         vdev_t *vdev;
432
433         vdev = malloc(sizeof(vdev_t));
434         memset(vdev, 0, sizeof(vdev_t));
435         STAILQ_INIT(&vdev->v_children);
436         vdev->v_guid = guid;
437         vdev->v_state = VDEV_STATE_OFFLINE;
438         vdev->v_read = read;
439         vdev->v_phys_read = 0;
440         vdev->v_read_priv = 0;
441         STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
442
443         return (vdev);
444 }
445
446 static int
447 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
448     vdev_t **vdevp, int is_newer)
449 {
450         int rc;
451         uint64_t guid, id, ashift, nparity;
452         const char *type;
453         const char *path;
454         vdev_t *vdev, *kid;
455         const unsigned char *kids;
456         int nkids, i, is_new;
457         uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
458
459         if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
460                         DATA_TYPE_UINT64, 0, &guid)
461             || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
462                            DATA_TYPE_UINT64, 0, &id)
463             || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
464                            DATA_TYPE_STRING, 0, &type)) {
465                 printf("ZFS: can't find vdev details\n");
466                 return (ENOENT);
467         }
468
469         if (strcmp(type, VDEV_TYPE_MIRROR)
470             && strcmp(type, VDEV_TYPE_DISK)
471 #ifdef ZFS_TEST
472             && strcmp(type, VDEV_TYPE_FILE)
473 #endif
474             && strcmp(type, VDEV_TYPE_RAIDZ)
475             && strcmp(type, VDEV_TYPE_REPLACING)) {
476                 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
477                 return (EIO);
478         }
479
480         is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
481
482         nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
483                         &is_offline);
484         nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
485                         &is_removed);
486         nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
487                         &is_faulted);
488         nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
489                         &is_degraded);
490         nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
491                         &isnt_present);
492
493         vdev = vdev_find(guid);
494         if (!vdev) {
495                 is_new = 1;
496
497                 if (!strcmp(type, VDEV_TYPE_MIRROR))
498                         vdev = vdev_create(guid, vdev_mirror_read);
499                 else if (!strcmp(type, VDEV_TYPE_RAIDZ))
500                         vdev = vdev_create(guid, vdev_raidz_read);
501                 else if (!strcmp(type, VDEV_TYPE_REPLACING))
502                         vdev = vdev_create(guid, vdev_replacing_read);
503                 else
504                         vdev = vdev_create(guid, vdev_disk_read);
505
506                 vdev->v_id = id;
507                 vdev->v_top = pvdev != NULL ? pvdev : vdev;
508                 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
509                         DATA_TYPE_UINT64, 0, &ashift) == 0)
510                         vdev->v_ashift = ashift;
511                 else
512                         vdev->v_ashift = 0;
513                 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
514                         DATA_TYPE_UINT64, 0, &nparity) == 0)
515                         vdev->v_nparity = nparity;
516                 else
517                         vdev->v_nparity = 0;
518                 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
519                                 DATA_TYPE_STRING, 0, &path) == 0) {
520                         if (strncmp(path, "/dev/", 5) == 0)
521                                 path += 5;
522                         vdev->v_name = strdup(path);
523                 } else {
524                         if (!strcmp(type, "raidz")) {
525                                 if (vdev->v_nparity == 1)
526                                         vdev->v_name = "raidz1";
527                                 else if (vdev->v_nparity == 2)
528                                         vdev->v_name = "raidz2";
529                                 else if (vdev->v_nparity == 3)
530                                         vdev->v_name = "raidz3";
531                                 else {
532                                         printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
533                                         return (EIO);
534                                 }
535                         } else {
536                                 vdev->v_name = strdup(type);
537                         }
538                 }
539         } else {
540                 is_new = 0;
541         }
542
543         if (is_new || is_newer) {
544                 /*
545                  * This is either new vdev or we've already seen this vdev,
546                  * but from an older vdev label, so let's refresh its state
547                  * from the newer label.
548                  */
549                 if (is_offline)
550                         vdev->v_state = VDEV_STATE_OFFLINE;
551                 else if (is_removed)
552                         vdev->v_state = VDEV_STATE_REMOVED;
553                 else if (is_faulted)
554                         vdev->v_state = VDEV_STATE_FAULTED;
555                 else if (is_degraded)
556                         vdev->v_state = VDEV_STATE_DEGRADED;
557                 else if (isnt_present)
558                         vdev->v_state = VDEV_STATE_CANT_OPEN;
559         }
560
561         rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
562                          DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
563         /*
564          * Its ok if we don't have any kids.
565          */
566         if (rc == 0) {
567                 vdev->v_nchildren = nkids;
568                 for (i = 0; i < nkids; i++) {
569                         rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
570                         if (rc)
571                                 return (rc);
572                         if (is_new)
573                                 STAILQ_INSERT_TAIL(&vdev->v_children, kid,
574                                                    v_childlink);
575                         kids = nvlist_next(kids);
576                 }
577         } else {
578                 vdev->v_nchildren = 0;
579         }
580
581         if (vdevp)
582                 *vdevp = vdev;
583         return (0);
584 }
585
586 static void
587 vdev_set_state(vdev_t *vdev)
588 {
589         vdev_t *kid;
590         int good_kids;
591         int bad_kids;
592
593         /*
594          * A mirror or raidz is healthy if all its kids are healthy. A
595          * mirror is degraded if any of its kids is healthy; a raidz
596          * is degraded if at most nparity kids are offline.
597          */
598         if (STAILQ_FIRST(&vdev->v_children)) {
599                 good_kids = 0;
600                 bad_kids = 0;
601                 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
602                         if (kid->v_state == VDEV_STATE_HEALTHY)
603                                 good_kids++;
604                         else
605                                 bad_kids++;
606                 }
607                 if (bad_kids == 0) {
608                         vdev->v_state = VDEV_STATE_HEALTHY;
609                 } else {
610                         if (vdev->v_read == vdev_mirror_read) {
611                                 if (good_kids) {
612                                         vdev->v_state = VDEV_STATE_DEGRADED;
613                                 } else {
614                                         vdev->v_state = VDEV_STATE_OFFLINE;
615                                 }
616                         } else if (vdev->v_read == vdev_raidz_read) {
617                                 if (bad_kids > vdev->v_nparity) {
618                                         vdev->v_state = VDEV_STATE_OFFLINE;
619                                 } else {
620                                         vdev->v_state = VDEV_STATE_DEGRADED;
621                                 }
622                         }
623                 }
624         }
625 }
626
627 static spa_t *
628 spa_find_by_guid(uint64_t guid)
629 {
630         spa_t *spa;
631
632         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
633                 if (spa->spa_guid == guid)
634                         return (spa);
635
636         return (0);
637 }
638
639 static spa_t *
640 spa_find_by_name(const char *name)
641 {
642         spa_t *spa;
643
644         STAILQ_FOREACH(spa, &zfs_pools, spa_link)
645                 if (!strcmp(spa->spa_name, name))
646                         return (spa);
647
648         return (0);
649 }
650
651 #ifdef BOOT2
652 static spa_t *
653 spa_get_primary(void)
654 {
655
656         return (STAILQ_FIRST(&zfs_pools));
657 }
658
659 static vdev_t *
660 spa_get_primary_vdev(const spa_t *spa)
661 {
662         vdev_t *vdev;
663         vdev_t *kid;
664
665         if (spa == NULL)
666                 spa = spa_get_primary();
667         if (spa == NULL)
668                 return (NULL);
669         vdev = STAILQ_FIRST(&spa->spa_vdevs);
670         if (vdev == NULL)
671                 return (NULL);
672         for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
673              kid = STAILQ_FIRST(&vdev->v_children))
674                 vdev = kid;
675         return (vdev);
676 }
677 #endif
678
679 static spa_t *
680 spa_create(uint64_t guid)
681 {
682         spa_t *spa;
683
684         spa = malloc(sizeof(spa_t));
685         memset(spa, 0, sizeof(spa_t));
686         STAILQ_INIT(&spa->spa_vdevs);
687         spa->spa_guid = guid;
688         STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
689
690         return (spa);
691 }
692
693 static const char *
694 state_name(vdev_state_t state)
695 {
696         static const char* names[] = {
697                 "UNKNOWN",
698                 "CLOSED",
699                 "OFFLINE",
700                 "REMOVED",
701                 "CANT_OPEN",
702                 "FAULTED",
703                 "DEGRADED",
704                 "ONLINE"
705         };
706         return names[state];
707 }
708
709 #ifdef BOOT2
710
711 #define pager_printf printf
712
713 #else
714
715 static void
716 pager_printf(const char *fmt, ...)
717 {
718         char line[80];
719         va_list args;
720
721         va_start(args, fmt);
722         vsprintf(line, fmt, args);
723         va_end(args);
724         pager_output(line);
725 }
726
727 #endif
728
729 #define STATUS_FORMAT   "        %s %s\n"
730
731 static void
732 print_state(int indent, const char *name, vdev_state_t state)
733 {
734         int i;
735         char buf[512];
736
737         buf[0] = 0;
738         for (i = 0; i < indent; i++)
739                 strcat(buf, "  ");
740         strcat(buf, name);
741         pager_printf(STATUS_FORMAT, buf, state_name(state));
742         
743 }
744
745 static void
746 vdev_status(vdev_t *vdev, int indent)
747 {
748         vdev_t *kid;
749         print_state(indent, vdev->v_name, vdev->v_state);
750
751         STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
752                 vdev_status(kid, indent + 1);
753         }
754 }
755
756 static void
757 spa_status(spa_t *spa)
758 {
759         static char bootfs[ZFS_MAXNAMELEN];
760         uint64_t rootid;
761         vdev_t *vdev;
762         int good_kids, bad_kids, degraded_kids;
763         vdev_state_t state;
764
765         pager_printf("  pool: %s\n", spa->spa_name);
766         if (zfs_get_root(spa, &rootid) == 0 &&
767             zfs_rlookup(spa, rootid, bootfs) == 0) {
768                 if (bootfs[0] == '\0')
769                         pager_printf("bootfs: %s\n", spa->spa_name);
770                 else
771                         pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
772         }
773         pager_printf("config:\n\n");
774         pager_printf(STATUS_FORMAT, "NAME", "STATE");
775
776         good_kids = 0;
777         degraded_kids = 0;
778         bad_kids = 0;
779         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
780                 if (vdev->v_state == VDEV_STATE_HEALTHY)
781                         good_kids++;
782                 else if (vdev->v_state == VDEV_STATE_DEGRADED)
783                         degraded_kids++;
784                 else
785                         bad_kids++;
786         }
787
788         state = VDEV_STATE_CLOSED;
789         if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
790                 state = VDEV_STATE_HEALTHY;
791         else if ((good_kids + degraded_kids) > 0)
792                 state = VDEV_STATE_DEGRADED;
793
794         print_state(0, spa->spa_name, state);
795         STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
796                 vdev_status(vdev, 1);
797         }
798 }
799
800 static void
801 spa_all_status(void)
802 {
803         spa_t *spa;
804         int first = 1;
805
806         STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
807                 if (!first)
808                         pager_printf("\n");
809                 first = 0;
810                 spa_status(spa);
811         }
812 }
813
814 static int
815 vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
816 {
817         vdev_t vtmp;
818         vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
819         spa_t *spa;
820         vdev_t *vdev, *top_vdev, *pool_vdev;
821         off_t off;
822         blkptr_t bp;
823         const unsigned char *nvlist;
824         uint64_t val;
825         uint64_t guid;
826         uint64_t pool_txg, pool_guid;
827         uint64_t is_log;
828         const char *pool_name;
829         const unsigned char *vdevs;
830         int i, rc, is_newer;
831         char *upbuf;
832         const struct uberblock *up;
833
834         /*
835          * Load the vdev label and figure out which
836          * uberblock is most current.
837          */
838         memset(&vtmp, 0, sizeof(vtmp));
839         vtmp.v_phys_read = read;
840         vtmp.v_read_priv = read_priv;
841         off = offsetof(vdev_label_t, vl_vdev_phys);
842         BP_ZERO(&bp);
843         BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
844         BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
845         BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
846         BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
847         DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
848         ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
849         if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
850                 return (EIO);
851
852         if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
853                 return (EIO);
854         }
855
856         nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
857
858         if (nvlist_find(nvlist,
859                         ZPOOL_CONFIG_VERSION,
860                         DATA_TYPE_UINT64, 0, &val)) {
861                 return (EIO);
862         }
863
864         if (val > SPA_VERSION) {
865                 printf("ZFS: unsupported ZFS version %u (should be %u)\n",
866                     (unsigned) val, (unsigned) SPA_VERSION);
867                 return (EIO);
868         }
869
870         if (nvlist_find(nvlist,
871                         ZPOOL_CONFIG_POOL_STATE,
872                         DATA_TYPE_UINT64, 0, &val)) {
873                 return (EIO);
874         }
875
876         if (val == POOL_STATE_DESTROYED) {
877                 /* We don't boot only from destroyed pools. */
878                 return (EIO);
879         }
880
881         if (nvlist_find(nvlist,
882                         ZPOOL_CONFIG_POOL_TXG,
883                         DATA_TYPE_UINT64, 0, &pool_txg)
884             || nvlist_find(nvlist,
885                            ZPOOL_CONFIG_POOL_GUID,
886                            DATA_TYPE_UINT64, 0, &pool_guid)
887             || nvlist_find(nvlist,
888                            ZPOOL_CONFIG_POOL_NAME,
889                            DATA_TYPE_STRING, 0, &pool_name)) {
890                 /*
891                  * Cache and spare devices end up here - just ignore
892                  * them.
893                  */
894                 /*printf("ZFS: can't find pool details\n");*/
895                 return (EIO);
896         }
897
898         is_log = 0;
899         (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
900             &is_log);
901         if (is_log)
902                 return (EIO);
903
904         /*
905          * Create the pool if this is the first time we've seen it.
906          */
907         spa = spa_find_by_guid(pool_guid);
908         if (!spa) {
909                 spa = spa_create(pool_guid);
910                 spa->spa_name = strdup(pool_name);
911         }
912         if (pool_txg > spa->spa_txg) {
913                 spa->spa_txg = pool_txg;
914                 is_newer = 1;
915         } else
916                 is_newer = 0;
917
918         /*
919          * Get the vdev tree and create our in-core copy of it.
920          * If we already have a vdev with this guid, this must
921          * be some kind of alias (overlapping slices, dangerously dedicated
922          * disks etc).
923          */
924         if (nvlist_find(nvlist,
925                         ZPOOL_CONFIG_GUID,
926                         DATA_TYPE_UINT64, 0, &guid)) {
927                 return (EIO);
928         }
929         vdev = vdev_find(guid);
930         if (vdev && vdev->v_phys_read)  /* Has this vdev already been inited? */
931                 return (EIO);
932
933         if (nvlist_find(nvlist,
934                         ZPOOL_CONFIG_VDEV_TREE,
935                         DATA_TYPE_NVLIST, 0, &vdevs)) {
936                 return (EIO);
937         }
938
939         rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
940         if (rc)
941                 return (rc);
942
943         /*
944          * Add the toplevel vdev to the pool if its not already there.
945          */
946         STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
947                 if (top_vdev == pool_vdev)
948                         break;
949         if (!pool_vdev && top_vdev)
950                 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
951
952         /*
953          * We should already have created an incomplete vdev for this
954          * vdev. Find it and initialise it with our read proc.
955          */
956         vdev = vdev_find(guid);
957         if (vdev) {
958                 vdev->v_phys_read = read;
959                 vdev->v_read_priv = read_priv;
960                 vdev->v_state = VDEV_STATE_HEALTHY;
961         } else {
962                 printf("ZFS: inconsistent nvlist contents\n");
963                 return (EIO);
964         }
965
966         /*
967          * Re-evaluate top-level vdev state.
968          */
969         vdev_set_state(top_vdev);
970
971         /*
972          * Ok, we are happy with the pool so far. Lets find
973          * the best uberblock and then we can actually access
974          * the contents of the pool.
975          */
976         upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
977         up = (const struct uberblock *)upbuf;
978         for (i = 0;
979              i < VDEV_UBERBLOCK_COUNT(vdev);
980              i++) {
981                 off = VDEV_UBERBLOCK_OFFSET(vdev, i);
982                 BP_ZERO(&bp);
983                 DVA_SET_OFFSET(&bp.blk_dva[0], off);
984                 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
985                 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
986                 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
987                 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
988                 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
989
990                 if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
991                         continue;
992
993                 if (up->ub_magic != UBERBLOCK_MAGIC)
994                         continue;
995                 if (up->ub_txg < spa->spa_txg)
996                         continue;
997                 if (up->ub_txg > spa->spa_uberblock.ub_txg) {
998                         spa->spa_uberblock = *up;
999                 } else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1000                         if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1001                                 spa->spa_uberblock = *up;
1002                 }
1003         }
1004         zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1005
1006         if (spap)
1007                 *spap = spa;
1008         return (0);
1009 }
1010
1011 static int
1012 ilog2(int n)
1013 {
1014         int v;
1015
1016         for (v = 0; v < 32; v++)
1017                 if (n == (1 << v))
1018                         return v;
1019         return -1;
1020 }
1021
1022 static int
1023 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1024 {
1025         blkptr_t gbh_bp;
1026         zio_gbh_phys_t zio_gb;
1027         char *pbuf;
1028         int i;
1029
1030         /* Artificial BP for gang block header. */
1031         gbh_bp = *bp;
1032         BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1033         BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1034         BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1035         BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1036         for (i = 0; i < SPA_DVAS_PER_BP; i++)
1037                 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1038
1039         /* Read gang header block using the artificial BP. */
1040         if (zio_read(spa, &gbh_bp, &zio_gb))
1041                 return (EIO);
1042
1043         pbuf = buf;
1044         for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1045                 blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1046
1047                 if (BP_IS_HOLE(gbp))
1048                         continue;
1049                 if (zio_read(spa, gbp, pbuf))
1050                         return (EIO);
1051                 pbuf += BP_GET_PSIZE(gbp);
1052         }
1053
1054         if (zio_checksum_verify(bp, buf))
1055                 return (EIO);
1056         return (0);
1057 }
1058
1059 static int
1060 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1061 {
1062         int cpfunc = BP_GET_COMPRESS(bp);
1063         uint64_t align, size;
1064         void *pbuf;
1065         int i, error;
1066
1067         error = EIO;
1068
1069         for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1070                 const dva_t *dva = &bp->blk_dva[i];
1071                 vdev_t *vdev;
1072                 int vdevid;
1073                 off_t offset;
1074
1075                 if (!dva->dva_word[0] && !dva->dva_word[1])
1076                         continue;
1077
1078                 vdevid = DVA_GET_VDEV(dva);
1079                 offset = DVA_GET_OFFSET(dva);
1080                 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1081                         if (vdev->v_id == vdevid)
1082                                 break;
1083                 }
1084                 if (!vdev || !vdev->v_read)
1085                         continue;
1086
1087                 size = BP_GET_PSIZE(bp);
1088                 if (vdev->v_read == vdev_raidz_read) {
1089                         align = 1ULL << vdev->v_top->v_ashift;
1090                         if (P2PHASE(size, align) != 0)
1091                                 size = P2ROUNDUP(size, align);
1092                 }
1093                 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1094                         pbuf = zfs_alloc(size);
1095                 else
1096                         pbuf = buf;
1097
1098                 if (DVA_GET_GANG(dva))
1099                         error = zio_read_gang(spa, bp, pbuf);
1100                 else
1101                         error = vdev->v_read(vdev, bp, pbuf, offset, size);
1102                 if (error == 0) {
1103                         if (cpfunc != ZIO_COMPRESS_OFF)
1104                                 error = zio_decompress_data(cpfunc, pbuf,
1105                                     BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1106                         else if (size != BP_GET_PSIZE(bp))
1107                                 bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1108                 }
1109                 if (buf != pbuf)
1110                         zfs_free(pbuf, size);
1111                 if (error == 0)
1112                         break;
1113         }
1114         if (error != 0)
1115                 printf("ZFS: i/o error - all block copies unavailable\n");
1116         return (error);
1117 }
1118
1119 static int
1120 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1121 {
1122         int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1123         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1124         int nlevels = dnode->dn_nlevels;
1125         int i, rc;
1126
1127         /*
1128          * Note: bsize may not be a power of two here so we need to do an
1129          * actual divide rather than a bitshift.
1130          */
1131         while (buflen > 0) {
1132                 uint64_t bn = offset / bsize;
1133                 int boff = offset % bsize;
1134                 int ibn;
1135                 const blkptr_t *indbp;
1136                 blkptr_t bp;
1137
1138                 if (bn > dnode->dn_maxblkid)
1139                         return (EIO);
1140
1141                 if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1142                         goto cached;
1143
1144                 indbp = dnode->dn_blkptr;
1145                 for (i = 0; i < nlevels; i++) {
1146                         /*
1147                          * Copy the bp from the indirect array so that
1148                          * we can re-use the scratch buffer for multi-level
1149                          * objects.
1150                          */
1151                         ibn = bn >> ((nlevels - i - 1) * ibshift);
1152                         ibn &= ((1 << ibshift) - 1);
1153                         bp = indbp[ibn];
1154                         rc = zio_read(spa, &bp, dnode_cache_buf);
1155                         if (rc)
1156                                 return (rc);
1157                         indbp = (const blkptr_t *) dnode_cache_buf;
1158                 }
1159                 dnode_cache_obj = dnode;
1160                 dnode_cache_bn = bn;
1161         cached:
1162
1163                 /*
1164                  * The buffer contains our data block. Copy what we
1165                  * need from it and loop.
1166                  */ 
1167                 i = bsize - boff;
1168                 if (i > buflen) i = buflen;
1169                 memcpy(buf, &dnode_cache_buf[boff], i);
1170                 buf = ((char*) buf) + i;
1171                 offset += i;
1172                 buflen -= i;
1173         }
1174
1175         return (0);
1176 }
1177
1178 /*
1179  * Lookup a value in a microzap directory. Assumes that the zap
1180  * scratch buffer contains the directory contents.
1181  */
1182 static int
1183 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1184 {
1185         const mzap_phys_t *mz;
1186         const mzap_ent_phys_t *mze;
1187         size_t size;
1188         int chunks, i;
1189
1190         /*
1191          * Microzap objects use exactly one block. Read the whole
1192          * thing.
1193          */
1194         size = dnode->dn_datablkszsec * 512;
1195
1196         mz = (const mzap_phys_t *) zap_scratch;
1197         chunks = size / MZAP_ENT_LEN - 1;
1198
1199         for (i = 0; i < chunks; i++) {
1200                 mze = &mz->mz_chunk[i];
1201                 if (!strcmp(mze->mze_name, name)) {
1202                         *value = mze->mze_value;
1203                         return (0);
1204                 }
1205         }
1206
1207         return (ENOENT);
1208 }
1209
1210 /*
1211  * Compare a name with a zap leaf entry. Return non-zero if the name
1212  * matches.
1213  */
1214 static int
1215 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1216 {
1217         size_t namelen;
1218         const zap_leaf_chunk_t *nc;
1219         const char *p;
1220
1221         namelen = zc->l_entry.le_name_numints;
1222                         
1223         nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1224         p = name;
1225         while (namelen > 0) {
1226                 size_t len;
1227                 len = namelen;
1228                 if (len > ZAP_LEAF_ARRAY_BYTES)
1229                         len = ZAP_LEAF_ARRAY_BYTES;
1230                 if (memcmp(p, nc->l_array.la_array, len))
1231                         return (0);
1232                 p += len;
1233                 namelen -= len;
1234                 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1235         }
1236
1237         return 1;
1238 }
1239
1240 /*
1241  * Extract a uint64_t value from a zap leaf entry.
1242  */
1243 static uint64_t
1244 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1245 {
1246         const zap_leaf_chunk_t *vc;
1247         int i;
1248         uint64_t value;
1249         const uint8_t *p;
1250
1251         vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1252         for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1253                 value = (value << 8) | p[i];
1254         }
1255
1256         return value;
1257 }
1258
1259 /*
1260  * Lookup a value in a fatzap directory. Assumes that the zap scratch
1261  * buffer contains the directory header.
1262  */
1263 static int
1264 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1265 {
1266         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1267         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1268         fat_zap_t z;
1269         uint64_t *ptrtbl;
1270         uint64_t hash;
1271         int rc;
1272
1273         if (zh.zap_magic != ZAP_MAGIC)
1274                 return (EIO);
1275
1276         z.zap_block_shift = ilog2(bsize);
1277         z.zap_phys = (zap_phys_t *) zap_scratch;
1278
1279         /*
1280          * Figure out where the pointer table is and read it in if necessary.
1281          */
1282         if (zh.zap_ptrtbl.zt_blk) {
1283                 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1284                                zap_scratch, bsize);
1285                 if (rc)
1286                         return (rc);
1287                 ptrtbl = (uint64_t *) zap_scratch;
1288         } else {
1289                 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1290         }
1291
1292         hash = zap_hash(zh.zap_salt, name);
1293
1294         zap_leaf_t zl;
1295         zl.l_bs = z.zap_block_shift;
1296
1297         off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1298         zap_leaf_chunk_t *zc;
1299
1300         rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1301         if (rc)
1302                 return (rc);
1303
1304         zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1305
1306         /*
1307          * Make sure this chunk matches our hash.
1308          */
1309         if (zl.l_phys->l_hdr.lh_prefix_len > 0
1310             && zl.l_phys->l_hdr.lh_prefix
1311             != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1312                 return (ENOENT);
1313
1314         /*
1315          * Hash within the chunk to find our entry.
1316          */
1317         int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1318         int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1319         h = zl.l_phys->l_hash[h];
1320         if (h == 0xffff)
1321                 return (ENOENT);
1322         zc = &ZAP_LEAF_CHUNK(&zl, h);
1323         while (zc->l_entry.le_hash != hash) {
1324                 if (zc->l_entry.le_next == 0xffff) {
1325                         zc = 0;
1326                         break;
1327                 }
1328                 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1329         }
1330         if (fzap_name_equal(&zl, zc, name)) {
1331                 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1332                         return (E2BIG);
1333                 *value = fzap_leaf_value(&zl, zc);
1334                 return (0);
1335         }
1336
1337         return (ENOENT);
1338 }
1339
1340 /*
1341  * Lookup a name in a zap object and return its value as a uint64_t.
1342  */
1343 static int
1344 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1345 {
1346         int rc;
1347         uint64_t zap_type;
1348         size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1349
1350         rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1351         if (rc)
1352                 return (rc);
1353
1354         zap_type = *(uint64_t *) zap_scratch;
1355         if (zap_type == ZBT_MICRO)
1356                 return mzap_lookup(dnode, name, value);
1357         else if (zap_type == ZBT_HEADER)
1358                 return fzap_lookup(spa, dnode, name, value);
1359         printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1360         return (EIO);
1361 }
1362
1363 /*
1364  * List a microzap directory. Assumes that the zap scratch buffer contains
1365  * the directory contents.
1366  */
1367 static int
1368 mzap_list(const dnode_phys_t *dnode)
1369 {
1370         const mzap_phys_t *mz;
1371         const mzap_ent_phys_t *mze;
1372         size_t size;
1373         int chunks, i;
1374
1375         /*
1376          * Microzap objects use exactly one block. Read the whole
1377          * thing.
1378          */
1379         size = dnode->dn_datablkszsec * 512;
1380         mz = (const mzap_phys_t *) zap_scratch;
1381         chunks = size / MZAP_ENT_LEN - 1;
1382
1383         for (i = 0; i < chunks; i++) {
1384                 mze = &mz->mz_chunk[i];
1385                 if (mze->mze_name[0])
1386                         //printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1387                         printf("%s\n", mze->mze_name);
1388         }
1389
1390         return (0);
1391 }
1392
1393 /*
1394  * List a fatzap directory. Assumes that the zap scratch buffer contains
1395  * the directory header.
1396  */
1397 static int
1398 fzap_list(const spa_t *spa, const dnode_phys_t *dnode)
1399 {
1400         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1401         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1402         fat_zap_t z;
1403         int i, j;
1404
1405         if (zh.zap_magic != ZAP_MAGIC)
1406                 return (EIO);
1407
1408         z.zap_block_shift = ilog2(bsize);
1409         z.zap_phys = (zap_phys_t *) zap_scratch;
1410
1411         /*
1412          * This assumes that the leaf blocks start at block 1. The
1413          * documentation isn't exactly clear on this.
1414          */
1415         zap_leaf_t zl;
1416         zl.l_bs = z.zap_block_shift;
1417         for (i = 0; i < zh.zap_num_leafs; i++) {
1418                 off_t off = (i + 1) << zl.l_bs;
1419                 char name[256], *p;
1420                 uint64_t value;
1421
1422                 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1423                         return (EIO);
1424
1425                 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1426
1427                 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1428                         zap_leaf_chunk_t *zc, *nc;
1429                         int namelen;
1430
1431                         zc = &ZAP_LEAF_CHUNK(&zl, j);
1432                         if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1433                                 continue;
1434                         namelen = zc->l_entry.le_name_numints;
1435                         if (namelen > sizeof(name))
1436                                 namelen = sizeof(name);
1437
1438                         /*
1439                          * Paste the name back together.
1440                          */
1441                         nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1442                         p = name;
1443                         while (namelen > 0) {
1444                                 int len;
1445                                 len = namelen;
1446                                 if (len > ZAP_LEAF_ARRAY_BYTES)
1447                                         len = ZAP_LEAF_ARRAY_BYTES;
1448                                 memcpy(p, nc->l_array.la_array, len);
1449                                 p += len;
1450                                 namelen -= len;
1451                                 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1452                         }
1453
1454                         /*
1455                          * Assume the first eight bytes of the value are
1456                          * a uint64_t.
1457                          */
1458                         value = fzap_leaf_value(&zl, zc);
1459
1460                         //printf("%s 0x%jx\n", name, (uintmax_t)value);
1461                         printf("%s\n", name);
1462                 }
1463         }
1464
1465         return (0);
1466 }
1467
1468 /*
1469  * List a zap directory.
1470  */
1471 static int
1472 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1473 {
1474         uint64_t zap_type;
1475         size_t size = dnode->dn_datablkszsec * 512;
1476
1477         if (dnode_read(spa, dnode, 0, zap_scratch, size))
1478                 return (EIO);
1479
1480         zap_type = *(uint64_t *) zap_scratch;
1481         if (zap_type == ZBT_MICRO)
1482                 return mzap_list(dnode);
1483         else
1484                 return fzap_list(spa, dnode);
1485 }
1486
1487 static int
1488 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1489 {
1490         off_t offset;
1491
1492         offset = objnum * sizeof(dnode_phys_t);
1493         return dnode_read(spa, &os->os_meta_dnode, offset,
1494                 dnode, sizeof(dnode_phys_t));
1495 }
1496
1497 static int
1498 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1499 {
1500         const mzap_phys_t *mz;
1501         const mzap_ent_phys_t *mze;
1502         size_t size;
1503         int chunks, i;
1504
1505         /*
1506          * Microzap objects use exactly one block. Read the whole
1507          * thing.
1508          */
1509         size = dnode->dn_datablkszsec * 512;
1510
1511         mz = (const mzap_phys_t *) zap_scratch;
1512         chunks = size / MZAP_ENT_LEN - 1;
1513
1514         for (i = 0; i < chunks; i++) {
1515                 mze = &mz->mz_chunk[i];
1516                 if (value == mze->mze_value) {
1517                         strcpy(name, mze->mze_name);
1518                         return (0);
1519                 }
1520         }
1521
1522         return (ENOENT);
1523 }
1524
1525 static void
1526 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1527 {
1528         size_t namelen;
1529         const zap_leaf_chunk_t *nc;
1530         char *p;
1531
1532         namelen = zc->l_entry.le_name_numints;
1533
1534         nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1535         p = name;
1536         while (namelen > 0) {
1537                 size_t len;
1538                 len = namelen;
1539                 if (len > ZAP_LEAF_ARRAY_BYTES)
1540                         len = ZAP_LEAF_ARRAY_BYTES;
1541                 memcpy(p, nc->l_array.la_array, len);
1542                 p += len;
1543                 namelen -= len;
1544                 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1545         }
1546
1547         *p = '\0';
1548 }
1549
1550 static int
1551 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1552 {
1553         int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1554         zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1555         fat_zap_t z;
1556         int i, j;
1557
1558         if (zh.zap_magic != ZAP_MAGIC)
1559                 return (EIO);
1560
1561         z.zap_block_shift = ilog2(bsize);
1562         z.zap_phys = (zap_phys_t *) zap_scratch;
1563
1564         /*
1565          * This assumes that the leaf blocks start at block 1. The
1566          * documentation isn't exactly clear on this.
1567          */
1568         zap_leaf_t zl;
1569         zl.l_bs = z.zap_block_shift;
1570         for (i = 0; i < zh.zap_num_leafs; i++) {
1571                 off_t off = (i + 1) << zl.l_bs;
1572
1573                 if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1574                         return (EIO);
1575
1576                 zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1577
1578                 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1579                         zap_leaf_chunk_t *zc;
1580
1581                         zc = &ZAP_LEAF_CHUNK(&zl, j);
1582                         if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1583                                 continue;
1584                         if (zc->l_entry.le_value_intlen != 8 ||
1585                             zc->l_entry.le_value_numints != 1)
1586                                 continue;
1587
1588                         if (fzap_leaf_value(&zl, zc) == value) {
1589                                 fzap_name_copy(&zl, zc, name);
1590                                 return (0);
1591                         }
1592                 }
1593         }
1594
1595         return (ENOENT);
1596 }
1597
1598 static int
1599 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1600 {
1601         int rc;
1602         uint64_t zap_type;
1603         size_t size = dnode->dn_datablkszsec * 512;
1604
1605         rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1606         if (rc)
1607                 return (rc);
1608
1609         zap_type = *(uint64_t *) zap_scratch;
1610         if (zap_type == ZBT_MICRO)
1611                 return mzap_rlookup(spa, dnode, name, value);
1612         else
1613                 return fzap_rlookup(spa, dnode, name, value);
1614 }
1615
1616 static int
1617 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1618 {
1619         char name[256];
1620         char component[256];
1621         uint64_t dir_obj, parent_obj, child_dir_zapobj;
1622         dnode_phys_t child_dir_zap, dataset, dir, parent;
1623         dsl_dir_phys_t *dd;
1624         dsl_dataset_phys_t *ds;
1625         char *p;
1626         int len;
1627
1628         p = &name[sizeof(name) - 1];
1629         *p = '\0';
1630
1631         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1632                 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1633                 return (EIO);
1634         }
1635         ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1636         dir_obj = ds->ds_dir_obj;
1637
1638         for (;;) {
1639                 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1640                         return (EIO);
1641                 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1642
1643                 /* Actual loop condition. */
1644                 parent_obj  = dd->dd_parent_obj;
1645                 if (parent_obj == 0)
1646                         break;
1647
1648                 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1649                         return (EIO);
1650                 dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1651                 child_dir_zapobj = dd->dd_child_dir_zapobj;
1652                 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1653                         return (EIO);
1654                 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1655                         return (EIO);
1656
1657                 len = strlen(component);
1658                 p -= len;
1659                 memcpy(p, component, len);
1660                 --p;
1661                 *p = '/';
1662
1663                 /* Actual loop iteration. */
1664                 dir_obj = parent_obj;
1665         }
1666
1667         if (*p != '\0')
1668                 ++p;
1669         strcpy(result, p);
1670
1671         return (0);
1672 }
1673
1674 static int
1675 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1676 {
1677         char element[256];
1678         uint64_t dir_obj, child_dir_zapobj;
1679         dnode_phys_t child_dir_zap, dir;
1680         dsl_dir_phys_t *dd;
1681         const char *p, *q;
1682
1683         if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1684                 return (EIO);
1685         if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1686                 return (EIO);
1687
1688         p = name;
1689         for (;;) {
1690                 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1691                         return (EIO);
1692                 dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1693
1694                 while (*p == '/')
1695                         p++;
1696                 /* Actual loop condition #1. */
1697                 if (*p == '\0')
1698                         break;
1699
1700                 q = strchr(p, '/');
1701                 if (q) {
1702                         memcpy(element, p, q - p);
1703                         element[q - p] = '\0';
1704                         p = q + 1;
1705                 } else {
1706                         strcpy(element, p);
1707                         p += strlen(p);
1708                 }
1709
1710                 child_dir_zapobj = dd->dd_child_dir_zapobj;
1711                 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1712                         return (EIO);
1713
1714                 /* Actual loop condition #2. */
1715                 if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1716                         return (ENOENT);
1717         }
1718
1719         *objnum = dd->dd_head_dataset_obj;
1720         return (0);
1721 }
1722
1723 #ifndef BOOT2
1724 static int
1725 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1726 {
1727         uint64_t dir_obj, child_dir_zapobj;
1728         dnode_phys_t child_dir_zap, dir, dataset;
1729         dsl_dataset_phys_t *ds;
1730         dsl_dir_phys_t *dd;
1731
1732         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1733                 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1734                 return (EIO);
1735         }
1736         ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1737         dir_obj = ds->ds_dir_obj;
1738
1739         if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1740                 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1741                 return (EIO);
1742         }
1743         dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1744
1745         child_dir_zapobj = dd->dd_child_dir_zapobj;
1746         if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1747                 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1748                 return (EIO);
1749         }
1750
1751         return (zap_list(spa, &child_dir_zap) != 0);
1752 }
1753 #endif
1754
1755 /*
1756  * Find the object set given the object number of its dataset object
1757  * and return its details in *objset
1758  */
1759 static int
1760 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1761 {
1762         dnode_phys_t dataset;
1763         dsl_dataset_phys_t *ds;
1764
1765         if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1766                 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1767                 return (EIO);
1768         }
1769
1770         ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1771         if (zio_read(spa, &ds->ds_bp, objset)) {
1772                 printf("ZFS: can't read object set for dataset %ju\n",
1773                     (uintmax_t)objnum);
1774                 return (EIO);
1775         }
1776
1777         return (0);
1778 }
1779
1780 /*
1781  * Find the object set pointed to by the BOOTFS property or the root
1782  * dataset if there is none and return its details in *objset
1783  */
1784 static int
1785 zfs_get_root(const spa_t *spa, uint64_t *objid)
1786 {
1787         dnode_phys_t dir, propdir;
1788         uint64_t props, bootfs, root;
1789
1790         *objid = 0;
1791
1792         /*
1793          * Start with the MOS directory object.
1794          */
1795         if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1796                 printf("ZFS: can't read MOS object directory\n");
1797                 return (EIO);
1798         }
1799
1800         /*
1801          * Lookup the pool_props and see if we can find a bootfs.
1802          */
1803         if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1804              && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1805              && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1806              && bootfs != 0)
1807         {
1808                 *objid = bootfs;
1809                 return (0);
1810         }
1811         /*
1812          * Lookup the root dataset directory
1813          */
1814         if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1815             || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1816                 printf("ZFS: can't find root dsl_dir\n");
1817                 return (EIO);
1818         }
1819
1820         /*
1821          * Use the information from the dataset directory's bonus buffer
1822          * to find the dataset object and from that the object set itself.
1823          */
1824         dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1825         *objid = dd->dd_head_dataset_obj;
1826         return (0);
1827 }
1828
1829 static int
1830 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1831 {
1832
1833         mount->spa = spa;
1834
1835         /*
1836          * Find the root object set if not explicitly provided
1837          */
1838         if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1839                 printf("ZFS: can't find root filesystem\n");
1840                 return (EIO);
1841         }
1842
1843         if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
1844                 printf("ZFS: can't open root filesystem\n");
1845                 return (EIO);
1846         }
1847
1848         mount->rootobj = rootobj;
1849
1850         return (0);
1851 }
1852
1853 static int
1854 zfs_spa_init(spa_t *spa)
1855 {
1856
1857         if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
1858                 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
1859                 return (EIO);
1860         }
1861         if (spa->spa_mos.os_type != DMU_OST_META) {
1862                 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
1863                 return (EIO);
1864         }
1865         return (0);
1866 }
1867
1868 static int
1869 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
1870 {
1871
1872         if (dn->dn_bonustype != DMU_OT_SA) {
1873                 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
1874
1875                 sb->st_mode = zp->zp_mode;
1876                 sb->st_uid = zp->zp_uid;
1877                 sb->st_gid = zp->zp_gid;
1878                 sb->st_size = zp->zp_size;
1879         } else {
1880                 sa_hdr_phys_t *sahdrp;
1881                 int hdrsize;
1882                 size_t size = 0;
1883                 void *buf = NULL;
1884
1885                 if (dn->dn_bonuslen != 0)
1886                         sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
1887                 else {
1888                         if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
1889                                 blkptr_t *bp = &dn->dn_spill;
1890                                 int error;
1891
1892                                 size = BP_GET_LSIZE(bp);
1893                                 buf = zfs_alloc(size);
1894                                 error = zio_read(spa, bp, buf);
1895                                 if (error != 0) {
1896                                         zfs_free(buf, size);
1897                                         return (error);
1898                                 }
1899                                 sahdrp = buf;
1900                         } else {
1901                                 return (EIO);
1902                         }
1903                 }
1904                 hdrsize = SA_HDR_SIZE(sahdrp);
1905                 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
1906                     SA_MODE_OFFSET);
1907                 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
1908                     SA_UID_OFFSET);
1909                 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
1910                     SA_GID_OFFSET);
1911                 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
1912                     SA_SIZE_OFFSET);
1913                 if (buf != NULL)
1914                         zfs_free(buf, size);
1915         }
1916
1917         return (0);
1918 }
1919
1920 /*
1921  * Lookup a file and return its dnode.
1922  */
1923 static int
1924 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
1925 {
1926         int rc;
1927         uint64_t objnum, rootnum, parentnum;
1928         const spa_t *spa;
1929         dnode_phys_t dn;
1930         const char *p, *q;
1931         char element[256];
1932         char path[1024];
1933         int symlinks_followed = 0;
1934         struct stat sb;
1935
1936         spa = mount->spa;
1937         if (mount->objset.os_type != DMU_OST_ZFS) {
1938                 printf("ZFS: unexpected object set type %ju\n",
1939                     (uintmax_t)mount->objset.os_type);
1940                 return (EIO);
1941         }
1942
1943         /*
1944          * Get the root directory dnode.
1945          */
1946         rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
1947         if (rc)
1948                 return (rc);
1949
1950         rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
1951         if (rc)
1952                 return (rc);
1953
1954         rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
1955         if (rc)
1956                 return (rc);
1957
1958         objnum = rootnum;
1959         p = upath;
1960         while (p && *p) {
1961                 while (*p == '/')
1962                         p++;
1963                 if (!*p)
1964                         break;
1965                 q = strchr(p, '/');
1966                 if (q) {
1967                         memcpy(element, p, q - p);
1968                         element[q - p] = 0;
1969                         p = q;
1970                 } else {
1971                         strcpy(element, p);
1972                         p = 0;
1973                 }
1974
1975                 rc = zfs_dnode_stat(spa, &dn, &sb);
1976                 if (rc)
1977                         return (rc);
1978                 if (!S_ISDIR(sb.st_mode))
1979                         return (ENOTDIR);
1980
1981                 parentnum = objnum;
1982                 rc = zap_lookup(spa, &dn, element, &objnum);
1983                 if (rc)
1984                         return (rc);
1985                 objnum = ZFS_DIRENT_OBJ(objnum);
1986
1987                 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
1988                 if (rc)
1989                         return (rc);
1990
1991                 /*
1992                  * Check for symlink.
1993                  */
1994                 rc = zfs_dnode_stat(spa, &dn, &sb);
1995                 if (rc)
1996                         return (rc);
1997                 if (S_ISLNK(sb.st_mode)) {
1998                         if (symlinks_followed > 10)
1999                                 return (EMLINK);
2000                         symlinks_followed++;
2001
2002                         /*
2003                          * Read the link value and copy the tail of our
2004                          * current path onto the end.
2005                          */
2006                         if (p)
2007                                 strcpy(&path[sb.st_size], p);
2008                         else
2009                                 path[sb.st_size] = 0;
2010                         if (sb.st_size + sizeof(znode_phys_t) <= dn.dn_bonuslen) {
2011                                 memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2012                                         sb.st_size);
2013                         } else {
2014                                 rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2015                                 if (rc)
2016                                         return (rc);
2017                         }
2018
2019                         /*
2020                          * Restart with the new path, starting either at
2021                          * the root or at the parent depending whether or
2022                          * not the link is relative.
2023                          */
2024                         p = path;
2025                         if (*p == '/')
2026                                 objnum = rootnum;
2027                         else
2028                                 objnum = parentnum;
2029                         objset_get_dnode(spa, &mount->objset, objnum, &dn);
2030                 }
2031         }
2032
2033         *dnode = dn;
2034         return (0);
2035 }