]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_alloc.c
815d53b3f988055fb2cc8aa53153d2aa9d2696d1
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
63 #include <rev_endian_fs.h>
64 #include <vm/vm_pager.h>
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/buf_internal.h>
70 #include <sys/kauth.h>
71 #include <sys/vnode_internal.h>
72 #include <sys/mount_internal.h>
73 #include <sys/kernel.h>
74 #include <sys/syslog.h>
75 #include <sys/quota.h>
79 #include <ufs/ufs/quota.h>
80 #include <ufs/ufs/inode.h>
82 #include <ufs/ffs/fs.h>
83 #include <ufs/ffs/ffs_extern.h>
86 #include <ufs/ufs/ufs_byte_order.h>
87 #include <architecture/byte_order.h>
88 #endif /* REV_ENDIAN_FS */
90 extern u_long nextgennumber
;
92 static ufs_daddr_t
ffs_alloccg(struct inode
*, int, ufs_daddr_t
, int);
93 static ufs_daddr_t
ffs_alloccgblk(struct fs
*, struct cg
*, ufs_daddr_t
);
94 static ufs_daddr_t
ffs_clusteralloc(struct inode
*, int, ufs_daddr_t
, int);
95 static ino_t
ffs_dirpref(struct inode
*);
96 static ufs_daddr_t
ffs_fragextend(struct inode
*, int, long, int, int);
97 static void ffs_fserr(struct fs
*, u_int
, char *);
98 static u_long ffs_hashalloc
99 (struct inode
*, int, long, int, u_int32_t (*)());
100 static ino_t
ffs_nodealloccg(struct inode
*, int, ufs_daddr_t
, int);
101 static ufs_daddr_t
ffs_mapsearch(struct fs
*, struct cg
*, ufs_daddr_t
, int);
102 static void ffs_clusteracct
103 (struct fs
*fs
, struct cg
*cgp
, ufs_daddr_t blkno
, int cnt
);
106 * Allocate a block in the file system.
108 * The size of the requested block is given, which must be some
109 * multiple of fs_fsize and <= fs_bsize.
110 * A preference may be optionally specified. If a preference is given
111 * the following hierarchy is used to allocate a block:
112 * 1) allocate the requested block.
113 * 2) allocate a rotationally optimal block in the same cylinder.
114 * 3) allocate a block in the same cylinder group.
115 * 4) quadradically rehash into other cylinder groups, until an
116 * available block is located.
117 * If no block preference is given the following heirarchy is used
118 * to allocate a block:
119 * 1) allocate a block in the cylinder group that contains the
120 * inode for the file.
121 * 2) quadradically rehash into other cylinder groups, until an
122 * available block is located.
124 ffs_alloc(ip
, lbn
, bpref
, size
, cred
, bnp
)
125 register struct inode
*ip
;
126 ufs_daddr_t lbn
, bpref
;
131 register struct fs
*fs
;
138 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
139 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
140 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
141 panic("ffs_alloc: bad size");
144 panic("ffs_alloc: missing credential\n");
145 #endif /* DIAGNOSTIC */
146 if (size
== fs
->fs_bsize
&& fs
->fs_cstotal
.cs_nbfree
== 0)
148 if (suser(cred
, NULL
) && freespace(fs
, fs
->fs_minfree
) <= 0)
150 devBlockSize
= vfs_devblocksize(vnode_mount(ITOV(ip
)));
152 if (error
= chkdq(ip
, (int64_t)size
, cred
, 0))
155 if (bpref
>= fs
->fs_size
)
158 cg
= ino_to_cg(fs
, ip
->i_number
);
160 cg
= dtog(fs
, bpref
);
161 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, size
,
162 (u_int32_t (*)())ffs_alloccg
);
164 ip
->i_blocks
+= btodb(size
, devBlockSize
);
165 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
171 * Restore user's disk quota because allocation failed.
173 (void) chkdq(ip
, (int64_t)-size
, cred
, FORCE
);
176 ffs_fserr(fs
, kauth_cred_getuid(cred
), "file system full");
177 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
182 * Reallocate a fragment to a bigger size
184 * The number and size of the old block is given, and a preference
185 * and new size is also specified. The allocator attempts to extend
186 * the original block. Failing that, the regular block allocator is
187 * invoked to get an appropriate block.
189 ffs_realloccg(ip
, lbprev
, bpref
, osize
, nsize
, cred
, bpp
)
190 register struct inode
*ip
;
197 register struct fs
*fs
;
199 int cg
, request
, error
;
200 ufs_daddr_t bprev
, bno
;
206 if ((u_int
)osize
> fs
->fs_bsize
|| fragoff(fs
, osize
) != 0 ||
207 (u_int
)nsize
> fs
->fs_bsize
|| fragoff(fs
, nsize
) != 0) {
209 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
210 ip
->i_dev
, fs
->fs_bsize
, osize
, nsize
, fs
->fs_fsmnt
);
211 panic("ffs_realloccg: bad size");
214 panic("ffs_realloccg: missing credential\n");
215 #endif /* DIAGNOSTIC */
216 if (suser(cred
, NULL
) != 0 && freespace(fs
, fs
->fs_minfree
) <= 0)
218 if ((bprev
= ip
->i_db
[lbprev
]) == 0) {
219 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
220 ip
->i_dev
, fs
->fs_bsize
, bprev
, fs
->fs_fsmnt
);
221 panic("ffs_realloccg: bad bprev");
224 * Allocate the extra space in the buffer.
226 if (error
= (int)buf_bread(ITOV(ip
), (daddr64_t
)((unsigned)lbprev
), osize
, NOCRED
, &bp
)) {
230 devBlockSize
= vfs_devblocksize(vnode_mount(ITOV(ip
)));
233 if (error
= chkdq(ip
, (int64_t)(nsize
- osize
), cred
, 0))
240 * Check for extension in the existing location.
242 cg
= dtog(fs
, bprev
);
243 if (bno
= ffs_fragextend(ip
, cg
, (long)bprev
, osize
, nsize
)) {
244 if ((ufs_daddr_t
)buf_blkno(bp
) != fsbtodb(fs
, bno
))
245 panic("bad blockno");
246 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
247 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
249 buf_setflags(bp
, B_DONE
);
250 bzero((char *)buf_dataptr(bp
) + osize
, (u_int
)buf_size(bp
) - osize
);
255 * Allocate a new disk location.
257 if (bpref
>= fs
->fs_size
)
259 switch ((int)fs
->fs_optim
) {
262 * Allocate an exact sized fragment. Although this makes
263 * best use of space, we will waste time relocating it if
264 * the file continues to grow. If the fragmentation is
265 * less than half of the minimum free reserve, we choose
266 * to begin optimizing for time.
269 if (fs
->fs_minfree
< 5 ||
270 fs
->fs_cstotal
.cs_nffree
>
271 fs
->fs_dsize
* fs
->fs_minfree
/ (2 * 100))
273 log(LOG_NOTICE
, "%s: optimization changed from SPACE to TIME\n",
275 fs
->fs_optim
= FS_OPTTIME
;
279 * At this point we have discovered a file that is trying to
280 * grow a small fragment to a larger fragment. To save time,
281 * we allocate a full sized block, then free the unused portion.
282 * If the file continues to grow, the `ffs_fragextend' call
283 * above will be able to grow it in place without further
284 * copying. If aberrant programs cause disk fragmentation to
285 * grow within 2% of the free reserve, we choose to begin
286 * optimizing for space.
288 request
= fs
->fs_bsize
;
289 if (fs
->fs_cstotal
.cs_nffree
<
290 fs
->fs_dsize
* (fs
->fs_minfree
- 2) / 100)
292 log(LOG_NOTICE
, "%s: optimization changed from TIME to SPACE\n",
294 fs
->fs_optim
= FS_OPTSPACE
;
297 printf("dev = 0x%x, optim = %d, fs = %s\n",
298 ip
->i_dev
, fs
->fs_optim
, fs
->fs_fsmnt
);
299 panic("ffs_realloccg: bad optim");
302 bno
= (ufs_daddr_t
)ffs_hashalloc(ip
, cg
, (long)bpref
, request
,
303 (u_int32_t (*)())ffs_alloccg
);
305 buf_setblkno(bp
, (daddr64_t
)((unsigned)fsbtodb(fs
, bno
)));
306 ffs_blkfree(ip
, bprev
, (long)osize
);
308 ffs_blkfree(ip
, bno
+ numfrags(fs
, nsize
),
309 (long)(request
- nsize
));
310 ip
->i_blocks
+= btodb(nsize
- osize
, devBlockSize
);
311 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
313 buf_setflags(bp
, B_DONE
);
314 bzero((char *)buf_dataptr(bp
) + osize
, (u_int
)buf_size(bp
) - osize
);
320 * Restore user's disk quota because allocation failed.
322 (void) chkdq(ip
, (int64_t)-(nsize
- osize
), cred
, FORCE
);
329 ffs_fserr(fs
, kauth_cred_getuid(cred
), "file system full");
330 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
335 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
337 * The vnode and an array of buffer pointers for a range of sequential
338 * logical blocks to be made contiguous is given. The allocator attempts
339 * to find a range of sequential blocks starting as close as possible to
340 * an fs_rotdelay offset from the end of the allocation for the logical
341 * block immediately preceeding the current range. If successful, the
342 * physical block numbers in the buffer pointers and in the inode are
343 * changed to reflect the new allocation. If unsuccessful, the allocation
344 * is left unchanged. The success in doing the reallocation is returned.
345 * Note that the error return is not reflected back to the user. Rather
346 * the previous block allocation will be used.
348 /* Note: This routine is unused in UBC cluster I/O */
351 int doreallocblks
= 1;
355 * Allocate an inode in the file system.
357 * If allocating a directory, use ffs_dirpref to select the inode.
358 * If allocating in a directory, the following hierarchy is followed:
359 * 1) allocate the preferred inode.
360 * 2) allocate an inode in the same cylinder group.
361 * 3) quadradically rehash into other cylinder groups, until an
362 * available inode is located.
363 * If no inode preference is given the following heirarchy is used
364 * to allocate an inode:
365 * 1) allocate an inode in cylinder group 0.
366 * 2) quadradically rehash into other cylinder groups, until an
367 * available inode is located.
377 register struct inode
*pip
;
378 register struct fs
*fs
;
379 register struct inode
*ip
;
387 if (fs
->fs_cstotal
.cs_nifree
== 0)
390 if ((mode
& IFMT
) == IFDIR
)
391 ipref
= ffs_dirpref(pip
);
393 ipref
= pip
->i_number
;
394 if (ipref
>= fs
->fs_ncg
* fs
->fs_ipg
)
396 cg
= ino_to_cg(fs
, ipref
);
398 * Track the number of dirs created one after another
399 * in a cg without intervening files.
401 if ((mode
& IFMT
) == IFDIR
) {
402 if (fs
->fs_contigdirs
[cg
] < 255)
403 fs
->fs_contigdirs
[cg
]++;
405 if (fs
->fs_contigdirs
[cg
] > 0)
406 fs
->fs_contigdirs
[cg
]--;
408 ino
= (ino_t
)ffs_hashalloc(pip
, cg
, (long)ipref
, mode
, ffs_nodealloccg
);
412 error
= ffs_vget_internal(pvp
->v_mount
, ino
, vpp
, NULL
, NULL
, mode
, 0);
414 ffs_vfree(pvp
, ino
, mode
);
420 printf("mode = 0%o, inum = %d, fs = %s\n",
421 ip
->i_mode
, ip
->i_number
, fs
->fs_fsmnt
);
422 panic("ffs_valloc: dup alloc");
424 if (ip
->i_blocks
) { /* XXX */
425 printf("free inode %s/%d had %d blocks\n",
426 fs
->fs_fsmnt
, ino
, ip
->i_blocks
);
431 * Set up a new generation number for this inode.
434 if (++nextgennumber
< (u_long
)tv
.tv_sec
)
435 nextgennumber
= tv
.tv_sec
;
436 ip
->i_gen
= nextgennumber
;
439 ffs_fserr(fs
, kauth_cred_getuid(cred
), "out of inodes");
440 uprintf("\n%s: create/symlink failed, no inodes free\n", fs
->fs_fsmnt
);
445 * Find a cylinder group to place a directory.
447 * The policy implemented by this algorithm is to allocate a
448 * directory inode in the same cylinder group as its parent
449 * directory, but also to reserve space for its files inodes
450 * and data. Restrict the number of directories which may be
451 * allocated one after another in the same cylinder group
452 * without intervening allocation of files.
458 register struct fs
*fs
;
459 int cg
, prefcg
, dirsize
, cgsize
;
460 int avgifree
, avgbfree
, avgndir
, curdirsize
;
461 int minifree
, minbfree
, maxndir
;
466 avgifree
= fs
->fs_cstotal
.cs_nifree
/ fs
->fs_ncg
;
467 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
468 avgndir
= fs
->fs_cstotal
.cs_ndir
/ fs
->fs_ncg
;
471 * Force allocation in another cg if creating a first level dir.
473 if (ITOV(pip
)->v_flag
& VROOT
) {
475 prefcg
= random() % fs
->fs_ncg
;
477 prefcg
= arc4random() % fs
->fs_ncg
;
480 minndir
= fs
->fs_ipg
;
481 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
482 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
483 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
484 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
486 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
488 for (cg
= 0; cg
< prefcg
; cg
++)
489 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
490 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
491 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
493 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
495 return ((ino_t
)(fs
->fs_ipg
* mincg
));
499 * Count various limits which used for
500 * optimal allocation of a directory inode.
502 maxndir
= min(avgndir
+ fs
->fs_ipg
/ 16, fs
->fs_ipg
);
503 minifree
= avgifree
- fs
->fs_ipg
/ 4;
506 minbfree
= avgbfree
- fs
->fs_fpg
/ fs
->fs_frag
/ 4;
509 cgsize
= fs
->fs_fsize
* fs
->fs_fpg
;
510 dirsize
= fs
->fs_avgfilesize
* fs
->fs_avgfpdir
;
511 curdirsize
= avgndir
? (cgsize
- avgbfree
* fs
->fs_bsize
) / avgndir
: 0;
512 if (dirsize
< curdirsize
)
513 dirsize
= curdirsize
;
514 maxcontigdirs
= min(cgsize
/ dirsize
, 255);
515 if (fs
->fs_avgfpdir
> 0)
516 maxcontigdirs
= min(maxcontigdirs
,
517 fs
->fs_ipg
/ fs
->fs_avgfpdir
);
518 if (maxcontigdirs
== 0)
522 * Limit number of dirs in one cg and reserve space for
523 * regular files, but only if we have no deficit in
526 prefcg
= ino_to_cg(fs
, pip
->i_number
);
527 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
528 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
529 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
530 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
531 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
532 return ((ino_t
)(fs
->fs_ipg
* cg
));
534 for (cg
= 0; cg
< prefcg
; cg
++)
535 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
536 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
537 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
538 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
539 return ((ino_t
)(fs
->fs_ipg
* cg
));
542 * This is a backstop when we have deficit in space.
544 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
545 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
546 return ((ino_t
)(fs
->fs_ipg
* cg
));
547 for (cg
= 0; cg
< prefcg
; cg
++)
548 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
550 return ((ino_t
)(fs
->fs_ipg
* cg
));
554 * Select the desired position for the next block in a file. The file is
555 * logically divided into sections. The first section is composed of the
556 * direct blocks. Each additional section contains fs_maxbpg blocks.
558 * If no blocks have been allocated in the first section, the policy is to
559 * request a block in the same cylinder group as the inode that describes
560 * the file. If no blocks have been allocated in any other section, the
561 * policy is to place the section in a cylinder group with a greater than
562 * average number of free blocks. An appropriate cylinder group is found
563 * by using a rotor that sweeps the cylinder groups. When a new group of
564 * blocks is needed, the sweep begins in the cylinder group following the
565 * cylinder group from which the previous allocation was made. The sweep
566 * continues until a cylinder group with greater than the average number
567 * of free blocks is found. If the allocation is for the first block in an
568 * indirect block, the information on the previous allocation is unavailable;
569 * here a best guess is made based upon the logical block number being
572 * If a section is already partially allocated, the policy is to
573 * contiguously allocate fs_maxcontig blocks. The end of one of these
574 * contiguous blocks and the beginning of the next is physically separated
575 * so that the disk head will be in transit between them for at least
576 * fs_rotdelay milliseconds. This is to allow time for the processor to
577 * schedule another I/O transfer.
580 ffs_blkpref(ip
, lbn
, indx
, bap
)
586 register struct fs
*fs
;
588 int avgbfree
, startcg
;
592 struct vnode
*vp
=ITOV(ip
);
593 struct mount
*mp
=vp
->v_mount
;
594 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
595 #endif /* REV_ENDIAN_FS */
601 if (bap
!= &ip
->i_db
[0])
602 prev
= NXSwapLong(bap
[indx
- 1]);
604 prev
= bap
[indx
- 1];
605 } else prev
= bap
[indx
- 1];
607 if (indx
% fs
->fs_maxbpg
== 0 || prev
== 0)
608 #else /* REV_ENDIAN_FS */
609 if (indx
% fs
->fs_maxbpg
== 0 || bap
[indx
- 1] == 0)
610 #endif /* REV_ENDIAN_FS */
613 cg
= ino_to_cg(fs
, ip
->i_number
);
614 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
617 * Find a cylinder with greater than average number of
618 * unused data blocks.
621 if (indx
== 0 || prev
== 0)
622 #else /* REV_ENDIAN_FS */
623 if (indx
== 0 || bap
[indx
- 1] == 0)
624 #endif /* REV_ENDIAN_FS */
626 ino_to_cg(fs
, ip
->i_number
) + lbn
/ fs
->fs_maxbpg
;
629 startcg
= dtog(fs
, prev
) + 1;
630 #else /* REV_ENDIAN_FS */
631 startcg
= dtog(fs
, bap
[indx
- 1]) + 1;
632 #endif /* REV_ENDIAN_FS */
633 startcg
%= fs
->fs_ncg
;
634 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
635 for (cg
= startcg
; cg
< fs
->fs_ncg
; cg
++)
636 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
638 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
640 for (cg
= 0; cg
<= startcg
; cg
++)
641 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
643 return (fs
->fs_fpg
* cg
+ fs
->fs_frag
);
648 * One or more previous blocks have been laid out. If less
649 * than fs_maxcontig previous blocks are contiguous, the
650 * next block is requested contiguously, otherwise it is
651 * requested rotationally delayed by fs_rotdelay milliseconds.
655 nextblk
= prev
+ fs
->fs_frag
;
656 if (indx
< fs
->fs_maxcontig
) {
659 if (bap
!= &ip
->i_db
[0])
660 prev
= NXSwapLong(bap
[indx
- fs
->fs_maxcontig
]);
662 prev
= bap
[indx
- fs
->fs_maxcontig
];
663 if (prev
+ blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
666 #endif /* REV_ENDIAN_FS */
667 nextblk
= bap
[indx
- 1] + fs
->fs_frag
;
668 if (indx
< fs
->fs_maxcontig
|| bap
[indx
- fs
->fs_maxcontig
] +
669 blkstofrags(fs
, fs
->fs_maxcontig
) != nextblk
)
673 #endif /* REV_ENDIAN_FS */
674 if (fs
->fs_rotdelay
!= 0)
676 * Here we convert ms of delay to frags as:
677 * (frags) = (ms) * (rev/sec) * (sect/rev) /
678 * ((sect/frag) * (ms/sec))
679 * then round up to the next block.
681 nextblk
+= roundup(fs
->fs_rotdelay
* fs
->fs_rps
* fs
->fs_nsect
/
682 (NSPF(fs
) * 1000), fs
->fs_frag
);
687 * Implement the cylinder overflow algorithm.
689 * The policy implemented by this algorithm is:
690 * 1) allocate the block in its requested cylinder group.
691 * 2) quadradically rehash on the cylinder group number.
692 * 3) brute force search for a free block.
696 ffs_hashalloc(ip
, cg
, pref
, size
, allocator
)
700 int size
; /* size for data blocks, mode for inodes */
701 u_int32_t (*allocator
)();
703 register struct fs
*fs
;
709 * 1: preferred cylinder group
711 result
= (*allocator
)(ip
, cg
, pref
, size
);
715 * 2: quadratic rehash
717 for (i
= 1; i
< fs
->fs_ncg
; i
*= 2) {
719 if (cg
>= fs
->fs_ncg
)
721 result
= (*allocator
)(ip
, cg
, 0, size
);
726 * 3: brute force search
727 * Note that we start at i == 2, since 0 was checked initially,
728 * and 1 is always checked in the quadratic rehash.
730 cg
= (icg
+ 2) % fs
->fs_ncg
;
731 for (i
= 2; i
< fs
->fs_ncg
; i
++) {
732 result
= (*allocator
)(ip
, cg
, 0, size
);
736 if (cg
== fs
->fs_ncg
)
743 * Determine whether a fragment can be extended.
745 * Check to see if the necessary fragments are available, and
746 * if they are, allocate them.
749 ffs_fragextend(ip
, cg
, bprev
, osize
, nsize
)
755 register struct fs
*fs
;
756 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
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
779 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
784 cgp
= (struct cg
*)buf_dataptr(bp
);
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 */
800 cgp
->cg_time
= tv
.tv_sec
;
801 bno
= dtogd(fs
, bprev
);
802 for (i
= numfrags(fs
, osize
); i
< frags
; i
++)
803 if (isclr(cg_blksfree(cgp
), bno
+ i
)) {
806 byte_swap_cgout(cgp
,fs
);
807 #endif /* REV_ENDIAN_FS */
812 * the current fragment can be extended
813 * deduct the count on fragment being extended into
814 * increase the count on the remaining fragment (if any)
815 * allocate the extended piece
817 for (i
= frags
; i
< fs
->fs_frag
- bbase
; i
++)
818 if (isclr(cg_blksfree(cgp
), bno
+ i
))
820 cgp
->cg_frsum
[i
- numfrags(fs
, osize
)]--;
822 cgp
->cg_frsum
[i
- frags
]++;
823 for (i
= numfrags(fs
, osize
); i
< frags
; i
++) {
824 clrbit(cg_blksfree(cgp
), bno
+ i
);
825 cgp
->cg_cs
.cs_nffree
--;
826 fs
->fs_cstotal
.cs_nffree
--;
827 fs
->fs_cs(fs
, cg
).cs_nffree
--;
832 byte_swap_cgout(cgp
,fs
);
833 #endif /* REV_ENDIAN_FS */
839 * Determine whether a block can be allocated.
841 * Check to see if a block of the appropriate size is available,
842 * and if it is, allocate it.
845 ffs_alloccg(ip
, cg
, bpref
, size
)
851 register struct fs
*fs
;
852 register struct cg
*cgp
;
856 int error
, bno
, frags
, allocsiz
;
858 struct vnode
*vp
=ITOV(ip
);
859 struct mount
*mp
=vp
->v_mount
;
860 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
861 #endif /* REV_ENDIAN_FS */
864 if (fs
->fs_cs(fs
, cg
).cs_nbfree
== 0 && size
== fs
->fs_bsize
)
866 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
867 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
872 cgp
= (struct cg
*)buf_dataptr(bp
);
875 byte_swap_cgin(cgp
,fs
);
876 #endif /* REV_ENDIAN_FS */
877 if (!cg_chkmagic(cgp
) ||
878 (cgp
->cg_cs
.cs_nbfree
== 0 && size
== fs
->fs_bsize
)) {
881 byte_swap_cgout(cgp
,fs
);
882 #endif /* REV_ENDIAN_FS */
887 cgp
->cg_time
= tv
.tv_sec
;
888 if (size
== fs
->fs_bsize
) {
889 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
892 byte_swap_cgout(cgp
,fs
);
893 #endif /* REV_ENDIAN_FS */
898 * check to see if any fragments are already available
899 * allocsiz is the size which will be allocated, hacking
900 * it down to a smaller size if necessary
902 frags
= numfrags(fs
, size
);
903 for (allocsiz
= frags
; allocsiz
< fs
->fs_frag
; allocsiz
++)
904 if (cgp
->cg_frsum
[allocsiz
] != 0)
906 if (allocsiz
== fs
->fs_frag
) {
908 * no fragments were available, so a block will be
909 * allocated, and hacked up
911 if (cgp
->cg_cs
.cs_nbfree
== 0) {
914 byte_swap_cgout(cgp
,fs
);
915 #endif /* REV_ENDIAN_FS */
919 bno
= ffs_alloccgblk(fs
, cgp
, bpref
);
920 bpref
= dtogd(fs
, bno
);
921 for (i
= frags
; i
< fs
->fs_frag
; i
++)
922 setbit(cg_blksfree(cgp
), bpref
+ i
);
923 i
= fs
->fs_frag
- frags
;
924 cgp
->cg_cs
.cs_nffree
+= i
;
925 fs
->fs_cstotal
.cs_nffree
+= i
;
926 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
931 byte_swap_cgout(cgp
,fs
);
932 #endif /* REV_ENDIAN_FS */
936 bno
= ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
);
940 byte_swap_cgout(cgp
,fs
);
941 #endif /* REV_ENDIAN_FS */
945 for (i
= 0; i
< frags
; i
++)
946 clrbit(cg_blksfree(cgp
), bno
+ i
);
947 cgp
->cg_cs
.cs_nffree
-= frags
;
948 fs
->fs_cstotal
.cs_nffree
-= frags
;
949 fs
->fs_cs(fs
, cg
).cs_nffree
-= frags
;
951 cgp
->cg_frsum
[allocsiz
]--;
952 if (frags
!= allocsiz
)
953 cgp
->cg_frsum
[allocsiz
- frags
]++;
956 byte_swap_cgout(cgp
,fs
);
957 #endif /* REV_ENDIAN_FS */
959 return (cg
* fs
->fs_fpg
+ bno
);
963 * Allocate a block in a cylinder group.
965 * This algorithm implements the following policy:
966 * 1) allocate the requested block.
967 * 2) allocate a rotationally optimal block in the same cylinder.
968 * 3) allocate the next available block on the block rotor for the
969 * specified cylinder group.
970 * Note that this routine only allocates fs_bsize blocks; these
971 * blocks may be fragmented by the routine that allocates them.
974 ffs_alloccgblk(fs
, cgp
, bpref
)
975 register struct fs
*fs
;
976 register struct cg
*cgp
;
979 ufs_daddr_t bno
, blkno
;
980 int cylno
, pos
, delta
;
984 if (bpref
== 0 || dtog(fs
, bpref
) != cgp
->cg_cgx
) {
985 bpref
= cgp
->cg_rotor
;
988 bpref
= blknum(fs
, bpref
);
989 bpref
= dtogd(fs
, bpref
);
991 * if the requested block is available, use it
993 if (ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bpref
))) {
997 if (fs
->fs_nrpos
<= 1 || fs
->fs_cpc
== 0) {
999 * Block layout information is not available.
1000 * Leaving bpref unchanged means we take the
1001 * next available free block following the one
1002 * we just allocated. Hopefully this will at
1003 * least hit a track cache on drives of unknown
1004 * geometry (e.g. SCSI).
1009 * check for a block available on the same cylinder
1011 cylno
= cbtocylno(fs
, bpref
);
1012 if (cg_blktot(cgp
)[cylno
] == 0)
1015 * check the summary information to see if a block is
1016 * available in the requested cylinder starting at the
1017 * requested rotational position and proceeding around.
1019 cylbp
= cg_blks(fs
, cgp
, cylno
);
1020 pos
= cbtorpos(fs
, bpref
);
1021 for (i
= pos
; i
< fs
->fs_nrpos
; i
++)
1024 if (i
== fs
->fs_nrpos
)
1025 for (i
= 0; i
< pos
; i
++)
1030 * found a rotational position, now find the actual
1031 * block. A panic if none is actually there.
1033 pos
= cylno
% fs
->fs_cpc
;
1034 bno
= (cylno
- pos
) * fs
->fs_spc
/ NSPB(fs
);
1035 if (fs_postbl(fs
, pos
)[i
] == -1) {
1036 printf("pos = %d, i = %d, fs = %s\n",
1037 pos
, i
, fs
->fs_fsmnt
);
1038 panic("ffs_alloccgblk: cyl groups corrupted");
1040 for (i
= fs_postbl(fs
, pos
)[i
];; ) {
1041 if (ffs_isblock(fs
, cg_blksfree(cgp
), bno
+ i
)) {
1042 bno
= blkstofrags(fs
, (bno
+ i
));
1045 delta
= fs_rotbl(fs
)[i
];
1047 delta
+ i
> fragstoblks(fs
, fs
->fs_fpg
))
1051 printf("pos = %d, i = %d, fs = %s\n", pos
, i
, fs
->fs_fsmnt
);
1052 panic("ffs_alloccgblk: can't find blk in cyl");
1056 * no blocks in the requested cylinder, so take next
1057 * available one in this cylinder group.
1059 bno
= ffs_mapsearch(fs
, cgp
, bpref
, (int)fs
->fs_frag
);
1062 cgp
->cg_rotor
= bno
;
1064 blkno
= fragstoblks(fs
, bno
);
1065 ffs_clrblock(fs
, cg_blksfree(cgp
), (long)blkno
);
1066 ffs_clusteracct(fs
, cgp
, blkno
, -1);
1067 cgp
->cg_cs
.cs_nbfree
--;
1068 fs
->fs_cstotal
.cs_nbfree
--;
1069 fs
->fs_cs(fs
, cgp
->cg_cgx
).cs_nbfree
--;
1070 cylno
= cbtocylno(fs
, bno
);
1071 cg_blks(fs
, cgp
, cylno
)[cbtorpos(fs
, bno
)]--;
1072 cg_blktot(cgp
)[cylno
]--;
1074 return (cgp
->cg_cgx
* fs
->fs_fpg
+ bno
);
1078 * Determine whether a cluster can be allocated.
1080 * We do not currently check for optimal rotational layout if there
1081 * are multiple choices in the same cylinder group. Instead we just
1082 * take the first one that we find following bpref.
1085 ffs_clusteralloc(ip
, cg
, bpref
, len
)
1091 register struct fs
*fs
;
1092 register struct cg
*cgp
;
1094 int i
, got
, run
, bno
, bit
, map
;
1098 struct vnode
*vp
=ITOV(ip
);
1099 struct mount
*mp
=vp
->v_mount
;
1100 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1101 #endif /* REV_ENDIAN_FS */
1104 if (fs
->fs_maxcluster
[cg
] < len
)
1106 if (buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))), (int)fs
->fs_cgsize
,
1109 cgp
= (struct cg
*)buf_dataptr(bp
);
1112 byte_swap_cgin(cgp
,fs
);
1113 #endif /* REV_ENDIAN_FS */
1114 if (!cg_chkmagic(cgp
)) {
1117 byte_swap_cgout(cgp
,fs
);
1118 #endif /* REV_ENDIAN_FS */
1122 * Check to see if a cluster of the needed size (or bigger) is
1123 * available in this cylinder group.
1125 lp
= &cg_clustersum(cgp
)[len
];
1126 for (i
= len
; i
<= fs
->fs_contigsumsize
; i
++)
1129 if (i
> fs
->fs_contigsumsize
) {
1131 * This is the first time looking for a cluster in this
1132 * cylinder group. Update the cluster summary information
1133 * to reflect the true maximum sized cluster so that
1134 * future cluster allocation requests can avoid reading
1135 * the cylinder group map only to find no clusters.
1137 lp
= &cg_clustersum(cgp
)[len
- 1];
1138 for (i
= len
- 1; i
> 0; i
--)
1141 fs
->fs_maxcluster
[cg
] = i
;
1144 byte_swap_cgout(cgp
,fs
);
1145 #endif /* REV_ENDIAN_FS */
1149 * Search the cluster map to find a big enough cluster.
1150 * We take the first one that we find, even if it is larger
1151 * than we need as we prefer to get one close to the previous
1152 * block allocation. We do not search before the current
1153 * preference point as we do not want to allocate a block
1154 * that is allocated before the previous one (as we will
1155 * then have to wait for another pass of the elevator
1156 * algorithm before it will be read). We prefer to fail and
1157 * be recalled to try an allocation in the next cylinder group.
1159 if (dtog(fs
, bpref
) != cg
)
1162 bpref
= fragstoblks(fs
, dtogd(fs
, blknum(fs
, bpref
)));
1163 mapp
= &cg_clustersfree(cgp
)[bpref
/ NBBY
];
1165 bit
= 1 << (bpref
% NBBY
);
1166 for (run
= 0, got
= bpref
; got
< cgp
->cg_nclusterblks
; got
++) {
1167 if ((map
& bit
) == 0) {
1174 if ((got
& (NBBY
- 1)) != (NBBY
- 1)) {
1181 if (got
== cgp
->cg_nclusterblks
) {
1184 byte_swap_cgout(cgp
,fs
);
1185 #endif /* REV_ENDIAN_FS */
1189 * Allocate the cluster that we have found.
1191 for (i
= 1; i
<= len
; i
++)
1192 if (!ffs_isblock(fs
, cg_blksfree(cgp
), got
- run
+ i
))
1193 panic("ffs_clusteralloc: map mismatch");
1194 bno
= cg
* fs
->fs_fpg
+ blkstofrags(fs
, got
- run
+ 1);
1195 if (dtog(fs
, bno
) != cg
)
1196 panic("ffs_clusteralloc: allocated out of group");
1197 len
= blkstofrags(fs
, len
);
1198 for (i
= 0; i
< len
; i
+= fs
->fs_frag
)
1199 if ((got
= ffs_alloccgblk(fs
, cgp
, bno
+ i
)) != bno
+ i
)
1200 panic("ffs_clusteralloc: lost block");
1203 byte_swap_cgout(cgp
,fs
);
1204 #endif /* REV_ENDIAN_FS */
1214 * Determine whether an inode can be allocated.
1216 * Check to see if an inode is available, and if it is,
1217 * allocate it using the following policy:
1218 * 1) allocate the requested inode.
1219 * 2) allocate the next available inode after the requested
1220 * inode in the specified cylinder group.
1223 ffs_nodealloccg(ip
, cg
, ipref
, mode
)
1229 register struct fs
*fs
;
1230 register struct cg
*cgp
;
1233 int error
, start
, len
, loc
, map
, i
;
1235 struct vnode
*vp
=ITOV(ip
);
1236 struct mount
*mp
=vp
->v_mount
;
1237 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1238 #endif /* REV_ENDIAN_FS */
1241 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1243 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1244 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1249 cgp
= (struct cg
*)buf_dataptr(bp
);
1252 byte_swap_cgin(cgp
,fs
);
1253 #endif /* REV_ENDIAN_FS */
1254 if (!cg_chkmagic(cgp
) || cgp
->cg_cs
.cs_nifree
== 0) {
1257 byte_swap_cgout(cgp
,fs
);
1258 #endif /* REV_ENDIAN_FS */
1264 cgp
->cg_time
= tv
.tv_sec
;
1266 ipref
%= fs
->fs_ipg
;
1267 if (isclr(cg_inosused(cgp
), ipref
))
1270 start
= cgp
->cg_irotor
/ NBBY
;
1271 len
= howmany(fs
->fs_ipg
- cgp
->cg_irotor
, NBBY
);
1272 loc
= skpc(0xff, len
, &cg_inosused(cgp
)[start
]);
1276 loc
= skpc(0xff, len
, &cg_inosused(cgp
)[0]);
1278 printf("cg = %d, irotor = %d, fs = %s\n",
1279 cg
, cgp
->cg_irotor
, fs
->fs_fsmnt
);
1280 panic("ffs_nodealloccg: map corrupted");
1284 i
= start
+ len
- loc
;
1285 map
= cg_inosused(cgp
)[i
];
1287 for (i
= 1; i
< (1 << NBBY
); i
<<= 1, ipref
++) {
1288 if ((map
& i
) == 0) {
1289 cgp
->cg_irotor
= ipref
;
1293 printf("fs = %s\n", fs
->fs_fsmnt
);
1294 panic("ffs_nodealloccg: block not in map");
1297 setbit(cg_inosused(cgp
), ipref
);
1298 cgp
->cg_cs
.cs_nifree
--;
1299 fs
->fs_cstotal
.cs_nifree
--;
1300 fs
->fs_cs(fs
, cg
).cs_nifree
--;
1302 if ((mode
& IFMT
) == IFDIR
) {
1303 cgp
->cg_cs
.cs_ndir
++;
1304 fs
->fs_cstotal
.cs_ndir
++;
1305 fs
->fs_cs(fs
, cg
).cs_ndir
++;
1309 byte_swap_cgout(cgp
,fs
);
1310 #endif /* REV_ENDIAN_FS */
1312 return (cg
* fs
->fs_ipg
+ ipref
);
1316 * Free a block or fragment.
1318 * The specified block or fragment is placed back in the
1319 * free map. If a fragment is deallocated, a possible
1320 * block reassembly is checked.
1323 ffs_blkfree(ip
, bno
, size
)
1324 register struct inode
*ip
;
1328 register struct fs
*fs
;
1329 register struct cg
*cgp
;
1333 int i
, error
, cg
, blk
, frags
, bbase
;
1335 struct vnode
*vp
=ITOV(ip
);
1336 struct mount
*mp
=vp
->v_mount
;
1337 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1338 #endif /* REV_ENDIAN_FS */
1341 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1342 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1343 ip
->i_dev
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1344 panic("blkfree: bad size");
1347 if ((u_int
)bno
>= fs
->fs_size
) {
1348 printf("bad block %d, ino %d\n", bno
, ip
->i_number
);
1349 ffs_fserr(fs
, ip
->i_uid
, "bad block");
1352 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1353 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1358 cgp
= (struct cg
*)buf_dataptr(bp
);
1361 byte_swap_cgin(cgp
,fs
);
1362 #endif /* REV_ENDIAN_FS */
1363 if (!cg_chkmagic(cgp
)) {
1366 byte_swap_cgout(cgp
,fs
);
1367 #endif /* REV_ENDIAN_FS */
1372 cgp
->cg_time
= tv
.tv_sec
;
1373 bno
= dtogd(fs
, bno
);
1374 if (size
== fs
->fs_bsize
) {
1375 blkno
= fragstoblks(fs
, bno
);
1376 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1377 printf("dev = 0x%x, block = %d, fs = %s\n",
1378 ip
->i_dev
, bno
, fs
->fs_fsmnt
);
1379 panic("blkfree: freeing free block");
1381 ffs_setblock(fs
, cg_blksfree(cgp
), blkno
);
1382 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1383 cgp
->cg_cs
.cs_nbfree
++;
1384 fs
->fs_cstotal
.cs_nbfree
++;
1385 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1386 i
= cbtocylno(fs
, bno
);
1387 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bno
)]++;
1388 cg_blktot(cgp
)[i
]++;
1390 bbase
= bno
- fragnum(fs
, bno
);
1392 * decrement the counts associated with the old frags
1394 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1395 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, -1);
1397 * deallocate the fragment
1399 frags
= numfrags(fs
, size
);
1400 for (i
= 0; i
< frags
; i
++) {
1401 if (isset(cg_blksfree(cgp
), bno
+ i
)) {
1402 printf("dev = 0x%x, block = %d, fs = %s\n",
1403 ip
->i_dev
, bno
+ i
, fs
->fs_fsmnt
);
1404 panic("blkfree: freeing free frag");
1406 setbit(cg_blksfree(cgp
), bno
+ i
);
1408 cgp
->cg_cs
.cs_nffree
+= i
;
1409 fs
->fs_cstotal
.cs_nffree
+= i
;
1410 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
1412 * add back in counts associated with the new frags
1414 blk
= blkmap(fs
, cg_blksfree(cgp
), bbase
);
1415 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, 1);
1417 * if a complete block has been reassembled, account for it
1419 blkno
= fragstoblks(fs
, bbase
);
1420 if (ffs_isblock(fs
, cg_blksfree(cgp
), blkno
)) {
1421 cgp
->cg_cs
.cs_nffree
-= fs
->fs_frag
;
1422 fs
->fs_cstotal
.cs_nffree
-= fs
->fs_frag
;
1423 fs
->fs_cs(fs
, cg
).cs_nffree
-= fs
->fs_frag
;
1424 ffs_clusteracct(fs
, cgp
, blkno
, 1);
1425 cgp
->cg_cs
.cs_nbfree
++;
1426 fs
->fs_cstotal
.cs_nbfree
++;
1427 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1428 i
= cbtocylno(fs
, bbase
);
1429 cg_blks(fs
, cgp
, i
)[cbtorpos(fs
, bbase
)]++;
1430 cg_blktot(cgp
)[i
]++;
1436 byte_swap_cgout(cgp
,fs
);
1437 #endif /* REV_ENDIAN_FS */
1443 * Verify allocation of a block or fragment. Returns true if block or
1444 * fragment is allocated, false if it is free.
1446 ffs_checkblk(ip
, bno
, size
)
1454 int i
, error
, frags
, free
;
1456 struct vnode
*vp
=ITOV(ip
);
1457 struct mount
*mp
=vp
->v_mount
;
1458 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1459 #endif /* REV_ENDIAN_FS */
1462 if ((u_int
)size
> fs
->fs_bsize
|| fragoff(fs
, size
) != 0) {
1463 printf("bsize = %d, size = %d, fs = %s\n",
1464 fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
1465 panic("checkblk: bad size");
1467 if ((u_int
)bno
>= fs
->fs_size
)
1468 panic("checkblk: bad block %d", bno
);
1469 error
= (int)buf_bread(ip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, dtog(fs
, bno
)))),
1470 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1475 cgp
= (struct cg
*)buf_dataptr(bp
);
1478 byte_swap_cgin(cgp
,fs
);
1479 #endif /* REV_ENDIAN_FS */
1480 if (!cg_chkmagic(cgp
)) {
1483 byte_swap_cgout(cgp
,fs
);
1484 #endif /* REV_ENDIAN_FS */
1488 bno
= dtogd(fs
, bno
);
1489 if (size
== fs
->fs_bsize
) {
1490 free
= ffs_isblock(fs
, cg_blksfree(cgp
), fragstoblks(fs
, bno
));
1492 frags
= numfrags(fs
, size
);
1493 for (free
= 0, i
= 0; i
< frags
; i
++)
1494 if (isset(cg_blksfree(cgp
), bno
+ i
))
1496 if (free
!= 0 && free
!= frags
)
1497 panic("checkblk: partially free fragment");
1501 byte_swap_cgout(cgp
,fs
);
1502 #endif /* REV_ENDIAN_FS */
1506 #endif /* DIAGNOSTIC */
1511 * The specified inode is placed back in the free map.
1514 ffs_vfree(struct vnode
*vp
, ino_t ino
, int mode
)
1516 register struct fs
*fs
;
1517 register struct cg
*cgp
;
1518 register struct inode
*pip
;
1523 struct mount
*mp
=vp
->v_mount
;
1524 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
1525 #endif /* REV_ENDIAN_FS */
1529 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
1530 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1531 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1532 cg
= ino_to_cg(fs
, ino
);
1533 error
= (int)buf_bread(pip
->i_devvp
, (daddr64_t
)((unsigned)fsbtodb(fs
, cgtod(fs
, cg
))),
1534 (int)fs
->fs_cgsize
, NOCRED
, &bp
);
1539 cgp
= (struct cg
*)buf_dataptr(bp
);
1542 byte_swap_cgin(cgp
,fs
);
1543 #endif /* REV_ENDIAN_FS */
1544 if (!cg_chkmagic(cgp
)) {
1547 byte_swap_cgout(cgp
,fs
);
1548 #endif /* REV_ENDIAN_FS */
1553 cgp
->cg_time
= tv
.tv_sec
;
1555 if (isclr(cg_inosused(cgp
), ino
)) {
1556 printf("dev = 0x%x, ino = %d, fs = %s\n",
1557 pip
->i_dev
, ino
, fs
->fs_fsmnt
);
1558 if (fs
->fs_ronly
== 0)
1559 panic("ifree: freeing free inode");
1561 clrbit(cg_inosused(cgp
), ino
);
1562 if (ino
< cgp
->cg_irotor
)
1563 cgp
->cg_irotor
= ino
;
1564 cgp
->cg_cs
.cs_nifree
++;
1565 fs
->fs_cstotal
.cs_nifree
++;
1566 fs
->fs_cs(fs
, cg
).cs_nifree
++;
1567 if ((mode
& IFMT
) == IFDIR
) {
1568 cgp
->cg_cs
.cs_ndir
--;
1569 fs
->fs_cstotal
.cs_ndir
--;
1570 fs
->fs_cs(fs
, cg
).cs_ndir
--;
1575 byte_swap_cgout(cgp
,fs
);
1576 #endif /* REV_ENDIAN_FS */
1582 * Find a block of the specified size in the specified cylinder group.
1584 * It is a panic if a request is made to find a block if none are
1588 ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
)
1589 register struct fs
*fs
;
1590 register struct cg
*cgp
;
1595 int start
, len
, loc
, i
;
1596 int blk
, field
, subfield
, pos
;
1599 * find the fragment by searching through the free block
1600 * map for an appropriate bit pattern
1603 start
= dtogd(fs
, bpref
) / NBBY
;
1605 start
= cgp
->cg_frotor
/ NBBY
;
1606 len
= howmany(fs
->fs_fpg
, NBBY
) - start
;
1607 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[start
],
1608 (u_char
*)fragtbl
[fs
->fs_frag
],
1609 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1613 loc
= scanc((u_int
)len
, (u_char
*)&cg_blksfree(cgp
)[0],
1614 (u_char
*)fragtbl
[fs
->fs_frag
],
1615 (u_char
)(1 << (allocsiz
- 1 + (fs
->fs_frag
% NBBY
))));
1617 printf("start = %d, len = %d, fs = %s\n",
1618 start
, len
, fs
->fs_fsmnt
);
1619 panic("ffs_alloccg: map corrupted");
1623 bno
= (start
+ len
- loc
) * NBBY
;
1624 cgp
->cg_frotor
= bno
;
1626 * found the byte in the map
1627 * sift through the bits to find the selected frag
1629 for (i
= bno
+ NBBY
; bno
< i
; bno
+= fs
->fs_frag
) {
1630 blk
= blkmap(fs
, cg_blksfree(cgp
), bno
);
1632 field
= around
[allocsiz
];
1633 subfield
= inside
[allocsiz
];
1634 for (pos
= 0; pos
<= fs
->fs_frag
- allocsiz
; pos
++) {
1635 if ((blk
& field
) == subfield
)
1641 printf("bno = %d, fs = %s\n", bno
, fs
->fs_fsmnt
);
1642 panic("ffs_alloccg: block not in map");
1647 * Update the cluster map because of an allocation or free.
1649 * Cnt == 1 means free; cnt == -1 means allocating.
1652 ffs_clusteracct(struct fs
*fs
, struct cg
*cgp
, ufs_daddr_t blkno
, int cnt
)
1656 u_char
*freemapp
, *mapp
;
1657 int i
, start
, end
, forw
, back
, map
, bit
;
1659 if (fs
->fs_contigsumsize
<= 0)
1661 freemapp
= cg_clustersfree(cgp
);
1662 sump
= cg_clustersum(cgp
);
1664 * Allocate or clear the actual block.
1667 setbit(freemapp
, blkno
);
1669 clrbit(freemapp
, blkno
);
1671 * Find the size of the cluster going forward.
1674 end
= start
+ fs
->fs_contigsumsize
;
1675 if (end
>= cgp
->cg_nclusterblks
)
1676 end
= cgp
->cg_nclusterblks
;
1677 mapp
= &freemapp
[start
/ NBBY
];
1679 bit
= 1 << (start
% NBBY
);
1680 for (i
= start
; i
< end
; i
++) {
1681 if ((map
& bit
) == 0)
1683 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
1692 * Find the size of the cluster going backward.
1695 end
= start
- fs
->fs_contigsumsize
;
1698 mapp
= &freemapp
[start
/ NBBY
];
1700 bit
= 1 << (start
% NBBY
);
1701 for (i
= start
; i
> end
; i
--) {
1702 if ((map
& bit
) == 0)
1704 if ((i
& (NBBY
- 1)) != 0) {
1708 bit
= 1 << (NBBY
- 1);
1713 * Account for old cluster and the possibly new forward and
1716 i
= back
+ forw
+ 1;
1717 if (i
> fs
->fs_contigsumsize
)
1718 i
= fs
->fs_contigsumsize
;
1725 * Update cluster summary information.
1727 lp
= &sump
[fs
->fs_contigsumsize
];
1728 for (i
= fs
->fs_contigsumsize
; i
> 0; i
--)
1731 fs
->fs_maxcluster
[cgp
->cg_cgx
] = i
;
1735 * Fserr prints the name of a file system with an error diagnostic.
1737 * The form of the error message is:
1741 ffs_fserr(fs
, uid
, cp
)
1747 log(LOG_ERR
, "uid %d on %s: %s\n", uid
, fs
->fs_fsmnt
, cp
);