ffs_alloc.c revision 1.57 1 /* $NetBSD: ffs_alloc.c,v 1.57 2002/12/27 16:07:13 hannken 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.57 2002/12/27 16:07:13 hannken 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 ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
65 static ufs_daddr_t ffs_alloccgblk __P((struct inode *, struct buf *, ufs_daddr_t));
66 #ifdef XXXUBC
67 static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t, int));
68 #endif
69 static ino_t ffs_dirpref __P((struct inode *));
70 static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
71 static void ffs_fserr __P((struct fs *, u_int, char *));
72 static u_long ffs_hashalloc __P((struct inode *, int, long, int,
73 ufs_daddr_t (*)(struct inode *, int, ufs_daddr_t, int)));
74 static ufs_daddr_t ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
75 static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *,
76 ufs_daddr_t, int));
77 #if defined(DIAGNOSTIC) || defined(DEBUG)
78 #ifdef XXXUBC
79 static int ffs_checkblk __P((struct inode *, ufs_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 ufs_daddr_t lbn, bpref;
113 int size;
114 struct ucred *cred;
115 ufs_daddr_t *bnp;
116 {
117 struct fs *fs = ip->i_fs;
118 ufs_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 = (ufs_daddr_t)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 ufs_daddr_t lbprev;
200 ufs_daddr_t bpref;
201 int osize, nsize;
202 struct ucred *cred;
203 struct buf **bpp;
204 ufs_daddr_t *blknop;
205 {
206 struct fs *fs = ip->i_fs;
207 struct buf *bp;
208 int cg, request, error;
209 ufs_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 if ((bprev = ufs_rw32(ip->i_ffs_db[lbprev], UFS_FSNEEDSWAP(fs))) == 0) {
244 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
245 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
246 panic("ffs_realloccg: bad bprev");
247 }
248 /*
249 * Allocate the extra space in the buffer.
250 */
251 if (bpp != NULL &&
252 (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0) {
253 brelse(bp);
254 return (error);
255 }
256 #ifdef QUOTA
257 if ((error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) != 0) {
258 if (bpp != NULL) {
259 brelse(bp);
260 }
261 return (error);
262 }
263 #endif
264 /*
265 * Check for extension in the existing location.
266 */
267 cg = dtog(fs, bprev);
268 if ((bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) != 0) {
269 ip->i_ffs_blocks += btodb(nsize - osize);
270 ip->i_flag |= IN_CHANGE | IN_UPDATE;
271
272 if (bpp != NULL) {
273 if (bp->b_blkno != fsbtodb(fs, bno))
274 panic("bad blockno");
275 allocbuf(bp, nsize);
276 bp->b_flags |= B_DONE;
277 memset(bp->b_data + osize, 0, nsize - osize);
278 *bpp = bp;
279 }
280 if (blknop != NULL) {
281 *blknop = bno;
282 }
283 return (0);
284 }
285 /*
286 * Allocate a new disk location.
287 */
288 if (bpref >= fs->fs_size)
289 bpref = 0;
290 switch ((int)fs->fs_optim) {
291 case FS_OPTSPACE:
292 /*
293 * Allocate an exact sized fragment. Although this makes
294 * best use of space, we will waste time relocating it if
295 * the file continues to grow. If the fragmentation is
296 * less than half of the minimum free reserve, we choose
297 * to begin optimizing for time.
298 */
299 request = nsize;
300 if (fs->fs_minfree < 5 ||
301 fs->fs_cstotal.cs_nffree >
302 fs->fs_dsize * fs->fs_minfree / (2 * 100))
303 break;
304
305 if (ffs_log_changeopt) {
306 log(LOG_NOTICE,
307 "%s: optimization changed from SPACE to TIME\n",
308 fs->fs_fsmnt);
309 }
310
311 fs->fs_optim = FS_OPTTIME;
312 break;
313 case FS_OPTTIME:
314 /*
315 * At this point we have discovered a file that is trying to
316 * grow a small fragment to a larger fragment. To save time,
317 * we allocate a full sized block, then free the unused portion.
318 * If the file continues to grow, the `ffs_fragextend' call
319 * above will be able to grow it in place without further
320 * copying. If aberrant programs cause disk fragmentation to
321 * grow within 2% of the free reserve, we choose to begin
322 * optimizing for space.
323 */
324 request = fs->fs_bsize;
325 if (fs->fs_cstotal.cs_nffree <
326 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
327 break;
328
329 if (ffs_log_changeopt) {
330 log(LOG_NOTICE,
331 "%s: optimization changed from TIME to SPACE\n",
332 fs->fs_fsmnt);
333 }
334
335 fs->fs_optim = FS_OPTSPACE;
336 break;
337 default:
338 printf("dev = 0x%x, optim = %d, fs = %s\n",
339 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
340 panic("ffs_realloccg: bad optim");
341 /* NOTREACHED */
342 }
343 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
344 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 ufs_daddr_t *bap, *sbap, *ebap = NULL;
422 struct cluster_save *buflist;
423 ufs_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];
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 = (ufs_daddr_t *)sbp->b_data;
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 = (ufs_daddr_t *)ebp->b_data;
499 }
500 /*
501 * Search the block map looking for an allocation of the desired size.
502 */
503 if ((newblk = (ufs_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 ufs_daddr_t ba;
521
522 if (i == ssize) {
523 bap = ebap;
524 soff = -i;
525 }
526 ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
527 #ifdef DIAGNOSTIC
528 if (!ffs_checkblk(ip,
529 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
530 panic("ffs_reallocblks: unallocated block 2");
531 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
532 panic("ffs_reallocblks: alloc mismatch");
533 #endif
534 #ifdef DEBUG
535 if (prtrealloc)
536 printf(" %d,", ba);
537 #endif
538 if (DOINGSOFTDEP(vp)) {
539 if (sbap == &ip->i_ffs_db[0] && i < ssize)
540 softdep_setup_allocdirect(ip, start_lbn + i,
541 blkno, ba, fs->fs_bsize, fs->fs_bsize,
542 buflist->bs_children[i]);
543 else
544 softdep_setup_allocindir_page(ip, start_lbn + i,
545 i < ssize ? sbp : ebp, soff + i, blkno,
546 ba, buflist->bs_children[i]);
547 }
548 *bap++ = ufs_rw32(blkno, UFS_FSNEEDSWAP(fs));
549 }
550 /*
551 * Next we must write out the modified inode and indirect blocks.
552 * For strict correctness, the writes should be synchronous since
553 * the old block values may have been written to disk. In practise
554 * they are almost never written, but if we are concerned about
555 * strict correctness, the `doasyncfree' flag should be set to zero.
556 *
557 * The test on `doasyncfree' should be changed to test a flag
558 * that shows whether the associated buffers and inodes have
559 * been written. The flag should be set when the cluster is
560 * started and cleared whenever the buffer or inode is flushed.
561 * We can then check below to see if it is set, and do the
562 * synchronous write only when it has been cleared.
563 */
564 if (sbap != &ip->i_ffs_db[0]) {
565 if (doasyncfree)
566 bdwrite(sbp);
567 else
568 bwrite(sbp);
569 } else {
570 ip->i_flag |= IN_CHANGE | IN_UPDATE;
571 if (!doasyncfree)
572 VOP_UPDATE(vp, NULL, NULL, 1);
573 }
574 if (ssize < len) {
575 if (doasyncfree)
576 bdwrite(ebp);
577 else
578 bwrite(ebp);
579 }
580 /*
581 * Last, free the old blocks and assign the new blocks to the buffers.
582 */
583 #ifdef DEBUG
584 if (prtrealloc)
585 printf("\n\tnew:");
586 #endif
587 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
588 if (!DOINGSOFTDEP(vp))
589 ffs_blkfree(ip,
590 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
591 fs->fs_bsize);
592 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
593 #ifdef DEBUG
594 if (!ffs_checkblk(ip,
595 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
596 panic("ffs_reallocblks: unallocated block 3");
597 if (prtrealloc)
598 printf(" %d,", blkno);
599 #endif
600 }
601 #ifdef DEBUG
602 if (prtrealloc) {
603 prtrealloc--;
604 printf("\n");
605 }
606 #endif
607 return (0);
608
609 fail:
610 if (ssize < len)
611 brelse(ebp);
612 if (sbap != &ip->i_ffs_db[0])
613 brelse(sbp);
614 return (ENOSPC);
615 #endif /* XXXUBC */
616 }
617
618 /*
619 * Allocate an inode in the file system.
620 *
621 * If allocating a directory, use ffs_dirpref to select the inode.
622 * If allocating in a directory, the following hierarchy is followed:
623 * 1) allocate the preferred inode.
624 * 2) allocate an inode in the same cylinder group.
625 * 3) quadradically rehash into other cylinder groups, until an
626 * available inode is located.
627 * If no inode preference is given the following hierarchy is used
628 * to allocate an inode:
629 * 1) allocate an inode in cylinder group 0.
630 * 2) quadradically rehash into other cylinder groups, until an
631 * available inode is located.
632 */
633 int
634 ffs_valloc(v)
635 void *v;
636 {
637 struct vop_valloc_args /* {
638 struct vnode *a_pvp;
639 int a_mode;
640 struct ucred *a_cred;
641 struct vnode **a_vpp;
642 } */ *ap = v;
643 struct vnode *pvp = ap->a_pvp;
644 struct inode *pip;
645 struct fs *fs;
646 struct inode *ip;
647 mode_t mode = ap->a_mode;
648 ino_t ino, ipref;
649 int cg, error;
650
651 *ap->a_vpp = NULL;
652 pip = VTOI(pvp);
653 fs = pip->i_fs;
654 if (fs->fs_cstotal.cs_nifree == 0)
655 goto noinodes;
656
657 if ((mode & IFMT) == IFDIR)
658 ipref = ffs_dirpref(pip);
659 else
660 ipref = pip->i_number;
661 if (ipref >= fs->fs_ncg * fs->fs_ipg)
662 ipref = 0;
663 cg = ino_to_cg(fs, ipref);
664 /*
665 * Track number of dirs created one after another
666 * in a same cg without intervening by files.
667 */
668 if ((mode & IFMT) == IFDIR) {
669 if (fs->fs_contigdirs[cg] < 65535)
670 fs->fs_contigdirs[cg]++;
671 } else {
672 if (fs->fs_contigdirs[cg] > 0)
673 fs->fs_contigdirs[cg]--;
674 }
675 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
676 if (ino == 0)
677 goto noinodes;
678 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
679 if (error) {
680 VOP_VFREE(pvp, ino, mode);
681 return (error);
682 }
683 ip = VTOI(*ap->a_vpp);
684 if (ip->i_ffs_mode) {
685 printf("mode = 0%o, inum = %d, fs = %s\n",
686 ip->i_ffs_mode, ip->i_number, fs->fs_fsmnt);
687 panic("ffs_valloc: dup alloc");
688 }
689 if (ip->i_ffs_blocks) { /* XXX */
690 printf("free inode %s/%d had %d blocks\n",
691 fs->fs_fsmnt, ino, ip->i_ffs_blocks);
692 ip->i_ffs_blocks = 0;
693 }
694 ip->i_flag &= ~IN_SPACECOUNTED;
695 ip->i_ffs_flags = 0;
696 /*
697 * Set up a new generation number for this inode.
698 */
699 ip->i_ffs_gen++;
700 return (0);
701 noinodes:
702 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
703 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
704 return (ENOSPC);
705 }
706
707 /*
708 * Find a cylinder group in which to place a directory.
709 *
710 * The policy implemented by this algorithm is to allocate a
711 * directory inode in the same cylinder group as its parent
712 * directory, but also to reserve space for its files inodes
713 * and data. Restrict the number of directories which may be
714 * allocated one after another in the same cylinder group
715 * without intervening allocation of files.
716 *
717 * If we allocate a first level directory then force allocation
718 * in another cylinder group.
719 */
720 static ino_t
721 ffs_dirpref(pip)
722 struct inode *pip;
723 {
724 register struct fs *fs;
725 int cg, prefcg, dirsize, cgsize;
726 int avgifree, avgbfree, avgndir, curdirsize;
727 int minifree, minbfree, maxndir;
728 int mincg, minndir;
729 int maxcontigdirs;
730
731 fs = pip->i_fs;
732
733 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
734 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
735 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
736
737 /*
738 * Force allocation in another cg if creating a first level dir.
739 */
740 if (ITOV(pip)->v_flag & VROOT) {
741 prefcg = random() % fs->fs_ncg;
742 mincg = prefcg;
743 minndir = fs->fs_ipg;
744 for (cg = prefcg; cg < fs->fs_ncg; cg++)
745 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
746 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
747 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
748 mincg = cg;
749 minndir = fs->fs_cs(fs, cg).cs_ndir;
750 }
751 for (cg = 0; cg < prefcg; cg++)
752 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
753 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
754 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
755 mincg = cg;
756 minndir = fs->fs_cs(fs, cg).cs_ndir;
757 }
758 return ((ino_t)(fs->fs_ipg * mincg));
759 }
760
761 /*
762 * Count various limits which used for
763 * optimal allocation of a directory inode.
764 */
765 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
766 minifree = avgifree - fs->fs_ipg / 4;
767 if (minifree < 0)
768 minifree = 0;
769 minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4;
770 if (minbfree < 0)
771 minbfree = 0;
772 cgsize = fs->fs_fsize * fs->fs_fpg;
773 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
774 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
775 if (dirsize < curdirsize)
776 dirsize = curdirsize;
777 maxcontigdirs = min(cgsize / dirsize, 255);
778 if (fs->fs_avgfpdir > 0)
779 maxcontigdirs = min(maxcontigdirs,
780 fs->fs_ipg / fs->fs_avgfpdir);
781 if (maxcontigdirs == 0)
782 maxcontigdirs = 1;
783
784 /*
785 * Limit number of dirs in one cg and reserve space for
786 * regular files, but only if we have no deficit in
787 * inodes or space.
788 */
789 prefcg = ino_to_cg(fs, pip->i_number);
790 for (cg = prefcg; cg < fs->fs_ncg; cg++)
791 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
792 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
793 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
794 if (fs->fs_contigdirs[cg] < maxcontigdirs)
795 return ((ino_t)(fs->fs_ipg * cg));
796 }
797 for (cg = 0; cg < prefcg; cg++)
798 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
799 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
800 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
801 if (fs->fs_contigdirs[cg] < maxcontigdirs)
802 return ((ino_t)(fs->fs_ipg * cg));
803 }
804 /*
805 * This is a backstop when we are deficient in space.
806 */
807 for (cg = prefcg; cg < fs->fs_ncg; cg++)
808 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
809 return ((ino_t)(fs->fs_ipg * cg));
810 for (cg = 0; cg < prefcg; cg++)
811 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
812 break;
813 return ((ino_t)(fs->fs_ipg * cg));
814 }
815
816 /*
817 * Select the desired position for the next block in a file. The file is
818 * logically divided into sections. The first section is composed of the
819 * direct blocks. Each additional section contains fs_maxbpg blocks.
820 *
821 * If no blocks have been allocated in the first section, the policy is to
822 * request a block in the same cylinder group as the inode that describes
823 * the file. If no blocks have been allocated in any other section, the
824 * policy is to place the section in a cylinder group with a greater than
825 * average number of free blocks. An appropriate cylinder group is found
826 * by using a rotor that sweeps the cylinder groups. When a new group of
827 * blocks is needed, the sweep begins in the cylinder group following the
828 * cylinder group from which the previous allocation was made. The sweep
829 * continues until a cylinder group with greater than the average number
830 * of free blocks is found. If the allocation is for the first block in an
831 * indirect block, the information on the previous allocation is unavailable;
832 * here a best guess is made based upon the logical block number being
833 * allocated.
834 *
835 * If a section is already partially allocated, the policy is to
836 * contiguously allocate fs_maxcontig blocks. The end of one of these
837 * contiguous blocks and the beginning of the next is physically separated
838 * so that the disk head will be in transit between them for at least
839 * fs_rotdelay milliseconds. This is to allow time for the processor to
840 * schedule another I/O transfer.
841 */
842 ufs_daddr_t
843 ffs_blkpref(ip, lbn, indx, bap)
844 struct inode *ip;
845 ufs_daddr_t lbn;
846 int indx;
847 ufs_daddr_t *bap;
848 {
849 struct fs *fs;
850 int cg;
851 int avgbfree, startcg;
852 ufs_daddr_t nextblk;
853
854 fs = ip->i_fs;
855 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
856 if (lbn < NDADDR + NINDIR(fs)) {
857 cg = ino_to_cg(fs, ip->i_number);
858 return (fs->fs_fpg * cg + fs->fs_frag);
859 }
860 /*
861 * Find a cylinder with greater than average number of
862 * unused data blocks.
863 */
864 if (indx == 0 || bap[indx - 1] == 0)
865 startcg =
866 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
867 else
868 startcg = dtog(fs,
869 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
870 startcg %= fs->fs_ncg;
871 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
872 for (cg = startcg; cg < fs->fs_ncg; cg++)
873 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
874 return (fs->fs_fpg * cg + fs->fs_frag);
875 }
876 for (cg = 0; cg < startcg; cg++)
877 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
878 return (fs->fs_fpg * cg + fs->fs_frag);
879 }
880 return (0);
881 }
882 /*
883 * One or more previous blocks have been laid out. If less
884 * than fs_maxcontig previous blocks are contiguous, the
885 * next block is requested contiguously, otherwise it is
886 * requested rotationally delayed by fs_rotdelay milliseconds.
887 */
888 nextblk = ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
889 if (indx < fs->fs_maxcontig ||
890 ufs_rw32(bap[indx - fs->fs_maxcontig], UFS_FSNEEDSWAP(fs)) +
891 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
892 return (nextblk);
893 if (fs->fs_rotdelay != 0)
894 /*
895 * Here we convert ms of delay to frags as:
896 * (frags) = (ms) * (rev/sec) * (sect/rev) /
897 * ((sect/frag) * (ms/sec))
898 * then round up to the next block.
899 */
900 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
901 (NSPF(fs) * 1000), fs->fs_frag);
902 return (nextblk);
903 }
904
905 /*
906 * Implement the cylinder overflow algorithm.
907 *
908 * The policy implemented by this algorithm is:
909 * 1) allocate the block in its requested cylinder group.
910 * 2) quadradically rehash on the cylinder group number.
911 * 3) brute force search for a free block.
912 */
913 /*VARARGS5*/
914 static u_long
915 ffs_hashalloc(ip, cg, pref, size, allocator)
916 struct inode *ip;
917 int cg;
918 long pref;
919 int size; /* size for data blocks, mode for inodes */
920 ufs_daddr_t (*allocator) __P((struct inode *, int, ufs_daddr_t, int));
921 {
922 struct fs *fs;
923 long result;
924 int i, icg = cg;
925
926 fs = ip->i_fs;
927 /*
928 * 1: preferred cylinder group
929 */
930 result = (*allocator)(ip, cg, pref, size);
931 if (result)
932 return (result);
933 /*
934 * 2: quadratic rehash
935 */
936 for (i = 1; i < fs->fs_ncg; i *= 2) {
937 cg += i;
938 if (cg >= fs->fs_ncg)
939 cg -= fs->fs_ncg;
940 result = (*allocator)(ip, cg, 0, size);
941 if (result)
942 return (result);
943 }
944 /*
945 * 3: brute force search
946 * Note that we start at i == 2, since 0 was checked initially,
947 * and 1 is always checked in the quadratic rehash.
948 */
949 cg = (icg + 2) % fs->fs_ncg;
950 for (i = 2; i < fs->fs_ncg; i++) {
951 result = (*allocator)(ip, cg, 0, size);
952 if (result)
953 return (result);
954 cg++;
955 if (cg == fs->fs_ncg)
956 cg = 0;
957 }
958 return (0);
959 }
960
961 /*
962 * Determine whether a fragment can be extended.
963 *
964 * Check to see if the necessary fragments are available, and
965 * if they are, allocate them.
966 */
967 static ufs_daddr_t
968 ffs_fragextend(ip, cg, bprev, osize, nsize)
969 struct inode *ip;
970 int cg;
971 long bprev;
972 int osize, nsize;
973 {
974 struct fs *fs;
975 struct cg *cgp;
976 struct buf *bp;
977 long bno;
978 int frags, bbase;
979 int i, error;
980
981 fs = ip->i_fs;
982 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
983 return (0);
984 frags = numfrags(fs, nsize);
985 bbase = fragnum(fs, bprev);
986 if (bbase > fragnum(fs, (bprev + frags - 1))) {
987 /* cannot extend across a block boundary */
988 return (0);
989 }
990 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
991 (int)fs->fs_cgsize, NOCRED, &bp);
992 if (error) {
993 brelse(bp);
994 return (0);
995 }
996 cgp = (struct cg *)bp->b_data;
997 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
998 brelse(bp);
999 return (0);
1000 }
1001 cgp->cg_time = ufs_rw32(time.tv_sec, UFS_FSNEEDSWAP(fs));
1002 bno = dtogd(fs, bprev);
1003 for (i = numfrags(fs, osize); i < frags; i++)
1004 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i)) {
1005 brelse(bp);
1006 return (0);
1007 }
1008 /*
1009 * the current fragment can be extended
1010 * deduct the count on fragment being extended into
1011 * increase the count on the remaining fragment (if any)
1012 * allocate the extended piece
1013 */
1014 for (i = frags; i < fs->fs_frag - bbase; i++)
1015 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1016 break;
1017 ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1018 if (i != frags)
1019 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1020 for (i = numfrags(fs, osize); i < frags; i++) {
1021 clrbit(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i);
1022 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1023 fs->fs_cstotal.cs_nffree--;
1024 fs->fs_cs(fs, cg).cs_nffree--;
1025 }
1026 fs->fs_fmod = 1;
1027 if (DOINGSOFTDEP(ITOV(ip)))
1028 softdep_setup_blkmapdep(bp, fs, bprev);
1029 bdwrite(bp);
1030 return (bprev);
1031 }
1032
1033 /*
1034 * Determine whether a block can be allocated.
1035 *
1036 * Check to see if a block of the appropriate size is available,
1037 * and if it is, allocate it.
1038 */
1039 static ufs_daddr_t
1040 ffs_alloccg(ip, cg, bpref, size)
1041 struct inode *ip;
1042 int cg;
1043 ufs_daddr_t bpref;
1044 int size;
1045 {
1046 struct cg *cgp;
1047 struct buf *bp;
1048 ufs_daddr_t bno, blkno;
1049 int error, frags, allocsiz, i;
1050 struct fs *fs = ip->i_fs;
1051 #ifdef FFS_EI
1052 const int needswap = UFS_FSNEEDSWAP(fs);
1053 #endif
1054
1055 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1056 return (0);
1057 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1058 (int)fs->fs_cgsize, NOCRED, &bp);
1059 if (error) {
1060 brelse(bp);
1061 return (0);
1062 }
1063 cgp = (struct cg *)bp->b_data;
1064 if (!cg_chkmagic(cgp, needswap) ||
1065 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1066 brelse(bp);
1067 return (0);
1068 }
1069 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1070 if (size == fs->fs_bsize) {
1071 bno = ffs_alloccgblk(ip, bp, bpref);
1072 bdwrite(bp);
1073 return (bno);
1074 }
1075 /*
1076 * check to see if any fragments are already available
1077 * allocsiz is the size which will be allocated, hacking
1078 * it down to a smaller size if necessary
1079 */
1080 frags = numfrags(fs, size);
1081 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1082 if (cgp->cg_frsum[allocsiz] != 0)
1083 break;
1084 if (allocsiz == fs->fs_frag) {
1085 /*
1086 * no fragments were available, so a block will be
1087 * allocated, and hacked up
1088 */
1089 if (cgp->cg_cs.cs_nbfree == 0) {
1090 brelse(bp);
1091 return (0);
1092 }
1093 bno = ffs_alloccgblk(ip, bp, bpref);
1094 bpref = dtogd(fs, bno);
1095 for (i = frags; i < fs->fs_frag; i++)
1096 setbit(cg_blksfree(cgp, needswap), bpref + i);
1097 i = fs->fs_frag - frags;
1098 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1099 fs->fs_cstotal.cs_nffree += i;
1100 fs->fs_cs(fs, cg).cs_nffree += i;
1101 fs->fs_fmod = 1;
1102 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1103 bdwrite(bp);
1104 return (bno);
1105 }
1106 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1107 #if 0
1108 /*
1109 * XXX fvdl mapsearch will panic, and never return -1
1110 * also: returning NULL as ufs_daddr_t ?
1111 */
1112 if (bno < 0) {
1113 brelse(bp);
1114 return (0);
1115 }
1116 #endif
1117 for (i = 0; i < frags; i++)
1118 clrbit(cg_blksfree(cgp, needswap), bno + i);
1119 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1120 fs->fs_cstotal.cs_nffree -= frags;
1121 fs->fs_cs(fs, cg).cs_nffree -= frags;
1122 fs->fs_fmod = 1;
1123 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1124 if (frags != allocsiz)
1125 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1126 blkno = cg * fs->fs_fpg + bno;
1127 if (DOINGSOFTDEP(ITOV(ip)))
1128 softdep_setup_blkmapdep(bp, fs, blkno);
1129 bdwrite(bp);
1130 return blkno;
1131 }
1132
1133 /*
1134 * Allocate a block in a cylinder group.
1135 *
1136 * This algorithm implements the following policy:
1137 * 1) allocate the requested block.
1138 * 2) allocate a rotationally optimal block in the same cylinder.
1139 * 3) allocate the next available block on the block rotor for the
1140 * specified cylinder group.
1141 * Note that this routine only allocates fs_bsize blocks; these
1142 * blocks may be fragmented by the routine that allocates them.
1143 */
1144 static ufs_daddr_t
1145 ffs_alloccgblk(ip, bp, bpref)
1146 struct inode *ip;
1147 struct buf *bp;
1148 ufs_daddr_t bpref;
1149 {
1150 struct cg *cgp;
1151 ufs_daddr_t bno, blkno;
1152 int cylno, pos, delta;
1153 short *cylbp;
1154 int i;
1155 struct fs *fs = ip->i_fs;
1156 #ifdef FFS_EI
1157 const int needswap = UFS_FSNEEDSWAP(fs);
1158 #endif
1159
1160 cgp = (struct cg *)bp->b_data;
1161 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1162 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1163 goto norot;
1164 }
1165 bpref = blknum(fs, bpref);
1166 bpref = dtogd(fs, bpref);
1167 /*
1168 * if the requested block is available, use it
1169 */
1170 if (ffs_isblock(fs, cg_blksfree(cgp, needswap),
1171 fragstoblks(fs, bpref))) {
1172 bno = bpref;
1173 goto gotit;
1174 }
1175 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1176 /*
1177 * Block layout information is not available.
1178 * Leaving bpref unchanged means we take the
1179 * next available free block following the one
1180 * we just allocated. Hopefully this will at
1181 * least hit a track cache on drives of unknown
1182 * geometry (e.g. SCSI).
1183 */
1184 goto norot;
1185 }
1186 /*
1187 * check for a block available on the same cylinder
1188 */
1189 cylno = cbtocylno(fs, bpref);
1190 if (cg_blktot(cgp, needswap)[cylno] == 0)
1191 goto norot;
1192 /*
1193 * check the summary information to see if a block is
1194 * available in the requested cylinder starting at the
1195 * requested rotational position and proceeding around.
1196 */
1197 cylbp = cg_blks(fs, cgp, cylno, needswap);
1198 pos = cbtorpos(fs, bpref);
1199 for (i = pos; i < fs->fs_nrpos; i++)
1200 if (ufs_rw16(cylbp[i], needswap) > 0)
1201 break;
1202 if (i == fs->fs_nrpos)
1203 for (i = 0; i < pos; i++)
1204 if (ufs_rw16(cylbp[i], needswap) > 0)
1205 break;
1206 if (ufs_rw16(cylbp[i], needswap) > 0) {
1207 /*
1208 * found a rotational position, now find the actual
1209 * block. A panic if none is actually there.
1210 */
1211 pos = cylno % fs->fs_cpc;
1212 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1213 if (fs_postbl(fs, pos)[i] == -1) {
1214 printf("pos = %d, i = %d, fs = %s\n",
1215 pos, i, fs->fs_fsmnt);
1216 panic("ffs_alloccgblk: cyl groups corrupted");
1217 }
1218 for (i = fs_postbl(fs, pos)[i];; ) {
1219 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), bno + i)) {
1220 bno = blkstofrags(fs, (bno + i));
1221 goto gotit;
1222 }
1223 delta = fs_rotbl(fs)[i];
1224 if (delta <= 0 ||
1225 delta + i > fragstoblks(fs, fs->fs_fpg))
1226 break;
1227 i += delta;
1228 }
1229 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1230 panic("ffs_alloccgblk: can't find blk in cyl");
1231 }
1232 norot:
1233 /*
1234 * no blocks in the requested cylinder, so take next
1235 * available one in this cylinder group.
1236 */
1237 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1238 if (bno < 0)
1239 return (0);
1240 cgp->cg_rotor = ufs_rw32(bno, needswap);
1241 gotit:
1242 blkno = fragstoblks(fs, bno);
1243 ffs_clrblock(fs, cg_blksfree(cgp, needswap), (long)blkno);
1244 ffs_clusteracct(fs, cgp, blkno, -1);
1245 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1246 fs->fs_cstotal.cs_nbfree--;
1247 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1248 cylno = cbtocylno(fs, bno);
1249 ufs_add16(cg_blks(fs, cgp, cylno, needswap)[cbtorpos(fs, bno)], -1,
1250 needswap);
1251 ufs_add32(cg_blktot(cgp, needswap)[cylno], -1, needswap);
1252 fs->fs_fmod = 1;
1253 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
1254 if (DOINGSOFTDEP(ITOV(ip)))
1255 softdep_setup_blkmapdep(bp, fs, blkno);
1256 return (blkno);
1257 }
1258
1259 #ifdef XXXUBC
1260 /*
1261 * Determine whether a cluster can be allocated.
1262 *
1263 * We do not currently check for optimal rotational layout if there
1264 * are multiple choices in the same cylinder group. Instead we just
1265 * take the first one that we find following bpref.
1266 */
1267 static ufs_daddr_t
1268 ffs_clusteralloc(ip, cg, bpref, len)
1269 struct inode *ip;
1270 int cg;
1271 ufs_daddr_t bpref;
1272 int len;
1273 {
1274 struct fs *fs;
1275 struct cg *cgp;
1276 struct buf *bp;
1277 int i, got, run, bno, bit, map;
1278 u_char *mapp;
1279 int32_t *lp;
1280
1281 fs = ip->i_fs;
1282 if (fs->fs_maxcluster[cg] < len)
1283 return (0);
1284 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1285 NOCRED, &bp))
1286 goto fail;
1287 cgp = (struct cg *)bp->b_data;
1288 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1289 goto fail;
1290 /*
1291 * Check to see if a cluster of the needed size (or bigger) is
1292 * available in this cylinder group.
1293 */
1294 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1295 for (i = len; i <= fs->fs_contigsumsize; i++)
1296 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1297 break;
1298 if (i > fs->fs_contigsumsize) {
1299 /*
1300 * This is the first time looking for a cluster in this
1301 * cylinder group. Update the cluster summary information
1302 * to reflect the true maximum sized cluster so that
1303 * future cluster allocation requests can avoid reading
1304 * the cylinder group map only to find no clusters.
1305 */
1306 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1307 for (i = len - 1; i > 0; i--)
1308 if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1309 break;
1310 fs->fs_maxcluster[cg] = i;
1311 goto fail;
1312 }
1313 /*
1314 * Search the cluster map to find a big enough cluster.
1315 * We take the first one that we find, even if it is larger
1316 * than we need as we prefer to get one close to the previous
1317 * block allocation. We do not search before the current
1318 * preference point as we do not want to allocate a block
1319 * that is allocated before the previous one (as we will
1320 * then have to wait for another pass of the elevator
1321 * algorithm before it will be read). We prefer to fail and
1322 * be recalled to try an allocation in the next cylinder group.
1323 */
1324 if (dtog(fs, bpref) != cg)
1325 bpref = 0;
1326 else
1327 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1328 mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1329 map = *mapp++;
1330 bit = 1 << (bpref % NBBY);
1331 for (run = 0, got = bpref;
1332 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1333 if ((map & bit) == 0) {
1334 run = 0;
1335 } else {
1336 run++;
1337 if (run == len)
1338 break;
1339 }
1340 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1341 bit <<= 1;
1342 } else {
1343 map = *mapp++;
1344 bit = 1;
1345 }
1346 }
1347 if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1348 goto fail;
1349 /*
1350 * Allocate the cluster that we have found.
1351 */
1352 #ifdef DIAGNOSTIC
1353 for (i = 1; i <= len; i++)
1354 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1355 got - run + i))
1356 panic("ffs_clusteralloc: map mismatch");
1357 #endif
1358 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1359 if (dtog(fs, bno) != cg)
1360 panic("ffs_clusteralloc: allocated out of group");
1361 len = blkstofrags(fs, len);
1362 for (i = 0; i < len; i += fs->fs_frag)
1363 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1364 panic("ffs_clusteralloc: lost block");
1365 bdwrite(bp);
1366 return (bno);
1367
1368 fail:
1369 brelse(bp);
1370 return (0);
1371 }
1372 #endif /* XXXUBC */
1373
1374 /*
1375 * Determine whether an inode can be allocated.
1376 *
1377 * Check to see if an inode is available, and if it is,
1378 * allocate it using the following policy:
1379 * 1) allocate the requested inode.
1380 * 2) allocate the next available inode after the requested
1381 * inode in the specified cylinder group.
1382 */
1383 static ufs_daddr_t
1384 ffs_nodealloccg(ip, cg, ipref, mode)
1385 struct inode *ip;
1386 int cg;
1387 ufs_daddr_t ipref;
1388 int mode;
1389 {
1390 struct cg *cgp;
1391 struct buf *bp;
1392 int error, start, len, loc, map, i;
1393 struct fs *fs = ip->i_fs;
1394 #ifdef FFS_EI
1395 const int needswap = UFS_FSNEEDSWAP(fs);
1396 #endif
1397
1398 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1399 return (0);
1400 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1401 (int)fs->fs_cgsize, NOCRED, &bp);
1402 if (error) {
1403 brelse(bp);
1404 return (0);
1405 }
1406 cgp = (struct cg *)bp->b_data;
1407 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
1408 brelse(bp);
1409 return (0);
1410 }
1411 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1412 if (ipref) {
1413 ipref %= fs->fs_ipg;
1414 if (isclr(cg_inosused(cgp, needswap), ipref))
1415 goto gotit;
1416 }
1417 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1418 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1419 NBBY);
1420 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[start]);
1421 if (loc == 0) {
1422 len = start + 1;
1423 start = 0;
1424 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[0]);
1425 if (loc == 0) {
1426 printf("cg = %d, irotor = %d, fs = %s\n",
1427 cg, ufs_rw32(cgp->cg_irotor, needswap),
1428 fs->fs_fsmnt);
1429 panic("ffs_nodealloccg: map corrupted");
1430 /* NOTREACHED */
1431 }
1432 }
1433 i = start + len - loc;
1434 map = cg_inosused(cgp, needswap)[i];
1435 ipref = i * NBBY;
1436 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1437 if ((map & i) == 0) {
1438 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1439 goto gotit;
1440 }
1441 }
1442 printf("fs = %s\n", fs->fs_fsmnt);
1443 panic("ffs_nodealloccg: block not in map");
1444 /* NOTREACHED */
1445 gotit:
1446 if (DOINGSOFTDEP(ITOV(ip)))
1447 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1448 setbit(cg_inosused(cgp, needswap), ipref);
1449 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1450 fs->fs_cstotal.cs_nifree--;
1451 fs->fs_cs(fs, cg).cs_nifree--;
1452 fs->fs_fmod = 1;
1453 if ((mode & IFMT) == IFDIR) {
1454 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1455 fs->fs_cstotal.cs_ndir++;
1456 fs->fs_cs(fs, cg).cs_ndir++;
1457 }
1458 bdwrite(bp);
1459 return (cg * fs->fs_ipg + ipref);
1460 }
1461
1462 /*
1463 * Free a block or fragment.
1464 *
1465 * The specified block or fragment is placed back in the
1466 * free map. If a fragment is deallocated, a possible
1467 * block reassembly is checked.
1468 */
1469 void
1470 ffs_blkfree(ip, bno, size)
1471 struct inode *ip;
1472 ufs_daddr_t bno;
1473 long size;
1474 {
1475 struct cg *cgp;
1476 struct buf *bp;
1477 ufs_daddr_t blkno;
1478 int i, error, cg, blk, frags, bbase;
1479 struct fs *fs = ip->i_fs;
1480 const int needswap = UFS_FSNEEDSWAP(fs);
1481
1482 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1483 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1484 printf("dev = 0x%x, bno = %u bsize = %d, size = %ld, fs = %s\n",
1485 ip->i_dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1486 panic("blkfree: bad size");
1487 }
1488 cg = dtog(fs, bno);
1489 if ((u_int)bno >= fs->fs_size) {
1490 printf("bad block %d, ino %d\n", bno, ip->i_number);
1491 ffs_fserr(fs, ip->i_ffs_uid, "bad block");
1492 return;
1493 }
1494 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1495 (int)fs->fs_cgsize, NOCRED, &bp);
1496 if (error) {
1497 brelse(bp);
1498 return;
1499 }
1500 cgp = (struct cg *)bp->b_data;
1501 if (!cg_chkmagic(cgp, needswap)) {
1502 brelse(bp);
1503 return;
1504 }
1505 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1506 bno = dtogd(fs, bno);
1507 if (size == fs->fs_bsize) {
1508 blkno = fragstoblks(fs, bno);
1509 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1510 printf("dev = 0x%x, block = %d, fs = %s\n",
1511 ip->i_dev, bno, fs->fs_fsmnt);
1512 panic("blkfree: freeing free block");
1513 }
1514 ffs_setblock(fs, cg_blksfree(cgp, needswap), blkno);
1515 ffs_clusteracct(fs, cgp, blkno, 1);
1516 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1517 fs->fs_cstotal.cs_nbfree++;
1518 fs->fs_cs(fs, cg).cs_nbfree++;
1519 i = cbtocylno(fs, bno);
1520 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bno)], 1,
1521 needswap);
1522 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1523 } else {
1524 bbase = bno - fragnum(fs, bno);
1525 /*
1526 * decrement the counts associated with the old frags
1527 */
1528 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1529 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1530 /*
1531 * deallocate the fragment
1532 */
1533 frags = numfrags(fs, size);
1534 for (i = 0; i < frags; i++) {
1535 if (isset(cg_blksfree(cgp, needswap), bno + i)) {
1536 printf("dev = 0x%x, block = %d, fs = %s\n",
1537 ip->i_dev, bno + i, fs->fs_fsmnt);
1538 panic("blkfree: freeing free frag");
1539 }
1540 setbit(cg_blksfree(cgp, needswap), bno + i);
1541 }
1542 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1543 fs->fs_cstotal.cs_nffree += i;
1544 fs->fs_cs(fs, cg).cs_nffree += i;
1545 /*
1546 * add back in counts associated with the new frags
1547 */
1548 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1549 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1550 /*
1551 * if a complete block has been reassembled, account for it
1552 */
1553 blkno = fragstoblks(fs, bbase);
1554 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1555 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1556 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1557 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1558 ffs_clusteracct(fs, cgp, blkno, 1);
1559 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1560 fs->fs_cstotal.cs_nbfree++;
1561 fs->fs_cs(fs, cg).cs_nbfree++;
1562 i = cbtocylno(fs, bbase);
1563 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs,
1564 bbase)], 1,
1565 needswap);
1566 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1567 }
1568 }
1569 fs->fs_fmod = 1;
1570 bdwrite(bp);
1571 }
1572
1573 #if defined(DIAGNOSTIC) || defined(DEBUG)
1574 #ifdef XXXUBC
1575 /*
1576 * Verify allocation of a block or fragment. Returns true if block or
1577 * fragment is allocated, false if it is free.
1578 */
1579 static int
1580 ffs_checkblk(ip, bno, size)
1581 struct inode *ip;
1582 ufs_daddr_t bno;
1583 long size;
1584 {
1585 struct fs *fs;
1586 struct cg *cgp;
1587 struct buf *bp;
1588 int i, error, frags, free;
1589
1590 fs = ip->i_fs;
1591 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1592 printf("bsize = %d, size = %ld, fs = %s\n",
1593 fs->fs_bsize, size, fs->fs_fsmnt);
1594 panic("checkblk: bad size");
1595 }
1596 if ((u_int)bno >= fs->fs_size)
1597 panic("checkblk: bad block %d", bno);
1598 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1599 (int)fs->fs_cgsize, NOCRED, &bp);
1600 if (error) {
1601 brelse(bp);
1602 return 0;
1603 }
1604 cgp = (struct cg *)bp->b_data;
1605 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1606 brelse(bp);
1607 return 0;
1608 }
1609 bno = dtogd(fs, bno);
1610 if (size == fs->fs_bsize) {
1611 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1612 fragstoblks(fs, bno));
1613 } else {
1614 frags = numfrags(fs, size);
1615 for (free = 0, i = 0; i < frags; i++)
1616 if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1617 free++;
1618 if (free != 0 && free != frags)
1619 panic("checkblk: partially free fragment");
1620 }
1621 brelse(bp);
1622 return (!free);
1623 }
1624 #endif /* XXXUBC */
1625 #endif /* DIAGNOSTIC */
1626
1627 /*
1628 * Free an inode.
1629 */
1630 int
1631 ffs_vfree(v)
1632 void *v;
1633 {
1634 struct vop_vfree_args /* {
1635 struct vnode *a_pvp;
1636 ino_t a_ino;
1637 int a_mode;
1638 } */ *ap = v;
1639
1640 if (DOINGSOFTDEP(ap->a_pvp)) {
1641 softdep_freefile(ap);
1642 return (0);
1643 }
1644 return (ffs_freefile(ap));
1645 }
1646
1647 /*
1648 * Do the actual free operation.
1649 * The specified inode is placed back in the free map.
1650 */
1651 int
1652 ffs_freefile(v)
1653 void *v;
1654 {
1655 struct vop_vfree_args /* {
1656 struct vnode *a_pvp;
1657 ino_t a_ino;
1658 int a_mode;
1659 } */ *ap = v;
1660 struct cg *cgp;
1661 struct inode *pip = VTOI(ap->a_pvp);
1662 struct fs *fs = pip->i_fs;
1663 ino_t ino = ap->a_ino;
1664 struct buf *bp;
1665 int error, cg;
1666 #ifdef FFS_EI
1667 const int needswap = UFS_FSNEEDSWAP(fs);
1668 #endif
1669
1670 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1671 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s",
1672 pip->i_dev, ino, fs->fs_fsmnt);
1673 cg = ino_to_cg(fs, ino);
1674 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1675 (int)fs->fs_cgsize, NOCRED, &bp);
1676 if (error) {
1677 brelse(bp);
1678 return (error);
1679 }
1680 cgp = (struct cg *)bp->b_data;
1681 if (!cg_chkmagic(cgp, needswap)) {
1682 brelse(bp);
1683 return (0);
1684 }
1685 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1686 ino %= fs->fs_ipg;
1687 if (isclr(cg_inosused(cgp, needswap), ino)) {
1688 printf("dev = 0x%x, ino = %d, fs = %s\n",
1689 pip->i_dev, ino, fs->fs_fsmnt);
1690 if (fs->fs_ronly == 0)
1691 panic("ifree: freeing free inode");
1692 }
1693 clrbit(cg_inosused(cgp, needswap), ino);
1694 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
1695 cgp->cg_irotor = ufs_rw32(ino, needswap);
1696 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1697 fs->fs_cstotal.cs_nifree++;
1698 fs->fs_cs(fs, cg).cs_nifree++;
1699 if ((ap->a_mode & IFMT) == IFDIR) {
1700 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1701 fs->fs_cstotal.cs_ndir--;
1702 fs->fs_cs(fs, cg).cs_ndir--;
1703 }
1704 fs->fs_fmod = 1;
1705 bdwrite(bp);
1706 return (0);
1707 }
1708
1709 /*
1710 * Find a block of the specified size in the specified cylinder group.
1711 *
1712 * It is a panic if a request is made to find a block if none are
1713 * available.
1714 */
1715 static ufs_daddr_t
1716 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1717 struct fs *fs;
1718 struct cg *cgp;
1719 ufs_daddr_t bpref;
1720 int allocsiz;
1721 {
1722 ufs_daddr_t bno;
1723 int start, len, loc, i;
1724 int blk, field, subfield, pos;
1725 int ostart, olen;
1726 #ifdef FFS_EI
1727 const int needswap = UFS_FSNEEDSWAP(fs);
1728 #endif
1729
1730 /*
1731 * find the fragment by searching through the free block
1732 * map for an appropriate bit pattern
1733 */
1734 if (bpref)
1735 start = dtogd(fs, bpref) / NBBY;
1736 else
1737 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1738 len = howmany(fs->fs_fpg, NBBY) - start;
1739 ostart = start;
1740 olen = len;
1741 loc = scanc((u_int)len,
1742 (const u_char *)&cg_blksfree(cgp, needswap)[start],
1743 (const u_char *)fragtbl[fs->fs_frag],
1744 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1745 if (loc == 0) {
1746 len = start + 1;
1747 start = 0;
1748 loc = scanc((u_int)len,
1749 (const u_char *)&cg_blksfree(cgp, needswap)[0],
1750 (const u_char *)fragtbl[fs->fs_frag],
1751 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1752 if (loc == 0) {
1753 printf("start = %d, len = %d, fs = %s\n",
1754 ostart, olen, fs->fs_fsmnt);
1755 printf("offset=%d %ld\n",
1756 ufs_rw32(cgp->cg_freeoff, needswap),
1757 (long)cg_blksfree(cgp, needswap) - (long)cgp);
1758 panic("ffs_alloccg: map corrupted");
1759 /* NOTREACHED */
1760 }
1761 }
1762 bno = (start + len - loc) * NBBY;
1763 cgp->cg_frotor = ufs_rw32(bno, needswap);
1764 /*
1765 * found the byte in the map
1766 * sift through the bits to find the selected frag
1767 */
1768 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1769 blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
1770 blk <<= 1;
1771 field = around[allocsiz];
1772 subfield = inside[allocsiz];
1773 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1774 if ((blk & field) == subfield)
1775 return (bno + pos);
1776 field <<= 1;
1777 subfield <<= 1;
1778 }
1779 }
1780 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1781 panic("ffs_alloccg: block not in map");
1782 return (-1);
1783 }
1784
1785 /*
1786 * Update the cluster map because of an allocation or free.
1787 *
1788 * Cnt == 1 means free; cnt == -1 means allocating.
1789 */
1790 void
1791 ffs_clusteracct(fs, cgp, blkno, cnt)
1792 struct fs *fs;
1793 struct cg *cgp;
1794 ufs_daddr_t blkno;
1795 int cnt;
1796 {
1797 int32_t *sump;
1798 int32_t *lp;
1799 u_char *freemapp, *mapp;
1800 int i, start, end, forw, back, map, bit;
1801 #ifdef FFS_EI
1802 const int needswap = UFS_FSNEEDSWAP(fs);
1803 #endif
1804
1805 if (fs->fs_contigsumsize <= 0)
1806 return;
1807 freemapp = cg_clustersfree(cgp, needswap);
1808 sump = cg_clustersum(cgp, needswap);
1809 /*
1810 * Allocate or clear the actual block.
1811 */
1812 if (cnt > 0)
1813 setbit(freemapp, blkno);
1814 else
1815 clrbit(freemapp, blkno);
1816 /*
1817 * Find the size of the cluster going forward.
1818 */
1819 start = blkno + 1;
1820 end = start + fs->fs_contigsumsize;
1821 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
1822 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1823 mapp = &freemapp[start / NBBY];
1824 map = *mapp++;
1825 bit = 1 << (start % NBBY);
1826 for (i = start; i < end; i++) {
1827 if ((map & bit) == 0)
1828 break;
1829 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1830 bit <<= 1;
1831 } else {
1832 map = *mapp++;
1833 bit = 1;
1834 }
1835 }
1836 forw = i - start;
1837 /*
1838 * Find the size of the cluster going backward.
1839 */
1840 start = blkno - 1;
1841 end = start - fs->fs_contigsumsize;
1842 if (end < 0)
1843 end = -1;
1844 mapp = &freemapp[start / NBBY];
1845 map = *mapp--;
1846 bit = 1 << (start % NBBY);
1847 for (i = start; i > end; i--) {
1848 if ((map & bit) == 0)
1849 break;
1850 if ((i & (NBBY - 1)) != 0) {
1851 bit >>= 1;
1852 } else {
1853 map = *mapp--;
1854 bit = 1 << (NBBY - 1);
1855 }
1856 }
1857 back = start - i;
1858 /*
1859 * Account for old cluster and the possibly new forward and
1860 * back clusters.
1861 */
1862 i = back + forw + 1;
1863 if (i > fs->fs_contigsumsize)
1864 i = fs->fs_contigsumsize;
1865 ufs_add32(sump[i], cnt, needswap);
1866 if (back > 0)
1867 ufs_add32(sump[back], -cnt, needswap);
1868 if (forw > 0)
1869 ufs_add32(sump[forw], -cnt, needswap);
1870
1871 /*
1872 * Update cluster summary information.
1873 */
1874 lp = &sump[fs->fs_contigsumsize];
1875 for (i = fs->fs_contigsumsize; i > 0; i--)
1876 if (ufs_rw32(*lp--, needswap) > 0)
1877 break;
1878 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1879 }
1880
1881 /*
1882 * Fserr prints the name of a file system with an error diagnostic.
1883 *
1884 * The form of the error message is:
1885 * fs: error message
1886 */
1887 static void
1888 ffs_fserr(fs, uid, cp)
1889 struct fs *fs;
1890 u_int uid;
1891 char *cp;
1892 {
1893
1894 log(LOG_ERR, "uid %d comm %s on %s: %s\n",
1895 uid, curproc->p_comm, fs->fs_fsmnt, cp);
1896 }
1897