Sat Jan 26 00:19:39 2013 UTC ()
make the buffer functions look exactly like the kernel ones and add other
cruft to make the kernel files compile.


(christos)
diff -r1.53 -r1.54 src/usr.sbin/makefs/ffs.c
diff -r1.12 -r1.13 src/usr.sbin/makefs/ffs/buf.c
diff -r1.2 -r1.3 src/usr.sbin/makefs/ffs/buf.h
diff -r1.20 -r1.21 src/usr.sbin/makefs/ffs/ffs_alloc.c
diff -r1.14 -r1.15 src/usr.sbin/makefs/ffs/ffs_balloc.c

cvs diff -r1.53 -r1.54 src/usr.sbin/makefs/ffs.c (expand / switch to unified diff)

--- src/usr.sbin/makefs/ffs.c 2013/01/24 01:10:47 1.53
+++ src/usr.sbin/makefs/ffs.c 2013/01/26 00:19:39 1.54
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ffs.c,v 1.53 2013/01/24 01:10:47 christos Exp $ */ 1/* $NetBSD: ffs.c,v 1.54 2013/01/26 00:19:39 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001 Wasabi Systems, Inc. 4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Written by Luke Mewburn for Wasabi Systems, Inc. 7 * Written by Luke Mewburn for Wasabi Systems, Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -61,27 +61,27 @@ @@ -61,27 +61,27 @@
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 65 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
66 */ 66 */
67 67
68#if HAVE_NBTOOL_CONFIG_H 68#if HAVE_NBTOOL_CONFIG_H
69#include "nbtool_config.h" 69#include "nbtool_config.h"
70#endif 70#endif
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73#if defined(__RCSID) && !defined(__lint) 73#if defined(__RCSID) && !defined(__lint)
74__RCSID("$NetBSD: ffs.c,v 1.53 2013/01/24 01:10:47 christos Exp $"); 74__RCSID("$NetBSD: ffs.c,v 1.54 2013/01/26 00:19:39 christos Exp $");
75#endif /* !__lint */ 75#endif /* !__lint */
76 76
77#include <sys/param.h> 77#include <sys/param.h>
78 78
79#if !HAVE_NBTOOL_CONFIG_H 79#if !HAVE_NBTOOL_CONFIG_H
80#include <sys/mount.h> 80#include <sys/mount.h>
81#endif 81#endif
82 82
83#include <assert.h> 83#include <assert.h>
84#include <errno.h> 84#include <errno.h>
85#include <fcntl.h> 85#include <fcntl.h>
86#include <stdarg.h> 86#include <stdarg.h>
87#include <stdio.h> 87#include <stdio.h>
@@ -935,27 +935,27 @@ ffs_write_file(union dinode *din, uint32 @@ -935,27 +935,27 @@ ffs_write_file(union dinode *din, uint32
935 errno = ffs_balloc(&in, offset, chunk, &bp); 935 errno = ffs_balloc(&in, offset, chunk, &bp);
936 bad_ffs_write_file: 936 bad_ffs_write_file:
937 if (errno != 0) 937 if (errno != 0)
938 err(1, 938 err(1,
939 "Writing inode %d (%s), bytes %lld + %lld", 939 "Writing inode %d (%s), bytes %lld + %lld",
940 ino, 940 ino,
941 isfile ? (char *)buf : 941 isfile ? (char *)buf :
942 inode_type(DIP(din, mode) & S_IFMT), 942 inode_type(DIP(din, mode) & S_IFMT),
943 (long long)offset, (long long)chunk); 943 (long long)offset, (long long)chunk);
944 memcpy(bp->b_data, p, chunk); 944 memcpy(bp->b_data, p, chunk);
945 errno = bwrite(bp); 945 errno = bwrite(bp);
946 if (errno != 0) 946 if (errno != 0)
947 goto bad_ffs_write_file; 947 goto bad_ffs_write_file;
948 brelse(bp); 948 brelse(bp, 0);
949 if (!isfile) 949 if (!isfile)
950 p += chunk; 950 p += chunk;
951 } 951 }
952  952
953 write_inode_and_leave: 953 write_inode_and_leave:
954 ffs_write_inode(&in.i_din, in.i_number, fsopts); 954 ffs_write_inode(&in.i_din, in.i_number, fsopts);
955 955
956 leave_ffs_write_file: 956 leave_ffs_write_file:
957 if (fbuf) 957 if (fbuf)
958 free(fbuf); 958 free(fbuf);
959 if (ffd != -1) 959 if (ffd != -1)
960 close(ffd); 960 close(ffd);
961} 961}

cvs diff -r1.12 -r1.13 src/usr.sbin/makefs/ffs/buf.c (expand / switch to unified diff)

--- src/usr.sbin/makefs/ffs/buf.c 2004/06/20 22:20:18 1.12
+++ src/usr.sbin/makefs/ffs/buf.c 2013/01/26 00:19:39 1.13
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $ */ 1/* $NetBSD: buf.c,v 1.13 2013/01/26 00:19:39 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001 Wasabi Systems, Inc. 4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Written by Luke Mewburn for Wasabi Systems, Inc. 7 * Written by Luke Mewburn for Wasabi Systems, Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -31,87 +31,89 @@ @@ -31,87 +31,89 @@
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE. 35 * POSSIBILITY OF SUCH DAMAGE.
36 */ 36 */
37 37
38#if HAVE_NBTOOL_CONFIG_H 38#if HAVE_NBTOOL_CONFIG_H
39#include "nbtool_config.h" 39#include "nbtool_config.h"
40#endif 40#endif
41 41
42#include <sys/cdefs.h> 42#include <sys/cdefs.h>
43#if defined(__RCSID) && !defined(__lint) 43#if defined(__RCSID) && !defined(__lint)
44__RCSID("$NetBSD: buf.c,v 1.12 2004/06/20 22:20:18 jmc Exp $"); 44__RCSID("$NetBSD: buf.c,v 1.13 2013/01/26 00:19:39 christos Exp $");
45#endif /* !__lint */ 45#endif /* !__lint */
46 46
47#include <sys/param.h> 47#include <sys/param.h>
48#include <sys/time.h> 48#include <sys/time.h>
49 49
50#include <assert.h> 50#include <assert.h>
51#include <errno.h> 51#include <errno.h>
52#include <stdio.h> 52#include <stdio.h>
53#include <stdlib.h> 53#include <stdlib.h>
54#include <unistd.h> 54#include <unistd.h>
55 55
56#include "makefs.h" 56#include "makefs.h"
57 57
58#include <ufs/ufs/dinode.h> 58#include <ufs/ufs/dinode.h>
59#include <ufs/ffs/fs.h> 59#include <ufs/ffs/fs.h>
60 60
61#include "ffs/buf.h" 61#include "ffs/buf.h"
62#include "ffs/ufs_inode.h" 62#include "ffs/ufs_inode.h"
63 63
64extern int sectorsize; /* XXX: from ffs.c & mkfs.c */ 64extern int sectorsize; /* XXX: from ffs.c & mkfs.c */
65 65
66TAILQ_HEAD(buftailhead,buf) buftail; 66TAILQ_HEAD(buftailhead,buf) buftail;
67 67
68int 68int
69bread(int fd, struct fs *fs, daddr_t blkno, int size, struct buf **bpp) 69bread(struct vnode *vp, daddr_t blkno, int size, struct kauth_cred *u1 __unused,
 70 int u2 __unused, struct buf **bpp)
