Print right boundary for internal nodes.
[dragonfly.git] / lib / libstand / hammerread.c
blob626214e7ddb838032980934c2f0b834da3e154d0
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon Schubert <corecode@fs.ei.tum.de>
6 * and Matthew Dillon <dillon@backplane.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * $DragonFly: src/lib/libstand/hammerread.c,v 1.2 2008/10/29 22:14:25 swildner Exp $
39 * This file is being used by boot2 and libstand (loader).
40 * Compile with -DTESTING to obtain a binary.
44 #if !defined(BOOT2) && !defined(TESTING)
45 #define LIBSTAND 1
46 #endif
48 #include <sys/param.h>
50 #include <stddef.h>
51 #include <stdint.h>
53 #ifdef TESTING
54 #include <sys/fcntl.h>
55 #include <sys/stat.h>
56 #include <unistd.h>
57 #include <err.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <errno.h>
62 #include <dirent.h>
63 #endif
65 #ifdef LIBSTAND
66 #include "stand.h"
67 #endif
69 #include <vfs/hammer/hammer_disk.h>
71 #ifndef BOOT2
72 struct blockentry {
73 hammer_off_t off;
74 int use;
75 char *data;
78 #ifdef TESTING
79 #define NUMCACHE 16
80 #else
81 #define NUMCACHE 6
82 #endif
84 struct hfs {
85 #ifdef TESTING
86 int fd;
87 #else // libstand
88 struct open_file *f;
89 #endif
90 hammer_off_t root;
91 int64_t buf_beg;
92 int lru;
93 struct blockentry cache[NUMCACHE];
96 static void *
97 hread(struct hfs *hfs, hammer_off_t off)
99 hammer_off_t boff = off & ~HAMMER_BUFMASK64;
101 boff &= HAMMER_OFF_LONG_MASK;
103 if (HAMMER_ZONE_DECODE(off) != HAMMER_ZONE_RAW_VOLUME_INDEX)
104 boff += hfs->buf_beg;
106 struct blockentry *be = NULL;
107 for (int i = 0; i < NUMCACHE; i++) {
108 if (be == NULL || be->use > hfs->cache[i].use)
109 be = &hfs->cache[i];
110 if (hfs->cache[i].off == boff) {
111 be = &hfs->cache[i];
112 break;
115 if (be->off != boff) {
116 // Didn't find any match
117 be->off = boff;
118 #ifdef TESTING
119 ssize_t res = pread(hfs->fd, be->data, HAMMER_BUFSIZE,
120 boff & HAMMER_OFF_SHORT_MASK);
121 if (res != HAMMER_BUFSIZE)
122 err(1, "short read on off %llx", boff);
123 #else // libstand
124 size_t rlen;
125 int rv = hfs->f->f_dev->dv_strategy(hfs->f->f_devdata, F_READ,
126 boff >> DEV_BSHIFT, HAMMER_BUFSIZE,
127 be->data, &rlen);
128 if (rv || rlen != HAMMER_BUFSIZE)
129 return (NULL);
130 #endif
133 be->use = ++hfs->lru;
134 return &be->data[off & HAMMER_BUFMASK];
137 #else /* BOOT2 */
139 struct dmadat {
140 char secbuf[DEV_BSIZE];
141 char buf[HAMMER_BUFSIZE];
144 static struct dmadat *dmadat;
146 struct hfs {
147 hammer_off_t root;
148 int64_t buf_beg;
151 static void *
152 hread(struct hfs *hfs, hammer_off_t off)
154 char *buf = dmadat->buf;
156 hammer_off_t boff = off & ~HAMMER_BUFMASK64;
157 boff &= HAMMER_OFF_LONG_MASK;
158 if (HAMMER_ZONE_DECODE(off) != HAMMER_ZONE_RAW_VOLUME_INDEX)
159 boff += hfs->buf_beg;
160 boff &= HAMMER_OFF_SHORT_MASK;
161 boff >>= DEV_BSHIFT;
162 if (dskread(buf, boff, HAMMER_BUFSIZE >> DEV_BSHIFT))
163 return (NULL);
164 return (&buf[off & HAMMER_BUFMASK]);
167 static void
168 bzero(void *buf, size_t size)
170 for (size_t i = 0; i < size; i++)
171 ((char *)buf)[i] = 0;
174 static void
175 bcopy(void *src, void *dst, size_t size)
177 memcpy(dst, src, size);
180 static size_t
181 strlen(const char *s)
183 size_t l = 0;
184 for (; *s != 0; s++)
185 l++;
186 return (l);
189 static int
190 memcmp(const void *a, const void *b, size_t len)
192 for (size_t p = 0; p < len; p++) {
193 int r = ((const char *)a)[p] - ((const char *)b)[p];
194 if (r != 0)
195 return (r);
198 return (0);
201 #endif
204 * (from hammer_btree.c)
206 * Compare two B-Tree elements, return -N, 0, or +N (e.g. similar to strcmp).
208 * Note that for this particular function a return value of -1, 0, or +1
209 * can denote a match if create_tid is otherwise discounted. A create_tid
210 * of zero is considered to be 'infinity' in comparisons.
212 * See also hammer_rec_rb_compare() and hammer_rec_cmp() in hammer_object.c.
214 static int
215 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
217 if (key1->localization < key2->localization)
218 return(-5);
219 if (key1->localization > key2->localization)
220 return(5);
222 if (key1->obj_id < key2->obj_id)
223 return(-4);
224 if (key1->obj_id > key2->obj_id)
225 return(4);
227 if (key1->rec_type < key2->rec_type)
228 return(-3);
229 if (key1->rec_type > key2->rec_type)
230 return(3);
232 if (key1->key < key2->key)
233 return(-2);
234 if (key1->key > key2->key)
235 return(2);
238 * A create_tid of zero indicates a record which is undeletable
239 * and must be considered to have a value of positive infinity.
241 if (key1->create_tid == 0) {
242 if (key2->create_tid == 0)
243 return(0);
244 return(1);
246 if (key2->create_tid == 0)
247 return(-1);
248 if (key1->create_tid < key2->create_tid)
249 return(-1);
250 if (key1->create_tid > key2->create_tid)
251 return(1);
252 return(0);
256 * Heuristical search for the first element whos comparison is <= 1. May
257 * return an index whos compare result is > 1 but may only return an index
258 * whos compare result is <= 1 if it is the first element with that result.
260 static int
261 hammer_btree_search_node(hammer_base_elm_t elm, hammer_node_ondisk_t node)
263 int b;
264 int s;
265 int i;
266 int r;
269 * Don't bother if the node does not have very many elements
271 b = 0;
272 s = node->count;
273 while (s - b > 4) {
274 i = b + (s - b) / 2;
275 r = hammer_btree_cmp(elm, &node->elms[i].leaf.base);
276 if (r <= 1) {
277 s = i;
278 } else {
279 b = i;
282 return(b);
285 #if 0
287 * (from hammer_subs.c)
289 * Return a namekey hash. The 64 bit namekey hash consists of a 32 bit
290 * crc in the MSB and 0 in the LSB. The caller will use the low bits to
291 * generate a unique key and will scan all entries with the same upper
292 * 32 bits when issuing a lookup.
294 * We strip bit 63 in order to provide a positive key, this way a seek
295 * offset of 0 will represent the base of the directory.
297 * This function can never return 0. We use the MSB-0 space to synthesize
298 * artificial directory entries such as "." and "..".
300 static int64_t
301 hammer_directory_namekey(const void *name, int len)
303 int64_t key;
305 key = (int64_t)(crc32(name, len) & 0x7FFFFFFF) << 32;
306 if (key == 0)
307 key |= 0x100000000LL;
308 return(key);
310 #else
311 static int64_t
312 hammer_directory_namekey(const void *name __unused, int len __unused)
314 return (0);
316 #endif
319 #ifndef BOOT2
321 * Misc
323 static u_int32_t
324 hammer_to_unix_xid(uuid_t *uuid)
326 return(*(u_int32_t *)&uuid->node[2]);
329 static int
330 hammer_get_dtype(u_int8_t obj_type)
332 switch(obj_type) {
333 case HAMMER_OBJTYPE_DIRECTORY:
334 return(DT_DIR);
335 case HAMMER_OBJTYPE_REGFILE:
336 return(DT_REG);
337 case HAMMER_OBJTYPE_DBFILE:
338 return(DT_DBF);
339 case HAMMER_OBJTYPE_FIFO:
340 return(DT_FIFO);
341 case HAMMER_OBJTYPE_SOCKET:
342 return(DT_SOCK);
343 case HAMMER_OBJTYPE_CDEV:
344 return(DT_CHR);
345 case HAMMER_OBJTYPE_BDEV:
346 return(DT_BLK);
347 case HAMMER_OBJTYPE_SOFTLINK:
348 return(DT_LNK);
349 default:
350 return(DT_UNKNOWN);
352 /* not reached */
355 static int
356 hammer_get_mode(u_int8_t obj_type)
358 switch(obj_type) {
359 case HAMMER_OBJTYPE_DIRECTORY:
360 return(S_IFDIR);
361 case HAMMER_OBJTYPE_REGFILE:
362 return(S_IFREG);
363 case HAMMER_OBJTYPE_DBFILE:
364 return(S_IFDB);
365 case HAMMER_OBJTYPE_FIFO:
366 return(S_IFIFO);
367 case HAMMER_OBJTYPE_SOCKET:
368 return(S_IFSOCK);
369 case HAMMER_OBJTYPE_CDEV:
370 return(S_IFCHR);
371 case HAMMER_OBJTYPE_BDEV:
372 return(S_IFBLK);
373 case HAMMER_OBJTYPE_SOFTLINK:
374 return(S_IFLNK);
375 default:
376 return(0);
378 /* not reached */
381 #if DEBUG > 1
382 static void
383 hprintb(hammer_base_elm_t e)
385 printf("%d/", e->localization);
386 if (e->obj_id >> 32 != 0)
387 printf("%lx%08lx",
388 (long)(e->obj_id >> 32),
389 (long)(e->obj_id & 0xffffffff));
390 else
391 printf("%lx", (long)e->obj_id);
392 printf("/%d/", e->rec_type);
393 if (e->key >> 32 != 0)
394 printf("%lx%08lx",
395 (long)(e->key >> 32),
396 (long)(e->key & 0xffffffff));
397 else
398 printf("%lx", (long)e->key);
399 #ifdef TESTING
400 printf("/%llx/%llx", e->create_tid, e->delete_tid);
401 #endif
403 #endif /* DEBUG > 1 */
404 #endif /* !BOOT2 */
406 static hammer_btree_leaf_elm_t
407 hfind(struct hfs *hfs, hammer_base_elm_t key, hammer_base_elm_t end)
409 #if DEBUG > 1
410 printf("searching for ");
411 hprintb(key);
412 printf(" end ");
413 hprintb(end);
414 printf("\n");
415 #endif
417 int n;
418 int r;
419 struct hammer_base_elm search = *key;
420 struct hammer_base_elm backtrack;
421 hammer_off_t nodeoff = hfs->root;
422 hammer_node_ondisk_t node;
423 hammer_btree_elm_t e = NULL;
425 loop:
426 node = hread(hfs, nodeoff);
427 if (node == NULL)
428 return (NULL);
430 #if DEBUG > 3
431 for (int i = 0; i < node->count; i++) {
432 printf("E: ");
433 hprintb(&node->elms[i].base);
434 printf("\n");
436 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
437 printf("B: ");
438 hprintb(&node->elms[node->count].base);
439 printf("\n");
441 #endif
443 n = hammer_btree_search_node(&search, node);
445 for (; n < node->count; n++) {
446 e = &node->elms[n];
447 r = hammer_btree_cmp(&search, &e->base);
449 if (r < 0)
450 break;
453 // unless we stopped right on the left side, we need to back off a bit
454 if (n > 0)
455 e = &node->elms[n - 1];
457 #if DEBUG > 2
458 printf(" found: ");
459 hprintb(&e->base);
460 printf("\n");
461 #endif
463 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
464 nodeoff = e->internal.subtree_offset;
465 backtrack = (e+1)->base;
466 goto loop;
469 r = hammer_btree_cmp(key, &e->base);
470 // If we're more off than the createtid, take the next elem
471 if (r > 1) {
472 e++;
473 n++;
476 // Skip deleted elements
477 while (n <= node->count && e->base.delete_tid != 0) {
478 e++;
479 n++;
482 // In the unfortunate event when there is no next
483 // element in this node, we repeat the search with
484 // a key beyond the right boundary
485 if (n > node->count) {
486 search = backtrack;
487 nodeoff = hfs->root;
489 #if DEBUG > 2
490 printf("hit right boundary (%d), resetting search to ",
491 node->count);
492 hprintb(&search);
493 printf("\n");
494 #endif
495 goto loop;
498 #if DEBUG > 1
499 printf(" result: ");
500 hprintb(&e->base);
501 printf("\n");
502 #endif
504 if (end != NULL)
505 if (hammer_btree_cmp(end, &e->base) < -1)
506 goto fail;
508 return (&e->leaf);
510 fail:
511 #if DEBUG > 1
512 printf(" fail.\n");
513 #endif
514 return (NULL);
517 #ifndef BOOT2
518 static int
519 hreaddir(struct hfs *hfs, ino_t ino, int64_t *off, struct dirent *de)
521 struct hammer_base_elm key, end;
523 #if DEBUG > 2
524 printf("%s(%llx, %lld)\n", __FUNCTION__, (long long)ino, *off);
525 #endif
527 bzero(&key, sizeof(key));
528 key.obj_id = ino;
529 key.localization = HAMMER_LOCALIZE_MISC;
530 key.rec_type = HAMMER_RECTYPE_DIRENTRY;
531 key.key = *off;
533 end = key;
534 end.key = HAMMER_MAX_KEY;
536 hammer_btree_leaf_elm_t e;
538 e = hfind(hfs, &key, &end);
539 if (e == NULL) {
540 errno = ENOENT;
541 return (-1);
544 *off = e->base.key + 1; // remember next pos
546 de->d_namlen = e->data_len - HAMMER_ENTRY_NAME_OFF;
547 de->d_type = hammer_get_dtype(e->base.obj_type);
548 hammer_data_ondisk_t ed = hread(hfs, e->data_offset);
549 if (ed == NULL)
550 return (-1);
551 de->d_ino = ed->entry.obj_id;
552 bcopy(ed->entry.name, de->d_name, de->d_namlen);
553 de->d_name[de->d_namlen] = 0;
555 return (0);
557 #endif
559 static ino_t
560 hresolve(struct hfs *hfs, ino_t dirino, const char *name)
562 struct hammer_base_elm key, end;
563 size_t namel = strlen(name);
565 #if DEBUG > 2
566 printf("%s(%llx, %s)\n", __FUNCTION__, (long long)dirino, name);
567 #endif
569 bzero(&key, sizeof(key));
570 key.obj_id = dirino;
571 key.localization = HAMMER_LOCALIZE_MISC;
572 key.key = hammer_directory_namekey(name, namel);
573 key.rec_type = HAMMER_RECTYPE_DIRENTRY;
574 end = key;
575 end.key = HAMMER_MAX_KEY;
577 hammer_btree_leaf_elm_t e;
578 while ((e = hfind(hfs, &key, &end)) != NULL) {
579 key.key = e->base.key + 1;
581 size_t elen = e->data_len - HAMMER_ENTRY_NAME_OFF;
582 hammer_data_ondisk_t ed = hread(hfs, e->data_offset);
583 if (ed == NULL)
584 return (-1);
585 #ifdef BOOT2
586 if (ls) {
587 for (int i = 0; i < elen; i++)
588 putchar(ed->entry.name[i]);
589 putchar(' ');
590 ls = 2;
591 continue;
593 #endif
594 if (elen == namel && memcmp(ed->entry.name, name, MIN(elen, namel)) == 0)
595 return (ed->entry.obj_id);
598 #if BOOT2
599 if (ls == 2)
600 printf("\n");
601 #endif
603 return -1;
606 static ino_t
607 hlookup(struct hfs *hfs, const char *path)
609 #if DEBUG > 2
610 printf("%s(%s)\n", __FUNCTION__, path);
611 #endif
613 #ifdef BOOT2
614 ls = 0;
615 #endif
616 ino_t ino = 1;
617 do {
618 char name[MAXPATHLEN + 1];
619 while (*path == '/')
620 path++;
621 if (*path == 0)
622 break;
623 for (char *n = name; *path != 0 && *path != '/'; path++, n++) {
624 n[0] = *path;
625 n[1] = 0;
628 #ifdef BOOT2
629 // A single ? means "list"
630 if (name[0] == '?' && name[1] == 0)
631 ls = 1;
632 #endif
634 ino = hresolve(hfs, ino, name);
635 } while (ino != (ino_t)-1 && *path != 0);
637 return (ino);
641 #ifndef BOOT2
642 static int
643 hstat(struct hfs *hfs, ino_t ino, struct stat* st)
645 struct hammer_base_elm key;
647 #if DEBUG > 2
648 printf("%s(%llx)\n", __FUNCTION__, (long long)ino);
649 #endif
651 bzero(&key, sizeof(key));
652 key.obj_id = ino;
653 key.localization = HAMMER_LOCALIZE_INODE;
654 key.rec_type = HAMMER_RECTYPE_INODE;
656 hammer_btree_leaf_elm_t e = hfind(hfs, &key, &key);
657 if (e == NULL) {
658 #ifndef BOOT2
659 errno = ENOENT;
660 #endif
661 return -1;
664 hammer_data_ondisk_t ed = hread(hfs, e->data_offset);
665 if (ed == NULL)
666 return (-1);
668 st->st_mode = ed->inode.mode | hammer_get_mode(ed->inode.obj_type);
669 st->st_uid = hammer_to_unix_xid(&ed->inode.uid);
670 st->st_gid = hammer_to_unix_xid(&ed->inode.gid);
671 st->st_size = ed->inode.size;
673 return (0);
675 #endif
677 static ssize_t
678 hreadf(struct hfs *hfs, ino_t ino, int64_t off, int64_t len, char *buf)
680 int64_t startoff = off;
681 struct hammer_base_elm key, end;
683 bzero(&key, sizeof(key));
684 key.obj_id = ino;
685 key.localization = HAMMER_LOCALIZE_MISC;
686 key.rec_type = HAMMER_RECTYPE_DATA;
687 end = key;
688 end.key = HAMMER_MAX_KEY;
690 while (len > 0) {
691 key.key = off + 1;
692 hammer_btree_leaf_elm_t e = hfind(hfs, &key, &end);
693 int64_t dlen;
695 if (e == NULL || off > e->base.key) {
696 bzero(buf, len);
697 off += len;
698 len = 0;
699 break;
702 int64_t doff = e->base.key - e->data_len;
703 if (off < doff) {
704 // sparse file, beginning
705 dlen = doff - off;
706 dlen = MIN(dlen, len);
707 bzero(buf, dlen);
708 } else {
709 int64_t boff = off - doff;
710 hammer_off_t roff = e->data_offset;
712 dlen = e->data_len;
713 dlen -= boff;
714 dlen = MIN(dlen, len);
716 while (boff >= HAMMER_BUFSIZE) {
717 boff -= HAMMER_BUFSIZE;
718 roff += HAMMER_BUFSIZE;
722 * boff - relative offset in disk buffer (not aligned)
723 * roff - base offset of disk buffer (not aligned)
724 * dlen - amount of data we think we can copy
726 * hread only reads 16K aligned buffers, check for
727 * a length overflow and truncate dlen appropriately.
729 if ((roff & ~HAMMER_BUFMASK64) != ((roff + boff + dlen - 1) & ~HAMMER_BUFMASK64))
730 dlen = HAMMER_BUFSIZE - ((boff + roff) & HAMMER_BUFMASK);
731 char *data = hread(hfs, roff);
732 if (data == NULL)
733 return (-1);
734 bcopy(data + boff, buf, dlen);
737 buf += dlen;
738 off += dlen;
739 len -= dlen;
742 return (off - startoff);
745 #ifdef BOOT2
746 struct hfs hfs;
748 static int
749 hammerinit(void)
751 if (dsk_meta)
752 return (0);
754 hammer_volume_ondisk_t volhead = hread(&hfs, HAMMER_ZONE_ENCODE(1, 0));
755 if (volhead == NULL)
756 return (-1);
757 if (volhead->vol_signature != HAMMER_FSBUF_VOLUME)
758 return (-1);
759 hfs.root = volhead->vol0_btree_root;
760 hfs.buf_beg = volhead->vol_buf_beg;
761 dsk_meta++;
762 return (0);
765 static ino_t
766 lookup(const char *path)
768 hammerinit();
770 ino_t ino = hlookup(&hfs, path);
772 if (ino == -1)
773 ino = 0;
774 return (ino);
777 static ssize_t
778 fsread(ino_t ino, void *buf, size_t len)
780 hammerinit();
782 ssize_t rlen = hreadf(&hfs, ino, fs_off, len, buf);
783 if (rlen != -1)
784 fs_off += rlen;
785 return (rlen);
787 #endif
789 #ifndef BOOT2
790 static int
791 hinit(struct hfs *hfs)
793 #if DEBUG
794 printf("hinit\n");
795 #endif
796 for (int i = 0; i < NUMCACHE; i++) {
797 hfs->cache[i].data = malloc(HAMMER_BUFSIZE);
798 hfs->cache[i].off = -1; // invalid
799 hfs->cache[i].use = 0;
801 #if DEBUG
802 if (hfs->cache[i].data == NULL)
803 printf("malloc failed\n");
804 #endif
806 hfs->lru = 0;
808 hammer_volume_ondisk_t volhead = hread(hfs, HAMMER_ZONE_ENCODE(1, 0));
809 if (volhead == NULL)
810 return (-1);
812 #ifdef TESTING
813 printf("signature: %svalid\n",
814 volhead->vol_signature != HAMMER_FSBUF_VOLUME ?
815 "in" :
816 "");
817 printf("name: %s\n", volhead->vol_name);
818 #endif
820 if (volhead->vol_signature != HAMMER_FSBUF_VOLUME) {
821 for (int i = 0; i < NUMCACHE; i++)
822 free(hfs->cache[i].data);
823 errno = ENODEV;
824 return (-1);
827 hfs->root = volhead->vol0_btree_root;
828 hfs->buf_beg = volhead->vol_buf_beg;
830 return (0);
833 static void
834 hclose(struct hfs *hfs)
836 #if DEBUG
837 printf("hclose\n");
838 #endif
839 for (int i = 0; i < NUMCACHE; i++)
840 free(hfs->cache[i].data);
842 #endif
844 #ifdef LIBSTAND
845 struct hfile {
846 struct hfs hfs;
847 ino_t ino;
848 int64_t fsize;
851 static int
852 hammer_open(const char *path, struct open_file *f)
854 struct hfile *hf = malloc(sizeof(*hf));
855 bzero(hf, sizeof(*hf));
857 f->f_fsdata = hf;
858 hf->hfs.f = f;
859 f->f_offset = 0;
861 int rv = hinit(&hf->hfs);
862 if (rv) {
863 free(hf);
864 return (rv);
867 #if DEBUG
868 printf("hammer_open %s %p %ld\n", path, f);
869 #endif
871 hf->ino = hlookup(&hf->hfs, path);
872 if (hf->ino == -1)
873 goto fail;
875 struct stat st;
876 if (hstat(&hf->hfs, hf->ino, &st) == -1)
877 goto fail;
878 hf->fsize = st.st_size;
880 #if DEBUG
881 printf(" %ld\n", (long)hf->fsize);
882 #endif
884 return (0);
886 fail:
887 #if DEBUG
888 printf("hammer_open fail\n");
889 #endif
890 hclose(&hf->hfs);
891 free(hf);
892 return (ENOENT);
895 static int
896 hammer_close(struct open_file *f)
898 struct hfile *hf = f->f_fsdata;
900 hclose(&hf->hfs);
901 f->f_fsdata = NULL;
902 free(hf);
903 return (0);
906 static int
907 hammer_read(struct open_file *f, void *buf, size_t len, size_t *resid)
909 struct hfile *hf = f->f_fsdata;
911 #if DEBUG
912 printf("hammer_read %p %ld %ld\n", f, f->f_offset, len);
913 #endif
915 if (f->f_offset >= hf->fsize)
916 return (EINVAL);
918 size_t maxlen = len;
919 if (f->f_offset + len > hf->fsize)
920 maxlen = hf->fsize - f->f_offset;
922 ssize_t rlen = hreadf(&hf->hfs, hf->ino, f->f_offset, maxlen, buf);
923 if (rlen == -1)
924 return (EINVAL);
926 f->f_offset += rlen;
928 *resid = len - rlen;
929 return (0);
932 static off_t
933 hammer_seek(struct open_file *f, off_t offset, int whence)
935 struct hfile *hf = f->f_fsdata;
937 switch (whence) {
938 case SEEK_SET:
939 f->f_offset = offset;
940 break;
941 case SEEK_CUR:
942 f->f_offset += offset;
943 break;
944 case SEEK_END:
945 f->f_offset = hf->fsize - offset;
946 break;
947 default:
948 return (-1);
950 return (f->f_offset);
953 static int
954 hammer_stat(struct open_file *f, struct stat *st)
956 struct hfile *hf = f->f_fsdata;
958 return (hstat(&hf->hfs, hf->ino, st));
961 static int
962 hammer_readdir(struct open_file *f, struct dirent *d)
964 struct hfile *hf = f->f_fsdata;
966 int64_t off = f->f_offset;
967 int rv = hreaddir(&hf->hfs, hf->ino, &off, d);
968 f->f_offset = off;
969 return (rv);
972 // libstand
973 struct fs_ops hammer_fsops = {
974 "hammer",
975 hammer_open,
976 hammer_close,
977 hammer_read,
978 null_write,
979 hammer_seek,
980 hammer_stat,
981 hammer_readdir
983 #endif // LIBSTAND
985 #ifdef TESTING
987 main(int argc, char **argv)
989 if (argc < 2) {
990 fprintf(stderr, "usage: hammerread <dev>\n");
991 return (1);
994 struct hfs hfs;
995 hfs.fd = open(argv[1], O_RDONLY);
996 if (hfs.fd == -1)
997 err(1, "unable to open %s", argv[1]);
999 if (hinit(&hfs) == -1)
1000 err(1, "invalid hammerfs");
1002 for (int i = 2; i < argc; i++) {
1003 ino_t ino = hlookup(&hfs, argv[i]);
1004 if (ino == (ino_t)-1) {
1005 warn("hlookup %s", argv[i]);
1006 continue;
1009 struct stat st;
1010 if (hstat(&hfs, ino, &st)) {
1011 warn("hstat %s", argv[i]);
1012 continue;
1015 printf("%s %d/%d %o %lld\n",
1016 argv[i],
1017 st.st_uid, st.st_gid,
1018 st.st_mode, st.st_size);
1020 if (S_ISDIR(st.st_mode)) {
1021 int64_t off = 0;
1022 struct dirent de;
1023 while (hreaddir(&hfs, ino, &off, &de) == 0) {
1024 printf("%s %d %llx\n",
1025 de.d_name, de.d_type, de.d_ino);
1027 } else if (S_ISREG(st.st_mode)) {
1028 char *buf = malloc(100000);
1029 int64_t off = 0;
1030 while (off < st.st_size) {
1031 int64_t len = MIN(100000, st.st_size - off);
1032 int64_t rl = hreadf(&hfs, ino, off, len, buf);
1033 fwrite(buf, rl, 1, stdout);
1034 off += rl;
1036 free(buf);
1040 return 0;
1042 #endif