usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / usr.sbin / makefs / hammer2 / hammer2_compat.h
blobaff6f68528e80be80875424b333f917cd73dea2e
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2022 Tomohiro Kusumi <tkusumi@netbsd.org>
5 * Copyright (c) 2011-2022 The DragonFly Project. All rights reserved.
7 * This code is derived from software contributed to The DragonFly Project
8 * by Matthew Dillon <dillon@dragonflybsd.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 #ifndef _HAMMER2_HAMMER2_COMPAT_H
39 #define _HAMMER2_HAMMER2_COMPAT_H
41 #include <sys/statvfs.h>
42 #include <sys/spinlock.h>
43 #include <sys/uio.h> /* struct iovec */
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <util.h> /* ecalloc */
49 #include <assert.h>
50 #include <err.h>
52 #include "ffs/buf.h"
54 #define INVARIANTS
56 #define MALLOC_DECLARE(type) struct __hack
57 #define MALLOC_DEFINE(type, shortdesc, longdesc) struct __hack
59 #define SYSCTL_NODE(parent, nbr, name, access, handler, descr) struct __hack
60 #define SYSCTL_INT(parent, nbr, name, access, ptr, val, descr) struct __hack
61 #define SYSCTL_LONG(parent, nbr, name, access, ptr, val, descr) struct __hack
63 #define VFS_SET(vfsops, fsname, flags) struct __hack
65 #define MODULE_VERSION(module, version) struct __hack
67 #define VOP_FSYNC(vp, waitfor, flags) (0)
69 #define kprintf(s, ...) printf(s, ## __VA_ARGS__)
70 #define krateprintf(r, X, ...) kprintf(X, ## __VA_ARGS__)
71 #define ksnprintf(s, n, ...) snprintf(s, n, ## __VA_ARGS__)
72 #define kstrdup(str, type) strdup(str)
74 #define kmalloc_create(typep, descr) do{}while(0)
75 #define kmalloc_destroy(typep) do{}while(0)
76 #define kmalloc(size, type, flags) ecalloc(1, size)
77 #define krealloc(addr, size, type, flags) realloc(addr, size)
78 #define kfree(addr, type) free(addr)
80 #define kmalloc_create_obj(typep, descr, objsize) do{}while(0)
81 #define kmalloc_destroy_obj(type) do{}while(0)
82 #define kmalloc_obj(size, type, flags) ecalloc(1, size)
83 #define kfree_obj(addr, type) free(addr)
85 #define kmalloc_raise_limit(typep, bytes) do{}while(0)
87 #define KASSERT(exp, msg) do { if (!(exp)) panic msg; } while(0)
88 //#define KASSERT(exp, msg) assert(exp)
89 #define KKASSERT(exp) assert(exp)
91 #define __debugvar
93 #define panic(s, ...) errx(1, s, ## __VA_ARGS__)
95 #define ERESTART (-1)
97 #define LK_SHARED 0x00000001
98 #define LK_EXCLUSIVE 0x00000002
99 #define LK_RELEASE 0x00000006
100 #define LK_NOWAIT 0x00000010
101 #define LK_RETRY 0x00020000
102 #define LK_PCATCH 0x04000000
104 #define MTX_EXCLUSIVE 0x80000000
105 #define MTX_MASK 0x0FFFFFFF
107 #define IO_APPEND 0x0002
108 #define IO_SYNC 0x0004
109 #define IO_ASYNC 0x0080
110 #define IO_DIRECT 0x0100
111 #define IO_RECURSE 0x0200
113 #define IO_SEQMAX 0x7F
114 #define IO_SEQSHIFT 16
116 #define VROOT 0x00000001
117 #define VKVABIO 0x00000040
118 #define VLASTWRITETS 0x00080000
120 #define VNOVAL (-1)
122 #define FORCECLOSE 0x0002
124 #define GETBLK_BHEAVY 0x0002
125 #define GETBLK_KVABIO 0x0010
127 #define NOOFFSET (-1LL)
129 #define KNOTE(list, hint)
131 #define NOTE_DELETE 0x0001
132 #define NOTE_WRITE 0x0002
133 #define NOTE_EXTEND 0x0004
134 #define NOTE_LINK 0x0010
136 extern int hz;
137 extern int ticks;
138 extern int64_t vnode_count;
140 struct thread {
141 void *td_proc;
144 typedef struct thread *thread_t;
145 extern struct thread *curthread;
147 struct lwkt_token {
150 typedef struct lwkt_token *lwkt_token_t;
152 struct mount {
153 int mnt_flag;
154 int mnt_kern_flag;
155 struct statfs mnt_stat;
156 struct statvfs mnt_vstat;
157 qaddr_t mnt_data;
158 unsigned int mnt_iosize_max;
161 struct bio {
162 struct m_buf *bio_buf;
163 off_t bio_offset;
166 struct bio_track {
169 struct namecache {
170 u_char nc_nlen;
171 char *nc_name;
174 struct nchandle {
175 struct namecache *ncp;
178 struct vop_generic_args {
179 int a_reserved[4];
182 struct vop_open_args {
183 struct m_vnode *a_vp;
184 int a_mode;
185 struct ucred *a_cred;
186 struct file **a_fpp;
189 struct vop_close_args {
190 struct m_vnode *a_vp;
191 int a_fflag;
192 struct file *a_fp;
195 struct vop_access_args {
196 struct m_vnode *a_vp;
197 int a_mode;
198 int a_flags;
199 struct ucred *a_cred;
202 struct vop_getattr_args {
203 struct m_vnode *a_vp;
204 struct vattr *a_vap;
207 struct vop_getattr_lite_args {
208 struct m_vnode *a_vp;
209 struct vattr_lite *a_lvap;
212 struct vop_setattr_args {
213 struct m_vnode *a_vp;
214 struct vattr *a_vap;
215 struct ucred *a_cred;
218 struct vop_read_args {
219 struct m_vnode *a_vp;
220 struct uio *a_uio;
221 int a_ioflag;
222 struct ucred *a_cred;
225 struct vop_write_args {
226 struct m_vnode *a_vp;
227 struct uio *a_uio;
228 int a_ioflag;
229 struct ucred *a_cred;
232 struct vop_ioctl_args {
233 struct m_vnode *a_vp;
234 u_long a_command;
235 caddr_t a_data;
236 int a_fflag;
237 struct ucred *a_cred;
238 struct sysmsg *a_sysmsg;
241 struct vop_kqfilter_args {
242 struct m_vnode *a_vp;
243 struct knote *a_kn;
246 struct vop_fsync_args {
247 struct m_vnode *a_vp;
248 int a_waitfor;
249 int a_flags;
252 struct vop_readdir_args {
253 struct m_vnode *a_vp;
254 struct uio *a_uio;
255 struct ucred *a_cred;
256 int *a_eofflag;
257 int *a_ncookies;
258 off_t **a_cookies;
259 int *a_ndirent; /* makefs */
262 struct vop_readlink_args {
263 struct m_vnode *a_vp;
264 struct uio *a_uio;
265 struct ucred *a_cred;
268 struct vop_inactive_args {
269 struct m_vnode *a_vp;
272 struct vop_reclaim_args {
273 struct m_vnode *a_vp;
276 struct vop_bmap_args {
277 struct m_vnode *a_vp;
278 off_t a_loffset;
279 off_t *a_doffsetp;
280 int *a_runp;
281 int *a_runb;
282 buf_cmd_t a_cmd;
285 struct vop_strategy_args {
286 struct m_vnode *a_vp;
287 struct bio *a_bio;
290 struct vop_advlock_args {
291 struct m_vnode *a_vp;
292 caddr_t a_id;
293 int a_op;
294 struct flock *a_fl;
295 int a_flags;
298 struct vop_getpages_args {
299 struct m_vnode *a_vp;
300 int a_count;
301 int a_reqpage;
302 //vm_ooffset_t a_offset;
303 int a_seqaccess;
306 struct vop_putpages_args {
307 struct m_vnode *a_vp;
308 int a_count;
309 int a_flags;
310 int *a_rtvals;
311 //vm_ooffset_t a_offset;
314 struct vop_mountctl_args {
315 int a_op;
316 struct file *a_fp;
317 const void *a_ctl;
318 int a_ctllen;
319 void *a_buf;
320 int a_buflen;
321 int *a_res;
322 struct m_vnode *a_vp;
325 struct vop_markatime_args {
326 int a_op;
327 struct m_vnode *a_vp;
328 struct ucred *a_cred;
331 struct vop_nresolve_args {
332 struct nchandle *a_nch;
333 struct m_vnode *a_dvp;
334 struct ucred *a_cred;
335 struct m_vnode **a_vpp; /* makefs */
338 struct vop_nlookupdotdot_args {
339 struct m_vnode *a_dvp;
340 struct m_vnode **a_vpp;
341 struct ucred *a_cred;
342 char **a_fakename;
345 struct vop_ncreate_args {
346 struct nchandle *a_nch;
347 struct m_vnode *a_dvp;
348 struct m_vnode **a_vpp;
349 struct ucred *a_cred;
350 struct vattr *a_vap;
353 struct vop_nmkdir_args {
354 struct nchandle *a_nch;
355 struct m_vnode *a_dvp;
356 struct m_vnode **a_vpp;
357 struct ucred *a_cred;
358 struct vattr *a_vap;
361 struct vop_nmknod_args {
362 struct nchandle *a_nch;
363 struct m_vnode *a_dvp;
364 struct m_vnode **a_vpp;
365 struct ucred *a_cred;
366 struct vattr *a_vap;
369 struct vop_nlink_args {
370 struct nchandle *a_nch;
371 struct m_vnode *a_dvp;
372 struct m_vnode *a_vp;
373 struct ucred *a_cred;
376 struct vop_nsymlink_args {
377 struct nchandle *a_nch;
378 struct m_vnode *a_dvp;
379 struct m_vnode **a_vpp;
380 struct ucred *a_cred;
381 struct vattr *a_vap;
382 char *a_target;
385 struct vop_nremove_args {
386 struct nchandle *a_nch;
387 struct m_vnode *a_dvp;
388 struct ucred *a_cred;
391 struct vop_nrmdir_args {
392 struct nchandle *a_nch;
393 struct m_vnode *a_dvp;
394 struct ucred *a_cred;
397 struct vop_nrename_args {
398 struct nchandle *a_fnch;
399 struct nchandle *a_tnch;
400 struct m_vnode *a_fdvp;
401 struct m_vnode *a_tdvp;
402 struct ucred *a_cred;
405 #define vop_defaultop NULL
406 #define vop_stdgetpages NULL
407 #define vop_stdputpages NULL
408 #define vop_stdnoread NULL
409 #define vop_stdnowrite NULL
410 #define fifo_vnoperate NULL
412 struct vop_ops {
413 int (*vop_default)(struct vop_generic_args *);
414 int (*vop_open)(struct vop_open_args *);
415 int (*vop_close)(struct vop_close_args *);
416 int (*vop_access)(struct vop_access_args *);
417 int (*vop_getattr)(struct vop_getattr_args *);
418 int (*vop_getattr_lite)(struct vop_getattr_lite_args *);
419 int (*vop_setattr)(struct vop_setattr_args *);
420 int (*vop_read)(struct vop_read_args *);
421 int (*vop_write)(struct vop_write_args *);
422 int (*vop_ioctl)(struct vop_ioctl_args *);
423 int (*vop_kqfilter)(struct vop_kqfilter_args *);
424 int (*vop_fsync)(struct vop_fsync_args *);
425 int (*vop_readdir)(struct vop_readdir_args *);
426 int (*vop_readlink)(struct vop_readlink_args *);
427 int (*vop_inactive)(struct vop_inactive_args *);
428 int (*vop_reclaim)(struct vop_reclaim_args *);
429 int (*vop_bmap)(struct vop_bmap_args *);
430 int (*vop_strategy)(struct vop_strategy_args *);
431 int (*vop_advlock)(struct vop_advlock_args *);
432 int (*vop_getpages)(struct vop_getpages_args *);
433 int (*vop_putpages)(struct vop_putpages_args *);
434 int (*vop_mountctl)(struct vop_mountctl_args *);
435 int (*vop_markatime)(struct vop_markatime_args *);
436 int (*vop_nresolve)(struct vop_nresolve_args *);
437 int (*vop_nlookupdotdot)(struct vop_nlookupdotdot_args *);
438 int (*vop_ncreate)(struct vop_ncreate_args *);
439 int (*vop_nmkdir)(struct vop_nmkdir_args *);
440 int (*vop_nmknod)(struct vop_nmknod_args *);
441 int (*vop_nlink)(struct vop_nlink_args *);
442 int (*vop_nsymlink)(struct vop_nsymlink_args *);
443 int (*vop_nremove)(struct vop_nremove_args *);
444 int (*vop_nrmdir)(struct vop_nrmdir_args *);
445 int (*vop_nrename)(struct vop_nrename_args *);
448 enum uio_seg {
449 UIO_USERSPACE,
450 UIO_SYSSPACE,
451 UIO_NOCOPY,
454 enum uio_rw {
455 UIO_READ,
456 UIO_WRITE
459 struct uio {
460 struct iovec *uio_iov;
461 int uio_iovcnt;
462 off_t uio_offset;
463 size_t uio_resid;
464 enum uio_seg uio_segflg;
465 enum uio_rw uio_rw;
466 struct thread *uio_td;
470 * Since makefs(8) is a single thread program, there should never be any
471 * lock contention. Therefore, lock(9)/mutex(9)/spinlock(9) emulation always
472 * succeed. Similarly, tsleep(9) should never be called.
474 struct lock {
475 int reserved;
478 typedef struct {
479 unsigned int mtx_lock;
480 } mtx_t;
482 typedef mtx_t hammer2_mtx_t;
484 typedef u_int mtx_state_t;
485 typedef mtx_state_t hammer2_mtx_state_t;
487 typedef struct spinlock hammer2_spin_t;
489 static __inline
490 void
491 cpu_pause(void)
495 static __inline
496 void
497 cpu_mfence(void)
501 static __inline
502 void
503 cpu_lfence(void)
507 static __inline
508 void
509 cpu_sfence(void)
513 static __inline
514 void
515 cpu_ccfence(void)
519 static __inline
520 void
521 trigger_syncer(struct mount *mp)
525 static __inline
526 void
527 trigger_syncer_start(struct mount *mp)
531 static __inline
532 void
533 trigger_syncer_stop(struct mount *mp)
537 static __inline
539 vfs_mountedon(struct m_vnode *vp)
541 return (0);
544 static __inline
545 uid_t
546 vop_helper_create_uid(struct mount *mp, mode_t dmode, uid_t duid,
547 struct ucred *cred, mode_t *modep)
549 return (getuid());
552 static __inline
554 vinitvmio(struct m_vnode *vp, off_t filesize, int blksize, int boff)
556 return (0);
559 static __inline
561 getnewvnode(enum vtagtype tag, struct mount *mp, struct m_vnode **vpp,
562 int lktimeout, int lkflags)
564 struct m_vnode *vp;
566 vp = ecalloc(1, sizeof(*vp));
567 vp->v_logical = 1;
568 vp->v_malloced = 1;
569 *vpp = vp;
571 vnode_count++;
573 return (0);
576 /* not freesomevnodes() */
577 static __inline
578 void
579 freevnode(struct m_vnode *vp)
581 assert(vp->v_malloced);
582 free(vp);
584 vnode_count--;
587 static __inline
589 vn_lock(struct m_vnode *vp, int flags)
591 return (0);
594 static __inline
595 void
596 vn_unlock(struct m_vnode *vp)
600 static __inline
602 vget(struct m_vnode *vp, int flags)
604 return (0);
607 static __inline
608 void
609 vput(struct m_vnode *vp)
613 static __inline
614 void
615 vrele(struct m_vnode *vp)
619 static __inline
620 void
621 vhold(struct m_vnode *vp)
625 static __inline
626 void
627 vdrop(struct m_vnode *vp)
631 static __inline
632 void
633 vx_put(struct m_vnode *vp)
637 static __inline
638 void
639 vx_downgrade(struct m_vnode *vp)
643 static __inline
644 void
645 vfinalize(struct m_vnode *vp)
649 static __inline
650 void
651 vsetflags(struct m_vnode *vp, int flags)
655 static __inline
656 void
657 vclrflags(struct m_vnode *vp, int flags)
661 static __inline
662 void
663 vsetisdirty(struct m_vnode *vp)
667 static __inline
668 void
669 vclrisdirty(struct m_vnode *vp)
673 static __inline
675 vfsync(struct m_vnode *vp, int waitfor, int passes,
676 int (*checkdef)(struct m_buf *),
677 int (*waitoutput)(struct m_vnode *, struct thread *))
679 return (0);
682 static __inline
684 nvtruncbuf(struct m_vnode *vp, off_t length, int blksize, int boff, int flags)
686 return (0);
689 static __inline
691 nvextendbuf(struct m_vnode *vp, off_t olength, off_t nlength, int oblksize,
692 int nblksize, int oboff, int nboff, int flags)
694 return (0);
697 static __inline
698 void
699 addaliasu(struct m_vnode *vp, int x, int y)
703 static __inline
704 void
705 bheavy(struct m_buf *bp)
709 static __inline
710 void
711 bkvasync(struct m_buf *bp)
715 static __inline
716 void
717 bwillwrite(int bytes)
721 static __inline
722 void
723 BUF_KERNPROC(struct m_buf *bp)
727 static __inline
729 bio_track_wait(struct bio_track *track, int slp_flags, int slp_timo)
731 return (0);
734 static __inline
735 void
736 cache_setvp(struct nchandle *nch, struct m_vnode *vp)
740 static __inline
741 void
742 cache_setunresolved(struct nchandle *nch)
746 static __inline
747 void
748 lockinit(struct lock *lkp, const char *wmesg, int timo, int flags)
752 static __inline
754 lockmgr(struct lock *lkp, uint32_t flags)
756 return (0);
759 static __inline
761 hammer2_mtx_ex(hammer2_mtx_t *mtx)
763 mtx->mtx_lock |= MTX_EXCLUSIVE;
764 mtx->mtx_lock++;
765 return (0);
768 static __inline
770 hammer2_mtx_ex_try(hammer2_mtx_t *mtx)
772 mtx->mtx_lock |= MTX_EXCLUSIVE;
773 mtx->mtx_lock++;
774 return (0);
777 static __inline
779 hammer2_mtx_sh(hammer2_mtx_t *mtx)
781 mtx->mtx_lock |= MTX_EXCLUSIVE;
782 mtx->mtx_lock++;
783 return (0);
786 static __inline
787 void
788 hammer2_mtx_sh_again(hammer2_mtx_t *mtx)
790 mtx->mtx_lock |= MTX_EXCLUSIVE;
791 mtx->mtx_lock++;
794 static __inline
796 hammer2_mtx_sh_try(hammer2_mtx_t *mtx)
798 mtx->mtx_lock |= MTX_EXCLUSIVE;
799 mtx->mtx_lock++;
800 return (0);
803 static __inline
804 void
805 hammer2_mtx_unlock(hammer2_mtx_t *mtx)
807 mtx->mtx_lock &= ~MTX_EXCLUSIVE;
808 mtx->mtx_lock--;
811 static __inline
813 hammer2_mtx_upgrade_try(hammer2_mtx_t *mtx)
815 return (0);
818 static __inline
820 hammer2_mtx_downgrade(hammer2_mtx_t *mtx)
822 return (0);
825 static __inline
827 hammer2_mtx_owned(hammer2_mtx_t *mtx)
829 return (1); /* XXX for asserts */
832 static __inline
833 void
834 hammer2_mtx_init(hammer2_mtx_t *mtx, const char *ident)
836 mtx->mtx_lock = 0;
839 static __inline
840 hammer2_mtx_state_t
841 hammer2_mtx_temp_release(hammer2_mtx_t *mtx)
843 return (0);
846 static __inline
847 void
848 hammer2_mtx_temp_restore(hammer2_mtx_t *mtx, hammer2_mtx_state_t state)
852 static __inline
854 hammer2_mtx_refs(hammer2_mtx_t *mtx)
856 return (mtx->mtx_lock & MTX_MASK);
859 static __inline
860 void
861 hammer2_spin_init(hammer2_spin_t *mtx, const char *ident)
863 mtx->lock = 0;
866 static __inline
867 void
868 hammer2_spin_sh(hammer2_spin_t *mtx)
870 mtx->lock++;
873 static __inline
874 void
875 hammer2_spin_ex(hammer2_spin_t *mtx)
877 mtx->lock++;
880 static __inline
881 void
882 hammer2_spin_unsh(hammer2_spin_t *mtx)
884 mtx->lock--;
887 static __inline
888 void
889 hammer2_spin_unex(hammer2_spin_t *mtx)
891 mtx->lock--;
894 static __inline
895 void
896 lwkt_gettoken(lwkt_token_t tok)
900 static __inline
901 void
902 lwkt_reltoken(lwkt_token_t tok)
906 static __inline
907 void
908 lwkt_yield(void)
912 static __inline
914 tsleep(const volatile void *ident, int flags, const char *wmesg, int timo)
916 assert(0);
919 static __inline
920 void
921 tsleep_interlock(const volatile void *ident, int flags)
923 assert(0);
926 static __inline
927 void
928 wakeup(const volatile void *ident)
930 assert(0);
933 static __inline
934 void
935 print_backtrace(int count)
939 static __inline
940 void
941 Debugger(const char *msg)
943 panic("%s: %s", __func__, msg);
946 #endif /* _HAMMER2_HAMMER2_COMPAT_H */