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