70{ 71{
71 off_t offset; 72 off_t offset;
72 ssize_t rv; 73 ssize_t rv;
 74 struct fs *fs = vp->fs;
73 75
74 assert (fs != NULL); 76 assert (fs != NULL);
75 assert (bpp != NULL); 77 assert (bpp != NULL);
76 78
77 if (debug & DEBUG_BUF_BREAD) 79 if (debug & DEBUG_BUF_BREAD)
78 printf("bread: fs %p blkno %lld size %d\n", 80 printf("bread: fs %p blkno %lld size %d\n",
79 fs, (long long)blkno, size); 81 fs, (long long)blkno, size);
80 *bpp = getblk(fd, fs, blkno, size); 82 *bpp = getblk(vp, blkno, size, 0, 0);
81 offset = (*bpp)->b_blkno * sectorsize; /* XXX */ 83 offset = (*bpp)->b_blkno * sectorsize; /* XXX */
82 if (debug & DEBUG_BUF_BREAD) 84 if (debug & DEBUG_BUF_BREAD)
83 printf("bread: bp %p blkno %lld offset %lld bcount %ld\n", 85 printf("bread: bp %p blkno %lld offset %lld bcount %ld\n",
84 (*bpp), (long long)(*bpp)->b_blkno, (long long) offset, 86 (*bpp), (long long)(*bpp)->b_blkno, (long long) offset,
85 (*bpp)->b_bcount); 87 (*bpp)->b_bcount);
86 if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1) 88 if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1)
87 err(1, "bread: lseek %lld (%lld)", 89 err(1, "bread: lseek %lld (%lld)",
88 (long long)(*bpp)->b_blkno, (long long)offset); 90 (long long)(*bpp)->b_blkno, (long long)offset);
89 rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount); 91 rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount);
90 if (debug & DEBUG_BUF_BREAD) 92 if (debug & DEBUG_BUF_BREAD)
91 printf("bread: read %ld (%lld) returned %d\n", 93 printf("bread: read %ld (%lld) returned %d\n",
92 (*bpp)->b_bcount, (long long)offset, (int)rv); 94 (*bpp)->b_bcount, (long long)offset, (int)rv);
93 if (rv == -1) /* read error */ 95 if (rv == -1) /* read error */
94 err(1, "bread: read %ld (%lld) returned %d", 96 err(1, "bread: read %ld (%lld) returned %d",
95 (*bpp)->b_bcount, (long long)offset, (int)rv); 97 (*bpp)->b_bcount, (long long)offset, (int)rv);
96 else if (rv != (*bpp)->b_bcount) /* short read */ 98 else if (rv != (*bpp)->b_bcount) /* short read */
97 err(1, "bread: read %ld (%lld) returned %d", 99 err(1, "bread: read %ld (%lld) returned %d",
98 (*bpp)->b_bcount, (long long)offset, (int)rv); 100 (*bpp)->b_bcount, (long long)offset, (int)rv);
99 else 101 else
100 return (0); 102 return (0);
101} 103}
102 104
103void 105void
104brelse(struct buf *bp) 106brelse(struct buf *bp, int u1 __unused)
105{ 107{
106 108
107 assert (bp != NULL); 109 assert (bp != NULL);
108 assert (bp->b_data != NULL); 110 assert (bp->b_data != NULL);
109 111
110 if (bp->b_lblkno < 0) { 112 if (bp->b_lblkno < 0) {
111 /* 113 /*
112 * XXX don't remove any buffers with negative logical block 114 * XXX don't remove any buffers with negative logical block
113 * numbers (lblkno), so that we retain the mapping 115 * numbers (lblkno), so that we retain the mapping
114 * of negative lblkno -> real blkno that ffs_balloc() 116 * of negative lblkno -> real blkno that ffs_balloc()
115 * sets up. 117 * sets up.
116 * 118 *
117 * if we instead released these buffers, and implemented 119 * if we instead released these buffers, and implemented
@@ -170,32 +172,36 @@ bcleanup(void) @@ -170,32 +172,36 @@ bcleanup(void)
170 if (TAILQ_EMPTY(&buftail)) 172 if (TAILQ_EMPTY(&buftail))
171 return; 173 return;
172 174
173 printf("bcleanup: unflushed buffers:\n"); 175 printf("bcleanup: unflushed buffers:\n");
174 TAILQ_FOREACH(bp, &buftail, b_tailq) { 176 TAILQ_FOREACH(bp, &buftail, b_tailq) {
175 printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n", 177 printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n",
176 (long long)bp->b_lblkno, (long long)bp->b_blkno, 178 (long long)bp->b_lblkno, (long long)bp->b_blkno,
177 bp->b_bcount, bp->b_bufsize); 179 bp->b_bcount, bp->b_bufsize);
178 } 180 }
179 printf("bcleanup: done\n"); 181 printf("bcleanup: done\n");
180} 182}
181 183
182struct buf * 184struct buf *
183getblk(int fd, struct fs *fs, daddr_t blkno, int size) 185getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused,
 186 int u2 __unused)
184{ 187{
185 static int buftailinitted; 188 static int buftailinitted;
186 struct buf *bp; 189 struct buf *bp;
187 void *n; 190 void *n;
 191 int fd = vp->fd;
 192 struct fs *fs = vp->fs;
188 193
 194 blkno += vp->offset;
189 assert (fs != NULL); 195 assert (fs != NULL);
190 if (debug & DEBUG_BUF_GETBLK) 196 if (debug & DEBUG_BUF_GETBLK)
191 printf("getblk: fs %p blkno %lld size %d\n", fs, 197 printf("getblk: fs %p blkno %lld size %d\n", fs,
192 (long long)blkno, size); 198 (long long)blkno, size);
193 199
194 bp = NULL; 200 bp = NULL;
195 if (!buftailinitted) { 201 if (!buftailinitted) {
196 if (debug & DEBUG_BUF_GETBLK) 202 if (debug & DEBUG_BUF_GETBLK)
197 printf("getblk: initialising tailq\n"); 203 printf("getblk: initialising tailq\n");
198 TAILQ_INIT(&buftail); 204 TAILQ_INIT(&buftail);
199 buftailinitted = 1; 205 buftailinitted = 1;
200 } else { 206 } else {
201 TAILQ_FOREACH(bp, &buftail, b_tailq) { 207 TAILQ_FOREACH(bp, &buftail, b_tailq) {

cvs diff -r1.2 -r1.3 src/usr.sbin/makefs/ffs/buf.h (expand / switch to unified diff)

--- src/usr.sbin/makefs/ffs/buf.h 2001/11/02 03:12:49 1.2
+++ src/usr.sbin/makefs/ffs/buf.h 2013/01/26 00:19:39 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: buf.h,v 1.2 2001/11/02 03:12:49 lukem Exp $ */ 1/* $NetBSD: buf.h,v 1.3 2013/01/26 00:19:39 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001 Wasabi Systems, Inc. 4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Written by Luke Mewburn for Wasabi Systems, Inc. 7 * Written by Luke Mewburn for Wasabi Systems, Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -31,35 +31,86 @@ @@ -31,35 +31,86 @@
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE. 35 * POSSIBILITY OF SUCH DAMAGE.
36 */ 36 */
37 37
38#ifndef _FFS_BUF_H 38#ifndef _FFS_BUF_H
39#define _FFS_BUF_H 39#define _FFS_BUF_H
40 40
41#include <sys/param.h> 41#include <sys/param.h>
42#include <sys/queue.h> 42#include <sys/queue.h>
43 43
 44#include <stdio.h>
 45#include <string.h>
 46#include <errno.h>
 47#include <time.h>
 48#include <stddef.h>
 49#include <stdlib.h>
 50#include <err.h>
 51
 52struct componentname {
 53 char *cn_nameptr;
 54 size_t cn_namelen;
 55};
 56
 57struct vnode {
 58 int fd;
 59 void *fs;
 60 void *v_data;
 61 int offset;
 62};
 63
 64#define vput(a) ((void)(a))
 65
44struct buf { 66struct buf {
45 void * b_data; 67 void * b_data;
46 long b_bufsize; 68 long b_bufsize;
47 long b_bcount; 69 long b_bcount;
48 daddr_t b_blkno; 70 daddr_t b_blkno;
49 daddr_t b_lblkno; 71 daddr_t b_lblkno;
50 int b_fd; 72 int b_fd;
51 struct fs * b_fs; 73 struct fs * b_fs;
52 74
53 TAILQ_ENTRY(buf) b_tailq; 75 TAILQ_ENTRY(buf) b_tailq;
54}; 76};
55 77
56void bcleanup(void); 78void bcleanup(void);
57int bread(int, struct fs *, daddr_t, int, struct buf **); 79int bread(struct vnode *, daddr_t, int, struct kauth_cred *,
58void brelse(struct buf *); 80 int, struct buf **);
 81void brelse(struct buf *, int);
59int bwrite(struct buf *); 82int bwrite(struct buf *);
60struct buf * getblk(int, struct fs *, daddr_t, int); 83struct buf * getblk(struct vnode *, daddr_t, int, int, int);
61 84
62#define bdwrite(bp) bwrite(bp) 85#define bdwrite(bp) bwrite(bp)
63#define clrbuf(bp) memset((bp)->b_data, 0, (u_int)(bp)->b_bcount) 86#define clrbuf(bp) memset((bp)->b_data, 0, (u_int)(bp)->b_bcount)
64 87
 88#define B_MODIFY 0
 89#define BC_AGE 0
 90
 91#define min(a, b) MIN((a), (b))
 92#define microtime(tv) gettimeofday((tv), NULL)
 93#define KASSERT(a)
 94#define IO_SYNC 1
 95
 96struct pool {
 97 size_t size;
 98};
 99
 100#define pool_init(p, s, a1, a2, a3, a4, a5, a6) (p)->size = (s)
 101#define pool_get(p, f) malloc((p)->size)
 102#define pool_put(p, a) free(a)
 103#define pool_destroy(p)
 104
 105#define MALLOC_DECLARE(a)
 106#define malloc_type_attach(a)
 107#define malloc_type_detach(a)
 108
 109#define mutex_enter(m)
 110#define mutex_exit(m)
 111#define mutex_init(m, t, i)
 112#define mutex_destroy(m)
 113
 114#define desiredvnodes 10000
 115
65#endif /* _FFS_BUF_H */ 116#endif /* _FFS_BUF_H */

cvs diff -r1.20 -r1.21 src/usr.sbin/makefs/ffs/ffs_alloc.c (expand / switch to unified diff)

--- src/usr.sbin/makefs/ffs/ffs_alloc.c 2013/01/22 09:39:19 1.20
+++ src/usr.sbin/makefs/ffs/ffs_alloc.c 2013/01/26 00:19:39 1.21
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ffs_alloc.c,v 1.20 2013/01/22 09:39:19 dholland Exp $ */ 1/* $NetBSD: ffs_alloc.c,v 1.21 2013/01/26 00:19:39 christos Exp $ */
2/* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */ 2/* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */
3 3
4/* 4/*
5 * Copyright (c) 2002 Networks Associates Technology, Inc. 5 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * This software was developed for the FreeBSD Project by Marshall 8 * This software was developed for the FreeBSD Project by Marshall
9 * Kirk McKusick and Network Associates Laboratories, the Security 9 * Kirk McKusick and Network Associates Laboratories, the Security
10 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 10 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
11 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 11 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
12 * research program 12 * research program
13 * 13 *
14 * Copyright (c) 1982, 1986, 1989, 1993 14 * Copyright (c) 1982, 1986, 1989, 1993
@@ -37,27 +37,27 @@ @@ -37,27 +37,27 @@
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE. 39 * SUCH DAMAGE.
40 * 40 *
41 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 41 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
42 */ 42 */
43 43
44#if HAVE_NBTOOL_CONFIG_H 44#if HAVE_NBTOOL_CONFIG_H
45#include "nbtool_config.h" 45#include "nbtool_config.h"
46#endif 46#endif
47 47
48#include <sys/cdefs.h> 48#include <sys/cdefs.h>
49#if defined(__RCSID) && !defined(__lint) 49#if defined(__RCSID) && !defined(__lint)
50__RCSID("$NetBSD: ffs_alloc.c,v 1.20 2013/01/22 09:39:19 dholland Exp $"); 50__RCSID("$NetBSD: ffs_alloc.c,v 1.21 2013/01/26 00:19:39 christos Exp $");
51#endif /* !__lint */ 51#endif /* !__lint */
52 52
53#include <sys/param.h> 53#include <sys/param.h>
54#include <sys/time.h> 54#include <sys/time.h>
55 55
56#include <errno.h> 56#include <errno.h>
57 57
58#include "makefs.h" 58#include "makefs.h"
59 59
60#include <ufs/ufs/dinode.h> 60#include <ufs/ufs/dinode.h>
61#include <ufs/ufs/ufs_bswap.h> 61#include <ufs/ufs/ufs_bswap.h>
62#include <ufs/ffs/fs.h> 62#include <ufs/ffs/fs.h>
63 63
@@ -294,62 +294,63 @@ ffs_hashalloc(struct inode *ip, int cg,  @@ -294,62 +294,63 @@ ffs_hashalloc(struct inode *ip, int cg,
294 * 294 *
295 * Check to see if a block of the appropriate size is available, 295 * Check to see if a block of the appropriate size is available,
296 * and if it is, allocate it. 296 * and if it is, allocate it.
297 */ 297 */
298static daddr_t 298static daddr_t
299ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size) 299ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
300{ 300{
301 struct cg *cgp; 301 struct cg *cgp;
302 struct buf *bp; 302 struct buf *bp;
303 daddr_t bno, blkno; 303 daddr_t bno, blkno;
304 int error, frags, allocsiz, i; 304 int error, frags, allocsiz, i;
305 struct fs *fs = ip->i_fs; 305 struct fs *fs = ip->i_fs;
306 const int needswap = UFS_FSNEEDSWAP(fs); 306 const int needswap = UFS_FSNEEDSWAP(fs);
 307 struct vnode vp = { ip->i_fd, ip->i_fs, NULL, 0 };
307 308
308 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 309 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
309 return (0); 310 return (0);
310 error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)), 311 error = bread(&vp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
311 (int)fs->fs_cgsize, &bp); 312 NULL, 0, &bp);
312 if (error) { 313 if (error) {
313 brelse(bp); 314 brelse(bp, 0);
314 return (0); 315 return (0);
315 } 316 }
316 cgp = (struct cg *)bp->b_data; 317 cgp = (struct cg *)bp->b_data;
317 if (!cg_chkmagic(cgp, needswap) || 318 if (!cg_chkmagic(cgp, needswap) ||
318 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 319 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
319 brelse(bp); 320 brelse(bp, 0);
320 return (0); 321 return (0);
321 } 322 }
322 if (size == fs->fs_bsize) { 323 if (size == fs->fs_bsize) {
323 bno = ffs_alloccgblk(ip, bp, bpref); 324 bno = ffs_alloccgblk(ip, bp, bpref);
324 bdwrite(bp); 325 bdwrite(bp);
325 return (bno); 326 return (bno);
326 } 327 }
327 /* 328 /*
328 * check to see if any fragments are already available 329 * check to see if any fragments are already available
329 * allocsiz is the size which will be allocated, hacking 330 * allocsiz is the size which will be allocated, hacking
330 * it down to a smaller size if necessary 331 * it down to a smaller size if necessary
331 */ 332 */
332 frags = numfrags(fs, size); 333 frags = numfrags(fs, size);
333 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 334 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
334 if (cgp->cg_frsum[allocsiz] != 0) 335 if (cgp->cg_frsum[allocsiz] != 0)
335 break; 336 break;
336 if (allocsiz == fs->fs_frag) { 337 if (allocsiz == fs->fs_frag) {
337 /* 338 /*
338 * no fragments were available, so a block will be  339 * no fragments were available, so a block will be
339 * allocated, and hacked up 340 * allocated, and hacked up
340 */ 341 */
341 if (cgp->cg_cs.cs_nbfree == 0) { 342 if (cgp->cg_cs.cs_nbfree == 0) {
342 brelse(bp); 343 brelse(bp, 0);
343 return (0); 344 return (0);
344 } 345 }
345 bno = ffs_alloccgblk(ip, bp, bpref); 346 bno = ffs_alloccgblk(ip, bp, bpref);
346 bpref = dtogd(fs, bno); 347 bpref = dtogd(fs, bno);
347 for (i = frags; i < fs->fs_frag; i++) 348 for (i = frags; i < fs->fs_frag; i++)
348 setbit(cg_blksfree(cgp, needswap), bpref + i); 349 setbit(cg_blksfree(cgp, needswap), bpref + i);
349 i = fs->fs_frag - frags; 350 i = fs->fs_frag - frags;
350 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap); 351 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
351 fs->fs_cstotal.cs_nffree += i; 352 fs->fs_cstotal.cs_nffree += i;
352 fs->fs_cs(fs, cg).cs_nffree += i; 353 fs->fs_cs(fs, cg).cs_nffree += i;
353 fs->fs_fmod = 1; 354 fs->fs_fmod = 1;
354 ufs_add32(cgp->cg_frsum[i], 1, needswap); 355 ufs_add32(cgp->cg_frsum[i], 1, needswap);
355 bdwrite(bp); 356 bdwrite(bp);
@@ -429,47 +430,48 @@ gotit: @@ -429,47 +430,48 @@ gotit:
429 * The specified block or fragment is placed back in the 430 * The specified block or fragment is placed back in the
430 * free map. If a fragment is deallocated, a possible  431 * free map. If a fragment is deallocated, a possible
431 * block reassembly is checked. 432 * block reassembly is checked.
432 */ 433 */
433void 434void
434ffs_blkfree(struct inode *ip, daddr_t bno, long size) 435ffs_blkfree(struct inode *ip, daddr_t bno, long size)
435{ 436{
436 struct cg *cgp; 437 struct cg *cgp;
437 struct buf *bp; 438 struct buf *bp;
438 int32_t fragno, cgbno; 439 int32_t fragno, cgbno;
439 int i, error, cg, blk, frags, bbase; 440 int i, error, cg, blk, frags, bbase;
440 struct fs *fs = ip->i_fs; 441 struct fs *fs = ip->i_fs;
441 const int needswap = UFS_FSNEEDSWAP(fs); 442 const int needswap = UFS_FSNEEDSWAP(fs);
 443 struct vnode vp = { ip->i_fd, ip->i_fs, NULL, 0 };
442 444
443 if (size > fs->fs_bsize || fragoff(fs, size) != 0 || 445 if (size > fs->fs_bsize || fragoff(fs, size) != 0 ||
444 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { 446 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
445 errx(1, "blkfree: bad size: bno %lld bsize %d size %ld", 447 errx(1, "blkfree: bad size: bno %lld bsize %d size %ld",
446 (long long)bno, fs->fs_bsize, size); 448 (long long)bno, fs->fs_bsize, size);
447 } 449 }
448 cg = dtog(fs, bno); 450 cg = dtog(fs, bno);
449 if (bno >= fs->fs_size) { 451 if (bno >= fs->fs_size) {
450 warnx("bad block %lld, ino %llu", (long long)bno, 452 warnx("bad block %lld, ino %llu", (long long)bno,
451 (unsigned long long)ip->i_number); 453 (unsigned long long)ip->i_number);
452 return; 454 return;
453 } 455 }
454 error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)), 456 error = bread(&vp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
455 (int)fs->fs_cgsize, &bp); 457 NULL, 0, &bp);
456 if (error) { 458 if (error) {
457 brelse(bp); 459 brelse(bp, 0);
458 return; 460 return;
459 } 461 }
460 cgp = (struct cg *)bp->b_data; 462 cgp = (struct cg *)bp->b_data;
461 if (!cg_chkmagic(cgp, needswap)) { 463 if (!cg_chkmagic(cgp, needswap)) {
462 brelse(bp); 464 brelse(bp, 0);
463 return; 465 return;
464 } 466 }
465 cgbno = dtogd(fs, bno); 467 cgbno = dtogd(fs, bno);
466 if (size == fs->fs_bsize) { 468 if (size == fs->fs_bsize) {
467 fragno = fragstoblks(fs, cgbno); 469 fragno = fragstoblks(fs, cgbno);
468 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) { 470 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) {
469 errx(1, "blkfree: freeing free block %lld", 471 errx(1, "blkfree: freeing free block %lld",
470 (long long)bno); 472 (long long)bno);
471 } 473 }
472 ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno); 474 ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno);
473 ffs_clusteracct(fs, cgp, fragno, 1); 475 ffs_clusteracct(fs, cgp, fragno, 1);
474 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap); 476 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
475 fs->fs_cstotal.cs_nbfree++; 477 fs->fs_cstotal.cs_nbfree++;

cvs diff -r1.14 -r1.15 src/usr.sbin/makefs/ffs/ffs_balloc.c (expand / switch to unified diff)

--- src/usr.sbin/makefs/ffs/ffs_balloc.c 2013/01/22 09:39:19 1.14
+++ src/usr.sbin/makefs/ffs/ffs_balloc.c 2013/01/26 00:19:39 1.15
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ffs_balloc.c,v 1.14 2013/01/22 09:39:19 dholland Exp $ */ 1/* $NetBSD: ffs_balloc.c,v 1.15 2013/01/26 00:19:39 christos Exp $ */
2/* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */ 2/* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */
3 3
4/* 4/*
5 * Copyright (c) 1982, 1986, 1989, 1993 5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California. All rights reserved. 6 * The Regents of the University of California. All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE. 30 * SUCH DAMAGE.
31 * 31 *
32 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95 32 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(__lint) 40#if defined(__RCSID) && !defined(__lint)
41__RCSID("$NetBSD: ffs_balloc.c,v 1.14 2013/01/22 09:39:19 dholland Exp $"); 41__RCSID("$NetBSD: ffs_balloc.c,v 1.15 2013/01/26 00:19:39 christos Exp $");
42#endif /* !__lint */ 42#endif /* !__lint */
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <sys/time.h> 45#include <sys/time.h>
46 46
47#include <assert.h> 47#include <assert.h>
48#include <errno.h> 48#include <errno.h>
49#include <stdio.h> 49#include <stdio.h>
50#include <stdlib.h> 50#include <stdlib.h>
51#include <string.h> 51#include <string.h>
52 52
53#include "makefs.h" 53#include "makefs.h"
54 54
@@ -85,26 +85,27 @@ ffs_balloc_ufs1(struct inode *ip, off_t  @@ -85,26 +85,27 @@ ffs_balloc_ufs1(struct inode *ip, off_t
85{ 85{
86 daddr_t lbn, lastlbn; 86 daddr_t lbn, lastlbn;
87 int size; 87 int size;
88 int32_t nb; 88 int32_t nb;
89 struct buf *bp, *nbp; 89 struct buf *bp, *nbp;
90 struct fs *fs = ip->i_fs; 90 struct fs *fs = ip->i_fs;
91 struct indir indirs[UFS_NIADDR + 2]; 91 struct indir indirs[UFS_NIADDR + 2];
92 daddr_t newb, pref; 92 daddr_t newb, pref;
93 int32_t *bap; 93 int32_t *bap;
94 int osize, nsize, num, i, error; 94 int osize, nsize, num, i, error;
95 int32_t *allocblk, allociblk[UFS_NIADDR + 1]; 95 int32_t *allocblk, allociblk[UFS_NIADDR + 1];
96 int32_t *allocib; 96 int32_t *allocib;
97 const int needswap = UFS_FSNEEDSWAP(fs); 97 const int needswap = UFS_FSNEEDSWAP(fs);
 98 struct vnode vp = { ip->i_fd, ip->i_fs, NULL, 0 };
98 99
99 lbn = lblkno(fs, offset); 100 lbn = lblkno(fs, offset);
100 size = blkoff(fs, offset) + bufsize; 101 size = blkoff(fs, offset) + bufsize;
101 if (bpp != NULL) { 102 if (bpp != NULL) {
102 *bpp = NULL; 103 *bpp = NULL;
103 } 104 }
104 105
105 assert(size <= fs->fs_bsize); 106 assert(size <= fs->fs_bsize);
106 if (lbn < 0) 107 if (lbn < 0)
107 return (EFBIG); 108 return (EFBIG);
108 109
109 /* 110 /*
110 * If the next write will extend the file into a new block, 111 * If the next write will extend the file into a new block,
@@ -128,83 +129,83 @@ ffs_balloc_ufs1(struct inode *ip, off_t  @@ -128,83 +129,83 @@ ffs_balloc_ufs1(struct inode *ip, off_t
128 129
129 if (lbn < UFS_NDADDR) { 130 if (lbn < UFS_NDADDR) {
130 nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap); 131 nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
131 if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) { 132 if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) {
132 133
133 /* 134 /*
134 * The block is an already-allocated direct block 135 * The block is an already-allocated direct block
135 * and the file already extends past this block, 136 * and the file already extends past this block,
136 * thus this must be a whole block. 137 * thus this must be a whole block.
137 * Just read the block (if requested). 138 * Just read the block (if requested).
138 */ 139 */
139 140
140 if (bpp != NULL) { 141 if (bpp != NULL) {
141 error = bread(ip->i_fd, ip->i_fs, lbn, 142 error = bread(&vp, lbn, fs->fs_bsize, NULL, 0,
142 fs->fs_bsize, bpp); 143 bpp);
143 if (error) { 144 if (error) {
144 brelse(*bpp); 145 brelse(*bpp, 0);
145 return (error); 146 return (error);
146 } 147 }
147 } 148 }
148 return (0); 149 return (0);
149 } 150 }
150 if (nb != 0) { 151 if (nb != 0) {
151 152
152 /* 153 /*
153 * Consider need to reallocate a fragment. 154 * Consider need to reallocate a fragment.
154 */ 155 */
155 156
156 osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size)); 157 osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
157 nsize = fragroundup(fs, size); 158 nsize = fragroundup(fs, size);
158 if (nsize <= osize) { 159 if (nsize <= osize) {
159 160
160 /* 161 /*
161 * The existing block is already 162 * The existing block is already
162 * at least as big as we want. 163 * at least as big as we want.
163 * Just read the block (if requested). 164 * Just read the block (if requested).
164 */ 165 */
165 166
166 if (bpp != NULL) { 167 if (bpp != NULL) {
167 error = bread(ip->i_fd, ip->i_fs, lbn, 168 error = bread(&vp, lbn, osize, NULL, 0,
168 osize, bpp); 169 bpp);
169 if (error) { 170 if (error) {
170 brelse(*bpp); 171 brelse(*bpp, 0);
171 return (error); 172 return (error);
172 } 173 }
173 } 174 }
174 return 0; 175 return 0;
175 } else { 176 } else {
176 warnx("need to ffs_realloccg; not supported!"); 177 warnx("need to ffs_realloccg; not supported!");
177 abort(); 178 abort();
178 } 179 }
179 } else { 180 } else {
180 181
181 /* 182 /*
182 * the block was not previously allocated, 183 * the block was not previously allocated,
183 * allocate a new block or fragment. 184 * allocate a new block or fragment.
184 */ 185 */
185 186
186 if (ip->i_ffs1_size < lblktosize(fs, lbn + 1)) 187 if (ip->i_ffs1_size < lblktosize(fs, lbn + 1))
187 nsize = fragroundup(fs, size); 188 nsize = fragroundup(fs, size);
188 else 189 else
189 nsize = fs->fs_bsize; 190 nsize = fs->fs_bsize;
190 error = ffs_alloc(ip, lbn, 191 error = ffs_alloc(ip, lbn,
191 ffs_blkpref_ufs1(ip, lbn, (int)lbn, 192 ffs_blkpref_ufs1(ip, lbn, (int)lbn,
192 &ip->i_ffs1_db[0]), 193 &ip->i_ffs1_db[0]),
193 nsize, &newb); 194 nsize, &newb);
194 if (error) 195 if (error)
195 return (error); 196 return (error);
196 if (bpp != NULL) { 197 if (bpp != NULL) {
197 bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize); 198 bp = getblk(&vp, lbn, nsize, 0, 0);
198 bp->b_blkno = fsbtodb(fs, newb); 199 bp->b_blkno = fsbtodb(fs, newb);
199 clrbuf(bp); 200 clrbuf(bp);
200 *bpp = bp; 201 *bpp = bp;
201 } 202 }
202 } 203 }
203 ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap); 204 ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
204 return (0); 205 return (0);
205 } 206 }
206 207
207 /* 208 /*
208 * Determine the number of levels of indirection. 209 * Determine the number of levels of indirection.
209 */ 210 */
210 211
@@ -222,140 +223,140 @@ ffs_balloc_ufs1(struct inode *ip, off_t  @@ -222,140 +223,140 @@ ffs_balloc_ufs1(struct inode *ip, off_t
222 */ 223 */
223 224
224 --num; 225 --num;
225 nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap); 226 nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
226 allocib = NULL; 227 allocib = NULL;
227 allocblk = allociblk; 228 allocblk = allociblk;
228 if (nb == 0) { 229 if (nb == 0) {
229 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0); 230 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
230 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb); 231 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
231 if (error) 232 if (error)
232 return error; 233 return error;
233 nb = newb; 234 nb = newb;
234 *allocblk++ = nb; 235 *allocblk++ = nb;
235 bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize); 236 bp = getblk(&vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
236 bp->b_blkno = fsbtodb(fs, nb); 237 bp->b_blkno = fsbtodb(fs, nb);
237 clrbuf(bp); 238 clrbuf(bp);
238 /* 239 /*
239 * Write synchronously so that indirect blocks 240 * Write synchronously so that indirect blocks
240 * never point at garbage. 241 * never point at garbage.
241 */ 242 */
242 if ((error = bwrite(bp)) != 0) 243 if ((error = bwrite(bp)) != 0)
243 return error; 244 return error;
244 allocib = &ip->i_ffs1_ib[indirs[0].in_off]; 245 allocib = &ip->i_ffs1_ib[indirs[0].in_off];
245 *allocib = ufs_rw32((int32_t)nb, needswap); 246 *allocib = ufs_rw32((int32_t)nb, needswap);
246 } 247 }
247 248
248 /* 249 /*
249 * Fetch through the indirect blocks, allocating as necessary. 250 * Fetch through the indirect blocks, allocating as necessary.
250 */ 251 */
251 252
252 for (i = 1;;) { 253 for (i = 1;;) {
253 error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,  254 error = bread(&vp, indirs[i].in_lbn, fs->fs_bsize, NULL, 0,
254 fs->fs_bsize, &bp); 255 &bp);
255 if (error) { 256 if (error) {
256 brelse(bp); 257 brelse(bp, 0);
257 return error; 258 return error;
258 } 259 }
259 bap = (int32_t *)bp->b_data; 260 bap = (int32_t *)bp->b_data;
260 nb = ufs_rw32(bap[indirs[i].in_off], needswap); 261 nb = ufs_rw32(bap[indirs[i].in_off], needswap);
261 if (i == num) 262 if (i == num)
262 break; 263 break;
263 i++; 264 i++;
264 if (nb != 0) { 265 if (nb != 0) {
265 brelse(bp); 266 brelse(bp, 0);
266 continue; 267 continue;
267 } 268 }
268 if (pref == 0) 269 if (pref == 0)
269 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0); 270 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
270 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb); 271 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
271 if (error) { 272 if (error) {
272 brelse(bp); 273 brelse(bp, 0);
273 return error; 274 return error;
274 } 275 }
275 nb = newb; 276 nb = newb;
276 *allocblk++ = nb; 277 *allocblk++ = nb;
277 nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn, 278 nbp = getblk(&vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
278 fs->fs_bsize); 
279 nbp->b_blkno = fsbtodb(fs, nb); 279 nbp->b_blkno = fsbtodb(fs, nb);
280 clrbuf(nbp); 280 clrbuf(nbp);
281 /* 281 /*
282 * Write synchronously so that indirect blocks 282 * Write synchronously so that indirect blocks
283 * never point at garbage. 283 * never point at garbage.
284 */ 284 */
285 285
286 if ((error = bwrite(nbp)) != 0) { 286 if ((error = bwrite(nbp)) != 0) {
287 brelse(bp); 287 brelse(bp, 0);
288 return error; 288 return error;
289 } 289 }
290 bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap); 290 bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
291 291
292 bwrite(bp); 292 bwrite(bp);
293 } 293 }
294 294
295 /* 295 /*
296 * Get the data block, allocating if necessary. 296 * Get the data block, allocating if necessary.
297 */ 297 */
298 298
299 if (nb == 0) { 299 if (nb == 0) {
300 pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]); 300 pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
301 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb); 301 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
302 if (error) { 302 if (error) {
303 brelse(bp); 303 brelse(bp, 0);
304 return error; 304 return error;
305 } 305 }
306 nb = newb; 306 nb = newb;
307 *allocblk++ = nb; 307 *allocblk++ = nb;
308 if (bpp != NULL) { 308 if (bpp != NULL) {
309 nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize); 309 nbp = getblk(&vp, lbn, fs->fs_bsize, 0, 0);
310 nbp->b_blkno = fsbtodb(fs, nb); 310 nbp->b_blkno = fsbtodb(fs, nb);
311 clrbuf(nbp); 311 clrbuf(nbp);
312 *bpp = nbp; 312 *bpp = nbp;
313 } 313 }
314 bap[indirs[num].in_off] = ufs_rw32(nb, needswap); 314 bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
315 315
316 /* 316 /*
317 * If required, write synchronously, otherwise use 317 * If required, write synchronously, otherwise use
318 * delayed write. 318 * delayed write.
319 */ 319 */
320 bwrite(bp); 320 bwrite(bp);
321 return (0); 321 return (0);
322 } 322 }
323 brelse(bp); 323 brelse(bp, 0);
324 if (bpp != NULL) { 324 if (bpp != NULL) {
325 error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp); 325 error = bread(&vp, lbn, (int)fs->fs_bsize, NULL, 0, &nbp);
326 if (error) { 326 if (error) {
327 brelse(nbp); 327 brelse(nbp, 0);
328 return error; 328 return error;
329 } 329 }
330 *bpp = nbp; 330 *bpp = nbp;
331 } 331 }
332 return (0); 332 return (0);
333} 333}
334 334
335static int 335static int
336ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp) 336ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
337{ 337{
338 daddr_t lbn, lastlbn; 338 daddr_t lbn, lastlbn;
339 int size; 339 int size;
340 struct buf *bp, *nbp; 340 struct buf *bp, *nbp;
341 struct fs *fs = ip->i_fs; 341 struct fs *fs = ip->i_fs;
342 struct indir indirs[UFS_NIADDR + 2]; 342 struct indir indirs[UFS_NIADDR + 2];
343 daddr_t newb, pref, nb; 343 daddr_t newb, pref, nb;
344 int64_t *bap; 344 int64_t *bap;
345 int osize, nsize, num, i, error; 345 int osize, nsize, num, i, error;
346 int64_t *allocblk, allociblk[UFS_NIADDR + 1]; 346 int64_t *allocblk, allociblk[UFS_NIADDR + 1];
347 int64_t *allocib; 347 int64_t *allocib;
348 const int needswap = UFS_FSNEEDSWAP(fs); 348 const int needswap = UFS_FSNEEDSWAP(fs);
 349 struct vnode vp = { ip->i_fd, ip->i_fs, NULL, 0 };
349 350
350 lbn = lblkno(fs, offset); 351 lbn = lblkno(fs, offset);
351 size = blkoff(fs, offset) + bufsize; 352 size = blkoff(fs, offset) + bufsize;
352 if (bpp != NULL) { 353 if (bpp != NULL) {
353 *bpp = NULL; 354 *bpp = NULL;
354 } 355 }
355 356
356 assert(size <= fs->fs_bsize); 357 assert(size <= fs->fs_bsize);
357 if (lbn < 0) 358 if (lbn < 0)
358 return (EFBIG); 359 return (EFBIG);
359 360
360 /* 361 /*
361 * If the next write will extend the file into a new block, 362 * If the next write will extend the file into a new block,
@@ -379,83 +380,83 @@ ffs_balloc_ufs2(struct inode *ip, off_t  @@ -379,83 +380,83 @@ ffs_balloc_ufs2(struct inode *ip, off_t
379 380
380 if (lbn < UFS_NDADDR) { 381 if (lbn < UFS_NDADDR) {
381 nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap); 382 nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
382 if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) { 383 if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) {
383 384
384 /* 385 /*
385 * The block is an already-allocated direct block 386 * The block is an already-allocated direct block
386 * and the file already extends past this block, 387 * and the file already extends past this block,
387 * thus this must be a whole block. 388 * thus this must be a whole block.
388 * Just read the block (if requested). 389 * Just read the block (if requested).
389 */ 390 */
390 391
391 if (bpp != NULL) { 392 if (bpp != NULL) {
392 error = bread(ip->i_fd, ip->i_fs, lbn, 393 error = bread(&vp, lbn, fs->fs_bsize, NULL, 0,
393 fs->fs_bsize, bpp); 394 bpp);
394 if (error) { 395 if (error) {
395 brelse(*bpp); 396 brelse(*bpp, 0);
396 return (error); 397 return (error);
397 } 398 }
398 } 399 }
399 return (0); 400 return (0);
400 } 401 }
401 if (nb != 0) { 402 if (nb != 0) {
402 403
403 /* 404 /*
404 * Consider need to reallocate a fragment. 405 * Consider need to reallocate a fragment.
405 */ 406 */
406 407
407 osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size)); 408 osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
408 nsize = fragroundup(fs, size); 409 nsize = fragroundup(fs, size);
409 if (nsize <= osize) { 410 if (nsize <= osize) {
410 411
411 /* 412 /*
412 * The existing block is already 413 * The existing block is already
413 * at least as big as we want. 414 * at least as big as we want.
414 * Just read the block (if requested). 415 * Just read the block (if requested).
415 */ 416 */
416 417
417 if (bpp != NULL) { 418 if (bpp != NULL) {
418 error = bread(ip->i_fd, ip->i_fs, lbn, 419 error = bread(&vp, lbn, osize, NULL, 0,
419 osize, bpp); 420 bpp);
420 if (error) { 421 if (error) {
421 brelse(*bpp); 422 brelse(*bpp, 0);
422 return (error); 423 return (error);
423 } 424 }
424 } 425 }
425 return 0; 426 return 0;
426 } else { 427 } else {
427 warnx("need to ffs_realloccg; not supported!"); 428 warnx("need to ffs_realloccg; not supported!");
428 abort(); 429 abort();
429 } 430 }
430 } else { 431 } else {
431 432
432 /* 433 /*
433 * the block was not previously allocated, 434 * the block was not previously allocated,
434 * allocate a new block or fragment. 435 * allocate a new block or fragment.
435 */ 436 */
436 437
437 if (ip->i_ffs2_size < lblktosize(fs, lbn + 1)) 438 if (ip->i_ffs2_size < lblktosize(fs, lbn + 1))
438 nsize = fragroundup(fs, size); 439 nsize = fragroundup(fs, size);
439 else 440 else
440 nsize = fs->fs_bsize; 441 nsize = fs->fs_bsize;
441 error = ffs_alloc(ip, lbn, 442 error = ffs_alloc(ip, lbn,
442 ffs_blkpref_ufs2(ip, lbn, (int)lbn, 443 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
443 &ip->i_ffs2_db[0]), 444 &ip->i_ffs2_db[0]),
444 nsize, &newb); 445 nsize, &newb);
445 if (error) 446 if (error)
446 return (error); 447 return (error);
447 if (bpp != NULL) { 448 if (bpp != NULL) {
448 bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize); 449 bp = getblk(&vp, lbn, nsize, 0, 0);
449 bp->b_blkno = fsbtodb(fs, newb); 450 bp->b_blkno = fsbtodb(fs, newb);
450 clrbuf(bp); 451 clrbuf(bp);
451 *bpp = bp; 452 *bpp = bp;
452 } 453 }
453 } 454 }
454 ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap); 455 ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
455 return (0); 456 return (0);
456 } 457 }
457 458
458 /* 459 /*
459 * Determine the number of levels of indirection. 460 * Determine the number of levels of indirection.
460 */ 461 */
461 462
@@ -473,112 +474,111 @@ ffs_balloc_ufs2(struct inode *ip, off_t  @@ -473,112 +474,111 @@ ffs_balloc_ufs2(struct inode *ip, off_t
473 */ 474 */
474 475
475 --num; 476 --num;
476 nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap); 477 nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
477 allocib = NULL; 478 allocib = NULL;
478 allocblk = allociblk; 479 allocblk = allociblk;
479 if (nb == 0) { 480 if (nb == 0) {
480 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0); 481 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
481 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb); 482 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
482 if (error) 483 if (error)
483 return error; 484 return error;
484 nb = newb; 485 nb = newb;
485 *allocblk++ = nb; 486 *allocblk++ = nb;
486 bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize); 487 bp = getblk(&vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
487 bp->b_blkno = fsbtodb(fs, nb); 488 bp->b_blkno = fsbtodb(fs, nb);
488 clrbuf(bp); 489 clrbuf(bp);
489 /* 490 /*
490 * Write synchronously so that indirect blocks 491 * Write synchronously so that indirect blocks
491 * never point at garbage. 492 * never point at garbage.
492 */ 493 */
493 if ((error = bwrite(bp)) != 0) 494 if ((error = bwrite(bp)) != 0)
494 return error; 495 return error;
495 allocib = &ip->i_ffs2_ib[indirs[0].in_off]; 496 allocib = &ip->i_ffs2_ib[indirs[0].in_off];
496 *allocib = ufs_rw64(nb, needswap); 497 *allocib = ufs_rw64(nb, needswap);
497 } 498 }
498 499
499 /* 500 /*
500 * Fetch through the indirect blocks, allocating as necessary. 501 * Fetch through the indirect blocks, allocating as necessary.
501 */ 502 */
502 503
503 for (i = 1;;) { 504 for (i = 1;;) {
504 error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,  505 error = bread(&vp, indirs[i].in_lbn, fs->fs_bsize, NULL, 0,
505 fs->fs_bsize, &bp); 506 &bp);
506 if (error) { 507 if (error) {
507 brelse(bp); 508 brelse(bp, 0);
508 return error; 509 return error;
509 } 510 }
510 bap = (int64_t *)bp->b_data; 511 bap = (int64_t *)bp->b_data;
511 nb = ufs_rw64(bap[indirs[i].in_off], needswap); 512 nb = ufs_rw64(bap[indirs[i].in_off], needswap);
512 if (i == num) 513 if (i == num)
513 break; 514 break;
514 i++; 515 i++;
515 if (nb != 0) { 516 if (nb != 0) {
516 brelse(bp); 517 brelse(bp, 0);
517 continue; 518 continue;
518 } 519 }
519 if (pref == 0) 520 if (pref == 0)
520 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0); 521 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
521 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb); 522 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
522 if (error) { 523 if (error) {
523 brelse(bp); 524 brelse(bp, 0);
524 return error; 525 return error;
525 } 526 }
526 nb = newb; 527 nb = newb;
527 *allocblk++ = nb; 528 *allocblk++ = nb;
528 nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn, 529 nbp = getblk(&vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
529 fs->fs_bsize); 
530 nbp->b_blkno = fsbtodb(fs, nb); 530 nbp->b_blkno = fsbtodb(fs, nb);
531 clrbuf(nbp); 531 clrbuf(nbp);
532 /* 532 /*
533 * Write synchronously so that indirect blocks 533 * Write synchronously so that indirect blocks
534 * never point at garbage. 534 * never point at garbage.
535 */ 535 */
536 536
537 if ((error = bwrite(nbp)) != 0) { 537 if ((error = bwrite(nbp)) != 0) {
538 brelse(bp); 538 brelse(bp, 0);
539 return error; 539 return error;
540 } 540 }
541 bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap); 541 bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
542 542
543 bwrite(bp); 543 bwrite(bp);
544 } 544 }
545 545
546 /* 546 /*
547 * Get the data block, allocating if necessary. 547 * Get the data block, allocating if necessary.
548 */ 548 */
549 549
550 if (nb == 0) { 550 if (nb == 0) {
551 pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]); 551 pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
552 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb); 552 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
553 if (error) { 553 if (error) {
554 brelse(bp); 554 brelse(bp, 0);
555 return error; 555 return error;
556 } 556 }
557 nb = newb; 557 nb = newb;
558 *allocblk++ = nb; 558 *allocblk++ = nb;
559 if (bpp != NULL) { 559 if (bpp != NULL) {
560 nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize); 560 nbp = getblk(&vp, lbn, fs->fs_bsize, 0, 0);
561 nbp->b_blkno = fsbtodb(fs, nb); 561 nbp->b_blkno = fsbtodb(fs, nb);
562 clrbuf(nbp); 562 clrbuf(nbp);
563 *bpp = nbp; 563 *bpp = nbp;
564 } 564 }
565 bap[indirs[num].in_off] = ufs_rw64(nb, needswap); 565 bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
566 566
567 /* 567 /*
568 * If required, write synchronously, otherwise use 568 * If required, write synchronously, otherwise use
569 * delayed write. 569 * delayed write.
570 */ 570 */
571 bwrite(bp); 571 bwrite(bp);
572 return (0); 572 return (0);
573 } 573 }
574 brelse(bp); 574 brelse(bp, 0);
575 if (bpp != NULL) { 575 if (bpp != NULL) {
576 error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp); 576 error = bread(&vp, lbn, (int)fs->fs_bsize, NULL, 0, &nbp);
577 if (error) { 577 if (error) {
578 brelse(nbp); 578 brelse(nbp, 0);
579 return error; 579 return error;
580 } 580 }
581 *bpp = nbp; 581 *bpp = nbp;
582 } 582 }
583 return (0); 583 return (0);
584} 584}