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