ffs_alloc.c revision 1.59 1 /* $NetBSD: ffs_alloc.c,v 1.59 2003/01/26 06:42:31 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.59 2003/01/26 06:42:31 tsutsui Exp $");
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_ffs.h"
43 #include "opt_quota.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufs_extern.h>
59 #include <ufs/ufs/ufs_bswap.h>
60
61 #include <ufs/ffs/fs.h>
62 #include <ufs/ffs/ffs_extern.h>
63
64 static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int));
65 static daddr_t ffs_alloccgblk __P((struct inode *, struct buf *, daddr_t));
66 #ifdef XXXUBC
67 static daddr_t ffs_clusteralloc __P((struct inode *, int, daddr_t, int));
68 #endif
69 static ino_t ffs_dirpref __P((struct inode *));
70 static daddr_t ffs_fragextend __P((struct inode *, int, daddr_t, int, int));
71 static void ffs_fserr __P((struct fs *, u_int, char *));
72 static daddr_t ffs_hashalloc __P((struct inode *, int, daddr_t, int,
73 daddr_t (*)(struct inode *, int, daddr_t, int)));
74 static daddr_t ffs_nodealloccg __P((struct inode *, int, daddr_t, int));
75 static daddr_t ffs_mapsearch __P((struct fs *, struct cg *,
76 daddr_t, int));
77 #if defined(DIAGNOSTIC) || defined(DEBUG)
78 #ifdef XXXUBC
79 static int ffs_checkblk __P((struct inode *, daddr_t, long size));
80 #endif
81 #endif
82
83 /* if 1, changes in optimalization strategy are logged */
84 int ffs_log_changeopt = 0;
85
86 /* in ffs_tables.c */
87 extern const int inside[], around[];
88 extern const u_char * const fragtbl[];
89
90 /*
91 * Allocate a block in the file system.
92 *
93 * The size of the requested block is given, which must be some
94 * multiple of fs_fsize and <= fs_bsize.
95 * A preference may be optionally specified. If a preference is given
96 * the following hierarchy is used to allocate a block:
97 * 1) allocate the requested block.
98 * 2) allocate a rotationally optimal block in the same cylinder.
99 * 3) allocate a block in the same cylinder group.
100 * 4) quadradically rehash into other cylinder groups, until an
101 * available block is located.
102 * If no block preference is given the following hierarchy is used
103 * to allocate a block:
104 * 1) allocate a block in the cylinder group that contains the
105 * inode for the file.
106 * 2) quadradically rehash into other cylinder groups, until an
107 * available block is located.
108 */
109 int
110 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
111 struct inode *ip;
112 daddr_t lbn, bpref;
113 int size;
114 struct ucred *cred;
115 daddr_t *bnp;
116 {
117 struct fs *fs = ip->i_fs;
118 daddr_t bno;
119 int cg;
120 #ifdef QUOTA
121 int error;
122 #endif
123
124 #ifdef UVM_PAGE_TRKOWN
125 if (ITOV(ip)->v_type == VREG &&
126 lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) {
127 struct vm_page *pg;
128 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
129 voff_t off = trunc_page(lblktosize(fs, lbn));
130 voff_t endoff = round_page(lblktosize(fs, lbn) + size);
131
132 simple_lock(&uobj->vmobjlock);
133 while (off < endoff) {
134 pg = uvm_pagelookup(uobj, off);
135 KASSERT(pg != NULL);
136 KASSERT(pg->owner == curproc->p_pid);
137 KASSERT((pg->flags & PG_CLEAN) == 0);
138 off += PAGE_SIZE;
139 }
140 simple_unlock(&uobj->vmobjlock);
141 }
142 #endif
143
144 *bnp = 0;
145 #ifdef DIAGNOSTIC
146 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
147 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
148 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
149 panic("ffs_alloc: bad size");
150 }
151 if (cred == NOCRED)
152 panic("ffs_alloc: missing credential");
153 #endif /* DIAGNOSTIC */
154 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
155 goto nospace;
156 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
157 goto nospace;
158 #ifdef QUOTA
159 if ((error = chkdq(ip, (long)btodb(size), cred, 0)) != 0)
160 return (error);
161 #endif
162 if (bpref >= fs->fs_size)
163 bpref = 0;
164 if (bpref == 0)
165 cg = ino_to_cg(fs, ip->i_number);
166 else
167 cg = dtog(fs, bpref);
168 bno = ffs_hashalloc(ip, cg, (long)bpref, size,
169 ffs_alloccg);
170 if (bno > 0) {
171 ip->i_ffs_blocks += btodb(size);
172 ip->i_flag |= IN_CHANGE | IN_UPDATE;
173 *bnp = bno;
174 return (0);
175 }
176 #ifdef QUOTA
177 /*
178 * Restore user's disk quota because allocation failed.
179 */
180 (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
181 #endif
182 nospace:
183 ffs_fserr(fs, cred->cr_uid, "file system full");
184 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
185 return (ENOSPC);
186 }
187
188 /*
189 * Reallocate a fragment to a bigger size
190 *
191 * The number and size of the old block is given, and a preference
192 * and new size is also specified. The allocator attempts to extend
193 * the original block. Failing that, the regular block allocator is
194 * invoked to get an appropriate block.
195 */
196 int
197 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp, blknop)
198 struct inode *ip;
199 daddr_t lbprev;
200 daddr_t bpref;
201 int osize, nsize;
202 struct ucred *cred;
203 struct buf **bpp;
204 daddr_t *blknop;
205 {
206 struct fs *fs = ip->i_fs;
207 struct buf *bp;
208 int cg, request, error;
209 daddr_t bprev, bno;
210
211 #ifdef UVM_PAGE_TRKOWN
212 if (ITOV(ip)->v_type == VREG) {
213 struct vm_page *pg;
214 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
215 voff_t off = trunc_page(lblktosize(fs, lbprev));
216 voff_t endoff = round_page(lblktosize(fs, lbprev) + osize);
217
218 simple_lock(&uobj->vmobjlock);
219 while (off < endoff) {
220 pg = uvm_pagelookup(uobj, off);
221 KASSERT(pg != NULL);
222 KASSERT(pg->owner == curproc->p_pid);
223 KASSERT((pg->flags & PG_CLEAN) == 0);
224 off += PAGE_SIZE;
225 }
226 simple_unlock(&uobj->vmobjlock);
227 }
228 #endif
229
230 #ifdef DIAGNOSTIC
231 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
232 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
233 printf(
234 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
235 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
236 panic("ffs_realloccg: bad size");
237 }
238 if (cred == NOCRED)
239 panic("ffs_realloccg: missing credential");
240 #endif /* DIAGNOSTIC */
241 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
242 goto nospace;
243 /* XXX ondisk32 */
244 if ((bprev = ufs_rw32(ip->i_ffs_db[lbprev], UFS_FSNEEDSWAP(fs))) == 0) {
245 printf("dev = 0x%x, bsize = %d, bprev = %" PRId64 ", fs = %s\n",
246 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
247 panic("ffs_realloccg: bad bprev");
248 }
249 /*
250 * Allocate the extra space in the buffer.
251 */
252 if (bpp != NULL &&
253 (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0) {
254 brelse(bp);
255 return (error);
256 }
257 #ifdef QUOTA
258 if ((error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) != 0) {
259 if (bpp != NULL) {
260 brelse(bp);
261 }
262 return (error);
263 }
264 #endif
265 /*
266 * Check for extension in the existing location.
267 */
268 cg = dtog(fs, bprev);
269 if ((bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) != 0) {
270 ip->i_ffs_blocks += btodb(nsize - osize);
271 ip->i_flag |= IN_CHANGE | IN_UPDATE;
272
273 if (bpp != NULL) {
274 if (bp->b_blkno != fsbtodb(fs, bno))
275 panic("bad blockno");
276 allocbuf(bp, nsize);
277 bp->b_flags |= B_DONE;
278 memset(bp->b_data + osize, 0, nsize - osize);
279 *bpp = bp;
280 }
281 if (blknop != NULL) {
282 *blknop = bno;
283 }
284 return (0);
285 }
286 /*
287 * Allocate a new disk location.
288 */
289 if (bpref >= fs->fs_size)
290 bpref = 0;
291 switch ((int)fs->fs_optim) {
292 case FS_OPTSPACE:
293 /*
294 * Allocate an exact sized fragment. Although this makes
295 * best use of space, we will waste time relocating it if
296 * the file continues to grow. If the fragmentation is
297 * less than half of the minimum free reserve, we choose
298 * to begin optimizing for time.
299 */
300 request = nsize;
301 if (fs->fs_minfree < 5 ||
302 fs->fs_cstotal.cs_nffree >
303 fs->fs_dsize * fs->fs_minfree / (2 * 100))
304 break;
305
306 if (ffs_log_changeopt) {
307 log(LOG_NOTICE,
308 "%s: optimization changed from SPACE to TIME\n",
309 fs->fs_fsmnt);
310 }
311
312 fs->fs_optim = FS_OPTTIME;
313 break;
314 case FS_OPTTIME:
315 /*
316 * At this point we have discovered a file that is trying to
317 * grow a small fragment to a larger fragment. To save time,
318 * we allocate a full sized block, then free the unused portion.
319 * If the file continues to grow, the `ffs_fragextend' call
320 * above will be able to grow it in place without further
321 * copying. If aberrant programs cause disk fragmentation to
322 * grow within 2% of the free reserve, we choose to begin
323 * optimizing for space.
324 */
325 request = fs->fs_bsize;
326 if (fs->fs_cstotal.cs_nffree <
327 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
328 break;
329
330 if (ffs_log_changeopt) {
331 log(LOG_NOTICE,
332 "%s: optimization changed from TIME to SPACE\n",
333 fs->fs_fsmnt);
334 }
335
336 fs->fs_optim = FS_OPTSPACE;
337 break;
338 default:
339 printf("dev = 0x%x, optim = %d, fs = %s\n",
340 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
341 panic("ffs_realloccg: bad optim");
342 /* NOTREACHED */
343 }
344 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
345 if (bno > 0) {
346 if (!DOINGSOFTDEP(ITOV(ip)))
347 ffs_blkfree(ip, bprev, (long)osize);
348 if (nsize < request)
349 ffs_blkfree(ip, bno + numfrags(fs, nsize),
350 (long)(request - nsize));
351 ip->i_ffs_blocks += btodb(nsize - osize);
352 ip->i_flag |= IN_CHANGE | IN_UPDATE;
353 if (bpp != NULL) {
354 bp->b_blkno = fsbtodb(fs, bno);
355 allocbuf(bp, nsize);
356 bp->b_flags |= B_DONE;
357 memset(bp->b_data + osize, 0, (u_int)nsize - osize);
358 *bpp = bp;
359 }
360 if (blknop != NULL) {
361 *blknop = bno;
362 }
363 return (0);
364 }
365 #ifdef QUOTA
366 /*
367 * Restore user's disk quota because allocation failed.
368 */
369 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
370 #endif
371 if (bpp != NULL) {
372 brelse(bp);
373 }
374
375 nospace:
376 /*
377 * no space available
378 */
379 ffs_fserr(fs, cred->cr_uid, "file system full");
380 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
381 return (ENOSPC);
382 }
383
384 /*
385 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
386 *
387 * The vnode and an array of buffer pointers for a range of sequential
388 * logical blocks to be made contiguous is given. The allocator attempts
389 * to find a range of sequential blocks starting as close as possible to
390 * an fs_rotdelay offset from the end of the allocation for the logical
391 * block immediately preceding the current range. If successful, the
392 * physical block numbers in the buffer pointers and in the inode are
393 * changed to reflect the new allocation. If unsuccessful, the allocation
394 * is left unchanged. The success in doing the reallocation is returned.
395 * Note that the error return is not reflected back to the user. Rather
396 * the previous block allocation will be used.
397 */
398 #ifdef XXXUBC
399 #ifdef DEBUG
400 #include <sys/sysctl.h>
401 int prtrealloc = 0;
402 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
403 #endif
404 #endif
405
406 int doasyncfree = 1;
407
408 int
409 ffs_reallocblks(v)
410 void *v;
411 {
412 #ifdef XXXUBC
413 struct vop_reallocblks_args /* {
414 struct vnode *a_vp;
415 struct cluster_save *a_buflist;
416 } */ *ap = v;
417 struct fs *fs;
418 struct inode *ip;
419 struct vnode *vp;
420 struct buf *sbp, *ebp;
421 int32_t *bap, *ebap = NULL, *sbap; /* XXX ondisk32 */
422 struct cluster_save *buflist;
423 daddr_t start_lbn, end_lbn, soff, newblk, blkno;
424 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
425 int i, len, start_lvl, end_lvl, pref, ssize;
426 #endif /* XXXUBC */
427
428 /* XXXUBC don't reallocblks for now */
429 return ENOSPC;
430
431 #ifdef XXXUBC
432 vp = ap->a_vp;
433 ip = VTOI(vp);
434 fs = ip->i_fs;
435 if (fs->fs_contigsumsize <= 0)
436 return (ENOSPC);
437 buflist = ap->a_buflist;
438 len = buflist->bs_nchildren;
439 start_lbn = buflist->bs_children[0]->b_lblkno;
440 end_lbn = start_lbn + len - 1;
441 #ifdef DIAGNOSTIC
442 for (i = 0; i < len; i++)
443 if (!ffs_checkblk(ip,
444 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
445 panic("ffs_reallocblks: unallocated block 1");
446 for (i = 1; i < len; i++)
447 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
448 panic("ffs_reallocblks: non-logical cluster");
449 blkno = buflist->bs_children[0]->b_blkno;
450 ssize = fsbtodb(fs, fs->fs_frag);
451 for (i = 1; i < len - 1; i++)
452 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
453 panic("ffs_reallocblks: non-physical cluster %d", i);
454 #endif
455 /*
456 * If the latest allocation is in a new cylinder group, assume that
457 * the filesystem has decided to move and do not force it back to
458 * the previous cylinder group.
459 */
460 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
461 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
462 return (ENOSPC);
463 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
464 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
465 return (ENOSPC);
466 /*
467 * Get the starting offset and block map for the first block.
468 */
469 if (start_lvl == 0) {
470 sbap = &ip->i_ffs_db[0]; /* XXX ondisk32 */
471 soff = start_lbn;
472 } else {
473 idp = &start_ap[start_lvl - 1];
474 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
475 brelse(sbp);
476 return (ENOSPC);
477 }
478 sbap = (int32_t *)sbp->b_data; /* XXX ondisk32 */
479 soff = idp->in_off;
480 }
481 /*
482 * Find the preferred location for the cluster.
483 */
484 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
485 /*
486 * If the block range spans two block maps, get the second map.
487 */
488 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
489 ssize = len;
490 } else {
491 #ifdef DIAGNOSTIC
492 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
493 panic("ffs_reallocblk: start == end");
494 #endif
495 ssize = len - (idp->in_off + 1);
496 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
497 goto fail;
498 ebap = (int32_t *)ebp->b_data; /* XXX ondisk32 */
499 }
500 /*
501 * Search the block map looking for an allocation of the desired size.
502 */
503 if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
504 len, ffs_clusteralloc)) == 0)
505 goto fail;
506 /*
507 * We have found a new contiguous block.
508 *
509 * First we have to replace the old block pointers with the new
510 * block pointers in the inode and indirect blocks associated
511 * with the file.
512 */
513 #ifdef DEBUG
514 if (prtrealloc)
515 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
516 start_lbn, end_lbn);
517 #endif
518 blkno = newblk;
519 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
520 daddr_t ba;
521
522 if (i == ssize) {
523 bap = ebap;
524 soff = -i;
525 }
526 /* XXX ondisk32 */
527 ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
528 #ifdef DIAGNOSTIC
529 if (!ffs_checkblk(ip,
530 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
531 panic("ffs_reallocblks: unallocated block 2");
532 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
533 panic("ffs_reallocblks: alloc mismatch");
534 #endif
535 #ifdef DEBUG
536 if (prtrealloc)
537 printf(" %d,", ba);
538 #endif
539 if (DOINGSOFTDEP(vp)) {
540 if (sbap == &ip->i_ffs_db[0] && i < ssize)
541 softdep_setup_allocdirect(ip, start_lbn + i,
542 blkno, ba, fs->fs_bsize, fs->fs_bsize,
543 buflist->bs_children[i]);
544 else
545 softdep_setup_allocindir_page(ip, start_lbn + i,
546 i < ssize ? sbp : ebp, soff + i, blkno,
547 ba, buflist->bs_children[i]);
548 }
549 /* XXX ondisk32 */
550 *bap++ = ufs_rw32((int32_t)blkno, UFS_FSNEEDSWAP(fs));
551 }
552 /*
553 * Next we must write out the modified inode and indirect blocks.
554 * For strict correctness, the writes should be synchronous since
555 * the old block values may have been written to disk. In practise
556 * they are almost never written, but if we are concerned about
557 * strict correctness, the `doasyncfree' flag should be set to zero.
558 *
559 * The test on `doasyncfree' should be changed to test a flag
560 * that shows whether the associated buffers and inodes have
561 * been written. The flag should be set when the cluster is
562 * started and cleared whenever the buffer or inode is flushed.
563 * We can then check below to see if it is set, and do the
564 * synchronous write only when it has been cleared.
565 */
566 if (sbap != &ip->i_ffs_db[0]) {
567 if (doasyncfree)
568 bdwrite(sbp);
569 else
570 bwrite(sbp);
571 } else {
572 ip->i_flag |= IN_CHANGE | IN_UPDATE;
573 if (!doasyncfree)
574 VOP_UPDATE(vp, NULL, NULL, 1);
575 }
576 if (ssize < len) {
577 if (doasyncfree)
578 bdwrite(ebp);
579 else
580 bwrite(ebp);
581 }
582 /*
583 * Last, free the old blocks and assign the new blocks to the buffers.
584 */
585 #ifdef DEBUG
586 if (prtrealloc)
587 printf("\n\tnew:");
588 #endif
589 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
590 if (!DOINGSOFTDEP(vp))
591 ffs_blkfree(ip,
592 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
593 fs->fs_bsize);
594 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
595 #ifdef DEBUG
596 if (!ffs_checkblk(ip,
597 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
598 panic("ffs_reallocblks: unallocated block 3");
599 if (prtrealloc)
600 printf(" %d,", blkno);
601 #endif
602 }
603 #ifdef DEBUG
604 if (prtrealloc) {
605 prtrealloc--;
606 printf("\n");
607 }
608 #endif
609 return (0);
610
611 fail:
612 if (ssize < len)
613 brelse(ebp);
614 if (sbap != &ip->i_ffs_db[0])
615 brelse(sbp);
616 return (ENOSPC);
617 #endif /* XXXUBC */
618 }
619
620 /*
621 * Allocate an inode in the file system.
622 *
623 * If allocating a directory, use ffs_dirpref to select the inode.
624 * If allocating in a directory, the following hierarchy is followed:
625 * 1) allocate the preferred inode.
626 * 2) allocate an inode in the same cylinder group.
627 * 3) quadradically rehash into other cylinder groups, until an
628 * available inode is located.
629 * If no inode preference is given the following hierarchy is used
630 * to allocate an inode:
631 * 1) allocate an inode in cylinder group 0.
632 * 2) quadradically rehash into other cylinder groups, until an
633 * available inode is located.
634 */
635 int
636 ffs_valloc(v)
637 void *v;
638 {
639 struct vop_valloc_args /* {
640 struct vnode *a_pvp;
641 int a_mode;
642 struct ucred *a_cred;
643 struct vnode **a_vpp;
644 } */ *ap = v;
645 struct vnode *pvp = ap->a_pvp;
646 struct inode *pip;
647 struct fs *fs;
648 struct inode *ip;
649 mode_t mode = ap->a_mode;
650 ino_t ino, ipref;
651 int cg, error;
652
653 *ap->a_vpp = NULL;
654 pip = VTOI(pvp);
655 fs = pip->i_fs;
656 if (fs->fs_cstotal.cs_nifree == 0)
657 goto noinodes;
658
659 if ((mode & IFMT) == IFDIR)
660 ipref = ffs_dirpref(pip);
661 else
662 ipref = pip->i_number;
663 if (ipref >= fs->fs_ncg * fs->fs_ipg)
664 ipref = 0;
665 cg = ino_to_cg(fs, ipref);
666 /*
667 * Track number of dirs created one after another
668 * in a same cg without intervening by files.
669 */
670 if ((mode & IFMT) == IFDIR) {
671 if (fs->fs_contigdirs[cg] < 65535)
672 fs->fs_contigdirs[cg]++;
673 } else {
674 if (fs->fs_contigdirs[cg] > 0)
675 fs->fs_contigdirs[cg]--;
676 }
677 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
678 if (ino == 0)
679 goto noinodes;
680 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
681 if (error) {
682 VOP_VFREE(pvp, ino, mode);
683 return (error);
684 }
685 ip = VTOI(*ap->a_vpp);
686 if (ip->i_ffs_mode) {
687 printf("mode = 0%o, inum = %d, fs = %s\n",
688 ip->i_ffs_mode, ip->i_number, fs->fs_fsmnt);
689 panic("ffs_valloc: dup alloc");
690 }
691 if (ip->i_ffs_blocks) { /* XXX */
692 printf("free inode %s/%d had %d blocks\n",
693 fs->fs_fsmnt, ino, ip->i_ffs_blocks);
694 ip->i_ffs_blocks = 0;
695 }
696 ip->i_flag &= ~IN_SPACECOUNTED;
697 ip->i_ffs_flags = 0;
698 /*
699 * Set up a new generation number for this inode.
700 */
701 ip->i_ffs_gen++;
702 return (0);
703 noinodes:
704 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
705 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
706 return (ENOSPC);
707 }
708
709 /*
710 * Find a cylinder group in which to place a directory.
711 *
712 * The policy implemented by this algorithm is to allocate a
713 * directory inode in the same cylinder group as its parent
714 * directory, but also to reserve space for its files inodes
715 * and data. Restrict the number of directories which may be
716 * allocated one after another in the same cylinder group
717 * without intervening allocation of files.
718 *
719 * If we allocate a first level directory then force allocation
720 * in another cylinder group.
721 */
722 static ino_t
723 ffs_dirpref(pip)
724 struct inode *pip;
725 {
726 register struct fs *fs;
727 int cg, prefcg, dirsize, cgsize;
728 int avgifree, avgbfree, avgndir, curdirsize;
729 int minifree, minbfree, maxndir;
730 int mincg, minndir;
731 int maxcontigdirs;
732
733 fs = pip->i_fs;
734
735 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
736 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
737 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
738
739 /*
740 * Force allocation in another cg if creating a first level dir.
741 */
742 if (ITOV(pip)->v_flag & VROOT) {
743 prefcg = random() % fs->fs_ncg;
744 mincg = prefcg;
745 minndir = fs->fs_ipg;
746 for (cg = prefcg; cg < fs->fs_ncg; cg++)
747 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
748 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
749 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
750 mincg = cg;
751 minndir = fs->fs_cs(fs, cg).cs_ndir;
752 }
753 for (cg = 0; cg < prefcg; cg++)
754 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
755 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
756 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
757 mincg = cg;
758 minndir = fs->fs_cs(fs, cg).cs_ndir;
759 }
760 return ((ino_t)(fs->fs_ipg * mincg));
761 }
762
763 /*
764 * Count various limits which used for
765 * optimal allocation of a directory inode.
766 */
767 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
768 minifree = avgifree - fs->fs_ipg / 4;
769 if (minifree < 0)
770 minifree = 0;
771 minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4;
772 if (minbfree < 0)
773 minbfree = 0;
774 cgsize = fs->fs_fsize * fs->fs_fpg;
775 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
776 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
777 if (dirsize < curdirsize)
778 dirsize = curdirsize;
779 maxcontigdirs = min(cgsize / dirsize, 255);
780 if (fs->fs_avgfpdir > 0)
781 maxcontigdirs = min(maxcontigdirs,
782 fs->fs_ipg / fs->fs_avgfpdir);
783 if (maxcontigdirs == 0)
784 maxcontigdirs = 1;
785
786 /*
787 * Limit number of dirs in one cg and reserve space for
788 * regular files, but only if we have no deficit in
789 * inodes or space.
790 */
791 prefcg = ino_to_cg(fs, pip->i_number);
792 for (cg = prefcg; cg < fs->fs_ncg; cg++)
793 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
794 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
795 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
796 if (fs->fs_contigdirs[cg] < maxcontigdirs)
797 return ((ino_t)(fs->fs_ipg * cg));
798 }
799 for (cg = 0; cg < prefcg; cg++)
800 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
801 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
802 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
803 if (fs->fs_contigdirs[cg] < maxcontigdirs)
804 return ((ino_t)(fs->fs_ipg * cg));
805 }
806 /*
807 * This is a backstop when we are deficient in space.
808 */
809 for (cg = prefcg; cg < fs->fs_ncg; cg++)
810 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
811 return ((ino_t)(fs->fs_ipg * cg));
812 for (cg = 0; cg < prefcg; cg++)
813 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
814 break;
815 return ((ino_t)(fs->fs_ipg * cg));
816 }
817
818 /*
819 * Select the desired position for the next block in a file. The file is
820 * logically divided into sections. The first section is composed of the
821 * direct blocks. Each additional section contains fs_maxbpg blocks.
822 *
823 * If no blocks have been allocated in the first section, the policy is to
824 * request a block in the same cylinder group as the inode that describes
825 * the file. If no blocks have been allocated in any other section, the
826 * policy is to place the section in a cylinder group with a greater than
827 * average number of free blocks. An appropriate cylinder group is found
828 * by using a rotor that sweeps the cylinder groups. When a new group of
829 * blocks is needed, the sweep begins in the cylinder group following the
830 * cylinder group from which the previous allocation was made. The sweep
831 * continues until a cylinder group with greater than the average number
832 * of free blocks is found. If the allocation is for the first block in an
833 * indirect block, the information on the previous allocation is unavailable;
834 * here a best guess is made based upon the logical block number being
835 * allocated.
836 *
837 * If a section is already partially allocated, the policy is to
838 * contiguously allocate fs_maxcontig blocks. The end of one of these
839 * contiguous blocks and the beginning of the next is physically separated
840 * so that the disk head will be in transit between them for at least
841 * fs_rotdelay milliseconds. This is to allow time for the processor to
842 * schedule another I/O transfer.
843 */
844 daddr_t
845 ffs_blkpref(ip, lbn, indx, bap)
846 struct inode *ip;
847 daddr_t lbn;
848 int indx;
849 int32_t *bap; /* XXX ondisk32 */
850 {
851 struct fs *fs;
852 int cg;
853 int avgbfree, startcg;
854 daddr_t nextblk;
855
856 fs = ip->i_fs;
857 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
858 if (lbn < NDADDR + NINDIR(fs)) {
859 cg = ino_to_cg(fs, ip->i_number);
860 return (fs->fs_fpg * cg + fs->fs_frag);
861 }
862 /*
863 * Find a cylinder with greater than average number of
864 * unused data blocks.
865 */
866 if (indx == 0 || bap[indx - 1] == 0)
867 startcg =
868 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
869 else
870 startcg = dtog(fs,
871 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
872 startcg %= fs->fs_ncg;
873 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
874 for (cg = startcg; cg < fs->fs_ncg; cg++)
875 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
876 return (fs->fs_fpg * cg + fs->fs_frag);
877 }
878 for (cg = 0; cg < startcg; cg++)
879 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
880 return (fs->fs_fpg * cg + fs->fs_frag);
881 }
882 return (0);
883 }
884 /*
885 * One or more previous blocks have been laid out. If less
886 * than fs_maxcontig previous blocks are contiguous, the
887 * next block is requested contiguously, otherwise it is
888 * requested rotationally delayed by fs_rotdelay milliseconds.
889 */
890 /* XXX ondisk32 */
891 nextblk = ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
892 if (indx < fs->fs_maxcontig ||
893 ufs_rw32(bap[indx - fs->fs_maxcontig], UFS_FSNEEDSWAP(fs)) +
894 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
895 return (nextblk);
896 if (fs->fs_rotdelay != 0)
897 /*
898 * Here we convert ms of delay to frags as:
899 * (frags) = (ms) * (rev/sec) * (sect/rev) /
900 * ((sect/frag) * (ms/sec))
901 * then round up to the next block.
902 */
903 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
904 (NSPF(fs) * 1000), fs->fs_frag);
905 return (nextblk);
906 }
907
908 /*
909 * Implement the cylinder overflow algorithm.
910 *
911 * The policy implemented by this algorithm is:
912 * 1) allocate the block in its requested cylinder group.
913 * 2) quadradically rehash on the cylinder group number.
914 * 3) brute force search for a free block.
915 */
916 /*VARARGS5*/
917 static daddr_t
918 ffs_hashalloc(ip, cg, pref, size, allocator)
919 struct inode *ip;
920 int cg;
921 daddr_t pref;
922 int size; /* size for data blocks, mode for inodes */
923 daddr_t (*allocator) __P((struct inode *, int, daddr_t, int));
924 {
925 struct fs *fs;
926 daddr_t result;
927 int i, icg = cg;
928
929 fs = ip->i_fs;
930 /*
931 * 1: preferred cylinder group
932 */
933 result = (*allocator)(ip, cg, pref, size);
934 if (result)
935 return (result);
936 /*
937 * 2: quadratic rehash
938 */
939 for (i = 1; i < fs->fs_ncg; i *= 2) {
940 cg += i;
941 if (cg >= fs->fs_ncg)
942 cg -= fs->fs_ncg;
943 result = (*allocator)(ip, cg, 0, size);
944 if (result)
945 return (result);
946 }
947 /*
948 * 3: brute force search
949 * Note that we start at i == 2, since 0 was checked initially,
950 * and 1 is always checked in the quadratic rehash.
951 */
952 cg = (icg + 2) % fs->fs_ncg;
953 for (i = 2; i < fs->fs_ncg; i++) {
954 result = (*allocator)(ip, cg, 0, size);
955 if (result)
956 return (result);
957 cg++;
958 if (cg == fs->fs_ncg)
959 cg = 0;
960 }
961 return (0);
962 }
963
964 /*
965 * Determine whether a fragment can be extended.
966 *
967 * Check to see if the necessary fragments are available, and
968 * if they are, allocate them.
969 */
970 static daddr_t
971 ffs_fragextend(ip, cg, bprev, osize, nsize)
972 struct inode *ip;
973 int cg;
974 daddr_t bprev;
975 int osize, nsize;
976 {
977 struct fs *fs;
978 struct cg *cgp;
979 struct buf *bp;
980 daddr_t bno;
981 int frags, bbase;
982 int i, error;
983
984 fs = ip->i_fs;
985 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
986 return (0);
987 frags = numfrags(fs, nsize);
988 bbase = fragnum(fs, bprev);
989 if (bbase > fragnum(fs, (bprev + frags - 1))) {
990 /* cannot extend across a block boundary */
991 return (0);
992 }
993 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
994 (int)fs->fs_cgsize, NOCRED, &bp);
995 if (error) {
996 brelse(bp);
997 return (0);
998 }
999 cgp = (struct cg *)bp->b_data;
1000 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1001 brelse(bp);
1002 return (0);
1003 }
1004 cgp->cg_time = ufs_rw32(time.tv_sec, UFS_FSNEEDSWAP(fs));
1005 bno = dtogd(fs, bprev);
1006 for (i = numfrags(fs, osize); i < frags; i++)
1007 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i)) {
1008 brelse(bp);
1009 return (0);
1010 }
1011 /*
1012 * the current fragment can be extended
1013 * deduct the count on fragment being extended into
1014 * increase the count on the remaining fragment (if any)
1015 * allocate the extended piece
1016 */
1017 for (i = frags; i < fs->fs_frag - bbase; i++)
1018 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1019 break;
1020 ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1021 if (i != frags)
1022 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1023 for (i = numfrags(fs, osize); i < frags; i++) {
1024 clrbit(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i);
1025 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1026 fs->fs_cstotal.cs_nffree--;
1027 fs->fs_cs(fs, cg).cs_nffree--;
1028 }
1029 fs->fs_fmod = 1;
1030 if (DOINGSOFTDEP(ITOV(ip)))
1031 softdep_setup_blkmapdep(bp, fs, bprev);
1032 bdwrite(bp);
1033 return (bprev);
1034 }
1035
1036 /*
1037 * Determine whether a block can be allocated.
1038 *
1039 * Check to see if a block of the appropriate size is available,
1040 * and if it is, allocate it.
1041 */
1042 static daddr_t
1043 ffs_alloccg(ip, cg, bpref, size)
1044 struct inode *ip;
1045 int cg;
1046 daddr_t bpref;
1047 int size;
1048 {
1049 struct cg *cgp;
1050 struct buf *bp;
1051 daddr_t bno, blkno;
1052 int error, frags, allocsiz, i;
1053 struct fs *fs = ip->i_fs;
1054 #ifdef FFS_EI
1055 const int needswap = UFS_FSNEEDSWAP(fs);
1056 #endif
1057
1058 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1059 return (0);
1060 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1061 (int)fs->fs_cgsize, NOCRED, &bp);
1062 if (error) {
1063 brelse(bp);
1064 return (0);
1065 }
1066 cgp = (struct cg *)bp->b_data;
1067 if (!cg_chkmagic(cgp, needswap) ||
1068 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1069 brelse(bp);
1070 return (0);
1071 }
1072 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1073 if (size == fs->fs_bsize) {
1074 bno = ffs_alloccgblk(ip, bp, bpref);
1075 bdwrite(bp);
1076 return (bno);
1077 }
1078 /*
1079 * check to see if any fragments are already available
1080 * allocsiz is the size which will be allocated, hacking
1081 * it down to a smaller size if necessary
1082 */
1083 frags = numfrags(fs, size);
1084 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1085 if (cgp->cg_frsum[allocsiz] != 0)
1086 break;
1087 if (allocsiz == fs->fs_frag) {
1088 /*
1089 * no fragments were available, so a block will be
1090 * allocated, and hacked up
1091 */
1092 if (cgp->cg_cs.cs_nbfree == 0) {
1093 brelse(bp);
1094 return (0);
1095 }
1096 bno = ffs_alloccgblk(ip, bp, bpref);
1097 bpref = dtogd(fs, bno);
1098 for (i = frags; i < fs->fs_frag; i++)
1099 setbit(cg_blksfree(cgp, needswap), bpref + i);
1100 i = fs->fs_frag - frags;
1101 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1102 fs->fs_cstotal.cs_nffree += i;
1103 fs->fs_cs(fs, cg).cs_nffree += i;
1104 fs->fs_fmod = 1;
1105 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1106 bdwrite(bp);
1107 return (bno);
1108 }
1109 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1110 #if 0
1111 /*
1112 * XXX fvdl mapsearch will panic, and never return -1
1113 * also: returning NULL as daddr_t ?
1114 */
1115 if (bno < 0) {
1116 brelse(bp);
1117 return (0);
1118 }
1119 #endif
1120 for (i = 0; i < frags; i++)
1121 clrbit(cg_blksfree(cgp, needswap), bno + i);
1122 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1123 fs->fs_cstotal.cs_nffree -= frags;
1124 fs->fs_cs(fs, cg).cs_nffree -= frags;
1125 fs->fs_fmod = 1;
1126 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1127 if (frags != allocsiz)
1128 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1129 blkno = cg * fs->fs_fpg + bno;
1130 if (DOINGSOFTDEP(ITOV(ip)))
1131 softdep_setup_blkmapdep(bp, fs, blkno);
1132 bdwrite(bp);
1133 return blkno;
1134 }
1135
1136 /*
1137 * Allocate a block in a cylinder group.
1138 *
1139 * This algorithm implements the following policy:
1140 * 1) allocate the requested block.
1141 * 2) allocate a rotationally optimal block in the same cylinder.
1142 * 3) allocate the next available block on the block rotor for the
1143 * specified cylinder group.
1144 * Note that this routine only allocates fs_bsize blocks; these
1145 * blocks may be fragmented by the routine that allocates them.
1146 */
1147 static daddr_t
1148 ffs_alloccgblk(ip, bp, bpref)
1149 struct inode *ip;
1150 struct buf *bp;
1151 daddr_t bpref;
1152 {
1153 struct cg *cgp;
1154 daddr_t bno, blkno;
1155 int cylno, pos, delta;
1156 short *cylbp;
1157 int i;
1158 struct fs *fs = ip->i_fs;
1159 #ifdef FFS_EI
1160 const int needswap = UFS_FSNEEDSWAP(fs);
1161 #endif
1162
1163 cgp = (struct cg *)bp->b_data;
1164 /* XXX ondisk32 */
1165 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1166 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1167 goto norot;
1168 }
1169 bpref = blknum(fs, bpref);
1170 bpref = dtogd(fs, bpref);
1171 /*
1172 * if the requested block is available, use it
1173 */
1174 if (ffs_isblock(fs, cg_blksfree(cgp, needswap),
1175 fragstoblks(fs, bpref))) {
1176 bno = bpref;
1177 goto gotit;
1178 }
1179 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1180 /*
1181 * Block layout information is not available.
1182 * Leaving bpref unchanged means we take the
1183 * next available free block following the one
1184 * we just allocated. Hopefully this will at
1185 * least hit a track cache on drives of unknown
1186 * geometry (e.g. SCSI).
1187 */
1188 goto norot;
1189 }
1190 /*
1191 * check for a block available on the same cylinder
1192 */
1193 cylno = cbtocylno(fs, bpref);
1194 if (cg_blktot(cgp, needswap)[cylno] == 0)
1195 goto norot;
1196 /*
1197 * check the summary information to see if a block is
1198 * available in the requested cylinder starting at the
1199 * requested rotational position and proceeding around.
1200 */
1201 cylbp = cg_blks(fs, cgp, cylno, needswap);
1202 pos = cbtorpos(fs, bpref);
1203 for (i = pos; i < fs->fs_nrpos; i++)
1204 if (ufs_rw16(cylbp[i], needswap) > 0)
1205 break;
1206 if (i == fs->fs_nrpos)
1207 for (i = 0; i < pos; i++)
1208 if (ufs_rw16(cylbp[i], needswap) > 0)
1209 break;
1210 if (ufs_rw16(cylbp[i], needswap) > 0) {
1211 /*
1212 * found a rotational position, now find the actual
1213 * block. A panic if none is actually there.
1214 */
1215 pos = cylno % fs->fs_cpc;
1216 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1217 if (fs_postbl(fs, pos)[i] == -1) {
1218 printf("pos = %d, i = %d, fs = %s\n",
1219 pos, i, fs->fs_fsmnt);
1220 panic("ffs_alloccgblk: cyl groups corrupted");
1221 }
1222 for (i = fs_postbl(fs, pos)[i];; ) {
1223 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), bno + i)) {
1224 bno = blkstofrags(fs, (bno + i));
1225 goto gotit;
1226 }
1227 delta = fs_rotbl(fs)[i];
1228 if (delta <= 0 ||
1229 delta + i > fragstoblks(fs, fs->fs_fpg))
1230 break;
1231 i += delta;
1232 }
1233 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1234 panic("ffs_alloccgblk: can't find blk in cyl");
1235 }
1236 norot:
1237 /*
1238 * no blocks in the requested cylinder, so take next
1239 * available one in this cylinder group.
1240 */
1241 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1242 if (bno < 0)
1243 return (0);
1244 /* XXX ondisk32 */
1245 cgp->cg_rotor = ufs_rw32((int32_t)bno, needswap);
1246 gotit:
1247 blkno = fragstoblks(fs, bno);
1248 ffs_clrblock(fs, cg_blksfree(cgp, needswap), (long)blkno);
1249 ffs_clusteracct(fs, cgp, blkno, -1);
1250 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1251 fs->fs_cstotal.cs_nbfree--;
1252 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1253 cylno = cbtocylno(fs, bno);
1254 ufs_add16(cg_blks(fs, cgp, cylno, needswap)[cbtorpos(fs, bno)], -1,
1255 needswap);
1256 ufs_add32(cg_blktot(cgp, needswap)[cylno], -1, needswap);
1257 fs->fs_fmod = 1;
1258 /* XXX ondisk32 */
1259 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
1260 if (DOINGSOFTDEP(ITOV(ip)))
1261 softdep_setup_blkmapdep(bp, fs, blkno);
1262 return (blkno);
1263 }
1264
1265 #ifdef XXXUBC
1266 /*
1267 * Determine whether a cluster can be allocated.
1268 *
1269 * We do not currently check for optimal rotational layout if there
1270 * are multiple choices in the same cylinder group. Instead we just
1271 * take the first one that we find following bpref.
1272 */
1273 static daddr_t
1274 ffs_clusteralloc(ip, cg, bpref, len)
1275 struct inode *ip;
1276 int cg;
1277 daddr_t bpref;
1278 int len;
1279 {
1280 struct fs *fs;
1281 struct cg *cgp;
1282 struct buf *bp;
1283 int i, got, run, bno, bit, map;
1284 u_char *mapp;
1285 int32_t *lp;
1286
1287 fs = ip->i_fs;
1288 if (fs->fs_maxcluster[cg] < len)
1289 return (0);
1290 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1291 NOCRED, &bp))
1292 goto fail;
1293 cgp = (struct cg *)bp->b_data;
1294 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1295 goto fail;
1296 /*
1297 * Check to see if a cluster of the needed size (or bigger) is
1298 * available in this cylinder group.
1299 */
1300 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1301 for (i = len; i <= fs->fs_contigsumsize; i++)
1302 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1303 break;
1304 if (i > fs->fs_contigsumsize) {
1305 /*
1306 * This is the first time looking for a cluster in this
1307 * cylinder group. Update the cluster summary information
1308 * to reflect the true maximum sized cluster so that
1309 * future cluster allocation requests can avoid reading
1310 * the cylinder group map only to find no clusters.
1311 */
1312 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1313 for (i = len - 1; i > 0; i--)
1314 if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1315 break;
1316 fs->fs_maxcluster[cg] = i;
1317 goto fail;
1318 }
1319 /*
1320 * Search the cluster map to find a big enough cluster.
1321 * We take the first one that we find, even if it is larger
1322 * than we need as we prefer to get one close to the previous
1323 * block allocation. We do not search before the current
1324 * preference point as we do not want to allocate a block
1325 * that is allocated before the previous one (as we will
1326 * then have to wait for another pass of the elevator
1327 * algorithm before it will be read). We prefer to fail and
1328 * be recalled to try an allocation in the next cylinder group.
1329 */
1330 if (dtog(fs, bpref) != cg)
1331 bpref = 0;
1332 else
1333 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1334 mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1335 map = *mapp++;
1336 bit = 1 << (bpref % NBBY);
1337 for (run = 0, got = bpref;
1338 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1339 if ((map & bit) == 0) {
1340 run = 0;
1341 } else {
1342 run++;
1343 if (run == len)
1344 break;
1345 }
1346 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1347 bit <<= 1;
1348 } else {
1349 map = *mapp++;
1350 bit = 1;
1351 }
1352 }
1353 if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1354 goto fail;
1355 /*
1356 * Allocate the cluster that we have found.
1357 */
1358 #ifdef DIAGNOSTIC
1359 for (i = 1; i <= len; i++)
1360 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1361 got - run + i))
1362 panic("ffs_clusteralloc: map mismatch");
1363 #endif
1364 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1365 if (dtog(fs, bno) != cg)
1366 panic("ffs_clusteralloc: allocated out of group");
1367 len = blkstofrags(fs, len);
1368 for (i = 0; i < len; i += fs->fs_frag)
1369 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1370 panic("ffs_clusteralloc: lost block");
1371 bdwrite(bp);
1372 return (bno);
1373
1374 fail:
1375 brelse(bp);
1376 return (0);
1377 }
1378 #endif /* XXXUBC */
1379
1380 /*
1381 * Determine whether an inode can be allocated.
1382 *
1383 * Check to see if an inode is available, and if it is,
1384 * allocate it using the following policy:
1385 * 1) allocate the requested inode.
1386 * 2) allocate the next available inode after the requested
1387 * inode in the specified cylinder group.
1388 */
1389 static daddr_t
1390 ffs_nodealloccg(ip, cg, ipref, mode)
1391 struct inode *ip;
1392 int cg;
1393 daddr_t ipref;
1394 int mode;
1395 {
1396 struct cg *cgp;
1397 struct buf *bp;
1398 int error, start, len, loc, map, i;
1399 struct fs *fs = ip->i_fs;
1400 #ifdef FFS_EI
1401 const int needswap = UFS_FSNEEDSWAP(fs);
1402 #endif
1403
1404 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1405 return (0);
1406 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1407 (int)fs->fs_cgsize, NOCRED, &bp);
1408 if (error) {
1409 brelse(bp);
1410 return (0);
1411 }
1412 cgp = (struct cg *)bp->b_data;
1413 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
1414 brelse(bp);
1415 return (0);
1416 }
1417 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1418 if (ipref) {
1419 ipref %= fs->fs_ipg;
1420 if (isclr(cg_inosused(cgp, needswap), ipref))
1421 goto gotit;
1422 }
1423 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1424 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1425 NBBY);
1426 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[start]);
1427 if (loc == 0) {
1428 len = start + 1;
1429 start = 0;
1430 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[0]);
1431 if (loc == 0) {
1432 printf("cg = %d, irotor = %d, fs = %s\n",
1433 cg, ufs_rw32(cgp->cg_irotor, needswap),
1434 fs->fs_fsmnt);
1435 panic("ffs_nodealloccg: map corrupted");
1436 /* NOTREACHED */
1437 }
1438 }
1439 i = start + len - loc;
1440 map = cg_inosused(cgp, needswap)[i];
1441 ipref = i * NBBY;
1442 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1443 if ((map & i) == 0) {
1444 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1445 goto gotit;
1446 }
1447 }
1448 printf("fs = %s\n", fs->fs_fsmnt);
1449 panic("ffs_nodealloccg: block not in map");
1450 /* NOTREACHED */
1451 gotit:
1452 if (DOINGSOFTDEP(ITOV(ip)))
1453 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1454 setbit(cg_inosused(cgp, needswap), ipref);
1455 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1456 fs->fs_cstotal.cs_nifree--;
1457 fs->fs_cs(fs, cg).cs_nifree--;
1458 fs->fs_fmod = 1;
1459 if ((mode & IFMT) == IFDIR) {
1460 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1461 fs->fs_cstotal.cs_ndir++;
1462 fs->fs_cs(fs, cg).cs_ndir++;
1463 }
1464 bdwrite(bp);
1465 return (cg * fs->fs_ipg + ipref);
1466 }
1467
1468 /*
1469 * Free a block or fragment.
1470 *
1471 * The specified block or fragment is placed back in the
1472 * free map. If a fragment is deallocated, a possible
1473 * block reassembly is checked.
1474 */
1475 void
1476 ffs_blkfree(ip, bno, size)
1477 struct inode *ip;
1478 daddr_t bno;
1479 long size;
1480 {
1481 struct cg *cgp;
1482 struct buf *bp;
1483 daddr_t blkno;
1484 int i, error, cg, blk, frags, bbase;
1485 struct fs *fs = ip->i_fs;
1486 const int needswap = UFS_FSNEEDSWAP(fs);
1487
1488 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1489 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1490 printf("dev = 0x%x, bno = %" PRId64 " bsize = %d, "
1491 "size = %ld, fs = %s\n",
1492 ip->i_dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1493 panic("blkfree: bad size");
1494 }
1495 cg = dtog(fs, bno);
1496 if ((u_int)bno >= fs->fs_size) {
1497 printf("bad block %" PRId64 ", ino %d\n", bno, ip->i_number);
1498 ffs_fserr(fs, ip->i_ffs_uid, "bad block");
1499 return;
1500 }
1501 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1502 (int)fs->fs_cgsize, NOCRED, &bp);
1503 if (error) {
1504 brelse(bp);
1505 return;
1506 }
1507 cgp = (struct cg *)bp->b_data;
1508 if (!cg_chkmagic(cgp, needswap)) {
1509 brelse(bp);
1510 return;
1511 }
1512 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1513 bno = dtogd(fs, bno);
1514 if (size == fs->fs_bsize) {
1515 blkno = fragstoblks(fs, bno);
1516 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1517 printf("dev = 0x%x, block = %" PRId64 ", fs = %s\n",
1518 ip->i_dev, bno, fs->fs_fsmnt);
1519 panic("blkfree: freeing free block");
1520 }
1521 ffs_setblock(fs, cg_blksfree(cgp, needswap), blkno);
1522 ffs_clusteracct(fs, cgp, blkno, 1);
1523 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1524 fs->fs_cstotal.cs_nbfree++;
1525 fs->fs_cs(fs, cg).cs_nbfree++;
1526 i = cbtocylno(fs, bno);
1527 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bno)], 1,
1528 needswap);
1529 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1530 } else {
1531 bbase = bno - fragnum(fs, bno);
1532 /*
1533 * decrement the counts associated with the old frags
1534 */
1535 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1536 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1537 /*
1538 * deallocate the fragment
1539 */
1540 frags = numfrags(fs, size);
1541 for (i = 0; i < frags; i++) {
1542 if (isset(cg_blksfree(cgp, needswap), bno + i)) {
1543 printf("dev = 0x%x, block = %" PRId64
1544 ", fs = %s\n",
1545 ip->i_dev, bno + i, fs->fs_fsmnt);
1546 panic("blkfree: freeing free frag");
1547 }
1548 setbit(cg_blksfree(cgp, needswap), bno + i);
1549 }
1550 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1551 fs->fs_cstotal.cs_nffree += i;
1552 fs->fs_cs(fs, cg).cs_nffree += i;
1553 /*
1554 * add back in counts associated with the new frags
1555 */
1556 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1557 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1558 /*
1559 * if a complete block has been reassembled, account for it
1560 */
1561 blkno = fragstoblks(fs, bbase);
1562 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1563 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1564 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1565 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1566 ffs_clusteracct(fs, cgp, blkno, 1);
1567 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1568 fs->fs_cstotal.cs_nbfree++;
1569 fs->fs_cs(fs, cg).cs_nbfree++;
1570 i = cbtocylno(fs, bbase);
1571 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs,
1572 bbase)], 1,
1573 needswap);
1574 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1575 }
1576 }
1577 fs->fs_fmod = 1;
1578 bdwrite(bp);
1579 }
1580
1581 #if defined(DIAGNOSTIC) || defined(DEBUG)
1582 #ifdef XXXUBC
1583 /*
1584 * Verify allocation of a block or fragment. Returns true if block or
1585 * fragment is allocated, false if it is free.
1586 */
1587 static int
1588 ffs_checkblk(ip, bno, size)
1589 struct inode *ip;
1590 daddr_t bno;
1591 long size;
1592 {
1593 struct fs *fs;
1594 struct cg *cgp;
1595 struct buf *bp;
1596 int i, error, frags, free;
1597
1598 fs = ip->i_fs;
1599 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1600 printf("bsize = %d, size = %ld, fs = %s\n",
1601 fs->fs_bsize, size, fs->fs_fsmnt);
1602 panic("checkblk: bad size");
1603 }
1604 if ((int32_t)bno >= fs->fs_size)
1605 panic("checkblk: bad block %d", bno);
1606 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1607 (int)fs->fs_cgsize, NOCRED, &bp);
1608 if (error) {
1609 brelse(bp);
1610 return 0;
1611 }
1612 cgp = (struct cg *)bp->b_data;
1613 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1614 brelse(bp);
1615 return 0;
1616 }
1617 bno = dtogd(fs, bno);
1618 if (size == fs->fs_bsize) {
1619 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1620 fragstoblks(fs, bno));
1621 } else {
1622 frags = numfrags(fs, size);
1623 for (free = 0, i = 0; i < frags; i++)
1624 if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1625 free++;
1626 if (free != 0 && free != frags)
1627 panic("checkblk: partially free fragment");
1628 }
1629 brelse(bp);
1630 return (!free);
1631 }
1632 #endif /* XXXUBC */
1633 #endif /* DIAGNOSTIC */
1634
1635 /*
1636 * Free an inode.
1637 */
1638 int
1639 ffs_vfree(v)
1640 void *v;
1641 {
1642 struct vop_vfree_args /* {
1643 struct vnode *a_pvp;
1644 ino_t a_ino;
1645 int a_mode;
1646 } */ *ap = v;
1647
1648 if (DOINGSOFTDEP(ap->a_pvp)) {
1649 softdep_freefile(ap);
1650 return (0);
1651 }
1652 return (ffs_freefile(ap));
1653 }
1654
1655 /*
1656 * Do the actual free operation.
1657 * The specified inode is placed back in the free map.
1658 */
1659 int
1660 ffs_freefile(v)
1661 void *v;
1662 {
1663 struct vop_vfree_args /* {
1664 struct vnode *a_pvp;
1665 ino_t a_ino;
1666 int a_mode;
1667 } */ *ap = v;
1668 struct cg *cgp;
1669 struct inode *pip = VTOI(ap->a_pvp);
1670 struct fs *fs = pip->i_fs;
1671 ino_t ino = ap->a_ino;
1672 struct buf *bp;
1673 int error, cg;
1674 #ifdef FFS_EI
1675 const int needswap = UFS_FSNEEDSWAP(fs);
1676 #endif
1677
1678 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1679 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s",
1680 pip->i_dev, ino, fs->fs_fsmnt);
1681 cg = ino_to_cg(fs, ino);
1682 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1683 (int)fs->fs_cgsize, NOCRED, &bp);
1684 if (error) {
1685 brelse(bp);
1686 return (error);
1687 }
1688 cgp = (struct cg *)bp->b_data;
1689 if (!cg_chkmagic(cgp, needswap)) {
1690 brelse(bp);
1691 return (0);
1692 }
1693 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1694 ino %= fs->fs_ipg;
1695 if (isclr(cg_inosused(cgp, needswap), ino)) {
1696 printf("dev = 0x%x, ino = %d, fs = %s\n",
1697 pip->i_dev, ino, fs->fs_fsmnt);
1698 if (fs->fs_ronly == 0)
1699 panic("ifree: freeing free inode");
1700 }
1701 clrbit(cg_inosused(cgp, needswap), ino);
1702 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
1703 cgp->cg_irotor = ufs_rw32(ino, needswap);
1704 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1705 fs->fs_cstotal.cs_nifree++;
1706 fs->fs_cs(fs, cg).cs_nifree++;
1707 if ((ap->a_mode & IFMT) == IFDIR) {
1708 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1709 fs->fs_cstotal.cs_ndir--;
1710 fs->fs_cs(fs, cg).cs_ndir--;
1711 }
1712 fs->fs_fmod = 1;
1713 bdwrite(bp);
1714 return (0);
1715 }
1716
1717 /*
1718 * Find a block of the specified size in the specified cylinder group.
1719 *
1720 * It is a panic if a request is made to find a block if none are
1721 * available.
1722 */
1723 static daddr_t
1724 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1725 struct fs *fs;
1726 struct cg *cgp;
1727 daddr_t bpref;
1728 int allocsiz;
1729 {
1730 daddr_t bno;
1731 int start, len, loc, i;
1732 int blk, field, subfield, pos;
1733 int ostart, olen;
1734 #ifdef FFS_EI
1735 const int needswap = UFS_FSNEEDSWAP(fs);
1736 #endif
1737
1738 /*
1739 * find the fragment by searching through the free block
1740 * map for an appropriate bit pattern
1741 */
1742 if (bpref)
1743 start = dtogd(fs, bpref) / NBBY;
1744 else
1745 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1746 len = howmany(fs->fs_fpg, NBBY) - start;
1747 ostart = start;
1748 olen = len;
1749 loc = scanc((u_int)len,
1750 (const u_char *)&cg_blksfree(cgp, needswap)[start],
1751 (const u_char *)fragtbl[fs->fs_frag],
1752 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1753 if (loc == 0) {
1754 len = start + 1;
1755 start = 0;
1756 loc = scanc((u_int)len,
1757 (const u_char *)&cg_blksfree(cgp, needswap)[0],
1758 (const u_char *)fragtbl[fs->fs_frag],
1759 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1760 if (loc == 0) {
1761 printf("start = %d, len = %d, fs = %s\n",
1762 ostart, olen, fs->fs_fsmnt);
1763 printf("offset=%d %ld\n",
1764 ufs_rw32(cgp->cg_freeoff, needswap),
1765 (long)cg_blksfree(cgp, needswap) - (long)cgp);
1766 panic("ffs_alloccg: map corrupted");
1767 /* NOTREACHED */
1768 }
1769 }
1770 bno = (start + len - loc) * NBBY;
1771 cgp->cg_frotor = ufs_rw32(bno, needswap);
1772 /*
1773 * found the byte in the map
1774 * sift through the bits to find the selected frag
1775 */
1776 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1777 blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
1778 blk <<= 1;
1779 field = around[allocsiz];
1780 subfield = inside[allocsiz];
1781 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1782 if ((blk & field) == subfield)
1783 return (bno + pos);
1784 field <<= 1;
1785 subfield <<= 1;
1786 }
1787 }
1788 printf("bno = %" PRId64 ", fs = %s\n", bno, fs->fs_fsmnt);
1789 panic("ffs_alloccg: block not in map");
1790 /* return (-1); */
1791 }
1792
1793 /*
1794 * Update the cluster map because of an allocation or free.
1795 *
1796 * Cnt == 1 means free; cnt == -1 means allocating.
1797 */
1798 void
1799 ffs_clusteracct(fs, cgp, blkno, cnt)
1800 struct fs *fs;
1801 struct cg *cgp;
1802 daddr_t blkno;
1803 int cnt;
1804 {
1805 int32_t *sump;
1806 int32_t *lp;
1807 u_char *freemapp, *mapp;
1808 int i, start, end, forw, back, map, bit;
1809 #ifdef FFS_EI
1810 const int needswap = UFS_FSNEEDSWAP(fs);
1811 #endif
1812
1813 if (fs->fs_contigsumsize <= 0)
1814 return;
1815 freemapp = cg_clustersfree(cgp, needswap);
1816 sump = cg_clustersum(cgp, needswap);
1817 /*
1818 * Allocate or clear the actual block.
1819 */
1820 if (cnt > 0)
1821 setbit(freemapp, blkno);
1822 else
1823 clrbit(freemapp, blkno);
1824 /*
1825 * Find the size of the cluster going forward.
1826 */
1827 start = blkno + 1;
1828 end = start + fs->fs_contigsumsize;
1829 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
1830 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1831 mapp = &freemapp[start / NBBY];
1832 map = *mapp++;
1833 bit = 1 << (start % NBBY);
1834 for (i = start; i < end; i++) {
1835 if ((map & bit) == 0)
1836 break;
1837 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1838 bit <<= 1;
1839 } else {
1840 map = *mapp++;
1841 bit = 1;
1842 }
1843 }
1844 forw = i - start;
1845 /*
1846 * Find the size of the cluster going backward.
1847 */
1848 start = blkno - 1;
1849 end = start - fs->fs_contigsumsize;
1850 if (end < 0)
1851 end = -1;
1852 mapp = &freemapp[start / NBBY];
1853 map = *mapp--;
1854 bit = 1 << (start % NBBY);
1855 for (i = start; i > end; i--) {
1856 if ((map & bit) == 0)
1857 break;
1858 if ((i & (NBBY - 1)) != 0) {
1859 bit >>= 1;
1860 } else {
1861 map = *mapp--;
1862 bit = 1 << (NBBY - 1);
1863 }
1864 }
1865 back = start - i;
1866 /*
1867 * Account for old cluster and the possibly new forward and
1868 * back clusters.
1869 */
1870 i = back + forw + 1;
1871 if (i > fs->fs_contigsumsize)
1872 i = fs->fs_contigsumsize;
1873 ufs_add32(sump[i], cnt, needswap);
1874 if (back > 0)
1875 ufs_add32(sump[back], -cnt, needswap);
1876 if (forw > 0)
1877 ufs_add32(sump[forw], -cnt, needswap);
1878
1879 /*
1880 * Update cluster summary information.
1881 */
1882 lp = &sump[fs->fs_contigsumsize];
1883 for (i = fs->fs_contigsumsize; i > 0; i--)
1884 if (ufs_rw32(*lp--, needswap) > 0)
1885 break;
1886 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1887 }
1888
1889 /*
1890 * Fserr prints the name of a file system with an error diagnostic.
1891 *
1892 * The form of the error message is:
1893 * fs: error message
1894 */
1895 static void
1896 ffs_fserr(fs, uid, cp)
1897 struct fs *fs;
1898 u_int uid;
1899 char *cp;
1900 {
1901
1902 log(LOG_ERR, "uid %d comm %s on %s: %s\n",
1903 uid, curproc->p_comm, fs->fs_fsmnt, cp);
1904 }
1905