| @@ -1,1316 +1,1318 @@ | | | @@ -1,1316 +1,1318 @@ |
1 | /* $NetBSD: ffs_alloc.c,v 1.106.8.7 2008/12/30 19:30:31 christos Exp $ */ | | 1 | /* $NetBSD: ffs_alloc.c,v 1.106.8.8 2009/01/04 19:20:32 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2008 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2008 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Wasabi Systems, Inc. | | 8 | * by Wasabi Systems, Inc. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 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 | | 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 | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 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 | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 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 | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | /* | | 32 | /* |
33 | * Copyright (c) 2002 Networks Associates Technology, Inc. | | 33 | * Copyright (c) 2002 Networks Associates Technology, Inc. |
34 | * All rights reserved. | | 34 | * All rights reserved. |
35 | * | | 35 | * |
36 | * This software was developed for the FreeBSD Project by Marshall | | 36 | * This software was developed for the FreeBSD Project by Marshall |
37 | * Kirk McKusick and Network Associates Laboratories, the Security | | 37 | * Kirk McKusick and Network Associates Laboratories, the Security |
38 | * Research Division of Network Associates, Inc. under DARPA/SPAWAR | | 38 | * Research Division of Network Associates, Inc. under DARPA/SPAWAR |
39 | * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS | | 39 | * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS |
40 | * research program | | 40 | * research program |
41 | * | | 41 | * |
42 | * Copyright (c) 1982, 1986, 1989, 1993 | | 42 | * Copyright (c) 1982, 1986, 1989, 1993 |
43 | * The Regents of the University of California. All rights reserved. | | 43 | * The Regents of the University of California. All rights reserved. |
44 | * | | 44 | * |
45 | * Redistribution and use in source and binary forms, with or without | | 45 | * Redistribution and use in source and binary forms, with or without |
46 | * modification, are permitted provided that the following conditions | | 46 | * modification, are permitted provided that the following conditions |
47 | * are met: | | 47 | * are met: |
48 | * 1. Redistributions of source code must retain the above copyright | | 48 | * 1. Redistributions of source code must retain the above copyright |
49 | * notice, this list of conditions and the following disclaimer. | | 49 | * notice, this list of conditions and the following disclaimer. |
50 | * 2. Redistributions in binary form must reproduce the above copyright | | 50 | * 2. Redistributions in binary form must reproduce the above copyright |
51 | * notice, this list of conditions and the following disclaimer in the | | 51 | * notice, this list of conditions and the following disclaimer in the |
52 | * documentation and/or other materials provided with the distribution. | | 52 | * documentation and/or other materials provided with the distribution. |
53 | * 3. Neither the name of the University nor the names of its contributors | | 53 | * 3. Neither the name of the University nor the names of its contributors |
54 | * may be used to endorse or promote products derived from this software | | 54 | * may be used to endorse or promote products derived from this software |
55 | * without specific prior written permission. | | 55 | * without specific prior written permission. |
56 | * | | 56 | * |
57 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 57 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
58 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 58 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
59 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 59 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
60 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 60 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
61 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 61 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
62 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 62 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
63 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 63 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
64 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 64 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
65 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 65 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
66 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 66 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
67 | * SUCH DAMAGE. | | 67 | * SUCH DAMAGE. |
68 | * | | 68 | * |
69 | * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 | | 69 | * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 |
70 | */ | | 70 | */ |
71 | | | 71 | |
72 | #include <sys/cdefs.h> | | 72 | #include <sys/cdefs.h> |
73 | __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.106.8.7 2008/12/30 19:30:31 christos Exp $"); | | 73 | __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.106.8.8 2009/01/04 19:20:32 christos Exp $"); |
74 | | | 74 | |
75 | #if defined(_KERNEL_OPT) | | 75 | #if defined(_KERNEL_OPT) |
76 | #include "opt_ffs.h" | | 76 | #include "opt_ffs.h" |
77 | #include "opt_quota.h" | | 77 | #include "opt_quota.h" |
78 | #endif | | 78 | #endif |
79 | | | 79 | |
80 | #include <sys/param.h> | | 80 | #include <sys/param.h> |
81 | #include <sys/systm.h> | | 81 | #include <sys/systm.h> |
82 | #include <sys/buf.h> | | 82 | #include <sys/buf.h> |
83 | #include <sys/fstrans.h> | | 83 | #include <sys/fstrans.h> |
84 | #include <sys/kauth.h> | | 84 | #include <sys/kauth.h> |
85 | #include <sys/kernel.h> | | 85 | #include <sys/kernel.h> |
86 | #include <sys/mount.h> | | 86 | #include <sys/mount.h> |
87 | #include <sys/proc.h> | | 87 | #include <sys/proc.h> |
88 | #include <sys/syslog.h> | | 88 | #include <sys/syslog.h> |
89 | #include <sys/vnode.h> | | 89 | #include <sys/vnode.h> |
90 | #include <sys/wapbl.h> | | 90 | #include <sys/wapbl.h> |
91 | | | 91 | |
92 | #include <miscfs/specfs/specdev.h> | | 92 | #include <miscfs/specfs/specdev.h> |
93 | #include <ufs/ufs/quota.h> | | 93 | #include <ufs/ufs/quota.h> |
94 | #include <ufs/ufs/ufsmount.h> | | 94 | #include <ufs/ufs/ufsmount.h> |
95 | #include <ufs/ufs/inode.h> | | 95 | #include <ufs/ufs/inode.h> |
96 | #include <ufs/ufs/ufs_extern.h> | | 96 | #include <ufs/ufs/ufs_extern.h> |
97 | #include <ufs/ufs/ufs_bswap.h> | | 97 | #include <ufs/ufs/ufs_bswap.h> |
98 | #include <ufs/ufs/ufs_wapbl.h> | | 98 | #include <ufs/ufs/ufs_wapbl.h> |
99 | | | 99 | |
100 | #include <ufs/ffs/fs.h> | | 100 | #include <ufs/ffs/fs.h> |
101 | #include <ufs/ffs/ffs_extern.h> | | 101 | #include <ufs/ffs/ffs_extern.h> |
102 | | | 102 | |
103 | static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int, int); | | 103 | static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int, int); |
104 | static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t, int); | | 104 | static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t, int); |
105 | static ino_t ffs_dirpref(struct inode *); | | 105 | static ino_t ffs_dirpref(struct inode *); |
106 | static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int); | | 106 | static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int); |
107 | static void ffs_fserr(struct fs *, u_int, const char *); | | 107 | static void ffs_fserr(struct fs *, u_int, const char *); |
108 | static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int, int, | | 108 | static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int, int, |
109 | daddr_t (*)(struct inode *, int, daddr_t, int, int)); | | 109 | daddr_t (*)(struct inode *, int, daddr_t, int, int)); |
110 | static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int, int); | | 110 | static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int, int); |
111 | static int32_t ffs_mapsearch(struct fs *, struct cg *, | | 111 | static int32_t ffs_mapsearch(struct fs *, struct cg *, |
112 | daddr_t, int); | | 112 | daddr_t, int); |
113 | static void ffs_blkfree_common(struct ufsmount *, struct fs *, dev_t, struct buf *, | | 113 | static void ffs_blkfree_common(struct ufsmount *, struct fs *, dev_t, struct buf *, |
114 | daddr_t, long, bool); | | 114 | daddr_t, long, bool); |
115 | static void ffs_freefile_common(struct ufsmount *, struct fs *, dev_t, struct buf *, ino_t, | | 115 | static void ffs_freefile_common(struct ufsmount *, struct fs *, dev_t, struct buf *, ino_t, |
116 | int, bool); | | 116 | int, bool); |
117 | | | 117 | |
118 | /* if 1, changes in optimalization strategy are logged */ | | 118 | /* if 1, changes in optimalization strategy are logged */ |
119 | int ffs_log_changeopt = 0; | | 119 | int ffs_log_changeopt = 0; |
120 | | | 120 | |
121 | /* in ffs_tables.c */ | | 121 | /* in ffs_tables.c */ |
122 | extern const int inside[], around[]; | | 122 | extern const int inside[], around[]; |
123 | extern const u_char * const fragtbl[]; | | 123 | extern const u_char * const fragtbl[]; |
124 | | | 124 | |
125 | /* Basic consistency check for block allocations */ | | 125 | /* Basic consistency check for block allocations */ |
126 | static int | | 126 | static int |
127 | ffs_check_bad_allocation(const char *func, struct fs *fs, daddr_t bno, | | 127 | ffs_check_bad_allocation(const char *func, struct fs *fs, daddr_t bno, |
128 | long size, dev_t dev, ino_t inum) | | 128 | long size, dev_t dev, ino_t inum) |
129 | { | | 129 | { |
130 | if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 || | | 130 | if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 || |
131 | fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { | | 131 | fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { |
132 | printf("dev = 0x%llx, bno = %" PRId64 " bsize = %d, " | | 132 | printf("dev = 0x%llx, bno = %" PRId64 " bsize = %d, " |
133 | "size = %ld, fs = %s\n", | | 133 | "size = %ld, fs = %s\n", |
134 | (long long)dev, bno, fs->fs_bsize, size, fs->fs_fsmnt); | | 134 | (long long)dev, bno, fs->fs_bsize, size, fs->fs_fsmnt); |
135 | panic("%s: bad size", func); | | 135 | panic("%s: bad size", func); |
136 | } | | 136 | } |
137 | | | 137 | |
138 | if (bno >= fs->fs_size) { | | 138 | if (bno >= fs->fs_size) { |
139 | printf("bad block %" PRId64 ", ino %llu\n", bno, | | 139 | printf("bad block %" PRId64 ", ino %llu\n", bno, |
140 | (unsigned long long)inum); | | 140 | (unsigned long long)inum); |
141 | ffs_fserr(fs, inum, "bad block"); | | 141 | ffs_fserr(fs, inum, "bad block"); |
142 | return EINVAL; | | 142 | return EINVAL; |
143 | } | | 143 | } |
144 | return 0; | | 144 | return 0; |
145 | } | | 145 | } |
146 | | | 146 | |
147 | /* | | 147 | /* |
148 | * Allocate a block in the file system. | | 148 | * Allocate a block in the file system. |
149 | * | | 149 | * |
150 | * The size of the requested block is given, which must be some | | 150 | * The size of the requested block is given, which must be some |
151 | * multiple of fs_fsize and <= fs_bsize. | | 151 | * multiple of fs_fsize and <= fs_bsize. |
152 | * A preference may be optionally specified. If a preference is given | | 152 | * A preference may be optionally specified. If a preference is given |
153 | * the following hierarchy is used to allocate a block: | | 153 | * the following hierarchy is used to allocate a block: |
154 | * 1) allocate the requested block. | | 154 | * 1) allocate the requested block. |
155 | * 2) allocate a rotationally optimal block in the same cylinder. | | 155 | * 2) allocate a rotationally optimal block in the same cylinder. |
156 | * 3) allocate a block in the same cylinder group. | | 156 | * 3) allocate a block in the same cylinder group. |
157 | * 4) quadradically rehash into other cylinder groups, until an | | 157 | * 4) quadradically rehash into other cylinder groups, until an |
158 | * available block is located. | | 158 | * available block is located. |
159 | * If no block preference is given the following hierarchy is used | | 159 | * If no block preference is given the following hierarchy is used |
160 | * to allocate a block: | | 160 | * to allocate a block: |
161 | * 1) allocate a block in the cylinder group that contains the | | 161 | * 1) allocate a block in the cylinder group that contains the |
162 | * inode for the file. | | 162 | * inode for the file. |
163 | * 2) quadradically rehash into other cylinder groups, until an | | 163 | * 2) quadradically rehash into other cylinder groups, until an |
164 | * available block is located. | | 164 | * available block is located. |
165 | * | | 165 | * |
166 | * => called with um_lock held | | 166 | * => called with um_lock held |
167 | * => releases um_lock before returning | | 167 | * => releases um_lock before returning |
168 | */ | | 168 | */ |
169 | int | | 169 | int |
170 | ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size, int flags, | | 170 | ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size, int flags, |
171 | kauth_cred_t cred, daddr_t *bnp) | | 171 | kauth_cred_t cred, daddr_t *bnp) |
172 | { | | 172 | { |
173 | struct ufsmount *ump; | | 173 | struct ufsmount *ump; |
174 | struct fs *fs; | | 174 | struct fs *fs; |
175 | daddr_t bno; | | 175 | daddr_t bno; |
176 | int cg; | | 176 | int cg; |
177 | #ifdef QUOTA | | 177 | #ifdef QUOTA |
178 | int error; | | 178 | int error; |
179 | #endif | | 179 | #endif |
180 | | | 180 | |
181 | fs = ip->i_fs; | | 181 | fs = ip->i_fs; |
182 | ump = ip->i_ump; | | 182 | ump = ip->i_ump; |
183 | | | 183 | |
184 | KASSERT(mutex_owned(&ump->um_lock)); | | 184 | KASSERT(mutex_owned(&ump->um_lock)); |
185 | | | 185 | |
186 | #ifdef UVM_PAGE_TRKOWN | | 186 | #ifdef UVM_PAGE_TRKOWN |
187 | if (ITOV(ip)->v_type == VREG && | | 187 | if (ITOV(ip)->v_type == VREG && |
188 | lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) { | | 188 | lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) { |
189 | struct vm_page *pg; | | 189 | struct vm_page *pg; |
190 | struct uvm_object *uobj = &ITOV(ip)->v_uobj; | | 190 | struct uvm_object *uobj = &ITOV(ip)->v_uobj; |
191 | voff_t off = trunc_page(lblktosize(fs, lbn)); | | 191 | voff_t off = trunc_page(lblktosize(fs, lbn)); |
192 | voff_t endoff = round_page(lblktosize(fs, lbn) + size); | | 192 | voff_t endoff = round_page(lblktosize(fs, lbn) + size); |
193 | | | 193 | |
194 | mutex_enter(&uobj->vmobjlock); | | 194 | mutex_enter(&uobj->vmobjlock); |
195 | while (off < endoff) { | | 195 | while (off < endoff) { |
196 | pg = uvm_pagelookup(uobj, off); | | 196 | pg = uvm_pagelookup(uobj, off); |
197 | KASSERT(pg != NULL); | | 197 | KASSERT(pg != NULL); |
198 | KASSERT(pg->owner == curproc->p_pid); | | 198 | KASSERT(pg->owner == curproc->p_pid); |
199 | off += PAGE_SIZE; | | 199 | off += PAGE_SIZE; |
200 | } | | 200 | } |
201 | mutex_exit(&uobj->vmobjlock); | | 201 | mutex_exit(&uobj->vmobjlock); |
202 | } | | 202 | } |
203 | #endif | | 203 | #endif |
204 | | | 204 | |
205 | *bnp = 0; | | 205 | *bnp = 0; |
206 | #ifdef DIAGNOSTIC | | 206 | #ifdef DIAGNOSTIC |
207 | if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { | | 207 | if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { |
208 | printf("dev = 0x%llx, bsize = %d, size = %d, fs = %s\n", | | 208 | printf("dev = 0x%llx, bsize = %d, size = %d, fs = %s\n", |
209 | ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); | | 209 | (unsigned long long)ip->i_dev, fs->fs_bsize, size, |
| | | 210 | fs->fs_fsmnt); |
210 | panic("ffs_alloc: bad size"); | | 211 | panic("ffs_alloc: bad size"); |
211 | } | | 212 | } |
212 | if (cred == NOCRED) | | 213 | if (cred == NOCRED) |
213 | panic("ffs_alloc: missing credential"); | | 214 | panic("ffs_alloc: missing credential"); |
214 | #endif /* DIAGNOSTIC */ | | 215 | #endif /* DIAGNOSTIC */ |
215 | if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) | | 216 | if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) |
216 | goto nospace; | | 217 | goto nospace; |
217 | if (freespace(fs, fs->fs_minfree) <= 0 && | | 218 | if (freespace(fs, fs->fs_minfree) <= 0 && |
218 | kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) | | 219 | kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) |
219 | goto nospace; | | 220 | goto nospace; |
220 | #ifdef QUOTA | | 221 | #ifdef QUOTA |
221 | mutex_exit(&ump->um_lock); | | 222 | mutex_exit(&ump->um_lock); |
222 | if ((error = chkdq(ip, btodb(size), cred, 0)) != 0) | | 223 | if ((error = chkdq(ip, btodb(size), cred, 0)) != 0) |
223 | return (error); | | 224 | return (error); |
224 | mutex_enter(&ump->um_lock); | | 225 | mutex_enter(&ump->um_lock); |
225 | #endif | | 226 | #endif |
226 | | | 227 | |
227 | if (bpref >= fs->fs_size) | | 228 | if (bpref >= fs->fs_size) |
228 | bpref = 0; | | 229 | bpref = 0; |
229 | if (bpref == 0) | | 230 | if (bpref == 0) |
230 | cg = ino_to_cg(fs, ip->i_number); | | 231 | cg = ino_to_cg(fs, ip->i_number); |
231 | else | | 232 | else |
232 | cg = dtog(fs, bpref); | | 233 | cg = dtog(fs, bpref); |
233 | bno = ffs_hashalloc(ip, cg, bpref, size, flags, ffs_alloccg); | | 234 | bno = ffs_hashalloc(ip, cg, bpref, size, flags, ffs_alloccg); |
234 | if (bno > 0) { | | 235 | if (bno > 0) { |
235 | DIP_ADD(ip, blocks, btodb(size)); | | 236 | DIP_ADD(ip, blocks, btodb(size)); |
236 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | | 237 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
237 | *bnp = bno; | | 238 | *bnp = bno; |
238 | return (0); | | 239 | return (0); |
239 | } | | 240 | } |
240 | #ifdef QUOTA | | 241 | #ifdef QUOTA |
241 | /* | | 242 | /* |
242 | * Restore user's disk quota because allocation failed. | | 243 | * Restore user's disk quota because allocation failed. |
243 | */ | | 244 | */ |
244 | (void) chkdq(ip, -btodb(size), cred, FORCE); | | 245 | (void) chkdq(ip, -btodb(size), cred, FORCE); |
245 | #endif | | 246 | #endif |
246 | if (flags & B_CONTIG) { | | 247 | if (flags & B_CONTIG) { |
247 | /* | | 248 | /* |
248 | * XXX ump->um_lock handling is "suspect" at best. | | 249 | * XXX ump->um_lock handling is "suspect" at best. |
249 | * For the case where ffs_hashalloc() fails early | | 250 | * For the case where ffs_hashalloc() fails early |
250 | * in the B_CONTIG case we reach here with um_lock | | 251 | * in the B_CONTIG case we reach here with um_lock |
251 | * already unlocked, so we can't release it again | | 252 | * already unlocked, so we can't release it again |
252 | * like in the normal error path. See kern/39206. | | 253 | * like in the normal error path. See kern/39206. |
253 | * | | 254 | * |
254 | * | | 255 | * |
255 | * Fail silently - it's up to our caller to report | | 256 | * Fail silently - it's up to our caller to report |
256 | * errors. | | 257 | * errors. |
257 | */ | | 258 | */ |
258 | return (ENOSPC); | | 259 | return (ENOSPC); |
259 | } | | 260 | } |
260 | nospace: | | 261 | nospace: |
261 | mutex_exit(&ump->um_lock); | | 262 | mutex_exit(&ump->um_lock); |
262 | ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full"); | | 263 | ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full"); |
263 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); | | 264 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); |
264 | return (ENOSPC); | | 265 | return (ENOSPC); |
265 | } | | 266 | } |
266 | | | 267 | |
267 | /* | | 268 | /* |
268 | * Reallocate a fragment to a bigger size | | 269 | * Reallocate a fragment to a bigger size |
269 | * | | 270 | * |
270 | * The number and size of the old block is given, and a preference | | 271 | * The number and size of the old block is given, and a preference |
271 | * and new size is also specified. The allocator attempts to extend | | 272 | * and new size is also specified. The allocator attempts to extend |
272 | * the original block. Failing that, the regular block allocator is | | 273 | * the original block. Failing that, the regular block allocator is |
273 | * invoked to get an appropriate block. | | 274 | * invoked to get an appropriate block. |
274 | * | | 275 | * |
275 | * => called with um_lock held | | 276 | * => called with um_lock held |
276 | * => return with um_lock released | | 277 | * => return with um_lock released |
277 | */ | | 278 | */ |
278 | int | | 279 | int |
279 | ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize, | | 280 | ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize, |
280 | int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop) | | 281 | int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop) |
281 | { | | 282 | { |
282 | struct ufsmount *ump; | | 283 | struct ufsmount *ump; |
283 | struct fs *fs; | | 284 | struct fs *fs; |
284 | struct buf *bp; | | 285 | struct buf *bp; |
285 | int cg, request, error; | | 286 | int cg, request, error; |
286 | daddr_t bprev, bno; | | 287 | daddr_t bprev, bno; |
287 | | | 288 | |
288 | fs = ip->i_fs; | | 289 | fs = ip->i_fs; |
289 | ump = ip->i_ump; | | 290 | ump = ip->i_ump; |
290 | | | 291 | |
291 | KASSERT(mutex_owned(&ump->um_lock)); | | 292 | KASSERT(mutex_owned(&ump->um_lock)); |
292 | | | 293 | |
293 | #ifdef UVM_PAGE_TRKOWN | | 294 | #ifdef UVM_PAGE_TRKOWN |
294 | if (ITOV(ip)->v_type == VREG) { | | 295 | if (ITOV(ip)->v_type == VREG) { |
295 | struct vm_page *pg; | | 296 | struct vm_page *pg; |
296 | struct uvm_object *uobj = &ITOV(ip)->v_uobj; | | 297 | struct uvm_object *uobj = &ITOV(ip)->v_uobj; |
297 | voff_t off = trunc_page(lblktosize(fs, lbprev)); | | 298 | voff_t off = trunc_page(lblktosize(fs, lbprev)); |
298 | voff_t endoff = round_page(lblktosize(fs, lbprev) + osize); | | 299 | voff_t endoff = round_page(lblktosize(fs, lbprev) + osize); |
299 | | | 300 | |
300 | mutex_enter(&uobj->vmobjlock); | | 301 | mutex_enter(&uobj->vmobjlock); |
301 | while (off < endoff) { | | 302 | while (off < endoff) { |
302 | pg = uvm_pagelookup(uobj, off); | | 303 | pg = uvm_pagelookup(uobj, off); |
303 | KASSERT(pg != NULL); | | 304 | KASSERT(pg != NULL); |
304 | KASSERT(pg->owner == curproc->p_pid); | | 305 | KASSERT(pg->owner == curproc->p_pid); |
305 | KASSERT((pg->flags & PG_CLEAN) == 0); | | 306 | KASSERT((pg->flags & PG_CLEAN) == 0); |
306 | off += PAGE_SIZE; | | 307 | off += PAGE_SIZE; |
307 | } | | 308 | } |
308 | mutex_exit(&uobj->vmobjlock); | | 309 | mutex_exit(&uobj->vmobjlock); |
309 | } | | 310 | } |
310 | #endif | | 311 | #endif |
311 | | | 312 | |
312 | #ifdef DIAGNOSTIC | | 313 | #ifdef DIAGNOSTIC |
313 | if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || | | 314 | if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || |
314 | (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { | | 315 | (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { |
315 | printf( | | 316 | printf( |
316 | "dev = 0x%llx, bsize = %d, osize = %d, nsize = %d, fs = %s\n", | | 317 | "dev = 0x%llx, bsize = %d, osize = %d, nsize = %d, fs = %s\n", |
317 | ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); | | 318 | (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize, |
| | | 319 | fs->fs_fsmnt); |
318 | panic("ffs_realloccg: bad size"); | | 320 | panic("ffs_realloccg: bad size"); |
319 | } | | 321 | } |
320 | if (cred == NOCRED) | | 322 | if (cred == NOCRED) |
321 | panic("ffs_realloccg: missing credential"); | | 323 | panic("ffs_realloccg: missing credential"); |
322 | #endif /* DIAGNOSTIC */ | | 324 | #endif /* DIAGNOSTIC */ |
323 | if (freespace(fs, fs->fs_minfree) <= 0 && | | 325 | if (freespace(fs, fs->fs_minfree) <= 0 && |
324 | kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) { | | 326 | kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) { |
325 | mutex_exit(&ump->um_lock); | | 327 | mutex_exit(&ump->um_lock); |
326 | goto nospace; | | 328 | goto nospace; |
327 | } | | 329 | } |
328 | if (fs->fs_magic == FS_UFS2_MAGIC) | | 330 | if (fs->fs_magic == FS_UFS2_MAGIC) |
329 | bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs)); | | 331 | bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs)); |
330 | else | | 332 | else |
331 | bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs)); | | 333 | bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs)); |
332 | | | 334 | |
333 | if (bprev == 0) { | | 335 | if (bprev == 0) { |
334 | printf("dev = 0x%llx, bsize = %d, bprev = %" PRId64 ", fs = %s\n", | | 336 | printf("dev = 0x%llx, bsize = %d, bprev = %" PRId64 ", fs = %s\n", |
335 | (unsigned long long)ip->i_dev, fs->fs_bsize, bprev, | | 337 | (unsigned long long)ip->i_dev, fs->fs_bsize, bprev, |
336 | fs->fs_fsmnt); | | 338 | fs->fs_fsmnt); |
337 | panic("ffs_realloccg: bad bprev"); | | 339 | panic("ffs_realloccg: bad bprev"); |
338 | } | | 340 | } |
339 | mutex_exit(&ump->um_lock); | | 341 | mutex_exit(&ump->um_lock); |
340 | | | 342 | |
341 | /* | | 343 | /* |
342 | * Allocate the extra space in the buffer. | | 344 | * Allocate the extra space in the buffer. |
343 | */ | | 345 | */ |
344 | if (bpp != NULL && | | 346 | if (bpp != NULL && |
345 | (error = bread(ITOV(ip), lbprev, osize, NOCRED, 0, &bp)) != 0) { | | 347 | (error = bread(ITOV(ip), lbprev, osize, NOCRED, 0, &bp)) != 0) { |
346 | brelse(bp, 0); | | 348 | brelse(bp, 0); |
347 | return (error); | | 349 | return (error); |
348 | } | | 350 | } |
349 | #ifdef QUOTA | | 351 | #ifdef QUOTA |
350 | if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) { | | 352 | if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) { |
351 | if (bpp != NULL) { | | 353 | if (bpp != NULL) { |
352 | brelse(bp, 0); | | 354 | brelse(bp, 0); |
353 | } | | 355 | } |
354 | return (error); | | 356 | return (error); |
355 | } | | 357 | } |
356 | #endif | | 358 | #endif |
357 | /* | | 359 | /* |
358 | * Check for extension in the existing location. | | 360 | * Check for extension in the existing location. |
359 | */ | | 361 | */ |
360 | cg = dtog(fs, bprev); | | 362 | cg = dtog(fs, bprev); |
361 | mutex_enter(&ump->um_lock); | | 363 | mutex_enter(&ump->um_lock); |
362 | if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) { | | 364 | if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) { |
363 | DIP_ADD(ip, blocks, btodb(nsize - osize)); | | 365 | DIP_ADD(ip, blocks, btodb(nsize - osize)); |
364 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | | 366 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
365 | | | 367 | |
366 | if (bpp != NULL) { | | 368 | if (bpp != NULL) { |
367 | if (bp->b_blkno != fsbtodb(fs, bno)) | | 369 | if (bp->b_blkno != fsbtodb(fs, bno)) |
368 | panic("bad blockno"); | | 370 | panic("bad blockno"); |
369 | allocbuf(bp, nsize, 1); | | 371 | allocbuf(bp, nsize, 1); |
370 | memset((char *)bp->b_data + osize, 0, nsize - osize); | | 372 | memset((char *)bp->b_data + osize, 0, nsize - osize); |
371 | mutex_enter(bp->b_objlock); | | 373 | mutex_enter(bp->b_objlock); |
372 | KASSERT(!cv_has_waiters(&bp->b_done)); | | 374 | KASSERT(!cv_has_waiters(&bp->b_done)); |
373 | bp->b_oflags |= BO_DONE; | | 375 | bp->b_oflags |= BO_DONE; |
374 | mutex_exit(bp->b_objlock); | | 376 | mutex_exit(bp->b_objlock); |
375 | *bpp = bp; | | 377 | *bpp = bp; |
376 | } | | 378 | } |
377 | if (blknop != NULL) { | | 379 | if (blknop != NULL) { |
378 | *blknop = bno; | | 380 | *blknop = bno; |
379 | } | | 381 | } |
380 | return (0); | | 382 | return (0); |
381 | } | | 383 | } |
382 | /* | | 384 | /* |
383 | * Allocate a new disk location. | | 385 | * Allocate a new disk location. |
384 | */ | | 386 | */ |
385 | if (bpref >= fs->fs_size) | | 387 | if (bpref >= fs->fs_size) |
386 | bpref = 0; | | 388 | bpref = 0; |
387 | switch ((int)fs->fs_optim) { | | 389 | switch ((int)fs->fs_optim) { |
388 | case FS_OPTSPACE: | | 390 | case FS_OPTSPACE: |
389 | /* | | 391 | /* |
390 | * Allocate an exact sized fragment. Although this makes | | 392 | * Allocate an exact sized fragment. Although this makes |
391 | * best use of space, we will waste time relocating it if | | 393 | * best use of space, we will waste time relocating it if |
392 | * the file continues to grow. If the fragmentation is | | 394 | * the file continues to grow. If the fragmentation is |
393 | * less than half of the minimum free reserve, we choose | | 395 | * less than half of the minimum free reserve, we choose |
394 | * to begin optimizing for time. | | 396 | * to begin optimizing for time. |
395 | */ | | 397 | */ |
396 | request = nsize; | | 398 | request = nsize; |
397 | if (fs->fs_minfree < 5 || | | 399 | if (fs->fs_minfree < 5 || |
398 | fs->fs_cstotal.cs_nffree > | | 400 | fs->fs_cstotal.cs_nffree > |
399 | fs->fs_dsize * fs->fs_minfree / (2 * 100)) | | 401 | fs->fs_dsize * fs->fs_minfree / (2 * 100)) |
400 | break; | | 402 | break; |
401 | | | 403 | |
402 | if (ffs_log_changeopt) { | | 404 | if (ffs_log_changeopt) { |
403 | log(LOG_NOTICE, | | 405 | log(LOG_NOTICE, |
404 | "%s: optimization changed from SPACE to TIME\n", | | 406 | "%s: optimization changed from SPACE to TIME\n", |
405 | fs->fs_fsmnt); | | 407 | fs->fs_fsmnt); |
406 | } | | 408 | } |
407 | | | 409 | |
408 | fs->fs_optim = FS_OPTTIME; | | 410 | fs->fs_optim = FS_OPTTIME; |
409 | break; | | 411 | break; |
410 | case FS_OPTTIME: | | 412 | case FS_OPTTIME: |
411 | /* | | 413 | /* |
412 | * At this point we have discovered a file that is trying to | | 414 | * At this point we have discovered a file that is trying to |
413 | * grow a small fragment to a larger fragment. To save time, | | 415 | * grow a small fragment to a larger fragment. To save time, |
414 | * we allocate a full sized block, then free the unused portion. | | 416 | * we allocate a full sized block, then free the unused portion. |
415 | * If the file continues to grow, the `ffs_fragextend' call | | 417 | * If the file continues to grow, the `ffs_fragextend' call |
416 | * above will be able to grow it in place without further | | 418 | * above will be able to grow it in place without further |
417 | * copying. If aberrant programs cause disk fragmentation to | | 419 | * copying. If aberrant programs cause disk fragmentation to |
418 | * grow within 2% of the free reserve, we choose to begin | | 420 | * grow within 2% of the free reserve, we choose to begin |
419 | * optimizing for space. | | 421 | * optimizing for space. |
420 | */ | | 422 | */ |
421 | request = fs->fs_bsize; | | 423 | request = fs->fs_bsize; |
422 | if (fs->fs_cstotal.cs_nffree < | | 424 | if (fs->fs_cstotal.cs_nffree < |
423 | fs->fs_dsize * (fs->fs_minfree - 2) / 100) | | 425 | fs->fs_dsize * (fs->fs_minfree - 2) / 100) |
424 | break; | | 426 | break; |
425 | | | 427 | |
426 | if (ffs_log_changeopt) { | | 428 | if (ffs_log_changeopt) { |
427 | log(LOG_NOTICE, | | 429 | log(LOG_NOTICE, |
428 | "%s: optimization changed from TIME to SPACE\n", | | 430 | "%s: optimization changed from TIME to SPACE\n", |
429 | fs->fs_fsmnt); | | 431 | fs->fs_fsmnt); |
430 | } | | 432 | } |
431 | | | 433 | |
432 | fs->fs_optim = FS_OPTSPACE; | | 434 | fs->fs_optim = FS_OPTSPACE; |
433 | break; | | 435 | break; |
434 | default: | | 436 | default: |
435 | printf("dev = 0x%llx, optim = %d, fs = %s\n", | | 437 | printf("dev = 0x%llx, optim = %d, fs = %s\n", |
436 | (unsigned long long)ip->i_dev, fs->fs_optim, fs->fs_fsmnt); | | 438 | (unsigned long long)ip->i_dev, fs->fs_optim, fs->fs_fsmnt); |
437 | panic("ffs_realloccg: bad optim"); | | 439 | panic("ffs_realloccg: bad optim"); |
438 | /* NOTREACHED */ | | 440 | /* NOTREACHED */ |
439 | } | | 441 | } |
440 | bno = ffs_hashalloc(ip, cg, bpref, request, 0, ffs_alloccg); | | 442 | bno = ffs_hashalloc(ip, cg, bpref, request, 0, ffs_alloccg); |
441 | if (bno > 0) { | | 443 | if (bno > 0) { |
442 | if (!DOINGSOFTDEP(ITOV(ip))) { | | 444 | if (!DOINGSOFTDEP(ITOV(ip))) { |
443 | if ((ip->i_ump->um_mountp->mnt_wapbl) && | | 445 | if ((ip->i_ump->um_mountp->mnt_wapbl) && |
444 | (ITOV(ip)->v_type != VREG)) { | | 446 | (ITOV(ip)->v_type != VREG)) { |
445 | UFS_WAPBL_REGISTER_DEALLOCATION( | | 447 | UFS_WAPBL_REGISTER_DEALLOCATION( |
446 | ip->i_ump->um_mountp, fsbtodb(fs, bprev), | | 448 | ip->i_ump->um_mountp, fsbtodb(fs, bprev), |
447 | osize); | | 449 | osize); |
448 | } else | | 450 | } else |
449 | ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize, | | 451 | ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize, |
450 | ip->i_number); | | 452 | ip->i_number); |
451 | } | | 453 | } |
452 | if (nsize < request) { | | 454 | if (nsize < request) { |
453 | if ((ip->i_ump->um_mountp->mnt_wapbl) && | | 455 | if ((ip->i_ump->um_mountp->mnt_wapbl) && |
454 | (ITOV(ip)->v_type != VREG)) { | | 456 | (ITOV(ip)->v_type != VREG)) { |
455 | UFS_WAPBL_REGISTER_DEALLOCATION( | | 457 | UFS_WAPBL_REGISTER_DEALLOCATION( |
456 | ip->i_ump->um_mountp, | | 458 | ip->i_ump->um_mountp, |
457 | fsbtodb(fs, (bno + numfrags(fs, nsize))), | | 459 | fsbtodb(fs, (bno + numfrags(fs, nsize))), |
458 | request - nsize); | | 460 | request - nsize); |
459 | } else | | 461 | } else |
460 | ffs_blkfree(fs, ip->i_devvp, | | 462 | ffs_blkfree(fs, ip->i_devvp, |
461 | bno + numfrags(fs, nsize), | | 463 | bno + numfrags(fs, nsize), |
462 | (long)(request - nsize), ip->i_number); | | 464 | (long)(request - nsize), ip->i_number); |
463 | } | | 465 | } |
464 | DIP_ADD(ip, blocks, btodb(nsize - osize)); | | 466 | DIP_ADD(ip, blocks, btodb(nsize - osize)); |
465 | ip->i_flag |= IN_CHANGE | IN_UPDATE; | | 467 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
466 | if (bpp != NULL) { | | 468 | if (bpp != NULL) { |
467 | bp->b_blkno = fsbtodb(fs, bno); | | 469 | bp->b_blkno = fsbtodb(fs, bno); |
468 | allocbuf(bp, nsize, 1); | | 470 | allocbuf(bp, nsize, 1); |
469 | memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize); | | 471 | memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize); |
470 | mutex_enter(bp->b_objlock); | | 472 | mutex_enter(bp->b_objlock); |
471 | KASSERT(!cv_has_waiters(&bp->b_done)); | | 473 | KASSERT(!cv_has_waiters(&bp->b_done)); |
472 | bp->b_oflags |= BO_DONE; | | 474 | bp->b_oflags |= BO_DONE; |
473 | mutex_exit(bp->b_objlock); | | 475 | mutex_exit(bp->b_objlock); |
474 | *bpp = bp; | | 476 | *bpp = bp; |
475 | } | | 477 | } |
476 | if (blknop != NULL) { | | 478 | if (blknop != NULL) { |
477 | *blknop = bno; | | 479 | *blknop = bno; |
478 | } | | 480 | } |
479 | return (0); | | 481 | return (0); |
480 | } | | 482 | } |
481 | mutex_exit(&ump->um_lock); | | 483 | mutex_exit(&ump->um_lock); |
482 | | | 484 | |
483 | #ifdef QUOTA | | 485 | #ifdef QUOTA |
484 | /* | | 486 | /* |
485 | * Restore user's disk quota because allocation failed. | | 487 | * Restore user's disk quota because allocation failed. |
486 | */ | | 488 | */ |
487 | (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE); | | 489 | (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE); |
488 | #endif | | 490 | #endif |
489 | if (bpp != NULL) { | | 491 | if (bpp != NULL) { |
490 | brelse(bp, 0); | | 492 | brelse(bp, 0); |
491 | } | | 493 | } |
492 | | | 494 | |
493 | nospace: | | 495 | nospace: |
494 | /* | | 496 | /* |
495 | * no space available | | 497 | * no space available |
496 | */ | | 498 | */ |
497 | ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full"); | | 499 | ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full"); |
498 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); | | 500 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); |
499 | return (ENOSPC); | | 501 | return (ENOSPC); |
500 | } | | 502 | } |
501 | | | 503 | |
502 | /* | | 504 | /* |
503 | * Allocate an inode in the file system. | | 505 | * Allocate an inode in the file system. |
504 | * | | 506 | * |
505 | * If allocating a directory, use ffs_dirpref to select the inode. | | 507 | * If allocating a directory, use ffs_dirpref to select the inode. |
506 | * If allocating in a directory, the following hierarchy is followed: | | 508 | * If allocating in a directory, the following hierarchy is followed: |
507 | * 1) allocate the preferred inode. | | 509 | * 1) allocate the preferred inode. |
508 | * 2) allocate an inode in the same cylinder group. | | 510 | * 2) allocate an inode in the same cylinder group. |
509 | * 3) quadradically rehash into other cylinder groups, until an | | 511 | * 3) quadradically rehash into other cylinder groups, until an |
510 | * available inode is located. | | 512 | * available inode is located. |
511 | * If no inode preference is given the following hierarchy is used | | 513 | * If no inode preference is given the following hierarchy is used |
512 | * to allocate an inode: | | 514 | * to allocate an inode: |
513 | * 1) allocate an inode in cylinder group 0. | | 515 | * 1) allocate an inode in cylinder group 0. |
514 | * 2) quadradically rehash into other cylinder groups, until an | | 516 | * 2) quadradically rehash into other cylinder groups, until an |
515 | * available inode is located. | | 517 | * available inode is located. |
516 | * | | 518 | * |
517 | * => um_lock not held upon entry or return | | 519 | * => um_lock not held upon entry or return |
518 | */ | | 520 | */ |
519 | int | | 521 | int |
520 | ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, | | 522 | ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, |
521 | struct vnode **vpp) | | 523 | struct vnode **vpp) |
522 | { | | 524 | { |
523 | struct ufsmount *ump; | | 525 | struct ufsmount *ump; |
524 | struct inode *pip; | | 526 | struct inode *pip; |
525 | struct fs *fs; | | 527 | struct fs *fs; |
526 | struct inode *ip; | | 528 | struct inode *ip; |
527 | struct timespec ts; | | 529 | struct timespec ts; |
528 | ino_t ino, ipref; | | 530 | ino_t ino, ipref; |
529 | int cg, error; | | 531 | int cg, error; |
530 | | | 532 | |
531 | UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount); | | 533 | UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount); |
532 | | | 534 | |
533 | *vpp = NULL; | | 535 | *vpp = NULL; |
534 | pip = VTOI(pvp); | | 536 | pip = VTOI(pvp); |
535 | fs = pip->i_fs; | | 537 | fs = pip->i_fs; |
536 | ump = pip->i_ump; | | 538 | ump = pip->i_ump; |
537 | | | 539 | |
538 | error = UFS_WAPBL_BEGIN(pvp->v_mount); | | 540 | error = UFS_WAPBL_BEGIN(pvp->v_mount); |
539 | if (error) { | | 541 | if (error) { |
540 | return error; | | 542 | return error; |
541 | } | | 543 | } |
542 | mutex_enter(&ump->um_lock); | | 544 | mutex_enter(&ump->um_lock); |
543 | if (fs->fs_cstotal.cs_nifree == 0) | | 545 | if (fs->fs_cstotal.cs_nifree == 0) |
544 | goto noinodes; | | 546 | goto noinodes; |
545 | | | 547 | |
546 | if ((mode & IFMT) == IFDIR) | | 548 | if ((mode & IFMT) == IFDIR) |
547 | ipref = ffs_dirpref(pip); | | 549 | ipref = ffs_dirpref(pip); |
548 | else | | 550 | else |
549 | ipref = pip->i_number; | | 551 | ipref = pip->i_number; |
550 | if (ipref >= fs->fs_ncg * fs->fs_ipg) | | 552 | if (ipref >= fs->fs_ncg * fs->fs_ipg) |
551 | ipref = 0; | | 553 | ipref = 0; |
552 | cg = ino_to_cg(fs, ipref); | | 554 | cg = ino_to_cg(fs, ipref); |
553 | /* | | 555 | /* |
554 | * Track number of dirs created one after another | | 556 | * Track number of dirs created one after another |
555 | * in a same cg without intervening by files. | | 557 | * in a same cg without intervening by files. |
556 | */ | | 558 | */ |
557 | if ((mode & IFMT) == IFDIR) { | | 559 | if ((mode & IFMT) == IFDIR) { |
558 | if (fs->fs_contigdirs[cg] < 255) | | 560 | if (fs->fs_contigdirs[cg] < 255) |
559 | fs->fs_contigdirs[cg]++; | | 561 | fs->fs_contigdirs[cg]++; |
560 | } else { | | 562 | } else { |
561 | if (fs->fs_contigdirs[cg] > 0) | | 563 | if (fs->fs_contigdirs[cg] > 0) |
562 | fs->fs_contigdirs[cg]--; | | 564 | fs->fs_contigdirs[cg]--; |
563 | } | | 565 | } |
564 | ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, ffs_nodealloccg); | | 566 | ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, ffs_nodealloccg); |
565 | if (ino == 0) | | 567 | if (ino == 0) |
566 | goto noinodes; | | 568 | goto noinodes; |
567 | UFS_WAPBL_END(pvp->v_mount); | | 569 | UFS_WAPBL_END(pvp->v_mount); |
568 | error = VFS_VGET(pvp->v_mount, ino, vpp); | | 570 | error = VFS_VGET(pvp->v_mount, ino, vpp); |
569 | if (error) { | | 571 | if (error) { |
570 | int err; | | 572 | int err; |
571 | err = UFS_WAPBL_BEGIN(pvp->v_mount); | | 573 | err = UFS_WAPBL_BEGIN(pvp->v_mount); |
572 | if (err == 0) | | 574 | if (err == 0) |
573 | ffs_vfree(pvp, ino, mode); | | 575 | ffs_vfree(pvp, ino, mode); |
574 | if (err == 0) | | 576 | if (err == 0) |
575 | UFS_WAPBL_END(pvp->v_mount); | | 577 | UFS_WAPBL_END(pvp->v_mount); |
576 | return (error); | | 578 | return (error); |
577 | } | | 579 | } |
578 | KASSERT((*vpp)->v_type == VNON); | | 580 | KASSERT((*vpp)->v_type == VNON); |
579 | ip = VTOI(*vpp); | | 581 | ip = VTOI(*vpp); |
580 | if (ip->i_mode) { | | 582 | if (ip->i_mode) { |
581 | #if 0 | | 583 | #if 0 |
582 | printf("mode = 0%o, inum = %d, fs = %s\n", | | 584 | printf("mode = 0%o, inum = %d, fs = %s\n", |
583 | ip->i_mode, ip->i_number, fs->fs_fsmnt); | | 585 | ip->i_mode, ip->i_number, fs->fs_fsmnt); |
584 | #else | | 586 | #else |
585 | printf("dmode %x mode %x dgen %x gen %x\n", | | 587 | printf("dmode %x mode %x dgen %x gen %x\n", |
586 | DIP(ip, mode), ip->i_mode, | | 588 | DIP(ip, mode), ip->i_mode, |
587 | DIP(ip, gen), ip->i_gen); | | 589 | DIP(ip, gen), ip->i_gen); |
588 | printf("size %llx blocks %llx\n", | | 590 | printf("size %llx blocks %llx\n", |
589 | (long long)DIP(ip, size), (long long)DIP(ip, blocks)); | | 591 | (long long)DIP(ip, size), (long long)DIP(ip, blocks)); |
590 | printf("ino %llu ipref %llu\n", (unsigned long long)ino, | | 592 | printf("ino %llu ipref %llu\n", (unsigned long long)ino, |
591 | (unsigned long long)ipref); | | 593 | (unsigned long long)ipref); |
592 | #if 0 | | 594 | #if 0 |
593 | error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), | | 595 | error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)), |
594 | (int)fs->fs_bsize, NOCRED, 0, &bp); | | 596 | (int)fs->fs_bsize, NOCRED, 0, &bp); |
595 | #endif | | 597 | #endif |
596 | | | 598 | |
597 | #endif | | 599 | #endif |
598 | panic("ffs_valloc: dup alloc"); | | 600 | panic("ffs_valloc: dup alloc"); |
599 | } | | 601 | } |
600 | if (DIP(ip, blocks)) { /* XXX */ | | 602 | if (DIP(ip, blocks)) { /* XXX */ |
601 | printf("free inode %s/%llu had %" PRId64 " blocks\n", | | 603 | printf("free inode %s/%llu had %" PRId64 " blocks\n", |
602 | fs->fs_fsmnt, (unsigned long long)ino, DIP(ip, blocks)); | | 604 | fs->fs_fsmnt, (unsigned long long)ino, DIP(ip, blocks)); |
603 | DIP_ASSIGN(ip, blocks, 0); | | 605 | DIP_ASSIGN(ip, blocks, 0); |
604 | } | | 606 | } |
605 | ip->i_flag &= ~IN_SPACECOUNTED; | | 607 | ip->i_flag &= ~IN_SPACECOUNTED; |
606 | ip->i_flags = 0; | | 608 | ip->i_flags = 0; |
607 | DIP_ASSIGN(ip, flags, 0); | | 609 | DIP_ASSIGN(ip, flags, 0); |
608 | /* | | 610 | /* |
609 | * Set up a new generation number for this inode. | | 611 | * Set up a new generation number for this inode. |
610 | */ | | 612 | */ |
611 | ip->i_gen++; | | 613 | ip->i_gen++; |
612 | DIP_ASSIGN(ip, gen, ip->i_gen); | | 614 | DIP_ASSIGN(ip, gen, ip->i_gen); |
613 | if (fs->fs_magic == FS_UFS2_MAGIC) { | | 615 | if (fs->fs_magic == FS_UFS2_MAGIC) { |
614 | vfs_timestamp(&ts); | | 616 | vfs_timestamp(&ts); |
615 | ip->i_ffs2_birthtime = ts.tv_sec; | | 617 | ip->i_ffs2_birthtime = ts.tv_sec; |
616 | ip->i_ffs2_birthnsec = ts.tv_nsec; | | 618 | ip->i_ffs2_birthnsec = ts.tv_nsec; |
617 | } | | 619 | } |
618 | return (0); | | 620 | return (0); |
619 | noinodes: | | 621 | noinodes: |
620 | mutex_exit(&ump->um_lock); | | 622 | mutex_exit(&ump->um_lock); |
621 | UFS_WAPBL_END(pvp->v_mount); | | 623 | UFS_WAPBL_END(pvp->v_mount); |
622 | ffs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes"); | | 624 | ffs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes"); |
623 | uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); | | 625 | uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); |
624 | return (ENOSPC); | | 626 | return (ENOSPC); |
625 | } | | 627 | } |
626 | | | 628 | |
627 | /* | | 629 | /* |
628 | * Find a cylinder group in which to place a directory. | | 630 | * Find a cylinder group in which to place a directory. |
629 | * | | 631 | * |
630 | * The policy implemented by this algorithm is to allocate a | | 632 | * The policy implemented by this algorithm is to allocate a |
631 | * directory inode in the same cylinder group as its parent | | 633 | * directory inode in the same cylinder group as its parent |
632 | * directory, but also to reserve space for its files inodes | | 634 | * directory, but also to reserve space for its files inodes |
633 | * and data. Restrict the number of directories which may be | | 635 | * and data. Restrict the number of directories which may be |
634 | * allocated one after another in the same cylinder group | | 636 | * allocated one after another in the same cylinder group |
635 | * without intervening allocation of files. | | 637 | * without intervening allocation of files. |
636 | * | | 638 | * |
637 | * If we allocate a first level directory then force allocation | | 639 | * If we allocate a first level directory then force allocation |
638 | * in another cylinder group. | | 640 | * in another cylinder group. |
639 | */ | | 641 | */ |
640 | static ino_t | | 642 | static ino_t |
641 | ffs_dirpref(struct inode *pip) | | 643 | ffs_dirpref(struct inode *pip) |
642 | { | | 644 | { |
643 | register struct fs *fs; | | 645 | register struct fs *fs; |
644 | int cg, prefcg; | | 646 | int cg, prefcg; |
645 | int64_t dirsize, cgsize, curdsz; | | 647 | int64_t dirsize, cgsize, curdsz; |
646 | int avgifree, avgbfree, avgndir; | | 648 | int avgifree, avgbfree, avgndir; |
647 | int minifree, minbfree, maxndir; | | 649 | int minifree, minbfree, maxndir; |
648 | int mincg, minndir; | | 650 | int mincg, minndir; |
649 | int maxcontigdirs; | | 651 | int maxcontigdirs; |
650 | | | 652 | |
651 | KASSERT(mutex_owned(&pip->i_ump->um_lock)); | | 653 | KASSERT(mutex_owned(&pip->i_ump->um_lock)); |
652 | | | 654 | |
653 | fs = pip->i_fs; | | 655 | fs = pip->i_fs; |
654 | | | 656 | |
655 | avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; | | 657 | avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; |
656 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; | | 658 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; |
657 | avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg; | | 659 | avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg; |
658 | | | 660 | |
659 | /* | | 661 | /* |
660 | * Force allocation in another cg if creating a first level dir. | | 662 | * Force allocation in another cg if creating a first level dir. |
661 | */ | | 663 | */ |
662 | if (ITOV(pip)->v_vflag & VV_ROOT) { | | 664 | if (ITOV(pip)->v_vflag & VV_ROOT) { |
663 | prefcg = random() % fs->fs_ncg; | | 665 | prefcg = random() % fs->fs_ncg; |
664 | mincg = prefcg; | | 666 | mincg = prefcg; |
665 | minndir = fs->fs_ipg; | | 667 | minndir = fs->fs_ipg; |
666 | for (cg = prefcg; cg < fs->fs_ncg; cg++) | | 668 | for (cg = prefcg; cg < fs->fs_ncg; cg++) |
667 | if (fs->fs_cs(fs, cg).cs_ndir < minndir && | | 669 | if (fs->fs_cs(fs, cg).cs_ndir < minndir && |
668 | fs->fs_cs(fs, cg).cs_nifree >= avgifree && | | 670 | fs->fs_cs(fs, cg).cs_nifree >= avgifree && |
669 | fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { | | 671 | fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
670 | mincg = cg; | | 672 | mincg = cg; |
671 | minndir = fs->fs_cs(fs, cg).cs_ndir; | | 673 | minndir = fs->fs_cs(fs, cg).cs_ndir; |
672 | } | | 674 | } |
673 | for (cg = 0; cg < prefcg; cg++) | | 675 | for (cg = 0; cg < prefcg; cg++) |
674 | if (fs->fs_cs(fs, cg).cs_ndir < minndir && | | 676 | if (fs->fs_cs(fs, cg).cs_ndir < minndir && |
675 | fs->fs_cs(fs, cg).cs_nifree >= avgifree && | | 677 | fs->fs_cs(fs, cg).cs_nifree >= avgifree && |
676 | fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { | | 678 | fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
677 | mincg = cg; | | 679 | mincg = cg; |
678 | minndir = fs->fs_cs(fs, cg).cs_ndir; | | 680 | minndir = fs->fs_cs(fs, cg).cs_ndir; |
679 | } | | 681 | } |
680 | return ((ino_t)(fs->fs_ipg * mincg)); | | 682 | return ((ino_t)(fs->fs_ipg * mincg)); |
681 | } | | 683 | } |
682 | | | 684 | |
683 | /* | | 685 | /* |
684 | * Count various limits which used for | | 686 | * Count various limits which used for |
685 | * optimal allocation of a directory inode. | | 687 | * optimal allocation of a directory inode. |
686 | */ | | 688 | */ |
687 | maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg); | | 689 | maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg); |
688 | minifree = avgifree - fs->fs_ipg / 4; | | 690 | minifree = avgifree - fs->fs_ipg / 4; |
689 | if (minifree < 0) | | 691 | if (minifree < 0) |
690 | minifree = 0; | | 692 | minifree = 0; |
691 | minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4; | | 693 | minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4; |
692 | if (minbfree < 0) | | 694 | if (minbfree < 0) |
693 | minbfree = 0; | | 695 | minbfree = 0; |
694 | cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg; | | 696 | cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg; |
695 | dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir; | | 697 | dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir; |
696 | if (avgndir != 0) { | | 698 | if (avgndir != 0) { |
697 | curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir; | | 699 | curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir; |
698 | if (dirsize < curdsz) | | 700 | if (dirsize < curdsz) |
699 | dirsize = curdsz; | | 701 | dirsize = curdsz; |
700 | } | | 702 | } |
701 | if (cgsize < dirsize * 255) | | 703 | if (cgsize < dirsize * 255) |
702 | maxcontigdirs = cgsize / dirsize; | | 704 | maxcontigdirs = cgsize / dirsize; |
703 | else | | 705 | else |
704 | maxcontigdirs = 255; | | 706 | maxcontigdirs = 255; |
705 | if (fs->fs_avgfpdir > 0) | | 707 | if (fs->fs_avgfpdir > 0) |
706 | maxcontigdirs = min(maxcontigdirs, | | 708 | maxcontigdirs = min(maxcontigdirs, |
707 | fs->fs_ipg / fs->fs_avgfpdir); | | 709 | fs->fs_ipg / fs->fs_avgfpdir); |
708 | if (maxcontigdirs == 0) | | 710 | if (maxcontigdirs == 0) |
709 | maxcontigdirs = 1; | | 711 | maxcontigdirs = 1; |
710 | | | 712 | |
711 | /* | | 713 | /* |
712 | * Limit number of dirs in one cg and reserve space for | | 714 | * Limit number of dirs in one cg and reserve space for |
713 | * regular files, but only if we have no deficit in | | 715 | * regular files, but only if we have no deficit in |
714 | * inodes or space. | | 716 | * inodes or space. |
715 | */ | | 717 | */ |
716 | prefcg = ino_to_cg(fs, pip->i_number); | | 718 | prefcg = ino_to_cg(fs, pip->i_number); |
717 | for (cg = prefcg; cg < fs->fs_ncg; cg++) | | 719 | for (cg = prefcg; cg < fs->fs_ncg; cg++) |
718 | if (fs->fs_cs(fs, cg).cs_ndir < maxndir && | | 720 | if (fs->fs_cs(fs, cg).cs_ndir < maxndir && |
719 | fs->fs_cs(fs, cg).cs_nifree >= minifree && | | 721 | fs->fs_cs(fs, cg).cs_nifree >= minifree && |
720 | fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { | | 722 | fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { |
721 | if (fs->fs_contigdirs[cg] < maxcontigdirs) | | 723 | if (fs->fs_contigdirs[cg] < maxcontigdirs) |
722 | return ((ino_t)(fs->fs_ipg * cg)); | | 724 | return ((ino_t)(fs->fs_ipg * cg)); |
723 | } | | 725 | } |
724 | for (cg = 0; cg < prefcg; cg++) | | 726 | for (cg = 0; cg < prefcg; cg++) |
725 | if (fs->fs_cs(fs, cg).cs_ndir < maxndir && | | 727 | if (fs->fs_cs(fs, cg).cs_ndir < maxndir && |
726 | fs->fs_cs(fs, cg).cs_nifree >= minifree && | | 728 | fs->fs_cs(fs, cg).cs_nifree >= minifree && |
727 | fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { | | 729 | fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { |
728 | if (fs->fs_contigdirs[cg] < maxcontigdirs) | | 730 | if (fs->fs_contigdirs[cg] < maxcontigdirs) |
729 | return ((ino_t)(fs->fs_ipg * cg)); | | 731 | return ((ino_t)(fs->fs_ipg * cg)); |
730 | } | | 732 | } |
731 | /* | | 733 | /* |
732 | * This is a backstop when we are deficient in space. | | 734 | * This is a backstop when we are deficient in space. |
733 | */ | | 735 | */ |
734 | for (cg = prefcg; cg < fs->fs_ncg; cg++) | | 736 | for (cg = prefcg; cg < fs->fs_ncg; cg++) |
735 | if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) | | 737 | if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) |
736 | return ((ino_t)(fs->fs_ipg * cg)); | | 738 | return ((ino_t)(fs->fs_ipg * cg)); |
737 | for (cg = 0; cg < prefcg; cg++) | | 739 | for (cg = 0; cg < prefcg; cg++) |
738 | if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) | | 740 | if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) |
739 | break; | | 741 | break; |
740 | return ((ino_t)(fs->fs_ipg * cg)); | | 742 | return ((ino_t)(fs->fs_ipg * cg)); |
741 | } | | 743 | } |
742 | | | 744 | |
743 | /* | | 745 | /* |
744 | * Select the desired position for the next block in a file. The file is | | 746 | * Select the desired position for the next block in a file. The file is |
745 | * logically divided into sections. The first section is composed of the | | 747 | * logically divided into sections. The first section is composed of the |
746 | * direct blocks. Each additional section contains fs_maxbpg blocks. | | 748 | * direct blocks. Each additional section contains fs_maxbpg blocks. |
747 | * | | 749 | * |
748 | * If no blocks have been allocated in the first section, the policy is to | | 750 | * If no blocks have been allocated in the first section, the policy is to |
749 | * request a block in the same cylinder group as the inode that describes | | 751 | * request a block in the same cylinder group as the inode that describes |
750 | * the file. If no blocks have been allocated in any other section, the | | 752 | * the file. If no blocks have been allocated in any other section, the |
751 | * policy is to place the section in a cylinder group with a greater than | | 753 | * policy is to place the section in a cylinder group with a greater than |
752 | * average number of free blocks. An appropriate cylinder group is found | | 754 | * average number of free blocks. An appropriate cylinder group is found |
753 | * by using a rotor that sweeps the cylinder groups. When a new group of | | 755 | * by using a rotor that sweeps the cylinder groups. When a new group of |
754 | * blocks is needed, the sweep begins in the cylinder group following the | | 756 | * blocks is needed, the sweep begins in the cylinder group following the |
755 | * cylinder group from which the previous allocation was made. The sweep | | 757 | * cylinder group from which the previous allocation was made. The sweep |
756 | * continues until a cylinder group with greater than the average number | | 758 | * continues until a cylinder group with greater than the average number |
757 | * of free blocks is found. If the allocation is for the first block in an | | 759 | * of free blocks is found. If the allocation is for the first block in an |
758 | * indirect block, the information on the previous allocation is unavailable; | | 760 | * indirect block, the information on the previous allocation is unavailable; |
759 | * here a best guess is made based upon the logical block number being | | 761 | * here a best guess is made based upon the logical block number being |
760 | * allocated. | | 762 | * allocated. |
761 | * | | 763 | * |
762 | * If a section is already partially allocated, the policy is to | | 764 | * If a section is already partially allocated, the policy is to |
763 | * contiguously allocate fs_maxcontig blocks. The end of one of these | | 765 | * contiguously allocate fs_maxcontig blocks. The end of one of these |
764 | * contiguous blocks and the beginning of the next is laid out | | 766 | * contiguous blocks and the beginning of the next is laid out |
765 | * contigously if possible. | | 767 | * contigously if possible. |
766 | * | | 768 | * |
767 | * => um_lock held on entry and exit | | 769 | * => um_lock held on entry and exit |
768 | */ | | 770 | */ |
769 | daddr_t | | 771 | daddr_t |
770 | ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags, | | 772 | ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags, |
771 | int32_t *bap /* XXX ondisk32 */) | | 773 | int32_t *bap /* XXX ondisk32 */) |
772 | { | | 774 | { |
773 | struct fs *fs; | | 775 | struct fs *fs; |
774 | int cg; | | 776 | int cg; |
775 | int avgbfree, startcg; | | 777 | int avgbfree, startcg; |
776 | | | 778 | |
777 | KASSERT(mutex_owned(&ip->i_ump->um_lock)); | | 779 | KASSERT(mutex_owned(&ip->i_ump->um_lock)); |
778 | | | 780 | |
779 | fs = ip->i_fs; | | 781 | fs = ip->i_fs; |
780 | | | 782 | |
781 | /* | | 783 | /* |
782 | * If allocating a contiguous file with B_CONTIG, use the hints | | 784 | * If allocating a contiguous file with B_CONTIG, use the hints |
783 | * in the inode extentions to return the desired block. | | 785 | * in the inode extentions to return the desired block. |
784 | * | | 786 | * |
785 | * For metadata (indirect blocks) return the address of where | | 787 | * For metadata (indirect blocks) return the address of where |
786 | * the first indirect block resides - we'll scan for the next | | 788 | * the first indirect block resides - we'll scan for the next |
787 | * available slot if we need to allocate more than one indirect | | 789 | * available slot if we need to allocate more than one indirect |
788 | * block. For data, return the address of the actual block | | 790 | * block. For data, return the address of the actual block |
789 | * relative to the address of the first data block. | | 791 | * relative to the address of the first data block. |
790 | */ | | 792 | */ |
791 | if (flags & B_CONTIG) { | | 793 | if (flags & B_CONTIG) { |
792 | KASSERT(ip->i_ffs_first_data_blk != 0); | | 794 | KASSERT(ip->i_ffs_first_data_blk != 0); |
793 | KASSERT(ip->i_ffs_first_indir_blk != 0); | | 795 | KASSERT(ip->i_ffs_first_indir_blk != 0); |
794 | if (flags & B_METAONLY) | | 796 | if (flags & B_METAONLY) |
795 | return ip->i_ffs_first_indir_blk; | | 797 | return ip->i_ffs_first_indir_blk; |
796 | else | | 798 | else |
797 | return ip->i_ffs_first_data_blk + blkstofrags(fs, lbn); | | 799 | return ip->i_ffs_first_data_blk + blkstofrags(fs, lbn); |
798 | } | | 800 | } |
799 | | | 801 | |
800 | if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { | | 802 | if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { |
801 | if (lbn < NDADDR + NINDIR(fs)) { | | 803 | if (lbn < NDADDR + NINDIR(fs)) { |
802 | cg = ino_to_cg(fs, ip->i_number); | | 804 | cg = ino_to_cg(fs, ip->i_number); |
803 | return (cgbase(fs, cg) + fs->fs_frag); | | 805 | return (cgbase(fs, cg) + fs->fs_frag); |
804 | } | | 806 | } |
805 | /* | | 807 | /* |
806 | * Find a cylinder with greater than average number of | | 808 | * Find a cylinder with greater than average number of |
807 | * unused data blocks. | | 809 | * unused data blocks. |
808 | */ | | 810 | */ |
809 | if (indx == 0 || bap[indx - 1] == 0) | | 811 | if (indx == 0 || bap[indx - 1] == 0) |
810 | startcg = | | 812 | startcg = |
811 | ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; | | 813 | ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; |
812 | else | | 814 | else |
813 | startcg = dtog(fs, | | 815 | startcg = dtog(fs, |
814 | ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1); | | 816 | ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1); |
815 | startcg %= fs->fs_ncg; | | 817 | startcg %= fs->fs_ncg; |
816 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; | | 818 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; |
817 | for (cg = startcg; cg < fs->fs_ncg; cg++) | | 819 | for (cg = startcg; cg < fs->fs_ncg; cg++) |
818 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { | | 820 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
819 | return (cgbase(fs, cg) + fs->fs_frag); | | 821 | return (cgbase(fs, cg) + fs->fs_frag); |
820 | } | | 822 | } |
821 | for (cg = 0; cg < startcg; cg++) | | 823 | for (cg = 0; cg < startcg; cg++) |
822 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { | | 824 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
823 | return (cgbase(fs, cg) + fs->fs_frag); | | 825 | return (cgbase(fs, cg) + fs->fs_frag); |
824 | } | | 826 | } |
825 | return (0); | | 827 | return (0); |
826 | } | | 828 | } |
827 | /* | | 829 | /* |
828 | * We just always try to lay things out contiguously. | | 830 | * We just always try to lay things out contiguously. |
829 | */ | | 831 | */ |
830 | return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag; | | 832 | return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag; |
831 | } | | 833 | } |
832 | | | 834 | |
833 | daddr_t | | 835 | daddr_t |
834 | ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags, | | 836 | ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags, |
835 | int64_t *bap) | | 837 | int64_t *bap) |
836 | { | | 838 | { |
837 | struct fs *fs; | | 839 | struct fs *fs; |
838 | int cg; | | 840 | int cg; |
839 | int avgbfree, startcg; | | 841 | int avgbfree, startcg; |
840 | | | 842 | |
841 | KASSERT(mutex_owned(&ip->i_ump->um_lock)); | | 843 | KASSERT(mutex_owned(&ip->i_ump->um_lock)); |
842 | | | 844 | |
843 | fs = ip->i_fs; | | 845 | fs = ip->i_fs; |
844 | | | 846 | |
845 | /* | | 847 | /* |
846 | * If allocating a contiguous file with B_CONTIG, use the hints | | 848 | * If allocating a contiguous file with B_CONTIG, use the hints |
847 | * in the inode extentions to return the desired block. | | 849 | * in the inode extentions to return the desired block. |
848 | * | | 850 | * |
849 | * For metadata (indirect blocks) return the address of where | | 851 | * For metadata (indirect blocks) return the address of where |
850 | * the first indirect block resides - we'll scan for the next | | 852 | * the first indirect block resides - we'll scan for the next |
851 | * available slot if we need to allocate more than one indirect | | 853 | * available slot if we need to allocate more than one indirect |
852 | * block. For data, return the address of the actual block | | 854 | * block. For data, return the address of the actual block |
853 | * relative to the address of the first data block. | | 855 | * relative to the address of the first data block. |
854 | */ | | 856 | */ |
855 | if (flags & B_CONTIG) { | | 857 | if (flags & B_CONTIG) { |
856 | KASSERT(ip->i_ffs_first_data_blk != 0); | | 858 | KASSERT(ip->i_ffs_first_data_blk != 0); |
857 | KASSERT(ip->i_ffs_first_indir_blk != 0); | | 859 | KASSERT(ip->i_ffs_first_indir_blk != 0); |
858 | if (flags & B_METAONLY) | | 860 | if (flags & B_METAONLY) |
859 | return ip->i_ffs_first_indir_blk; | | 861 | return ip->i_ffs_first_indir_blk; |
860 | else | | 862 | else |
861 | return ip->i_ffs_first_data_blk + blkstofrags(fs, lbn); | | 863 | return ip->i_ffs_first_data_blk + blkstofrags(fs, lbn); |
862 | } | | 864 | } |
863 | | | 865 | |
864 | if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { | | 866 | if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { |
865 | if (lbn < NDADDR + NINDIR(fs)) { | | 867 | if (lbn < NDADDR + NINDIR(fs)) { |
866 | cg = ino_to_cg(fs, ip->i_number); | | 868 | cg = ino_to_cg(fs, ip->i_number); |
867 | return (cgbase(fs, cg) + fs->fs_frag); | | 869 | return (cgbase(fs, cg) + fs->fs_frag); |
868 | } | | 870 | } |
869 | /* | | 871 | /* |
870 | * Find a cylinder with greater than average number of | | 872 | * Find a cylinder with greater than average number of |
871 | * unused data blocks. | | 873 | * unused data blocks. |
872 | */ | | 874 | */ |
873 | if (indx == 0 || bap[indx - 1] == 0) | | 875 | if (indx == 0 || bap[indx - 1] == 0) |
874 | startcg = | | 876 | startcg = |
875 | ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; | | 877 | ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; |
876 | else | | 878 | else |
877 | startcg = dtog(fs, | | 879 | startcg = dtog(fs, |
878 | ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1); | | 880 | ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1); |
879 | startcg %= fs->fs_ncg; | | 881 | startcg %= fs->fs_ncg; |
880 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; | | 882 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; |
881 | for (cg = startcg; cg < fs->fs_ncg; cg++) | | 883 | for (cg = startcg; cg < fs->fs_ncg; cg++) |
882 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { | | 884 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
883 | return (cgbase(fs, cg) + fs->fs_frag); | | 885 | return (cgbase(fs, cg) + fs->fs_frag); |
884 | } | | 886 | } |
885 | for (cg = 0; cg < startcg; cg++) | | 887 | for (cg = 0; cg < startcg; cg++) |
886 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { | | 888 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
887 | return (cgbase(fs, cg) + fs->fs_frag); | | 889 | return (cgbase(fs, cg) + fs->fs_frag); |
888 | } | | 890 | } |
889 | return (0); | | 891 | return (0); |
890 | } | | 892 | } |
891 | /* | | 893 | /* |
892 | * We just always try to lay things out contiguously. | | 894 | * We just always try to lay things out contiguously. |
893 | */ | | 895 | */ |
894 | return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag; | | 896 | return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag; |
895 | } | | 897 | } |
896 | | | 898 | |
897 | | | 899 | |
898 | /* | | 900 | /* |
899 | * Implement the cylinder overflow algorithm. | | 901 | * Implement the cylinder overflow algorithm. |
900 | * | | 902 | * |
901 | * The policy implemented by this algorithm is: | | 903 | * The policy implemented by this algorithm is: |
902 | * 1) allocate the block in its requested cylinder group. | | 904 | * 1) allocate the block in its requested cylinder group. |
903 | * 2) quadradically rehash on the cylinder group number. | | 905 | * 2) quadradically rehash on the cylinder group number. |
904 | * 3) brute force search for a free block. | | 906 | * 3) brute force search for a free block. |
905 | * | | 907 | * |
906 | * => called with um_lock held | | 908 | * => called with um_lock held |
907 | * => returns with um_lock released on success, held on failure | | 909 | * => returns with um_lock released on success, held on failure |
908 | * (*allocator releases lock on success, retains lock on failure) | | 910 | * (*allocator releases lock on success, retains lock on failure) |
909 | */ | | 911 | */ |
910 | /*VARARGS5*/ | | 912 | /*VARARGS5*/ |
911 | static daddr_t | | 913 | static daddr_t |
912 | ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, | | 914 | ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, |
913 | int size /* size for data blocks, mode for inodes */, | | 915 | int size /* size for data blocks, mode for inodes */, |
914 | int flags, daddr_t (*allocator)(struct inode *, int, daddr_t, int, int)) | | 916 | int flags, daddr_t (*allocator)(struct inode *, int, daddr_t, int, int)) |
915 | { | | 917 | { |
916 | struct fs *fs; | | 918 | struct fs *fs; |
917 | daddr_t result; | | 919 | daddr_t result; |
918 | int i, icg = cg; | | 920 | int i, icg = cg; |
919 | | | 921 | |
920 | fs = ip->i_fs; | | 922 | fs = ip->i_fs; |
921 | /* | | 923 | /* |
922 | * 1: preferred cylinder group | | 924 | * 1: preferred cylinder group |
923 | */ | | 925 | */ |
924 | result = (*allocator)(ip, cg, pref, size, flags); | | 926 | result = (*allocator)(ip, cg, pref, size, flags); |
925 | if (result) | | 927 | if (result) |
926 | return (result); | | 928 | return (result); |
927 | | | 929 | |
928 | if (flags & B_CONTIG) | | 930 | if (flags & B_CONTIG) |
929 | return (result); | | 931 | return (result); |
930 | /* | | 932 | /* |
931 | * 2: quadratic rehash | | 933 | * 2: quadratic rehash |
932 | */ | | 934 | */ |
933 | for (i = 1; i < fs->fs_ncg; i *= 2) { | | 935 | for (i = 1; i < fs->fs_ncg; i *= 2) { |
934 | cg += i; | | 936 | cg += i; |
935 | if (cg >= fs->fs_ncg) | | 937 | if (cg >= fs->fs_ncg) |
936 | cg -= fs->fs_ncg; | | 938 | cg -= fs->fs_ncg; |
937 | result = (*allocator)(ip, cg, 0, size, flags); | | 939 | result = (*allocator)(ip, cg, 0, size, flags); |
938 | if (result) | | 940 | if (result) |
939 | return (result); | | 941 | return (result); |
940 | } | | 942 | } |
941 | /* | | 943 | /* |
942 | * 3: brute force search | | 944 | * 3: brute force search |
943 | * Note that we start at i == 2, since 0 was checked initially, | | 945 | * Note that we start at i == 2, since 0 was checked initially, |
944 | * and 1 is always checked in the quadratic rehash. | | 946 | * and 1 is always checked in the quadratic rehash. |
945 | */ | | 947 | */ |
946 | cg = (icg + 2) % fs->fs_ncg; | | 948 | cg = (icg + 2) % fs->fs_ncg; |
947 | for (i = 2; i < fs->fs_ncg; i++) { | | 949 | for (i = 2; i < fs->fs_ncg; i++) { |
948 | result = (*allocator)(ip, cg, 0, size, flags); | | 950 | result = (*allocator)(ip, cg, 0, size, flags); |
949 | if (result) | | 951 | if (result) |
950 | return (result); | | 952 | return (result); |
951 | cg++; | | 953 | cg++; |
952 | if (cg == fs->fs_ncg) | | 954 | if (cg == fs->fs_ncg) |
953 | cg = 0; | | 955 | cg = 0; |
954 | } | | 956 | } |
955 | return (0); | | 957 | return (0); |
956 | } | | 958 | } |
957 | | | 959 | |
958 | /* | | 960 | /* |
959 | * Determine whether a fragment can be extended. | | 961 | * Determine whether a fragment can be extended. |
960 | * | | 962 | * |
961 | * Check to see if the necessary fragments are available, and | | 963 | * Check to see if the necessary fragments are available, and |
962 | * if they are, allocate them. | | 964 | * if they are, allocate them. |
963 | * | | 965 | * |
964 | * => called with um_lock held | | 966 | * => called with um_lock held |
965 | * => returns with um_lock released on success, held on failure | | 967 | * => returns with um_lock released on success, held on failure |
966 | */ | | 968 | */ |
967 | static daddr_t | | 969 | static daddr_t |
968 | ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize) | | 970 | ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize) |
969 | { | | 971 | { |
970 | struct ufsmount *ump; | | 972 | struct ufsmount *ump; |
971 | struct fs *fs; | | 973 | struct fs *fs; |
972 | struct cg *cgp; | | 974 | struct cg *cgp; |
973 | struct buf *bp; | | 975 | struct buf *bp; |
974 | daddr_t bno; | | 976 | daddr_t bno; |
975 | int frags, bbase; | | 977 | int frags, bbase; |
976 | int i, error; | | 978 | int i, error; |
977 | u_int8_t *blksfree; | | 979 | u_int8_t *blksfree; |
978 | | | 980 | |
979 | fs = ip->i_fs; | | 981 | fs = ip->i_fs; |
980 | ump = ip->i_ump; | | 982 | ump = ip->i_ump; |
981 | | | 983 | |
982 | KASSERT(mutex_owned(&ump->um_lock)); | | 984 | KASSERT(mutex_owned(&ump->um_lock)); |
983 | | | 985 | |
984 | if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) | | 986 | if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) |
985 | return (0); | | 987 | return (0); |
986 | frags = numfrags(fs, nsize); | | 988 | frags = numfrags(fs, nsize); |
987 | bbase = fragnum(fs, bprev); | | 989 | bbase = fragnum(fs, bprev); |
988 | if (bbase > fragnum(fs, (bprev + frags - 1))) { | | 990 | if (bbase > fragnum(fs, (bprev + frags - 1))) { |
989 | /* cannot extend across a block boundary */ | | 991 | /* cannot extend across a block boundary */ |
990 | return (0); | | 992 | return (0); |
991 | } | | 993 | } |
992 | mutex_exit(&ump->um_lock); | | 994 | mutex_exit(&ump->um_lock); |
993 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), | | 995 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
994 | (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp); | | 996 | (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp); |
995 | if (error) | | 997 | if (error) |
996 | goto fail; | | 998 | goto fail; |
997 | cgp = (struct cg *)bp->b_data; | | 999 | cgp = (struct cg *)bp->b_data; |
998 | if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) | | 1000 | if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) |
999 | goto fail; | | 1001 | goto fail; |
1000 | cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs)); | | 1002 | cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs)); |
1001 | if ((fs->fs_magic != FS_UFS1_MAGIC) || | | 1003 | if ((fs->fs_magic != FS_UFS1_MAGIC) || |
1002 | (fs->fs_old_flags & FS_FLAGS_UPDATED)) | | 1004 | (fs->fs_old_flags & FS_FLAGS_UPDATED)) |
1003 | cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs)); | | 1005 | cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs)); |
1004 | bno = dtogd(fs, bprev); | | 1006 | bno = dtogd(fs, bprev); |
1005 | blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)); | | 1007 | blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)); |
1006 | for (i = numfrags(fs, osize); i < frags; i++) | | 1008 | for (i = numfrags(fs, osize); i < frags; i++) |
1007 | if (isclr(blksfree, bno + i)) | | 1009 | if (isclr(blksfree, bno + i)) |
1008 | goto fail; | | 1010 | goto fail; |
1009 | /* | | 1011 | /* |
1010 | * the current fragment can be extended | | 1012 | * the current fragment can be extended |
1011 | * deduct the count on fragment being extended into | | 1013 | * deduct the count on fragment being extended into |
1012 | * increase the count on the remaining fragment (if any) | | 1014 | * increase the count on the remaining fragment (if any) |
1013 | * allocate the extended piece | | 1015 | * allocate the extended piece |
1014 | */ | | 1016 | */ |
1015 | for (i = frags; i < fs->fs_frag - bbase; i++) | | 1017 | for (i = frags; i < fs->fs_frag - bbase; i++) |
1016 | if (isclr(blksfree, bno + i)) | | 1018 | if (isclr(blksfree, bno + i)) |
1017 | break; | | 1019 | break; |
1018 | ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs)); | | 1020 | ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs)); |
1019 | if (i != frags) | | 1021 | if (i != frags) |
1020 | ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs)); | | 1022 | ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs)); |
1021 | mutex_enter(&ump->um_lock); | | 1023 | mutex_enter(&ump->um_lock); |
1022 | for (i = numfrags(fs, osize); i < frags; i++) { | | 1024 | for (i = numfrags(fs, osize); i < frags; i++) { |
1023 | clrbit(blksfree, bno + i); | | 1025 | clrbit(blksfree, bno + i); |
1024 | ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs)); | | 1026 | ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs)); |
1025 | fs->fs_cstotal.cs_nffree--; | | 1027 | fs->fs_cstotal.cs_nffree--; |
1026 | fs->fs_cs(fs, cg).cs_nffree--; | | 1028 | fs->fs_cs(fs, cg).cs_nffree--; |
1027 | } | | 1029 | } |
1028 | fs->fs_fmod = 1; | | 1030 | fs->fs_fmod = 1; |
1029 | ACTIVECG_CLR(fs, cg); | | 1031 | ACTIVECG_CLR(fs, cg); |
1030 | mutex_exit(&ump->um_lock); | | 1032 | mutex_exit(&ump->um_lock); |
1031 | if (DOINGSOFTDEP(ITOV(ip))) | | 1033 | if (DOINGSOFTDEP(ITOV(ip))) |
1032 | softdep_setup_blkmapdep(bp, fs, bprev); | | 1034 | softdep_setup_blkmapdep(bp, fs, bprev); |
1033 | bdwrite(bp); | | 1035 | bdwrite(bp); |
1034 | return (bprev); | | 1036 | return (bprev); |
1035 | | | 1037 | |
1036 | fail: | | 1038 | fail: |
1037 | brelse(bp, 0); | | 1039 | brelse(bp, 0); |
1038 | mutex_enter(&ump->um_lock); | | 1040 | mutex_enter(&ump->um_lock); |
1039 | return (0); | | 1041 | return (0); |
1040 | } | | 1042 | } |
1041 | | | 1043 | |
1042 | /* | | 1044 | /* |
1043 | * Determine whether a block can be allocated. | | 1045 | * Determine whether a block can be allocated. |
1044 | * | | 1046 | * |
1045 | * Check to see if a block of the appropriate size is available, | | 1047 | * Check to see if a block of the appropriate size is available, |
1046 | * and if it is, allocate it. | | 1048 | * and if it is, allocate it. |
1047 | */ | | 1049 | */ |
1048 | static daddr_t | | 1050 | static daddr_t |
1049 | ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int flags) | | 1051 | ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int flags) |
1050 | { | | 1052 | { |
1051 | struct ufsmount *ump; | | 1053 | struct ufsmount *ump; |
1052 | struct fs *fs = ip->i_fs; | | 1054 | struct fs *fs = ip->i_fs; |
1053 | struct cg *cgp; | | 1055 | struct cg *cgp; |
1054 | struct buf *bp; | | 1056 | struct buf *bp; |
1055 | int32_t bno; | | 1057 | int32_t bno; |
1056 | daddr_t blkno; | | 1058 | daddr_t blkno; |
1057 | int error, frags, allocsiz, i; | | 1059 | int error, frags, allocsiz, i; |
1058 | u_int8_t *blksfree; | | 1060 | u_int8_t *blksfree; |
1059 | #ifdef FFS_EI | | 1061 | #ifdef FFS_EI |
1060 | const int needswap = UFS_FSNEEDSWAP(fs); | | 1062 | const int needswap = UFS_FSNEEDSWAP(fs); |
1061 | #endif | | 1063 | #endif |
1062 | | | 1064 | |
1063 | ump = ip->i_ump; | | 1065 | ump = ip->i_ump; |
1064 | | | 1066 | |
1065 | KASSERT(mutex_owned(&ump->um_lock)); | | 1067 | KASSERT(mutex_owned(&ump->um_lock)); |
1066 | | | 1068 | |
1067 | if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) | | 1069 | if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) |
1068 | return (0); | | 1070 | return (0); |
1069 | mutex_exit(&ump->um_lock); | | 1071 | mutex_exit(&ump->um_lock); |
1070 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), | | 1072 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
1071 | (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp); | | 1073 | (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp); |
1072 | if (error) | | 1074 | if (error) |
1073 | goto fail; | | 1075 | goto fail; |
1074 | cgp = (struct cg *)bp->b_data; | | 1076 | cgp = (struct cg *)bp->b_data; |
1075 | if (!cg_chkmagic(cgp, needswap) || | | 1077 | if (!cg_chkmagic(cgp, needswap) || |
1076 | (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) | | 1078 | (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) |
1077 | goto fail; | | 1079 | goto fail; |
1078 | cgp->cg_old_time = ufs_rw32(time_second, needswap); | | 1080 | cgp->cg_old_time = ufs_rw32(time_second, needswap); |
1079 | if ((fs->fs_magic != FS_UFS1_MAGIC) || | | 1081 | if ((fs->fs_magic != FS_UFS1_MAGIC) || |
1080 | (fs->fs_old_flags & FS_FLAGS_UPDATED)) | | 1082 | (fs->fs_old_flags & FS_FLAGS_UPDATED)) |
1081 | cgp->cg_time = ufs_rw64(time_second, needswap); | | 1083 | cgp->cg_time = ufs_rw64(time_second, needswap); |
1082 | if (size == fs->fs_bsize) { | | 1084 | if (size == fs->fs_bsize) { |
1083 | mutex_enter(&ump->um_lock); | | 1085 | mutex_enter(&ump->um_lock); |
1084 | blkno = ffs_alloccgblk(ip, bp, bpref, flags); | | 1086 | blkno = ffs_alloccgblk(ip, bp, bpref, flags); |
1085 | ACTIVECG_CLR(fs, cg); | | 1087 | ACTIVECG_CLR(fs, cg); |
1086 | mutex_exit(&ump->um_lock); | | 1088 | mutex_exit(&ump->um_lock); |
1087 | bdwrite(bp); | | 1089 | bdwrite(bp); |
1088 | return (blkno); | | 1090 | return (blkno); |
1089 | } | | 1091 | } |
1090 | /* | | 1092 | /* |
1091 | * check to see if any fragments are already available | | 1093 | * check to see if any fragments are already available |
1092 | * allocsiz is the size which will be allocated, hacking | | 1094 | * allocsiz is the size which will be allocated, hacking |
1093 | * it down to a smaller size if necessary | | 1095 | * it down to a smaller size if necessary |
1094 | */ | | 1096 | */ |
1095 | blksfree = cg_blksfree(cgp, needswap); | | 1097 | blksfree = cg_blksfree(cgp, needswap); |
1096 | frags = numfrags(fs, size); | | 1098 | frags = numfrags(fs, size); |
1097 | for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) | | 1099 | for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) |
1098 | if (cgp->cg_frsum[allocsiz] != 0) | | 1100 | if (cgp->cg_frsum[allocsiz] != 0) |
1099 | break; | | 1101 | break; |
1100 | if (allocsiz == fs->fs_frag) { | | 1102 | if (allocsiz == fs->fs_frag) { |
1101 | /* | | 1103 | /* |
1102 | * no fragments were available, so a block will be | | 1104 | * no fragments were available, so a block will be |
1103 | * allocated, and hacked up | | 1105 | * allocated, and hacked up |
1104 | */ | | 1106 | */ |
1105 | if (cgp->cg_cs.cs_nbfree == 0) | | 1107 | if (cgp->cg_cs.cs_nbfree == 0) |
1106 | goto fail; | | 1108 | goto fail; |
1107 | mutex_enter(&ump->um_lock); | | 1109 | mutex_enter(&ump->um_lock); |
1108 | blkno = ffs_alloccgblk(ip, bp, bpref, flags); | | 1110 | blkno = ffs_alloccgblk(ip, bp, bpref, flags); |
1109 | bno = dtogd(fs, blkno); | | 1111 | bno = dtogd(fs, blkno); |
1110 | for (i = frags; i < fs->fs_frag; i++) | | 1112 | for (i = frags; i < fs->fs_frag; i++) |
1111 | setbit(blksfree, bno + i); | | 1113 | setbit(blksfree, bno + i); |
1112 | i = fs->fs_frag - frags; | | 1114 | i = fs->fs_frag - frags; |
1113 | ufs_add32(cgp->cg_cs.cs_nffree, i, needswap); | | 1115 | ufs_add32(cgp->cg_cs.cs_nffree, i, needswap); |
1114 | fs->fs_cstotal.cs_nffree += i; | | 1116 | fs->fs_cstotal.cs_nffree += i; |
1115 | fs->fs_cs(fs, cg).cs_nffree += i; | | 1117 | fs->fs_cs(fs, cg).cs_nffree += i; |
1116 | fs->fs_fmod = 1; | | 1118 | fs->fs_fmod = 1; |
1117 | ufs_add32(cgp->cg_frsum[i], 1, needswap); | | 1119 | ufs_add32(cgp->cg_frsum[i], 1, needswap); |
1118 | ACTIVECG_CLR(fs, cg); | | 1120 | ACTIVECG_CLR(fs, cg); |
1119 | mutex_exit(&ump->um_lock); | | 1121 | mutex_exit(&ump->um_lock); |
1120 | bdwrite(bp); | | 1122 | bdwrite(bp); |
1121 | return (blkno); | | 1123 | return (blkno); |
1122 | } | | 1124 | } |
1123 | bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); | | 1125 | bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); |
1124 | #if 0 | | 1126 | #if 0 |
1125 | /* | | 1127 | /* |
1126 | * XXX fvdl mapsearch will panic, and never return -1 | | 1128 | * XXX fvdl mapsearch will panic, and never return -1 |
1127 | * also: returning NULL as daddr_t ? | | 1129 | * also: returning NULL as daddr_t ? |
1128 | */ | | 1130 | */ |
1129 | if (bno < 0) | | 1131 | if (bno < 0) |
1130 | goto fail; | | 1132 | goto fail; |
1131 | #endif | | 1133 | #endif |
1132 | for (i = 0; i < frags; i++) | | 1134 | for (i = 0; i < frags; i++) |
1133 | clrbit(blksfree, bno + i); | | 1135 | clrbit(blksfree, bno + i); |
1134 | mutex_enter(&ump->um_lock); | | 1136 | mutex_enter(&ump->um_lock); |
1135 | ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap); | | 1137 | ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap); |
1136 | fs->fs_cstotal.cs_nffree -= frags; | | 1138 | fs->fs_cstotal.cs_nffree -= frags; |
1137 | fs->fs_cs(fs, cg).cs_nffree -= frags; | | 1139 | fs->fs_cs(fs, cg).cs_nffree -= frags; |
1138 | fs->fs_fmod = 1; | | 1140 | fs->fs_fmod = 1; |
1139 | ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap); | | 1141 | ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap); |
1140 | if (frags != allocsiz) | | 1142 | if (frags != allocsiz) |
1141 | ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap); | | 1143 | ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap); |
1142 | blkno = cg * fs->fs_fpg + bno; | | 1144 | blkno = cg * fs->fs_fpg + bno; |
1143 | ACTIVECG_CLR(fs, cg); | | 1145 | ACTIVECG_CLR(fs, cg); |
1144 | mutex_exit(&ump->um_lock); | | 1146 | mutex_exit(&ump->um_lock); |
1145 | if (DOINGSOFTDEP(ITOV(ip))) | | 1147 | if (DOINGSOFTDEP(ITOV(ip))) |
1146 | softdep_setup_blkmapdep(bp, fs, blkno); | | 1148 | softdep_setup_blkmapdep(bp, fs, blkno); |
1147 | bdwrite(bp); | | 1149 | bdwrite(bp); |
1148 | return blkno; | | 1150 | return blkno; |
1149 | | | 1151 | |
1150 | fail: | | 1152 | fail: |
1151 | brelse(bp, 0); | | 1153 | brelse(bp, 0); |
1152 | mutex_enter(&ump->um_lock); | | 1154 | mutex_enter(&ump->um_lock); |
1153 | return (0); | | 1155 | return (0); |
1154 | } | | 1156 | } |
1155 | | | 1157 | |
1156 | /* | | 1158 | /* |
1157 | * Allocate a block in a cylinder group. | | 1159 | * Allocate a block in a cylinder group. |
1158 | * | | 1160 | * |
1159 | * This algorithm implements the following policy: | | 1161 | * This algorithm implements the following policy: |
1160 | * 1) allocate the requested block. | | 1162 | * 1) allocate the requested block. |
1161 | * 2) allocate a rotationally optimal block in the same cylinder. | | 1163 | * 2) allocate a rotationally optimal block in the same cylinder. |
1162 | * 3) allocate the next available block on the block rotor for the | | 1164 | * 3) allocate the next available block on the block rotor for the |
1163 | * specified cylinder group. | | 1165 | * specified cylinder group. |
1164 | * Note that this routine only allocates fs_bsize blocks; these | | 1166 | * Note that this routine only allocates fs_bsize blocks; these |
1165 | * blocks may be fragmented by the routine that allocates them. | | 1167 | * blocks may be fragmented by the routine that allocates them. |
1166 | */ | | 1168 | */ |
1167 | static daddr_t | | 1169 | static daddr_t |
1168 | ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int flags) | | 1170 | ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int flags) |
1169 | { | | 1171 | { |
1170 | struct ufsmount *ump; | | 1172 | struct ufsmount *ump; |
1171 | struct fs *fs = ip->i_fs; | | 1173 | struct fs *fs = ip->i_fs; |
1172 | struct cg *cgp; | | 1174 | struct cg *cgp; |
1173 | daddr_t blkno; | | 1175 | daddr_t blkno; |
1174 | int32_t bno; | | 1176 | int32_t bno; |
1175 | u_int8_t *blksfree; | | 1177 | u_int8_t *blksfree; |
1176 | #ifdef FFS_EI | | 1178 | #ifdef FFS_EI |
1177 | const int needswap = UFS_FSNEEDSWAP(fs); | | 1179 | const int needswap = UFS_FSNEEDSWAP(fs); |
1178 | #endif | | 1180 | #endif |
1179 | | | 1181 | |
1180 | ump = ip->i_ump; | | 1182 | ump = ip->i_ump; |
1181 | | | 1183 | |
1182 | KASSERT(mutex_owned(&ump->um_lock)); | | 1184 | KASSERT(mutex_owned(&ump->um_lock)); |
1183 | | | 1185 | |
1184 | cgp = (struct cg *)bp->b_data; | | 1186 | cgp = (struct cg *)bp->b_data; |
1185 | blksfree = cg_blksfree(cgp, needswap); | | 1187 | blksfree = cg_blksfree(cgp, needswap); |
1186 | if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) { | | 1188 | if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) { |
1187 | bpref = ufs_rw32(cgp->cg_rotor, needswap); | | 1189 | bpref = ufs_rw32(cgp->cg_rotor, needswap); |
1188 | } else { | | 1190 | } else { |
1189 | bpref = blknum(fs, bpref); | | 1191 | bpref = blknum(fs, bpref); |
1190 | bno = dtogd(fs, bpref); | | 1192 | bno = dtogd(fs, bpref); |
1191 | /* | | 1193 | /* |
1192 | * if the requested block is available, use it | | 1194 | * if the requested block is available, use it |
1193 | */ | | 1195 | */ |
1194 | if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno))) | | 1196 | if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno))) |
1195 | goto gotit; | | 1197 | goto gotit; |
1196 | /* | | 1198 | /* |
1197 | * if the requested data block isn't available and we are | | 1199 | * if the requested data block isn't available and we are |
1198 | * trying to allocate a contiguous file, return an error. | | 1200 | * trying to allocate a contiguous file, return an error. |
1199 | */ | | 1201 | */ |
1200 | if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG) | | 1202 | if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG) |
1201 | return (0); | | 1203 | return (0); |
1202 | } | | 1204 | } |
1203 | | | 1205 | |
1204 | /* | | 1206 | /* |
1205 | * Take the next available block in this cylinder group. | | 1207 | * Take the next available block in this cylinder group. |
1206 | */ | | 1208 | */ |
1207 | bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); | | 1209 | bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); |
1208 | if (bno < 0) | | 1210 | if (bno < 0) |
1209 | return (0); | | 1211 | return (0); |
1210 | cgp->cg_rotor = ufs_rw32(bno, needswap); | | 1212 | cgp->cg_rotor = ufs_rw32(bno, needswap); |
1211 | gotit: | | 1213 | gotit: |
1212 | blkno = fragstoblks(fs, bno); | | 1214 | blkno = fragstoblks(fs, bno); |
1213 | ffs_clrblock(fs, blksfree, blkno); | | 1215 | ffs_clrblock(fs, blksfree, blkno); |
1214 | ffs_clusteracct(fs, cgp, blkno, -1); | | 1216 | ffs_clusteracct(fs, cgp, blkno, -1); |
1215 | ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap); | | 1217 | ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap); |
1216 | fs->fs_cstotal.cs_nbfree--; | | 1218 | fs->fs_cstotal.cs_nbfree--; |
1217 | fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--; | | 1219 | fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--; |
1218 | if ((fs->fs_magic == FS_UFS1_MAGIC) && | | 1220 | if ((fs->fs_magic == FS_UFS1_MAGIC) && |
1219 | ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) { | | 1221 | ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) { |
1220 | int cylno; | | 1222 | int cylno; |
1221 | cylno = old_cbtocylno(fs, bno); | | 1223 | cylno = old_cbtocylno(fs, bno); |
1222 | KASSERT(cylno >= 0); | | 1224 | KASSERT(cylno >= 0); |
1223 | KASSERT(cylno < fs->fs_old_ncyl); | | 1225 | KASSERT(cylno < fs->fs_old_ncyl); |
1224 | KASSERT(old_cbtorpos(fs, bno) >= 0); | | 1226 | KASSERT(old_cbtorpos(fs, bno) >= 0); |
1225 | KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos); | | 1227 | KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos); |
1226 | ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1, | | 1228 | ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1, |
1227 | needswap); | | 1229 | needswap); |
1228 | ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap); | | 1230 | ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap); |
1229 | } | | 1231 | } |
1230 | fs->fs_fmod = 1; | | 1232 | fs->fs_fmod = 1; |
1231 | blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno; | | 1233 | blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno; |
1232 | if (DOINGSOFTDEP(ITOV(ip))) { | | 1234 | if (DOINGSOFTDEP(ITOV(ip))) { |
1233 | mutex_exit(&ump->um_lock); | | 1235 | mutex_exit(&ump->um_lock); |
1234 | softdep_setup_blkmapdep(bp, fs, blkno); | | 1236 | softdep_setup_blkmapdep(bp, fs, blkno); |
1235 | mutex_enter(&ump->um_lock); | | 1237 | mutex_enter(&ump->um_lock); |
1236 | } | | 1238 | } |
1237 | return (blkno); | | 1239 | return (blkno); |
1238 | } | | 1240 | } |
1239 | | | 1241 | |
1240 | /* | | 1242 | /* |
1241 | * Determine whether an inode can be allocated. | | 1243 | * Determine whether an inode can be allocated. |
1242 | * | | 1244 | * |
1243 | * Check to see if an inode is available, and if it is, | | 1245 | * Check to see if an inode is available, and if it is, |
1244 | * allocate it using the following policy: | | 1246 | * allocate it using the following policy: |
1245 | * 1) allocate the requested inode. | | 1247 | * 1) allocate the requested inode. |
1246 | * 2) allocate the next available inode after the requested | | 1248 | * 2) allocate the next available inode after the requested |
1247 | * inode in the specified cylinder group. | | 1249 | * inode in the specified cylinder group. |
1248 | */ | | 1250 | */ |
1249 | static daddr_t | | 1251 | static daddr_t |
1250 | ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int flags) | | 1252 | ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int flags) |
1251 | { | | 1253 | { |
1252 | struct ufsmount *ump = ip->i_ump; | | 1254 | struct ufsmount *ump = ip->i_ump; |
1253 | struct fs *fs = ip->i_fs; | | 1255 | struct fs *fs = ip->i_fs; |
1254 | struct cg *cgp; | | 1256 | struct cg *cgp; |
1255 | struct buf *bp, *ibp; | | 1257 | struct buf *bp, *ibp; |
1256 | u_int8_t *inosused; | | 1258 | u_int8_t *inosused; |
1257 | int error, start, len, loc, map, i; | | 1259 | int error, start, len, loc, map, i; |
1258 | int32_t initediblk; | | 1260 | int32_t initediblk; |
1259 | daddr_t nalloc; | | 1261 | daddr_t nalloc; |
1260 | struct ufs2_dinode *dp2; | | 1262 | struct ufs2_dinode *dp2; |
1261 | #ifdef FFS_EI | | 1263 | #ifdef FFS_EI |
1262 | const int needswap = UFS_FSNEEDSWAP(fs); | | 1264 | const int needswap = UFS_FSNEEDSWAP(fs); |
1263 | #endif | | 1265 | #endif |
1264 | | | 1266 | |
1265 | KASSERT(mutex_owned(&ump->um_lock)); | | 1267 | KASSERT(mutex_owned(&ump->um_lock)); |
1266 | UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp); | | 1268 | UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp); |
1267 | | | 1269 | |
1268 | if (fs->fs_cs(fs, cg).cs_nifree == 0) | | 1270 | if (fs->fs_cs(fs, cg).cs_nifree == 0) |
1269 | return (0); | | 1271 | return (0); |
1270 | mutex_exit(&ump->um_lock); | | 1272 | mutex_exit(&ump->um_lock); |
1271 | ibp = NULL; | | 1273 | ibp = NULL; |
1272 | initediblk = -1; | | 1274 | initediblk = -1; |
1273 | retry: | | 1275 | retry: |
1274 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), | | 1276 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
1275 | (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp); | | 1277 | (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp); |
1276 | if (error) | | 1278 | if (error) |
1277 | goto fail; | | 1279 | goto fail; |
1278 | cgp = (struct cg *)bp->b_data; | | 1280 | cgp = (struct cg *)bp->b_data; |
1279 | if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) | | 1281 | if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) |
1280 | goto fail; | | 1282 | goto fail; |
1281 | | | 1283 | |
1282 | if (ibp != NULL && | | 1284 | if (ibp != NULL && |
1283 | initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) { | | 1285 | initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) { |
1284 | /* Another thread allocated more inodes so we retry the test. */ | | 1286 | /* Another thread allocated more inodes so we retry the test. */ |
1285 | brelse(ibp, BC_INVAL); | | 1287 | brelse(ibp, BC_INVAL); |
1286 | ibp = NULL; | | 1288 | ibp = NULL; |
1287 | } | | 1289 | } |
1288 | /* | | 1290 | /* |
1289 | * Check to see if we need to initialize more inodes. | | 1291 | * Check to see if we need to initialize more inodes. |
1290 | */ | | 1292 | */ |
1291 | if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) { | | 1293 | if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) { |
1292 | initediblk = ufs_rw32(cgp->cg_initediblk, needswap); | | 1294 | initediblk = ufs_rw32(cgp->cg_initediblk, needswap); |
1293 | nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap); | | 1295 | nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap); |
1294 | if (nalloc + INOPB(fs) > initediblk && | | 1296 | if (nalloc + INOPB(fs) > initediblk && |
1295 | initediblk < ufs_rw32(cgp->cg_niblk, needswap)) { | | 1297 | initediblk < ufs_rw32(cgp->cg_niblk, needswap)) { |
1296 | /* | | 1298 | /* |
1297 | * We have to release the cg buffer here to prevent | | 1299 | * We have to release the cg buffer here to prevent |
1298 | * a deadlock when reading the inode block will | | 1300 | * a deadlock when reading the inode block will |
1299 | * run a copy-on-write that might use this cg. | | 1301 | * run a copy-on-write that might use this cg. |
1300 | */ | | 1302 | */ |
1301 | brelse(bp, 0); | | 1303 | brelse(bp, 0); |
1302 | bp = NULL; | | 1304 | bp = NULL; |
1303 | error = ffs_getblk(ip->i_devvp, fsbtodb(fs, | | 1305 | error = ffs_getblk(ip->i_devvp, fsbtodb(fs, |
1304 | ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)), | | 1306 | ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)), |
1305 | FFS_NOBLK, fs->fs_bsize, false, &ibp); | | 1307 | FFS_NOBLK, fs->fs_bsize, false, &ibp); |
1306 | if (error) | | 1308 | if (error) |
1307 | goto fail; | | 1309 | goto fail; |
1308 | goto retry; | | 1310 | goto retry; |
1309 | } | | 1311 | } |
1310 | } | | 1312 | } |
1311 | | | 1313 | |
1312 | cgp->cg_old_time = ufs_rw32(time_second, needswap); | | 1314 | cgp->cg_old_time = ufs_rw32(time_second, needswap); |
1313 | if ((fs->fs_magic != FS_UFS1_MAGIC) || | | 1315 | if ((fs->fs_magic != FS_UFS1_MAGIC) || |
1314 | (fs->fs_old_flags & FS_FLAGS_UPDATED)) | | 1316 | (fs->fs_old_flags & FS_FLAGS_UPDATED)) |
1315 | cgp->cg_time = ufs_rw64(time_second, needswap); | | 1317 | cgp->cg_time = ufs_rw64(time_second, needswap); |
1316 | inosused = cg_inosused(cgp, needswap); | | 1318 | inosused = cg_inosused(cgp, needswap); |