2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
60 #include <rev_endian_fs.h>
61 #include <vm/vm_pager.h>
63 #include <sys/param.h>
64 #include <sys/systm.h>
67 #include <sys/vnode.h>
68 #include <sys/mount.h>
69 #include <sys/kernel.h>
70 #include <sys/syslog.h>
71 #include <sys/quota.h>
75 #include <ufs/ufs/quota.h>
76 #include <ufs/ufs/inode.h>
78 #include <ufs/ffs/fs.h>
79 #include <ufs/ffs/ffs_extern.h>
82 #include <ufs/ufs/ufs_byte_order.h>
83 #include <architecture/byte_order.h>
84 #endif /* REV_ENDIAN_FS */
86 extern u_long nextgennumber
;
88 static ufs_daddr_t ffs_alloccg
__P((struct inode
*, int, ufs_daddr_t
, int));
89 static ufs_daddr_t ffs_alloccgblk
__P((struct fs
*, struct cg
*, ufs_daddr_t
));
90 static ufs_daddr_t ffs_clusteralloc
__P((struct inode
*, int, ufs_daddr_t
,
92 static ino_t ffs_dirpref
__P((struct inode
*));
93 static ufs_daddr_t ffs_fragextend
__P((struct inode
*, int, long, int, int));
94 static void ffs_fserr
__P((struct fs
*, u_int
, char *));
95 static u_long ffs_hashalloc
96 __P((struct inode
*, int, long, int, u_int32_t (*)()));
97 static ino_t ffs_nodealloccg
__P((struct inode
*, int, ufs_daddr_t
, int));
98 static ufs_daddr_t ffs_mapsearch
__P((struct fs
*, struct cg
*, ufs_daddr_t
,
102 * Allocate a block in the file system.
104 * The size of the requested block is given, which must be some
105 * multiple of fs_fsize and <= fs_bsize.
106 * A preference may be optionally specified. If a preference is given
107 * the following hierarchy is used to allocate a block:
108 * 1) allocate the requested block.
109 * 2) allocate a rotationally optimal block in the same cylinder.
110 * 3) allocate a block in the same cylinder group.
111 * 4) quadradically rehash into other cylinder groups, until an
112 * available block is located.
113 * If no block preference is given the following heirarchy is used
114 * to allocate a block:
115 * 1) allocate a block in the cylinder group that contains the
116 * inode for the file.
117 * 2) quadradically rehash into other cylinder groups, until an
118 * available block is located.
120 ffs_alloc(ip
, lbn
, bpref
, size
, cred
, bnp
)
121 register struct inode
*ip
;
122 ufs_daddr_t lbn
, bpref
;
127 register struct fs
*fs
;
134 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
135 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
136 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
137 panic("ffs_alloc: bad size");
140 panic("ffs_alloc: missing credential\n");
141 #endif /* DIAGNOSTIC */
142 if (size
== fs
->fs_bsize
&& fs
->fs_cstotal
.cs_nbfree
== 0)
144 if (cred
->cr_uid
!= 0 && freespace(fs
, fs
->fs_minfree
) <= 0)
146 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
148 if (error
= chkdq(ip
, (int64_t)size
, cred
, 0))
151 if (bpref
>= fs
->fs_size
)
154 cg
= ino_to_cg(fs
, ip
->i_number
);
156 cg
= dtog(fs
, bpref
);
157 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, size
,
158 (u_int32_t (*)())ffs_alloccg
);
160 ip
->i_blocks
+= btodb(size
, devBlockSize
);
161 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
167 * Restore user's disk quota because allocation failed.
169 (void) chkdq(ip
, (int64_t)-size
, cred
, FORCE
);
172 ffs_fserr(fs
, cred
->cr_uid
, "file system full");
173 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
178 * Reallocate a fragment to a bigger size
180 * The number and size of the old block is given, and a preference
181 * and new size is also specified. The allocator attempts to extend
182 * the original block. Failing that, the regular block allocator is
183 * invoked to get an appropriate block.
185 ffs_realloccg(ip
, lbprev
, bpref
, osize
, nsize
, cred
, bpp
)
186 register struct inode
*ip
;
193 register struct fs
*fs
;
195 int cg
, request
, error
;
196 ufs_daddr_t bprev
, bno
;
202 if ((u_int
)osize
> fs
->fs_bsize
|| fragoff(fs
, osize
) != 0 ||
203 (u_int
)nsize
> fs
->fs_bsize
|| fragoff(fs
, nsize
) != 0) {
205 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
206 ip
->i_dev
, fs
->fs_bsize
, osize
, nsize
, fs
->fs_fsmnt
);
207 panic("ffs_realloccg: bad size");
210 panic("ffs_realloccg: missing credential\n");
211 #endif /* DIAGNOSTIC */
212 if (cred
->cr_uid
!= 0 && freespace(fs
, fs
->fs_minfree
) <= 0)
214 if ((bprev
= ip
->i_db
[lbprev
]) == 0) {
215 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
216 ip
->i_dev
, fs
->fs_bsize
, bprev
, fs
->fs_fsmnt
);
217 panic("ffs_realloccg: bad bprev");
220 * Allocate the extra space in the buffer.
222 if (error
= bread(ITOV(ip
), lbprev
, osize
, NOCRED
, &bp
)) {
226 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
229 if (error
= chkdq(ip
, (int64_t)(nsize
- osize
), cred
, 0))
236 * Check for extension in the existing location.
238 cg
= dtog(fs
, bprev
);
239 if (bno
= ffs_fragextend(ip
, cg
, (long)bprev
, osize
, nsize
)) {
240 if (bp
->b_blkno
!= fsbtodb(fs
, bno
))
241 panic("bad blockno");
242 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
243 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
245 bp
->b_flags
|= B_DONE
;
246 bzero((char *)bp
->b_data
+ osize
, (u_int
)bp
->b_bufsize
- osize
);
251 * Allocate a new disk location.
253 if (bpref
>= fs
->fs_size
)
255 switch ((int)fs
->fs_optim
) {
258 * Allocate an exact sized fragment. Although this makes
259 * best use of space, we will waste time relocating it if
260 * the file continues to grow. If the fragmentation is
261 * less than half of the minimum free reserve, we choose
262 * to begin optimizing for time.
265 if (fs
->fs_minfree
< 5 ||
266 fs
->fs_cstotal
.cs_nffree
>
267 fs
->fs_dsize
* fs
->fs_minfree
/ (2 * 100))
269 log(LOG_NOTICE
, "%s: optimization changed from SPACE to TIME\n",
271 fs
->fs_optim
= FS_OPTTIME
;
275 * At this point we have discovered a file that is trying to
276 * grow a small fragment to a larger fragment. To save time,
277 * we allocate a full sized block, then free the unused portion.
278 * If the file continues to grow, the `ffs_fragextend' call
279 * above will be able to grow it in place without further
280 * copying. If aberrant programs cause disk fragmentation to
281 * grow within 2% of the free reserve, we choose to begin
282 * optimizing for space.
284 request
= fs
->fs_bsize
;
285 if (fs
->fs_cstotal
.cs_nffree
<
286 fs
->fs_dsize
* (fs
->fs_minfree
- 2) / 100)
288 log(LOG_NOTICE
, "%s: optimization changed from TIME to SPACE\n",
290 fs
->fs_optim
= FS_OPTSPACE
;
293 printf("dev = 0x%x, optim = %d, fs = %s\n",
294 ip
->i_dev
, fs
->fs_optim
, fs
->fs_fsmnt
);
295 panic("ffs_realloccg: bad optim");
298 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, request
,
299 (u_int32_t (*)())ffs_alloccg
);
301 bp
->b_blkno
= fsbtodb(fs
, bno
);
302 ffs_blkfree(ip
, bprev
, (long)osize
);
304 ffs_blkfree(ip
, bno
+ numfrags(fs
, nsize
),
305 (long)(request
- nsize
));
306 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
307 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
309 bp
->b_flags
|= B_DONE
;
310 bzero((char *)bp
->b_data
+ osize
, (u_int
)bp
->b_bufsize
- osize
);
316 * Restore user's disk quota because allocation failed.
318 (void) chkdq(ip
, (int64_t)-(nsize
- osize
), cred
, FORCE
);
325 ffs_fserr(fs
, cred
->cr_uid
, "file system full");
326 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
331 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
333 * The vnode and an array of buffer pointers for a range of sequential
334 * logical blocks to be made contiguous is given. The allocator attempts
335 * to find a range of sequential blocks starting as close as possible to
336 * an fs_rotdelay offset from the end of the allocation for the logical
337 * block immediately preceeding the current range. If successful, the
338 * physical block numbers in the buffer pointers and in the inode are
339 * changed to reflect the new allocation. If unsuccessful, the allocation
340 * is left unchanged. The success in doing the reallocation is returned.
341 * Note that the error return is not reflected back to the user. Rather
342 * the previous block allocation will be used.
344 /* Note: This routine is unused in UBC cluster I/O */
347 int doreallocblks
= 1;
351 struct vop_reallocblks_args
*ap
;
357 * Allocate an inode in the file system.
359 * If allocating a directory, use ffs_dirpref to select the inode.
360 * If allocating in a directory, the following hierarchy is followed:
361 * 1) allocate the preferred inode.
362 * 2) allocate an inode in the same cylinder group.
363 * 3) quadradically rehash into other cylinder groups, until an
364 * available inode is located.
365 * If no inode preference is given the following heirarchy is used
366 * to allocate an inode:
367 * 1) allocate an inode in cylinder group 0.
368 * 2) quadradically rehash into other cylinder groups, until an
369 * available inode is located.
373 struct vop_valloc_args
/* {
376 struct ucred *a_cred;
377 struct vnode **a_vpp;
380 register struct vnode
*pvp
= ap
->a_pvp
;
381 register struct inode
*pip
;
382 register struct fs
*fs
;
383 register struct inode
*ip
;
384 mode_t mode
= ap
->a_mode
;
391 if (fs
->fs_cstotal
.cs_nifree
== 0)
394 if ((mode
& IFMT
) == IFDIR
)
395 ipref
= ffs_dirpref(pip
);
397 ipref
= pip
->i_number
;
398 if (ipref
>= fs
->fs_ncg
* fs
->fs_ipg
)
400 cg
= ino_to_cg(fs
, ipref
);
402 * Track the number of dirs created one after another
403 * in a cg without intervening files.
405 if ((mode
& IFMT
) == IFDIR
) {
406 if (fs
->fs_contigdirs
[cg
] < 255)
407 fs
->fs_contigdirs
[cg
]++;
409 if (fs
->fs_contigdirs
[cg
] > 0)
410 fs
->fs_contigdirs
[cg
]--;
412 ino
= (ino_t
)ffs_hashalloc(pip
, cg
, (long)ipref
, mode
, ffs_nodealloccg
);
415 error
= VFS_VGET(pvp
->v_mount
, (void *)ino
, ap
->a_vpp
);
417 VOP_VFREE(pvp
, ino
, mode
);
420 ip
= VTOI(*ap
->a_vpp
);
422 printf("mode = 0%o, inum = %d, fs = %s\n",
423 ip
->i_mode
, ip
->i_number
, fs
->fs_fsmnt
);
424 panic("ffs_valloc: dup alloc");
426 if (ip
->i_blocks
) { /* XXX */
427 printf("free inode %s/%d had %d blocks\n",
428 fs
->fs_fsmnt
, ino
, ip
->i_blocks
);
433 * Set up a new generation number for this inode.
435 if (++nextgennumber
< (u_long
)time
.tv_sec
)
436 nextgennumber
= time
.tv_sec
;
437 ip
->i_gen
= nextgennumber
;
440 ffs_fserr(fs
, ap
->a_cred
->cr_uid
, "out of inodes");
441 uprintf("\n%s: create/symlink failed, no inodes free\n", fs
->fs_fsmnt
);
446 * Find a cylinder group to place a directory.
448 * The policy implemented by this algorithm is to allocate a
449 * directory inode in the same cylinder group as its parent
450 * directory, but also to reserve space for its files inodes
451 * and data. Restrict the number of directories which may be
452 * allocated one after another in the same cylinder group
453 * without intervening allocation of files.
459 register struct fs
*fs
;
460 int cg
, prefcg
, dirsize
, cgsize
;
461 int avgifree
, avgbfree
, avgndir
, curdirsize
;
462 int minifree
, minbfree
, maxndir
;
467 avgifree
= fs
->fs_cstotal
.cs_nifree
/ fs
->fs_ncg
;
468 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
469 avgndir
= fs
->fs_cstotal
.cs_ndir
/ fs
->fs_ncg
;
472 * Force allocation in another cg if creating a first level dir.
474 if (ITOV(pip
)->v_flag
& VROOT
) {
476 prefcg
= random() % fs
->fs_ncg
;
478 prefcg
= arc4random() % fs
->fs_ncg
;
481 minndir
= fs
->fs_ipg
;
482 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
483 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
484 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
485 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
487 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
489 for (cg
= 0; cg
< prefcg
; cg
++)
490 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
491 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
492 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
494 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
496 return ((ino_t
)(fs
->fs_ipg
* mincg
));
500 * Count various limits which used for
501 * optimal allocation of a directory inode.
503 maxndir
= min(avgndir
+ fs
->fs_ipg
/ 16, fs
->fs_ipg
);
504 minifree
= avgifree
- fs
->fs_ipg
/ 4;
507 minbfree
= avgbfree
- fs
->fs_fpg
/ fs
->fs_frag
/ 4;
510 cgsize
= fs
->fs_fsize
* fs
->fs_fpg
;
511 dirsize
= fs
->fs_avgfilesize
* fs
->fs_avgfpdir
;
512 curdirsize
= avgndir
? (cgsize
- avgbfree
* fs
->fs_bsize
) / avgndir
: 0;
513 if (dirsize
< curdirsize
)
514 dirsize
= curdirsize
;
515 maxcontigdirs
= min(cgsize
/ dirsize
, 255);
516 if (fs
->fs_avgfpdir
> 0)
517 maxcontigdirs
= min(maxcontigdirs
,
518 fs
->fs_ipg
/ fs
->fs_avgfpdir
);
519 if (maxcontigdirs
== 0)
523 * Limit number of dirs in one cg and reserve space for
524 * regular files, but only if we have no deficit in
527 prefcg
= ino_to_cg(fs
, pip
->i_number
);
528 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
529 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
530 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
531 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
532 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
533 return ((ino_t
)(fs
->fs_ipg
* cg
));
535 for (cg
= 0; cg
< prefcg
; cg
++)
536 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
537 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
538 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
539 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
540 return ((ino_t
)(fs
->fs_ipg
* cg
));
543 * This is a backstop when we have deficit in space.
545 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
546 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
547 return ((ino_t
)(fs
->fs_ipg
* cg
));
548 for (cg
= 0; cg
< prefcg
; cg
++)
549 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
551 return ((ino_t
)(fs
->fs_ipg
* cg
));
555 * Select the desired position for the next block in a file. The file is
556 * logically divided into sections. The first section is composed of the
557 * direct blocks. Each additional section contains fs_maxbpg blocks.
559 * If no blocks have been allocated in the first section, the policy is to
560 * request a block in the same cylinder group as the inode that describes
561 * the file. If no blocks have been allocated in any other section, the
562 * policy is to place the section in a cylinder group with a greater than
563 * average number of free blocks. An appropriate cylinder group is found
564 * by using a rotor that sweeps the cylinder groups. When a new group of
565 * blocks is needed, the sweep begins in the cylinder group following the
566 * cylinder group from which the previous allocation was made. The sweep
567 * continues until a cylinder group with greater than the average number
568 * of free blocks is found. If the allocation is for the first block in an
569 * indirect block, the information on the previous allocation is unavailable;
570 * here a best guess is made based upon the logical block number being
573 * If a section is already partially allocated, the policy is to
574 * contiguously allocate fs_maxcontig blocks. The end of one of these
575 * contiguous blocks and the beginning of the next is physically separated
576 * so that the disk head will be in transit between them for at least
577 * fs_rotdelay milliseconds. This is to allow time for the processor to
578 * schedule another I/O transfer.
581 ffs_blkpref(ip
, lbn
, indx
, bap
)
587 register struct fs
*fs
;
589 int avgbfree
, startcg
;
593 struct vnode
*vp
=ITOV(ip
);
594 struct mount
*mp
=vp
->v_mount
;
595 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
596 #endif /* REV_ENDIAN_FS */
602 if (bap
!= &ip
->i_db
[0])
603 prev
= NXSwapLong(bap
[indx
- 1]);
605 prev
= bap
[indx
- 1];
606 } else prev
= bap
[indx
- 1];
608 if (indx
% fs
->fs_maxbpg
== 0 || prev
== 0)
609 #else /* REV_ENDIAN_FS */
610 if (indx
% fs
->fs_maxbpg
== 0 || bap
[indx
- 1] == 0)
611 #endif /* REV_ENDIAN_FS */
614 cg
= ino_to_cg(fs
, ip
->i_number
);
615 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
618 * Find a cylinder with greater than average number of
619 * unused data blocks.
622 if (indx
== 0 || prev
== 0)
623 #else /* REV_ENDIAN_FS */
624 if (indx
== 0 || bap
[indx
- 1] == 0)
625 #endif /* REV_ENDIAN_FS */
627 ino_to_cg(fs
, ip
->i_number
) + lbn
/ fs
->fs_maxbpg
;
630 startcg
= dtog(fs
, prev
) + 1;
631 #else /* REV_ENDIAN_FS */
632 startcg
= dtog(fs
, bap
[indx
- 1]) + 1;
633 #endif /* REV_ENDIAN_FS */
634 startcg
%= fs
->fs_ncg
;
635 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
636 for (cg
= startcg
; cg
< fs
->fs_ncg
; cg
++)
637 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
639 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
641 for (cg
= 0; cg
<= startcg
; cg
++)
642 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
644 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
649 * One or more previous blocks have been laid out. If less
650 * than fs_maxcontig previous blocks are contiguous, the
651 * next block is requested contiguously, otherwise it is
652 * requested rotationally delayed by fs_rotdelay milliseconds.
656 nextblk
= prev
+ fs
->fs_frag
;
657 if (indx
< fs
->fs_maxcontig
) {
660 if (bap
!= &ip
->i_db
[0])
661 prev
= NXSwapLong(bap
[indx
- fs
->fs_maxcontig
]);
663 prev
= bap
[indx
- fs
->fs_maxcontig
];
664 if (prev
+ blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
667 #endif /* REV_ENDIAN_FS */
668 nextblk
= bap
[indx
- 1] + fs
->fs_frag
;
669 if (indx
< fs
->fs_maxcontig
|| bap
[indx
- fs
->fs_maxcontig
] +
670 blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
674 #endif /* REV_ENDIAN_FS */
675 if (fs
->fs_rotdelay
!= 0)
677 * Here we convert ms of delay to frags as:
678 * (frags) = (ms) * (rev/sec) * (sect/rev) /
679 * ((sect/frag) * (ms/sec))
680 * then round up to the next block.
682 nextblk
+= roundup(fs
->fs_rotdelay
* fs
->fs_rps
* fs
->fs_nsect
/
683 (NSPF(fs
) * 1000), fs
->fs_frag
);
688 * Implement the cylinder overflow algorithm.
690 * The policy implemented by this algorithm is:
691 * 1) allocate the block in its requested cylinder group.
692 * 2) quadradically rehash on the cylinder group number.
693 * 3) brute force search for a free block.
697 ffs_hashalloc(ip
, cg
, pref
, size
, allocator
)
701 int size
; /* size for data blocks, mode for inodes */
702 u_int32_t (*allocator
)();
704 register struct fs
*fs
;
710 * 1: preferred cylinder group
712 result
= (*allocator
)(ip
, cg
, pref
, size
);
716 * 2: quadratic rehash
718 for (i
= 1; i
< fs
->fs_ncg
; i
*= 2) {
720 if (cg
>= fs
->fs_ncg
)
722 result
= (*allocator
)(ip
, cg
, 0, size
);
727 * 3: brute force search
728 * Note that we start at i == 2, since 0 was checked initially,
729 * and 1 is always checked in the quadratic rehash.
731 cg
= (icg
+ 2) % fs
->fs_ncg
;
732 for (i
= 2; i
< fs
->fs_ncg
; i
++) {
733 result
= (*allocator
)(ip
, cg
, 0, size
);
737 if (cg
== fs
->fs_ncg
)
744 * Determine whether a fragment can be extended.
746 * Check to see if the necessary fragments are available, and
747 * if they are, allocate them.
750 ffs_fragextend(ip
, cg
, bprev
, osize
, nsize
)
756 register struct fs
*fs
;
757 register struct cg
*cgp
;
763 struct vnode
*vp
=ITOV(ip
);
764 struct mount
*mp
=vp
->v_mount
;
765 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
766 #endif /* REV_ENDIAN_FS */
769 if (fs
->fs_cs(fs
, cg
).cs_nffree
< numfrags(fs
, nsize
- osize
))
771 frags
= numfrags(fs
, nsize
); /* number of fragments needed */
772 bbase
= fragnum(fs
, bprev
); /* offset in a frag (it is mod fragsize */
773 if (bbase
> fragnum(fs
, (bprev
+ frags
- 1))) {
774 /* cannot extend across a block boundary */
777 /* read corresponding cylinder group info */
778 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
779 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
784 cgp
= (struct cg
*)bp
->b_data
;
787 byte_swap_cgin(cgp
, fs
);
789 #endif /* REV_ENDIAN_FS */
791 if (!cg_chkmagic(cgp
)) {
794 byte_swap_cgout(cgp
,fs
);
795 #endif /* REV_ENDIAN_FS */
799 cgp
->cg_time
= time
.tv_sec
;
800 bno
= dtogd(fs
, bprev
);
801 for (i
= numfrags(fs
, osize
); i
< frags
; i
++)
802 if (isclr(cg_blksfree(cgp
), bno
+ i
)) {
805 byte_swap_cgout(cgp
,fs
);
806 #endif /* REV_ENDIAN_FS */
811 * the current fragment can be extended
812 * deduct the count on fragment being extended into
813 * increase the count on the remaining fragment (if any)
814 * allocate the extended piece
816 for (i
= frags
; i
< fs
->fs_frag
- bbase
; i
++)
817 if (isclr(cg_blksfree(cgp
), bno
+ i
))
819 cgp
->cg_frsum
[i
- numfrags(fs
, osize
)]--;
821 cgp
->cg_frsum
[i
- frags
]++;
822 for (i
= numfrags(fs
, osize
); i
< frags
; i
++) {
823 clrbit(cg_blksfree(cgp
), bno
+ i
);
824 cgp
->cg_cs
.cs_nffree
--;
825 fs
->fs_cstotal
.cs_nffree
--;
826 fs
->fs_cs(fs
, cg
).cs_nffree
--;
831 byte_swap_cgout(cgp
,fs
);
832 #endif /* REV_ENDIAN_FS */
838 * Determine whether a block can be allocated.
840 * Check to see if a block of the appropriate size is available,
841 * and if it is, allocate it.
844 ffs_alloccg(ip
, cg
, bpref
, size
)
850 register struct fs
*fs
;
851 register struct cg
*cgp
;
854 int error
, bno
, frags
, allocsiz
;
856 struct vnode
*vp
=ITOV(ip
);
857 struct mount
*mp
=vp
->v_mount
;
858 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
859 #endif /* REV_ENDIAN_FS */
862 if (fs
->fs_cs(fs
, cg
).cs_nbfree
== 0 && size
== fs
->fs_bsize
)
864 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
865 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
870 cgp
= (struct cg
*)bp
->b_data
;
873 byte_swap_cgin(cgp
,fs
);
874 #endif /* REV_ENDIAN_FS */
875 if (!cg_chkmagic(cgp
) ||
876 (cgp
->cg_cs
.cs_nbfree
== 0 && size
== fs
->fs_bsize
)) {
879 byte_swap_cgout(cgp
,fs
);
880 #endif /* REV_ENDIAN_FS */
884 cgp
->cg_time
= time
.tv_sec
;
885 if (size
== fs
->fs_bsize
) {
886 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
889 byte_swap_cgout(cgp
,fs
);
890 #endif /* REV_ENDIAN_FS */
895 * check to see if any fragments are already available
896 * allocsiz is the size which will be allocated, hacking
897 * it down to a smaller size if necessary
899 frags
= numfrags(fs
, size
);
900 for (allocsiz
= frags
; allocsiz
< fs
->fs_frag
; allocsiz
++)
901 if (cgp
->cg_frsum
[allocsiz
] != 0)
903 if (allocsiz
== fs
->fs_frag
) {
905 * no fragments were available, so a block will be
906 * allocated, and hacked up
908 if (cgp
->cg_cs
.cs_nbfree
== 0) {
911 byte_swap_cgout(cgp
,fs
);
912 #endif /* REV_ENDIAN_FS */
916 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
917 bpref
= dtogd(fs
, bno
);
918 for (i
= frags
; i
< fs
->fs_frag
; i
++)
919 setbit(cg_blksfree(cgp
), bpref
+ i
);
920 i
= fs
->fs_frag
- frags
;
921 cgp
->cg_cs
.cs_nffree
+= i
;
922 fs
->fs_cstotal
.cs_nffree
+= i
;
923 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
928 byte_swap_cgout(cgp
,fs
);
929 #endif /* REV_ENDIAN_FS */
933 bno
= ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
);
937 byte_swap_cgout(cgp
,fs
);
938 #endif /* REV_ENDIAN_FS */
942 for (i
= 0; i
< frags
; i
++)
943 clrbit(cg_blksfree(cgp
), bno
+ i
);
944 cgp
->cg_cs
.cs_nffree
-= frags
;
945 fs
->fs_cstotal
.cs_nffree
-= frags
;
946 fs
->fs_cs(fs
, cg
).cs_nffree
-= frags
;
948 cgp
->cg_frsum
[allocsiz
]--;
949 if (frags
!= allocsiz
)
950 cgp
->cg_frsum
[allocsiz
- frags
]++;
953 byte_swap_cgout(cgp
,fs
);
954 #endif /* REV_ENDIAN_FS */
956 return (cg
* fs
->fs_fpg
+ bno
);
960 * Allocate a block in a cylinder group.
962 * This algorithm implements the following policy:
963 * 1) allocate the requested block.
964 * 2) allocate a rotationally optimal block in the same cylinder.
965 * 3) allocate the next available block on the block rotor for the
966 * specified cylinder group.
967 * Note that this routine only allocates fs_bsize blocks; these
968 * blocks may be fragmented by the routine that allocates them.
971 ffs_alloccgblk(fs
, cgp
, bpref
)
972 register struct fs
*fs
;
973 register struct cg
*cgp
;
976 ufs_daddr_t bno
, blkno
;
977 int cylno
, pos
, delta
;
981 if (bpref
== 0 || dtog(fs
, bpref
) != cgp
->cg_cgx
) {
982 bpref
= cgp
->cg_rotor
;
985 bpref
= blknum(fs
, bpref
);
986 bpref
= dtogd(fs
, bpref
);
988 * if the requested block is available, use it
990 if (ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bpref
))) {
994 if (fs
->fs_nrpos
<= 1 || fs
->fs_cpc
== 0) {
996 * Block layout information is not available.
997 * Leaving bpref unchanged means we take the
998 * next available free block following the one
999 * we just allocated. Hopefully this will at
1000 * least hit a track cache on drives of unknown
1001 * geometry (e.g. SCSI).
1006 * check for a block available on the same cylinder
1008 cylno
= cbtocylno(fs
, bpref
);
1009 if (cg_blktot(cgp
)[cylno
] == 0)
1012 * check the summary information to see if a block is
1013 * available in the requested cylinder starting at the
1014 * requested rotational position and proceeding around.
1016 cylbp
= cg_blks(fs
, cgp
, cylno
);
1017 pos
= cbtorpos(fs
, bpref
);
1018 for (i
= pos
; i
< fs
->fs_nrpos
; i
++)
1021 if (i
== fs
->fs_nrpos
)
1022 for (i
= 0; i
< pos
; i
++)
1027 * found a rotational position, now find the actual
1028 * block. A panic if none is actually there.
1030 pos
= cylno
% fs
->fs_cpc
;
1031 bno
= (cylno
- pos
) * fs
->fs_spc
/ NSPB(fs
);
1032 if (fs_postbl(fs
, pos
)[i
] == -1) {
1033 printf("pos = %d, i = %d, fs = %s\n",
1034 pos
, i
, fs
->fs_fsmnt
);
1035 panic("ffs_alloccgblk: cyl groups corrupted");
1037 for (i
= fs_postbl(fs
, pos
)[i
];; ) {
1038 if (ffs_isblock(fs
, cg_blksfree(cgp
), bno
+ i
)) {
1039 bno
= blkstofrags(fs
, (bno
+ i
));
1042 delta
= fs_rotbl(fs
)[i
];
1044 delta
+ i
> fragstoblks(fs
, fs
->fs_fpg
))
1048 printf("pos = %d, i = %d, fs = %s\n", pos
, i
, fs
->fs_fsmnt
);
1049 panic("ffs_alloccgblk: can't find blk in cyl");
1053 * no blocks in the requested cylinder, so take next
1054 * available one in this cylinder group.
1056 bno
= ffs_mapsearch(fs
, cgp
, bpref
, (int)fs
->fs_frag
);
1059 cgp
->cg_rotor
= bno
;
1061 blkno
= fragstoblks(fs
, bno
);
1062 ffs_clrblock(fs
, cg_blksfree(cgp
), (long)blkno
);
1063 ffs_clusteracct(fs
, cgp
, blkno
, -1);
1064 cgp
->cg_cs
.cs_nbfree
--;
1065 fs
->fs_cstotal
.cs_nbfree
--;
1066 fs
->fs_cs(fs
, cgp
->cg_cgx
).cs_nbfree
--;
1067 cylno
= cbtocylno(fs
, bno
);
1068 cg_blks(fs
, cgp
, cylno
)[cbtorpos(fs
, bno
)]--;
1069 cg_blktot(cgp
)[cylno
]--;
1071 return (cgp
->cg_cgx
* fs
->fs_fpg
+ bno
);
1075 * Determine whether a cluster can be allocated.
1077 * We do not currently check for optimal rotational layout if there
1078 * are multiple choices in the same cylinder group. Instead we just
1079 * take the first one that we find following bpref.
1082 ffs_clusteralloc(ip
, cg
, bpref
, len
)
1088 register struct fs
*fs
;
1089 register struct cg
*cgp
;
1091 int i
, got
, run
, bno
, bit
, map
;
1095 struct vnode
*vp
=ITOV(ip
);
1096 struct mount
*mp
=vp
->v_mount
;
1097 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1098 #endif /* REV_ENDIAN_FS */
1101 if (fs
->fs_maxcluster
[cg
] < len
)
1103 if (bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)), (int)fs
->fs_cgsize
,
1106 cgp
= (struct cg
*)bp
->b_data
;
1109 byte_swap_cgin(cgp
,fs
);
1110 #endif /* REV_ENDIAN_FS */
1111 if (!cg_chkmagic(cgp
)) {
1114 byte_swap_cgout(cgp
,fs
);
1115 #endif /* REV_ENDIAN_FS */
1119 * Check to see if a cluster of the needed size (or bigger) is
1120 * available in this cylinder group.
1122 lp
= &cg_clustersum(cgp
)[len
];
1123 for (i
= len
; i
<= fs
->fs_contigsumsize
; i
++)
1126 if (i
> fs
->fs_contigsumsize
) {
1128 * This is the first time looking for a cluster in this
1129 * cylinder group. Update the cluster summary information
1130 * to reflect the true maximum sized cluster so that
1131 * future cluster allocation requests can avoid reading
1132 * the cylinder group map only to find no clusters.
1134 lp
= &cg_clustersum(cgp
)[len
- 1];
1135 for (i
= len
- 1; i
> 0; i
--)
1138 fs
->fs_maxcluster
[cg
] = i
;
1141 byte_swap_cgout(cgp
,fs
);
1142 #endif /* REV_ENDIAN_FS */
1146 * Search the cluster map to find a big enough cluster.
1147 * We take the first one that we find, even if it is larger
1148 * than we need as we prefer to get one close to the previous
1149 * block allocation. We do not search before the current
1150 * preference point as we do not want to allocate a block
1151 * that is allocated before the previous one (as we will
1152 * then have to wait for another pass of the elevator
1153 * algorithm before it will be read). We prefer to fail and
1154 * be recalled to try an allocation in the next cylinder group.
1156 if (dtog(fs
, bpref
) != cg
)
1159 bpref
= fragstoblks(fs
, dtogd(fs
, blknum(fs
, bpref
)));
1160 mapp
= &cg_clustersfree(cgp
)[bpref
/ NBBY
];
1162 bit
= 1 << (bpref
% NBBY
);
1163 for (run
= 0, got
= bpref
; got
< cgp
->cg_nclusterblks
; got
++) {
1164 if ((map
& bit
) == 0) {
1171 if ((got
& (NBBY
- 1)) != (NBBY
- 1)) {
1178 if (got
== cgp
->cg_nclusterblks
) {
1181 byte_swap_cgout(cgp
,fs
);
1182 #endif /* REV_ENDIAN_FS */
1186 * Allocate the cluster that we have found.
1188 for (i
= 1; i
<= len
; i
++)
1189 if (!ffs_isblock(fs
, cg_blksfree(cgp
), got
- run
+ i
))
1190 panic("ffs_clusteralloc: map mismatch");
1191 bno
= cg
* fs
->fs_fpg
+ blkstofrags(fs
, got
- run
+ 1);
1192 if (dtog(fs
, bno
) != cg
)
1193 panic("ffs_clusteralloc: allocated out of group");
1194 len
= blkstofrags(fs
, len
);
1195 for (i
= 0; i
< len
; i
+= fs
->fs_frag
)
1196 if ((got
= ffs_alloccgblk(fs
, cgp
, bno
+ i
)) != bno
+ i
)
1197 panic("ffs_clusteralloc: lost block");
1200 byte_swap_cgout(cgp
,fs
);
1201 #endif /* REV_ENDIAN_FS */
1211 * Determine whether an inode can be allocated.
1213 * Check to see if an inode is available, and if it is,
1214 * allocate it using the following policy:
1215 * 1) allocate the requested inode.
1216 * 2) allocate the next available inode after the requested
1217 * inode in the specified cylinder group.
1220 ffs_nodealloccg(ip
, cg
, ipref
, mode
)
1226 register struct fs
*fs
;
1227 register struct cg
*cgp
;
1229 int error
, start
, len
, loc
, map
, i
;
1231 struct vnode
*vp
=ITOV(ip
);
1232 struct mount
*mp
=vp
->v_mount
;
1233 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1234 #endif /* REV_ENDIAN_FS */
1237 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1239 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
1240 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1245 cgp
= (struct cg
*)bp
->b_data
;
1248 byte_swap_cgin(cgp
,fs
);
1249 #endif /* REV_ENDIAN_FS */
1250 if (!cg_chkmagic(cgp
) || cgp
->cg_cs
.cs_nifree
== 0) {
1253 byte_swap_cgout(cgp
,fs
);
1254 #endif /* REV_ENDIAN_FS */
1259 cgp
->cg_time
= time
.tv_sec
;
1261 ipref
%= fs
->fs_ipg
;
1262 if (isclr(cg_inosused(cgp
), ipref
))
1265 start
= cgp
->cg_irotor
/ NBBY
;
1266 len
= howmany(fs
->fs_ipg
- cgp
->cg_irotor
, NBBY
);
1267 loc
= skpc(0xff, len
, &cg_inosused(cgp
)[start
]);
1271 loc
= skpc(0xff, len
, &cg_inosused(cgp
)[0]);
1273 printf("cg = %d, irotor = %d, fs = %s\n",
1274 cg
, cgp
->cg_irotor
, fs
->fs_fsmnt
);
1275 panic("ffs_nodealloccg: map corrupted");
1279 i
= start
+ len
- loc
;
1280 map
= cg_inosused(cgp
)[i
];
1282 for (i
= 1; i
< (1 << NBBY
); i
<<= 1, ipref
++) {
1283 if ((map
& i
) == 0) {
1284 cgp
->cg_irotor
= ipref
;
1288 printf("fs = %s\n", fs
->fs_fsmnt
);
1289 panic("ffs_nodealloccg: block not in map");
1292 setbit(cg_inosused(cgp
), ipref
);
1293 cgp
->cg_cs
.cs_nifree
--;
1294 fs
->fs_cstotal
.cs_nifree
--;
1295 fs
->fs_cs(fs
, cg
).cs_nifree
--;
1297 if ((mode
& IFMT
) == IFDIR
) {
1298 cgp
->cg_cs
.cs_ndir
++;
1299 fs
->fs_cstotal
.cs_ndir
++;
1300 fs
->fs_cs(fs
, cg
).cs_ndir
++;
1304 byte_swap_cgout(cgp
,fs
);
1305 #endif /* REV_ENDIAN_FS */
1307 return (cg
* fs
->fs_ipg
+ ipref
);
1311 * Free a block or fragment.
1313 * The specified block or fragment is placed back in the
1314 * free map. If a fragment is deallocated, a possible
1315 * block reassembly is checked.
1317 ffs_blkfree(ip
, bno
, size
)
1318 register struct inode
*ip
;
1322 register struct fs
*fs
;
1323 register struct cg
*cgp
;
1326 int i
, error
, cg
, blk
, frags
, bbase
;
1328 struct vnode
*vp
=ITOV(ip
);
1329 struct mount
*mp
=vp
->v_mount
;
1330 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1331 #endif /* REV_ENDIAN_FS */
1333 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1334 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1335 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1336 panic("blkfree: bad size");
1339 if ((u_int
)bno
>= fs
->fs_size
) {
1340 printf("bad block %d, ino %d\n", bno
, ip
->i_number
);
1341 ffs_fserr(fs
, ip
->i_uid
, "bad block");
1344 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
1345 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1350 cgp
= (struct cg
*)bp
->b_data
;
1353 byte_swap_cgin(cgp
,fs
);
1354 #endif /* REV_ENDIAN_FS */
1355 if (!cg_chkmagic(cgp
)) {
1358 byte_swap_cgout(cgp
,fs
);
1359 #endif /* REV_ENDIAN_FS */
1363 cgp
->cg_time
= time
.tv_sec
;
1364 bno
= dtogd(fs
, bno
);
1365 if (size
== fs
->fs_bsize
) {
1366 blkno
= fragstoblks(fs
, bno
);
1367 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1368 printf("dev = 0x%x, block = %d, fs = %s\n",
1369 ip
->i_dev
, bno
, fs
->fs_fsmnt
);
1370 panic("blkfree: freeing free block");
1372 ffs_setblock(fs
, cg_blksfree(cgp
), blkno
);
1373 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1374 cgp
->cg_cs
.cs_nbfree
++;
1375 fs
->fs_cstotal
.cs_nbfree
++;
1376 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1377 i
= cbtocylno(fs
, bno
);
1378 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bno
)]++;
1379 cg_blktot(cgp
)[i
]++;
1381 bbase
= bno
- fragnum(fs
, bno
);
1383 * decrement the counts associated with the old frags
1385 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1386 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, -1);
1388 * deallocate the fragment
1390 frags
= numfrags(fs
, size
);
1391 for (i
= 0; i
< frags
; i
++) {
1392 if (isset(cg_blksfree(cgp
), bno
+ i
)) {
1393 printf("dev = 0x%x, block = %d, fs = %s\n",
1394 ip
->i_dev
, bno
+ i
, fs
->fs_fsmnt
);
1395 panic("blkfree: freeing free frag");
1397 setbit(cg_blksfree(cgp
), bno
+ i
);
1399 cgp
->cg_cs
.cs_nffree
+= i
;
1400 fs
->fs_cstotal
.cs_nffree
+= i
;
1401 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
1403 * add back in counts associated with the new frags
1405 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1406 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, 1);
1408 * if a complete block has been reassembled, account for it
1410 blkno
= fragstoblks(fs
, bbase
);
1411 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1412 cgp
->cg_cs
.cs_nffree
-= fs
->fs_frag
;
1413 fs
->fs_cstotal
.cs_nffree
-= fs
->fs_frag
;
1414 fs
->fs_cs(fs
, cg
).cs_nffree
-= fs
->fs_frag
;
1415 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1416 cgp
->cg_cs
.cs_nbfree
++;
1417 fs
->fs_cstotal
.cs_nbfree
++;
1418 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1419 i
= cbtocylno(fs
, bbase
);
1420 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bbase
)]++;
1421 cg_blktot(cgp
)[i
]++;
1427 byte_swap_cgout(cgp
,fs
);
1428 #endif /* REV_ENDIAN_FS */
1434 * Verify allocation of a block or fragment. Returns true if block or
1435 * fragment is allocated, false if it is free.
1437 ffs_checkblk(ip
, bno
, size
)
1445 int i
, error
, frags
, free
;
1447 struct vnode
*vp
=ITOV(ip
);
1448 struct mount
*mp
=vp
->v_mount
;
1449 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1450 #endif /* REV_ENDIAN_FS */
1453 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1454 printf("bsize = %d, size = %d, fs = %s\n",
1455 fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1456 panic("checkblk: bad size");
1458 if ((u_int
)bno
>= fs
->fs_size
)
1459 panic("checkblk: bad block %d", bno
);
1460 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, dtog(fs
, bno
))),
1461 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1466 cgp
= (struct cg
*)bp
->b_data
;
1469 byte_swap_cgin(cgp
,fs
);
1470 #endif /* REV_ENDIAN_FS */
1471 if (!cg_chkmagic(cgp
)) {
1474 byte_swap_cgout(cgp
,fs
);
1475 #endif /* REV_ENDIAN_FS */
1479 bno
= dtogd(fs
, bno
);
1480 if (size
== fs
->fs_bsize
) {
1481 free
= ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bno
));
1483 frags
= numfrags(fs
, size
);
1484 for (free
= 0, i
= 0; i
< frags
; i
++)
1485 if (isset(cg_blksfree(cgp
), bno
+ i
))
1487 if (free
!= 0 && free
!= frags
)
1488 panic("checkblk: partially free fragment");
1492 byte_swap_cgout(cgp
,fs
);
1493 #endif /* REV_ENDIAN_FS */
1497 #endif /* DIAGNOSTIC */
1502 * The specified inode is placed back in the free map.
1506 struct vop_vfree_args
/* {
1507 struct vnode *a_pvp;
1512 register struct fs
*fs
;
1513 register struct cg
*cgp
;
1514 register struct inode
*pip
;
1515 ino_t ino
= ap
->a_ino
;
1519 struct vnode
*vp
=ap
->a_pvp
;
1520 struct mount
*mp
=vp
->v_mount
;
1521 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1522 #endif /* REV_ENDIAN_FS */
1524 pip
= VTOI(ap
->a_pvp
);
1526 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
1527 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1528 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1529 cg
= ino_to_cg(fs
, ino
);
1530 error
= bread(pip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
1531 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1536 cgp
= (struct cg
*)bp
->b_data
;
1539 byte_swap_cgin(cgp
,fs
);
1540 #endif /* REV_ENDIAN_FS */
1541 if (!cg_chkmagic(cgp
)) {
1544 byte_swap_cgout(cgp
,fs
);
1545 #endif /* REV_ENDIAN_FS */
1549 cgp
->cg_time
= time
.tv_sec
;
1551 if (isclr(cg_inosused(cgp
), ino
)) {
1552 printf("dev = 0x%x, ino = %d, fs = %s\n",
1553 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1554 if (fs
->fs_ronly
== 0)
1555 panic("ifree: freeing free inode");
1557 clrbit(cg_inosused(cgp
), ino
);
1558 if (ino
< cgp
->cg_irotor
)
1559 cgp
->cg_irotor
= ino
;
1560 cgp
->cg_cs
.cs_nifree
++;
1561 fs
->fs_cstotal
.cs_nifree
++;
1562 fs
->fs_cs(fs
, cg
).cs_nifree
++;
1563 if ((ap
->a_mode
& IFMT
) == IFDIR
) {
1564 cgp
->cg_cs
.cs_ndir
--;
1565 fs
->fs_cstotal
.cs_ndir
--;
1566 fs
->fs_cs(fs
, cg
).cs_ndir
--;
1571 byte_swap_cgout(cgp
,fs
);
1572 #endif /* REV_ENDIAN_FS */
1578 * Find a block of the specified size in the specified cylinder group.
1580 * It is a panic if a request is made to find a block if none are
1584 ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
)
1585 register struct fs
*fs
;
1586 register struct cg
*cgp
;
1591 int start
, len
, loc
, i
;
1592 int blk
, field
, subfield
, pos
;
1595 * find the fragment by searching through the free block
1596 * map for an appropriate bit pattern
1599 start
= dtogd(fs
, bpref
) / NBBY
;
1601 start
= cgp
->cg_frotor
/ NBBY
;
1602 len
= howmany(fs
->fs_fpg
, NBBY
) - start
;
1603 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[start
],
1604 (u_char
*)fragtbl
[fs
->fs_frag
],
1605 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1609 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[0],
1610 (u_char
*)fragtbl
[fs
->fs_frag
],
1611 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1613 printf("start = %d, len = %d, fs = %s\n",
1614 start
, len
, fs
->fs_fsmnt
);
1615 panic("ffs_alloccg: map corrupted");
1619 bno
= (start
+ len
- loc
) * NBBY
;
1620 cgp
->cg_frotor
= bno
;
1622 * found the byte in the map
1623 * sift through the bits to find the selected frag
1625 for (i
= bno
+ NBBY
; bno
< i
; bno
+= fs
->fs_frag
) {
1626 blk
= blkmap(fs
, cg_blksfree(cgp
), bno
);
1628 field
= around
[allocsiz
];
1629 subfield
= inside
[allocsiz
];
1630 for (pos
= 0; pos
<= fs
->fs_frag
- allocsiz
; pos
++) {
1631 if ((blk
& field
) == subfield
)
1637 printf("bno = %d, fs = %s\n", bno
, fs
->fs_fsmnt
);
1638 panic("ffs_alloccg: block not in map");
1643 * Update the cluster map because of an allocation or free.
1645 * Cnt == 1 means free; cnt == -1 means allocating.
1647 ffs_clusteracct(fs
, cgp
, blkno
, cnt
)
1655 u_char
*freemapp
, *mapp
;
1656 int i
, start
, end
, forw
, back
, map
, bit
;
1658 if (fs
->fs_contigsumsize
<= 0)
1660 freemapp
= cg_clustersfree(cgp
);
1661 sump
= cg_clustersum(cgp
);
1663 * Allocate or clear the actual block.
1666 setbit(freemapp
, blkno
);
1668 clrbit(freemapp
, blkno
);
1670 * Find the size of the cluster going forward.
1673 end
= start
+ fs
->fs_contigsumsize
;
1674 if (end
>= cgp
->cg_nclusterblks
)
1675 end
= cgp
->cg_nclusterblks
;
1676 mapp
= &freemapp
[start
/ NBBY
];
1678 bit
= 1 << (start
% NBBY
);
1679 for (i
= start
; i
< end
; i
++) {
1680 if ((map
& bit
) == 0)
1682 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
1691 * Find the size of the cluster going backward.
1694 end
= start
- fs
->fs_contigsumsize
;
1697 mapp
= &freemapp
[start
/ NBBY
];
1699 bit
= 1 << (start
% NBBY
);
1700 for (i
= start
; i
> end
; i
--) {
1701 if ((map
& bit
) == 0)
1703 if ((i
& (NBBY
- 1)) != 0) {
1707 bit
= 1 << (NBBY
- 1);
1712 * Account for old cluster and the possibly new forward and
1715 i
= back
+ forw
+ 1;
1716 if (i
> fs
->fs_contigsumsize
)
1717 i
= fs
->fs_contigsumsize
;
1724 * Update cluster summary information.
1726 lp
= &sump
[fs
->fs_contigsumsize
];
1727 for (i
= fs
->fs_contigsumsize
; i
> 0; i
--)
1730 fs
->fs_maxcluster
[cgp
->cg_cgx
] = i
;
1734 * Fserr prints the name of a file system with an error diagnostic.
1736 * The form of the error message is:
1740 ffs_fserr(fs
, uid
, cp
)
1746 log(LOG_ERR
, "uid %d on %s: %s\n", uid
, fs
->fs_fsmnt
, cp
);