ffs_alloc.c revision 1.113.4.3 1 /* $NetBSD: ffs_alloc.c,v 1.113.4.3 2013/10/29 10:04:12 sborrill Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
35 *
36 * This software was developed for the FreeBSD Project by Marshall
37 * Kirk McKusick and Network Associates Laboratories, the Security
38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
40 * research program
41 *
42 * Copyright (c) 1982, 1986, 1989, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
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
55 * without specific prior written permission.
56 *
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
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
67 * SUCH DAMAGE.
68 *
69 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.113.4.3 2013/10/29 10:04:12 sborrill Exp $");
74
75 #if defined(_KERNEL_OPT)
76 #include "opt_ffs.h"
77 #include "opt_quota.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/buf.h>
83 #include <sys/fstrans.h>
84 #include <sys/kauth.h>
85 #include <sys/kernel.h>
86 #include <sys/mount.h>
87 #include <sys/proc.h>
88 #include <sys/syslog.h>
89 #include <sys/vnode.h>
90 #include <sys/wapbl.h>
91
92 #include <miscfs/specfs/specdev.h>
93 #include <ufs/ufs/quota.h>
94 #include <ufs/ufs/ufsmount.h>
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/ufs_extern.h>
97 #include <ufs/ufs/ufs_bswap.h>
98 #include <ufs/ufs/ufs_wapbl.h>
99
100 #include <ufs/ffs/fs.h>
101 #include <ufs/ffs/ffs_extern.h>
102
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);
105 #ifdef XXXUBC
106 static daddr_t ffs_clusteralloc(struct inode *, int, daddr_t, int);
107 #endif
108 static ino_t ffs_dirpref(struct inode *);
109 static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int);
110 static void ffs_fserr(struct fs *, u_int, const char *);
111 static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int, int,
112 daddr_t (*)(struct inode *, int, daddr_t, int, int));
113 static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int, int);
114 static int32_t ffs_mapsearch(struct fs *, struct cg *,
115 daddr_t, int);
116 #if defined(DIAGNOSTIC) || defined(DEBUG)
117 #ifdef XXXUBC
118 static int ffs_checkblk(struct inode *, daddr_t, long size);
119 #endif
120 #endif
121
122 /* if 1, changes in optimalization strategy are logged */
123 int ffs_log_changeopt = 0;
124
125 /* in ffs_tables.c */
126 extern const int inside[], around[];
127 extern const u_char * const fragtbl[];
128
129 /*
130 * Allocate a block in the file system.
131 *
132 * The size of the requested block is given, which must be some
133 * multiple of fs_fsize and <= fs_bsize.
134 * A preference may be optionally specified. If a preference is given
135 * the following hierarchy is used to allocate a block:
136 * 1) allocate the requested block.
137 * 2) allocate a rotationally optimal block in the same cylinder.
138 * 3) allocate a block in the same cylinder group.
139 * 4) quadradically rehash into other cylinder groups, until an
140 * available block is located.
141 * If no block preference is given the following hierarchy is used
142 * to allocate a block:
143 * 1) allocate a block in the cylinder group that contains the
144 * inode for the file.
145 * 2) quadradically rehash into other cylinder groups, until an
146 * available block is located.
147 *
148 * => called with um_lock held
149 * => releases um_lock before returning
150 */
151 int
152 ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size, int flags,
153 kauth_cred_t cred, daddr_t *bnp)
154 {
155 struct ufsmount *ump;
156 struct fs *fs;
157 daddr_t bno;
158 int cg;
159 #ifdef QUOTA
160 int error;
161 #endif
162
163 fs = ip->i_fs;
164 ump = ip->i_ump;
165
166 KASSERT(mutex_owned(&ump->um_lock));
167
168 #ifdef UVM_PAGE_TRKOWN
169 if (ITOV(ip)->v_type == VREG &&
170 lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) {
171 struct vm_page *pg;
172 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
173 voff_t off = trunc_page(lblktosize(fs, lbn));
174 voff_t endoff = round_page(lblktosize(fs, lbn) + size);
175
176 mutex_enter(&uobj->vmobjlock);
177 while (off < endoff) {
178 pg = uvm_pagelookup(uobj, off);
179 KASSERT(pg != NULL);
180 KASSERT(pg->owner == curproc->p_pid);
181 off += PAGE_SIZE;
182 }
183 mutex_exit(&uobj->vmobjlock);
184 }
185 #endif
186
187 *bnp = 0;
188 #ifdef DIAGNOSTIC
189 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
190 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
191 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
192 panic("ffs_alloc: bad size");
193 }
194 if (cred == NOCRED)
195 panic("ffs_alloc: missing credential");
196 #endif /* DIAGNOSTIC */
197 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
198 goto nospace;
199 if (freespace(fs, fs->fs_minfree) <= 0 &&
200 kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0)
201 goto nospace;
202 #ifdef QUOTA
203 mutex_exit(&ump->um_lock);
204 if ((error = chkdq(ip, btodb(size), cred, 0)) != 0)
205 return (error);
206 mutex_enter(&ump->um_lock);
207 #endif
208
209 if (bpref >= fs->fs_size)
210 bpref = 0;
211 if (bpref == 0)
212 cg = ino_to_cg(fs, ip->i_number);
213 else
214 cg = dtog(fs, bpref);
215 bno = ffs_hashalloc(ip, cg, bpref, size, flags, ffs_alloccg);
216 if (bno > 0) {
217 DIP_ADD(ip, blocks, btodb(size));
218 ip->i_flag |= IN_CHANGE | IN_UPDATE;
219 *bnp = bno;
220 return (0);
221 }
222 #ifdef QUOTA
223 /*
224 * Restore user's disk quota because allocation failed.
225 */
226 (void) chkdq(ip, -btodb(size), cred, FORCE);
227 #endif
228 if (flags & B_CONTIG) {
229 /*
230 * XXX ump->um_lock handling is "suspect" at best.
231 * For the case where ffs_hashalloc() fails early
232 * in the B_CONTIG case we reach here with um_lock
233 * already unlocked, so we can't release it again
234 * like in the normal error path. See kern/39206.
235 *
236 *
237 * Fail silently - it's up to our caller to report
238 * errors.
239 */
240 return (ENOSPC);
241 }
242 nospace:
243 mutex_exit(&ump->um_lock);
244 ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
245 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
246 return (ENOSPC);
247 }
248
249 /*
250 * Reallocate a fragment to a bigger size
251 *
252 * The number and size of the old block is given, and a preference
253 * and new size is also specified. The allocator attempts to extend
254 * the original block. Failing that, the regular block allocator is
255 * invoked to get an appropriate block.
256 *
257 * => called with um_lock held
258 * => return with um_lock released
259 */
260 int
261 ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize,
262 int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop)
263 {
264 struct ufsmount *ump;
265 struct fs *fs;
266 struct buf *bp;
267 int cg, request, error;
268 daddr_t bprev, bno;
269
270 fs = ip->i_fs;
271 ump = ip->i_ump;
272
273 KASSERT(mutex_owned(&ump->um_lock));
274
275 #ifdef UVM_PAGE_TRKOWN
276 if (ITOV(ip)->v_type == VREG) {
277 struct vm_page *pg;
278 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
279 voff_t off = trunc_page(lblktosize(fs, lbprev));
280 voff_t endoff = round_page(lblktosize(fs, lbprev) + osize);
281
282 mutex_enter(&uobj->vmobjlock);
283 while (off < endoff) {
284 pg = uvm_pagelookup(uobj, off);
285 KASSERT(pg != NULL);
286 KASSERT(pg->owner == curproc->p_pid);
287 KASSERT((pg->flags & PG_CLEAN) == 0);
288 off += PAGE_SIZE;
289 }
290 mutex_exit(&uobj->vmobjlock);
291 }
292 #endif
293
294 #ifdef DIAGNOSTIC
295 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
296 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
297 printf(
298 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
299 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
300 panic("ffs_realloccg: bad size");
301 }
302 if (cred == NOCRED)
303 panic("ffs_realloccg: missing credential");
304 #endif /* DIAGNOSTIC */
305 if (freespace(fs, fs->fs_minfree) <= 0 &&
306 kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
307 mutex_exit(&ump->um_lock);
308 goto nospace;
309 }
310 if (fs->fs_magic == FS_UFS2_MAGIC)
311 bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs));
312 else
313 bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs));
314
315 if (bprev == 0) {
316 printf("dev = 0x%x, bsize = %d, bprev = %" PRId64 ", fs = %s\n",
317 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
318 panic("ffs_realloccg: bad bprev");
319 }
320 mutex_exit(&ump->um_lock);
321
322 /*
323 * Allocate the extra space in the buffer.
324 */
325 if (bpp != NULL &&
326 (error = bread(ITOV(ip), lbprev, osize, NOCRED, 0, &bp)) != 0) {
327 brelse(bp, 0);
328 return (error);
329 }
330 #ifdef QUOTA
331 if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) {
332 if (bpp != NULL) {
333 brelse(bp, 0);
334 }
335 return (error);
336 }
337 #endif
338 /*
339 * Check for extension in the existing location.
340 */
341 cg = dtog(fs, bprev);
342 mutex_enter(&ump->um_lock);
343 if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) {
344 DIP_ADD(ip, blocks, btodb(nsize - osize));
345 ip->i_flag |= IN_CHANGE | IN_UPDATE;
346
347 if (bpp != NULL) {
348 if (bp->b_blkno != fsbtodb(fs, bno))
349 panic("bad blockno");
350 allocbuf(bp, nsize, 1);
351 memset((char *)bp->b_data + osize, 0, nsize - osize);
352 mutex_enter(bp->b_objlock);
353 KASSERT(!cv_has_waiters(&bp->b_done));
354 bp->b_oflags |= BO_DONE;
355 mutex_exit(bp->b_objlock);
356 *bpp = bp;
357 }
358 if (blknop != NULL) {
359 *blknop = bno;
360 }
361 return (0);
362 }
363 /*
364 * Allocate a new disk location.
365 */
366 if (bpref >= fs->fs_size)
367 bpref = 0;
368 switch ((int)fs->fs_optim) {
369 case FS_OPTSPACE:
370 /*
371 * Allocate an exact sized fragment. Although this makes
372 * best use of space, we will waste time relocating it if
373 * the file continues to grow. If the fragmentation is
374 * less than half of the minimum free reserve, we choose
375 * to begin optimizing for time.
376 */
377 request = nsize;
378 if (fs->fs_minfree < 5 ||
379 fs->fs_cstotal.cs_nffree >
380 fs->fs_dsize * fs->fs_minfree / (2 * 100))
381 break;
382
383 if (ffs_log_changeopt) {
384 log(LOG_NOTICE,
385 "%s: optimization changed from SPACE to TIME\n",
386 fs->fs_fsmnt);
387 }
388
389 fs->fs_optim = FS_OPTTIME;
390 break;
391 case FS_OPTTIME:
392 /*
393 * At this point we have discovered a file that is trying to
394 * grow a small fragment to a larger fragment. To save time,
395 * we allocate a full sized block, then free the unused portion.
396 * If the file continues to grow, the `ffs_fragextend' call
397 * above will be able to grow it in place without further
398 * copying. If aberrant programs cause disk fragmentation to
399 * grow within 2% of the free reserve, we choose to begin
400 * optimizing for space.
401 */
402 request = fs->fs_bsize;
403 if (fs->fs_cstotal.cs_nffree <
404 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
405 break;
406
407 if (ffs_log_changeopt) {
408 log(LOG_NOTICE,
409 "%s: optimization changed from TIME to SPACE\n",
410 fs->fs_fsmnt);
411 }
412
413 fs->fs_optim = FS_OPTSPACE;
414 break;
415 default:
416 printf("dev = 0x%x, optim = %d, fs = %s\n",
417 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
418 panic("ffs_realloccg: bad optim");
419 /* NOTREACHED */
420 }
421 bno = ffs_hashalloc(ip, cg, bpref, request, 0, ffs_alloccg);
422 if (bno > 0) {
423 if (!DOINGSOFTDEP(ITOV(ip))) {
424 if ((ip->i_ump->um_mountp->mnt_wapbl) &&
425 (ITOV(ip)->v_type != VREG)) {
426 UFS_WAPBL_REGISTER_DEALLOCATION(
427 ip->i_ump->um_mountp, fsbtodb(fs, bprev),
428 osize);
429 } else
430 ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize,
431 ip->i_number);
432 }
433 if (nsize < request) {
434 if ((ip->i_ump->um_mountp->mnt_wapbl) &&
435 (ITOV(ip)->v_type != VREG)) {
436 UFS_WAPBL_REGISTER_DEALLOCATION(
437 ip->i_ump->um_mountp,
438 fsbtodb(fs, (bno + numfrags(fs, nsize))),
439 request - nsize);
440 } else
441 ffs_blkfree(fs, ip->i_devvp,
442 bno + numfrags(fs, nsize),
443 (long)(request - nsize), ip->i_number);
444 }
445 DIP_ADD(ip, blocks, btodb(nsize - osize));
446 ip->i_flag |= IN_CHANGE | IN_UPDATE;
447 if (bpp != NULL) {
448 bp->b_blkno = fsbtodb(fs, bno);
449 allocbuf(bp, nsize, 1);
450 memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
451 mutex_enter(bp->b_objlock);
452 KASSERT(!cv_has_waiters(&bp->b_done));
453 bp->b_oflags |= BO_DONE;
454 mutex_exit(bp->b_objlock);
455 *bpp = bp;
456 }
457 if (blknop != NULL) {
458 *blknop = bno;
459 }
460 return (0);
461 }
462 mutex_exit(&ump->um_lock);
463
464 #ifdef QUOTA
465 /*
466 * Restore user's disk quota because allocation failed.
467 */
468 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
469 #endif
470 if (bpp != NULL) {
471 brelse(bp, 0);
472 }
473
474 nospace:
475 /*
476 * no space available
477 */
478 ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
479 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
480 return (ENOSPC);
481 }
482
483 #if 0
484 /*
485 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
486 *
487 * The vnode and an array of buffer pointers for a range of sequential
488 * logical blocks to be made contiguous is given. The allocator attempts
489 * to find a range of sequential blocks starting as close as possible
490 * from the end of the allocation for the logical block immediately
491 * preceding the current range. If successful, the physical block numbers
492 * in the buffer pointers and in the inode are changed to reflect the new
493 * allocation. If unsuccessful, the allocation is left unchanged. The
494 * success in doing the reallocation is returned. Note that the error
495 * return is not reflected back to the user. Rather the previous block
496 * allocation will be used.
497
498 */
499 #ifdef XXXUBC
500 #ifdef DEBUG
501 #include <sys/sysctl.h>
502 int prtrealloc = 0;
503 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
504 #endif
505 #endif
506
507 /*
508 * NOTE: when re-enabling this, it must be updated for UFS2 and WAPBL.
509 */
510
511 int doasyncfree = 1;
512
513 int
514 ffs_reallocblks(void *v)
515 {
516 #ifdef XXXUBC
517 struct vop_reallocblks_args /* {
518 struct vnode *a_vp;
519 struct cluster_save *a_buflist;
520 } */ *ap = v;
521 struct fs *fs;
522 struct inode *ip;
523 struct vnode *vp;
524 struct buf *sbp, *ebp;
525 int32_t *bap, *ebap = NULL, *sbap; /* XXX ondisk32 */
526 struct cluster_save *buflist;
527 daddr_t start_lbn, end_lbn, soff, newblk, blkno;
528 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
529 int i, len, start_lvl, end_lvl, pref, ssize;
530 struct ufsmount *ump;
531 #endif /* XXXUBC */
532
533 /* XXXUBC don't reallocblks for now */
534 return ENOSPC;
535
536 #ifdef XXXUBC
537 vp = ap->a_vp;
538 ip = VTOI(vp);
539 fs = ip->i_fs;
540 ump = ip->i_ump;
541 if (fs->fs_contigsumsize <= 0)
542 return (ENOSPC);
543 buflist = ap->a_buflist;
544 len = buflist->bs_nchildren;
545 start_lbn = buflist->bs_children[0]->b_lblkno;
546 end_lbn = start_lbn + len - 1;
547 #ifdef DIAGNOSTIC
548 for (i = 0; i < len; i++)
549 if (!ffs_checkblk(ip,
550 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
551 panic("ffs_reallocblks: unallocated block 1");
552 for (i = 1; i < len; i++)
553 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
554 panic("ffs_reallocblks: non-logical cluster");
555 blkno = buflist->bs_children[0]->b_blkno;
556 ssize = fsbtodb(fs, fs->fs_frag);
557 for (i = 1; i < len - 1; i++)
558 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
559 panic("ffs_reallocblks: non-physical cluster %d", i);
560 #endif
561 /*
562 * If the latest allocation is in a new cylinder group, assume that
563 * the filesystem has decided to move and do not force it back to
564 * the previous cylinder group.
565 */
566 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
567 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
568 return (ENOSPC);
569 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
570 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
571 return (ENOSPC);
572 /*
573 * Get the starting offset and block map for the first block.
574 */
575 if (start_lvl == 0) {
576 sbap = &ip->i_ffs1_db[0];
577 soff = start_lbn;
578 } else {
579 idp = &start_ap[start_lvl - 1];
580 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize,
581 NOCRED, B_MODIFY, &sbp)) {
582 brelse(sbp, 0);
583 return (ENOSPC);
584 }
585 sbap = (int32_t *)sbp->b_data;
586 soff = idp->in_off;
587 }
588 /*
589 * Find the preferred location for the cluster.
590 */
591 mutex_enter(&ump->um_lock);
592 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
593 /*
594 * If the block range spans two block maps, get the second map.
595 */
596 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
597 ssize = len;
598 } else {
599 #ifdef DIAGNOSTIC
600 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
601 panic("ffs_reallocblk: start == end");
602 #endif
603 ssize = len - (idp->in_off + 1);
604 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize,
605 NOCRED, B_MODIFY, &ebp))
606 goto fail;
607 ebap = (int32_t *)ebp->b_data; /* XXX ondisk32 */
608 }
609 /*
610 * Search the block map looking for an allocation of the desired size.
611 */
612 if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
613 len, flags, ffs_clusteralloc)) == 0) {
614 mutex_exit(&ump->um_lock);
615 goto fail;
616 }
617 /*
618 * We have found a new contiguous block.
619 *
620 * First we have to replace the old block pointers with the new
621 * block pointers in the inode and indirect blocks associated
622 * with the file.
623 */
624 #ifdef DEBUG
625 if (prtrealloc)
626 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
627 start_lbn, end_lbn);
628 #endif
629 blkno = newblk;
630 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
631 daddr_t ba;
632
633 if (i == ssize) {
634 bap = ebap;
635 soff = -i;
636 }
637 /* XXX ondisk32 */
638 ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
639 #ifdef DIAGNOSTIC
640 if (!ffs_checkblk(ip,
641 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
642 panic("ffs_reallocblks: unallocated block 2");
643 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
644 panic("ffs_reallocblks: alloc mismatch");
645 #endif
646 #ifdef DEBUG
647 if (prtrealloc)
648 printf(" %d,", ba);
649 #endif
650 if (DOINGSOFTDEP(vp)) {
651 if (sbap == &ip->i_ffs1_db[0] && i < ssize)
652 softdep_setup_allocdirect(ip, start_lbn + i,
653 blkno, ba, fs->fs_bsize, fs->fs_bsize,
654 buflist->bs_children[i]);
655 else
656 softdep_setup_allocindir_page(ip, start_lbn + i,
657 i < ssize ? sbp : ebp, soff + i, blkno,
658 ba, buflist->bs_children[i]);
659 }
660 /* XXX ondisk32 */
661 *bap++ = ufs_rw32((u_int32_t)blkno, UFS_FSNEEDSWAP(fs));
662 }
663 /*
664 * Next we must write out the modified inode and indirect blocks.
665 * For strict correctness, the writes should be synchronous since
666 * the old block values may have been written to disk. In practise
667 * they are almost never written, but if we are concerned about
668 * strict correctness, the `doasyncfree' flag should be set to zero.
669 *
670 * The test on `doasyncfree' should be changed to test a flag
671 * that shows whether the associated buffers and inodes have
672 * been written. The flag should be set when the cluster is
673 * started and cleared whenever the buffer or inode is flushed.
674 * We can then check below to see if it is set, and do the
675 * synchronous write only when it has been cleared.
676 */
677 if (sbap != &ip->i_ffs1_db[0]) {
678 if (doasyncfree)
679 bdwrite(sbp);
680 else
681 bwrite(sbp);
682 } else {
683 ip->i_flag |= IN_CHANGE | IN_UPDATE;
684 if (!doasyncfree)
685 ffs_update(vp, NULL, NULL, 1);
686 }
687 if (ssize < len) {
688 if (doasyncfree)
689 bdwrite(ebp);
690 else
691 bwrite(ebp);
692 }
693 /*
694 * Last, free the old blocks and assign the new blocks to the buffers.
695 */
696 #ifdef DEBUG
697 if (prtrealloc)
698 printf("\n\tnew:");
699 #endif
700 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
701 if (!DOINGSOFTDEP(vp))
702 ffs_blkfree(fs, ip->i_devvp,
703 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
704 fs->fs_bsize, ip->i_number);
705 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
706 #ifdef DEBUG
707 if (!ffs_checkblk(ip,
708 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
709 panic("ffs_reallocblks: unallocated block 3");
710 if (prtrealloc)
711 printf(" %d,", blkno);
712 #endif
713 }
714 #ifdef DEBUG
715 if (prtrealloc) {
716 prtrealloc--;
717 printf("\n");
718 }
719 #endif
720 return (0);
721
722 fail:
723 if (ssize < len)
724 brelse(ebp, 0);
725 if (sbap != &ip->i_ffs1_db[0])
726 brelse(sbp, 0);
727 return (ENOSPC);
728 #endif /* XXXUBC */
729 }
730 #endif /* 0 */
731
732 /*
733 * Allocate an inode in the file system.
734 *
735 * If allocating a directory, use ffs_dirpref to select the inode.
736 * If allocating in a directory, the following hierarchy is followed:
737 * 1) allocate the preferred inode.
738 * 2) allocate an inode in the same cylinder group.
739 * 3) quadradically rehash into other cylinder groups, until an
740 * available inode is located.
741 * If no inode preference is given the following hierarchy is used
742 * to allocate an inode:
743 * 1) allocate an inode in cylinder group 0.
744 * 2) quadradically rehash into other cylinder groups, until an
745 * available inode is located.
746 *
747 * => um_lock not held upon entry or return
748 */
749 int
750 ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
751 struct vnode **vpp)
752 {
753 struct ufsmount *ump;
754 struct inode *pip;
755 struct fs *fs;
756 struct inode *ip;
757 struct timespec ts;
758 ino_t ino, ipref;
759 int cg, error;
760
761 UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount);
762
763 *vpp = NULL;
764 pip = VTOI(pvp);
765 fs = pip->i_fs;
766 ump = pip->i_ump;
767
768 error = UFS_WAPBL_BEGIN(pvp->v_mount);
769 if (error) {
770 return error;
771 }
772 mutex_enter(&ump->um_lock);
773 if (fs->fs_cstotal.cs_nifree == 0)
774 goto noinodes;
775
776 if ((mode & IFMT) == IFDIR)
777 ipref = ffs_dirpref(pip);
778 else
779 ipref = pip->i_number;
780 if (ipref >= fs->fs_ncg * fs->fs_ipg)
781 ipref = 0;
782 cg = ino_to_cg(fs, ipref);
783 /*
784 * Track number of dirs created one after another
785 * in a same cg without intervening by files.
786 */
787 if ((mode & IFMT) == IFDIR) {
788 if (fs->fs_contigdirs[cg] < 255)
789 fs->fs_contigdirs[cg]++;
790 } else {
791 if (fs->fs_contigdirs[cg] > 0)
792 fs->fs_contigdirs[cg]--;
793 }
794 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, ffs_nodealloccg);
795 if (ino == 0)
796 goto noinodes;
797 UFS_WAPBL_END(pvp->v_mount);
798 error = VFS_VGET(pvp->v_mount, ino, vpp);
799 if (error) {
800 int err;
801 err = UFS_WAPBL_BEGIN(pvp->v_mount);
802 if (err == 0)
803 ffs_vfree(pvp, ino, mode);
804 if (err == 0)
805 UFS_WAPBL_END(pvp->v_mount);
806 return (error);
807 }
808 KASSERT((*vpp)->v_type == VNON);
809 ip = VTOI(*vpp);
810 if (ip->i_mode) {
811 #if 0
812 printf("mode = 0%o, inum = %d, fs = %s\n",
813 ip->i_mode, ip->i_number, fs->fs_fsmnt);
814 #else
815 printf("dmode %x mode %x dgen %x gen %x\n",
816 DIP(ip, mode), ip->i_mode,
817 DIP(ip, gen), ip->i_gen);
818 printf("size %llx blocks %llx\n",
819 (long long)DIP(ip, size), (long long)DIP(ip, blocks));
820 printf("ino %llu ipref %llu\n", (unsigned long long)ino,
821 (unsigned long long)ipref);
822 #if 0
823 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
824 (int)fs->fs_bsize, NOCRED, 0, &bp);
825 #endif
826
827 #endif
828 panic("ffs_valloc: dup alloc");
829 }
830 if (DIP(ip, blocks)) { /* XXX */
831 printf("free inode %s/%llu had %" PRId64 " blocks\n",
832 fs->fs_fsmnt, (unsigned long long)ino, DIP(ip, blocks));
833 DIP_ASSIGN(ip, blocks, 0);
834 }
835 ip->i_flag &= ~IN_SPACECOUNTED;
836 ip->i_flags = 0;
837 DIP_ASSIGN(ip, flags, 0);
838 /*
839 * Set up a new generation number for this inode.
840 */
841 ip->i_gen++;
842 DIP_ASSIGN(ip, gen, ip->i_gen);
843 if (fs->fs_magic == FS_UFS2_MAGIC) {
844 vfs_timestamp(&ts);
845 ip->i_ffs2_birthtime = ts.tv_sec;
846 ip->i_ffs2_birthnsec = ts.tv_nsec;
847 }
848 return (0);
849 noinodes:
850 mutex_exit(&ump->um_lock);
851 UFS_WAPBL_END(pvp->v_mount);
852 ffs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
853 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
854 return (ENOSPC);
855 }
856
857 /*
858 * Find a cylinder group in which to place a directory.
859 *
860 * The policy implemented by this algorithm is to allocate a
861 * directory inode in the same cylinder group as its parent
862 * directory, but also to reserve space for its files inodes
863 * and data. Restrict the number of directories which may be
864 * allocated one after another in the same cylinder group
865 * without intervening allocation of files.
866 *
867 * If we allocate a first level directory then force allocation
868 * in another cylinder group.
869 */
870 static ino_t
871 ffs_dirpref(struct inode *pip)
872 {
873 register struct fs *fs;
874 int cg, prefcg;
875 int64_t dirsize, cgsize, curdsz;
876 int avgifree, avgbfree, avgndir;
877 int minifree, minbfree, maxndir;
878 int mincg, minndir;
879 int maxcontigdirs;
880
881 KASSERT(mutex_owned(&pip->i_ump->um_lock));
882
883 fs = pip->i_fs;
884
885 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
886 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
887 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
888
889 /*
890 * Force allocation in another cg if creating a first level dir.
891 */
892 if (ITOV(pip)->v_vflag & VV_ROOT) {
893 prefcg = random() % fs->fs_ncg;
894 mincg = prefcg;
895 minndir = fs->fs_ipg;
896 for (cg = prefcg; cg < fs->fs_ncg; cg++)
897 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
898 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
899 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
900 mincg = cg;
901 minndir = fs->fs_cs(fs, cg).cs_ndir;
902 }
903 for (cg = 0; cg < prefcg; cg++)
904 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
905 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
906 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
907 mincg = cg;
908 minndir = fs->fs_cs(fs, cg).cs_ndir;
909 }
910 return ((ino_t)(fs->fs_ipg * mincg));
911 }
912
913 /*
914 * Count various limits which used for
915 * optimal allocation of a directory inode.
916 * Try cylinder groups with >75% avgifree and avgbfree.
917 * Avoid cylinder groups with no free blocks or inodes as that
918 * triggers an I/O-expensive cylinder group scan.
919 */
920 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
921 minifree = avgifree - avgifree / 4;
922 if (minifree < 1)
923 minifree = 1;
924 minbfree = avgbfree - avgbfree / 4;
925 if (minbfree < 1)
926 minbfree = 1;
927 cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
928 dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
929 if (avgndir != 0) {
930 curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
931 if (dirsize < curdsz)
932 dirsize = curdsz;
933 }
934 if (cgsize < dirsize * 255)
935 maxcontigdirs = (avgbfree * fs->fs_bsize) / dirsize;
936 else
937 maxcontigdirs = 255;
938 if (fs->fs_avgfpdir > 0)
939 maxcontigdirs = min(maxcontigdirs,
940 fs->fs_ipg / fs->fs_avgfpdir);
941 if (maxcontigdirs == 0)
942 maxcontigdirs = 1;
943
944 /*
945 * Limit number of dirs in one cg and reserve space for
946 * regular files, but only if we have no deficit in
947 * inodes or space.
948 */
949 prefcg = ino_to_cg(fs, pip->i_number);
950 for (cg = prefcg; cg < fs->fs_ncg; cg++)
951 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
952 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
953 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
954 if (fs->fs_contigdirs[cg] < maxcontigdirs)
955 return ((ino_t)(fs->fs_ipg * cg));
956 }
957 for (cg = 0; cg < prefcg; cg++)
958 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
959 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
960 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
961 if (fs->fs_contigdirs[cg] < maxcontigdirs)
962 return ((ino_t)(fs->fs_ipg * cg));
963 }
964 /*
965 * This is a backstop when we are deficient in space.
966 */
967 for (cg = prefcg; cg < fs->fs_ncg; cg++)
968 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
969 return ((ino_t)(fs->fs_ipg * cg));
970 for (cg = 0; cg < prefcg; cg++)
971 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
972 break;
973 return ((ino_t)(fs->fs_ipg * cg));
974 }
975
976 /*
977 * Select the desired position for the next block in a file. The file is
978 * logically divided into sections. The first section is composed of the
979 * direct blocks. Each additional section contains fs_maxbpg blocks.
980 *
981 * If no blocks have been allocated in the first section, the policy is to
982 * request a block in the same cylinder group as the inode that describes
983 * the file. If no blocks have been allocated in any other section, the
984 * policy is to place the section in a cylinder group with a greater than
985 * average number of free blocks. An appropriate cylinder group is found
986 * by using a rotor that sweeps the cylinder groups. When a new group of
987 * blocks is needed, the sweep begins in the cylinder group following the
988 * cylinder group from which the previous allocation was made. The sweep
989 * continues until a cylinder group with greater than the average number
990 * of free blocks is found. If the allocation is for the first block in an
991 * indirect block, the information on the previous allocation is unavailable;
992 * here a best guess is made based upon the logical block number being
993 * allocated.
994 *
995 * If a section is already partially allocated, the policy is to
996 * contiguously allocate fs_maxcontig blocks. The end of one of these
997 * contiguous blocks and the beginning of the next is laid out
998 * contigously if possible.
999 *
1000 * => um_lock held on entry and exit
1001 */
1002 daddr_t
1003 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags,
1004 int32_t *bap /* XXX ondisk32 */)
1005 {
1006 struct fs *fs;
1007 int cg;
1008 int avgbfree, startcg;
1009
1010 KASSERT(mutex_owned(&ip->i_ump->um_lock));
1011
1012 fs = ip->i_fs;
1013
1014 /*
1015 * If allocating a contiguous file with B_CONTIG, use the hints
1016 * in the inode extentions to return the desired block.
1017 *
1018 * For metadata (indirect blocks) return the address of where
1019 * the first indirect block resides - we'll scan for the next
1020 * available slot if we need to allocate more than one indirect
1021 * block. For data, return the address of the actual block
1022 * relative to the address of the first data block.
1023 */
1024 if (flags & B_CONTIG) {
1025 KASSERT(ip->i_ffs_first_data_blk != 0);
1026 KASSERT(ip->i_ffs_first_indir_blk != 0);
1027 if (flags & B_METAONLY)
1028 return ip->i_ffs_first_indir_blk;
1029 else
1030 return ip->i_ffs_first_data_blk + blkstofrags(fs, lbn);
1031 }
1032
1033 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1034 if (lbn < NDADDR + NINDIR(fs)) {
1035 cg = ino_to_cg(fs, ip->i_number);
1036 return (cgbase(fs, cg) + fs->fs_frag);
1037 }
1038 /*
1039 * Find a cylinder with greater than average number of
1040 * unused data blocks.
1041 */
1042 if (indx == 0 || bap[indx - 1] == 0)
1043 startcg =
1044 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1045 else
1046 startcg = dtog(fs,
1047 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
1048 startcg %= fs->fs_ncg;
1049 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1050 for (cg = startcg; cg < fs->fs_ncg; cg++)
1051 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1052 return (cgbase(fs, cg) + fs->fs_frag);
1053 }
1054 for (cg = 0; cg < startcg; cg++)
1055 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1056 return (cgbase(fs, cg) + fs->fs_frag);
1057 }
1058 return (0);
1059 }
1060 /*
1061 * We just always try to lay things out contiguously.
1062 */
1063 return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
1064 }
1065
1066 daddr_t
1067 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags,
1068 int64_t *bap)
1069 {
1070 struct fs *fs;
1071 int cg;
1072 int avgbfree, startcg;
1073
1074 KASSERT(mutex_owned(&ip->i_ump->um_lock));
1075
1076 fs = ip->i_fs;
1077
1078 /*
1079 * If allocating a contiguous file with B_CONTIG, use the hints
1080 * in the inode extentions to return the desired block.
1081 *
1082 * For metadata (indirect blocks) return the address of where
1083 * the first indirect block resides - we'll scan for the next
1084 * available slot if we need to allocate more than one indirect
1085 * block. For data, return the address of the actual block
1086 * relative to the address of the first data block.
1087 */
1088 if (flags & B_CONTIG) {
1089 KASSERT(ip->i_ffs_first_data_blk != 0);
1090 KASSERT(ip->i_ffs_first_indir_blk != 0);
1091 if (flags & B_METAONLY)
1092 return ip->i_ffs_first_indir_blk;
1093 else
1094 return ip->i_ffs_first_data_blk + blkstofrags(fs, lbn);
1095 }
1096
1097 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1098 if (lbn < NDADDR + NINDIR(fs)) {
1099 cg = ino_to_cg(fs, ip->i_number);
1100 return (cgbase(fs, cg) + fs->fs_frag);
1101 }
1102 /*
1103 * Find a cylinder with greater than average number of
1104 * unused data blocks.
1105 */
1106 if (indx == 0 || bap[indx - 1] == 0)
1107 startcg =
1108 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1109 else
1110 startcg = dtog(fs,
1111 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
1112 startcg %= fs->fs_ncg;
1113 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1114 for (cg = startcg; cg < fs->fs_ncg; cg++)
1115 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1116 return (cgbase(fs, cg) + fs->fs_frag);
1117 }
1118 for (cg = 0; cg < startcg; cg++)
1119 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1120 return (cgbase(fs, cg) + fs->fs_frag);
1121 }
1122 return (0);
1123 }
1124 /*
1125 * We just always try to lay things out contiguously.
1126 */
1127 return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
1128 }
1129
1130
1131 /*
1132 * Implement the cylinder overflow algorithm.
1133 *
1134 * The policy implemented by this algorithm is:
1135 * 1) allocate the block in its requested cylinder group.
1136 * 2) quadradically rehash on the cylinder group number.
1137 * 3) brute force search for a free block.
1138 *
1139 * => called with um_lock held
1140 * => returns with um_lock released on success, held on failure
1141 * (*allocator releases lock on success, retains lock on failure)
1142 */
1143 /*VARARGS5*/
1144 static daddr_t
1145 ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
1146 int size /* size for data blocks, mode for inodes */,
1147 int flags, daddr_t (*allocator)(struct inode *, int, daddr_t, int, int))
1148 {
1149 struct fs *fs;
1150 daddr_t result;
1151 int i, icg = cg;
1152
1153 fs = ip->i_fs;
1154 /*
1155 * 1: preferred cylinder group
1156 */
1157 result = (*allocator)(ip, cg, pref, size, flags);
1158 if (result)
1159 return (result);
1160
1161 if (flags & B_CONTIG)
1162 return (result);
1163 /*
1164 * 2: quadratic rehash
1165 */
1166 for (i = 1; i < fs->fs_ncg; i *= 2) {
1167 cg += i;
1168 if (cg >= fs->fs_ncg)
1169 cg -= fs->fs_ncg;
1170 result = (*allocator)(ip, cg, 0, size, flags);
1171 if (result)
1172 return (result);
1173 }
1174 /*
1175 * 3: brute force search
1176 * Note that we start at i == 2, since 0 was checked initially,
1177 * and 1 is always checked in the quadratic rehash.
1178 */
1179 cg = (icg + 2) % fs->fs_ncg;
1180 for (i = 2; i < fs->fs_ncg; i++) {
1181 result = (*allocator)(ip, cg, 0, size, flags);
1182 if (result)
1183 return (result);
1184 cg++;
1185 if (cg == fs->fs_ncg)
1186 cg = 0;
1187 }
1188 return (0);
1189 }
1190
1191 /*
1192 * Determine whether a fragment can be extended.
1193 *
1194 * Check to see if the necessary fragments are available, and
1195 * if they are, allocate them.
1196 *
1197 * => called with um_lock held
1198 * => returns with um_lock released on success, held on failure
1199 */
1200 static daddr_t
1201 ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
1202 {
1203 struct ufsmount *ump;
1204 struct fs *fs;
1205 struct cg *cgp;
1206 struct buf *bp;
1207 daddr_t bno;
1208 int frags, bbase;
1209 int i, error;
1210 u_int8_t *blksfree;
1211
1212 fs = ip->i_fs;
1213 ump = ip->i_ump;
1214
1215 KASSERT(mutex_owned(&ump->um_lock));
1216
1217 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1218 return (0);
1219 frags = numfrags(fs, nsize);
1220 bbase = fragnum(fs, bprev);
1221 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1222 /* cannot extend across a block boundary */
1223 return (0);
1224 }
1225 mutex_exit(&ump->um_lock);
1226 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1227 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1228 if (error)
1229 goto fail;
1230 cgp = (struct cg *)bp->b_data;
1231 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1232 goto fail;
1233 cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs));
1234 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1235 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1236 cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs));
1237 bno = dtogd(fs, bprev);
1238 blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
1239 for (i = numfrags(fs, osize); i < frags; i++)
1240 if (isclr(blksfree, bno + i))
1241 goto fail;
1242 /*
1243 * the current fragment can be extended
1244 * deduct the count on fragment being extended into
1245 * increase the count on the remaining fragment (if any)
1246 * allocate the extended piece
1247 */
1248 for (i = frags; i < fs->fs_frag - bbase; i++)
1249 if (isclr(blksfree, bno + i))
1250 break;
1251 ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1252 if (i != frags)
1253 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1254 mutex_enter(&ump->um_lock);
1255 for (i = numfrags(fs, osize); i < frags; i++) {
1256 clrbit(blksfree, bno + i);
1257 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1258 fs->fs_cstotal.cs_nffree--;
1259 fs->fs_cs(fs, cg).cs_nffree--;
1260 }
1261 fs->fs_fmod = 1;
1262 ACTIVECG_CLR(fs, cg);
1263 mutex_exit(&ump->um_lock);
1264 if (DOINGSOFTDEP(ITOV(ip)))
1265 softdep_setup_blkmapdep(bp, fs, bprev);
1266 bdwrite(bp);
1267 return (bprev);
1268
1269 fail:
1270 brelse(bp, 0);
1271 mutex_enter(&ump->um_lock);
1272 return (0);
1273 }
1274
1275 /*
1276 * Determine whether a block can be allocated.
1277 *
1278 * Check to see if a block of the appropriate size is available,
1279 * and if it is, allocate it.
1280 */
1281 static daddr_t
1282 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int flags)
1283 {
1284 struct ufsmount *ump;
1285 struct fs *fs = ip->i_fs;
1286 struct cg *cgp;
1287 struct buf *bp;
1288 int32_t bno;
1289 daddr_t blkno;
1290 int error, frags, allocsiz, i;
1291 u_int8_t *blksfree;
1292 #ifdef FFS_EI
1293 const int needswap = UFS_FSNEEDSWAP(fs);
1294 #endif
1295
1296 ump = ip->i_ump;
1297
1298 KASSERT(mutex_owned(&ump->um_lock));
1299
1300 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1301 return (0);
1302 mutex_exit(&ump->um_lock);
1303 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1304 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1305 if (error)
1306 goto fail;
1307 cgp = (struct cg *)bp->b_data;
1308 if (!cg_chkmagic(cgp, needswap) ||
1309 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1310 goto fail;
1311 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1312 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1313 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1314 cgp->cg_time = ufs_rw64(time_second, needswap);
1315 if (size == fs->fs_bsize) {
1316 mutex_enter(&ump->um_lock);
1317 blkno = ffs_alloccgblk(ip, bp, bpref, flags);
1318 ACTIVECG_CLR(fs, cg);
1319 mutex_exit(&ump->um_lock);
1320 bdwrite(bp);
1321 return (blkno);
1322 }
1323 /*
1324 * check to see if any fragments are already available
1325 * allocsiz is the size which will be allocated, hacking
1326 * it down to a smaller size if necessary
1327 */
1328 blksfree = cg_blksfree(cgp, needswap);
1329 frags = numfrags(fs, size);
1330 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1331 if (cgp->cg_frsum[allocsiz] != 0)
1332 break;
1333 if (allocsiz == fs->fs_frag) {
1334 /*
1335 * no fragments were available, so a block will be
1336 * allocated, and hacked up
1337 */
1338 if (cgp->cg_cs.cs_nbfree == 0)
1339 goto fail;
1340 mutex_enter(&ump->um_lock);
1341 blkno = ffs_alloccgblk(ip, bp, bpref, flags);
1342 bno = dtogd(fs, blkno);
1343 for (i = frags; i < fs->fs_frag; i++)
1344 setbit(blksfree, bno + i);
1345 i = fs->fs_frag - frags;
1346 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1347 fs->fs_cstotal.cs_nffree += i;
1348 fs->fs_cs(fs, cg).cs_nffree += i;
1349 fs->fs_fmod = 1;
1350 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1351 ACTIVECG_CLR(fs, cg);
1352 mutex_exit(&ump->um_lock);
1353 bdwrite(bp);
1354 return (blkno);
1355 }
1356 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1357 #if 0
1358 /*
1359 * XXX fvdl mapsearch will panic, and never return -1
1360 * also: returning NULL as daddr_t ?
1361 */
1362 if (bno < 0)
1363 goto fail;
1364 #endif
1365 for (i = 0; i < frags; i++)
1366 clrbit(blksfree, bno + i);
1367 mutex_enter(&ump->um_lock);
1368 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1369 fs->fs_cstotal.cs_nffree -= frags;
1370 fs->fs_cs(fs, cg).cs_nffree -= frags;
1371 fs->fs_fmod = 1;
1372 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1373 if (frags != allocsiz)
1374 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1375 blkno = cgbase(fs, cg) + bno;
1376 ACTIVECG_CLR(fs, cg);
1377 mutex_exit(&ump->um_lock);
1378 if (DOINGSOFTDEP(ITOV(ip)))
1379 softdep_setup_blkmapdep(bp, fs, blkno);
1380 bdwrite(bp);
1381 return blkno;
1382
1383 fail:
1384 brelse(bp, 0);
1385 mutex_enter(&ump->um_lock);
1386 return (0);
1387 }
1388
1389 /*
1390 * Allocate a block in a cylinder group.
1391 *
1392 * This algorithm implements the following policy:
1393 * 1) allocate the requested block.
1394 * 2) allocate a rotationally optimal block in the same cylinder.
1395 * 3) allocate the next available block on the block rotor for the
1396 * specified cylinder group.
1397 * Note that this routine only allocates fs_bsize blocks; these
1398 * blocks may be fragmented by the routine that allocates them.
1399 */
1400 static daddr_t
1401 ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int flags)
1402 {
1403 struct ufsmount *ump;
1404 struct fs *fs = ip->i_fs;
1405 struct cg *cgp;
1406 daddr_t blkno;
1407 int32_t bno;
1408 u_int8_t *blksfree;
1409 #ifdef FFS_EI
1410 const int needswap = UFS_FSNEEDSWAP(fs);
1411 #endif
1412
1413 ump = ip->i_ump;
1414
1415 KASSERT(mutex_owned(&ump->um_lock));
1416
1417 cgp = (struct cg *)bp->b_data;
1418 blksfree = cg_blksfree(cgp, needswap);
1419 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1420 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1421 } else {
1422 bpref = blknum(fs, bpref);
1423 bno = dtogd(fs, bpref);
1424 /*
1425 * if the requested block is available, use it
1426 */
1427 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1428 goto gotit;
1429 /*
1430 * if the requested data block isn't available and we are
1431 * trying to allocate a contiguous file, return an error.
1432 */
1433 if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG)
1434 return (0);
1435 }
1436
1437 /*
1438 * Take the next available block in this cylinder group.
1439 */
1440 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1441 if (bno < 0)
1442 return (0);
1443 cgp->cg_rotor = ufs_rw32(bno, needswap);
1444 gotit:
1445 blkno = fragstoblks(fs, bno);
1446 ffs_clrblock(fs, blksfree, blkno);
1447 ffs_clusteracct(fs, cgp, blkno, -1);
1448 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1449 fs->fs_cstotal.cs_nbfree--;
1450 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1451 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1452 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1453 int cylno;
1454 cylno = old_cbtocylno(fs, bno);
1455 KASSERT(cylno >= 0);
1456 KASSERT(cylno < fs->fs_old_ncyl);
1457 KASSERT(old_cbtorpos(fs, bno) >= 0);
1458 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
1459 ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
1460 needswap);
1461 ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
1462 }
1463 fs->fs_fmod = 1;
1464 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
1465 if (DOINGSOFTDEP(ITOV(ip))) {
1466 mutex_exit(&ump->um_lock);
1467 softdep_setup_blkmapdep(bp, fs, blkno);
1468 mutex_enter(&ump->um_lock);
1469 }
1470 return (blkno);
1471 }
1472
1473 #ifdef XXXUBC
1474 /*
1475 * Determine whether a cluster can be allocated.
1476 *
1477 * We do not currently check for optimal rotational layout if there
1478 * are multiple choices in the same cylinder group. Instead we just
1479 * take the first one that we find following bpref.
1480 */
1481
1482 /*
1483 * This function must be fixed for UFS2 if re-enabled.
1484 */
1485 static daddr_t
1486 ffs_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
1487 {
1488 struct ufsmount *ump;
1489 struct fs *fs;
1490 struct cg *cgp;
1491 struct buf *bp;
1492 int i, got, run, bno, bit, map;
1493 u_char *mapp;
1494 int32_t *lp;
1495
1496 fs = ip->i_fs;
1497 ump = ip->i_ump;
1498
1499 KASSERT(mutex_owned(&ump->um_lock));
1500 if (fs->fs_maxcluster[cg] < len)
1501 return (0);
1502 mutex_exit(&ump->um_lock);
1503 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1504 NOCRED, 0, &bp))
1505 goto fail;
1506 cgp = (struct cg *)bp->b_data;
1507 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1508 goto fail;
1509 /*
1510 * Check to see if a cluster of the needed size (or bigger) is
1511 * available in this cylinder group.
1512 */
1513 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1514 for (i = len; i <= fs->fs_contigsumsize; i++)
1515 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1516 break;
1517 if (i > fs->fs_contigsumsize) {
1518 /*
1519 * This is the first time looking for a cluster in this
1520 * cylinder group. Update the cluster summary information
1521 * to reflect the true maximum sized cluster so that
1522 * future cluster allocation requests can avoid reading
1523 * the cylinder group map only to find no clusters.
1524 */
1525 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1526 for (i = len - 1; i > 0; i--)
1527 if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1528 break;
1529 mutex_enter(&ump->um_lock);
1530 fs->fs_maxcluster[cg] = i;
1531 mutex_exit(&ump->um_lock);
1532 goto fail;
1533 }
1534 /*
1535 * Search the cluster map to find a big enough cluster.
1536 * We take the first one that we find, even if it is larger
1537 * than we need as we prefer to get one close to the previous
1538 * block allocation. We do not search before the current
1539 * preference point as we do not want to allocate a block
1540 * that is allocated before the previous one (as we will
1541 * then have to wait for another pass of the elevator
1542 * algorithm before it will be read). We prefer to fail and
1543 * be recalled to try an allocation in the next cylinder group.
1544 */
1545 if (dtog(fs, bpref) != cg)
1546 bpref = 0;
1547 else
1548 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1549 mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1550 map = *mapp++;
1551 bit = 1 << (bpref % NBBY);
1552 for (run = 0, got = bpref;
1553 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1554 if ((map & bit) == 0) {
1555 run = 0;
1556 } else {
1557 run++;
1558 if (run == len)
1559 break;
1560 }
1561 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1562 bit <<= 1;
1563 } else {
1564 map = *mapp++;
1565 bit = 1;
1566 }
1567 }
1568 if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1569 goto fail;
1570 /*
1571 * Allocate the cluster that we have found.
1572 */
1573 #ifdef DIAGNOSTIC
1574 for (i = 1; i <= len; i++)
1575 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1576 got - run + i))
1577 panic("ffs_clusteralloc: map mismatch");
1578 #endif
1579 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1580 if (dtog(fs, bno) != cg)
1581 panic("ffs_clusteralloc: allocated out of group");
1582 len = blkstofrags(fs, len);
1583 mutex_enter(&ump->um_lock);
1584 for (i = 0; i < len; i += fs->fs_frag)
1585 if ((got = ffs_alloccgblk(ip, bp, bno + i, flags)) != bno + i)
1586 panic("ffs_clusteralloc: lost block");
1587 ACTIVECG_CLR(fs, cg);
1588 mutex_exit(&ump->um_lock);
1589 bdwrite(bp);
1590 return (bno);
1591
1592 fail:
1593 brelse(bp, 0);
1594 mutex_enter(&ump->um_lock);
1595 return (0);
1596 }
1597 #endif /* XXXUBC */
1598
1599 /*
1600 * Determine whether an inode can be allocated.
1601 *
1602 * Check to see if an inode is available, and if it is,
1603 * allocate it using the following policy:
1604 * 1) allocate the requested inode.
1605 * 2) allocate the next available inode after the requested
1606 * inode in the specified cylinder group.
1607 */
1608 static daddr_t
1609 ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int flags)
1610 {
1611 struct ufsmount *ump = ip->i_ump;
1612 struct fs *fs = ip->i_fs;
1613 struct cg *cgp;
1614 struct buf *bp, *ibp;
1615 u_int8_t *inosused;
1616 int error, start, len, loc, map, i;
1617 int32_t initediblk;
1618 daddr_t nalloc;
1619 struct ufs2_dinode *dp2;
1620 #ifdef FFS_EI
1621 const int needswap = UFS_FSNEEDSWAP(fs);
1622 #endif
1623
1624 KASSERT(mutex_owned(&ump->um_lock));
1625 UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
1626
1627 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1628 return (0);
1629 mutex_exit(&ump->um_lock);
1630 ibp = NULL;
1631 initediblk = -1;
1632 retry:
1633 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1634 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1635 if (error)
1636 goto fail;
1637 cgp = (struct cg *)bp->b_data;
1638 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0)
1639 goto fail;
1640
1641 if (ibp != NULL &&
1642 initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) {
1643 /* Another thread allocated more inodes so we retry the test. */
1644 brelse(ibp, 0);
1645 ibp = NULL;
1646 }
1647 /*
1648 * Check to see if we need to initialize more inodes.
1649 */
1650 if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) {
1651 initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
1652 nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap);
1653 if (nalloc + INOPB(fs) > initediblk &&
1654 initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
1655 /*
1656 * We have to release the cg buffer here to prevent
1657 * a deadlock when reading the inode block will
1658 * run a copy-on-write that might use this cg.
1659 */
1660 brelse(bp, 0);
1661 bp = NULL;
1662 error = ffs_getblk(ip->i_devvp, fsbtodb(fs,
1663 ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
1664 FFS_NOBLK, fs->fs_bsize, false, &ibp);
1665 if (error)
1666 goto fail;
1667 goto retry;
1668 }
1669 }
1670
1671 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1672 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1673 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1674 cgp->cg_time = ufs_rw64(time_second, needswap);
1675 inosused = cg_inosused(cgp, needswap);
1676 if (ipref) {
1677 ipref %= fs->fs_ipg;
1678 if (isclr(inosused, ipref))
1679 goto gotit;
1680 }
1681 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1682 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1683 NBBY);
1684 loc = skpc(0xff, len, &inosused[start]);
1685 if (loc == 0) {
1686 len = start + 1;
1687 start = 0;
1688 loc = skpc(0xff, len, &inosused[0]);
1689 if (loc == 0) {
1690 printf("cg = %d, irotor = %d, fs = %s\n",
1691 cg, ufs_rw32(cgp->cg_irotor, needswap),
1692 fs->fs_fsmnt);
1693 panic("ffs_nodealloccg: map corrupted");
1694 /* NOTREACHED */
1695 }
1696 }
1697 i = start + len - loc;
1698 map = inosused[i];
1699 ipref = i * NBBY;
1700 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1701 if ((map & i) == 0) {
1702 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1703 goto gotit;
1704 }
1705 }
1706 printf("fs = %s\n", fs->fs_fsmnt);
1707 panic("ffs_nodealloccg: block not in map");
1708 /* NOTREACHED */
1709 gotit:
1710 UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp, cg * fs->fs_ipg + ipref,
1711 mode);
1712 /*
1713 * Check to see if we need to initialize more inodes.
1714 */
1715 if (ibp != NULL) {
1716 KASSERT(initediblk == ufs_rw32(cgp->cg_initediblk, needswap));
1717 memset(ibp->b_data, 0, fs->fs_bsize);
1718 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1719 for (i = 0; i < INOPB(fs); i++) {
1720 /*
1721 * Don't bother to swap, it's supposed to be
1722 * random, after all.
1723 */
1724 dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1;
1725 dp2++;
1726 }
1727 initediblk += INOPB(fs);
1728 cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
1729 }
1730
1731 mutex_enter(&ump->um_lock);
1732 ACTIVECG_CLR(fs, cg);
1733 setbit(inosused, ipref);
1734 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1735 fs->fs_cstotal.cs_nifree--;
1736 fs->fs_cs(fs, cg).cs_nifree--;
1737 fs->fs_fmod = 1;
1738 if ((mode & IFMT) == IFDIR) {
1739 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1740 fs->fs_cstotal.cs_ndir++;
1741 fs->fs_cs(fs, cg).cs_ndir++;
1742 }
1743 mutex_exit(&ump->um_lock);
1744 if (DOINGSOFTDEP(ITOV(ip)))
1745 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1746 if (ibp != NULL) {
1747 bwrite(bp);
1748 bawrite(ibp);
1749 } else
1750 bdwrite(bp);
1751 return (cg * fs->fs_ipg + ipref);
1752 fail:
1753 if (bp != NULL)
1754 brelse(bp, 0);
1755 if (ibp != NULL)
1756 brelse(ibp, 0);
1757 mutex_enter(&ump->um_lock);
1758 return (0);
1759 }
1760
1761 /*
1762 * Allocate a block or fragment.
1763 *
1764 * The specified block or fragment is removed from the
1765 * free map, possibly fragmenting a block in the process.
1766 *
1767 * This implementation should mirror fs_blkfree
1768 *
1769 * => um_lock not held on entry or exit
1770 */
1771 int
1772 ffs_blkalloc(struct inode *ip, daddr_t bno, long size)
1773 {
1774 struct ufsmount *ump = ip->i_ump;
1775 struct fs *fs = ip->i_fs;
1776 struct cg *cgp;
1777 struct buf *bp;
1778 int32_t fragno, cgbno;
1779 int i, error, cg, blk, frags, bbase;
1780 u_int8_t *blksfree;
1781 const int needswap = UFS_FSNEEDSWAP(fs);
1782
1783 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1784 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1785 printf("dev = 0x%x, bno = %" PRId64 " bsize = %d, "
1786 "size = %ld, fs = %s\n",
1787 ip->i_dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1788 panic("blkalloc: bad size");
1789 }
1790 cg = dtog(fs, bno);
1791 if (bno >= fs->fs_size) {
1792 printf("bad block %" PRId64 ", ino %" PRId64 "\n", bno,
1793 ip->i_number);
1794 ffs_fserr(fs, ip->i_uid, "bad block");
1795 return EINVAL;
1796 }
1797 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1798 (int)fs->fs_cgsize, NOCRED, B_MODIFY, &bp);
1799 if (error) {
1800 brelse(bp, 0);
1801 return error;
1802 }
1803 cgp = (struct cg *)bp->b_data;
1804 if (!cg_chkmagic(cgp, needswap)) {
1805 brelse(bp, 0);
1806 return EIO;
1807 }
1808 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1809 cgp->cg_time = ufs_rw64(time_second, needswap);
1810 cgbno = dtogd(fs, bno);
1811 blksfree = cg_blksfree(cgp, needswap);
1812
1813 mutex_enter(&ump->um_lock);
1814 if (size == fs->fs_bsize) {
1815 fragno = fragstoblks(fs, cgbno);
1816 if (!ffs_isblock(fs, blksfree, fragno)) {
1817 mutex_exit(&ump->um_lock);
1818 brelse(bp, 0);
1819 return EBUSY;
1820 }
1821 ffs_clrblock(fs, blksfree, fragno);
1822 ffs_clusteracct(fs, cgp, fragno, -1);
1823 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1824 fs->fs_cstotal.cs_nbfree--;
1825 fs->fs_cs(fs, cg).cs_nbfree--;
1826 } else {
1827 bbase = cgbno - fragnum(fs, cgbno);
1828
1829 frags = numfrags(fs, size);
1830 for (i = 0; i < frags; i++) {
1831 if (isclr(blksfree, cgbno + i)) {
1832 mutex_exit(&ump->um_lock);
1833 brelse(bp, 0);
1834 return EBUSY;
1835 }
1836 }
1837 /*
1838 * if a complete block is being split, account for it
1839 */
1840 fragno = fragstoblks(fs, bbase);
1841 if (ffs_isblock(fs, blksfree, fragno)) {
1842 ufs_add32(cgp->cg_cs.cs_nffree, fs->fs_frag, needswap);
1843 fs->fs_cstotal.cs_nffree += fs->fs_frag;
1844 fs->fs_cs(fs, cg).cs_nffree += fs->fs_frag;
1845 ffs_clusteracct(fs, cgp, fragno, -1);
1846 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1847 fs->fs_cstotal.cs_nbfree--;
1848 fs->fs_cs(fs, cg).cs_nbfree--;
1849 }
1850 /*
1851 * decrement the counts associated with the old frags
1852 */
1853 blk = blkmap(fs, blksfree, bbase);
1854 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1855 /*
1856 * allocate the fragment
1857 */
1858 for (i = 0; i < frags; i++) {
1859 clrbit(blksfree, cgbno + i);
1860 }
1861 ufs_add32(cgp->cg_cs.cs_nffree, -i, needswap);
1862 fs->fs_cstotal.cs_nffree -= i;
1863 fs->fs_cs(fs, cg).cs_nffree -= i;
1864 /*
1865 * add back in counts associated with the new frags
1866 */
1867 blk = blkmap(fs, blksfree, bbase);
1868 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1869 }
1870 fs->fs_fmod = 1;
1871 ACTIVECG_CLR(fs, cg);
1872 mutex_exit(&ump->um_lock);
1873 bdwrite(bp);
1874 return 0;
1875 }
1876
1877 /*
1878 * Free a block or fragment.
1879 *
1880 * The specified block or fragment is placed back in the
1881 * free map. If a fragment is deallocated, a possible
1882 * block reassembly is checked.
1883 *
1884 * => um_lock not held on entry or exit
1885 */
1886 void
1887 ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
1888 ino_t inum)
1889 {
1890 struct cg *cgp;
1891 struct buf *bp;
1892 struct ufsmount *ump;
1893 int32_t fragno, cgbno;
1894 daddr_t cgblkno;
1895 int i, error, cg, blk, frags, bbase;
1896 u_int8_t *blksfree;
1897 dev_t dev;
1898 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
1899 const int needswap = UFS_FSNEEDSWAP(fs);
1900
1901 cg = dtog(fs, bno);
1902 if (devvp_is_snapshot) {
1903 dev = VTOI(devvp)->i_devvp->v_rdev;
1904 ump = VFSTOUFS(devvp->v_mount);
1905 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1906 } else {
1907 dev = devvp->v_rdev;
1908 ump = VFSTOUFS(devvp->v_specmountpoint);
1909 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1910 if (ffs_snapblkfree(fs, devvp, bno, size, inum))
1911 return;
1912 }
1913 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1914 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1915 printf("dev = 0x%x, bno = %" PRId64 " bsize = %d, "
1916 "size = %ld, fs = %s\n",
1917 dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1918 panic("blkfree: bad size");
1919 }
1920
1921 if (bno >= fs->fs_size) {
1922 printf("bad block %" PRId64 ", ino %llu\n", bno,
1923 (unsigned long long)inum);
1924 ffs_fserr(fs, inum, "bad block");
1925 return;
1926 }
1927 error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
1928 NOCRED, B_MODIFY, &bp);
1929 if (error) {
1930 brelse(bp, 0);
1931 return;
1932 }
1933 cgp = (struct cg *)bp->b_data;
1934 if (!cg_chkmagic(cgp, needswap)) {
1935 brelse(bp, 0);
1936 return;
1937 }
1938 cgp->cg_old_time = ufs_rw32(time_second, needswap);
1939 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
1940 (fs->fs_old_flags & FS_FLAGS_UPDATED))
1941 cgp->cg_time = ufs_rw64(time_second, needswap);
1942 cgbno = dtogd(fs, bno);
1943 blksfree = cg_blksfree(cgp, needswap);
1944 mutex_enter(&ump->um_lock);
1945 if (size == fs->fs_bsize) {
1946 fragno = fragstoblks(fs, cgbno);
1947 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1948 if (devvp_is_snapshot) {
1949 mutex_exit(&ump->um_lock);
1950 brelse(bp, 0);
1951 return;
1952 }
1953 printf("dev = 0x%x, block = %" PRId64 ", fs = %s\n",
1954 dev, bno, fs->fs_fsmnt);
1955 panic("blkfree: freeing free block");
1956 }
1957 ffs_setblock(fs, blksfree, fragno);
1958 ffs_clusteracct(fs, cgp, fragno, 1);
1959 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1960 fs->fs_cstotal.cs_nbfree++;
1961 fs->fs_cs(fs, cg).cs_nbfree++;
1962 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
1963 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
1964 i = old_cbtocylno(fs, cgbno);
1965 KASSERT(i >= 0);
1966 KASSERT(i < fs->fs_old_ncyl);
1967 KASSERT(old_cbtorpos(fs, cgbno) >= 0);
1968 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
1969 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
1970 needswap);
1971 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
1972 }
1973 } else {
1974 bbase = cgbno - fragnum(fs, cgbno);
1975 /*
1976 * decrement the counts associated with the old frags
1977 */
1978 blk = blkmap(fs, blksfree, bbase);
1979 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1980 /*
1981 * deallocate the fragment
1982 */
1983 frags = numfrags(fs, size);
1984 for (i = 0; i < frags; i++) {
1985 if (isset(blksfree, cgbno + i)) {
1986 printf("dev = 0x%x, block = %" PRId64
1987 ", fs = %s\n",
1988 dev, bno + i, fs->fs_fsmnt);
1989 panic("blkfree: freeing free frag");
1990 }
1991 setbit(blksfree, cgbno + i);
1992 }
1993 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1994 fs->fs_cstotal.cs_nffree += i;
1995 fs->fs_cs(fs, cg).cs_nffree += i;
1996 /*
1997 * add back in counts associated with the new frags
1998 */
1999 blk = blkmap(fs, blksfree, bbase);
2000 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
2001 /*
2002 * if a complete block has been reassembled, account for it
2003 */
2004 fragno = fragstoblks(fs, bbase);
2005 if (ffs_isblock(fs, blksfree, fragno)) {
2006 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
2007 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
2008 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
2009 ffs_clusteracct(fs, cgp, fragno, 1);
2010 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
2011 fs->fs_cstotal.cs_nbfree++;
2012 fs->fs_cs(fs, cg).cs_nbfree++;
2013 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
2014 ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
2015 i = old_cbtocylno(fs, bbase);
2016 KASSERT(i >= 0);
2017 KASSERT(i < fs->fs_old_ncyl);
2018 KASSERT(old_cbtorpos(fs, bbase) >= 0);
2019 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
2020 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
2021 bbase)], 1, needswap);
2022 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
2023 }
2024 }
2025 }
2026 fs->fs_fmod = 1;
2027 ACTIVECG_CLR(fs, cg);
2028 mutex_exit(&ump->um_lock);
2029 bdwrite(bp);
2030 }
2031
2032 #if defined(DIAGNOSTIC) || defined(DEBUG)
2033 #ifdef XXXUBC
2034 /*
2035 * Verify allocation of a block or fragment. Returns true if block or
2036 * fragment is allocated, false if it is free.
2037 */
2038 static int
2039 ffs_checkblk(struct inode *ip, daddr_t bno, long size)
2040 {
2041 struct fs *fs;
2042 struct cg *cgp;
2043 struct buf *bp;
2044 int i, error, frags, free;
2045
2046 fs = ip->i_fs;
2047 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
2048 printf("bsize = %d, size = %ld, fs = %s\n",
2049 fs->fs_bsize, size, fs->fs_fsmnt);
2050 panic("checkblk: bad size");
2051 }
2052 if (bno >= fs->fs_size)
2053 panic("checkblk: bad block %d", bno);
2054 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
2055 (int)fs->fs_cgsize, NOCRED, 0, &bp);
2056 if (error) {
2057 brelse(bp, 0);
2058 return 0;
2059 }
2060 cgp = (struct cg *)bp->b_data;
2061 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
2062 brelse(bp, 0);
2063 return 0;
2064 }
2065 bno = dtogd(fs, bno);
2066 if (size == fs->fs_bsize) {
2067 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
2068 fragstoblks(fs, bno));
2069 } else {
2070 frags = numfrags(fs, size);
2071 for (free = 0, i = 0; i < frags; i++)
2072 if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
2073 free++;
2074 if (free != 0 && free != frags)
2075 panic("checkblk: partially free fragment");
2076 }
2077 brelse(bp, 0);
2078 return (!free);
2079 }
2080 #endif /* XXXUBC */
2081 #endif /* DIAGNOSTIC */
2082
2083 /*
2084 * Free an inode.
2085 */
2086 int
2087 ffs_vfree(struct vnode *vp, ino_t ino, int mode)
2088 {
2089
2090 if (DOINGSOFTDEP(vp)) {
2091 softdep_freefile(vp, ino, mode);
2092 return (0);
2093 }
2094 return ffs_freefile(VTOI(vp)->i_fs, VTOI(vp)->i_devvp, ino, mode);
2095 }
2096
2097 /*
2098 * Do the actual free operation.
2099 * The specified inode is placed back in the free map.
2100 *
2101 * => um_lock not held on entry or exit
2102 */
2103 int
2104 ffs_freefile(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
2105 {
2106 struct ufsmount *ump;
2107 struct cg *cgp;
2108 struct buf *bp;
2109 int error, cg;
2110 daddr_t cgbno;
2111 u_int8_t *inosused;
2112 dev_t dev;
2113 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
2114 #ifdef FFS_EI
2115 const int needswap = UFS_FSNEEDSWAP(fs);
2116 #endif
2117
2118 if (!devvp_is_snapshot) {
2119 UFS_WAPBL_JLOCK_ASSERT(devvp->v_specinfo->si_mountpoint);
2120 }
2121
2122 cg = ino_to_cg(fs, ino);
2123 if (devvp_is_snapshot) {
2124 dev = VTOI(devvp)->i_devvp->v_rdev;
2125 ump = VFSTOUFS(devvp->v_mount);
2126 cgbno = fragstoblks(fs, cgtod(fs, cg));
2127 } else {
2128 dev = devvp->v_rdev;
2129 ump = VFSTOUFS(devvp->v_specmountpoint);
2130 cgbno = fsbtodb(fs, cgtod(fs, cg));
2131 }
2132 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2133 panic("ifree: range: dev = 0x%x, ino = %llu, fs = %s",
2134 dev, (unsigned long long)ino, fs->fs_fsmnt);
2135 error = bread(devvp, cgbno, (int)fs->fs_cgsize,
2136 NOCRED, B_MODIFY, &bp);
2137 if (error) {
2138 brelse(bp, 0);
2139 return (error);
2140 }
2141 cgp = (struct cg *)bp->b_data;
2142 if (!cg_chkmagic(cgp, needswap)) {
2143 brelse(bp, 0);
2144 return (0);
2145 }
2146 cgp->cg_old_time = ufs_rw32(time_second, needswap);
2147 if ((fs->fs_magic != FS_UFS1_MAGIC) ||
2148 (fs->fs_old_flags & FS_FLAGS_UPDATED))
2149 cgp->cg_time = ufs_rw64(time_second, needswap);
2150 inosused = cg_inosused(cgp, needswap);
2151 ino %= fs->fs_ipg;
2152 if (isclr(inosused, ino)) {
2153 printf("ifree: dev = 0x%x, ino = %llu, fs = %s\n",
2154 dev, (unsigned long long)ino + cg * fs->fs_ipg,
2155 fs->fs_fsmnt);
2156 if (fs->fs_ronly == 0)
2157 panic("ifree: freeing free inode");
2158 }
2159 clrbit(inosused, ino);
2160 if (!devvp_is_snapshot)
2161 UFS_WAPBL_UNREGISTER_INODE(devvp->v_specmountpoint,
2162 ino + cg * fs->fs_ipg, mode);
2163 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
2164 cgp->cg_irotor = ufs_rw32(ino, needswap);
2165 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
2166 mutex_enter(&ump->um_lock);
2167 fs->fs_cstotal.cs_nifree++;
2168 fs->fs_cs(fs, cg).cs_nifree++;
2169 if ((mode & IFMT) == IFDIR) {
2170 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
2171 fs->fs_cstotal.cs_ndir--;
2172 fs->fs_cs(fs, cg).cs_ndir--;
2173 }
2174 fs->fs_fmod = 1;
2175 ACTIVECG_CLR(fs, cg);
2176 mutex_exit(&ump->um_lock);
2177 bdwrite(bp);
2178 return (0);
2179 }
2180
2181 /*
2182 * Check to see if a file is free.
2183 */
2184 int
2185 ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
2186 {
2187 struct cg *cgp;
2188 struct buf *bp;
2189 daddr_t cgbno;
2190 int ret, cg;
2191 u_int8_t *inosused;
2192 const bool devvp_is_snapshot = (devvp->v_type != VBLK);
2193
2194 cg = ino_to_cg(fs, ino);
2195 if (devvp_is_snapshot)
2196 cgbno = fragstoblks(fs, cgtod(fs, cg));
2197 else
2198 cgbno = fsbtodb(fs, cgtod(fs, cg));
2199 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2200 return 1;
2201 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, 0, &bp)) {
2202 brelse(bp, 0);
2203 return 1;
2204 }
2205 cgp = (struct cg *)bp->b_data;
2206 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
2207 brelse(bp, 0);
2208 return 1;
2209 }
2210 inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
2211 ino %= fs->fs_ipg;
2212 ret = isclr(inosused, ino);
2213 brelse(bp, 0);
2214 return ret;
2215 }
2216
2217 /*
2218 * Find a block of the specified size in the specified cylinder group.
2219 *
2220 * It is a panic if a request is made to find a block if none are
2221 * available.
2222 */
2223 static int32_t
2224 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
2225 {
2226 int32_t bno;
2227 int start, len, loc, i;
2228 int blk, field, subfield, pos;
2229 int ostart, olen;
2230 u_int8_t *blksfree;
2231 #ifdef FFS_EI
2232 const int needswap = UFS_FSNEEDSWAP(fs);
2233 #endif
2234
2235 /* KASSERT(mutex_owned(&ump->um_lock)); */
2236
2237 /*
2238 * find the fragment by searching through the free block
2239 * map for an appropriate bit pattern
2240 */
2241 if (bpref)
2242 start = dtogd(fs, bpref) / NBBY;
2243 else
2244 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
2245 blksfree = cg_blksfree(cgp, needswap);
2246 len = howmany(fs->fs_fpg, NBBY) - start;
2247 ostart = start;
2248 olen = len;
2249 loc = scanc((u_int)len,
2250 (const u_char *)&blksfree[start],
2251 (const u_char *)fragtbl[fs->fs_frag],
2252 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2253 if (loc == 0) {
2254 len = start + 1;
2255 start = 0;
2256 loc = scanc((u_int)len,
2257 (const u_char *)&blksfree[0],
2258 (const u_char *)fragtbl[fs->fs_frag],
2259 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
2260 if (loc == 0) {
2261 printf("start = %d, len = %d, fs = %s\n",
2262 ostart, olen, fs->fs_fsmnt);
2263 printf("offset=%d %ld\n",
2264 ufs_rw32(cgp->cg_freeoff, needswap),
2265 (long)blksfree - (long)cgp);
2266 printf("cg %d\n", cgp->cg_cgx);
2267 panic("ffs_alloccg: map corrupted");
2268 /* NOTREACHED */
2269 }
2270 }
2271 bno = (start + len - loc) * NBBY;
2272 cgp->cg_frotor = ufs_rw32(bno, needswap);
2273 /*
2274 * found the byte in the map
2275 * sift through the bits to find the selected frag
2276 */
2277 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2278 blk = blkmap(fs, blksfree, bno);
2279 blk <<= 1;
2280 field = around[allocsiz];
2281 subfield = inside[allocsiz];
2282 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2283 if ((blk & field) == subfield)
2284 return (bno + pos);
2285 field <<= 1;
2286 subfield <<= 1;
2287 }
2288 }
2289 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
2290 panic("ffs_alloccg: block not in map");
2291 /* return (-1); */
2292 }
2293
2294 /*
2295 * Update the cluster map because of an allocation or free.
2296 *
2297 * Cnt == 1 means free; cnt == -1 means allocating.
2298 */
2299 void
2300 ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
2301 {
2302 int32_t *sump;
2303 int32_t *lp;
2304 u_char *freemapp, *mapp;
2305 int i, start, end, forw, back, map, bit;
2306 #ifdef FFS_EI
2307 const int needswap = UFS_FSNEEDSWAP(fs);
2308 #endif
2309
2310 /* KASSERT(mutex_owned(&ump->um_lock)); */
2311
2312 if (fs->fs_contigsumsize <= 0)
2313 return;
2314 freemapp = cg_clustersfree(cgp, needswap);
2315 sump = cg_clustersum(cgp, needswap);
2316 /*
2317 * Allocate or clear the actual block.
2318 */
2319 if (cnt > 0)
2320 setbit(freemapp, blkno);
2321 else
2322 clrbit(freemapp, blkno);
2323 /*
2324 * Find the size of the cluster going forward.
2325 */
2326 start = blkno + 1;
2327 end = start + fs->fs_contigsumsize;
2328 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
2329 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
2330 mapp = &freemapp[start / NBBY];
2331 map = *mapp++;
2332 bit = 1 << (start % NBBY);
2333 for (i = start; i < end; i++) {
2334 if ((map & bit) == 0)
2335 break;
2336 if ((i & (NBBY - 1)) != (NBBY - 1)) {
2337 bit <<= 1;
2338 } else {
2339 map = *mapp++;
2340 bit = 1;
2341 }
2342 }
2343 forw = i - start;
2344 /*
2345 * Find the size of the cluster going backward.
2346 */
2347 start = blkno - 1;
2348 end = start - fs->fs_contigsumsize;
2349 if (end < 0)
2350 end = -1;
2351 mapp = &freemapp[start / NBBY];
2352 map = *mapp--;
2353 bit = 1 << (start % NBBY);
2354 for (i = start; i > end; i--) {
2355 if ((map & bit) == 0)
2356 break;
2357 if ((i & (NBBY - 1)) != 0) {
2358 bit >>= 1;
2359 } else {
2360 map = *mapp--;
2361 bit = 1 << (NBBY - 1);
2362 }
2363 }
2364 back = start - i;
2365 /*
2366 * Account for old cluster and the possibly new forward and
2367 * back clusters.
2368 */
2369 i = back + forw + 1;
2370 if (i > fs->fs_contigsumsize)
2371 i = fs->fs_contigsumsize;
2372 ufs_add32(sump[i], cnt, needswap);
2373 if (back > 0)
2374 ufs_add32(sump[back], -cnt, needswap);
2375 if (forw > 0)
2376 ufs_add32(sump[forw], -cnt, needswap);
2377
2378 /*
2379 * Update cluster summary information.
2380 */
2381 lp = &sump[fs->fs_contigsumsize];
2382 for (i = fs->fs_contigsumsize; i > 0; i--)
2383 if (ufs_rw32(*lp--, needswap) > 0)
2384 break;
2385 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
2386 }
2387
2388 /*
2389 * Fserr prints the name of a file system with an error diagnostic.
2390 *
2391 * The form of the error message is:
2392 * fs: error message
2393 */
2394 static void
2395 ffs_fserr(struct fs *fs, u_int uid, const char *cp)
2396 {
2397
2398 log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
2399 uid, curproc->p_pid, curproc->p_comm, fs->fs_fsmnt, cp);
2400 }
2401