Merge branch 'vendor/GCC50'
[dragonfly.git] / lib / libstand / hammer2.c
1 /*
2  * Copyright (c) 2011-2013 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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.
21  *
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.
34  */
35
36 #if !defined(BOOT2) && !defined(TESTING)
37 #define LIBSTAND        1
38 #endif
39
40 #ifdef BOOT2
41 #include "boot2.h"
42 #endif
43
44 #ifdef TESTING
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/uuid.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stddef.h>
51 #include <stdint.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <string.h>
55 #include <strings.h>
56 #include <errno.h>
57 #endif
58
59 #ifdef LIBSTAND
60 #include "stand.h"
61 #endif
62
63 #include <vfs/hammer2/hammer2_disk.h>
64
65 uint32_t iscsi_crc32(const void *buf, size_t size);
66 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
67
68 static hammer2_media_data_t media;
69 static hammer2_blockref_t saved_base;
70
71 #define hammer2_icrc32(buf, size)       iscsi_crc32(buf, size)
72
73 struct hammer2_fs {
74         hammer2_blockref_t              sroot;
75         hammer2_blockset_t              sroot_blockset;
76 #if defined(TESTING)
77         int                             fd;
78 #elif defined(LIBSTAND)
79         struct open_file                *f;
80 #elif defined(BOOT2)
81         /* BOOT2 doesn't use a descriptor */
82 #else
83 #error "hammer2: unknown library API"
84 #endif
85 };
86
87 struct hammer2_inode {
88         struct hammer2_inode_data       ino;    /* raw inode data */
89         off_t                           doff;   /* disk inode offset */
90 };
91
92 #ifdef BOOT2
93
94 static void
95 bzero(void *buf, size_t size)
96 {
97         for (size_t i = 0; i < size; i++)
98                 ((char *)buf)[i] = 0;
99 }
100
101 static void
102 bcopy(void *src, void *dst, size_t size)
103 {
104         memcpy(dst, src, size);
105 }
106
107 #if 0
108 static size_t
109 strlen(const char *s)
110 {
111         size_t l = 0;
112         for (; *s != 0; s++)
113                 l++;
114         return (l);
115 }
116 #endif
117
118 static int
119 memcmp(const void *a, const void *b, size_t len)
120 {
121         for (size_t p = 0; p < len; p++) {
122                 int r = ((const char *)a)[p] - ((const char *)b)[p];
123                 if (r != 0)
124                         return (r);
125         }
126
127         return (0);
128 }
129
130 #endif
131
132 static
133 off_t
134 blockoff(hammer2_blockref_t *bref)
135 {
136         return(bref->data_off & ~HAMMER2_OFF_MASK_RADIX);
137 }
138
139 static
140 size_t
141 blocksize(hammer2_blockref_t *bref)
142 {
143         return(1 << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX));
144 }
145
146 static
147 hammer2_key_t
148 hammer2_dirhash(const unsigned char *name, size_t len)
149 {
150         const unsigned char *aname = name;
151         uint32_t crcx;
152         uint64_t key;
153         size_t i;
154         size_t j;
155
156         key = 0;
157
158         /*
159          * m32
160          */
161         crcx = 0;
162         for (i = j = 0; i < len; ++i) {
163                 if (aname[i] == '.' ||
164                     aname[i] == '-' ||
165                     aname[i] == '_' ||
166                     aname[i] == '~') {
167                         if (i != j)
168                                 crcx += hammer2_icrc32(aname + j, i - j);
169                         j = i + 1;
170                 }
171         }
172         if (i != j)
173                 crcx += hammer2_icrc32(aname + j, i - j);
174
175         /*
176          * The directory hash utilizes the top 32 bits of the 64-bit key.
177          * Bit 63 must be set to 1.
178          */
179         crcx |= 0x80000000U;
180         key |= (uint64_t)crcx << 32;
181
182         /*
183          * l16 - crc of entire filename
184          *
185          * This crc reduces degenerate hash collision conditions
186          */
187         crcx = hammer2_icrc32(aname, len);
188         crcx = crcx ^ (crcx << 16);
189         key |= crcx & 0xFFFF0000U;
190
191         /*
192          * Set bit 15.  This allows readdir to strip bit 63 so a positive
193          * 64-bit cookie/offset can always be returned, and still guarantee
194          * that the values 0x0000-0x7FFF are available for artificial entries.
195          * ('.' and '..').
196          */
197         key |= 0x8000U;
198
199         return (key);
200 }
201
202 /*
203  * Low level read
204  */
205 static
206 int
207 h2read(struct hammer2_fs *hfs, void *buf, size_t nbytes, off_t off)
208 {
209 #if defined(LIBSTAND)
210         size_t rlen;
211 #endif
212         int rc;
213
214 #if defined(TESTING)
215         rc = pread(hfs->fd, &media, nbytes, off);
216         if (rc == (int)nbytes)
217                 rc = 0;
218         else
219                 rc = -1;
220 #elif defined(LIBSTAND)
221         rc = hfs->f->f_dev->dv_strategy(hfs->f->f_devdata, F_READ,
222                                         off >> DEV_BSHIFT, nbytes,
223                                         buf, &rlen);
224         if (rc || rlen != nbytes)
225                 rc = -1;
226 #elif defined(BOOT2)
227         rc = dskread(buf, off >> DEV_BSHIFT, nbytes >> DEV_BSHIFT);
228         if (rc)
229                 rc = -1;
230 #else
231 #error "hammer2: unknown library API"
232 #endif
233         return rc;
234 }
235
236 /*
237  * Common code
238  *
239  * Initialize for HAMMER2 filesystem access given a hammer2_fs with
240  * its device file descriptor initialized.
241  */
242
243 /*
244  * Lookup within the block specified by (*base), loading the block from disk
245  * if necessary.  Locate the first key within the requested range and
246  * recursively run through indirect blocks as needed.  The caller may loop
247  * by setting key_beg to *key_ret.
248  *
249  * Returns 0 if nothing could be found and the key range has been exhausted.
250  * returns -1 if a disk error occured.  Otherwise returns the size of the
251  * data block and returns the data block in *pptr and bref in *bref_ret.
252  *
253  * NOTE! When reading file data, the returned bref's key will be the nearest
254  *       data block we looked up.  The file read procedure must handle any
255  *       zero-fill or skip.  However, we will truncate the return value to
256  *       the file size.
257  */
258 static int
259 h2lookup(struct hammer2_fs *hfs, hammer2_blockref_t *base,
260          hammer2_key_t key_beg, hammer2_key_t key_end,
261          hammer2_blockref_t *bref_ret, void **pptr)
262 {
263         hammer2_blockref_t *bref;
264         hammer2_blockref_t best;
265         hammer2_key_t scan_beg;
266         hammer2_key_t scan_end;
267         int i;
268         int rc;
269         int count;
270         int dev_boff;
271         int dev_bsize;
272
273         if (base == NULL) {
274                 saved_base.data_off = (hammer2_off_t)-1;
275                 return(0);
276         }
277         if (base->data_off == (hammer2_off_t)-1)
278                 return(-1);
279
280         /*
281          * Calculate the number of blockrefs to scan
282          */
283         switch(base->type) {
284         case HAMMER2_BREF_TYPE_VOLUME:
285                 count = HAMMER2_SET_COUNT;
286                 break;
287         case HAMMER2_BREF_TYPE_INODE:
288                 count = HAMMER2_SET_COUNT;
289                 break;
290         case HAMMER2_BREF_TYPE_INDIRECT:
291                 count = blocksize(base) / sizeof(hammer2_blockref_t);
292                 break;
293         }
294
295         /*
296          * Find the best candidate (the lowest blockref within the specified
297          * range).  The array can be fully set associative so make no ordering
298          * assumptions.
299          */
300 again:
301         best.key = HAMMER2_KEY_MAX;
302         best.type = 0;
303
304         for (i = 0; i < count; ++i) {
305                 /*
306                  * [re]load when returning from our recursion
307                  */
308                 if (base->type != HAMMER2_BREF_TYPE_VOLUME &&
309                     base->data_off != saved_base.data_off) {
310                         if (h2read(hfs, &media,
311                                    blocksize(base), blockoff(base))) {
312                                 return(-1);
313                         }
314                         saved_base = *base;
315                 }
316
317                 /*
318                  * Special case embedded file data
319                  */
320                 if (base->type == HAMMER2_BREF_TYPE_INODE) {
321                         if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
322                                 *pptr = media.ipdata.u.data;
323                                 bref_ret->type = HAMMER2_BREF_TYPE_DATA;
324                                 bref_ret->key = 0;
325                                 return HAMMER2_EMBEDDED_BYTES;
326                         }
327                 }
328
329                 /*
330                  * Calculate the bref in our scan.
331                  */
332                 switch(base->type) {
333                 case HAMMER2_BREF_TYPE_VOLUME:
334                         bref = &hfs->sroot_blockset.blockref[i];
335                         break;
336                 case HAMMER2_BREF_TYPE_INODE:
337                         bref = &media.ipdata.u.blockset.blockref[i];
338                         break;
339                 case HAMMER2_BREF_TYPE_INDIRECT:
340                         bref = &media.npdata[i];
341                         break;
342                 }
343                 if (bref->type == 0)
344                         continue;
345                 if (bref->key > best.key)
346                         continue;
347                 scan_beg = bref->key;
348                 scan_end = scan_beg + ((hammer2_key_t)1 << bref->keybits) - 1;
349                 if (scan_end >= key_beg && scan_beg <= key_end) {
350                         best = *bref;
351                 }
352         }
353
354         /*
355          * Figure out what to do with the results.
356          */
357         switch(best.type) {
358         case 0:
359                 /*
360                  * Return 0
361                  */
362                 rc = 0;
363                 break;
364         case HAMMER2_BREF_TYPE_INDIRECT:
365                 /*
366                  * Matched an indirect block.  If the block turns out to
367                  * contain nothing we continue the iteration, otherwise
368                  * we return the data from the recursion.
369                  *
370                  * Be sure to handle the overflow case when recalculating
371                  * key_beg.
372                  */
373                 rc = h2lookup(hfs, &best, key_beg, key_end, bref_ret, pptr);
374                 if (rc == 0) {
375                         key_beg = best.key +
376                                   ((hammer2_key_t)1 << best.keybits);
377                         if (key_beg > best.key && key_beg <= key_end)
378                                 goto again;
379                 }
380                 break;
381         case HAMMER2_BREF_TYPE_INODE:
382         case HAMMER2_BREF_TYPE_DATA:
383                 /*
384                  * Terminal match.  Leaf elements might not be data-aligned.
385                  */
386                 dev_bsize = blocksize(&best);
387                 if (dev_bsize < HAMMER2_LBUFSIZE)
388                         dev_bsize = HAMMER2_LBUFSIZE;
389                 dev_boff = blockoff(&best) -
390                            (blockoff(&best) & ~HAMMER2_LBUFMASK64);
391                 if (h2read(hfs, &media, dev_bsize, blockoff(&best) - dev_boff))
392                         return(-1);
393                 saved_base.data_off = (hammer2_off_t)-1;
394                 *bref_ret = best;
395                 *pptr = media.buf + dev_boff;
396                 rc = blocksize(&best);
397                 break;
398         }
399         return(rc);
400 }
401
402 static
403 void
404 h2resolve(struct hammer2_fs *hfs, const char *path,
405           hammer2_blockref_t *bref, hammer2_inode_data_t **inop)
406 {
407         hammer2_blockref_t bres;
408         hammer2_inode_data_t *ino;
409         hammer2_key_t key;
410         ssize_t bytes;
411         size_t len;
412
413         /*
414          * Start point (superroot)
415          */
416         *bref = hfs->sroot;
417         if (inop)
418                 *inop = NULL;
419
420         /*
421          * Iterate path elements
422          */
423         while (*path) {
424                 while (*path == '/')
425                         ++path;
426                 if (*path == 0) /* terminal */
427                         break;
428
429                 /*
430                  * Calculate path element and look for it in the directory
431                  */
432                 for (len = 0; path[len]; ++len) {
433                         if (path[len] == '/')
434                                 break;
435                 }
436                 key = hammer2_dirhash(path, len);
437                 for (;;) {
438                         bytes = h2lookup(hfs, bref,
439                                          key, key | 0xFFFFU,
440                                          &bres, (void **)&ino);
441                         if (bytes == 0)
442                                 break;
443                         if (len == ino->name_len &&
444                             memcmp(path, ino->filename, len) == 0) {
445                                 if (inop)
446                                         *inop = ino;
447                                 break;
448                         }
449                         key = bres.key + 1;
450                 }
451
452                 /*
453                  * Lookup failure
454                  */
455                 if (bytes == 0) {
456                         bref->data_off = (hammer2_off_t)-1;
457                         break;
458                 }
459
460                 /*
461                  * Check path continuance, inode must be a directory or
462                  * we fail.
463                  */
464                 path += len;
465                 if (*path && ino->type != HAMMER2_OBJTYPE_DIRECTORY) {
466                         bref->data_off = (hammer2_off_t)-1;
467                         break;
468                 }
469                 *bref = bres;
470         }
471 }
472
473 static
474 ssize_t
475 h2readfile(struct hammer2_fs *hfs, hammer2_blockref_t *bref,
476            off_t off, off_t filesize, void *buf, size_t len)
477 {
478         hammer2_blockref_t bres;
479         ssize_t total;
480         ssize_t bytes;
481         ssize_t zfill;
482         char *data;
483
484         /*
485          * EOF edge cases
486          */
487         if (off >= filesize)
488                 return (0);
489         if (off + len > filesize)
490                 len = filesize - off;
491
492         /*
493          * Loop until done 
494          */
495         total = 0;
496         while (len) {
497                 /*
498                  * Find closest bres >= requested offset.
499                  */
500                 bytes = h2lookup(hfs, bref, off, off + len - 1,
501                                  &bres, (void **)&data);
502
503                 if (bytes < 0) {
504                         if (total == 0)
505                                 total = -1;
506                         break;
507                 }
508
509                 /*
510                  * Load the data into the buffer.  First handle a degenerate
511                  * zero-fill case.
512                  */
513                 if (bytes == 0) {
514                         bzero(buf, len);
515                         total += len;
516                         break;
517                 }
518
519                 /*
520                  * Returned record overlaps to the left of the requested
521                  * position.  It must overlap in this case or h2lookup()
522                  * would have returned something else.
523                  */
524                 if (bres.key < off) {
525                         data += off - bres.key;
526                         bytes -= off - bres.key;
527                 }
528
529                 /*
530                  * Returned record overlaps to the right of the requested
531                  * position, handle zero-fill.  Again h2lookup() only returns
532                  * this case if there is an actual overlap.
533                  */
534                 if (bres.key > off) {
535                         zfill = (ssize_t)(bres.key - off);
536                         bzero(buf, zfill);
537                         len -= zfill;
538                         off += zfill;
539                         total += zfill;
540                         buf = (char *)buf + zfill;
541                 }
542
543                 /*
544                  * Trim returned request before copying.
545                  */
546                 if (bytes > len)
547                         bytes = len;
548                 bcopy(data, buf, bytes);
549                 len -= bytes;
550                 off += bytes;
551                 total += bytes;
552                 buf = (char *)buf + bytes;
553         }
554         return (total);
555 }
556
557 static
558 int
559 h2init(struct hammer2_fs *hfs)
560 {
561 #if 0
562         uint32_t crc0;
563 #endif
564         hammer2_tid_t best_tid = 0;
565         void *data;
566         off_t off;
567         int best;
568         int i;
569
570         /*
571          * Find the best volume header.
572          *
573          * WARNING BIOS BUGS: It looks like some BIOSes will implode when
574          * given a disk offset beyond the EOM.  XXX We need to probe the
575          * size of the media and limit our accesses, until then we have
576          * to give up if the first volume header does not have a hammer2
577          * signature.
578          *
579          * XXX Probably still going to be problems w/ HAMMER2 volumes on
580          *     media which is too small w/certain BIOSes.
581          */
582         best = -1;
583         for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
584                 off = i * HAMMER2_ZONE_BYTES64;
585                 if (i)
586                         no_io_error = 1;
587                 if (h2read(hfs, &media, sizeof(media.voldata), off))
588                         break;
589                 if (media.voldata.magic != HAMMER2_VOLUME_ID_HBO)
590                         break;
591                 if (best < 0 || best_tid < media.voldata.mirror_tid) {
592                         best = i;
593                         best_tid = media.voldata.mirror_tid;
594                 }
595         }
596         no_io_error = 0;
597         if (best < 0)
598                 return(-1);
599
600         /*
601          * Reload the best volume header and set up the blockref.
602          * We messed with media, clear the cache before continuing.
603          */
604         off = best * HAMMER2_ZONE_BYTES64;
605         if (h2read(hfs, &media, sizeof(media.voldata), off))
606                 return(-1);
607         hfs->sroot.type = HAMMER2_BREF_TYPE_VOLUME;
608         hfs->sroot.data_off = off;
609         hfs->sroot_blockset = media.voldata.sroot_blockset;
610         h2lookup(hfs, NULL, 0, 0, NULL, NULL);
611
612         /*
613          * Lookup sroot and clear the cache again.
614          */
615         h2lookup(hfs, &hfs->sroot, 0, 0, &hfs->sroot, &data);
616         h2lookup(hfs, NULL, 0, 0, NULL, NULL);
617
618         return (0);
619 }
620
621 /************************************************************************
622  *                              BOOT2 SUPPORT                           *
623  ************************************************************************
624  *
625  */
626 #ifdef BOOT2
627
628 static struct hammer2_fs hfs;
629
630 static int
631 boot2_hammer2_init(void)
632 {
633         if (h2init(&hfs))
634                 return(-1);
635         return(0);
636 }
637
638 static boot2_ino_t
639 boot2_hammer2_lookup(const char *path)
640 {
641         hammer2_blockref_t bref;
642
643         h2resolve(&hfs, path, &bref, NULL);
644         return ((boot2_ino_t)bref.data_off);
645 }
646
647 static ssize_t
648 boot2_hammer2_read(boot2_ino_t ino, void *buf, size_t len)
649 {
650         hammer2_blockref_t bref;
651         ssize_t total;
652
653         bzero(&bref, sizeof(bref));
654         bref.type = HAMMER2_BREF_TYPE_INODE;
655         bref.data_off = ino;
656
657         total = h2readfile(&hfs, &bref, fs_off, 0x7FFFFFFF, buf, len);
658         if (total > 0)
659                 fs_off += total;
660         return total;
661 }
662
663 const struct boot2_fsapi boot2_hammer2_api = {
664         .fsinit = boot2_hammer2_init,
665         .fslookup = boot2_hammer2_lookup,
666         .fsread = boot2_hammer2_read
667 };
668
669 #endif
670
671 /************************************************************************
672  *                              BOOT2 SUPPORT                           *
673  ************************************************************************
674  *
675  */
676 #ifdef LIBSTAND
677
678 struct hfile {
679         struct hammer2_fs hfs;
680         hammer2_blockref_t bref;
681         int64_t         fsize;
682         uint32_t        mode;
683         uint8_t         type;
684 };
685
686 static
687 int
688 hammer2_get_dtype(uint8_t type)
689 {
690         switch(type) {
691         case HAMMER2_OBJTYPE_DIRECTORY:
692                 return(DT_DIR);
693         case HAMMER2_OBJTYPE_REGFILE:
694                 return(DT_REG);
695         case HAMMER2_OBJTYPE_FIFO:
696                 return(DT_FIFO);
697         case HAMMER2_OBJTYPE_CDEV:
698                 return(DT_CHR);
699         case HAMMER2_OBJTYPE_BDEV:
700                 return(DT_BLK);
701         case HAMMER2_OBJTYPE_SOFTLINK:
702                 return(DT_LNK);
703         case HAMMER2_OBJTYPE_HARDLINK:
704                 return(DT_UNKNOWN);
705         case HAMMER2_OBJTYPE_SOCKET:
706                 return(DT_SOCK);
707         default:
708                 return(DT_UNKNOWN);
709         }
710 }
711
712 static
713 mode_t
714 hammer2_get_mode(uint8_t type)
715 {
716         switch(type) {
717         case HAMMER2_OBJTYPE_DIRECTORY:
718                 return(S_IFDIR);
719         case HAMMER2_OBJTYPE_REGFILE:
720                 return(S_IFREG);
721         case HAMMER2_OBJTYPE_FIFO:
722                 return(S_IFIFO);
723         case HAMMER2_OBJTYPE_CDEV:
724                 return(S_IFCHR);
725         case HAMMER2_OBJTYPE_BDEV:
726                 return(S_IFBLK);
727         case HAMMER2_OBJTYPE_SOFTLINK:
728                 return(S_IFLNK);
729         case HAMMER2_OBJTYPE_HARDLINK:
730                 return(0);
731         case HAMMER2_OBJTYPE_SOCKET:
732                 return(S_IFSOCK);
733         default:
734                 return(0);
735         }
736 }
737
738 static int
739 hammer2_open(const char *path, struct open_file *f)
740 {
741         struct hfile *hf = malloc(sizeof(*hf));
742         hammer2_inode_data_t *ipdata;
743
744         bzero(hf, sizeof(*hf));
745         f->f_offset = 0;
746         f->f_fsdata = hf;
747         hf->hfs.f = f;
748
749         if (h2init(&hf->hfs)) {
750                 f->f_fsdata = NULL;
751                 free(hf);
752                 errno = ENOENT;
753                 return(-1);
754         }
755         h2resolve(&hf->hfs, path, &hf->bref, &ipdata);
756         if (hf->bref.data_off == (hammer2_off_t)-1 ||
757             (hf->bref.type != HAMMER2_BREF_TYPE_INODE &&
758             hf->bref.type != HAMMER2_BREF_TYPE_VOLUME)) {
759                 f->f_fsdata = NULL;
760                 free(hf);
761                 errno = ENOENT;
762                 return(-1);
763         }
764         if (ipdata) {
765                 hf->fsize = ipdata->size;
766                 hf->type = ipdata->type;
767                 hf->mode = ipdata->mode | hammer2_get_mode(ipdata->type);
768         } else {
769                 hf->fsize = 0;
770                 hf->type = HAMMER2_OBJTYPE_DIRECTORY;
771                 hf->mode = 0755 | S_IFDIR;
772         }
773         return(0);
774 }
775
776 static int
777 hammer2_close(struct open_file *f)
778 {
779         struct hfile *hf = f->f_fsdata;
780
781         f->f_fsdata = NULL;
782         if (hf)
783                 free(hf);
784         return (0);
785 }
786
787 static int
788 hammer2_read(struct open_file *f, void *buf, size_t len, size_t *resid)
789 {
790         struct hfile *hf = f->f_fsdata;
791         ssize_t total;
792         int rc = 0;
793
794         total = h2readfile(&hf->hfs, &hf->bref,
795                            f->f_offset, hf->fsize, buf, len);
796         if (total < 0) {
797                 rc = EIO;
798                 total = 0;
799         } else {
800                 f->f_offset += total;
801                 rc = 0;
802         }
803         *resid = len - total;
804         return rc;
805 }
806
807 static off_t
808 hammer2_seek(struct open_file *f, off_t offset, int whence)
809 {
810         struct hfile *hf = f->f_fsdata;
811
812         switch (whence) {
813         case SEEK_SET:
814                 f->f_offset = offset;
815                 break;
816         case SEEK_CUR:
817                 f->f_offset += offset;
818                 break;
819         case SEEK_END:
820                 f->f_offset = hf->fsize - offset;
821                 break;
822         default:
823                 return (-1);
824         }
825         return (f->f_offset);
826 }
827
828 static int
829 hammer2_stat(struct open_file *f, struct stat *st)
830 {
831         struct hfile *hf = f->f_fsdata;
832
833         st->st_mode = hf->mode;
834         st->st_uid = 0;
835         st->st_gid = 0;
836         st->st_size = hf->fsize;
837
838         return (0);
839 }
840
841 static int
842 hammer2_readdir(struct open_file *f, struct dirent *den)
843 {
844         struct hfile *hf = f->f_fsdata;
845         hammer2_blockref_t bres;
846         hammer2_inode_data_t *ipdata;
847         int bytes;
848
849         for (;;) {
850                 bytes = h2lookup(&hf->hfs, &hf->bref,
851                                  f->f_offset | HAMMER2_DIRHASH_VISIBLE, 
852                                  HAMMER2_KEY_MAX,
853                                  &bres, (void **)&ipdata);
854                 if (bytes <= 0)
855                         break;
856                 den->d_namlen = ipdata->name_len;
857                 den->d_type = hammer2_get_dtype(ipdata->type);
858                 den->d_ino = ipdata->inum;
859                 bcopy(ipdata->filename, den->d_name, den->d_namlen);
860                 den->d_name[den->d_namlen] = 0;
861
862                 f->f_offset = bres.key + 1;
863
864                 return(0);
865         }
866         return ENOENT;
867 }
868
869 struct fs_ops hammer_fsops = {
870         "hammer2",
871         hammer2_open,
872         hammer2_close,
873         hammer2_read,
874         null_write,
875         hammer2_seek,
876         hammer2_stat,
877         hammer2_readdir
878 };
879
880 #endif