1 /* $NetBSD: v7fs_io_kern.c,v 1.3 2015/03/28 19:24:05 maxv Exp $ */
4 * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: v7fs_io_kern.c,v 1.3 2015/03/28 19:24:05 maxv Exp $");
34 #if defined _KERNEL_OPT
37 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: v7fs_io_kern.c,v 1.3 2015/03/28 19:24:05 maxv Exp $");
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
46 #include <sys/kauth.h>
47 #include <sys/mutex.h>
49 #include <fs/v7fs/v7fs.h>
50 #include "v7fs_endian.h"
51 #include "v7fs_impl.h"
54 #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
56 #define DPRINTF(fmt, args...) ((void)0)
64 static bool v7fs_os_read_n(void *, uint8_t *, daddr_t
, int);
65 static bool v7fs_os_read(void *, uint8_t *, daddr_t
);
66 static bool v7fs_os_write_n(void *, uint8_t *, daddr_t
, int);
67 static bool v7fs_os_write(void *, uint8_t *, daddr_t
);
68 static void v7fs_os_lock(void *);
69 static void v7fs_os_unlock(void *);
70 static bool lock_init(struct lock_ops
*);
73 v7fs_io_init(struct v7fs_self
**fs
,
74 const struct v7fs_mount_device
*mount_device
, size_t block_size
)
76 struct vnode
*vp
= mount_device
->device
.vnode
;
78 struct local_io
*local
;
81 if ((p
= kmem_zalloc(sizeof(*p
), KM_SLEEP
)) == NULL
)
85 p
->scratch_remain
= V7FS_SELF_NSCRATCH
;
88 p
->endian
= mount_device
->endian
;
93 if ((local
= kmem_zalloc(sizeof(*local
), KM_SLEEP
)) == NULL
) {
97 p
->io
.read
= v7fs_os_read
;
98 p
->io
.read_n
= v7fs_os_read_n
;
99 p
->io
.write
= v7fs_os_write
;
100 p
->io
.write_n
= v7fs_os_write_n
;
101 p
->scratch_free
= -1; /* free all scratch buffer */
103 p
->io
.cookie
= local
;
105 local
->cred
= NOCRED
; /* upper layer check cred. */
109 if (!lock_init(&p
->sb_lock
))
111 if (!lock_init(&p
->ilist_lock
))
113 if (!lock_init(&p
->mem_lock
))
126 lock_init(struct lock_ops
*ops
)
128 if ((ops
->cookie
= kmem_zalloc(sizeof(kmutex_t
), KM_SLEEP
)) == NULL
) {
131 mutex_init(ops
->cookie
, MUTEX_DEFAULT
, IPL_NONE
);
132 ops
->lock
= v7fs_os_lock
;
133 ops
->unlock
= v7fs_os_unlock
;
138 v7fs_io_fini(struct v7fs_self
*fs
)
141 kmem_free(fs
->io
.cookie
, sizeof(struct local_io
));
143 if (fs
->sb_lock
.cookie
) {
144 mutex_destroy(fs
->sb_lock
.cookie
);
145 kmem_free(fs
->sb_lock
.cookie
, sizeof(kmutex_t
));
147 if (fs
->ilist_lock
.cookie
) {
148 mutex_destroy(fs
->ilist_lock
.cookie
);
149 kmem_free(fs
->ilist_lock
.cookie
, sizeof(kmutex_t
));
151 if (fs
->mem_lock
.cookie
) {
152 mutex_destroy(fs
->mem_lock
.cookie
);
153 kmem_free(fs
->mem_lock
.cookie
, sizeof(kmutex_t
));
155 kmem_free(fs
, sizeof(*fs
));
159 v7fs_os_read_n(void *self
, uint8_t *buf
, daddr_t block
, int count
)
163 for (i
= 0; i
< count
; i
++) {
164 if (!v7fs_os_read(self
, buf
, block
))
174 v7fs_os_read(void *self
, uint8_t *buf
, daddr_t block
)
176 struct local_io
*bio
= (struct local_io
*)self
;
177 struct buf
*bp
= NULL
;
179 if (bread(bio
->vp
, block
, DEV_BSIZE
, 0, &bp
) != 0)
181 memcpy(buf
, bp
->b_data
, DEV_BSIZE
);
186 DPRINTF("block %ld read failed.\n", (long)block
);
194 v7fs_os_write_n(void *self
, uint8_t *buf
, daddr_t block
, int count
)
198 for (i
= 0; i
< count
; i
++) {
199 if (!v7fs_os_write(self
, buf
, block
))
209 v7fs_os_write(void *self
, uint8_t *buf
, daddr_t block
)
211 struct local_io
*bio
= (struct local_io
*)self
;
214 if ((bp
= getblk(bio
->vp
, block
, DEV_BSIZE
, 0, 0)) == 0) {
215 DPRINTF("getblk failed. block=%ld\n", (long)block
);
219 memcpy(bp
->b_data
, buf
, DEV_BSIZE
);
221 if (bwrite(bp
) != 0) {
222 DPRINTF("bwrite failed. block=%ld\n", (long)block
);
230 v7fs_os_lock(void *self
)
233 mutex_enter((kmutex_t
*)self
);
237 v7fs_os_unlock(void *self
)
240 mutex_exit((kmutex_t
*)self
);